# -*- mode: python; indent-tabs-mode: t -*- import os import os.path import subprocess import datetime processor.maildir_base = "~/Maildir" processor.auto_reload_rcfile = True def is_to_or_from(mail,address): """ Return true if [mail] is to or from an address that contains [address]. """ return ( False or mail["From"].contains(address) or mail["Reply-To"].contains(address) or mail.target.contains(address)) def is_to_or_from_re(mail,address): """ Return true if [mail] is to or from an address that matches the regex [address]. """ return ( mail["From"].matches(address) or mail.target.matches(address)) def move_ham(mail,folder): y = datetime.datetime.now().year folder = "FOLDERS.Ham."+str(y)+folder dir = processor.maildir_base+"/"+folder if not os.path.isfile(dir+"/maildirfolder"): os.makedirs(dir+"/tmp", 0o777, True) os.makedirs(dir+"/new", 0o777, True) os.makedirs(dir+"/cur", 0o777, True) open(dir+"/maildirfolder", 'a').close() mail.move(folder) def bogofilter_auto(mail): p = subprocess.Popen( ["bogofilter", "-u", "-v", "-I", mail.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) (output, _) = p.communicate() processor.log("*** Bogofilter result: {0!r}".format(output.rstrip())) if p.returncode not in [0, 1, 2]: processor.log_error( "Error running bogofilter: Return code = {0!r}".format( p.returncode)) return p.returncode def bogofilter_ham(mail): subprocess.call(["bogofilter", "-S", "-n", "-I", mail.path]) def bogofilter_spam(mail): subprocess.call(["bogofilter", "-N", "-s", "-I", mail.path]) def handle_incoming_ham_training(mail): bogofilter_ham(mail) handle_incoming_ham(mail) def handle_incoming_spam_training(mail): bogofilter_spam(mail) mail.move("FOLDERS.Spam") def handle_incoming_ham(mail): my_filters(mail) def handle_incoming_spam(mail): mail.move("FOLDERS.Spam") def handle_incoming_unknown(mail): # Filter spam # Whitelist if ( False or is_to_or_from(mail, "fsf.org") or is_to_or_from(mail, "gnu.org") or is_to_or_from(mail, "parabola.nu") or is_to_or_from(mail, "parabolagnulinux.org") or mail["From"] == "MAILER-DAEMON@yahoo.com" or mail["From"] == "3174451635@mms.att.net" or mail["From"].contains("@facebookmail.com>") or mail["From"].contains("@gandi.net") or mail["From"].contains("@github.com>") or mail["From"].contains("@goodwillindy.org>") or mail["From"].contains("@lpi.org>") or mail["From"].contains("@msdlt.k12.in.us>") or mail["From"].contains("@parabola.nu") or mail["From"].contains("@post.oreilly.com>") or mail["From"].contains("@scouting.org>") or mail["From"].contains("@wolframalpha.com>") or mail["From"].contains("margieshu@sbcglobal.net") or mail["From"].contains("parabolagnulinux.org") or mail["List-Id"].matches(".*\.(gnu|gnome|archlinux|parabolagnulinuxlibre)\.org") or mail["List-Id"].matches(".*\.parabola\.nu") or mail["Subject"].contains("[Dev]") or mail["Subject"].contains("[Maintenance]") or mail["Subject"].contains("[PATCH") or mail["Subject"].contains("[systemd-devel]") ): handle_incoming_ham_training(mail) return spam = bogofilter_auto(mail) if spam == 0: handle_incoming_spam(mail) return elif spam == 1: handle_incoming_ham(mail) return elif spam == 2: mail.move("FOLDERS.MysteryMeat") return else: mail.move("FOLDERS.BogoFail") return def my_filters(mail): # Sort mail from GNU mailing lists for list in [ 'bug-gsrc', 'bug-make', 'help-make', 'social', 'help-grub', 'bug-gnuzilla', 'bug-librejs' ]: if ( False or mail["List-Id"].matches(list+"\.gnu\.org") or mail["Subject"].contains('['+list+']') ): move_ham(mail,".software."+list) return # Sort mail from other software mailing lists if ( False or mail["List-Id"].matches("social-discuss\.gnu\.org") or mail["Subject"].contains("social-discuss") ): move_ham(mail,".software.social") return if mail["List-Id"].matches("networkmanager-list\.gnome\.org"): move_ham(mail,".software.networkmanager") return if mail["List-Id"].matches("maintenance\.lists\.parabola(gnulinux\.org|\.nu)"): move_ham(mail,".software.parabola.maintenance") return if is_to_or_from(mail, "labs@parabola.nu"): move_ham(mail,".software.parabola.labs") return if ( False or mail["List-Id"].matches("parabolagnulinux\.org") or mail["List-Id"].matches("parabola\.nu") or is_to_or_from(mail, "parabolagnulinux.org") or is_to_or_from(mail, "kiwwwi.com.ar") or is_to_or_from(mail, "parabola.nu") ): if mail["Subject"].contains("[Django]"): move_ham(mail,".software.parabola.maintenance.django") return else: move_ham(mail,".software.parabola.dev") return if (mail["List-Id"].matches("pacman-dev.archlinux.org")): move_ham(mail,".software.pacman-dev") return if (mail["List-Id"].matches("systemd-devel.lists.freedesktop.org") or is_to_or_from(mail, "systemd-devel@lists.freedesktop.org")): move_ham(mail,".software.systemd-devel") return for subject_re in [ "\[Stow-[^\]]*\].*", ]: if mail["Subject"].matches(subject_re): move_ham(mail,".software") return # Sort mail from some social websites if mail["From"].matches("facebook(|mail)\.com"): move_ham(mail,".Social.Facebook") return if ( False or mail["From"].matches("identi\.ca") or mail["From"].matches("statusnet") ): move_ham(mail,".Social.Identica") return if ( False or mail["From"].contains("@gandi.net") or mail["From"].contains("@ramhost.us") or mail["From"].contains("@startcom.org") ): move_ham(mail,".lukeshu-com") return if mail["From"].matches("twitter\.com"): move_ham(mail,".Social.Twitter") return if mail["From"].matches("@xkcd\.com"): move_ham(mail,".Social.xkcd") return # Sort mail related to Troop 276 if ( False or mail["List-Id"].contains("troopmailinglist.troop276.net") or is_to_or_from(mail,"t276_announcements@att.net") or mail["Subject"].matches("troop") or mail["Subject"].matches("merit\s*badge") or is_to_or_from(mail,"jsting@sbcglobal.net") or is_to_or_from(mail,"trdindy@comcast.net") or is_to_or_from(mail,"wjensen111@aol.com") or is_to_or_from(mail,"dhoyt@yourhomecompany.com") or is_to_or_from(mail,"salupo_vincent_p@lilly.com") or is_to_or_from(mail,"basu@maharjan.org") or is_to_or_from(mail,"muellerindy@yahoo.com") or is_to_or_from(mail,"solorzano.luis@rocketmail.com") or is_to_or_from(mail,"eldredmac@comcast.net")# MacDonell or is_to_or_from(mail,"mitchprather@sbcglobal.net") or is_to_or_from(mail,"oa_wap@yahoo.com") or is_to_or_from(mail,"mytroop.us") or is_to_or_from(mail,"crossroadsbsa.org") ): move_ham(mail,".BoyScouts") return # Sort mail from misc people if ( False or is_to_or_from(mail,"margieshu@sbcglobal.net") or is_to_or_from(mail,"3174451635@mms.att.net") ): move_ham(mail,".misc.Mom") return for address in [ "nintendo.com", "nintendo-news.com", ]: if mail["From"].contains(address): move_ham(mail,".misc.Nintendo") return for address in [ "@lpi.org", "@pearson.com", "CompTIA", ]: if mail["From"].contains(address): move_ham(mail,".misc.CompTIA") return # Sort mail from FRC people for address in [ "@ni.com", "@usfirst.org", "BBonahoom@stanleyworks.com", "allison.m.babcock@gmail.com", "bryanbonahoom@gmail.com", "cdewalt3@yahoo.com", "dave.nelson@ecolab.com", "dickaustin190@yahoo.com", "djnels1@comcast.net", # Dave and Julie Nelson "gamefreak207@gmail.com", # Brett Leedy "jason.zielke@gmail.com", "jeffreysmith@msdlt.k12.in.us", "sarahlittell@comcast.net", "silioso@gmail.com", "skiplittell@comcast.net", "tswilson4801@att.net", "wcxctrack829@aim.com", # Pat "william.walk@gmail.com", ]: if is_to_or_from(mail,address): move_ham(mail,".School.Robotics") return for subject_re in [ "robotics", "\b1024\b", "\b4272\b", "kil-?a-?bytes", ]: if mail["Subject"].matches(subject_re): move_ham(mail,".School.Robotics") return if mail["Subject"].contains("[Quizroom]"): move_ham(mail,".School.CS408") return # Sort mail from software people for address in [ "@archlinux.org", "@canonical.org", "@cnuk.org", "@eff.org", "@foocorp.net", "@fsf.org", "@github.com", "@gitorious.org", "@gnome.org", "@gnu.org", "@ietf.org", "@kde.org", "@nongnu.org", "@sourceforge.com", "@thyrsus.com", "trustees@core3.amsl.com", ]: if is_to_or_from(mail,address): move_ham(mail,".software") return # Sort mail from the school newspaper if ( False or is_to_or_from(mail, "@lnnorthstar.org") or is_to_or_from(mail, "lnnorthstar.org@tigertech.net") ): move_ham(mail,".School.Newspaper") return # Sort mail from various employers/people who pay me if is_to_or_from(mail, "@precisepath.com"): move_ham(mail,".Work.PrecisePath") return if is_to_or_from(mail,"susyphil@aol.com"): move_ham(mail,".Work.PMCH") for address in [ "d.farrar@comcast.net", "dfarrar@avacoustics.net", "@vmware.com", ]: if is_to_or_from(mail,address): move_ham(mail,".Work.FAST") return # Sort misc newsletters if ( False or mail["From"].contains("newsletter") or mail["From"].contains("auto@comicsbyemail.com") or mail["From"].contains("oreilly.com") or mail["Subject"].contains("newsletter") or mail["From"].contains("Info@mailing.jamendo.com") or mail["From"].contains("info@demandprogress.org") or (mail["From"].contains("@sparkfun.com") and mail["Message-Id"].contains("rsgsv.net")) ): move_ham(mail,".misc.Newsletters") return if ( False or mail["From"].contains("@msdlt.k12.in.us") or mail["From"].contains("ltschoolfoundation@gmail.com") or mail["From"].contains("naviance.com") or is_to_or_from(mail,"ibwhite@comcast.net") or mail["Subject"].contains("IOA") or mail["From"].contains("nths.org") or mail["Subject"].contains("NTHS") or mail["Subject"].contains("National Technical Honor Society") or mail["Subject"].contains("NHS") or mail["Subject"].contains("National Honor Society") ): move_ham(mail,".School") return # from college stuff if ( False or mail["Subject"].contains("NYLF") # National Youth Leadership Conference or mail["Subject"].contains("NSHSS") ): move_ham(mail,".College.Societies") return if ( False or mail["From"].contains("admissions@") or mail["Subject"].contains("college") # now we get to the BS or mail["From"].contains("@dreamitdoitindiana.com") or mail["From"].contains("@indianatechinfo.org") ): move_ham(mail,".College") return if mail["From"].contains("@projectwonderful.com"): move_ham(mail,".misc.ProjectWonderful") return if ( False or mail["From"].matches("@localhost") or mail["From"].matches("@[^,>]*\.local") or mail["From"].matches("@[^,>]*\.lan") or mail["To"].matches("luke@") ): move_ham(mail,".LocalSystems") return if ( False or mail["Subject"].contains("password") or mail["Subject"].contains("account") ): move_ham(mail,".misc.accounts") return move_ham(mail,"") handle_mapping = { "Inbox": handle_incoming_unknown, "Bulk Mail": handle_incoming_unknown, "FOLDERS.Spam.training": handle_incoming_spam_training, "FOLDERS.Ham.training": handle_incoming_ham_training, } processor.maildirs = handle_mapping.keys() for mail in processor: handle_mapping[mail.maildir](mail)