#! /usr/bin/env python # # Copyright (C) 2003-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. """Process held messages. Usage: held [options] file ... Options: --help / -h Print this help message and exit. --quiet / -q Don't print status messages. --discard / -d Discard referenced messages (default). --accept / -a Accept referenced messages. --reject / -r Reject referenced messages. --comment= / -c Replace the default reason in log messages and reject notices with . --forward=
/ -f
Forward the message to
in addition to other actions. --preserve / -p Preserve the message for the site admin in the spam/ directory in addition to other actions. At most one of --discard, -d, --accept, -a, --reject, -r may be specified. If none are specified, the default is to discard the referenced messages. Each file argument is a held message file, usually in Mailman's data/ directory, with name of the form heldmsg-LISTNAME-NNN.pck (or .txt) containing a message to be processed according to the specified options. This script should be run from Mailman's bin/ directory. """ import os import re import sys import getopt import paths from Mailman import mm_cfg from Mailman.MailList import MailList from Mailman.i18n import _ try: True, False except NameError: True = 1 False = 0 cre = re.compile(r'heldmsg-(?P.*)-(?P[0-9]+)\.(pck|txt)$') def usage(code, msg=''): if code: fd = sys.stderr else: fd = sys.stdout print >> fd, _(__doc__) if msg: print >> fd, msg sys.exit(code) def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'hqdarc:f:p', ['help', 'quiet', 'discard', 'accept', 'reject', 'comment=', 'forward=', 'preserve']) except getopt.error, msg: usage(1, msg) quiet = False discard = accept = reject = comment = forward = fwto = preserve = None for opt, arg in opts: if opt in ('-h', '--help'): usage(0) elif opt in ('-q', '--quiet'): quiet = True elif opt in ('-a', '--accept', '-d', '--discard', '-r', '--reject'): if discard or accept or reject: usage(1, 'At most one of --discard, -d, --accept, -a, --reject, -r may be specified.') elif opt in ('-d', '--discard'): discard = True elif opt in ('-a', '--accept'): accept = True elif opt in ('-r', '--reject'): reject = True elif opt in ('-c', '--comment'): comment = arg elif opt in ('-f', '--forward'): forward = True fwto = arg elif opt in ('-p', '--preserve'): preserve = True if accept: action = mm_cfg.APPROVE acresp = _('Accepted') elif reject: action = mm_cfg.REJECT acresp = _('Rejected') else: action = mm_cfg.DISCARD acresp = _('Discarded') files = args if not files: print _('Nothing to do.') # Mapping from listnames to sequence of request ids actions = {} # Cruise through all the named files, collating by mailing list. We'll # lock the list once, process all holds for that list and move on. for f in files: basename = os.path.basename(f) mo = cre.match(basename) if not mo: print >> sys.stderr, _('Ignoring non-held message: %(f)s') continue listname, id = mo.group('listname', 'id') try: id = int(id) except (ValueError, TypeError): print >> sys.stderr, _('Ignoring held msg w/bad id: %(f)s') continue actions.setdefault(listname, []).append(id) # Now do the actions for listname, ids in actions.items(): mlist = MailList(listname) try: for id in ids: mlist.HandleRequest(id=id, value=action, comment=comment, preserve=preserve, forward=forward, addr=fwto ) if not quiet: print _('%(acresp)s held msg #%(id)s for list %(listname)s') mlist.Save() finally: mlist.Unlock() if __name__ == '__main__': main()