Extensions/StoreExtensions.cs
author Thomas
Tue, 03 Apr 2018 10:17:13 +0200
branchOUT-369
changeset 2100 91f72cbed562
parent 2069 e7a29b63f826
child 2109 5b0c92cceb00
child 2172 652eec18140e
permissions -rw-r--r--
Close branch OUT-369
Dean@1438
     1
´╗┐using System;
Dean@1438
     2
using Outlook = Microsoft.Office.Interop.Outlook;
Dean@1438
     3
Dean@1438
     4
namespace pEp
Dean@1438
     5
{
Dean@1438
     6
    /// <summary>
Dean@1438
     7
    /// Contains extensions for the Store as well as utility methods specific for pEp.
Dean@1438
     8
    /// </summary>
Dean@1438
     9
    internal static class StoreExtensions
Dean@1438
    10
    {
Dean@1438
    11
        /// <summary>
Thomas@1784
    12
        /// Determines if the given Outlook store should always be decrypted by comparing against the account settings.
Thomas@1784
    13
        /// The assumption is each account has its own unique data store (which may not be true for POP3).
Thomas@1784
    14
        /// </summary>
Thomas@1784
    15
        /// <param name="store">The Outlook store to process with.</param>
Thomas@1784
    16
        /// <returns>True if the Outlook store should always be decrypted, otherwise false.</returns>
Thomas@1784
    17
        public static bool GetIsDecryptAlwaysEnabled(this Outlook.Store store)
Thomas@1784
    18
        {
Thomas@1784
    19
            bool isEnabled = PEPSettings.ACCOUNT_SETTING_IS_DECRYPT_ALWAYS_ENABLED_DEFAULT;
Thomas@1784
    20
            Outlook.Store deliveryStore = null;
Thomas@1784
    21
            Outlook.Account account = null;
Thomas@1784
    22
            Outlook.Accounts accounts = null;
Thomas@1784
    23
            Outlook.NameSpace ns = null;
Thomas@1784
    24
Thomas@1784
    25
            try
Thomas@1784
    26
            {
Thomas@1784
    27
                if (store != null)
Thomas@1784
    28
                {
Thomas@2069
    29
                    ns = Globals.ThisAddIn.Application.Session;
Thomas@2069
    30
                    accounts = ns.Accounts;
Thomas@2069
    31
Thomas@2069
    32
                    // Note: Index starts at 1
Thomas@2069
    33
                    for (int i = 1; i <= accounts.Count; i++)
Thomas@1784
    34
                    {
Thomas@2069
    35
                        account = accounts[i];
Thomas@2069
    36
Thomas@2069
    37
                        // Getting delivery store can fail for new accounts when Outlook is not restarted
Thomas@2069
    38
                        try
Thomas@1784
    39
                        {
Thomas@2069
    40
                            deliveryStore = account.DeliveryStore;
Thomas@2069
    41
                        }
Thomas@2069
    42
                        catch
Thomas@2069
    43
                        {
Thomas@2069
    44
                            deliveryStore = null;
Thomas@2069
    45
                            Log.Warning("GetIsDecryptAlwaysEnabled: Failure getting DeliveryStore.");
Thomas@2069
    46
                        }
Thomas@2069
    47
Thomas@2069
    48
                        if ((deliveryStore != null) &&
Thomas@2069
    49
                            (deliveryStore.StoreID == store.StoreID))
Thomas@2069
    50
                        {
Thomas@2069
    51
                            isEnabled = account.GetIsDecryptAlwaysEnabled();
Thomas@1784
    52
                            break;
Thomas@1784
    53
                        }
Thomas@1784
    54
Thomas@2069
    55
                        account = null;
Thomas@2069
    56
                        deliveryStore = null;
Thomas@1784
    57
                    }
Thomas@1784
    58
                }
Thomas@1784
    59
                else
Thomas@1784
    60
                {
Thomas@1784
    61
                    isEnabled = PEPSettings.ACCOUNT_SETTING_IS_DECRYPT_ALWAYS_ENABLED_DEFAULT;
Thomas@1784
    62
                    Log.Warning("GetIsDecryptAlwaysEnabled: null store, returning default.");
Thomas@1784
    63
                }
Thomas@1784
    64
            }
Thomas@1784
    65
            catch (Exception ex)
Thomas@1784
    66
            {
Thomas@1784
    67
                isEnabled = PEPSettings.ACCOUNT_SETTING_IS_DECRYPT_ALWAYS_ENABLED_DEFAULT;
Thomas@1784
    68
                Log.Error("GetIsDecryptAlwaysEnabled: Failure occured, returning default. " + ex.ToString());
Thomas@1784
    69
            }
Thomas@1784
    70
            finally
Thomas@1784
    71
            {
Thomas@1784
    72
                account = null;
Thomas@1784
    73
                accounts = null;
Thomas@1784
    74
                deliveryStore = null;
Thomas@1784
    75
                ns = null;
Thomas@1784
    76
            }
Thomas@1784
    77
Thomas@1784
    78
            return isEnabled;
Thomas@1784
    79
        }
Thomas@1784
    80
Thomas@1784
    81
        /// <summary>
Dean@1438
    82
        /// Determines if the given Outlook store has pEp enabled by comparing against the account settings.
Thomas@1444
    83
        /// The assumption is each account has its own unique data store (which may not be true for POP3).
Dean@1438
    84
        /// </summary>
Dean@1438
    85
        /// <param name="store">The Outlook store to process with.</param>
Dean@1438
    86
        /// <returns>True if the Outlook store has pEp enabled, otherwise false.</returns>
Dean@1438
    87
        public static bool GetIsPEPEnabled(this Outlook.Store store)
Dean@1438
    88
        {
Dean@1438
    89
            bool defaultResult = PEPSettings.ACCOUNT_SETTING_IS_PEP_ENABLED_DEFAULT;
Dean@1438
    90
            bool result = defaultResult;
Dean@1438
    91
            Outlook.Store deliveryStore = null;
Dean@1438
    92
            Outlook.Account account = null;
Dean@1438
    93
            Outlook.Accounts accounts = null;
Dean@1438
    94
            Outlook.NameSpace ns = null;
Dean@1438
    95
Dean@1438
    96
            try
Dean@1438
    97
            {
Dean@1438
    98
                if (store != null)
Dean@1438
    99
                {
Thomas@2069
   100
                    ns = Globals.ThisAddIn.Application.Session;
Thomas@2069
   101
                    accounts = ns.Accounts;
Thomas@2069
   102
Thomas@2069
   103
                    // Note: Index starts at 1
Thomas@2069
   104
                    for (int i = 1; i <= accounts.Count; i++)
Dean@1438
   105
                    {
Thomas@2069
   106
                        account = accounts[i];
Thomas@2069
   107
Thomas@2069
   108
                        // Getting delivery store can fail for new accounts when Outlook is not restarted
Thomas@2069
   109
                        try
Dean@1438
   110
                        {
Thomas@2069
   111
                            deliveryStore = account.DeliveryStore;
Thomas@2069
   112
                        }
Thomas@2069
   113
                        catch
Thomas@2069
   114
                        {
Thomas@2069
   115
                            deliveryStore = null;
Thomas@2069
   116
                            Log.Warning("GetIsPEPEnabled: Failure getting DeliveryStore.");
Thomas@2069
   117
                        }
Thomas@2069
   118
Thomas@2069
   119
                        if ((deliveryStore != null) &&
Thomas@2069
   120
                            (deliveryStore.StoreID == store.StoreID))
Thomas@2069
   121
                        {
Thomas@2069
   122
                            result = account.GetIsPEPEnabled();
Dean@1438
   123
                            break;
Dean@1438
   124
                        }
Dean@1438
   125
Thomas@2069
   126
                        if (account != null)
Thomas@2069
   127
                        {
Thomas@2069
   128
                            // Marshal.ReleaseComObject(account);
Thomas@2069
   129
                            account = null;
Thomas@2069
   130
                        }
Thomas@2058
   131
Thomas@2069
   132
                        if (deliveryStore != null)
Dean@1438
   133
                        {
Thomas@2069
   134
                            // Marshal.ReleaseComObject(deliveryStore);
Thomas@2069
   135
                            deliveryStore = null;
Dean@1438
   136
                        }
Dean@1438
   137
                    }
Dean@1438
   138
                }
Dean@1438
   139
                else
Dean@1438
   140
                {
Dean@1438
   141
                    result = defaultResult;
Dean@1438
   142
                    Log.Warning("GetIsPEPEnabled: null store, returning default.");
Dean@1438
   143
                }
Dean@1438
   144
            }
Dean@1438
   145
            catch (Exception ex)
Dean@1438
   146
            {
Dean@1438
   147
                result = defaultResult;
Dean@1438
   148
                Log.Error("GetIsPEPEnabled: Failure occured, returning default. " + ex.ToString());
Dean@1438
   149
            }
Dean@1438
   150
            finally
Dean@1438
   151
            {
Dean@1438
   152
                // Release objects
Dean@1438
   153
                if (deliveryStore != null)
Dean@1438
   154
                {
vb@1507
   155
                    // Marshal.ReleaseComObject(deliveryStore);
Dean@1438
   156
                    deliveryStore = null;
Dean@1438
   157
                }
Dean@1438
   158
Dean@1438
   159
                if (account != null)
Dean@1438
   160
                {
vb@1507
   161
                    // Marshal.ReleaseComObject(account);
Dean@1438
   162
                    account = null;
Dean@1438
   163
                }
Dean@1438
   164
Dean@1438
   165
                if (accounts != null)
Dean@1438
   166
                {
vb@1507
   167
                    // Marshal.ReleaseComObject(accounts);
Dean@1438
   168
                    accounts = null;
Dean@1438
   169
                }
Dean@1438
   170
Dean@1438
   171
                if (ns != null)
Dean@1438
   172
                {
vb@1507
   173
                    // Marshal.ReleaseComObject(ns);
Dean@1438
   174
                    ns = null;
Dean@1438
   175
                }
Dean@1438
   176
            }
Dean@1438
   177
Dean@1438
   178
            return (result);
Dean@1438
   179
        }
Dean@1438
   180
Dean@1438
   181
        /// <summary>
Dean@1438
   182
        /// Determines if the given outlook store is to store messages securely by comparing against the account settings.
Thomas@1444
   183
        /// The assumption is each account has its own unique data store (which may not be true for POP3).
Dean@1438
   184
        /// </summary>
Dean@1438
   185
        /// <param name="store">The Outlook store to process with.</param>
Dean@1438
   186
        /// <returns>True if the outlook store is to store messages securely, otherwise false.</returns>
Dean@1438
   187
        public static bool GetIsSecureStorageEnabled(this Outlook.Store store)
Dean@1438
   188
        {
Dean@1438
   189
            bool defaultResult = PEPSettings.ACCOUNT_SETTING_IS_SECURE_STORAGE_ENABLED_DEFAULT;
Dean@1438
   190
            bool result = defaultResult;
Dean@1438
   191
            Outlook.Store deliveryStore = null;
Dean@1438
   192
            Outlook.Account account = null;
Dean@1438
   193
            Outlook.Accounts accounts = null;
Dean@1438
   194
            Outlook.NameSpace ns = null;
Dean@1438
   195
            PEPSettings.PEPAccountSettings acctSettings;
Dean@1438
   196
Dean@1438
   197
            if (Globals.ThisAddIn.Settings.IsEncryptAllAccountsEnabled)
Dean@1438
   198
            {
Dean@1438
   199
                return (true);
Dean@1438
   200
            }
Dean@1438
   201
Dean@1438
   202
            try
Dean@1438
   203
            {
Dean@1438
   204
                if (store != null)
Dean@1438
   205
                {
Thomas@2069
   206
                    ns = Globals.ThisAddIn.Application.Session;
Thomas@2069
   207
                    accounts = ns.Accounts;
Thomas@2069
   208
Thomas@2069
   209
                    // Note: Index starts at 1
Thomas@2069
   210
                    for (int i = 1; i <= accounts.Count; i++)
Thomas@2058
   211
                    {
Thomas@2069
   212
                        account = accounts[i];
Thomas@2069
   213
Thomas@2069
   214
                        // Getting delivery store can fail for new accounts when Outlook is not restarted
Thomas@2069
   215
                        try
Thomas@2058
   216
                        {
Thomas@2069
   217
                            deliveryStore = account.DeliveryStore;
Thomas@2058
   218
                        }
Thomas@2069
   219
                        catch
Thomas@2069
   220
                        {
Thomas@2069
   221
                            deliveryStore = null;
Thomas@2069
   222
                            Log.Warning("GetIsSecureStorageEnabled: Failure getting DeliveryStore.");
Thomas@2069
   223
                        }
Dean@1438
   224
Thomas@2069
   225
                        if ((deliveryStore != null) &&
Thomas@2069
   226
                            (deliveryStore.StoreID == store.StoreID))
Thomas@2069
   227
                        {
Thomas@2069
   228
                            acctSettings = Globals.ThisAddIn.Settings.GetAccountSettings(account.SmtpAddress);
Thomas@2069
   229
                            if (acctSettings != null)
Thomas@2069
   230
                            {
Thomas@2069
   231
                                result = acctSettings.IsSecureStorageEnabled;
Thomas@2069
   232
                                break;
Thomas@2069
   233
                            }
Thomas@2069
   234
                            else
Thomas@2069
   235
                            {
Thomas@2069
   236
                                result = defaultResult;
Thomas@2069
   237
                                break;
Thomas@2069
   238
                            }
Thomas@2069
   239
                        }
Dean@1438
   240
Thomas@2069
   241
                        if (account != null)
Dean@1438
   242
                        {
Thomas@2069
   243
                            // Marshal.ReleaseComObject(account);
Thomas@2069
   244
                            account = null;
Thomas@2069
   245
                        }
Dean@1438
   246
Thomas@2069
   247
                        if (deliveryStore != null)
Thomas@2069
   248
                        {
Thomas@2069
   249
                            // Marshal.ReleaseComObject(deliveryStore);
Thomas@2069
   250
                            deliveryStore = null;
Dean@1438
   251
                        }
Dean@1438
   252
                    }
Dean@1438
   253
                }
Dean@1438
   254
                else
Dean@1438
   255
                {
Dean@1438
   256
                    result = defaultResult;
Dean@1438
   257
                    Log.Warning("GetIsSecureStorageEnabled: null store, returning default.");
Dean@1438
   258
                }
Dean@1438
   259
            }
Dean@1438
   260
            catch (Exception ex)
Dean@1438
   261
            {
Dean@1438
   262
                result = defaultResult;
Dean@1438
   263
                Log.Error("GetIsSecureStorageEnabled: Failure occured, returning default. " + ex.ToString());
Dean@1438
   264
            }
Dean@1438
   265
            finally
Dean@1438
   266
            {
Dean@1438
   267
                // Release objects
Dean@1438
   268
                if (deliveryStore != null)
Dean@1438
   269
                {
vb@1507
   270
                    // Marshal.ReleaseComObject(deliveryStore);
Dean@1438
   271
                    deliveryStore = null;
Dean@1438
   272
                }
Dean@1438
   273
Dean@1438
   274
                if (account != null)
Dean@1438
   275
                {
vb@1507
   276
                    // Marshal.ReleaseComObject(account);
Dean@1438
   277
                    account = null;
Dean@1438
   278
                }
Dean@1438
   279
Dean@1438
   280
                if (accounts != null)
Dean@1438
   281
                {
vb@1507
   282
                    // Marshal.ReleaseComObject(accounts);
Dean@1438
   283
                    accounts = null;
Dean@1438
   284
                }
Dean@1438
   285
Dean@1438
   286
                if (ns != null)
Dean@1438
   287
                {
vb@1507
   288
                    // Marshal.ReleaseComObject(ns);
Dean@1438
   289
                    ns = null;
Dean@1438
   290
                }
Dean@1438
   291
            }
Dean@1438
   292
Dean@1438
   293
            return (result);
Dean@1438
   294
        }
Dean@1438
   295
Dean@1438
   296
        /// <summary>
Dean@1454
   297
        /// Determines if the given outlook store is of the given account type.
Dean@1454
   298
        /// Note: If ANY account using the store of the given type, true will be returned.
Dean@1438
   299
        /// </summary>
Dean@1438
   300
        /// <param name="store">The Outlook store to process with.</param>
Dean@1454
   301
        /// <param name="acctType">The account type to check for.</param>
Dean@1454
   302
        /// <returns>True if the outlook store is of the given account type, otherwise false.</returns>
Dean@1454
   303
        public static bool GetIsAccountType(this Outlook.Store store,
Dean@1454
   304
                                            Outlook.OlAccountType acctType)
Dean@1438
   305
        {
Dean@1438
   306
            bool defaultResult = false;
Dean@1438
   307
            bool result = defaultResult;
Dean@1438
   308
            Outlook.Account account = null;
Dean@1438
   309
            Outlook.Accounts accounts = null;
Dean@1438
   310
            Outlook.Store deliveryStore = null;
Dean@1438
   311
            Outlook.NameSpace ns = null;
Dean@1438
   312
Dean@1438
   313
            try
Dean@1438
   314
            {
Dean@1438
   315
                if (store != null)
Dean@1438
   316
                {
Thomas@2069
   317
                    ns = Globals.ThisAddIn.Application.Session;
Thomas@2069
   318
                    accounts = ns.Accounts;
Thomas@2069
   319
Thomas@2069
   320
                    // Note: Index starts at 1
Thomas@2069
   321
                    for (int i = 1; i <= accounts.Count; i++)
Dean@1438
   322
                    {
Thomas@2069
   323
                        account = accounts[i];
Thomas@2069
   324
Thomas@2069
   325
                        // Getting delivery store can fail for new accounts when Outlook is not restarted
Thomas@2069
   326
                        try
Dean@1438
   327
                        {
Thomas@2069
   328
                            deliveryStore = account.DeliveryStore;
Thomas@2069
   329
                        }
Thomas@2069
   330
                        catch
Thomas@2069
   331
                        {
Thomas@2069
   332
                            deliveryStore = null;
Thomas@2069
   333
                            Log.Warning("GetIsAccountType: Failure getting DeliveryStore.");
Thomas@2069
   334
                        }
Thomas@2069
   335
Thomas@2069
   336
                        if ((deliveryStore != null) &&
Thomas@2069
   337
                            (deliveryStore.StoreID == store.StoreID) &&
Thomas@2069
   338
                            (account.AccountType == acctType))
Thomas@2069
   339
                        {
Thomas@2069
   340
                            result = true;
Dean@1438
   341
                            break;
Dean@1438
   342
                        }
Dean@1438
   343
Thomas@2069
   344
                        if (account != null)
Thomas@2069
   345
                        {
Thomas@2069
   346
                            // Marshal.ReleaseComObject(account);
Thomas@2069
   347
                            account = null;
Thomas@2069
   348
                        }
Thomas@2058
   349
Thomas@2069
   350
                        if (deliveryStore != null)
Dean@1438
   351
                        {
Thomas@2069
   352
                            // Marshal.ReleaseComObject(deliveryStore);
Thomas@2069
   353
                            deliveryStore = null;
Dean@1438
   354
                        }
Dean@1438
   355
                    }
Dean@1438
   356
                }
Dean@1438
   357
                else
Dean@1438
   358
                {
Dean@1438
   359
                    result = defaultResult;
Dean@1454
   360
                    Log.Warning("GetIsAccountType: null store, returning default.");
Dean@1438
   361
                }
Dean@1438
   362
            }
Dean@1438
   363
            catch (Exception ex)
Dean@1438
   364
            {
Dean@1438
   365
                result = defaultResult;
Dean@1454
   366
                Log.Error("GetIsAccountType: Failure occured, returning default. " + ex.ToString());
Dean@1438
   367
            }
Dean@1438
   368
            finally
Dean@1438
   369
            {
Dean@1438
   370
                // Release objects
Dean@1438
   371
                if (account != null)
Dean@1438
   372
                {
vb@1507
   373
                    // Marshal.ReleaseComObject(account);
Dean@1438
   374
                    account = null;
Dean@1438
   375
                }
Dean@1438
   376
Dean@1438
   377
                if (accounts != null)
Dean@1438
   378
                {
vb@1507
   379
                    // Marshal.ReleaseComObject(accounts);
Dean@1438
   380
                    accounts = null;
Dean@1438
   381
                }
Dean@1438
   382
Dean@1438
   383
                if (deliveryStore != null)
Dean@1438
   384
                {
vb@1507
   385
                    // Marshal.ReleaseComObject(deliveryStore);
Dean@1438
   386
                    deliveryStore = null;
Dean@1438
   387
                }
Dean@1438
   388
Dean@1438
   389
                if (ns != null)
Dean@1438
   390
                {
vb@1507
   391
                    // Marshal.ReleaseComObject(ns);
Dean@1438
   392
                    ns = null;
Dean@1438
   393
                }
Dean@1438
   394
            }
Dean@1438
   395
Dean@1438
   396
            return (result);
Dean@1438
   397
        }
Thomas@1673
   398
Thomas@1673
   399
        /// <summary>
Thomas@1673
   400
        /// Determines a store's maximum allowable size for a message that is being submitted through that store.
Thomas@1673
   401
        /// </summary>
Thomas@1673
   402
        /// <param name="store">The Outlook store to process with.</param>
Thomas@1673
   403
        /// <returns>The maximum allowed size in kB.</returns>
Thomas@1673
   404
        public static int GetMaxSubmitSize(this Outlook.Store store)
Thomas@1673
   405
        {
Thomas@1673
   406
            const int DEFAULT_MAX_SIZE = 20480; // kB, see: https://support.microsoft.com/en-us/help/2222370/-attachment-size-exceeds-the-allowable-limit-error-when-you-add-a-large-attachment-to-an-email-message-in-outlook-2010
Thomas@1673
   407
            int maxFileSize = DEFAULT_MAX_SIZE;
Thomas@1673
   408
            Outlook.PropertyAccessor propertyAccessor = null;
Thomas@1673
   409
Thomas@1673
   410
            try
Thomas@1673
   411
            {
Thomas@1673
   412
                propertyAccessor = store.PropertyAccessor;
Thomas@1673
   413
                maxFileSize = propertyAccessor.GetProperty(MapiProperty.PidTagMaximumSubmitMessageSize.DaslName);
Thomas@1673
   414
            }
Thomas@1936
   415
            catch (Exception ex)
Thomas@1673
   416
            {
Thomas@1673
   417
                maxFileSize = DEFAULT_MAX_SIZE;
Thomas@1936
   418
                Log.Verbose("GetMaxSubmitSize: Error getting size limit. " + ex.ToString());
Thomas@1673
   419
            }
Thomas@1673
   420
            finally
Thomas@1673
   421
            {
Thomas@1673
   422
                propertyAccessor = null;
Thomas@1673
   423
            }
Thomas@1673
   424
Thomas@1673
   425
            return maxFileSize;
Thomas@1673
   426
        }
Dean@1438
   427
    }
Dean@1438
   428
}