notify.py 15.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/python3

import configparser
import email.mime.text
import mysql.connector
import os
import subprocess
import sys
import textwrap

config = configparser.RawConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + '/../conf/config')

aur_db_host = config.get('database', 'host')
aur_db_name = config.get('database', 'name')
aur_db_user = config.get('database', 'user')
aur_db_pass = config.get('database', 'password')
aur_db_socket = config.get('database', 'socket')

aur_location = config.get('options', 'aur_location')
aur_request_ml = config.get('options', 'aur_request_ml')

sendmail = config.get('notifications', 'sendmail')
sender = config.get('notifications', 'sender')
reply_to = config.get('notifications', 'reply-to')


28
29
30
def headers_cc(cclist):
    return {'Cc': str.join(', ', cclist)}

31

32
33
34
def headers_msgid(thread_id):
    return {'Message-ID': thread_id}

35

36
37
38
def headers_reply(thread_id):
    return {'In-Reply-To': thread_id, 'References': thread_id}

39

40
def send_notification(to, subject, body, refs, headers={}):
41
42
43
    wrapped = ''
    for line in body.splitlines():
        wrapped += textwrap.fill(line, break_long_words=False) + '\n'
44
45
46
47
    if refs:
        body = wrapped + '\n' + refs
    else:
        body = wrapped
48
49
50
51
52
53
54
55

    for recipient in to:
        msg = email.mime.text.MIMEText(body, 'plain', 'utf-8')
        msg['Subject'] = subject
        msg['From'] = sender
        msg['Reply-to'] = reply_to
        msg['To'] = recipient

56
57
        for key, value in headers.items():
            msg[key] = value
58
59
60
61

        p = subprocess.Popen([sendmail, '-t', '-oi'], stdin=subprocess.PIPE)
        p.communicate(msg.as_bytes())

62

63
64
65
66
def username_from_id(cur, uid):
    cur.execute('SELECT UserName FROM Users WHERE ID = %s', [uid])
    return cur.fetchone()[0]

67

68
69
70
71
def pkgbase_from_id(cur, pkgbase_id):
    cur.execute('SELECT Name FROM PackageBases WHERE ID = %s', [pkgbase_id])
    return cur.fetchone()[0]

72

73
74
75
76
77
def pkgbase_from_pkgreq(cur, reqid):
    cur.execute('SELECT PackageBaseID FROM PackageRequests WHERE ID = %s',
                [reqid])
    return cur.fetchone()[0]

78

79
80
81
82
def get_user_email(cur, uid):
    cur.execute('SELECT Email FROM Users WHERE ID = %s', [uid])
    return cur.fetchone()[0]

83

84
85
86
87
88
89
90
def get_maintainer_email(cur, pkgbase_id):
    cur.execute('SELECT Users.Email FROM Users ' +
                'INNER JOIN PackageBases ' +
                'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
                'PackageBases.ID = %s', [pkgbase_id])
    return cur.fetchone()[0]

91

92
93
def get_recipients(cur, pkgbase_id, uid):
    cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
94
95
96
97
                'INNER JOIN PackageNotifications ' +
                'ON PackageNotifications.UserID = Users.ID WHERE ' +
                'PackageNotifications.UserID != %s AND ' +
                'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
98
99
    return [row[0] for row in cur.fetchall()]

100

101
102
103
104
105
106
107
108
109
110
def get_comment_recipients(cur, pkgbase_id, uid):
    cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
                'INNER JOIN PackageNotifications ' +
                'ON PackageNotifications.UserID = Users.ID WHERE ' +
                'Users.CommentNotify = 1 AND ' +
                'PackageNotifications.UserID != %s AND ' +
                'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
    return [row[0] for row in cur.fetchall()]


111
112
113
114
115
116
117
118
119
120
def get_update_recipients(cur, pkgbase_id, uid):
    cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
                'INNER JOIN PackageNotifications ' +
                'ON PackageNotifications.UserID = Users.ID WHERE ' +
                'Users.UpdateNotify = 1 AND ' +
                'PackageNotifications.UserID != %s AND ' +
                'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
    return [row[0] for row in cur.fetchall()]


121
122
123
124
125
126
127
128
129
130
def get_ownership_recipients(cur, pkgbase_id, uid):
    cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
                'INNER JOIN PackageNotifications ' +
                'ON PackageNotifications.UserID = Users.ID WHERE ' +
                'Users.OwnershipNotify = 1 AND ' +
                'PackageNotifications.UserID != %s AND ' +
                'PackageNotifications.PackageBaseID = %s', [uid, pkgbase_id])
    return [row[0] for row in cur.fetchall()]


