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