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.

401 lines
17 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2010-2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
  3. # © 2014-2016 Trever L. Adams
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from openerp import models, fields, api, _
  6. from openerp.exceptions import UserError, ValidationError
  7. import logging
  8. # from pprint import pformat
  9. try:
  10. from freeswitchESL import ESL
  11. except ImportError:
  12. import ESL
  13. # import sys
  14. import StringIO
  15. import re
  16. import json
  17. _logger = logging.getLogger(__name__)
  18. class FreeSWITCHServer(models.Model):
  19. '''FreeSWITCH server object, stores the parameters of the FreeSWITCH Servers'''
  20. _name = "freeswitch.server"
  21. _description = "FreeSWITCH Servers"
  22. name = fields.Char(string='FreeSWITCH Server Name', required=True)
  23. active = fields.Boolean(
  24. string='Active', help="The active field allows you to hide the "
  25. "FreeSWITCH server without deleting it.", default=True)
  26. ip_address = fields.Char(
  27. string='FreeSWITCH IP address or DNS', required=True,
  28. help="IP address or DNS name of the FreeSWITCH server.")
  29. port = fields.Integer(
  30. string='Port', required=True, default=8021,
  31. help="TCP port on which the FreeSWITCH Event Socket listens. "
  32. "Defined in "
  33. "/etc/freeswitch/autoload_configs/event_socket.conf.xml on "
  34. "FreeSWITCH.")
  35. out_prefix = fields.Char(
  36. string='Out Prefix', size=4, help="Prefix to dial to make outgoing "
  37. "calls. If you don't use a prefix to make outgoing calls, "
  38. "leave empty.")
  39. password = fields.Char(
  40. string='Event Socket Password', required=True,
  41. help="Password that OpenERP will use to communicate with the "
  42. "FreeSWITCH Event Socket. Refer to "
  43. "/etc/freeswitch/autoload_configs/event_socket.conf.xml "
  44. "on your FreeSWITCH server.")
  45. context = fields.Char(
  46. string='Dialplan Context', required=True, default="XML default",
  47. help="FreeSWITCH dialplan context from which the calls will be "
  48. "made; e.g. 'XML default'. Refer to /etc/freeswitch/dialplan/* "
  49. "on your FreeSWITCH server.")
  50. wait_time = fields.Integer(
  51. string='Wait Time (sec)', required=True,
  52. help="Amount of time (in seconds) FreeSWITCH will try to reach "
  53. "the user's phone before hanging up.", default=60)
  54. alert_info = fields.Char(
  55. string='Alert-Info SIP Header',
  56. help="Set Alert-Info header in SIP request to user's IP Phone "
  57. "for the click2dial feature. If empty, the Alert-Info header "
  58. "will not be added. You can use it to have a special ring tone "
  59. "for click2dial (a silent one !) or to activate auto-answer "
  60. "for example.")
  61. company_id = fields.Many2one(
  62. 'res.company', string='Company',
  63. default=lambda self: self.env['res.company']._company_default_get(
  64. 'freeswitch.server'),
  65. help="Company who uses the FreeSWITCH server.")
  66. @api.multi
  67. @api.constrains(
  68. 'out_prefix', 'wait_time', 'port', 'context', 'alert_info', 'password')
  69. def _check_validity(self):
  70. for server in self:
  71. out_prefix = ('Out prefix', server.out_prefix)
  72. dialplan_context = ('Dialplan context', server.context)
  73. alert_info = ('Alert-Info SIP header', server.alert_info)
  74. password = ('Event Socket password', server.password)
  75. if out_prefix[1] and not out_prefix[1].isdigit():
  76. raise ValidationError(
  77. _("Only use digits for the '%s' on the FreeSWITCH server "
  78. "'%s'" % (out_prefix[0], server.name)))
  79. if server.wait_time < 1 or server.wait_time > 120:
  80. raise ValidationError(
  81. _("You should set a 'Wait time' value between 1 and 120 "
  82. "seconds for the FreeSWITCH server '%s'"
  83. % server.name))
  84. if server.port > 65535 or server.port < 1:
  85. raise ValidationError(
  86. _("You should set a TCP port between 1 and 65535 for the "
  87. "FreeSWITCH server '%s'" % server.name))
  88. for check_str in [dialplan_context, alert_info, password]:
  89. if check_str[1]:
  90. try:
  91. check_str[1].encode('ascii')
  92. except UnicodeEncodeError:
  93. raise ValidationError(
  94. _("The '%s' should only have ASCII caracters for "
  95. "the FreeSWITCH server '%s'"
  96. % (check_str[0], server.name)))
  97. @api.model
  98. def _connect_to_freeswitch(self):
  99. '''
  100. Open the connection to the FreeSWITCH Event Socket
  101. Returns an instance of the FreeSWITCH Event Socket
  102. '''
  103. user = self.env.user
  104. fs_server = user.get_freeswitch_server_from_user()
  105. # We check if the current user has a chan type
  106. if not user.freeswitch_chan_type:
  107. raise UserError(
  108. _('No channel type configured for the current user.'))
  109. # We check if the current user has an internal number
  110. if not user.resource:
  111. raise UserError(
  112. _('No resource name configured for the current user'))
  113. _logger.debug(
  114. "User's phone: %s/%s", user.freeswitch_chan_type, user.resource)
  115. _logger.debug(
  116. "FreeSWITCH server: %s:%d", fs_server.ip_address, fs_server.port)
  117. # Connect to the FreeSWITCH Event Socket
  118. try:
  119. fs_manager = ESL.ESLconnection(
  120. str(fs_server.ip_address),
  121. str(fs_server.port), str(fs_server.password))
  122. except Exception, e:
  123. _logger.error(
  124. "Error in the request to the FreeSWITCH Event Socket %s",
  125. fs_server.ip_address)
  126. _logger.error("Here is the error message: %s", e)
  127. raise UserError(
  128. _("Problem in the request from Odoo to FreeSWITCH. "
  129. "Here is the error message: %s" % e))
  130. # return (False, False, False)
  131. return (user, fs_server, fs_manager)
  132. @api.multi
  133. def test_es_connection(self):
  134. self.ensure_one()
  135. fs_manager = False
  136. try:
  137. fs_manager = ESL.ESLconnection(
  138. str(self.ip_address),
  139. str(self.port), str(self.password))
  140. except Exception, e:
  141. raise UserError(
  142. _("Connection Test Failed! The error message is: %s" % e))
  143. finally:
  144. try:
  145. if fs_manager.connected() is not 1:
  146. raise UserError(
  147. _("Connection Test Failed! Check Host, Port and Password"))
  148. else:
  149. fs_manager.disconnect()
  150. except Exception, e:
  151. pass
  152. raise UserError(_(
  153. "Connection Test Successfull! Odoo can successfully login to "
  154. "the FreeSWITCH Event Socket."))
  155. @api.model
  156. def _get_calling_number(self):
  157. user, fs_server, fs_manager = self._connect_to_freeswitch()
  158. calling_party_number = False
  159. try:
  160. request = "channels like /" + re.sub(r'/', r':', user.resource) + \
  161. ("/" if user.freeswitch_chan_type == "FreeTDM" else "@") + \
  162. " as json"
  163. ret = fs_manager.api('show', str(request))
  164. f = json.load(StringIO.StringIO(ret.getBody()))
  165. if int(f['row_count']) > 0:
  166. if (f['rows'][0]['initial_cid_name'] == 'Odoo Connector' or
  167. f['rows'][0]['direction'] == 'inbound'):
  168. calling_party_number = f['rows'][0]['dest']
  169. else:
  170. calling_party_number = f['rows'][0]['cid_num']
  171. except Exception, e:
  172. _logger.error(
  173. "Error in the Status request to FreeSWITCH server %s",
  174. fs_server.ip_address)
  175. _logger.error(
  176. "Here are the details of the error: '%s'" % unicode(e))
  177. raise UserError(
  178. _("Can't get calling number from FreeSWITCH.\nHere is the "
  179. "error: '%s'" % unicode(e)))
  180. finally:
  181. fs_manager.disconnect()
  182. _logger.debug("Calling party number: '%s'", calling_party_number)
  183. return calling_party_number
  184. @api.model
  185. def get_record_from_my_channel(self):
  186. calling_number = self.env['freeswitch.server']._get_calling_number()
  187. # calling_number = "0641981246"
  188. if calling_number:
  189. record = self.env['phone.common'].get_record_from_phone_number(
  190. calling_number)
  191. if record:
  192. return record
  193. else:
  194. return calling_number
  195. else:
  196. return False
  197. class ResUsers(models.Model):
  198. _inherit = "res.users"
  199. internal_number = fields.Char(
  200. string='Internal Number',
  201. help="User's internal phone number.")
  202. dial_suffix = fields.Char(
  203. string='User-specific Dial Suffix',
  204. help="User-specific dial suffix such as aa=2wb for SCCP "
  205. "auto answer.")
  206. callerid = fields.Char(
  207. string='Caller ID', copy=False,
  208. help="Caller ID used for the calls initiated by this user.")
  209. cdraccount = fields.Char(
  210. string='CDR Account',
  211. help="Call Detail Record (CDR) account used for billing this "
  212. "user.")
  213. freeswitch_chan_type = fields.Selection([
  214. ('user', 'SIP'),
  215. ('FreeTDM', 'FreeTDM'),
  216. ('verto.rtc', 'Verto'),
  217. ('skinny', 'Skinny'),
  218. ('h323', 'H323'),
  219. ('dingaling', 'XMPP/JINGLE'),
  220. ('gsmopen', 'GSM SMS/Voice'),
  221. ('skypeopen', 'SkypeOpen'),
  222. ('Khomp', 'Khomp'),
  223. ('opal', 'Opal Multi-protocol'),
  224. ('portaudio', 'Portaudio'),
  225. ], string='FreeSWITCH Channel Type', default='user',
  226. help="FreeSWITCH channel type, as used in the FreeSWITCH "
  227. "dialplan. If the user has a regular IP phone, the channel type "
  228. "is 'SIP'. Use Verto for verto.rtc connections only if you "
  229. "haven't added '${verto_contact ${dialed_user}@${dialed_domain}}' "
  230. "to the default dial string. Otherwise, use SIP. (This better "
  231. "allows for changes to the user directory and changes in type of "
  232. "phone without the need for further changes in OpenERP/Odoo.)")
  233. resource = fields.Char(
  234. string='Resource Name',
  235. help="Resource name for the channel type selected. For example, "
  236. "if you use 'user/phone1' in your FreeSWITCH dialplan to ring "
  237. "the SIP phone of this user, then the resource name for this user "
  238. "is 'phone1'. For a SIP phone, the phone number is often used as "
  239. "resource name, but not always. FreeTDM will be the span followed "
  240. "by the port (i.e. 1/5).")
  241. alert_info = fields.Char(
  242. string='User-specific Alert-Info SIP Header',
  243. help="Set a user-specific Alert-Info header in SIP request to "
  244. "user's IP Phone for the click2dial feature. If empty, the "
  245. "Alert-Info header will not be added. You can use it to have a "
  246. "special ring tone for click2dial (a silent one !) or to "
  247. "activate auto-answer for example.")
  248. variable = fields.Char(
  249. string='User-specific Variable',
  250. help="Set a user-specific 'Variable' field in the FreeSWITCH "
  251. "Event Socket 'originate' request for the click2dial "
  252. "feature. If you want to have several variable headers, separate "
  253. "them with '|'.")
  254. freeswitch_server_id = fields.Many2one(
  255. 'freeswitch.server', string='FreeSWITCH Server',
  256. help="FreeSWITCH server on which the user's phone is connected. "
  257. "If you leave this field empty, it will use the first FreeSWITCH "
  258. "server of the user's company.")
  259. @api.multi
  260. @api.constrains('resource', 'internal_number', 'callerid')
  261. def _check_validity(self):
  262. for user in self:
  263. strings_to_check = [
  264. (_('Resource Name'), user.resource),
  265. (_('Internal Number'), user.internal_number),
  266. (_('Caller ID'), user.callerid),
  267. ]
  268. for check_string in strings_to_check:
  269. if check_string[1]:
  270. try:
  271. check_string[1].encode('ascii')
  272. except UnicodeEncodeError:
  273. raise ValidationError(_(
  274. "The '%s' for the user '%s' should only have "
  275. "ASCII caracters"),
  276. check_string[0], user.name)
  277. @api.multi
  278. def get_freeswitch_server_from_user(self):
  279. '''Returns an freeswitch.server recordset'''
  280. self.ensure_one()
  281. # We check if the user has an FreeSWITCH server configured
  282. if self.freeswitch_server_id.id:
  283. fs_server = self.freeswitch_server_id
  284. else:
  285. freeswitch_servers = self.env['freeswitch.server'].search(
  286. [('company_id', '=', self.company_id.id)])
  287. # If the user doesn't have an FreeSWITCH server,
  288. # we take the first one of the user's company
  289. if not freeswitch_servers:
  290. raise UserError(
  291. _("No FreeSWITCH server configured for the company '%s'.")
  292. % self.company_id.name)
  293. else:
  294. fs_server = freeswitch_servers[0]
  295. return fs_server
  296. class PhoneCommon(models.AbstractModel):
  297. _inherit = 'phone.common'
  298. @api.model
  299. def click2dial(self, erp_number):
  300. res = super(PhoneCommon, self).click2dial(erp_number)
  301. if not erp_number:
  302. raise UserError(_('Missing phone number'))
  303. user, fs_server, fs_manager = \
  304. self.env['freeswitch.server']._connect_to_freeswitch()
  305. fs_number = self.convert_to_dial_number(erp_number)
  306. # Add 'out prefix'
  307. if fs_server.out_prefix:
  308. _logger.debug('Out prefix = %s', fs_server.out_prefix)
  309. fs_number = '%s%s' % (fs_server.out_prefix, fs_number)
  310. _logger.debug('Number to be sent to FreeSWITCH = %s', fs_number)
  311. # The user should have a CallerID
  312. if not user.callerid:
  313. raise UserError(_('No callerID configured for the current user'))
  314. variable = ""
  315. if user.freeswitch_chan_type == 'user':
  316. # We can only have one alert-info header in a SIP request
  317. if user.alert_info:
  318. variable += 'alert_info=' + user.alert_info
  319. elif fs_server.alert_info:
  320. variable += 'alert_info=' + fs_server.alert_info
  321. if user.variable:
  322. for user_variable in user.variable.split('|'):
  323. if len(variable) and len(user_variable):
  324. variable += ','
  325. variable += user_variable.strip()
  326. if user.callerid:
  327. caller_name = re.search(r'([^<]*).*',
  328. user.callerid).group(1).strip()
  329. caller_number = re.search(r'.*<(.*)>.*',
  330. user.callerid).group(1).strip()
  331. if caller_name:
  332. if len(variable):
  333. variable += ','
  334. caller_name = caller_name.replace(",", r"\,")
  335. variable += 'effective_caller_id_name=' + caller_name
  336. if caller_number:
  337. if len(variable):
  338. variable += ','
  339. variable += 'effective_caller_id_number=' + caller_number
  340. if fs_server.wait_time != 60:
  341. if len(variable):
  342. variable += ','
  343. variable += 'ignore_early_media=true' + ','
  344. variable += 'originate_timeout=' + str(fs_server.wait_time)
  345. channel = '%s/%s' % (user.freeswitch_chan_type, user.resource)
  346. if user.dial_suffix:
  347. channel += '/%s' % user.dial_suffix
  348. try:
  349. # originate <csv global vars>user/2005 1005 DP_TYPE DP_NAME
  350. # 'Caller ID name showed to aleg' 90125
  351. dial_string = (('<' + variable + '>') if variable else '') + \
  352. channel + ' ' + fs_number + ' ' + fs_server.context + ' ' + \
  353. '\'Odoo Connector\' ' + fs_number
  354. # raise orm.except_orm(_('Error :'), dial_string)
  355. fs_manager.api('originate', dial_string.encode("ascii"))
  356. except Exception, e:
  357. _logger.error(
  358. "Error in the Originate request to FreeSWITCH server %s",
  359. fs_server.ip_address)
  360. _logger.error(
  361. "Here are the details of the error: '%s'", unicode(e))
  362. raise UserError(
  363. _("Click to dial with FreeSWITCH failed.\nHere is the error: "
  364. "'%s'")
  365. % unicode(e))
  366. finally:
  367. fs_manager.disconnect()
  368. res['dialed_number'] = fs_number
  369. return res