Rename the CommentNotify table to PackageNotifications
[aur.git] / scripts / notify.py
blob08cdda856e2ef42324171021fc5e8f6cff102587
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_request_recipients(cur, pkgbase_id, uid):
99 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
100 'INNER JOIN PackageBases ' +
101 'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
102 'Users.ID = %s OR PackageBases.ID = %s', [uid, pkgbase_id])
103 return [row[0] for row in cur.fetchall()]
106 def get_comment(cur, comment_id):
107 cur.execute('SELECT Comments FROM PackageComments WHERE ID = %s',
108 [comment_id])
109 return cur.fetchone()[0]
112 def get_flagger_comment(cur, pkgbase_id):
113 cur.execute('SELECT FlaggerComment FROM PackageBases WHERE ID = %s',
114 [pkgbase_id])
115 return cur.fetchone()[0]
118 def get_request_comment(cur, reqid):
119 cur.execute('SELECT Comments FROM PackageRequests WHERE ID = %s', [reqid])
120 return cur.fetchone()[0]
123 def get_request_closure_comment(cur, reqid):
124 cur.execute('SELECT ClosureComment FROM PackageRequests WHERE ID = %s',
125 [reqid])
126 return cur.fetchone()[0]
129 def send_resetkey(cur, uid):
130 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
131 [uid])
132 username, to, resetkey = cur.fetchone()
134 subject = 'AUR Password Reset'
135 body = 'A password reset request was submitted for the account %s ' \
136 'associated with your email address. If you wish to reset your ' \
137 'password follow the link [1] below, otherwise ignore this ' \
138 'message and nothing will happen.' % (username)
139 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
141 send_notification([to], subject, body, refs)
144 def welcome(cur, uid):
145 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
146 [uid])
147 username, to, resetkey = cur.fetchone()
149 subject = 'Welcome to the Arch User Repository'
150 body = 'Welcome to the Arch User Repository! In order to set an initial ' \
151 'password for your new account, please click the link [1] below. ' \
152 'If the link does not work, try copying and pasting it into your ' \
153 'browser.'
154 refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
156 send_notification([to], subject, body, refs)
159 def comment(cur, uid, pkgbase_id, comment_id):
160 user = username_from_id(cur, uid)
161 pkgbase = pkgbase_from_id(cur, pkgbase_id)
162 to = get_recipients(cur, pkgbase_id, uid)
163 text = get_comment(cur, comment_id)
165 user_uri = aur_location + '/account/' + user + '/'
166 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
168 subject = 'AUR Comment for %s' % (pkgbase)
169 body = '%s [1] added the following comment to %s [2]:' % (user, pkgbase)
170 body += '\n\n' + text + '\n\n'
171 body += 'If you no longer wish to receive notifications about this ' \
172 'package, please go to the package page [2] and select "%s".' % \
173 ('Disable notifications')
174 refs = '[1] ' + user_uri + '\n'
175 refs += '[2] ' + pkgbase_uri
176 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
177 headers = headers_reply(thread_id)
179 send_notification(to, subject, body, refs, headers)
182 def flag(cur, uid, pkgbase_id):
183 user = username_from_id(cur, uid)
184 pkgbase = pkgbase_from_id(cur, pkgbase_id)
185 to = [get_maintainer_email(cur, pkgbase_id)]
186 text = get_flagger_comment(cur, pkgbase_id)
188 user_uri = aur_location + '/account/' + user + '/'
189 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
191 subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
192 body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
193 (pkgbase, user)
194 body += '\n\n' + text
195 refs = '[1] ' + pkgbase_uri + '\n'
196 refs += '[2] ' + user_uri
198 send_notification(to, subject, body, refs)
201 def comaintainer_add(cur, pkgbase_id, uid):
202 pkgbase = pkgbase_from_id(cur, pkgbase_id)
203 to = [get_user_email(cur, uid)]
205 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
207 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
208 body = 'You were added to the co-maintainer list of %s [1].' % (pkgbase)
209 refs = '[1] ' + pkgbase_uri + '\n'
211 send_notification(to, subject, body, refs)
214 def comaintainer_remove(cur, pkgbase_id, uid):
215 pkgbase = pkgbase_from_id(cur, pkgbase_id)
216 to = [get_user_email(cur, uid)]
218 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
220 subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
221 body = ('You were removed from the co-maintainer list of %s [1].' %
222 (pkgbase))
223 refs = '[1] ' + pkgbase_uri + '\n'
225 send_notification(to, subject, body, refs)
228 def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
229 user = username_from_id(cur, uid)
230 old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
231 if new_pkgbase_id:
232 new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
233 to = get_recipients(cur, old_pkgbase_id, uid)
235 user_uri = aur_location + '/account/' + user + '/'
236 pkgbase_uri = aur_location + '/pkgbase/' + old_pkgbase + '/'
238 subject = 'AUR Package deleted: %s' % (old_pkgbase)
239 if new_pkgbase_id:
240 new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
241 body = '%s [1] merged %s [2] into %s [3].\n\n' \
242 'If you no longer wish receive notifications about the new ' \
243 'package, please go to [3] and click "%s".' %\
244 (user, old_pkgbase, new_pkgbase, 'Disable notifications')
245 refs = '[1] ' + user_uri + '\n'
246 refs += '[2] ' + pkgbase_uri + '\n'
247 refs += '[3] ' + new_pkgbase_uri
248 else:
249 body = '%s [1] deleted %s [2].\n\n' \
250 'You will no longer receive notifications about this ' \
251 'package.' % (user, old_pkgbase)
252 refs = '[1] ' + user_uri + '\n'
253 refs += '[2] ' + pkgbase_uri
255 send_notification(to, subject, body, refs)
258 def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
259 user = username_from_id(cur, uid)
260 pkgbase = pkgbase_from_id(cur, pkgbase_id)
261 to = [aur_request_ml]
262 cc = get_request_recipients(cur, pkgbase_id, uid)
263 text = get_request_comment(cur, reqid)
265 user_uri = aur_location + '/account/' + user + '/'
266 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
268 subject = '[PRQ#%d] %s Request for %s' % \
269 (int(reqid), reqtype.title(), pkgbase)
270 if merge_into:
271 merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
272 body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
273 (user, pkgbase, merge_into)
274 body += '\n\n' + text
275 refs = '[1] ' + user_uri + '\n'
276 refs += '[2] ' + pkgbase_uri + '\n'
277 refs += '[3] ' + merge_into_uri
278 else:
279 body = '%s [1] filed a %s request for %s [2]:' % \
280 (user, reqtype, pkgbase)
281 body += '\n\n' + text
282 refs = '[1] ' + user_uri + '\n'
283 refs += '[2] ' + pkgbase_uri + '\n'
284 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
285 headers = headers_reply(thread_id)
286 headers.update(headers_cc(cc))
288 send_notification(to, subject, body, refs, headers)
291 def request_close(cur, uid, reqid, reason):
292 user = username_from_id(cur, uid)
293 pkgbase_id = pkgbase_from_pkgreq(cur, reqid)
294 to = [aur_request_ml]
295 cc = get_request_recipients(cur, pkgbase_id, uid)
296 text = get_request_closure_comment(cur, reqid)
298 user_uri = aur_location + '/account/' + user + '/'
300 subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
301 body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
302 if text.strip() == '':
303 body += '.'
304 else:
305 body += ':\n\n' + text
306 refs = '[1] ' + user_uri
307 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
308 headers = headers_reply(thread_id)
309 headers.update(headers_cc(cc))
311 send_notification(to, subject, body, refs, headers)
314 if __name__ == '__main__':
315 action = sys.argv[1]
316 action_map = {
317 'send-resetkey': send_resetkey,
318 'welcome': welcome,
319 'comment': comment,
320 'flag': flag,
321 'comaintainer-add': comaintainer_add,
322 'comaintainer-remove': comaintainer_remove,
323 'delete': delete,
324 'request-open': request_open,
325 'request-close': request_close,
328 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
329 passwd=aur_db_pass, db=aur_db_name,
330 unix_socket=aur_db_socket, buffered=True)
331 cur = db.cursor()
333 action_map[action](cur, *sys.argv[2:])
335 db.commit()
336 db.close()