コード例 #1
0
ファイル: OutlookGnuPG.cs プロジェクト: eabeles/OutlookGnuPG
        private void Application_ItemSend(object Item, ref bool Cancel)
        {
            Outlook.MailItem mailItem = Item as Outlook.MailItem;

              if (mailItem == null)
            return;

            #if VS2008
              //var inspector = Application.ActiveInspector();
              var inspector = mailItem.GetInspector;
              var currentRibbons = Globals.Ribbons[inspector];
              var currentRibbon = currentRibbons.GnuPGRibbonCompose;
            #else
              GnuPGRibbon currentRibbon = ribbon;
            #endif

              if (currentRibbon == null)
            return;

              string mail = mailItem.Body;
              Outlook.OlBodyFormat mailType = mailItem.BodyFormat;
              bool needToEncrypt = currentRibbon.EncryptButton.Checked;
              bool needToSign = currentRibbon.SignButton.Checked;

              // Early out when we don't need to sign/encrypt
              if (!needToEncrypt && !needToSign)
            return;

              if (mailType != Outlook.OlBodyFormat.olFormatPlain)
              {
            MessageBox.Show(
            "OutlookGnuPG can only sign/encrypt plain text mails. Please change the format, or disable signing/encrypting for this mail.",
            "Invalid Mail Format",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);

            Cancel = true; // Prevent sending the mail
            return;
              }

              // Still no gpg.exe path... Annoy the user once again, maybe he'll get it ;)
              if (string.IsNullOrEmpty(_settings.GnuPgPath))
            Settings();

              // Stubborn, give up
              if (string.IsNullOrEmpty(_settings.GnuPgPath))
              {
            MessageBox.Show(
            "OutlookGnuPG can only sign/encrypt when you provide a valid gpg.exe path. Please open Settings and configure it.",
            "Invalid GnuPG Executable",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);

            Cancel = true; // Prevent sending the mail
            return;
              }

              string passphrase = string.Empty;
              string privateKey = string.Empty;
              if (needToSign)
              {
            // Popup UI to select the passphrase and private key.
            Passphrase passphraseDialog = new Passphrase(_settings.DefaultKey, "Sign");
            DialogResult passphraseResult = passphraseDialog.ShowDialog();
            if (passphraseResult != DialogResult.OK)
            {
              // The user closed the passphrase dialog, prevent sending the mail
              Cancel = true;
              return;
            }

            passphrase = passphraseDialog.EnteredPassphrase;
            privateKey = passphraseDialog.SelectedKey;
            passphraseDialog.Close();

            if (string.IsNullOrEmpty(privateKey))
            {
              MessageBox.Show(
              "OutlookGnuPG needs a private key for signing. No keys were detected.",
              "Invalid Private Key",
              MessageBoxButtons.OK,
              MessageBoxIcon.Error);

              Cancel = true; // Prevent sending the mail
              return;
            }
              }

            #if VS2008
              IList<string> recipients = new List<string> { string.Empty };
            #else
              IList<string> recipients = new List<string>();
              recipients.Add(string.Empty);
            #endif
              if (needToEncrypt)
              {
            // Popup UI to select the encryption targets
            List<string> mailRecipients = new List<string>();
            foreach (Outlook.Recipient mailRecipient in mailItem.Recipients)
              mailRecipients.Add(GetAddressCN(((Outlook.Recipient)mailRecipient).Address));

            Recipient recipientDialog = new Recipient(mailRecipients); // Passing in the first addres, maybe it matches
            DialogResult recipientResult = recipientDialog.ShowDialog();

            if (recipientResult != DialogResult.OK)
            {
              // The user closed the recipient dialog, prevent sending the mail
              Cancel = true;
              return;
            }

            recipients = recipientDialog.SelectedKeys;
            recipientDialog.Close();

            if (recipients.Count == 0)
            {
              MessageBox.Show(
              "OutlookGnuPG needs a recipient when encrypting. No keys were detected/selected.",
              "Invalid Recipient Key",
              MessageBoxButtons.OK,
              MessageBoxIcon.Error);

              Cancel = true; // Prevent sending the mail
              return;
            }
              }

              // Sign and encrypt the plaintext mail
              if ((needToSign) && (needToEncrypt))
              {
            mail = SignAndEncryptEmail(mail, privateKey, passphrase, recipients);
              }
              else if (needToSign)
              {
            // Sign the plaintext mail if needed
            mail = SignEmail(mail, privateKey, passphrase);
              }
              else if (needToEncrypt)
              {
            // Encrypt the plaintext mail if needed
            mail = EncryptEmail(mail, passphrase, recipients);
              }

              // Update the new content
              if (mail != _gnuPgErrorString)
            mailItem.Body = mail;
              else
            Cancel = true;
        }