notify.py 22.6 KB
Newer Older
1
#!/usr/bin/env python3
2
3

import email.mime.text
4
5
import email.utils
import smtplib
6
7
8
9
import subprocess
import sys
import textwrap

Lukas Fleischer's avatar
Lukas Fleischer committed
10
11
import aurweb.config
import aurweb.db
Lukas Fleischer's avatar
Lukas Fleischer committed
12
import aurweb.l10n
13

Lukas Fleischer's avatar
Lukas Fleischer committed
14
aur_location = aurweb.config.get('options', 'aur_location')
15
16


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

20

21
22
23
def headers_msgid(thread_id):
    return {'Message-ID': thread_id}

24

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

28

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

33

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

39

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

45

46
class Notification:
Lukas Fleischer's avatar
Lukas Fleischer committed
47
48
49
    def __init__(self):
        self._l10n = aurweb.l10n.Translator()

50
51
52
53
54
55
    def get_refs(self):
        return ()

    def get_headers(self):
        return {}

Lukas Fleischer's avatar
Lukas Fleischer committed
56
    def get_body_fmt(self, lang):
57
        body = ''
Lukas Fleischer's avatar
Lukas Fleischer committed
58
        for line in self.get_body(lang).splitlines():
59
60
61
            if line == '-- ':
                body += '-- \n'
                continue
62
63
64
            body += textwrap.fill(line, break_long_words=False) + '\n'
        for i, ref in enumerate(self.get_refs()):
            body += '\n' + '[%d] %s' % (i + 1, ref)
Lukas Fleischer's avatar
Lukas Fleischer committed
65
        return body.rstrip()
66

Lukas Fleischer's avatar
Lukas Fleischer committed
67
    def send(self):
68
69
        sender = aurweb.config.get('notifications', 'sender')
        reply_to = aurweb.config.get('notifications', 'reply-to')
70
71
72
        reason = self.__class__.__name__
        if reason.endswith('Notification'):
            reason = reason[:-len('Notification')]
73
74

        for recipient in self.get_recipients():
Lukas Fleischer's avatar
Lukas Fleischer committed
75
76
77
78
            to, lang = recipient
            msg = email.mime.text.MIMEText(self.get_body_fmt(lang),
                                           'plain', 'utf-8')
            msg['Subject'] = self.get_subject(lang)
79
80
            msg['From'] = sender
            msg['Reply-to'] = reply_to
Lukas Fleischer's avatar
Lukas Fleischer committed
81
            msg['To'] = to
82
            msg['X-AUR-Reason'] = reason
83
            msg['Date'] = email.utils.formatdate(localtime=True)
84
85
86
87

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

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
            sendmail = aurweb.config.get('notifications', 'sendmail')
            if sendmail:
                # send email using the sendmail binary specified in the
                # configuration file
                p = subprocess.Popen([sendmail, '-t', '-oi'],
                                     stdin=subprocess.PIPE)
                p.communicate(msg.as_bytes())
            else:
                # send email using smtplib; no local MTA required
                server_addr = aurweb.config.get('notifications', 'smtp-server')

                server = smtplib.SMTP(server_addr)
                server.set_debuglevel(0)
                server.sendmail(sender, recipient, msg.as_bytes())
                server.quit()
103
104
105
106


class ResetKeyNotification(Notification):
    def __init__(self, conn, uid):
107
108
109
110
        cur = conn.execute('SELECT UserName, Email, BackupEmail, ' +
                           'LangPreference, ResetKey ' +
                           'FROM Users WHERE ID = ?', [uid])
        self._username, self._to, self._backup, self._lang, self._resetkey = cur.fetchone()
Lukas Fleischer's avatar
Lukas Fleischer committed
111
        super().__init__()
112
113

    def get_recipients(self):
114
115
116
117
        if self._backup:
            return [(self._to, self._lang), (self._backup, self._lang)]
        else:
            return [(self._to, self._lang)]
118

Lukas Fleischer's avatar
Lukas Fleischer committed
119
120
    def get_subject(self, lang):
        return self._l10n.translate('AUR Password Reset', lang)
121

Lukas Fleischer's avatar
Lukas Fleischer committed
122
123
    def get_body(self, lang):
        return self._l10n.translate(
124
125
126
127
128
                'A password reset request was submitted for the account '
                '{user} 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.',
                lang).format(user=self._username)
129
130
131
132
133
134

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


class WelcomeNotification(ResetKeyNotification):
Lukas Fleischer's avatar
Lukas Fleischer committed
135
136
137
    def get_subject(self, lang):
        return self._l10n.translate('Welcome to the Arch User Repository',
                                    lang)
