From 5c9b7d7000d0898fc4bb74702eeb72f879bbea78 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Fri, 13 May 2022 00:49:25 +0200 Subject: [PATCH] Added example plugin how to use Kolab for contact suggestions --- plugins/kolab/KolabContactsSuggestions.php | 48 +++++++++++++++++++ plugins/kolab/LICENSE | 20 ++++++++ plugins/kolab/index.php | 39 +++++++++++++++ .../AddressBook/KolabAddressBook.php | 40 +++++++++++++--- 4 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 plugins/kolab/KolabContactsSuggestions.php create mode 100644 plugins/kolab/LICENSE create mode 100644 plugins/kolab/index.php diff --git a/plugins/kolab/KolabContactsSuggestions.php b/plugins/kolab/KolabContactsSuggestions.php new file mode 100644 index 000000000..5c36f4709 --- /dev/null +++ b/plugins/kolab/KolabContactsSuggestions.php @@ -0,0 +1,48 @@ + \strlen($sQuery)) { + return []; + } + + $oActions = \RainLoop\Api::Actions(); + $oMailClient = $oActions->MailClient(); + if (!$oMailClient->IsLoggined()) { + $oAccount = $oActions->getAccountFromToken(); + $oAccount->IncConnectAndLoginHelper($oActions->Plugins(), $oMailClient, $oActions->Config()); + } + $oImapClient = $oMailClient->ImapClient(); + + $metadata = $oImapClient->FolderGetMetadata($this->sFolderName, [\MailSo\Imap\Enumerations\MetadataKeys::KOLAB_CTYPE]); + if ($metadata && 'contact' !== \array_shift($metadata)) { + // Throw error +// $oImapClient->FolderList() : array + return []; + } + $oImapClient->FolderSelect($this->sFolderName); + + $sQuery = \MailSo\Imap\SearchCriterias::escapeSearchString($oImapClient, $sQuery); + $aUids = \array_slice( + $oImapClient->MessageSimpleSearch("FROM {$sQuery}"), + 0, $iLimit + ); + + $aResult = []; + foreach ($oImapClient->Fetch(['BODY.PEEK[HEADER.FIELDS (FROM)]'], \implode(',', $aUids), true) as $oFetchResponse) { + $oHeaders = new \MailSo\Mime\HeaderCollection($oFetchResponse->GetHeaderFieldsValue()); + $oFrom = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::FROM_, true); + foreach ($oFrom as $oMail) { + $aResult[] = [$oMail->GetEmail(), $oMail->GetDisplayName()]; + } + } + + return $aResult; + } +} diff --git a/plugins/kolab/LICENSE b/plugins/kolab/LICENSE new file mode 100644 index 000000000..4aed64b3a --- /dev/null +++ b/plugins/kolab/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2015 RainLoop Team + +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/kolab/index.php b/plugins/kolab/index.php new file mode 100644 index 000000000..1d78c263c --- /dev/null +++ b/plugins/kolab/index.php @@ -0,0 +1,39 @@ +addHook('main.fabrica', 'MainFabrica'); + } + + public function Supported() : string + { + return ''; + } + + /** + * @param mixed $mResult + */ + public function MainFabrica(string $sName, &$mResult) + { + if ('suggestions' === $sName) { + if (!\is_array($mResult)) { + $mResult = array(); + } +// $sFolder = \trim($this->Config()->Get('plugin', 'mailbox', '')); +// if ($sFolder) { + include_once __DIR__ . '/KolabContactsSuggestions.php'; + $mResult[] = new KolabContactsSuggestions(); +// } + } + } +} diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AddressBook/KolabAddressBook.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AddressBook/KolabAddressBook.php index d45a45f5e..916cbdcf7 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AddressBook/KolabAddressBook.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AddressBook/KolabAddressBook.php @@ -8,7 +8,9 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt { use CardDAV; - protected $sFolderName; + protected + $oImapClient, + $sFolderName; function __construct(\MailSo\Imap\ImapClient $oImapClient) { @@ -21,9 +23,11 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt if ($metadata && 'contact' !== \array_shift($metadata)) { // Throw error // $this->oImapClient->FolderList() : array + return false; } $this->oImapClient->FolderSelect($sFolderName); $this->sFolderName = $sFolderName; + return true; } public function IsSupported() : bool @@ -84,10 +88,15 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt $oMessage->SubParts->append($oPart); // Search in IMAP folder: - $email = \MailSo\Imap\SearchCriterias::escapeSearchString($sEmail); $aUids = $this->oImapClient->MessageSimpleSearch("SUBJECT {$sUID}"); -// $aUids = $this->oImapClient->MessageSimpleSearch("OR SUBJECT {$sUID} FROM {$email}"); -// $aUids = $this->oImapClient->MessageSimpleSearch("OR SUBJECT {$sUID} FROM {$email} BODY {$email}"); +/* + $email = \MailSo\Imap\SearchCriterias::escapeSearchString($this->oImapClient, $sEmail); + $aUids = $this->oImapClient->MessageSimpleSearch("OR SUBJECT {$sUID} FROM {$email}"); + $aUids = $this->oImapClient->MessageSimpleSearch("OR SUBJECT {$sUID} FROM {$email} BODY {$email}"); + + $aUids = $this->oImapClient->MessageSimpleSearch('HEADER Subject '.$sUID); + return 1 === \count($aUids) && \is_numeric($aUids[0]) ? (int) $aUids[0] : null; +*/ if ($aUids) { // Replace Message @@ -141,8 +150,27 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt public function GetSuggestions(string $sEmail, string $sSearch, int $iLimit = 20) : array { - // TODO - return []; + $sSearch = \trim($sSearch); + if (2 > \strlen($sSearch) || !$this->SetFolder(/*TODO 'Contacts'*/)) { + return []; + } + + $sSearch = \MailSo\Imap\SearchCriterias::escapeSearchString($this->oImapClient, $sSearch); + $aUids = \array_slice( + $this->oImapClient->MessageSimpleSearch("FROM {$sSearch}"), + 0, $iLimit + ); + + $aResult = []; + foreach ($this->oImapClient->Fetch(['BODY.PEEK[HEADER.FIELDS (FROM)]'], \implode(',', $aUids), true) as $oFetchResponse) { + $oHeaders = new \MailSo\Mime\HeaderCollection($oFetchResponse->GetHeaderFieldsValue()); + $oFrom = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::FROM_, true); + foreach ($oFrom as $oMail) { + $aResult[] = [$oMail->GetEmail(), $oMail->GetDisplayName()]; + } + } + + return $aResult; } public function IncFrec(string $sEmail, array $aEmails, bool $bCreateAuto = true) : bool