#!/usr/bin/python """XMail and mailman integration script To be used as a cmdalias script. If anyone has a better way of integrating this I'd be happy to listen. I'd prefer a dynamic alias handling scheme. """ # Change these to reflect your local situation MAILMAN_HOME = '/data/mailman' XMAIL_HOME = '/data/MailRoot' ECONV = XMAIL_HOME + '/bin/econv' LOG = MAILMAN_HOME + '/logs/xmail_integration' import sys, re, os, tempfile def log(msg): import time timestamp = time.strftime('%Y%m%d-%H:%M:%S', time.localtime()) _logfile = open(LOG, 'a') _logfile.write('%s [%s %s]: %s\n' % ( timestamp, sys.argv[1], sys.argv[2], msg)) _logfile.close() rcpt = sys.argv[1] spoolfile = sys.argv[2] # Isolate the list name and any mailman suffixes, and the VERP if present splitlocal = re.compile("(?P[^+@]+?)" "(?P-bounces|-confirm|-join|-leave|-owner|-request|" "-admin|-subscribe|-unsubscribe)?" "(?P\+.*)?@").match match = splitlocal(rcpt) if not match: # Hmm, not an address format we could handle.. log("Couldn't match on address") sys.exit(1) name, command, verp = match.groups() # Normalize the list name to lower case so it matches the file names correctly. name = name.lower() # If this is no list, bail out now listconf = '%s/lists/%s/config.pck' % (MAILMAN_HOME, name) if not os.path.isfile(listconf): log("Not an existing list") sys.exit(1) # So, this is a list email. We'll now need to convert this and pass it on to # mailman. First, a sanity check if verp and command not in ('-bounces', '-confirm'): # Erm; we don't support VERP for anything else. So this must be something # not mailman related then. Bail out and let XMail handle it. print "Mailman integration: found VERP when VERP not supported" log("Found VERP portion where we didn't expect one.") sys.exit(1) # Normalize (strip the '-', default to 'post') command = command and command[1:] or 'post' # Pipe the spool file through econv and pass the result to mailman os.system('%s --unix --input %s | %s %s %s >> %s &>1' % ( ECONV, spoolfile, MAILMAN_HOME + '/mail/mailman', command, name, LOG)) # Remove temp spoolfile. os.remove(spoolfile) # Record the completed transfer. log("Message transferred successfully")