Compare commits

...

10 Commits

Author SHA1 Message Date
39daf5524c Write essential hooks for mailu integration 2023-09-27 11:38:50 -07:00
4919aaf8d8 WIP commit 2023-09-05 13:25:24 -07:00
20c991ccf1 Update config.php for mailu 2023-09-05 11:11:17 -07:00
Petter Joelson
74af0aace4 Update Nextcloud version and remove from main menu 2021-06-10 10:31:25 +00:00
Alexander Vassilevski
d326b84408 revert changeUser hook 2021-02-21 19:36:54 -08:00
Alexander Vassilevski
646ff2a029 experimenting with changeUser hook 2021-02-21 19:29:12 -08:00
Alexander Vassilevski
f9e9ba3eb8 Merge branch 'master' of gitlab.com:collective.tools/ctautomailhooks 2021-02-21 19:07:23 -08:00
Alexander Vassilevski
fe013d6eea added password update hook 2021-02-21 19:06:57 -08:00
Alexander Vassilevski
a7b542ead0 fixing error msgs and ensuring return status; 2021-02-21 18:58:13 -08:00
Alexander Vassilevski
14a6f49d58 removing third param; 2021-02-21 18:55:23 -08:00
3 changed files with 90 additions and 113 deletions

View File

@ -12,12 +12,8 @@
<category>tools</category> <category>tools</category>
<bugs>https://github.com/sashetov/ctautomailhooks</bugs> <bugs>https://github.com/sashetov/ctautomailhooks</bugs>
<dependencies> <dependencies>
<nextcloud min-version="15" max-version="15"/> <nextcloud min-version="15" max-version="25"/>
</dependencies> </dependencies>
<navigations> <navigations>
<navigation>
<name>C T Auto Mail Hooks</name>
<route>ctautomailhooks.page.index</route>
</navigation>
</navigations> </navigations>
</info> </info>

View File

