[FIX] Security fixes for sql injections
[odoo/odoo.git] / addons / email_template / email_template_mailbox.py
1 from osv import osv, fields
2 import time
3 import email_template_engines
4 import netsvc
5 from tools.translate import _
6 import tools
7
8 LOGGER = netsvc.Logger()
9
10 class email_template_mailbox(osv.osv):
11     _name = "email_template.mailbox"
12     _description = 'Email Mailbox'
13     _rec_name = "subject"
14     _order = "date_mail desc"
15     
16     def run_mail_scheduler(self, cursor, user, context=None):
17         """
18         This method is called by Open ERP Scheduler
19         to periodically send emails
20         """
21         try:
22             self.send_all_mail(cursor, user, context)
23         except Exception, e:
24             LOGGER.notifyChannel(
25                                  _("Email Template"),
26                                  netsvc.LOG_ERROR,
27                                  _("Error sending mail: %s" % str(e)))
28         
29     def send_all_mail(self, cr, uid, ids=None, context=None):
30         if ids is None:
31             ids = []
32         if context is None:
33             context = {}
34         filters = [('folder', '=', 'outbox'), ('state', '!=', 'sending')]
35         if 'filters' in context.keys():
36             for each_filter in context['filters']:
37                 filters.append(each_filter)
38         ids = self.search(cr, uid, filters, context=context)
39         self.write(cr, uid, ids, {'state':'sending'}, context)
40         self.send_this_mail(cr, uid, ids, context)
41         return True
42     
43     def send_this_mail(self, cr, uid, ids=None, context=None):
44         if ids is None:
45             ids = []
46         for id in ids:
47             try:
48                 account_obj = self.pool.get('email_template.account')
49                 values = self.read(cr, uid, id, [], context) 
50                 payload = {}
51                 if values['attachments_ids']:
52                     for attid in values['attachments_ids']:
53                         attachment = self.pool.get('ir.attachment').browse(cr, uid, attid, context)#,['datas_fname','datas'])
54                         payload[attachment.datas_fname] = attachment.datas
55                 result = account_obj.send_mail(cr, uid,
56                               [values['account_id'][0]],
57                               {'To':values.get('email_to', u'') or u'', 'CC':values.get('email_cc', u'') or u'', 'BCC':values.get('email_bcc', u'') or u''},
58                               values['subject'] or u'',
59                               {'text':values.get('body_text', u'') or u'', 'html':values.get('body_html', u'') or u''},
60                               payload=payload, context=context)
61                 
62                 if result == True:
63                     self.write(cr, uid, id, {'folder':'sent', 'state':'na', 'date_mail':time.strftime("%Y-%m-%d %H:%M:%S")}, context)
64                     self.historise(cr, uid, [id], "Email sent successfully", context)
65                 else:
66                     self.historise(cr, uid, [id], result, context)
67             except Exception, error:
68                 logger = netsvc.Logger()
69                 logger.notifyChannel(_("Power Email"), netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\nError: %s") % (id, error))
70                 self.historise(cr, uid, [id], error, context)
71             self.write(cr, uid, id, {'state':'na'}, context)
72         return True
73     
74     def historise(self, cr, uid, ids, message='', context=None):
75         for id in ids:
76             history = self.read(cr, uid, id, ['history'], context).get('history', '')
77             self.write(cr, uid, id, {'history':history or '' + "\n" + time.strftime("%Y-%m-%d %H:%M:%S") + ": " + tools.ustr(message)}, context)
78     
79     _columns = {
80             'email_from':fields.char(
81                             'From', 
82                             size=64),
83             'email_to':fields.char(
84                             'Recepient (To)', 
85                             size=250,),
86             'email_cc':fields.char(
87                             ' CC', 
88                             size=250),
89             'email_bcc':fields.char(
90                             ' BCC', 
91                             size=250),
92             'subject':fields.char(
93                             ' Subject', 
94                             size=200,),
95             'body_text':fields.text(
96                             'Standard Body (Text)'),
97             'body_html':fields.text(
98                             'Body (Text-Web Client Only)'),
99             'attachments_ids':fields.many2many(
100                             'ir.attachment', 
101                             'mail_attachments_rel', 
102                             'mail_id', 
103                             'att_id', 
104                             'Attachments'),
105             'account_id' :fields.many2one(
106                             'email_template.account',
107                             'User account', 
108                             required=True),
109             'user':fields.related(
110                             'account_id', 
111                             'user', 
112                             type="many2one", 
113                             relation="res.users", 
114                             string="User"),
115             'server_ref':fields.integer(
116                             'Server Reference of mail', 
117                             help="Applicable for inward items only"),
118             'mail_type':fields.selection([
119                             ('multipart/mixed', 
120                              'Has Attachments'),
121                             ('multipart/alternative', 
122                              'Plain Text & HTML with no attachments'),
123                             ('multipart/related', 
124                              'Intermixed content'),
125                             ('text/plain', 
126                              'Plain Text'),
127                             ('text/html', 
128                              'HTML Body'),
129                             ], 'Mail Contents'),
130             #I like GMAIL which allows putting same mail in many folders
131             #Lets plan it for 0.9
132             'folder':fields.selection([
133                             ('drafts', 'Drafts'),
134                             ('outbox', 'Outbox'),
135                             ('trash', 'Trash'),
136                             ('sent', 'Sent Items'),
137                             ], 'Folder', required=True),
138             'state':fields.selection([
139                             ('na', 'Not Applicable'),
140                             ('sending', 'Sending'),
141                             ], 'Status', required=True),
142             'date_mail':fields.datetime(
143                             'Rec/Sent Date'),
144             'history':fields.text(
145                             'History', 
146                             readonly=True, 
147                             store=True)
148         }
149
150     _defaults = {
151         'state': lambda * a: 'na',
152         'folder': lambda * a: 'outbox',
153     } 
154
155     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
156         if context is None:
157             context = {}
158         if context.get('company', False):
159             users_groups = self.pool.get('res.users').browse(cr, uid, uid, context).groups_id
160             group_acc_rel = {}
161             #get all accounts and get a table of {group1:[account1,account2],group2:[account1]}
162             for each_account_id in self.pool.get('email_template.account').search(cr, uid, [('state', '=', 'approved'), ('company', '=', 'yes')], context=context):
163                 account = self.pool.get('email_template.account').browse(cr, uid, each_account_id, context)
164                 for each_group in account.allowed_groups:
165                     if not account.id in group_acc_rel.get(each_group, []):
166                         groups = group_acc_rel.get(each_group, [])
167                         groups.append(account.id)
168                         group_acc_rel[each_group] = groups
169             users_company_accounts = []
170             for each_group in group_acc_rel.keys():
171                 if each_group in users_groups:
172                     for each_account in group_acc_rel[each_group]:
173                         if not each_account in users_company_accounts:
174                             users_company_accounts.append(each_account)
175             args.append(('account_id', 'in', users_company_accounts))
176         return super(osv.osv, self).search(cr, uid, args, offset, limit,
177                 order, context=context, count=count)
178
179 email_template_mailbox()
180
181 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: