f73930eee301c511be34629641ccae65c5333eb3
[project/luci.git] / applications / luci-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 if     nixio.fs.access("/etc/init.d/asterisk")   then
21    server = "asterisk"
22 elseif nixio.fs.access("/etc/init.d/freeswitch") then
23    server = "freeswitch"
24 else
25    server = ""
26 end
27
28 modulename = "pbx"
29
30 function mysplit(inputstr, sep)
31         if sep == nil then
32                 sep = "%s"
33         end
34         t={} ; i=1
35         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
36                 t[i] = str
37                 i = i + 1
38         end
39         return t
40 end
41
42 function format_two_indices(string, ind1, ind2)
43         lines=mysplit(string, "\n")
44
45         words={}
46         for index,value in ipairs(lines) do
47                 words[index]=mysplit(value)
48         end
49
50         output = ""
51         for index,value in ipairs(words) do
52                 if value[ind1] ~= nil and value[ind2] ~= nil then
53                         output = output .. string.format("%-40s \t %-20s\n", value[ind1], value[ind2])
54                 end
55         end
56         return output
57 end
58
59 function format_one_index(string, ind1)
60         lines=mysplit(string, "\n")
61
62         words={}
63         for index,value in ipairs(lines) do
64                 words[index]=mysplit(value)
65         end
66
67         output = ""
68         for index,value in ipairs(words) do
69                 if value[ind1] ~= nil then
70                         output = output .. string.format("%-40s\n", value[ind1])
71                 end
72         end
73         return output
74 end
75
76 m = Map (modulename, translate("PBX Main Page"),
77          translate("This configuration page allows you to configure a phone system (PBX) service which\
78                    permits making phone calls with, and sharing multiple Google and SIP (like Sipgate,\
79                    SipSorcery, and Betamax) accounts among many SIP devices. Note that Google\
80                    accounts, SIP accounts, and local user accounts are configured in the\
81                    \"Google Accounts\", \"SIP Accounts\", and \"User Accounts\" sub-sections.\
82       You must configure at least one local SIP account\
83       on this PBX, to make and receive calls with your Google/SIP accounts.\
84       Configuring multiple users will allow you to make free calls between users, and share the configured\
85       Google and SIP accounts. If you have more than one Google and SIP accounts set up,\
86       you should probably configure how calls to and from them are routed in the \"Call Routing\" page.\
87       If you're interested in using your own PBX from anywhere in the world,\
88       then visit the \"Remote Usage\" section in the \"Advanced Settings\" page."))
89
90 ----------------------------------------------------------------------------------------------------
91 s = m:section(NamedSection, "connection_status", "main", translate("Service Control and Connection Status"))
92 s.anonymous = true
93
94 s:option (DummyValue, "status", translate("Service Status"))
95
96 sts = s:option(DummyValue, "_sts") 
97 sts.template = "cbi/tvalue"
98 sts.rows = 20
99
100 function sts.cfgvalue(self, section)
101
102    if server == "asterisk" then
103       reg  = luci.sys.exec("asterisk -rx 'sip show registry' | sed 's/peer-//'")
104       jab  = luci.sys.exec("asterisk -rx 'jabber show connections' | grep onnected")
105       usrs = luci.sys.exec("asterisk -rx 'sip show users'")
106       chan = luci.sys.exec("asterisk -rx 'core show channels'")
107       return format_two_indices(reg, 1, 5) .. format_two_indices(jab, 2, 4) .. "\n" 
108              .. format_one_index(usrs,1) .. "\n" .. chan
109    elseif server == "freeswitch" then
110       return "Freeswitch is not supported yet.\n"
111    else
112       return "Neither Asterisk nor FreeSwitch discovered, please install Asterisk, as Freeswitch is not supported yet.\n"
113    end
114 end
115
116 return m