Added example plugin how to use Kolab for contact suggestions

This commit is contained in:
the-djmaze 2022-05-13 00:49:25 +02:00
parent f7ba43437e
commit 5c9b7d7000
4 changed files with 141 additions and 6 deletions

View file

@ -0,0 +1,48 @@
<?php
class KolabContactsSuggestions implements \RainLoop\Providers\Suggestions\ISuggestions
{
// TODO: make setting
public $sFolderName = 'Contacts';
public function Process(\RainLoop\Model\Account $oAccount, string $sQuery, int $iLimit = 20): array
{
$sQuery = \trim($sQuery);
if (2 > \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;
}
}

20
plugins/kolab/LICENSE Normal file
View file

@ -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.

39
plugins/kolab/index.php Normal file
View file

@ -0,0 +1,39 @@
<?php
class KolabPlugin extends \RainLoop\Plugins\AbstractPlugin
{
const
NAME = 'Kolab',
VERSION = '0.1',
RELEASE = '2022-05-13',
CATEGORY = 'Security',
DESCRIPTION = 'Get contacts suggestions from Kolab.',
REQUIRED = '2.15.2';
public function Init() : void
{
$this->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();
// }
}
}
}

View file

@ -8,7 +8,9 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
{ {
use CardDAV; use CardDAV;
protected $sFolderName; protected
$oImapClient,
$sFolderName;
function __construct(\MailSo\Imap\ImapClient $oImapClient) function __construct(\MailSo\Imap\ImapClient $oImapClient)
{ {
@ -21,9 +23,11 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
if ($metadata && 'contact' !== \array_shift($metadata)) { if ($metadata && 'contact' !== \array_shift($metadata)) {
// Throw error // Throw error
// $this->oImapClient->FolderList() : array // $this->oImapClient->FolderList() : array
return false;
} }
$this->oImapClient->FolderSelect($sFolderName); $this->oImapClient->FolderSelect($sFolderName);
$this->sFolderName = $sFolderName; $this->sFolderName = $sFolderName;
return true;
} }
public function IsSupported() : bool public function IsSupported() : bool
@ -84,10 +88,15 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
$oMessage->SubParts->append($oPart); $oMessage->SubParts->append($oPart);
// Search in IMAP folder: // Search in IMAP folder:
$email = \MailSo\Imap\SearchCriterias::escapeSearchString($sEmail);
$aUids = $this->oImapClient->MessageSimpleSearch("SUBJECT {$sUID}"); $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) { if ($aUids) {
// Replace Message // Replace Message
@ -141,10 +150,29 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
public function GetSuggestions(string $sEmail, string $sSearch, int $iLimit = 20) : array public function GetSuggestions(string $sEmail, string $sSearch, int $iLimit = 20) : array
{ {
// TODO $sSearch = \trim($sSearch);
if (2 > \strlen($sSearch) || !$this->SetFolder(/*TODO 'Contacts'*/)) {
return []; 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 public function IncFrec(string $sEmail, array $aEmails, bool $bCreateAuto = true) : bool
{ {
return false; return false;