notify.py 18.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
aur_location = aurweb.config.get('options', 'aur_location')
12
13


14
15
16
def headers_cc(cclist):
    return {'Cc': str.join(', ', cclist)}

17

18
19
20
def headers_msgid(thread_id):
    return {'Message-ID': thread_id}

21

22
23
24
def headers_reply(thread_id):
    return {'In-Reply-To': thread_id, 'References': thread_id}

25

Lukas Fleischer's avatar
Lukas Fleischer committed
26
27
def username_from_id(conn, uid):
    cur = conn.execute('SELECT UserName FROM Users WHERE ID = ?', [uid])
28
29
    return cur.fetchone()[0]

30

Lukas Fleischer's avatar
Lukas Fleischer committed
31
32
33
def pkgbase_from_id(conn, pkgbase_id):
    cur = conn.execute('SELECT Name FROM PackageBases WHERE ID = ?',
                       [pkgbase_id])
34
35
    return cur.fetchone()[0]

36

Lukas Fleischer's avatar
Lukas Fleischer committed
37
38
39
def pkgbase_from_pkgreq(conn, reqid):
    cur = conn.execute('SELECT PackageBaseID FROM PackageRequests ' +
                       'WHERE ID = ?', [reqid])
40
41
    return cur.fetchone()[0]

42

Lukas Fleischer's avatar
Lukas Fleischer committed
43
44
def get_user_email(conn, uid):
    cur = conn.execute('SELECT Email FROM Users WHERE ID = ?', [uid])
45
46
    return cur.fetchone()[0]

47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
class Notification:
    def get_refs(self):
        return ()

    def get_headers(self):
        return {}

    def send(self):
        body = ''
        for line in self.get_body().splitlines():
            body += textwrap.fill(line, break_long_words=False) + '\n'
        for i, ref in enumerate(self.get_refs()):
            body += '\n' + '[%d] %s' % (i + 1, ref)
        body = body.rstrip()

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

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

            for key, value in self.get_headers().items():
                msg[key] = value

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


class ResetKeyNotification(Notification):
    def __init__(self, conn, uid):
        cur = conn.execute('SELECT UserName, Email, ResetKey FROM Users ' +
                           'WHERE ID = ?', [uid])
        self._username, self._to, self._resetkey = cur.fetchone()

    def get_recipients(self):
        return [self._to]

    def get_subject(self):
        return 'AUR Password Reset'

    def get_body(self):
        return 'A password reset request was submitted for the account %s ' \
               '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.' % (self._username)

    def get_refs(self):
        return (aur_location + '/passreset/?resetkey=' + self._resetkey,)


class WelcomeNotification(ResetKeyNotification):
    def get_subject(self):
        return 'Welcome to the Arch User Repository'

    def get_body(self):
        return '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.'


class CommentNotification(Notification):
    def __init__(self, conn, uid, pkgbase_id, comment_id):
        self._user = username_from_id(conn, uid)
        self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
        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])
        self._to = [row[0] for row in cur.fetchall()]
        cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?',
                           [comment_id])
        self._text = cur.fetchone()[0]

    def get_recipients(self):
        return self._to

    def get_subject(self):
        return 'AUR Comment for %s' % (self._pkgbase)

    def get_body(self):
        body = '%s [1] added the following comment to %s [2]:' % \
               (self._user, self._pkgbase)
        body += '\n\n' + self._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')
        return body

    def get_refs(self):
        return (aur_location + '/account/' + self._user + '/',
                aur_location + '/pkgbase/' + self._pkgbase + '/')

    def get_headers(self):
        thread_id = '<pkg-notifications-' + self._pkgbase + \
                    '@aur.archlinux.org>'
        return headers_reply(thread_id)


class UpdateNotification(Notification):
    def __init__(self, conn, uid, pkgbase_id):
        self._user = username_from_id(conn, uid)
        self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
        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])
        self._to = [row[0] for row in cur.fetchall()]

    def get_recipients(self):
        return self._to

    def get_subject(self):
        return 'AUR Package Update: %s' % (self._pkgbase)

    def get_body(self):
        body = '%s [1] pushed a new commit to %s [2].' % \
               (self._user, self._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')
        return body

    def get_refs(self):
        return (aur_location + '/account/' + self._user + '/',
                aur_location + '/pkgbase/' + self._pkgbase + '/')

    def get_headers(self):
        thread_id = '<pkg-notifications-' + self._pkgbase + \
                    '@aur.archlinux.org>'
        return headers_reply(thread_id)


class FlagNotification(Notification):
    def __init__(self, conn, uid, pkgbase_id):
        self._user = username_from_id(conn, uid)
        self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
        cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
                           'LEFT JOIN PackageComaintainers ' +
                           'ON PackageComaintainers.UsersID = Users.ID ' +
                           'INNER JOIN PackageBases ' +
                           'ON PackageBases.MaintainerUID = Users.ID OR ' +
                           'PackageBases.ID = PackageComaintainers.PackageBaseID ' +
                           'WHERE PackageBases.ID = ?', [pkgbase_id])
        self._to = [row[0] for row in cur.fetchall()]
        cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' +
                           'ID = ?', [pkgbase_id])
        self._text = cur.fetchone()[0]

    def get_recipients(self):
        return self._to

    def get_subject(self):
        return 'AUR Out-of-date Notification for %s' % (self._pkgbase)

    def get_body(self):
        body = 'Your package %s [1] has been flagged out-of-date by ' \
               '%s [2]:' % (self._pkgbase, self._user)
        body += '\n\n' + self._text
        return body

    def get_refs(self):
        return (aur_location + '/pkgbase/' + self._pkgbase + '/',
                aur_location + '/account/' + self._user + '/')