138

Lukas Fleischer's avatar
Lukas Fleischer committed
139
140
141
142
143
144
    def get_body(self, lang):
        return self._l10n.translate(
                '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.', lang)
145
146
147
148
149
150


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)
Lukas Fleischer's avatar
Lukas Fleischer committed
151
152
        cur = conn.execute('SELECT DISTINCT Users.Email, Users.LangPreference '
                           'FROM Users INNER JOIN PackageNotifications ' +
153
154
155
156
157
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'Users.CommentNotify = 1 AND ' +
                           'PackageNotifications.UserID != ? AND ' +
                           'PackageNotifications.PackageBaseID = ?',
                           [uid, pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
158
        self._recipients = cur.fetchall()
159
160
161
        cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?',
                           [comment_id])
        self._text = cur.fetchone()[0]
Lukas Fleischer's avatar
Lukas Fleischer committed
162
        super().__init__()
163
164

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
165
        return self._recipients
166

Lukas Fleischer's avatar
Lukas Fleischer committed
167
    def get_subject(self, lang):
168
169
        return self._l10n.translate('AUR Comment for {pkgbase}',
                                    lang).format(pkgbase=self._pkgbase)
170

Lukas Fleischer's avatar
Lukas Fleischer committed
171
172
    def get_body(self, lang):
        body = self._l10n.translate(
173
174
                '{user} [1] added the following comment to {pkgbase} [2]:',
                lang).format(user=self._user, pkgbase=self._pkgbase)
175
        body += '\n\n' + self._text + '\n\n-- \n'
Lukas Fleischer's avatar
Lukas Fleischer committed
176
177
178
179
        dnlabel = self._l10n.translate('Disable notifications', lang)
        body += self._l10n.translate(
                'If you no longer wish to receive notifications about this '
                'package, please go to the package page [2] and select '
180
                '"{label}".', lang).format(label=dnlabel)
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
        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)
Lukas Fleischer's avatar
Lukas Fleischer committed
197
198
        cur = conn.execute('SELECT DISTINCT Users.Email, ' +
                           'Users.LangPreference FROM Users ' +
199
200
201
202
203
204
                           'INNER JOIN PackageNotifications ' +
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'Users.UpdateNotify = 1 AND ' +
                           'PackageNotifications.UserID != ? AND ' +
                           'PackageNotifications.PackageBaseID = ?',
                           [uid, pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
205
206
        self._recipients = cur.fetchall()
        super().__init__()
207
208

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
209
        return self._recipients
210

Lukas Fleischer's avatar
Lukas Fleischer committed
211
    def get_subject(self, lang):
212
213
        return self._l10n.translate('AUR Package Update: {pkgbase}',
                                    lang).format(pkgbase=self._pkgbase)
214

Lukas Fleischer's avatar
Lukas Fleischer committed
215
    def get_body(self, lang):
216
217
218
219
        body = self._l10n.translate('{user} [1] pushed a new commit to '
                                    '{pkgbase} [2].', lang).format(
                                            user=self._user,
                                            pkgbase=self._pkgbase)
220
        body += '\n\n-- \n'
Lukas Fleischer's avatar
Lukas Fleischer committed
221
222
223
224
        dnlabel = self._l10n.translate('Disable notifications', lang)
        body += self._l10n.translate(
                'If you no longer wish to receive notifications about this '
                'package, please go to the package page [2] and select '
225
                '"{label}".', lang).format(label=dnlabel)
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        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)
Lukas Fleischer's avatar
Lukas Fleischer committed
242
243
        cur = conn.execute('SELECT DISTINCT Users.Email, ' +
                           'Users.LangPreference FROM Users ' +
244
245
246
247
248
249
                           '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])
Lukas Fleischer's avatar
Lukas Fleischer committed
250
        self._recipients = cur.fetchall()
251
252
253
        cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' +
                           'ID = ?', [pkgbase_id])
        self._text = cur.fetchone()[0]
Lukas Fleischer's avatar
Lukas Fleischer committed
254
        super().__init__()
255
256

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
257
        return self._recipients
258

Lukas Fleischer's avatar
Lukas Fleischer committed
259
    def get_subject(self, lang):
260
261
262
        return self._l10n.translate('AUR Out-of-date Notification for '
                                    '{pkgbase}',
                                    lang).format(pkgbase=self._pkgbase)
263

Lukas Fleischer's avatar
Lukas Fleischer committed
264
265
    def get_body(self, lang):
        body = self._l10n.translate(
266
267
268
                'Your package {pkgbase} [1] has been flagged out-of-date by '
                '{user} [2]:', lang).format(pkgbase=self._pkgbase,
                                            user=self._user)
269
270
271
272
273
274
275
276
277
278
279
280
        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)
