aboutsummaryrefslogblamecommitdiffstats
path: root/bin/discard
blob: c30198441f9db01636c15bffda1454db0658ad99 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16














                                                                   
                                                                                





































































































                                                                               
#! @PYTHON@
#
# Copyright (C) 2003 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.

"""Discard held messages.

Usage:
    discard [options] file ...

Options:
    --help / -h
        Print this help message and exit.

    --quiet / -q
        Don't print status messages.
"""

# TODO: add command line arguments for specifying other actions than DISCARD,
# and also for specifying other __handlepost() arguments, i.e. comment,
# preserve, forward, addr

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<listname>.*)-(?P<id>[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:], 'hq', ['help', 'quiet'])
    except getopt.error, msg:
        usage(1, msg)

    quiet = False
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-q', '--quiet'):
            quiet = True

    files = args
    if not files:
        print _('Nothing to do.')

    # Mapping from listnames to sequence of request ids
    discards = {}

    # 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
        discards.setdefault(listname, []).append(id)

    # Now do the discards
    for listname, ids in discards.items():
        mlist = MailList(listname)
        try:
            for id in ids:
                # No comment, no preserve, no forward, no forwarding address
                mlist.HandleRequest(id, mm_cfg.DISCARD, '', False, False, '')
                if not quiet:
                    print _('Discarded held msg #%(id)s for list %(listname)s')
            mlist.Save()
        finally:
            mlist.Unlock()



if __name__ == '__main__':
    main()