[aur-dev] [PATCH/RFC 1/2] Port notification routines to Python

Lukas Fleischer lfleischer at archlinux.org
Sun Jun 28 07:59:49 UTC 2015


Use a Python script for sending notification emails. The notification
action and additional parameters are passed via command line arguments.
For comment and package request notifications, the text is passed via
stdin.

Signed-off-by: Lukas Fleischer <lfleischer at archlinux.org>
---
 conf/config.proto            |   5 +
 scripts/notify.py            | 232 +++++++++++++++++++++++++++++++++++++++++++
 web/html/passreset.php       |   9 +-
 web/lib/acctfuncs.inc.php    |  72 ++++++++------
 web/lib/pkgbasefuncs.inc.php | 106 ++------------------
 web/lib/pkgreqfuncs.inc.php  | 114 ++-------------------
 6 files changed, 297 insertions(+), 241 deletions(-)
 create mode 100755 scripts/notify.py

diff --git a/conf/config.proto b/conf/config.proto
index 2fbc27a..52da796 100644
--- a/conf/config.proto
+++ b/conf/config.proto
@@ -31,6 +31,11 @@ snapshot_uri = /cgit/aur.git/snapshot/%s.tar.gz
 enable-maintenance = 1
 maintenance-exceptions = 127.0.0.1
 
+[notifications]
+sendmail = /usr/bin/sendmail
+sender = notify at aur.archlinux.org
+reply-to = noreply at aur.archlinux.org
+
 [fingerprints]
 Ed25519 = SHA256:HQ03dn6EasJHNDlt51KpQpFkT3yBX83x7BoIkA1iv2k
 ECDSA = SHA256:L71Q91yHwmHPYYkJMDgj0xmUuw16qFOhJbBr1mzsiOI
