1 # Copyright (C) 2009-2023 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <https://www.gnu.org/licenses/>.
18 """The `mailman inject` subcommand."""
23 from mailman
.app
.inject
import inject_text
24 from mailman
.config
import config
25 from mailman
.core
.i18n
import _
26 from mailman
.interfaces
.command
import ICLISubCommand
27 from mailman
.interfaces
.listmanager
import IListManager
28 from mailman
.utilities
.options
import I18nCommand
29 from public
import public
30 from zope
.component
import getUtility
31 from zope
.interface
import implementer
34 def show_queues(ctx
, param
, value
):
36 print('Available queues:')
37 for switchboard
in sorted(config
.switchboards
):
38 print(' ', switchboard
)
40 # Returning None tells click to process the rest of the command line.
45 help=_("Inject a message from a file into a mailing list's queue."))
49 The name of the queue to inject the message to. QUEUE must be one of the
50 directories inside the queue directory. If omitted, the incoming queue is
54 is_flag
=True, default
=False, is_eager
=True, expose_value
=False,
56 help=_('Show a list of all available queue names and exit.'))
58 '--filename', '-f', 'message_file',
59 default
='-', type=click
.File(encoding
='utf-8'),
61 Name of file containing the message to inject. If not given, or
62 '-' (without the quotes) standard input is used."""))
64 '--metadata', '-m', 'keywords',
65 multiple
=True, metavar
='KEY=VALUE',
67 Additional metadata key/value pairs to add to the message metadata
68 dictionary. Use the format key=value. Multiple -m options are
70 @click.argument('listspec')
72 def inject(ctx
, queue
, message_file
, keywords
, listspec
):
73 mlist
= getUtility(IListManager
).get(listspec
)
75 ctx
.fail(_('No such list: ${listspec}'))
76 queue_name
= ('in' if queue
is None else queue
)
77 switchboard
= config
.switchboards
.get(queue_name
)
78 if switchboard
is None:
79 ctx
.fail(_('No such queue: ${queue}'))
81 message_text
= message_file
.read()
82 except KeyboardInterrupt:
86 for keyvalue
in keywords
:
87 key
, equals
, value
= keyvalue
.partition('=')
89 inject_text(mlist
, message_text
, switchboard
=queue
, **kws
)
93 @implementer(ICLISubCommand
)