[ADD]: clean module to fetch email and process of it.
[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 email.header import Header
36 from email.header import decode_header
37
38 import netsvc
39 from osv import osv
40 from osv import fields
41 from tools.translate import _
42
43 command_re = re.compile("^Set-([a-z]+) *: *(.+)$", re.I + re.UNICODE)
44
45 logger = netsvc.Logger()
46
47 def html2plaintext(html, body_id=None, encoding='utf-8'):
48     ## (c) Fry-IT, www.fry-it.com, 2007
49     ## <peter@fry-it.com>
50     ## download here: http://www.peterbe.com/plog/html2plaintext
51
52     """ from an HTML text, convert the HTML to plain text.
53     If @body_id is provided then this is the tag where the
54     body (not necessarily <body>) starts.
55     """
56     try:
57         from BeautifulSoup import BeautifulSoup, SoupStrainer, Comment
58     except:
59         return html
60
61     urls = []
62     if body_id is not None:
63         strainer = SoupStrainer(id=body_id)
64     else:
65         strainer = SoupStrainer('body')
66
67     soup = BeautifulSoup(html, parseOnlyThese=strainer, fromEncoding=encoding)
68     for link in soup.findAll('a'):
69         title = link.renderContents()
70         for url in [x[1] for x in link.attrs if x[0]=='href']:
71             urls.append(dict(url=url, tag=str(link), title=title))
72
73     html = soup.__str__()
74
75     url_index = []
76     i = 0
77     for d in urls:
78         if d['title'] == d['url'] or 'http://'+d['title'] == d['url']:
79             html = html.replace(d['tag'], d['url'])
80         else:
81             i += 1
82             html = html.replace(d['tag'], '%s [%s]' % (d['title'], i))
83             url_index.append(d['url'])
84
85     html = html.replace('<strong>','*').replace('</strong>','*')
86     html = html.replace('<b>','*').replace('</b>','*')
87     html = html.replace('<h3>','*').replace('</h3>','*')
88     html = html.replace('<h2>','**').replace('</h2>','**')
89     html = html.replace('<h1>','**').replace('</h1>','**')
90     html = html.replace('<em>','/').replace('</em>','/')
91
92     # the only line breaks we respect is those of ending tags and
93     # breaks
94
95     html = html.replace('\n',' ')
96     html = html.replace('<br>', '\n')
97     html = html.replace('<tr>', '\n')
98     html = html.replace('</p>', '\n\n')
99     html = re.sub('<br\s*/>', '\n', html)
100     html = html.replace(' ' * 2, ' ')
101
102     # for all other tags we failed to clean up, just remove then and
103     # complain about them on the stderr
104     def desperate_fixer(g):
105         #print >>sys.stderr, "failed to clean up %s" % str(g.group())
106         return ' '
107
108     html = re.sub('<.*?>', desperate_fixer, html)
109
110     # lstrip all lines
111     html = '\n'.join([x.lstrip() for x in html.splitlines()])
112
113     for i, url in enumerate(url_index):
114         if i == 0:
115             html += '\n\n'
116         html += '[%s] %s\n' % (i+1, url)
117     return html
118     
119 class mail_server(osv.osv):
120     
121     _name = 'email.server'
122     _description = "POP/IMAP Server"
123     
124     _columns = {
125         'name':fields.char('Name', size=256, required=True, readonly=False),
126         'active':fields.boolean('Active', required=False),
127         'state':fields.selection([
128             ('draft','Not Confirme'),
129             ('wating','Waiting for Verification'),
130             ('done','Confirmed'),
131         ],'State', select=True, readonly=True),
132         'server' : fields.char('SMTP Server', size=256, required=True, readonly=True, states={'draft':[('readonly',False)]}),
133         'port' : fields.integer('SMTP Port', required=True, readonly=True, states={'draft':[('readonly',False)]}),
134         'type':fields.selection([
135             ('pop','POP Server'),
136             ('imap','IMAP Server'),
137         ],'State', select=True, readonly=True),
138         'is_ssl':fields.boolean('SSL ?', required=False),
139         'date': fields.date('Date'),
140         'user' : fields.char('User Name', size=256, required=True, readonly=True, states={'draft':[('readonly',False)]}),
141         'password' : fields.char('Password', size=1024, invisible=True, required=True, readonly=True, states={'draft':[('readonly',False)]}),
142         'note': fields.text('Description'),
143         'action_id':fields.many2one('ir.actions.server', 'Reply Email', required=False, domain="[('state','=','email')]"),
144         'object_id': fields.many2one('ir.model',"Model", required=True),
145         'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly',False)]}, help="Priority between 0 to 10, select define the order of Processing"),
146     }
147     _defaults = {
148         'type': lambda *a: "imap",
149         'state': lambda *a: "draft",
150         'active': lambda *a: True,
151         'priority': lambda *a: 5,
152         'date': lambda *a: time.strftime('%Y-%m-%d'),
153     }
154     
155     def check_duplicate(self, cr, uid, ids):
156         vals = self.read(cr, uid, ids, ['user', 'password'])[0]
157         cr.execute("select count(id) from email_server where user='%s' and password='%s'" % (vals['user'], vals['password']))
158         res = cr.fetchone()
159         if res:
160             if res[0] > 1:
161                 return False
162         return True 
163
164     _constraints = [
165         (check_duplicate, 'Warning! Can\'t have duplicate server configuration!', ['user', 'password'])
166     ]
167     
168     def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
169         port = 0
170         if server_type == 'pop':
171             port = ssl and 995 or 110
172         elif server_type == 'imap':
173             port = ssl and 993 or 143
174         
175         return {'value':{'port':port}}
176     
177     def _process_email(self, cr, uid, server, message, context={}):
178         history_pool = self.pool.get('mail.server.history')
179         msg_txt = email.message_from_string(message)
180         message_id = msg_txt.get('Message-ID', False)
181         
182         msg = {}
183         if not message_id:
184             return False
185         
186         fields = msg_txt.keys()
187         
188         msg['id'] = message_id
189         msg['message-id'] = message_id
190         
191         if 'Subject' in fields:
192             msg['subject'] = msg_txt.get('Subject')
193         
194         if 'Content-Type' in fields:
195             msg['content-type'] = msg_txt.get('Content-Type')
196         
197         if 'From' in fields:
198             msg['from'] = msg_txt.get('From')
199         
200         if 'Delivered-To' in fields:
201             msg['to'] = msg_txt.get('Delivered-To')
202         
203         if 'Cc' in fields:
204             msg['cc'] = msg_txt.get('Cc')
205         
206         if 'Reply-To' in fields:
207             msg['reply'] = msg_txt.get('Reply-To')
208         
209         if 'Date' in fields:
210             msg['date'] = msg_txt.get('Date')
211         
212         if 'Content-Transfer-Encoding' in fields:
213             msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
214         
215         if 'References' in fields:
216             msg['references'] = msg_txt.get('References')
217
218         if 'X-openerp-caseid' in fields:
219             msg['caseid'] = msg_txt.get('X-openerp-caseid')
220         
221         if 'X-Priority' in fields:
222             msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0]
223         
224         if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', None):
225             msg['body'] = msg_txt.get_payload(decode=True)
226         
227         attachents = {}
228         if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', None):
229             body = ""
230             counter = 1
231             for part in msg_txt.walk():
232                 if part.get_content_maintype() == 'multipart':
233                     continue
234                 
235                 if part.get_content_maintype()=='text':
236                     content = part.get_payload(decode=True)
237                     if part.get_content_subtype() == 'html':
238                         body = html2plaintext(content)
239                     elif part.get_content_subtype() == 'plain':
240                         body = content
241                     
242                     filename = part.get_filename()
243                     if filename :
244                         attachents[filename] = part.get_payload(decode=True)
245                     
246                 elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text':
247                     filename = part.get_filename();
248                     if filename :
249                         attachents[filename] = part.get_payload(decode=True)
250                     else:
251                         body += part.get_payload(decode=True)
252
253             msg['body'] = body
254             msg['attachments'] = attachents
255         
256         if msg.get('references', False):
257             id = False
258             ref = msg.get('references').split('\r\n')
259             if ref:
260                 hids = history_pool.search(cr, uid, [('name','=',ref[0])])
261                 if hids:
262                     id = hids[0]
263                     history = history_pool.browse(cr, uid, id)
264                     model_pool = self.pool.get(server.object_id.model)
265                     context.update({
266                         'message_id':ref[0]
267                     })
268                     model_pool.message_update(cr, uid, [history.res_id], msg, context=context)
269             res_id = id
270         else:
271             model_pool = self.pool.get(server.object_id.model)
272             res_id = model_pool.message_new(cr, uid, msg, context)
273
274             for attactment in attachents or []:
275                 data_attach = {
276                     'name': attactment,
277                     'datas':binascii.b2a_base64(str(attachents.get(attactment))),
278                     'datas_fname': attactment,
279                     'description': 'Mail attachment',
280                     'res_model': server.object_id.model,
281                     'res_id': res_id,
282                 }
283                 self.pool.get('ir.attachment').create(cr, uid, data_attach)
284             
285             if server.action_id:
286                 action_pool = self.pool.get('ir.actions.server')
287                 action_pool.run(cr, uid, [server.action_id.id], {'active_id':res_id, 'active_ids':[res_id]})
288             
289             res = {
290                 'name': message_id, 
291                 'res_id': res_id, 
292                 'server_id': server.id, 
293                 'note': msg.get('body', msg.get('from')),
294                 'ref_id':msg.get('references', msg.get('id')),
295                 'type':server.type
296             }
297             his_id = history_pool.create(cr, uid, res)
298             
299         return res_id
300     
301     def _fetch_mails(self, cr, uid, ids=False, context={}):
302         if not ids:
303             ids = self.search(cr, uid, [])
304         return self.fetch_mail(cr, uid, ids, context)
305     
306     def fetch_mail(self, cr, uid, ids, context={}):
307         fp = os.popen('ping www.google.com -c 1 -w 5',"r")
308         if not fp.read():
309             logger.notifyChannel('imap', netsvc.LOG_WARNING, 'No address associated with hostname !')
310         
311         for server in self.browse(cr, uid, ids, context):
312             logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail start checking for new emails on %s' % (server.name))
313             
314             try:
315                 if server.type == 'imap':
316                     imap_server = None
317                     if server.is_ssl:
318                         imap_server = IMAP4_SSL(server.server, int(server.port))
319                     else:
320                         imap_server = IMAP4(server.server, int(server.port))
321                     
322                     imap_server.login(server.user, server.password)
323                     imap_server.select()
324                     result, data = imap_server.search(None, '(UNSEEN)')
325                     count = 0
326                     for num in data[0].split():
327                         result, data = imap_server.fetch(num, '(RFC822)')
328                         self._process_email(cr, uid, server, data[0][1], context)
329                         imap_server.store(num, '+FLAGS', '\\Seen')
330                         count += 1
331                     logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch %s email(s) from %s' % (count, server.name))
332                     
333                     imap_server.close()
334                     imap_server.logout()
335             except Exception, e:
336                 logger.notifyChannel('IMAP', netsvc.LOG_WARNING, '%s' % (e))
337                 
338         return True
339                 
340 mail_server()
341
342 class mail_server_history(osv.osv):
343
344     _name = "mail.server.history"
345     _description = "Mail Server History"
346     
347     _columns = {
348         'name': fields.char('Message Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
349         'ref_id': fields.char('Referance Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
350         'res_id': fields.integer("Resource ID", readonly=True, select=True),
351         'server_id': fields.many2one('email.server',"Mail Server", readonly=True, select=True),
352         'model_id':fields.related('server_id', 'object_id', type='many2one', relation='ir.model', string='Model', readonly=True, select=True), 
353         'note': fields.text('Notes', readonly=True),
354         'create_date': fields.datetime('Created Date', readonly=True),
355         'type':fields.selection([
356             ('pop','POP Server'),
357             ('imap','IMAP Server'),
358         ],'State', select=True, readonly=True),
359     }
360     _order = 'id desc'
361     
362 mail_server_history()
363
364 class fetchmail_tool(osv.osv):
365     """
366     OpenERP Model : fetchmail_tool
367     """
368     
369     _name = 'email.server.tools'
370     _description = "Email Tools"
371     _auto = False
372     
373     def to_email(self, text):
374         _email = re.compile(r'.*<.*@.*\..*>', re.UNICODE)
375         def record(path):
376             eml = path.group()
377             index = eml.index('<')
378             eml = eml[index:-1].replace('<','').replace('>','')
379             return eml
380
381         bits = _email.sub(record, text)
382         return bits
383     
384     def get_partner(self, cr, uid, from_email, context=None):
385         """
386         @param self: The object pointer
387         @param cr: the current row, from the database cursor,
388         @param uid: the current user’s ID for security checks
389         @param from_email: email address based on that function will search for the correct 
390         """
391         
392         res = {
393             'partner_address_id': False,
394             'partner_id': False
395         }
396         from_email = self.to_email(from_email)
397         address_ids = self.pool.get('res.partner.address').search(cr, uid, [('email', '=', from_email)])
398         if address_ids:
399             address = self.pool.get('res.partner.address').browse(cr, uid, address_ids[0])
400             res['partner_address_id'] = address_ids[0]
401             res['partner_id'] = address.partner_id.id
402         
403         return res
404         
405 fetchmail_tool()