Initial commit

This commit is contained in:
Alexander Vassilevski 2021-01-23 22:10:44 -08:00
commit 9044313269
8 changed files with 258 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.vim

1
README.md Normal file
View File

@ -0,0 +1 @@
Collective Tools Auto Mail Hooks App for Nextcloud

3
appinfo/app.php Normal file
View File

@ -0,0 +1,3 @@
<?php
$app = new OCA\CTAutoMailHooks\AppInfo\Application();
$app->getContainer()->query('UserHooks')->register();

23
appinfo/info.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<info xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd">
<id>ctautomailhooks</id>
<name>C T Auto Mail Hooks</name>
<summary>creates/updates/deletes mail user for nextcloud on user creation for collective.tools</summary>
<description><![CDATA[creates/updates/deletes mail user for nextcloud on user creation for collective.tools]]></description>
<version>0.0.1</version>
<licence>agpl</licence>
<author mail="alexander@vassilevski.com" >Alex</author>
<namespace>CTAutoMailHooks</namespace>
<category>tools</category>
<bugs>https://github.com/sashetov/ctautomailhooks</bugs>
<dependencies>
<nextcloud min-version="15" max-version="15"/>
</dependencies>
<navigations>
<navigation>
<name>C T Auto Mail Hooks</name>
<route>ctautomailhooks.page.index</route>
</navigation>
</navigations>
</info>

56
img/app.svg Normal file
View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
height="32"
width="32"
version="1"
viewBox="0 0 32 32"
id="svg4"
sodipodi:docname="app.svg"
inkscape:version="0.92.1 r">
<metadata
id="metadata10">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs8" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="789"
inkscape:window-height="480"
id="namedview6"
showgrid="false"
inkscape:zoom="7.375"
inkscape:cx="-8.3389831"
inkscape:cy="16"
inkscape:window-x="0"
inkscape:window-y="27"
inkscape:window-maximized="0"
inkscape:current-layer="svg4" />
<path
d="M13.733 0a.915.915 0 0 0-.933.934V3.6c-1.182.304-2.243.794-3.267 1.4L7.6 3.068a.93.93 0 0 0-1.334 0l-3.2 3.2a.93.93 0 0 0 0 1.334L5 9.535c-.607 1.024-1.097 2.085-1.4 3.267H.933a.915.915 0 0 0-.933.934v4.533c0 .53.403.934.933.934H3.6c.303 1.182.793 2.243 1.4 3.267l-1.934 1.935a.93.93 0 0 0 0 1.333l3.2 3.2a.93.93 0 0 0 1.333 0L9.532 27c1.024.61 2.085 1.097 3.266 1.4v2.667c0 .53.402.933.932.933h4.534c.53 0 .933-.403.933-.935V28.4c1.18-.305 2.24-.795 3.265-1.4L24.4 28.93a.93.93 0 0 0 1.332 0l3.2-3.2a.93.93 0 0 0 0-1.333L27 22.465c.607-1.024 1.096-2.085 1.4-3.266h2.665a.915.915 0 0 0 .935-.933v-4.534a.915.915 0 0 0-.934-.933H28.4c-.304-1.182-.792-2.243-1.4-3.267L28.932 7.6a.93.93 0 0 0 0-1.334l-3.2-3.2a.93.93 0 0 0-1.333 0L22.465 5c-1.024-.607-2.084-1.097-3.266-1.4V.933A.915.915 0 0 0 18.267 0zM16 8.87A7.134 7.134 0 0 1 23.13 16 7.134 7.134 0 0 1 16 23.133c-3.936 0-7.13-3.196-7.13-7.132S12.063 8.87 16 8.87z"
display="block"
fill="#fff"
id="path2"
style="fill:#ffffff" />
</svg>

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -0,0 +1,17 @@
<?php
namespace OCA\CTAutoMailHooks\AppInfo;
use \OCP\AppFramework\App;
use \OCA\CTAutoMailHooks\Hooks\UserHooks;
class Application extends App {
public function __construct(array $urlParams = array()) {
parent::__construct('auto_mail_accounts', $urlParams);
$container = $this->getContainer();
$container->registerService('UserHooks', function($c) {
return new UserHooks(
$c->query('ServerContainer')->getLogger(),
$c->query('OCA\CTAutoMailHooks\Config'),
$c->query('ServerContainer')->getUserManager()
);
});
}
}

25
lib/Config.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace OCA\CTAutoMailHooks;
use OCP\ILogger;
use \OCP\IConfig;
class Config {
const CONFIG_KEY = 'ct_auto_mail_hooks';
const CONFIG_KEY_GLESYS_KEYS="glesys_keys";
const CONFIG_KEY_EMAIL_ADDR_SUFFIX="email_suffix";
private $logger;
private $appConfiguration;
private $logContext = ['app' => '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];
}
}

132
lib/Hooks/UserHooks.php Normal file
View File

@ -0,0 +1,132 @@
<?php
namespace OCA\CTAutoMailHooks\Hooks;
use OCP\ILogger;
use OCP\IUserManager;
use OCA\CTAutoMailHooks\Config;
class UserHooks {
private $logger;
private $config;
private $userManager;
private $logContext = ['app' => '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;
}
}