Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.04 KB | None | 0 0
  1. --
  2. -- FusionPBX
  3. -- Version: MPL 1.1
  4. --
  5. -- The contents of this file are subject to the Mozilla Public License Version
  6. -- 1.1 (the "License"); you may not use this file except in compliance with
  7. -- the License. You may obtain a copy of the License at
  8. -- http://www.mozilla.org/MPL/
  9. --
  10. -- Software distributed under the License is distributed on an "AS IS" basis,
  11. -- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. -- for the specific language governing rights and limitations under the
  13. -- License.
  14. --
  15. -- The Original Code is FusionPBX
  16. --
  17. -- The Initial Developer of the Original Code is
  18. -- Mark J Crane <markjcrane@fusionpbx.com>
  19. -- Copyright (C) 2010 - 2016
  20. -- the Initial Developer. All Rights Reserved.
  21. --
  22. -- Contributor(s):
  23. -- Mark J Crane <markjcrane@fusionpbx.com>
  24. -- Errol W Samuels <ewsamuels@gmail.com>
  25.  
  26. --user defined variables
  27. local extension = argv[1];
  28. local direction = argv[2] or extension and 'inbound' or 'all';
  29.  
  30. -- we can use any number because other box should check sip_h_X_*** headers first
  31. local pickup_number = '*8' -- extension and '**' or '*8'
  32.  
  33. --set the debug options
  34. debug["sql"] = false;
  35.  
  36. --include config.lua
  37. require "resources.functions.config";
  38.  
  39. --add the function
  40. require "resources.functions.explode";
  41. require "resources.functions.trim";
  42. require "resources.functions.channel_utils";
  43.  
  44. --prepare the api object
  45. api = freeswitch.API();
  46.  
  47. --Get intercept logger
  48. local log = require "resources.functions.log".intercept
  49.  
  50. --include database class
  51. local Database = require "resources.functions.database"
  52.  
  53. --get the hostname
  54. local hostname = trim(api:execute("switchname", ""));
  55.  
  56. -- redirect call to another box
  57. local function make_proxy_call(destination, call_hostname)
  58. destination = destination .. "@" .. domain_name
  59. local profile, proxy = "internal", call_hostname;
  60.  
  61. local sip_auth_username = session:getVariable("sip_auth_username");
  62. local sip_auth_password = api:execute("user_data", sip_auth_username .. "@" .. domain_name .." param password");
  63. local auth = "sip_auth_username="..sip_auth_username..",sip_auth_password='"..sip_auth_password.."'"
  64. dial_string = "{sip_invite_domain=" .. domain_name .. "," .. auth .. "}sofia/" .. profile .. "/" .. destination .. ";fs_path=sip:" .. proxy;
  65. log.notice("Send call to other host....");
  66. session:execute("bridge", dial_string);
  67. end
  68.  
  69. -- check pin number if defined
  70. local function pin(pin_number)
  71. if not pin_number then
  72. return true
  73. end
  74.  
  75. --sleep
  76. session:sleep(500);
  77. --get the user pin number
  78. local min_digits = 2;
  79. local max_digits = 20;
  80. local max_tries = "3";
  81. local digit_timeout = "5000";
  82. local digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+");
  83.  
  84. --validate the user pin number
  85. local pin_number_table = explode(",",pin_number);
  86. for index,pin_number in pairs(pin_number_table) do
  87. if (digits == pin_number) then
  88. --set the authorized pin number that was used
  89. session:setVariable("pin_number", pin_number);
  90. --done
  91. return true;
  92. end
  93. end
  94.  
  95. --if not authorized play a message and then hangup
  96. session:streamFile("phrase:voicemail_fail_auth:#");
  97. session:hangup("NORMAL_CLEARING");
  98. return;
  99. end
  100.  
  101. -- do intercept if we get redirected request from another box
  102. local function proxy_intercept()
  103. -- Proceed calls from other boxes
  104.  
  105. -- Check if this call from other box with setted intercept_uuid
  106. local intercept_uuid = session:getVariable("sip_h_X-intercept_uuid")
  107.  
  108. if intercept_uuid and #intercept_uuid > 0 then
  109. log.notice("Get intercept_uuid from sip header. Do intercept....")
  110. session:execute("intercept", intercept_uuid)
  111. return true
  112. end
  113.  
  114. -- Check if this call from other box and we need parent uuid for channel
  115. local child_intercept_uuid = session:getVariable("sip_h_X-child_intercept_uuid")
  116. if (not child_intercept_uuid) or (#child_intercept_uuid == 0) then
  117. return
  118. end
  119.  
  120. -- search parent uuid
  121. log.notice("Get child_intercept_uuid from sip header.")
  122. local parent_uuid =
  123. channel_variable(child_intercept_uuid, 'ent_originate_aleg_uuid') or
  124. channel_variable(child_intercept_uuid, 'cc_member_session_uuid') or
  125. channel_variable(child_intercept_uuid, 'fifo_bridge_uuid') or
  126. child_intercept_uuid
  127.  
  128. if parent_uuid == child_intercept_uuid then
  129. log.notice("Can not found parent call. Try intercept child.")
  130. session:execute("intercept", child_intercept_uuid)
  131. return true
  132. end
  133.  
  134. -- search parent hostname
  135. call_hostname = hostname
  136. --[[ parent and child have to be on same box so we do not search it
  137. log.notice("Found parent channel try detect parent hostname")
  138. local dbh = Database.new('switch')
  139. local sql = "SELECT hostname FROM channels WHERE uuid='" .. parent_uuid .. "'"
  140. local call_hostname = dbh:first_value(sql)
  141. dbh:release()
  142.  
  143. if not call_hostname then
  144. log.notice("Can not find host name. Channels is dead?")
  145. return true
  146. end
  147. --]]
  148.  
  149. if hostname == call_hostname then
  150. log.notice("Found parent call on local machine. Do intercept....")
  151. session:execute("intercept", parent_uuid);
  152. return true
  153. end
  154.  
  155. log.noticef("Found parent call on remote machine `%s`.", call_hostname)
  156. session:execute("export", "sip_h_X-intercept_uuid="..parent_uuid);
  157. make_proxy_call(pickup_number, call_hostname)
  158. return true
  159. end
  160.  
  161. -- return array of extensions for group
  162. local function select_group_extensions()
  163. -- connect to Fusion database
  164. local dbh = Database.new('system');
  165.  
  166. --get the call groups the extension is a member of
  167. local sql = "SELECT call_group FROM v_extensions ";
  168. sql = sql .. "WHERE domain_uuid = '"..domain_uuid.."' ";
  169. sql = sql .. "AND (extension = '"..caller_id_number.."'";
  170. sql = sql .. "OR number_alias = '"..caller_id_number.."')";
  171. sql = sql .. "limit 1";
  172. local call_group = dbh:first_value(sql) or ''
  173. log.noticef("call_group: `%s`", call_group);
  174. call_groups = explode(",", call_group);
  175.  
  176. --get the extensions in the call groups
  177. sql = "SELECT extension, number_alias FROM v_extensions ";
  178. sql = sql .. "WHERE domain_uuid = '"..domain_uuid.."' ";
  179. sql = sql .. "AND (";
  180. for key,call_group in ipairs(call_groups) do
  181. if (key > 1) then
  182. sql = sql .. "OR ";
  183. end
  184. if (#call_group > 0) then
  185. sql = sql .. "call_group like '%"..call_group.."%' ";
  186. else
  187. sql = sql .. "call_group = '' ";
  188. end
  189. end
  190. sql = sql .. ") ";
  191. if (debug["sql"]) then
  192. log.notice("sql "..sql);
  193. end
  194. local extensions = {}
  195. dbh:query(sql, function(row)
  196. local member = row.extension
  197. if row.number_alias and #row.number_alias > 0 then
  198. member = row.number_alias
  199. end
  200. extensions[#extensions+1] = member
  201. log.noticef("member `%s`", member)
  202. end);
  203.  
  204. -- release Fusion database
  205. dbh:release()
  206.  
  207. -- return result
  208. return extensions
  209. end
  210.  
  211. --check if the session is ready
  212. if ( session:ready() ) then
  213. --answer the session
  214. session:answer();
  215. --get session variables
  216. domain_uuid = session:getVariable("domain_uuid");
  217. domain_name = session:getVariable("domain_name");
  218. pin_number = session:getVariable("pin_number");
  219. context = session:getVariable("context");
  220. caller_id_number = session:getVariable("caller_id_number");
  221. end
  222.  
  223. --check if the session is ready
  224. if ( session:ready() ) then
  225. if proxy_intercept() then
  226. return
  227. end
  228. end
  229.  
  230. --check if the session is ready
  231. if ( session:ready() ) then
  232. --if the pin number is provided then require it
  233. if not pin(pin_number) then
  234. return
  235. end
  236. end
  237.  
  238. if ( session:ready() ) then
  239. -- select intercept mode
  240. if not extension then
  241. log.notice("GROUP INTERCEPT")
  242. extensions = select_group_extensions()
  243. else
  244. log.noticef("INTERCEPT %s", extension)
  245. extensions = {extension}
  246. end
  247.  
  248. --connect to FS database
  249. --local dbh = Database.new('switch')
  250. if (file_exists(database_dir.."/core.db")) then
  251. --dbh = freeswitch.Dbh("core:core"); -- when using sqlite
  252. dbh = freeswitch.Dbh("sqlite://"..database_dir.."/core.db");
  253. else
  254. dofile(scripts_dir.."/resources/functions/database_handle.lua");
  255. dbh = database_handle('switch');
  256. end
  257.  
  258. --check the database to get the uuid of a ringing call
  259. call_hostname = "";
  260. sql = "SELECT uuid, call_uuid, hostname FROM channels ";
  261. sql = sql .. "WHERE callstate in ('RINGING', 'EARLY') ";
  262. -- next check should prevent pickup call from extension
  263. -- e.g. if extension 100 dial some cell phone and some one else dial *8
  264. -- he can pickup this call.
  265. if not direction:find('all') then
  266. sql = sql .. "AND (1 <> 1 "
  267. -- calls from freeswitch to user
  268. if direction:find('inbound') then
  269. sql = sql .. "OR direction = 'outbound' ";
  270. end
  271.  
  272. -- calls from user to freeswitch
  273. if direction:find('outbound') then
  274. sql = sql .. "OR direction = 'inbound' ";
  275. end
  276. sql = sql .. ")"
  277. end
  278.  
  279. sql = sql .. "AND (1<>1 ";
  280. for key,extension in pairs(extensions) do
  281. sql = sql .. "OR presence_id = '"..extension.."@"..domain_name.."' ";
  282. end
  283. sql = sql .. ") ";
  284. sql = sql .. "and call_uuid is not null ";
  285. sql = sql .. "limit 1 ";
  286. if (debug["sql"]) then
  287. log.notice("sql "..sql);
  288. end
  289. local is_child
  290. dbh:query(sql, function(row)
  291. -- for key, val in pairs(row) do
  292. -- log.notice("row "..key.." "..val);
  293. -- end
  294. -- log.notice("-----------------------");
  295. is_child = (row.uuid == row.call_uuid)
  296. uuid = row.call_uuid;
  297. old_uuid=row.uuid;
  298. call_hostname = row.hostname;
  299. end);
  300.  
  301. if (uuid ~= nil) then
  302. mydump=api:execute("uuid_dump", uuid)
  303. if mydump=='-ERR No such channel!\n'
  304. then
  305. log.notice("BIG FAIL!")
  306. a1_uuid = api:executeString("uuid_getvar "..old_uuid.." transfer_source")
  307. log.notice("a1_uuid::"..a1_uuid)
  308. local a,b
  309. a,b=string.find(a1_uuid, "uuid_br:")
  310. good_uuid=string.sub(a1_uuid, b+1)
  311. log.notice("good_uuid"..good_uuid)
  312. uuid=good_uuid
  313. else log.notice("It Works!")
  314. end
  315. end
  316.  
  317. if is_child then
  318. -- we need intercept `parent` call e.g. call in FIFO/CallCenter Queue
  319. if (call_hostname == hostname) then
  320. log.notice("Found child call on local machine. Try find parent channel.")
  321. local parent_uuid =
  322. channel_variable(uuid, 'ent_originate_aleg_uuid') or
  323. channel_variable(uuid, 'cc_member_session_uuid') or
  324. channel_variable(uuid, 'fifo_bridge_uuid') or
  325. uuid
  326.  
  327. --[[ parent and child have to be on same box so we do not search it
  328. if parent_uuid ~= uuid then
  329. local sql = "SELECT hostname FROM channels WHERE uuid='" .. uuid .. "'"
  330. call_hostname = dbh:first_value(sql)
  331. end
  332. --]]
  333.  
  334. if call_hostname then
  335. uuid = parent_uuid
  336. if call_hostname ~= hostname then
  337. log.noticef("Found parent call on remote machine `%s`.", call_hostname)
  338. else
  339. log.notice("Found parent call on local machine.")
  340. end
  341. end
  342.  
  343. else
  344. log.noticef("Found child call on remote machine `%s`.", call_hostname)
  345. -- we can not find parent on this box because channel on other box so we have to
  346. -- forward call to this box
  347. session:execute("export", "sip_h_X-child_intercept_uuid="..uuid);
  348. return make_proxy_call(pickup_number, call_hostname)
  349. end
  350. end
  351.  
  352. --release FS database
  353. dbh:release()
  354. end
  355.  
  356. log.noticef( "Hostname: %s Call Hostname: %s", hostname, call_hostname);
  357.  
  358. --intercept a call that is ringing
  359. if (uuid ~= nil) then
  360. if (session:getVariable("billmsec") == nil) then
  361. if (hostname == call_hostname) then
  362. session:execute("intercept", uuid);
  363. else
  364. session:execute("export", "sip_h_X-intercept_uuid="..uuid);
  365. make_proxy_call(pickup_number, call_hostname)
  366. end
  367. end
  368. end
  369.  
  370. --notes
  371. --originate a call
  372. --cmd = "originate user/1007@voip.example.com &intercept("..uuid..")";
  373. --api = freeswitch.API();
  374. --result = api:executeString(cmd);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement