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