You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.8 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013 Therp BV (<http://therp.nl>)
  6. # All Rights Reserved
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp.osv import fields
  23. from openerp.osv.orm import TransientModel
  24. class attach_mail_manually(TransientModel):
  25. _name = 'fetchmail.attach.mail.manually'
  26. _columns = {
  27. 'folder_id': fields.many2one('fetchmail.server.folder', 'Folder',
  28. readonly=True),
  29. 'mail_ids': fields.one2many(
  30. 'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails'),
  31. }
  32. def default_get(self, cr, uid, fields_list, context=None):
  33. if context is None:
  34. context = {}
  35. defaults = super(attach_mail_manually, self).default_get(cr, uid,
  36. fields_list, context)
  37. for folder in self.pool.get('fetchmail.server.folder').browse(cr, uid,
  38. [context.get('default_folder_id')], context):
  39. defaults['mail_ids']=[]
  40. connection = folder.server_id.connect()
  41. connection.select(folder.path)
  42. result, msgids = connection.search(None, 'UNDELETED')
  43. if result != 'OK':
  44. logger.error('Could not search mailbox %s on %s' % (
  45. folder.path, this.server))
  46. continue
  47. attach_mail_manually_mail._columns['object_id'].selection=[
  48. (folder.model_id.model, folder.model_id.name)]
  49. for msgid in msgids[0].split():
  50. result, msgdata = connection.fetch(msgid, '(RFC822)')
  51. if result != 'OK':
  52. logger.error('Could not fetch %s in %s on %s' % (
  53. msgid, folder.path, this.server))
  54. continue
  55. mail_message = self.pool.get('mail.message').parse_message(
  56. msgdata[0][1])
  57. defaults['mail_ids'].append((0, 0, {
  58. 'msgid': msgid,
  59. 'subject': mail_message.get('subject', ''),
  60. 'date': mail_message.get('date', ''),
  61. 'object_id': folder.model_id.model+',False'
  62. }))
  63. connection.close()
  64. return defaults
  65. def attach_mails(self, cr, uid, ids, context=None):
  66. for this in self.browse(cr, uid, ids, context):
  67. for mail in this.mail_ids:
  68. connection = this.folder_id.server_id.connect()
  69. connection.select(this.folder_id.path)
  70. result, msgdata = connection.fetch(mail.msgid, '(RFC822)')
  71. if result != 'OK':
  72. logger.error('Could not fetch %s in %s on %s' % (
  73. msgid, folder.path, this.server))
  74. continue
  75. mail_message = self.pool.get('mail.message').parse_message(
  76. msgdata[0][1], this.folder_id.server_id.original)
  77. this.folder_id.server_id.attach_mail(connection,
  78. mail.object_id.id, this.folder_id, mail_message,
  79. mail.msgid)
  80. connection.close()
  81. return {'type': 'ir.actions.act_window_close'}
  82. class attach_mail_manually_mail(TransientModel):
  83. _name = 'fetchmail.attach.mail.manually.mail'
  84. _columns = {
  85. 'wizard_id': fields.many2one('fetchmail.attach.mail.manually',
  86. readonly=True),
  87. 'msgid': fields.char('Message id', size=16, readonly=True),
  88. 'subject': fields.char('Subject', size=128, readonly=True),
  89. 'date': fields.datetime('Date', readonly=True),
  90. 'object_id': fields.reference('Object',
  91. selection=lambda self, cr, uid, context:
  92. [(m.model, m.name) for m in
  93. self.pool.get('ir.model').browse(cr, uid,
  94. self.pool.get('ir.model').search(cr, uid, []),
  95. context)], size=128),
  96. }