summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-pbx/luasrc/model/cbi/pbx.lua
blob: 8a45740e25286044b750aea1158b3bbbf46e8c22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
--[[
    Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>

    This file is part of luci-pbx.

    luci-pbx is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    luci-pbx is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with luci-pbx.  If not, see <http://www.gnu.org/licenses/>.
]]--

modulename = "pbx"

if     nixio.fs.access("/etc/init.d/asterisk")   then
   server = "asterisk"
elseif nixio.fs.access("/etc/init.d/freeswitch") then
   server = "freeswitch"
else
   server = ""
end

-- My implementation of the function which splits a string into tokens
-- at the specified separator and returns a table containing the tokens.
function mysplit(inputstr, sep)
        if sep == nil then
                sep = "%s"
        end
        t={} ; i=1
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                t[i] = str
                i = i + 1
        end
        return t
end

-- Returns formatted output of string containing only the words at the indices
-- specified in the table "indices".
function format_indices(string, indices)
   if indices == nil then
      return "Error: No indices to format specified.\n" 
   end

   -- Split input into separate lines.
   lines = {}
   lines = mysplit(string, "\n")
   
   -- Split lines into separate words.
   splitlines = {}
   for lpos,line in ipairs(lines) do
      splitlines[lpos] = mysplit(line)
   end
   
   -- For each split line, if the word at all indices specified
   -- to be formatted are not null, add the formatted line to the
   -- gathered output.
   output = ""
   for lpos,splitline in ipairs(splitlines) do
      loutput = ""
      for ipos,index in ipairs(indices) do
	 if splitline[index] ~= nil then
	    loutput = loutput .. string.format("%-50s", splitline[index])
	 else
	    loutput = nil
	    break
	 end
      end

      if loutput ~= nil then
	 output = output .. loutput .. "\n"
      end
   end
   return output
end


m = Map (modulename, translate("PBX Main Page"),
 translate("This configuration page allows you to configure a phone system (PBX) service which\
      permits making phone calls through multiple Google and SIP (like Sipgate,\
      SipSorcery, and Betamax) accounts and sharing them among many SIP devices. \
      Note that Google accounts, SIP accounts, and local user accounts are configured in the \
      \"Google Accounts\", \"SIP Accounts\", and \"User Accounts\" sub-sections. \
      You must add at least one User Account to this PBX, and then configure a SIP device or \
      softphone to use the account, in order to make and receive calls with your Google/SIP \
      accounts. Configuring multiple users will allow you to make free calls between all users,\
      and share the configured Google and SIP accounts. If you have more than one Google and SIP \
      accounts set up, you should probably configure how calls to and from them are routed in \
      the \"Call Routing\" page. If you're interested in using your own PBX from anywhere in the\
      world, then visit the \"Remote Usage\" section in the \"Advanced Settings\" page."))

-----------------------------------------------------------------------------------------
s = m:section(NamedSection, "connection_status", "main",
	      translate("Service Control and Connection Status"))
s.anonymous = true

s:option (DummyValue, "status", translate("Service Status"))

sts = s:option(DummyValue, "_sts") 
sts.template = "cbi/tvalue"
sts.rows = 20

function sts.cfgvalue(self, section)

   if server == "asterisk" then
      regs = luci.sys.exec("asterisk -rx 'sip show registry' | sed 's/peer-//'")
      jabs = luci.sys.exec("asterisk -rx 'jabber show connections' | grep onnected")
      usrs = luci.sys.exec("asterisk -rx 'sip show users'")
      chan = luci.sys.exec("asterisk -rx 'core show channels'")

      return format_indices(regs, {1, 5}) ..
             format_indices(jabs, {2, 4}) .. "\n" ..
	     format_indices(usrs, {1}   ) .. "\n" .. chan

   elseif server == "freeswitch" then
      return "Freeswitch is not supported yet.\n"
   else
      return "Neither Asterisk nor FreeSwitch discovered, please install Asterisk, as Freeswitch is not supported yet.\n"
   end
end

return m