aboutsummaryrefslogtreecommitdiff
path: root/philter.py
diff options
context:
space:
mode:
Diffstat (limited to 'philter.py')
-rwxr-xr-xphilter.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/philter.py b/philter.py
new file mode 100755
index 0000000..38229aa
--- /dev/null
+++ b/philter.py
@@ -0,0 +1,83 @@
+#! /usr/bin/python
+#
+# Email filter
+#
+# This is a slighted channged version of Philter available at
+# http://philter.sourceforge.net written by Prabhakar V. Chaganti and
+# distributed under GPLv2.
+#
+# Minor changes by rhatto at riseup.net.
+#
+
+import ConfigParser, os, rfc822, re, string, posixpath
+
+
+class Philter:
+ def __init__(self,match,header,destination,maildir):
+ self.re = re.compile(match)
+ self.header = header
+ self.destination = destination
+ self.maildir = maildir
+
+
+ def __str__(self):
+ return ("philter : \n \t match: %s \n\t header: %s \n\t destination: %s \n\t maildir: %s" %
+ (self.re,self.header,self.destination,self.maildir))
+
+
+
+class PhilterDriver:
+ __propFile = open(string.join((posixpath.expanduser('~'),'/.config/scripts/philterrc'),''))
+ __newDir = '/new'
+
+ def createPhilters(self):
+ sections = PhilterDriver.__config.sections()
+ sections.sort()
+ philters=[]
+ maildir = PhilterDriver.__config.get('DEFAULT','maildir')
+ for section in sections:
+ philters.append(Philter((PhilterDriver.__config.get(section,'match')),
+ string.split(PhilterDriver.__config.get(section,'header'),','),
+ PhilterDriver.__config.get(section,'destination'),
+ string.join((maildir,'/',PhilterDriver.__config.get(section,'destination'),'/new'),'')))
+ return philters
+
+
+ def parseConfig(self):
+ PhilterDriver.__config = ConfigParser.ConfigParser()
+ PhilterDriver.__config.readfp(PhilterDriver.__propFile)
+
+
+ def philterMaildir(self, philters):
+ inbox = string.join([PhilterDriver.__config.get('DEFAULT','maildir'),
+ "/",PhilterDriver.__config.get('DEFAULT','inbox'),
+ PhilterDriver.__newDir],"")
+ newMessages = os.listdir(inbox)
+ maildir = PhilterDriver.__config.get('DEFAULT','maildir')
+ found = 0
+ for newMessage in newMessages:
+ msg = rfc822.Message(open(string.join([inbox,"/",newMessage],"")))
+ for philter in philters:
+ for hdr in philter.header:
+ if msg.getheader(hdr):
+ if philter.re.search(string.lower(msg.getheader(hdr))):
+ os.rename(string.join((maildir,'INBOX/new/',newMessage),''),
+ string.join((philter.maildir,'/',newMessage),''))
+ found = 1
+ break
+
+ if found:
+ found = 0
+ break
+
+
+ def main(self):
+ driver = PhilterDriver()
+ driver.parseConfig()
+ philters = driver.createPhilters()
+ driver.philterMaildir(philters)
+
+
+
+if __name__ == '__main__':
+ PhilterDriver().main()