PEPMessage.cs
author Dean
Thu, 01 Sep 2016 18:10:07 +0200
changeset 1224 51bc4eefa2dd
parent 1223 778f4c240173
child 1232 b22b3146cf77
permissions -rw-r--r--
OUT-101: Initial support for PGP/MIME messages based on outlook_mime_support branch.
Dean@384
     1
using Microsoft.Win32;
Dean@384
     2
using pEpCOMServerAdapterLib;
Dean@384
     3
using System;
Dean@384
     4
using System.Collections.Generic;
Dean@1094
     5
using System.ComponentModel;
Dean@384
     6
using System.Runtime.InteropServices;
Dean@384
     7
using Outlook = Microsoft.Office.Interop.Outlook;
Dean@384
     8
Dean@384
     9
namespace pEp
Dean@384
    10
{
Dean@384
    11
    /// <summary>
Dean@389
    12
    /// Class for a completely in-memory message based on the pEp engine text_message.
Dean@384
    13
    /// </summary>
Dean@1094
    14
    internal class PEPMessage : INotifyPropertyChanged,
Dean@1094
    15
                                Interfaces.ICopy<PEPMessage>,
Dean@1094
    16
                                Interfaces.IReset
Dean@384
    17
    {
Dean@1094
    18
        /// <summary>
Dean@1094
    19
        /// Event raised when a property is changed on a component.
Dean@1094
    20
        /// </summary>
Dean@1094
    21
        public event PropertyChangedEventHandler PropertyChanged;
Dean@1094
    22
Dean@1223
    23
        // pEp named MAPI properties
Dean@1212
    24
        public const string PR_X_ENC_STATUS_NAME           = "X-EncStatus";
Dean@1212
    25
        public const string PR_X_KEY_LIST_NAME             = "X-KeyList";
Dean@1212
    26
        public const string PR_X_PEP_NEVER_UNSECURE_NAME   = "X-pEp-Never-Unsecure";
Dean@1212
    27
        public const string PR_X_PEP_PROTOCOL_VERSION_NAME = "X-pEp-Version";
Dean@780
    28
Dean@1223
    29
        public static readonly MAPIProperty.MAPIProp XPidNameEncStatus          = new MAPIProperty.MAPIProp(PR_X_ENC_STATUS_NAME,           MAPIProperty.PS_INTERNET_HEADERS);
Dean@1223
    30
        public static readonly MAPIProperty.MAPIProp XPidNameKeyList            = new MAPIProperty.MAPIProp(PR_X_KEY_LIST_NAME,             MAPIProperty.PS_INTERNET_HEADERS);
Dean@1223
    31
        public static readonly MAPIProperty.MAPIProp XPidNamePEPNeverUnsecure   = new MAPIProperty.MAPIProp(PR_X_PEP_NEVER_UNSECURE_NAME,   MAPIProperty.PS_INTERNET_HEADERS);
Dean@1223
    32
        public static readonly MAPIProperty.MAPIProp XPidNamePEPProtocolVersion = new MAPIProperty.MAPIProp(PR_X_PEP_PROTOCOL_VERSION_NAME, MAPIProperty.PS_INTERNET_HEADERS);
Dean@1224
    33
Dean@384
    34
        private List<PEPAttachment> _Attachments;
Dean@384
    35
        private List<PEPIdentity>   _BCC;
Dean@384
    36
        private List<PEPIdentity>   _CC;
Dean@1130
    37
        private _pEp_color          _Rating;
Dean@1113
    38
        private string              _ConversationID;
Dean@1113
    39
        private string              _ConversationIndex;
Dean@1113
    40
        private string              _ConversationTopic;
Dean@384
    41
        private _pEp_msg_direction  _Direction;
Dean@1094
    42
        private bool                _ForceUnencrypted;
Dean@384
    43
        private PEPIdentity         _From;
Dean@384
    44
        private string              _ID;
Dean@1094
    45
        private string              _KeyList;
Dean@384
    46
        private List<string>        _Keywords;
Dean@384
    47
        private string              _LongMsg;
Dean@697
    48
        private string              _LongMsgFormattedHTML;
Dean@697
    49
        private string              _LongMsgFormattedRTF;
Dean@1104
    50
        private bool                _NeverUnsecure;
Dean@1212
    51
        private string              _PEPProtocolVersion;
Dean@721
    52
        private DateTime?           _ReceivedOn;
Dean@721
    53
        private DateTime?           _SentOn;
Dean@384
    54
        private string              _ShortMsg;
Dean@384
    55
        private List<PEPIdentity>   _To;
Dean@384
    56
Dean@384
    57
        /**************************************************************
Dean@384
    58
         * 
Dean@384
    59
         * Constructors
Dean@384
    60
         * 
Dean@384
    61
         *************************************************************/
Dean@384
    62
Dean@384
    63
        /// <summary>
Dean@384
    64
        /// Default constructor.
Dean@384
    65
        /// </summary>
Dean@384
    66
        public PEPMessage()
Dean@384
    67
        {
Dean@1094
    68
            this.Reset();
Dean@384
    69
        }
Dean@384
    70
Dean@384
    71
        /**************************************************************
Dean@384
    72
         * 
Dean@384
    73
         * Property Accessors
Dean@384
    74
         * 
Dean@384
    75
         *************************************************************/
Dean@384
    76
Dean@910
    77
        #region Property Accessors
Dean@910
    78
Dean@384
    79
        /// <summary>
Dean@384
    80
        /// Gets the list of attachements for this message.
Dean@1094
    81
        ///          MailItem : Corresponds to property 'Attachments' which contains 'Attachment'
Dean@1094
    82
        /// CryptableMailItem : not exposed
Dean@1094
    83
        ///      text_message : Corresponds to property 'attachments' which contains 'blob'
Dean@384
    84
        /// </summary>
Dean@384
    85
        public List<PEPAttachment> Attachments
Dean@384
    86
        {
Dean@384
    87
            get { return (this._Attachments); }
Dean@384
    88
        }
Dean@384
    89
Dean@384
    90
        /// <summary>
Dean@387
    91
        /// Gets the list of identities to be blind carbon copied on the message.
Dean@1094
    92
        ///          MailItem : Component of property 'Recipients' which contains 'Recipient'
Dean@1094
    93
        /// CryptableMailItem : not exposed directly (part of Recipients)
Dean@1094
    94
        ///      text_message : Corresponds to property 'bcc'
Dean@384
    95
        /// </summary>
Dean@384
    96
        public List<PEPIdentity> BCC
Dean@384
    97
        {
Dean@384
    98
            get { return (this._BCC); }
Dean@384
    99
        }
Dean@384
   100
Dean@384
   101
        /// <summary>
Dean@384
   102
        /// Gets the list of identities to be carbon copied on the message.
Dean@1094
   103
        ///          MailItem : Component of property 'Recipients' which contains 'Recipient'
Dean@1094
   104
        /// CryptableMailItem : not exposed directly (part of Recipients)
Dean@1094
   105
        ///      text_message : Corresponds to property 'cc'
Dean@384
   106
        /// </summary>
Dean@384
   107
        public List<PEPIdentity> CC
Dean@384
   108
        {
Dean@384
   109
            get { return (this._CC); }
Dean@384
   110
        }
Dean@384
   111
Dean@384
   112
        /// <summary>
Dean@1130
   113
        /// Gets or sets the pEp rating of the message.
Dean@1130
   114
        /// This should corresponds primarily with the decryption rating.
Dean@1094
   115
        /// Warning: Since this is stored as a header field, care must be taken not to apply this to a MailItem on an 
Dean@1094
   116
        /// untrusted server or on an outgoing message.
Dean@1094
   117
        ///          MailItem : Corresponds to MAPI Property PR_X_ENC_STATUS (Header field)
Dean@1094
   118
        /// CryptableMailItem : not exposed (processed internally)
Dean@1094
   119
        ///      text_message : Corresponds with the key PR_X_ENC_STATUS_NAME within 'opt_fields' array
Dean@1094
   120
        /// </summary>
Dean@1130
   121
        public _pEp_color Rating
Dean@1094
   122
        {
Dean@1130
   123
            get { return (this._Rating); }
Dean@1094
   124
            set
Dean@1094
   125
            {
Dean@1130
   126
                this._Rating = value;
Dean@1130
   127
                this.RaisePropertyChangedEvent("Rating");
Dean@1094
   128
            }
Dean@1094
   129
        }
Dean@1094
   130
Dean@1094
   131
        /// <summary>
Dean@1113
   132
        /// Gets or sets the conversation ID of the message.
Dean@1113
   133
        /// Outlook should normally manage this itself.
Dean@1113
   134
        ///          MailItem : Corresponds to property 'ConversationID' (Underlying MAPI property PidTagConversationId)
Dean@1113
   135
        /// CryptableMailItem : Corresponds to property 'ConversationID'
Dean@1113
   136
        ///      text_message : not supported
Dean@1113
   137
        /// </summary>
Dean@1113
   138
        public string ConversationID
Dean@1113
   139
        {
Dean@1113
   140
            get { return (this._ConversationID); }
Dean@1113
   141
            set
Dean@1113
   142
            {
Dean@1113
   143
                this._ConversationID = value;
Dean@1113
   144
                this.RaisePropertyChangedEvent("ConversationID");
Dean@1113
   145
            }
Dean@1113
   146
        }
Dean@1113
   147
Dean@1113
   148
        /// <summary>
Dean@1113
   149
        /// Gets or sets the conversation index of the message.
Dean@1113
   150
        /// Outlook should normally manage this itself.
Dean@1113
   151
        ///          MailItem : Corresponds to property 'ConversationIndex' (Underlying MAPI property PidTagConversationIndex)
Dean@1113
   152
        /// CryptableMailItem : Corresponds to property 'ConversationIndex'
Dean@1113
   153
        ///      text_message : not supported
Dean@1113
   154
        /// </summary>
Dean@1113
   155
        public string ConversationIndex
Dean@1113
   156
        {
Dean@1113
   157
            get { return (this._ConversationIndex); }
Dean@1113
   158
            set
Dean@1113
   159
            {
Dean@1113
   160
                this._ConversationIndex = value;
Dean@1113
   161
                this.RaisePropertyChangedEvent("ConversationIndex");
Dean@1113
   162
            }
Dean@1113
   163
        }
Dean@1113
   164
Dean@1113
   165
        /// <summary>
Dean@1113
   166
        /// Gets or sets the conversation index of the message.
Dean@1113
   167
        /// Outlook should normally manage this itself.
Dean@1113
   168
        ///          MailItem : Corresponds to property 'ConversationTopic' (Underlying MAPI property PidTagConversationTopic)
Dean@1113
   169
        /// CryptableMailItem : Corresponds to property 'ConversationTopic'
Dean@1113
   170
        ///      text_message : not supported
Dean@1113
   171
        /// </summary>
Dean@1113
   172
        public string ConversationTopic
Dean@1113
   173
        {
Dean@1113
   174
            get { return (this._ConversationTopic); }
Dean@1113
   175
            set
Dean@1113
   176
            {
Dean@1113
   177
                this._ConversationTopic = value;
Dean@1113
   178
                this.RaisePropertyChangedEvent("ConversationTopic");
Dean@1113
   179
            }
Dean@1113
   180
        }
Dean@1113
   181
Dean@1113
   182
        /// <summary>
Dean@721
   183
        /// Gets the date and time when the message was either sent or received.
Dean@1094
   184
        /// This corresponds with the SentOn and ReceivedOn properties defined separately.
Dean@691
   185
        /// </summary>
Dean@691
   186
        public DateTime? DateTimeSentOrReceived
Dean@691
   187
        {
Dean@721
   188
            get
Dean@721
   189
            {
Dean@721
   190
                if (this._Direction == _pEp_msg_direction.pEp_dir_incoming)
Dean@721
   191
                {
Dean@721
   192
                    return (this._ReceivedOn);
Dean@721
   193
                }
Dean@721
   194
                else
Dean@721
   195
                {
Dean@721
   196
                    return (this._SentOn);
Dean@721
   197
                }
Dean@721
   198
            }
Dean@691
   199
        }
Dean@691
   200
Dean@691
   201
        /// <summary>
Dean@384
   202
        /// Gets or sets the direction (incoming or outgoing) of the message.
Dean@1094
   203
        ///          MailItem : not supported (calculated from various properties)
Dean@1094
   204
        /// CryptableMailItem : Corresponds 1-to-1 with property 'IsIncoming'
Dean@1094
   205
        ///      text_message : Corresponds to property 'dir'
Dean@384
   206
        /// </summary>
Dean@384
   207
        public _pEp_msg_direction Direction
Dean@384
   208
        {
Dean@384
   209
            get { return (this._Direction); }
Dean@1094
   210
            set
Dean@1094
   211
            {
Dean@1094
   212
                this._Direction = value;
Dean@1094
   213
                this.RaisePropertyChangedEvent("Direction");
Dean@1094
   214
            }
Dean@1094
   215
        }
Dean@1094
   216
Dean@1094
   217
        /// <summary>
Dean@1094
   218
        /// Gets or sets the force unencrypted status.
Dean@1094
   219
        ///          MailItem : Corresponds to UserProperty USER_PROPERTY_KEY_FORCE_UNENCRYPTED
Dean@1094
   220
        /// CryptableMailItem : Corresponds to property 'ForceUnencryptedBool'
Dean@1094
   221
        ///      text_message : not supported
Dean@1094
   222
        /// </summary>
Dean@1094
   223
        public bool ForceUnencrypted
Dean@1094
   224
        {
Dean@1094
   225
            get { return (this._ForceUnencrypted); }
Dean@1094
   226
            set
Dean@1094
   227
            {
Dean@1094
   228
                this._ForceUnencrypted = value;
Dean@1094
   229
                this.RaisePropertyChangedEvent("ForceUnencrypted");
Dean@1094
   230
            }
Dean@384
   231
        }
Dean@384
   232
Dean@384
   233
        /// <summary>
Dean@384
   234
        /// Gets or sets the from identity of the message.
Dean@384
   235
        /// Warning: this value can be null.
Dean@1094
   236
        ///          MailItem : not supported (calculated from various properties)
Dean@1094
   237
        /// CryptableMailItem : Corresponds to property 'From'
Dean@1094
   238
        ///      text_message : Corresponds to property 'from'
Dean@384
   239
        /// </summary>
Dean@384
   240
        public PEPIdentity From
Dean@384
   241
        {
Dean@384
   242
            get { return (this._From); }
Dean@1094
   243
            set
Dean@1094
   244
            {
Dean@1094
   245
                this._From = value;
Dean@1094
   246
                this.RaisePropertyChangedEvent("From");
Dean@1094
   247
            }
Dean@384
   248
        }
Dean@384
   249
Dean@384
   250
        /// <summary>
Dean@384
   251
        /// Gets or sets the ID of the message.
Dean@384
   252
        /// Warning: this value can be null.
Dean@1095
   253
        ///          MailItem : Corresponds to MAPI property PidTagInternetMessageId
Dean@1095
   254
        /// CryptableMailItem : not supported
Dean@1094
   255
        ///      text_message : Corresponds to property 'id'
Dean@384
   256
        /// </summary>
Dean@384
   257
        public string ID
Dean@384
   258
        {
Dean@384
   259
            get { return (this._ID); }
Dean@1094
   260
            set
Dean@1094
   261
            {
Dean@1094
   262
                this._ID = value;
Dean@1094
   263
                this.RaisePropertyChangedEvent("ID");
Dean@1094
   264
            }
Dean@384
   265
        }
Dean@384
   266
Dean@384
   267
        /// <summary>
Dean@927
   268
        /// Gets whether this message is encrypted.
Dean@927
   269
        /// This will forward the call to the static method of the same purpose.
Dean@927
   270
        /// </summary>
Dean@927
   271
        public bool IsEncrypted
Dean@927
   272
        {
Dean@927
   273
            get { return (PEPMessage.GetIsEncrypted(this)); }
Dean@927
   274
        }
Dean@927
   275
Dean@927
   276
        /// <summary>
Dean@1224
   277
        /// Gets whether this message is secured using PGP/MIME format.
Dean@1224
   278
        /// This will forward the call to the static method of the same purpose.
Dean@1224
   279
        /// </summary>
Dean@1224
   280
        public bool IsPGPMIMEEncrypted
Dean@1224
   281
        {
Dean@1224
   282
            get { return (PEPMessage.GetIsPGPMIMEEncrypted(this)); }
Dean@1224
   283
        }
Dean@1224
   284
Dean@1224
   285
        /// <summary>
Dean@1094
   286
        /// Gets or sets the list of keys associated with this message.
Dean@1094
   287
        /// Commonly this contains the list of decryption keys.
Dean@1094
   288
        /// Warning: Since this is stored as a header field, care must be taken not to apply this to a MailItem on an 
Dean@1094
   289
        /// untrusted server or on an outgoing message.
Dean@1094
   290
        ///          MailItem : Corresponds to MAPI Property PR_X_KEY_LIST (Header field)
Dean@1118
   291
        /// CryptableMailItem : not supported
Dean@1094
   292
        ///      text_message : Corresponds with the key PR_X_KEY_LIST_NAME within 'opt_fields' array. 
Dean@1094
   293
        ///                     This is also an out parameter of the decrypt function.
Dean@1094
   294
        /// </summary>
Dean@1094
   295
        public string KeyList
Dean@1094
   296
        {
Dean@1094
   297
            get { return (this._KeyList); }
Dean@1094
   298
            set
Dean@1094
   299
            {
Dean@1094
   300
                this._KeyList = value;
Dean@1094
   301
                this.RaisePropertyChangedEvent("KeyList");
Dean@1094
   302
            }
Dean@1094
   303
        }
Dean@1094
   304
Dean@1094
   305
        /// <summary>
Dean@384
   306
        /// Gets the list of keywords associated with the message.
Dean@1094
   307
        ///          MailItem : Corresponds to property 'Categories'
Dean@1094
   308
        /// CryptableMailItem : not supported
Dean@1094
   309
        ///      text_message : Corresponds to property 'keywords'
Dean@384
   310
        /// </summary>
Dean@384
   311
        public List<string> Keywords
Dean@384
   312
        {
Dean@384
   313
            get { return (this._Keywords); }
Dean@384
   314
        }
Dean@384
   315
Dean@384
   316
        /// <summary>
Dean@1094
   317
        /// Gets or sets the plain text long-form (body) of the message.
Dean@384
   318
        /// Warning: this value can be null.
Dean@1094
   319
        ///          MailItem : Corresponds to property 'Body' (also BodyFormat)
Dean@1094
   320
        /// CryptableMailItem : not exposed (processed internally)
Dean@1094
   321
        ///      text_message : Corresponds to property 'longmsg'
Dean@384
   322
        /// </summary>
Dean@384
   323
        public string LongMsg
Dean@384
   324
        {
Dean@384
   325
            get { return (this._LongMsg); }
Dean@1094
   326
            set
Dean@1094
   327
            {
Dean@1094
   328
                this._LongMsg = value;
Dean@1094
   329
                this.RaisePropertyChangedEvent("LongMsg");
Dean@1094
   330
            }
Dean@384
   331
        }
Dean@384
   332
Dean@384
   333
        /// <summary>
Dean@697
   334
        /// Gets or sets the HTML formatted long-form (body) of the message.
Dean@384
   335
        /// Warning: this value can be null.
Dean@1094
   336
        ///          MailItem : Corresponds to property 'HTMLBody' (also BodyFormat)
Dean@1094
   337
        /// CryptableMailItem : not exposed (processed internally)
Dean@1094
   338
        ///      text_message : Corresponds to property 'longmsg_formatted'
Dean@384
   339
        /// </summary>
Dean@697
   340
        public string LongMsgFormattedHTML
Dean@384
   341
        {
Dean@697
   342
            get { return (this._LongMsgFormattedHTML); }
Dean@1094
   343
            set
Dean@1094
   344
            {
Dean@1094
   345
                this._LongMsgFormattedHTML = value;
Dean@1094
   346
                this.RaisePropertyChangedEvent("LongMsgFormattedHTML");
Dean@1094
   347
            }
Dean@697
   348
        }
Dean@697
   349
Dean@697
   350
        /// <summary>
Dean@697
   351
        /// Gets or sets the RTF formatted long-form (body) of the message.
Dean@1094
   352
        /// Warning: this value can be null, it is only supported when creating from a MailItem. It is
Dean@1094
   353
        /// not applied to a MailItem during .ApplyTo as it's unsupported by the engine.
Dean@1094
   354
        ///          MailItem : Corresponds to property 'RTFBody' (also BodyFormat)
Dean@1094
   355
        /// CryptableMailItem : not exposed (processed internally)
Dean@1094
   356
        ///      text_message : not supported
Dean@697
   357
        /// </summary>
Dean@697
   358
        public string LongMsgFormattedRTF
Dean@697
   359
        {
Dean@697
   360
            get { return (this._LongMsgFormattedRTF); }
Dean@1094
   361
            set
Dean@1094
   362
            {
Dean@1094
   363
                this._LongMsgFormattedRTF = value;
Dean@1094
   364
                this.RaisePropertyChangedEvent("LongMsgFormattedRTF");
Dean@1094
   365
            }
Dean@384
   366
        }
Dean@384
   367
Dean@384
   368
        /// <summary>
Dean@1104
   369
        /// Gets or sets whether this message is marked to never be unsecure.
Dean@1104
   370
        /// Note: While this is stored as a header field, it is OK to apply to any MailItem and outgoing messages.
Dean@1104
   371
        /// It's intended function is to flag a message to keep it encrypted.
Dean@1104
   372
        ///          MailItem : Corresponds to MAPI Property PR_X_PEP_NEVER_UNSECURE (Header field)
Dean@1104
   373
        /// CryptableMailItem : Corresponds to property 'NeverUnsecure'
Dean@1104
   374
        ///      text_message : not supported
Dean@1104
   375
        /// </summary>
Dean@1104
   376
        public bool NeverUnsecure
Dean@1104
   377
        {
Dean@1104
   378
            get { return (this._NeverUnsecure); }
Dean@1104
   379
            set
Dean@1104
   380
            {
Dean@1104
   381
                this._NeverUnsecure = value;
Dean@1104
   382
                this.RaisePropertyChangedEvent("NeverUnsecure");
Dean@1104
   383
            }
Dean@1104
   384
        }
Dean@1104
   385
Dean@1104
   386
        /// <summary>
Dean@1212
   387
        /// Gets or sets the pEp protocol (or 'format') version of this message.
Dean@1212
   388
        /// This is not to be confused with pEp engine version which can be different.
Dean@1094
   389
        /// This is commonly set after decryption.
Dean@1094
   390
        /// Note: While this is stored as a header field, it is OK to apply to any MailItem and outgoing messages.
Dean@1094
   391
        /// It's intended function is just to show the version of pEp last used to process the message.
Dean@1212
   392
        ///          MailItem : Corresponds to MAPI Property PR_X_PEP_PROTOCOL_VERSION (Header field)
Dean@1094
   393
        /// CryptableMailItem : not supported
Dean@1212
   394
        ///      text_message : Corresponds with the key PR_X_PEP_PROTOCOL_VERSION_NAME within 'opt_fields' array. 
Dean@384
   395
        /// </summary>
Dean@1212
   396
        public string PEPProtocolVersion
Dean@384
   397
        {
Dean@1212
   398
            get { return (this._PEPProtocolVersion); }
Dean@1094
   399
            set
Dean@1094
   400
            {
Dean@1212
   401
                this._PEPProtocolVersion = value;
Dean@1212
   402
                this.RaisePropertyChangedEvent("PEPProtocolVersion");
Dean@1094
   403
            }
Dean@384
   404
        }
Dean@384
   405
Dean@384
   406
        /// <summary>
Dean@721
   407
        /// Gets or sets the date and time when the message was received.
Dean@1094
   408
        ///          MailItem : Corresponds to property 'ReceivedTime'
Dean@1094
   409
        /// CryptableMailItem : not supported
Dean@1094
   410
        ///      text_message : Corresponds to property 'recv'
Dean@721
   411
        /// </summary>
Dean@721
   412
        public DateTime? ReceivedOn
Dean@721
   413
        {
Dean@721
   414
            get { return (this._ReceivedOn); }
Dean@1094
   415
            set
Dean@1094
   416
            {
Dean@1094
   417
                this._ReceivedOn = value;
Dean@1094
   418
                this.RaisePropertyChangedEvent("ReceivedOn");
Dean@1094
   419
            }
Dean@721
   420
        }
Dean@721
   421
Dean@721
   422
        /// <summary>
Dean@721
   423
        /// Gets or sets the date and time when the message was sent.
Dean@1094
   424
        ///          MailItem : Corresponds to property 'SentOn'
Dean@1094
   425
        /// CryptableMailItem : not supported
Dean@1094
   426
        ///      text_message : Corresponds to property 'sent'
Dean@721
   427
        /// </summary>
Dean@721
   428
        public DateTime? SentOn
Dean@721
   429
        {
Dean@721
   430
            get { return (this._SentOn); }
Dean@1094
   431
            set
Dean@1094
   432
            {
Dean@1094
   433
                this._SentOn = value;
Dean@1094
   434
                this.RaisePropertyChangedEvent("SentOn");
Dean@1094
   435
            }
Dean@721
   436
        }
Dean@721
   437
Dean@721
   438
        /// <summary>
Dean@384
   439
        /// Gets or sets the short-form (subject) of the message.
Dean@384
   440
        /// Warning: this value can be null.
Dean@1094
   441
        ///          MailItem : Corresponds to property 'Subject'
Dean@1094
   442
        /// CryptableMailItem : not supported
Dean@1094
   443
        ///      text_message : Corresponds to property 'shortmsg'
Dean@384
   444
        /// </summary>
Dean@384
   445
        public string ShortMsg
Dean@384
   446
        {
Dean@384
   447
            get { return (this._ShortMsg); }
Dean@1094
   448
            set
Dean@1094
   449
            {
Dean@1094
   450
                this._ShortMsg = value;
Dean@1094
   451
                this.RaisePropertyChangedEvent("ShortMsg");
Dean@1094
   452
            }
Dean@384
   453
        }
Dean@384
   454
Dean@384
   455
        /// <summary>
Dean@384
   456
        /// Gets the list of identities to receive the message.
Dean@1094
   457
        ///          MailItem : Component of property 'Recipients' which contains 'Recipient'
Dean@1094
   458
        /// CryptableMailItem : not exposed directly (part of Recipients)
Dean@1094
   459
        ///      text_message : Corresponds to property 'to'
Dean@384
   460
        /// </summary>
Dean@384
   461
        public List<PEPIdentity> To
Dean@384
   462
        {
Dean@384
   463
            get { return (this._To); }
Dean@384
   464
        }
Dean@384
   465
Dean@625
   466
        /// <summary>
Dean@625
   467
        /// Gets the total number of all recipients in the message (BCC, CC &amp; To).
Dean@625
   468
        /// </summary>
Dean@625
   469
        public int RecipientCount
Dean@625
   470
        {
Dean@625
   471
            get { return (this._BCC.Count + this._CC.Count + this._To.Count); }
Dean@625
   472
        }
Dean@625
   473
Dean@625
   474
        /// <summary>
Dean@625
   475
        /// Gets a list of all recipients in the message (BCC, CC &amp; To).
Dean@625
   476
        /// Each recipient in the returned list is a copy.
Dean@625
   477
        /// </summary>
Dean@625
   478
        public PEPIdentity[] Recipients
Dean@625
   479
        {
Dean@625
   480
            get
Dean@625
   481
            {
Dean@625
   482
                List<PEPIdentity> recipients = new List<PEPIdentity>();
Dean@625
   483
Dean@625
   484
                // BCC
Dean@625
   485
                for (int i = 0; i < this._BCC.Count; i++)
Dean@625
   486
                {
Dean@625
   487
                    recipients.Add(this._BCC[i].Copy());
Dean@625
   488
                }
Dean@625
   489
Dean@625
   490
                // CC
Dean@625
   491
                for (int i = 0; i < this._CC.Count; i++)
Dean@625
   492
                {
Dean@625
   493
                    recipients.Add(this._CC[i].Copy());
Dean@625
   494
                }
Dean@625
   495
Dean@625
   496
                // To
Dean@625
   497
                for (int i = 0; i < this._To.Count; i++)
Dean@625
   498
                {
Dean@625
   499
                    recipients.Add(this._To[i].Copy());
Dean@625
   500
                }
Dean@625
   501
Dean@625
   502
                return (recipients.ToArray());
Dean@625
   503
            }
Dean@625
   504
        }
Dean@625
   505
Dean@910
   506
        #endregion
Dean@910
   507
Dean@384
   508
        /**************************************************************
Dean@384
   509
         * 
Dean@384
   510
         * Methods
Dean@384
   511
         * 
Dean@384
   512
         *************************************************************/
Dean@384
   513
Dean@384
   514
        /// <summary>
Dean@1094
   515
        /// Raises the property changed event, if possible, with the given arguments.
Dean@1094
   516
        /// </summary>
Dean@1094
   517
        /// <param name="propertyName">The name of the property that changed.</param>
Dean@1094
   518
        private void RaisePropertyChangedEvent(string propertyName)
Dean@1094
   519
        {
Dean@1094
   520
            if (this.PropertyChanged != null)
Dean@1094
   521
            {
Dean@1094
   522
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Dean@1094
   523
            }
Dean@1094
   524
            return;
Dean@1094
   525
        }
Dean@1094
   526
Dean@1094
   527
        /// <summary>
Dean@1094
   528
        /// Returns this pEp message as a new pEp engine text_message struct.
Dean@910
   529
        /// Warning: Any identity members (groups) are lost, call FlattenAllRecipientIdentities() before this method.
Dean@384
   530
        /// </summary>
Dean@1094
   531
        /// <param name="alwaysAddOptFields">Whether to always add optional fields (default is false). When false, 
Dean@1094
   532
        /// optional fields will not be added if they are undefined/null/empty in the PEPMessage. This is to prevent 
Dean@1094
   533
        /// against adding optional fields when they have never been set.
Dean@1094
   534
        /// </param>
Dean@384
   535
        /// <returns>A pEp engine identity.</returns>
Dean@1094
   536
        public text_message ToCOMType(bool alwaysAddOptFields = false)
Dean@384
   537
        {
Dean@1095
   538
            long recvSec;
Dean@1095
   539
            long sentSec;
Dean@1095
   540
            TimeSpan span;
Dean@384
   541
            List<blob> attachments = new List<blob>();
Dean@384
   542
            List<pEp_identity_s> bcc = new List<pEp_identity_s>();
Dean@384
   543
            List<pEp_identity_s> cc = new List<pEp_identity_s>();
Dean@384
   544
            List<pEp_identity_s> to = new List<pEp_identity_s>();
Dean@1094
   545
            List<opt_field> optionalFields = new List<opt_field>();
Dean@1094
   546
            opt_field field;
Dean@384
   547
            text_message result = new text_message();
Dean@384
   548
Dean@388
   549
            // Convert attachments
Dean@384
   550
            for (int i = 0; i < this._Attachments.Count; i++)
Dean@384
   551
            {
Dean@384
   552
                attachments.Add(this._Attachments[i].ToCOMType());
Dean@384
   553
            }
Dean@398
   554
Dean@388
   555
            // Convert BCC
Dean@384
   556
            for (int i = 0; i < this._BCC.Count; i++)
Dean@384
   557
            {
Dean@384
   558
                bcc.Add(this._BCC[i].ToCOMType());
Dean@384
   559
            }
Dean@398
   560
Dean@388
   561
            // Convert CC
Dean@384
   562
            for (int i = 0; i < this._CC.Count; i++)
Dean@384
   563
            {
Dean@384
   564
                cc.Add(this._CC[i].ToCOMType());
Dean@384
   565
            }
Dean@398
   566
Dean@388
   567
            // Convert To
Dean@388
   568
            for (int i = 0; i < this._To.Count; i++)
Dean@388
   569
            {
Dean@388
   570
                to.Add(this._To[i].ToCOMType());
Dean@388
   571
            }
Dean@388
   572
Dean@1094
   573
            // Create optional fields
Dean@1094
   574
            if ((alwaysAddOptFields) ||
Dean@1130
   575
                (this._Rating != _pEp_color.pEp_rating_undefined))
Dean@1094
   576
            {
Dean@1094
   577
                field = new opt_field();
Dean@1094
   578
                field.name = PEPMessage.PR_X_ENC_STATUS_NAME;
Dean@1130
   579
                field.value = this._Rating.ToString().Remove(0, 11); // Remove the 'pEp_rating_' text
Dean@1094
   580
                optionalFields.Add(field);
Dean@1094
   581
            }
Dean@1094
   582
Dean@1094
   583
            if ((alwaysAddOptFields) ||
Dean@1094
   584
                (string.IsNullOrEmpty(this._KeyList) == false))
Dean@1094
   585
            {
Dean@1094
   586
                field = new opt_field();
Dean@1094
   587
                field.name = PEPMessage.PR_X_KEY_LIST_NAME;
Dean@1094
   588
                field.value = this._KeyList;
Dean@1094
   589
                optionalFields.Add(field);
Dean@1094
   590
            }
Dean@1094
   591
Dean@1094
   592
            if ((alwaysAddOptFields) ||
Dean@1104
   593
                (this._NeverUnsecure == true))
Dean@1095
   594
            {
Dean@1095
   595
                field = new opt_field();
Dean@1104
   596
                field.name = PEPMessage.PR_X_PEP_NEVER_UNSECURE_NAME;
Dean@1095
   597
                field.value = "1";
Dean@1095
   598
                optionalFields.Add(field);
Dean@1095
   599
            }
Dean@1095
   600
Dean@1095
   601
            if ((alwaysAddOptFields) ||
Dean@1212
   602
                (string.IsNullOrEmpty(this._PEPProtocolVersion) == false))
Dean@1094
   603
            {
Dean@1094
   604
                field = new opt_field();
Dean@1212
   605
                field.name = PEPMessage.PR_X_PEP_PROTOCOL_VERSION_NAME;
Dean@1212
   606
                field.value = this._PEPProtocolVersion;
Dean@1094
   607
                optionalFields.Add(field);
Dean@1094
   608
            }
Dean@1094
   609
Dean@1095
   610
            // ReceivedOn
Dean@1095
   611
            recvSec = -1;
Dean@1095
   612
            if (this._ReceivedOn != null)
Dean@1095
   613
            {
Dean@1095
   614
                span = (DateTime)this._ReceivedOn - new DateTime(1970, 1, 1);
Dean@1095
   615
                recvSec = (long)span.TotalSeconds;
Dean@1095
   616
            }
Dean@1095
   617
Dean@1095
   618
            // SentOn
Dean@1095
   619
            sentSec = -1;
Dean@1095
   620
            if (this._SentOn != null)
Dean@1095
   621
            {
Dean@1095
   622
                span = (DateTime)this._SentOn - new DateTime(1970, 1, 1);
Dean@1095
   623
                sentSec = (long)span.TotalSeconds;
Dean@1095
   624
            }
Dean@1095
   625
Dean@1095
   626
            /* Note: Skip the following properties which are not supported in text_message
Dean@1113
   627
             *   • ConversationID
Dean@1113
   628
             *   • ConversationIndex
Dean@1113
   629
             *   • ConversationTopic
Dean@1094
   630
             *   • ForceUnencrypted
Dean@1095
   631
             *   • LongMsgFormattedRTF
Dean@1095
   632
             * 
Dean@1095
   633
             * Also not the following are handled as optional fields
Dean@1130
   634
             *   • Rating
Dean@1104
   635
             *   • NeverUnsecure
Dean@1095
   636
             *   • KeyList
Dean@1212
   637
             *   • PEPProtocolVersion
Dean@1095
   638
             * 
Dean@1095
   639
             * This also skips a number of text_message properties currently unsupported in PEPMessage.
Dean@697
   640
             */
Dean@388
   641
            result.attachments = attachments.ToArray();
Dean@388
   642
            result.bcc = bcc.ToArray();
Dean@384
   643
            result.cc = cc.ToArray();
Dean@384
   644
            result.dir = this._Direction;
Dean@1095
   645
            result.from = (this._From != null ? this._From.ToCOMType() : result.from);
Dean@384
   646
            result.id = this._ID;
Dean@384
   647
            result.keywords = this._Keywords.ToArray();
Dean@384
   648
            result.longmsg = this._LongMsg;
Dean@697
   649
            result.longmsg_formatted = this._LongMsgFormattedHTML;
Dean@1094
   650
            result.opt_fields = optionalFields.ToArray();
Dean@1095
   651
            result.recv = ((recvSec >= 0) ? recvSec : result.recv);
Dean@1095
   652
            result.sent = ((sentSec >= 0) ? sentSec : result.sent);
Dean@384
   653
            result.shortmsg = this._ShortMsg;
Dean@384
   654
            result.to = to.ToArray();
Dean@384
   655
Dean@384
   656
            return (result);
Dean@384
   657
        }
Dean@384
   658
Dean@384
   659
        /// <summary>
Dean@389
   660
        /// Gets a deep copy of the object and all it's data.
Dean@389
   661
        /// </summary>
Dean@389
   662
        /// <returns>The deep copy of the object.</returns>
Dean@389
   663
        public PEPMessage Copy()
Dean@389
   664
        {
Dean@390
   665
            return (this.Copy(false));
Dean@390
   666
        }
Dean@390
   667
Dean@390
   668
        /// <summary>
Dean@390
   669
        /// Gets a copy of the PEPMessage with or without data.
Dean@390
   670
        /// </summary>
Dean@586
   671
        /// <param name="createWithoutContent">Whether to include content such as text body, attachments 
Dean@586
   672
        /// and optional properties.</param>
Dean@390
   673
        /// <returns>The copy of the PEPMessage.</returns>
Dean@390
   674
        public PEPMessage Copy(bool createWithoutContent = false)
Dean@390
   675
        {
Dean@389
   676
            PEPMessage copy = new PEPMessage();
Dean@389
   677
Dean@389
   678
            // Attachments
Dean@389
   679
            copy.Attachments.Clear();
Dean@390
   680
            if (createWithoutContent == false)
Dean@389
   681
            {
Dean@390
   682
                for (int i = 0; i < this._Attachments.Count; i++)
Dean@390
   683
                {
Dean@390
   684
                    copy.Attachments.Add(this._Attachments[i].Copy());
Dean@390
   685
                }
Dean@389
   686
            }
Dean@389
   687
Dean@389
   688
            // BCC
Dean@389
   689
            copy.BCC.Clear();
Dean@389
   690
            for (int i = 0; i < this._BCC.Count; i++)
Dean@389
   691
            {
Dean@389
   692
                copy.BCC.Add(this._BCC[i].Copy());
Dean@389
   693
            }
Dean@389
   694
Dean@389
   695
            // CC
Dean@389
   696
            copy.CC.Clear();
Dean@389
   697
            for (int i = 0; i < this._CC.Count; i++)
Dean@389
   698
            {
Dean@389
   699
                copy.CC.Add(this._CC[i].Copy());
Dean@389
   700
            }
Dean@389
   701
Dean@1130
   702
            copy.Rating = this._Rating;
Dean@1113
   703
            copy.ConversationID = this._ConversationID;
Dean@1113
   704
            copy.ConversationIndex = this.ConversationIndex;
Dean@1113
   705
            copy.ConversationTopic = this.ConversationTopic;
Dean@389
   706
            copy.Direction = this._Direction;
Dean@1094
   707
            copy.ForceUnencrypted = this._ForceUnencrypted;
Dean@389
   708
            copy.From = (this._From == null ? null : this._From.Copy());
Dean@389
   709
            copy.ID = this._ID;
Dean@1094
   710
            copy.KeyList = this._KeyList;
Dean@389
   711
Dean@389
   712
            // Keywords
Dean@389
   713
            copy.Keywords.Clear();
Dean@389
   714
            for (int i = 0; i < this._Keywords.Count; i++)
Dean@389
   715
            {
Dean@389
   716
                copy.Keywords.Add(this._Keywords[i]);
Dean@389
   717
            }
Dean@398
   718
Dean@390
   719
            // Body
Dean@390
   720
            if (createWithoutContent == false)
Dean@390
   721
            {
Dean@390
   722
                copy.LongMsg = this._LongMsg;
Dean@697
   723
                copy.LongMsgFormattedHTML = this._LongMsgFormattedHTML;
Dean@697
   724
                copy.LongMsgFormattedRTF = this._LongMsgFormattedRTF;
Dean@390
   725
            }
Dean@390
   726
            else
Dean@390
   727
            {
Dean@390
   728
                copy.LongMsg = null;
Dean@697
   729
                copy.LongMsgFormattedHTML = null;
Dean@697
   730
                copy.LongMsgFormattedRTF = null;
Dean@390
   731
            }
Dean@389
   732
Dean@1104
   733
            copy.NeverUnsecure = this._NeverUnsecure;
Dean@1212
   734
            copy.PEPProtocolVersion = this._PEPProtocolVersion;
Dean@604
   735
Dean@721
   736
            // ReceivedOn
Dean@721
   737
            if (this._ReceivedOn != null)
Dean@721
   738
            {
Dean@721
   739
                copy.ReceivedOn = new DateTime(((DateTime)this._ReceivedOn).Ticks);
Dean@721
   740
            }
Dean@721
   741
            else
Dean@721
   742
            {
Dean@721
   743
                copy.ReceivedOn = null;
Dean@721
   744
            }
Dean@721
   745
Dean@721
   746
            // SentOn
Dean@721
   747
            if (this._SentOn != null)
Dean@721
   748
            {
Dean@721
   749
                copy.SentOn = new DateTime(((DateTime)this._SentOn).Ticks);
Dean@721
   750
            }
Dean@721
   751
            else
Dean@721
   752
            {
Dean@721
   753
                copy.SentOn = null;
Dean@721
   754
            }
Dean@721
   755
Dean@389
   756
            copy.ShortMsg = this._ShortMsg;
Dean@389
   757
Dean@389
   758
            // To
Dean@389
   759
            copy.To.Clear();
Dean@389
   760
            for (int i = 0; i < this._To.Count; i++)
Dean@389
   761
            {
Dean@389
   762
                copy.To.Add(this._To[i].Copy());
Dean@389
   763
            }
Dean@389
   764
Dean@389
   765
            return (copy);
Dean@389
   766
        }
Dean@389
   767
Dean@389
   768
        /// <summary>
Dean@1094
   769
        /// Resets the object to it's default state/values.
Dean@1094
   770
        /// </summary>
Dean@1094
   771
        public void Reset()
Dean@1094
   772
        {
Dean@1094
   773
            this._Attachments = new List<PEPAttachment>();
Dean@1094
   774
            this._BCC = new List<PEPIdentity>();
Dean@1094
   775
            this._CC = new List<PEPIdentity>();
Dean@1130
   776
            this._Rating = _pEp_color.pEp_rating_undefined;
Dean@1113
   777
            this._ConversationID = null;
Dean@1113
   778
            this._ConversationIndex = null;
Dean@1113
   779
            this._ConversationTopic = null;
Dean@1094
   780
            this._Direction = _pEp_msg_direction.pEp_dir_incoming;
Dean@1094
   781
            this._ForceUnencrypted = false;
Dean@1094
   782
            this._From = null;
Dean@1094
   783
            this._ID = null;
Dean@1094
   784
            this._KeyList = null;
Dean@1094
   785
            this._Keywords = new List<string>();
Dean@1094
   786
            this._LongMsg = null;
Dean@1094
   787
            this._LongMsgFormattedHTML = null;
Dean@1094
   788
            this._LongMsgFormattedRTF = null;
Dean@1104
   789
            this._NeverUnsecure = false;
Dean@1212
   790
            this._PEPProtocolVersion = null;
Dean@1094
   791
            this._ReceivedOn = null;
Dean@1094
   792
            this._SentOn = null;
Dean@1094
   793
            this._ShortMsg = null;
Dean@1094
   794
            this._To = new List<PEPIdentity>();
Dean@1094
   795
Dean@1094
   796
            this.RaisePropertyChangedEvent("Attachments");
Dean@1094
   797
            this.RaisePropertyChangedEvent("BCC");
Dean@1094
   798
            this.RaisePropertyChangedEvent("CC");
Dean@1130
   799
            this.RaisePropertyChangedEvent("Rating");
Dean@1113
   800
            this.RaisePropertyChangedEvent("ConversationID");
Dean@1113
   801
            this.RaisePropertyChangedEvent("ConversationIndex");
Dean@1113
   802
            this.RaisePropertyChangedEvent("ConversationTopic");
Dean@1094
   803
            this.RaisePropertyChangedEvent("Direction");
Dean@1094
   804
            this.RaisePropertyChangedEvent("ForceUnencrypted");
Dean@1094
   805
            this.RaisePropertyChangedEvent("From");
Dean@1094
   806
            this.RaisePropertyChangedEvent("ID");
Dean@1094
   807
            this.RaisePropertyChangedEvent("KeyList");
Dean@1094
   808
            this.RaisePropertyChangedEvent("Keywords");
Dean@1094
   809
            this.RaisePropertyChangedEvent("LongMsg");
Dean@1094
   810
            this.RaisePropertyChangedEvent("LongMsgFormattedHTML");
Dean@1094
   811
            this.RaisePropertyChangedEvent("LongMsgFormattedRTF");
Dean@1104
   812
            this.RaisePropertyChangedEvent("NeverUnsecure");
Dean@1212
   813
            this.RaisePropertyChangedEvent("PEPProtocolVersion");
Dean@1094
   814
            this.RaisePropertyChangedEvent("ReceivedOn");
Dean@1094
   815
            this.RaisePropertyChangedEvent("SentOn");
Dean@1094
   816
            this.RaisePropertyChangedEvent("ShortMsg");
Dean@1094
   817
            this.RaisePropertyChangedEvent("To");
Dean@1094
   818
Dean@1094
   819
            return;
Dean@1094
   820
        }
Dean@1094
   821
Dean@1094
   822
        /// <summary>
Dean@1095
   823
        /// Copies main properties that are unsupported by the engine from the given message into this message.
Dean@1113
   824
        /// This is commonly used to restore information that would otherwise be lost in conversions to/from 
Dean@1095
   825
        /// text_message during engine processing.
Dean@1095
   826
        /// Key properties that should be set separately for encryption concerns (such as LongMsgFormattedRTF) are ignored.
Dean@1095
   827
        /// </summary>
Dean@1095
   828
        /// <param name="msg">The message to copy over properties from.</param>
Dean@1095
   829
        public void SetNonEnginePropertiesFrom(PEPMessage msg)
Dean@1095
   830
        {
Dean@1095
   831
            /* Include the following properties:
Dean@1113
   832
             *   • ConversationID
Dean@1113
   833
             *   • ConversationIndex
Dean@1113
   834
             *   • ConversationTopic
Dean@1095
   835
             *   • ForceUnencrypted
Dean@1104
   836
             *   • NeverUnsecure
Dean@1095
   837
             * 
Dean@1095
   838
             * Also note:
Dean@1130
   839
             *              Rating: This is handled by the decrypt function of the engine only. It should not be
Dean@1095
   840
             *                      added back separately so is skipped.
Dean@1095
   841
             *             KeyList: This is handled by the decrypt function of the engine only. It should not be
Dean@1095
   842
             *                      added back separately so is skipped.
Dean@1095
   843
             * LongMsgFormattedRTF: This is completely unsupported by the engine but otherwise should be encrypted.
Dean@1095
   844
             *                      Due to this, it's completely skipped.
Dean@1212
   845
             *  PEPProtocolVersion: This is handled in both the decrypt and encrypt functions of the engine.
Dean@1095
   846
             *                      It should almost always be set within the text_message there isn't needed to add back.
Dean@1095
   847
             */
Dean@1095
   848
Dean@1095
   849
            if (msg != null)
Dean@1095
   850
            {
Dean@1113
   851
                this._ConversationID = msg.ConversationID;
Dean@1113
   852
                this._ConversationIndex = msg.ConversationIndex;
Dean@1113
   853
                this._ConversationTopic = msg.ConversationTopic;
Dean@1095
   854
                this._ForceUnencrypted = msg.ForceUnencrypted;
Dean@1104
   855
                this._NeverUnsecure = msg.NeverUnsecure;
Dean@1095
   856
Dean@1113
   857
                this.RaisePropertyChangedEvent("ConversationID");
Dean@1113
   858
                this.RaisePropertyChangedEvent("ConversationIndex");
Dean@1113
   859
                this.RaisePropertyChangedEvent("ConversationTopic");
Dean@1095
   860
                this.RaisePropertyChangedEvent("ForceUnencrypted");
Dean@1104
   861
                this.RaisePropertyChangedEvent("NeverUnsecure");
Dean@1095
   862
            }
Dean@1095
   863
Dean@1095
   864
            return;
Dean@1095
   865
        }
Dean@1095
   866
Dean@1095
   867
        /// <summary>
Dean@384
   868
        /// Applies this pEp message's data to the given Outlook item.
Dean@384
   869
        /// </summary>
Dean@384
   870
        /// <param name="omi">The Outlook mail item to apply this pEp message's data to.</param>
Dean@1094
   871
        /// <param name="includeInternalHeaderFields">Whether to include internal header fields (Stored as MAPI properites) such as 
Dean@1130
   872
        /// Rating and KeyList. Warning: Never apply these to outgoing messages -- used only for mirrors or internal MailItems.
Dean@1212
   873
        /// Note that some header fields will be applied regardless such as NeverUnsecure or PEPProtocolVersion. 
Dean@1094
   874
        /// That is because these are not considered internal only and don't contain sensitive information.</param>
Dean@910
   875
        /// <returns>The status of the method.</returns>
Dean@910
   876
        public Globals.ReturnStatus ApplyTo(Outlook.MailItem omi,
Dean@1094
   877
                                            bool includeInternalHeaderFields = false)
Dean@384
   878
        {
Dean@1113
   879
            byte[] bytes;
Dean@543
   880
            Outlook.Attachments attachments = null;
Dean@543
   881
            Outlook.Recipient newRecipient = null;
Dean@543
   882
            Outlook.Recipients recipients = null;
Dean@543
   883
            Outlook.Account currAccount = null;
Dean@543
   884
            Outlook.Account sendUsingAccount = null;
Dean@543
   885
            Outlook.Accounts accounts = null;
Thomas@1119
   886
            Outlook.Recipient fromRecipient = null;
Dean@910
   887
            Globals.ReturnStatus status = Globals.ReturnStatus.Success;
Thomas@1119
   888
            bool fromRecipientRemoved = false;
Dean@408
   889
Dean@543
   890
            try
Dean@408
   891
            {
Dean@1094
   892
                // Remove all existing recipients
Dean@543
   893
                recipients = omi.Recipients;
Dean@543
   894
                while (recipients.Count > 0)
Dean@543
   895
                {
Dean@543
   896
                    recipients.Remove(1);
Dean@543
   897
                }
Dean@543
   898
Dean@543
   899
                // Set recipients
Dean@543
   900
                for (int i = 0; i < this._BCC.Count; i++)
Dean@543
   901
                {
Dean@795
   902
                    if (string.IsNullOrWhiteSpace(this._BCC[i].Address) == false)
Dean@543
   903
                    {
Dean@795
   904
                        // Add by address
Dean@543
   905
                        newRecipient = recipients.Add(this._BCC[i].Address);
Dean@543
   906
                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
Dean@543
   907
Dean@543
   908
                        Marshal.ReleaseComObject(newRecipient);
Dean@543
   909
                        newRecipient = null;
Dean@543
   910
                    }
Dean@795
   911
                    else if (string.IsNullOrWhiteSpace(this._BCC[i].Username) == false)
Dean@795
   912
                    {
Dean@795
   913
                        // Add by username (required for distribution lists)
Dean@795
   914
                        newRecipient = recipients.Add(this._BCC[i].Username);
Dean@795
   915
                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
Dean@795
   916
Dean@795
   917
                        Marshal.ReleaseComObject(newRecipient);
Dean@795
   918
                        newRecipient = null;
Dean@795
   919
                    }
Dean@543
   920
                }
Dean@543
   921
Dean@543
   922
                for (int i = 0; i < this._CC.Count; i++)
Dean@543
   923
                {
Dean@795
   924
                    if (string.IsNullOrWhiteSpace(this._CC[i].Address) == false)
Dean@543
   925
                    {
Dean@795
   926
                        // Add by address
Dean@543
   927
                        newRecipient = recipients.Add(this._CC[i].Address);
Dean@543
   928
                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olCC;
Dean@543
   929
Dean@543
   930
                        Marshal.ReleaseComObject(newRecipient);
Dean@543
   931
                        newRecipient = null;
Dean@543
   932
                    }
Dean@795
   933
                    else if (string.IsNullOrWhiteSpace(this._CC[i].Username) == false)
Dean@795
   934
                    {
Dean@795
   935
                        // Add by username (required for distribution lists)
Dean@795
   936
                        newRecipient = recipients.Add(this._CC[i].Username);
Dean@795
   937
                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olCC;
Dean@795
   938
Dean@795
   939
                        Marshal.ReleaseComObject(newRecipient);
Dean@795
   940
                        newRecipient = null;
Dean@795
   941
                    }
Dean@543
   942
                }
Dean@543
   943
Dean@543
   944
                for (int i = 0; i < this._To.Count; i++)
Dean@543
   945
                {
Dean@795
   946
                    if (string.IsNullOrWhiteSpace(this._To[i].Address) == false)
Dean@543
   947
                    {
Dean@795
   948
                        // Add by address
Dean@543
   949
                        newRecipient = recipients.Add(this._To[i].Address);
Dean@543
   950
                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
Dean@543
   951
Dean@543
   952
                        Marshal.ReleaseComObject(newRecipient);
Dean@543
   953
                        newRecipient = null;
Dean@543
   954
                    }
Dean@795
   955
                    else if (string.IsNullOrWhiteSpace(this._To[i].Username) == false)
Dean@795
   956
                    {
Dean@795
   957
                        // Add by username (required for distribution lists)
Dean@795
   958
                        newRecipient = recipients.Add(this._To[i].Username);
Dean@795
   959
                        newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
Dean@795
   960
Dean@795
   961
                        Marshal.ReleaseComObject(newRecipient);
Dean@795
   962
                        newRecipient = null;
Dean@795
   963
                    }
Dean@543
   964
                }
Dean@543
   965
Thomas@1119
   966
                /* Add the pEp From identity as its own recipient. This will be removed later.
Dean@1135
   967
                 * However, here it simplifies getting the Sender/From AddressEntry, which will be set later.
Dean@1135
   968
                 * 
Dean@1135
   969
                 * Note: Outlook will not allow the Sender to be set for received mail items.
Dean@1135
   970
                 * If you try to set the sender in this situation, it will always throw an error.
Dean@1135
   971
                 * To mitigate this, the Sender should only be set to the MailItem if one is not already existing that already
Dean@1135
   972
                 * matches the Sender we would otherwise try to set. This is considered good enough as determining
Dean@1135
   973
                 * if a mail item is received is not completely reliable.
Thomas@1119
   974
                 */
Dean@1141
   975
                if ((this._From != null) &&
Dean@1141
   976
                    ((omi.Sender == null) ||
Dean@1141
   977
                     ((omi.Sender != null) &&
Dean@1141
   978
                      (this._From != null) &&
Dean@1141
   979
                      (this._From.EqualsByAddress(omi.Sender.Address) == false))))
Thomas@1119
   980
                {
Thomas@1131
   981
                    if (string.IsNullOrWhiteSpace(this._From.Address) == false)
Thomas@1131
   982
                    {
Thomas@1131
   983
                        // Add by address
Thomas@1131
   984
                        fromRecipient = recipients.Add(this._From.Address);
Thomas@1131
   985
                        fromRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
Thomas@1131
   986
                    }
Thomas@1131
   987
                    else if (string.IsNullOrWhiteSpace(this._From.Username) == false)
Thomas@1131
   988
                    {
Thomas@1131
   989
                        // Add by username (required for distribution lists)
Thomas@1131
   990
                        fromRecipient = recipients.Add(this._From.Username);
Thomas@1131
   991
                        fromRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
Thomas@1131
   992
                    }
Thomas@1119
   993
                }
Thomas@1119
   994
Dean@910
   995
                try
Dean@910
   996
                {
Dean@910
   997
                    recipients.ResolveAll();
Dean@910
   998
                }
Dean@910
   999
                catch { }
Dean@543
  1000
Thomas@1119
  1001
                /* Set sender account
Dean@543
  1002
                 * Note that if fails, will be empty which eventually will use the default send account
Dean@543
  1003
                 * If the send using account is already populated, this cannot be re-set.
Dean@543
  1004
                 * So far this doesn't appear to be an issue as it occurs when applying unencrypted data to a mirror.
Dean@543
  1005
                 * However, the mirror SendUsingAccount is already correct at this point and doesn't need to be set.
Dean@543
  1006
                 */
Dean@543
  1007
                sendUsingAccount = omi.SendUsingAccount;
Dean@543
  1008
Dean@543
  1009
                if ((this._From != null) &&
Dean@543
  1010
                    (this._From.Address != null) &&
Dean@543
  1011
                    (sendUsingAccount == null))
Dean@543
  1012
                {
Dean@543
  1013
                    accounts = Globals.ThisAddIn.Application.Session.Accounts;
Dean@543
  1014
Dean@543
  1015
                    // Note: Index starts at 1
Dean@543
  1016
                    for (int i = 1; i <= accounts.Count; i++)
Dean@543
  1017
                    {
Dean@543
  1018
                        currAccount = accounts[i];
Dean@543
  1019
Dean@543
  1020
                        if ((currAccount.SmtpAddress != null) &&
Dean@543
  1021
                            (currAccount.SmtpAddress.ToUpper() == this._From.Address.ToUpper()))
Dean@543
  1022
                        {
Dean@543
  1023
                            /* Try to set the SendUsingAccount
Dean@543
  1024
                             * This will fail if the mail item is already marked as sent or the SendUsingAccount is not null, etc...
Dean@564
  1025
                             * If it fails, Outlook will in the end just use the default account.
Dean@564
  1026
                             * This property should ideally be set before a mail item is saved.
Dean@543
  1027
                             */
Dean@543
  1028
                            try
Dean@543
  1029
                            {
Dean@564
  1030
                                omi.SendUsingAccount = currAccount;
Dean@543
  1031
                            }
Dean@543
  1032
                            catch { }
Dean@543
  1033
Dean@543
  1034
                            Marshal.ReleaseComObject(currAccount);
Dean@543
  1035
                            currAccount = null;
Dean@543
  1036
Dean@543
  1037
                            break;
Dean@543
  1038
                        }
Dean@543
  1039
Dean@543
  1040
                        Marshal.ReleaseComObject(currAccount);
Dean@543
  1041
                        currAccount = null;
Dean@543
  1042
                    }
Dean@543
  1043
                }
Dean@543
  1044
Thomas@1119
  1045
                /* Set Sender
Thomas@1119
  1046
                 * Outlook takes the Sender property to display the respective name in the Sent folder.
Thomas@1119
  1047
                 * This Sender property is based on the name, the email address and the EntryID MAPI properties. As the EntryID can't be accessed
Thomas@1119
  1048
                 * directly with MAPI methods, we have to set these properties indirectly through the Sender.
Thomas@1119
  1049
                 */
Thomas@1119
  1050
                if (fromRecipient != null)
Thomas@1119
  1051
                {
Thomas@1119
  1052
                    try
Thomas@1119
  1053
                    {
Thomas@1119
  1054
                        omi.Sender = fromRecipient.AddressEntry;
Thomas@1119
  1055
                    }
Thomas@1119
  1056
                    catch
Thomas@1119
  1057
                    {
Thomas@1119
  1058
                        Globals.Log("PEPMessage.ApplyTo: Failed to set the Sender.");
Thomas@1119
  1059
                    }
Thomas@1119
  1060
Thomas@1119
  1061
                    //Remove the From recipient from the Recipients collection by searching for a match by EntryID
Thomas@1119
  1062
                    for (int i = 1; i <= recipients.Count; i++)
Thomas@1119
  1063
                    {
Thomas@1119
  1064
                        newRecipient = recipients[i];
Thomas@1119
  1065
                        if (newRecipient != null)
Thomas@1119
  1066
                        {
Thomas@1119
  1067
                            if ((string.IsNullOrEmpty(newRecipient.EntryID) == false) &&
Thomas@1119
  1068
                                (string.IsNullOrEmpty(fromRecipient.EntryID) == false) &&
Thomas@1119
  1069
                                (newRecipient.EntryID == fromRecipient.EntryID))
Thomas@1119
  1070
                            {
Thomas@1119
  1071
                                try
Thomas@1119
  1072
                                {
Thomas@1119
  1073
                                    recipients.Remove(i);
Thomas@1119
  1074
                                    fromRecipientRemoved = true;
Thomas@1119
  1075
                                }
Thomas@1119
  1076
                                catch
Thomas@1119
  1077
                                {
Thomas@1119
  1078
                                    Globals.Log("PEPMessage.ApplyTo: Error removing 'From' recipient from Recipients collection.");
Thomas@1119
  1079
                                }
Thomas@1119
  1080
                                break;
Thomas@1119
  1081
                            }
Thomas@1119
  1082
Thomas@1119
  1083
                            Marshal.ReleaseComObject(newRecipient);
Thomas@1119
  1084
                            newRecipient = null;
Thomas@1119
  1085
                        }
Thomas@1119
  1086
                    }
Thomas@1119
  1087
Thomas@1119
  1088
                    //Fallback solution in case there is no EntryID match
Thomas@1119
  1089
                    if (fromRecipientRemoved == false)
Thomas@1119
  1090
                    {
Thomas@1119
  1091
                        try
Thomas@1119
  1092
                        {
Thomas@1119
  1093
                            recipients.Remove(recipients.Count);
Thomas@1119
  1094
                            Globals.Log("PEPMessage.ApplyTo: No EntryID match for 'From' recipient in Recipients collection. Removed using fallback solution.");
Thomas@1119
  1095
                        }
Thomas@1119
  1096
                        catch
Thomas@1119
  1097
                        {
Thomas@1119
  1098
                            Globals.Log("PEPMessage.ApplyTo: No EntryID match for 'From' recipient in Recipients collection. Fallback solution also failed.");
Thomas@1119
  1099
                        }
Thomas@1119
  1100
                    }
Dean@1132
  1101
Thomas@1119
  1102
                    // Set Sender's name and email properties via MAPIProperty (won't be created automatically when assigníng Sender)
Thomas@1119
  1103
                    try
Thomas@1119
  1104
                    {
Thomas@1119
  1105
                        MAPIHelper.SetProperty(omi, MAPIProperty.PidTagSenderName, this._From.Username);
Thomas@1119
  1106
                        MAPIHelper.SetProperty(omi, MAPIProperty.PidTagSenderEmailAddress, this._From.Address);
Thomas@1119
  1107
                    }
Thomas@1119
  1108
                    catch (Exception ex)
Thomas@1119
  1109
                    {
Thomas@1119
  1110
                        Globals.Log("PEPMessage.ApplyTo: Unable to set sender's name or email address via MAPIProperty. " + ex.Message);
Thomas@1119
  1111
                    }
Dean@1135
  1112
Dean@1135
  1113
                    Marshal.ReleaseComObject(fromRecipient);
Dean@1135
  1114
                    fromRecipient = null;
Thomas@1119
  1115
                }
Thomas@1119
  1116
Dean@1056
  1117
                /* Set the encoding to UTF-8
Dean@1056
  1118
                 * See: https://msdn.microsoft.com/en-us/library/office/ff860730.aspx
Dean@1058
  1119
                 * All PEPMessages should be UTF especially those coming from the engine.
Dean@1058
  1120
                 * 
Dean@1058
  1121
                 * It is important that the encoding is set BEFORE the body and other text of the 
Dean@1058
  1122
                 * mail item. This is necessary to avoid character encoding issues where
Dean@1058
  1123
                 * Outlook will not respect the UTF encoding of strings set to the body/subject/etc.
Dean@1058
  1124
                 * It will instead try to use the strings with the encoding of the codepage.
Dean@1056
  1125
                 */
Dean@1056
  1126
                try
Dean@1056
  1127
                {
Dean@1056
  1128
                    MAPIHelper.SetProperty(omi, MAPIProperty.PidTagInternetCodepage, 65001);
Dean@1056
  1129
                    MAPIHelper.SetProperty(omi, MAPIProperty.PidTagMessageCodepage, 65001);
Dean@1056
  1130
                }
Dean@1056
  1131
                catch
Dean@1056
  1132
                {
Dean@1056
  1133
                    Globals.Log("PEPMessage.ApplyTo: Failed to set UTF-8 encoding.");
Dean@1056
  1134
                }
Dean@1056
  1135
Dean@543
  1136
                // Set the subject
Dean@543
  1137
                omi.Subject = this._ShortMsg;
Dean@543
  1138
Dean@1224
  1139
                /* Set the body
Dean@1224
  1140
                 * PGP/MIME format must be handled specially.
Dean@1224
  1141
                 * In addition, RTF format is ignored. Only HTML is used the same as the pEp engine.
Dean@1224
  1142
                 */
Dean@1224
  1143
                if (this.IsPGPMIMEEncrypted)
Dean@1224
  1144
                {
Dean@1224
  1145
                    // Note: Must not set the body as this will break the MIME structure
Dean@1224
  1146
                    MAPIHelper.SetProperty(omi, MAPIProperty.PidTagMessageClass, MAPIPropertyValue.PidTagMessageClassSMIMEMultipartSigned);
Dean@1224
  1147
                }
Dean@1224
  1148
                else if (string.IsNullOrWhiteSpace(this._LongMsgFormattedHTML))
Dean@543
  1149
                {
Dean@543
  1150
                    omi.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
Dean@543
  1151
                    omi.Body = this._LongMsg;
Dean@543
  1152
                }
Dean@543
  1153
                else
Dean@543
  1154
                {
Dean@543
  1155
                    omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
Dean@697
  1156
                    omi.HTMLBody = this._LongMsgFormattedHTML;
Dean@543
  1157
                }
Dean@543
  1158
Dean@543
  1159
                // Remove any previous attachments
Dean@543
  1160
                attachments = omi.Attachments;
Dean@543
  1161
                while (attachments.Count > 0)
Dean@543
  1162
                {
Dean@543
  1163
                    attachments.Remove(1);
Dean@543
  1164
                }
Dean@543
  1165
Dean@543
  1166
                // Add new attachments
Dean@1224
  1167
                if (this.IsPGPMIMEEncrypted)
Dean@543
  1168
                {
Dean@1224
  1169
                    for (int i = 0; i < this._Attachments.Count; i++)
Dean@1224
  1170
                    {
Dean@1224
  1171
                        this._Attachments[i].AddTo(attachments,
Dean@1224
  1172
                                                   ("attachment" + i.ToString()),
Dean@1224
  1173
                                                   MAPIPropertyValue.PidTagAttachTagMIME);
Dean@1224
  1174
                    }
Dean@1224
  1175
                }
Dean@1224
  1176
                else
Dean@1224
  1177
                {
Dean@1224
  1178
                    for (int i = 0; i < this._Attachments.Count; i++)
Dean@1224
  1179
                    {
Dean@1224
  1180
                        this._Attachments[i].AddTo(attachments,
Dean@1224
  1181
                                                   ("attachment" + i.ToString()));
Dean@1224
  1182
Dean@1224
  1183
                    }
Dean@543
  1184
                }
Dean@543
  1185
Dean@1095
  1186
                // ID
Dean@1224
  1187
                try
Dean@1224
  1188
                {
Dean@1224
  1189
                    MAPIHelper.SetProperty(omi, MAPIProperty.PidTagInternetMessageId, this._ID);
Dean@1224
  1190
                }
Dean@1224
  1191
                catch { }
Dean@1095
  1192
Dean@1113
  1193
                // Conversation information
Dean@1113
  1194
                try
Dean@1113
  1195
                {
Dean@1113
  1196
                    /* Note: PidTagConversationId cannot be set even through the MAPI accessor.
Dean@1113
  1197
                     * This is by design since this property is computed automatically from other properties.
Dean@1113
  1198
                     * See: https://msdn.microsoft.com/en-us/library/ee204279.aspx
Dean@1113
  1199
                     */
Dean@1217
  1200
                    bytes = MAPIHelper.StringToPtypBinary(this._ConversationIndex);
Dean@1217
  1201
                    if ((bytes != null) &&
Dean@1217
  1202
                        (bytes.Length > 0))
Dean@1113
  1203
                    {
Dean@1113
  1204
                        MAPIHelper.SetProperty(omi, MAPIProperty.PidTagConversationIndex, bytes);
Dean@1113
  1205
                    }
Dean@1113
  1206
Dean@1113
  1207
                    MAPIHelper.SetProperty(omi, MAPIProperty.PidTagConversationIndexTracking, false);
Dean@1113
  1208
                    MAPIHelper.SetProperty(omi, MAPIProperty.PidTagConversationTopic, this._ConversationTopic);
Dean@1113
  1209
                }
Dean@1113
  1210
                catch (Exception ex1)
Dean@1113
  1211
                {
Dean@1113
  1212
                    Globals.Log("PEPMessage.ApplyTo: Failure setting conversation information. " + ex1.ToString());
Dean@1113
  1213
                }
Dean@1113
  1214
Dean@1094
  1215
                /* Add internal properties
Dean@1154
  1216
                 * Note: there is no need to check for defaults as it's all handled within the SetInterpretedProperty method.
Dean@1154
  1217
                 * Also, .Save() must never be called here as the .ApplyTo() method can be used on MailItems that need to change properties before save.
Dean@1094
  1218
                 */
Dean@1094
  1219
                if (includeInternalHeaderFields)
Dean@543
  1220
                {
Dean@1130
  1221
                    // Rating, ignore return status
Dean@1094
  1222
                    CryptableMailItem.SetInterpretedProperty(omi,
Dean@1223
  1223
                                                             PEPMessage.XPidNameEncStatus.DASLName,
Dean@1130
  1224
                                                             this._Rating);
Dean@1094
  1225
Dean@1094
  1226
                    // KeyList, ignore return status
Dean@1094
  1227
                    CryptableMailItem.SetInterpretedProperty(omi,
Dean@1223
  1228
                                                             PEPMessage.XPidNameKeyList.DASLName,
Dean@1094
  1229
                                                             this._KeyList);
Dean@543
  1230
                }
Dean@1094
  1231
Dean@1104
  1232
                // NeverUnsecure, ignore return status
Dean@1094
  1233
                CryptableMailItem.SetInterpretedProperty(omi,
Dean@1223
  1234
                                                         PEPMessage.XPidNamePEPNeverUnsecure.DASLName,
Dean@1104
  1235
                                                         this._NeverUnsecure);
Dean@1094
  1236
Dean@1212
  1237
                // PEPProtocolVersion, ignore return status
Dean@1094
  1238
                CryptableMailItem.SetInterpretedProperty(omi,
Dean@1223
  1239
                                                         PEPMessage.XPidNamePEPProtocolVersion.DASLName,
Dean@1212
  1240
                                                         this._PEPProtocolVersion);
Dean@408
  1241
            }
Dean@545
  1242
            catch (Exception ex)
Dean@545
  1243
            {
Dean@910
  1244
                status = Globals.ReturnStatus.Failure;
Dean@910
  1245
                Globals.Log("PEPMessage.ApplyTo: Failure occured, " + ex.ToString());
Dean@545
  1246
            }
Dean@543
  1247
            finally
Dean@543
  1248
            {
Dean@543
  1249
                if (attachments != null)
Dean@466
  1250
                {
Dean@543
  1251
                    Marshal.ReleaseComObject(attachments);
Dean@543
  1252
                    attachments = null;
Dean@543
  1253
                }
Dean@408
  1254
Dean@543
  1255
                if (newRecipient != null)
Dean@543
  1256
                {
Dean@466
  1257
                    Marshal.ReleaseComObject(newRecipient);
Dean@543
  1258
                    newRecipient = null;
Dean@543
  1259
                }
Dean@543
  1260
Dean@543
  1261
                if (recipients != null)
Dean@543
  1262
                {
Dean@543
  1263
                    Marshal.ReleaseComObject(recipients);
Dean@543
  1264
                    recipients = null;
Dean@543
  1265
                }
Dean@543
  1266
Dean@543
  1267
                if (currAccount != null)
Dean@543
  1268
                {
Dean@543
  1269
                    Marshal.ReleaseComObject(currAccount);
Dean@543
  1270
                    currAccount = null;
Dean@543
  1271
                }
Dean@543
  1272
Dean@543
  1273
                if (sendUsingAccount != null)
Dean@543
  1274
                {
Dean@543
  1275
                    Marshal.ReleaseComObject(sendUsingAccount);
Dean@543
  1276
                    sendUsingAccount = null;
Dean@543
  1277
                }
Dean@543
  1278
Dean@543
  1279
                if (accounts != null)
Dean@543
  1280
                {
Dean@543
  1281
                    Marshal.ReleaseComObject(accounts);
Dean@543
  1282
                    accounts = null;
Dean@543
  1283
                }
Thomas@1119
  1284
Thomas@1119
  1285
                if (fromRecipient != null)
Thomas@1119
  1286
                {
Thomas@1119
  1287
                    Marshal.ReleaseComObject(fromRecipient);
Thomas@1119
  1288
                    fromRecipient = null;
Thomas@1119
  1289
                }
Dean@408
  1290
            }
Dean@408
  1291
Dean@910
  1292
            return (status);
Dean@384
  1293
        }
Dean@384
  1294
Dean@398
  1295
        /// <summary>
Dean@398
  1296
        /// Recursivley converts all "BCC", "CC", and "To" identities into a 'flat' list of any members.
Dean@398
  1297
        /// This will remove groups (hierarchy) and convert a group into it's members.
Dean@398
  1298
        /// </summary>
Dean@398
  1299
        public void FlattenAllRecipientIdentities()
Dean@398
  1300
        {
Dean@398
  1301
            this._BCC = PEPIdentity.ToFlatList(this._BCC);
Dean@398
  1302
            this._CC = PEPIdentity.ToFlatList(this._CC);
Dean@398
  1303
            this._To = PEPIdentity.ToFlatList(this._To);
Dean@398
  1304
Dean@398
  1305
            return;
Dean@398
  1306
        }
Dean@413
  1307
Dean@808
  1308
        /// <summary>
Dean@808
  1309
        /// Processes all content IDs in the image attachments as well as HTML body.
Dean@808
  1310
        /// This will convert the CID's to the image filename only.
Dean@808
  1311
        /// </summary>
Dean@808
  1312
        public void NormalizeContentIDs()
Dean@808
  1313
        {
Dean@808
  1314
            int currImgTagIndex;
Dean@808
  1315
            int currImgTagStopIndex;
Dean@808
  1316
            int currImgTagCIDIndex;
Dean@808
  1317
            int currImgTagCIDStopIndex;
Dean@920
  1318
            int tempIndex;
Dean@808
  1319
            string currCID;
Dean@808
  1320
            string newCID;
Dean@808
  1321
            string beginning;
Dean@808
  1322
            string ending;
Dean@808
  1323
            string currFileName;
Dean@808
  1324
            string workingHTML;
Dean@808
  1325
            Dictionary<string, string> modifiedCIDs = new Dictionary<string, string>();
Dean@808
  1326
Dean@808
  1327
            // Modify CIDs for all attachments and store modified CIDs in the dictionary
Dean@808
  1328
            for (int i = 0; i < this._Attachments.Count; i++)
Dean@808
  1329
            {
Dean@808
  1330
                currCID = this._Attachments[i].ContentID;
Dean@808
  1331
                currFileName = this._Attachments[i].FileName;
Dean@808
  1332
Dean@808
  1333
                if (string.IsNullOrEmpty(currCID))
Dean@808
  1334
                {
Dean@920
  1335
                    /* Just use the file name (even if file name is null or empty)
Dean@920
  1336
                     * It's doesn't matter as it shouldn't be replaced in HTML.
Dean@920
  1337
                     * This situation should mean either the attachment isn't an embedded image, or
Dean@920
  1338
                     * the filename is already used as the CID in the HTML.
Dean@920
  1339
                     */
Dean@808
  1340
                    newCID = currFileName;
Dean@808
  1341
                }
Dean@808
  1342
                else
Dean@808
  1343
                {
Dean@808
  1344
                    if (string.IsNullOrEmpty(currFileName))
Dean@808
  1345
                    {
Dean@808
  1346
                        // A name must exist for HTML so create one
Dean@808
  1347
                        newCID = "attachment" + i.ToString();
Dean@808
  1348
                    }
Dean@808
  1349
                    else
Dean@808
  1350
                    {
Dean@843
  1351
                        newCID = currFileName;
Dean@808
  1352
                    }
Dean@808
  1353
Dean@808
  1354
                    modifiedCIDs.Add(currCID, newCID);
Dean@808
  1355
                }
Dean@808
  1356
Dean@808
  1357
                this._Attachments[i].ContentID = newCID;
Dean@808
  1358
            }
Dean@808
  1359
Dean@808
  1360
            /* Process the HTML body replacing all modified CIDs
Dean@808
  1361
             * Since no HTML document library is provided in standard C#, text operations are done.
Dean@808
  1362
             * This should work ok, but there are some potential issue with malformed HTML that might render correctly.
Dean@808
  1363
             * Only " is supported instead of ' as well.
Dean@808
  1364
             * This shouldn't be an issue since the method is primarily run on mail items generated internally.
Dean@808
  1365
             */
Dean@808
  1366
            workingHTML = this._LongMsgFormattedHTML;
Dean@808
  1367
            if (string.IsNullOrEmpty(workingHTML) == false)
Dean@808
  1368
            {
Dean@920
  1369
                tempIndex = workingHTML.IndexOf("<img", 0);
Dean@920
  1370
                currImgTagIndex = (tempIndex > -1 ? (tempIndex + 4) : -1); // MUST start index after "<img"
Dean@808
  1371
Dean@808
  1372
                while (currImgTagIndex > -1)
Dean@808
  1373
                {
Dean@920
  1374
                    try
Dean@920
  1375
                    {
Dean@920
  1376
                        tempIndex = workingHTML.IndexOf("src=\"cid:", currImgTagIndex);
Dean@920
  1377
                        currImgTagCIDIndex = (tempIndex > -1 ? (tempIndex + 9) : -1);
Dean@920
  1378
                        currImgTagCIDStopIndex = workingHTML.IndexOf("\"", currImgTagCIDIndex);
Dean@920
  1379
                        currImgTagStopIndex = workingHTML.IndexOf(">", currImgTagIndex);
Dean@920
  1380
                    }
Dean@920
  1381
                    catch
Dean@920
  1382
                    {
Dean@920
  1383
                        Globals.Log("NormalizeContentIDs: Incorrect index detected when calculating CID position, skipping current img.");
Dean@920
  1384
Dean@920
  1385
                        // Likely System.ArgumentOutOfRangeException from incorrect index
Dean@920
  1386
                        // Just invalidate all locations and will not change this <img>
Dean@920
  1387
                        currImgTagCIDIndex = -1;
Dean@920
  1388
                        currImgTagCIDStopIndex = -1;
Dean@920
  1389
                        currImgTagStopIndex = -1;
Dean@920
  1390
                    }
Dean@843
  1391
Dean@808
  1392
                    // Validate relative index positions
Dean@808
  1393
                    if ((currImgTagCIDIndex < currImgTagStopIndex) &&
Dean@808
  1394
                        (currImgTagCIDStopIndex < currImgTagStopIndex) &&
Dean@808
  1395
                        ((currImgTagCIDStopIndex - currImgTagCIDIndex) >= 1))
Dean@808
  1396
                    {
Dean@808
  1397
                        // Split the HTML at the CID and modify if necessary
Dean@920
  1398
                        try
Dean@920
  1399
                        {
Dean@920
  1400
                            beginning = workingHTML.Substring(0, currImgTagCIDIndex);
Dean@920
  1401
                            ending = workingHTML.Substring(currImgTagCIDStopIndex);
Dean@920
  1402
                            currCID = workingHTML.Substring(currImgTagCIDIndex, (currImgTagCIDStopIndex - currImgTagCIDIndex));
Dean@920
  1403
                        }
Dean@920
  1404
                        catch
Dean@920
  1405
                        {
Dean@920
  1406
                            Globals.Log("NormalizeContentIDs: Error splitting HTML at CID, skipping current img.");
Dean@920
  1407
Dean@920
  1408
                            beginning = null;
Dean@920
  1409
                            ending = null;
Dean@920
  1410
                            currCID = null;
Dean@920
  1411
                        }
Dean@808
  1412
Dean@808
  1413
                        // Lookup the new CID
Dean@808
  1414
                        newCID = null;
Dean@808
  1415
                        try
Dean@808
  1416
                        {
Dean@920
  1417
                            if (currCID != null)
Dean@920
  1418
                            {
Dean@920
  1419
                                newCID = modifiedCIDs[currCID];
Dean@920
  1420
                            }
Dean@808
  1421
                        }
Dean@808
  1422
                        catch
Dean@808
  1423
                        {
Dean@808
  1424
                            newCID = null;
Dean@808
  1425
                        }
Dean@808
  1426
Dean@808
  1427
                        // Replace
Dean@920
  1428
                        if ((beginning != null) &&
Dean@920
  1429
                            (string.IsNullOrEmpty(newCID) == false) &&
Dean@920
  1430
                            (ending != null))
Dean@808
  1431
                        {
Dean@808
  1432
                            workingHTML = beginning + newCID + ending;
Dean@808
  1433
                        }
Dean@808
  1434
                    }
Dean@808
  1435
Dean@920
  1436
                    try
Dean@920
  1437
                    {
Dean@920
  1438
                        tempIndex = workingHTML.IndexOf("<img", currImgTagIndex);
Dean@920
  1439
                        currImgTagIndex = (tempIndex > -1 ? (tempIndex + 4) : -1); // MUST start index after "<img"
Dean@920
  1440
                    }
Dean@920
  1441
                    catch
Dean@920
  1442
                    {
Dean@920
  1443
                        Globals.Log("NormalizeContentIDs: Incorrect index detected, stopping calculation.");
Dean@920
  1444
Dean@920
  1445
                        // Likely System.ArgumentOutOfRangeException from incorrect index
Dean@920
  1446
                        // Stop processing the HTML and use whatever is processed up to this point
Dean@920
  1447
                        currImgTagIndex = -1;
Dean@920
  1448
                    }
Dean@808
  1449
                }
Dean@808
  1450
            }
Dean@808
  1451
            this._LongMsgFormattedHTML = workingHTML;
Dean@808
  1452
Dean@808
  1453
            return;
Dean@808
  1454
        }
Dean@927
  1455
Dean@927
  1456
        /**************************************************************
Dean@927
  1457
         * 
Dean@927
  1458
         * Static Methods
Dean@927
  1459
         * 
Dean@927
  1460
         *************************************************************/
Dean@927
  1461
Dean@927
  1462
        /// <summary>
Dean@927
  1463
        /// Constructs a new message from the given pEp engine text_message.
Dean@927
  1464
        /// The output will never be null.
Dean@927
  1465
        /// </summary>
Dean@927
  1466
        /// <param name="msg">The text_message to construct from.</param>
Dean@927
  1467
        /// <param name="createdMessage">The output newly created message (will never be null).</param>
Dean@927
  1468
        /// <returns>The status of the method.</returns>
markus@954
  1469
        /// <remarks>In some contexts which call this message, a partial conversion of the
markus@954
  1470
        /// <paramref name="text_message"/> is acceptable, non-critical, and better than failure (e. G. displaying
markus@954
  1471
        /// a preview to the user). Thus, this method will always create a <see cref="PEPMessage"/>
markus@954
  1472
        /// which is "best effort", and return the <see cref="Globals.ReturnStatus"/> which can be evaluated
markus@954
  1473
        /// in the more critical contexts. Callers of that message need to be aware of this
markus@954
  1474
        /// and check the result if appropriate.</remarks>
Dean@927
  1475
        public static Globals.ReturnStatus Create(text_message msg,
Dean@927
  1476
                                                  out PEPMessage createdMessage)
Dean@927
  1477
        {
Dean@927
  1478
            Globals.ReturnStatus status = Globals.ReturnStatus.Success;
Dean@927
  1479
            PEPMessage newMessage = new PEPMessage();
Dean@927
  1480
Dean@927
  1481
            try
Dean@927
  1482
            {
Dean@1095
  1483
                /* Note: Skip the following properties which are not supported in the text_message
Dean@1113
  1484
                 *   • ConversationID
Dean@1113
  1485
                 *   • ConversationIndex
Dean@1113
  1486
                 *   • ConversationTopic
Dean@1095
  1487
                 *   • ForceUnencrypted
Dean@1095
  1488
                 *   • LongMsgFormattedRTF
Dean@1095
  1489
                 * 
Dean@1095
  1490
                 * Also not the following are handled as optional fields
Dean@1130
  1491
                 *   • Rating
Dean@1095
  1492
                 *   • KeyList
Dean@1104
  1493
                 *   • NeverUnsecure
Dean@1212
  1494
                 *   • PEPProtocolVersion
Dean@1095
  1495
                 * 
Dean@1095
  1496
                 * This also skips a number of text_message properties currently unsupported in PEPMessage.
Dean@1095
  1497
                 */
Dean@1095
  1498
Dean@927
  1499
                // Attachments
Dean@927
  1500
                newMessage.Attachments.Clear();
Dean@927
  1501
                if (msg.attachments != null)
Dean@927
  1502
                {
Dean@927
  1503
                    for (int i = 0; i < msg.attachments.Length; i++)
Dean@927
  1504
                    {
Dean@927
  1505
                        newMessage.Attachments.Add(new PEPAttachment((blob)msg.attachments.GetValue(i)));
Dean@927
  1506
                    }
Dean@927
  1507
                }
Dean@927
  1508
Dean@927
  1509
                // BCC
Dean@927
  1510
                newMessage.BCC.Clear();
Dean@927
  1511
                if (msg.bcc != null)
Dean@927
  1512
                {
Dean@927
  1513
                    for (int i = 0; i < msg.bcc.Length; i++)
Dean@927
  1514
                    {
Dean@927
  1515
                        newMessage.BCC.Add(new PEPIdentity((pEp_identity_s)msg.bcc.GetValue(i)));
Dean@927
  1516
                    }
Dean@927
  1517
                }
Dean@927
  1518
Dean@927
  1519
                // CC
Dean@927
  1520
                newMessage.CC.Clear();
Dean@927
  1521
                if (msg.cc != null)
Dean@927
  1522
                {
Dean@927
  1523
                    for (int i = 0; i < msg.cc.Length; i++)
Dean@927
  1524
                    {
Dean@927
  1525
                        newMessage.CC.Add(new PEPIdentity((pEp_identity_s)msg.cc.GetValue(i)));
Dean@927
  1526
                    }
Dean@927
  1527
                }
Dean@927
  1528
Dean@927
  1529
                newMessage.Direction = msg.dir;
Dean@927
  1530
                newMessage.From = new PEPIdentity(msg.from);
Dean@927
  1531
                newMessage.ID = msg.id;
Dean@927
  1532
Dean@927
  1533
                // Keywords
Dean@927
  1534
                newMessage.Keywords.Clear();
Dean@927
  1535
                if (msg.keywords != null)
Dean@927
  1536
                {
Dean@927
  1537
                    for (int i = 0; i < msg.keywords.Length; i++)
Dean@927
  1538
                    {
Dean@927
  1539
                        newMessage.Keywords.Add((string)msg.keywords.GetValue(i));
Dean@927
  1540
                    }
Dean@927
  1541
                }
Dean@927
  1542
Dean@927
  1543
                newMessage.LongMsg = msg.longmsg;
Dean@927
  1544
                newMessage.LongMsgFormattedHTML = msg.longmsg_formatted;
Dean@927
  1545
Dean@927
  1546
                // Optional properties
Dean@927
  1547
                if (msg.opt_fields != null)
Dean@927
  1548
                {
Dean@927
  1549
                    for (int i = 0; i < msg.opt_fields.Length; i++)
Dean@927
  1550
                    {
Dean@1094
  1551
                        if (msg.opt_fields[i].name != null)
Dean@1094
  1552
                        {
Dean@1094
  1553
                            switch (msg.opt_fields[i].name)
Dean@1094
  1554
                            {
Dean@1094
  1555
                                case (PEPMessage.PR_X_ENC_STATUS_NAME):
Dean@1095
  1556
                                    {
Dean@1095
  1557
                                        try
Dean@1095
  1558
                                        {
Dean@1095
  1559
                                            // Note: ignore character case when parsing
Dean@1130
  1560
                                            newMessage.Rating = (_pEp_color)Enum.Parse(typeof(_pEp_color), ("pEp_rating_" + msg.opt_fields[i].value), true);
Dean@1095
  1561
                                        }
Dean@1095
  1562
                                        catch
Dean@1095
  1563
                                        {
Dean@1130
  1564
                                            newMessage.Rating = _pEp_color.pEp_rating_undefined;
Dean@1095
  1565
                                        }
Dean@1095
  1566
                                        break;
Dean@1095
  1567
                                    }
Dean@1094
  1568
                                case (PEPMessage.PR_X_KEY_LIST_NAME):
Dean@1095
  1569
                                    {
Dean@1095
  1570
                                        newMessage.KeyList = msg.opt_fields[i].value;
Dean@1095
  1571
                                        break;
Dean@1095
  1572
                                    }
Dean@1104
  1573
                                case (PEPMessage.PR_X_PEP_NEVER_UNSECURE_NAME):
Dean@1095
  1574
                                    {
Dean@1095
  1575
                                        // If it exists it's true, value doesn't matter
Dean@1104
  1576
                                        newMessage.NeverUnsecure = true;
Dean@1095
  1577
                                        break;
Dean@1095
  1578
                                    }
Dean@1212
  1579
                                case (PEPMessage.PR_X_PEP_PROTOCOL_VERSION_NAME):
Dean@1095
  1580
                                    {
Dean@1212
  1581
                                        newMessage.PEPProtocolVersion = msg.opt_fields[i].value;
Dean@1095
  1582
                                        break;
Dean@1095
  1583
                                    }
Dean@1094
  1584
                            }
Dean@1094
  1585
                        }
Dean@927
  1586
                    }
Dean@927
  1587
                }
Dean@927
  1588
Dean@927
  1589
                // ReceivedOn
Dean@927
  1590
                if (msg.recv > 0)
Dean@927
  1591
                {
Dean@927
  1592
                    newMessage.ReceivedOn = new DateTime(1970, 1, 1).AddSeconds(msg.recv);
Dean@927
  1593
                }
Dean@927
  1594
                else
Dean@927
  1595
                {
Dean@927
  1596
                    newMessage.ReceivedOn = null;
Dean@927
  1597
                }
Dean@927
  1598
Dean@927
  1599
                // SentOn
Dean@927
  1600
                if (msg.sent > 0)
Dean@927
  1601
                {
Dean@927
  1602
                    newMessage.SentOn = new DateTime(1970, 1, 1).AddSeconds(msg.sent);
Dean@927
  1603
                }
Dean@927
  1604
                else
Dean@927
  1605
                {
Dean@927
  1606
                    newMessage.SentOn = null;
Dean@927
  1607
                }
Dean@927
  1608
Dean@927
  1609
                newMessage.ShortMsg = msg.shortmsg;
Dean@927
  1610
Dean@927
  1611
                // To
Dean@927
  1612
                newMessage.To.Clear();
Dean@927
  1613
                if (msg.to != null)
Dean@927
  1614
                {
Dean@927
  1615
                    for (int i = 0; i < msg.to.Length; i++)
Dean@927
  1616
                    {
Dean@927
  1617
                        newMessage.To.Add(new PEPIdentity((pEp_identity_s)msg.to.GetValue(i)));
Dean@927
  1618
                    }
Dean@927
  1619
                }
Dean@927
  1620
            }
Dean@927
  1621
            catch (Exception ex)
Dean@927
  1622
            {
Dean@927
  1623
                status = Globals.ReturnStatus.Failure;
Dean@927
  1624
                Globals.Log("PEPMessage.Create: Failure occured, " + ex.ToString());
Dean@927
  1625
            }
Dean@927
  1626
Dean@927
  1627
            createdMessage = newMessage;
Dean@927
  1628
            return (status);
Dean@927
  1629
        }
Dean@927
  1630
Dean@927
  1631
        /// <summary>
Dean@927
  1632
        /// Contructs a new message from the given outlook mail item.
Dean@927
  1633
        /// The output will never be null.
Dean@927
  1634
        /// </summary>
Dean@927
  1635
        /// <param name="omi">The outlook mail item to create the message from.</param>
Dean@927
  1636
        /// <param name="createdMessage">The output newly created message (will never be null).</param>
Dean@927
  1637
        /// <param name="createWithoutContent">Whether to include content such as text body, attachments 
Dean@927
  1638
        /// and optional properties.</param>
Dean@927
  1639
        /// <returns>The status of the method.</returns>
markus@954
  1640
        /// <remarks>In some contexts which call this message, a partial conversion of the
markus@954
  1641
        /// <paramref name="text_message"/> is acceptable, non-critical, and better than failure (e. G. displaying
markus@954
  1642
        /// a preview to the user). Thus, this method will always create a <see cref="PEPMessage"/>
markus@954
  1643
        /// which is "best effort", and return the <see cref="Globals.ReturnStatus"/> which can be evaluated
markus@954
  1644
        /// in the more critical contexts. Callers of that message need to be aware of this
markus@954
  1645
        /// and check the result if appropriate.</remarks>
Dean@927
  1646
        public static Globals.ReturnStatus Create(Outlook.MailItem omi,
Dean@927
  1647
                                                  out PEPMessage createdMessage,
Dean@927
  1648
                                                  bool createWithoutContent = false)
Dean@927
  1649
        {
Dean@927
  1650
            byte[] rtfBody;
Dean@927
  1651
            string delim;
Dean@927
  1652
            string[] keywords;
Dean@1094
  1653
            object propValue;
Dean@927
  1654
            DateTime? receivedOn = null;
Dean@927
  1655
            DateTime? sentOn = null;
Dean@927
  1656
            PEPIdentity ident;
Dean@927
  1657
            Outlook.Attachment currAttachment = null;
Dean@927
  1658
            Outlook.Attachments attachments = null;
Dean@927
  1659
            Outlook.Recipient currRecipient = null;
Dean@927
  1660
            Outlook.Recipients recipients = null;
Dean@927
  1661
            Globals.ReturnStatus status = Globals.ReturnStatus.Success;
Dean@927
  1662
            Globals.ReturnStatus sts;
Dean@927
  1663
            PEPMessage newMessage = new PEPMessage();
Dean@927
  1664
Dean@927
  1665
            try
Dean@927
  1666
            {
Dean@927
  1667
                Globals.LogVerbose("PEPMessage.Create: Creating new PEPMessage from OMI started, calculating recipients.");
Dean@927
  1668
Dean@1095
  1669
                /* Note: Skip the following properties which are not supported in the MailItem
Dean@1095
  1670
                 *   • Direction
Dean@1095
  1671
                 */
Dean@1095
  1672
Dean@927
  1673
                // Calculate recipients
Dean@927
  1674
                newMessage.BCC.Clear();
Dean@927
  1675
                newMessage.CC.Clear();
Dean@927
  1676
                newMessage.To.Clear();
Dean@927
  1677
                recipients = omi.Recipients;
Dean@927
  1678
                for (int i = 1; i <= recipients.Count; i++)
Dean@927
  1679
                {
Dean@927
  1680
                    currRecipient = recipients[i];
Dean@927
  1681
Dean@927
  1682
                    switch ((Outlook.OlMailRecipientType)currRecipient.Type)
Dean@927
  1683
                    {
Dean@927
  1684
                        case Outlook.OlMailRecipientType.olBCC:
Dean@1095
  1685
                            {
Dean@1202
  1686
                                sts = PEPIdentity.Create(currRecipient, out ident);
Dean@1095
  1687
                                if (sts == Globals.ReturnStatus.Success)
Dean@1095
  1688
                                {
Dean@1095
  1689
                                    newMessage.BCC.Add(ident);
Dean@1095
  1690
                                }
Dean@1095
  1691
                                else
Dean@1095
  1692
                                {
Dean@1095
  1693
                                    // Update the status, only the first failure type is recorded
Dean@1095
  1694
                                    if (status == Globals.ReturnStatus.Success)
Dean@1095
  1695
                                    {
Dean@1095
  1696
                                        status = sts;
Dean@1095
  1697
                                    }
Dean@1194
  1698
Dean@1194
  1699
                                    Globals.Log("PEPMessage.Create: Failure creating 'BCC' identity.");
Dean@1095
  1700
                                }
Dean@927
  1701
Dean@1095
  1702
                                break;
Dean@1095
  1703
                            }
Dean@1095
  1704
                        case Outlook.OlMailRecipientType.olCC:
Dean@927
  1705
                            {
Dean@1202
  1706
                                sts = PEPIdentity.Create(currRecipient, out ident);
Dean@1095
  1707
                                if (sts == Globals.ReturnStatus.Success)
Dean@1095
  1708
                                {
Dean@1095
  1709
                                    newMessage.CC.Add(ident);
Dean@1095
  1710
                                }
Dean@1095
  1711
                                else
Dean@1095
  1712
                                {
Dean@1095
  1713
                                    // Update the status, only the first failure type is recorded
Dean@1095
  1714
                                    if (status == Globals.ReturnStatus.Success)
Dean@1095
  1715
                                    {
Dean@1095
  1716
                                        status = sts;
Dean@1095
  1717
                                    }
Dean@1194
  1718
Dean@1194
  1719
                                    Globals.Log("PEPMessage.Create: Failure creating 'CC' identity.");
Dean@1095
  1720
                                }
Dean@1095
  1721
Dean@1095
  1722
                                break;
Dean@927
  1723
                            }
Dean@1095
  1724
                        case Outlook.OlMailRecipientType.olTo:
Dean@927
  1725
                            {
Dean@1202
  1726
                                sts = PEPIdentity.Create(currRecipient, out ident);
Dean@1095
  1727
                                if (sts == Globals.ReturnStatus.Success)
Dean@927
  1728
                                {
Dean@1095
  1729
                                    newMessage.To.Add(ident);
Dean@927
  1730
                                }
Dean@1095
  1731
                                else
Dean@1095
  1732
                                {
Dean@1095
  1733
                                    // Update the status, only the first failure type is recorded
Dean@1095
  1734
                                    if (status == Globals.ReturnStatus.Success)
Dean@1095
  1735
                                    {
Dean@1095
  1736
                                        status = sts;
Dean@1095
  1737
                                    }
Dean@1194
  1738
Dean@1194
  1739
                                    Globals.Log("PEPMessage.Create: Failure creating 'To' identity.");
Dean@1095
  1740
                                }
Dean@1095
  1741
Dean@1095
  1742
                                break;
Dean@927
  1743
                            }
Dean@927
  1744
                    }
Dean@927
  1745
Dean@927
  1746
                    Marshal.ReleaseComObject(currRecipient);
Dean@927
  1747
                    currRecipient = null;
Dean@927
  1748
                }
Dean@927
  1749
Dean@927
  1750
                Globals.LogVerbose("PEPMessage.Create: Recipients calculated, calculating main properties.");
Dean@927
  1751
Dean@927
  1752
                newMessage.Direction = CryptableMailItem.GetIsIncoming(omi) ? _pEp_msg_direction.pEp_dir_incoming : _pEp_msg_direction.pEp_dir_outgoing;
Dean@927
  1753
Dean@927
  1754
                // From
Dean@927
  1755
                sts = PEPIdentity.GetFromIdentity(omi, out ident);
Dean@927
  1756
                if (sts == Globals.ReturnStatus.Success)
Dean@927
  1757
                {
Dean@927
  1758
                    newMessage.From = ident;
Dean@927
  1759
                }
Dean@927
  1760
                else
Dean@927
  1761
                {
Dean@927
  1762
                    // Update the status, only the first failure type is recorded
Dean@927
  1763
                    if (status == Globals.ReturnStatus.Success)
Dean@927
  1764
                    {
Dean@927
  1765
                        status = sts;
Dean@927
  1766
                    }
Dean@1194
  1767
Dean@1194
  1768
                    Globals.Log("PEPMessage.Create: Failure creating 'From' identity.");
Dean@927
  1769
                }
Dean@927
  1770
Dean@927
  1771
                newMessage.ID = (string)MAPIHelper.GetProperty(omi, MAPIProperty.PidTagInternetMessageId, "");
Dean@927
  1772
                newMessage.ShortMsg = omi.Subject;
Dean@927
  1773
Dean@927
  1774
                /* Date & Times
Dean@927
  1775
                 * 
Dean@927
  1776
                 * Note: The mail item date can be invalid:
Dean@927
  1777
                 * For incoming this commonly occurs when creating a mirror -- it's created without a received time.
Dean@927
  1778
                 * For outgoing this commonly occurs for unsent mail.
Dean@927
  1779
                 * In these cases, the invalid date and time will be returned as 1/1/4501 at 12:00AM
Dean@927
  1780
                 * This situation is filtered here and the date is set as null.
Dean@927
  1781
                 */
Dean@927
  1782
                receivedOn = omi.ReceivedTime;
Dean@927
  1783
                if ((receivedOn != null) &&
Dean@927
  1784
                    (((DateTime)receivedOn).Year > 4000)) // ~2000 years from now, no issue
Dean@927
  1785
                {
Dean@927
  1786
                    receivedOn = null;
Dean@927
  1787
                }
Dean@927
  1788
                newMessage.ReceivedOn = receivedOn;
Dean@927
  1789
Dean@927
  1790
                sentOn = omi.SentOn;
Dean@927
  1791
                if ((sentOn != null) &&
Dean@927
  1792
                    (((DateTime)sentOn).Year > 4000)) // ~2000 years from now, no issue
Dean@927
  1793
                {
Dean@927
  1794
                    sentOn = null;
Dean@927
  1795
                }
Dean@927
  1796
                newMessage.SentOn = sentOn;
Dean@927
  1797
Dean@1113
  1798
                // Conversation information
Dean@1113
  1799
                newMessage.ConversationID = omi.ConversationID;
Dean@1113
  1800
                newMessage.ConversationIndex = omi.ConversationIndex;
Dean@1113
  1801
                newMessage.ConversationTopic = omi.ConversationTopic;
Dean@1113
  1802
Dean@1095
  1803
                // ForceUnencrypted
Dean@1095
  1804
                CryptableMailItem.GetInterpretedProperty(omi,
Dean@1095
  1805
                                                         CryptableMailItem.USER_PROPERTY_KEY_FORCE_UNENCRYPTED,
Dean@1095
  1806
                                                         out propValue);
Dean@1095
  1807
                newMessage.ForceUnencrypted = (bool)propValue;
Dean@1095
  1808
Dean@1130
  1809
                // Rating
Dean@1095
  1810
                CryptableMailItem.GetInterpretedProperty(omi,
Dean@1223
  1811
                                                         PEPMessage.XPidNameEncStatus.DASLName,
Dean@1095
  1812
                                                         out propValue);
Dean@1130
  1813
                newMessage.Rating = (_pEp_color)propValue;
Dean@1095
  1814
Dean@1095
  1815
                // KeyList
Dean@1095
  1816
                CryptableMailItem.GetInterpretedProperty(omi,
Dean@1223
  1817
                                                         PEPMessage.XPidNameKeyList.DASLName,
Dean@1095
  1818
                                                         out propValue);
Dean@1095
  1819
                newMessage.KeyList = (string)propValue;
Dean@1095
  1820
Dean@1104
  1821
                // NeverUnsecure
Dean@1095
  1822
                CryptableMailItem.GetInterpretedProperty(omi,
Dean@1223
  1823
                                                         PEPMessage.XPidNamePEPNeverUnsecure.DASLName,
Dean@1095
  1824
                                                         out propValue);
Dean@1104
  1825
                newMessage.NeverUnsecure = (bool)propValue;
Dean@1095
  1826
Dean@1212
  1827
                // PEPProtocolVersion
Dean@1095
  1828
                CryptableMailItem.GetInterpretedProperty(omi,
Dean@1223
  1829
                                                         PEPMessage.XPidNamePEPProtocolVersion.DASLName,
Dean@1095
  1830
                                                         out propValue);
Dean@1212
  1831
                newMessage.PEPProtocolVersion = (string)propValue;
Dean@1095
  1832
Dean@927
  1833
                Globals.LogVerbose("PEPMessage.Create: Main properties calculated, calculating body and attachments.");
Dean@927
  1834
Dean@927
  1835
                // Calculate text body and attachments
Dean@927
  1836
                if (createWithoutContent == false)
Dean@927
  1837
                {
Dean@927
  1838
                    // Body
Dean@927
  1839
                    if (omi.Body != null)
Dean@927
  1840
                    {
Dean@927
  1841
                        newMessage.LongMsg = omi.Body.Replace("\r\n", "\n");
Dean@927
  1842
Dean@927
  1843
                        // Save as RTF
Dean@927
  1844
                        try
Dean@927
  1845
                        {
Dean@927
  1846
                            rtfBody = omi.RTFBody;
Dean@927
  1847
                            newMessage.LongMsgFormattedRTF = System.Text.Encoding.ASCII.GetString(rtfBody, 0, rtfBody.Length);
Dean@927
  1848
                        }
Dean@927
  1849
                        catch
Dean@927
  1850
                        {
Dean@927
  1851
                            Globals.Log("PEPMessage.Create: Unable to read RTF body in MailItem.");
Dean@927
  1852
                        }
Dean@927
  1853
Dean@1095
  1854
                        // Force any rich text into HTML
Dean@927
  1855
                        // WARNING: This is technically a modifcation of the original MailItem
Dean@927
  1856
                        // It should be further investigated if this can be removed in the future.
Dean@927
  1857
                        if (omi.BodyFormat == Outlook.OlBodyFormat.olFormatRichText)
Dean@927
  1858
                        {
Dean@927
  1859
                            omi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
Dean@927
  1860
                        }
Dean@927
  1861
Dean@927
  1862
                        if (omi.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
Dean@927
  1863
                        {
Dean@927
  1864
                            newMessage.LongMsgFormattedHTML = omi.HTMLBody;
Dean@927
  1865
                        }
Dean@927
  1866
                    }
Dean@927
  1867
Dean@927
  1868
                    // Attachments
Dean@927
  1869
                    newMessage.Attachments.Clear();
Dean@927
  1870
                    attachments = omi.Attachments;
Dean@927
  1871
                    for (int i = 1; i <= attachments.Count; i++)
Dean@927
  1872
                    {
Dean@927
  1873
                        currAttachment = attachments[i];
Dean@927
  1874
Dean@927
  1875
                        try
Dean@927
  1876
                        {
Dean@927
  1877
                            newMessage.Attachments.Add(new PEPAttachment(currAttachment));
Dean@927
  1878
                        }
Dean@927
  1879
                        catch { }
Dean@927
  1880
Dean@927
  1881
                        Marshal.ReleaseComObject(currAttachment);
Dean@927
  1882
                        currAttachment = null;
Dean@927
  1883
                    }
Dean@927
  1884
Dean@927
  1885
                    // Keywords
Dean@927
  1886
                    if (omi.Categories != null)
Dean@927
  1887
                    {
Dean@927
  1888
                        try
Dean@927
  1889
                        {
Dean@927
  1890
                            newMessage.Keywords.Clear();
Dean@927
  1891
Dean@927
  1892
                            using (RegistryKey key1 = Registry.CurrentUser.OpenSubKey("Control Panel\\International"))
Dean@927
  1893
                            {
Dean@927
  1894
                                delim = key1.GetValue("sList").ToString();
Dean@927
  1895
                                keywords = omi.Categories.Split(delim.ToCharArray());
Dean@927
  1896
Dean@927
  1897
                                for (int i = 0; i < keywords.Length; i++)
Dean@927
  1898
                                {
Dean@927
  1899
                                    newMessage.Keywords.Add(keywords[i]);
Dean@927
  1900
                                }
Dean@927
  1901
                            }
Dean@927
  1902
                        }
Dean@927
  1903
                        catch
Dean@927
  1904
                        {
Dean@927
  1905
                            newMessage.Keywords.Clear();
Dean@927
  1906
                            Globals.Log("PEPMessage.Create: Unable to set keywords.");
Dean@927
  1907
                        }
Dean@927
  1908
                    }
Dean@927
  1909
                }
Dean@927
  1910
Dean@927
  1911
                Globals.LogVerbose("PEPMessage.Create: New PEPMessage created from OMI.");
Dean@927
  1912
            }
Dean@927
  1913
            catch (Exception ex)
Dean@927
  1914
            {
Dean@927
  1915
                status = Globals.ReturnStatus.Failure;
Dean@927
  1916
                Globals.Log("PEPMessage.Create: failure occured, " + ex.ToString());
Dean@927
  1917
            }
Dean@927
  1918
            finally
Dean@927
  1919
            {
Dean@927
  1920
                // Free resources
Dean@927
  1921
                if (currAttachment != null)
Dean@927
  1922
                {
Dean@927
  1923
                    Marshal.ReleaseComObject(currAttachment);
Dean@927
  1924
                    currAttachment = null;
Dean@927
  1925
                }
Dean@927
  1926
Dean@927
  1927
                if (attachments != null)
Dean@927
  1928
                {
Dean@927
  1929
                    Marshal.ReleaseComObject(attachments);
Dean@927
  1930
                    attachments = null;
Dean@927
  1931
                }
Dean@927
  1932
Dean@927
  1933
                if (currRecipient != null)
Dean@927
  1934
                {
Dean@927
  1935
                    Marshal.ReleaseComObject(currRecipient);
Dean@927
  1936
                    currRecipient = null;
Dean@927
  1937
                }
Dean@927
  1938
Dean@927
  1939
                if (recipients != null)
Dean@927
  1940
                {
Dean@927
  1941
                    Marshal.ReleaseComObject(recipients);
Dean@927
  1942
                    recipients = null;
Dean@927
  1943
                }
Dean@927
  1944
            }
Dean@927
  1945
Dean@927
  1946
            createdMessage = newMessage;
Dean@972
  1947
Dean@972
  1948
            Globals.LogVerbose("PEPMessage.Create: ReturnStatus=" + status.ToString());
Dean@927
  1949
            return (status);
Dean@927
  1950
        }
Dean@927
  1951
Dean@927
  1952
        /// <summary>
Dean@927
  1953
        /// Determines if the given text is PGP text based on starting text sequence.
Dean@927
  1954
        /// The starting sequence must be "-----BEGIN PGP MESSAGE-----"
Dean@927
  1955
        /// </summary>
Dean@927
  1956
        /// <param name="text">The text to test if it is PGP text.</param>
Dean@927
  1957
        /// <returns>True if the given text is PGP text, otherwise false.</returns>
Dean@927
  1958
        public static bool IsPGPText(string text)
Dean@927
  1959
        {
Dean@927
  1960
            if (string.IsNullOrEmpty(text) == false)
Dean@927
  1961
            {
Dean@927
  1962
                string pgp_text = text.Trim();
Dean@927
  1963
                return (pgp_text.StartsWith("-----BEGIN PGP MESSAGE-----"));
Dean@927
  1964
            }
Dean@927
  1965
            else
Dean@927
  1966
            {
Dean@927
  1967
                return (false);
Dean@927
  1968
            }
Dean@927
  1969
        }
Dean@927
  1970
Dean@927
  1971
        /// <summary>
Dean@1224
  1972
        /// Determines if the given message is encrypted.
Dean@927
  1973
        /// Currently, only PGP encrypted messages will be detected.
Dean@927
  1974
        /// </summary>
Dean@927
  1975
        /// <param name="omi">The outlook mail item to check encryption for.</param>
Dean@1224
  1976
        /// <returns>True if the given message is encrypted, otherwise false.</returns>
Dean@927
  1977
        public static bool GetIsEncrypted(Outlook.MailItem omi)
Dean@927
  1978
        {
Dean@927
  1979
            if (omi != null)
Dean@927
  1980
            {
Dean@1224
  1981
                // Partitioned or inline PGP format
Dean@927
  1982
                if (omi.Body != null && PEPMessage.IsPGPText(omi.Body))
Dean@927
  1983
                {
Dean@927
  1984
                    return (true);
Dean@927
  1985
                }
Dean@927
  1986
Dean@1224
  1987
                // PGP/MIME format
Dean@1224
  1988
                if (PEPMessage.GetIsPGPMIMEEncrypted(omi))
Dean@927
  1989
                {
Dean@1224
  1990
                    return (true);
Dean@927
  1991
                }
Dean@927
  1992
            }
Dean@927
  1993
Dean@927
  1994
            return (false);
Dean@927
  1995
        }
Dean@927
  1996
Dean@927
  1997
        /// <summary>
Dean@1224
  1998
        /// Determines if the given message is encrypted in PGP/MIME format.
Dean@1224
  1999
        /// </summary>
Dean@1224
  2000
        /// <param name="msg">The message to check encryption for.</param>
Dean@1224
  2001
        /// <returns>True if the given message is PGP/MIME encrypted, otherwise false.</returns>
Dean@1224
  2002
        public static bool GetIsPGPMIMEEncrypted(Outlook.MailItem omi)
Dean@1224
  2003
        {
Dean@1224
  2004
            bool result = false;
Dean@1224
  2005
            bool versionInfoFound = false;
Dean@1224
  2006
            bool encryptedContentFound = false;
Dean@1224
  2007
            byte[] copy;
Dean@1224
  2008
            PEPAttachment attach;
Dean@1224
  2009
            Outlook.Attachment attachment = null;
Dean@1224
  2010
            Outlook.Attachments attachments = null;
Dean@1224
  2011
Dean@1224
  2012
            try
Dean@1224
  2013
            {
Dean@1224
  2014
                if (omi != null)
Dean@1224
  2015
                {
Dean@1224
  2016
                    attachments = omi.Attachments;
Dean@1224
  2017
Dean@1224
  2018
                    // Require only two attachments (version identification & encrypted content)
Dean@1224
  2019
                    // However, allow the attachments to be in any order
Dean@1224
  2020
                    if (attachments.Count == 2)
Dean@1224
  2021
                    {
Dean@1224
  2022
                        // Note: attachment index starts at 1
Dean@1224
  2023
                        for (int i = 1; i <= attachments.Count; i++)
Dean@1224
  2024
                        {
Dean@1224
  2025
                            attachment = attachments[i];
Dean@1224
  2026
                            attach = new PEPAttachment(attachment);
Dean@1224
  2027
Dean@1224
  2028
                            // Check for version identification
Dean@1224
  2029
                            if ((string.IsNullOrEmpty(attach.MIMEType) == false) &&
Dean@1224
  2030
                                (string.Equals(attach.MIMEType.Trim(), "application/pgp-encrypted", StringComparison.OrdinalIgnoreCase)))
Dean@1224
  2031
                            {
Dean@1224
  2032
                                // Allow any data
Dean@1224
  2033
                                if ((attach.Data != null) &&
Dean@1224
  2034
                                    (attach.Data.Length > 0))
Dean@1224
  2035
                                {
Dean@1224
  2036
                                    versionInfoFound = true;
Dean@1224
  2037
                                }
Dean@1224
  2038
                            }
Dean@1224
  2039
Dean@1224
  2040
                            // Check for encrypted content
Dean@1224
  2041
                            if ((string.IsNullOrEmpty(attach.MIMEType) == false) &&
Dean@1224
  2042
                                (string.Equals(attach.MIMEType.Trim(), "application/octet-stream", StringComparison.OrdinalIgnoreCase)))
Dean@1224
  2043
                            {
Dean@1224
  2044
                                // Require data to start with PGP text and be at least 100 bytes
Dean@1224
  2045
                                if ((attach.Data != null) &&
Dean@1224
  2046
                                    (attach.Data.Length > 100))
Dean@1224
  2047
                                {
Dean@1224
  2048
                                    // Create a copy
Dean@1224
  2049
                                    copy = new byte[100];
Dean@1224
  2050
                                    for (int j = 0; j < 100; j++)
Dean@1224
  2051
                                    {
Dean@1224
  2052
                                        copy[j] = attach.Data[j];
Dean@1224
  2053
                                    }
Dean@1224
  2054
Dean@1224
  2055
                                    // Check for PGP text after converting to string
Dean@1224
  2056
                                    if (PEPMessage.IsPGPText(System.Text.Encoding.ASCII.GetString(copy)))
Dean@1224
  2057
                                    {
Dean@1224
  2058
                                        encryptedContentFound = true;
Dean@1224
  2059
                                    }
Dean@1224
  2060
                                }
Dean@1224
  2061
                            }
Dean@1224
  2062
Dean@1224
  2063
                            Marshal.ReleaseComObject(attachment);
Dean@1224
  2064
                            attachment = null;
Dean@1224
  2065
                        }
Dean@1224
  2066
Dean@1224
  2067
                        if (versionInfoFound && encryptedContentFound)
Dean@1224
  2068
                        {
Dean@1224
  2069
                            result = true;
Dean@1224
  2070
                        }
Dean@1224
  2071
                    }
Dean@1224
  2072
                }
Dean@1224
  2073
            }
Dean@1224
  2074
            catch { }
Dean@1224
  2075
            finally
Dean@1224
  2076
            {
Dean@1224
  2077
                if (attachment != null)
Dean@1224
  2078
                {
Dean@1224
  2079
                    Marshal.ReleaseComObject(attachment);
Dean@1224
  2080
                    attachment = null;
Dean@1224
  2081
                }
Dean@1224
  2082
Dean@1224
  2083
                if (attachments != null)
Dean@1224
  2084
                {
Dean@1224
  2085
                    Marshal.ReleaseComObject(attachments);
Dean@1224
  2086
                    attachments = null;
Dean@1224
  2087
                }
Dean@1224
  2088
            }
Dean@1224
  2089
Dean@1224
  2090
            return (result);
Dean@1224
  2091
        }
Dean@1224
  2092
Dean@1224
  2093
        /// <summary>
Dean@927
  2094
        /// Determines if the given PEPMessage is encrypted.
Dean@927
  2095
        /// Currently, only PGP encrypted messages will be detected.
Dean@927
  2096
        /// </summary>
Dean@927
  2097
        /// <param name="msg">The message to check encryption for.</param>
Dean@927
  2098
        /// <returns>True if the given message is encrypted, otherwise false.</returns>
Dean@927
  2099
        public static bool GetIsEncrypted(PEPMessage msg)
Dean@927
  2100
        {
Dean@927
  2101
            if (msg != null)
Dean@927
  2102
            {
Dean@1224
  2103
                // Partitioned or inline PGP format
Dean@927
  2104
                if (msg.LongMsg != null && PEPMessage.IsPGPText(msg.LongMsg))
Dean@927
  2105
                {
Dean@927
  2106
                    return (true);
Dean@927
  2107
                }
Dean@927
  2108
Dean@1224
  2109
                // PGP/MIME format
Dean@1224
  2110
                if (PEPMessage.GetIsPGPMIMEEncrypted(msg))
Dean@927
  2111
                {
Dean@1224
  2112
                    return (true);
Dean@927
  2113
                }
Dean@927
  2114
            }
Dean@927
  2115
Dean@927
  2116
            return (false);
Dean@927
  2117
        }
Dean@1224
  2118
Dean@1224
  2119
        /// <summary>
Dean@1224
  2120
        /// Determines if the given message is encrypted in PGP/MIME format.
Dean@1224
  2121
        /// </summary>
Dean@1224
  2122
        /// <param name="msg">The message to check encryption for.</param>
Dean@1224
  2123
        /// <returns>True if the given message is PGP/MIME encrypted, otherwise false.</returns>
Dean@1224
  2124
        public static bool GetIsPGPMIMEEncrypted(PEPMessage msg)
Dean@1224
  2125
        {
Dean@1224
  2126
            bool result = false;
Dean@1224
  2127
            bool versionInfoFound = false;
Dean@1224
  2128
            bool encryptedContentFound = false;
Dean@1224
  2129
            byte[] copy;
Dean@1224
  2130
Dean@1224
  2131
            if (msg != null)
Dean@1224
  2132
            {
Dean@1224
  2133
                // Require only two attachments (version identification & encrypted content)
Dean@1224
  2134
                // However, allow the attachments to be in any order
Dean@1224
  2135
                if (msg.Attachments.Count == 2)
Dean@1224
  2136
                {
Dean@1224
  2137
                    foreach (PEPAttachment attach in msg.Attachments)
Dean@1224
  2138
                    {
Dean@1224
  2139
                        // Check for version identification
Dean@1224
  2140
                        if ((string.IsNullOrEmpty(attach.MIMEType) == false) &&
Dean@1224
  2141
                            (string.Equals(attach.MIMEType.Trim(), "application/pgp-encrypted", StringComparison.OrdinalIgnoreCase)))
Dean@1224
  2142
                        {
Dean@1224
  2143
                            // Allow any data
Dean@1224
  2144
                            if ((attach.Data != null) &&
Dean@1224
  2145
                                (attach.Data.Length > 0))
Dean@1224
  2146
                            {
Dean@1224
  2147
                                versionInfoFound = true;
Dean@1224
  2148
                            }
Dean@1224
  2149
                        }
Dean@1224
  2150
Dean@1224
  2151
                        // Check for encrypted content
Dean@1224
  2152
                        if ((string.IsNullOrEmpty(attach.MIMEType) == false) &&
Dean@1224
  2153
                            (string.Equals(attach.MIMEType.Trim(), "application/octet-stream", StringComparison.OrdinalIgnoreCase)))
Dean@1224
  2154
                        {
Dean@1224
  2155
                            // Require data to start with PGP text and be at least 100 bytes
Dean@1224
  2156
                            if ((attach.Data != null) &&
Dean@1224
  2157
                                (attach.Data.Length > 100))
Dean@1224
  2158
                            {
Dean@1224
  2159
                                // Create a copy
Dean@1224
  2160
                                copy = new byte[100];
Dean@1224
  2161
                                for (int j = 0; j < 100; j++)
Dean@1224
  2162
                                {
Dean@1224
  2163
                                    copy[j] = attach.Data[j];
Dean@1224
  2164
                                }
Dean@1224
  2165
Dean@1224
  2166
                                // Check for PGP text after converting to string
Dean@1224
  2167
                                if (PEPMessage.IsPGPText(System.Text.Encoding.ASCII.GetString(copy)))
Dean@1224
  2168
                                {
Dean@1224
  2169
                                    encryptedContentFound = true;
Dean@1224
  2170
                                }
Dean@1224
  2171
                            }
Dean@1224
  2172
                        }
Dean@1224
  2173
                    }
Dean@1224
  2174
Dean@1224
  2175
                    if (versionInfoFound && encryptedContentFound)
Dean@1224
  2176
                    {
Dean@1224
  2177
                        result = true;
Dean@1224
  2178
                    }
Dean@1224
  2179
                }
Dean@1224
  2180
            }
Dean@1224
  2181
Dean@1224
  2182
            return (result);
Dean@1224
  2183
        }
Dean@384
  2184
    }
Dean@384
  2185
}