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