summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/sip_account.lua
diff options
context:
space:
mode:
Diffstat (limited to 'misc/freeswitch/scripts/common/sip_account.lua')
-rw-r--r--misc/freeswitch/scripts/common/sip_account.lua137
1 files changed, 137 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/sip_account.lua b/misc/freeswitch/scripts/common/sip_account.lua
new file mode 100644
index 0000000..28a00df
--- /dev/null
+++ b/misc/freeswitch/scripts/common/sip_account.lua
@@ -0,0 +1,137 @@
+-- Gemeinschaft 5 module: sip account class
+-- (c) AMOOMA GmbH 2012
+--
+
+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;
+ 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`, \
+ `b`.`host` \
+ FROM `sip_accounts` `a` JOIN `sip_domains` `b` ON `a`.`sip_domain_id` = `b`.`id` \
+ 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 state = nil
+ local sql_query = "SELECT `callstate` FROM `channels` \
+ WHERE `name` LIKE (\"\%" .. self.record.auth_name .. "@%\") \
+ OR `name` LIKE (\"\%" .. self.record.auth_name .. "@%\") LIMIT 1";
+
+ self.database:query(sql_query, function(channel_entry)
+ state = channel_entry.callstate;
+ end)
+
+ return state;
+end