d0f4323c348b01bd8ef3c4d8115c30f8e2d92a08
[project/luci.git] / libs / sgi-webuci / root / usr / lib / boa / luci.lua
1 module("luci-plugin", package.seeall)
2
3 function normalize(path)
4     local newpath
5         while newpath ~= path do
6                 if (newpath) then
7                         path = newpath
8                 end
9                 newpath = string.gsub(path, "/[^/]+/../", "/")
10         end
11         return newpath
12 end
13
14 function init(path)
15         -- NB: path points to ROOT/usr/lib/boa, change it to /usr/lib/lua
16         root = normalize(path .. '/../../../')
17         path = normalize(path .. '/../lua/')
18         package.cpath = path..'?.so;'..package.cpath
19         package.path = path..'?.lua;'..package.path
20
21         require("luci.dispatcher")
22         require("luci.sgi.webuci")
23         require("luci.model.uci")
24
25         if (root ~= '/') then
26                 -- Entering dummy mode
27                 luci.model.uci.confdir_default = root .. '/etc/config'
28                 luci.model.uci.savedir_state   = root .. '/var/state'
29                 uci.set_confdir(luci.model.uci.confdir_default)
30                 
31                 luci.sys.hostname = function() return "" end
32                 luci.sys.loadavg  = function() return 0,0,0,0,0 end
33                 luci.sys.reboot   = function() return end
34                 luci.sys.sysinfo  = function() return "","","" end
35                 luci.sys.syslog   = function() return "" end
36                 
37                 luci.sys.net.arptable           = function() return {} end
38                 luci.sys.net.devices            = function() return {} end
39                 luci.sys.net.routes             = function() return {} end
40                 luci.sys.wifi.getiwconfig       = function() return {} end
41                 luci.sys.wifi.iwscan            = function() return {} end
42                 
43                 luci.sys.user.checkpasswd   = function() return true end
44         end
45 end
46
47 function prepare_req(uri)
48         luci.dispatcher.createindex()
49         env = {}
50         env.REQUEST_URI = uri
51         -- TODO: setting luci-plugin.reload = true allows this function to trigger a context reload
52 end
53
54 function handle_req(context)
55         env.SERVER_PROTOCOL = context.server_proto
56         env.REMOTE_ADDR     = context.remote_addr
57         env.REQUEST_METHOD  = context.request_method
58         env.PATH_INFO       = context.uri
59         env.REMOTE_PORT     = context.remote_port
60         env.SERVER_ADDR     = context.server_addr
61         env.HTTP_COOKIE     = context.cookie
62         env.SCRIPT_NAME     = env.REQUEST_URI:sub(1, #env.REQUEST_URI - #env.PATH_INFO)
63
64         luci.sgi.webuci.run(env, vars)
65 end