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
|
# Copyright (C) 1998-2009 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman 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
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
"""-request robot command queue runner."""
__metaclass__ = type
__all__ = [
'CommandRunner',
'Results',
]
# See the delivery diagram in IncomingRunner.py. This module handles all
# email destined for mylist-request, -join, and -leave. It no longer handles
# bounce messages (i.e. -admin or -bounces), nor does it handle mail to
# -owner.
import re
import logging
from StringIO import StringIO
from email.Errors import HeaderParseError
from email.Header import decode_header, make_header
from email.Iterators import typed_subpart_iterator
from zope.interface import implements
from mailman.config import config
from mailman.core.i18n import _
from mailman.email.message import Message, UserNotification
from mailman.interfaces.command import ContinueProcessing, IEmailResults
from mailman.queue import Runner
NL = '\n'
log = logging.getLogger('mailman.vette')
class CommandFinder:
"""Generate commands from the content of a message."""
def __init__(self, msg, msgdata, results):
self.command_lines = []
self.ignored_lines = []
self.processed_lines = []
# Depending on where the message was destined to, add some implicit
# commands. For example, if this was sent to the -join or -leave
# addresses, it's the same as if 'join' or 'leave' commands were sent
# to the -request address.
subaddress = msgdata.get('subaddress')
if subaddress == 'join':
self.command_lines.append('join')
elif subaddress == 'leave':
self.command_lines.append('leave')
elif subaddress == 'confirm':
mo = re.match(config.mta.verp_confirm_regexp, msg.get('to', ''))
if mo:
self.command_lines.append('confirm ' + mo.group('cookie'))
# Extract the subject header and do RFC 2047 decoding.
raw_subject = msg.get('subject', '')
try:
subject = unicode(make_header(decode_header(raw_subject)))
# Mail commands must be ASCII.
self.command_lines.append(subject.encode('us-ascii'))
except (HeaderParseError, UnicodeError, LookupError):
# The Subject header was unparseable or not ASCII, so just ignore
# it.
pass
# Find the first text/plain part of the message.
part = None
for part in typed_subpart_iterator(msg, 'text', 'plain'):
break
if part is None or part is not msg:
# Either there was no text/plain part or we ignored some
# non-text/plain parts.
print >> results, _('Ignoring non-text/plain MIME parts')
if part is None:
# There was no text/plain part to be found.
return
body = part.get_payload(decode=True)
# text/plain parts better have string payloads.
assert isinstance(body, basestring), 'Non-string decoded payload'
lines = body.splitlines()
# Use no more lines than specified
max_lines = int(config.mailman.email_commands_max_lines)
self.command_lines.extend(lines[:max_lines])
self.ignored_lines.extend(lines[max_lines:])
def __iter__(self):
"""Return each command line, split into commands and arguments.
:return: 2-tuples where the first element is the command and the
second element is a tuple of the arguments.
"""
while self.command_lines:
line = self.command_lines.pop(0)
self.processed_lines.append(line)
parts = line.strip().split()
if len(parts) == 0:
continue
command = parts.pop(0)
yield command, tuple(parts)
class Results:
"""The email command results."""
implements(IEmailResults)
def __init__(self):
self._output = StringIO()
print >> self._output, _("""\
The results of your email command are provided below.
""")
def write(self, text):
self._output.write(text)
def __unicode__(self):
value = self._output.getvalue()
assert isinstance(value, unicode), 'Not a unicode: %r' % value
return value
class CommandRunner(Runner):
"""The email command runner."""
def _dispose(self, mlist, msg, msgdata):
message_id = msg.get('message-id', 'n/a')
# The policy here is similar to the Replybot policy. If a message has
# "Precedence: bulk|junk|list" and no "X-Ack: yes" header, we discard
# the command message.
precedence = msg.get('precedence', '').lower()
ack = msg.get('x-ack', '').lower()
if ack <> 'yes' and precedence in ('bulk', 'junk', 'list'):
log.info('%s Precedence: %s message discarded by: %s',
message_id, precedence, mlist.request_address)
return False
# Do replybot for commands.
replybot = config.handlers['replybot']
replybot.process(mlist, msg, msgdata)
if mlist.autorespond_requests == 1:
# Respond and discard.
log.info('%s -request message replied and discard', message_id)
return False
# Now craft the response and process the command lines.
results = Results()
# Include just a few key pieces of information from the original: the
# sender, date, and message id.
print >> results, _('- Original message details:')
subject = msg.get('subject', 'n/a')
date = msg.get('date', 'n/a')
from_ = msg.get('from', 'n/a')
print >> results, _(' From: $from_')
print >> results, _(' Subject: $subject')
print >> results, _(' Date: $date')
print >> results, _(' Message-ID: $message_id')
print >> results, _('\n- Results:')
finder = CommandFinder(msg, msgdata, results)
for command_name, arguments in finder:
command = config.commands.get(command_name)
if command is None:
print >> results, _('No such command: $command_name')
else:
status = command.process(
mlist, msg, msgdata, arguments, results)
assert status in ContinueProcessing, (
'Invalid status: %s' % status)
if status == ContinueProcessing.no:
break
# All done. Strip blank lines and send the response.
lines = filter(None, (line.strip() for line in finder.command_lines))
if len(lines) > 0:
print >> results, _('\n- Unprocessed:')
for line in lines:
print >> results, line
lines = filter(None, (line.strip() for line in finder.ignored_lines))
if len(lines) > 0:
print >> results, _('\n- Ignored:')
for line in lines:
print >> results, line
print >> results, _('\n- Done.')
# Send a reply, but do not attach the original message. This is a
# compromise because the original message is often helpful in tracking
# down problems, but it's also a vector for backscatter spam.
language = config.languages[msgdata['lang']]
reply = UserNotification(msg.sender, mlist.bounces_address,
_('The results of your email commands'),
lang=language)
# Find a charset for the response body. Try ascii first, then
# latin-1 and finally falling back to utf-8.
reply_body = unicode(results)
for charset in ('us-ascii', 'latin-1'):
try:
reply_body.encode(charset)
break
except UnicodeError:
pass
else:
charset = 'utf-8'
reply.set_payload(reply_body, charset=charset)
reply.send(mlist)
|