[Mailman-Users] procmail before mailman- closer!

John [H2O] washakie at gmail.com
Sat Jun 14 10:11:39 CEST 2008


Well, I finally gave up and renamed the list! It is working now. However, I'm
having another challenge. I'm trying to create a filter that will accept
messages with a specific combination of user/subject/attachment type and
then execute the attachment as a python file. But it seems the file is
actually getting executed twice - I guess because when it is 'read' by
procmail, it is actually run... is this correct? Below is my method, could
someone help explain how to avoid running the attachment twice?

Here is how I achieve this:
1) procmail filters initiate a python script call:
:0
* ^Subject:.*RunMySecretSubject*
{
	LOG="RunScript "
	:0:
	| echo "Job received at " `date` >> /home/username/jobs;
/home/username/runAttached.py
}

2) python script checks the mail:
#!/usr/bin/python

import sys,datetime, os
import subprocess as sub
from email import Parser

def run_cmd(cmd):
    """ RUN A BASH CMD """
    p = sub.call(cmd, shell=True)

now=datetime.datetime.now()
pyFile='runAttached_pyfile.py'
try: 
    os.remove(pyFile)
except: pass
rawData=sys.stdin.read()
p=Parser.Parser()
msg=p.parsestr(rawData)

dbg = file('/home/username/debug','w')
dbg.write('Trying to parse file at: %s\n' % now)

for part in msg.walk():
    if part.get_content_maintype()=="multipart":
        dbg.write('found multipart :: \n')
        continue
    name=part.get_param("name")
    if name==None:
        name="noName"
    dbg.write("Partname == %s\n" % name)
    if (name.endswith('.shx')):
        # grab the logfile
        f2=file(pyFile,'wb')
        body=part.get_payload(decode=1)
        f2.write(body)
        f2.close()
        os.chmod(pyFile,0774)
        cmd = "python %s" % pyFile
        run_cmd(cmd)
        dbg.write("file: %s contains:\n %s \n\nFinished at: %s\n" %
(name,body,datetime.datetime.now()))
        run_cmd(cmd)
dbg.close()


-- 
View this message in context: http://www.nabble.com/procmail-before-mailman-tp17789754p17836952.html
Sent from the Mailman - Users mailing list archive at Nabble.com.



More information about the Mailman-Users mailing list