notify.py 16.1 KB
Newer Older
1
#!/usr/bin/env python3
2
3
4
5
6
7

import email.mime.text
import subprocess
import sys
import textwrap

Lukas Fleischer's avatar
Lukas Fleischer committed
8
9
import aurweb.config
import aurweb.db
10

Lukas Fleischer's avatar
Lukas Fleischer committed
11
12
aur_location = aurweb.config.get('options', 'aur_location')
aur_request_ml = aurweb.config.get('options', 'aur_request_ml')
13

Lukas Fleischer's avatar
Lukas Fleischer committed
14
15
16
sendmail = aurweb.config.get('notifications', 'sendmail')
sender = aurweb.config.get('notifications', 'sender')
reply_to = aurweb.config.get('notifications', 'reply-to')
17
18


19
20
21
def headers_cc(cclist):
    return {'Cc': str.join(', ', cclist)}

22

23
24
25
def headers_msgid(thread_id):
    return {'Message-ID': thread_id}

26

27
28
29
def headers_reply(thread_id):
    return {'In-Reply-To': thread_id, 'References': thread_id}

30

31
def send_notification(to, subject, body, refs, headers={}):
32
33
34
    wrapped = ''
    for line in body.splitlines():
        wrapped += textwrap.fill(line, break_long_words=False) + '\n'
35
36
37
    if refs:
        body = wrapped + '\n' + refs
    else:
38
        body = wrapped.rstrip()
39
40
41
42
43
44
45
46

    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

47
48
        for key, value in headers.items():
            msg[key] = value
49
50
51
52

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

53

Lukas Fleischer's avatar
Lukas Fleischer committed
54
55
def username_from_id(conn, uid):
    cur = conn.execute('SELECT UserName FROM Users WHERE ID = ?', [uid])
56
57
    return cur.fetchone()[0]

58

Lukas Fleischer's avatar
Lukas Fleischer committed
59
60
61
def pkgbase_from_id(conn, pkgbase_id):
    cur = conn.execute('SELECT Name FROM PackageBases WHERE ID = ?',
                       [pkgbase_id])
62
63
    return cur.fetchone()[0]

64

Lukas Fleischer's avatar
Lukas Fleischer committed
65
66
67
def pkgbase_from_pkgreq(conn, reqid):
    cur = conn.execute('SELECT PackageBaseID FROM PackageRequests ' +
                       'WHERE ID = ?', [reqid])
68
69
    return cur.fetchone()[0]

70

Lukas Fleischer's avatar
Lukas Fleischer committed
71
72
def get_user_email(conn, uid):
    cur = conn.execute('SELECT Email FROM Users WHERE ID = ?', [uid])
73
74
    return cur.fetchone()[0]

75

76
77
78
79
def get_flag_recipients(conn, pkgbase_id):
    cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
                       'LEFT JOIN PackageComaintainers ' +
                       'ON PackageComaintainers.UsersID = Users.ID ' +
Lukas Fleischer's avatar
Lukas Fleischer committed
80
                       'INNER JOIN PackageBases ' +
81
82
83
84
                       'ON PackageBases.MaintainerUID = Users.ID OR ' +
                       'PackageBases.ID = PackageComaintainers.PackageBaseID ' +
                       'WHERE PackageBases.ID = ?', [pkgbase_id])
    return [row[0] for row in cur.fetchall()]
85

86

Lukas Fleischer's avatar
Lukas Fleischer committed
87
88
89
90
91
92
93
def get_recipients(conn, pkgbase_id, uid):
    cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
                       'INNER JOIN PackageNotifications ' +
                       'ON PackageNotifications.UserID = Users.ID WHERE ' +
                       'PackageNotifications.UserID != ? AND ' +
                       'PackageNotifications.PackageBaseID = ?',
                       [uid, pkgbase_id])
94
95
    return [row[0] for row in cur.fetchall()]

96

