[Mailman-Users] Writing a custom handler

Chris Nulk cnulk at scu.edu
Wed Jul 3 16:40:43 CEST 2013


On 7/2/2013 5:23 PM, Mark Sapiro wrote:
> On 07/02/2013 03:09 PM, Chris Nulk wrote:
>> In the updated code, I did change the populating of the banlist in the
>> Read_GlobalBan_File function.  Now, it strips and lowercases the
>> addresses before it checks if the address is in the banlist. Before, it
>> checked then populated a stripped, lowercase version. This could have
>> resulted in duplicates if the address in the banlist but the address
>> checked had a different case profile.

>>      # Go through possible senders.  Check if any of them are
>>      #   on the global ban list
>>      for sender in msg.get_senders():
>>          if sender.lower() in banlist:
>
> Not necessary to lowercase sender here as msg.get_senders() always
> returns lowercased addresses unless called with a preserve_case argument
> with a True value.

Thanks.  It was more of a just to make sure thing.

Actually, I code I was referring to was in Read_GlobalBan_File:

def Read_GlobalBan_File(ban_file):
     try:
         with open(ban_file) as f:
             for addr in f:
                 # strip and lowercase addr
                 addr = addr.lower().strip()
                 # if addr is not in banlist, add it - to avoid 
duplicates, should not add blank addresses
                 if (addr) and (addr not in banlist):
                     banlist.append(addr)

In previous versions, lower() and strip() were in the banlist.append not 
where they are now.  That would have allowed the possibility of 
duplicates if the same addr was presented with different cases (i.e. 
jdoe at example.com and JDoe at example.com)

>> # copied almost verbatim from Mailman/Handlers/Moderate.py
>> def do_discard_globalban(mlist, msg, sender):
>>      # forward auto-discards to list owners?
>>      if mlist.forward_auto_discards:
>
> Note that no forwarding of the discard occurs unless the list's Privacy
> options... -> Sender filters -> forward_auto_discards is set to Yes.
> Otherwise, no notice is sent and the only indication is a "Message
> discarded, msgid: ..." entry in the 'vette' log.
>
> You may wish to remove the above condition.

I will let the list-owners decide if they want the messages. Granted, it 
will be all or nothing for the discards.

>>          lang = mlist.preferred_language
>>          nmsg = Message.UserNotification(mlist.GetOwnerEmail(),
>
> mlist.GetOwnerEmail() returns the LISTNAME-owner address which is the
> recipient of this mail. This argument can be a string with a single
> address as it's value as it is here or a list of strings, each of which
> is an address. So the above line could be changed to
>
>          nmsg = Message.UserNotification([
>                                           mlist.GetOwnerEmail(),
>                                           'you at example.com',
>                                           ],
>
> with whatever list of addresses you want, or maybe since list owners
> aren't much involved with this ban list
>
>          nmsg = Message.UserNotification('you at example.com',
>

Okay.  Makes sense.  Here is the modified do_discard_globalban code:

def do_discard_globalban(mlist, msg, sender):
     # forward discarded message to site administrator(s) if defined
     #    in mm_cfg.GLOBALBANLIST_NOTIFY
     notifylist = []
     if mm_cfg.GLOBALBANLIST_NOTIFY:
         notifylist.append(mm_cfg.GLOBALBANLIST_NOTIFY)

     # forward auto-discards to list owners?
     if mlist.forward_auto_discards:
         notifylist.append(mlist.GetOwnerEmail())

     # Send message if notifylist is not empty
     if notifylist:
         lang = mlist.preferred_language
         nmsg = Message.UserNotification(notifylist,
                                         mlist.GetBouncesEmail(),
                                         _('Global Ban List Auto-discard 
notification'),
                                         lang=lang)
         nmsg.set_type('multipart/mixed')
         text = MIMEText(Utils.wrap(_("""\
The sender - %(sender)s - of the attached message is on the Global Ban 
list.  Therefore, the message
has been automatically discarded.""")),
                         _charset=Utils.GetCharSet(lang))
         nmsg.attach(text)
         nmsg.attach(MIMEMessage(msg))
         nmsg.send(mlist)
     # Discard the message
     raise Errors.DiscardMessage


Thanks again for the help,
Chris


More information about the Mailman-Users mailing list