class OwnershipEventNotification(Notification):
    def __init__(self, conn, uid, pkgbase_id):
        self._user = username_from_id(conn, uid)
        self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
        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])
        self._to = [row[0] for row in cur.fetchall()]
        cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' +
                           'ID = ?', [pkgbase_id])
        self._text = cur.fetchone()[0]

    def get_recipients(self):
        return self._to

    def get_subject(self):
        return 'AUR Ownership Notification for %s' % (self._pkgbase)

    def get_refs(self):
        return (aur_location + '/pkgbase/' + self._pkgbase + '/',
                aur_location + '/account/' + self._user + '/')


class AdoptNotification(OwnershipEventNotification):
    def get_body(self):
        return 'The package %s [1] was adopted by %s [2].' % \
               (self._pkgbase, self._user)


class DisownNotification(OwnershipEventNotification):
    def get_body(self):
        return 'The package %s [1] was disowned by %s [2].' % \
               (self._pkgbase, self._user)


class ComaintainershipEventNotification(Notification):
    def __init__(self, conn, uid, pkgbase_id):
        self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
        self._to = get_user_email(conn, uid)

    def get_recipients(self):
        return [self._to]

    def get_subject(self):
        return 'AUR Co-Maintainer Notification for %s' % (self._pkgbase)

    def get_refs(self):
        return (aur_location + '/pkgbase/' + self._pkgbase + '/',)


class ComaintainerAddNotification(ComaintainershipEventNotification):
    def get_body(self):
        return 'You were added to the co-maintainer list of %s [1].' % \
               (self._pkgbase)


class ComaintainerRemoveNotification(ComaintainershipEventNotification):
    def get_body(self):
        return 'You were removed from the co-maintainer list of %s [1].' % \
               (self._pkgbase)


class DeleteNotification(Notification):
    def __init__(self, conn, uid, old_pkgbase_id, new_pkgbase_id=None):
        self._user = username_from_id(conn, uid)
        self._old_pkgbase = pkgbase_from_id(conn, old_pkgbase_id)
        if new_pkgbase_id:
            self._new_pkgbase = pkgbase_from_id(conn, new_pkgbase_id)
        else:
            self._new_pkgbase = None
        cur = conn.execute('SELECT DISTINCT Users.Email FROM Users ' +
                           'INNER JOIN PackageNotifications ' +
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'PackageNotifications.UserID != ? AND ' +
                           'PackageNotifications.PackageBaseID = ?',
                           [uid, old_pkgbase_id])
        self._to = [row[0] for row in cur.fetchall()]

    def get_recipients(self):
        return self._to

    def get_subject(self):
        return 'AUR Package deleted: %s' % (self._old_pkgbase)

    def get_body(self):
        if self._new_pkgbase:
            return '%s [1] merged %s [2] into %s [3].\n\n' \
                   'If you no longer wish receive notifications about the ' \
                   'new package, please go to [3] and click "%s".' % \
                   (self._user, self._old_pkgbase, self._new_pkgbase,
                    'Disable notifications')
        else:
            return '%s [1] deleted %s [2].\n\n' \
                   'You will no longer receive notifications about this ' \
                   'package.' % (self._user, self._old_pkgbase)

    def get_refs(self):
        refs = (aur_location + '/account/' + self._user + '/',
                aur_location + '/pkgbase/' + self._old_pkgbase + '/')
        if self._new_pkgbase:
            refs += (aur_location + '/pkgbase/' + self._new_pkgbase + '/',)
        return refs


