From fbb834c75e74c6ecdf99a4746bfe7ba3105a59b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=EF=BF=BD=EF=BF=BD?= Date: Fri, 16 May 2014 19:40:14 +0000 Subject: [PATCH] add script to create emails by hand from the commandline --- .gitattributes | 1 + src/mail_add.php | 147 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/mail_add.php diff --git a/.gitattributes b/.gitattributes index 2b205bfe..75c54189 100644 --- a/.gitattributes +++ b/.gitattributes @@ -654,6 +654,7 @@ src/generate_apache_conf.php -text src/generate_bind_conf.php -text src/inotify_do_actions.sh -text src/inotify_update_domains.sh -text +src/mail_add.php -text src/mail_dodelete.php -text src/mem_add -text src/mem_del -text diff --git a/src/mail_add.php b/src/mail_add.php new file mode 100644 index 00000000..47ff2b9f --- /dev/null +++ b/src/mail_add.php @@ -0,0 +1,147 @@ +#!/usr/bin/php -q + ... + +email: full email adress, including domain, which must exist +alias: one or many aliases the email should forward to, space separated + +EOF; + error_log($u); + exit(1); +} + +if (count($argv) < 2) { + usage(); +} + +$user = preg_split('/@/', $argv[1])[0]; // first argument is user@domain +$domain = preg_split('/@/', $argv[1])[1]; +$recipients = array_slice($argv, 2); // rest is recipients + +// there's no function to do that, oddly enough... +// there's one to extract the compte from the mail_id (!) but we +// haven't created it yet... +$db->query('SELECT id,compte FROM domaines WHERE domaine="'.addslashes($domain).'"'); +if ($db->next_record()) { + $compte = $db->f('compte'); + $domain_id = $db->f('id'); +} +else { + error_log("domain $domain not found"); + exit(2); +} + +foreach ($generators as $generator) { + $password = `$generator 2>/dev/null`; + if (!is_null($password) and strlen($password) > 7) { + $password = trim($password); + break; + } +} +if (is_null($password)) { + error_log('password generators failed to produce 8 characters: ' . join("\n", $generators)); + exit(3); +} + +/* need not to be $quota because that would replace alternc's global + * $quota... even though we don't say global $quota anywhere here, yay + * php scoping. + */ +$quotas = $default_quotas; +$r = join(", ", $recipients); + +print <<su($compte); +print "cuid: $cuid\n"; + +/* function signature is: + * function create($dom_id, $mail,$type="",$dontcheck=false) + * yet $type is never passed anywhere and is actually empty in the + * database (!) $dontcheck is undocumented, so we'll ignore it + * + * also, this function explicitely tells me to not use it, but doesn't + * provide an alternative. i choose to disobey instead of rewriting it + * from scratch + */ +if (!($mail_id = $mail->create($domain_id, $user))) { + error_log('failed to create: ' . $err->errstr()); + exit(4); +} + +/* function set_passwd($mail_id,$pass) + * + * just set the password + * + * no idea why this is a different function. + */ +if (!$mail->set_passwd($mail_id,$password)) { + error_log("failed to set password on mail $mail_id: " . $err->errstr()); + exit(5); +} + +/* function set_details($mail_id, $islocal, $quotamb, + * $recipients,$delivery="dovecot",$dontcheck=false) + * + * you read that right, recipients is a string (!) + * + * if we have no aliases, it's a mailbox. deal with it. + */ +if (!$mail->set_details($mail_id, !count($recipients), $quota, join("\n", $recipients))) { + error_log('failed to set details: ' . $err->errstr()); + exit(6); +} + +// maybe we need to call the hooks? i don't know! + /* $rh=$hooks->invoke("mail_edit_post",array($mail_id)); */ + /* if (in_array(false,$res,true)) { */ + /* include ("mail_edit.php"); */ + /* exit(); */ + /* } else { */ + /* foreach($rh as $h) if ($h) $error.=$h."
"; */ + /* } */ +