summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/dialplan/presence.lua
blob: 234b9089882ef99b7a9252e8f6492d1128d89c9d (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
-- Gemeinschaft 5 module: presence class
-- (c) AMOOMA GmbH 2012
-- 

module(...,package.seeall)

Presence = {}

-- Create Presence object
function Presence.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self)
  self.__index = self
  self.log = arg.log;
  self.domain = arg.domain;
  self.uuid = arg.uuid;
  self.inbound = arg.inbound;
  self.accounts = arg.accounts;

  return object
end


function Presence.init(self, arg)
  self.log = arg.log or self.log;
  self.domain = arg.domain or self.domain;
  self.uuid = arg.uuid or self.uuid;
  self.inbound = arg.inbound or self.inbound;
  self.accounts = arg.accounts or self.accounts;
end


function Presence.set(self, state, caller_number)
  if not self.accounts or #self.accounts == 0 then
    return nil;
  end

  state = state or "terminated";
  local direction = "outbound";
  
  if self.inbound then
    direction = "inbound";
  end

  for index, account in pairs(self.accounts) do
    if account ~= '' then
      local event = freeswitch.Event('PRESENCE_IN');
      event:addHeader('proto', 'sip');
      event:addHeader('from', account .. '@' .. self.domain);
      event:addHeader('event_type', 'presence');
      event:addHeader('alt_event_type', 'dialog');
      event:addHeader('presence-call-direction', direction);
      event:addHeader('answer-state', state);
      event:addHeader('unique-id', self.uuid);
      if caller_number then
        if self.inbound then
          event:addHeader('Caller-Destination-Number', caller_number);
        else
          event:addHeader('Other-Leg-Caller-ID-Number', caller_number);
        end
      end
      event:fire();
      self.log:debug('PRESENCE - account: ' .. account .. '@' .. self.domain .. ', state: ' .. state .. ', direction: ' .. direction .. ', uid: ' ..self.uuid);
    end
  end

  return true;
end


function Presence.early(self, caller_number)
  return self:set("early", caller_number);
end


function Presence.confirmed(self, caller_number)
  return self:set("confirmed", caller_number);
end


function Presence.terminated(self, caller_number)
  return self:set("terminated", caller_number);
end