Lukas Fleischer's avatar
Lukas Fleischer committed
281
282
        cur = conn.execute('SELECT DISTINCT Users.Email, ' +
                           'Users.LangPreference FROM Users ' +
283
284
285
286
287
288
                           'INNER JOIN PackageNotifications ' +
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'Users.OwnershipNotify = 1 AND ' +
                           'PackageNotifications.UserID != ? AND ' +
                           'PackageNotifications.PackageBaseID = ?',
                           [uid, pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
289
        self._recipients = cur.fetchall()
290
291
292
        cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' +
                           'ID = ?', [pkgbase_id])
        self._text = cur.fetchone()[0]
Lukas Fleischer's avatar
Lukas Fleischer committed
293
        super().__init__()
294
295

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
296
        return self._recipients
297

Lukas Fleischer's avatar
Lukas Fleischer committed
298
    def get_subject(self, lang):
299
300
        return self._l10n.translate('AUR Ownership Notification for {pkgbase}',
                                    lang).format(pkgbase=self._pkgbase)
301
302
303
304
305
306
307

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


class AdoptNotification(OwnershipEventNotification):
Lukas Fleischer's avatar
Lukas Fleischer committed
308
309
    def get_body(self, lang):
        return self._l10n.translate(
310
311
                'The package {pkgbase} [1] was adopted by {user} [2].',
                lang).format(pkgbase=self._pkgbase, user=self._user)
312
313
314


class DisownNotification(OwnershipEventNotification):
315
    def get_body(self, lang):
Lukas Fleischer's avatar
Lukas Fleischer committed
316
        return self._l10n.translate(
317
318
319
                'The package {pkgbase} [1] was disowned by {user} '
                '[2].', lang).format(pkgbase=self._pkgbase,
                                     user=self._user)
320
321
322
323
324


class ComaintainershipEventNotification(Notification):
    def __init__(self, conn, uid, pkgbase_id):
        self._pkgbase = pkgbase_from_id(conn, pkgbase_id)
Lukas Fleischer's avatar
Lukas Fleischer committed
325
326
327
328
        cur = conn.execute('SELECT Email, LangPreference FROM Users ' +
                           'WHERE ID = ?', [uid])
        self._to, self._lang = cur.fetchone()
        super().__init__()
329
330

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
331
        return [(self._to, self._lang)]
332

Lukas Fleischer's avatar
Lukas Fleischer committed
333
    def get_subject(self, lang):
334
335
336
        return self._l10n.translate('AUR Co-Maintainer Notification for '
                                    '{pkgbase}',
                                    lang).format(pkgbase=self._pkgbase)
337
338
339
340
341
342

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


class ComaintainerAddNotification(ComaintainershipEventNotification):
Lukas Fleischer's avatar
Lukas Fleischer committed
343
344
    def get_body(self, lang):
        return self._l10n.translate(
345
346
                'You were added to the co-maintainer list of {pkgbase} [1].',
                lang).format(pkgbase=self._pkgbase)
347
348
349


class ComaintainerRemoveNotification(ComaintainershipEventNotification):
Lukas Fleischer's avatar
Lukas Fleischer committed
350
351
    def get_body(self, lang):
        return self._l10n.translate(
352
353
                'You were removed from the co-maintainer list of {pkgbase} '
                '[1].', lang).format(pkgbase=self._pkgbase)
354
355
356
357
358
359
360
361
362
363


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
Lukas Fleischer's avatar
Lukas Fleischer committed
364
365
        cur = conn.execute('SELECT DISTINCT Users.Email, ' +
                           'Users.LangPreference FROM Users ' +
366
367
368
369
370
                           'INNER JOIN PackageNotifications ' +
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'PackageNotifications.UserID != ? AND ' +
                           'PackageNotifications.PackageBaseID = ?',
                           [uid, old_pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
371
372
        self._recipients = cur.fetchall()
        super().__init__()
373
374

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
375
        return self._recipients
376

Lukas Fleischer's avatar
Lukas Fleischer committed
377
    def get_subject(self, lang):
378
379
        return self._l10n.translate('AUR Package deleted: {pkgbase}',
                                    lang).format(pkgbase=self._old_pkgbase)
380

Lukas Fleischer's avatar
Lukas Fleischer committed
381
    def get_body(self, lang):
382
        if self._new_pkgbase:
Lukas Fleischer's avatar
Lukas Fleischer committed
383
384
            dnlabel = self._l10n.translate('Disable notifications', lang)
            return self._l10n.translate(
385
                    '{user} [1] merged {old} [2] into {new} [3].\n\n'
386
                    '-- \n'
Lukas Fleischer's avatar
Lukas Fleischer committed
387
                    'If you no longer wish receive notifications about the '
388
389
390
                    'new package, please go to [3] and click "{label}".',
                    lang).format(user=self._user, old=self._old_pkgbase,
                                 new=self._new_pkgbase, label=dnlabel)
391
        else:
Lukas Fleischer's avatar
Lukas Fleischer committed
392
            return self._l10n.translate(
393
                    '{user} [1] deleted {pkgbase} [2].\n\n'
Lukas Fleischer's avatar
Lukas Fleischer committed
394
                    'You will no longer receive notifications about this '
395
396
                    'package.', lang).format(user=self._user,
                                             pkgbase=self._old_pkgbase)
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

    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):
Lukas Fleischer's avatar
Lukas Fleischer committed
427
        return [(self._to, 'en')]
428

Lukas Fleischer's avatar
Lukas Fleischer committed
429
    def get_subject(self, lang):
430
431
432
        return '[PRQ#%d] %s Request for %s' % \
               (self._reqid, self._reqtype.title(), self._pkgbase)

Lukas Fleischer's avatar
Lukas Fleischer committed
433
    def get_body(self, lang):
434
435
436
437
438
        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:
439
440
441
            an = 'an' if self._reqtype[0] in 'aeiou' else 'a'
            body = '%s [1] filed %s %s request for %s [2]:' % \
                   (self._user, an, self._reqtype, self._pkgbase)
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
            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()]
473
474
475
476
477
478
479
480
        cur = conn.execute('SELECT PackageRequests.ClosureComment, ' +
                           'RequestTypes.Name, ' +
                           'PackageRequests.PackageBaseName ' +
                           'FROM PackageRequests ' +
                           'INNER JOIN RequestTypes ' +
                           'ON RequestTypes.ID = PackageRequests.ReqTypeID ' +
                           'WHERE PackageRequests.ID = ?', [reqid])
        self._text, self._reqtype, self._pkgbase = cur.fetchone()
481
482
483
484
        self._reqid = int(reqid)
        self._reason = reason

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
485
        return [(self._to, 'en')]
486

Lukas Fleischer's avatar
Lukas Fleischer committed
487
    def get_subject(self, lang):
488
489
490
491
        return '[PRQ#%d] %s Request for %s %s' % (self._reqid,
                                                  self._reqtype.title(),
                                                  self._pkgbase,
                                                  self._reason.title())
492

Lukas Fleischer's avatar
Lukas Fleischer committed
493
    def get_body(self, lang):
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
        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)
