Filter class

TheSaint fc14301589 at icqmail.com
Sat Oct 7 08:08:46 EDT 2006


On 17:02, sabato 30 settembre 2006 TheSaint wrote:

> Hello NG,
> 
> Curious to know whether exists a filter class.
> I'm doing some rough mail filtering on my own criteria, but I'm very new on
> programming and I like to find some clue on passing a config file of rules
> which will be regex by Python.
> 
> TIA

I went to study some long and I developed a small program, herebelow.
But I'd appreciate if it vill come some suggestion for a pythonic cosmetic
fashion :)

Thank to everybody.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#! /usr/bin/env python
"""" Test the mailserver to clean unwanted mail"""

from poplib import POP3
import ConfigParser , sys , os , re
MULTILINE = 8

def call_POP(args):
    if len(args) != 4: return
    for cnt in range(len(args)):
        if args[cnt] =='' or args[cnt] == None : return
    a = POP3(args[0])
    a.user(args[1])
    print 'Server %s reply %s' %(args[0], a.pass_(args[2]))
    (numMsgs, totalSize) = a.stat()
    msg9 = ''
    for i in range(1, numMsgs +1):
        for hdrline in a.top(i,0)[1]:
            for argmn in args[3]:
                if not re.search(argmn[1], hdrline): continue
                lsenty = 0
                if argmn[0].startswith('a') and not argmn[0].startswith('d'):
                    continue
                #list only the messages that aren't matching
                if lsenty == i: continue # no duplicate messages
                fwrt.write('Msg No %03d\nTEST  "%s" \n %s\n' \
                  %(i,argmn[1], hdrline))
                lsenty = i
    fwrt.write('\n\n')
    a.quit()

def get_mydir(file):
    if os.name == 'posix':
        adir = os.path.split(file)[0]
        afil = os.path.split(file)[1]
        if adir == '': adir = os.getcwd()
        for pth in ('~/', '$HOME/'):
            if pth in adir: adir = os.environ['HOME']
        file = adir + '/' + afil
    if not os.path.exists(file):
        print 'Configuration file not found'
        return 0
    return file

if len(sys.argv) != 3:
    print 'Incorrect  Number of arguments!'
    sys.exit(0)
infil = get_mydir(sys.argv[1])
if not infil: sys.exit(0)
opnfil = sys.argv[2]
fwrt = open(opnfil,'w')

cfg = ConfigParser.ConfigParser()
cfg.read(infil)
infil = ''
fltr = 'Filters'
if  fltr in cfg.sections():
    infil = cfg.items(fltr)
for section in cfg.sections():
    if section.lower() in ('setting', 'filters'): continue
    adir = 0
    afil = {}
    for pth in ('server', 'user', 'pass'):
        afil[adir] = cfg.get(section, pth)
        adir += 1
    afil[adir] = infil
    print 'Now trying to connect to ' + section
    call_POP(afil)
fwrt.close()

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Also the config File enclosed (some detail is obscured for obvious purposes).

==========================================================================
[Jaring]
PASS = xxxxxxxxxxxxxxxxxxx
USER = wwwwwwwwwwwwwwwwwwwww
SERVER = eeeeeeeeeeeeeeeee

[Vodafone]
SERVER = popmmmmmmmmmmmmmm
USER = godddllrrrrrrrrr
PASS = rrettttttttttt

[TMnet]
SERVER = fgotttttttttee
USER = gggggggrrrrrrdfng
PASS = rrrrrrrrrrrrrrro

[YahooIt]
SERVER = ffpogakfbmb
USER = fglkfrtrtthajl
PASS = fg8uarewaehueh

[Setting]
#All section is not impemented. It's supposed to give either a filepath or
# none
LOG = no
# obviously how much we want to know about the process
VERBOSE = 3
# Some case we won't need errors from python and let the matter die as is
SWALLOW_ERR = yes
# some implementation of threading to have the way to log on
# all the server in parallel
PARALLEL = yes

[Filters]
# A = Allow (anything which starts with "a" is filtered as allowed)
# to keep as good.

A01 = ^From: .*\.cz
A02 = ^(To|Cc): .*wwwwwwwwwwwwwwwwwwwww
A03 = ^(To|Cc): .*godddllrrrrrrrrr@
A04 = ^(To|Cc): .*fglkfrtrtthajl@
A05 = ^(To|Cc): .*122951205u@
A06 = ^From: .*\.my

# D = Deny (anything which starts with "a" is filtered as denied) and will
# fill the list for deletion.

D01 = ^From: .*\.com\.my
D02 = \*\*\*SPAM\*\*\*
==========================================================================

Let me say that is an experimental program for learning purpose, yet. More
things should be implemented like :
1 Real deletion for those email listed in the report file.
2 a single group pattern for the regex search
3 Logging errors and all functions mentioned in the "Setting" section.
4 last but not least a GUI.

The idea was born when i was looking for the "mailfilter" package for Arch
Linux. Actually has been retired. Then I tought " It would be good if a
platform indipendent program will do the same job or a bit more".

Testing on win32 platform is good help.
So, if somebody like to join, very welcome for all of the possible chances,
please try to contact me at _fulvio AT tm DOT net DOT my_.

Fulvio



More information about the Python-list mailing list