class RequestOpenNotification(Notification):
    def __init__(self, conn, uid, reqid, reqtype, pkgbase_id, merge_into=None):
        self._user = username_from_id(conn, uid)
        self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
        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])
        self._to = aurweb.config.get('options', 'aur_request_ml')
        self._cc = [row[0] for row in cur.fetchall()]
        cur = conn.execute('SELECT Comments FROM PackageRequests WHERE ID = ?',
                           [reqid])
        self._text = cur.fetchone()[0]
        self._reqid = int(reqid)
        self._reqtype = reqtype
        self._merge_into = merge_into

    def get_recipients(self):
        return [self._to]

    def get_subject(self):
        return '[PRQ#%d] %s Request for %s' % \
               (self._reqid, self._reqtype.title(), self._pkgbase)

    def get_body(self):
        if self._merge_into:
            body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
                   (self._user, self._pkgbase, self._merge_into)
            body += '\n\n' + self._text
        else:
            body = '%s [1] filed a %s request for %s [2]:' % \
                   (self._user, self._reqtype, self._pkgbase)
            body += '\n\n' + self._text
        return body

    def get_refs(self):
        refs = (aur_location + '/account/' + self._user + '/',
                aur_location + '/pkgbase/' + self._pkgbase + '/')
        if self._merge_into:
            refs += (aur_location + '/pkgbase/' + self._merge_into + '/',)
        return refs

    def get_headers(self):
        thread_id = '<pkg-request-' + str(self._reqid) + '@aur.archlinux.org>'
        # Use a deterministic Message-ID for the first email referencing a
        # request.
        headers = headers_msgid(thread_id)
        headers.update(headers_cc(self._cc))
        return headers


class RequestCloseNotification(Notification):
    def __init__(self, conn, uid, reqid, reason):
        self._user = username_from_id(conn, uid) if int(uid) else None
        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])
        self._to = aurweb.config.get('options', 'aur_request_ml')
        self._cc = [row[0] for row in cur.fetchall()]
        cur = conn.execute('SELECT ClosureComment FROM PackageRequests ' +
                           'WHERE ID = ?', [reqid])
        self._text = cur.fetchone()[0]
        self._reqid = int(reqid)
        self._reason = reason

    def get_recipients(self):
        return [self._to]

    def get_subject(self):
        return '[PRQ#%d] Request %s' % (self._reqid, self._reason.title())

    def get_body(self):
        if self._user:
            body = 'Request #%d has been %s by %s [1]' % \
                   (self._reqid, self._reason, self._user)
        else:
            body = 'Request #%d has been %s automatically by the Arch User ' \
                   'Repository package request system' % \
                   (self._reqid, self._reason)
        if self._text.strip() == '':
            body += '.'
        else:
            body += ':\n\n' + self._text
        return body

    def get_refs(self):
        if self._user:
            return (aur_location + '/account/' + self._user + '/',)
        else:
            return ()

    def get_headers(self):
        thread_id = '<pkg-request-' + str(self._reqid) + '@aur.archlinux.org>'
        headers = headers_reply(thread_id)
        headers.update(headers_cc(self._cc))
        return headers


class TUVoteReminderNotification(Notification):
    def __init__(self, conn, vote_id):
        self._vote_id = int(vote_id)
        cur = conn.execute('SELECT Email FROM Users ' +
                           'WHERE AccountTypeID IN (2, 4) AND ID NOT IN ' +
                           '(SELECT UserID FROM TU_Votes ' +
                           'WHERE TU_Votes.VoteID = ?)', [vote_id])
        self._to = [row[0] for row in cur.fetchall()]

    def get_recipients(self):
        return self._to

    def get_subject(self):
        return 'TU Vote Reminder: Proposal %d' % (self._vote_id)

    def get_body(self):
        return 'Please remember to cast your vote on proposal %d [1]. ' \
               'The voting period ends in less than 48 hours.' % \
               (self._vote_id)

    def get_refs(self):
        return (aur_location + '/tu/?id=' + str(self._vote_id),)
462
463


464
def main():
465
466
    action = sys.argv[1]
    action_map = {
467
468
469
470
471
472
473
474
475
476
477
478
479
        'send-resetkey': ResetKeyNotification,
        'welcome': WelcomeNotification,
        'comment': CommentNotification,
        'update': UpdateNotification,
        'flag': FlagNotification,
        'adopt': AdoptNotification,
        'disown': DisownNotification,
        'comaintainer-add': ComaintainerAddNotification,
        'comaintainer-remove': ComaintainerRemoveNotification,
        'delete': DeleteNotification,
        'request-open': RequestOpenNotification,
        'request-close': RequestCloseNotification,
        'tu-vote-reminder': TUVoteReminderNotification,
480
481
    }

Lukas Fleischer's avatar
Lukas Fleischer committed
482
    conn = aurweb.db.Connection()
483

484
485
    notification = action_map[action](conn, *sys.argv[2:])
    notification.send()
486

Lukas Fleischer's avatar
Lukas Fleischer committed
487
488
    conn.commit()
    conn.close()
489
490
491
492


if __name__ == '__main__':
    main()