~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/app/inject.py

  • Committer: klm
  • Date: 1998-01-07 21:21:35 UTC
  • Revision ID: vcs-imports@canonical.com-19980107212135-sv0y521ps0xye37r
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2001-2012 by the Free Software Foundation, Inc.
2
 
#
3
 
# This file is part of GNU Mailman.
4
 
#
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)
8
 
# any later version.
9
 
#
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
13
 
# more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License along with
16
 
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
"""Inject a message into a queue."""
19
 
 
20
 
from __future__ import absolute_import, print_function, unicode_literals
21
 
 
22
 
__metaclass__ = type
23
 
__all__ = [
24
 
    'inject_message',
25
 
    'inject_text',
26
 
    ]
27
 
 
28
 
 
29
 
from email import message_from_string
30
 
from email.utils import formatdate, make_msgid
31
 
 
32
 
from mailman.config import config
33
 
from mailman.email.message import Message
34
 
from mailman.utilities.email import add_message_hash
35
 
 
36
 
 
37
 
 
38
 
def inject_message(mlist, msg, recipients=None, switchboard=None, **kws):
39
 
    """Inject a message into a queue.
40
 
 
41
 
    If the message does not have a Message-ID header, one is added.  An
42
 
    X-Message-Id-Hash header is also always added.
43
 
 
44
 
    :param mlist: The mailing list this message is destined for.
45
 
    :type mlist: IMailingList
46
 
    :param msg: The Message object to inject.
47
 
    :type msg: a Message object
48
 
    :param recipients: Optional set of recipients to put into the message's
49
 
        metadata.
50
 
    :type recipients: sequence of strings
51
 
    :param switchboard: Optional name of switchboard to inject this message
52
 
        into.  If not given, the 'in' switchboard is used.
53
 
    :type switchboard: string
54
 
    :param kws: Additional values for the message metadata.
55
 
    :type kws: dictionary
56
 
    """
57
 
    if switchboard is None:
58
 
        switchboard = 'in'
59
 
    # Since we're crafting the message from whole cloth, let's make sure this
60
 
    # message has a Message-ID.
61
 
    if 'message-id' not in msg:
62
 
        msg['Message-ID'] = make_msgid()
63
 
    add_message_hash(msg)
64
 
    # Ditto for Date: as required by RFC 2822.
65
 
    if 'date' not in msg:
66
 
        msg['Date'] = formatdate(localtime=True)
67
 
    msg.original_size = len(msg.as_string())
68
 
    msgdata = dict(
69
 
        listname=mlist.fqdn_listname,
70
 
        original_size=msg.original_size,
71
 
        )
72
 
    msgdata.update(kws)
73
 
    if recipients is not None:
74
 
        msgdata['recipients'] = recipients
75
 
    config.switchboards[switchboard].enqueue(msg, **msgdata)
76
 
 
77
 
 
78
 
 
79
 
def inject_text(mlist, text, recipients=None, switchboard=None, **kws):
80
 
    """Turn text into a message and inject that into a queue.
81
 
 
82
 
    If the text does not have a Message-ID header, one is added.  An
83
 
    X-Message-Id-Hash header is also always added.
84
 
 
85
 
    :param mlist: The mailing list this message is destined for.
86
 
    :type mlist: IMailingList
87
 
    :param text: The text of the message to inject.  This will be parsed into
88
 
        a Message object.
89
 
    :type text: byte string
90
 
    :param recipients: Optional set of recipients to put into the message's
91
 
        metadata.
92
 
    :type recipients: sequence of strings
93
 
    :param switchboard: Optional name of switchboard to inject this message
94
 
        into.  If not given, the 'in' switchboard is used.
95
 
    :type switchboard: string
96
 
    :param kws: Additional values for the message metadata.
97
 
    :type kws: dictionary
98
 
    """
99
 
    message = message_from_string(text, Message)
100
 
    inject_message(mlist, message, recipients, switchboard, **kws)