2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 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
|
|
|
|
def in
|
|
|
|
if params['event'] == 'newCall'
|
2017-09-08 08:28:34 +00:00
|
|
|
config_inbound = config_integration[:inbound] || {}
|
2016-04-23 09:04:33 +00:00
|
|
|
block_caller_ids = config_inbound[:block_caller_ids] || []
|
|
|
|
|
2016-04-21 07:36:25 +00:00
|
|
|
# check if call need to be blocked
|
2017-10-01 12:25:52 +00:00
|
|
|
block_caller_ids.each do |item|
|
2018-05-04 14:05:10 +00:00
|
|
|
next if item[:caller_id] != params['from']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-04-21 07:36:25 +00:00
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
2016-04-23 09:04:33 +00:00
|
|
|
content = xml.Response(onHangup: url, onAnswer: url) do
|
2016-04-21 07:36:25 +00:00
|
|
|
xml.Reject('reason' => 'busy')
|
|
|
|
end
|
|
|
|
|
|
|
|
send_data content, type: 'application/xml; charset=UTF-8;'
|
|
|
|
|
2016-04-23 09:04:33 +00:00
|
|
|
#params['Reject'] = 'busy'
|
|
|
|
params['comment'] = 'reject, busy'
|
|
|
|
if params['user']
|
|
|
|
params['comment'] = "#{params['user']} -> reject, busy"
|
|
|
|
end
|
2016-11-29 20:12:27 +00:00
|
|
|
Cti::Log.process(params)
|
2016-04-21 07:36:25 +00:00
|
|
|
return true
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|
|
|
|
|
2016-11-29 20:12:27 +00:00
|
|
|
Cti::Log.process(params)
|
2016-04-23 09:04:33 +00:00
|
|
|
|
2016-04-21 07:36:25 +00:00
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
2016-04-23 09:04:33 +00:00
|
|
|
content = xml.Response(onHangup: url, onAnswer: url)
|
2016-04-21 07:36:25 +00:00
|
|
|
send_data content, type: 'application/xml; charset=UTF-8;'
|
|
|
|
end
|
|
|
|
|
|
|
|
# set caller id of outbound call
|
|
|
|
def out
|
2017-12-07 13:57:42 +00:00
|
|
|
config_outbound = config_integration[:outbound]
|
|
|
|
routing_table = nil
|
|
|
|
default_caller_id = nil
|
|
|
|
if config_outbound.present?
|
|
|
|
routing_table = config_outbound[:routing_table]
|
|
|
|
default_caller_id = config_outbound[:default_caller_id]
|
|
|
|
end
|
2016-04-21 07:36:25 +00:00
|
|
|
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
|
|
|
|
# set callerId
|
|
|
|
content = nil
|
|
|
|
to = params[:to]
|
2016-04-23 09:04:33 +00:00
|
|
|
from = nil
|
2017-12-07 13:57:42 +00:00
|
|
|
if to && routing_table.present?
|
|
|
|
routing_table.each do |row|
|
2016-04-21 07:36:25 +00:00
|
|
|
dest = row[:dest].gsub(/\*/, '.+?')
|
|
|
|
next if to !~ /^#{dest}$/
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-04-23 09:04:33 +00:00
|
|
|
from = row[:caller_id]
|
|
|
|
content = xml.Response(onHangup: url, onAnswer: url) do
|
|
|
|
xml.Dial(callerId: from) { xml.Number(params[:to]) }
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|
|
|
|
break
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-12-07 13:57:42 +00:00
|
|
|
if !content && default_caller_id.present?
|
2016-04-23 09:04:33 +00:00
|
|
|
from = default_caller_id
|
|
|
|
content = xml.Response(onHangup: url, onAnswer: url) do
|
2016-04-21 07:36:25 +00:00
|
|
|
xml.Dial(callerId: default_caller_id) { xml.Number(params[:to]) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2016-04-23 09:04:33 +00:00
|
|
|
content = xml.Response(onHangup: url, onAnswer: url)
|
2016-04-21 07:36:25 +00:00
|
|
|
end
|
|
|
|
|
2017-12-07 13:57:42 +00:00
|
|
|
send_data(content, type: 'application/xml; charset=UTF-8;')
|
|
|
|
if from.present?
|
2016-04-23 09:04:33 +00:00
|
|
|
params['from'] = from
|
|
|
|
end
|
2016-11-29 20:12:27 +00:00
|
|
|
Cti::Log.process(params)
|
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')
|
2016-04-21 13:43:13 +00:00
|
|
|
xml_error('Feature is disable, please contact your admin to enable it!')
|
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?
|
2016-04-21 13:43:13 +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
|
|
|
|
end
|