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.

124 lines
5.0 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. import logging
  23. from openerp.osv import fields
  24. from openerp.osv.orm import TransientModel
  25. class attach_mail_manually(TransientModel):
  26. _name = 'fetchmail.attach.mail.manually'
  27. _columns = {
  28. 'folder_id': fields.many2one(
  29. 'fetchmail.server.folder', 'Folder', readonly=True),
  30. 'mail_ids': fields.one2many(
  31. 'fetchmail.attach.mail.manually.mail', 'wizard_id', 'Emails'),
  32. }
  33. def default_get(self, cr, uid, fields_list, context=None):
  34. if context is None:
  35. context = {}
  36. defaults = super(attach_mail_manually, self).default_get(
  37. cr, uid, fields_list, context)
  38. for folder in self.pool.get('fetchmail.server.folder').browse(
  39. cr, uid, [context.get('default_folder_id')], context):
  40. defaults['mail_ids'] = []
  41. connection = folder.server_id.connect()
  42. connection.select(folder.path)
  43. result, msgids = connection.search(
  44. None,
  45. 'FLAGGED' if folder.flag_nonmatching else 'UNDELETED')
  46. if result != 'OK':
  47. logging.error(
  48. 'Could not search mailbox %s on %s',
  49. folder.path,
  50. folder.server_id.server)
  51. continue
  52. attach_mail_manually_mail._columns['object_id'].selection = [
  53. (folder.model_id.model, folder.model_id.name)
  54. ]
  55. for msgid in msgids[0].split():
  56. result, msgdata = connection.fetch(msgid, '(RFC822)')
  57. if result != 'OK':
  58. logging.error(
  59. 'Could not fetch %s in %s on %s',
  60. msgid, folder.path, folder.server_id.server)
  61. continue
  62. mail_message = self.pool.get('mail.message').parse_message(
  63. msgdata[0][1])
  64. defaults['mail_ids'].append(
  65. (0, 0,
  66. {
  67. 'msgid': msgid,
  68. 'subject': mail_message.get('subject', ''),
  69. 'date': mail_message.get('date', ''),
  70. 'object_id': folder.model_id.model + ',False'
  71. }))
  72. connection.close()
  73. return defaults
  74. def attach_mails(self, cr, uid, ids, context=None):
  75. for this in self.browse(cr, uid, ids, context):
  76. for mail in this.mail_ids:
  77. connection = this.folder_id.server_id.connect()
  78. connection.select(this.folder_id.path)
  79. result, msgdata = connection.fetch(mail.msgid, '(RFC822)')
  80. if result != 'OK':
  81. logging.error(
  82. 'Could not fetch %s in %s on %s',
  83. mail.msgid, this.folder_id.path,
  84. this.folder_id.server_id.server)
  85. continue
  86. mail_message = self.pool.get('mail.message').parse_message(
  87. msgdata[0][1], this.folder_id.server_id.original)
  88. this.folder_id.server_id.attach_mail(
  89. connection,
  90. mail.object_id.id, this.folder_id, mail_message,
  91. mail.msgid)
  92. connection.close()
  93. return {'type': 'ir.actions.act_window_close'}
  94. class attach_mail_manually_mail(TransientModel):
  95. _name = 'fetchmail.attach.mail.manually.mail'
  96. _columns = {
  97. 'wizard_id': fields.many2one(
  98. 'fetchmail.attach.mail.manually', readonly=True),
  99. 'msgid': fields.char('Message id', size=16, readonly=True),
  100. 'subject': fields.char('Subject', size=128, readonly=True),
  101. 'date': fields.datetime('Date', readonly=True),
  102. 'object_id': fields.reference(
  103. 'Object',
  104. selection=lambda self, cr, uid, context:
  105. [(m.model, m.name) for m in
  106. self.pool.get('ir.model').browse(
  107. cr, uid, self.pool.get('ir.model').search(cr, uid, []),
  108. context)],
  109. size=128),
  110. }