#! /usr/bin/env python # # Copyright (C) 2002 by the Free Software Foundation, Inc. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA # # 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. """ Apply a command to postings held for approval. Commands are: approve - allow the posting to be sent to the list reject - prevent the posting from being sent to the list, return an error to the sender discard - similar to reject, but does not send an error to the sender. You may specify which postings should be processed using a regular expression to match the subject or the from fields of the header. If either matches then the posting will be processed according to the command supplied. %% bin/list_handle_pending [-s |-f |--subject=|--from=] (-a|-r|-d|--approve|--reject|--discard) """ import sys import getopt import string import re # regular expressions module import paths from Mailman.Utils import write from Mailman import mm_cfg from Mailman.MailList import MailList from Mailman import Errors m = None r = None def usage(msg='', code=1): write(__doc__ % globals(), file=sys.stderr) if msg: write(msg, file=sys.stdout) sys.exit(code) def atexit(): """Unlock a locked list, but do not implicitly Save() it. This does not get run if the interpreter exits because of a signal, or if os._exit() is called. It will get called if an exception occurs though. """ global m if not m: return if m.Locked(): write('Unlocking (but not saving) list:', m.internal_name(), file=sys.stderr) m.Unlock() write('Finalizing', file=sys.stderr) del m def main(): global m global r try: (opts, args) = getopt.getopt(sys.argv[2:], 'ards:f:', ['approve', 'reject', 'discard', 'subject=', 'from=']) except getopt.error, m: usage(m) sys.exit(2) run = 'd' subject_re = None subject_re_compiled = None from_re = None from_re_compiled = None listname = None for opt, arg in opts: if opt in ('-a', '--approve'): run = 'a' elif opt in ('-r', '--reject'): run = 'r' elif opt in ('-d', '--discard'): run = 'd' elif opt in ('-s', '--subject'): subject_re = arg elif opt in ('-f', '--from'): from_re = arg if len(args) > 0: usage('Extraneous arguments detected.') sys.exit(2) else: listname = string.lower(sys.argv.pop(1)) # first try to open mailing list write('Loading list:', listname, file=sys.stderr, nl=0) Lockit = 1 try: m = MailList(listname, Lockit) msgids = m.GetHeldMessageIds() except Errors.MMUnknownListError: write('Got Mailman Unknown List Error (does the list exist?). Exitting.', file=sys.stderr) sys.exit(2) write('(locked)', file=sys.stderr) sys.exitfunc = atexit if run == 'a': action_value = mm_cfg.APPROVE action_verb = 'approved' elif run == 'd': action_value = mm_cfg.DISCARD action_verb = 'discarded' else : action_value = mm_cfg.REJECT action_verb = 'rejected' if subject_re is not None: subject_re_compiled = re.compile(subject_re) if from_re is not None: from_re_compiled = re.compile(from_re) print len(msgids), ' messages held for approval.' for msgid in msgids: act = 0 msgrecord = m.GetRecord(msgid) msgsender = None msgfrom = None if len(msgrecord) == 5: ptime, msgfrom, msgsubject, reason, filename = msgrecord else: ptime, msgfrom, msgsubject, reason, filename, msgdata = msgrecord if subject_re_compiled is not None: if subject_re_compiled.match(msgsubject): act = 1 if from_re_compiled is not None: if from_re_compiled.match(msgfrom): act = 1 if act == 1: print 'Pending message ', msgsubject, ' automatically ' + action_verb m.HandleRequest(msgid, action_value, 'Pending message automatically ' + action_verb) msgids = m.GetHeldMessageIds() print len(msgids), 'Number of messages held for approval.' m.Save() #sys.exitfunc = atexit main()