Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft/dev sync nextcloud addressbook #2

Merged
171 changes: 118 additions & 53 deletions plugins/nextcloud/NextcloudAddressBook.php
Original file line number Diff line number Diff line change
@@ -1,90 +1,127 @@
<?php

use RainLoop\Providers\AddressBook\Classes\Contact;

class NextcloudAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInterface
{
use RainLoop\Providers\AddressBook\CardDAV;
private const URI = 'webmail';

private const SETTINGS_KEY = 'nextcloudAddressBookUri';

private string $defaultUri;
private string $defaultName;
private string $defaultDescription;
private bool $ignoreSystemAddressBook;
private $contactsManager;
private $key;

function __construct()
function __construct(string $defaultUri = 'webmail', string $defaultName = 'WebMail', string $defaultDescription = 'Recipients from snappymail', bool $ignoreSystemAddressBook = true)
{
$this->defaultUri = $defaultUri;
$this->defaultName = $defaultName;
$this->defaultDescription = $defaultDescription;
$this->ignoreSystemAddressBook = $ignoreSystemAddressBook;

$this->GetSavedAddressBookKey();
}

private function getContactsManager()
{
$this->contactsManager = \OC::$server->getContactsManager();
if ($this->contactsManager == null) {
$this->contactsManager = \OC::$server->getContactsManager();
}

return $this->contactsManager;
}

foreach($this->contactsManager->getUserAddressBooks() as $addressBook) {
if($addressBook->isSystemAddressBook()) {
$this->contactsManager->unregisterAddressBook($addressBook);
private function GetSavedAddressBookKey() : string
{
if ($this->ignoreSystemAddressBook) {
foreach ($this->getContactsManager()->getUserAddressBooks() as $addressBook) {
if ($addressBook->isSystemAddressBook()) {
$this->getContactsManager()->unregisterAddressBook($addressBook);
}
}
}

$uid = \OC::$server->getUserSession()->getUser()->getUID();
$cardDavBackend = \OC::$server->get(\OCA\DAV\CardDAV\CardDavBackend::class);
$principalUri = 'principals/users/'. $uid;
$addressBookId = $cardDavBackend->getAddressBooksByUri($principalUri, self::URI);
$principalUri = 'principals/users/' . $uid;
$uri = $this->GetSavedUri();
$addressBookId = $cardDavBackend->getAddressBooksByUri($principalUri, $uri);

if ($addressBookId === null) {
$addressBookId = $cardDavBackend->createAddressBook($principalUri, self::URI, array_filter([
'{DAV:}displayname' => 'Webmail',
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => 'Recipients from snappymail',
]));
} else {
$addressBookId = $addressBookId['id'];
return $cardDavBackend->createAddressBook($principalUri, $uri, array_filter([
'{DAV:}displayname' => $this->defaultName,
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => $this->defaultDescription,
]));
}
$this->key = $addressBookId;

return $addressBookId['id'];
}

public function IsSupported() : bool {
public function IsSupported() : bool
{
// Maybe just return true, contacts app is just a frontend
//return \OC::$server->getAppManager()->isEnabledForUser('contacts');
return true;
return \OC::$server->getAppManager()->isEnabledForUser('contacts');
}

public function SetEmail(string $sEmail) : bool {
public function SetEmail(string $sEmail) : bool
{
return true;
}

public function Sync() : bool {
public function Sync() : bool
{
return false;
}

public function Export(string $sType = 'vcf') : bool {
public function Export(string $sType = 'vcf') : bool
{
return false;
}

public function ContactSave(Contact $oContact) : bool {
public function ContactSave(Contact $oContact) : bool
{
return false;
}

public function DeleteContacts(array $aContactIds) : bool {
public function DeleteContacts(array $aContactIds) : bool
{
return false;
}

public function DeleteAllContacts(string $sEmail) : bool {
public function DeleteAllContacts(string $sEmail) : bool
{
return false;
}

public function GetContacts(int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array {
public function GetContacts(int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array
{
return [];
}

public function GetContactByEmail(string $sEmail) : ?Contact {
public function GetContactByEmail(string $sEmail) : ?Contact
{
return null;
}

public function GetContactByID($mID, bool $bIsStrID = false) : ?Contact {
public function GetContactByID($mID, bool $bIsStrID = false) : ?Contact
{
return null;
}

public function GetSuggestions(string $sSearch, int $iLimit = 20) : array {
public function GetSuggestions(string $sSearch, int $iLimit = 20) : array
{
return [];
}

private function GetEmailObjects(array $aEmails) : array {
private function GetEmailObjects(array $aEmails) : array
{
$aEmailsObjects = \array_map(function ($mItem) {
$oResult = null;
try {
$oResult = \MailSo\Mime\Email::Parse(\trim($mItem));
}
catch (\Throwable $oException) {
} catch (\Throwable $oException) {
unset($oException);
}
return $oResult;
Expand All @@ -100,42 +137,70 @@ private function GetEmailObjects(array $aEmails) : array {
* Add/increment email address usage
* Handy for "most used" sorting suggestions in PdoAddressBook
*/
public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool {
public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool
{
if ($bCreateAuto) {
$aEmailsObjects = $this->GetEmailObjects($aEmails);

if (!count($aEmailsObjects)) {
return false;
}
foreach ($aEmailsObjects as $oEmail) {
if ('' === \trim($oEmail->GetEmail())) {
continue;
}
$sEmail = \trim($oEmail->GetEmail(true));
$existingResults = $this->contactsManager->search($sEmail, ['EMAIL'], ['strict_search' => true]);

if (!empty($existingResults)) {
continue;
}

$properties = [
'EMAIL' => $sEmail,
'FN' => $sEmail
];

if ('' !== \trim($oEmail->GetDisplayName())) {
$properties['FN'] = $oEmail->GetDisplayName();
}
$this->contactsManager->createOrUpdate($properties, $this->key);
foreach ($aEmailsObjects as $oEmail) {
$this->createOrUpdateContact($oEmail);
}

return true;
}

return false;
}

public function Test() : string {
private function createOrUpdateContact($oEmail)
{
if ('' === \trim($oEmail->GetEmail())) {
return;
}
$sEmail = \trim($oEmail->GetEmail(true));
$existingResults = $this->getContactsManager()->search($sEmail, ['EMAIL'], ['strict_search' => true]);

if (!empty($existingResults)) {
return;
}

$properties = [
'EMAIL' => $sEmail,
'FN' => $sEmail
];

if ('' !== \trim($oEmail->GetDisplayName())) {
$properties['FN'] = $oEmail->GetDisplayName();
}
$this->getContactsManager()->createOrUpdate($properties, $this->GetSavedAddressBookKey());
}

public function Test() : string
{
return '';
}

private function Account() : \RainLoop\Model\Account
{
return \RainLoop\Api::Actions()->getAccountFromToken();
}

private function SettingsProvider() : \RainLoop\Providers\Settings
{
return \RainLoop\Api::Actions()->SettingsProvider(true);
}

private function Settings() : \RainLoop\Settings
{
return $this->SettingsProvider()->Load($this->Account());
}

private function GetSavedUri() : string
{
return $this->Settings()->GetConf(self::SETTINGS_KEY, $this->defaultUri);
}
}
14 changes: 14 additions & 0 deletions plugins/nextcloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SnappyMail plugin for nextcloud

## Nextcloud Addressbook for recipients

This plugin can let user to choose which nextcloud addressbook to use save recipients. This is opt-in feature (enabled by admin). After admin enable this, user will find a dropdown in his/her SnappyMail's `Contacts` section, containing all his/her addressbook.

### Admin settings

- `enableNcAddressbook` : Enable User to choose Nextcloud addressbook for recipients. Default value: `false`
- `disableSnappymailContactsUI` : Disable SnappyMail internal addressbook. This is recomended if nextcloud addressbook is being used. Default value: `false`
- `defaultNCAddressbookUri` : Default nextcloud addressbook URI for recipients. Default value: `webmail`
- `defaultNCAddressbookName` : Default nextcloud addressbook Name for recipients. Default value: `WebMail`
- `defaultNCAddressbookDescription` : Default nextcloud addressbook description for recipients. Default value: `Recipients from snappymail`

Loading
Loading