4c5fcbdecddb3b3fa9a2dbf7550d1742757df0eb
[project/luci.git] / applications / luci-app-pbx / luasrc / model / cbi / pbx.lua
1 --[[
2     Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
3
4     This file is part of luci-pbx.
5
6     luci-pbx is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     luci-pbx is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with luci-pbx.  If not, see <http://www.gnu.org/licenses/>.
18 ]]--
19
20 modulename = "pbx"
21
22
23 if     nixio.fs.access("/etc/init.d/asterisk")   then
24    server = "asterisk"
25 elseif nixio.fs.access("/etc/init.d/freeswitch") then
26    server = "freeswitch"
27 else
28    server = ""
29 end
30
31
32 -- Returns formatted output of string containing only the words at the indices
33 -- specified in the table "indices".
34 function format_indices(string, indices)
35    if indices == nil then
36       return "Error: No indices to format specified.\n" 
37    end
38
39    -- Split input into separate lines.
40    lines = luci.util.split(luci.util.trim(string), "\n")
41    
42    -- Split lines into separate words.
43    splitlines = {}
44    for lpos,line in ipairs(lines) do
45       splitlines[lpos] = luci.util.split(luci.util.trim(line), "%s+", nil, true)
46    end
47    
48    -- For each split line, if the word at all indices specified
49    -- to be formatted are not null, add the formatted line to the
50    -- gathered output.
51    output = ""
52    for lpos,splitline in ipairs(splitlines) do
53       loutput = ""
54       for ipos,index in ipairs(indices) do
55          if splitline[index] ~= nil then
56             loutput = loutput .. string.format("%-40s", splitline[index])
57          else
58             loutput = nil
59             break
60          end
61       end
62       
63       if loutput ~= nil then
64          output = output .. loutput .. "\n"
65       end
66    end
67    return output
68 end
69
70
71 m = Map (modulename, translate("PBX Main Page"),
72       translate("This configuration page allows you to configure a phone system (PBX) service which \
73       permits making phone calls through multiple Google and SIP (like Sipgate, \
74       SipSorcery, and Betamax) accounts and sharing them among many SIP devices. \
75       Note that Google accounts, SIP accounts, and local user accounts are configured in the \
76       \"Google Accounts\", \"SIP Accounts\", and \"User Accounts\" sub-sections. \
77       You must add at least one User Account to this PBX, and then configure a SIP device or \
78       softphone to use the account, in order to make and receive calls with your Google/SIP \
79       accounts. Configuring multiple users will allow you to make free calls between all users, \
80       and share the configured Google and SIP accounts. If you have more than one Google and SIP \
81       accounts set up, you should probably configure how calls to and from them are routed in \
82       the \"Call Routing\" page. If you're interested in using your own PBX from anywhere in the \
83       world, then visit the \"Remote Usage\" section in the \"Advanced Settings\" page."))
84
85 -----------------------------------------------------------------------------------------
86 s = m:section(NamedSection, "connection_status", "main",
87               translate("PBX Service Status"))
88 s.anonymous = true
89
90 s:option (DummyValue, "status", translate("Service Status"))
91
92 sts = s:option(DummyValue, "_sts") 
93 sts.template = "cbi/tvalue"
94 sts.rows = 20
95
96 function sts.cfgvalue(self, section)
97
98    if server == "asterisk" then
99       regs = luci.sys.exec("asterisk -rx 'sip show registry' | sed 's/peer-//'")
100       jabs = luci.sys.exec("asterisk -rx 'jabber show connections' | grep onnected")
101       usrs = luci.sys.exec("asterisk -rx 'sip show users'")
102       chan = luci.sys.exec("asterisk -rx 'core show channels'")
103
104       return format_indices(regs, {1, 5}) ..
105              format_indices(jabs, {2, 4}) .. "\n" ..
106              format_indices(usrs, {1}   ) .. "\n" .. chan
107
108    elseif server == "freeswitch" then
109       return "Freeswitch is not supported yet.\n"
110    else
111       return "Neither Asterisk nor FreeSwitch discovered, please install Asterisk, as Freeswitch is not supported yet.\n"
112    end
113 end
114
115 return m