Lukas Fleischer's avatar
Lukas Fleischer committed
97
98
99
100
101
102
103
104
def get_comment_recipients(conn, pkgbase_id, uid):
    cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
                       'INNER JOIN PackageNotifications ' +
                       'ON PackageNotifications.UserID = Users.ID WHERE ' +
                       'Users.CommentNotify = 1 AND ' +
                       'PackageNotifications.UserID != ? AND ' +
                       'PackageNotifications.PackageBaseID = ?',
                       [uid, pkgbase_id])
105
106
107
    return [row[0] for row in cur.fetchall()]


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


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


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

140

Lukas Fleischer's avatar
Lukas Fleischer committed
141
def get_tu_vote_reminder_recipients(conn, vote_id):
142
    cur = conn.execute('SELECT Email FROM Users ' +
143
                       'WHERE AccountTypeID IN (2, 4) AND ID NOT IN ' +
144
145
                       '(SELECT UserID FROM TU_Votes ' +
                       'WHERE TU_Votes.VoteID = ?)', [vote_id])
146
147
148
    return [row[0] for row in cur.fetchall()]


Lukas Fleischer's avatar
Lukas Fleischer committed
149
150
151
def get_comment(conn, comment_id):
    cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?',
                       [comment_id])
152
153
    return cur.fetchone()[0]

154

Lukas Fleischer's avatar
Lukas Fleischer committed
155
156
157
def get_flagger_comment(conn, pkgbase_id):
    cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ID = ?',
                       [pkgbase_id])
158
159
    return cur.fetchone()[0]

160

Lukas Fleischer's avatar
Lukas Fleischer committed
161
162
163
def get_request_comment(conn, reqid):
    cur = conn.execute('SELECT Comments FROM PackageRequests WHERE ID = ?',
                       [reqid])
164
165
    return cur.fetchone()[0]

166

Lukas Fleischer's avatar
Lukas Fleischer committed
167
168
169
def get_request_closure_comment(conn, reqid):
    cur = conn.execute('SELECT ClosureComment FROM PackageRequests ' +
                       'WHERE ID = ?', [reqid])
170
171
    return cur.fetchone()[0]

172

Lukas Fleischer's avatar
Lukas Fleischer committed
173
174
175
def send_resetkey(conn, uid):
    cur = conn.execute('SELECT UserName, Email, ResetKey FROM Users ' +
                       'WHERE ID = ?', [uid])
176
177
178
179
    username, to, resetkey = cur.fetchone()

    subject = 'AUR Password Reset'
    body = 'A password reset request was submitted for the account %s ' \
180
181
182
           '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)
183
    refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
184

185
    send_notification([to], subject, body, refs)
186

187

Lukas Fleischer's avatar
Lukas Fleischer committed
188
189
190
def welcome(conn, uid):
    cur = conn.execute('SELECT UserName, Email, ResetKey FROM Users ' +
                       'WHERE ID = ?', [uid])
191
192
193
    username, to, resetkey = cur.fetchone()

    subject = 'Welcome to the Arch User Repository'
194
195
196
197
    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.'
198
    refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
199

200
    send_notification([to], subject, body, refs)
201

202

Lukas Fleischer's avatar
Lukas Fleischer committed
203
204
205
206
207
def comment(conn, uid, pkgbase_id, comment_id):
    user = username_from_id(conn, uid)
    pkgbase = pkgbase_from_id(conn, pkgbase_id)
    to = get_comment_recipients(conn, pkgbase_id, uid)
    text = get_comment(conn, comment_id)
208

209
210
211
    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

212
    subject = 'AUR Comment for %s' % (pkgbase)
213
214
215
216
217
    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')
218
219
    refs = '[1] ' + user_uri + '\n'
    refs += '[2] ' + pkgbase_uri
220
    thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
221
    headers = headers_reply(thread_id)
222

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

225

