2017-02-23 15:29:53 +01:00
|
|
|
|
|
2018-02-28 16:00:51 +01:00
|
|
|
|
/// Copyright 2018 Kopano b.v.
|
2017-02-15 12:01:04 +01:00
|
|
|
|
///
|
|
|
|
|
/// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
/// it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
/// as published by the Free Software Foundation.
|
|
|
|
|
///
|
|
|
|
|
/// This program is distributed in the hope that it will be useful,
|
|
|
|
|
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
|
/// GNU Affero General Public License for more details.
|
|
|
|
|
///
|
|
|
|
|
/// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
/// along with this program.If not, see<http://www.gnu.org/licenses/>.
|
|
|
|
|
///
|
|
|
|
|
/// Consult LICENSE file for details
|
|
|
|
|
using Acacia.Utils;
|
2018-02-28 16:00:51 +01:00
|
|
|
|
using Acacia.Native;
|
|
|
|
|
using Acacia.Native.MAPI;
|
2017-02-14 14:46:24 +01:00
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
2017-02-23 15:29:53 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
2017-02-14 14:46:24 +01:00
|
|
|
|
using System.Security;
|
|
|
|
|
using System.Text;
|
2017-02-23 15:29:53 +01:00
|
|
|
|
using System.Threading;
|
2017-02-14 14:46:24 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2017-02-23 15:29:53 +01:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using NSOutlook = Microsoft.Office.Interop.Outlook;
|
2017-02-14 14:46:24 +01:00
|
|
|
|
|
|
|
|
|
namespace Acacia.Stubs.OutlookWrappers
|
|
|
|
|
{
|
|
|
|
|
[TypeConverter(typeof(ExpandableObjectConverter))]
|
2017-02-23 15:29:53 +01:00
|
|
|
|
class AccountWrapper : ComWrapper<NSOutlook.Application>, IAccount, LogContext
|
2017-02-14 14:46:24 +01:00
|
|
|
|
{
|
2017-11-30 08:26:12 +01:00
|
|
|
|
private readonly string _accountId;
|
2017-02-14 14:46:24 +01:00
|
|
|
|
private readonly string _regPath;
|
|
|
|
|
private readonly IStore _store;
|
|
|
|
|
|
2017-02-23 15:29:53 +01:00
|
|
|
|
internal AccountWrapper(NSOutlook.Application item, string regPath, IStore store) : base(item)
|
2017-02-14 14:46:24 +01:00
|
|
|
|
{
|
2017-11-30 08:26:12 +01:00
|
|
|
|
this._accountId = System.IO.Path.GetFileName(regPath);
|
2017-02-14 14:46:24 +01:00
|
|
|
|
this._regPath = regPath;
|
|
|
|
|
this._store = store;
|
|
|
|
|
|
|
|
|
|
// Cache the SmtpAddress, it is used as the key
|
|
|
|
|
SmtpAddress = RegistryUtil.GetValueString(_regPath, OutlookConstants.REG_VAL_EMAIL, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void DoRelease()
|
|
|
|
|
{
|
|
|
|
|
_store.Dispose();
|
2017-02-23 15:29:53 +01:00
|
|
|
|
base.DoRelease();
|
2017-02-14 14:46:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Browsable(false)]
|
|
|
|
|
public string LogContextId
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "ZPushAccount(" + SmtpAddress + ")";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return SmtpAddress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-02-14 15:16:13 +01:00
|
|
|
|
/// Triggers an Outlook send/receive operation for this account.
|
2017-02-14 14:46:24 +01:00
|
|
|
|
/// </summary>
|
2018-04-13 12:50:04 +02:00
|
|
|
|
public void SendReceive(AcaciaTask after = null)
|
2017-02-14 14:46:24 +01:00
|
|
|
|
{
|
2018-04-13 12:50:04 +02:00
|
|
|
|
ThisAddIn.Instance.SendReceive(this, after);
|
2017-02-14 14:46:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
|
|
public AccountType AccountType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-11-30 08:26:12 +01:00
|
|
|
|
return (UserName == null) ? AccountType.Other : AccountType.EAS;
|
2017-02-14 14:46:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 08:26:12 +01:00
|
|
|
|
public string AccountId
|
|
|
|
|
{
|
|
|
|
|
get { return _accountId; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 14:46:24 +01:00
|
|
|
|
[Browsable(false)]
|
|
|
|
|
public IStore Store
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _store;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 16:35:05 +02:00
|
|
|
|
|
|
|
|
|
public string BackingFilePath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = (byte[])Registry.GetValue(_regPath, OutlookConstants.REG_VAL_EAS_STOREID, null);
|
|
|
|
|
// Find the last index of 00
|
|
|
|
|
int start = bytes.Length - 2;
|
|
|
|
|
while (start > 2)
|
|
|
|
|
{
|
|
|
|
|
if (bytes[start - 1] == 0 && bytes[start - 2] == 0)
|
|
|
|
|
break;
|
|
|
|
|
--start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (start <= 2)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return System.Text.Encoding.Unicode.GetString(bytes, start, bytes.Length - start - 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-02-14 14:46:24 +01:00
|
|
|
|
public string DisplayName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return RegistryUtil.GetValueString(_regPath, OutlookConstants.REG_VAL_DISPLAYNAME, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string SmtpAddress
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string UserName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return RegistryUtil.GetValueString(_regPath, OutlookConstants.REG_VAL_EAS_USERNAME, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ServerURL
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return RegistryUtil.GetValueString(_regPath, OutlookConstants.REG_VAL_EAS_SERVER, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string DeviceId
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-04-04 14:06:09 +02:00
|
|
|
|
string devId = RegistryUtil.GetValueString(_regPath, OutlookConstants.REG_VAL_EAS_DEVICEID, null);
|
|
|
|
|
if (devId == null)
|
|
|
|
|
devId = RegistryUtil.GetValueString(_regPath, OutlookConstants.REG_VAL_EAS_DEVICEID, null);
|
|
|
|
|
return devId;
|
2017-02-14 14:46:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Browsable(false)]
|
|
|
|
|
public SecureString Password
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-09-06 08:32:09 +02:00
|
|
|
|
return PasswordEncryption.Decrypt(EncryptedPassword);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[Browsable(false)]
|
|
|
|
|
public byte[] EncryptedPassword
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (byte[])Registry.GetValue(_regPath, OutlookConstants.REG_VAL_EAS_PASSWORD, null);
|
2017-02-14 14:46:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Browsable(false)]
|
|
|
|
|
public bool HasPassword
|
|
|
|
|
{
|
|
|
|
|
get { return Registry.GetValue(_regPath, OutlookConstants.REG_VAL_EAS_PASSWORD, null) != null; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string StoreID
|
|
|
|
|
{
|
|
|
|
|
get { return GetStoreId(_regPath); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetStoreId(string regPath)
|
|
|
|
|
{
|
|
|
|
|
return StringUtil.BytesToHex((byte[])Registry.GetValue(regPath, OutlookConstants.REG_VAL_EAS_STOREID, null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string DomainName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
int index = SmtpAddress.IndexOf('@');
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return SmtpAddress;
|
|
|
|
|
else
|
|
|
|
|
return SmtpAddress.Substring(index + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 15:29:53 +01:00
|
|
|
|
private NSOutlook.Account FindAccountObject()
|
|
|
|
|
{
|
|
|
|
|
using (ComRelease com = new ComRelease())
|
|
|
|
|
{
|
|
|
|
|
NSOutlook.NameSpace session = com.Add(_item.Session);
|
|
|
|
|
foreach(NSOutlook.Account account in session.Accounts.ComEnum(false))
|
|
|
|
|
{
|
|
|
|
|
if (account.SmtpAddress == this.SmtpAddress)
|
|
|
|
|
return account;
|
|
|
|
|
else
|
|
|
|
|
com.Add(account);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-02-28 16:00:51 +01:00
|
|
|
|
unsafe public void SetAccountProp(PropTag propTag, object value)
|
2017-12-01 11:22:22 +01:00
|
|
|
|
{
|
2018-02-28 16:00:51 +01:00
|
|
|
|
// Use IOlkAccount to notify while we're running
|
|
|
|
|
// IOlkAccount can only be accessed on main thread
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp1: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
ThisAddIn.Instance.InUI(() =>
|
2017-12-01 11:22:22 +01:00
|
|
|
|
{
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp2: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
using (ComRelease com = new ComRelease())
|
|
|
|
|
{
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp3: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
NSOutlook.Account account = com.Add(FindAccountObject());
|
|
|
|
|
IOlkAccount olk = com.Add(account.IOlkAccount);
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp4: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
|
2018-09-25 12:36:57 +02:00
|
|
|
|
switch (propTag.type)
|
2018-02-28 16:00:51 +01:00
|
|
|
|
{
|
|
|
|
|
case PropType.UNICODE:
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp5: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
fixed (char* ptr = ((string)value).ToCharArray())
|
|
|
|
|
{
|
|
|
|
|
ACCT_VARIANT val = new ACCT_VARIANT()
|
|
|
|
|
{
|
|
|
|
|
dwType = (uint)PropType.UNICODE,
|
|
|
|
|
lpszW = ptr
|
|
|
|
|
};
|
|
|
|
|
olk.SetProp(propTag, &val);
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp6: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
olk.SaveChanges(0);
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp7: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case PropType.LONG:
|
|
|
|
|
{
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp8: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
ACCT_VARIANT val = new ACCT_VARIANT()
|
|
|
|
|
{
|
|
|
|
|
dwType = (uint)PropType.LONG,
|
|
|
|
|
dw = (uint)value
|
|
|
|
|
};
|
|
|
|
|
olk.SetProp(propTag, &val);
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountProp9: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
olk.SaveChanges(0);
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountPropA: {0}: {1}", propTag, value);
|
2018-02-28 16:00:51 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-25 12:36:57 +02:00
|
|
|
|
Logger.Instance.Trace(this, "SetAccountPropDone: {0}: {1}", propTag, value);
|
|
|
|
|
|
2018-02-28 16:00:51 +01:00
|
|
|
|
}
|
2018-09-25 12:36:57 +02:00
|
|
|
|
}, true);
|
2017-12-01 11:22:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 14:06:09 +02:00
|
|
|
|
public string this[string index]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return RegistryUtil.GetValueString(_regPath, index, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
RegistryUtil.RemoveValue(_regPath, index);
|
|
|
|
|
else
|
|
|
|
|
RegistryUtil.SetValueString(_regPath, index, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 14:46:24 +01:00
|
|
|
|
#endregion
|
2018-02-28 16:00:51 +01:00
|
|
|
|
|
|
|
|
|
public string RegistryBaseKey { get { return _regPath; } }
|
2017-02-14 14:46:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|