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