Merge branch 'alias' into 'master'
[mailman.git] / src / mailman / core / pipelines.py
blob204f567d300e2bd0d0b634bc126ab23fdf453c95
1 # Copyright (C) 2008-2019 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)
8 # any later version.
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.
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/>.
18 """Application support for pipeline processing."""
20 import logging
22 from mailman.app.bounces import bounce_message
23 from mailman.config import config
24 from mailman.interfaces.handler import IHandler
25 from mailman.interfaces.pipeline import (
26 DiscardMessage, IPipeline, RejectMessage)
27 from mailman.utilities.modules import add_components
28 from public import public
31 dlog = logging.getLogger('mailman.debug')
32 vlog = logging.getLogger('mailman.vette')
35 @public
36 def process(mlist, msg, msgdata, pipeline_name='built-in'):
37 """Process the message through the given pipeline.
39 :param mlist: the IMailingList for this message.
40 :param msg: The Message object.
41 :param msgdata: The message metadata dictionary.
42 :param pipeline_name: The name of the pipeline to process through.
43 """
44 message_id = msg.get('message-id', 'n/a')
45 pipeline = config.pipelines[pipeline_name]
46 for handler in pipeline:
47 dlog.debug('{} pipeline {} processing: {}'.format(
48 message_id, pipeline_name, handler.name))
49 try:
50 handler.process(mlist, msg, msgdata)
51 except DiscardMessage as error:
52 vlog.info(
53 '{} discarded by "{}" pipeline handler "{}": {}'.format(
54 message_id, pipeline_name, handler.name, error.message))
55 except RejectMessage as error:
56 vlog.info(
57 '{} rejected by "{}" pipeline handler "{}": {}'.format(
58 message_id, pipeline_name, handler.name, str(error)))
59 bounce_message(mlist, msg, error)
62 @public
63 def initialize():
64 """Initialize the pipelines."""
65 # Find all handlers in the registered plugins.
66 add_components('handlers', IHandler, config.handlers)
67 # Set up some pipelines.
68 add_components('pipelines', IPipeline, config.pipelines)