aboutsummaryrefslogtreecommitdiffstats
path: root/Mailman/Commands/cmd_set.py
blob: c3eaa9a6412d0f8710fce512d828f7350db3de37 (plain) (blame)
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
# Copyright (C) 2002 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.

from email.Utils import parseaddr, formatdate

from Mailman import mm_cfg
from Mailman import Errors
from Mailman import MemberAdaptor
from Mailman import i18n

def _(s): return s

OVERVIEW = _("""
    set ...
        Set or view your membership options.

        Use `set help' (without the quotes) to get a more detailed list of the
        options you can change.

        Use `set show' (without the quotes) to view your current option
        settings.
""")

DETAILS = _("""
    set help
        Show this detailed help.

    set show [address=<address>]
        View your current option settings.  If you're posting from an address
        other than your membership address, specify your membership address
        with `address=<address>' (no brackets around the email address, and no
        quotes!).

    set authenticate <password> [address=<address>]
        To set any of your options, you must include this command first, along
        with your membership password.  If you're posting from an address
        other than your membership address, specify your membership address
        with `address=<address>' (no brackets around the email address, and no
        quotes!).

    set ack on
    set ack off
        When the `ack' option is turned on, you will receive an
        acknowledgement message whenever you post a message to the list.

    set digest plain
    set digest mime
    set digest off
        When the `digest' option is turned off, you will receive postings
        immediately when they are posted.  Use `set digest plain' if instead
        you want to receive postings bundled into a plain text digest
        (i.e. RFC 1153 digest).  Use `set digest mime' if instead you want to
        receive postings bundled together into a MIME digest.

    set delivery on
    set delivery off
        Turn delivery on or off.  This does not unsubscribe you, but instead
        tells Mailman not to deliver messages to you for now.  This is useful
        if you're going on vacation.  Be sure to use `set delivery on' when
        you return from vacation!

    set myposts on
    set myposts off
        Use `set myposts off' to not receive copies of messages you post to
        the list.  This has no effect if you're receiving digests.

    set hide on
    set hide off
        Use `set hide on' to conceal your email address when people request
        the membership list.

    set duplicates on
    set duplicates off
        Use `set duplicates off' if you want Mailman to not send you messages
        if your address is explicitly mentioned in the To: or Cc: fields of
        the message.  This can reduce the number of duplicate postings you
        will receive.

    set reminders on
    set reminders off
        Use `set reminders off' if you want to disable the monthly password
        reminder for this mailing list.
""")

_ = i18n._

STOP = 1



def gethelp(mlist):
    return _(OVERVIEW)