@ -4,9 +4,11 @@ use OCP\ILogger;
use \OCP\IConfig; use \OCP\IConfig;
class Config { class Config {
const CONFIG_KEY = 'ct_auto_mail_hooks'; const CONFIG_KEY = 'ct_auto_mail_hooks';
const CONFIG_KEY_GLESYS_KEYS="glesys_keys"; const CONFIG_KEY_MAILU_KEYS="mailu_keys";
const CONFIG_KEY_MAILU_API="mailu_api";
const CONFIG_KEY_EMAIL_ADDR_SUFFIX="email_suffix"; const CONFIG_KEY_EMAIL_ADDR_SUFFIX="email_suffix";
const CONFIG_KEY_EMAIL_ADDR_QUOTA_MB="quota"; const CONFIG_KEY_EMAIL_ADDR_QUOTA_MB="quota";
const DEFAULT_EMAIL_ADDR_QUOTA_MB="200"; const DEFAULT_EMAIL_ADDR_QUOTA_MB="200";
private $logger; private $logger;
private $appConfiguration; private $appConfiguration;
@ -17,8 +19,11 @@ class Config {
$this->appConfiguration = $this->appConfiguration =
$nextCloudConfiguration->getSystemValue(self::CONFIG_KEY); $nextCloudConfiguration->getSystemValue(self::CONFIG_KEY);
} }
public function getGlesysKeys() { public function getMailUKeys() {
return $this->getConfValue(self::CONFIG_KEY_GLESYS_KEYS); return $this->getConfValue(self::CONFIG_KEY_MAILU_KEYS);
}
public function getMailUAPI() {
return $this->getConfValue(self::CONFIG_KEY_MAILU_API);
} }
public function getEmailAddressSuffix() { public function getEmailAddressSuffix() {
return $this->getConfValue(self::CONFIG_KEY_EMAIL_ADDR_SUFFIX); return $this->getConfValue(self::CONFIG_KEY_EMAIL_ADDR_SUFFIX);

View File

@ -24,17 +24,18 @@ class UserHooks {
} }
private function createUserCB() { private function createUserCB() {
return function (\OC\User\User $user, string $password) { return function (\OC\User\User $user, string $password) {
$uid = $user->getUID();
$name = $user->getDisplayName();
$email_suffix = $this->config->getEmailAddressSuffix();
$quota = $this->config->getEmailQuotaMB();
$newuser = array( $newuser = array(
'userid' => $uid, 'email' => $user->getUID() . $this->config->getEmailAddressSuffix(),
'password' => $password, 'displayed_name' => $user->getDisplayName(),
'name' => $name, 'raw_password' => $password,
'email' => $uid . $email_suffix, 'quota_bytes' => $this->config->getEmailQuotaMB() * 1024 * 1024,
'quota' => $quota // in MB 'enabled' => true,
'enable_imap' => true,
'spam_enable' => true,
'spam_mark_as_read' => true,
'spam_threshold' => 80
); );
if($this->createEmailAccount($newuser)) { if($this->createEmailAccount($newuser)) {
$this->logger->warning( $this->logger->warning(
"Automatically created mail account for uid " . $uid "Automatically created mail account for uid " . $uid
@ -49,129 +50,104 @@ class UserHooks {
} }
}; };
} }
private function deleteUserCB() { private function deleteUserCB() {
return function (\OC\User\User $user) { return function (\OC\User\User $user) {
$uid = $user->getUID(); $email = $user->getUID() . $this->config->getEmailAddressSuffix();
$email_suffix = $this->config->getEmailAddressSuffix(); if($this->deleteEmailAccount($email)) {
$email = $uid . $email_suffix; $this->logger->warning("Deleted mail account: " . $email, $this->logContext);
$user_data = array('email' => $email); } else {
if($this->deleteEmailAccount($user_data)) $this->logger->error("Error deleting mail account" . $email);
$this->logger->warning(
"Deleted mail account: " . $email, $this->logContext);
else $this->logger->error("Error deleting mail account" . $uid);
};
}
private function updateEmailPasswordCB() {
return function (
\OC\User\User $user, string $password, string $recoverPassword) {
$uid = $user->getUID();
$email_suffix = $this->config->getEmailAddressSuffix();
$email = $uid . $email_suffix;
$user_data = array(
'email' => $email,
'password' => $password
);
if($this->updateEmailPassword($user_data))
$this->logger->warning(
"Updated glesys password for account: " . $email,
$this->logContext);
else $this->logger->error("Error deleting mail account" . $uid);
};
}
private function createImapFolders($user_data) {
$server = 'mail.glesys.se';
$email = $user_data['email'];
$passw = $user_data['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: ' .
\imap_last_error();
$this->logger->error($message);
} }
} };
\imap_close($mbox);
return true;
} }
private function updateEmailPasswordCB() {
return function (\OC\User\User $user, string $password) {
$email = $user->getUID() . $this->config->getEmailAddressSuffix();
if($this->updateEmailPassword($email, $password)) {
$this->logger->warning("Updated glesys password for account: " . $email, $this->logContext);
} else {
$this->logger->error("Error updating mail password for account" . $email);
}
};
}
private function createEmailAccount($user_data) { private function createEmailAccount($user_data) {
$keys = $this->config->getGlesysKeys(); $ch = curl_init($this.config->getMailUAPI . 'user');
$fields = array( $payload = json_encode($user_data);
'emailaccount' => $user_data['email'],
'password' => $user_data['password'],
'quota' => $user_data['quota']
);
$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_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $keys); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization:' . $this->config->getMailUKeys()));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch); $result = curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch)) {
$message = 'Error:' . curl_error($ch); $message = 'createEmailAccount Error:' . curl_error($ch);
$this->logger->error($message); $this->logger->error($message);
return false; return false;
} }
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
$xml = simplexml_load_string($result);
$code = $xml->status->code; if ($code == 200) {
if ($code == 200) return $this->createImapFolders($user_data); return true;
else $this->logger->warning( }
"Error: non-200 status: " . $xml->status->text);
$this->logger->error("createEmailAccount returned ".$code." message: ".$result);
return false; return false;
} }
private function deleteEmailAccount($user_data){
$ch = curl_init(); private function deleteEmailAccount($user_data) {
$keys = $this->config->getGlesysKeys(); $ch = curl_init($this.config->getMailUAPI . 'user/' . urlencode($user_data->email));
curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this->config->getMailUKeys()));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user_data["email"]); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $keys); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch); $result = curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch)) {
$message = 'Error: ' . curl_error($ch); $message = 'deleteEmailAccount Error:' . curl_error($ch);
$this->logger->error($message); $this->logger->error($message);
return false; return false;
} }
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
return true;
if ($code == 200) {
return true;
}
$this->logger->error("deleteEmailAccount returned ".$code." message: ".$result);
return false;
} }
private function updateEmailPassword($user_data) { private function updateEmailPassword($user_data) {
$keys = $this->config->getGlesysKeys(); $ch = curl_init($this.config->getMailUAPI . 'user/' . urlencode($user_data->email));
$fields = array( $payload = json_encode(array("raw_password" => $user_data['password']));
'emailaccount' => $user_data['email'], curl_setopt($ch, CURLOPT_POSTFIELDS, $payload );
'password' => $user_data['password'] curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
); 'Authorization:' . $this->config->getMailUKeys()));
$query = http_build_query($fields);
$ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_URL, curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
'https://api.glesys.com/email/editaccount/');
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); $result = curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch)) {
$message = 'Error:' . curl_error($ch); $message = 'updateEmailPassword Error:' . curl_error($ch);
$this->logger->error($message); $this->logger->error($message);
return false; return false;
} }
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
$xml = simplexml_load_string($result);
$code = $xml->status->code; if ($code == 200) {
if ($code != 200) return true;
$this->logger->warning( }
"Error: non-200 status: " . $xml->status->text);
$this->logger->error("updateEmailPassword returned ".$code." message: ".$result);
return false;
} }
} }