sipgate_controller.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. require 'builder'
  3. class Integration::SipgateController < ApplicationController
  4. skip_before_action :verify_csrf_token
  5. before_action :check_configured
  6. # notify about inbound call / block inbound call
  7. def in
  8. if params['event'] == 'newCall'
  9. config_inbound = config_integration[:inbound] || {}
  10. block_caller_ids = config_inbound[:block_caller_ids] || []
  11. # check if call need to be blocked
  12. block_caller_ids.each do |item|
  13. next if item[:caller_id] != params['from']
  14. xml = Builder::XmlMarkup.new(indent: 2)
  15. xml.instruct!
  16. content = xml.Response(onHangup: url, onAnswer: url) do
  17. xml.Reject('reason' => 'busy')
  18. end
  19. send_data content, type: 'application/xml; charset=UTF-8;'
  20. #params['Reject'] = 'busy'
  21. params['comment'] = 'reject, busy'
  22. if params['user']
  23. params['comment'] = "#{params['user']} -> reject, busy"
  24. end
  25. Cti::Log.process(params)
  26. return true
  27. end
  28. end
  29. Cti::Log.process(params)
  30. xml = Builder::XmlMarkup.new(indent: 2)
  31. xml.instruct!
  32. content = xml.Response(onHangup: url, onAnswer: url)
  33. send_data content, type: 'application/xml; charset=UTF-8;'
  34. end
  35. # set caller id of outbound call
  36. def out
  37. config_outbound = config_integration[:outbound]
  38. routing_table = nil
  39. default_caller_id = nil
  40. if config_outbound.present?
  41. routing_table = config_outbound[:routing_table]
  42. default_caller_id = config_outbound[:default_caller_id]
  43. end
  44. xml = Builder::XmlMarkup.new(indent: 2)
  45. xml.instruct!
  46. # set callerId
  47. content = nil
  48. to = params[:to]
  49. from = nil
  50. if to && routing_table.present?
  51. routing_table.each do |row|
  52. dest = row[:dest].gsub(/\*/, '.+?')
  53. next if to !~ /^#{dest}$/
  54. from = row[:caller_id]
  55. content = xml.Response(onHangup: url, onAnswer: url) do
  56. xml.Dial(callerId: from) { xml.Number(params[:to]) }
  57. end
  58. break
  59. end
  60. if !content && default_caller_id.present?
  61. from = default_caller_id
  62. content = xml.Response(onHangup: url, onAnswer: url) do
  63. xml.Dial(callerId: default_caller_id) { xml.Number(params[:to]) }
  64. end
  65. end
  66. else
  67. content = xml.Response(onHangup: url, onAnswer: url)
  68. end
  69. send_data(content, type: 'application/xml; charset=UTF-8;')
  70. if from.present?
  71. params['from'] = from
  72. end
  73. Cti::Log.process(params)
  74. end
  75. private
  76. def check_configured
  77. http_log_config facility: 'sipgate.io'
  78. if !Setting.get('sipgate_integration')
  79. xml_error('Feature is disable, please contact your admin to enable it!')
  80. return
  81. end
  82. if config_integration.blank? || config_integration[:inbound].blank? || config_integration[:outbound].blank?
  83. xml_error('Feature not configured, please contact your admin!')
  84. return
  85. end
  86. true
  87. end
  88. def config_integration
  89. @config_integration ||= Setting.get('sipgate_config')
  90. end
  91. def xml_error(error)
  92. xml = Builder::XmlMarkup.new(indent: 2)
  93. xml.instruct!
  94. content = xml.Response() do
  95. xml.Error(error)
  96. end
  97. send_data content, type: 'application/xml; charset=UTF-8;', status: 422
  98. end
  99. def base_url
  100. http_type = Setting.get('http_type')
  101. fqdn = Setting.get('sipgate_alternative_fqdn')
  102. if fqdn.blank?
  103. fqdn = Setting.get('fqdn')
  104. end
  105. "#{http_type}://#{fqdn}/api/v1/sipgate"
  106. end
  107. def url
  108. "#{base_url}/#{params['direction']}"
  109. end
  110. end