131
132
def get_request_recipients(cur, reqid):
    cur.execute('SELECT DISTINCT Users.Email FROM PackageRequests ' +
133
                'INNER JOIN PackageBases ' +
134
135
136
137
138
                'ON PackageBases.ID = PackageRequests.PackageBaseID ' +
                'INNER JOIN Users ' +
                'ON Users.ID = PackageRequests.UsersID ' +
                'OR Users.ID = PackageBases.MaintainerUID ' +
                'WHERE PackageRequests.ID = %s', [reqid])
139
140
    return [row[0] for row in cur.fetchall()]

141

142
143
144
145
146
def get_comment(cur, comment_id):
    cur.execute('SELECT Comments FROM PackageComments WHERE ID = %s',
                [comment_id])
    return cur.fetchone()[0]

147

148
149
150
151
152
def get_flagger_comment(cur, pkgbase_id):
    cur.execute('SELECT FlaggerComment FROM PackageBases WHERE ID = %s',
                [pkgbase_id])
    return cur.fetchone()[0]

153

154
155
156
157
def get_request_comment(cur, reqid):
    cur.execute('SELECT Comments FROM PackageRequests WHERE ID = %s', [reqid])
    return cur.fetchone()[0]

158

159
160
161
162
163
def get_request_closure_comment(cur, reqid):
    cur.execute('SELECT ClosureComment FROM PackageRequests WHERE ID = %s',
                [reqid])
    return cur.fetchone()[0]

164

165
166
167
168
169
170
171
def send_resetkey(cur, uid):
    cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
                [uid])
    username, to, resetkey = cur.fetchone()

    subject = 'AUR Password Reset'
    body = 'A password reset request was submitted for the account %s ' \
172
173
174
           'associated with your email address. If you wish to reset your ' \
           'password follow the link [1] below, otherwise ignore this ' \
           'message and nothing will happen.' % (username)
175
    refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
176

177
    send_notification([to], subject, body, refs)
178

179

180
181
182
183
184
185
def welcome(cur, uid):
    cur.execute('SELECT UserName, Email, ResetKey FROM Users WHERE ID = %s',
                [uid])
    username, to, resetkey = cur.fetchone()

    subject = 'Welcome to the Arch User Repository'
186
187
188
189
    body = 'Welcome to the Arch User Repository! In order to set an initial ' \
           'password for your new account, please click the link [1] below. ' \
           'If the link does not work, try copying and pasting it into your ' \
           'browser.'
190
    refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
191

192
    send_notification([to], subject, body, refs)
193

194

195
def comment(cur, uid, pkgbase_id, comment_id):
196
197
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
198
    to = get_comment_recipients(cur, pkgbase_id, uid)
199
    text = get_comment(cur, comment_id)
200

201
202
203
    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

204
    subject = 'AUR Comment for %s' % (pkgbase)
205
206
207
208
209
    body = '%s [1] added the following comment to %s [2]:' % (user, pkgbase)
    body += '\n\n' + text + '\n\n'
    body += 'If you no longer wish to receive notifications about this ' \
            'package, please go to the package page [2] and select "%s".' % \
            ('Disable notifications')
210
211
    refs = '[1] ' + user_uri + '\n'
    refs += '[2] ' + pkgbase_uri
212
    thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
213
    headers = headers_reply(thread_id)
214

215
    send_notification(to, subject, body, refs, headers)
216

217

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def update(cur, uid, pkgbase_id):
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = get_update_recipients(cur, pkgbase_id, uid)

    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

    subject = 'AUR Package Update: %s' % (pkgbase)
    body = '%s [1] pushed a new commit to %s [2].' % (user, pkgbase)
    body += '\n\n'
    body += 'If you no longer wish to receive notifications about this ' \
            'package, please go to the package page [2] and select "%s".' % \
            ('Disable notifications')
    refs = '[1] ' + user_uri + '\n'
    refs += '[2] ' + pkgbase_uri
    thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
    headers = headers_reply(thread_id)

    send_notification(to, subject, body, refs, headers)


240
241
242
243
def flag(cur, uid, pkgbase_id):
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = [get_maintainer_email(cur, pkgbase_id)]
244
    text = get_flagger_comment(cur, pkgbase_id)
245
246
247
248
249

    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

    subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
250
    body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
251
           (pkgbase, user)
252
253
254
    body += '\n\n' + text
    refs = '[1] ' + pkgbase_uri + '\n'
    refs += '[2] ' + user_uri
255

256
    send_notification(to, subject, body, refs)
257

258

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
def adopt(cur, pkgbase_id, uid):
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = get_ownership_recipients(cur, pkgbase_id, uid)

    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

    subject = 'AUR Ownership Notification for %s' % (pkgbase)
    body = 'The package %s [1] was adopted by %s [2].' % (pkgbase, user)
    refs = '[1] ' + pkgbase_uri + '\n'
    refs += '[2] ' + user_uri

    send_notification(to, subject, body, refs)


