"""Calculate the regular (i.e. non-digest) recipients of the message.
This module calculates the non-digest recipients for the message based on the
list's membership and configuration options. It places the list of recipients
on the `recips' attribute of the message. This attribute is used by the
SendmailDeliver and BulkDeliver modules.
"""
import email.Utils
from Mailman import mm_cfg
from Mailman import Utils
from Mailman import Message
from Mailman import Errors
from Mailman.MemberAdaptor import ENABLED
from Mailman.MailList import MailList
from Mailman.i18n import _
from Mailman.Logging.Syslog import syslog
from Mailman.Errors import MMUnknownListError
try:
set
except NameError: from sets import Set as set
def process(mlist, msg, msgdata):
if msgdata.has_key('recips'):
return
include_sender = 1
sender = msg.get_sender()
try:
if mlist.getMemberOption(sender, mm_cfg.DontReceiveOwnPosts):
include_sender = 0
except Errors.NotAMemberError:
pass
missing = []
password = msg.get('urgent', missing)
if password is not missing:
if mlist.Authenticate((mm_cfg.AuthListModerator,
mm_cfg.AuthListAdmin),
password):
recips = mlist.getMemberCPAddresses(mlist.getRegularMemberKeys() +
mlist.getDigestMemberKeys())
msgdata['recips'] = recips
return
else:
realname = mlist.real_name
text = _("""\
Your urgent message to the %(realname)s mailing list was not authorized for
delivery. The original message as received by Mailman is attached.
""")
raise Errors.RejectMessage, Utils.wrap(text)
recips = [mlist.getMemberCPAddress(m)
for m in mlist.getRegularMemberKeys()
if mlist.getDeliveryStatus(m) == ENABLED]
if not include_sender:
try:
recips.remove(mlist.getMemberCPAddress(sender))
except (Errors.NotAMemberError, ValueError):
pass
do_topic_filters(mlist, msg, msgdata, recips)
recips = do_exclude(mlist, msg, msgdata, recips)
recips = do_include(mlist, msg, msgdata, recips)
msgdata['recips'] = recips
def do_topic_filters(mlist, msg, msgdata, recips):
if not mlist.topics_enabled:
return
hits = msgdata.get('topichits')
zaprecips = []
if hits:
for user in recips:
utopics = mlist.getMemberTopics(user)
if not utopics:
continue
for topic in utopics:
if topic in hits:
break
else:
zaprecips.append(user)
else:
for user in recips:
if not mlist.getMemberTopics(user):
continue
if not mlist.getMemberOption(user,
mm_cfg.ReceiveNonmatchingTopics):
zaprecips.append(user)
for user in zaprecips:
recips.remove(user)
def do_exclude(mlist, msg, msgdata, recips):
if not mlist.regular_exclude_lists:
return recips
recips = set(recips)
destinations = email.Utils.getaddresses(msg.get_all('to', []) +
msg.get_all('cc', []))
destinations = [y.lower() for x,y in destinations]
for listname in mlist.regular_exclude_lists:
listname = listname.lower()
if listname not in destinations:
continue
listlhs, hostname = listname.split('@')
if listlhs == mlist.internal_name():
syslog('error', 'Exclude list %s is a self reference.',
listname)
continue
try:
slist = MailList(listlhs, lock=False)
except MMUnknownListError:
syslog('error', 'Exclude list %s not found.', listname)
continue
if not mm_cfg.ALLOW_CROSS_DOMAIN_SIBLING \
and slist.host_name != hostname:
syslog('error', 'Exclude list %s is not in the same domain.',
listname)
continue
srecips = set([slist.getMemberCPAddress(m)
for m in slist.getRegularMemberKeys()
if slist.getDeliveryStatus(m) == ENABLED])
recips -= srecips
return list(recips)
def do_include(mlist, msg, msgdata, recips):
if not mlist.regular_include_lists:
return recips
recips = set(recips)
destinations = email.Utils.getaddresses(msg.get_all('to', []) +
msg.get_all('cc', []))
destinations = [y.lower() for x,y in destinations]
for listname in mlist.regular_include_lists:
listname = listname.lower()
if listname in destinations:
continue
listlhs, hostname = listname.split('@')
if listlhs == mlist.internal_name():
syslog('error', 'Include list %s is a self reference.',
listname)
continue
try:
slist = MailList(listlhs, lock=False)
except MMUnknownListError:
syslog('error', 'Include list %s not found.', listname)
continue
if not mm_cfg.ALLOW_CROSS_DOMAIN_SIBLING \
and slist.host_name != hostname:
syslog('error', 'Include list %s is not in the same domain.',
listname)
continue
srecips = set([slist.getMemberCPAddress(m)
for m in slist.getRegularMemberKeys()
if slist.getDeliveryStatus(m) == ENABLED])
recips |= srecips
return list(recips)