notify.py 23.7 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_msgid(thread_id):
    return {'Message-ID': thread_id}

20

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

24

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

29

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

35

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

41

42
43
44
45
46
47
48
class Notification:
    def get_refs(self):
        return ()

    def get_headers(self):
        return {}

49
50
51
    def get_cc(self):
        return []

Lukas Fleischer's avatar
Lukas Fleischer committed
52
    def get_body_fmt(self, lang):
53
        body = ''
Lukas Fleischer's avatar
Lukas Fleischer committed
54
        for line in self.get_body(lang).splitlines():
55
56
57
            if line == '-- ':
                body += '-- \n'
                continue
58
59
60
            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
61
        return body.rstrip()
62

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

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

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

87
88
89
90
91
92
93
94
95
96
            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')
97
98
99
100
101
102
                server_port = aurweb.config.getint('notifications',
                                                   'smtp-port')
                use_ssl = aurweb.config.getboolean('notifications',
                                                   'smtp-use-ssl')
                use_starttls = aurweb.config.getboolean('notifications',
                                                        'smtp-use-starttls')
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
                user = aurweb.config.get('notifications', 'smtp-user')
                passwd = aurweb.config.get('notifications', 'smtp-password')

                if use_ssl:
                    server = smtplib.SMTP_SSL(server_addr, server_port)
                else:
                    server = smtplib.SMTP(server_addr, server_port)

                if use_starttls:
                    server.ehlo()
                    server.starttls()
                    server.ehlo()

                if user and passwd:
                    server.login(user, passwd)
118
119

                server.set_debuglevel(0)
120
                deliver_to = [to] + self.get_cc()
121
                server.sendmail(sender, deliver_to, msg.as_bytes())
122
                server.quit()
123
124
125
126


class ResetKeyNotification(Notification):
    def __init__(self, conn, uid):
127
128
        cur = conn.execute('SELECT UserName, Email, BackupEmail, ' +
                           'LangPreference, ResetKey ' +
129
                           'FROM Users WHERE ID = ? AND Suspended = 0', [uid])
130
131
        self._username, self._to, self._backup, self._lang, self._resetkey = \
            cur.fetchone()
Lukas Fleischer's avatar
Lukas Fleischer committed
132
        super().__init__()
133
134

    def get_recipients(self):
135
136
137
138
        if self._backup:
            return [(self._to, self._lang), (self._backup, self._lang)]
        else:
            return [(self._to, self._lang)]
139

Lukas Fleischer's avatar
Lukas Fleischer committed
140
    def get_subject(self, lang):
141
        return aurweb.l10n.translator.translate('AUR Password Reset', lang)
142

Lukas Fleischer's avatar
Lukas Fleischer committed
143
    def get_body(self, lang):
144
145
146
147
148
149
        return aurweb.l10n.translator.translate(
            '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)
150
151
152
153
154
155

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


class WelcomeNotification(ResetKeyNotification):
Lukas Fleischer's avatar
Lukas Fleischer committed
156
    def get_subject(self, lang):
157
158
159
        return aurweb.l10n.translator.translate(
            'Welcome to the Arch User Repository',
            lang)
160

Lukas Fleischer's avatar
Lukas Fleischer committed
161
    def get_body(self, lang):
162
163
164
165
166
        return aurweb.l10n.translator.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)
