summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/dialplan/dtmf.lua
blob: 64f538e91f1ad231390a893f329e86ceb4f3bd5c (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
-- Gemeinschaft 5 module: dtmf class
-- (c) AMOOMA GmbH 2013
-- 

module(...,package.seeall)

Dtmf = {}

-- create dtmf object
function Dtmf.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.class = 'Dtmf';
  self.log = arg.log;
  self.bleg = arg.bleg
  self.digit_timeout = arg.digit_timeout or 5;
  self.router = arg.router;
  self.digit_duration_min = arg.digit_duration_min or 500;

  return object;
end


function Dtmf.detect(self, caller, sequence, digit, duration, calee)
  if not tonumber(duration) or duration < self.digit_duration_min then
    return;
  end

  local timestamp = os.time();
  if timestamp - sequence.updated > self.digit_timeout then
    sequence.digits = digit;
  else
    sequence.digits = sequence.digits .. digit;
  end

  caller.dtmf_digits = sequence.digits;

  if calee then
    self.log:debug('DTMF_RECEIVER callee - digit: [', digit, '][', duration, '], sequence: ', sequence.digits);
  else
    self.log:debug('DTMF_RECEIVER caller - digit: [', digit, '][', duration, '], sequence: ', sequence.digits);
  end

  local route = self.router:route_run('dtmf', true);
  sequence.updated = timestamp;

  if not route then
    return;
  end

  if route.type == 'dialplanfunction' or route.type == 'phonenumber' or route.type == 'unknown' then
    self:transfer(caller, route.destination_number, calee)
  else
    self.log:notice('DTMF_RECEIVER - unhandled destination: ', route.type, '=', route.id);
  end
end


function Dtmf.transfer(self, caller, destination, calee)
  require 'common.fapi'
  local fapi = common.fapi.FApi:new{ log = log };
  local callee_uuid = caller:to_s('bridge_to');
  
  self.log:notice('DTMF_RECEIVER_TRANSFER - destination: ', destination, ', uuid: ', caller.uuid, ', callee_uuid: ', callee_uuid, ', callee_initiated: ', calee);
  if calee then
    caller:execute('transfer', destination);
    fapi:execute('uuid_kill', callee_uuid);
  else
    if caller.account then
      fapi:execute('uuid_setvar_multi', callee_uuid .. ' gs_auth_account_type=' .. caller.account.class .. ';gs_auth_account_uuid=' .. caller.account.uuid);
    end
    fapi:execute('uuid_transfer', callee_uuid .. ' ' .. destination);
    caller.session:hangup();
  end
end