commit 90443132693ef9006cc3a4446e9cfc2803d5b3b3 Author: Alexander Vassilevski Date: Sat Jan 23 22:10:44 2021 -0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f97279 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.vim diff --git a/README.md b/README.md new file mode 100644 index 0000000..fb5950b --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Collective Tools Auto Mail Hooks App for Nextcloud diff --git a/appinfo/app.php b/appinfo/app.php new file mode 100644 index 0000000..57b0d8b --- /dev/null +++ b/appinfo/app.php @@ -0,0 +1,3 @@ +getContainer()->query('UserHooks')->register(); diff --git a/appinfo/info.xml b/appinfo/info.xml new file mode 100644 index 0000000..f2a2ba7 --- /dev/null +++ b/appinfo/info.xml @@ -0,0 +1,23 @@ + + + ctautomailhooks + C T Auto Mail Hooks + creates/updates/deletes mail user for nextcloud on user creation for collective.tools + + 0.0.1 + agpl + Alex + CTAutoMailHooks + tools + https://github.com/sashetov/ctautomailhooks + + + + + + C T Auto Mail Hooks + ctautomailhooks.page.index + + + diff --git a/img/app.svg b/img/app.svg new file mode 100644 index 0000000..fe370f8 --- /dev/null +++ b/img/app.svg @@ -0,0 +1,56 @@ + + + + + + image/svg+xml + + + + + + + + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php new file mode 100644 index 0000000..0f41c5b --- /dev/null +++ b/lib/AppInfo/Application.php @@ -0,0 +1,17 @@ +getContainer(); + $container->registerService('UserHooks', function($c) { + return new UserHooks( + $c->query('ServerContainer')->getLogger(), + $c->query('OCA\CTAutoMailHooks\Config'), + $c->query('ServerContainer')->getUserManager() + ); + }); + } +} diff --git a/lib/Config.php b/lib/Config.php new file mode 100644 index 0000000..08b8bac --- /dev/null +++ b/lib/Config.php @@ -0,0 +1,25 @@ + 'ct_auto_mail_hooks']; + public function __construct(ILogger $logger, IConfig $nextCloudConfiguration) { + $this->logger = $logger; + $this->appConfiguration = $nextCloudConfiguration->getSystemValue(self::CONFIG_KEY); + } + public function getGlesysKeys() { + return $this->getConfValue(self::CONFIG_KEY_GLESYS_KEYS); + } + public function getEmailAddressSuffix() { + return $this->getConfValue(self::CONFIG_KEY_EMAIL_ADDR_SUFFIX); + } + private function getConfValue($configKey) { + return $this->appConfiguration[$configKey]; + } +} diff --git a/lib/Hooks/UserHooks.php b/lib/Hooks/UserHooks.php new file mode 100644 index 0000000..96ad3c1 --- /dev/null +++ b/lib/Hooks/UserHooks.php @@ -0,0 +1,132 @@ + 'ct_auto_mail_hooks']; + public function __construct( + ILogger $logger, Config $config, IUserManager $userManager) { + $this->logger = $logger; + $this->config = $config; + $this->userManager = $userManager; + } + public function register() { + $this->userManager->listen( + '\OC\User', 'postCreateUser',$this->createUserCB()); + $this->userManager->listen( + '\OC\User', 'preDelete', $this->deleteUserCB()); + } + private function createUserCB() { + return function (\OC\User\User $user, string $password) { + $uid = $user->getUID(); + $name = $user->getDisplayName(); + $email_suffix = $this->config->getEmailAddressSuffix(); + $newuser = array( + 'userid' => $uid, + 'password' => $password, + 'name' => $name, + 'email' => $uid . $email_suffix + ); + $this->logger->warning("newuser:". print_r($newuser,1)); + if($this->createEmailAccount($newuser)) { + $this->logger->warning( + "Automatically created mail account for uid " . $uid + . " with e-mail address \"" . $newuser["email"] + . " e-mail address suffix was \"". $email_suffix . "\"." + , $this->logContext); + } + else { + $this->logger->error( + "Error creating mail account OR mail folders for uid " + . $uid); + } + }; + } + private function deleteUserCB() { + return function (\OC\User\User $user) { + $uid = $user->getUID(); + $email_suffix = $this->config->getEmailAddressSuffix(); + $email = $uid . $email_suffix; + $user_data = array('email' => $email); + $this->logger->warning("user_data:". print_r($user_data,1)); + if($this->deleteEmailAccount($user_data)) + $this->logger->warning( + "Deleted mail account: " . $email, $this->logContext); + else $this->logger->error("Error deleting mail account" . $uid); + }; + } + private function createImapFolders($newuser) { + $server = 'mail.glesys.se'; + $email = $newuser['email']; + $passw = $newuser['password']; + $folders = array('Sent', 'Drafts', 'Trash', 'Junk', 'Archive'); + $mbox = \imap_open("{" . $server . "}", $email, $passw, OP_HALFOPEN); + if(!$mbox){ + $message = "can't connect: " . \imap_last_error(); + $this->logger->error($message); + return false; + } + foreach ($folders as $folder) { + if (! @\imap_createmailbox( $mbox, + \imap_utf7_encode("{" . $server . "}INBOX." . $folder ))) { + $message = 'Error creating ' . $folder . ' folder.'; + $this->logger->error($message); + return false; + } + } + \imap_close($mbox); + return true; + } + private function createEmailAccount($newuser) { + $keys = $this->config->getGlesysKeys(); + $this->logger->debug("glesysKeys:" . $keys); + $fields = array( + 'emailaccount' => $newuser['email'], + 'password' => $newuser['password'], + 'quota' => 1000 + ); + $this->logger->warning("fields:" . print_r($fields, 1)); + $query = http_build_query($fields); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, + 'https://api.glesys.com/email/createaccount/'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_USERPWD, $keys); + curl_setopt($ch, CURLOPT_POSTFIELDS, $query); + $result = curl_exec($ch); + if (curl_errno($ch)) { + $message = 'Error:' . curl_error($ch); + $this->logger->error($message); + return false; + } + curl_close($ch); + $xml = simplexml_load_string($result); + $code = $xml->status->code; + $this->logger->warning("xml status text:" . $xml->status->text); + if ($code == 200) return $this->createImapFolders($newuser); + return false; + } + private function deleteEmailAccount($user){ + $ch = curl_init(); + $keys = $this->config->getGlesysKeys(); + $this->logger->debug("glesysKeys:" . $keys); + curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user["email"]); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_USERPWD, $keys); + $result = curl_exec($ch); + if (curl_errno($ch)) { + $message = 'Error:' . curl_error($ch); + $this->logger->error($message); + return false; + } + curl_close($ch); + return true; + } +}