# Copyright (C) 2018 by the Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. """This is a sample withlist framework that can be used to test a custom handler. Save this file in Mailman's bin/ directory as bin/test_handler.py, and edit it if needed. Also create a test list if you don't already have one. Your handler is a .py file in the Mailman/Handlers/ directory. Then run this with bin/withlist -l -r test_handler test_list handler /path/to/test_message kw1=val1 kw2=val2 ... where: test_list is the name of your test list handler is the name of your handler without the .py extension /path/to/test_message is a file containing your a test message kwn=valn are keyword/value pairs to be added to the otherwise empty metadata dictionary passed to your handler. These are only needed if your handler uses them. This will create a message object from your test message source, import your handler and call its process() function with the test list, the message and the metadata as arguments and then write the resultant message and metadata to the standard output, save the possibly updated test list and exit. """ import re import sys import email from Mailman import Errors, Message def test_handler(mlist, *args): newargs = [] msgdata = {} for i in range(len(args)): mo = re.search('(?P\w+)=(?P.+)', args[i]) if mo: try: v = eval(mo.group('v')) except SyntaxError: v = mo.group('v') msgdata[mo.group('k')] = v else: newargs.append(args[i]) if len(newargs) != 2: print >> sys.stderr, __doc__ print >> sys.stderr, "Incorrect argument count." return fp = open(newargs[1]) msg = email.message_from_file(fp, Message.Message) fp.close() mod = 'Mailman.Handlers.' + newargs[0] __import__(mod) try: sys.modules[mod].process(mlist, msg, msgdata) except Errors.DiscardMessage, e: print 'Handler raised DiscardMessage: {0}'.format(repr(e)) except Errors.RejectMessage, e: print 'Handler raised RejectMessage: {0}'.format(repr(e)) except Errors.HoldMessage, e: print 'Handler raised HoldMessage: {0}'.format(repr(e)) print >> sys.stderr, 'Saving list %s' % mlist.internal_name() mlist.Save() print '---- message after handler process ----' print msg.as_string() print '---- end of message after handler process ----' if msgdata: print '\n---- message metadata ----' for k, v in msgdata.items(): print ' %s: %r' % (k, v)