summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/dialplan_default.lua
blob: 2b651c500b58ca9581752d4561bef045760e04a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
-- Gemeinschaft 5 default dialplan
-- (c) AMOOMA GmbH 2012-2013
-- 


function hangup_hook_caller(s, status, arg)
  log:info('HANGUP_HOOK: ', status)
  if tostring(status) == 'transfer' then
    if start_caller and start_caller.destination then
      log:info('CALL_TRANSFERRED - destination was: ', start_caller.destination.type, '=', start_caller.destination.id,', number: ' .. tostring(start_caller.destination.number) .. ', to: ' .. start_caller:to_s('sip_refer_to'));
      start_caller.auth_account            = start_caller.dialplan:object_find{class = start_caller.destination.type, id = start_caller.destination.id};
      start_caller.forwarding_number       = start_caller.destination.number;
      start_caller.forwarding_service      = 'transfer';
    end
  end
end

function input_call_back_caller(s, object_type, object_data, arg)
  if object_type == 'dtmf' then
    require 'dialplan.dtmf'
    local dtmf = dialplan.dtmf.Dtmf:new{ log = log, router = dtmf_router }:detect(start_caller, start_caller.dtmf, object_data.digit, object_data.duration);
  end
end

function input_call_back_callee(s, object_type, object_data, arg)
  if object_type == 'dtmf' then
    require 'dialplan.dtmf'
    local dtmf = dialplan.dtmf.Dtmf:new{ log = log, router = dtmf_router }:detect(start_caller, start_caller.dtmf_callee, object_data.digit, object_data.duration, true);
  end
end


-- initialize logging
require 'common.log'
log = common.log.Log:new{ prefix = '### [' .. session:get_uuid() .. '] ' };

-- caller session object
require 'dialplan.session'
start_caller = dialplan.session.Session:new{ log = log, session = session };

-- connect to database
require 'common.database'
local database = common.database.Database:new{ log = log }:connect();
if not database:connected() then
  log:critical('DIALPLAN_DEFAULT - database connect failed');
  return;
end

-- dialplan object
require 'dialplan.dialplan'

local start_dialplan = dialplan.dialplan.Dialplan:new{ log = log, caller = start_caller, database = database };
start_dialplan:configuration_read();
start_caller.dialplan = start_dialplan;
start_caller.local_node_id = start_dialplan.node_id;
start_caller:init_channel_variables();
start_caller.dtmf = {
  updated = os.time(),
  digits = '';
};
start_caller.dtmf_callee = {
  updated = os.time(),
  digits = '';
};

if start_dialplan.config.parameters.dump_variables then
  start_caller:execute('info', 'notice');
end

if start_caller.from_node and not start_dialplan:auth_node() then
  log:debug('DIALPLAN_DEFAULT - node unauthorized - node_id: ', start_caller.node_id, ', domain: ', start_dialplan.domain);
  start_dialplan:hangup(401, start_dialplan.domain);
else
  if not start_dialplan:auth_sip_account() then
    local gateway = start_dialplan:auth_gateway()

    if gateway then
      start_caller.gateway_name = gateway.name;
      start_caller.gateway_id = gateway.id;
      start_caller.from_gateway = true;
      start_caller.gateway = gateway;
    else
      log:debug('AUTHENTICATION_REQUIRED_SIP_ACCOUNT - contact host: ' , start_caller.sip_contact_host, ', ip: ', start_caller.sip_network_ip, ', domain: ', start_dialplan.domain);
      start_dialplan:hangup(407, start_dialplan.domain);
      if database then
        database:release();
      end
      return;
    end
  end
end

if start_caller.from_node then
  log:debug('AUTHENTICATION_REQUIRED_NODE - node_id: ', start_caller.node_id, ', domain: ', start_dialplan.domain);
  start_dialplan:hangup(407, start_dialplan.domain);
else
  start_destination = { type = 'unknown' }
  start_caller.session:setHangupHook('hangup_hook_caller', 'destination_number');
  
  require 'dialplan.router'
  dtmf_router =  dialplan.router.Router:new{ log = log, database = database, caller = start_caller, variables = start_caller };
  start_dialplan.dtmf_detection = #dtmf_router:read_table('dtmf') > 0;

  if start_dialplan.dtmf_detection then
    start_dialplan.detect_dtmf_after_bridge_caller = true;
    start_dialplan.detect_dtmf_after_bridge_callee = true;
    start_caller.session:setInputCallback('input_call_back_caller', 'start_dialplan');
  end

  start_dialplan:run(start_destination);
end

-- release database handle
if database then
  database:release();
end