summaryrefslogtreecommitdiff
path: root/modules/mm_mailcmd.py
blob: 0f71566732b238f621493ab93c8bfafe0806d684 (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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# Process mail commands.
# Try to stay close to majordomo commands, but accept common mistakes.
# Not implemented: get / index / which




import string, os, sys
import mm_message, mm_err, mm_cfg, mm_utils




option_descs = { 'digest' :
		     'receive mail from the list bundled together instead of '
		     'one post at a time',
		 'nomail' :
		     'Stop delivering mail.  Useful if you plan on taking a '
		     'short vacation.',
		 'norcv'  :
		     'Turn this on to NOT receive posts you send to the list. '
		     'does not work if digest is set',
		 'ack'    :
		     'Turn this on to receive acknowlegement mail when you '
		     'send mail to the list',
		 'plain'  :
		    'Get plain, not MIME-compliant, '
		    'digests (only if digest is set)',
		 'hide'   :
		    'Conceals your email from the list of subscribers'
	       }
option_info = { 'digest' : 0,
		'nomail' : mm_cfg.DisableDelivery,
		'norcv'  : mm_cfg.DontReceiveOwnPosts,
		'ack'    : mm_cfg.AcknowlegePosts,
		'plain'  : mm_cfg.DisableMime,
		'hide'   : mm_cfg.ConcealSubscription
		}

class MailCommandHandler:
    def __init__(self):
	self._response_buffer = ''
	self._cmd_dispatch = {
	    'subscribe' : self.ProcessSubscribeCmd,
	    'unsubscribe' : self.ProcessUnsubscribeCmd,
	    'who' : self.ProcessWhoCmd,
	    'info' : self.ProcessInfoCmd,
	    'lists' : self.ProcessListsCmd,
	    'help' : self.ProcessHelpCmd,
	    'set' : self.ProcessSetCmd,
	    'options' : self.ProcessOptionsCmd,
	    'password' : self.ProcessPasswordCmd,
	    }

    def AddToResponse(self, text):
	self._response_buffer = self._response_buffer + text + "\n"

    def AddError(self, text):
	self._response_buffer = self._response_buffer + "**** " + text + "\n"
	
    def ParseMailCommands(self):
	mail = mm_message.IncomingMessage()
	subject = mail.getheader("subject")
	if subject:
	    subject = string.strip(subject)
	if (subject and self._cmd_dispatch.has_key(string.split(subject)[0])):
	    lines = [subject] + string.split(mail.body, '\n')
        else:
	    lines = string.split(mail.body, '\n')
	    if subject:
		self.AddError("Subject line ignored: %s" % subject)
	for line in lines:
	    line = string.strip(line)
	    if not line:
		continue
	    self.AddToResponse("\n>>>> %s" % line)
	    line = string.strip(line)
	    if not line:
		continue
	    args = string.split(line)
	    cmd = string.lower(args[0])
	    args = args[1:]
	    if cmd == 'end':
		self.AddError("End of commands.")
		break
	    if not self._cmd_dispatch.has_key(cmd):
		self.AddError("%s: Command UNKNOWN." % cmd)
	    else:
		self._cmd_dispatch[cmd](args, line, mail)
	self.SendMailCmdResponse(mail)

    def SendMailCmdResponse(self, mail):
	self.SendTextToUser(subject = 'Mailman results for %s' % 
			              self.real_name,
			    recipient = mail.GetSender(),
			    sender = self.GetRequestEmail(),
			    text = self._response_buffer)
	self._response_buffer = ''

    def ProcessPasswordCmd(self, args, cmd, mail):
	if len(args) <> 2:
	    self.AddError("Usage: password <oldpw> <newpw>")
	    return
	try:
	    self.ChangeUserPassword(mail.GetSender(),
				    args[0], args[1], args[1])
	    self.AddToResponse('Succeded.')
	except mm_err.MMListNotReady:
	    self.AddError("List is not functional.")
	except mm_err.MMNotAMemberError:
	    self.AddError("%s isn't subscribed to this list." %
			  mail.GetSender())
	except mm_err.MMBadPasswordError:
	    self.AddError("You gave the wrong password.")
	except:
	    self.AddError("An unknown Mailman error occured.")
	    self.AddError("Please forward on your request to %s" %
			  self.GetAdminEmail())
	    self.AddError("%s" % sys.exc_type)

    def ProcessOptionsCmd(self, args, cmd, mail):
	sender = self.FindUser(mail.GetSender())
	if not sender:
	    self.AddError("%s is not a member of the list." % mail.GetSender())
	    return
	options = option_info.keys()
	options.sort()
	value = ''
	for option in options:
	    if self.GetUserOption(sender, option_info[option]):
		value = 'on'
	    else:
		value = 'off'
	    self.AddToResponse("%s: %s" % (option, value))
	self.AddToResponse("")
	self.AddToResponse("To change an option, do: set <option> <on|off> <password>")
	self.AddToResponse("")
	self.AddToResponse("Option explanations:")
	self.AddToResponse("--------------------")
	for option in options:
	    self.AddToResponse("%s:" % option)
	    self.AddToResponse(option_descs[option])
	    self.AddToResponse("")
	    
    def ProcessSetCmd(self, args, cmd, mail):
	def ShowSetUsage(s=self, od = option_descs):
	    options = od.keys()
	    options.sort()
	    s.AddError("Usage: set <option> <on|off> <password>")
	    s.AddError("Valid options are:")
	    for option in options:
		s.AddError("%s:  %s" % (option, od[option]))
	if len(args) <> 3:
	    ShowSetUsage()
	    return
	if args[1] == 'on':
	    value = 1
	elif args[1] == 'off':
	    value = 0
	else:
	    ShowSetUsage()
	    return
	if option_info.has_key(args[0]):
	    try:
		sender = self.FindUser(mail.GetSender())
		if not sender:
		    self.AddError("You aren't subscribed.")
		    return
		self.ConfirmUserPassword(sender, args[2])
		self.SetUserOption(sender, option_info[args[0]], value)
		self.AddToResponse("Succeeded.")
	    except mm_err.MMBadPasswordError:
		self.AddError("You gave the wrong password.")
	    except:
		self.AddError("An unknown Mailman error occured.")
		self.AddError("Please forward on your request to %s" %
			      self.GetAdminEmail())
		self.AddError("%s" % sys.exc_type)
	elif args[0] == 'digest':
	    try:
		self.SetUserDigest(mail.GetSender(), args[2], value)
		self.AddToResponse("Succeeded.")
	    except mm_err.MMAlreadyDigested:
		self.AddError("You are already receiving digests.")
	    except mm_err.MMAlreadyUndigested:
		self.AddError("You already have digests off.")
	    except mm_err.MMBadEmailError:
		self.AddError("Email address '%s' not accepted by Mailman." % 
			      mail.GetSender())
	    except mm_err.MMMustDigestError:
		self.AddError("List only accepts digest members.")
	    except mm_err.MMCantDigestError:
		self.AddError("List doesn't accept digest members.")
	    except mm_err.MMNotAMemberError:
		self.AddError("%s isn't subscribed to this list." % mail.GetSender())
	    except mm_err.MMListNotReady:
		self.AddError("List is not functional.")
	    except mm_err.MMNoSuchUserError:
		self.AddError("%s is not subscribed to this list." %mail.GetSender())
	    except mm_err.MMBadPasswordError:
		self.AddError("You gave the wrong password.")
	    except mm_err.MMNeedApproval:
		self.AddApprovalMsg(cmd)
	    except:
		# TODO: Should log the error we got if we got here.
		self.AddError("An unknown Mailman error occured.")
		self.AddError("Please forward on your request to %s" %
			      self.GetAdminEmail())
		self.AddError("%s" % sys.exc_type)
	else:
	    ShowSetUsage()
	    return
	    
    def ProcessListsCmd(self, args, cmd, mail):
	lists = os.listdir(mm_cfg.LIST_DATA_DIR)
	lists.sort()
	self.AddToResponse("Public mailing lists run by Mailman@%s:"
			   % self.host_name)
	for list in lists:
	    if list == self._internal_name:
		listob = self
	    else:
		try:
		    import maillist
		    listob = maillist.MailList(list)
		except:
		    continue
	    # We can mention this list if you already know about it.
	    if not listob.advertised and listob <> self: 
		continue
	    self.AddToResponse("%s (requests to %s):\n\t%s" % 
			       (listob.real_name, listob.GetRequestEmail(),
				listob.description))
	
    def ProcessInfoCmd(self, args, cmd, mail):
	if self.closed and not self.IsMember(mail.GetSender()):
	    self.AddError("Closed list: only members may see info.")
	    return

	self.AddToResponse("Here is the info for list %s:" % self.real_name)
	if not self.info:
	    self.AddToResponse("No information on list %s found." %
			       self.real_name)
	else:
	    self.AddToResponse(self.info)
	
    def ProcessWhoCmd(self, args, cmd, mail):
	def AddTab(str):
	    return '\t' + str

	if self.closed == 2:
	    self.AddError("Closed list: No one may see subscription list.")
	    return
	if self.closed and not self.IsMember(mail.GetSender()):
	    self.AddError("Closed list: only members may see list subscribers.")
	    return
	if not len(self.digest_members) and not len(self.members):
	    self.AddToResponse("NO MEMBERS.")
	    return
	
	def NotHidden(x, s=self, v=mm_cfg.ConcealSubscription):
	    return not s.GetUserOption(x, v)

	if len(self.members):
	    self.AddToResponse("Non-Digest Members:")
	    self.AddToResponse(string.join(map(AddTab, filter(NotHidden,
						      self.members)), "\n"))

	if len(self.digest_members):
	    self.AddToResponse("")
	    self.AddToResponse("Digest Members:")
	    self.AddToResponse(string.join(map(AddTab, filter(NotHidden,
					     self.digest_members)), "\n"))


    def ProcessUnsubscribeCmd(self, args, cmd, mail):
	if not len(args):
	    self.AddError("Must supply a password.")
	    return
	if len(args) > 2:
		self.AddError("Extra text '%s' not understood.  IGNORED." % 
			      string.join(args[1:]))

	if len(args) == 2:
	    addr = args[1]
	else:
	    addr = mail.GetSender()
	try:
	    self.ConfirmUserPassword(addr, args[0])
	    self.DeleteMember(addr)
	    self.AddToResponse("Succeded.")
	except mm_err.MMListNotReady:
	    self.AddError("List is not functional.")
	except mm_err.MMNoSuchUserError:
	    self.AddError("%s is not subscribed to this list." %mail.GetSender())
	except mm_err.MMBadPasswordError:
	    self.AddError("You gave the wrong password.")
	except:
	    # TODO: Should log the error we got if we got here.
	    self.AddError("An unknown Mailman error occured.")
	    self.AddError("Please forward on your request to %s" %
			  self.GetAdminEmail())
	    self.AddError("%s %s" % (sys.exc_type, sys.exc_value))
	    self.AddError("%s" % sys.exc_traceback)

    def ProcessSubscribeCmd(self, args, cmd, mail):
	digest = self.digest_is_default
	if not len(args):
	    password = "%s%s" % (mm_utils.GetRandomSeed(), 
				 mm_utils.GetRandomSeed())
	elif len(args) == 1:
	    if string.lower(args[0]) == 'digest':
		digest = 1
		password = "%s%s" % (mm_utils.GetRandomSeed(), 
				 mm_utils.GetRandomSeed())
	    elif string.lower(args[0]) == 'nodigest':
		digest = 0
		password = "%s%s" % (mm_utils.GetRandomSeed(), 
				 mm_utils.GetRandomSeed())
	    else:
		password = args[0]

	elif len(args) == 2:
	    if string.lower(args[1]) == 'nodigest':
		digest = 0
		password = args[0]
	    elif string.lower(args[1]) == 'digest':
		digest = 1
		password = args[0]
	    elif string.lower(args[0]) == 'nodigest':
		digest = 0
		password = args[1]
	    elif string.lower(args[0]) == 'digest':
		digest = 1
		password = args[1]
	    else:
		self.AddError("Usage: subscribe [password] [digest|nodigest]")
		return
	elif len(args) > 2:
		self.AddError("Usage: subscribe [password] [digest|nodigest]")
		return

	try:
	    self.AddMember(mail.GetSender(), password, digest)
	    self.AddToResponse("Succeded.")
	except mm_err.MMBadEmailError:
	    self.AddError("Email address '%s' not accepted by Mailman." % 
			  mail.GetSender())
	except mm_err.MMMustDigestError:
	    self.AddError("List only accepts digest members.")
	except mm_err.MMCantDigestError:
	    self.AddError("List doesn't accept digest members.")
	except mm_err.MMListNotReady:
	    self.AddError("List is not functional.")
	except mm_err.MMNeedApproval:
	    self.AddApprovalMsg(cmd)
        except mm_err.MMHostileAddress:
	    self.AddError("Email address '%s' not accepted by Mailman "
			  "(insecure address)" % mail.GetSender())
	except mm_err.MMAlreadyAMember:
	    self.AddError("%s is already a list member." % mail.GetSender())
	except:
	    # TODO: Should log the error we got if we got here.
	    self.AddError("An unknown Mailman error occured.")
	    self.AddError("Please forward on your request to %s" %
			  self.GetAdminEmail())
	    self.AddError("%s" % sys.exc_type)
		
    def AddApprovalMsg(self, cmd):
        self.AddError('''Your request to %s:

        %s

has been forwarded to the person running the list.

This is probably because you are trying to subscribe to a 'closed' list.

You will receive email notification of the list owner's decision about
your subscription request.

Any questions about the list owner's policy should be directed to:

        %s

''' % (self.GetRequestEmail(), cmd, self.GetAdminEmail()))


    def ProcessHelpCmd(self, args, cmd, mail):
	self.AddToResponse("**** Help for %s:" % self.real_name)
	self.AddToResponse("""
This is email command help for version %s of the "Mailman" list manager.
The following describes commands you can send to get information about and
control your subscription to mailman lists at this site.  About the
descriptions - words in "<>"s signify REQUIRED items and words in "[]"
denote OPTIONAL items.  Do not include the "<>"s or "[]"s when you use the
commands.

The following commands are valid:

    subscribe [password] [digest-option]
        Subscribe to the mailing list.  Your password must be given to
        unsubscribe or change your options.  When you subscribe to the
        list, you'll be reminded of your password periodically.
        'digest-option' may be either: 'nodigest' or 'digest' (no quotes!)
	To subscribe this way, you must subscribe from the account in 
	which you wish to receive mail.

    unsubscribe <password> [address]
        Unsubscribe from the mailing list.  Your password must match
	the one you gave when you subscribed.  If you are trying to
	unsubscribe from a different address than the one you subscribed
	from, you may specify it in the 'address' field.

    who
        See who is on this mailing list.

    info
        View the introductory information for this list.

    lists
        See what other mailing lists are run by this Mailman server.

    help
        This message.

    set <option> <on|off> <password> 
	Turn on or off list options.  Valid options are:

	ack:
	Turn this on to receive acknowlegement mail when you send mail to
	the list 

	digest:
	receive mail from the list bundled together instead of one post at
	a time 

	plain:
	Get plain-text, not MIME-compliant, digests (only if digest is set)

	nomail:
	Stop delivering mail.  Useful if you plan on taking a short vacation.

	norcv:
	Turn this on to NOT receive posts you send to the list. 
	Does not work if digest is set

	hide:
	Conceals your address when people look at who is on this list.


    options
	Show the current values of your list options.

    password <oldpassword> <newpassword> 
        Change your list password.
    
    end
       Stop processing commands (good to do if your mailer automatically
       adds a signature file - it'll save you from a lot of cruft).


Commands should be sent to %s

Questions and concerns for the attention of a person should be sent to
%s
""" % (mm_cfg.VERSION, self.GetRequestEmail(), self.GetAdminEmail()))