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

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

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

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

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

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


29
30
31
32
33
34
35
def headers_cc(cclist):
    return {'Cc': str.join(', ', cclist)}

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

def send_notification(to, subject, body, refs, headers={}):
36
37
    body = '\n'.join([textwrap.fill(line) for line in body.splitlines()])
    body += '\n\n' + refs
38
39
40
41
42
43
44
45

    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

46
47
        for key, value in headers.items():
            msg[key] = value
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

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

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

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

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

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

def get_recipients(cur, pkgbase_id, uid):
    cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
                'INNER JOIN CommentNotify ' +
                'ON CommentNotify.UserID = Users.ID WHERE ' +
                'CommentNotify.UserID != %s AND ' +
                'CommentNotify.PackageBaseID = %s', [uid, pkgbase_id])
    return [row[0] for row in cur.fetchall()]

def get_request_recipients(cur, pkgbase_id, uid):
    cur.execute('SELECT DISTINCT Users.Email FROM Users ' +
                'INNER JOIN PackageBases ' +
                'ON PackageBases.MaintainerUID = Users.ID WHERE ' +
                'Users.ID = %s OR PackageBases.ID = %s', [uid, pkgbase_id])
    return [row[0] for row in cur.fetchall()]

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

    subject = 'AUR Password Reset'
    body = 'A password reset request was submitted for the account %s ' \
94
95
96
           '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)
97
    refs = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
98

99
    send_notification([to], subject, body, refs)
100
101
102
103
104
105
106

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

    subject = 'Welcome to the Arch User Repository'
107
108
109
110
    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.'
111
    body = '[1] ' + aur_location + '/passreset/?resetkey=' + resetkey
112

113
    send_notification([to], subject, body, refs)
114
115
116
117
118
119
120
121
122

def comment(cur, uid, pkgbase_id):
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = get_recipients(cur, pkgbase_id, uid)
    text = sys.stdin.read()

    uri = aur_location + '/pkgbase/' + pkgbase + '/'

123
124
125
    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

126
    subject = 'AUR Comment for %s' % (pkgbase)
127
128
129
130
131
    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')
132
133
    refs = '[1] ' + user_uri + '\n'
    refs += '[2] ' + pkgbase_uri
134
    thread_id = '<pkg-notifications-' + pkgbase + '@aur.archlinux.org>'
135
    headers = headers_reply(thread_id)
136

137
    send_notification(to, subject, body, refs, headers)
138
139
140
141
142

def flag(cur, uid, pkgbase_id):
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = [get_maintainer_email(cur, pkgbase_id)]
143
    text = sys.stdin.read()
144
145
146
147
148

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

    subject = 'AUR Out-of-date Notification for %s' % (pkgbase)
149
    body = 'Your package %s [1] has been flagged out-of-date by %s [2]:' % \
150
           (pkgbase, user)
151
    body += '\n\n' + text + '\n\n'
152
153
    body += '[1] ' + pkgbase_uri + '\n'
    body += '[2] ' + user_uri
154
155
156
157
158
159
160
161
162
163

    send_notification(to, subject, body)

def delete(cur, uid, old_pkgbase_id, new_pkgbase_id=None):
    user = username_from_id(cur, uid)
    old_pkgbase = pkgbase_from_id(cur, old_pkgbase_id)
    if new_pkgbase_id:
        new_pkgbase = pkgbase_from_id(cur, new_pkgbase_id)
    to = get_recipients(cur, old_pkgbase_id, uid)

164
165
166
    user_uri = aur_location + '/account/' + user + '/'
    pkgbase_uri = aur_location + '/pkgbase/' + pkgbase + '/'

167
168
169
    subject = 'AUR Package deleted: %s' % (old_pkgbase)
    if new_pkgbase_id:
        new_pkgbase_uri = aur_location + '/pkgbase/' + new_pkgbase + '/'
170
        body = '%s [1] merged %s [2] into %s [3].\n\n' \
171
172
173
               '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')
174
175
176
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri + '\n'
        refs += '[3] ' + new_pkgbase_uri
177
    else:
178
179
180
        body = '%s [1] deleted %s [2].\n\n' \
               'You will no longer receive notifications about this ' \
               'package.' % (user, old_pkgbase)
181
182
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri
183

184
    send_notification(to, subject, body, refs)
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

def request_open(cur, uid, reqid, reqtype, pkgbase_id, merge_into=None):
    user = username_from_id(cur, uid)
    pkgbase = pkgbase_from_id(cur, pkgbase_id)
    to = [aur_request_ml]
    cc = get_request_recipients(cur, pkgbase_id, uid)
    text = sys.stdin.read()

    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 + '/'
200
201
        body = '%s [1] filed a request to merge %s [2] into %s [3]:' % \
               (user, pkgbase, merge_into)
202
203
204
205
        body += '\n\n' + text
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri + '\n'
        refs += '[3] ' + merge_into_uri
206
    else:
207
208
        body = '%s [1] filed a %s request for %s [2]:' % \
               (user, reqtype, pkgbase)
209
210
211
        body += '\n\n' + text
        refs = '[1] ' + user_uri + '\n'
        refs += '[2] ' + pkgbase_uri + '\n'
212
    thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
213
    headers = headers_reply(thread_id) + headers_cc(cc)
214

215
    send_notification(to, subject, body, refs, headers)
216
217
218
219
220
221
222
223
224
225
226
227
228

def request_close(cur, uid, reqid, reason):
    user = username_from_id(cur, uid)
    pkgbase_id = pkgbase_from_pkgreq(cur, reqid)
    to = [aur_request_ml]
    cc = get_request_recipients(cur, pkgbase_id, uid)
    text = sys.stdin.read()

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

    subject = '[PRQ#%d] Request %s' % (int(reqid), reason.title())
    body = 'Request #%d has been %s by %s [1]' % (int(reqid), reason, user)
    if text.strip() == '':
229
        body += '.'
230
    else:
231
232
        body += ':\n\n' + text
    refs = '[1] ' + user_uri
233
    thread_id = '<pkg-request-' + reqid + '@aur.archlinux.org>'
234
    headers = headers_reply(thread_id) + headers_cc(cc)
235

236
    send_notification(to, subject, body, refs, headers)
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259


if __name__ == '__main__':
    action = sys.argv[1]
    action_map = {
        'send-resetkey': send_resetkey,
        'welcome': welcome,
        'comment': comment,
        'flag': flag,
        'delete': delete,
        'request-open': request_open,
        'request-close': request_close,
    }

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

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

    db.commit()
    db.close()