#!/usr/bin/python ################################################### # Pipe Messages v1.0 # Copyright (c) 2005 S. Alexander Jacobson. All Rights Reserved ################################################### """ 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. """ import email import email.Errors import mailbox import os import sys def msgfactory(fp): try: return email.message_from_file(fp) except email.Errors.MessageParseError: return '' def pipe(msg,cmd): (childIn,childOut)=os.popen4(cmd) childIn.write(str(msg)) childIn.close() print childOut.read() childOut.close() def main(): if len(sys.argv) < 3: usage() return folder=sys.argv[1] target=sys.argv[2] fp=1 try: fp=open(folder,"rb") except: return mbox = mailbox.PortableUnixMailbox(fp, msgfactory) skip=mbox.next() #imap placeholder object for msg in mbox: pipe(msg,target) fp.close() fp=open(folder,"wb") fp.write(str(skip)) fp.close() #os.remove(folder) def usage(): print """ usage: pipeMsg.py folder target e.g. $ pipeMsg.py myFolder "wc -l" Will show the length of every message in your inbox """ main()