Simple notification handler

Hi All,
I know this is a relatively easy question, but I don't have enough of the Mailman/Handlers examples and what I got from searching the web to get a clear picture of how to create a simple handler that does this :
- Parse the Subject line looking for "*rror*"
- If so, send an email to an arbitrary address
The handler will not stop the normal flow of the list in any manner (e.g. it won't hold an email or something). I'll be using the list to store backup reports in which the subject line will have either "OK" or "Error" and I just want to notify someone when an error occurs so they can go in the list private Archive and see what's going on.
I understand the pipeline system and how I could add the Handler for a specific list, but I'm looking for a detailed Python API listing somewhere to see what I can really do in the process() function, e.g.
def process(mlist, msg, msgdata): subject = msg.getheader('subject') # parse subject (I'll find that) and if error # msg.send("arbitraryemail@whatever.com")
Thanx for pointers,
Yves Moisan

Yves Moisan wrote:
- Parse the Subject line looking for "*rror*"
- If so, send an email to an arbitrary address
[...]
I understand the pipeline system and how I could add the Handler for a specific list, but I'm looking for a detailed Python API listing somewhere to see what I can really do in the process() function, e.g.
def process(mlist, msg, msgdata): subject = msg.getheader('subject') # parse subject (I'll find that) and if error # msg.send("arbitraryemail@whatever.com")
The Mailman API is documented in the source.
You need something like
import re from Mailman import Utils from Mailman.Message import UserNotification NTEXT = """The text of the notification. """ NRECIP = 'recipient@example.com' NFROM = 'sender@example.com' NSUBJ = 'Notification subject'
def process(mlist, msg, msgdata): subject = Utils.oneline(msg['subject'], 'us-ascii') if re.search('.*rror.*', subject, re.IGNORECASE) nmsg = UserNotification(NRECIP, NFROM, NSUBJ, NTEXT) nmsg.send(mlist)

Thank you Mark. I'll give it a try.
Yves
You need something like
import re from Mailman import Utils from Mailman.Message import UserNotification NTEXT = """The text of the notification. """ NRECIP = 'recipient@example.com' NFROM = 'sender@example.com' NSUBJ = 'Notification subject'
def process(mlist, msg, msgdata): subject = Utils.oneline(msg['subject'], 'us-ascii') if re.search('.*rror.*', subject, re.IGNORECASE) nmsg = UserNotification(NRECIP, NFROM, NSUBJ, NTEXT) nmsg.send(mlist)

On 12/21/2011 8:43 AM, Yves Moisan wrote:
Thank you Mark. I'll give it a try.
See http://docs.python.org/library/re.html#regular-expression-syntax. You may want a different re. E,g. r'\W[Ee]rror\W' without IGNORECASE.

Hi Mark,
It seems the handler stops the list from working. When I delete the new pipeline the list works fine so it's the Python file (or the way I included it in the pipeline ?) that has a problem.
I've tried including a she-bang line or not (see below for both the handler and the text file used to shove it in the list pipeline), importing Message in addition to UserNotification and a few other things to no avail. One thing I find odd is that the handler doesn't get a .pyc file upon restart. Should I expect that or does it get created the first time it is invoked ?
I've copied the file over from a windows machine and I took care of chowning it so it has the same permissions as the other Handlers. The file looks ok in vi. What other options do I have before pdb ?
Thanx,
Yves Moisan
=== BackupErrors.py ===
#!/usr/bin/python # I also treid without, like in the other handlers on my server # BackupErrors.py """Add a list specific alert generator in case there was a problem in the backup, in which case the Subject of the email will contain the word 'ERROR' """
import re from Mailman import Utils from Mailman.Message import UserNotification # from Mailman.Message import Message,UserNotification NTEXT = """Texte du corps du message TESTS. """ NRECIP = 'xyz@example.com' NFROM = 'abc@example.com' NSUBJ = 'Error in backup'
def process(mlist, msg, msgdata): subject = Utils.oneline(msg['subject'], 'us-ascii') if re.search('.*error.*', subject, re.IGNORECASE) nmsg = UserNotification(NRECIP, NFROM, NSUBJ, NTEXT) nmsg.send(mlist)
=== backupHandlerPipeline.txt ===
mlist.pipeline = [ # These are the modules that do tasks common to all delivery paths. 'SpamDetect', 'Approve', 'Replybot', 'BackupErrors', 'Moderate', 'Hold', 'MimeDel', 'Scrubber', 'Emergency', 'Tagger', 'CalcRecips', 'AvoidDuplicates', 'Cleanse', 'CleanseDKIM', 'CookHeaders', # And now we send the message to the digest mbox file, and to the arch and # news queues. Runners will provide further processing of the message, # specific to those delivery paths. 'ToDigest', 'ToArchive', 'ToUsenet', # Now we'll do a few extra things specific to the member delivery # (outgoing) path, finally leaving the message in the outgoing queue. 'AfterDelivery', 'Acknowledge', 'ToOutgoing', ]

Yves Moisan wrote:
It seems the handler stops the list from working. When I delete the new pipeline the list works fine so it's the Python file (or the way I included it in the pipeline ?) that has a problem.
I've tried including a she-bang line or not (see below for both the handler and the text file used to shove it in the list pipeline), importing Message in addition to UserNotification and a few other things to no avail. One thing I find odd is that the handler doesn't get a .pyc file upon restart. Should I expect that or does it get created the first time it is invoked ?
The .pyc file will not get created until the handler is imported which does not happen until IncomingRunner handles a post for a list which has the handler in its pipeline.
Also, IncomingRunner is a python process which imports the handler, so no shebang line is required.
I've copied the file over from a windows machine and I took care of chowning it so it has the same permissions as the other Handlers. The file looks ok in vi. What other options do I have before pdb ?
Check Mailman's error log.
Also, see below.
=== BackupErrors.py ===
[...]
def process(mlist, msg, msgdata): subject = Utils.oneline(msg['subject'], 'us-ascii') if re.search('.*error.*', subject, re.IGNORECASE)
I misled you here. The above line needs to be
if re.search('.*error.*', subject, re.IGNORECASE):
The missing colon results in a syntax error which causes all list posts to be shunted. There will be an error message and traceback in Mailman's error log. If the messages that were shunted are wanted, you can process them with Mailman's bin/unshunt. If not, remove them from qfiles/shunt.

Hi Mark,
Our emails crossed here. I just tried importing my handler on the python prompt and the syntax error popped obvious. The minute the pipeline for the list was set with /bin/config_list I could see the pyc so I knew I was in business.
A final question : I see now that even restarting Mailman won't change the list-specific handler application and one needs to explicitly delete it if one wants to get rid of it. That's a good thing. But where are list-specific pipeline defs stored ?
Thanx!
Yves
Yves Moisan wrote:
It seems the handler stops the list from working. When I delete the new pipeline the list works fine so it's the Python file (or the way I included it in the pipeline ?) that has a problem.
I've tried including a she-bang line or not (see below for both the handler and the text file used to shove it in the list pipeline), importing Message in addition to UserNotification and a few other things to no avail. One thing I find odd is that the handler doesn't get a .pyc file upon restart. Should I expect that or does it get created the first time it is invoked ?
The .pyc file will not get created until the handler is imported which does not happen until IncomingRunner handles a post for a list which has the handler in its pipeline.
Also, IncomingRunner is a python process which imports the handler, so no shebang line is required.
I've copied the file over from a windows machine and I took care of chowning it so it has the same permissions as the other Handlers. The file looks ok in vi. What other options do I have before pdb ?
Check Mailman's error log.
Also, see below.
=== BackupErrors.py ===
[...]
def process(mlist, msg, msgdata): subject = Utils.oneline(msg['subject'], 'us-ascii') if re.search('.*error.*', subject, re.IGNORECASE)
I misled you here. The above line needs to be
if re.search('.*error.*', subject, re.IGNORECASE):
The missing colon results in a syntax error which causes all list posts to be shunted. There will be an error message and traceback in Mailman's error log. If the messages that were shunted are wanted, you can process them with Mailman's bin/unshunt. If not, remove them from qfiles/shunt.

Yves Moisan wrote:
A final question : I see now that even restarting Mailman won't change the list-specific handler application and one needs to explicitly delete it if one wants to get rid of it. That's a good thing. But where are list-specific pipeline defs stored ?
The pipeline attribute is stored along with all the other attributes of the list object, the persistent state of which is stored in lists/LISTNAME/config.pck.
participants (2)
-
Mark Sapiro
-
Yves Moisan