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