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.

225 lines
8.7 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Nicolas Bessi
  5. # Copyright 2012 Camptocamp SA
  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. from openerp.osv import orm, fields
  22. from openerp.addons.server_environment import serv_config
  23. class IrMail(orm.Model):
  24. _inherit = "ir.mail_server"
  25. def _get_smtp_conf(self, cr, uid, ids, name, args, context=None):
  26. """
  27. Return configuration
  28. """
  29. res = {}
  30. for mail_server in self.browse(cr, uid, ids, context=context):
  31. global_section_name = 'outgoing_mail'
  32. # default vals
  33. config_vals = {'smtp_port': 587}
  34. if serv_config.has_section(global_section_name):
  35. config_vals.update((serv_config.items(global_section_name)))
  36. custom_section_name = '.'.join((global_section_name, mail_server.name))
  37. if serv_config.has_section(custom_section_name):
  38. config_vals.update(serv_config.items(custom_section_name))
  39. if config_vals.get('smtp_port'):
  40. config_vals['smtp_port'] = int(config_vals['smtp_port'])
  41. res[mail_server.id] = config_vals
  42. return res
  43. _columns = {
  44. 'smtp_host': fields.function(
  45. _get_smtp_conf,
  46. string='SMTP Server',
  47. type="char",
  48. multi='outgoing_mail_config',
  49. states={'draft': [('readonly', True)]},
  50. help="Hostname or IP of SMTP server"),
  51. 'smtp_port': fields.function(
  52. _get_smtp_conf,
  53. string='SMTP Port',
  54. type="integer",
  55. multi='outgoing_mail_config',
  56. states={'draft': [('readonly', True)]},
  57. help="SMTP Port. Usually 465 for SSL, and 25 or 587 for other cases.",
  58. size=5),
  59. 'smtp_user': fields.function(
  60. _get_smtp_conf,
  61. string='Username',
  62. type="char",
  63. multi='outgoing_mail_config',
  64. states={'draft': [('readonly', True)]},
  65. help="Optional username for SMTP authentication",
  66. size=64),
  67. 'smtp_pass': fields.function(
  68. _get_smtp_conf,
  69. string='Password',
  70. type="char",
  71. multi='outgoing_mail_config',
  72. states={'draft': [('readonly', True)]},
  73. help="Optional password for SMTP authentication",
  74. size=64),
  75. 'smtp_encryption': fields.function(
  76. _get_smtp_conf,
  77. string='smtp_encryption',
  78. type="selection",
  79. multi='outgoing_mail_config',
  80. selection=[('none','None'),
  81. ('starttls','TLS (STARTTLS)'),
  82. ('ssl','SSL/TLS')],
  83. states={'draft': [('readonly', True)]},
  84. help="Choose the connection encryption scheme:\n"
  85. "- none: SMTP sessions are done in cleartext.\n"
  86. "- starttls: TLS encryption is requested at start of SMTP session (Recommended)\n"
  87. "- ssl: SMTP sessions are encrypted with SSL/TLS through a dedicated port (default: 465)",)
  88. }
  89. class FetchmailServer(orm.Model):
  90. """Incoming POP/IMAP mail server account"""
  91. _inherit = 'fetchmail.server'
  92. def _get_incom_conf(self, cr, uid, ids, name, args, context=None):
  93. """
  94. Return configuration
  95. """
  96. res = {}
  97. for fetchmail in self.browse(cr, uid, ids, context=context):
  98. global_section_name = 'incoming_mail'
  99. key_types = {'port': int,
  100. 'is_ssl': lambda a: bool(int(a)),
  101. 'attach': lambda a: bool(int(a)),
  102. 'original': lambda a: bool(int(a)),
  103. }
  104. # default vals
  105. config_vals = {'port': 993,
  106. 'is_ssl': 0,
  107. 'attach': 0,
  108. 'original': 0,
  109. }
  110. if serv_config.has_section(global_section_name):
  111. config_vals.update(serv_config.items(global_section_name))
  112. custom_section_name = '.'.join((global_section_name, fetchmail.name))
  113. if serv_config.has_section(custom_section_name):
  114. config_vals.update(serv_config.items(custom_section_name))
  115. for key, to_type in key_types.iteritems():
  116. if config_vals.get(key):
  117. config_vals[key] = to_type(config_vals[key])
  118. res[fetchmail.id] = config_vals
  119. return res
  120. def _type_search(self, cr, uid, obj, name, args, context=None):
  121. result_ids = []
  122. # read all incomming servers values
  123. all_ids = self.search(cr, uid, [], context=context)
  124. results = self.read(cr, uid, all_ids, ['id', 'type'], context=context)
  125. args = args[:]
  126. i = 0
  127. while i < len(args):
  128. operator = args[i][1]
  129. if operator == '=':
  130. for res in results:
  131. if (res['type'] == args[i][2]) and (res['id'] not in result_ids):
  132. result_ids.append(res['id'])
  133. elif operator == 'in':
  134. for search_vals in args[i][2]:
  135. for res in results:
  136. if (res['type'] == search_vals) and (res['id'] not in result_ids):
  137. result_ids.append(res['id'])
  138. else:
  139. continue
  140. i += 1
  141. return [('id', 'in', result_ids)]
  142. _columns = {
  143. 'server': fields.function(
  144. _get_incom_conf,
  145. string='Server',
  146. type="char",
  147. multi='income_mail_config',
  148. states={'draft': [('readonly', True)]},
  149. help="Hostname or IP of the mail server"),
  150. 'port': fields.function(
  151. _get_incom_conf,
  152. string='Port',
  153. type="integer",
  154. states={'draft': [('readonly', True)]},
  155. multi='income_mail_config'),
  156. 'type': fields.function(
  157. _get_incom_conf,
  158. string='Type',
  159. type="selection",
  160. selection=[('pop', 'POP Server'),
  161. ('imap', 'IMAP Server'),
  162. ('local', 'Local Server'),
  163. ],
  164. multi='income_mail_config',
  165. fnct_search=_type_search,
  166. states={'draft': [('readonly', True)]},
  167. help="pop, imap, local"),
  168. 'is_ssl': fields.function(
  169. _get_incom_conf,
  170. string='Is SSL',
  171. type="boolean",
  172. multi='income_mail_config',
  173. states={'draft': [('readonly', True)]},
  174. help='Connections are encrypted with SSL/TLS through'
  175. ' a dedicated port (default: IMAPS=993, POP3S=995)'),
  176. 'attach': fields.function(
  177. _get_incom_conf,
  178. string='Keep Attachments',
  179. type="boolean",
  180. multi='income_mail_config',
  181. states={'draft': [('readonly', True)]},
  182. help="Whether attachments should be downloaded. "
  183. "If not enabled, incoming emails will be stripped of any "
  184. "attachments before being processed"),
  185. 'original': fields.function(
  186. _get_incom_conf,
  187. string='Keep Original',
  188. type="boolean",
  189. multi='income_mail_config',
  190. states={'draft': [('readonly', True)]},
  191. help="Whether a full original copy of each email should be kept "
  192. "for reference and attached to each processed message. This "
  193. "will usually double the size of your message database."),
  194. 'user': fields.function(
  195. _get_incom_conf,
  196. string='Username',
  197. type="char",
  198. states={'draft': [('readonly', True)]},
  199. multi='income_mail_config'),
  200. 'password': fields.function(
  201. _get_incom_conf,
  202. string='password',
  203. type="char",
  204. states={'draft': [('readonly', True)]},
  205. multi='income_mail_config')
  206. }