[MERGE] latest trunk
[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         'note': fields.text('Description'),
64         'action_id':fields.many2one('ir.actions.server', 'Server Action', help="Optional custom server action to trigger for each incoming mail, "
65                                                                                "on the record that was created or updated by this mail"),
66         'object_id': fields.many2one('ir.model', "Target document type", required=True, help="Process each incoming mail as part of a conversation "
67                                                                                              "corresponding to this document type. This will create "
68                                                                                              "new documents for new conversations, or attach follow-up "
69                                                                                              "emails to the existing conversations (documents)."),
70         'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly', False)]}, help="Defines the order of processing, "
71                                                                                                                   "lower values mean higher priority"),
72         'message_ids': fields.one2many('mail.message', 'fetchmail_server_id', 'Messages', readonly=True),
73     }
74     _defaults = {
75         'state': "draft",
76         'active': True,
77         'priority': 5,
78         'attach': True,
79     }
80
81     def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
82         port = 0
83         if server_type == 'pop':
84             port = ssl and 995 or 110
85         elif server_type == 'imap':
86             port = ssl and 993 or 143
87         return {'value':{'port':port}}
88
89     def set_draft(self, cr, uid, ids, context=None):
90         self.write(cr, uid, ids , {'state':'draft'})
91         return True
92
93     def connect(self, cr, uid, server_id, context=None):
94         if isinstance(server_id, (list,tuple)):
95             server_id = server_id[0]
96         server = self.browse(cr, uid, server_id, context)
97         if server.type == 'imap':
98             if server.is_ssl:
99                 connection = IMAP4_SSL(server.server, int(server.port))
100             else:
101                 connection = IMAP4(server.server, int(server.port))
102             connection.login(server.user, server.password)
103         elif server.type == 'pop':
104             if server.is_ssl:
105                 connection = POP3_SSL(server.server, int(server.port))
106             else:
107                 connection = POP3(server.server, int(server.port))
108             #TODO: use this to remove only unread messages
109             #connection.user("recent:"+server.user)
110             connection.user(server.user)
111             connection.pass_(server.password)
112         return connection
113
114     def button_confirm_login(self, cr, uid, ids, context=None):
115         if context is None:
116             context = {}
117         for server in self.browse(cr, uid, ids, context=context):
118             try:
119                 connection = server.connect()
120                 server.write({'state':'done'})
121             except Exception, e:
122                 logger.exception("Failed to connect to %s server %s", server.type, server.name)
123                 raise osv.except_osv(_("Connection test failed!"), _("Here is what we got instead:\n %s") % e)
124             finally:
125                 try:
126                     if connection:
127                         if server.type == 'imap':
128                             connection.close()
129                         elif server.type == 'pop':
130                             connection.quit()
131                 except Exception:
132                     # ignored, just a consequence of the previous exception
133                     pass
134         return True
135
136     def _fetch_mails(self, cr, uid, ids=False, context=None):
137         if not ids:
138             ids = self.search(cr, uid, [('state','=','done')])
139         return self.fetch_mail(cr, uid, ids, context=context)
140
141     def fetch_mail(self, cr, uid, ids, context=None):
142         """WARNING: meant for cron usage only - will commit() after each email!"""
143         if context is None:
144             context = {}
145         mail_thread = self.pool.get('mail.thread')
146         action_pool = self.pool.get('ir.actions.server')
147         for server in self.browse(cr, uid, ids, context=context):
148             logger.info('start checking for new emails on %s server %s', server.type, server.name)
149             context.update({'fetchmail_server_id': server.id, 'server_type': server.type})
150             count = 0
151             if server.type == 'imap':
152                 try:
153                     imap_server = server.connect()
154                     imap_server.select()
155                     result, data = imap_server.search(None, '(UNSEEN)')
156                     for num in data[0].split():
157                         result, data = imap_server.fetch(num, '(RFC822)')
158                         res_id = mail_thread.message_process(cr, uid, server.object_id.model, data[0][1],
159                                                              save_original=server.original,
160                                                              strip_attachments=(not server.attach),
161                                                              context=context)
162                         if res_id and server.action_id:
163                             action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id]})
164                             imap_server.store(num, '+FLAGS', '\\Seen')
165                             cr.commit()
166                         count += 1
167                     logger.info("fetched/processed %s email(s) on %s server %s", count, server.type, server.name)
168                 except Exception, e:
169                     logger.exception("Failed to fetch mail from %s server %s", server.type, server.name)
170                 finally:
171                     if imap_server:
172                         imap_server.close()
173                         imap_server.logout()
174             elif server.type == 'pop':
175                 try:
176                     pop_server = server.connect()
177                     (numMsgs, totalSize) = pop_server.stat()
178                     pop_server.list()
179                     for num in range(1, numMsgs + 1):
180                         (header, msges, octets) = pop_server.retr(num)
181                         msg = '\n'.join(msges)
182                         res_id = mail_thread.message_process(cr, uid, server.object_id.model,
183                                                              msg,
184                                                              save_original=server.original,
185                                                              strip_attachments=(not server.attach),
186                                                              context=context)
187                         if res_id and server.action_id:
188                             action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id]})
189                         pop_server.dele(num)
190                         cr.commit()
191                     logger.info("fetched/processed %s email(s) on %s server %s", numMsgs, server.type, server.name)
192                 except Exception, e:
193                     logger.exception("Failed to fetch mail from %s server %s", server.type, server.name)
194                 finally:
195                     if pop_server:
196                         pop_server.quit()
197             server.write({'date': time.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)})
198         return True
199
200 class mail_message(osv.osv):
201     _inherit = "mail.message"
202     _columns = {
203         'fetchmail_server_id': fields.many2one('fetchmail.server', "Inbound Mail Server",
204                                                readonly=True,
205                                                select=True,
206                                                oldname='server_id'),
207     }
208
209     def create(self, cr, uid, values, context=None):
210         if context is None:
211             context={}
212         fetchmail_server_id = context.get('fetchmail_server_id')
213         if fetchmail_server_id:
214             values['fetchmail_server_id'] = fetchmail_server_id
215         res = super(mail_message,self).create(cr, uid, values, context=context)
216         return res
217
218     def write(self, cr, uid, ids, values, context=None):
219         if context is None:
220             context={}
221         fetchmail_server_id = context.get('fetchmail_server_id')
222         if fetchmail_server_id:
223             values['fetchmail_server_id'] = server_id
224         res = super(mail_message,self).write(cr, uid, ids, values, context=context)
225         return res
226
227
228 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: