notify: Fix notification of request initiator
[aur.git] / scripts / notify.py
blob25102a269ee52f6e6b7f8525e481e85106700ed5
1 #!/usr/bin/python3
3 import configparser
4 import email.mime.text
5 import mysql.connector
6 import os
7 import subprocess
8 import sys
9 import textwrap
11 config = configparser.RawConfigParser()
12 config.read(os.path.dirname(os.path.realpath(__file__)) + '/../conf/config')
14 aur_db_host = config.get('database', 'host')
15 aur_db_name = config.get('database', 'name')
16 aur_db_user = config.get('database', 'user')
17 aur_db_pass = config.get('database', 'password')
18 aur_db_socket = config.get('database', 'socket')
20 aur_location = config.get('options', 'aur_location')
21 aur_request_ml = config.get('options', 'aur_request_ml')
23 sendmail = config.get('notifications', 'sendmail')
24 sender = config.get('notifications', 'sender')
25 reply_to = config.get('notifications', 'reply-to')
28 def headers_cc(cclist):
29 return {'Cc': str.join(', ', cclist)}
32 def headers_msgid(thread_id):
33 return {'Message-ID': thread_id}
36 def headers_reply(thread_id):
37 return {'In-Reply-To': thread_id, 'References': thread_id}
40 def send_notification(to, subject, body, refs, headers={}):
41 wrapped = ''
42 for line in body.splitlines():
43 wrapped += textwrap.fill(line, break_long_words=False) + '\n'
44 body = wrapped + '\n' + refs
46 for recipient in to:
47 msg = email.mime.text.MIMEText(body, 'plain', 'utf-8')
48 msg['Subject'] = subject
49 msg['From'] = sender
50 msg['Reply-to'] = reply_to
51 msg['To'] = recipient
53 for key, value in headers.items():
54 msg[key] = value
56 p = subprocess.Popen([sendmail, '-t', '-oi'], stdin=subprocess.PIPE)
57 p.communicate(msg.as_bytes())
60 def username_from_id(cur, uid):
61 cur.execute('SELECT UserName FROM Users WHERE ID = %s', [uid])
62 return cur.fetchone()[0]
65 def pkgbase_from_id(cur, pkgbase_id):
66 cur.execute('SELECT Name FROM PackageBases WHERE ID = %s', [pkgbase_id])
67 return cur.fetchone()[0]
70 def pkgbase_from_pkgreq(cur, reqid):
71 cur.execute('SELECT PackageBaseID FROM PackageRequests WHERE ID = %s',
72 [reqid])
73 return cur.fetchone()[0]
76 def get_user_email(cur, uid):
77 cur.execute('SELECT Email FROM Users WHERE ID = %s', [uid])
78 return cur.fetchone()[0]
81 def get_maintainer_email(cur, pkgbase_id):
82 cur.execute('SELECT Users.Email FROM Users ' +
83 'INNER JOIN PackageBases ' +
84 'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
85 'PackageBases.ID = %s', [pkgbase_id])
86 return cur.fetchone()[0]
89 def get_recipients(cur, pkgbase_id, uid):
90 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
91 'INNER JOIN PackageNotifications ' +
92 'ON PackageNotifications.UserID = Users.ID WHERE ' +
93 'PackageNotifications.UserID != %s AND ' +
94 'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
95 return [row[0] for row in cur.fetchall()]
98 def get_comment_recipients(cur, pkgbase_id, uid):
99 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
100 'INNER JOIN PackageNotifications ' +
101 'ON PackageNotifications.UserID = Users.ID WHERE ' +
102 'Users.CommentNotify = 1 AND ' +
103 'PackageNotifications.UserID != %s AND ' +
104 'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
105 return [row[0] for row in cur.fetchall()]
108 def get_update_recipients(cur, pkgbase_id, uid):
109 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
110 'INNER JOIN PackageNotifications ' +
111 'ON PackageNotifications.UserID = Users.ID WHERE ' +
112 'Users.UpdateNotify = 1 AND ' +
113 'PackageNotifications.UserID != %s AND ' +
114 'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
115 return [row[0] for row in cur.fetchall()]
118 def get_request_recipients(cur, reqid):
119 cur.execute('SELECT DISTINCT Users.Email FROM PackageRequests ' +
120 'INNER JOIN PackageBases ' +
121 'ON PackageBases.ID = PackageRequests.PackageBaseID ' +
122 'INNER JOIN Users ' +
123 'ON Users.ID = PackageRequests.UsersID ' +
124 'OR Users.ID = PackageBases.MaintainerUID ' +
125 'WHERE PackageRequests.ID = %s', [reqid])
126 return [row[0] for row in cur.fetchall()]
129 def get_comment(cur, comment_id):
130 cur.execute('SELECT Comments FROM PackageComments WHERE ID = %s',
131 [comment_id])
132 return cur.fetchone()[0]
135 def get_flagger_comment(cur, pkgbase_id):
136 cur.execute('SELECT FlaggerComment FROM PackageBases WHERE ID = %s',
137 [pkgbase_id])
138 return cur.fetchone()[0]
141 def get_request_comment(cur, reqid):
142 cur.execute('SELECT Comments FROM PackageRequests WHERE ID = %s', [reqid])
143 return cur.fetchone()[0]
146 def get_request_closure_comment(cur, reqid):
147 cur.execute('SELECT ClosureComment FROM PackageRequests WHERE ID = %s',
148 [reqid])
149 return cur.fetchone()[0]
152 def send_resetkey(cur, uid):
153 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
154 [uid])
155 username, to, resetkey = cur.fetchone()
157 subject = 'AUR Password Reset'
158 body = 'A password reset request was submitted for the account %s ' \
159 'associated with your email address. If you wish to reset your ' \
160 'password follow the link [1] below, otherwise ignore this ' \
161 'message and nothing will happen.' % (username)
162 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
164 send_notification([to], subject, body, refs)
167 def welcome(cur, uid):
168 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
169 [uid])
170 username, to, resetkey = cur.fetchone()
172 subject = 'Welcome to the Arch User Repository'
173 body = 'Welcome to the Arch User Repository! In order to set an initial ' \
174 'password for your new account, please click the link [1] below. ' \
175 'If the link does not work, try copying and pasting it into your ' \
176 'browser.'
177 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
179 send_notification([to], subject, body, refs)
182 def comment(cur, uid, pkgbase_id, comment_id):
183 user = username_from_id(cur, uid)
184 pkgbase = pkgbase_from_id(cur, pkgbase_id)
185 to = get_comment_recipients(cur, pkgbase_id, uid)
186 text = get_comment(cur, comment_id)
188 user_uri = aur_location + '/account/' + user + '/'
189 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
191 subject = 'AUR Comment for %s' % (pkgbase)
192 body = '%s [1] added the following comment to %s [2]:' % (user, pkgbase)
193 body += '\n\n' + text + '\n\n'
194 body += 'If you no longer wish to receive notifications about this ' \
195 'package, please go to the package page [2] and select "%s".' % \
196 ('Disable notifications')
197 refs = '[1] ' + user_uri + '\n'
198 refs += '[2] ' + pkgbase_uri
199 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
200 headers = headers_reply(thread_id)
202 send_notification(to, subject, body, refs, headers)
205 def update(cur, uid, pkgbase_id):
206 user = username_from_id(cur, uid)
207 pkgbase = pkgbase_from_id(cur, pkgbase_id)
208 to = get_update_recipients(cur, pkgbase_id, uid)
210 user_uri = aur_location + '/account/' + user + '/'
211 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
213 subject = 'AUR Package Update: %s' % (pkgbase)
214 body = '%s [1] pushed a new commit to %s [2].' % (user, pkgbase)
215 body += '\n\n'
216 body += 'If you no longer wish to receive notifications about this ' \
217 'package, please go to the package page [2] and select "%s".' % \
218 ('Disable notifications')
219 refs = '[1] ' + user_uri + '\n'
220 refs += '[2] ' + pkgbase_uri
221 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
222 headers = headers_reply(thread_id)
224 send_notification(to, subject, body, refs, headers)
227 def flag(cur, uid, pkgbase_id):
228 user = username_from_id(cur, uid)
229 pkgbase = pkgbase_from_id(cur, pkgbase_id)
230 to = [get_maintainer_email(cur, pkgbase_id)]
231 text = get_flagger_comment(cur, pkgbase_id)
233 user_uri = aur_location + '/account/' + user + '/'
234 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
236 subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
237 body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
238 (pkgbase, user)
239 body += '\n\n' + text
240 refs = '[1] ' + pkgbase_uri + '\n'
241 refs += '[2] ' + user_uri
243 send_notification(to, subject, body, refs)
246 def comaintainer_add(cur, pkgbase_id, uid):
247 pkgbase = pkgbase_from_id(cur, pkgbase_id)
248 to = [get_user_email(cur, uid)]
250 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
252 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
253 body = 'You were added to the co-maintainer list of %s [1].' % (pkgbase)
254 refs = '[1] ' + pkgbase_uri + '\n'
256 send_notification(to, subject, body, refs)
259 def comaintainer_remove(cur, pkgbase_id, uid):
260 pkgbase = pkgbase_from_id(cur, pkgbase_id)
261 to = [get_user_email(cur, uid)]
263 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
265 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
266 body = ('You were removed from the co-maintainer list of %s [1].' %
267 (pkgbase))
268 refs = '[1] ' + pkgbase_uri + '\n'
270 send_notification(to, subject, body, refs)
273 def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
274 user = username_from_id(cur, uid)
275 old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
276 if new_pkgbase_id:
277 new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
278 to = get_recipients(cur, old_pkgbase_id, uid)
280 user_uri = aur_location + '/account/' + user + '/'
281 pkgbase_uri = aur_location + '/pkgbase/' + old_pkgbase + '/'
283 subject = 'AUR Package deleted: %s' % (old_pkgbase)
284 if new_pkgbase_id:
285 new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
286 body = '%s [1] merged %s [2] into %s [3].\n\n' \
287 'If you no longer wish receive notifications about the new ' \
288 'package, please go to [3] and click "%s".' %\
289 (user, old_pkgbase, new_pkgbase, 'Disable notifications')
290 refs = '[1] ' + user_uri + '\n'
291 refs += '[2] ' + pkgbase_uri + '\n'
292 refs += '[3] ' + new_pkgbase_uri
293 else:
294 body = '%s [1] deleted %s [2].\n\n' \
295 'You will no longer receive notifications about this ' \
296 'package.' % (user, old_pkgbase)
297 refs = '[1] ' + user_uri + '\n'
298 refs += '[2] ' + pkgbase_uri
300 send_notification(to, subject, body, refs)
303 def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
304 user = username_from_id(cur, uid)
305 pkgbase = pkgbase_from_id(cur, pkgbase_id)
306 to = [aur_request_ml]
307 cc = get_request_recipients(cur, reqid)
308 text = get_request_comment(cur, reqid)
310 user_uri = aur_location + '/account/' + user + '/'
311 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
313 subject = '[PRQ#%d] %s Request for %s' % \
314 (int(reqid), reqtype.title(), pkgbase)
315 if merge_into:
316 merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
317 body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
318 (user, pkgbase, merge_into)
319 body += '\n\n' + text
320 refs = '[1] ' + user_uri + '\n'
321 refs += '[2] ' + pkgbase_uri + '\n'
322 refs += '[3] ' + merge_into_uri
323 else:
324 body = '%s [1] filed a %s request for %s [2]:' % \
325 (user, reqtype, pkgbase)
326 body += '\n\n' + text
327 refs = '[1] ' + user_uri + '\n'
328 refs += '[2] ' + pkgbase_uri + '\n'
329 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
330 # Use a deterministic Message-ID for the first email referencing a request.
331 headers = headers_msgid(thread_id)
332 headers.update(headers_cc(cc))
334 send_notification(to, subject, body, refs, headers)
337 def request_close(cur, uid, reqid, reason):
338 user = username_from_id(cur, uid)
339 to = [aur_request_ml]
340 cc = get_request_recipients(cur, reqid)
341 text = get_request_closure_comment(cur, reqid)
343 user_uri = aur_location + '/account/' + user + '/'
345 subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
346 body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
347 if text.strip() == '':
348 body += '.'
349 else:
350 body += ':\n\n' + text
351 refs = '[1] ' + user_uri
352 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
353 headers = headers_reply(thread_id)
354 headers.update(headers_cc(cc))
356 send_notification(to, subject, body, refs, headers)
359 if __name__ == '__main__':
360 action = sys.argv[1]
361 action_map = {
362 'send-resetkey': send_resetkey,
363 'welcome': welcome,
364 'comment': comment,
365 'update': update,
366 'flag': flag,
367 'comaintainer-add': comaintainer_add,
368 'comaintainer-remove': comaintainer_remove,
369 'delete': delete,
370 'request-open': request_open,
371 'request-close': request_close,
374 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
375 passwd=aur_db_pass, db=aur_db_name,
376 unix_socket=aur_db_socket, buffered=True)
377 cur = db.cursor()
379 action_map[action](cur, *sys.argv[2:])
381 db.commit()
382 db.close()