1
# Copyright (C) 2002-2012 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)
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
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
"""If the user wishes it, do not send duplicates of the same message.
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.
26
from __future__ import absolute_import, print_function, unicode_literals
34
from email.utils import getaddresses, formataddr
35
from zope.interface import implementer
37
from mailman.core.i18n import _
38
from mailman.interfaces.handler import IHandler
45
@implementer(IHandler)
46
class AvoidDuplicates:
47
"""If the user wishes it, do not send duplicates of the same message."""
49
name = 'avoid-duplicates'
50
description = _('Suppress some duplicates of the same message.')
52
def process(self, mlist, msg, msgdata):
54
recips = msgdata.get('recipients')
58
# Seed this set with addresses we don't care about dup avoiding.
59
listaddrs = set((mlist.posting_address,
60
mlist.bounces_address,
62
mlist.request_address))
63
explicit_recips = listaddrs.copy()
64
# Figure out the set of explicit recipients.
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
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
87
# If this recipient is explicitly addressed...
88
if r in explicit_recips:
90
# If the member wants to receive duplicates, or if the
91
# recipient is not a member at all, they will get a copy.
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
101
msgdata.setdefault('add-dup-header', set()).add(r)
103
elif r in cc_addresses:
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.
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
115
msg['CC'] = COMMASPACE.join(cc_addresses.values())