class SetCommands:
    def __init__(self):
        self.__address = None
        self.__authok = 0

    def process(self, res, args):
        if not args:
            res.results.append(_(DETAILS))
            return STOP
        subcmd = args.pop(0)
        methname = 'set_' + subcmd
        method = getattr(self, methname, None)
        if method is None:
            res.results.append(_('Bad set command: %(subcmd)s'))
            res.results.append(_(DETAILS))
            return STOP
        return method(res, args)

    def set_help(self, res, args=1):
        res.results.append(_(DETAILS))
        if args:
            return STOP

    def _usage(self, res):
        res.results.append(_('Usage:'))
        return self.set_help(res)

    def set_show(self, res, args):
        mlist = res.mlist
        if not args:
            realname, address = parseaddr(res.msg['from'])
        elif len(args) == 1 and args[0].startswith('address='):
            # Send the results to the address, not the From: dude
            address = args[0][8:]
            res.returnaddr = address
        else:
            return self._usage(res)
        if not mlist.isMember(address):
            listname = mlist.real_name
            res.results.append(
                _('You are not a member of the %(listname)s mailing list'))
            return STOP
        res.results.append(_('Your current option settings:'))
        opt = mlist.getMemberOption(address, mm_cfg.AcknowledgePosts)
        onoff = opt and _('on') or _('off')
        res.results.append(_('    ack %(onoff)s'))
        # Digests are a special ternary value
        digestsp = mlist.getMemberOption(address, mm_cfg.Digests)
        if digestsp:
            plainp = mlist.getMemberOption(address, mm_cfg.DisableMime)
            if plainp:
                res.results.append(_('    digest plain'))
            else:
                res.results.append(_('    digest mime'))
        else:
            res.results.append(_('    digest off'))
        # If their membership is disabled, let them know why
        status = mlist.getDeliveryStatus(address)
        how = None
        if status == MemberAdaptor.ENABLED:
            status = _('delivery on')
        elif status == MemberAdaptor.BYUSER:
            status = _('delivery off')
            how = _('by you')
        elif status == MemberAdaptor.BYADMIN:
            status = _('delivery off')
            how = _('by the admin')
        elif status == MemberAdaptor.BYBOUNCE:
            status = _('delivery off')
            how = _('due to bounces')
        else:
            assert status == MemberAdaptor.UNKNOWN
            status = _('delivery off')
            how = _('for unknown reasons')
        changetime = mlist.getDeliveryStatusChangeTime(address)
        if how and changetime > 0:
            date = formatdate(changetime)
            res.results.append(_('    %(status)s (%(how)s on %(date)s)'))
        else:
            res.results.append('    ' + status)
        opt = mlist.getMemberOption(address, mm_cfg.DontReceiveOwnPosts)
        # sense is reversed
        onoff = (not opt) and _('on') or _('off')
        res.results.append(_('    myposts %(onoff)s'))
        opt = mlist.getMemberOption(address, mm_cfg.ConcealSubscription)
        onoff = opt and _('on') or _('off')
        res.results.append(_('    hide %(onoff)s'))
        opt = mlist.getMemberOption(address, mm_cfg.DontReceiveDuplicates)
        # sense is reversed
        onoff = (not opt) and _('on') or _('off')
        res.results.append(_('    duplicates %(onoff)s'))
        opt = mlist.getMemberOption(address, mm_cfg.SuppressPasswordReminder)
        # sense is reversed
        onoff = (not opt) and _('on') or _('off')
        res.results.append(_('    reminders %(onoff)s'))

    def set_authenticate(self, res, args):
        mlist = res.mlist
        if len(args) == 1:
            realname, address = parseaddr(res.msg['from'])
            password = args[0]
        elif len(args) == 2 and args[1].startswith('address='):
            password = args[0]
            address = args[1][8:]
        else:
            return self._usage(res)
        # See if the password matches
        if not mlist.isMember(address):
            listname = mlist.real_name
            res.results.append(
                _('You are not a member of the %(listname)s mailing list'))
            return STOP
        if not mlist.Authenticate((mm_cfg.AuthUser,
                                   mm_cfg.AuthListAdmin),
                                  password, address):
            res.results.append(_('You did not give the correct password'))
            return STOP
        self.__authok = 1
        self.__address = address

    def _status(self, res, arg):
        status = arg.lower()
        if status == 'on':
            flag = 1
        elif status == 'off':
            flag = 0
        else:
            res.results.append(_('Bad argument: %(arg)s'))
            self._usage(res)
            return -1
        # See if we're authenticated
        if not self.__authok:
            res.results.append(_('Not authenticated'))
            self._usage(res)
            return -1
        return flag

    def set_ack(self, res, args):
        mlist = res.mlist
        if len(args) <> 1:
            return self._usage(res)
        status = self._status(res, args[0])
        if status < 0:
            return STOP
        mlist.setMemberOption(self.__address, mm_cfg.AcknowledgePosts, status)
        res.results.append(_('ack option set'))

    def set_digest(self, res, args):
        mlist = res.mlist
        if len(args) <> 1:
            return self._usage(res)
        if not self.__authok:
            res.results.append(_('Not authenticated'))
            self._usage(res)
            return STOP
        arg = args[0].lower()
        if arg == 'off':
            try:
                mlist.setMemberOption(self.__address, mm_cfg.Digests, 0)
            except Errors.AlreadyReceivingRegularDeliveries:
                pass
        elif arg == 'plain':
            try:
                mlist.setMemberOption(self.__address, mm_cfg.Digests, 1)
            except Errors.AlreadyReceivingDigests:
                pass
            mlist.setMemberOption(self.__address, mm_cfg.DisableMime, 1)
        elif arg == 'mime':
            try:
                mlist.setMemberOption(self.__address, mm_cfg.Digests, 1)
            except Errors.AlreadyReceivingDigests:
                pass
            mlist.setMemberOption(self.__address, mm_cfg.DisableMime, 0)
        else:
            res.results.append(_('Bad argument: %(arg)s'))
            self._usage(res)
            return STOP
        res.results.append(_('digest option set'))

    def set_delivery(self, res, args):
        mlist = res.mlist
        if len(args) <> 1:
            return self._usage(res)
        status = self._status(res, args[0])
        if status < 0:
            return STOP
        # sense is reversed
        mlist.setMemberOption(self.__address, mm_cfg.DisableDelivery,
                              not status)
        res.results.append(_('delivery option set'))

    def set_myposts(self, res, args):
        mlist = res.mlist
        if len(args) <> 1:
            return self._usage(res)
        status = self._status(res, args[0])
        if status < 0:
            return STOP
        # sense is reversed
        mlist.setMemberOption(self.__address, mm_cfg.DontReceiveOwnPosts,
                              not status)
        res.results.append(_('myposts option set'))
        
    def set_hide(self, res, args):
        mlist = res.mlist
        if len(args) <> 1:
            return self._usage(res)
        status = self._status(res, args[0])
        if status < 0:
            return STOP
        mlist.setMemberOption(self.__address, mm_cfg.ConcealSubscription,
                              status)
        res.results.append(_('hide option set'))

    def set_duplicates(self, res, args):
        mlist = res.mlist
        if len(args) <> 1:
            return self._usage(res)
        status = self._status(res, args[0])
        if status < 0:
            return STOP
        # sense is reversed
        mlist.setMemberOption(self.__address, mm_cfg.DontReceiveDuplicates,
                              not status)
        res.results.append(_('duplicates option set'))

    def set_reminders(self, res, args):
        mlist = res.mlist
        if len(args) <> 1:
            return self._usage(res)
        status = self._status(res, args[0])
        if status < 0:
            return STOP
        # sense is reversed
        mlist.setMemberOption(self.__address, mm_cfg.SuppressPasswordReminder,
                              not status)
        res.results.append(_('reminder option set'))
        


def process(res, args):
    # We need to keep some state between set commands
    if not getattr(res, 'setstate', None):
        res.setstate = SetCommands()
    res.setstate.process(res, args)