[MERGE] merged from trunk main addons branch
[odoo/odoo.git] / addons / fetchmail / fetchmail.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 ##############################################################################
4 #
5 #    OpenERP, Open Source Management Solution    
6 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
7 #    mga@tinyerp.com
8 #
9 #    This program is free software: you can redistribute it and/or modify
10 #    it under the terms of the GNU General Public License as published by
11 #    the Free Software Foundation, either version 3 of the License, or
12 #    (at your option) any later version.
13 #
14 #    This program is distributed in the hope that it will be useful,
15 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #    GNU General Public License for more details.
18 #
19 #    You should have received a copy of the GNU General Public License
20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 ##############################################################################
23
24 import os
25 import re
26 import time
27
28 import email
29 import binascii
30 import mimetypes
31
32 from imaplib import IMAP4
33 from imaplib import IMAP4_SSL   
34
35 from poplib import POP3
36 from poplib import POP3_SSL
37
38 from email.header import Header
39 from email.header import decode_header
40
41 import netsvc
42 from osv import osv
43 from osv import fields
44 from tools.translate import _
45
46 logger = netsvc.Logger()
47
48 def html2plaintext(html, body_id=None, encoding='utf-8'):
49     ## (c) Fry-IT, www.fry-it.com, 2007
50     ## <peter@fry-it.com>
51     ## download here: http://www.peterbe.com/plog/html2plaintext
52
53     """ from an HTML text, convert the HTML to plain text.
54     If @body_id is provided then this is the tag where the
55     body (not necessarily <body>) starts.
56     """
57     try:
58         from BeautifulSoup import BeautifulSoup, SoupStrainer, Comment
59     except:
60         return html
61
62     urls = []
63     if body_id is not None:
64         strainer = SoupStrainer(id=body_id)
65     else:
66         strainer = SoupStrainer('body')
67
68     soup = BeautifulSoup(html, parseOnlyThese=strainer, fromEncoding=encoding)
69     for link in soup.findAll('a'):
70         title = link.renderContents()
71         for url in [x[1] for x in link.attrs if x[0]=='href']:
72             urls.append(dict(url=url, tag=str(link), title=title))
73
74     html = soup.__str__()
75
76     url_index = []
77     i = 0
78     for d in urls:
79         if d['title'] == d['url'] or 'http://'+d['title'] == d['url']:
80             html = html.replace(d['tag'], d['url'])
81         else:
82             i += 1
83             html = html.replace(d['tag'], '%s [%s]' % (d['title'], i))
84             url_index.append(d['url'])
85
86     html = html.replace('<strong>','*').replace('</strong>','*')
87     html = html.replace('<b>','*').replace('</b>','*')
88     html = html.replace('<h3>','*').replace('</h3>','*')
89     html = html.replace('<h2>','**').replace('</h2>','**')
90     html = html.replace('<h1>','**').replace('</h1>','**')
91     html = html.replace('<em>','/').replace('</em>','/')
92
93     # the only line breaks we respect is those of ending tags and
94     # breaks
95
96     html = html.replace('\n',' ')
97     html = html.replace('<br>', '\n')
98     html = html.replace('<tr>', '\n')
99     html = html.replace('</p>', '\n\n')
100     html = re.sub('<br\s*/>', '\n', html)
101     html = html.replace(' ' * 2, ' ')
102
103     # for all other tags we failed to clean up, just remove then and
104     # complain about them on the stderr
105     def desperate_fixer(g):
106         #print >>sys.stderr, "failed to clean up %s" % str(g.group())
107         return ' '
108
109     html = re.sub('<.*?>', desperate_fixer, html)
110
111     # lstrip all lines
112     html = '\n'.join([x.lstrip() for x in html.splitlines()])
113
114     for i, url in enumerate(url_index):
115         if i == 0:
116             html += '\n\n'
117         html += '[%s] %s\n' % (i+1, url)
118     return html
119     
120 class email_server(osv.osv):
121     
122     _name = 'email.server'
123     _description = "POP/IMAP Server"
124     
125     _columns = {
126         'name':fields.char('Name', size=256, required=True, readonly=False),
127         'active':fields.boolean('Active', required=False),
128         'state':fields.selection([
129             ('draft','Not Confirmed'),
130             ('wating','Waiting for Verification'),
131             ('done','Confirmed'),
132         ],'State', select=True, readonly=True),
133         'server' : fields.char('Server', size=256, required=True, readonly=True, states={'draft':[('readonly',False)]}),
134         'port' : fields.integer('Port', required=True, readonly=True, states={'draft':[('readonly',False)]}),
135         'type':fields.selection([
136             ('pop','POP Server'),
137             ('imap','IMAP Server'),
138         ],'State', select=True, readonly=False),
139         'is_ssl':fields.boolean('SSL ?', required=False),
140         'attach':fields.boolean('Add Attachments ?', required=False),
141         'date': fields.date('Date', readonly=True, states={'draft':[('readonly',False)]}),
142         'user' : fields.char('User Name', size=256, required=True, readonly=True, states={'draft':[('readonly',False)]}),
143         'password' : fields.char('Password', size=1024, invisible=True, required=True, readonly=True, states={'draft':[('readonly',False)]}),
144         'note': fields.text('Description'),
145         'action_id':fields.many2one('ir.actions.server', 'Reply Email', required=False, domain="[('state','=','email')]"),
146         'object_id': fields.many2one('ir.model',"Model", required=True),
147         'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly',False)]}, help="Priority between 0 to 10, select define the order of Processing"),
148         'user_id':fields.many2one('res.users', 'User', required=False),
149     }
150     _defaults = {
151         'state': lambda *a: "draft",
152         'active': lambda *a: True,
153         'priority': lambda *a: 5,
154         'date': lambda *a: time.strftime('%Y-%m-%d'),
155         'user_id': lambda self, cr, uid, ctx: uid,
156     }
157     
158     def check_duplicate(self, cr, uid, ids):
159         vals = self.read(cr, uid, ids, ['user', 'password'])[0]
160         cr.execute("select count(id) from email_server where user='%s' and password='%s'" % (vals['user'], vals['password']))
161         res = cr.fetchone()
162         if res:
163             if res[0] > 1:
164                 return False
165         return True 
166
167     _constraints = [
168         (check_duplicate, 'Warning! Can\'t have duplicate server configuration!', ['user', 'password'])
169     ]
170     
171     def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
172         port = 0
173         if server_type == 'pop':
174             port = ssl and 995 or 110
175         elif server_type == 'imap':
176             port = ssl and 993 or 143
177         
178         return {'value':{'port':port}}
179     
180     def _process_email(self, cr, uid, server, message, context={}):
181         context.update({
182             'server_id':server.id
183         })
184         history_pool = self.pool.get('mail.server.history')
185         msg_txt = email.message_from_string(message)
186         message_id = msg_txt.get('Message-ID', False)
187
188         msg = {}
189         if not message_id:
190             return False
191         
192         fields = msg_txt.keys()
193         
194         msg['id'] = message_id
195         msg['message-id'] = message_id
196
197         def _decode_header(txt):
198             txt = txt.replace('\r', '')
199             return ' '.join(map(lambda (x, y): unicode(x, y or 'ascii'), decode_header(txt)))
200         
201         if 'Subject' in fields:
202             msg['subject'] = _decode_header(msg_txt.get('Subject'))
203         
204         if 'Content-Type' in fields:
205             msg['content-type'] = msg_txt.get('Content-Type')
206         
207         if 'From' in fields:
208             msg['from'] = _decode_header(msg_txt.get('From'))
209         
210         if 'Delivered-To' in fields:
211             msg['to'] = _decode_header(msg_txt.get('Delivered-To'))
212         
213         if 'Cc' in fields:
214             msg['cc'] = _decode_header(msg_txt.get('Cc'))
215         
216         if 'Reply-To' in fields:
217             msg['reply'] = _decode_header(msg_txt.get('Reply-To'))
218         
219         if 'Date' in fields:
220             msg['date'] = msg_txt.get('Date')
221         
222         if 'Content-Transfer-Encoding' in fields:
223             msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
224         
225         if 'References' in fields:
226             msg['references'] = msg_txt.get('References')
227
228         if 'X-openerp-caseid' in fields:
229             msg['caseid'] = msg_txt.get('X-openerp-caseid')
230         
231         if 'X-Priority' in fields:
232             msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0]
233         
234         if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', None):
235             encoding = msg_txt.get_content_charset()
236             msg['body'] = msg_txt.get_payload(decode=True)
237             if encoding:
238                 msg['body'] = msg['body'].decode(encoding).encode('utf-8')
239         
240         attachents = {}
241         if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', None):
242             body = ""
243             counter = 1
244             for part in msg_txt.walk():
245                 if part.get_content_maintype() == 'multipart':
246                     continue
247                 
248                 encoding = part.get_content_charset()
249
250                 if part.get_content_maintype()=='text':
251                     content = part.get_payload(decode=True)
252                     filename = part.get_filename()
253                     if filename :
254                         attachents[filename] = content
255                     else:
256                         if encoding:
257                             content = unicode(content, encoding)
258                         if part.get_content_subtype() == 'html':
259                             body = html2plaintext(content)
260                         elif part.get_content_subtype() == 'plain':
261                             body = content
262                 elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text':
263                     filename = part.get_filename();
264                     if filename :
265                         attachents[filename] = part.get_payload(decode=True)
266                     else:
267                         res = part.get_payload(decode=True)
268                         if encoding:
269                             res = res.decode(encoding).encode('utf-8')
270
271                         body += res
272
273             msg['body'] = body
274             msg['attachments'] = attachents
275
276
277         res_id = False
278         if msg.get('references', False):
279             id = False
280             ref = msg.get('references')
281             if '\r\n' in ref:
282                 ref = msg.get('references').split('\r\n')
283             else:
284                 ref = msg.get('references').split(' ')
285                 
286             if ref:
287                 hids = history_pool.search(cr, uid, [('name','=',ref[0].strip())])
288                 if hids:
289                     id = hids[0]
290                     history = history_pool.browse(cr, uid, id)
291                     model_pool = self.pool.get(server.object_id.model)
292                     context.update({
293                         'references_id':ref[0]
294                     })
295                     vals = {
296                     
297                     }
298                     if hasattr(model_pool, 'message_update'):
299                         model_pool.message_update(cr, uid, [history.res_id], vals, msg, context=context)
300                     else:
301                         logger.notifyChannel('imap', netsvc.LOG_WARNING, 'method def message_update is not define in model %s' % (model_pool._name))
302                         return False
303             res_id = id
304         else:
305             model_pool = self.pool.get(server.object_id.model)
306             if hasattr(model_pool, 'message_new'):
307                 res_id = model_pool.message_new(cr, uid, msg, context)
308             else:
309                 logger.notifyChannel('imap', netsvc.LOG_WARNING, 'method def message_new is not define in model %s' % (model_pool._name))
310                 return False
311             
312             if server.attach:
313                 for attactment in attachents or []:
314                     data_attach = {
315                         'name': attactment,
316                         'datas':binascii.b2a_base64(str(attachents.get(attactment))),
317                         'datas_fname': attactment,
318                         'description': 'Mail attachment',
319                         'res_model': server.object_id.model,
320                         'res_id': res_id,
321                     }
322                     self.pool.get('ir.attachment').create(cr, uid, data_attach)
323             
324             if server.action_id:
325                 action_pool = self.pool.get('ir.actions.server')
326                 action_pool.run(cr, uid, [server.action_id.id], {'active_id':res_id, 'active_ids':[res_id]})
327             
328             res = {
329                 'name': message_id, 
330                 'res_id': res_id, 
331                 'server_id': server.id, 
332                 'note': msg.get('body', msg.get('from')),
333                 'ref_id':msg.get('references', msg.get('id')),
334                 'type':server.type
335             }
336             his_id = history_pool.create(cr, uid, res)
337             
338         return res_id
339
340     def set_draft(self, cr, uid, ids, context={}):
341         self.write(cr, uid, ids , {'state':'draft'})
342         return True
343         
344     def button_fetch_mail(self, cr, uid, ids, context={}):
345         self.fetch_mail(cr, uid, ids)
346 #        sendmail_thread = threading.Thread(target=self.fetch_mail, args=(cr, uid, ids))
347 #        sendmail_thread.start()
348         return True
349         
350     def _fetch_mails(self, cr, uid, ids=False, context={}):
351         if not ids:
352             ids = self.search(cr, uid, [])
353         return self.fetch_mail(cr, uid, ids, context)
354     
355     def fetch_mail(self, cr, uid, ids, context={}):
356
357         for server in self.browse(cr, uid, ids, context):
358             logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail start checking for new emails on %s' % (server.name))
359             
360             count = 0
361             try:
362                 if server.type == 'imap':
363                     imap_server = None
364                     if server.is_ssl:
365                         imap_server = IMAP4_SSL(server.server, int(server.port))
366                     else:
367                         imap_server = IMAP4(server.server, int(server.port))
368                     
369                     imap_server.login(server.user, server.password)
370                     imap_server.select()
371                     result, data = imap_server.search(None, '(UNSEEN)')
372                     for num in data[0].split():
373                         result, data = imap_server.fetch(num, '(RFC822)')
374                         if self._process_email(cr, uid, server, data[0][1], context):
375                             imap_server.store(num, '+FLAGS', '\\Seen')
376                             count += 1
377                     logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch/process %s email(s) from %s' % (count, server.name))
378                     
379                     imap_server.close()
380                     imap_server.logout()
381                 elif server.type == 'pop':
382                     pop_server = None
383                     if server.is_ssl:
384                         pop_server = POP3_SSL(server.server, int(server.port))
385                     else:
386                         pop_server = POP3(server.server, int(server.port))
387                    
388                     #TODO: use this to remove only unread messages
389                     #pop_server.user("recent:"+server.user)
390                     pop_server.user(server.user)
391                     pop_server.pass_(server.password)
392                     pop_server.list()
393
394                     (numMsgs, totalSize) = pop_server.stat()
395                     for num in range(1, numMsgs + 1):
396                         (header, msges, octets) = pop_server.retr(num)
397                         msg = '\n'.join(msges)
398                         self._process_email(cr, uid, server, msg, context)
399                         pop_server.dele(num)
400
401                     pop_server.quit()
402                     
403                     logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch %s email(s) from %s' % (numMsgs, server.name))
404                 
405                 self.write(cr, uid, [server.id], {'state':'done'})
406             except Exception, e:
407                 logger.notifyChannel(server.type, netsvc.LOG_WARNING, '%s' % (e))
408                 
409         return True
410
411 email_server()
412
413 class mail_server_history(osv.osv):
414
415     _name = "mail.server.history"
416     _description = "Mail Server History"
417     
418     _columns = {
419         'name': fields.char('Message Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
420         'ref_id': fields.char('Referance Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
421         'res_id': fields.integer("Resource ID", readonly=True, select=True),
422         'server_id': fields.many2one('email.server',"Mail Server", readonly=True, select=True),
423         'model_id':fields.related('server_id', 'object_id', type='many2one', relation='ir.model', string='Model', readonly=True, select=True), 
424         'note': fields.text('Notes', readonly=True),
425         'create_date': fields.datetime('Created Date', readonly=True),
426         'type':fields.selection([
427             ('pop','POP Server'),
428             ('imap','IMAP Server'),
429         ],'State', select=True, readonly=True),
430     }
431     _order = 'id desc'
432     
433 mail_server_history()
434
435 class fetchmail_tool(osv.osv):
436
437     _name = 'email.server.tools'
438     _description = "Email Tools"
439     _auto = False
440     
441     def to_email(self, text):
442         _email = re.compile(r'.*<.*@.*\..*>', re.UNICODE)
443         def record(path):
444             eml = path.group()
445             index = eml.index('<')
446             eml = eml[index:-1].replace('<','').replace('>','')
447             return eml
448
449         bits = _email.sub(record, text)
450         return bits
451     
452     def get_partner(self, cr, uid, from_email, context=None):
453         """
454         @param self: The object pointer
455         @param cr: the current row, from the database cursor,
456         @param uid: the current user’s ID for security checks
457         @param from_email: email address based on that function will search for the correct 
458         """
459         
460         res = {
461             'partner_address_id': False,
462             'partner_id': False
463         }
464         from_email = self.to_email(from_email)
465         address_ids = self.pool.get('res.partner.address').search(cr, uid, [('email', '=', from_email)])
466         if address_ids:
467             address = self.pool.get('res.partner.address').browse(cr, uid, address_ids[0])
468             res['partner_address_id'] = address_ids[0]
469             res['partner_id'] = address.partner_id.id
470         
471         return res
472         
473 fetchmail_tool()