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.

232 lines
8.9 KiB

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