On Wed, Jan 20, 2010 at 09:44:25AM -0500, Tim Gray wrote:
> On Wed 20, Jan'10 at 2:11 PM +0000, Chris G wrote:
> >I can upload/attach the scripts if anyone is interested, getAliases.py
> >and getLists.py are trivial but there's a bit more to the mail
> >filtering one.
>
> I wouldn't turn down the chance to see them, but they should be easy
> to implement.
>
> However, what does your filtering one do that procmail doesn't? I
> guess it saves you from editing your procmail setup every time you
> add a mailing list as well... I'd be interested in seeing that one
> for sure.
You've hit the nail on the head! :-) It's like procmail but driven
from the same 'configuration' file as getAliases.py and getLists.py.
Will the mutt list allow attachments - I'll try, they're not very big
so I'm sure they won't hit any size restrictions.
--
Chris Green
#!/usr/bin/python
import sys
#
#
#
#
home = "/home/chris"
filtfile = home + "/.mutt/filter"
#
#
# Get mailing lists from filter file
#
f = open(filtfile, 'r')
for ln in f:
if ln[0] == '#': # ignore comments
continue
#
#
# split the line into fields
#
fld = ln.split()
tocc = fld[2]
if ("/" in tocc): # / in field indicates there's a subject match too
x = tocc.split("/")
tocc = x[0]
sys.stdout.write("alias ")
sys.stdout.write(fld[0] + " ")
sys.stdout.write(tocc + "\n")
#!/usr/bin/python
import sys
#
#
#
#
home = "/home/chris"
filtfile = home + "/.mutt/filter"
lastNm = ""
#
#
# Get mailing lists from filter file
#
f = open(filtfile, 'r')
for ln in f:
if ln[0] == '#': # ignore comments
continue
#
#
# split the line into fields
#
fld = ln.split()
tocc = fld[2]
if ("/" in tocc): # / in field indicates there's a subject match too
x = tocc.split("/")
tocc = x[0]
#
#
# If it's a mailing list (destination directory is "Li") and it isn't
# a second address for the same mailing list then output the address
#
if fld[1] == "Li" and fld[0] != lastNm:
sys.stdout.write(tocc + " ")
lastNm = fld[0]
#!/usr/bin/python
#
#
# Mail filtering script, replacement for the perl version
#
import email
import mailbox
import os
import sys
import time
#
#
# Redirect any exceptions to a file
#
sys.stderr = open("/home/chris/tmp/mail.err", 'a')
#
#
# Some constants (i.e. configuration)
#
home = "/home/chris"
logfile = home + "/tmp/mail.log"
filtfile = home + "/.mutt/filter"
mldir = home + "/Mail/"
indir = mldir + "In/"
def log(s):
lf = open(logfile, 'a')
lf.write(s + "\n")
lf.close()
#
#
#
#
mbName = ""
#
#
# Get the message from standard input and extract the To:, Cc: and
# Subject: headers
#
m = email.message_from_file(sys.stdin)
msgcc = m.get("Cc", "unknown").lower()
msgto = m.get("To", "unknown").lower()
msgsb = m.get("Subject", "unknown")
#
#
# See if it's in our filter file
#
f = open(filtfile, 'r')
for ln in f:
if ln[0] == '#': # ignore comments
continue
#
#
# split the line into fields
#
fld = ln.split()
nm = fld[0] # name/alias
dd = fld[1] + "/" # destination directory
if ("/" in fld[2]): # / in field indicates there's a subject match too
x = fld[2].split("/")
tocc = x[0].lower()
sb = x[1].lower()
else:
tocc = fld[2].lower()
sb = ""
#
#
# see if the filter line matches the message
#
if (tocc in msgcc or tocc in msgto) and (sb in msgsb.lower()):
mbName = mldir + dd + nm
if len(fld) > 3:
m.replace_header("Subject", msgsb.replace('[' + fld[3] + ']', ''))
break
log("From: " + m.get("From", "unknown"))
log("Destination is: " + mbName)
log("\n\n")
#
#
# if destination mb name hasn't been set yet then set to the default
# inbox
#
if mbName == "":
mbName = indir + "inbox"
#
#
# set up the mb for adding the new message, will create if it doesn't exist
#
dest = mailbox.Maildir(mbName, factory=None)
dest.add(m) # add the new message
dest.flush()