applications/luci-asterisk: rework dialplan management

This commit is contained in:
Jo-Philipp Wich 2009-03-21 04:36:08 +00:00
parent 684f695c13
commit 1a3d44865f
6 changed files with 422 additions and 152 deletions

View file

@ -25,6 +25,11 @@ AST_BIN = "/usr/sbin/asterisk"
AST_FLAGS = "-r -x"
--- LuCI Asterisk - Resync uci context
function uci_resync()
uci = luci.model.uci.cursor()
end
--- LuCI Asterisk io interface
-- Handles low level io.
-- @type module
@ -318,3 +323,57 @@ function dialzone.ucisection(i)
end)
return hash
end
--- LuCI Asterisk - Dialplan
-- @type module
dialplan = luci.util.class()
--- Parse a dialplan section
-- @param plan Table containing the plan info
-- @return Table with parsed information
function dialplan.parse(z)
if z['.name'] then
local plan = {
zones = { },
name = z['.name'],
description = z.description or z['.name']
}
for _, name in ipairs(tools.parse_list(z.include)) do
local zone = dialzone.zone(name)
if zone then
plan.zones[#plan.zones+1] = zone
end
end
return plan
end
end
--- Get a list of known dial plans
-- @return Associative table of plans and table of plan names
function dialplan.plans()
local plans = { }
local pnames = { }
uci:foreach("asterisk", "dialplan",
function(p)
plans[p['.name']] = dialplan.parse(p)
pnames[#pnames+1] = p['.name']
end)
return plans, pnames
end
--- Get a specific dial plan
-- @param name Name of the dial plan
-- @return Table containing plan information
function dialplan.plan(n)
local plan
uci:foreach("asterisk", "dialplan",
function(p)
if p['.name'] == n then
plan = dialplan.parse(p)
end
end)
return plan
end

View file

@ -40,76 +40,116 @@ function index()
cbi("asterisk-mod-res-feature"), "Feature Module Configuration", 9 )
entry({"admin", "asterisk"}, cbi("asterisk/main"), "Asterisk", 99).i18n = "asterisk"
entry({"admin", "asterisk"}, cbi("asterisk/main"), "Asterisk", 99).i18n = "asterisk"
entry({"admin", "asterisk", "phones"}, cbi("asterisk/phones"), "Phones", 1)
entry({"admin", "asterisk", "phones", "sip"}, cbi("asterisk/phone_sip"), nil, 1).leaf = true
--entry({"admin", "asterisk", "phones", "exten"}, cbi("asterisk/phone_exten"), "Extensions", 2).leaf = true
entry({"admin", "asterisk", "phones"}, cbi("asterisk/phones"), "Phones", 1)
entry({"admin", "asterisk", "phones", "sip"}, cbi("asterisk/phone_sip"), nil, 1).leaf = true
--entry({"admin", "asterisk", "phones", "exten"}, cbi("asterisk/phone_exten"), "Extensions", 2).leaf = true
entry({"admin", "asterisk", "trunks"}, cbi("asterisk/trunks"), "Trunks", 2)
entry({"admin", "asterisk", "trunks", "sip"}, cbi("asterisk/trunk_sip"), nil, 1).leaf = true
entry({"admin", "asterisk", "trunks"}, cbi("asterisk/trunks"), "Trunks", 2)
entry({"admin", "asterisk", "trunks", "sip"}, cbi("asterisk/trunk_sip"), nil, 1).leaf = true
--entry({"admin", "asterisk", "dialplans"}, cbi("asterisk/dialplans"), "Call Routing", 3)
entry({"admin", "asterisk", "dialplans"}, call("handle_dialplan"), "Call Routing", 3)
entry({"admin", "asterisk", "dialplans", "out"}, cbi("asterisk/dialplan_out"), nil, 1).leaf = true
entry({"admin", "asterisk", "dialplans", "zones"}, cbi("asterisk/dialzones"), "Dial Zones", 2).leaf = true
--entry({"admin", "asterisk", "dialplans"}, cbi("asterisk/dialplans"), "Call Routing", 3)
entry({"admin", "asterisk", "dialplans"}, call("handle_dialplan"), "Call Routing", 3)
entry({"admin", "asterisk", "dialplans", "out"}, cbi("asterisk/dialplan_out"), nil, 1).leaf = true
entry({"admin", "asterisk", "dialplans", "zones"}, call("handle_dialzones"), "Dial Zones", 2).leaf = true
end
function handle_dialplan()
local uci = luci.model.uci.cursor()
local ast = require "luci.asterisk"
local err = false
if luci.http.formvalue("delete") then
local del = luci.http.formvalue("delete")
if #del > 0 and not del:match("[^a-zA-Z0-9_]") then
uci:delete("asterisk", del)
uci:foreach("asterisk", "dialplan",
function(s)
if s.include then
local inc = type(s.include) == "table" and s.include or
luci.util.split(s.include, "%s+", nil, true)
for k, v in pairs(luci.http.formvaluetable("delzone")) do
local plan = ast.dialplan.plan(k)
if #v > 0 and plan then
local newinc = { }
local inc2 = { }
for _, v in ipairs(inc) do
if v ~= del then
inc2[#inc2+1] = v
end
end
for _, z in ipairs(plan.zones) do
if z.name ~= v then
newinc[#newinc+1] = z.name
end
end
uci:set("asterisk", s['.name'], "include", inc2)
end
end)
uci:delete("asterisk", plan.name, "include")
if #newinc > 0 then
uci:set("asterisk", plan.name, "include", newinc)
end
uci:save("asterisk")
uci:commit("asterisk")
end
end
for k, v in pairs(luci.http.formvaluetable("create_entry")) do
if #v > 0 and not v:match("[^a-zA-Z0-9_]") then
uci:section("asterisk", "dialzone", v, {
context = k
} )
for k, v in pairs(luci.http.formvaluetable("addzone")) do
local plan = ast.dialplan.plan(k)
local zone = ast.dialzone.zone(v)
if #v > 0 and plan and zone then
local newinc = { zone.name }
local inc = uci:get("asterisk", k, "include")
inc = type(inc) == "table" and inc or
type(inc) == "string" and #inc > 0 and
luci.util.split(inc, "%s+", nil, true) or { }
for _, z in ipairs(plan.zones) do
newinc[#newinc+1] = z.name
end
inc[#inc+1] = v
uci:delete("asterisk", plan.name, "include")
if #newinc > 0 then
uci:set("asterisk", plan.name, "include", newinc)
end
uci:set("asterisk", k, "include", inc)
uci:save("asterisk")
uci:commit("asterisk")
luci.http.redirect(luci.dispatcher.build_url(
"asterisk", "dialplans", "out", v
))
return
end
end
luci.template.render("asterisk/dialplans")
local aname = luci.http.formvalue("addplan")
if aname and #aname > 0 then
if aname:match("^[a-zA-Z0-9_]+$") then
uci:section("asterisk", "dialplan", aname, { })
uci:save("asterisk")
else
err = true
end
end
local dname = luci.http.formvalue("delplan")
if dname and #dname > 0 then
if uci:get("asterisk", dname) == "dialplan" then
uci:delete("asterisk", dname)
uci:save("asterisk")
end
end
ast.uci_resync()
luci.template.render("asterisk/dialplans", { create_error = err })
end
function handle_dialzones()
local ast = require "luci.asterisk"
local uci = luci.model.uci.cursor()
local err = false
if luci.http.formvalue("newzone") then
local name = luci.http.formvalue("newzone_name")
if name and name:match("^[a-zA-Z0-9_]+$") then
uci:section("asterisk", "dialzone", name, {
uses = ast.tools.parse_list(luci.http.formvalue("newzone_uses") or {}),
match = ast.tools.parse_list(luci.http.formvalue("newzone_match") or {})
})
uci:save("asterisk")
else
err = true
end
end
if luci.http.formvalue("delzone") then
local name = luci.http.formvalue("delzone")
if uci:get("asterisk", name) == "dialzone" then
uci:delete("asterisk", name)
uci:save("asterisk")
end
end
luci.template.render("asterisk/dialzones", { create_error = err })
end

View file

@ -114,7 +114,7 @@ if arg[1] then
intl = entry:option(DynamicList, "international", "Intl. prefix matches (optional)")
trunk = entry:option(ListValue, "uses", "Used trunk")
trunk = entry:option(MultiValue, "uses", "Used trunk")
for _, v in ipairs(find_trunks(cbimap.uci)) do
trunk:value(unpack(v))
end

View file

@ -17,53 +17,11 @@ $Id$
<%
local uci = luci.model.uci.cursor_state()
local ast = require "luci.asterisk"
function find_rules(plan)
local r = { }
if plan and plan.include then
local i = luci.util.split(plan.include, "%s+", nil, true)
for _, i in ipairs(i) do
i = uci:get("asterisk", "dialzone", i)
if i then
r[#r+1] = i
end
end
end
return r
end
dp_lookup_table = { }
function dialplan_lookup(s)
if not dp_lookup_table[s['.name']] then
s.childs = { }
s.matches = type(s.match) == "table" and s.match or { s.match }
s.name, s.type = s['.name'], s['.type']
s['.name'], s['.type'] = nil, nil
dp_lookup_table[s.name] = s
end
end
uci:foreach("asterisk", "dialplan", dialplan_lookup)
uci:foreach("asterisk", "dialzone", dialplan_lookup)
for k, p in pairs(dp_lookup_table) do
if p.include then
local i = type(p.include) == "string"
and luci.util.split(p.include, "%s+", nil, true) or p.include
for _, i in ipairs(i) do
i = dp_lookup_table[i]
if i then
p.childs[#p.childs+1] = i
i.parent = p
end
end
end
end
function digit_pattern(s)
return "<code style='padding: 2px; border:1px solid #CCCCCC; background-color: #FFFFFF'>%s</code>" % s
function digit_pattern(s,t)
return "<code style='padding: 2px; border:1px solid #CCCCCC; background-color: #FFFFFF'%s>%s</code>"
%{ t and " title='" .. t .. "'" or "", s }
end
function rowstyle(i)
@ -72,18 +30,33 @@ $Id$
}
end
function link_trunks(s)
local l = { }
for s in s:gmatch("(%S+)") do
if s:match("^[sS][iI][pP]/") then
l[#l+1] = '<a href="%s">%s</a>' %{
luci.dispatcher.build_url("admin", "asterisk", "trunks",
"sip", (s:gsub("^.+/",""))),
(s:gsub("^.+/","SIP: "))
}
function format_matches(z)
local html = { }
if z.localprefix then
for _, m in ipairs(z.matches) do
html[#html+1] =
digit_pattern(z.localprefix, "local prefix") .. " " ..
digit_pattern(m)
end
end
return '<small>%s</small>' % table.concat(l, ", ")
if #z.intlmatches > 0 then
for _, i in ipairs(z.intlmatches) do
for _, m in ipairs(z.matches) do
html[#html+1] = "%s %s" %{
digit_pattern("(%s)" % i, "intl. prefix"),
digit_pattern(m)
}
end
end
else
for _, m in ipairs(z.matches) do
html[#html+1] = digit_pattern(m)
end
end
return table.concat(html, "; ")
end
%>
@ -100,69 +73,92 @@ $Id$
<div class="cbi-map-descr"></div><!-- tblsection -->
<fieldset class="cbi-section" id="cbi-asterisk-sip">
<!--<legend>Dialplans</legend>-->
<div class="cbi-section-descr"></div>
<div class="cbi-section-descr">
Here you can manage your dial plans which are used to route outgoing calls from your local extensions.
</div>
<% for name, plan in luci.util.kspairs(dp_lookup_table) do
if plan.type == "dialplan" then %>
<% for i, plan in pairs(ast.dialplan.plans()) do %>
<div class="cbi-section-node">
<table class="cbi-section-table">
<tr class="cbi-section-table-titles">
<th style="text-align: left; padding: 3px" class="cbi-section-table-cell" colspan="5">
<big>&nbsp;Dialplan <em><%=name%></em></big>
<th style="text-align: left; padding: 3px" class="cbi-section-table-cell">
<big>Dialplan <em><%=plan.name%></em></big>
</th>
<td>
<a href="<%=luci.dispatcher.build_url('admin', 'asterisk', 'dialplans')%>?delplan=<%=plan.name%>">
<img style="border:none" alt="Remove this dialplan" title="Remove this dialplan" src="/luci-static/resources/cbi/remove.gif" />
</a>
</td>
</tr>
<tr class="cbi-section-table-descr">
<th style="width: 5%; text-align:right" class="cbi-section-table-cell">Prepend</th>
<th style="width: 20%; text-align:left" class="cbi-section-table-cell">- Match</th>
<th style="text-align:left" class="cbi-section-table-cell">Trunk</th>
<th style="width: 40%; text-align:left" class="cbi-section-table-cell">Description</th>
<th style="width: 4%; text-align:left" class="cbi-section-table-cell"></th>
</tr>
<% for i, rule in pairs(plan.childs) do
if rule.type == "dialzone" then %>
<!-- dialzones -->
<% local zones_used = { } %>
<% for i, zone in ipairs(plan.zones) do zones_used[zone.name] = true %>
<tr class="cbi-section-table-row <%=rowstyle(i)%>">
<td style="text-align:right" class="cbi-value-field">
<% for _ in ipairs(rule.matches) do %>
<%=rule.addprefix and digit_pattern(rule.addprefix)%>&nbsp;<br />
<% end %>
<td style="text-align: left; padding: 3px" class="cbi-section-table-cell">
<strong>&#x2514; Dialzone <em><%=zone.name%></em></strong>
<p style="padding-left: 1em; margin-bottom:0">
Description: <%=zone.description%><br />
Lines:
<%=ast.tools.hyperlinks(
zone.trunks, function(v)
return luci.dispatcher.build_url("admin", "asterisk", "trunks", "%s") % v:lower()
end
)%><br />
Matches:
<%=format_matches(zone)%>
</p>
</td>
<td style="text-align:left" class="cbi-value-field">
<% for _, m in ipairs(rule.matches) do %>
<%=rule.localprefix and "%s " % digit_pattern(rule.localprefix)%>
<%=digit_pattern(m)%><br />
<% end %>
</td>
<td style="text-align:left" class="cbi-value-field">
<%=rule.uses and link_trunks(rule.uses)%>
</td>
<td style="text-align:left" class="cbi-value-field">
<%=rule.description or rule.name%>
</td>
<td style="text-align:left" class="cbi-value-field">
<a href="<%=luci.dispatcher.build_url('admin', 'asterisk', 'dialplans', 'out', rule.name)%>">
<img style="border:none" alt="Edit entry" title="Edit entry" src="/luci-static/resources/cbi/edit.gif" />
<td style="width:5%" class="cbi-value-field">
<a href="<%=luci.dispatcher.build_url('admin', 'asterisk', 'dialplans', 'out', zone.name)%>">
<img style="border:none" alt="Edit dialzone" title="Edit dialzone" src="/luci-static/resources/cbi/edit.gif" />
</a>
<a href="<%=luci.dispatcher.build_url('admin', 'asterisk', 'dialplans')%>?delete=<%=rule.name%>">
<img style="border:none" alt="Delete entry" title="Delete entry" src="/luci-static/resources/cbi/remove.gif" />
<a href="<%=luci.dispatcher.build_url('admin', 'asterisk', 'dialplans')%>?delzone.<%=plan.name%>=<%=zone.name%>">
<img style="border:none" alt="Remove from this dialplan" title="Remove from this dialplan" src="/luci-static/resources/cbi/remove.gif" />
</a>
</td>
</tr>
<% end end %>
<% end %>
<tr class="cbi-section-table-row">
<td style="text-align: left; padding: 3px" class="cbi-section-table-cell" colspan="2">
<hr style="margin-bottom:0.5em; border-width:0 0 1px 0" />
<select style="width:30%" name="addzone.<%=plan.name%>">
<option value="">-- Add dialzone --</option>
<% for _, zone in pairs(ast.dialzone.zones()) do %>
<% if not zones_used[zone.name] then %>
<option value="<%=zone.name%>"><%=zone.name%> (<%=zone.description%>)</option>
<% end %>
<% end %>
</select>
<input type="submit" class="cbi-button cbi-button-add" value=" &raquo; " title="Add Zone ..."/>
&nbsp; &nbsp;
<a href="<%=luci.dispatcher.build_url('admin/asterisk/dialplans/zones')%>" class="cbi-title-ref">Manage dialzones</a>
</td>
</tr>
<!-- /dialzones -->
</table>
<div class="cbi-section-create cbi-tblsection-create">
<input type="text" class="cbi-section-create-name" name="create_entry.<%=name%>"/>
<input type="submit" class="cbi-button cbi-button-add" value="Add entry" title="Add entry"/>
</div>
<div class="cbi-section-create cbi-tblsection-create"></div>
</div>
<br />
<% end end %>
<% end %>
<div class="cbi-section-node">
<div class="cbi-section-create cbi-tblsection-create" style="padding: 3px">
<h3>Create a new dialplan</h3>
The name is required and must be unique. It may only contain the characters A-Z, a-z, 0-9 and _ .<br />
<%- if create_error then %>
<br /><span style="color:red">Invalid name given!</span><br />
<% end -%>
<br />
<input type="text" class="cbi-section-create-name" name="addplan" style="width:200px" />
<input type="submit" class="cbi-button cbi-button-add" value="Add dialplan" title="Add dialplan"/>
</div>
</div>
</fieldset>
</div>

View file

@ -0,0 +1,174 @@
<%#
LuCI - Lua Configuration Interface
Copyright 2008 Steven Barth <steven@midlink.org>
Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
$Id$
-%>
<%+header%>
<%
local uci = luci.model.uci.cursor_state()
local ast = require("luci.asterisk")
function digit_pattern(s)
return "<code style='padding: 2px; border:1px solid #CCCCCC; background-color: #FFFFFF'>%s</code>" % s
end
function rowstyle(i)
return "cbi-rowstyle-%i" %{
( i % 2 ) == 0 and 2 or 1
}
end
local function find_trunks()
local t = { }
uci:foreach("asterisk", "sip",
function(s)
if uci:get_bool("asterisk", s['.name'], "provider") then
t[#t+1] = {
"SIP/%s" % s['.name'],
"SIP: %s" % s['.name']
}
end
end)
uci:foreach("asterisk", "iax",
function(s)
t[#t+1] = {
"IAX/%s" % s['.name'],
"IAX: %s" % s.extension or s['.name']
}
end)
return t
end
%>
<form method="post" action="<%=luci.dispatcher.build_url("admin", "asterisk", "dialplans", "zones")%>" enctype="multipart/form-data">
<div>
<script type="text/javascript" src="/luci-static/resources/cbi.js"></script>
<input type="hidden" name="cbi.submit" value="1" />
<input type="submit" value="Save" class="hidden" />
</div>
<div class="cbi-map" id="cbi-asterisk">
<h2><a id="content" name="content">Dial Zone Management</a></h2>
<div class="cbi-map-descr">
<a href="<%=luci.dispatcher.build_url("admin/asterisk/dialplans")%>" class="cbi-title-ref">Back to dialplan overview</a><br /><br />
Here you can manage your dial zones. The zones are used to route outgoing calls to the destination.
Each zone groups multiple trunks and number matches to represent a logical destination. Zones can
also be used to enforce certain dial restrictions on selected extensions.
</div>
<!-- tblsection -->
<fieldset class="cbi-section" id="cbi-asterisk-sip">
<div class="cbi-section-node">
<table class="cbi-section-table">
<tr class="cbi-section-table-titles">
<th style="text-align: left; padding: 3px" class="cbi-section-table-cell" colspan="6">
<h3>Dialzone Overview</h3>
</th>
</tr>
<tr class="cbi-section-table-descr">
<th style="width: 5%; text-align:right" class="cbi-section-table-cell">Name</th>
<th style="width: 5%; text-align:right" class="cbi-section-table-cell">Prepend</th>
<th style="width: 20%; text-align:left" class="cbi-section-table-cell">- Match</th>
<th style="text-align:left" class="cbi-section-table-cell">Trunk</th>
<th style="width: 35%; text-align:left" class="cbi-section-table-cell">Description</th>
<th style="width: 4%; text-align:left" class="cbi-section-table-cell"></th>
</tr>
<% for i, rule in pairs(ast.dialzone.zones()) do %>
<tr class="cbi-section-table-row <%=rowstyle(i)%>">
<td style="text-align:right" class="cbi-value-field">
<%=rule.name%>
</td>
<td style="text-align:right" class="cbi-value-field">
<% for _ in ipairs(rule.matches) do %>
<%=rule.addprefix and digit_pattern(rule.addprefix)%>&nbsp;<br />
<% end %>
</td>
<td style="text-align:left" class="cbi-value-field">
<% for _, m in ipairs(rule.matches) do %>
<%=rule.localprefix and "%s " % digit_pattern(rule.localprefix)%>
<%=digit_pattern(m)%><br />
<% end %>
</td>
<td style="text-align:left" class="cbi-value-field">
<%=ast.tools.hyperlinks(
rule.trunks, function(v)
return luci.dispatcher.build_url("admin", "asterisk", "trunks", "%s") % v:lower()
end
)%>
</td>
<td style="text-align:left" class="cbi-value-field">
<%=rule.description or rule.name%>
</td>
<td style="text-align:left" class="cbi-value-field">
<a href="<%=luci.dispatcher.build_url('admin', 'asterisk', 'dialplans', 'out', rule.name)%>">
<img style="border:none" alt="Edit entry" title="Edit entry" src="/luci-static/resources/cbi/edit.gif" />
</a>
<a href="<%=luci.dispatcher.build_url('admin', 'asterisk', 'dialplans', 'zones')%>?delzone=<%=rule.name%>">
<img style="border:none" alt="Delete entry" title="Delete entry" src="/luci-static/resources/cbi/remove.gif" />
</a>
</td>
</tr>
<% end %>
</table>
<div class="cbi-section-create cbi-tblsection-create"></div>
</div>
<br />
<div class="cbi-section-node">
<div class="cbi-section-create cbi-tblsection-create" style="padding: 3px">
<h3>Create a new dialzone</h3>
The name is required and must be unique. It may only contain the characters A-Z, a-z, 0-9 and _ .<br />
You can specifiy multiple number matches by separating them with spaces.<br />
<%- if create_error then %>
<br /><span style="color:red">Invalid name given!</span><br />
<% end -%>
<table>
<tr>
<td style="padding:3px">
<label for="create1">1) Name</label><br />
<input type="text" class="cbi-section-create-name" id="create1" name="newzone_name" style="width:200px" />
<br /><br />
<label for="create2">2) Number Match</label><br />
<input type="text" class="cbi-section-create-name" id="create2" name="newzone_match" style="width:200px" />
</td>
<td style="padding:3px">
<label for="create3">3) Trunks</label><br />
<select class="cbi-input-select" multiple="multiple" id="create3" name="newzone_uses" size="4" style="width:200px">
<% for i, t in ipairs(find_trunks()) do %>
<option value="<%=t[1]%>"><%=t[2]%></option>
<% end %>
</select>
</td>
</tr>
</table>
<br />
<input type="submit" class="cbi-button cbi-button-add" name="newzone" value="Add entry" title="Add entry"/>
</div>
</div>
</fieldset>
</div>
</form>
<div class="clear"></div>
<%+footer%>

View file

@ -75,6 +75,7 @@ config 'sip' 'PAP2T2'
option 'incoming' 'provider_inbound'
config 'sip' 'providerphone'
option 'provider' 'yes'
option 'type' 'friend'
option 'timeout' '55'
option 'internationalprefix' '0011'
@ -188,7 +189,7 @@ config 'dialzone' 'interstate'
option 'localprefix' '0'
config 'dialzone' 'mobile'
option 'uses' 'SIP/iinetphone'
option 'uses' 'SIP/providerphone'
option 'match' '04XXXXXXXX'
option 'localprefix' '0'