Lukas Fleischer's avatar
Lukas Fleischer committed
226
227
228
229
def update(conn, uid, pkgbase_id):
    user = username_from_id(conn, uid)
    pkgbase = pkgbase_from_id(conn, pkgbase_id)
    to = get_update_recipients(conn, pkgbase_id, uid)
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

    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)


Lukas Fleischer's avatar
Lukas Fleischer committed
248
249
250
def flag(conn, uid, pkgbase_id):
    user = username_from_id(conn, uid)
    pkgbase = pkgbase_from_id(conn, pkgbase_id)
251
    to = get_flag_recipients(conn, pkgbase_id)
Lukas Fleischer's avatar
Lukas Fleischer committed
252
    text = get_flagger_comment(conn, pkgbase_id)
253
254
255
256
257

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

    subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
258
    body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
259
           (pkgbase, user)
260
261
262
    body += '\n\n' + text
    refs = '[1] ' + pkgbase_uri + '\n'
    refs += '[2] ' + user_uri
263

264
    send_notification(to, subject, body, refs)
265

266

Lukas Fleischer's avatar
Lukas Fleischer committed
267
268
269
270
def adopt(conn, pkgbase_id, uid):
    user = username_from_id(conn, uid)
    pkgbase = pkgbase_from_id(conn, pkgbase_id)
    to = get_ownership_recipients(conn, pkgbase_id, uid)
271
272
273
274
275
276
277
278
279
280
281
282

    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)


Lukas Fleischer's avatar
Lukas Fleischer committed
283
284
285
286
def disown(conn, pkgbase_id, uid):
    user = username_from_id(conn, uid)
    pkgbase = pkgbase_from_id(conn, pkgbase_id)
    to = get_ownership_recipients(conn, pkgbase_id, uid)
287
288
289
290
291
292
293
294
295
296
297
298

    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)


Lukas Fleischer's avatar
Lukas Fleischer committed
299
300
301
def comaintainer_add(conn, pkgbase_id, uid):
    pkgbase = pkgbase_from_id(conn, pkgbase_id)
    to = [get_user_email(conn, uid)]
302
303
304
305
306

    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)
307
    refs = '[1] ' + pkgbase_uri
308
309
310

    send_notification(to, subject, body, refs)

311

Lukas Fleischer's avatar
Lukas Fleischer committed
312
313
314
def comaintainer_remove(conn, pkgbase_id, uid):
    pkgbase = pkgbase_from_id(conn, pkgbase_id)
    to = [get_user_email(conn, uid)]
315
316
317
318

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

    subject = 'AUR Co-Maintainer Notification for %s' % (pkgbase)
319
320
    body = ('You were removed from the co-maintainer list of %s [1].' %
            (pkgbase))
321
    refs = '[1] ' + pkgbase_uri
322
323
324

    send_notification(to, subject, body, refs)

325

Lukas Fleischer's avatar
Lukas Fleischer committed
326
327
328
def delete(conn, uid, old_pkgbase_id, new_pkgbase_id=None):
    user = username_from_id(conn, uid)
    old_pkgbase = pkgbase_from_id(conn, old_pkgbase_id)
329
    if new_pkgbase_id:
Lukas Fleischer's avatar
Lukas Fleischer committed
330
331
        new_pkgbase = pkgbase_from_id(conn, new_pkgbase_id)
    to = get_recipients(conn, old_pkgbase_id, uid)
332

333
    user_uri = aur_location + '/account/' + user + '/'
334
    pkgbase_uri = aur_location + '/pkgbase/' + old_pkgbase + '/'
335

336
337
338
    subject = 'AUR Package deleted: %s' % (old_pkgbase)
    if new_pkgbase_id:
        new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
339
        body = '%s [1] merged %s [2] into %s [3].\n\n' \
340
341
342
               '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')
343
344
345
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri + '\n'
        refs += '[3] ' + new_pkgbase_uri
346
    else:
