[IMP] Changed all module categories, limited number of 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 time
23
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
33 logger = netsvc.Logger()
34
35
36 class email_server(osv.osv):
37
38     _name = 'email.server'
39     _description = "POP/IMAP Server"
40
41     _columns = {
42         'name':fields.char('Name', size=256, required=True, readonly=False),
43         'active':fields.boolean('Active', required=False),
44         'state':fields.selection([
45             ('draft', 'Not Confirmed'),
46             ('waiting', 'Waiting for Verification'),
47             ('done', 'Confirmed'),
48         ], 'State', select=True, readonly=True),
49         'server' : fields.char('Server', size=256, required=True, readonly=True, 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, readonly=False),
55         'is_ssl':fields.boolean('SSL ?', required=False),
56         'attach':fields.boolean('Add Attachments ?', required=False, help="Fetches mail with attachments if true."),
57         'date': fields.date('Date', readonly=True, states={'draft':[('readonly', False)]}),
58         'user' : fields.char('User Name', size=256, required=True, readonly=True, states={'draft':[('readonly', False)]}),
59         'password' : fields.char('Password', size=1024, invisible=True, required=True, readonly=True, states={'draft':[('readonly', False)]}),
60         'note': fields.text('Description'),
61         'action_id':fields.many2one('ir.actions.server', 'Email Server Action', required=False, domain="[('state','=','email')]", help="An Email Server Action. It will be run whenever an e-mail is fetched from server."),
62         'object_id': fields.many2one('ir.model', "Model", required=True, help="OpenObject Model. Generates a record of this model.\nSelect Object with message_new attrbutes."),
63         'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly', False)]}, help="Priority between 0 to 10, select define the order of Processing"),
64         'user_id':fields.many2one('res.users', 'User', required=False),
65         'message_ids': fields.one2many('mailgate.message', 'server_id', 'Messages', readonly=True),
66     }
67     _defaults = {
68         'state': lambda *a: "draft",
69         'active': lambda *a: True,
70         'priority': lambda *a: 5,
71         'date': lambda *a: time.strftime('%Y-%m-%d'),
72         'user_id': lambda self, cr, uid, ctx: uid,
73     }
74
75     def check_duplicate(self, cr, uid, ids, context=None):
76         # RFC *-* Why this limitation? why not in SQL constraint?
77         vals = self.read(cr, uid, ids, ['user', 'password'], context=context)[0]
78         cr.execute("select count(id) from email_server where user=%s and password=%s", (vals['user'], vals['password']))
79         res = cr.fetchone()
80         if res:
81             if res[0] > 1:
82                 return False
83         return True
84
85     def check_model(self, cr, uid, ids, context = None):
86         if context is None:
87             context = {}
88         current_rec = self.read(cr, uid, ids, context)
89         if current_rec:
90             current_rec = current_rec[0]
91             model_name = self.pool.get('ir.model').browse(cr, uid, current_rec.get('object_id')[0]).model
92             model = self.pool.get(model_name)
93             if hasattr(model, 'message_new'):
94                 return True
95         return False
96
97     _constraints = [
98         (check_duplicate, 'Warning! Can\'t have duplicate server configuration!', ['user', 'password']),
99         (check_model, 'Warning! Record for selected Model can not be created\nPlease choose valid Model', ['object_id'])
100     ]
101
102     def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
103         port = 0
104         if server_type == 'pop':
105             port = ssl and 995 or 110
106         elif server_type == 'imap':
107             port = ssl and 993 or 143
108
109         return {'value':{'port':port}}
110
111     def set_draft(self, cr, uid, ids, context=None):
112         self.write(cr, uid, ids , {'state':'draft'})
113         return True
114     
115     def button_confirm_login(self, cr, uid, ids, context=None):
116         if context is None:
117             context = {}
118         for server in self.browse(cr, uid, ids, context=context):
119             logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail start checking for new emails on %s' % (server.name))
120             context.update({'server_id': server.id, 'server_type': server.type})
121             try:
122                 if server.type == 'imap':
123                     imap_server = None
124                     if server.is_ssl:
125                         imap_server = IMAP4_SSL(server.server, int(server.port))
126                     else:
127                         imap_server = IMAP4(server.server, int(server.port))
128
129                     imap_server.login(server.user, server.password)
130                     ret_server = imap_server
131                     
132                 elif server.type == 'pop':
133                     pop_server = None
134                     if server.is_ssl:
135                         pop_server = POP3_SSL(server.server, int(server.port))
136                     else:
137                         pop_server = POP3(server.server, int(server.port))
138
139                     #TODO: use this to remove only unread messages
140                     #pop_server.user("recent:"+server.user)
141                     pop_server.user(server.user)
142                     pop_server.pass_(server.password)
143                     ret_server = pop_server
144                     
145                 self.write(cr, uid, [server.id], {'state':'done'})
146                 if context.get('get_server',False):
147                     return ret_server
148             except Exception, e:
149                 logger.notifyChannel(server.type, netsvc.LOG_WARNING, '%s' % (e))
150         return True
151
152     def button_fetch_mail(self, cr, uid, ids, context=None):
153         self.fetch_mail(cr, uid, ids, context=context)
154         return True
155
156     def _fetch_mails(self, cr, uid, ids=False, context=None):
157         if not ids:
158             ids = self.search(cr, uid, [])
159         return self.fetch_mail(cr, uid, ids, context=context)
160
161     def fetch_mail(self, cr, uid, ids, context=None):
162         if context is None:
163             context = {}
164         email_tool = self.pool.get('email.server.tools')
165         action_pool = self.pool.get('ir.actions.server')
166         context.update({'get_server': True})
167         for server in self.browse(cr, uid, ids, context=context):
168             count = 0
169             user = server.user_id.id or uid
170             try:
171                 if server.type == 'imap':
172                     imap_server = self.button_confirm_login(cr, uid, [server.id], context=context)
173                     imap_server.select()
174                     result, data = imap_server.search(None, '(UNSEEN)')
175                     for num in data[0].split():
176                         result, data = imap_server.fetch(num, '(RFC822)')
177                         res_id = email_tool.process_email(cr, user, server.object_id.model, data[0][1], attach=server.attach, context=context)
178                         if res_id and server.action_id:
179                             action_pool.run(cr, user, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id]})
180
181                             imap_server.store(num, '+FLAGS', '\\Seen')
182                         count += 1
183                     logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch/process %s email(s) from %s' % (count, server.name))
184
185                     imap_server.close()
186                     imap_server.logout()
187                 elif server.type == 'pop':
188                     pop_server = self.button_confirm_login(cr, uid, [server.id], context=context)
189                     pop_server.list()
190                     (numMsgs, totalSize) = pop_server.stat()
191                     for num in range(1, numMsgs + 1):
192                         (header, msges, octets) = pop_server.retr(num)
193                         msg = '\n'.join(msges)
194                         res_id = email_tool.process_email(cr, user, server.object_id.model, msg, attach=server.attach, context=context)
195                         if res_id and server.action_id:
196                             action_pool.run(cr, user, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id]})
197
198                         pop_server.dele(num)
199
200                     pop_server.quit()
201
202                     logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch %s email(s) from %s' % (numMsgs, server.name))
203
204             except Exception, e:
205                 logger.notifyChannel(server.type, netsvc.LOG_WARNING, '%s' % (tools.ustr(e)))
206
207         return True
208
209 email_server()
210
211 class mailgate_message(osv.osv):
212
213     _inherit = "mailgate.message"
214
215     _columns = {
216         'server_id': fields.many2one('email.server', "Mail Server", readonly=True, select=True),
217         'server_type':fields.selection([
218             ('pop', 'POP Server'),
219             ('imap', 'IMAP Server'),
220         ], 'Server Type', select=True, readonly=True),
221     }
222     _order = 'id desc'
223
224     def create(self, cr, uid, values, context=None):
225         if context is None:
226             context={}
227         server_id = context.get('server_id',False)
228         server_type = context.get('server_type',False)
229         if server_id:
230             values['server_id'] = server_id
231         if server_type:
232             values['server_type'] = server_type
233         res = super(mailgate_message,self).create(cr, uid, values, context=context)
234         return res
235
236     def write(self, cr, uid, ids, values, context=None):
237         if context is None:
238             context={}
239         server_id = context.get('server_id',False)
240         server_type = context.get('server_type',False)
241         if server_id:
242             values['server_id'] = server_id
243         if server_type:
244             values['server_type'] = server_type
245         res = super(mailgate_message,self).write(cr, uid, ids, values, context=context)
246         return res
247
248 mailgate_message()
249
250 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: