summaryrefslogtreecommitdiff
path: root/Mailman/Cgi/handle_opts.py
blob: 48fcca8bae8c9431fd334d858c0428501d90ed33 (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
# 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.

"""Process input to user options form."""

import sys
import os
import string
import cgi

from Mailman import mm_cfg
from Mailman import Utils
from Mailman import MailList
from Mailman import Errors
from Mailman.htmlformat import *
from Mailman.Logging.Syslog import syslog



def PrintResults(mlist, operation, doc, results):
    replacements = mlist.GetStandardReplacements()
    replacements['<mm-results>'] = results
    replacements['<mm-operation>'] = operation
    output = mlist.ParseTags('handle_opts.html', replacements)
    doc.AddItem(output)
    print doc.Format(bgcolor="#ffffff")
    # hrm...
    sys.exit(0)



def main():
    doc = Document()

    path = os.environ.get('PATH_INFO')
    if path:
        parts = Utils.GetPathPieces(path)
    else:
        parts = []

    if len(parts) < 2:
        doc.AddItem(Header(2, "Error"))
        doc.AddItem(Bold("Invalid options to CGI script."))
        print doc.Format(bgcolor="#ffffff")
        return

    listname = string.lower(parts[0])
    user = parts[1]

    if len(parts) < 2:
        doc.AddItem(Header(2, "Error"))
        doc.AddItem(Bold("Invalid options to CGI script."))
        print doc.Format(bgcolor="#ffffff")
        return

    try:
        mlist = MailList.MailList(listname)
    except Errors.MMListError, e:
        doc.AddItem(Header(2, "Error"))
        doc.AddItem(Bold('No such list <em>%s</em>' % listname))
        print doc.Format(bgcolor="#ffffff")
        syslog('error', 'No such list "%s": %s\n' % (listname, e))
        return

    try:
        process_form(mlist, user, doc)
    finally:
        mlist.Save()
        mlist.Unlock()



def process_form(mlist, user, doc):
    form = cgi.FieldStorage()
    error = 0
    operation = ""
    user = Utils.LCDomain(user)

    if not Utils.FindMatchingAddresses(user, mlist.members,
                                       mlist.digest_members):
        PrintResults(mlist, operation, doc, "%s not a member!<p>" % user)

    if form.has_key("unsub"):
        operation = "Unsubscribe"
        if not form.has_key("upw"):
            PrintResults(mlist, operation, doc,
                         "You must give your password to unsubscribe.<p>")
        else:
            try:
                pw = form["upw"].value
                if mlist.ConfirmUserPassword(user, pw):
                    mlist.DeleteMember(user, "web cmd")
            except Errors.MMListNotReadyError:
                PrintResults(mlist, operation, doc, "List is not functional.")
            except Errors.MMNoSuchUserError:
                PrintResults(mlist, operation, doc,
                             "You seem to already be not a member.<p>")
            except Errors.MMBadUserError:
                PrintResults(mlist, operation, doc,
                             "Your account has gone awry - "
                             "please contact the list administrator!<p>")
            except Errors.MMBadPasswordError:
                PrintResults(mlist, operation, doc,
                             "That password was incorrect.<p>")
        PrintResults(mlist, operation, doc, "You have been unsubscribed.<p>")

    elif form.has_key("emailpw"):
        try:
            mlist.MailUserPassword(user)
            PrintResults(mlist, operation, doc,
                         "A reminder of your password "
                         "has been emailed to you.<p>")
        except Errors.MMBadUserError:
            PrintResults(mlist, operation, doc,
                         "The password entry for `%s' has not "
                         'been found.  The list administrator is being '
                         'notified.<p>' % user)

    elif form.has_key("othersubs"):
        if not form.has_key('othersubspw'):
            PrintResults(mlist, operation, doc,
                         "You must specify your password.")
        else:
            try:
                mlist.ConfirmUserPassword(user, form['othersubspw'].value)
            except Errors.MMListNotReadyError:
                PrintResults(mlist, operation, doc,
                             "The list is currently not functional.")
            except Errors.MMNotAMemberError:
                PrintResults(mlist, operation, doc,
                             "You seem to no longer be a list member.")
            except Errors.MMBadPasswordError:
                PrintResults(mlist, operation, doc, "Incorrect password.")
            except Errors.MMBadUserError:
                PrintResults(
                    mlist, operation, doc,
                    "You have no password.  Contact the list administrator.")

            doc.AddItem(Header(2, "List Subscriptions for %s on %s"
                               % (user, mlist.host_name)))
            doc.AddItem("Click a link to visit your options page for"
                        " that mailing list:")

            def optionslinks(listname, user=user):
                mlist = MailList.MailList(listname, lock=0)
                addrs = Utils.FindMatchingAddresses(user, mlist.members,
                                                    mlist.digest_members)
                if addrs:
                    addr = Utils.ObscureEmail(addrs[0])
                    if mlist.obscure_addresses:
                        addr = Utils.ObscureEmail(addr)
                    url = mlist.GetOptionsURL(addr)
                    link = Link(url, mlist.real_name)
                    return mlist.internal_name(), link

            all_links = filter(None, map(optionslinks, Utils.list_names()))
            all_links.sort()
            items = OrderedList()
            for name, link in all_links:
                items.AddItem(link)
            doc.AddItem(items)
            print doc.Format(bgcolor="#ffffff")

    elif form.has_key("changepw"):
        if form.has_key('opw') and \
                form.has_key('newpw') and \
                form.has_key('confpw'):
            # then
            try:
                mlist.ConfirmUserPassword(user, form['opw'].value)
                mlist.ChangeUserPassword(user, form['newpw'].value,
                                         form['confpw'].value)
            except Errors.MMListNotReadyError:
                PrintResults(mlist, operation, doc,
                             "The list is currently not functional.")
            except Errors.MMNotAMemberError:
                PrintResults(mlist, operation, doc,
                             "You seem to no longer be a list member.")
            except Errors.MMBadPasswordError:
                PrintResults(mlist, operation, doc,
                             "The old password you supplied was incorrect.")
            except Errors.MMPasswordsMustMatch:
                PrintResults(mlist, operation, doc, "Passwords must match.")

            PrintResults(mlist, operation, doc,
                         "Your password has been changed.")
        else:
            PrintResults(mlist, operation, doc,
                         "You must specify your old password,"
                         " and your new password twice.")

    else:
        # if key doesn't exist, or its value can't be int()'ified, return the
        # current value (essentially a noop)
        def getval(key, default, form=form):
            if form.has_key(key):
                try:
                    return int(form[key].value)
                except ValueError:
                    return default
            return default

        useropt = mlist.GetUserOption
        digest_value = getval('digest', useropt(user, mm_cfg.Digests))
        mime = getval('mime', useropt(user, mm_cfg.DisableMime))
        dont_receive = getval('dontreceive',
                              useropt(user, mm_cfg.DontReceiveOwnPosts))
        ack_posts = getval('ackposts', useropt(user, mm_cfg.AcknowledgePosts))
        disable_mail = getval('disablemail',
                              useropt(user, mm_cfg.DisableDelivery))
        conceal = getval('conceal', useropt(user, mm_cfg.ConcealSubscription))

        if not form.has_key("digpw"):
            PrintResults(mlist, operation, doc,
                         "You must supply a password to change options.")
        try:
            mlist.ConfirmUserPassword(user, form['digpw'].value)
        except Errors.MMAlreadyDigested:
            pass
        except Errors.MMAlreadyUndigested:
            pass
        except Errors.MMMustDigestError:
            PrintResults(mlist, operation, doc,
                         "List only accepts digest members.")
        except Errors.MMCantDigestError:
            PrintResults(mlist, operation, doc,
                         "List doesn't accept digest members.")
        except Errors.MMNotAMemberError:
            PrintResults(mlist, operation, doc,
                         "%s isn't subscribed to this list."
                         % mail.GetSender())
        except Errors.MMListNotReadyError:
            PrintResults(mlist, operation, doc, "List is not functional.")
        except Errors.MMNoSuchUserError:
            PrintResults(mlist, operation, doc,
                         "%s is not subscribed to this list."
                         % mail.GetSender())
        except Errors.MMBadPasswordError:
            PrintResults(mlist, operation, doc, "You gave the wrong password.")

        mlist.SetUserOption(user, mm_cfg.DisableDelivery, disable_mail)
        mlist.SetUserOption(user, mm_cfg.DontReceiveOwnPosts, dont_receive)
        mlist.SetUserOption(user, mm_cfg.AcknowledgePosts, ack_posts)
        mlist.SetUserOption(user, mm_cfg.DisableMime, mime)
        msg = 'You have successfully set your options.'
        try:
            mlist.SetUserDigest(user, digest_value)
            # digest mode changed from on to off, so send the current digest
            # to the user.
            if digest_value == 0:
                PrintResults(mlist, operation, doc,
                             'You may get one last digest.')
        except (Errors.MMAlreadyDigested, Errors.MMAlreadyUndigested):
            pass
        except Errors.MMCantDigestError:
            msg = 'The list administrator has disabled digest delivery for ' \
                  'this list, however your other options have been ' \
                  'successfully set.'
        mlist.SetUserOption(user, mm_cfg.ConcealSubscription, conceal)
        PrintResults(mlist, operation, doc, msg)