aboutsummaryrefslogtreecommitdiffstats
path: root/bin/discard
blob: 34cb811a203616c6693cf9e67d44387b2ef7c53d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! @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 C_

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, C_(__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 C_('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, C_('Ignoring non-held message: %(f)s')
            continue
        listname, id = mo.group('listname', 'id')
        try:
            id = int(id)
        except (ValueError, TypeError):
            print >> sys.stderr, C_('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 C_('Discarded held msg #%(id)s for list %(listname)s')
            mlist.Save()
        finally:
            mlist.Unlock()



if __name__ == '__main__':
    main()