#! /usr/bin/env python # # Copyright (C) 2009 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. """Reprocess messages from heldmsg-* files that don't appear in admindb. Usage: %(PROGRAM)s [options] filename ... Where: --data=path -d path Use path as the directory to search for held message files instead of the default which is Mailman's data/ directory. --all -a Process all 'heldmsg-*' files in the 'data/' or 'path' directory. --listname=list -l list Process all 'heldmsg-list-*' files in the 'data/' or 'path' directory. --noremove -n Do not remove the heldmsg-* files as they are processed. The default is to remove them. --verbose -v Print information about what's done. --help -h Print this help message and exit. Supply one or more filenames or the --all/-a option or the --listname/-l option. These 3 methods are mutually exclusive. Sometimes, for whatever reason, a list's lists/LISTNAME/request.pck file gets lost or corrupted leaving orphaned heldmsg-LISTNAME-* files in Mailman's data/ directory which don't appear in the list's admindb interface. This script will go through some or all of the heldmsg-* files in a directory and requeue the contents as a new message to the list. Presumably, the message will be held again for the original reason, but the new held message will be in the admindb interface. This script must run from Mailman's bin/ directory. """ import os import re import sys import getopt import cPickle import paths from Mailman import mm_cfg from Mailman.i18n import _ from Mailman.Message import Message from Mailman.Queue.Switchboard import Switchboard PROGRAM = sys.argv[0] try: True, False except NameError: True = 1 False = 0 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:], 'hd:al:nv', ['help', 'data=', 'all', 'list=', 'noremove', 'verbose']) except getopt.error, msg: usage(1, msg) all = noremove = verbose = False data = list = None for opt, arg in opts: if opt in ('-h', '--help'): usage(0) elif opt in ('-a', '--all'): all = True message = 'No heldmsg-* files found' elif opt in ('-n', '--noremove'): noremove = True elif opt in ('-d', '--data'): data = arg elif opt in ('-l', '--list'): list = arg message = 'No heldmsg-%s-* files found' % list elif opt in ('-v', '--verbose'): verbose = True fnre = re.compile(r'^heldmsg-(?P.+)-(?P\d+)\.(?P[^.-]+)$') filenames = args if not data: data = mm_cfg.DATA_DIR if filenames and (all or list): usage(1) if all and (filenames or list): usage(1) if list and (filenames or all): usage(1) if not (filenames or all or list): usage(1) if not filenames: for f in os.listdir(data): mo = fnre.match(f) if mo: if all: filenames.append(f) elif mo.group('list') == list: filenames.append(f) if not filenames: usage(2, message) for f in filenames: mo = fnre.match(f) if not mo: print >> sys.stderr, 'Not a heldmsg file: %s' % f continue fp = open(os.path.join(data, f)) plain = True if mo.group('ext') == 'pck': msg = cPickle.load(fp) if isinstance(msg, Message): plain = False else: msg = fp.read() fp.close() if verbose: print 'Requeueing message from file %s' % f inq = Switchboard(mm_cfg.INQUEUE_DIR) inq.enqueue(msg, _plaintext = plain, tolist = True, listname = mo.group('list')) if not noremove: if verbose: print 'Removing file %s' % f os.unlink(os.path.join(data, f)) if __name__ == '__main__': main()