From ae362b482a882ef30a14e96bf5df3ff70a24ec28 Mon Sep 17 00:00:00 2001 From: Erik Fryklund Date: Thu, 11 Apr 2019 11:21:24 +0200 Subject: [PATCH 1/6] plugin hmail-alias integration --- plugins/hmail-aliases/LICENSE | 20 ++++++ plugins/hmail-aliases/README | 1 + plugins/hmail-aliases/VERSION | 1 + plugins/hmail-aliases/index.php | 113 ++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 plugins/hmail-aliases/LICENSE create mode 100644 plugins/hmail-aliases/README create mode 100644 plugins/hmail-aliases/VERSION create mode 100755 plugins/hmail-aliases/index.php diff --git a/plugins/hmail-aliases/LICENSE b/plugins/hmail-aliases/LICENSE new file mode 100644 index 0000000000..8e08efd68d --- /dev/null +++ b/plugins/hmail-aliases/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2019 Karobolas + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/plugins/hmail-aliases/README b/plugins/hmail-aliases/README new file mode 100644 index 0000000000..57efea0de8 --- /dev/null +++ b/plugins/hmail-aliases/README @@ -0,0 +1 @@ +Plugin which allows you to use hmail aliases diff --git a/plugins/hmail-aliases/VERSION b/plugins/hmail-aliases/VERSION new file mode 100644 index 0000000000..9f8e9b69a3 --- /dev/null +++ b/plugins/hmail-aliases/VERSION @@ -0,0 +1 @@ +1.0 \ No newline at end of file diff --git a/plugins/hmail-aliases/index.php b/plugins/hmail-aliases/index.php new file mode 100755 index 0000000000..59963c4f67 --- /dev/null +++ b/plugins/hmail-aliases/index.php @@ -0,0 +1,113 @@ +addHook('event.login-post-login-provide', 'loginPostLoginProvide'); + } + + public function Supported() + { + if (!class_exists('mysqli')) { + return 'The PHP exention mysqli must be installed to use this plugin'; + } + + return ''; + } + + /** + * @param \RainLoop\Model\Account $oAccount + * @throws \RainLoop\Exceptions\ClientException + */ + public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount) + { + $dbservername = \trim($this->Config()->Get('plugin', 'hamilserver', '')); + $dbusername = \trim($this->Config()->Get('plugin', 'hmaildbusername', '')); + $dbpassword = \trim($this->Config()->Get('plugin', 'hmaildbpassword', '')); + $dbname = \trim($this->Config()->Get('plugin', 'hmaildbtable', '')); + $dbport = \trim($this->Config()->Get('plugin', 'hmaildbport', '')); + $datadir = \trim($this->Config()->Get('plugin', 'rainloopDatalocation', '')); + + if ($datadir != ""){ + $userpath = $datadir.'data/_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; + } else { + $userpath = APP_INDEX_ROOT_PATH.'_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; + } + + $hmailconn = mysqli_connect($dbservername, $dbusername, $dbpassword, $dbname, $dbport); + + // Check connection + if (!$hmailconn) { + echo "Hmail-aliases: connection to db failed"; + return; + } + //Get aliases + $result = $hmailconn->query("SELECT * FROM " . $dbname . ".hm_aliases WHERE aliasvalue='".$oAccount->Email()."'"); + + if ($result->num_rows > 0) { + $newidentitiesobj = array(); + + //Get user account + $result2 = $hmailconn->query("SELECT * FROM " . $dbname . ".hm_accounts WHERE accountaddress='".$oAccount->Email()."'"); + $result2 = $result2->fetch_assoc(); + $firstname = $result2['accountpersonfirstname']; + $lastname = $result2['accountpersonlastname']; + + if ($firstname == "" && $lastname == "") { + $name = ""; + } else { + $name = $firstname." ".$lastname; + } + + //Get existing settings. If not a alias created by hmail. Transfer settings to the new array. + $identities = file_get_contents($userpath, true); + if ($identities != "") { + $identities = json_decode($identities, true); + error_log(print_r($identities, true)); + foreach ($identities as $row) { + if (strpos($row['Id'], 'HMAIL') === false) { + array_push($newidentitiesobj, $row); + } + } + } + + // output data of each row + while ($row = $result->fetch_assoc()) { + $obj = array(); + $obj['Id'] = "HMAIL".base64_encode($row["aliasname"]); + $obj['Email'] = $row["aliasname"]; + $obj['Name'] = $name; + $obj['ReplyTo'] = ""; + $obj['Bcc'] = ""; + $obj['Signature'] = ""; + $obj['SignatureInsertBefore'] = false; + array_push($newidentitiesobj, $obj); + } + file_put_contents($userpath, json_encode($newidentitiesobj)); + } + } + + /** + * @return array + */ + public function configMapping() + { + return array( + \RainLoop\Plugins\Property::NewInstance('hamilserver')->SetLabel('db-host') + ->SetDefaultValue('localhost'), + \RainLoop\Plugins\Property::NewInstance('hmaildbusername')->SetLabel('db-username') + ->SetDefaultValue(''), + \RainLoop\Plugins\Property::NewInstance('hmaildbpassword')->SetLabel('db-password') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD), + \RainLoop\Plugins\Property::NewInstance('hmaildbtable')->SetLabel('db-table') + ->SetDefaultValue('mailserver'), + \RainLoop\Plugins\Property::NewInstance('hmaildbport')->SetLabel('db-port') + ->SetDefaultValue('3306') + ->SetDescription('Connect to mysql hmailserver. The user must have rights to read "hm_aliases" table.'), + \RainLoop\Plugins\Property::NewInstance('rainloopDatalocation')->SetLabel('Data folder location') + ->SetDefaultValue('') + ->SetDescription('Incase of custom data directory location. Eg. nextcloud/owncloud version (Leave blank for default)') + ); + } +} From 1cf32d6c90418824bef609d45519e5c72b626e2d Mon Sep 17 00:00:00 2001 From: Timothy Sturm Date: Wed, 1 Mar 2023 17:44:31 -0600 Subject: [PATCH 2/6] Create Rainloop identities from hMail Use com api instead of direct dp access --- plugins/hmail-aliases/index.php | 134 +++++++++++++++++++++++++------- 1 file changed, 107 insertions(+), 27 deletions(-) diff --git a/plugins/hmail-aliases/index.php b/plugins/hmail-aliases/index.php index 59963c4f67..dec50c4e20 100755 --- a/plugins/hmail-aliases/index.php +++ b/plugins/hmail-aliases/index.php @@ -9,11 +9,12 @@ public function Init() public function Supported() { - if (!class_exists('mysqli')) { - return 'The PHP exention mysqli must be installed to use this plugin'; - } + if (!class_exists('COM')) + { + return 'The PHP extension COM must be installed to use this plugin'; + } - return ''; + return ''; } /** @@ -22,19 +23,104 @@ public function Supported() */ public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount) { - $dbservername = \trim($this->Config()->Get('plugin', 'hamilserver', '')); - $dbusername = \trim($this->Config()->Get('plugin', 'hmaildbusername', '')); - $dbpassword = \trim($this->Config()->Get('plugin', 'hmaildbpassword', '')); - $dbname = \trim($this->Config()->Get('plugin', 'hmaildbtable', '')); - $dbport = \trim($this->Config()->Get('plugin', 'hmaildbport', '')); - $datadir = \trim($this->Config()->Get('plugin', 'rainloopDatalocation', '')); - - if ($datadir != ""){ - $userpath = $datadir.'data/_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; - } else { - $userpath = APP_INDEX_ROOT_PATH.'_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; - } + $oProvider = new HmailserverChangePasswordDriver(); + $oLogger($this->Manager()->Actions()->Logger()); + $sLogin = (string) $this->Config()->Get('plugin', 'login', ''); + $sPassword = (string) $this->Config()->Get('plugin', 'password', ''); + $bResult = false; + + try + { + $oHmailApp = new COM("hMailServer.Application"); + $oHmailApp->Connect(); + + if ($oHmailApp->Authenticate($sLogin, $sPassword)) + { + $sEmail = $oHmailAccount->Email(); + $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); + + $oHmailDomain = $oHmailApp->Domains->ItemByName($sDomain); + if ($oHmailDomain) + { + $oHmailAccount = $oHmailDomain->Accounts->ItemByAddress($sEmail); + if ($oHmailAccount) + { + // Get account details for alias + + $firstName = $oHmailAccount->PersonFirstName; + $lastName = $oHmailAccount->PersonLastName; + if ($firstName == "" && $lastName == "") { + $name = ""; + } else { + $name = $firstName." ".$lastName; + } + + // ========vvv========= Update Rainloop Idnetity ========vvv========= + + $datadir = \trim($this->Config()->Get('plugin', 'rainloopDatalocation', '')); + if ($datadir != ""){ + $userpath = $datadir.'data/_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; + } else { + $userpath = APP_INDEX_ROOT_PATH.'_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; + } + + $newidentitiesobj = array(); + + //Get existing settings. If not a alias created by hmail. Transfer settings to the new array. + $identities = file_get_contents($userpath, true); + if ($identities != "") { + $identities = json_decode($identities, true); + error_log(print_r($identities, true)); + foreach ($identities as $row) { + if (strpos($row['Id'], 'HMAIL') === false) { + array_push($newidentitiesobj, $row); + } + } + } + + $obj = array(); + $obj['Id'] = "HMAIL".base64_encode($sEmail); + $obj['Email'] = $sEmail; + $obj['Name'] = $name; + $obj['ReplyTo'] = ""; + $obj['Bcc'] = ""; + $obj['Signature'] = ""; + $obj['SignatureInsertBefore'] = false; + array_push($newidentitiesobj, $obj); + + file_put_contents($userpath, json_encode($newidentitiesobj)); + // ========^^^========= Update Rainloop Idnetity ========^^^========= + + $bResult = true; + } + else + { + $this->oLogger->Write('HMAILSERVER: Unknown account ('.$sEmail.')', \MailSo\Log\Enumerations\Type::ERROR); + } + } + else + { + $this->oLogger->Write('HMAILSERVER: Unknown domain ('.$sDomain.')', \MailSo\Log\Enumerations\Type::ERROR); + } + } + else + { + $this->oLogger->Write('HMAILSERVER: Auth error', \MailSo\Log\Enumerations\Type::ERROR); + } + } + catch (\Exception $oException) + { + if ($this->oLogger) + { + $this->oLogger->WriteException($oException); + } + } + + return $bResult; + + //========================================================================== + $hmailconn = mysqli_connect($dbservername, $dbusername, $dbpassword, $dbname, $dbport); // Check connection @@ -94,17 +180,11 @@ public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount) public function configMapping() { return array( - \RainLoop\Plugins\Property::NewInstance('hamilserver')->SetLabel('db-host') - ->SetDefaultValue('localhost'), - \RainLoop\Plugins\Property::NewInstance('hmaildbusername')->SetLabel('db-username') - ->SetDefaultValue(''), - \RainLoop\Plugins\Property::NewInstance('hmaildbpassword')->SetLabel('db-password') - ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD), - \RainLoop\Plugins\Property::NewInstance('hmaildbtable')->SetLabel('db-table') - ->SetDefaultValue('mailserver'), - \RainLoop\Plugins\Property::NewInstance('hmaildbport')->SetLabel('db-port') - ->SetDefaultValue('3306') - ->SetDescription('Connect to mysql hmailserver. The user must have rights to read "hm_aliases" table.'), + \RainLoop\Plugins\Property::NewInstance('login')->SetLabel('HmailServer Admin Login') + ->SetDefaultValue('Administrator'), + \RainLoop\Plugins\Property::NewInstance('password')->SetLabel('HmailServer Admin Password') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD) + ->SetDefaultValue(''), \RainLoop\Plugins\Property::NewInstance('rainloopDatalocation')->SetLabel('Data folder location') ->SetDefaultValue('') ->SetDescription('Incase of custom data directory location. Eg. nextcloud/owncloud version (Leave blank for default)') From 3a2b30bc673196330a4db5d26524c87506621ac2 Mon Sep 17 00:00:00 2001 From: Timothy Sturm Date: Wed, 1 Mar 2023 19:44:11 -0600 Subject: [PATCH 3/6] remove old code oops --- plugins/hmail-aliases/index.php | 211 ++++++++++++-------------------- 1 file changed, 78 insertions(+), 133 deletions(-) diff --git a/plugins/hmail-aliases/index.php b/plugins/hmail-aliases/index.php index dec50c4e20..5bb55a11cd 100755 --- a/plugins/hmail-aliases/index.php +++ b/plugins/hmail-aliases/index.php @@ -23,155 +23,100 @@ public function Supported() */ public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount) { - $oProvider = new HmailserverChangePasswordDriver(); - $oLogger($this->Manager()->Actions()->Logger()); - $sLogin = (string) $this->Config()->Get('plugin', 'login', ''); - $sPassword = (string) $this->Config()->Get('plugin', 'password', ''); - $bResult = false; - - try + $oProvider = new HmailserverChangePasswordDriver(); + $oLogger($this->Manager()->Actions()->Logger()); + $sLogin = (string) $this->Config()->Get('plugin', 'login', ''); + $sPassword = (string) $this->Config()->Get('plugin', 'password', ''); + $bResult = false; + + try + { + $oHmailApp = new COM("hMailServer.Application"); + $oHmailApp->Connect(); + + if ($oHmailApp->Authenticate($sLogin, $sPassword)) { - $oHmailApp = new COM("hMailServer.Application"); - $oHmailApp->Connect(); + $sEmail = $oHmailAccount->Email(); + $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); - if ($oHmailApp->Authenticate($sLogin, $sPassword)) + $oHmailDomain = $oHmailApp->Domains->ItemByName($sDomain); + if ($oHmailDomain) { - $sEmail = $oHmailAccount->Email(); - $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); - - $oHmailDomain = $oHmailApp->Domains->ItemByName($sDomain); - if ($oHmailDomain) + $oHmailAccount = $oHmailDomain->Accounts->ItemByAddress($sEmail); + if ($oHmailAccount) { - $oHmailAccount = $oHmailDomain->Accounts->ItemByAddress($sEmail); - if ($oHmailAccount) - { - // Get account details for alias - - $firstName = $oHmailAccount->PersonFirstName; - $lastName = $oHmailAccount->PersonLastName; - if ($firstName == "" && $lastName == "") { - $name = ""; - } else { - $name = $firstName." ".$lastName; - } - - // ========vvv========= Update Rainloop Idnetity ========vvv========= - - $datadir = \trim($this->Config()->Get('plugin', 'rainloopDatalocation', '')); - if ($datadir != ""){ - $userpath = $datadir.'data/_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; - } else { - $userpath = APP_INDEX_ROOT_PATH.'_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; - } - - $newidentitiesobj = array(); - - //Get existing settings. If not a alias created by hmail. Transfer settings to the new array. - $identities = file_get_contents($userpath, true); - if ($identities != "") { - $identities = json_decode($identities, true); - error_log(print_r($identities, true)); - foreach ($identities as $row) { - if (strpos($row['Id'], 'HMAIL') === false) { - array_push($newidentitiesobj, $row); - } - } - } - - $obj = array(); - $obj['Id'] = "HMAIL".base64_encode($sEmail); - $obj['Email'] = $sEmail; - $obj['Name'] = $name; - $obj['ReplyTo'] = ""; - $obj['Bcc'] = ""; - $obj['Signature'] = ""; - $obj['SignatureInsertBefore'] = false; - array_push($newidentitiesobj, $obj); - - file_put_contents($userpath, json_encode($newidentitiesobj)); - // ========^^^========= Update Rainloop Idnetity ========^^^========= - - $bResult = true; - } - else - { - $this->oLogger->Write('HMAILSERVER: Unknown account ('.$sEmail.')', \MailSo\Log\Enumerations\Type::ERROR); - } + // Get account details for alias + + $firstName = $oHmailAccount->PersonFirstName; + $lastName = $oHmailAccount->PersonLastName; + if ($firstName == "" && $lastName == "") { + $name = ""; + } else { + $name = $firstName." ".$lastName; + } + + // ========vvv========= Update Rainloop Idnetity ========vvv========= + + $datadir = \trim($this->Config()->Get('plugin', 'rainloopDatalocation', '')); + if ($datadir != ""){ + $userpath = $datadir.'data/_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; + } else { + $userpath = APP_INDEX_ROOT_PATH.'_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; + } + + $newidentitiesobj = array(); + + //Get existing settings. If not a alias created by hmail. Transfer settings to the new array. + $identities = file_get_contents($userpath, true); + if ($identities != "") { + $identities = json_decode($identities, true); + error_log(print_r($identities, true)); + foreach ($identities as $row) { + if (strpos($row['Id'], 'HMAIL') === false) { + array_push($newidentitiesobj, $row); + } + } + } + + $obj = array(); + $obj['Id'] = "HMAIL".base64_encode($sEmail); + $obj['Email'] = $sEmail; + $obj['Name'] = $name; + $obj['ReplyTo'] = ""; + $obj['Bcc'] = ""; + $obj['Signature'] = ""; + $obj['SignatureInsertBefore'] = false; + array_push($newidentitiesobj, $obj); + + file_put_contents($userpath, json_encode($newidentitiesobj)); + // ========^^^========= Update Rainloop Idnetity ========^^^========= + + $bResult = true; } else { - $this->oLogger->Write('HMAILSERVER: Unknown domain ('.$sDomain.')', \MailSo\Log\Enumerations\Type::ERROR); + $this->oLogger->Write('HMAILSERVER: Unknown account ('.$sEmail.')', \MailSo\Log\Enumerations\Type::ERROR); } } else { - $this->oLogger->Write('HMAILSERVER: Auth error', \MailSo\Log\Enumerations\Type::ERROR); + $this->oLogger->Write('HMAILSERVER: Unknown domain ('.$sDomain.')', \MailSo\Log\Enumerations\Type::ERROR); } } - catch (\Exception $oException) + else { - if ($this->oLogger) - { - $this->oLogger->WriteException($oException); - } + $this->oLogger->Write('HMAILSERVER: Auth error', \MailSo\Log\Enumerations\Type::ERROR); + } + } + catch (\Exception $oException) + { + if ($this->oLogger) + { + $this->oLogger->WriteException($oException); } + } - return $bResult; - - //========================================================================== - - - $hmailconn = mysqli_connect($dbservername, $dbusername, $dbpassword, $dbname, $dbport); - - // Check connection - if (!$hmailconn) { - echo "Hmail-aliases: connection to db failed"; - return; - } - //Get aliases - $result = $hmailconn->query("SELECT * FROM " . $dbname . ".hm_aliases WHERE aliasvalue='".$oAccount->Email()."'"); - - if ($result->num_rows > 0) { - $newidentitiesobj = array(); - - //Get user account - $result2 = $hmailconn->query("SELECT * FROM " . $dbname . ".hm_accounts WHERE accountaddress='".$oAccount->Email()."'"); - $result2 = $result2->fetch_assoc(); - $firstname = $result2['accountpersonfirstname']; - $lastname = $result2['accountpersonlastname']; - - if ($firstname == "" && $lastname == "") { - $name = ""; - } else { - $name = $firstname." ".$lastname; - } - - //Get existing settings. If not a alias created by hmail. Transfer settings to the new array. - $identities = file_get_contents($userpath, true); - if ($identities != "") { - $identities = json_decode($identities, true); - error_log(print_r($identities, true)); - foreach ($identities as $row) { - if (strpos($row['Id'], 'HMAIL') === false) { - array_push($newidentitiesobj, $row); - } - } - } - - // output data of each row - while ($row = $result->fetch_assoc()) { - $obj = array(); - $obj['Id'] = "HMAIL".base64_encode($row["aliasname"]); - $obj['Email'] = $row["aliasname"]; - $obj['Name'] = $name; - $obj['ReplyTo'] = ""; - $obj['Bcc'] = ""; - $obj['Signature'] = ""; - $obj['SignatureInsertBefore'] = false; - array_push($newidentitiesobj, $obj); - } - file_put_contents($userpath, json_encode($newidentitiesobj)); - } + return $bResult; } /** From d26835755493d53b081b52313415f78ad1c955f3 Mon Sep 17 00:00:00 2001 From: Timothy Sturm Date: Wed, 1 Mar 2023 21:01:44 -0600 Subject: [PATCH 4/6] Working implementation of hmail-aliases without database or direct filesystem IO --- plugins/hmail-aliases/index.php | 138 +++++++++++++------------------- 1 file changed, 56 insertions(+), 82 deletions(-) diff --git a/plugins/hmail-aliases/index.php b/plugins/hmail-aliases/index.php index 5bb55a11cd..b17873a0ac 100755 --- a/plugins/hmail-aliases/index.php +++ b/plugins/hmail-aliases/index.php @@ -23,100 +23,77 @@ public function Supported() */ public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount) { - $oProvider = new HmailserverChangePasswordDriver(); - $oLogger($this->Manager()->Actions()->Logger()); - $sLogin = (string) $this->Config()->Get('plugin', 'login', ''); - $sPassword = (string) $this->Config()->Get('plugin', 'password', ''); - $bResult = false; - - try - { - $oHmailApp = new COM("hMailServer.Application"); - $oHmailApp->Connect(); - - if ($oHmailApp->Authenticate($sLogin, $sPassword)) + // Get Logger + $oLogger = $this->Manager()->Actions()->Logger(); + // Load values from configuration + $sLogin = (string) $this->Config()->Get('plugin', 'login', ''); + $sPassword = (string) $this->Config()->Get('plugin', 'password', ''); + // prime result value + $bResult = false; + + try { - $sEmail = $oHmailAccount->Email(); - $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); + // Create a connection with the hMailServer + $oHmailApp = new COM("hMailServer.Application"); + $oHmailApp->Connect(); - $oHmailDomain = $oHmailApp->Domains->ItemByName($sDomain); - if ($oHmailDomain) + //$oLogger->Write("Connected"); + if ($oHmailApp->Authenticate($sLogin, $sPassword)) { - $oHmailAccount = $oHmailDomain->Accounts->ItemByAddress($sEmail); - if ($oHmailAccount) + //$oLogger->Write("Authenticated"); + $sEmail = $oAccount->Email(); + //$oLogger->Write("Using ". $sEmail); + $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); + //$oLogger->Write("Searching for domain ".$sDomain); + + $oHmailDomain = $oHmailApp->Domains->ItemByName($sDomain); + if ($oHmailDomain) { - // Get account details for alias - - $firstName = $oHmailAccount->PersonFirstName; - $lastName = $oHmailAccount->PersonLastName; - if ($firstName == "" && $lastName == "") { - $name = ""; - } else { - $name = $firstName." ".$lastName; - } - - // ========vvv========= Update Rainloop Idnetity ========vvv========= - - $datadir = \trim($this->Config()->Get('plugin', 'rainloopDatalocation', '')); - if ($datadir != ""){ - $userpath = $datadir.'data/_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; - } else { - $userpath = APP_INDEX_ROOT_PATH.'_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; - } - - $newidentitiesobj = array(); - - //Get existing settings. If not a alias created by hmail. Transfer settings to the new array. - $identities = file_get_contents($userpath, true); - if ($identities != "") { - $identities = json_decode($identities, true); - error_log(print_r($identities, true)); - foreach ($identities as $row) { - if (strpos($row['Id'], 'HMAIL') === false) { - array_push($newidentitiesobj, $row); - } - } - } - - $obj = array(); - $obj['Id'] = "HMAIL".base64_encode($sEmail); - $obj['Email'] = $sEmail; - $obj['Name'] = $name; - $obj['ReplyTo'] = ""; - $obj['Bcc'] = ""; - $obj['Signature'] = ""; - $obj['SignatureInsertBefore'] = false; - array_push($newidentitiesobj, $obj); - - file_put_contents($userpath, json_encode($newidentitiesobj)); - // ========^^^========= Update Rainloop Idnetity ========^^^========= - - $bResult = true; + $oHmailAccount = $oHmailDomain->Accounts->ItemByAddress($sEmail); + if ($oHmailAccount) + { + // Get account details for alias + $firstName = $oHmailAccount->PersonFirstName; + $lastName = $oHmailAccount->PersonLastName; + if ($firstName == "" && $lastName == "") { + $name = ""; + } else { + $name = $firstName." ".$lastName; + } + + // ========vvv========= Update Rainloop Identity ========vvv========= + $identity = \RainLoop\Model\Identity::NewInstanceFromAccount($oAccount); + $identity->FromJSON(array('Email' => $sEmail, 'Name' => $name)); + // TODO Account::DoIdentityUpdate is possible if I knew how to use actoinParams + $result = $this->Manager()->Actions()->SetIdentities($oAccount, array($identity)); + $oLogger->Write('HMAILSERVER Identity Update Successful'); + // ========^^^========= Update Rainloop Idnetity ========^^^========= + $bResult = true; + } + else + { + $oLogger->Write('HMAILSERVER: Unknown account ('.$sEmail.')', \MailSo\Log\Enumerations\Type::ERROR); + } } else { - $this->oLogger->Write('HMAILSERVER: Unknown account ('.$sEmail.')', \MailSo\Log\Enumerations\Type::ERROR); + $oLogger->Write('HMAILSERVER: Unknown domain ('.$sDomain.')', \MailSo\Log\Enumerations\Type::ERROR); } } else { - $this->oLogger->Write('HMAILSERVER: Unknown domain ('.$sDomain.')', \MailSo\Log\Enumerations\Type::ERROR); + $oLogger->Write('HMAILSERVER: Auth error', \MailSo\Log\Enumerations\Type::ERROR); } } - else + catch (\Exception $oException) { - $this->oLogger->Write('HMAILSERVER: Auth error', \MailSo\Log\Enumerations\Type::ERROR); - } - } - catch (\Exception $oException) - { - if ($this->oLogger) - { - $this->oLogger->WriteException($oException); + if ($oLogger) + { + $oLogger->WriteException($oException); + } } - } - return $bResult; + return $bResult; } /** @@ -129,10 +106,7 @@ public function configMapping() ->SetDefaultValue('Administrator'), \RainLoop\Plugins\Property::NewInstance('password')->SetLabel('HmailServer Admin Password') ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD) - ->SetDefaultValue(''), - \RainLoop\Plugins\Property::NewInstance('rainloopDatalocation')->SetLabel('Data folder location') - ->SetDefaultValue('') - ->SetDescription('Incase of custom data directory location. Eg. nextcloud/owncloud version (Leave blank for default)') + ->SetDefaultValue('') ); } } From 3224d655fe57374fcef7034095f9bafa9723d13a Mon Sep 17 00:00:00 2001 From: Timothy Sturm Date: Wed, 1 Mar 2023 21:04:21 -0600 Subject: [PATCH 5/6] Update license for hmail server alias plugin --- plugins/hmail-aliases/LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/hmail-aliases/LICENSE b/plugins/hmail-aliases/LICENSE index 8e08efd68d..69d4b4db02 100644 --- a/plugins/hmail-aliases/LICENSE +++ b/plugins/hmail-aliases/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2019 Karobolas +Copyright (c) 2019 Karobolas, 2023 Oxymoron290 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in From d349d20e92057a5a043d4a9b2a7e33748153f90b Mon Sep 17 00:00:00 2001 From: Timothy Sturm Date: Wed, 1 Mar 2023 21:33:49 -0600 Subject: [PATCH 6/6] hMail Alias plugin - actually load existing identity. --- plugins/hmail-aliases/index.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/hmail-aliases/index.php b/plugins/hmail-aliases/index.php index b17873a0ac..aa26843b59 100755 --- a/plugins/hmail-aliases/index.php +++ b/plugins/hmail-aliases/index.php @@ -62,10 +62,17 @@ public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount) } // ========vvv========= Update Rainloop Identity ========vvv========= - $identity = \RainLoop\Model\Identity::NewInstanceFromAccount($oAccount); + $identities = $this->Manager()->Actions()->GetIdentities($oAccount); + if(empty($identities)){ + // I am assuming [0] is the "default" identity... + $identity = \RainLoop\Model\Identity::NewInstanceFromAccount($oAccount); + array_push($identities, $identity); + } + + $identity = $identities[0]; $identity->FromJSON(array('Email' => $sEmail, 'Name' => $name)); // TODO Account::DoIdentityUpdate is possible if I knew how to use actoinParams - $result = $this->Manager()->Actions()->SetIdentities($oAccount, array($identity)); + $result = $this->Manager()->Actions()->SetIdentities($oAccount, $identities); $oLogger->Write('HMAILSERVER Identity Update Successful'); // ========^^^========= Update Rainloop Idnetity ========^^^========= $bResult = true;