2 Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
4 This file is part of luci-pbx.
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.
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.
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/>.
20 if nixio.fs.access("/etc/init.d/asterisk") then
22 elseif nixio.fs.access("/etc/init.d/freeswitch") then
28 modulename = "pbx-calls"
29 voipmodulename = "pbx-voip"
30 googlemodulename = "pbx-google"
31 usersmodulename = "pbx-users"
37 -- Checks whether the entered extension is valid syntactically.
38 function is_valid_extension(exten)
39 return (exten:match("[#*+0-9NXZ]+$") ~= nil)
43 m = Map (modulename, translate("Call Routing"),
44 translate("This is where you indicate which Google/SIP accounts are used to call what \
45 country/area codes, which users can use which SIP/Google accounts, how incoming \
46 calls are routed, what numbers can get into this PBX with a password, and what \
47 numbers are blacklisted."))
49 -- Recreate the config, and restart services after changes are commited to the configuration.
50 function m.on_after_commit(self)
51 luci.sys.call("/etc/init.d/pbx-" .. server .. " restart 1\>/dev/null 2\>/dev/null")
52 luci.sys.call("/etc/init.d/" .. server .. " restart 1\>/dev/null 2\>/dev/null")
55 -- Add Google accounts to all valid accounts, and accounts valid for incoming and outgoing calls.
56 m.uci:foreach(googlemodulename, "gtalk_jabber",
58 -- Add this provider to list of valid accounts.
59 if s1.username ~= nil and s1.name ~= nil then
60 allvalidaccounts[s1.name] = s1.username
62 if s1.make_outgoing_calls == "yes" then
63 -- Add provider to the associative array of valid outgoing accounts.
64 validoutaccounts[s1.name] = s1.username
67 if s1.register == "yes" then
68 -- Add provider to the associative array of valid outgoing accounts.
69 validinaccounts[s1.name] = s1.username
74 -- Add SIP accounts to all valid accounts, and accounts valid for incoming and outgoing calls.
75 m.uci:foreach(voipmodulename, "voip_provider",
77 -- Add this provider to list of valid accounts.
78 if s1.defaultuser ~= nil and s1.host ~= nil and s1.name ~= nil then
79 allvalidaccounts[s1.name] = s1.defaultuser .. "@" .. s1.host
81 if s1.make_outgoing_calls == "yes" then
82 -- Add provider to the associative array of valid outgoing accounts.
83 validoutaccounts[s1.name] = s1.defaultuser .. "@" .. s1.host
86 if s1.register == "yes" then
87 -- Add provider to the associative array of valid outgoing accounts.
88 validinaccounts[s1.name] = s1.defaultuser .. "@" .. s1.host
93 ----------------------------------------------------------------------------------------------------
94 s = m:section(NamedSection, "outgoing_calls", "call_routing", translate("Outgoing Calls"),
95 translate("If you have more than one account which can make outgoing calls, you \
96 should enter a list of phone numbers and prefixes in the following fields for each \
97 provider listed. Invalid prefixes are removed silently, and only 0-9, X, Z, N, #, *, \
98 and + are valid characters. The letter X matches 0-9, Z matches 1-9, and N matches 2-9. \
99 For example to make calls to Germany through a provider, you can enter 49. To make calls \
100 to North America, you can enter 1NXXNXXXXXX. If one of your providers can make \"local\" \
101 calls to an area code like New York's 646, you can enter 646NXXXXXX for that \
102 provider. You should leave one account with an empty list to make calls with \
103 it by default, if no other provider's prefixes match. The system will automatically \
104 replace an empty list with a message that the provider dials all numbers. Be as specific as \
105 possible (i.e. 1NXXNXXXXXX is better than 1). Please note all international dial codes \
106 are discarded (e.g. 00, 011, 010, 0011). Entries can be made in a space-separated \
107 list, and/or one per line by hitting enter after every one."))
111 m.uci:foreach(googlemodulename, "gtalk_jabber",
113 -- If this provider is valid AND is enabled for outgoing calls, add it to the section.
114 if s1.username ~= nil and s1.name ~= nil and
115 s1.make_outgoing_calls == "yes" then
116 patt = s:option(DynamicList, s1.name, s1.username)
118 -- If the saved field is empty, we return a string
119 -- telling the user that this account would dial any exten.
120 function patt.cfgvalue(self, section)
121 value = self.map:get(section, self.option)
124 return {"Dials any number"}
130 -- Write only valid extensions into the config file.
131 function patt.write(self, section, value)
134 for index, field in ipairs(value) do
135 val = luci.util.trim(value[index])
136 if is_valid_extension(val) == true then
137 newvalue[nindex] = val
141 DynamicList.write(self, section, newvalue)
146 m.uci:foreach(voipmodulename, "voip_provider",
149 -- If this provider is valid AND is enabled for outgoing calls, add it to the section.
150 if s1.defaultuser ~= nil and s1.host ~= nil and
151 s1.name ~= nil and s1.make_outgoing_calls == "yes" then
152 patt = s:option(DynamicList, s1.name, s1.defaultuser .. "@" .. s1.host)
154 -- If the saved field is empty, we return a string
155 -- telling the user that this account would dial any exten.
156 function patt.cfgvalue(self, section)
157 value = self.map:get(section, self.option)
160 return {"Dials any number"}
166 -- Write only valid extensions into the config file.
167 function patt.write(self, section, value)
170 for index, field in ipairs(value) do
171 val = luci.util.trim(value[index])
172 if is_valid_extension(val) == true then
173 newvalue[nindex] = val
177 DynamicList.write(self, section, newvalue)
182 -- If there are no accounts enabled for outgoing calls.
183 if # allvalidaccounts == 0 then
184 warn = s:option(DummyValue, "warn")
185 warn.default = "NOTE: There are no Google or SIP provider accounts configured."
186 elseif # validoutaccounts == 0 then
187 warn.s:option(DummyValue, "warn")
188 warn.default = "NOTE: There are no Google or SIP provider accounts enabled for outgoing calls."
191 ----------------------------------------------------------------------------------------------------
192 s = m:section(NamedSection, "incoming_calls", "call_routing", translate("Incoming Calls"),
193 translate("For each provider that receives calls, here you can restrict which users to ring \
194 on incoming calls. If the list is empty, the system will indicate that all users \
195 which are enabled for incoming calls will ring. Invalid usernames will be rejected \
196 silently. Also, entering a username here overrides the user's setting to not receive \
197 incoming calls, so this way, you can make users ring only for select providers. \
198 Entries can be made in a space-separated list, and/or one per \
199 line by hitting enter after every one."))
202 m.uci:foreach(googlemodulename, "gtalk_jabber",
204 if s1.username ~= nil and s1.register == "yes" then
205 field_name=string.gsub(s1.username, "%W", "_")
206 gtalkaccts = s:option(DynamicList, field_name, s1.username)
208 -- If the saved field is empty, we return a string
209 -- telling the user that this account would dial any exten.
210 function gtalkaccts.cfgvalue(self, section)
211 value = self.map:get(section, self.option)
214 return {"Rings all users"}
220 -- Write only valid user names.
221 function gtalkaccts.write(self, section, value)
224 for index, field in ipairs(value) do
225 trimuser = luci.util.trim(value[index])
226 if allvalidusers[trimuser] == true then
227 newvalue[nindex] = trimuser
231 DynamicList.write(self, section, newvalue)
237 m.uci:foreach(voipmodulename, "voip_provider",
239 if s1.defaultuser ~= nil and s1.host ~= nil and s1.register == "yes" then
240 field_name=string.gsub(s1.defaultuser .. "_" .. s1.host, "%W", "_")
241 voipaccts = s:option(DynamicList, field_name, s1.defaultuser .. "@" .. s1.host)
243 -- If the saved field is empty, we return a string
244 -- telling the user that this account would dial any exten.
245 function voipaccts.cfgvalue(self, section)
246 value = self.map:get(section, self.option)
249 return {"Rings all users"}
255 -- Write only valid user names.
256 function voipaccts.write(self, section, value)
259 for index, field in ipairs(value) do
260 trimuser = luci.util.trim(value[index])
261 if allvalidusers[trimuser] == true then
262 newvalue[nindex] = trimuser
266 DynamicList.write(self, section, newvalue)
272 ----------------------------------------------------------------------------------------------------
273 s = m:section(NamedSection, "providers_user_can_use", "call_routing",
274 translate("Providers Used for Outgoing Calls"),
275 translate("If you would like, you could restrict which providers users are allowed to use for outgoing \
276 calls. By default all users can use all providers. To show up in the list below the user should \
277 be allowed to make outgoing calls in the \"User Accounts\" page. Enter VoIP providers in the format \
278 username@some.host.name, as listed in \"Outgoing Calls\" above. It's easiest to copy and paste \
279 the providers from above. Invalid entries will be rejected silently. Entries can be made in a \
280 space-separated list, and/or one per line by hitting enter after every one."))
283 m.uci:foreach(usersmodulename, "local_user",
285 -- Add user to list of all valid users.
286 if s1.defaultuser ~= nil then allvalidusers[s1.defaultuser] = true end
288 if s1.defaultuser ~= nil and s1.can_call == "yes" then
289 providers = s:option(DynamicList, s1.defaultuser, s1.defaultuser)
291 -- If the saved field is empty, we return a string
292 -- telling the user that this account would dial any exten.
293 function providers.cfgvalue(self, section)
294 value = self.map:get(section, self.option)
297 return {"Uses all provider accounts"}
300 -- Convert internal names to user@host values.
301 for i,v in ipairs(value) do
302 newvalue[i] = validoutaccounts[v]
308 -- Cook the new values prior to entering them into the config file.
309 -- Also, enter them only if they are valid.
310 function providers.write(self, section, value)
313 for index, field in ipairs(value) do
314 cooked = string.gsub(luci.util.trim(value[index]), "%W", "_")
315 if validoutaccounts[cooked] ~= nil then
316 cookedvalue[cindex] = cooked
320 DynamicList.write(self, section, cookedvalue)
325 ----------------------------------------------------------------------------------------------------
326 s = m:section(TypedSection, "callthrough_numbers", translate("Call-through Numbers"),
327 translate("Designate numbers which will be allowed to call through this system and which user's \
328 privileges it will have."))
332 num = s:option(DynamicList, "callthrough_number_list", translate("Call-through Numbers"))
333 num.datatype = "uinteger"
335 p = s:option(ListValue, "enabled", translate("Enabled"))
336 p:value("yes", translate("Yes"))
337 p:value("no", translate("No"))
340 user = s:option(Value, "defaultuser", translate("User Name"),
341 translate("The number(s) specified above will be able to dial out with this user's providers. \
342 Invalid usernames are dropped silently, please verify that the entry was accepted."))
343 function user.write(self, section, value)
344 trimuser = luci.util.trim(value)
345 if allvalidusers[trimuser] == true then
346 Value.write(self, section, trimuser)
350 pwd = s:option(Value, "pin", translate("PIN"),
351 translate("Your PIN disappears when saved for your protection. It will be changed \
352 only when you enter a value different from the saved one. Leaving the PIN \
353 empty is possible, but please beware of the security implications."))
357 -- We skip reading off the saved value and return nothing.
358 function pwd.cfgvalue(self, section)
362 -- We check the entered value against the saved one, and only write if the entered value is
363 -- something other than the empty string, and it differes from the saved value.
364 function pwd.write(self, section, value)
365 local orig_pwd = m:get(section, self.option)
366 if value and #value > 0 and orig_pwd ~= value then
367 Value.write(self, section, value)
371 ----------------------------------------------------------------------------------------------------
372 s = m:section(NamedSection, "blacklisting", "call_routing", translate("Blacklisted Numbers"),
373 translate("Enter phone numbers that you want to decline calls from automatically. \
374 You should probably omit the country code and any leading \
375 zeroes, but please experiment to make sure you are blocking numbers from your \
376 desired area successfully."))
379 b = s:option(DynamicList, "blacklist1", translate("Dynamic List of Blacklisted Numbers"),
380 translate("Specify numbers individually here. Press enter to add more numbers."))
382 b.datatype = "uinteger"
384 b = s:option(Value, "blacklist2", translate("Space-Separated List of Blacklisted Numbers"),
385 translate("Copy-paste large lists of numbers here."))
386 b.template = "cbi/tvalue"