コード例 #1
0
        void Skype_FileTransferStatusChanged(IFileTransfer pTransfer, TFileTransferStatus Status)
        {
            try
            {
                WriteToLog(pTransfer.Type.ToString() + " " + pTransfer.Filename + " Status: " + Status.ToString());

                if (pTransfer.Type == TFileTransferType.fileTransferTypeOutgoing)  // sending
                {
                    string peer     = pTransfer.PartnerHandle;
                    string password = GetToken(peer);
                    if (string.IsNullOrEmpty(password))
                    {
                        return; // no encryption
                    }
                    if (peer != this.UserName)
                    {
                        return;                           // tricky: no duplicated encryption
                    }
                    string filepath = pTransfer.FilePath; //.Filename;
                    if (Status == TFileTransferStatus.fileTransferStatusNew)
                    {
                        // encrypt the file content
                        byte[] content = File.ReadAllBytes(filepath);
                        if (content == null)
                        {
                            WriteToLog(" -- read 0 bytes...");
                            return;
                        }
                        WriteToLog(" -- read " + content.Length + " bytes...");

                        byte[] content2 = SkypeEncryptor.Encrypt(content, password);
                        if (content2 == null)
                        {
                            WriteToLog(" -- encrypt error!");
                            return;
                        }
                        WriteToLog(" -- encrypt " + content2.Length + " bytes...");

                        // Tricky: at this time the file is still held by Skype,
                        //    so the bytes are actually written into cache (sometime even not before receiver downloaded it)
                        File.WriteAllBytes(filepath, content2);
                        WriteToLog(" -- write " + content2.Length + " bytes...");
                    }
                    else if (Status == TFileTransferStatus.fileTransferStatusCancelled ||
                             Status == TFileTransferStatus.fileTransferStatusCompleted ||
                             Status == TFileTransferStatus.fileTransferStatusFailed)
                    {
                        if (pTransfer.PartnerHandle == this.UserName) // message for myself
                        {
                            // decrypt the file content
                            byte[] content = File.ReadAllBytes(filepath);
                            if (content == null)
                            {
                                WriteToLog(" -- read 0 bytes...");
                                return;
                            }
                            WriteToLog(" -- read " + content.Length + " bytes...");


                            byte[] content2 = SkypeEncryptor.Decrypt(content, password);
                            if (content2 == null)
                            {
                                WriteToLog(" -- decrypt error!");
                                return;
                            }
                            WriteToLog(" -- decrypt " + content2.Length + " bytes...");

                            // Read the tricky thing above
                            File.WriteAllBytes(filepath, content2);
                            WriteToLog(" -- write " + content2.Length + " bytes...");
                        }
                    }
                }
                else  // receiving
                {
                    string peer     = pTransfer.PartnerHandle;
                    string password = GetToken(peer);
                    if (string.IsNullOrEmpty(password))
                    {
                        return; // no encryption
                    }
                    if (Status == TFileTransferStatus.fileTransferStatusCompleted)
                    {
                        string filepath = pTransfer.FilePath; //.Filename;
                        // decrypt the file content
                        byte[] content = File.ReadAllBytes(filepath);
                        if (content == null)
                        {
                            WriteToLog(" -- read 0 bytes...");
                            return;
                        }
                        WriteToLog(" -- read " + content.Length + " bytes...");


                        byte[] content2 = SkypeEncryptor.Decrypt(content, password);
                        if (content2 == null)
                        {
                            WriteToLog(" -- decrypt error!");
                            return;
                        }
                        WriteToLog(" -- decrypt " + content2.Length + " bytes...");

                        // Read the tricky thing above
                        File.WriteAllBytes(filepath, content2);
                        WriteToLog(" -- write " + content2.Length + " bytes...");
                    }
                }
            }
            catch (Exception ex)
            {
                WriteToLog(ex.Message);
            }
        }