Lukas Fleischer's avatar
Lukas Fleischer committed
523
        cur = conn.execute('SELECT Email, LangPreference FROM Users ' +
524
525
526
                           'WHERE AccountTypeID IN (2, 4) AND ID NOT IN ' +
                           '(SELECT UserID FROM TU_Votes ' +
                           'WHERE TU_Votes.VoteID = ?)', [vote_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
527
528
        self._recipients = cur.fetchall()
        super().__init__()
529
530

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
531
        return self._recipients
532

Lukas Fleischer's avatar
Lukas Fleischer committed
533
    def get_subject(self, lang):
534
535
        return self._l10n.translate('TU Vote Reminder: Proposal {id}',
                                    lang).format(id=self._vote_id)
536

Lukas Fleischer's avatar
Lukas Fleischer committed
537
538
    def get_body(self, lang):
        return self._l10n.translate(
539
540
541
                'Please remember to cast your vote on proposal {id} [1]. '
                'The voting period ends in less than 48 hours.',
                lang).format(id=self._vote_id)
542
543
544

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


547
def main():
548
549
    action = sys.argv[1]
    action_map = {
550
551
552
553
554
555
556
557
558
559
560
561
562
        '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,
563
564
    }

Lukas Fleischer's avatar
Lukas Fleischer committed
565
    conn = aurweb.db.Connection()
566

567
568
    notification = action_map[action](conn, *sys.argv[2:])
    notification.send()
569

Lukas Fleischer's avatar
Lukas Fleischer committed
570
571
    conn.commit()
    conn.close()
572
573
574
575


if __name__ == '__main__':
    main()