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