1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
24 from imaplib import IMAP4
25 from imaplib import IMAP4_SSL
26 from poplib import POP3
27 from poplib import POP3_SSL
30 from osv import osv, fields
32 from tools.translate import _
34 logger = logging.getLogger('fetchmail')
36 class fetchmail_server(osv.osv):
37 """Incoming POP/IMAP mail server account"""
38 _name = 'fetchmail.server'
39 _description = "POP/IMAP Server"
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),
81 def default_get(self, cr, uid, fields, context=None):
85 result = super(fetchmail_server, self).default_get(cr, uid, fields, context=context)
87 model = context.pop('fetchmail_model', False) or False
89 if isinstance(model, basestring):
90 model_id = self.pool.get('ir.model').search(cr, uid, [('model', '=', model)], context=context)
92 object_id = model_id[0],
97 def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
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}}
105 def set_draft(self, cr, uid, ids, context=None):
106 self.write(cr, uid, ids , {'state':'draft'})
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':
115 connection = IMAP4_SSL(server.server, int(server.port))
117 connection = IMAP4(server.server, int(server.port))
118 connection.login(server.user, server.password)
119 elif server.type == 'pop':
121 connection = POP3_SSL(server.server, int(server.port))
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)
130 def button_confirm_login(self, cr, uid, ids, context=None):
133 for server in self.browse(cr, uid, ids, context=context):
135 connection = server.connect()
136 server.write({'state':'done'})
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))
143 if server.type == 'imap':
145 elif server.type == 'pop':
148 # ignored, just a consequence of the previous exception
152 def _fetch_mails(self, cr, uid, ids=False, context=None):
154 ids = self.search(cr, uid, [('state','=','done')])
155 return self.fetch_mail(cr, uid, ids, context=context)
157 def fetch_mail(self, cr, uid, ids, context=None):
158 """WARNING: meant for cron usage only - will commit() after each email!"""
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})
167 if server.type == 'imap':
169 imap_server = server.connect()
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),
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')
183 logger.info("fetched/processed %s email(s) on %s server %s", count, server.type, server.name)
185 logger.exception("Failed to fetch mail from %s server %s", server.type, server.name)
190 elif server.type == 'pop':
192 pop_server = server.connect()
193 (numMsgs, totalSize) = pop_server.stat()
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,
200 save_original=server.original,
201 strip_attachments=(not server.attach),
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]})
207 logger.info("fetched/processed %s email(s) on %s server %s", numMsgs, server.type, server.name)
209 logger.exception("Failed to fetch mail from %s server %s", server.type, server.name)
213 server.write({'date': time.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)})
216 class mail_message(osv.osv):
217 _inherit = "mail.message"
219 'fetchmail_server_id': fields.many2one('fetchmail.server', "Inbound Mail Server",
222 oldname='server_id'),
225 def create(self, cr, uid, values, context=None):
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)
234 def write(self, cr, uid, ids, values, context=None):
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)
244 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: