using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using IcgSoftware.Threema.CoreMsgApi.Helpers; using IcgSoftware.Threema.CoreMsgApi.Results; namespace IcgSoftware.Threema.CoreMsgApi.Com { [ComVisible(true)] [ProgId("Threema.MsgApi.Com.MessageTool")] [Guid("097F1D26-B38E-4501-845D-6DE7DBFB5EAA")] public class MessageToolWrapper : IMessageToolWrapper { /// /// Wrapper to send simple message /// /// Recipient id /// Sender id /// Sender sercret /// Text message /// Optional api url /// Message id public string SendTextMessageSimple(string to, string from, string secret, string text, string apiUrl = APIConnector.DEFAULTAPIURL) { APIConnector apiConnector = this.CreateConnector(from, secret, apiUrl); return apiConnector.SendTextMessageSimple(to, DataUtils.Utf8Endcode(text)); } /// /// Wrapper to send text message E2E /// /// Recipient id /// Sender id /// Sender sercret /// Sender private key /// Text message /// Optional api url /// Message id public string SendTextMessage(string to, string from, string secret, string privateKey, string text, string apiUrl = APIConnector.DEFAULTAPIURL) { byte[] privateKeyBytes = GetKey(privateKey, Key.KeyType.PRIVATE); E2EHelper e2EHelper = new E2EHelper(this.CreateConnector(from, secret, apiUrl), privateKeyBytes); return e2EHelper.SendTextMessage(to, DataUtils.Utf8Endcode(text)); } /// /// Wrapper to send image message E2E /// /// Recipient id /// Sender id /// Sender sercret /// Sender private key /// File path to image /// Optional api url /// Message id public string SendImageMessage(string to, string from, string secret, string privateKey, string imageFilePath, string apiUrl = APIConnector.DEFAULTAPIURL) { byte[] privateKeyBytes = GetKey(privateKey, Key.KeyType.PRIVATE); E2EHelper e2EHelper = new E2EHelper(this.CreateConnector(from, secret, apiUrl), privateKeyBytes); return e2EHelper.SendImageMessage(to, imageFilePath); } /// /// Wrapper to send file message E2E /// /// Recipient id /// Sender id /// Sender sercret /// Sender private key /// File path to file /// File path to thumbnail /// Optional api url /// Message id public string SendFileMessage(string to, string from, string secret, string privateKey, string file, string thumbnail = null, string apiUrl = APIConnector.DEFAULTAPIURL) { byte[] privateKeyBytes = GetKey(privateKey, Key.KeyType.PRIVATE); FileInfo fileInfo = file != null ? new FileInfo(file) : null; FileInfo thumbnailInfo = thumbnail != null ? new FileInfo(thumbnail) : null; E2EHelper e2EHelper = new E2EHelper(this.CreateConnector(from, secret, apiUrl), privateKeyBytes); return e2EHelper.SendFileMessage(to, fileInfo, thumbnailInfo); } /// /// Wrapper to id lookup via email /// /// Email for lookup /// Sender id /// Sender secret /// Optional api url /// id public string LookupEmail(string email, string from, string secret, string apiUrl = APIConnector.DEFAULTAPIURL) { APIConnector apiConnector = this.CreateConnector(from, secret, apiUrl); return apiConnector.LookupEmail(email); } /// /// Wrapper to id lookup via phone number /// /// Phone number for lookup /// Sender id /// Sender secret /// Optional api url /// id public string LookupPhone(string phoneNo, string from, string secret, string apiUrl = APIConnector.DEFAULTAPIURL) { APIConnector apiConnector = this.CreateConnector(from, secret, apiUrl); return apiConnector.LookupPhone(phoneNo); } /// /// Wrapper to lookup/fetch public key /// /// Id for lookup /// Sender id /// Sender secret /// Optional api url /// public key has hex-string public string LookupKey(string threemaId, string from, string secret, string apiUrl = APIConnector.DEFAULTAPIURL) { APIConnector apiConnector = this.CreateConnector(from, secret, apiUrl); byte[] publicKey = apiConnector.LookupKey(threemaId); if (publicKey != null) { return new Key(Key.KeyType.PUBLIC, publicKey).Encode(); } return null; } /// /// Wrapper to lookup capabilities /// /// Id for lookup /// Sender id /// Sender secret /// Optional api url /// Array with capatilities public System.Collections.ArrayList LookupKeyCapability(string threemaId, string from, string secret, string apiUrl = APIConnector.DEFAULTAPIURL) { CapabilityResult capabilities = this.CreateConnector(from, secret, apiUrl) .LookupKeyCapability(threemaId); var result = new ArrayList(); capabilities.Capabilities.ToList().ForEach(c => result.Add(c)); return result; } /// /// Wrapper to lookup credits /// /// From id /// From secret /// Optional api url /// credits or null public int? LookupCredits(string from, string secret, string apiUrl = APIConnector.DEFAULTAPIURL) { return this.CreateConnector(from, secret, apiUrl).LookupCredits(); } /// /// Wrapper to receive message and download files /// /// Sender id /// From id /// From secret /// From private key /// Message id /// Nonce as hex-string /// Box message as hex-string /// Optional path to output folder /// Optional api url /// Array with message-type, message-id and message public ArrayList ReceiveMessage(string id, string from, string secret, string privateKey, string messageId, string nonce, string box, string outputFolder = null, string apiUrl = APIConnector.DEFAULTAPIURL) { byte[] privateKeyBytes = GetKey(privateKey, Key.KeyType.PRIVATE); byte[] nonceBytes = DataUtils.HexStringToByteArray(nonce); E2EHelper e2EHelper = new E2EHelper(this.CreateConnector(from, secret, apiUrl), privateKeyBytes); byte[] boxBytes = DataUtils.HexStringToByteArray(box); ReceiveMessageResult res = e2EHelper.ReceiveMessage(id, messageId, boxBytes, nonceBytes, outputFolder); ArrayList result = new ArrayList(); result.Add(res.Message.GetTypeCode().ToString()); result.Add(res.MessageId); result.Add(res.Message.ToString()); return result; } private APIConnector CreateConnector(string gatewayId, string secret, string apiUrl) { return new APIConnector(gatewayId, secret, apiUrl, new PublicKeyStoreNone()); } private byte[] GetKey(string argument, string expectedKeyType) { Key key; if (File.Exists(argument)) { key = DataUtils.ReadKeyFile(argument, expectedKeyType); } else { key = IcgSoftware.Threema.CoreMsgApi.Key.DecodeKey(argument, expectedKeyType); } return key.key; } } }