sipgate_controller.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 unless 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][:routing_table]
  38. default_caller_id = config_integration[:outbound][:default_caller_id]
  39. xml = Builder::XmlMarkup.new(indent: 2)
  40. xml.instruct!
  41. # set callerId
  42. content = nil
  43. to = params[:to]
  44. from = nil
  45. if to
  46. config_outbound.each do |row|
  47. dest = row[:dest].gsub(/\*/, '.+?')
  48. next if to !~ /^#{dest}$/
  49. from = row[:caller_id]
  50. content = xml.Response(onHangup: url, onAnswer: url) do
  51. xml.Dial(callerId: from) { xml.Number(params[:to]) }
  52. end
  53. break
  54. end
  55. if !content && default_caller_id
  56. from = default_caller_id
  57. content = xml.Response(onHangup: url, onAnswer: url) do
  58. xml.Dial(callerId: default_caller_id) { xml.Number(params[:to]) }
  59. end
  60. end
  61. else
  62. content = xml.Response(onHangup: url, onAnswer: url)
  63. end
  64. send_data content, type: 'application/xml; charset=UTF-8;'
  65. if from
  66. params['from'] = from
  67. end
  68. Cti::Log.process(params)
  69. end
  70. private
  71. def check_configured
  72. http_log_config facility: 'sipgate.io'
  73. if !Setting.get('sipgate_integration')
  74. xml_error('Feature is disable, please contact your admin to enable it!')
  75. return
  76. end
  77. if !config_integration || !config_integration[:inbound] || !config_integration[:outbound]
  78. xml_error('Feature not configured, please contact your admin!')
  79. return
  80. end
  81. end
  82. def config_integration
  83. @config_integration ||= Setting.get('sipgate_config')
  84. end
  85. def xml_error(error)
  86. xml = Builder::XmlMarkup.new(indent: 2)
  87. xml.instruct!
  88. content = xml.Response() do
  89. xml.Error(error)
  90. end
  91. send_data content, type: 'application/xml; charset=UTF-8;', status: 422
  92. end
  93. def base_url
  94. http_type = Setting.get('http_type')
  95. fqdn = Setting.get('fqdn')
  96. "#{http_type}://#{fqdn}/api/v1/sipgate"
  97. end
  98. def url
  99. "#{base_url}/#{params['direction']}"
  100. end
  101. end