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
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
|
# Copyright (C) 1998,1999,2000 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""Add the message to the list's current digest and possibly send it.
This handler will add the current message to the list's currently accumulating
digest. If the digest has reached its size threshold, it is delivered by
creating an OutgoingMessage of the digest, setting the `isdigest' attribute,
and injecting it into the pipeline.
"""
import os
import string
import re
from Mailman import Utils
from Mailman import Message
from Mailman import mm_cfg
from stat import ST_SIZE
from errno import ENOENT
MIME_SEPARATOR = '__--__--'
MIME_NONSEPARATOR = ' %s ' % MIME_SEPARATOR
EXCLUDE_HEADERS = ('received', 'errors-to')
def process(mlist, msg, msgdata):
# short circuit non-digestable lists, or for messages that are already
# digests
if not mlist.digestable or msgdata.get('isdigest'):
return
digestfile = os.path.join(mlist.fullpath(), 'next-digest')
topicsfile = os.path.join(mlist.fullpath(), 'next-digest-topics')
omask = os.umask(002)
try:
digestfp = open(digestfile, 'a+')
topicsfp = open(topicsfile, 'a+')
finally:
os.umask(omask)
# For the sender, use either the From: field's name comment or the mail
# address. Don't use Sender: field because by now it's been munged into
# the list-admin's address
name, addr = msg.getaddr('from')
sender = quotemime(name or addr)
fromline = quotemime(msg.getheader('from'))
date = quotemime(msg.getheader('date'))
body = quotemime(msg.body)
subject = quotemime(msg.getheader('subject'))
# don't include the redundant subject prefix in the TOC entries
mo = re.match('(re:? *)?(%s)' % re.escape(mlist.subject_prefix),
subject, re.IGNORECASE)
if mo:
subject = subject[:mo.start(2)] + subject[mo.end(2):]
topicsfp.write(' %d. %s (%s)\n' % (mlist.next_post_number,
subject, sender))
# We exclude specified headers and all X-* headers
kept_headers = []
keeping = 0
have_content_type = 0
have_content_description = 0
# speed up the inner loop
lower, split, excludes = string.lower, string.split, EXCLUDE_HEADERS
for h in msg.headers:
if lower(h[:2]) == 'x-' or lower(split(h, ':')[0]) in excludes:
keeping = 0
elif h and h[0] in (' ', '\t'):
if keeping and kept_headers:
# continuation of something we're keeping
kept_headers[-1] = kept_headers[-1] + h
else:
keeping = 1
if lower(h[:7]) == 'content-':
kept_headers.append(h)
if lower(h[:12]) == 'content-type':
have_content_type = 1
elif lower(h[:19]) == 'content-description':
have_content_description = 1
else:
kept_headers.append(quotemime(h))
# after processing the headers
if have_content_type and not have_content_description:
kept_headers.append('Content-Description: %s\n' % subject)
# TBD: reply-to munging happens elsewhere in the pipeline
digestfp.write('--%s\n\nMessage: %d\n%s\n%s' %
(MIME_SEPARATOR, mlist.next_post_number,
string.join(kept_headers, ''),
body))
digestfp.write('\n')
mlist.next_post_number = mlist.next_post_number + 1
topicsfp.close()
digestfp.close()
# if the current digest size exceeds the threshold, send the digest by
# injection into the list's message pipeline
try:
size = os.stat(digestfile)[ST_SIZE]
if size/1024.0 >= mlist.digest_size_threshhold:
inject_digest(mlist, digestfile, topicsfile)
except OSError, e:
code, msg = e
if code == ENOENT:
mlist.LogMsg('error', 'Lost digest file: %s' % digestfile)
mlist.LogMsg('error', str(e))
def inject_digest(mlist, digestfile, topicsfile):
fp = open(topicsfile, 'r+')
topicsdata = fp.read()
fp.close()
topicscount = string.count(topicsdata, '\n')
fp = open(digestfile)
#
# filters for recipient calculation
def delivery_enabled_p(x, s=mlist, v=mm_cfg.DisableDelivery):
return not s.GetUserOption(x, v)
def likes_mime_p(x, s=mlist, v=mm_cfg.DisableMime):
return not s.GetUserOption(x, v)
def hates_mime_p(x, s=mlist, v=mm_cfg.DisableMime):
return s.GetUserOption(x, v)
#
# these people have switched their options from digest delivery to
# non-digest delivery. they need to get one last digest...
try:
final_digesters = mlist.one_last_digest.keys()
mlist.one_last_digest = {}
except AttributeError:
final_digesters = []
#
# calculate various recipient lists
digestmembers = mlist.GetDigestMembers() + final_digesters
recipients = filter(delivery_enabled_p, digestmembers)
mime_recips = filter(likes_mime_p, recipients)
text_recips = filter(hates_mime_p, recipients)
#
# log this digest injection
mlist.LogMsg('digest',
'%s v %d - %d msgs, %d recips (%d mime, %d text, %d disabled)'
% (mlist.real_name, mlist.next_digest_number, topicscount,
len(digestmembers), len(mime_recips), len(text_recips),
len(digestmembers) - len(recipients)))
# do any deliveries
if mime_recips or text_recips:
digest = Digest(mlist, topicsdata, fp.read())
# Generate the MIME digest, but only queue it for delivery so we don't
# hold the lock too long.
msg = digest.asMIME()
msg['To'] = mlist.GetListEmail()
msg.Enqueue(mlist, recips=mime_recips, isdigest=1, approved=1)
# Generate the RFC934 "plain text" digest, and again, just queue it
msg = digest.asText()
msg['To'] = mlist.GetListEmail()
msg.Enqueue(mlist, recips=text_recips, isdigest=1, approved=1)
# zap accumulated digest information for the next round
os.unlink(digestfile)
os.unlink(topicsfile)
mlist.next_digest_number = mlist.next_digest_number + 1
mlist.next_post_number = 1
mlist.LogMsg('digest', 'next %s digest: #%d, post#%d' %
(mlist.internal_name(), mlist.next_digest_number,
mlist.next_post_number))
def quotemime(text):
if not text:
return text
return string.join(string.split(text, MIME_SEPARATOR), MIME_NONSEPARATOR)
class Digest:
"""A digest, representable as either a MIME or plain text message."""
def __init__(self, mlist, toc, body):
self.__mlist = mlist
self.__toc = toc
self.__body = body
self.__volume = 'Vol %d #%d' % (mlist.volume, mlist.next_digest_number)
numtopics = string.count(self.__toc, '\n')
self.__numinfo = '%d msg%s' % (numtopics, numtopics <> 1 and 's' or '')
def ComposeBaseHeaders(self, msg):
"""Populate the message with the presentation-independent headers."""
mlist = self.__mlist
msg['From'] = mlist.GetRequestEmail()
msg['Subject'] = ('%s digest, %s - %s' %
(mlist.real_name, self.__volume, self.__numinfo))
msg['Reply-to'] = mlist.GetListEmail()
msg['X-Mailer'] = "Mailman v%s" % mm_cfg.VERSION
msg['MIME-version'] = '1.0'
def TemplateRefs(self):
"""Resolve references in a format string against list settings.
The resolution is done against a copy of the lists attribute
dictionary, with the addition of some of settings for computed
items - got_listinfo_url, got_request_email, got_list_email, and
got_owner_email.
"""
# Collect the substitutions:
if hasattr(self, 'substitutions'):
return Utils.SafeDict(self.substitutions)
mlist = self.__mlist
substs = Utils.SafeDict()
substs.update(mlist.__dict__)
substs.update(
{'got_listinfo_url' : mlist.GetAbsoluteScriptURL('listinfo'),
'got_request_email': mlist.GetRequestEmail(),
'got_list_email' : mlist.GetListEmail(),
'got_owner_email' : mlist.GetAdminEmail(),
'cgiext' : mm_cfg.CGIEXT,
})
return substs
def asMIME(self):
return self.Present(mime=1)
def asText(self):
return self.Present(mime=0)
def Present(self, mime):
"""Produce a rendering of the digest, as an OutgoingMessage."""
msg = Message.OutgoingMessage()
self.ComposeBaseHeaders(msg)
digestboundary = MIME_SEPARATOR
if mime:
import mimetools
envboundary = mimetools.choose_boundary()
msg['Content-type'] = 'multipart/mixed; boundary=' + envboundary
else:
envboundary = MIME_SEPARATOR
msg['Content-type'] = 'text/plain'
dashbound = "--" + envboundary
# holds lines of the message
lines = []
# Masthead:
if mime:
lines.append(dashbound)
lines.append("Content-type: text/plain; charset=us-ascii")
lines.append("Content-description: Masthead (%s digest, %s)"
% (self.__mlist.real_name, self.__volume))
lines.append('')
masthead = Utils.maketext('masthead.txt', self.TemplateRefs())
lines = lines + string.split(masthead, '\n')
# List-specific header:
if self.__mlist.digest_header:
lines.append('')
if mime:
lines.append(dashbound)
lines.append("Content-type: text/plain; charset=us-ascii")
lines.append("Content-description: Digest Header")
lines.append('')
lines.append(self.__mlist.digest_header % self.TemplateRefs())
# Table of contents:
lines.append('')
if mime:
lines.append(dashbound)
lines.append("Content-type: text/plain; charset=us-ascii")
lines.append("Content-description: Today's Topics (%s)" %
self.__numinfo)
lines.append('')
lines.append("Today's Topics:")
lines.append('')
lines.append(self.__toc)
# Digest text:
if mime:
lines.append(dashbound)
lines.append('Content-type: multipart/digest; boundary="%s"'
% digestboundary)
lines.append('')
lines.append(self.__body)
# End multipart digest text part
lines.append('')
lines.append("--" + digestboundary + "--")
else:
lines.append(
filterDigestHeaders(self.__body,
mm_cfg.DEFAULT_PLAIN_DIGEST_KEEP_HEADERS,
digestboundary))
# List-specific footer:
if self.__mlist.digest_footer:
lines.append(dashbound)
if mime:
lines.append("Content-type: text/plain; charset=us-ascii")
lines.append("Content-description: Digest Footer")
lines.append('')
lines.append(self.__mlist.digest_footer % self.TemplateRefs())
# Close:
if mime:
# Close encompassing mime envelope.
lines.append('')
lines.append(dashbound + "--")
lines.append('')
lines.append("End of %s Digest" % self.__mlist.real_name)
msg.body = string.join(lines, '\n')
return msg
def filterDigestHeaders(body, keep_headers, mimesep):
"""Return copy of body that omits non-crucial headers."""
state = "sep" # "sep", "head", or "body"
lines = string.split(body, "\n")
at = 1
text = [lines[0]]
kept_last = 0
while at < len(lines):
l, at = lines[at], at + 1
if state == "body":
# Snarf the body up to, and including, the next separator:
text.append(l)
if string.strip(l) == '--' + mimesep:
state = "sep"
continue
elif state == "sep":
state = "head"
# Keep the one (blank) line between separator and headers.
text.append(l)
kept_last = 0
continue
elif state == "head":
l = string.strip(l)
if l == '':
state = "body"
text.append(l)
continue
elif l[0] in [' ', '\t']:
# Continuation line - keep if the prior line was kept.
if kept_last:
text.append(l)
continue
else:
where = string.find(l, ':')
if where == -1:
# Malformed header line - interesting, keep it.
text.append(l)
kept_last = 1
else:
field = l[:where]
if string.lower(field) in keep_headers:
text.append(l)
kept_last = 1
else:
kept_last = 0
return string.join(text, '\n')
|