git-update: Make maximum blob size configurable
[aur.git] / scripts / notify.py
blob6ea25d120c30415453ad14093f74849def8af9c4
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 if refs:
45 body = wrapped + '\n' + refs
46 else:
47 body = wrapped
49 for recipient in to:
50 msg = email.mime.text.MIMEText(body, 'plain', 'utf-8')
51 msg['Subject'] = subject
52 msg['From'] = sender
53 msg['Reply-to'] = reply_to
54 msg['To'] = recipient
56 for key, value in headers.items():
57 msg[key] = value
59 p = subprocess.Popen([sendmail, '-t', '-oi'], stdin=subprocess.PIPE)
60 p.communicate(msg.as_bytes())
63 def username_from_id(cur, uid):
64 cur.execute('SELECT UserName FROM Users WHERE ID = %s', [uid])
65 return cur.fetchone()[0]
68 def pkgbase_from_id(cur, pkgbase_id):
69 cur.execute('SELECT Name FROM PackageBases WHERE ID = %s', [pkgbase_id])
70 return cur.fetchone()[0]
73 def pkgbase_from_pkgreq(cur, reqid):
74 cur.execute('SELECT PackageBaseID FROM PackageRequests WHERE ID = %s',
75 [reqid])
76 return cur.fetchone()[0]
79 def get_user_email(cur, uid):
80 cur.execute('SELECT Email FROM Users WHERE ID = %s', [uid])
81 return cur.fetchone()[0]
84 def get_maintainer_email(cur, pkgbase_id):
85 cur.execute('SELECT Users.Email FROM Users ' +
86 'INNER JOIN PackageBases ' +
87 'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
88 'PackageBases.ID = %s', [pkgbase_id])
89 return cur.fetchone()[0]
92 def get_recipients(cur, pkgbase_id, uid):
93 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
94 'INNER JOIN PackageNotifications ' +
95 'ON PackageNotifications.UserID = Users.ID WHERE ' +
96 'PackageNotifications.UserID != %s AND ' +
97 'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
98 return [row[0] for row in cur.fetchall()]
101 def get_comment_recipients(cur, pkgbase_id, uid):
102 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
103 'INNER JOIN PackageNotifications ' +
104 'ON PackageNotifications.UserID = Users.ID WHERE ' +
105 'Users.CommentNotify = 1 AND ' +
106 'PackageNotifications.UserID != %s AND ' +
107 'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
108 return [row[0] for row in cur.fetchall()]
111 def get_update_recipients(cur, pkgbase_id, uid):
112 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
113 'INNER JOIN PackageNotifications ' +
114 'ON PackageNotifications.UserID = Users.ID WHERE ' +
115 'Users.UpdateNotify = 1 AND ' +
116 'PackageNotifications.UserID != %s AND ' +
117 'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
118 return [row[0] for row in cur.fetchall()]
121 def get_ownership_recipients(cur, pkgbase_id, uid):
122 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
123 'INNER JOIN PackageNotifications ' +
124 'ON PackageNotifications.UserID = Users.ID WHERE ' +
125 'Users.OwnershipNotify = 1 AND ' +
126 'PackageNotifications.UserID != %s AND ' +
127 'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
128 return [row[0] for row in cur.fetchall()]
131 def get_request_recipients(cur, reqid):
132 cur.execute('SELECT DISTINCT Users.Email FROM PackageRequests ' +
133 'INNER JOIN PackageBases ' +
134 'ON PackageBases.ID = PackageRequests.PackageBaseID ' +
135 'INNER JOIN Users ' +
136 'ON Users.ID = PackageRequests.UsersID ' +
137 'OR Users.ID = PackageBases.MaintainerUID ' +
138 'WHERE PackageRequests.ID = %s', [reqid])
139 return [row[0] for row in cur.fetchall()]
142 def get_comment(cur, comment_id):
143 cur.execute('SELECT Comments FROM PackageComments WHERE ID = %s',
144 [comment_id])
145 return cur.fetchone()[0]
148 def get_flagger_comment(cur, pkgbase_id):
149 cur.execute('SELECT FlaggerComment FROM PackageBases WHERE ID = %s',
150 [pkgbase_id])
151 return cur.fetchone()[0]
154 def get_request_comment(cur, reqid):
155 cur.execute('SELECT Comments FROM PackageRequests WHERE ID = %s', [reqid])
156 return cur.fetchone()[0]
159 def get_request_closure_comment(cur, reqid):
160 cur.execute('SELECT ClosureComment FROM PackageRequests WHERE ID = %s',
161 [reqid])
162 return cur.fetchone()[0]
165 def send_resetkey(cur, uid):
166 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
167 [uid])
168 username, to, resetkey = cur.fetchone()
170 subject = 'AUR Password Reset'
171 body = 'A password reset request was submitted for the account %s ' \
172 'associated with your email address. If you wish to reset your ' \
173 'password follow the link [1] below, otherwise ignore this ' \
174 'message and nothing will happen.' % (username)
175 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
177 send_notification([to], subject, body, refs)
180 def welcome(cur, uid):
181 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
182 [uid])
183 username, to, resetkey = cur.fetchone()
185 subject = 'Welcome to the Arch User Repository'
186 body = 'Welcome to the Arch User Repository! In order to set an initial ' \
187 'password for your new account, please click the link [1] below. ' \
188 'If the link does not work, try copying and pasting it into your ' \
189 'browser.'
190 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
192 send_notification([to], subject, body, refs)
195 def comment(cur, uid, pkgbase_id, comment_id):
196 user = username_from_id(cur, uid)
197 pkgbase = pkgbase_from_id(cur, pkgbase_id)
198 to = get_comment_recipients(cur, pkgbase_id, uid)
199 text = get_comment(cur, comment_id)
201 user_uri = aur_location + '/account/' + user + '/'
202 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
204 subject = 'AUR Comment for %s' % (pkgbase)
205 body = '%s [1] added the following comment to %s [2]:' % (user, pkgbase)
206 body += '\n\n' + text + '\n\n'
207 body += 'If you no longer wish to receive notifications about this ' \
208 'package, please go to the package page [2] and select "%s".' % \
209 ('Disable notifications')
210 refs = '[1] ' + user_uri + '\n'
211 refs += '[2] ' + pkgbase_uri
212 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
213 headers = headers_reply(thread_id)
215 send_notification(to, subject, body, refs, headers)
218 def update(cur, uid, pkgbase_id):
219 user = username_from_id(cur, uid)
220 pkgbase = pkgbase_from_id(cur, pkgbase_id)
221 to = get_update_recipients(cur, pkgbase_id, uid)
223 user_uri = aur_location + '/account/' + user + '/'
224 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
226 subject = 'AUR Package Update: %s' % (pkgbase)
227 body = '%s [1] pushed a new commit to %s [2].' % (user, pkgbase)
228 body += '\n\n'
229 body += 'If you no longer wish to receive notifications about this ' \
230 'package, please go to the package page [2] and select "%s".' % \
231 ('Disable notifications')
232 refs = '[1] ' + user_uri + '\n'
233 refs += '[2] ' + pkgbase_uri
234 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
235 headers = headers_reply(thread_id)
237 send_notification(to, subject, body, refs, headers)
240 def flag(cur, uid, pkgbase_id):
241 user = username_from_id(cur, uid)
242 pkgbase = pkgbase_from_id(cur, pkgbase_id)
243 to = [get_maintainer_email(cur, pkgbase_id)]
244 text = get_flagger_comment(cur, pkgbase_id)
246 user_uri = aur_location + '/account/' + user + '/'
247 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
249 subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
250 body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
251 (pkgbase, user)
252 body += '\n\n' + text
253 refs = '[1] ' + pkgbase_uri + '\n'
254 refs += '[2] ' + user_uri
256 send_notification(to, subject, body, refs)
259 def adopt(cur, pkgbase_id, uid):
260 user = username_from_id(cur, uid)
261 pkgbase = pkgbase_from_id(cur, pkgbase_id)
262 to = get_ownership_recipients(cur, pkgbase_id, uid)
264 user_uri = aur_location + '/account/' + user + '/'
265 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
267 subject = 'AUR Ownership Notification for %s' % (pkgbase)
268 body = 'The package %s [1] was adopted by %s [2].' % (pkgbase, user)
269 refs = '[1] ' + pkgbase_uri + '\n'
270 refs += '[2] ' + user_uri
272 send_notification(to, subject, body, refs)
275 def disown(cur, pkgbase_id, uid):
276 user = username_from_id(cur, uid)
277 pkgbase = pkgbase_from_id(cur, pkgbase_id)
278 to = get_ownership_recipients(cur, pkgbase_id, uid)
280 user_uri = aur_location + '/account/' + user + '/'
281 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
283 subject = 'AUR Ownership Notification for %s' % (pkgbase)
284 body = 'The package %s [1] was disowned by %s [2].' % (pkgbase, user)
285 refs = '[1] ' + pkgbase_uri + '\n'
286 refs += '[2] ' + user_uri
288 send_notification(to, subject, body, refs)
291 def comaintainer_add(cur, pkgbase_id, uid):
292 pkgbase = pkgbase_from_id(cur, pkgbase_id)
293 to = [get_user_email(cur, uid)]
295 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
297 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
298 body = 'You were added to the co-maintainer list of %s [1].' % (pkgbase)
299 refs = '[1] ' + pkgbase_uri + '\n'
301 send_notification(to, subject, body, refs)
304 def comaintainer_remove(cur, pkgbase_id, uid):
305 pkgbase = pkgbase_from_id(cur, pkgbase_id)
306 to = [get_user_email(cur, uid)]
308 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
310 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
311 body = ('You were removed from the co-maintainer list of %s [1].' %
312 (pkgbase))
313 refs = '[1] ' + pkgbase_uri + '\n'
315 send_notification(to, subject, body, refs)
318 def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
319 user = username_from_id(cur, uid)
320 old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
321 if new_pkgbase_id:
322 new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
323 to = get_recipients(cur, old_pkgbase_id, uid)
325 user_uri = aur_location + '/account/' + user + '/'
326 pkgbase_uri = aur_location + '/pkgbase/' + old_pkgbase + '/'
328 subject = 'AUR Package deleted: %s' % (old_pkgbase)
329 if new_pkgbase_id:
330 new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
331 body = '%s [1] merged %s [2] into %s [3].\n\n' \
332 'If you no longer wish receive notifications about the new ' \
333 'package, please go to [3] and click "%s".' %\
334 (user, old_pkgbase, new_pkgbase, 'Disable notifications')
335 refs = '[1] ' + user_uri + '\n'
336 refs += '[2] ' + pkgbase_uri + '\n'
337 refs += '[3] ' + new_pkgbase_uri
338 else:
339 body = '%s [1] deleted %s [2].\n\n' \
340 'You will no longer receive notifications about this ' \
341 'package.' % (user, old_pkgbase)
342 refs = '[1] ' + user_uri + '\n'
343 refs += '[2] ' + pkgbase_uri
345 send_notification(to, subject, body, refs)
348 def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
349 user = username_from_id(cur, uid)
350 pkgbase = pkgbase_from_id(cur, pkgbase_id)
351 to = [aur_request_ml]
352 cc = get_request_recipients(cur, reqid)
353 text = get_request_comment(cur, reqid)
355 user_uri = aur_location + '/account/' + user + '/'
356 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
358 subject = '[PRQ#%d] %s Request for %s' % \
359 (int(reqid), reqtype.title(), pkgbase)
360 if merge_into:
361 merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
362 body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
363 (user, pkgbase, merge_into)
364 body += '\n\n' + text
365 refs = '[1] ' + user_uri + '\n'
366 refs += '[2] ' + pkgbase_uri + '\n'
367 refs += '[3] ' + merge_into_uri
368 else:
369 body = '%s [1] filed a %s request for %s [2]:' % \
370 (user, reqtype, pkgbase)
371 body += '\n\n' + text
372 refs = '[1] ' + user_uri + '\n'
373 refs += '[2] ' + pkgbase_uri + '\n'
374 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
375 # Use a deterministic Message-ID for the first email referencing a request.
376 headers = headers_msgid(thread_id)
377 headers.update(headers_cc(cc))
379 send_notification(to, subject, body, refs, headers)
382 def request_close(cur, uid, reqid, reason):
383 to = [aur_request_ml]
384 cc = get_request_recipients(cur, reqid)
385 text = get_request_closure_comment(cur, reqid)
387 subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
388 if int(uid):
389 user = username_from_id(cur, uid)
390 user_uri = aur_location + '/account/' + user + '/'
391 body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
392 refs = '[1] ' + user_uri
393 else:
394 body = 'Request #%d has been %s automatically by the Arch User ' \
395 'Repository package request system' % (int(reqid), reason)
396 refs = None
397 if text.strip() == '':
398 body += '.'
399 else:
400 body += ':\n\n' + text
401 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
402 headers = headers_reply(thread_id)
403 headers.update(headers_cc(cc))
405 send_notification(to, subject, body, refs, headers)
408 if __name__ == '__main__':
409 action = sys.argv[1]
410 action_map = {
411 'send-resetkey': send_resetkey,
412 'welcome': welcome,
413 'comment': comment,
414 'update': update,
415 'flag': flag,
416 'adopt': adopt,
417 'disown': disown,
418 'comaintainer-add': comaintainer_add,
419 'comaintainer-remove': comaintainer_remove,
420 'delete': delete,
421 'request-open': request_open,
422 'request-close': request_close,
425 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
426 passwd=aur_db_pass, db=aur_db_name,
427 unix_socket=aur_db_socket, buffered=True)
428 cur = db.cursor()
430 action_map[action](cur, *sys.argv[2:])
432 db.commit()
433 db.close()