EncryptionStatusPanel.cs
author Dean Looyengoed
Mon, 28 Dec 2015 23:11:28 +0100
changeset 155 cda43d18bcd3
parent 154 8b05adf8a371
child 156 dcb5ac2a9cd4
permissions -rw-r--r--
Add state to handshake form and decouple it from the encryption status panel
vb@133
     1
´╗┐using System;
vb@133
     2
using System.Collections;
vb@133
     3
using System.Collections.Generic;
vb@133
     4
using System.Linq;
vb@133
     5
using System.Text;
vb@133
     6
using System.IO;
vb@133
     7
using System.Windows.Forms;
vb@133
     8
using System.Threading;
vb@133
     9
using System.Runtime.InteropServices;
vb@133
    10
using Office = Microsoft.Office.Core;
vb@133
    11
using Outlook = Microsoft.Office.Interop.Outlook;
vb@133
    12
using Color = System.Drawing.Color;
vb@133
    13
using pEpCOMServerAdapterLib;
vb@133
    14
using System.Diagnostics;
Dean@151
    15
using Microsoft.Win32;
vb@133
    16
vb@133
    17
namespace pEp
vb@133
    18
{
vb@133
    19
    partial class EncryptionStatusPanel
vb@133
    20
    {
vb@133
    21
        #region Form Region Factory
vb@133
    22
vb@133
    23
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
vb@133
    24
        [Microsoft.Office.Tools.Outlook.FormRegionName("pEp.FormRegion1")]
vb@133
    25
        public partial class FormRegion1Factory
vb@133
    26
        {
vb@133
    27
            // Occurs before the form region is initialized.
vb@133
    28
            // To prevent the form region from appearing, set e.Cancel to true.
vb@133
    29
            // Use e.OutlookItem to get a reference to the current Outlook item.
vb@133
    30
            private void FormRegion1Factory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
vb@133
    31
            {
vb@133
    32
            }
vb@133
    33
        }
vb@133
    34
vb@133
    35
        #endregion
vb@133
    36
vb@133
    37
        static void StopHere(string reason)
vb@133
    38
        {
vb@133
    39
#if DEBUG
vb@133
    40
            MessageBox.Show(reason);
vb@133
    41
#else
vb@133
    42
            try
vb@133
    43
            {
vb@133
    44
                ThisAddIn.pEp.log("internal error", "pEp for Outlook", reason, "in EncryptionStatusPanel.cs");
vb@133
    45
            }
vb@133
    46
            catch (COMException) { }
vb@133
    47
            catch (Exception) { }
vb@133
    48
#endif
vb@133
    49
        }
vb@133
    50
vb@133
    51
        private CryptableMailItem _mailItem = null;
vb@133
    52
vb@133
    53
        CryptableMailItem mailItem
vb@133
    54
        {
vb@133
    55
            get
vb@133
    56
            {
vb@133
    57
                try
vb@133
    58
                {
vb@133
    59
                    if (this.OutlookItem == null)
vb@133
    60
                    {
vb@133
    61
                        _mailItem = null;
vb@133
    62
                        return null;
vb@133
    63
                    }
vb@133
    64
                }
vb@133
    65
                catch (COMException)
vb@133
    66
                {
vb@133
    67
                    return null;
vb@133
    68
                }
vb@133
    69
vb@133
    70
                Outlook.MailItem omi = null;
vb@133
    71
vb@133
    72
                try
vb@133
    73
                {
vb@133
    74
                    omi = (Outlook.MailItem)this.OutlookItem;
vb@133
    75
                }
vb@133
    76
                catch
vb@133
    77
                {
vb@133
    78
                    _mailItem = null;
vb@133
    79
                    return null;
vb@133
    80
                }
vb@133
    81
vb@133
    82
                Outlook.MAPIFolder folder = omi.Parent;
vb@136
    83
                Outlook.MAPIFolder deleted = folder.Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
vb@136
    84
                if (deleted != null && folder.EntryID == deleted.EntryID)
vb@133
    85
                {
vb@133
    86
                    _mailItem = null;
vb@133
    87
                    return null;
vb@133
    88
                }
vb@133
    89
vb@133
    90
                if (_mailItem == null) {
vb@133
    91
                    _mailItem = new CryptableMailItem(omi);
vb@133
    92
                }
vb@133
    93
                else
vb@133
    94
                {
vb@133
    95
                    if (_mailItem.EntryID != omi.EntryID)
vb@133
    96
                    {
vb@133
    97
                        _mailItem = new CryptableMailItem(omi);
vb@133
    98
                    }
vb@133
    99
                }
vb@133
   100
vb@133
   101
                return _mailItem;
vb@133
   102
            }
vb@133
   103
        }
vb@133
   104
vb@133
   105
        // Occurs before the form region is displayed.
vb@133
   106
        // Use this.OutlookItem to get a reference to the current Outlook item.
vb@133
   107
        // Use this.OutlookFormRegion to get a reference to the form region.
vb@133
   108
        private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
vb@133
   109
        {
vb@133
   110
            if (this.OutlookItem == null)
vb@133
   111
            {
vb@133
   112
                enc_status = _pEp_color.pEp_rating_undefined;
vb@133
   113
            }
vb@133
   114
            else
vb@133
   115
            {
vb@133
   116
                Outlook.MailItem omi = (Outlook.MailItem)this.OutlookItem;
vb@133
   117
                try
vb@133
   118
                {
vb@133
   119
                    omi.PropertyChange += mailItem_PropertyChange;
dean@144
   120
                    if (CryptableMailItem.IsEncryptedStore(omi))
vb@133
   121
                    {
vb@133
   122
                        if (PreviewUnencrypted.isPGPEncrypted(omi) && omi.Sender != null)
vb@133
   123
                        {
vb@133
   124
                            omi.Open += omi_Open;
vb@133
   125
                        }
vb@133
   126
                    }
vb@133
   127
                }
vb@133
   128
                catch { }
vb@133
   129
vb@133
   130
                if (ThisAddIn.addIn.enabled)
vb@133
   131
                {
vb@133
   132
                    reset_enc_status();
vb@133
   133
                }
vb@133
   134
                else
vb@133
   135
                {
vb@133
   136
                    timerRefresh.Enabled = true;
vb@133
   137
                }
vb@133
   138
            }
vb@133
   139
        }
vb@133
   140
vb@133
   141
        void omi_Open(ref bool Cancel)
vb@133
   142
        {
dean@144
   143
            if (mailItem != null && _mailItem.MirrorDisplay())
vb@133
   144
                Cancel = true;
vb@133
   145
        }
vb@133
   146
vb@133
   147
        private bool in_reset = false;
vb@133
   148
vb@133
   149
        protected void makePreview()
vb@133
   150
        {
vb@130
   151
            WindowFormRegionCollection formRegions;
vb@130
   152
vb@130
   153
            if (Globals.ThisAddIn.Application.ActiveInspector() != null)
vb@130
   154
                return;
vb@130
   155
            else if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
vb@130
   156
                formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveExplorer()];
vb@130
   157
            else
vb@133
   158
                return;
vb@133
   159
vb@133
   160
            try
vb@133
   161
            {
vb@133
   162
                if (formRegions.PreviewUnencrypted.Visible)
vb@133
   163
                {
dean@144
   164
                    byte[] rtf = mailItem.MirrorGetRTFBody();
vb@133
   165
                    formRegions.PreviewUnencrypted.textPreview.Rtf = System.Text.Encoding.ASCII.GetString(rtf, 0, rtf.Length);
dean@144
   166
                    formRegions.PreviewUnencrypted.textSubject.Text = mailItem.MirrorGetSubject();
dean@144
   167
                    formRegions.PreviewUnencrypted.textFrom.Text = mailItem.FromUsername + " <" + mailItem.FromAddress + ">";
vb@133
   168
                }
vb@133
   169
            }
vb@133
   170
            catch { }
vb@133
   171
        }
vb@133
   172
vb@133
   173
        protected void reset_enc_status()
vb@133
   174
        {
vb@133
   175
            if (in_reset)
vb@133
   176
                return;
vb@133
   177
vb@133
   178
            in_reset = true;
vb@133
   179
vb@133
   180
            if (mailItem != null)
vb@133
   181
            {
vb@133
   182
                if (_mailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
vb@133
   183
                {
dean@144
   184
                    if (_mailItem.SendUnencrypted == CheckState.Checked)
vb@133
   185
                    {
vb@133
   186
                        enc_status = _pEp_color.pEp_rating_unencrypted;
vb@133
   187
                    }
vb@133
   188
                    else
vb@133
   189
                    {
vb@133
   190
                        ThisAddIn.verbose("get_color in reset_enc_status");
dean@144
   191
                        enc_status = _mailItem.ColorRating;
vb@133
   192
                    }
vb@133
   193
                }
vb@133
   194
                else
vb@133
   195
                {
vb@133
   196
                    _mailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
vb@133
   197
                    timerRefresh.Interval = 500;
vb@133
   198
                    timerRefresh.Enabled = true;
vb@133
   199
                }
vb@133
   200
            }
vb@133
   201
vb@133
   202
            in_reset = false;
vb@133
   203
        }
vb@133
   204
vb@133
   205
        void mailItem_PropertyChange(string Name)
vb@133
   206
        {
vb@133
   207
            switch (Name)
vb@133
   208
            {
vb@133
   209
                case "To":
vb@133
   210
                    timerRefresh.Enabled = true;
vb@133
   211
                    break;
vb@133
   212
                // Outlook bug: there are always both events, so one is enough
vb@133
   213
                //case "CC":
vb@133
   214
                //    reset_enc_status();
vb@133
   215
                //    break;
vb@133
   216
            }
vb@133
   217
        }
vb@133
   218
vb@133
   219
        // Occurs when the form region is closed.
vb@133
   220
        // Use this.OutlookItem to get a reference to the current Outlook item.
vb@133
   221
        // Use this.OutlookFormRegion to get a reference to the form region.
vb@133
   222
        private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
vb@133
   223
        {
vb@133
   224
        }
vb@133
   225
vb@133
   226
        private struct EncStatusData
vb@133
   227
        {
vb@133
   228
            public string title;
vb@133
   229
            public string explanation;
vb@133
   230
            public string suggestion;
vb@133
   231
            public Color background;
vb@133
   232
            public Color foreground;
vb@133
   233
            public string button_text;
vb@133
   234
            public ContextMenuStrip strip;
vb@133
   235
        };
vb@133
   236
vb@133
   237
        private EncStatusData enc_status_data;
vb@133
   238
        private _pEp_color _enc_status;
vb@133
   239
vb@133
   240
        public _pEp_color enc_status {
vb@133
   241
            get
vb@133
   242
            {
vb@133
   243
                return _enc_status;
vb@133
   244
            }
vb@133
   245
            set
vb@133
   246
            {
vb@133
   247
                if (value != _enc_status)
vb@133
   248
                {
vb@133
   249
                    _enc_status = value;
vb@133
   250
vb@133
   251
                    switch (value)
vb@133
   252
                    {
vb@133
   253
                        case _pEp_color.pEp_rating_trusted:
vb@133
   254
                        case _pEp_color.pEp_rating_trusted_and_anonymized:
vb@133
   255
                            enc_status_data.explanation = pEp.Properties.Resources.RatingTrustedExplanation;
vb@133
   256
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingTrustedSuggestion;
vb@133
   257
                            enc_status_data.title = pEp.Properties.Resources.RatingTrustedTitle;
vb@133
   258
                            enc_status_data.background = Color.Green;
vb@133
   259
                            enc_status_data.foreground = Color.White;
vb@133
   260
                            enc_status_data.button_text = pEp.Properties.Resources.RatingTrustedButtontext;
vb@133
   261
                            enc_status_data.strip = new ContextMenuStrip();
vb@133
   262
                            enc_status_data.strip.Items.Add("");
vb@133
   263
                            enc_status_data.strip.Opening += secure_strip_Opening;
vb@133
   264
                            break;
vb@133
   265
vb@133
   266
                        case _pEp_color.pEp_rating_reliable:
vb@133
   267
                            enc_status_data.explanation = pEp.Properties.Resources.RatingReliableExplaination;
vb@133
   268
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingReliableSuggestion;
vb@133
   269
                            enc_status_data.title = pEp.Properties.Resources.RatingReliableTitle;
vb@133
   270
                            enc_status_data.background = Color.Gold;
vb@133
   271
                            enc_status_data.foreground = Color.Black;
vb@133
   272
                            enc_status_data.button_text = pEp.Properties.Resources.RatingReliableButtontext;
vb@133
   273
                            enc_status_data.strip = null;
vb@133
   274
                            break;
vb@133
   275
vb@133
   276
                        case _pEp_color.pEp_rating_unreliable:
vb@133
   277
                            enc_status_data.explanation = pEp.Properties.Resources.RatingUnreliableExplanation;
vb@133
   278
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingUnreliableSuggestion;
vb@133
   279
                            enc_status_data.title = pEp.Properties.Resources.RatingUnreliableTitle;
vb@133
   280
                            enc_status_data.background = Color.Gainsboro;
vb@133
   281
                            enc_status_data.foreground = Color.Black;
vb@133
   282
                            enc_status_data.button_text = pEp.Properties.Resources.RatingUnreliableButtontext;
vb@133
   283
                            enc_status_data.strip = null;
vb@133
   284
                            break;
vb@133
   285
vb@133
   286
                        case _pEp_color.pEp_rating_unencrypted:
vb@133
   287
                            enc_status_data.explanation = pEp.Properties.Resources.RatingUnencryptedExplanation;
vb@133
   288
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingUnencryptedSuggestion;
vb@133
   289
                            enc_status_data.title = pEp.Properties.Resources.RatingUnencryptedTitle;
vb@133
   290
                            enc_status_data.background = Color.Gainsboro;
vb@133
   291
                            enc_status_data.foreground = Color.Black;
vb@133
   292
                            enc_status_data.button_text = pEp.Properties.Resources.RatingUnencryptedButtontext;
vb@133
   293
                            enc_status_data.strip = null;
vb@133
   294
                            break;
vb@133
   295
vb@133
   296
                        case _pEp_color.pEp_rating_under_attack:
vb@133
   297
                            enc_status_data.explanation = pEp.Properties.Resources.RatingUnderattackExplanation;
vb@133
   298
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingUnderattackSuggestion;
vb@133
   299
                            enc_status_data.title = pEp.Properties.Resources.RatingUnderattackTitle;
vb@133
   300
                            enc_status_data.background = Color.Red;
vb@133
   301
                            enc_status_data.foreground = Color.White;
vb@133
   302
                            enc_status_data.button_text = pEp.Properties.Resources.RatingUnderattackButtontext;
vb@133
   303
                            enc_status_data.strip = null;
vb@133
   304
                            break;
vb@133
   305
vb@133
   306
                        case _pEp_color.pEp_rating_undefined:
vb@133
   307
                            // will not happen
vb@133
   308
                            enc_status_data.explanation = pEp.Properties.Resources.RatingUndefinedExplanation;
vb@133
   309
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingUndefinedSuggestion;
vb@133
   310
                            enc_status_data.title = pEp.Properties.Resources.RatingUndefinedTitle;
vb@133
   311
                            enc_status_data.background = Color.Gainsboro;
vb@133
   312
                            enc_status_data.foreground = Color.Black;
vb@133
   313
                            enc_status_data.button_text = pEp.Properties.Resources.RatingUndefinedButtontext;
vb@133
   314
                            enc_status_data.strip = null;
vb@133
   315
                            break;
vb@133
   316
vb@133
   317
                        case _pEp_color.pEp_rating_cannot_decrypt:
vb@133
   318
                        case _pEp_color.pEp_rating_have_no_key:
vb@133
   319
                            enc_status_data.explanation = pEp.Properties.Resources.RatingHavenokeyExplanation;
vb@133
   320
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingHavenokeySuggestion;
vb@133
   321
                            enc_status_data.title = pEp.Properties.Resources.RatingHavenokeyTitle;
vb@133
   322
                            enc_status_data.background = Color.Gainsboro;
vb@133
   323
                            enc_status_data.foreground = Color.Black;
vb@133
   324
                            enc_status_data.button_text = pEp.Properties.Resources.RatingHavenokeyButtontext;
vb@133
   325
                            enc_status_data.strip = null;
vb@133
   326
                            break;
vb@133
   327
                    }
vb@133
   328
vb@133
   329
                    linkExplanation.Text = enc_status_data.title;
vb@133
   330
                    EncryptionStatus.BackColor = enc_status_data.background;
vb@133
   331
                    EncryptionStatus.ForeColor = enc_status_data.foreground;
vb@133
   332
                    EncryptionStatus.Text = enc_status_data.button_text;
vb@133
   333
                    EncryptionStatus.ContextMenuStrip = enc_status_data.strip;
vb@133
   334
                }
vb@133
   335
            }
vb@133
   336
        }
vb@133
   337
vb@133
   338
        void secure_strip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
vb@133
   339
        {
vb@133
   340
            enc_status_data.strip.Items.Clear();
vb@133
   341
dean@144
   342
            if (mailItem.IsIncoming)
vb@133
   343
            {
vb@133
   344
                pEp_identity_s ident = new pEp_identity_s();
dean@144
   345
                ident.address = mailItem.FromAddress;
dean@144
   346
                ident.username = mailItem.FromUsername;
dean@144
   347
                ident.user_id = CryptableMailItem.GetUserIDFromAddress(ident.address, ident.username);
vb@133
   348
                ident = ThisAddIn.pEp.update_identity(ident);
vb@133
   349
                string text = pEp.Properties.Resources.IdentityTrustText + ident.username + " <" + ident.address + ">";
vb@133
   350
                enc_status_data.strip.Items.Add(text, null, (x, y) => { undoHandshake(ident); });
vb@133
   351
            }
vb@133
   352
            else
vb@133
   353
            {
dean@144
   354
                foreach (pEp_identity_s ident in mailItem.Addresses)
vb@133
   355
                {
vb@133
   356
                    pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(ident);
vb@133
   357
                    if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_confirmed_encryption)
vb@133
   358
                    {
vb@133
   359
                        string text = pEp.Properties.Resources.IdentityTrustText + ident.username + " <" + ident.address + ">";
vb@133
   360
                        enc_status_data.strip.Items.Add(text, null, (x, y) => { undoHandshake(ident); });
vb@133
   361
                    }
vb@133
   362
                }
vb@133
   363
            }
vb@133
   364
        }
vb@133
   365
vb@133
   366
        public static string quadruple(string text)
vb@133
   367
        {
vb@133
   368
            List<string> result = new List<string>();
vb@133
   369
            for (int i=0; i<text.Length; i+=4)
vb@133
   370
            {
vb@133
   371
                try
vb@133
   372
                {
vb@133
   373
                    result.Add(text.Substring(i, 4));
vb@133
   374
                }
vb@133
   375
                catch (ArgumentOutOfRangeException)
vb@133
   376
                {
vb@133
   377
                    result.Add(text.Substring(i));
vb@133
   378
                    break;
vb@133
   379
                }
vb@133
   380
            }
vb@133
   381
            return String.Join(" ", result);
vb@133
   382
        }
vb@133
   383
vb@133
   384
        private void doHandshake(pEp_identity_s identityPartner)
vb@133
   385
        {
Dean@155
   386
            string ownShort;
Dean@155
   387
            string ownLong;
Dean@155
   388
            string ownFpr;
Dean@155
   389
            string partnerShort;
Dean@155
   390
            string partnerLong;
Dean@155
   391
            string partnerFpr;
Dean@155
   392
            string[] lines;
Dean@155
   393
            pEp_identity_s me;
Dean@155
   394
            FormHandshake handshakeDialog;
Dean@155
   395
            FormHandshake.State state = new FormHandshake.State();
Dean@155
   396
dean@144
   397
            Debug.Assert(mailItem.ColorRating == _pEp_color.pEp_rating_yellow);
vb@132
   398
vb@139
   399
            ThisAddIn.verbose("doHandshake");
vb@139
   400
            ThisAddIn.verbose(identityPartner.address);
vb@139
   401
vb@132
   402
            if (identityPartner.username == "")
vb@133
   403
                identityPartner.username = identityPartner.address;
vb@139
   404
            ThisAddIn.verbose(identityPartner.username);
vb@133
   405
dean@144
   406
            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
vb@139
   407
            ThisAddIn.verbose(identityPartner.user_id);
vb@133
   408
dean@144
   409
            if (mailItem.IsIncoming)
vb@133
   410
            {
dean@144
   411
                string entryID = mailItem.ReceivedByEntryID;
vb@133
   412
                string address = "";
vb@133
   413
vb@133
   414
                foreach (Outlook.Account a in ThisAddIn.addIn.Application.Session.Accounts){
vb@133
   415
                    if (a.CurrentUser.EntryID == entryID) {
vb@133
   416
                        address = a.SmtpAddress;
vb@133
   417
                        break;
vb@133
   418
                    }
vb@133
   419
                }
vb@133
   420
vb@133
   421
                if (address == "")
vb@133
   422
                    address = ThisAddIn.addIn.Application.Session.Accounts[1].SmtpAddress;
vb@133
   423
vb@133
   424
                me = ThisAddIn.addIn.mySelf(address);
vb@133
   425
            }
vb@133
   426
            else // outgoing
vb@133
   427
            {
dean@144
   428
                ThisAddIn.verbose("mailItem.from_address: " + mailItem.FromAddress);
dean@144
   429
                me = ThisAddIn.addIn.mySelf(mailItem.FromAddress);
vb@133
   430
            }
vb@133
   431
vb@139
   432
            ThisAddIn.verbose(me.fpr);
vb@139
   433
Dean@155
   434
            ownShort = ThisAddIn.pEp.trustwords(me.fpr, max_words: 5).ToLower();
Dean@155
   435
            ownLong  = ThisAddIn.pEp.trustwords(me.fpr).ToLower();
Dean@155
   436
            ownFpr   = quadruple(me.fpr);
vb@133
   437
vb@139
   438
            ThisAddIn.verbose(identityPartner.fpr);
vb@139
   439
Dean@155
   440
            partnerShort = ThisAddIn.pEp.trustwords(identityPartner.fpr, max_words: 5).ToLower();
Dean@155
   441
            partnerLong  = ThisAddIn.pEp.trustwords(identityPartner.fpr).ToLower();
Dean@155
   442
            partnerFpr   = quadruple(identityPartner.fpr);
vb@133
   443
Dean@155
   444
            if (ownFpr.CompareTo(partnerFpr) > 0)
vb@133
   445
            {
Dean@155
   446
                state.TrustwordsShort = partnerShort + ownShort;
Dean@155
   447
                state.TrustwordsFull  = partnerLong  + ownLong;
Dean@155
   448
Dean@155
   449
                lines = new string[3];
Dean@155
   450
                lines[0] = pEp.Properties.Resources.TrustwordsPartnershortPartner + partnerFpr;
Dean@155
   451
                lines[2] = pEp.Properties.Resources.TrustwordsPartnershortMyself + ownFpr;
Dean@155
   452
                state.Fingerprint = lines;
vb@133
   453
            }
vb@133
   454
            else
vb@133
   455
            {
Dean@155
   456
                state.TrustwordsShort = ownShort + " " + partnerShort;
Dean@155
   457
                state.TrustwordsFull  = ownLong  + " " + partnerLong;
Dean@155
   458
Dean@155
   459
                lines = new string[3];
Dean@155
   460
                lines[0] = pEp.Properties.Resources.TrustwordsOwnshortMyself + ownFpr;
Dean@155
   461
                lines[2] = pEp.Properties.Resources.TrustwordsOwnshortPartner + partnerFpr;
Dean@155
   462
                state.Fingerprint = lines;
vb@133
   463
            }
vb@133
   464
vb@139
   465
            ThisAddIn.verbose("try to show dialog");
vb@139
   466
Dean@155
   467
            // Create and show handshake dialog
Dean@155
   468
            handshakeDialog = new FormHandshake(state);
Dean@155
   469
vb@132
   470
            DialogResult result = handshakeDialog.ShowDialog(this);
vb@133
   471
            ThisAddIn.verbose("handshakeDialog.ShowDialog(this) => " + result.ToString());
vb@133
   472
vb@133
   473
            switch (result)
vb@133
   474
            {
vb@133
   475
                case DialogResult.Yes:
dean@144
   476
                    mailItem.ColorRating = _pEp_color.pEp_rating_trusted;
vb@132
   477
                    identityPartner = ThisAddIn.pEp.trust_personal_key(identityPartner);
vb@133
   478
                    reset_enc_status(); 
vb@133
   479
                    break;
vb@133
   480
                case DialogResult.No:
vb@133
   481
                    ThisAddIn.pEp.key_compromized(identityPartner);
vb@133
   482
                    identityPartner.comm_type = _pEp_comm_type.pEp_ct_compromized;
dean@144
   483
                    mailItem.ColorRating = _pEp_color.pEp_rating_red;
vb@133
   484
                    reset_enc_status(); 
vb@133
   485
                    break;
vb@133
   486
            }
vb@133
   487
        }
vb@133
   488
vb@133
   489
        void btnConfirm_Click(object sender, EventArgs e)
vb@133
   490
        {
vb@133
   491
            Button btn = (Button)sender;
vb@133
   492
            TabControl tc = (TabControl)btn.Parent.Parent.Parent;
vb@133
   493
            tc.SelectedIndex = 1;
vb@133
   494
        }
vb@133
   495
vb@133
   496
        private void undoHandshake(pEp_identity_s identityPartner)
vb@133
   497
        {
vb@133
   498
            ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   499
//            Debug.Assert((identityPartner.comm_type & _pEp_comm_type.pEp_ct_confirmed) != _pEp_comm_type.pEp_ct_unknown);
vb@133
   500
            identityPartner.comm_type ^= _pEp_comm_type.pEp_ct_confirmed;
vb@133
   501
            ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   502
            reset_enc_status();
vb@133
   503
        }
vb@133
   504
vb@133
   505
        private void EncryptionStatus_Click(object sender, EventArgs e)
vb@133
   506
        {
vb@133
   507
            switch (enc_status)
vb@133
   508
            {
vb@133
   509
                case _pEp_color.pEp_rating_green:
vb@133
   510
                    try
vb@133
   511
                    {
dean@144
   512
                        string[] key_list = mailItem.Keylist;
vb@133
   513
                        if (key_list.Length > 0)
vb@133
   514
                        {
vb@133
   515
                            string msg;
vb@133
   516
                            if (key_list.Length > 1)
vb@133
   517
                                msg = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageMultiple + " {0}.", String.Join(", ", key_list));
vb@133
   518
                            else
vb@133
   519
                                msg = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageSingle + " {0}.", key_list[0]);
vb@133
   520
                            MessageBox.Show(msg, pEp.Properties.Resources.EncryptionStatusClickMessageBox, MessageBoxButtons.OK, MessageBoxIcon.Information);
vb@133
   521
                        }
vb@133
   522
                    }
vb@133
   523
                    catch (Exception ex)
vb@133
   524
                    {
vb@133
   525
                        StopHere(ex.ToString());
vb@133
   526
                    }
vb@133
   527
                    break;
vb@133
   528
vb@133
   529
                case _pEp_color.pEp_rating_yellow:
vb@133
   530
                    try
vb@133
   531
                    {
dean@144
   532
                        if (mailItem.IsIncoming)
vb@133
   533
                        {
vb@133
   534
                            pEp_identity_s identityPartner = new pEp_identity_s();
dean@144
   535
                            identityPartner.address = mailItem.FromAddress;
dean@144
   536
                            identityPartner.username = mailItem.FromUsername;
dean@144
   537
                            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
vb@139
   538
vb@133
   539
                            try
vb@133
   540
                            {
vb@133
   541
                                identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   542
                            }
vb@133
   543
                            catch
vb@133
   544
                            {
vb@133
   545
                                enc_status = _pEp_color.pEp_rating_undefined;
vb@133
   546
                                return;
vb@133
   547
                            }
vb@133
   548
                            doHandshake(identityPartner);
vb@133
   549
                        }
vb@133
   550
                        else // outgoing
vb@133
   551
                        {
vb@139
   552
                            ThisAddIn.verbose("yellow, handshake initiated");
vb@139
   553
vb@133
   554
                            List<pEp_identity_s> partnersToCheck = new List<pEp_identity_s>();
vb@133
   555
dean@144
   556
                            foreach (pEp_identity_s identity in mailItem.Addresses)
vb@133
   557
                            {
vb@139
   558
                                ThisAddIn.verbose("adding " + identity.username + " <" + identity.address + ">");
vb@139
   559
vb@133
   560
                                try
vb@133
   561
                                {
vb@133
   562
                                    pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(identity);
vb@133
   563
                                    if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_unconfirmed_encryption
vb@133
   564
                                        && identityPartner.comm_type < _pEp_comm_type.pEp_ct_confirmed_encryption)
vb@133
   565
                                    {
vb@133
   566
                                        partnersToCheck.Add(identityPartner);
vb@133
   567
                                    }
vb@133
   568
                                }
vb@133
   569
                                catch
vb@133
   570
                                {
vb@133
   571
                                }
vb@133
   572
                            }
vb@133
   573
vb@133
   574
                            if (partnersToCheck.Count == 0)
vb@133
   575
                            {
vb@133
   576
                                enc_status = _pEp_color.pEp_rating_undefined;
vb@133
   577
                                return;
vb@133
   578
                            }
vb@133
   579
dean@144
   580
                            pEp_identity_s[] addresses = mailItem.Addresses;
vb@133
   581
vb@133
   582
                            if (addresses.Length == 1)
vb@133
   583
                            {
vb@133
   584
                                doHandshake(partnersToCheck[0]);
vb@133
   585
                            }
vb@133
   586
                            else if (addresses.Length > 1)
vb@133
   587
                            {
vb@133
   588
                                contextMenuHandshake.Items.Clear();
vb@133
   589
                                foreach (pEp_identity_s identity in partnersToCheck) {
vb@133
   590
                                    string text = identity.username + " <" + identity.address + ">";
vb@133
   591
                                    contextMenuHandshake.Items.Add(text, null, (x, y) => { doHandshake(identity); });
vb@133
   592
                                }
vb@133
   593
                                contextMenuHandshake.Show(EncryptionStatus, 10, 10);
vb@133
   594
                            }
vb@133
   595
                        }
vb@133
   596
                    }
