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.

234 lines
8.4 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=None):
  125. if context is None:
  126. context = self.pool['res.users'].context_get(cr, uid)
  127. result_ids = []
  128. # read all incomming servers values
  129. all_ids = self.search(cr, uid, [], context=context)
  130. results = self.read(cr, uid, all_ids, ['id', 'type'], context=context)
  131. args = args[:]
  132. i = 0
  133. while i < len(args):
  134. operator = args[i][1]
  135. if operator == '=':
  136. for res in results:
  137. if (res['type'] == args[i][2]) and \
  138. (res['id'] not in result_ids):
  139. result_ids.append(res['id'])
  140. elif operator == 'in':
  141. for search_vals in args[i][2]:
  142. for res in results:
  143. if (res['type'] == search_vals) and \
  144. (res['id'] not in result_ids):
  145. result_ids.append(res['id'])
  146. else:
  147. continue
  148. i += 1
  149. return [('id', 'in', result_ids)]
  150. _columns = {
  151. 'server': fields.function(
  152. _get_incom_conf,
  153. method=True,
  154. string='Server',
  155. type="char",
  156. multi='income_mail_config',
  157. size=256, help="Hostname or IP of the mail server"),
  158. 'port': fields.function(
  159. _get_incom_conf,
  160. method=True,
  161. string='Port',
  162. type="integer",
  163. multi='income_mail_config',
  164. help="Hostname or IP of the mail server"),
  165. 'type': fields.function(
  166. _get_incom_conf,
  167. method=True,
  168. string='Type',
  169. type="char",
  170. multi='income_mail_config',
  171. fnct_search=_type_search,
  172. size=64,
  173. help="pop, imap, local"),
  174. 'is_ssl': fields.function(
  175. _get_incom_conf,
  176. method=True,
  177. string='Is SSL',
  178. type="boolean",
  179. multi='income_mail_config',
  180. help='Connections are encrypted with SSL/TLS through'
  181. ' a dedicated port (default: IMAPS=993, POP3S=995)'),
  182. 'attach': fields.function(
  183. _get_incom_conf,
  184. method=True,
  185. string='Keep Attachments',
  186. type="boolean",
  187. multi='income_mail_config',
  188. help="Whether attachments should be downloaded. "
  189. "If not enabled, incoming emails will be stripped of any "
  190. "attachments before being processed"),
  191. 'original': fields.function(
  192. _get_incom_conf,
  193. method=True,
  194. string='Keep Original',
  195. type="boolean",
  196. multi='income_mail_config',
  197. help="Whether a full original copy of each email should be kept "
  198. "for reference and attached to each processed message. This "
  199. "will usually double the size of your message database."),
  200. 'user': fields.function(
  201. _get_incom_conf,
  202. method=True,
  203. string='Username',
  204. type="char",
  205. multi='income_mail_config',
  206. size=64),
  207. 'password': fields.function(
  208. _get_incom_conf,
  209. method=True,
  210. string='password',
  211. type="char",
  212. multi='income_mail_config',
  213. size=64)}
  214. FetchmailServer()