Port notification routines to Python
[aur.git] / scripts / notify.py
blob017555a71c8f73188439b4de3c99dc298cca03cf
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 e-mail address. If you wish to reset your ' \
92 'password follow the link below, otherwise ignore this message ' \
93 '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 %s! In order to set an initial password for your new ' \
106 'account, please click the link below. If the link does not work ' \
107 'try copying and pasting it into your browser.' % (aur_location)
108 body += '\n\n'
109 body += '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
111 send_notification([to], subject, body)
113 def comment(cur, uid, pkgbase_id):
114 user = username_from_id(cur, uid)
115 pkgbase = pkgbase_from_id(cur, pkgbase_id)
116 to = get_recipients(cur, pkgbase_id, uid)
117 text = sys.stdin.read()
119 uri = aur_location + '/pkgbase/' + pkgbase + '/'
121 subject = 'AUR Comment for %s' % (pkgbase)
122 body = 'from %s\n%s wrote:\n\n%s\n\n--- \n' \
123 'If you no longer wish to receive notifications about this ' \
124 'package, please go the the above package page and click the ' \
125 'UnNotify button.' % (uri, user, text)
126 thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
128 send_notification(to, subject, body, reference=thread_id)
130 def flag(cur, uid, pkgbase_id):
131 user = username_from_id(cur, uid)
132 pkgbase = pkgbase_from_id(cur, pkgbase_id)
133 to = [get_maintainer_email(cur, pkgbase_id)]
135 user_uri = aur_location + '/account/' + user + '/'
136 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
138 subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
139 body = 'Your package %s has been flagged out of date by %s [1]. ' \
140 'You may view your package at:\n%s\n\n[1] %s' % \
141 (pkgbase, user, pkgbase_uri, user_uri)
143 send_notification(to, subject, body)
145 def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
146 user = username_from_id(cur, uid)
147 old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
148 if new_pkgbase_id:
149 new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
150 to = get_recipients(cur, old_pkgbase_id, uid)
152 subject = 'AUR Package deleted: %s' % (old_pkgbase)
153 if new_pkgbase_id:
154 new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
155 body = '%s merged "%s" into "%s".\n\nYou will no longer receive ' \
156 'notifications about this package, please go to %s and click ' \
157 'the Notify button if you wish to recieve them again.' % \
158 (user, old_pkgbase, new_pkgbase, new_pkgbase_uri)
159 else:
160 body = '%s deleted "%s".\n\nYou will no longer receive ' \
161 'notifications about this package.' % (user, old_pkgbase)
163 send_notification(to, subject, body)
165 def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
166 user = username_from_id(cur, uid)
167 pkgbase = pkgbase_from_id(cur, pkgbase_id)
168 to = [aur_request_ml]
169 cc = get_request_recipients(cur, pkgbase_id, uid)
170 text = sys.stdin.read()
172 user_uri = aur_location + '/account/' + user + '/'
173 pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'
175 subject = '[PRQ#%d] %s Request for %s' % \
176 (int(reqid), reqtype.title(), pkgbase)
177 if merge_into:
178 merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
179 body = '%s [1] filed a request to merge %s [2] into %s [3]:\n\n' \
180 '%s\n\n[1] %s\n[2] %s\n[3] %s\n' % \
181 (user, pkgbase, merge_into, text, user_uri, pkgbase_uri,
182 merge_into_uri)
183 else:
184 body = '%s [1] filed a %s request for %s [2]:\n\n' \
185 '%s\n\n[1] %s\n[2] %s\n' % \
186 (user, reqtype, pkgbase, text, user_uri, pkgbase_uri)
187 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
189 send_notification(to, subject, body, cc, thread_id)
191 def request_close(cur, uid, reqid, reason):
192 user = username_from_id(cur, uid)
193 pkgbase_id = pkgbase_from_pkgreq(cur, reqid)
194 to = [aur_request_ml]
195 cc = get_request_recipients(cur, pkgbase_id, uid)
196 text = sys.stdin.read()
198 user_uri = aur_location + '/account/' + user + '/'
200 subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
201 body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
202 if text.strip() == '':
203 body += '.\n\n'
204 else:
205 body += ':\n\n' + text + '\n\n'
206 body += '[1] ' + user_uri
207 thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
209 send_notification(to, subject, body, cc, thread_id)
212 if __name__ == '__main__':
213 action = sys.argv[1]
214 action_map = {
215 'send-resetkey': send_resetkey,
216 'welcome': welcome,
217 'comment': comment,
218 'flag': flag,
219 'delete': delete,
220 'request-open': request_open,
221 'request-close': request_close,
224 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
225 passwd=aur_db_pass, db=aur_db_name,
226 unix_socket=aur_db_socket, buffered=True)
227 cur = db.cursor()
229 action_map[action](cur, *sys.argv[2:])
231 db.commit()
232 db.close()