2 LuCI - Lua Configuration Interface
3 Asterisk PBX interface library
5 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 module("luci.asterisk", package.seeall)
19 local _io = require("io")
20 local sys = require("luci.sys")
21 local util = require("luci.util")
23 AST_BIN = "/usr/sbin/asterisk"
27 --- LuCI Asterisk io interface
28 -- Handles low level io.
30 io = luci.util.class()
32 --- Execute command and return output
33 -- @param command String containing the command to execute
34 -- @return String containing the command output
35 function io.exec(command)
36 local fh = _io.popen( "%s %s %q" %{ AST_BIN, AST_FLAGS, command }, "r" )
37 assert(fh, "Failed to invoke asterisk")
39 local buffer = fh:read("*a")
44 --- Execute command and invoke given callback for each readed line
45 -- @param command String containing the command to execute
46 -- @param callback Function to call back for each line
47 -- @return Always true
48 function io.execl(command, callback)
50 local fh = _io.popen( "%s %s %q" %{ AST_BIN, AST_FLAGS, command }, "r" )
51 assert(fh, "Failed to invoke asterisk")
62 --- Execute command and return an iterator that returns one line per invokation
63 -- @param command String containing the command to execute
64 -- @return Iterator function
65 function io.execi(command)
66 local fh = _io.popen( "%s %s %q" %{ AST_BIN, AST_FLAGS, command }, "r" )
67 assert(fh, "Failed to invoke asterisk")
70 local ln = fh:read("*l")
71 if not ln then fh:close() end
77 --- LuCI Asterisk - core status
78 core = luci.util.class()
80 --- Retrive version string.
81 -- @return String containing the reported asterisk version
82 function core.version(self)
83 local version = io.exec("core show version")
84 return version:gsub(" *\n", "")
88 --- LuCI Asterisk - SIP information.
90 sip = luci.util.class()
92 --- Get a list of known SIP peers
93 -- @return Table containing each SIP peer
94 function sip.peers(self)
98 for line in io.execi("sip show peers") do
101 elseif not line:match(" sip peers ") then
102 local online, delay, id, uid
103 local name, host, dyn, nat, acl, port, status =
104 line:match("(.-) +(.-) +([D ]) ([N ]) (.) (%d+) +(.+)")
106 if host == '(Unspecified)' then host = nil end
107 if port == '0' then port = nil else port = tonumber(port) end
109 dyn = ( dyn == 'D' and true or false )
110 nat = ( nat == 'N' and true or false )
111 acl = ( acl ~= ' ' and true or false )
113 online, delay = status:match("(OK) %((%d+) ms%)")
115 if online == 'OK' then
117 delay = tonumber(delay)
118 elseif status ~= 'Unmonitored' then
126 id, uid = name:match("(.+)/(.+)")
128 if not ( id and uid ) then
150 --- Get informations of given SIP peer
151 -- @param peer String containing the name of the SIP peer
152 function sip.peer(peer)
156 for line in io.execi("sip show peer " .. peer) do
158 local key, val = line:match("(.-) *: +(.*)")
161 key = key:gsub("^ +",""):gsub(" +$", "")
162 val = val:gsub("^ +",""):gsub(" +$", "")
164 if key == "* Name" then
166 elseif key == "Addr->IP" then
167 info.address, info.port = val:match("(.+) Port (.+)")
168 info.port = tonumber(info.port)
169 elseif key == "Status" then
170 info.online, info.delay = val:match("(OK) %((%d+) ms%)")
171 if info.online == 'OK' then
173 info.delay = tonumber(info.delay)
174 elseif status ~= 'Unmonitored' then
183 if val == 'Yes' or val == 'yes' or val == '<Set>' then
185 elseif val == 'No' or val == 'no' then
187 elseif val == '<Not set>' or val == '(none)' then