diff --git a/scripts/notify.py b/scripts/notify.py
new file mode 100755
index 0000000..017555a
--- /dev/null
+++ b/scripts/notify.py
@@ -0,0 +1,232 @@
+#!/usr/bin/python3
+
+import configparser
+import email.mime.text
+import mysql.connector
+import os
+import smtplib
+import subprocess
+import sys
+import textwrap
+
+config = configparser.RawConfigParser()
+config.read(os.path.dirname(os.path.realpath(__file__)) + '/../conf/config')
+
+aur_db_host = config.get('database', 'host')
+aur_db_name = config.get('database', 'name')
+aur_db_user = config.get('database', 'user')
+aur_db_pass = config.get('database', 'password')
+aur_db_socket = config.get('database', 'socket')
+
+aur_location = config.get('options', 'aur_location')
+aur_request_ml = config.get('options', 'aur_request_ml')
+
+sendmail = config.get('notifications', 'sendmail')
+sender = config.get('notifications', 'sender')
+reply_to = config.get('notifications', 'reply-to')
+
+
+def send_notification(to, subject, body, cc=None, reference=None):
+    body = str.join('\n', [textwrap.fill(line) for line in body.splitlines()])
+
+    for recipient in to:
+        msg = email.mime.text.MIMEText(body, 'plain', 'utf-8')
+        msg['Subject'] = subject
+        msg['From'] = sender
+        msg['Reply-to'] = reply_to
+        msg['To'] = recipient
+
+        if cc:
+            msg['Cc'] = str.join(', ', cc)
+
+        if reference:
+            msg['In-Reply-To'] = reference
+            msg['References'] = reference
+
+        p = subprocess.Popen([sendmail, '-t', '-oi'], stdin=subprocess.PIPE)
+        p.communicate(msg.as_bytes())
+
+def username_from_id(cur, uid):
+    cur.execute('SELECT UserName FROM Users WHERE ID = %s', [uid])
+    return cur.fetchone()[0]
+
+def pkgbase_from_id(cur, pkgbase_id):
+    cur.execute('SELECT Name FROM PackageBases WHERE ID = %s', [pkgbase_id])
+    return cur.fetchone()[0]
+
+def pkgbase_from_pkgreq(cur, reqid):
+    cur.execute('SELECT PackageBaseID FROM PackageRequests WHERE ID = %s',
+                [reqid])
+    return cur.fetchone()[0]
+
+def get_maintainer_email(cur, pkgbase_id):
+    cur.execute('SELECT Users.Email FROM Users ' +
+                'INNER JOIN PackageBases ' +
+                'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
+                'PackageBases.ID = %s', [pkgbase_id])
+    return cur.fetchone()[0]
+
+def get_recipients(cur, pkgbase_id, uid):
+    cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
+                'INNER JOIN CommentNotify ' +
+                'ON CommentNotify.UserID = Users.ID WHERE ' +
+                'CommentNotify.UserID != %s AND ' +
+                'CommentNotify.PackageBaseID = %s', [uid, pkgbase_id])
+    return [row[0] for row in cur.fetchall()]
+
+def get_request_recipients(cur, pkgbase_id, uid):
+    cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
+                'INNER JOIN PackageBases ' +
+                'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
+                'Users.ID = %s OR PackageBases.ID = %s', [uid, pkgbase_id])
+    return [row[0] for row in cur.fetchall()]
+
+def send_resetkey(cur, uid):
+    cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
+                [uid])
+    username, to, resetkey = cur.fetchone()
+
+    subject = 'AUR Password Reset'
+    body = 'A password reset request was submitted for the account %s ' \
+           'associated with your e-mail address. If you wish to reset your ' \
+           'password follow the link below, otherwise ignore this message ' \
+           'and nothing will happen.' % (username)
+    body += '\n\n'
+    body += '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
+
+    send_notification([to], subject, body)
+
+def welcome(cur, uid):
+    cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
+                [uid])
+    username, to, resetkey = cur.fetchone()
+
+    subject = 'Welcome to the Arch User Repository'
+    body = 'Welcome to %s! In order to set an initial password for your new ' \
+           'account, please click the link below. If the link does not work ' \
+           'try copying and pasting it into your browser.' % (aur_location)
+    body += '\n\n'
+    body += '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
+
+    send_notification([to], subject, body)
+
+def comment(cur, uid, pkgbase_id):
+    user = username_from_id(cur, uid)
+    pkgbase = pkgbase_from_id(cur, pkgbase_id)
+    to = get_recipients(cur, pkgbase_id, uid)
+    text = sys.stdin.read()
+
+    uri = aur_location + '/pkgbase/' + pkgbase + '/'
+
+    subject = 'AUR Comment for %s' % (pkgbase)
+    body = 'from %s\n%s wrote:\n\n%s\n\n--- \n' \
+           'If you no longer wish to receive notifications about this ' \
+           'package, please go the the above package page and click the ' \
+           'UnNotify button.' % (uri, user, text)
+    thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
+
+    send_notification(to, subject, body, reference=thread_id)
+
+def flag(cur, uid, pkgbase_id):
+    user = username_from_id(cur, uid)
+    pkgbase = pkgbase_from_id(cur, pkgbase_id)
+    to = [get_maintainer_email(cur, pkgbase_id)]
+
+    user_uri = aur_location + '/account/' + user + '/'
+    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
+
+    subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
+    body = 'Your package %s has been flagged out of date by %s [1]. ' \
+           'You may view your package at:\n%s\n\n[1] %s' % \
+           (pkgbase, user, pkgbase_uri, user_uri)
+
+    send_notification(to, subject, body)
+
+def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
+    user = username_from_id(cur, uid)
+    old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
+    if new_pkgbase_id:
+        new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
+    to = get_recipients(cur, old_pkgbase_id, uid)
+
+    subject = 'AUR Package deleted: %s' % (old_pkgbase)
+    if new_pkgbase_id:
+        new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
+        body = '%s merged "%s" into "%s".\n\nYou will no longer receive ' \
+               'notifications about this package, please go to %s and click ' \
+               'the Notify button if you wish to recieve them again.' % \
+               (user, old_pkgbase, new_pkgbase, new_pkgbase_uri)
+    else:
+        body = '%s deleted "%s".\n\nYou will no longer receive ' \
+               'notifications about this package.' % (user, old_pkgbase)
+
+    send_notification(to, subject, body)
+
+def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
+    user = username_from_id(cur, uid)
+    pkgbase = pkgbase_from_id(cur, pkgbase_id)
+    to = [aur_request_ml]
+    cc = get_request_recipients(cur, pkgbase_id, uid)
+    text = sys.stdin.read()
+
+    user_uri = aur_location + '/account/' + user + '/'
+    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
+
+    subject = '[PRQ#%d] %s Request for %s' % \
+              (int(reqid), reqtype.title(), pkgbase)
+    if merge_into:
+        merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
+        body = '%s [1] filed a request to merge %s [2] into %s [3]:\n\n' \
+               '%s\n\n[1] %s\n[2] %s\n[3] %s\n' % \
+               (user, pkgbase, merge_into, text, user_uri, pkgbase_uri,
+                merge_into_uri)
+    else:
+        body = '%s [1] filed a %s request for %s [2]:\n\n' \
+               '%s\n\n[1] %s\n[2] %s\n' % \
+               (user, reqtype, pkgbase, text, user_uri, pkgbase_uri)
+    thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
+
+    send_notification(to, subject, body, cc, thread_id)
+
+def request_close(cur, uid, reqid, reason):
+    user = username_from_id(cur, uid)
+    pkgbase_id = pkgbase_from_pkgreq(cur, reqid)
+    to = [aur_request_ml]
+    cc = get_request_recipients(cur, pkgbase_id, uid)
+    text = sys.stdin.read()
+
+    user_uri = aur_location + '/account/' + user + '/'
+
+    subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
+    body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
+    if text.strip() == '':
+        body += '.\n\n'
+    else:
+        body += ':\n\n' + text + '\n\n'
+    body += '[1] ' + user_uri
+    thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
+
+    send_notification(to, subject, body, cc, thread_id)
+
+
+if __name__ == '__main__':
+    action = sys.argv[1]
+    action_map = {
+        'send-resetkey': send_resetkey,
+        'welcome': welcome,
+        'comment': comment,
+        'flag': flag,
+        'delete': delete,
+        'request-open': request_open,
+        'request-close': request_close,
+    }
+
+    db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
+                                 passwd=aur_db_pass, db=aur_db_name,
+                                 unix_socket=aur_db_socket, buffered=True)
+    cur = db.cursor()
+
+    action_map[action](cur, *sys.argv[2:])
+
+    db.commit()
+    db.close()
diff --git a/web/html/passreset.php b/web/html/passreset.php
index 29f2c64..cb2f6bc 100644
--- a/web/html/passreset.php
+++ b/web/html/passreset.php
@@ -46,14 +46,7 @@ if (isset($_GET['resetkey'], $_POST['email'], $_POST['password'], $_POST['confir
 	if (empty($email)) {
 		$error = __('Missing a required field.');
 	} else {
-		$subject = 'AUR Password Reset';
-		$body = __('A password reset request was submitted for the ' .
-			   'account %s associated with your e-mail address. ' .
-			   'If you wish to reset your password follow the ' .
-			   'link below, otherwise ignore this message and ' .
-			   'nothing will happen.', $username);
-		send_resetkey($email, $subject, $body);
-
+		send_resetkey($email);
 		header('Location: ' . get_uri('/passreset/') . '?step=confirm');
 		exit();
 	}
diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php
index 861de0a..bb73c65 100644
--- a/web/lib/acctfuncs.inc.php
+++ b/web/lib/acctfuncs.inc.php
@@ -288,26 +288,14 @@ function process_account_form($TYPE,$A,$U="",$T="",$S="",$E="",$P="",$C="",
 				"<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>");
 		print "<p>\n";
 
-		if (!$send_resetkey) {
+		if ($send_resetkey) {
+			send_resetkey($email, true);
+			print __("A password reset key has been sent to your e-mail address.");
+			print "</p>\n";
+		} else {
 			print __("Click on the Login link above to use your account.");
 			print "</p>\n";
-			return;
 		}
-
-		$subject = 'Welcome to the Arch User Repository';
-		$body = __('Welcome to %s! In order ' .
-			'to set an initial password ' .
-			'for your new account, ' .
-			'please click the link ' .
-			'below. If the link does ' .
-			'not work try copying and ' .
-			'pasting it into your ' .
-			'browser.',
-			aur_location());
-		send_resetkey($email, $subject, $body);
-
-		print __("A password reset key has been sent to your e-mail address.");
-		print "</p>\n";
 	} else {
 		/* Modify an existing account. */
 		$q = "SELECT InactivityTS FROM Users WHERE ";
@@ -705,12 +693,11 @@ function create_resetkey($resetkey, $uid) {
  * Send a reset key to a specific e-mail address
  *
  * @param string $email E-mail address of the user resetting their password
- * @param string $subject Subject of the email
- * @param string $body Body of the email
+ * @param bool $welcome Whether to use the welcome message
  *
  * @return void
  */
-function send_resetkey($email, $subject, $body) {
+function send_resetkey($email, $welcome=false) {
 	$uid = uid_from_email($email);
 	if ($uid == null) {
 		return;
@@ -721,16 +708,7 @@ function send_resetkey($email, $subject, $body) {
 	create_resetkey($resetkey, $uid);
 
 	/* Send e-mail with confirmation link. */
-	$body = wordwrap($body, 70);
-	$body .=  "\n\n". get_uri('/passreset/', true) .
-		  "?resetkey={$resetkey}";
-	$headers = "MIME-Version: 1.0\r\n" .
-		   "Content-type: text/plain; charset=UTF-8\r\n" .
-		   "Reply-to: noreply at aur.archlinux.org\r\n" .
-		   "From: notify at aur.archlinux.org\r\n" .
-		   "X-Mailer: PHP\r\n" .
-		   "X-MimeOLE: Produced By AUR";
-	@mail($email, $subject, $body, $headers);
+	notify(array($welcome ? 'welcome' : 'send-resetkey', $uid));
 }
 
 /**
@@ -1309,3 +1287,37 @@ function account_set_ssh_keys($uid, $ssh_keys, $ssh_fingerprints) {
 
 	return true;
 }
+
+/*
+ * Invoke the email notification script.
+ *
+ * @param string $params Command line parameters for the script.
+ * @param string $text Text to pass via stdin.
+ *
+ * @return void
+ */
+function notify($params, $text='') {
+	$cmd = realpath('../../scripts/notify.py');
+	foreach ($params as $param) {
+		$cmd .= ' ' . escapeshellarg($param);
+	}
+
+	$descspec = array(
+		0 => array('pipe', 'r'),
+		1 => array('pipe', 'w'),
+		2 => array('pipe', 'w')
+	);
+
+	$p = proc_open($cmd, $descspec, $pipes);
+
+	if (!is_resource($p)) {
+		return false;
+	}
+
+	fwrite($pipes[0], $text);
+	fclose($pipes[0]);
+	fclose($pipes[1]);
+	fclose($pipes[2]);
+
+	return proc_close($p);
+}
diff --git a/web/lib/pkgbasefuncs.inc.php b/web/lib/pkgbasefuncs.inc.php
index 92202bf..5d191eb 100644
--- a/web/lib/pkgbasefuncs.inc.php
+++ b/web/lib/pkgbasefuncs.inc.php
@@ -97,37 +97,7 @@ function pkgbase_add_comment($base_id, $uid, $comment) {
 	$bcc = array();
 
 	if ($result) {
-		while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
-			array_push($bcc, $row['Email']);
-		}
-
-		$q = "SELECT Name FROM PackageBases WHERE ID = ";
-		$q.= intval($base_id);
-		$result = $dbh->query($q);
-		$row = $result->fetch(PDO::FETCH_ASSOC);
-
-		/*
-		 * TODO: Add native language emails for users, based on their
-		 * preferences. Simply making these strings translatable won't
-		 * work, users would be getting emails in the language that the
-		 * user who posted the comment was in.
-		 */
-		$body = 'from ' . get_pkgbase_uri($row['Name'], true) . "\n"
-		. username_from_sid($_COOKIE['AURSID']) . " wrote:\n\n"
-		. $comment
-		. "\n\n---\nIf you no longer wish to receive notifications about this package, please go the the above package page and click the UnNotify button.";
-		$body = wordwrap($body, 70);
-		$bcc = implode(', ', $bcc);
-		$thread_id = "<pkg-notifications-" . $row['Name'] . "@aur.archlinux.org>";
-		$headers = "MIME-Version: 1.0\r\n" .
-			   "Content-type: text/plain; charset=UTF-8\r\n" .
-			   "Bcc: $bcc\r\n" .
-			   "Reply-to: noreply at aur.archlinux.org\r\n" .
-			   "From: notify at aur.archlinux.org\r\n" .
-			   "In-Reply-To: $thread_id\r\n" .
-			   "References: $thread_id\r\n" .
-			   "X-Mailer: AUR";
-		@mail('undisclosed-recipients: ;', "AUR Comment for " . $row['Name'], $body, $headers);
+		notify(array('comment', $uid, $base_id), $comment);
 	}
 }
 
@@ -355,33 +325,11 @@ function pkgbase_flag($base_ids) {
 	$q.= " OutOfDateTS = UNIX_TIMESTAMP()";
 	$q.= " WHERE ID IN (" . implode(",", $base_ids) . ")";
 	$q.= " AND OutOfDateTS IS NULL";
+	$dbh->exec($q);
 
-	$affected_pkgs = $dbh->exec($q);
-
-	if ($affected_pkgs > 0) {
-		/* Notify of flagging by e-mail. */
-		$f_name = username_from_sid($_COOKIE['AURSID']);
-		$f_email = email_from_sid($_COOKIE['AURSID']);
-		$f_uid = uid_from_sid($_COOKIE['AURSID']);
-		$q = "SELECT PackageBases.Name, Users.Email ";
-		$q.= "FROM PackageBases, Users ";
-		$q.= "WHERE PackageBases.ID IN (" . implode(",", $base_ids) .") ";
-		$q.= "AND Users.ID = PackageBases.MaintainerUID ";
-		$q.= "AND Users.ID != " . $f_uid;
-		$result = $dbh->query($q);
-		if ($result) {
-			while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
-				$body = "Your package " . $row['Name'] . " has been flagged out of date by " . $f_name . " [1]. You may view your package at:\n" . get_pkgbase_uri($row['Name'], true) . "\n\n[1] - " . get_user_uri($f_name, true);
-				$body = wordwrap($body, 70);
-				$headers = "MIME-Version: 1.0\r\n" .
-					   "Content-type: text/plain; charset=UTF-8\r\n" .
-					   "Reply-to: noreply at aur.archlinux.org\r\n" .
-					   "From: notify at aur.archlinux.org\r\n" .
-					   "X-Mailer: PHP\r\n" .
-					   "X-MimeOLE: Produced By AUR";
-				@mail($row['Email'], "AUR Out-of-date Notification for ".$row['Name'], $body, $headers);
-			}
-		}
+	$uid = uid_from_sid($_COOKIE['AURSID']);
+	foreach ($base_ids as $base_id) {
+		notify(array('flag', $uid, $base_id));
 	}
 
 	return array(true, __("The selected packages have been flagged out-of-date."));
@@ -449,46 +397,12 @@ function pkgbase_delete ($base_ids, $merge_base_id, $via, $grant=false) {
 		$merge_base_name = pkgbase_name_from_id($merge_base_id);
 	}
 
-	/* Send e-mail notifications. */
+	$uid = uid_from_sid($_COOKIE['AURSID']);
 	foreach ($base_ids as $base_id) {
-		$q = "SELECT CommentNotify.*, Users.Email ";
-		$q.= "FROM CommentNotify, Users ";
-		$q.= "WHERE Users.ID = CommentNotify.UserID ";
-		$q.= "AND CommentNotify.UserID != " . uid_from_sid($_COOKIE['AURSID']) . " ";
-		$q.= "AND CommentNotify.PackageBaseID = " . $base_id;
-		$result = $dbh->query($q);
-		$bcc = array();
-
-		while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
-			array_push($bcc, $row['Email']);
-		}
-		if (!empty($bcc)) {
-			$pkgbase_name = pkgbase_name_from_id($base_id);
-
-			/*
-			 * TODO: Add native language emails for users, based on
-			 * their preferences. Simply making these strings
-			 * translatable won't work, users would be getting
-			 * emails in the language that the user who posted the
-			 * comment was in.
-			 */
-			$body = "";
-			if ($merge_base_id) {
-				$body .= username_from_sid($_COOKIE['AURSID']) . " merged \"".$pkgbase_name."\" into \"$merge_base_name\".\n\n";
-				$body .= "You will no longer receive notifications about this package, please go to https://aur.archlinux.org" . get_pkgbase_uri($merge_base_name) . " and click the Notify button if you wish to recieve them again.";
-			} else {
-				$body .= username_from_sid($_COOKIE['AURSID']) . " deleted \"".$pkgbase_name."\".\n\n";
-				$body .= "You will no longer receive notifications about this package.";
-			}
-			$body = wordwrap($body, 70);
-			$bcc = implode(', ', $bcc);
-			$headers = "MIME-Version: 1.0\r\n" .
-				   "Content-type: text/plain; charset=UTF-8\r\n" .
-				   "Bcc: $bcc\r\n" .
-				   "Reply-to: noreply at aur.archlinux.org\r\n" .
-				   "From: notify at aur.archlinux.org\r\n" .
-				   "X-Mailer: AUR";
-			@mail('undisclosed-recipients: ;', "AUR Package deleted: " . $pkgbase_name, $body, $headers);
+		if ($merge_base_id) {
+			notify(array('delete', $uid, $base_id, $merge_base_id));
+		} else {
+			notify(array('delete', $uid, $base_id));
 		}
 	}
 
diff --git a/web/lib/pkgreqfuncs.inc.php b/web/lib/pkgreqfuncs.inc.php
index c056d68..86627f0 100644
--- a/web/lib/pkgreqfuncs.inc.php
+++ b/web/lib/pkgreqfuncs.inc.php
@@ -153,59 +153,12 @@ function pkgreq_file($ids, $type, $merge_into, $comments) {
 	$dbh->exec($q);
 	$request_id = $dbh->lastInsertId();
 
-	/*
-	 * Send e-mail notifications.
-	 * TODO: Move notification logic to separate function where it belongs.
-	 */
-	$cc = array(pkgreq_get_creator_email($request_id));
-
-	$q = "SELECT Users.Email ";
-	$q.= "FROM Users INNER JOIN PackageBases ";
-	$q.= "ON PackageBases.MaintainerUID = Users.ID ";
-	$q.= "WHERE PackageBases.ID = " . $base_id;
-	$result = $dbh->query($q);
-	if ($row = $result->fetch(PDO::FETCH_ASSOC)) {
-		$cc[] = $row['Email'];
+	/* Send e-mail notifications. */
+	$params = array('request-open', $uid, $request_id, $type, $base_id);
+	if ($type === 'merge') {
+		$params[] = $merge_into;
 	}
-
-	$q = "SELECT Name FROM PackageBases WHERE ID = " . $base_id;
-	$result = $dbh->query($q);
-	$row = $result->fetch(PDO::FETCH_ASSOC);
-
-	/*
-	 * TODO: Add native language emails for users, based on their
-	 * preferences. Simply making these strings translatable won't
-	 * work, users would be getting emails in the language that the
-	 * user who posted the comment was in.
-	 */
-	$username = username_from_sid($_COOKIE['AURSID']);
-	if ($type == 'merge') {
-		$body =
-			$username . " [1] filed a request to merge " .
-			$row['Name'] . " [2] into " . $merge_into .
-			" [3]:\n\n" .  $comments . "\n\n" .
-			"[1] " . get_user_uri($username, true) . "\n" .
-			"[2] " . get_pkgbase_uri($row['Name'], true) . "\n" .
-			"[3] " . get_pkgbase_uri($merge_into, true) . "\n";
-	} else {
-		$body =
-			$username . " [1] filed a " . $type . " request for " .
-			$row['Name'] . " [2]:\n\n" . $comments . "\n\n" .
-			"[1] " . get_user_uri($username, true) . "\n" .
-			"[2] " . get_pkgbase_uri($row['Name'], true) . "\n";
-	}
-	$body = wordwrap($body, 70);
-	$cc = array_unique($cc);
-	$headers = "MIME-Version: 1.0\r\n" .
-		   "Content-type: text/plain; charset=UTF-8\r\n" .
-		   "Cc: " . implode(', ', $cc) . "\r\n";
-	$thread_id = "<pkg-request-" . $request_id . "@aur.archlinux.org>";
-	$headers .= "From: notify at aur.archlinux.org\r\n" .
-		    "Message-ID: $thread_id\r\n" .
-		    "X-Mailer: AUR";
-	$ml = config_get('options', 'aur_request_ml');
-	@mail($ml, "[PRQ#" . $request_id . "] " . ucfirst($type) .
-		   " Request for " .  $row['Name'], $body, $headers);
+	notify($params);
 
 	$auto_orphan_age = config_get('options', 'auto_orphan_age');
 	$auto_delete_age = config_get('options', 'auto_delete_age');
@@ -277,61 +230,8 @@ function pkgreq_close($id, $reason, $comments, $auto_close=false) {
 	$q.= "WHERE ID = " . intval($id);
 	$dbh->exec($q);
 
-	/*
-	 * Send e-mail notifications.
-	 * TODO: Move notification logic to separate function where it belongs.
-	 */
-	$cc = array(pkgreq_get_creator_email($id));
-
-	$q = "SELECT Users.Email ";
-	$q.= "FROM Users INNER JOIN PackageBases ";
-	$q.= "ON PackageBases.MaintainerUID = Users.ID ";
-	$q.= "INNER JOIN PackageRequests ";
-	$q.= "ON PackageRequests.PackageBaseID = PackageBases.ID ";
-	$q.= "WHERE PackageRequests.ID = " . $id;
-	$result = $dbh->query($q);
-	if ($row = $result->fetch(PDO::FETCH_ASSOC)) {
-		$cc[] = $row['Email'];
-	}
-
-	/*
-	 * TODO: Add native language emails for users, based on their
-	 * preferences. Simply making these strings translatable won't
-	 * work, users would be getting emails in the language that the
-	 * user who posted the comment was in.
-	 */
-	if ($auto_close) {
-		$body = "Request #" . intval($id) . " has been " . $reason .
-			" automatically by the Arch User Repository package " .
-			"request system";
-	} else {
-		$username = username_from_sid($_COOKIE['AURSID']);
-		$body = "Request #" . intval($id) . " has been " . $reason .
-			" by " . $username . " [1]";
-	}
-	if (!empty(trim($comments))) {
-		$body .= ":\n\n" . $comments . "\n";
-	} else {
-		$body .= ".\n";
-	}
-	if (!$auto_close) {
-		$body .= "\n";
-		$body .= "[1] " . get_user_uri($username, true);
-		$body .= "\n";
-	}
-	$body = wordwrap($body, 70);
-	$cc = array_unique($cc);
-	$headers = "MIME-Version: 1.0\r\n" .
-		   "Content-type: text/plain; charset=UTF-8\r\n" .
-		   "Cc: " . implode(', ', $cc) . "\r\n";
-	$thread_id = "<pkg-request-" . $id . "@aur.archlinux.org>";
-	$headers .= "From: notify at aur.archlinux.org\r\n" .
-		    "In-Reply-To: $thread_id\r\n" .
-		    "References: $thread_id\r\n" .
-		    "X-Mailer: AUR";
-	$ml = config_get('options', 'aur_request_ml');
-	@mail($ml, "[PRQ#" . $id . "] Request " . ucfirst($reason),
-	      $body, $headers);
+	/* Send e-mail notifications. */
+	notify(array('request-close', $uid, $request_id, $reason), $comments);
 
 	return array(true, __("Request closed successfully."));
 }
-- 
2.4.4


More information about the aur-dev mailing list