Update .gitignore
[aur.git] / scripts / notify.py
blobddd6e49b775f03bf0d87879222858deb0c9aa187
1 #!/usr/bin/python3
3 import email.mime.text
4 import subprocess
5 import sys
6 import textwrap
8 import aurweb.config
9 import aurweb.db
11 aur_location = aurweb.config.get('options', 'aur_location')
12 aur_request_ml = aurweb.config.get('options', 'aur_request_ml')
14 sendmail = aurweb.config.get('notifications', 'sendmail')
15 sender = aurweb.config.get('notifications', 'sender')
16 reply_to = aurweb.config.get('notifications', 'reply-to')
19 def headers_cc(cclist):
20 return {'Cc': str.join(', ', cclist)}
23 def headers_msgid(thread_id):
24 return {'Message-ID': thread_id}
27 def headers_reply(thread_id):
28 return {'In-Reply-To': thread_id, 'References': thread_id}
31 def send_notification(to, subject, body, refs, headers={}):
32 wrapped = ''
33 for line in body.splitlines():
34 wrapped += textwrap.fill(line, break_long_words=False) + '\n'
35 if refs:
36 body = wrapped + '\n' + refs
37 else:
38 body = wrapped
40 for recipient in to:
41 msg = email.mime.text.MIMEText(body, 'plain', 'utf-8')
42 msg['Subject'] = subject
43 msg['From'] = sender
44 msg['Reply-to'] = reply_to
45 msg['To'] = recipient
47 for key, value in headers.items():
48 msg[key] = value
50 p = subprocess.Popen([sendmail, '-t', '-oi'], stdin=subprocess.PIPE)
51 p.communicate(msg.as_bytes())
54 def username_from_id(conn, uid):
55 cur = conn.execute('SELECT UserName FROM Users WHERE ID = ?', [uid])
56 return cur.fetchone()[0]
59 def pkgbase_from_id(conn, pkgbase_id):
60 cur = conn.execute('SELECT Name FROM PackageBases WHERE ID = ?',
61 [pkgbase_id])
62 return cur.fetchone()[0]
65 def pkgbase_from_pkgreq(conn, reqid):
66 cur = conn.execute('SELECT PackageBaseID FROM PackageRequests ' +
67 'WHERE ID = ?', [reqid])
68 return cur.fetchone()[0]
71 def get_user_email(conn, uid):
72 cur = conn.execute('SELECT Email FROM Users WHERE ID = ?', [uid])
73 return cur.fetchone()[0]
76 def get_maintainer_email(conn, pkgbase_id):
77 cur = conn.execute('SELECT Users.Email FROM Users ' +
78 'INNER JOIN PackageBases ' +
79 'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
80 'PackageBases.ID = ?', [pkgbase_id])
81 return cur.fetchone()[0]
84 def get_recipients(conn, pkgbase_id, uid):
85 cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
86 'INNER JOIN PackageNotifications ' +
87 'ON PackageNotifications.UserID = Users.ID WHERE ' +
88 'PackageNotifications.UserID != ? AND ' +
89 'PackageNotifications.PackageBaseID = ?',
90 [uid, pkgbase_id])
91 return [row[0] for row in cur.fetchall()]
94 def get_comment_recipients(conn, pkgbase_id, uid):
95 cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
96 'INNER JOIN PackageNotifications ' +
97 'ON PackageNotifications.UserID = Users.ID WHERE ' +
98 'Users.CommentNotify = 1 AND ' +
99 'PackageNotifications.UserID != ? AND ' +
100 'PackageNotifications.PackageBaseID = ?',
101 [uid, pkgbase_id])
102 return [row[0] for row in cur.fetchall()]
105 def get_update_recipients(conn, pkgbase_id, uid):
106 cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
107 'INNER JOIN PackageNotifications ' +
108 'ON PackageNotifications.UserID = Users.ID WHERE ' +
109 'Users.UpdateNotify = 1 AND ' +
110 'PackageNotifications.UserID != ? AND ' +
111 'PackageNotifications.PackageBaseID = ?',
112 [uid, pkgbase_id])
113 return [row[0] for row in cur.fetchall()]
116 def get_ownership_recipients(conn, pkgbase_id, uid):
117 cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
118 'INNER JOIN PackageNotifications ' +
119 'ON PackageNotifications.UserID = Users.ID WHERE ' +
120 'Users.OwnershipNotify = 1 AND ' +
121 'PackageNotifications.UserID != ? AND ' +
122 'PackageNotifications.PackageBaseID = ?',
123 [uid, pkgbase_id])
124 return [row[0] for row in cur.fetchall()]
127 def get_request_recipients(conn, reqid):
128 cur = conn.execute('SELECT DISTINCT Users.Email FROM PackageRequests ' +
129 'INNER JOIN PackageBases ' +
130 'ON PackageBases.ID = PackageRequests.PackageBaseID ' +
131 'INNER JOIN Users ' +
132 'ON Users.ID = PackageRequests.UsersID ' +
133 'OR Users.ID = PackageBases.MaintainerUID ' +
134 'WHERE PackageRequests.ID = ?', [reqid])
135 return [row[0] for row in cur.fetchall()]
138 def get_tu_vote_reminder_recipients(conn, vote_id):
139 cur = conn.execute('SELECT Users.Email FROM Users ' +
140 'WHERE AccountTypeID = 2 ' +
141 'EXCEPT SELECT Users.Email FROM Users ' +
142 'INNER JOIN TU_Votes ' +
143 'ON TU_Votes.UserID = Users.ID ' +
144 'WHERE TU_Votes.VoteID = ?', [vote_id])
145 return [row[0] for row in cur.fetchall()]
148 def get_comment(conn, comment_id):
149 cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?',
150 [comment_id])
151 return cur.fetchone()[0]
154 def get_flagger_comment(conn, pkgbase_id):
155 cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ID = ?',
156 [pkgbase_id])
157 return cur.fetchone()[0]
160 def get_request_comment(conn, reqid):
161 cur = conn.execute('SELECT Comments FROM PackageRequests WHERE ID = ?',
162 [reqid])
163 return cur.fetchone()[0]
166 def get_request_closure_comment(conn, reqid):
167 cur = conn.execute('SELECT ClosureComment FROM PackageRequests ' +
168 'WHERE ID = ?', [reqid])
169 return cur.fetchone()[0]
172 def send_resetkey(conn, uid):
173 cur = conn.execute('SELECT UserName, Email, ResetKey FROM Users ' +
174 'WHERE ID = ?', [uid])
175 username, to, resetkey = cur.fetchone()
177 subject = 'AUR Password Reset'
178 body = 'A password reset request was submitted for the account %s ' \
179 'associated with your email address. If you wish to reset your ' \
180 'password follow the link [1] below, otherwise ignore this ' \
181 'message and nothing will happen.' % (username)
182 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
184 send_notification([to], subject, body, refs)
187 def welcome(conn, uid):
188 cur = conn.execute('SELECT UserName, Email, ResetKey FROM Users ' +
189 'WHERE ID = ?', [uid])
190 username, to, resetkey = cur.fetchone()
192 subject = 'Welcome to the Arch User Repository'
193 body = 'Welcome to the Arch User Repository! In order to set an initial ' \
194 'password for your new account, please click the link [1] below. ' \
195 'If the link does not work, try copying and pasting it into your ' \
196 'browser.'
197 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
199 send_notification([to], subject, body, refs)
202 def comment(conn, uid, pkgbase_id, comment_id):
203 user = username_from_id(conn, uid)
204 pkgbase = pkgbase_from_id(conn, pkgbase_id)
205 to = get_comment_recipients(conn, pkgbase_id, uid)
206 text = get_comment(conn, comment_id)
208 user_uri = aur_location + '/account/' + user + '/'
209 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
211 subject = 'AUR Comment for %s' % (pkgbase)
212 body = '%s [1] added the following comment to %s [2]:' % (user, pkgbase)
213 body += '\n\n' + text + '\n\n'
214 body += 'If you no longer wish to receive notifications about this ' \
215 'package, please go to the package page [2] and select "%s".' % \
216 ('Disable notifications')
217 refs = '[1] ' + user_uri + '\n'
218 refs += '[2] ' + pkgbase_uri
219 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
220 headers = headers_reply(thread_id)
222 send_notification(to, subject, body, refs, headers)
225 def update(conn, uid, pkgbase_id):
226 user = username_from_id(conn, uid)
227 pkgbase = pkgbase_from_id(conn, pkgbase_id)
228 to = get_update_recipients(conn, pkgbase_id, uid)
230 user_uri = aur_location + '/account/' + user + '/'
231 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
233 subject = 'AUR Package Update: %s' % (pkgbase)
234 body = '%s [1] pushed a new commit to %s [2].' % (user, pkgbase)
235 body += '\n\n'
236 body += 'If you no longer wish to receive notifications about this ' \
237 'package, please go to the package page [2] and select "%s".' % \
238 ('Disable notifications')
239 refs = '[1] ' + user_uri + '\n'
240 refs += '[2] ' + pkgbase_uri
241 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
242 headers = headers_reply(thread_id)
244 send_notification(to, subject, body, refs, headers)
247 def flag(conn, uid, pkgbase_id):
248 user = username_from_id(conn, uid)
249 pkgbase = pkgbase_from_id(conn, pkgbase_id)
250 to = [get_maintainer_email(conn, pkgbase_id)]
251 text = get_flagger_comment(conn, pkgbase_id)
253 user_uri = aur_location + '/account/' + user + '/'
254 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
256 subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
257 body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
258 (pkgbase, user)
259 body += '\n\n' + text
260 refs = '[1] ' + pkgbase_uri + '\n'
261 refs += '[2] ' + user_uri
263 send_notification(to, subject, body, refs)
266 def adopt(conn, pkgbase_id, uid):
267 user = username_from_id(conn, uid)
268 pkgbase = pkgbase_from_id(conn, pkgbase_id)
269 to = get_ownership_recipients(conn, pkgbase_id, uid)
271 user_uri = aur_location + '/account/' + user + '/'
272 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
274 subject = 'AUR Ownership Notification for %s' % (pkgbase)
275 body = 'The package %s [1] was adopted by %s [2].' % (pkgbase, user)
276 refs = '[1] ' + pkgbase_uri + '\n'
277 refs += '[2] ' + user_uri
279 send_notification(to, subject, body, refs)
282 def disown(conn, pkgbase_id, uid):
283 user = username_from_id(conn, uid)
284 pkgbase = pkgbase_from_id(conn, pkgbase_id)
285 to = get_ownership_recipients(conn, pkgbase_id, uid)
287 user_uri = aur_location + '/account/' + user + '/'
288 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
290 subject = 'AUR Ownership Notification for %s' % (pkgbase)
291 body = 'The package %s [1] was disowned by %s [2].' % (pkgbase, user)
292 refs = '[1] ' + pkgbase_uri + '\n'
293 refs += '[2] ' + user_uri
295 send_notification(to, subject, body, refs)
298 def comaintainer_add(conn, pkgbase_id, uid):
299 pkgbase = pkgbase_from_id(conn, pkgbase_id)
300 to = [get_user_email(conn, uid)]
302 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
304 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
305 body = 'You were added to the co-maintainer list of %s [1].' % (pkgbase)
306 refs = '[1] ' + pkgbase_uri + '\n'
308 send_notification(to, subject, body, refs)
311 def comaintainer_remove(conn, pkgbase_id, uid):
312 pkgbase = pkgbase_from_id(conn, pkgbase_id)
313 to = [get_user_email(conn, uid)]
315 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
317 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
318 body = ('You were removed from the co-maintainer list of %s [1].' %
319 (pkgbase))
320 refs = '[1] ' + pkgbase_uri + '\n'
322 send_notification(to, subject, body, refs)
325 def delete(conn, uid, old_pkgbase_id, new_pkgbase_id=None):
326 user = username_from_id(conn, uid)
327 old_pkgbase = pkgbase_from_id(conn, old_pkgbase_id)
328 if new_pkgbase_id:
329 new_pkgbase = pkgbase_from_id(conn, new_pkgbase_id)
330 to = get_recipients(conn, old_pkgbase_id, uid)
332 user_uri = aur_location + '/account/' + user + '/'
333 pkgbase_uri = aur_location + '/pkgbase/' + old_pkgbase + '/'
335 subject = 'AUR Package deleted: %s' % (old_pkgbase)
336 if new_pkgbase_id:
337 new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
338 body = '%s [1] merged %s [2] into %s [3].\n\n' \
339 'If you no longer wish receive notifications about the new ' \
340 'package, please go to [3] and click "%s".' %\
341 (user, old_pkgbase, new_pkgbase, 'Disable notifications')
342 refs = '[1] ' + user_uri + '\n'
343 refs += '[2] ' + pkgbase_uri + '\n'
344 refs += '[3] ' + new_pkgbase_uri
345 else:
346 body = '%s [1] deleted %s [2].\n\n' \
347 'You will no longer receive notifications about this ' \
348 'package.' % (user, old_pkgbase)
349 refs = '[1] ' + user_uri + '\n'
350 refs += '[2] ' + pkgbase_uri
352 send_notification(to, subject, body, refs)
355 def request_open(conn, uid, reqid, reqtype, pkgbase_id, merge_into=None):
356 user = username_from_id(conn, uid)
357 pkgbase = pkgbase_from_id(conn, pkgbase_id)
358 to = [aur_request_ml]
359 cc = get_request_recipients(conn, reqid)
360 text = get_request_comment(conn, reqid)
362 user_uri = aur_location + '/account/' + user + '/'
363 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
365 subject = '[PRQ#%d] %s Request for %s' % \
366 (int(reqid), reqtype.title(), pkgbase)
367 if merge_into:
368 merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
369 body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
370 (user, pkgbase, merge_into)
371 body += '\n\n' + text
372 refs = '[1] ' + user_uri + '\n'
373 refs += '[2] ' + pkgbase_uri + '\n'
374 refs += '[3] ' + merge_into_uri
375 else:
376 body = '%s [1] filed a %s request for %s [2]:' % \
377 (user, reqtype, pkgbase)
378 body += '\n\n' + text
379 refs = '[1] ' + user_uri + '\n'
380 refs += '[2] ' + pkgbase_uri + '\n'
381 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
382 # Use a deterministic Message-ID for the first email referencing a request.
383 headers = headers_msgid(thread_id)
384 headers.update(headers_cc(cc))
386 send_notification(to, subject, body, refs, headers)
389 def request_close(conn, uid, reqid, reason):
390 to = [aur_request_ml]
391 cc = get_request_recipients(conn, reqid)
392 text = get_request_closure_comment(conn, reqid)
394 subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
395 if int(uid):
396 user = username_from_id(conn, uid)
397 user_uri = aur_location + '/account/' + user + '/'
398 body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
399 refs = '[1] ' + user_uri
400 else:
401 body = 'Request #%d has been %s automatically by the Arch User ' \
402 'Repository package request system' % (int(reqid), reason)
403 refs = None
404 if text.strip() == '':
405 body += '.'
406 else:
407 body += ':\n\n' + text
408 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
409 headers = headers_reply(thread_id)
410 headers.update(headers_cc(cc))
412 send_notification(to, subject, body, refs, headers)
415 def tu_vote_reminder(conn, vote_id):
416 to = get_tu_vote_reminder_recipients(conn, vote_id)
418 vote_uri = aur_location + '/tu/?id=' + vote_id
420 subject = 'TU Vote Reminder: Proposal %d' % (int(vote_id))
421 body = 'Please remember to cast your vote on proposal %d [1]. ' \
422 'The voting period ends in less than 48 hours.' % (int(vote_id))
423 refs = '[1] ' + vote_uri
425 send_notification(to, subject, body, refs)
428 def main():
429 action = sys.argv[1]
430 action_map = {
431 'send-resetkey': send_resetkey,
432 'welcome': welcome,
433 'comment': comment,
434 'update': update,
435 'flag': flag,
436 'adopt': adopt,
437 'disown': disown,
438 'comaintainer-add': comaintainer_add,
439 'comaintainer-remove': comaintainer_remove,
440 'delete': delete,
441 'request-open': request_open,
442 'request-close': request_close,
443 'tu-vote-reminder': tu_vote_reminder,
446 conn = aurweb.db.Connection()
448 action_map[action](conn, *sys.argv[2:])
450 conn.commit()
451 conn.close()
454 if __name__ == '__main__':
455 main()