summaryrefslogtreecommitdiffstats
path: root/wallace/module_optout.py
blob: 5540d80fe02e03ab7ec9380a482418630f3085eb (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
# Copyright 2010-2012 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# 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; version 3 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import json
import os
import random
import tempfile
import time
from urlparse import urlparse
import urllib

from email import message_from_file
from email.utils import formataddr
from email.utils import getaddresses

import modules

import pykolab

from pykolab.translate import _

log = pykolab.getLogger('pykolab.wallace')
conf = pykolab.getConf()

mybasepath = '/var/spool/pykolab/wallace/optout/'

def __init__():
    modules.register('optout', execute, description=description())

def description():
    return """Consult the opt-out service."""

def execute(*args, **kw):
    if not os.path.isdir(mybasepath):
        os.makedirs(mybasepath)

    for stage in ['incoming', 'ACCEPT', 'REJECT', 'HOLD', 'DEFER' ]:
        if not os.path.isdir(os.path.join(mybasepath, stage)):
            os.makedirs(os.path.join(mybasepath, stage))

    # TODO: Test for correct call.
    filepath = args[0]

    if kw.has_key('stage'):
        log.debug(_("Issuing callback after processing to stage %s") % (kw['stage']), level=8)
        log.debug(_("Testing cb_action_%s()") % (kw['stage']), level=8)
        if hasattr(modules, 'cb_action_%s' % (kw['stage'])):
            log.debug(_("Attempting to execute cb_action_%s()") % (kw['stage']), level=8)
            exec('modules.cb_action_%s(%r, %r)' % (kw['stage'],'optout',filepath))
            return

        #modules.next_module('optout')

    log.debug(_("Consulting opt-out service for %r, %r") % (args, kw), level=8)

    message = message_from_file(open(filepath, 'r'))
    envelope_sender = getaddresses(message.get_all('From', []))

    recipients = {
            "To": getaddresses(message.get_all('To', [])),
            "Cc": getaddresses(message.get_all('Cc', []))
            # TODO: Are those all recipient addresses?
        }

    # optout answers are ACCEPT, REJECT, HOLD or DEFER
    answers = [ 'ACCEPT', 'REJECT', 'HOLD', 'DEFER' ]

    # Initialize our results placeholders.
    _recipients = {}

    for answer in answers:
        _recipients[answer] = {
                "To": [],
                "Cc": []
            }

    for recipient_type in recipients.keys():
        for recipient in recipients[recipient_type]:
            log.debug(
                    _("Running opt-out consult from envelope sender '%s " + \
                        "<%s>' to recipient %s <%s>") % (
                            envelope_sender[0][0],
                            envelope_sender[0][1],
                            recipient[0],
                            recipient[1]
                        ),
                    level=8
                )

            optout_answer = request(
                    {
                            'unique-message-id': 'bogus',
                            'envelope_sender': envelope_sender[0][1],
                            'recipient': recipient[1]
                        }
                )

            _recipients[optout_answer][recipient_type].append(recipient)

    #print _recipients

    ##
    ## TODO
    ##
    ## If one of them all is DEFER, DEFER the entire message and discard the
    ## other results.
    ##

    for answer in answers:
        # Create the directory for the answer
        if not os.path.isdir(os.path.join(mybasepath, answer)):
            os.makedirs(os.path.join(mybasepath, answer))

        # Consider using a new mktemp()-like call
        new_filepath = os.path.join(mybasepath, answer, os.path.basename(filepath))

        # Write out a message file representing the new contents for the message
        # use formataddr(recipient)
        _message = message_from_file(open(filepath, 'r'))

        use_this = False

        for recipient_type in _recipients[answer].keys():
            _message.__delitem__(recipient_type)
            if not len(_recipients[answer][recipient_type]) == 0:
                _message.__setitem__(
                        recipient_type,
                        ',\n  '.join(
                                [formataddr(x) for x in _recipients[answer][recipient_type]]
                            )
                    )

                use_this = True

        if use_this:
            # TODO: Do not set items with an empty list.

            (fp, filename) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/optout/%s" % (answer))
            os.write(fp, _message.__str__())
            os.close(fp)

            # Callback with new filename
            if hasattr(modules, 'cb_action_%s' % (answer)):
                log.debug(_("Attempting to execute cb_action_%s(%r, %r)") % (answer, 'optout', filename), level=8)
                exec('modules.cb_action_%s(%r, %r)' % (answer,'optout', filename))

    os.unlink(filepath)

    #print "Moving filepath %s to new_filepath %s" % (filepath, new_filepath)
    #os.rename(filepath, new_filepath)

    #if hasattr(modules, 'cb_action_%s' % (optout_answer)):
        #log.debug(_("Attempting to execute cb_action_%s()") % (optout_answer), level=8)
        #exec('modules.cb_action_%s(%r, %r)' % (optout_answer,'optout', new_filepath))
        #return

def request(params=None):
    params = json.dumps(params)

    optout_url = conf.get('wallace_optout', 'optout_url')

    try:
        f = urllib.urlopen(optout_url, params)
    except Exception, e:
        log.error(_("Could not send request to optout_url %s") % (optout_url))
        return "DEFER"

    response = f.read()

    try:
        response_data = json.loads(response)
    except ValueError, e:
        # Some data is not JSON
        print "Response data is not JSON"

    return response_data['result']