2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2016-04-21 07:36:25 +00:00
|
|
|
require 'builder'
|
|
|
|
|
|
|
|
class Integration::SipgateController < ApplicationController
|
2017-02-15 12:29:25 +00:00
|
|
|
skip_before_action :verify_csrf_token
|
2016-11-30 07:11:44 +00:00
|
|
|
before_action :check_configured
|
2016-11-29 19:57:03 +00:00
|
|
|
|
2016-04-21 07:36:25 +00:00
|
|
|
# notify about inbound call / block inbound call
|
2019-07-09 22:07:32 +00:00
|
|
|
def event
|
2016-04-21 07:36:25 +00:00
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
local_params = ActiveSupport::HashWithIndifferentAccess.new(params.permit!.to_h)
|
2016-04-23 09:04:33 +00:00
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
cti = Cti::Driver::SipgateIo.new(params: local_params, config: config_integration)
|
2016-04-21 07:36:25 +00:00
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
result = cti.process
|
2016-04-21 07:36:25 +00:00
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
# check if inbound call should get rejected
|
|
|
|
if result[:action] == 'reject'
|
|
|
|
response_reject(result)
|
|
|
|
return true
|
|
|
|
end
|
2016-04-21 07:36:25 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# check if outbound call changes the outbound caller_id
|
2019-07-09 22:07:32 +00:00
|
|
|
if result[:action] == 'set_caller_id'
|
|
|
|
response_set_caller_id(result)
|
|
|
|
return true
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
if result[:action] == 'invalid_direction'
|
2021-11-15 15:58:19 +00:00
|
|
|
response_error(__('Invalid direction!'))
|
2019-07-09 22:07:32 +00:00
|
|
|
return true
|
2016-04-23 09:04:33 +00:00
|
|
|
end
|
2019-07-09 22:07:32 +00:00
|
|
|
|
|
|
|
response_ok(response)
|
|
|
|
true
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-29 19:57:03 +00:00
|
|
|
def check_configured
|
|
|
|
http_log_config facility: 'sipgate.io'
|
|
|
|
|
2016-04-21 07:36:25 +00:00
|
|
|
if !Setting.get('sipgate_integration')
|
2021-12-15 21:00:53 +00:00
|
|
|
xml_error(__('Feature is disabled, please contact your administrator!'))
|
2016-11-29 19:57:03 +00:00
|
|
|
return
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|
2017-12-07 13:57:42 +00:00
|
|
|
if config_integration.blank? || config_integration[:inbound].blank? || config_integration[:outbound].blank?
|
2021-11-15 15:58:19 +00:00
|
|
|
xml_error(__('Feature not configured, please contact your admin!'))
|
2016-11-29 19:57:03 +00:00
|
|
|
return
|
2016-04-21 13:43:13 +00:00
|
|
|
end
|
2017-11-23 08:09:44 +00:00
|
|
|
|
|
|
|
true
|
2016-04-21 13:43:13 +00:00
|
|
|
end
|
|
|
|
|
2017-09-08 08:28:34 +00:00
|
|
|
def config_integration
|
|
|
|
@config_integration ||= Setting.get('sipgate_config')
|
2016-11-29 19:57:03 +00:00
|
|
|
end
|
2016-04-23 09:04:33 +00:00
|
|
|
|
2016-04-21 13:43:13 +00:00
|
|
|
def xml_error(error)
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
content = xml.Response() do
|
|
|
|
xml.Error(error)
|
|
|
|
end
|
2016-04-23 09:04:33 +00:00
|
|
|
send_data content, type: 'application/xml; charset=UTF-8;', status: 422
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def base_url
|
|
|
|
http_type = Setting.get('http_type')
|
2018-10-10 13:26:32 +00:00
|
|
|
fqdn = Setting.get('sipgate_alternative_fqdn')
|
|
|
|
if fqdn.blank?
|
|
|
|
fqdn = Setting.get('fqdn')
|
|
|
|
end
|
2016-04-21 07:36:25 +00:00
|
|
|
"#{http_type}://#{fqdn}/api/v1/sipgate"
|
|
|
|
end
|
|
|
|
|
2016-04-23 09:04:33 +00:00
|
|
|
def url
|
|
|
|
"#{base_url}/#{params['direction']}"
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|
2019-07-09 22:07:32 +00:00
|
|
|
|
|
|
|
def response_reject(_result)
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
content = xml.Response(onHangup: url, onAnswer: url) do
|
|
|
|
xml.Reject({ reason: 'busy' })
|
|
|
|
end
|
|
|
|
send_data content, type: 'application/xml; charset=UTF-8;'
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_set_caller_id(result)
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
content = xml.Response(onHangup: url, onAnswer: url) do
|
|
|
|
xml.Dial(callerId: result[:params][:from_caller_id]) { xml.Number(result[:params][:to_caller_id]) }
|
|
|
|
end
|
|
|
|
send_data(content, type: 'application/xml; charset=UTF-8;')
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_ok(_result)
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
content = xml.Response(onHangup: url, onAnswer: url)
|
|
|
|
send_data content, type: 'application/xml; charset=UTF-8;'
|
|
|
|
end
|
|
|
|
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|