AUTHORS: Add date ranges to current maintainers
[aur.git] / scripts / notify.py
blobf6bf6ffb030087acb67b1060e2aa72184f1d7e33
1 #!/usr/bin/python3
3 import configparser
4 import email.mime.text
5 import mysql.connector
6 import os
7 import smtplib
8 import subprocess
9 import sys
10 import textwrap
12 config = configparser.RawConfigParser()
13 config.read(os.path.dirname(os.path.realpath(__file__)) + '/../conf/config')
15 aur_db_host = config.get('database', 'host')
16 aur_db_name = config.get('database', 'name')
17 aur_db_user = config.get('database', 'user')
18 aur_db_pass = config.get('database', 'password')
19 aur_db_socket = config.get('database', 'socket')
21 aur_location = config.get('options', 'aur_location')
22 aur_request_ml = config.get('options', 'aur_request_ml')
24 sendmail = config.get('notifications', 'sendmail')
25 sender = config.get('notifications', 'sender')
26 reply_to = config.get('notifications', 'reply-to')
29 def send_notification(to, subject, body, cc=None, reference=None):
30 body = str.join('\n', [textwrap.fill(line) for line in body.splitlines()])
32 for recipient in to:
33 msg = email.mime.text.MIMEText(body, 'plain', 'utf-8')
34 msg['Subject'] = subject
35 msg['From'] = sender
36 msg['Reply-to'] = reply_to
37 msg['To'] = recipient
39 if cc:
40 msg['Cc'] = str.join(', ', cc)
42 if reference:
43 msg['In-Reply-To'] = reference
44 msg['References'] = reference
46 p = subprocess.Popen([sendmail, '-t', '-oi'], stdin=subprocess.PIPE)
47 p.communicate(msg.as_bytes())
49 def username_from_id(cur, uid):
50 cur.execute('SELECT UserName FROM Users WHERE ID = %s', [uid])
51 return cur.fetchone()[0]
53 def pkgbase_from_id(cur, pkgbase_id):
54 cur.execute('SELECT Name FROM PackageBases WHERE ID = %s', [pkgbase_id])
55 return cur.fetchone()[0]
57 def pkgbase_from_pkgreq(cur, reqid):
58 cur.execute('SELECT PackageBaseID FROM PackageRequests WHERE ID = %s',
59 [reqid])
60 return cur.fetchone()[0]
62 def get_maintainer_email(cur, pkgbase_id):
63 cur.execute('SELECT Users.Email FROM Users ' +
64 'INNER JOIN PackageBases ' +
65 'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
66 'PackageBases.ID = %s', [pkgbase_id])
67 return cur.fetchone()[0]
69 def get_recipients(cur, pkgbase_id, uid):
70 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
71 'INNER JOIN CommentNotify ' +
72 'ON CommentNotify.UserID = Users.ID WHERE ' +
73 'CommentNotify.UserID != %s AND ' +
74 'CommentNotify.PackageBaseID = %s', [uid, pkgbase_id])
75 return [row[0] for row in cur.fetchall()]
77 def get_request_recipients(cur, pkgbase_id, uid):
78 cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
79 'INNER JOIN PackageBases ' +
80 'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
81 'Users.ID = %s OR PackageBases.ID = %s', [uid, pkgbase_id])
82 return [row[0] for row in cur.fetchall()]
84 def send_resetkey(cur, uid):
85 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
86 [uid])
87 username, to, resetkey = cur.fetchone()
89 subject = 'AUR Password Reset'
90 body = 'A password reset request was submitted for the account %s ' \
91 'associated with your email address. If you wish to reset your ' \
92 'password follow the link [1] below, otherwise ignore this ' \
93 'message and nothing will happen.' % (username)
94 body += '\n\n'
95 body += '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
97 send_notification([to], subject, body)
99 def welcome(cur, uid):
100 cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
101 [uid])
102 username, to, resetkey = cur.fetchone()
104 subject = 'Welcome to the Arch User Repository'
105 body = 'Welcome to the Arch User Repository! In order to set an initial ' \
106 'password for your new account, please click the link [1] below. ' \
107 'If the link does not work, try copying and pasting it into your ' \
108 'browser.'
109 body += '\n\n'
110 body += '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
112 send_notification([to], subject, body)
114 def comment(cur, uid, pkgbase_id):
115 user = username_from_id(cur, uid)
116 pkgbase = pkgbase_from_id(cur, pkgbase_id)
117 to = get_recipients(cur, pkgbase_id, uid)
118 text = sys.stdin.read()
120 uri = aur_location + '/pkgbase/' + pkgbase + '/'
122 user_uri = aur_location + '/account/' + user + '/'
123 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
125 subject = 'AUR Comment for %s' % (pkgbase)
126 body = '%s [1] added the following comment to %s [2]:' % (user, pkgbase)
127 body += '\n\n' + text + '\n\n'
128 body += 'If you no longer wish to receive notifications about this ' \
129 'package, please go to the package page [2] and select "%s".' % \
130 ('Disable notifications')
131 body += '\n\n'
132 body += '[1] ' + user_uri + '\n'
133 body += '[2] ' + pkgbase_uri
134 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
136 send_notification(to, subject, body, reference=thread_id)
138 def flag(cur, uid, pkgbase_id):
139 user = username_from_id(cur, uid)
140 pkgbase = pkgbase_from_id(cur, pkgbase_id)
141 to = [get_maintainer_email(cur, pkgbase_id)]
142 text = sys.stdin.read()
144 user_uri = aur_location + '/account/' + user + '/'
145 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
147 subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
148 body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
149 (pkgbase, user)
150 body += '\n\n' + text + '\n\n'
151 body += '[1] ' + pkgbase_uri + '\n'
152 body += '[2] ' + user_uri
154 send_notification(to, subject, body)
156 def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
157 user = username_from_id(cur, uid)
158 old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
159 if new_pkgbase_id:
160 new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
161 to = get_recipients(cur, old_pkgbase_id, uid)
163 user_uri = aur_location + '/account/' + user + '/'
164 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
166 subject = 'AUR Package deleted: %s' % (old_pkgbase)
167 if new_pkgbase_id:
168 new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
169 body = '%s [1] merged %s [2] into %s [3].\n\n' \
170 'If you no longer wish receive notifications about the new ' \
171 'package, please go to [3] and click "%s".' %\
172 (user, old_pkgbase, new_pkgbase, 'Disable notifications')
173 body += '\n\n'
174 body += '[1] ' + user_uri + '\n'
175 body += '[2] ' + pkgbase_uri + '\n'
176 body += '[3] ' + new_pkgbase_uri
177 else:
178 body = '%s [1] deleted %s [2].\n\n' \
179 'You will no longer receive notifications about this ' \
180 'package.' % (user, old_pkgbase)
181 body += '\n\n'
182 body += '[1] ' + user_uri + '\n'
183 body += '[2] ' + pkgbase_uri
185 send_notification(to, subject, body)
187 def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
188 user = username_from_id(cur, uid)
189 pkgbase = pkgbase_from_id(cur, pkgbase_id)
190 to = [aur_request_ml]
191 cc = get_request_recipients(cur, pkgbase_id, uid)
192 text = sys.stdin.read()
194 user_uri = aur_location + '/account/' + user + '/'
195 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
197 subject = '[PRQ#%d] %s Request for %s' % \
198 (int(reqid), reqtype.title(), pkgbase)
199 if merge_into:
200 merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
201 body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
202 (user, pkgbase, merge_into)
203 body += '\n\n' + text + '\n\n'
204 body += '[1] ' + user_uri + '\n'
205 body += '[2] ' + pkgbase_uri + '\n'
206 body += '[3] ' + merge_into_uri
207 else:
208 body = '%s [1] filed a %s request for %s [2]:' % \
209 (user, reqtype, pkgbase)
210 body += '\n\n' + text + '\n\n'
211 body += '[1] ' + user_uri + '\n'
212 body += '[2] ' + pkgbase_uri + '\n'
213 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
215 send_notification(to, subject, body, cc, thread_id)
217 def request_close(cur, uid, reqid, reason):
218 user = username_from_id(cur, uid)
219 pkgbase_id = pkgbase_from_pkgreq(cur, reqid)
220 to = [aur_request_ml]
221 cc = get_request_recipients(cur, pkgbase_id, uid)
222 text = sys.stdin.read()
224 user_uri = aur_location + '/account/' + user + '/'
226 subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
227 body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
228 if text.strip() == '':
229 body += '.\n\n'
230 else:
231 body += ':\n\n' + text + '\n\n'
232 body += '[1] ' + user_uri
233 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
235 send_notification(to, subject, body, cc, thread_id)
238 if __name__ == '__main__':
239 action = sys.argv[1]
240 action_map = {
241 'send-resetkey': send_resetkey,
242 'welcome': welcome,
243 'comment': comment,
244 'flag': flag,
245 'delete': delete,
246 'request-open': request_open,
247 'request-close': request_close,
250 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
251 passwd=aur_db_pass, db=aur_db_name,
252 unix_socket=aur_db_socket, buffered=True)
253 cur = db.cursor()
255 action_map[action](cur, *sys.argv[2:])
257 db.commit()
258 db.close()