3 ##############################################################################
5 # OpenERP, Open Source Management Solution
6 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
22 ##############################################################################
32 from imaplib import IMAP4
33 from imaplib import IMAP4_SSL
35 from poplib import POP3
36 from poplib import POP3_SSL
38 from email.header import Header
39 from email.header import decode_header
43 from osv import fields
44 from tools.translate import _
46 logger = netsvc.Logger()
48 def html2plaintext(html, body_id=None, encoding='utf-8'):
49 ## (c) Fry-IT, www.fry-it.com, 2007
51 ## download here: http://www.peterbe.com/plog/html2plaintext
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.
58 from BeautifulSoup import BeautifulSoup, SoupStrainer, Comment
63 if body_id is not None:
64 strainer = SoupStrainer(id=body_id)
66 strainer = SoupStrainer('body')
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))
79 if d['title'] == d['url'] or 'http://'+d['title'] == d['url']:
80 html = html.replace(d['tag'], d['url'])
83 html = html.replace(d['tag'], '%s [%s]' % (d['title'], i))
84 url_index.append(d['url'])
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>','/')
93 # the only line breaks we respect is those of ending tags and
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, ' ')
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())
109 html = re.sub('<.*?>', desperate_fixer, html)
112 html = '\n'.join([x.lstrip() for x in html.splitlines()])
114 for i, url in enumerate(url_index):
117 html += '[%s] %s\n' % (i+1, url)
120 class mail_server(osv.osv):
122 _name = 'email.server'
123 _description = "POP/IMAP Server"
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 Confirme'),
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 'date': fields.date('Date'),
141 'user' : fields.char('User Name', size=256, required=True, readonly=True, states={'draft':[('readonly',False)]}),
142 'password' : fields.char('Password', size=1024, invisible=True, required=True, readonly=True, states={'draft':[('readonly',False)]}),
143 'note': fields.text('Description'),
144 'action_id':fields.many2one('ir.actions.server', 'Reply Email', required=False, domain="[('state','=','email')]"),
145 'object_id': fields.many2one('ir.model',"Model", required=True),
146 'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly',False)]}, help="Priority between 0 to 10, select define the order of Processing"),
149 # 'type': lambda *a: "imap",
150 'state': lambda *a: "draft",
151 'active': lambda *a: True,
152 'priority': lambda *a: 5,
153 'date': lambda *a: time.strftime('%Y-%m-%d'),
156 def check_duplicate(self, cr, uid, ids):
157 vals = self.read(cr, uid, ids, ['user', 'password'])[0]
158 cr.execute("select count(id) from email_server where user='%s' and password='%s'" % (vals['user'], vals['password']))
166 (check_duplicate, 'Warning! Can\'t have duplicate server configuration!', ['user', 'password'])
169 def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
171 if server_type == 'pop':
172 port = ssl and 995 or 110
173 elif server_type == 'imap':
174 port = ssl and 993 or 143
176 return {'value':{'port':port}}
178 def _process_email(self, cr, uid, server, message, context={}):
180 'server_id':server.id
182 history_pool = self.pool.get('mail.server.history')
183 msg_txt = email.message_from_string(message)
184 message_id = msg_txt.get('Message-ID', False)
190 fields = msg_txt.keys()
192 msg['id'] = message_id
193 msg['message-id'] = message_id
195 if 'Subject' in fields:
196 msg['subject'] = msg_txt.get('Subject')
198 if 'Content-Type' in fields:
199 msg['content-type'] = msg_txt.get('Content-Type')
202 msg['from'] = msg_txt.get('From')
204 if 'Delivered-To' in fields:
205 msg['to'] = msg_txt.get('Delivered-To')
208 msg['cc'] = msg_txt.get('Cc')
210 if 'Reply-To' in fields:
211 msg['reply'] = msg_txt.get('Reply-To')
214 msg['date'] = msg_txt.get('Date')
216 if 'Content-Transfer-Encoding' in fields:
217 msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
219 if 'References' in fields:
220 msg['references'] = msg_txt.get('References')
222 if 'X-openerp-caseid' in fields:
223 msg['caseid'] = msg_txt.get('X-openerp-caseid')
225 if 'X-Priority' in fields:
226 msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0]
228 if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', None):
229 msg['body'] = msg_txt.get_payload(decode=True)
232 if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', None):
235 for part in msg_txt.walk():
236 if part.get_content_maintype() == 'multipart':
239 if part.get_content_maintype()=='text':
240 content = part.get_payload(decode=True)
241 if part.get_content_subtype() == 'html':
242 body = html2plaintext(content)
243 elif part.get_content_subtype() == 'plain':
246 filename = part.get_filename()
248 attachents[filename] = part.get_payload(decode=True)
250 elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text':
251 filename = part.get_filename();
253 attachents[filename] = part.get_payload(decode=True)
255 body += part.get_payload(decode=True)
258 msg['attachments'] = attachents
261 if msg.get('references', False):
263 ref = msg.get('references')
265 ref = msg.get('references').split('\r\n')
267 ref = msg.get('references').split(' ')
270 hids = history_pool.search(cr, uid, [('name','=',ref[0].strip())])
273 history = history_pool.browse(cr, uid, id)
274 model_pool = self.pool.get(server.object_id.model)
276 'references_id':ref[0]
281 if hasattr(model_pool, 'message_update'):
282 model_pool.message_update(cr, uid, [history.res_id], vals, msg, context=context)
284 logger.notifyChannel('imap', netsvc.LOG_WARNING, 'method def message_update is not define in model %s' % (model_pool._name))
288 model_pool = self.pool.get(server.object_id.model)
289 if hasattr(model_pool, 'message_new'):
290 res_id = model_pool.message_new(cr, uid, msg, context)
292 logger.notifyChannel('imap', netsvc.LOG_WARNING, 'method def message_new is not define in model %s' % (model_pool._name))
295 # for attactment in attachents or []:
297 # 'name': attactment,
298 # 'datas':binascii.b2a_base64(str(attachents.get(attactment))),
299 # 'datas_fname': attactment,
300 # 'description': 'Mail attachment',
301 # 'res_model': server.object_id.model,
304 # self.pool.get('ir.attachment').create(cr, uid, data_attach)
307 action_pool = self.pool.get('ir.actions.server')
308 action_pool.run(cr, uid, [server.action_id.id], {'active_id':res_id, 'active_ids':[res_id]})
313 'server_id': server.id,
314 'note': msg.get('body', msg.get('from')),
315 'ref_id':msg.get('references', msg.get('id')),
318 his_id = history_pool.create(cr, uid, res)
322 def _fetch_mails(self, cr, uid, ids=False, context={}):
324 ids = self.search(cr, uid, [])
325 return self.fetch_mail(cr, uid, ids, context)
327 def fetch_mail(self, cr, uid, ids, context={}):
328 fp = os.popen('ping www.google.com -c 1 -w 5',"r")
330 logger.notifyChannel('imap', netsvc.LOG_WARNING, 'No address associated with hostname !')
332 for server in self.browse(cr, uid, ids, context):
333 logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail start checking for new emails on %s' % (server.name))
337 if server.type == 'imap':
340 imap_server = IMAP4_SSL(server.server, int(server.port))
342 imap_server = IMAP4(server.server, int(server.port))
344 imap_server.login(server.user, server.password)
346 result, data = imap_server.search(None, '(UNSEEN)')
347 for num in data[0].split():
348 result, data = imap_server.fetch(num, '(RFC822)')
349 if self._process_email(cr, uid, server, data[0][1], context):
350 imap_server.store(num, '+FLAGS', '\\Seen')
352 logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch/process %s email(s) from %s' % (count, server.name))
356 elif server.type == 'pop':
359 pop_server = POP3_SSL(server.server, int(server.port))
361 pop_server = POP3(server.server, int(server.port))
363 #TODO: use this to remove only unread messages
364 #pop_server.user("recent:"+server.user)
365 pop_server.user(server.user)
366 pop_server.pass_(server.password)
369 (numMsgs, totalSize) = pop_server.stat()
370 for num in range(1, numMsgs + 1):
371 (header, msges, octets) = pop_server.retr(num)
372 msg = '\n'.join(msges)
373 self._process_email(cr, uid, server, msg, context)
378 logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch %s email(s) from %s' % (numMsgs, server.name))
380 # except Exception, e:
381 # logger.notifyChannel('IMAP', netsvc.LOG_WARNING, '%s' % (e))
387 class mail_server_history(osv.osv):
389 _name = "mail.server.history"
390 _description = "Mail Server History"
393 'name': fields.char('Message Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
394 'ref_id': fields.char('Referance Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
395 'res_id': fields.integer("Resource ID", readonly=True, select=True),
396 'server_id': fields.many2one('email.server',"Mail Server", readonly=True, select=True),
397 'model_id':fields.related('server_id', 'object_id', type='many2one', relation='ir.model', string='Model', readonly=True, select=True),
398 'note': fields.text('Notes', readonly=True),
399 'create_date': fields.datetime('Created Date', readonly=True),
400 'type':fields.selection([
401 ('pop','POP Server'),
402 ('imap','IMAP Server'),
403 ],'State', select=True, readonly=True),
407 mail_server_history()
409 class fetchmail_tool(osv.osv):
411 _name = 'email.server.tools'
412 _description = "Email Tools"
415 def to_email(self, text):
416 _email = re.compile(r'.*<.*@.*\..*>', re.UNICODE)
419 index = eml.index('<')
420 eml = eml[index:-1].replace('<','').replace('>','')
423 bits = _email.sub(record, text)
426 def get_partner(self, cr, uid, from_email, context=None):
428 @param self: The object pointer
429 @param cr: the current row, from the database cursor,
430 @param uid: the current user’s ID for security checks
431 @param from_email: email address based on that function will search for the correct
435 'partner_address_id': False,
438 from_email = self.to_email(from_email)
439 address_ids = self.pool.get('res.partner.address').search(cr, uid, [('email', '=', from_email)])
441 address = self.pool.get('res.partner.address').browse(cr, uid, address_ids[0])
442 res['partner_address_id'] = address_ids[0]
443 res['partner_id'] = address.partner_id.id