summaryrefslogtreecommitdiff
path: root/src/mailman/runners/docs/command.rst
blob: 0dbce2ee151a6bea4441e4416da727264a5ccbde (plain)
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
==================
The command runner
==================

This runner's purpose is to process and respond to email commands.  Commands
are extensible using the Mailman plug-in system, but Mailman comes with a
number of email commands out of the box.  These are processed when a message
is sent to the list's ``-request`` address.

    >>> mlist = create_list('test@example.com')
    >>> mlist.send_welcome_messages = False


A command in the Subject
========================

For example, the ``echo`` command simply echoes the original command back to
the sender.  The command can be in the ``Subject`` header.
::

    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ... To: test-request@example.com
    ... Subject: echo hello
    ... Message-ID: <aardvark>
    ...
    ... """)

    >>> from mailman.app.inject import inject_message
    >>> filebase = inject_message(mlist, msg, switchboard='command')
    >>> from mailman.runners.command import CommandRunner
    >>> from mailman.testing.helpers import make_testable_runner
    >>> command = make_testable_runner(CommandRunner)
    >>> command.run()

And now the response is in the ``virgin`` queue.
::

    >>> from mailman.testing.helpers import get_queue_messages
    >>> messages = get_queue_messages('virgin')
    >>> len(messages)
    1

    >>> print(messages[0].msg.as_string())
    Subject: The results of your email commands
    From: test-bounces@example.com
    To: aperson@example.com
    ...
    <BLANKLINE>
    The results of your email command are provided below.
    <BLANKLINE>
    - Original message details:
        From: aperson@example.com
        Subject: echo hello
        Date: ...
        Message-ID: <aardvark>
    <BLANKLINE>
    - Results:
    echo hello
    <BLANKLINE>
    - Done.
    <BLANKLINE>

    >>> dump_msgdata(messages[0].msgdata)
    _parsemsg           : False
    listid              : test.example.com
    nodecorate          : True
    recipients          : {'aperson@example.com'}
    reduced_list_headers: True
    version             : ...


A command in the body
=====================

The command can also be found in the body of the message, as long as the
message is plain text.
::

    >>> msg = message_from_string("""\
    ... From: bperson@example.com
    ... To: test-request@example.com
    ... Message-ID: <bobcat>
    ...
    ... echo foo bar
    ... """)

    >>> filebase = inject_message(mlist, msg, switchboard='command')
    >>> command.run()
    >>> messages = get_queue_messages('virgin')
    >>> len(messages)
    1

    >>> print(messages[0].msg.as_string())
    Subject: The results of your email commands
    From: test-bounces@example.com
    To: bperson@example.com
    ...
    Precedence: bulk
    <BLANKLINE>
    The results of your email command are provided below.
    <BLANKLINE>
    - Original message details:
        From: bperson@example.com
        Subject: n/a
        Date: ...
        Message-ID: <bobcat>
    <BLANKLINE>
    - Results:
    echo foo bar
    <BLANKLINE>
    - Done.
    <BLANKLINE>


Implicit commands
=================

For some commands, specifically for joining and leaving a mailing list, there
are email aliases that act like commands, even when there's nothing else in
the ``Subject`` or body.  For example, to join a mailing list, a user need
only email the ``-join`` address or ``-subscribe`` address (the latter is
deprecated).

Because Dirk has never registered with Mailman before, he gets two responses.
The first is a confirmation message so that Dirk can validate his email
address, and the other is the results of his email command.
::

    >>> msg = message_from_string("""\
    ... From: Dirk Person <dperson@example.com>
    ... To: test-join@example.com
    ...
    ... """)

    >>> filebase = inject_message(
    ...     mlist, msg, switchboard='command', subaddress='join')
    >>> command.run()
    >>> messages = get_queue_messages('virgin', sort_on='subject')
    >>> len(messages)
    2

    >>> from mailman.interfaces.subscriptions import ISubscriptionManager

    >>> manager = ISubscriptionManager(mlist)
    >>> for item in messages:
    ...     subject = item.msg['subject']
    ...     print('Subject:', subject)
    ...     if 'confirm' in str(subject):
    ...         token = str(subject).split()[1].strip()
    ...         new_token, token_owner, member = manager.confirm(token)
    ...         assert new_token is None, 'Confirmation failed'
    Subject: The results of your email commands
    Subject: confirm ...

.. Clear the queue
    >>> ignore = get_queue_messages('virgin')

Similarly, to leave a mailing list, the user need only email the ``-leave`` or
``-unsubscribe`` address (the latter is deprecated).
::

    >>> msg = message_from_string("""\
    ... From: dperson@example.com
    ... To: test-leave@example.com
    ...
    ... """)

    >>> from mailman.interfaces.mailinglist import SubscriptionPolicy
    >>> mlist.unsubscription_policy = SubscriptionPolicy.open
    >>> filebase = inject_message(
    ...     mlist, msg, switchboard='command', subaddress='leave')
    >>> command.run()
    >>> messages = get_queue_messages('virgin')
    >>> len(messages)
    2

    >>> print(messages[0].msg.as_string())
    MIME-Version: 1.0
    ...
    Subject: You have been unsubscribed from the Test mailing list
    From: test-bounces@example.com
    To: dperson@example.com
    ...

    >>> print(messages[1].msg.as_string())
    Subject: The results of your email commands
    From: test-bounces@example.com
    To: dperson@example.com
    ...
    <BLANKLINE>
    - Results:
    Dirk Person <dperson@example.com> left test@example.com
    <BLANKLINE>
    - Done.
    <BLANKLINE>

The ``-confirm`` address is also available as an implicit command.
::

    >>> msg = message_from_string("""\
    ... From: dperson@example.com
    ... To: test-confirm+123@example.com
    ...
    ... """)

    >>> filebase = inject_message(
    ...     mlist, msg, switchboard='command', subaddress='confirm')
    >>> command.run()
    >>> messages = get_queue_messages('virgin')
    >>> len(messages)
    1

    >>> print(messages[0].msg.as_string())
    Subject: The results of your email commands
    From: test-bounces@example.com
    To: dperson@example.com
    ...
    <BLANKLINE>
    The results of your email command are provided below.
    <BLANKLINE>
    - Original message details:
    From: dperson@example.com
    Subject: n/a
    Date: ...
    Message-ID: ...
    <BLANKLINE>
    - Results:
    Confirmation token did not match
    <BLANKLINE>
    - Done.
    <BLANKLINE>


Stopping command processing
===========================

The ``end`` command stops email processing, so that nothing following is
looked at by the command queue.
::

    >>> msg = message_from_string("""\
    ... From: cperson@example.com
    ... To: test-request@example.com
    ... Message-ID: <caribou>
    ...
    ... echo foo bar
    ... end ignored
    ... echo baz qux
    ... """)

    >>> filebase = inject_message(mlist, msg, switchboard='command')
    >>> command.run()
    >>> messages = get_queue_messages('virgin')
    >>> len(messages)
    1

    >>> print(messages[0].msg.as_string())
    Subject: The results of your email commands
    ...
    <BLANKLINE>
    - Results:
    echo foo bar
    <BLANKLINE>
    - Unprocessed:
    echo baz qux
    <BLANKLINE>
    - Done.
    <BLANKLINE>

The ``stop`` command is an alias for ``end``.
::

    >>> msg = message_from_string("""\
    ... From: cperson@example.com
    ... To: test-request@example.com
    ... Message-ID: <caribou>
    ...
    ... echo foo bar
    ... stop ignored
    ... echo baz qux
    ... """)

    >>> filebase = inject_message(mlist, msg, switchboard='command')
    >>> command.run()
    >>> messages = get_queue_messages('virgin')
    >>> len(messages)
    1

    >>> print(messages[0].msg.as_string())
    Subject: The results of your email commands
    ...
    <BLANKLINE>
    - Results:
    echo foo bar
    <BLANKLINE>
    - Unprocessed:
    echo baz qux
    <BLANKLINE>
    - Done.
    <BLANKLINE>