-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailfilter.py
191 lines (144 loc) · 4.19 KB
/
mailfilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import poplib, imaplib, smtplib
import email, email.parser, email.message, email.header, rfc822
import sys, os, os.path, re, datetime, time
import imp, pickle, argparse
import pprint, traceback
class Filter:
def matches( self, msg ):
raise Exception("not implemented")
def __str__( self ):
return type(self)
class Yes (Filter):
def matches( self, msg ):
return True
def __str__( self ):
return "Yes"
class And (Filter):
def __init__(self, *filters):
self.filters = filters
def matches( self, msg ):
for f in self.filters:
if not f.matches( msg ):
return False
return True
def __str__( self ):
return "( " + reduce( lambda s, t: "%s and %s" % (s, t), self.filters ) + " )"
class Not (Filter):
def __init__(self, filter):
self.filter = filter
def matches( self, msg ):
return not self.filter.matches(msg)
def __str__( self ):
return "Not %s" % self.filter
class Or (Filter):
def __init__(self, *filters):
self.filters = filters
def matches( self, msg ):
for f in self.filters:
if f.matches( msg ):
return True
return False
def __str__( self ):
return "( " + reduce( lambda s, t: "%s or %s" % (s, t), self.filters ) + " )"
class HeaderFilter (Filter):
def __init__(self, header):
self.header = header
def matches( self, msg ):
#TODO: allow access to xx_vars here!
if self.header.startswith("xx_"):
try:
hh = getattr( msg, self.header )
except:
hh = None
else:
hh = msg.get_all(self.header)
if not type(hh) in (tuple, list, set):
hh = ( hh, )
if hh:
for h in hh:
h = decode_header_str( h )
if self.matches_value( h ):
return True
return False
def matches_value( self, v ):
return Exception("not implemented")
class Matches (HeaderFilter):
def __init__(self, header, regex, flags = 0):
HeaderFilter.__init__(self, header)
if type(regex) == str or type(regex) == unicode:
self.regex = re.compile(regex, flags)
else:
self.regex = regex
def matches_value( self, v ):
if v.startswith('^'):
m = self.regex.match( v )
else:
m = self.regex.search( v )
if m: return True
else: return False
def __str__( self ):
return "%s ~~ %s" % (self.header, self.regex.pattern)
class Equals (HeaderFilter):
def __init__(self, header, value):
HeaderFilter.__init__(self, header)
self.value = value
def matches_value( self, v ):
return v == self.value
def __str__( self ):
return "%s == %s" % (self.header, self.value)
class HasTag (Equals):
def __init__(self, tag):
Equals.__init__(self, "xx_flags", tag)
class Contains (Matches):
def __init__(self, header, value, case_sensitive = False):
if case_sensitive:
f = rs.CASE_SENSITIVE
else:
f = 0
if type(value) != str:
p = "(" + "|".join( map( lambda s: re.escape(s), value ) ) + ")"
else:
p = re.escape(value)
Matches.__init__(self, header, '.*' + p + '.*', f)
class Greater (HeaderFilter):
def __init__(self, header, value):
HeaderFilter.__init__(self, header)
self.value = value
def matches_value( self, v ):
return v > self.value
def __str__( self ):
return "%s > %s" % (self.header, self.value)
class Less (HeaderFilter):
def __init__(self, header, value):
HeaderFilter.__init__(self, header)
self.value = value
def matches_value( self, v ):
return v < self.value
def __str__( self ):
return "%s < %s" % (self.header, self.value)
class Older (HeaderFilter):
def __init__(self, header, age):
HeaderFilter.__init__(self, header)
if type(age) == str or type(age) == unicode:
if age.endswith("y"):
age = int(age[:-1]) * 365 * 24 * 60 * 60
elif age.endswith("w"):
age = int(age[:-1]) * 7 * 24 * 60 * 60
elif age.endswith("d"):
age = int(age[:-1]) * 24 * 60 * 60
elif age.endswith("h"):
age = int(age[:-1]) * 60 * 60
elif age.endswith("m"):
age = int(age[:-1]) * 60
elif age.endswith("s"):
age = int(age);
else:
age = int(age) * 24 * 60 * 60;
self.age = age
def matches_value( self, v ):
t0 = time.time()
then = rfc822.parsedate_tz( v )
t1 = rfc822.mktime_tz(then)
return (t0 - t1) > self.age
def __str__( self ):
return "%s older than %s seconds" % (self.header, self.value)