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.

489 lines
21 KiB

10 years ago
10 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Asterisk Click2dial module for OpenERP
  5. # Copyright (C) 2010-2013 Alexis de Lattre <alexis@via.ecp.fr>
  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 openerp.osv import fields, orm
  22. from openerp.tools.translate import _
  23. import logging
  24. # Lib for phone number reformating -> pip install phonenumbers
  25. import phonenumbers
  26. try:
  27. # Lib py-asterisk from http://code.google.com/p/py-asterisk/
  28. # -> pip install py-Asterisk
  29. from Asterisk import Manager
  30. except ImportError:
  31. Manager = None
  32. _logger = logging.getLogger(__name__)
  33. class asterisk_server(orm.Model):
  34. '''Asterisk server object, stores the parameters of the Asterisk IPBXs'''
  35. _name = "asterisk.server"
  36. _description = "Asterisk Servers"
  37. _columns = {
  38. 'name': fields.char('Asterisk Server Name', size=50, required=True),
  39. 'active': fields.boolean(
  40. 'Active', help="The active field allows you to hide the Asterisk "
  41. "server without deleting it."),
  42. 'ip_address': fields.char(
  43. 'Asterisk IP address or DNS', size=50, required=True,
  44. help="IP address or DNS name of the Asterisk server."),
  45. 'port': fields.integer(
  46. 'Port', required=True,
  47. help="TCP port on which the Asterisk Manager Interface listens. "
  48. "Defined in /etc/asterisk/manager.conf on Asterisk."),
  49. 'out_prefix': fields.char(
  50. 'Out Prefix', size=4, help="Prefix to dial to make outgoing "
  51. "calls. If you don't use a prefix to make outgoing calls, "
  52. "leave empty."),
  53. 'login': fields.char(
  54. 'AMI Login', size=30, required=True,
  55. help="Login that OpenERP will use to communicate with the "
  56. "Asterisk Manager Interface. Refer to /etc/asterisk/manager.conf "
  57. "on your Asterisk server."),
  58. 'password': fields.char(
  59. 'AMI Password', size=30, required=True,
  60. help="Password that OpenERP will use to communicate with the "
  61. "Asterisk Manager Interface. Refer to /etc/asterisk/manager.conf "
  62. "on your Asterisk server."),
  63. 'context': fields.char(
  64. 'Dialplan Context', size=50, required=True,
  65. help="Asterisk dialplan context from which the calls will be "
  66. "made. Refer to /etc/asterisk/extensions.conf on your Asterisk "
  67. "server."),
  68. 'wait_time': fields.integer(
  69. 'Wait Time (sec)', required=True,
  70. help="Amount of time (in seconds) Asterisk will try to reach "
  71. "the user's phone before hanging up."),
  72. 'extension_priority': fields.integer(
  73. 'Extension Priority', required=True,
  74. help="Priority of the extension in the Asterisk dialplan. Refer "
  75. "to /etc/asterisk/extensions.conf on your Asterisk server."),
  76. 'alert_info': fields.char(
  77. 'Alert-Info SIP Header', size=255,
  78. help="Set Alert-Info header in SIP request to user's IP Phone "
  79. "for the click2dial feature. If empty, the Alert-Info header "
  80. "will not be added. You can use it to have a special ring tone "
  81. "for click2dial (a silent one !) or to activate auto-answer "
  82. "for example."),
  83. 'company_id': fields.many2one(
  84. 'res.company', 'Company',
  85. help="Company who uses the Asterisk server."),
  86. }
  87. _defaults = {
  88. 'active': True,
  89. 'port': 5038, # Default AMI port
  90. 'extension_priority': 1,
  91. 'wait_time': 15,
  92. 'company_id': lambda self, cr, uid, context:
  93. self.pool['res.company']._company_default_get(
  94. cr, uid, 'asterisk.server', context=context),
  95. }
  96. def _check_validity(self, cr, uid, ids):
  97. for server in self.browse(cr, uid, ids):
  98. out_prefix = ('Out prefix', server.out_prefix)
  99. dialplan_context = ('Dialplan context', server.context)
  100. alert_info = ('Alert-Info SIP header', server.alert_info)
  101. login = ('AMI login', server.login)
  102. password = ('AMI password', server.password)
  103. if out_prefix[1] and not out_prefix[1].isdigit():
  104. raise orm.except_orm(
  105. _('Error:'),
  106. _("Only use digits for the '%s' on the Asterisk server "
  107. "'%s'" % (out_prefix[0], server.name)))
  108. if server.wait_time < 1 or server.wait_time > 120:
  109. raise orm.except_orm(
  110. _('Error:'),
  111. _("You should set a 'Wait time' value between 1 and 120 "
  112. "seconds for the Asterisk server '%s'" % server.name))
  113. if server.extension_priority < 1:
  114. raise orm.except_orm(
  115. _('Error:'),
  116. _("The 'extension priority' must be a positive value for "
  117. "the Asterisk server '%s'" % server.name))
  118. if server.port > 65535 or server.port < 1:
  119. raise orm.except_orm(
  120. _('Error:'),
  121. _("You should set a TCP port between 1 and 65535 for the "
  122. "Asterisk server '%s'" % server.name))
  123. for check_str in [dialplan_context, alert_info, login, password]:
  124. if check_str[1]:
  125. try:
  126. check_str[1].encode('ascii')
  127. except UnicodeEncodeError:
  128. raise orm.except_orm(
  129. _('Error:'),
  130. _("The '%s' should only have ASCII caracters for "
  131. "the Asterisk server '%s'"
  132. % (check_str[0], server.name)))
  133. return True
  134. _constraints = [(
  135. _check_validity,
  136. "Error message in raise",
  137. [
  138. 'out_prefix', 'wait_time', 'extension_priority', 'port',
  139. 'context', 'alert_info', 'login', 'password']
  140. )]
  141. def _reformat_number(
  142. self, cr, uid, erp_number, ast_server=None, context=None):
  143. '''
  144. This function is dedicated to the transformation of the number
  145. available in OpenERP to the number that Asterisk should dial.
  146. You may have to inherit this function in another module specific
  147. for your company if you are not happy with the way I reformat
  148. the OpenERP numbers.
  149. '''
  150. assert(erp_number), 'Missing phone number'
  151. _logger.debug('Number before reformat = %s' % erp_number)
  152. if not ast_server:
  153. ast_server = self._get_asterisk_server_from_user(
  154. cr, uid, context=context)
  155. # erp_number are supposed to be in E.164 format, so no need to
  156. # give a country code here
  157. parsed_num = phonenumbers.parse(erp_number, None)
  158. country_code = ast_server.company_id.country_id.code
  159. assert(country_code), 'Missing country on company'
  160. _logger.debug('Country code = %s' % country_code)
  161. to_dial_number = phonenumbers.format_out_of_country_calling_number(
  162. parsed_num, country_code.upper()).replace(' ', '').replace('-', '')
  163. # Add 'out prefix' to all numbers
  164. if ast_server.out_prefix:
  165. _logger.debug('Out prefix = %s' % ast_server.out_prefix)
  166. to_dial_number = '%s%s' % (ast_server.out_prefix, to_dial_number)
  167. _logger.debug('Number to be sent to Asterisk = %s' % to_dial_number)
  168. return to_dial_number
  169. def _get_asterisk_server_from_user(self, cr, uid, context=None):
  170. '''Returns an asterisk.server browse object'''
  171. # We check if the user has an Asterisk server configured
  172. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  173. if user.asterisk_server_id.id:
  174. ast_server = user.asterisk_server_id
  175. else:
  176. asterisk_server_ids = self.search(
  177. cr, uid, [('company_id', '=', user.company_id.id)],
  178. context=context)
  179. # If the user doesn't have an asterisk server,
  180. # we take the first one of the user's company
  181. if not asterisk_server_ids:
  182. raise orm.except_orm(
  183. _('Error:'),
  184. _("No Asterisk server configured for the company '%s'.")
  185. % user.company_id.name)
  186. else:
  187. ast_server = self.browse(
  188. cr, uid, asterisk_server_ids[0], context=context)
  189. return ast_server
  190. def _connect_to_asterisk(self, cr, uid, context=None):
  191. '''
  192. Open the connection to the Asterisk Manager
  193. Returns an instance of the Asterisk Manager
  194. '''
  195. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  196. ast_server = self._get_asterisk_server_from_user(
  197. cr, uid, context=context)
  198. # We check if the current user has a chan type
  199. if not user.asterisk_chan_type:
  200. raise orm.except_orm(
  201. _('Error:'),
  202. _('No channel type configured for the current user.'))
  203. # We check if the current user has an internal number
  204. if not user.resource:
  205. raise orm.except_orm(
  206. _('Error:'),
  207. _('No resource name configured for the current user'))
  208. _logger.debug(
  209. "User's phone: %s/%s" % (user.asterisk_chan_type, user.resource))
  210. _logger.debug(
  211. "Asterisk server: %s:%d"
  212. % (ast_server.ip_address, ast_server.port))
  213. # Connect to the Asterisk Manager Interface
  214. try:
  215. ast_manager = Manager.Manager(
  216. (ast_server.ip_address, ast_server.port),
  217. ast_server.login, ast_server.password)
  218. except Exception, e:
  219. _logger.error(
  220. "Error in the request to the Asterisk Manager Interface %s"
  221. % ast_server.ip_address)
  222. _logger.error("Here is the error message: %s" % e)
  223. raise orm.except_orm(
  224. _('Error:'),
  225. _("Problem in the request from OpenERP to Asterisk. "
  226. "Here is the error message: %s" % e))
  227. return (user, ast_server, ast_manager)
  228. def test_ami_connection(self, cr, uid, ids, context=None):
  229. assert len(ids) == 1, 'Only 1 ID'
  230. ast_server = self.browse(cr, uid, ids[0], context=context)
  231. try:
  232. ast_manager = Manager.Manager(
  233. (ast_server.ip_address, ast_server.port),
  234. ast_server.login,
  235. ast_server.password)
  236. except Exception, e:
  237. raise orm.except_orm(
  238. _("Connection Test Failed!"),
  239. _("Here is the error message: %s" % e))
  240. finally:
  241. try:
  242. if ast_manager:
  243. ast_manager.Logoff()
  244. except Exception:
  245. pass
  246. raise orm.except_orm(
  247. _("Connection Test Successfull!"),
  248. _("OpenERP can successfully login to the Asterisk Manager "
  249. "Interface."))
  250. def _get_calling_number(self, cr, uid, context=None):
  251. user, ast_server, ast_manager = self._connect_to_asterisk(
  252. cr, uid, context=context)
  253. calling_party_number = False
  254. try:
  255. list_chan = ast_manager.Status()
  256. # from pprint import pprint
  257. # pprint(list_chan)
  258. _logger.debug("Result of Status AMI request: %s", list_chan)
  259. for chan in list_chan.values():
  260. sip_account = user.asterisk_chan_type + '/' + user.resource
  261. # 4 = Ring
  262. if (
  263. chan.get('ChannelState') == '4' and
  264. chan.get('ConnectedLineNum') == user.internal_number):
  265. _logger.debug("Found a matching Event in 'Ring' state")
  266. calling_party_number = chan.get('CallerIDNum')
  267. break
  268. # 6 = Up
  269. if (
  270. chan.get('ChannelState') == '6'
  271. and sip_account in chan.get('BridgedChannel', '')):
  272. _logger.debug("Found a matching Event in 'Up' state")
  273. calling_party_number = chan.get('CallerIDNum')
  274. break
  275. # Compatibility with Asterisk 1.4
  276. if (
  277. chan.get('State') == 'Up'
  278. and sip_account in chan.get('Link', '')):
  279. _logger.debug("Found a matching Event in 'Up' state")
  280. calling_party_number = chan.get('CallerIDNum')
  281. break
  282. except Exception, e:
  283. _logger.error(
  284. "Error in the Status request to Asterisk server %s"
  285. % ast_server.ip_address)
  286. _logger.error(
  287. "Here are the details of the error: '%s'" % unicode(e))
  288. raise orm.except_orm(
  289. _('Error:'),
  290. _("Can't get calling number from Asterisk.\nHere is the "
  291. "error: '%s'" % unicode(e)))
  292. finally:
  293. ast_manager.Logoff()
  294. _logger.debug("Calling party number: '%s'" % calling_party_number)
  295. return calling_party_number
  296. def get_record_from_my_channel(self, cr, uid, context=None):
  297. calling_number = self.pool['asterisk.server']._get_calling_number(
  298. cr, uid, context=context)
  299. # calling_number = "0641981246"
  300. if calling_number:
  301. record = self.pool['phone.common'].get_record_from_phone_number(
  302. cr, uid, calling_number, context=context)
  303. if record:
  304. return record
  305. else:
  306. return calling_number
  307. else:
  308. return False
  309. class res_users(orm.Model):
  310. _inherit = "res.users"
  311. _columns = {
  312. 'internal_number': fields.char(
  313. 'Internal Number', size=15,
  314. help="User's internal phone number."),
  315. 'dial_suffix': fields.char(
  316. 'User-specific Dial Suffix', size=15,
  317. help="User-specific dial suffix such as aa=2wb for SCCP "
  318. "auto answer."),
  319. 'callerid': fields.char(
  320. 'Caller ID', size=50,
  321. help="Caller ID used for the calls initiated by this user."),
  322. # You'd probably think: Asterisk should reuse the callerID of sip.conf!
  323. # But it cannot, cf
  324. # http://lists.digium.com/pipermail/asterisk-users/2012-January/269787.html
  325. 'cdraccount': fields.char(
  326. 'CDR Account', size=50,
  327. help="Call Detail Record (CDR) account used for billing this "
  328. "user."),
  329. 'asterisk_chan_type': fields.selection([
  330. ('SIP', 'SIP'),
  331. ('IAX2', 'IAX2'),
  332. ('DAHDI', 'DAHDI'),
  333. ('Zap', 'Zap'),
  334. ('Skinny', 'Skinny'),
  335. ('MGCP', 'MGCP'),
  336. ('mISDN', 'mISDN'),
  337. ('H323', 'H323'),
  338. ('SCCP', 'SCCP'),
  339. ('Local', 'Local'),
  340. ], 'Asterisk Channel Type',
  341. help="Asterisk channel type, as used in the Asterisk dialplan. "
  342. "If the user has a regular IP phone, the channel type is 'SIP'."),
  343. 'resource': fields.char(
  344. 'Resource Name', size=64,
  345. help="Resource name for the channel type selected. For example, "
  346. "if you use 'Dial(SIP/phone1)' in your Asterisk dialplan to ring "
  347. "the SIP phone of this user, then the resource name for this user "
  348. "is 'phone1'. For a SIP phone, the phone number is often used as "
  349. "resource name, but not always."),
  350. 'alert_info': fields.char(
  351. 'User-specific Alert-Info SIP Header', size=255,
  352. help="Set a user-specific Alert-Info header in SIP request to "
  353. "user's IP Phone for the click2dial feature. If empty, the "
  354. "Alert-Info header will not be added. You can use it to have a "
  355. "special ring tone for click2dial (a silent one !) or to "
  356. "activate auto-answer for example."),
  357. 'variable': fields.char(
  358. 'User-specific Variable', size=255,
  359. help="Set a user-specific 'Variable' field in the Asterisk "
  360. "Manager Interface 'originate' request for the click2dial "
  361. "feature. If you want to have several variable headers, separate "
  362. "them with '|'."),
  363. 'asterisk_server_id': fields.many2one(
  364. 'asterisk.server', 'Asterisk Server',
  365. help="Asterisk server on which the user's phone is connected. "
  366. "If you leave this field empty, it will use the first Asterisk "
  367. "server of the user's company."),
  368. }
  369. _defaults = {
  370. 'asterisk_chan_type': 'SIP',
  371. }
  372. def _check_validity(self, cr, uid, ids):
  373. for user in self.browse(cr, uid, ids):
  374. strings_to_check = [
  375. (_('Resource Name'), user.resource),
  376. (_('Internal Number'), user.internal_number),
  377. (_('Caller ID'), user.callerid),
  378. ]
  379. for check_string in strings_to_check:
  380. if check_string[1]:
  381. try:
  382. check_string[1].encode('ascii')
  383. except UnicodeEncodeError:
  384. raise orm.except_orm(
  385. _('Error:'),
  386. _("The '%s' for the user '%s' should only have "
  387. "ASCII caracters")
  388. % (check_string[0], user.name))
  389. return True
  390. _constraints = [(
  391. _check_validity,
  392. "Error message in raise",
  393. ['resource', 'internal_number', 'callerid']
  394. )]
  395. class phone_common(orm.AbstractModel):
  396. _inherit = 'phone.common'
  397. def click2dial(self, cr, uid, erp_number, context=None):
  398. if not erp_number:
  399. orm.except_orm(
  400. _('Error:'),
  401. _('Missing phone number'))
  402. user, ast_server, ast_manager = \
  403. self.pool['asterisk.server']._connect_to_asterisk(
  404. cr, uid, context=context)
  405. ast_number = self.pool['asterisk.server']._reformat_number(
  406. cr, uid, erp_number, ast_server, context=context)
  407. # The user should have a CallerID
  408. if not user.callerid:
  409. raise orm.except_orm(
  410. _('Error:'),
  411. _('No callerID configured for the current user'))
  412. variable = []
  413. if user.asterisk_chan_type == 'SIP':
  414. # We can only have one alert-info header in a SIP request
  415. if user.alert_info:
  416. variable.append(
  417. 'SIPAddHeader=Alert-Info: %s' % user.alert_info)
  418. elif ast_server.alert_info:
  419. variable.append(
  420. 'SIPAddHeader=Alert-Info: %s' % ast_server.alert_info)
  421. if user.variable:
  422. for user_variable in user.variable.split('|'):
  423. variable.append(user_variable.strip())
  424. channel = '%s/%s' % (user.asterisk_chan_type, user.resource)
  425. if user.dial_suffix:
  426. channel += '/%s' % user.dial_suffix
  427. try:
  428. ast_manager.Originate(
  429. channel,
  430. context=ast_server.context,
  431. extension=ast_number,
  432. priority=str(ast_server.extension_priority),
  433. timeout=str(ast_server.wait_time * 1000),
  434. caller_id=user.callerid,
  435. account=user.cdraccount,
  436. variable=variable)
  437. except Exception, e:
  438. _logger.error(
  439. "Error in the Originate request to Asterisk server %s"
  440. % ast_server.ip_address)
  441. _logger.error(
  442. "Here are the details of the error: '%s'" % unicode(e))
  443. raise orm.except_orm(
  444. _('Error:'),
  445. _("Click to dial with Asterisk failed.\nHere is the error: "
  446. "'%s'")
  447. % unicode(e))
  448. finally:
  449. ast_manager.Logoff()
  450. return {'dialed_number': ast_number}