167
168
169
170
171
172


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
173
174
        cur = conn.execute('SELECT DISTINCT Users.Email, Users.LangPreference '
                           'FROM Users INNER JOIN PackageNotifications ' +
175
176
177
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'Users.CommentNotify = 1 AND ' +
                           'PackageNotifications.UserID != ? AND ' +
178
179
                           'PackageNotifications.PackageBaseID = ? AND ' +
                           'Users.Suspended = 0',
180
                           [uid, pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
181
        self._recipients = cur.fetchall()
182
183
184
        cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?',
                           [comment_id])
        self._text = cur.fetchone()[0]
Lukas Fleischer's avatar
Lukas Fleischer committed
185
        super().__init__()
186
187

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
188
        return self._recipients
189

Lukas Fleischer's avatar
Lukas Fleischer committed
190
    def get_subject(self, lang):
191
192
193
        return aurweb.l10n.translator.translate(
            'AUR Comment for {pkgbase}',
            lang).format(pkgbase=self._pkgbase)
194

Lukas Fleischer's avatar
Lukas Fleischer committed
195
    def get_body(self, lang):
196
197
198
        body = aurweb.l10n.translator.translate(
            '{user} [1] added the following comment to {pkgbase} [2]:',
            lang).format(user=self._user, pkgbase=self._pkgbase)
199
        body += '\n\n' + self._text + '\n\n-- \n'
200
201
202
203
204
205
        dnlabel = aurweb.l10n.translator.translate(
            'Disable notifications', lang)
        body += aurweb.l10n.translator.translate(
            'If you no longer wish to receive notifications about this '
            'package, please go to the package page [2] and select '
            '"{label}".', lang).format(label=dnlabel)
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        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
222
223
        cur = conn.execute('SELECT DISTINCT Users.Email, ' +
                           'Users.LangPreference FROM Users ' +
224
225
226
227
                           'INNER JOIN PackageNotifications ' +
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'Users.UpdateNotify = 1 AND ' +
                           'PackageNotifications.UserID != ? AND ' +
228
229
                           'PackageNotifications.PackageBaseID = ? AND ' +
                           'Users.Suspended = 0',
230
                           [uid, pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
231
232
        self._recipients = cur.fetchall()
        super().__init__()
233
234

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
235
        return self._recipients
236

Lukas Fleischer's avatar
Lukas Fleischer committed
237
    def get_subject(self, lang):
238
239
240
        return aurweb.l10n.translator.translate(
            'AUR Package Update: {pkgbase}',
            lang).format(pkgbase=self._pkgbase)
241

Lukas Fleischer's avatar
Lukas Fleischer committed
242
    def get_body(self, lang):
243
244
245
        body = aurweb.l10n.translator.translate(
            '{user} [1] pushed a new commit to {pkgbase} [2].',
            lang).format(user=self._user, pkgbase=self._pkgbase)
246
        body += '\n\n-- \n'
247
248
249
250
251
252
        dnlabel = aurweb.l10n.translator.translate(
            'Disable notifications', lang)
        body += aurweb.l10n.translator.translate(
            'If you no longer wish to receive notifications about this '
            'package, please go to the package page [2] and select '
            '"{label}".', lang).format(label=dnlabel)
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
        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)
269
270
271
272
273
274
275
276
277
278
        cur = conn.execute(
            'SELECT DISTINCT Users.Email, ' +
            'Users.LangPreference 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 = ? AND ' +
            'Users.Suspended = 0', [pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
279
        self._recipients = cur.fetchall()
280
281
282
        cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' +
                           'ID = ?', [pkgbase_id])
        self._text = cur.fetchone()[0]
Lukas Fleischer's avatar
Lukas Fleischer committed
283
        super().__init__()
284
285

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
286
        return self._recipients
287

Lukas Fleischer's avatar
Lukas Fleischer committed
288
    def get_subject(self, lang):
289
290
291
        return aurweb.l10n.translator.translate(
            'AUR Out-of-date Notification for {pkgbase}',
            lang).format(pkgbase=self._pkgbase)
292

Lukas Fleischer's avatar
Lukas Fleischer committed
293
    def get_body(self, lang):
294
295
296
297
        body = aurweb.l10n.translator.translate(
            'Your package {pkgbase} [1] has been flagged out-of-date by '
            '{user} [2]:', lang).format(pkgbase=self._pkgbase,
                                        user=self._user)
298
299
300
301
302
303
304
305
306
307
308
309
        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
310
311
        cur = conn.execute('SELECT DISTINCT Users.Email, ' +
                           'Users.LangPreference FROM Users ' +
312
313
314
315
                           'INNER JOIN PackageNotifications ' +
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'Users.OwnershipNotify = 1 AND ' +
                           'PackageNotifications.UserID != ? AND ' +
316
317
                           'PackageNotifications.PackageBaseID = ? AND ' +
                           'Users.Suspended = 0',
318
                           [uid, pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
319
        self._recipients = cur.fetchall()
320
321
322
        cur = conn.execute('SELECT FlaggerComment FROM PackageBases WHERE ' +
                           'ID = ?', [pkgbase_id])
        self._text = cur.fetchone()[0]
Lukas Fleischer's avatar
Lukas Fleischer committed
323
        super().__init__()
324
325

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
326
        return self._recipients
327

Lukas Fleischer's avatar
Lukas Fleischer committed
328
    def get_subject(self, lang):
329
330
331
        return aurweb.l10n.translator.translate(
            'AUR Ownership Notification for {pkgbase}',
            lang).format(pkgbase=self._pkgbase)
332
333
334
335
336
337
338

    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
339
    def get_body(self, lang):
340
341
342
        return aurweb.l10n.translator.translate(
            'The package {pkgbase} [1] was adopted by {user} [2].',
            lang).format(pkgbase=self._pkgbase, user=self._user)
343
344
345


class DisownNotification(OwnershipEventNotification):
346
    def get_body(self, lang):
347
348
349
350
        return aurweb.l10n.translator.translate(
            'The package {pkgbase} [1] was disowned by {user} '
            '[2].', lang).format(pkgbase=self._pkgbase,
                                 user=self._user)
351
352
353
354
355


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
356
        cur = conn.execute('SELECT Email, LangPreference FROM Users ' +
357
                           'WHERE ID = ? AND Suspended = 0', [uid])
Lukas Fleischer's avatar
Lukas Fleischer committed
358
359
        self._to, self._lang = cur.fetchone()
        super().__init__()
360
361

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

Lukas Fleischer's avatar
Lukas Fleischer committed
364
    def get_subject(self, lang):
365
366
367
        return aurweb.l10n.translator.translate(
            'AUR Co-Maintainer Notification for {pkgbase}',
            lang).format(pkgbase=self._pkgbase)
368
369
370
371
372
373

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


class ComaintainerAddNotification(ComaintainershipEventNotification):
Lukas Fleischer's avatar
Lukas Fleischer committed
374
    def get_body(self, lang):
375
376
377
        return aurweb.l10n.translator.translate(
            'You were added to the co-maintainer list of {pkgbase} [1].',
            lang).format(pkgbase=self._pkgbase)
378
379
380


class ComaintainerRemoveNotification(ComaintainershipEventNotification):
Lukas Fleischer's avatar
Lukas Fleischer committed
381
    def get_body(self, lang):
382
383
384
        return aurweb.l10n.translator.translate(
            'You were removed from the co-maintainer list of {pkgbase} '
            '[1].', lang).format(pkgbase=self._pkgbase)
385
386
387
388
389
390
391
392
393
394


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
395
396
        cur = conn.execute('SELECT DISTINCT Users.Email, ' +
                           'Users.LangPreference FROM Users ' +
397
398
399
                           'INNER JOIN PackageNotifications ' +
                           'ON PackageNotifications.UserID = Users.ID WHERE ' +
                           'PackageNotifications.UserID != ? AND ' +
400
401
                           'PackageNotifications.PackageBaseID = ? AND ' +
                           'Users.Suspended = 0',
402
                           [uid, old_pkgbase_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
403
404
        self._recipients = cur.fetchall()
        super().__init__()
405
406

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
407
        return self._recipients
408

Lukas Fleischer's avatar
Lukas Fleischer committed
409
    def get_subject(self, lang):
410
411
412
        return aurweb.l10n.translator.translate(
            'AUR Package deleted: {pkgbase}',
            lang).format(pkgbase=self._old_pkgbase)
413

Lukas Fleischer's avatar
Lukas Fleischer committed
414
    def get_body(self, lang):
415
        if self._new_pkgbase:
416
417
418
419
420
421
422
423
424
            dnlabel = aurweb.l10n.translator.translate(
                'Disable notifications', lang)
            return aurweb.l10n.translator.translate(
                '{user} [1] merged {old} [2] into {new} [3].\n\n'
                '-- \n'
                'If you no longer wish receive notifications about the '
                'new package, please go to [3] and click "{label}".',
                lang).format(user=self._user, old=self._old_pkgbase,
                             new=self._new_pkgbase, label=dnlabel)
425
        else:
426
427
428
429
430
            return aurweb.l10n.translator.translate(
                '{user} [1] deleted {pkgbase} [2].\n\n'
                'You will no longer receive notifications about this '
                'package.', lang).format(user=self._user,
                                         pkgbase=self._old_pkgbase)
431
432
433
434
435
436
437
438
439
440
441
442
443

    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)
444
445
446
447
448
449
450
451
452
        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 = ? AND ' +
            'Users.Suspended = 0', [reqid])
453
454
455
456
457
458
459
460
461
462
        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
463
        return [(self._to, 'en')]
464

465
466
467
    def get_cc(self):
        return self._cc

Lukas Fleischer's avatar
Lukas Fleischer committed
468
    def get_subject(self, lang):
469
470
471
        return '[PRQ#%d] %s Request for %s' % \
               (self._reqid, self._reqtype.title(), self._pkgbase)

Lukas Fleischer's avatar
Lukas Fleischer committed
472
    def get_body(self, lang):
473
474
475
476
477
        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:
478
479
480
            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)
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
            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)
        return headers


class RequestCloseNotification(Notification):
    def __init__(self, conn, uid, reqid, reason):
        self._user = username_from_id(conn, uid) if int(uid) else None
502
503
504
505
506
507
508
509
510
        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 = ? AND ' +
            'Users.Suspended = 0', [reqid])
511
512
        self._to = aurweb.config.get('options', 'aur_request_ml')
        self._cc = [row[0] for row in cur.fetchall()]
513
514
515
516
517
518
519
520
        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()
521
522
523
524
        self._reqid = int(reqid)
        self._reason = reason

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

527
528
529
    def get_cc(self):
        return self._cc

Lukas Fleischer's avatar
Lukas Fleischer committed
530
    def get_subject(self, lang):
531
532
533
534
        return '[PRQ#%d] %s Request for %s %s' % (self._reqid,
                                                  self._reqtype.title(),
                                                  self._pkgbase,
                                                  self._reason.title())
535

Lukas Fleischer's avatar
Lukas Fleischer committed
536
    def get_body(self, lang):
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
        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)
        return headers


class TUVoteReminderNotification(Notification):
    def __init__(self, conn, vote_id):
        self._vote_id = int(vote_id)
Lukas Fleischer's avatar
Lukas Fleischer committed
565
        cur = conn.execute('SELECT Email, LangPreference FROM Users ' +
566
567
                           'WHERE AccountTypeID IN (2, 4) AND ID NOT IN ' +
                           '(SELECT UserID FROM TU_Votes ' +
568
569
                           'WHERE TU_Votes.VoteID = ?) AND ' +
                           'Users.Suspended = 0', [vote_id])
Lukas Fleischer's avatar
Lukas Fleischer committed
570
571
        self._recipients = cur.fetchall()
        super().__init__()
572
573

    def get_recipients(self):
Lukas Fleischer's avatar
Lukas Fleischer committed
574
        return self._recipients
575

Lukas Fleischer's avatar
Lukas Fleischer committed
576
    def get_subject(self, lang):
577
578
579
        return aurweb.l10n.translator.translate(
            'TU Vote Reminder: Proposal {id}',
            lang).format(id=self._vote_id)
580

Lukas Fleischer's avatar
Lukas Fleischer committed
581
    def get_body(self, lang):
582
583
584
585
        return aurweb.l10n.translator.translate(
            '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)
586
587
588

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


591
def main():
592
593
    action = sys.argv[1]
    action_map = {
594
595
596
597
598
599
600
601
602
603
604
605
606
        '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,
607
608
    }

Lukas Fleischer's avatar
Lukas Fleischer committed
609
    conn = aurweb.db.Connection()
610

611
612
    notification = action_map[action](conn, *sys.argv[2:])
    notification.send()
613

Lukas Fleischer's avatar
Lukas Fleischer committed
614
615
    conn.commit()
    conn.close()
616
617
618
619


if __name__ == '__main__':
    main()