1
from dateutil.relativedelta import relativedelta
2
from osv import fields,osv,orm
3
from tools import config
4
from tools.translate import _
7
AVAILABLE_EMAIL_FIELDS = [
9
('subject', 'Subject'),
14
('isEmpty', 'is Empty'),
15
('isNotEmpty', 'is not Empty'),
16
('contains', 'contains'),
17
('notContains', 'doesn\'t contain'),
18
('endsWith', 'ends with'),
19
('startsWith', 'starts with')
22
class mail_message(osv.osv):
23
_inherit = "mail.message"
25
def send(self, cr, uid, ids, auto_commit=False, context=None):
26
ew_obj = self.pool.get('email.whitelist')
28
for message in self.browse(cr, uid, ids, context=context):
29
if ew_obj.check_whitelist(cr, uid, ids, msg=message):
30
# email passed whitelist, add it to send list
31
ew_ids.append(message['id'])
33
# email contain un-whitelist items, cancel it
34
message.write({'state':'cancel'})
35
return super(mail_message,self).send(cr, uid, ew_ids, auto_commit, context)
38
class email_whitelist(osv.osv):
39
_name = 'email.whitelist'
41
'field': fields.selection(AVAILABLE_EMAIL_FIELDS, 'Field'),
42
'op': fields.selection(OPERATORS, 'Comparator'),
43
'value': fields.text('Value')
46
def check_whitelist(self, cr, uid, ids, context=None, msg=None):
48
Parses the user inputs and return a true or false
49
if the email passes the whitelist or not.
54
ew_ids = self.search(cr, uid, [])
56
# No whitelists, send nothing
59
# starting whitelists checking"
62
for ew in self.browse(cr,uid,ew_ids,context):
64
if ew.field == "body":
71
if eval('self.'+ew.op+'("""'+msg[param]+'""", "'.strip()+ew.value+'")'):
76
tags = ['email_to', 'email_cc', 'email_bcc', 'reply_to']
79
for r in msg[tag].split(','):
80
if not eval('self.'+ew.op+'("'+r.strip()+'", "'+ew.value+'")'):
81
# found recipient not in whitelist, cancel email
85
def isEmpty(self, string, value):
91
def isNotEmpty(self, string, value):
92
return not self.isEmpty(string, value)
94
def contains(self, string, value):
95
if string.lower().find(value.lower()) > -1:
100
def notContains(self, string, value):
101
return not self.contains(string, value)
103
def endsWith(self, string, value):
104
return string.lower().endswith(value.lower())
106
def startsWith(self, string, value):
107
return string.lower().startswith(value.lower())