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.

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