347
348
349
        body = '%s [1] deleted %s [2].\n\n' \
               'You will no longer receive notifications about this ' \
               'package.' % (user, old_pkgbase)
350
351
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri
352

353
    send_notification(to, subject, body, refs)
354

355

Lukas Fleischer's avatar
Lukas Fleischer committed
356
357
358
def request_open(conn, uid, reqid, reqtype, pkgbase_id, merge_into=None):
    user = username_from_id(conn, uid)
    pkgbase = pkgbase_from_id(conn, pkgbase_id)
359
    to = [aur_request_ml]
Lukas Fleischer's avatar
Lukas Fleischer committed
360
361
    cc = get_request_recipients(conn, reqid)
    text = get_request_comment(conn, reqid)
362
363
364
365
366
367
368
369

    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 + '/'
370
371
        body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
               (user, pkgbase, merge_into)
372
373
374
375
        body += '\n\n' + text
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri + '\n'
        refs += '[3] ' + merge_into_uri
376
    else:
377
378
        body = '%s [1] filed a %s request for %s [2]:' % \
               (user, reqtype, pkgbase)
379
380
        body += '\n\n' + text
        refs = '[1] ' + user_uri + '\n'
381
        refs += '[2] ' + pkgbase_uri
382
    thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
383
384
    # Use a deterministic Message-ID for the first email referencing a request.
    headers = headers_msgid(thread_id)
385
    headers.update(headers_cc(cc))
386

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

389

Lukas Fleischer's avatar
Lukas Fleischer committed
390
def request_close(conn, uid, reqid, reason):
391
    to = [aur_request_ml]
Lukas Fleischer's avatar
Lukas Fleischer committed
392
393
    cc = get_request_recipients(conn, reqid)
    text = get_request_closure_comment(conn, reqid)
394
395

    subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
396
    if int(uid):
Lukas Fleischer's avatar
Lukas Fleischer committed
397
        user = username_from_id(conn, uid)
398
399
400
401
402
403
404
        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
405
    if text.strip() == '':
406
        body += '.'
407
    else:
408
        body += ':\n\n' + text
409
    thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
410
411
    headers = headers_reply(thread_id)
    headers.update(headers_cc(cc))
412

413
    send_notification(to, subject, body, refs, headers)
414
415


Lukas Fleischer's avatar
Lukas Fleischer committed
416
417
def tu_vote_reminder(conn, vote_id):
    to = get_tu_vote_reminder_recipients(conn, vote_id)
418
419
420
421
422
423
424
425
426
427
428

    vote_uri = aur_location + '/tu/?id=' + vote_id

    subject = 'TU Vote Reminder: Proposal %d' % (int(vote_id))
    body = 'Please remember to cast your vote on proposal %d [1]. ' \
           'The voting period ends in less than 48 hours.' % (int(vote_id))
    refs = '[1] ' + vote_uri

    send_notification(to, subject, body, refs)


429
def main():
430
431
432
433
434
    action = sys.argv[1]
    action_map = {
        'send-resetkey': send_resetkey,
        'welcome': welcome,
        'comment': comment,
435
        'update': update,
436
        'flag': flag,
437
438
        'adopt': adopt,
        'disown': disown,
439
440
        'comaintainer-add': comaintainer_add,
        'comaintainer-remove': comaintainer_remove,
441
442
443
        'delete': delete,
        'request-open': request_open,
        'request-close': request_close,
444
        'tu-vote-reminder': tu_vote_reminder,
445
446
    }

Lukas Fleischer's avatar
Lukas Fleischer committed
447
    conn = aurweb.db.Connection()
448

Lukas Fleischer's avatar
Lukas Fleischer committed
449
    action_map[action](conn, *sys.argv[2:])
450

Lukas Fleischer's avatar
Lukas Fleischer committed
451
452
    conn.commit()
    conn.close()
453
454
455
456


if __name__ == '__main__':
    main()