summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/sip_account.lua
blob: e7ee0d71777f1350d7bea085e0d051ff9790c4e0 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
-- Gemeinschaft 5 module: sip account class
-- (c) AMOOMA GmbH 2012-2013
-- 

module(...,package.seeall)

SipAccount = {}

-- Create SipAccount object
function SipAccount.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.class = 'sipaccount';
  self.log = arg.log;
  self.database = arg.database;
  self.record = arg.record;
  self.domain = arg.domain;
  return object;
end


function SipAccount.find_by_sql(self, where)
  local sql_query = 'SELECT \
    `a`.`id`, \
    `a`.`uuid`, \
    `a`.`auth_name`, \
    `a`.`caller_name`, \
    `a`.`password`, \
    `a`.`voicemail_pin`, \
    `a`.`tenant_id`, \
    `a`.`sip_domain_id`, \
    `a`.`call_waiting`, \
    `a`.`clir`, \
    `a`.`clip`, \
    `a`.`clip_no_screening`, \
    `a`.`sip_accountable_type`,  \
    `a`.`sip_accountable_id`, \
    `a`.`hotdeskable`, \
    `a`.`gs_node_id`, \
    `a`.`language_code`, \
    `b`.`host`, \
    `c`.`sip_host`, \
    `c`.`profile_name` \
    FROM `sip_accounts` `a` \
    JOIN `sip_domains` `b` ON `a`.`sip_domain_id` = `b`.`id`  \
    LEFT JOIN `sip_registrations` `c` ON `a`.`auth_name` = `c`.`sip_user` \
    WHERE ' .. where .. ' LIMIT 1';

  local sip_account = nil;
  self.database:query(sql_query, function(account_entry)
    sip_account = SipAccount:new(self);
    sip_account.record = account_entry;
    sip_account.id = tonumber(account_entry.id);
    sip_account.uuid = account_entry.uuid;
  end)

  return sip_account;
end


-- find sip account by id
function SipAccount.find_by_id(self, id)
  local sql_query = '`a`.`id`= ' .. tonumber(id);
  return self:find_by_sql(sql_query);
end

-- find sip account by uuid
function SipAccount.find_by_uuid(self, uuid)
  local sql_query = '`a`.`uuid`= "' .. uuid .. '"';
  return self:find_by_sql(sql_query);
end

-- Find SIP Account by auth_name
function SipAccount.find_by_auth_name(self, auth_name, domain)
  local sql_query = '`a`.`auth_name`= "' .. auth_name .. '"';

  if domain then
    sql_query = sql_query .. ' AND `b`.`host` = "' .. domain .. '"';
  end

  return self:find_by_sql(sql_query);
end

-- retrieve Phone Numbers for SIP Account
function SipAccount.phone_numbers(self, id)
  id = id or self.record.id;
  if not id then
    return false;
  end

  local sql_query = "SELECT * FROM `phone_numbers` WHERE `phone_numberable_type` = \"SipAccount\" AND `phone_numberable_id`=" .. self.record.id;
  local phone_numbers = {}

  self.database:query(sql_query, function(entry)
    table.insert(phone_numbers,entry.number);
  end)

  return phone_numbers;
end

-- retrieve Ringtone for SIP Account
function SipAccount.ringtone(self, id)
  id = id or self.record.id;
  if not id then
    return false;
  end

  local sql_query = "SELECT * FROM `ringtones` WHERE `ringtoneable_type` = \"SipAccount\" AND `ringtoneable_id`=" .. self.record.id .. " LIMIT 1";
  local ringtone = nil;

  self.database:query(sql_query, function(entry)
    ringtone = entry;
  end)

  return ringtone;
end

function SipAccount.send_text(self, text)
  local event = freeswitch.Event("NOTIFY");
  event:addHeader("profile", "gemeinschaft");
  event:addHeader("event-string", "text");
  event:addHeader("user", self.record.auth_name);
  event:addHeader("host", self.record.host);
  event:addHeader("content-type", "text/plain");
  event:addBody(text);
  event:fire();
end


function SipAccount.call_state(self)
  local sql_query = 'SELECT `callstate` FROM `calls_active` \
    WHERE `sip_account_id` = ' .. self.id .. ' \
    OR `b_sip_account_id` = ' .. self.id .. ' \
    LIMIT 1';

  return self.database:query_return_value(sql_query);
end


function SipAccount.call_forwarding_on(self, service, destination, destination_type, timeout, source)
  
  if not self.call_forwarding then
    require 'common.call_forwarding';
    self.call_forwarding = common.call_forwarding.CallForwarding:new{ log = self.log, database = self.database, parent = self, domain = self.domain };
  end

  return self.call_forwarding:call_forwarding_on(service, destination, destination_type, timeout, source)
end


function SipAccount.call_forwarding_off(self, service, source, delete)
  if not self.call_forwarding then
    require 'common.call_forwarding';
    self.call_forwarding = common.call_forwarding.CallForwarding:new{ log = self.log, database = self.database, parent = self, domain = self.domain };
  end

  return self.call_forwarding:call_forwarding_off(service, source, delete)
end