summaryrefslogtreecommitdiff
path: root/src/mailman/bin/inject.py
blob: 2bc8a49e3f55a67b8574cb88fa8c56b1c9f4c59e (plain)
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
# Copyright (C) 2002-2009 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman 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
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys

from email import message_from_string

from mailman import Utils
from mailman.Message import Message
from mailman.configuration import config
from mailman.i18n import _
from mailman.inject import inject_text
from mailman.options import SingleMailingListOptions



class ScriptOptions(SingleMailingListOptions):
    usage=_("""\
%prog [options] [filename]

Inject a message from a file into Mailman's incoming queue.  'filename' is the
name of the plaintext message file to inject.  If omitted, or the string '-',
standard input is used.
""")

    def add_options(self):
        super(ScriptOptions, self).add_options()
        self.parser.add_option(
            '-q', '--queue',
            type='string', help=_("""\
The name of the queue to inject the message to.  The queuename must be one of
the directories inside the qfiles directory.  If omitted, the incoming queue
is used."""))

    def sanity_check(self):
        if not self.options.listname:
            self.parser.error(_('Missing listname'))
        if len(self.arguments) == 0:
            self.filename = '-'
        elif len(self.arguments) > 1:
            self.parser.print_error(_('Unexpected arguments'))
        else:
            self.filename = self.arguments[0]



def main():
    options = ScriptOptions()
    options.initialize()

    if options.options.queue is None:
        qdir = config.INQUEUE_DIR
    else:
        qdir = os.path.join(config.QUEUE_DIR, options.options.queue)
        if not os.path.isdir(qdir):
            options.parser.error(_('Bad queue directory: $qdir'))

    fqdn_listname = options.options.listname
    mlist = config.db.list_manager.get(fqdn_listname)
    if mlist is None:
        options.parser.error(_('No such list: $fqdn_listname'))

    if options.filename == '-':
        message_text = sys.stdin.read()
    else:
        with open(options.filename) as fp:
            message_text = fp.read()

    inject_text(mlist, message_text, qdir=qdir)



if __name__ == '__main__':
    main()