def disown(cur, pkgbase_id, uid):
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = get_ownership_recipients(cur, pkgbase_id, uid)

    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

    subject = 'AUR Ownership Notification for %s' % (pkgbase)
    body = 'The package %s [1] was disowned by %s [2].' % (pkgbase, user)
    refs = '[1] ' + pkgbase_uri + '\n'
    refs += '[2] ' + user_uri

    send_notification(to, subject, body, refs)


291
292
293
294
295
296
297
298
299
300
301
302
def comaintainer_add(cur, pkgbase_id, uid):
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = [get_user_email(cur, uid)]

    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

    subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
    body = 'You were added to the co-maintainer list of %s [1].' % (pkgbase)
    refs = '[1] ' + pkgbase_uri + '\n'

    send_notification(to, subject, body, refs)

303

304
305
306
307
308
309
310
def comaintainer_remove(cur, pkgbase_id, uid):
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = [get_user_email(cur, uid)]

    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

    subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
311
312
    body = ('You were removed from the co-maintainer list of %s [1].' %
            (pkgbase))
313
314
315
316
    refs = '[1] ' + pkgbase_uri + '\n'

    send_notification(to, subject, body, refs)

317

318
319
320
321
322
323
324
def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
    user = username_from_id(cur, uid)
    old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
    if new_pkgbase_id:
        new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
    to = get_recipients(cur, old_pkgbase_id, uid)

325
    user_uri = aur_location + '/account/' + user + '/'
326
    pkgbase_uri = aur_location + '/pkgbase/' + old_pkgbase + '/'
327

328
329
330
    subject = 'AUR Package deleted: %s' % (old_pkgbase)
    if new_pkgbase_id:
        new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
331
        body = '%s [1] merged %s [2] into %s [3].\n\n' \
332
333
334
               'If you no longer wish receive notifications about the new ' \
               'package, please go to [3] and click "%s".' %\
               (user, old_pkgbase, new_pkgbase, 'Disable notifications')
335
336
337
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri + '\n'
        refs += '[3] ' + new_pkgbase_uri
338
    else:
339
340
341
        body = '%s [1] deleted %s [2].\n\n' \
               'You will no longer receive notifications about this ' \
               'package.' % (user, old_pkgbase)
342
343
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri
344

345
    send_notification(to, subject, body, refs)
346

347

348
349
350
351
def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = [aur_request_ml]
352
    cc = get_request_recipients(cur, reqid)
353
    text = get_request_comment(cur, reqid)
354
355
356
357
358
359
360
361

    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

    subject = '[PRQ#%d] %s Request for %s' % \
              (int(reqid), reqtype.title(), pkgbase)
    if merge_into:
        merge_into_uri = aur_location + '/pkgbase/' + merge_into + '/'
362
363
        body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
               (user, pkgbase, merge_into)
364
365
366
367
        body += '\n\n' + text
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri + '\n'
        refs += '[3] ' + merge_into_uri
368
    else:
369
370
        body = '%s [1] filed a %s request for %s [2]:' % \
               (user, reqtype, pkgbase)
371
372
373
        body += '\n\n' + text
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri + '\n'
374
    thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
375
376
    # Use a deterministic Message-ID for the first email referencing a request.
    headers = headers_msgid(thread_id)
377
    headers.update(headers_cc(cc))
378

379
    send_notification(to, subject, body, refs, headers)
380

381

382
383
def request_close(cur, uid, reqid, reason):
    to = [aur_request_ml]
384
    cc = get_request_recipients(cur, reqid)
385
    text = get_request_closure_comment(cur, reqid)
386
387

    subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
388
389
390
391
392
393
394
395
396
    if int(uid):
        user = username_from_id(cur, uid)
        user_uri = aur_location + '/account/' + user + '/'
        body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
        refs = '[1] ' + user_uri
    else:
        body = 'Request #%d has been %s automatically by the Arch User ' \
               'Repository package request system' % (int(reqid), reason)
        refs = None
397
    if text.strip() == '':
398
        body += '.'
399
    else:
400
        body += ':\n\n' + text
401
    thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
402
403
    headers = headers_reply(thread_id)
    headers.update(headers_cc(cc))
404

405
    send_notification(to, subject, body, refs, headers)
406
407
408
409
410
411
412
413


if __name__ == '__main__':
    action = sys.argv[1]
    action_map = {
        'send-resetkey': send_resetkey,
        'welcome': welcome,
        'comment': comment,
414
        'update': update,
415
        'flag': flag,
416
417
        'adopt': adopt,
        'disown': disown,
418
419
        'comaintainer-add': comaintainer_add,
        'comaintainer-remove': comaintainer_remove,
420
421
422
423
424
425
426
427
428
429
430
431
432
433
        'delete': delete,
        'request-open': request_open,
        'request-close': request_close,
    }

    db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
                                 passwd=aur_db_pass, db=aur_db_name,
                                 unix_socket=aur_db_socket, buffered=True)
    cur = db.cursor()

    action_map[action](cur, *sys.argv[2:])

    db.commit()
    db.close()