コード例 #2
0
        private void Skype_MessageStatus(ChatMessage pMessage, TChatMessageStatus aStatus)
        {
            try
            {
                WriteToLog(pMessage.Sender.Handle.ToString() + " " + aStatus.ToString() + " " + pMessage.Body);
                string peer     = pMessage.Chat.DialogPartner;
                string password = GetToken(peer);

                if (aStatus == TChatMessageStatus.cmsSending)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        // encrypt
                        byte[] msgbytes = SkypeEncryptor.Encrypt(System.Text.Encoding.Unicode.GetBytes(pMessage.Body), password);
                        pMessage.Body = Convert.ToBase64String(msgbytes); // alter the message body
                        WriteToLog("Message encrypted");
                    }
                }
                else if (aStatus == TChatMessageStatus.cmsReceived || aStatus == TChatMessageStatus.cmsRead)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        // decrypt
                        byte[] msgbytes = Convert.FromBase64String(pMessage.Body);
                        byte[] dec      = SkypeEncryptor.Decrypt(msgbytes, password);
                        string msg      = System.Text.Encoding.Unicode.GetString(dec);
                        WriteToLog("Message decrypted: " + msg);
                        //pMessage.Body = msg; // which is not editable
                    }

                    if (false) // (pMessage.Type == TChatMessageType.cmeSaid)
                    {
                        // Make sure the request came from the mobile account
                        if (pMessage.Sender.Handle.Equals(this._strMobileUser))
                        {
                            // OK, they want to make a change request or send an SMS
                            try
                            {
                                if (pMessage.Body.Length > 3 && pMessage.Body.ToLower().Substring(0, 3).Equals("sms"))
                                {
                                    // This is an SMS request
                                    WriteToLog("SMS request received from " + this._strMobileUser);

                                    // Get the number to SMS to
                                    string[] strBits = pMessage.Body.Split(" ".ToCharArray());
                                    // The number to call is the second argument
                                    // (string is in the format "sms [number] [message]"
                                    string strSmsTarget = strBits[1];
                                    // See if it is a quickswitch number
                                    int intQuickSwitch;
                                    if (int.TryParse(strSmsTarget, out intQuickSwitch) && (intQuickSwitch > 0 && intQuickSwitch < _strShortCutNums.Length))
                                    {
                                        // Yes, this is a quickswitch number. Get the number for it
                                        strSmsTarget = this._strShortCutNums[intQuickSwitch];
                                    }
                                    WriteToLog("Sending SMS to " + strSmsTarget);

                                    // Get the message
                                    string strMessage = pMessage.Body.Substring(pMessage.Body.IndexOf(" ", 4) + 1);
                                    WriteToLog("Message is: " + strMessage);

                                    // Send the SMS
                                    _objSkype.SendSms(strSmsTarget, strMessage, null);
                                    WriteToLog("SMS sent");
                                    pMessage.Chat.SendMessage("SMS sent to " + strSmsTarget);
                                    WriteToLog();
                                }
                                else
                                {
                                    // This is a SkypeOut change request
                                    WriteToLog("Forwarding change request received from " + this._strMobileUser);
                                    string strNewNum = "";
                                    switch (pMessage.Body.ToLower())
                                    {
                                    case "off":
                                        // Switch off forwarding
                                        _objSkype.CurrentUserProfile.CallApplyCF      = false;
                                        _objSkype.CurrentUserProfile.CallForwardRules = "";
                                        break;

                                    case "1":
                                    case "2":
                                    case "3":
                                    case "4":
                                    case "5":
                                        // Quick switch number
                                        strNewNum = this._strShortCutNums[int.Parse(pMessage.Body)];
                                        break;

                                    case "contacts":
                                        // List all the contact numbers
                                        string strContacts = "Quick switch numbers:" + Environment.NewLine;
                                        for (int i = 1; i <= 5; i++)
                                        {
                                            strContacts += i.ToString() + ": " + this._strShortCutNums[i] + Environment.NewLine;
                                        }
                                        WriteToLog("Sending user the list of quick switch numbers");
                                        pMessage.Chat.SendMessage(strContacts);
                                        return;     // Exit out of the function completely

                                    default:
                                        strNewNum = pMessage.Body;
                                        break;
                                    }

                                    if (string.IsNullOrEmpty(strNewNum))
                                    {
                                        pMessage.Chat.SendMessage("Switched off call forwarding");
                                        WriteToLog("Switched off call forwarding");
                                    }
                                    else
                                    {
                                        _objSkype.CurrentUserProfile.CallApplyCF         = true;
                                        _objSkype.CurrentUserProfile.CallForwardRules    = "0,60," + strNewNum;
                                        _objSkype.CurrentUserProfile.CallNoAnswerTimeout = 5;
                                        pMessage.Chat.SendMessage("Reset call forwarding to " + strNewNum);
                                        WriteToLog("Changed SkypeOut forwarding to " + strNewNum);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                pMessage.Chat.SendMessage("Error:  " + ex.Message);
                            }
                        }
                        else
                        {
                            // Someone else? Log and ignore
                            WriteToLog("Chat message received from " + pMessage.Sender.Handle + " was ignored");
                            WriteToLog("Message was: " + pMessage.Body);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }