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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
|
The Spec Mail Exploder
======================
There's an address, notifications@specs.launchpad.net, which looks at
all incoming email to see if it looks like a change notifications from a
Moin wiki. If it receive such notification, it will look up the relevant
Launchpad specification, and re-send the mail to the spec's related
people.
First, let's take a look at how a Moin change notification looks like:
>>> from canonical.launchpad.mail.ftests import read_test_message
>>> moin_change = read_test_message('moin-change.txt')
It contains some headers, which are of no use to us, and the body is
base64 encoded.
>>> print str(moin_change) #doctest: -NORMALIZE_WHITESPACE
From webmaster@ubuntu.com Mon Mar 20 10:31:59 2006
Return-path: <webmaster@ubuntu.com>
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
From: Launchpad Wiki <webmaster@ubuntu.com>
To: Launchpad Wiki <webmaster@ubuntu.com>
Date: Mon, 20 Mar 2006 10:26:28 -0000
Message-ID: <20060320102628.17391.34951@palmer.ubuntu.com>
Subject: [Launchpad Wiki] Update of "MediaIntegrityCheck" by ...
Status: RO
Content-Length: 1220
<BLANKLINE>
RGVhciBXaWtpIHVzZXIsDQo...
Let's take a look at the decode body, since we'll have to use it in
order to extract any useful information about the wiki page.
>>> print moin_change.get_payload(decode=True)
Dear Wiki user,
<BLANKLINE>
You have subscribed to a wiki page or wiki category on "Ubuntu Wiki"
for change notification.
<BLANKLINE>
The following page has been changed by JamesHenstridge:
https://wiki.ubuntu.com/MediaIntegrityCheck
<BLANKLINE>
The comment on the change is:
A comment about the change
<BLANKLINE>
-----------------------------------------------------------------...
...
The Mail Handler
----------------
The mail handler that handles mail on notifications@specs.launchpad.net
is BlueprintHandler.
>>> from canonical.config import config
>>> from lp.services.mail.handlers import mail_handlers
>>> handler = mail_handlers.get(config.launchpad.specs_domain)
>>> handler is not None
True
It has a helper method to help associate a URL with a specification. We
use this method instead of simply ISpecificationSet.getByURL() since the
Ubuntu wikis are special, they have more than one domain name mapping to
the same wiki. So if we start with the correct URL, of course we get the
specification with that URL.
>>> spec = handler._getSpecByURL(
... 'https://wiki.ubuntu.com/MediaIntegrityCheck')
>>> spec.specurl
u'https://wiki.ubuntu.com/MediaIntegrityCheck'
But if someone would edit the same wiki page on wiki.edubuntu.org, we
would want the same spec's subscribers to be notified about it.
>>> spec = handler._getSpecByURL(
... 'https://wiki.edubuntu.org/MediaIntegrityCheck')
>>> spec.specurl
u'https://wiki.ubuntu.com/MediaIntegrityCheck'
And the same with wiki.kubuntu.org URLs.
>>> spec = handler._getSpecByURL(
... 'https://wiki.kubuntu.org/MediaIntegrityCheck')
>>> spec.specurl
u'https://wiki.ubuntu.com/MediaIntegrityCheck'
If no matching specification is found, None is returned.
>>> spec = handler._getSpecByURL(
... 'https://wiki.kubuntu.org/NonExistant')
>>> spec is None
True
We use a logger to see what the handler does:
>>> from canonical.launchpad.scripts import log
>>> OLD_LOG_LEVEL = log._log.getEffectiveLevel()
>>> log._log.setLevel(10)
Now, if we pass the email to it, we can see that it finds the correct
spec, and mails it to the related people.
>>> handler.process(
... moin_change, 'notifications@specs.launchpad.net', log=log)
True
>>> log._log.setLevel(OLD_LOG_LEVEL)
>>> from lp.blueprints.interfaces.specification import ISpecificationSet
>>> spec = getUtility(ISpecificationSet).getByURL(
... 'https://wiki.ubuntu.com/MediaIntegrityCheck')
>>> spec.notificationRecipientAddresses()
['test@canonical.com']
Let's look at the email that got sent:
>>> import transaction
>>> from lp.services.mail import stub
>>> transaction.commit()
>>> len(stub.test_emails)
1
The email got sent to all related people:
>>> from_addr, to_addrs, raw_message = stub.test_emails.pop()
>>> to_addrs == spec.notificationRecipientAddresses()
True
We practically bounced the message, we didn't change anything about it,
except for setting the Sender header to our bounce address:
>>> import email
>>> sent_msg = email.message_from_string(raw_message)
>>> sent_body = sent_msg.get_payload(decode=True)
>>> sent_body == moin_change.get_payload(decode=True)
True
>>> sent_msg['To'] == moin_change['To']
True
>>> sent_msg['From'] == moin_change['From']
True
>>> sent_msg['Subject'] == moin_change['Subject']
True
>>> sent_msg['Sender'] == config.canonical.bounce_address
True
If we get a change notification about a spec which doesn't exist in
Launchpad, nothing happens.
>>> log._log.setLevel(10)
>>> moin_change = read_test_message('moin-change-nonexistant.txt')
>>> handler.process(
... moin_change, 'notifications@specs.launchpad.net', log=log)
True
>>> print getUtility(ISpecificationSet).getByURL(
... 'https://wiki.ubuntu.com/NonExistant')
None
In order to prevent loops, we set the X-Loop header:
>>> sent_msg['X-Loop']
'notifications@specs.launchpad.net'
So that if we someone re-sends the notification we sent to us, we'll
simply emit a warning and drop the email.
>>> class FakeFileAlias:
... http_url = 'http://librarian/foo.txt'
>>> del sent_msg['Sender']
>>> sent_msg['Sender'] = 'webmaster@ubuntu.com'
>>> handler.process(
... sent_msg, 'notifications@specs.launchpad.net',
... filealias=FakeFileAlias(), log=log)
WARNING...:Got back a notification we sent: http://librarian/foo.txt
True
>>> log._log.setLevel(OLD_LOG_LEVEL)
No emails were sent:
>>> transaction.commit()
>>> len(stub.test_emails)
0
To prevent bad things happening if someone subscribes this address to
specifications for example, we ignore all email that are sent from
Launchpad:
>>> del sent_msg['X-Loop']
>>> del sent_msg['Sender']
>>> sent_msg['Sender'] = config.canonical.bounce_address
>>> log._log.setLevel(10)
>>> handler.process(
... sent_msg, 'notifications@specs.launchpad.net',
... filealias=FakeFileAlias(), log=log)
WARNING...:We received an email from Launchpad: http://librarian/foo.txt
True
>>> log._log.setLevel(OLD_LOG_LEVEL)
Again, no emails were sent:
>>> transaction.commit()
>>> len(stub.test_emails)
0
Let's pass a notification from wiki.kubuntu.org regarding
MediaIntegrityCheck to see that the correct specification will be found,
and that the notification will be forwarded to the specification's
subscribers:
>>> kubuntu_change = read_test_message('moin-change-kubuntu.txt')
>>> log._log.setLevel(10)
>>> handler.process(
... kubuntu_change, 'notifications@specs.launchpad.net', log=log)
True
>>> log._log.setLevel(OLD_LOG_LEVEL)
>>> transaction.commit()
>>> len(stub.test_emails)
1
>>> from_addr, to_addrs, raw_message = stub.test_emails.pop()
>>> to_addrs == spec.notificationRecipientAddresses()
True
Lastly, let's simulate sending a moin notification, and use handleMail()
instead to ensure that handler above handles the email. This will make
sure that the handler is setup properly to handle unknown users, since
webmaster@ubuntu.com doesn't belong to any Person in Launchpad:
>>> from lp.registry.interfaces.person import IPersonSet
>>> getUtility(IPersonSet).getByEmail('webmaster@ubuntu.com') is None
True
>>> moin_change = read_test_message('moin-change.txt')
>>> moin_change['X-Launchpad-Original-To'] = "notifications@%s" % (
... config.launchpad.specs_domain)
>>> moin_change['Sender'] = 'webmaster@ubuntu.com'
>>> from lp.services.mail.incoming import handleMail
>>> from lp.services.mail.sendmail import sendmail
>>> sendmail(moin_change, bulk=False)
'...'
>>> transaction.commit()
>>> handleMail()
>>> transaction.commit()
>>> len(stub.test_emails)
1
>>> from_addr, to_addrs, raw_message = stub.test_emails.pop()
>>> to_addrs == spec.notificationRecipientAddresses()
True
>>> sent_msg = email.message_from_string(raw_message)
>>> sent_msg['Message-Id'] == moin_change['Message-Id']
True
|