Add global comment notification setting
[aur.git] / scripts / notify.py
blob6b10600937fd41c6fad833601021401860b4a122
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_request_recipients(cur, pkgbase_id, uid):
109 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
110 'INNER JOIN PackageBases ' +
111 'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
112 'Users.ID = %s OR PackageBases.ID = %s', [uid, pkgbase_id])
113 return [row[0] for row in cur.fetchall()]
116 def get_comment(cur, comment_id):
117 cur.execute('SELECT Comments FROM PackageComments WHERE ID = %s',
118 [comment_id])
119 return cur.fetchone()[0]
122 def get_flagger_comment(cur, pkgbase_id):
123 cur.execute('SELECT FlaggerComment FROM PackageBases WHERE ID = %s',
124 [pkgbase_id])
125 return cur.fetchone()[0]
128 def get_request_comment(cur, reqid):
129 cur.execute('SELECT Comments FROM PackageRequests WHERE ID = %s', [reqid])
130 return cur.fetchone()[0]
133 def get_request_closure_comment(cur, reqid):
134 cur.execute('SELECT ClosureComment FROM PackageRequests WHERE ID = %s',
135 [reqid])
136 return cur.fetchone()[0]
139 def send_resetkey(cur, uid):
140 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
141 [uid])
142 username, to, resetkey = cur.fetchone()
144 subject = 'AUR Password Reset'
145 body = 'A password reset request was submitted for the account %s ' \
146 'associated with your email address. If you wish to reset your ' \
147 'password follow the link [1] below, otherwise ignore this ' \
148 'message and nothing will happen.' % (username)
149 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
151 send_notification([to], subject, body, refs)
154 def welcome(cur, uid):
155 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
156 [uid])
157 username, to, resetkey = cur.fetchone()
159 subject = 'Welcome to the Arch User Repository'
160 body = 'Welcome to the Arch User Repository! In order to set an initial ' \
161 'password for your new account, please click the link [1] below. ' \
162 'If the link does not work, try copying and pasting it into your ' \
163 'browser.'
164 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
166 send_notification([to], subject, body, refs)
169 def comment(cur, uid, pkgbase_id, comment_id):
170 user = username_from_id(cur, uid)
171 pkgbase = pkgbase_from_id(cur, pkgbase_id)
172 to = get_comment_recipients(cur, pkgbase_id, uid)
173 text = get_comment(cur, comment_id)
175 user_uri = aur_location + '/account/' + user + '/'
176 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
178 subject = 'AUR Comment for %s' % (pkgbase)
179 body = '%s [1] added the following comment to %s [2]:' % (user, pkgbase)
180 body += '\n\n' + text + '\n\n'
181 body += 'If you no longer wish to receive notifications about this ' \
182 'package, please go to the package page [2] and select "%s".' % \
183 ('Disable notifications')
184 refs = '[1] ' + user_uri + '\n'
185 refs += '[2] ' + pkgbase_uri
186 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
187 headers = headers_reply(thread_id)
189 send_notification(to, subject, body, refs, headers)
192 def flag(cur, uid, pkgbase_id):
193 user = username_from_id(cur, uid)
194 pkgbase = pkgbase_from_id(cur, pkgbase_id)
195 to = [get_maintainer_email(cur, pkgbase_id)]
196 text = get_flagger_comment(cur, pkgbase_id)
198 user_uri = aur_location + '/account/' + user + '/'
199 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
201 subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
202 body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
203 (pkgbase, user)
204 body += '\n\n' + text
205 refs = '[1] ' + pkgbase_uri + '\n'
206 refs += '[2] ' + user_uri
208 send_notification(to, subject, body, refs)
211 def comaintainer_add(cur, pkgbase_id, uid):
212 pkgbase = pkgbase_from_id(cur, pkgbase_id)
213 to = [get_user_email(cur, uid)]
215 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
217 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
218 body = 'You were added to the co-maintainer list of %s [1].' % (pkgbase)
219 refs = '[1] ' + pkgbase_uri + '\n'
221 send_notification(to, subject, body, refs)
224 def comaintainer_remove(cur, pkgbase_id, uid):
225 pkgbase = pkgbase_from_id(cur, pkgbase_id)
226 to = [get_user_email(cur, uid)]
228 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
230 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
231 body = ('You were removed from the co-maintainer list of %s [1].' %
232 (pkgbase))
233 refs = '[1] ' + pkgbase_uri + '\n'
235 send_notification(to, subject, body, refs)
238 def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
239 user = username_from_id(cur, uid)
240 old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
241 if new_pkgbase_id:
242 new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
243 to = get_recipients(cur, old_pkgbase_id, uid)
245 user_uri = aur_location + '/account/' + user + '/'
246 pkgbase_uri = aur_location + '/pkgbase/' + old_pkgbase + '/'
248 subject = 'AUR Package deleted: %s' % (old_pkgbase)
249 if new_pkgbase_id:
250 new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
251 body = '%s [1] merged %s [2] into %s [3].\n\n' \
252 'If you no longer wish receive notifications about the new ' \
253 'package, please go to [3] and click "%s".' %\
254 (user, old_pkgbase, new_pkgbase, 'Disable notifications')
255 refs = '[1] ' + user_uri + '\n'
256 refs += '[2] ' + pkgbase_uri + '\n'
257 refs += '[3] ' + new_pkgbase_uri
258 else:
259 body = '%s [1] deleted %s [2].\n\n' \
260 'You will no longer receive notifications about this ' \
261 'package.' % (user, old_pkgbase)
262 refs = '[1] ' + user_uri + '\n'
263 refs += '[2] ' + pkgbase_uri
265 send_notification(to, subject, body, refs)
268 def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
269 user = username_from_id(cur, uid)
270 pkgbase = pkgbase_from_id(cur, pkgbase_id)
271 to = [aur_request_ml]
272 cc = get_request_recipients(cur, pkgbase_id, uid)
273 text = get_request_comment(cur, reqid)
275 user_uri = aur_location + '/account/' + user + '/'
276 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
278 subject = '[PRQ#%d] %s Request for %s' % \
279 (int(reqid), reqtype.title(), pkgbase)
280 if merge_into:
281 merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
282 body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
283 (user, pkgbase, merge_into)
284 body += '\n\n' + text
285 refs = '[1] ' + user_uri + '\n'
286 refs += '[2] ' + pkgbase_uri + '\n'
287 refs += '[3] ' + merge_into_uri
288 else:
289 body = '%s [1] filed a %s request for %s [2]:' % \
290 (user, reqtype, pkgbase)
291 body += '\n\n' + text
292 refs = '[1] ' + user_uri + '\n'
293 refs += '[2] ' + pkgbase_uri + '\n'
294 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
295 headers = headers_reply(thread_id)
296 headers.update(headers_cc(cc))
298 send_notification(to, subject, body, refs, headers)
301 def request_close(cur, uid, reqid, reason):
302 user = username_from_id(cur, uid)
303 pkgbase_id = pkgbase_from_pkgreq(cur, reqid)
304 to = [aur_request_ml]
305 cc = get_request_recipients(cur, pkgbase_id, uid)
306 text = get_request_closure_comment(cur, reqid)
308 user_uri = aur_location + '/account/' + user + '/'
310 subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
311 body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
312 if text.strip() == '':
313 body += '.'
314 else:
315 body += ':\n\n' + text
316 refs = '[1] ' + user_uri
317 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
318 headers = headers_reply(thread_id)
319 headers.update(headers_cc(cc))
321 send_notification(to, subject, body, refs, headers)
324 if __name__ == '__main__':
325 action = sys.argv[1]
326 action_map = {
327 'send-resetkey': send_resetkey,
328 'welcome': welcome,
329 'comment': comment,
330 'flag': flag,
331 'comaintainer-add': comaintainer_add,
332 'comaintainer-remove': comaintainer_remove,
333 'delete': delete,
334 'request-open': request_open,
335 'request-close': request_close,
338 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
339 passwd=aur_db_pass, db=aur_db_name,
340 unix_socket=aur_db_socket, buffered=True)
341 cur = db.cursor()
343 action_map[action](cur, *sys.argv[2:])
345 db.commit()
346 db.close()