Launchpad automatic translations update.
[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 import addons
36
37 import netsvc
38 from osv import osv, fields
39 import tools
40 from tools.translate import _
41
42 logger = logging.getLogger('fetchmail')
43
44 class fetchmail_server(osv.osv):
45     """Incoming POP/IMAP mail server account"""
46     _name = 'fetchmail.server'
47     _description = "POP/IMAP Server"
48     _order = 'priority'
49
50     _columns = {
51         'name':fields.char('Name', size=256, required=True, readonly=False),
52         'active':fields.boolean('Active', required=False),
53         'state':fields.selection([
54             ('draft', 'Not Confirmed'),
55             ('done', 'Confirmed'),
56         ], 'State', select=True, readonly=True),
57         'server' : fields.char('Server Name', size=256, readonly=True, help="Hostname or IP of the mail server", states={'draft':[('readonly', False)]}),
58         'port' : fields.integer('Port', readonly=True, states={'draft':[('readonly', False)]}),
59         'type':fields.selection([
60             ('pop', 'POP Server'),
61             ('imap', 'IMAP Server'),
62             ('local', 'Local Server'),
63         ], 'Server Type', select=True, required=True, readonly=False),
64         'is_ssl':fields.boolean('SSL/TLS', help="Connections are encrypted with SSL/TLS through a dedicated port (default: IMAPS=993, POP3S=995)"),
65         'attach':fields.boolean('Keep Attachments', help="Whether attachments should be downloaded. "
66                                                          "If not enabled, incoming emails will be stripped of any attachments before being processed"),
67         'original':fields.boolean('Keep Original', help="Whether a full original copy of each email should be kept for reference"
68                                                         "and attached to each processed message. This will usually double the size of your message database."),
69         'date': fields.datetime('Last Fetch Date', readonly=True),
70         'user' : fields.char('Username', size=256, readonly=True, states={'draft':[('readonly', False)]}),
71         'password' : fields.char('Password', size=1024, readonly=True, states={'draft':[('readonly', False)]}),
72         'action_id':fields.many2one('ir.actions.server', 'Server Action', help="Optional custom server action to trigger for each incoming mail, "
73                                                                                "on the record that was created or updated by this mail"),
74         'object_id': fields.many2one('ir.model', "Create a New Record", required=True, help="Process each incoming mail as part of a conversation "
75                                                                                              "corresponding to this document type. This will create "
76                                                                                              "new documents for new conversations, or attach follow-up "
77                                                                                              "emails to the existing conversations (documents)."),
78         'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly', False)]}, help="Defines the order of processing, "
79                                                                                                                   "lower values mean higher priority"),
80         'message_ids': fields.one2many('mail.message', 'fetchmail_server_id', 'Messages', readonly=True),
81         'configuration' : fields.text('Configuration'),
82         'script' : fields.char('Script', readonly=True, size=64),
83     }
84     _defaults = {
85         'state': "draft",
86         'type': "pop",
87         'active': True,
88         'priority': 5,
89         'attach': True,
90         'script': '/mail/static/scripts/openerp_mailgate.py',
91     }
92
93
94     def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False, object_id=False):
95         port = 0
96         values = {}
97         if server_type == 'pop':
98             port = ssl and 995 or 110
99         elif server_type == 'imap':
100             port = ssl and 993 or 143
101         else:
102             values['server'] = ''
103         values['port'] = port
104
105         conf = {
106             'dbname' : cr.dbname,
107             'uid' : uid,
108             'model' : 'MODELNAME',
109         }
110         if object_id:
111             m = self.pool.get('ir.model')
112             r = m.read(cr,uid,[object_id],['model'])
113             conf['model']=r[0]['model']
114         values['configuration'] = """Use the below script with the following command line options with your Mail Transport Agent (MTA)
115
116 openerp_mailgate.py -u %(uid)d -p PASSWORD -o %(model)s -d %(dbname)s --host=HOSTNAME --port=PORT 
117 """ % conf
118
119         return {'value':values}
120
121     def set_draft(self, cr, uid, ids, context=None):
122         self.write(cr, uid, ids , {'state':'draft'})
123         return True
124
125     def connect(self, cr, uid, server_id, context=None):
126         if isinstance(server_id, (list,tuple)):
127             server_id = server_id[0]
128         server = self.browse(cr, uid, server_id, context)
129         if server.type == 'imap':
130             if server.is_ssl:
131                 connection = IMAP4_SSL(server.server, int(server.port))
132             else:
133                 connection = IMAP4(server.server, int(server.port))
134             connection.login(server.user, server.password)
135         elif server.type == 'pop':
136             if server.is_ssl:
137                 connection = POP3_SSL(server.server, int(server.port))
138             else:
139                 connection = POP3(server.server, int(server.port))
140             #TODO: use this to remove only unread messages
141             #connection.user("recent:"+server.user)
142             connection.user(server.user)
143             connection.pass_(server.password)
144         return connection
145
146     def button_confirm_login(self, cr, uid, ids, context=None):
147         if context is None:
148             context = {}
149         for server in self.browse(cr, uid, ids, context=context):
150             try:
151                 connection = server.connect()
152                 server.write({'state':'done'})
153             except Exception, e:
154                 logger.exception("Failed to connect to %s server %s", server.type, server.name)
155                 raise osv.except_osv(_("Connection test failed!"), _("Here is what we got instead:\n %s") % tools.ustr(e))
156             finally:
157                 try:
158                     if connection:
159                         if server.type == 'imap':
160                             connection.close()
161                         elif server.type == 'pop':
162                             connection.quit()
163                 except Exception:
164                     # ignored, just a consequence of the previous exception
165                     pass
166         return True
167
168     def _fetch_mails(self, cr, uid, ids=False, context=None):
169         if not ids:
170             ids = self.search(cr, uid, [('state','=','done')])
171         return self.fetch_mail(cr, uid, ids, context=context)
172
173     def fetch_mail(self, cr, uid, ids, context=None):
174         """WARNING: meant for cron usage only - will commit() after each email!"""
175         if context is None:
176             context = {}
177         mail_thread = self.pool.get('mail.thread')
178         action_pool = self.pool.get('ir.actions.server')
179         for server in self.browse(cr, uid, ids, context=context):
180             logger.info('start checking for new emails on %s server %s', server.type, server.name)
181             context.update({'fetchmail_server_id': server.id, 'server_type': server.type})
182             count = 0
183             if server.type == 'imap':
184                 try:
185                     imap_server = server.connect()
186                     imap_server.select()
187                     result, data = imap_server.search(None, '(UNSEEN)')
188                     for num in data[0].split():
189                         result, data = imap_server.fetch(num, '(RFC822)')
190                         res_id = mail_thread.message_process(cr, uid, server.object_id.model, data[0][1],
191                                                              save_original=server.original,
192                                                              strip_attachments=(not server.attach),
193                                                              context=context)
194                         if res_id and server.action_id:
195                             action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id]})
196                             imap_server.store(num, '+FLAGS', '\\Seen')
197                             cr.commit()
198                         count += 1
199                     logger.info("fetched/processed %s email(s) on %s server %s", count, server.type, server.name)
200                 except Exception, e:
201                     logger.exception("Failed to fetch mail from %s server %s", server.type, server.name)
202                 finally:
203                     if imap_server:
204                         imap_server.close()
205                         imap_server.logout()
206             elif server.type == 'pop':
207                 try:
208                     pop_server = server.connect()
209                     (numMsgs, totalSize) = pop_server.stat()
210                     pop_server.list()
211                     for num in range(1, numMsgs + 1):
212                         (header, msges, octets) = pop_server.retr(num)
213                         msg = '\n'.join(msges)
214                         res_id = mail_thread.message_process(cr, uid, server.object_id.model,
215                                                              msg,
216                                                              save_original=server.original,
217                                                              strip_attachments=(not server.attach),
218                                                              context=context)
219                         if res_id and server.action_id:
220                             action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id]})
221                         pop_server.dele(num)
222                         cr.commit()
223                     logger.info("fetched/processed %s email(s) on %s server %s", numMsgs, server.type, server.name)
224                 except Exception, e:
225                     logger.exception("Failed to fetch mail from %s server %s", server.type, server.name)
226                 finally:
227                     if pop_server:
228                         pop_server.quit()
229             server.write({'date': time.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)})
230         return True
231
232 class mail_message(osv.osv):
233     _inherit = "mail.message"
234     _columns = {
235         'fetchmail_server_id': fields.many2one('fetchmail.server', "Inbound Mail Server",
236                                                readonly=True,
237                                                select=True,
238                                                oldname='server_id'),
239     }
240
241     def create(self, cr, uid, values, context=None):
242         if context is None:
243             context={}
244         fetchmail_server_id = context.get('fetchmail_server_id')
245         if fetchmail_server_id:
246             values['fetchmail_server_id'] = fetchmail_server_id
247         res = super(mail_message,self).create(cr, uid, values, context=context)
248         return res
249
250     def write(self, cr, uid, ids, values, context=None):
251         if context is None:
252             context={}
253         fetchmail_server_id = context.get('fetchmail_server_id')
254         if fetchmail_server_id:
255             values['fetchmail_server_id'] = server_id
256         res = super(mail_message,self).write(cr, uid, ids, values, context=context)
257         return res
258
259
260 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: