[MERGE] forward port of branch 7.0 up to 65d92da
[odoo/odoo.git] / addons / fetchmail / fetchmail.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import logging
23 import time
24 from imaplib import IMAP4
25 from imaplib import IMAP4_SSL
26 from poplib import POP3
27 from poplib import POP3_SSL
28 try:
29     import cStringIO as StringIO
30 except ImportError:
31     import StringIO
32
33 import zipfile
34 import base64
35 from openerp import addons
36
37 from openerp.osv import fields, osv
38 from openerp import tools
39 from openerp.tools.translate import _
40
41 _logger = logging.getLogger(__name__)
42
43 class fetchmail_server(osv.osv):
44     """Incoming POP/IMAP mail server account"""
45     _name = 'fetchmail.server'
46     _description = "POP/IMAP Server"
47     _order = 'priority'
48
49     _columns = {
50         'name':fields.char('Name', size=256, required=True, readonly=False),
51         'active':fields.boolean('Active', required=False),
52         'state':fields.selection([
53             ('draft', 'Not Confirmed'),
54             ('done', 'Confirmed'),
55         ], 'Status', select=True, readonly=True),
56         'server' : fields.char('Server Name', size=256, readonly=True, help="Hostname or IP of the mail server", states={'draft':[('readonly', False)]}),
57         'port' : fields.integer('Port', readonly=True, states={'draft':[('readonly', False)]}),
58         'type':fields.selection([
59             ('pop', 'POP Server'),
60             ('imap', 'IMAP Server'),
61             ('local', 'Local Server'),
62         ], 'Server Type', select=True, required=True, readonly=False),
63         'is_ssl':fields.boolean('SSL/TLS', help="Connections are encrypted with SSL/TLS through a dedicated port (default: IMAPS=993, POP3S=995)"),
64         'attach':fields.boolean('Keep Attachments', help="Whether attachments should be downloaded. "
65                                                          "If not enabled, incoming emails will be stripped of any attachments before being processed"),
66         'original':fields.boolean('Keep Original', help="Whether a full original copy of each email should be kept for reference"
67                                                         "and attached to each processed message. This will usually double the size of your message database."),
68         'date': fields.datetime('Last Fetch Date', readonly=True),
69         'user' : fields.char('Username', size=256, readonly=True, states={'draft':[('readonly', False)]}),
70         'password' : fields.char('Password', size=1024, readonly=True, states={'draft':[('readonly', False)]}),
71         'action_id':fields.many2one('ir.actions.server', 'Server Action', help="Optional custom server action to trigger for each incoming mail, "
72                                                                                "on the record that was created or updated by this mail"),
73         'object_id': fields.many2one('ir.model', "Create a New Record", help="Process each incoming mail as part of a conversation "
74                                                                                              "corresponding to this document type. This will create "
75                                                                                              "new documents for new conversations, or attach follow-up "
76                                                                                              "emails to the existing conversations (documents)."),
77         'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly', False)]}, help="Defines the order of processing, "
78                                                                                                                   "lower values mean higher priority"),
79         'message_ids': fields.one2many('mail.mail', 'fetchmail_server_id', 'Messages', readonly=True),
80         'configuration' : fields.text('Configuration', readonly=True),
81         'script' : fields.char('Script', readonly=True, size=64),
82     }
83     _defaults = {
84         'state': "draft",
85         'type': "pop",
86         'active': True,
87         'priority': 5,
88         'attach': True,
89         'script': '/mail/static/scripts/openerp_mailgate.py',
90     }
91
92     def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False, object_id=False):
93         port = 0
94         values = {}
95         if server_type == 'pop':
96             port = ssl and 995 or 110
97         elif server_type == 'imap':
98             port = ssl and 993 or 143
99         else:
100             values['server'] = ''
101         values['port'] = port
102
103         conf = {
104             'dbname' : cr.dbname,
105             'uid' : uid,
106             'model' : 'MODELNAME',
107         }
108         if object_id:
109             m = self.pool.get('ir.model')
110             r = m.read(cr,uid,[object_id],['model'])
111             conf['model']=r[0]['model']
112         values['configuration'] = """Use the below script with the following command line options with your Mail Transport Agent (MTA)
113
114 openerp_mailgate.py --host=HOSTNAME --port=PORT -u %(uid)d -p PASSWORD -d %(dbname)s
115
116 Example configuration for the postfix mta running locally:
117
118 /etc/postfix/virtual_aliases:
119 @youdomain openerp_mailgate@localhost
120
121 /etc/aliases:
122 openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p PASSWORD -d %(dbname)s"
123
124 """ % conf
125
126         return {'value':values}
127
128     def set_draft(self, cr, uid, ids, context=None):
129         self.write(cr, uid, ids , {'state':'draft'})
130         return True
131
132     def connect(self, cr, uid, server_id, context=None):
133         if isinstance(server_id, (list,tuple)):
134             server_id = server_id[0]
135         server = self.browse(cr, uid, server_id, context)
136         if server.type == 'imap':
137             if server.is_ssl:
138                 connection = IMAP4_SSL(server.server, int(server.port))
139             else:
140                 connection = IMAP4(server.server, int(server.port))
141             connection.login(server.user, server.password)
142         elif server.type == 'pop':
143             if server.is_ssl:
144                 connection = POP3_SSL(server.server, int(server.port))
145             else:
146                 connection = POP3(server.server, int(server.port))
147             #TODO: use this to remove only unread messages
148             #connection.user("recent:"+server.user)
149             connection.user(server.user)
150             connection.pass_(server.password)
151         return connection
152
153     def button_confirm_login(self, cr, uid, ids, context=None):
154         if context is None:
155             context = {}
156         for server in self.browse(cr, uid, ids, context=context):
157             try:
158                 connection = server.connect()
159                 server.write({'state':'done'})
160             except Exception, e:
161                 _logger.exception("Failed to connect to %s server %s.", server.type, server.name)
162                 raise osv.except_osv(_("Connection test failed!"), _("Here is what we got instead:\n %s.") % tools.ustr(e))
163             finally:
164                 try:
165                     if connection:
166                         if server.type == 'imap':
167                             connection.close()
168                         elif server.type == 'pop':
169                             connection.quit()
170                 except Exception:
171                     # ignored, just a consequence of the previous exception
172                     pass
173         return True
174
175     def _fetch_mails(self, cr, uid, ids=False, context=None):
176         if not ids:
177             ids = self.search(cr, uid, [('state','=','done'),('type','in',['pop','imap'])])
178         return self.fetch_mail(cr, uid, ids, context=context)
179
180     def fetch_mail(self, cr, uid, ids, context=None):
181         """WARNING: meant for cron usage only - will commit() after each email!"""
182         if context is None:
183             context = {}
184         context['fetchmail_cron_running'] = True
185         mail_thread = self.pool.get('mail.thread')
186         action_pool = self.pool.get('ir.actions.server')
187         for server in self.browse(cr, uid, ids, context=context):
188             _logger.info('start checking for new emails on %s server %s', server.type, server.name)
189             context.update({'fetchmail_server_id': server.id, 'server_type': server.type})
190             count, failed = 0, 0
191             imap_server = False
192             pop_server = False
193             if server.type == 'imap':
194                 try:
195                     imap_server = server.connect()
196                     imap_server.select()
197                     result, data = imap_server.search(None, '(UNSEEN)')
198                     for num in data[0].split():
199                         res_id = None
200                         result, data = imap_server.fetch(num, '(RFC822)')
201                         imap_server.store(num, '-FLAGS', '\\Seen')
202                         try:
203                             res_id = mail_thread.message_process(cr, uid, server.object_id.model,
204                                                                  data[0][1],
205                                                                  save_original=server.original,
206                                                                  strip_attachments=(not server.attach),
207                                                                  context=context)
208                         except Exception:
209                             _logger.exception('Failed to process mail from %s server %s.', server.type, server.name)
210                             failed += 1
211                         if res_id and server.action_id:
212                             action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids': [res_id], 'active_model': context.get("thread_model", server.object_id.model)})
213                         imap_server.store(num, '+FLAGS', '\\Seen')
214                         cr.commit()
215                         count += 1
216                     _logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", count, server.type, server.name, (count - failed), failed)
217                 except Exception:
218                     _logger.exception("General failure when trying to fetch mail from %s server %s.", server.type, server.name)
219                 finally:
220                     if imap_server:
221                         imap_server.close()
222                         imap_server.logout()
223             elif server.type == 'pop':
224                 try:
225                     pop_server = server.connect()
226                     (numMsgs, totalSize) = pop_server.stat()
227                     pop_server.list()
228                     for num in range(1, numMsgs + 1):
229                         (header, msges, octets) = pop_server.retr(num)
230                         msg = '\n'.join(msges)
231                         res_id = None
232                         try:
233                             res_id = mail_thread.message_process(cr, uid, server.object_id.model,
234                                                                  msg,
235                                                                  save_original=server.original,
236                                                                  strip_attachments=(not server.attach),
237                                                                  context=context)
238                             pop_server.dele(num)
239                         except Exception:
240                             _logger.exception('Failed to process mail from %s server %s.', server.type, server.name)
241                             failed += 1
242                         if res_id and server.action_id:
243                             action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids': [res_id], 'active_model': context.get("thread_model", server.object_id.model)})
244                         cr.commit()
245                     _logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", numMsgs, server.type, server.name, (numMsgs - failed), failed)
246                 except Exception:
247                     _logger.exception("General failure when trying to fetch mail from %s server %s.", server.type, server.name)
248                 finally:
249                     if pop_server:
250                         pop_server.quit()
251             server.write({'date': time.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)})
252         return True
253
254     def _update_cron(self, cr, uid, context=None):
255         if context and context.get('fetchmail_cron_running'):
256             return
257
258         try:
259             cron = self.pool['ir.model.data'].get_object(
260                 cr, uid, 'fetchmail', 'ir_cron_mail_gateway_action', context=context)
261         except ValueError:
262             # Nevermind if default cron cannot be found
263             return
264
265         # Enabled/Disable cron based on the number of 'done' server of type pop or imap
266         cron.toggle(model=self._name, domain=[('state','=','done'), ('type','in',['pop','imap'])])
267
268     def create(self, cr, uid, values, context=None):
269         res = super(fetchmail_server, self).create(cr, uid, values, context=context)
270         self._update_cron(cr, uid, context=context)
271         return res
272
273     def write(self, cr, uid, ids, values, context=None):
274         res = super(fetchmail_server, self).write(cr, uid, ids, values, context=context)
275         self._update_cron(cr, uid, context=context)
276         return res
277
278     def unlink(self, cr, uid, ids, context=None):
279         res = super(fetchmail_server, self).unlink(cr, uid, ids, context=context)
280         self._update_cron(cr, uid, context=context)
281         return res
282
283 class mail_mail(osv.osv):
284     _inherit = "mail.mail"
285     _columns = {
286         'fetchmail_server_id': fields.many2one('fetchmail.server', "Inbound Mail Server",
287                                                readonly=True,
288                                                select=True,
289                                                oldname='server_id'),
290     }
291
292     def create(self, cr, uid, values, context=None):
293         if context is None:
294             context = {}
295         fetchmail_server_id = context.get('fetchmail_server_id')
296         if fetchmail_server_id:
297             values['fetchmail_server_id'] = fetchmail_server_id
298         res = super(mail_mail, self).create(cr, uid, values, context=context)
299         return res
300
301     def write(self, cr, uid, ids, values, context=None):
302         if context is None:
303             context = {}
304         fetchmail_server_id = context.get('fetchmail_server_id')
305         if fetchmail_server_id:
306             values['fetchmail_server_id'] = fetchmail_server_id
307         res = super(mail_mail, self).write(cr, uid, ids, values, context=context)
308         return res
309
310
311 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: