Add missing database and account fields
[aur.git] / scripts / notify.py
blob5e5f3772d70c5063101e0babaad7f51747fe25fd
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_ownership_recipients(cur, pkgbase_id, uid):
119 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
120 'INNER JOIN PackageNotifications ' +
121 'ON PackageNotifications.UserID = Users.ID WHERE ' +
122 'Users.OwnershipNotify = 1 AND ' +
123 'PackageNotifications.UserID != %s AND ' +
124 'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
125 return [row[0] for row in cur.fetchall()]
128 def get_request_recipients(cur, reqid):
129 cur.execute('SELECT DISTINCT Users.Email FROM PackageRequests ' +
130 'INNER JOIN PackageBases ' +
131 'ON PackageBases.ID = PackageRequests.PackageBaseID ' +
132 'INNER JOIN Users ' +
133 'ON Users.ID = PackageRequests.UsersID ' +
134 'OR Users.ID = PackageBases.MaintainerUID ' +
135 'WHERE PackageRequests.ID = %s', [reqid])
136 return [row[0] for row in cur.fetchall()]
139 def get_comment(cur, comment_id):
140 cur.execute('SELECT Comments FROM PackageComments WHERE ID = %s',
141 [comment_id])
142 return cur.fetchone()[0]
145 def get_flagger_comment(cur, pkgbase_id):
146 cur.execute('SELECT FlaggerComment FROM PackageBases WHERE ID = %s',
147 [pkgbase_id])
148 return cur.fetchone()[0]
151 def get_request_comment(cur, reqid):
152 cur.execute('SELECT Comments FROM PackageRequests WHERE ID = %s', [reqid])
153 return cur.fetchone()[0]
156 def get_request_closure_comment(cur, reqid):
157 cur.execute('SELECT ClosureComment FROM PackageRequests WHERE ID = %s',
158 [reqid])
159 return cur.fetchone()[0]
162 def send_resetkey(cur, uid):
163 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
164 [uid])
165 username, to, resetkey = cur.fetchone()
167 subject = 'AUR Password Reset'
168 body = 'A password reset request was submitted for the account %s ' \
169 'associated with your email address. If you wish to reset your ' \
170 'password follow the link [1] below, otherwise ignore this ' \
171 'message and nothing will happen.' % (username)
172 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
174 send_notification([to], subject, body, refs)
177 def welcome(cur, uid):
178 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
179 [uid])
180 username, to, resetkey = cur.fetchone()
182 subject = 'Welcome to the Arch User Repository'
183 body = 'Welcome to the Arch User Repository! In order to set an initial ' \
184 'password for your new account, please click the link [1] below. ' \
185 'If the link does not work, try copying and pasting it into your ' \
186 'browser.'
187 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
189 send_notification([to], subject, body, refs)
192 def comment(cur, uid, pkgbase_id, comment_id):
193 user = username_from_id(cur, uid)
194 pkgbase = pkgbase_from_id(cur, pkgbase_id)
195 to = get_comment_recipients(cur, pkgbase_id, uid)
196 text = get_comment(cur, comment_id)
198 user_uri = aur_location + '/account/' + user + '/'
199 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
201 subject = 'AUR Comment for %s' % (pkgbase)
202 body = '%s [1] added the following comment to %s [2]:' % (user, pkgbase)
203 body += '\n\n' + text + '\n\n'
204 body += 'If you no longer wish to receive notifications about this ' \
205 'package, please go to the package page [2] and select "%s".' % \
206 ('Disable notifications')
207 refs = '[1] ' + user_uri + '\n'
208 refs += '[2] ' + pkgbase_uri
209 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
210 headers = headers_reply(thread_id)
212 send_notification(to, subject, body, refs, headers)
215 def update(cur, uid, pkgbase_id):
216 user = username_from_id(cur, uid)
217 pkgbase = pkgbase_from_id(cur, pkgbase_id)
218 to = get_update_recipients(cur, pkgbase_id, uid)
220 user_uri = aur_location + '/account/' + user + '/'
221 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
223 subject = 'AUR Package Update: %s' % (pkgbase)
224 body = '%s [1] pushed a new commit to %s [2].' % (user, pkgbase)
225 body += '\n\n'
226 body += 'If you no longer wish to receive notifications about this ' \
227 'package, please go to the package page [2] and select "%s".' % \
228 ('Disable notifications')
229 refs = '[1] ' + user_uri + '\n'
230 refs += '[2] ' + pkgbase_uri
231 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
232 headers = headers_reply(thread_id)
234 send_notification(to, subject, body, refs, headers)
237 def flag(cur, uid, pkgbase_id):
238 user = username_from_id(cur, uid)
239 pkgbase = pkgbase_from_id(cur, pkgbase_id)
240 to = [get_maintainer_email(cur, pkgbase_id)]
241 text = get_flagger_comment(cur, pkgbase_id)
243 user_uri = aur_location + '/account/' + user + '/'
244 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
246 subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
247 body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
248 (pkgbase, user)
249 body += '\n\n' + text
250 refs = '[1] ' + pkgbase_uri + '\n'
251 refs += '[2] ' + user_uri
253 send_notification(to, subject, body, refs)
256 def adopt(cur, pkgbase_id, uid):
257 user = username_from_id(cur, uid)
258 pkgbase = pkgbase_from_id(cur, pkgbase_id)
259 to = get_ownership_recipients(cur, pkgbase_id, uid)
261 user_uri = aur_location + '/account/' + user + '/'
262 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
264 subject = 'AUR Ownership Notification for %s' % (pkgbase)
265 body = 'The package %s [1] was adopted by %s [2].' % (pkgbase, user)
266 refs = '[1] ' + pkgbase_uri + '\n'
267 refs += '[2] ' + user_uri
269 send_notification(to, subject, body, refs)
272 def disown(cur, pkgbase_id, uid):
273 user = username_from_id(cur, uid)
274 pkgbase = pkgbase_from_id(cur, pkgbase_id)
275 to = get_ownership_recipients(cur, pkgbase_id, uid)
277 user_uri = aur_location + '/account/' + user + '/'
278 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
280 subject = 'AUR Ownership Notification for %s' % (pkgbase)
281 body = 'The package %s [1] was disowned by %s [2].' % (pkgbase, user)
282 refs = '[1] ' + pkgbase_uri + '\n'
283 refs += '[2] ' + user_uri
285 send_notification(to, subject, body, refs)
288 def comaintainer_add(cur, pkgbase_id, uid):
289 pkgbase = pkgbase_from_id(cur, pkgbase_id)
290 to = [get_user_email(cur, uid)]
292 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
294 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
295 body = 'You were added to the co-maintainer list of %s [1].' % (pkgbase)
296 refs = '[1] ' + pkgbase_uri + '\n'
298 send_notification(to, subject, body, refs)
301 def comaintainer_remove(cur, pkgbase_id, uid):
302 pkgbase = pkgbase_from_id(cur, pkgbase_id)
303 to = [get_user_email(cur, uid)]
305 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
307 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
308 body = ('You were removed from the co-maintainer list of %s [1].' %
309 (pkgbase))
310 refs = '[1] ' + pkgbase_uri + '\n'
312 send_notification(to, subject, body, refs)
315 def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
316 user = username_from_id(cur, uid)
317 old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
318 if new_pkgbase_id:
319 new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
320 to = get_recipients(cur, old_pkgbase_id, uid)
322 user_uri = aur_location + '/account/' + user + '/'
323 pkgbase_uri = aur_location + '/pkgbase/' + old_pkgbase + '/'
325 subject = 'AUR Package deleted: %s' % (old_pkgbase)
326 if new_pkgbase_id:
327 new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
328 body = '%s [1] merged %s [2] into %s [3].\n\n' \
329 'If you no longer wish receive notifications about the new ' \
330 'package, please go to [3] and click "%s".' %\
331 (user, old_pkgbase, new_pkgbase, 'Disable notifications')
332 refs = '[1] ' + user_uri + '\n'
333 refs += '[2] ' + pkgbase_uri + '\n'
334 refs += '[3] ' + new_pkgbase_uri
335 else:
336 body = '%s [1] deleted %s [2].\n\n' \
337 'You will no longer receive notifications about this ' \
338 'package.' % (user, old_pkgbase)
339 refs = '[1] ' + user_uri + '\n'
340 refs += '[2] ' + pkgbase_uri
342 send_notification(to, subject, body, refs)
345 def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
346 user = username_from_id(cur, uid)
347 pkgbase = pkgbase_from_id(cur, pkgbase_id)
348 to = [aur_request_ml]
349 cc = get_request_recipients(cur, reqid)
350 text = get_request_comment(cur, reqid)
352 user_uri = aur_location + '/account/' + user + '/'
353 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
355 subject = '[PRQ#%d] %s Request for %s' % \
356 (int(reqid), reqtype.title(), pkgbase)
357 if merge_into:
358 merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
359 body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
360 (user, pkgbase, merge_into)
361 body += '\n\n' + text
362 refs = '[1] ' + user_uri + '\n'
363 refs += '[2] ' + pkgbase_uri + '\n'
364 refs += '[3] ' + merge_into_uri
365 else:
366 body = '%s [1] filed a %s request for %s [2]:' % \
367 (user, reqtype, pkgbase)
368 body += '\n\n' + text
369 refs = '[1] ' + user_uri + '\n'
370 refs += '[2] ' + pkgbase_uri + '\n'
371 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
372 # Use a deterministic Message-ID for the first email referencing a request.
373 headers = headers_msgid(thread_id)
374 headers.update(headers_cc(cc))
376 send_notification(to, subject, body, refs, headers)
379 def request_close(cur, uid, reqid, reason):
380 user = username_from_id(cur, uid)
381 to = [aur_request_ml]
382 cc = get_request_recipients(cur, reqid)
383 text = get_request_closure_comment(cur, reqid)
385 user_uri = aur_location + '/account/' + user + '/'
387 subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
388 body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
389 if text.strip() == '':
390 body += '.'
391 else:
392 body += ':\n\n' + text
393 refs = '[1] ' + user_uri
394 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
395 headers = headers_reply(thread_id)
396 headers.update(headers_cc(cc))
398 send_notification(to, subject, body, refs, headers)
401 if __name__ == '__main__':
402 action = sys.argv[1]
403 action_map = {
404 'send-resetkey': send_resetkey,
405 'welcome': welcome,
406 'comment': comment,
407 'update': update,
408 'flag': flag,
409 'adopt': adopt,
410 'disown': disown,
411 'comaintainer-add': comaintainer_add,
412 'comaintainer-remove': comaintainer_remove,
413 'delete': delete,
414 'request-open': request_open,
415 'request-close': request_close,
418 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
419 passwd=aur_db_pass, db=aur_db_name,
420 unix_socket=aur_db_socket, buffered=True)
421 cur = db.cursor()
423 action_map[action](cur, *sys.argv[2:])
425 db.commit()
426 db.close()