diff options
Diffstat (limited to '')
| -rw-r--r-- | mailman/bin/add_members.py | 137 |
1 files changed, 63 insertions, 74 deletions
diff --git a/mailman/bin/add_members.py b/mailman/bin/add_members.py index 44ddebc7f..f7f65b953 100644 --- a/mailman/bin/add_members.py +++ b/mailman/bin/add_members.py @@ -19,82 +19,69 @@ from __future__ import with_statement import os import sys -import optparse +import codecs from cStringIO import StringIO from email.utils import parseaddr from mailman import Errors -from mailman import MailList from mailman import Message from mailman import Utils from mailman import Version from mailman import i18n from mailman.app.membership import add_member from mailman.configuration import config -from mailman.initialize import initialize -from mailman.interfaces import DeliveryMode +from mailman.interfaces import AlreadySubscribedError, DeliveryMode +from mailman.options import SingleMailingListOptions _ = i18n._ -def parseargs(): - parser = optparse.OptionParser(version=Version.MAILMAN_VERSION, - usage=_("""\ -%prog [options] listname +class ScriptOptions(SingleMailingListOptions): + usage=_("""\ +%prog [options] Add members to a list. 'listname' is the name of the Mailman list you are adding members to; the list must already exist. You must supply at least one of -r and -d options. At most one of the files can be '-'. -""")) - parser.add_option('-r', '--regular-members-file', - type='string', dest='regular', help=_("""\ +""") + + def add_options(self): + super(ScriptOptions, self).add_options() + self.parser.add_option( + '-r', '--regular-members-file', + type='string', dest='regular', help=_("""\ A file containing addresses of the members to be added, one address per line. This list of people become non-digest members. If file is '-', read addresses from stdin.""")) - parser.add_option('-d', '--digest-members-file', - type='string', dest='digest', help=_("""\ + self.parser.add_option( + '-d', '--digest-members-file', + type='string', dest='digest', help=_("""\ Similar to -r, but these people become digest members.""")) - parser.add_option('-w', '--welcome-msg', - type='string', metavar='<y|n>', help=_("""\ + self.parser.add_option( + '-w', '--welcome-msg', + type='yesno', metavar='<y|n>', help=_("""\ Set whether or not to send the list members a welcome message, overriding whatever the list's 'send_welcome_msg' setting is.""")) - parser.add_option('-a', '--admin-notify', - type='string', metavar='<y|n>', help=_("""\ + self.parser.add_option( + '-a', '--admin-notify', + type='yesno', metavar='<y|n>', help=_("""\ Set whether or not to send the list administrators a notification on the success/failure of these subscriptions, overriding whatever the list's 'admin_notify_mchanges' setting is.""")) - parser.add_option('-C', '--config', - help=_('Alternative configuration file to use')) - opts, args = parser.parse_args() - if not args: - parser.error(_('Missing listname')) - if len(args) > 1: - parser.error(_('Unexpected arguments')) - if opts.welcome_msg is not None: - ch = opts.welcome_msg[0].lower() - if ch == 'y': - opts.welcome_msg = True - elif ch == 'n': - opts.welcome_msg = False - else: - parser.error(_('Illegal value for -w: $opts.welcome_msg')) - if opts.admin_notify is not None: - ch = opts.admin_notify[0].lower() - if ch == 'y': - opts.admin_notify = True - elif ch == 'n': - opts.admin_notify = False - else: - parser.error(_('Illegal value for -a: $opts.admin_notify')) - if opts.regular is None and opts.digest is None: - parser.error(_('At least one of -r or -d is required')) - if opts.regular == '-' and opts.digest == '-': - parser.error(_("-r and -d cannot both be '-'")) - return parser, opts, args + + def sanity_check(self): + if not self.options.listname: + self.parser.error(_('Missing listname')) + if len(self.arguments) > 0: + self.parser.print_error(_('Unexpected arguments')) + if self.options.regular is None and self.options.digest is None: + parser.error(_('At least one of -r or -d is required')) + if self.options.regular == '-' and self.options.digest == '-': + parser.error(_("-r and -d cannot both be '-'")) @@ -102,10 +89,11 @@ def readfile(filename): if filename == '-': fp = sys.stdin else: - fp = open(filename) + # XXX Need to specify other encodings. + fp = codecs.open(filename, encoding='utf-8') # Strip all the lines of whitespace and discard blank lines try: - return [line.strip() for line in fp if line] + return set(line.strip() for line in fp if line) finally: if fp is not sys.stdin: fp.close() @@ -127,70 +115,71 @@ def addall(mlist, subscribers, delivery_mode, ack, admin_notify, outfp): for subscriber in subscribers: try: fullname, address = parseaddr(subscriber) + # Watch out for the empty 8-bit string. + if not fullname: + fullname = u'' password = Utils.MakeRandomPassword() add_member(mlist, address, fullname, password, delivery_mode, config.DEFAULT_SERVER_LANGUAGE, ack, admin_notify) except AlreadySubscribedError: print >> tee, _('Already a member: $subscriber') except Errors.InvalidEmailAddress: - if userdesc.address == '': + if not address: print >> tee, _('Bad/Invalid email address: blank line') else: - print >> tee, _('Bad/Invalid email address: $member') + print >> tee, _('Bad/Invalid email address: $subscriber') else: print >> tee, _('Subscribing: $subscriber') def main(): - parser, opts, args = parseargs() - initialize(opts.config) + options = ScriptOptions() + options.initialize() - listname = args[0].lower().strip() - mlist = config.db.list_manager.get(listname) + fqdn_listname = options.options.listname + mlist = config.db.list_manager.get(fqdn_listname) if mlist is None: - parser.error(_('No such list: $listname')) + parser.error(_('No such list: $fqdn_listname')) # Set up defaults. - if opts.welcome_msg is None: - send_welcome_msg = mlist.send_welcome_msg - else: - send_welcome_msg = opts.welcome_msg - if opts.admin_notify is None: - admin_notify = mlist.admin_notify_mchanges - else: - admin_notify = opts.admin_notify + send_welcome_msg = (options.options.welcome_msg + if options.options.welcome_msg is not None + else mlist.send_welcome_msg) + admin_notify = (options.options.admin_notify + if options.options.admin_notify is not None + else mlist.admin_notify) with i18n.using_language(mlist.preferred_language): - if opts.digest: - dmembers = readfile(opts.digest) + if options.options.digest: + dmembers = readfile(options.options.digest) else: - dmembers = [] - if opts.regular: - nmembers = readfile(opts.regular) + dmembers = set() + if options.options.regular: + nmembers = readfile(options.options.regular) else: - nmembers = [] + nmembers = set() if not dmembers and not nmembers: print _('Nothing to do.') sys.exit(0) - s = StringIO() + outfp = StringIO() if nmembers: addall(mlist, nmembers, DeliveryMode.regular, - send_welcome_msg, admin_notify, s) + send_welcome_msg, admin_notify, outfp) if dmembers: addall(mlist, dmembers, DeliveryMode.mime_digests, - send_welcome_msg, admin_notify, s) + send_welcome_msg, admin_notify, outfp) - config.db.flush() + config.db.commit() if admin_notify: subject = _('$mlist.real_name subscription notification') msg = Message.UserNotification( - mlist.owner, mlist.no_reply_address, subject, s.getvalue(), - mlist.preferred_language) + mlist.owner, mlist.no_reply_address, subject, + outfp.getvalue(), mlist.preferred_language) msg.send(mlist) |
