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