sipgate_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'builder'
  3. class Integration::SipgateController < ApplicationController
  4. skip_before_action :verify_csrf_token
  5. before_action :check_configured, :check_token
  6. # notify about inbound call / block inbound call
  7. def event
  8. local_params = ActiveSupport::HashWithIndifferentAccess.new(params.permit!.to_h)
  9. cti = Cti::Driver::SipgateIo.new(params: local_params, config: config_integration)
  10. result = cti.process
  11. # check if inbound call should get rejected
  12. if result[:action] == 'reject'
  13. response_reject(result)
  14. return true
  15. end
  16. # check if outbound call changes the outbound caller_id
  17. if result[:action] == 'set_caller_id'
  18. response_set_caller_id(result)
  19. return true
  20. end
  21. if result[:action] == 'invalid_direction'
  22. response_error(__("Invalid 'direction'!"))
  23. return true
  24. end
  25. response_ok(response)
  26. true
  27. end
  28. private
  29. def check_token
  30. if Setting.get('sipgate_token') != params[:token]
  31. response_unauthorized(__('Invalid token, please contact your admin!'))
  32. return
  33. end
  34. true
  35. end
  36. def check_configured
  37. http_log_config facility: 'sipgate.io'
  38. if !Setting.get('sipgate_integration')
  39. xml_error(__('Feature is disabled, please contact your administrator!'))
  40. return
  41. end
  42. if config_integration.blank? || config_integration[:inbound].blank? || config_integration[:outbound].blank?
  43. xml_error(__('Feature not configured, please contact your admin!'))
  44. return
  45. end
  46. true
  47. end
  48. def config_integration
  49. @config_integration ||= Setting.get('sipgate_config')
  50. end
  51. def xml_error(error, code = 422)
  52. xml = Builder::XmlMarkup.new(indent: 2)
  53. xml.instruct!
  54. content = xml.Response() do
  55. xml.Error(error)
  56. end
  57. send_data content, type: 'application/xml; charset=UTF-8;', status: code
  58. end
  59. def base_url
  60. http_type = Setting.get('http_type')
  61. fqdn = Setting.get('sipgate_alternative_fqdn')
  62. if fqdn.blank?
  63. fqdn = Setting.get('fqdn')
  64. end
  65. "#{http_type}://#{fqdn}/api/v1/sipgate/#{Setting.get('sipgate_token')}"
  66. end
  67. def url
  68. "#{base_url}/#{params['direction']}"
  69. end
  70. def response_unauthorized(error)
  71. xml_error(error, 401)
  72. end
  73. def response_reject(_result)
  74. xml = Builder::XmlMarkup.new(indent: 2)
  75. xml.instruct!
  76. content = xml.Response(onHangup: url, onAnswer: url) do
  77. xml.Reject({ reason: 'busy' })
  78. end
  79. send_data content, type: 'application/xml; charset=UTF-8;'
  80. end
  81. def response_set_caller_id(result)
  82. xml = Builder::XmlMarkup.new(indent: 2)
  83. xml.instruct!
  84. content = xml.Response(onHangup: url, onAnswer: url) do
  85. xml.Dial(callerId: result[:params][:from_caller_id]) { xml.Number(result[:params][:to_caller_id]) }
  86. end
  87. send_data(content, type: 'application/xml; charset=UTF-8;')
  88. end
  89. def response_ok(_result)
  90. xml = Builder::XmlMarkup.new(indent: 2)
  91. xml.instruct!
  92. content = xml.Response(onHangup: url, onAnswer: url)
  93. send_data content, type: 'application/xml; charset=UTF-8;'
  94. end
  95. end