AvoidDuplicates.py [plain text]
"""If the user wishes it, do not send duplicates of the same message.
This module keeps an in-memory dictionary of Message-ID: and recipient pairs.
If a message with an identical Message-ID: is about to be sent to someone who
has already received a copy, we either drop the message, add a duplicate
warning header, or pass it through, depending on the user's preferences.
"""
from email.Utils import getaddresses, formataddr
from Mailman import mm_cfg
COMMASPACE = ', '
try:
True, False
except NameError:
True = 1
False = 0
def process(mlist, msg, msgdata):
recips = msgdata['recips']
if not recips:
return
explicit_recips = {}
listaddrs = [mlist.GetListEmail(), mlist.GetBouncesEmail(),
mlist.GetOwnerEmail(), mlist.GetRequestEmail()]
for addr in listaddrs:
explicit_recips[addr] = True
ccaddrs = {}
for header in ('to', 'cc', 'resent-to', 'resent-cc'):
addrs = getaddresses(msg.get_all(header, []))
if header == 'cc':
for name, addr in addrs:
ccaddrs[addr] = name, addr
for name, addr in addrs:
if not addr:
continue
explicit_recips[addr] = True
for addr in listaddrs:
del explicit_recips[addr]
if not explicit_recips:
return
newrecips = []
for r in recips:
if explicit_recips.has_key(r):
send_duplicate = True
if mlist.isMember(r) and \
mlist.getMemberOption(r, mm_cfg.DontReceiveDuplicates):
send_duplicate = False
if send_duplicate:
msgdata.setdefault('add-dup-header', {})[r] = True
newrecips.append(r)
elif ccaddrs.has_key(r):
del ccaddrs[r]
else:
newrecips.append(r)
msgdata['recips'] = newrecips
del msg['cc']
if ccaddrs:
msg['Cc'] = COMMASPACE.join([formataddr(i) for i in ccaddrs.values()])