~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/handlers/avoid_duplicates.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) 2002-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
 
"""If the user wishes it, do not send duplicates of the same message.
19
 
 
20
 
This module keeps an in-memory dictionary of Message-ID: and recipient pairs.
21
 
If a message with an identical Message-ID: is about to be sent to someone who
22
 
has already received a copy, we either drop the message, add a duplicate
23
 
warning header, or pass it through, depending on the user's preferences.
24
 
"""
25
 
 
26
 
from __future__ import absolute_import, print_function, unicode_literals
27
 
 
28
 
__metaclass__ = type
29
 
__all__ = [
30
 
    'AvoidDuplicates',
31
 
    ]
32
 
 
33
 
 
34
 
from email.utils import getaddresses, formataddr
35
 
from zope.interface import implementer
36
 
 
37
 
from mailman.core.i18n import _
38
 
from mailman.interfaces.handler import IHandler
39
 
 
40
 
 
41
 
COMMASPACE = ', '
42
 
 
43
 
 
44
 
 
45
 
@implementer(IHandler)
46
 
class AvoidDuplicates:
47
 
    """If the user wishes it, do not send duplicates of the same message."""
48
 
 
49
 
    name = 'avoid-duplicates'
50
 
    description = _('Suppress some duplicates of the same message.')
51
 
 
52
 
    def process(self, mlist, msg, msgdata):
53
 
        """See `IHandler`."""
54
 
        recips = msgdata.get('recipients')
55
 
        # Short circuit
56
 
        if not recips:
57
 
            return
58
 
        # Seed this set with addresses we don't care about dup avoiding.
59
 
        listaddrs = set((mlist.posting_address,
60
 
                         mlist.bounces_address,
61
 
                         mlist.owner_address,
62
 
                         mlist.request_address))
63
 
        explicit_recips = listaddrs.copy()
64
 
        # Figure out the set of explicit recipients.
65
 
        cc_addresses = {}
66
 
        for header in ('to', 'cc', 'resent-to', 'resent-cc'):
67
 
            addrs = getaddresses(msg.get_all(header, []))
68
 
            header_addresses = dict((addr, formataddr((name, addr)))
69
 
                                    for name, addr in addrs
70
 
                                    if addr)
71
 
            if header == 'cc':
72
 
                # Yes, it's possible that an address is mentioned in multiple
73
 
                # CC headers using different names.  In that case, the last
74
 
                # real name will win, but that doesn't seem like such a big
75
 
                # deal.  Besides, how else would you chose?
76
 
                cc_addresses.update(header_addresses)
77
 
            # Ignore the list addresses for purposes of dup avoidance.
78
 
            explicit_recips |= set(header_addresses)
79
 
        # Now strip out the list addresses.
80
 
        explicit_recips -= listaddrs
81
 
        if not explicit_recips:
82
 
            # No one was explicitly addressed, so we can't do any dup
83
 
            # collapsing
84
 
            return
85
 
        newrecips = set()
86
 
        for r in recips:
87
 
            # If this recipient is explicitly addressed...
88
 
            if r in explicit_recips:
89
 
                send_duplicate = True
90
 
                # If the member wants to receive duplicates, or if the
91
 
                # recipient is not a member at all, they will get a copy.
92
 
                # header.
93
 
                member = mlist.members.get_member(r)
94
 
                if member and not member.receive_list_copy:
95
 
                    send_duplicate = False
96
 
                # We'll send a duplicate unless the user doesn't wish it.  If
97
 
                # personalization is enabled, the add-dupe-header flag will
98
 
                # add a X-Mailman-Duplicate: yes header for this user's
99
 
                # message.
100
 
                if send_duplicate:
101
 
                    msgdata.setdefault('add-dup-header', set()).add(r)
102
 
                    newrecips.add(r)
103
 
                elif r in cc_addresses:
104
 
                    del cc_addresses[r]
105
 
            else:
106
 
                # Otherwise, this is the first time they've been in the recips
107
 
                # list.  Add them to the newrecips list and flag them as
108
 
                # having received this message.
109
 
                newrecips.add(r)
110
 
        # Set the new list of recipients.  XXX recips should always be a set.
111
 
        msgdata['recipients'] = list(newrecips)
112
 
        # RFC 2822 specifies zero or one CC header
113
 
        if cc_addresses:
114
 
            del msg['cc']
115
 
            msg['CC'] = COMMASPACE.join(cc_addresses.values())