vb@133
   597
                    catch (COMException ex)
vb@133
   598
                    {
vb@133
   599
                        StopHere(ex.ToString());
vb@133
   600
                    }
vb@133
   601
                    catch (Exception ex)
vb@133
   602
                    {
vb@133
   603
                        StopHere(ex.ToString());
vb@133
   604
                    }
vb@133
   605
                    break;
vb@133
   606
vb@133
   607
                case _pEp_color.pEp_rating_unencrypted:
dean@144
   608
                    if (mailItem.IsIncoming)
vb@133
   609
                    {
vb@133
   610
                        MessageBox.Show(pEp.Properties.Resources.RatingUnencryptedMessageboxReasonBody, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   611
                    }
vb@133
   612
                    else
vb@133
   613
                    {
vb@133
   614
                        try
vb@133
   615
                        {
vb@133
   616
                            string reason = "";
dean@144
   617
                            foreach (pEp_identity_s identity in mailItem.Addresses)
vb@133
   618
                            {
vb@133
   619
                                if (identity.comm_type < _pEp_comm_type.pEp_ct_security_by_obscurity)
vb@133
   620
                                {
vb@133
   621
                                    if (reason != "")
vb@133
   622
                                        reason += "\n";
vb@133
   623
                                    // BUG: reason string in English language only and trivial
vb@133
   624
                                    string ct = identity.comm_type.ToString();
vb@133
   625
                                    ct = new string(ct.Skip(7).ToArray());
vb@133
   626
                                    ct = ct.Replace("_", " ");
vb@133
   627
                                    reason += identity.username + " <" + identity.address + ">: " + ct;
vb@133
   628
                                }
vb@133
   629
                            }
vb@133
   630
                            if (reason != "")
vb@133
   631
                                MessageBox.Show(reason, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   632
                        }
vb@133
   633
                        catch (COMException ex)
vb@133
   634
                        {
vb@133
   635
                            StopHere(ex.ToString());
vb@133
   636
                        }
vb@133
   637
                    }
vb@133
   638
                    break;
vb@133
   639
vb@133
   640
                case _pEp_color.pEp_rating_cannot_decrypt:
vb@133
   641
                case _pEp_color.pEp_rating_have_no_key:
vb@133
   642
                    try
vb@133
   643
                    {
dean@144
   644
                        string[] keys = mailItem.Keylist;
vb@133
   645
vb@133
   646
                        if (keys.Length > 1)
vb@133
   647
                        {
vb@133
   648
                            string reason;
vb@133
   649
vb@133
   650
                            if (keys.Length == 2)
vb@133
   651
                                reason = pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodyNokey;
vb@133
   652
                            else
vb@133
   653
                                reason = string.Format(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodySomekey, keys.Length - 1);
vb@133
   654
vb@133
   655
                            string keyinfo = "";
vb@133
   656
vb@133
   657
                            foreach (string key in keys)
vb@133
   658
                            {
vb@133
   659
                                if (key.Length >= 8)
vb@133
   660
                                    keyinfo += "\n" + key;
vb@133
   661
                            }
vb@133
   662
vb@133
   663
                            reason += keyinfo;
vb@133
   664
vb@133
   665
                            MessageBox.Show(reason, pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   666
                        }
vb@133
   667
                        else if (keys.Length == 1)
vb@133
   668
                        {
vb@133
   669
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonError + keys[0], pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   670
                        }
vb@133
   671
                        else
vb@133
   672
                        {
vb@133
   673
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonUnknownformat, pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   674
                        }
vb@133
   675
                    }
vb@133
   676
                    catch (COMException ex)
vb@133
   677
                    {
vb@133
   678
                        StopHere(ex.ToString());
vb@133
   679
                    }
vb@133
   680
                    break;
vb@133
   681
                    
vb@133
   682
                case _pEp_color.pEp_rating_undefined:
vb@133
   683
                    if (mailItem != null)
vb@133
   684
                    {
dean@144
   685
                        enc_status = mailItem.ColorRating;
vb@133
   686
                    }
vb@133
   687
                    break;
vb@133
   688
            }
vb@133
   689
        }
vb@133
   690
vb@133
   691
        private void linkExplanation_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
vb@133
   692
        {
vb@133
   693
            if (enc_status == _pEp_color.pEp_rating_undefined)
vb@133
   694
            {
vb@133
   695
                enc_status_data.explanation = pEp.Properties.Resources.RatingUndefinedExplanation;
vb@133
   696
                enc_status_data.suggestion = pEp.Properties.Resources.RatingUndefinedSuggestion;
vb@133
   697
                enc_status_data.title = pEp.Properties.Resources.RatingUndefinedTitle;
vb@133
   698
                enc_status_data.background = Color.Gainsboro;
vb@133
   699
                enc_status_data.foreground = Color.Black;
vb@133
   700
                enc_status_data.button_text = pEp.Properties.Resources.RatingUndefinedButtontext;
vb@133
   701
                enc_status_data.strip = null;
vb@133
   702
            }
vb@133
   703
vb@133
   704
            var result = MessageBox.Show(
vb@133
   705
                            enc_status_data.explanation + "\n\n" + enc_status_data.suggestion,
vb@133
   706
                            enc_status_data.title,
vb@133
   707
                            MessageBoxButtons.OK,
vb@133
   708
                            enc_status == _pEp_color.pEp_rating_under_attack
vb@133
   709
                                ? MessageBoxIcon.Stop
vb@133
   710
                                : MessageBoxIcon.Information
vb@133
   711
                        );
vb@133
   712
        }
vb@133
   713
vb@133
   714
        private void timerRefresh_Tick(object sender, EventArgs e)
vb@133
   715
        {
vb@133
   716
            timerRefresh.Interval = 100;
vb@133
   717
            timerRefresh.Enabled = false; // only once
vb@133
   718
vb@133
   719
            if (!ThisAddIn.addIn.enabled)
vb@133
   720
                ThisAddIn.addIn.enable();
vb@133
   721
vb@133
   722
            makePreview();
vb@133
   723
vb@133
   724
            reset_enc_status();
vb@133
   725
        }
vb@133
   726
vb@133
   727
        private void EncryptionStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
   728
        {
vb@133
   729
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
vb@133
   730
            {
vb@133
   731
                CryptableMailItem cmi = mailItem;
vb@133
   732
dean@144
   733
                if (cmi != null && !cmi.IsIncoming && !cmi.IsInSentFolder && cmi.OutgoingColor > _pEp_color.pEp_rating_unencrypted)
vb@133
   734
                {
dean@144
   735
                    if (cmi.SendUnencrypted == CheckState.Indeterminate)
vb@133
   736
                    {
vb@133
   737
                        sendUnencryptedToolStripMenuItem.CheckState = CheckState.Unchecked;
vb@133
   738
                    }
vb@133
   739
                    else {
dean@144
   740
                        sendUnencryptedToolStripMenuItem.CheckState = cmi.SendUnencrypted;
vb@133
   741
                    }
vb@133
   742
vb@133
   743
                    Control _sender = (Control)sender;
vb@133
   744
                    contextMenuEdit.Show(_sender.PointToScreen(e.Location));
vb@133
   745
                }
vb@133
   746
            }
vb@133
   747
        }
vb@133
   748
vb@133
   749
        private void sendUnencryptedToolStripMenuItem_Click(object sender, EventArgs e)
vb@133
   750
        {
vb@133
   751
            sendUnencryptedToolStripMenuItem.Checked = !sendUnencryptedToolStripMenuItem.Checked;
dean@144
   752
            mailItem.SendUnencrypted = sendUnencryptedToolStripMenuItem.CheckState;
vb@133
   753
            timerRefresh.Enabled = true;
vb@133
   754
        }
Dean@151
   755
Dean@151
   756
        /// <summary>
Dean@151
   757
        /// Event handler for when the options picture box is clicked.
Dean@151
   758
        /// This will open the options form for user editing.
Dean@151
   759
        /// </summary>
Dean@151
   760
        private void PictureBoxOptions_Click(object sender, EventArgs e)
Dean@151
   761
        {
Dean@152
   762
            FormOptions frm = new FormOptions();
Dean@154
   763
            frm.StartPosition = FormStartPosition.CenterScreen;
Dean@154
   764
            frm.ShowDialog(this.ParentForm); // Must show as dialog to block code
Dean@151
   765
Dean@154
   766
            if (ThisAddIn.addIn.use_keyserver)
Dean@154
   767
                ThisAddIn.pEp.start_keyserver_lookup();
Dean@154
   768
            else
Dean@154
   769
                ThisAddIn.pEp.stop_keyserver_lookup();
Dean@151
   770
Dean@151
   771
            return;
Dean@151
   772
        }
Dean@151
   773
Dean@151
   774
        /// <summary>
Dean@151
   775
        /// Event handler for when the mouse hovers over the options picture box.
Dean@151
   776
        /// This is a work-around to show a tool tip.
Dean@151
   777
        /// </summary>
Dean@151
   778
        private void PictureBoxOptions_MouseHover(object sender, EventArgs e)
Dean@151
   779
        {
Dean@151
   780
            ToolTip toolTip = new ToolTip();
Dean@151
   781
            toolTip.SetToolTip(this.PictureBoxOptions, "Click to open options");
Dean@151
   782
            return;
Dean@151
   783
        }
vb@133
   784
    }
vb@133
   785
}