wcs/wcs/ctl/process_bounce.py

89 lines
2.8 KiB
Python

# w.c.s. - web application for online forms
# Copyright (C) 2005-2010 Entr'ouvert
#
# 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
import sys
import time
import os
import email.Parser
import imp
from Bouncers import BouncerAPI
from qommon.ctl import Command, make_option
COMMA_SPACE = ', '
class CmdProcessBounce(Command):
name = 'process_bounce'
def execute(self, options, args):
from qommon.tokens import Token
from qommon.bounces import Bounce
import publisher
try:
parser = email.Parser.Parser()
bouncers_dir = os.path.join(os.path.dirname(__file__), 'Bouncers')
sys.path.append(bouncers_dir)
msg = parser.parse(sys.stdin)
for modname in BouncerAPI.BOUNCE_PIPELINE:
__import__(modname)
addrs = sys.modules[modname].process(msg)
if addrs is BouncerAPI.Stop:
return # Stop means to ignore message
if addrs:
break
else:
return # didn't find any match
try:
to = msg['To']
local_part, server_part = to.split('@')
token_id = local_part.split('+')[1]
except (IndexError, KeyError):
return
pub = publisher.WcsPublisher.create_publisher()
pub.app_dir = os.path.join(pub.app_dir, server_part)
try:
token = Token.get(token_id)
except KeyError:
return
if token.type != 'email-bounce':
return
token.remove_self()
bounce = Bounce()
bounce.arrival_time = time.time()
bounce.bounce_message = msg.as_string()
bounce.addrs = addrs
bounce.original_message = token.email_message
bounce.original_rcpts = token.email_rcpts
bounce.email_type = token.email_type
bounce.store()
except Exception, e:
import traceback
file('/tmp/bounces-error', 'a+').write(traceback.format_exc())
sys.exit(1)
CmdProcessBounce.register()