This is the initial release of luci-pbx as submitted to the mailing list.
[project/luci.git] / applications / luci-pbx / luasrc / model / cbi / pbx-voip.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-voip"
29
30 m = Map (modulename, translate("SIP Accounts"),
31 translate("This is where you set up your SIP (VoIP) accounts ts like Sipgate, SipSorcery,\
32 the popular Betamax providers, and any other providers with SIP settings in order to start \
33 using them for dialing and receiving calls (SIP uri and real phone calls). Click \"Add\" to\
34 add as many accounts as you wish."))
35
36 -- Recreate the config, and restart services after changes are commited to the configuration.
37 function m.on_after_commit(self)
38 commit = false
39 -- Create a field "name" for each account which identifies the account in the backend.
40 m.uci:foreach(modulename, "voip_provider",
41 function(s1)
42 if s1.defaultuser ~= nil and s1.host ~= nil then
43 name=string.gsub(s1.defaultuser.."_"..s1.host, "%W", "_")
44 if s1.name ~= name then
45 m.uci:set(modulename, s1['.name'], "name", name)
46 commit = true
47 end
48 end
49 end)
50 if commit == true then m.uci:commit(modulename) end
51
52 luci.sys.call("/etc/init.d/pbx-" .. server .. " restart 1\>/dev/null 2\>/dev/null")
53 luci.sys.call("/etc/init.d/" .. server .. " restart 1\>/dev/null 2\>/dev/null")
54 end
55
56 -----------------------------------------------------------------------------
57 s = m:section(TypedSection, "voip_provider", translate("SIP Provider Accounts"))
58 s.anonymous = true
59 s.addremove = true
60
61 s:option(Value, "defaultuser", translate("User Name"))
62 pwd = s:option(Value, "secret", translate("Password"),
63 translate("When your password is saved, it disappears from this field and is not displayed\
64 for your protection. The previously saved password will be changed only when you\
65 enter a value different from the saved one."))
66
67
68
69 pwd.password = true
70 pwd.rmempty = false
71
72 -- We skip reading off the saved value and return nothing.
73 function pwd.cfgvalue(self, section)
74 return ""
75 end
76
77 -- We check the entered value against the saved one, and only write if the entered value is
78 -- something other than the empty string, and it differes from the saved value.
79 function pwd.write(self, section, value)
80 local orig_pwd = m:get(section, self.option)
81 if value and #value > 0 and orig_pwd ~= value then
82 Value.write(self, section, value)
83 end
84 end
85
86 h = s:option(Value, "host", translate("SIP Server/Registrar"))
87 h.datatype = "host"
88
89 p = s:option(ListValue, "register", translate("Enable Incoming Calls (Register via SIP)"),
90 translate("This option should be set to \"Yes\" if you have a DID \(real telephone number\)\
91 associated with this SIP account or want to receive SIP uri calls through this\
92 provider."))
93 p:value("yes", translate("Yes"))
94 p:value("no", translate("No"))
95 p.default = "yes"
96
97 p = s:option(ListValue, "make_outgoing_calls", translate("Enable Outgoing Calls"),
98 translate("Use this account to make outgoing calls."))
99 p:value("yes", translate("Yes"))
100 p:value("no", translate("No"))
101 p.default = "yes"
102
103 from = s:option(Value, "fromdomain",
104 translate("SIP Realm (needed by some providers)"))
105 from.optional = true
106 from.datatype = "host"
107
108 port = s:option(Value, "port", translate("SIP Server/Registrar Port"))
109 port.optional = true
110 port.datatype = "port"
111
112 op = s:option(Value, "outboundproxy", translate("Outbound Proxy"))
113 op.optional = true
114 op.datatype = "host"
115
116 return m