* luci/app-olsr: improved plugin handling
[project/luci.git] / applications / luci-olsr / luasrc / model / cbi / olsr / olsrdplugins.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 require("luci.fs")
16 require("luci.ip")
17
18
19 if arg[1] then
20         mp = Map("olsrd", "OLSR - Plugins")
21
22         p = mp:section(TypedSection, "LoadPlugin")
23         p:depends("library", arg[1])
24         p.anonymous = true
25
26         ign = p:option(Flag, "ignore", "Enable")
27         ign.enabled  = "0"
28         ign.disabled = "1"
29         function ign.cfgvalue(self, section)
30                 return Flag.cfgvalue(self, section) or "0"
31         end
32
33         lib = p:option(DummyValue, "library", translate("library"))
34         lib.default = arg[1]
35
36         local function Range(x,y)
37                 local t = {}
38                 for i = x, y do t[#t+1] = i end
39                 return t
40         end
41
42         local function Cidr2IpMask(val)
43                 if val then
44                         for i = 1, #val do
45                                 local cidr = luci.ip.IPv4(val[i]) or luci.ip.IPv6(val[i])
46                                 if cidr then
47                                         val[i] = cidr:network():string() .. " " .. cidr:mask():string()
48                                 end
49                         end
50                         return val
51                 end
52         end
53
54         local function IpMask2Cidr(val)
55                 if val then
56                         for i = 1, #val do
57                                 local ip, mask = val[i]:gmatch("([^%s+])%s+([^%s+])")()
58                                 local cidr
59                                 if ip and mask and ip:match(":") then
60                                         cidr = luci.ip.IPv6(ip, mask)
61                                 elseif ip and mask then
62                                         cidr = luci.ip.IPv4(ip, mask)
63                                 end
64
65                                 if cidr then
66                                         val[i] = cidr:string()
67                                 end
68                         end
69                         return val
70                 end
71         end
72
73
74         local knownPlParams = {
75                 ["olsrd_bmf.so.1.5.3"] = {
76                         { Value,                "BmfInterface",                 "bmf0" },
77                         { Value,                "BmfInterfaceIp",               "10.10.10.234/24" },
78                         { Flag,                 "DoLocalBroadcast",             "no" },
79                         { Flag,                 "CapturePacketsOnOlsrInterfaces", "yes" },
80                         { ListValue,    "BmfMechanism",                 { "UnicastPromiscuous", "Broadcast" } },
81                         { Value,                "BroadcastRetransmitCount",     "2" },
82                         { Value,                "FanOutLimit",                  "4" },
83                         { DynamicList,  "NonOlsrIf",                    "eth1" }
84                 },
85
86                 ["olsrd_dyn_gw.so.0.4"] = {
87                         { Value,                "Interval",                             "40" },
88                         { DynamicList,  "Ping",                                 "141.1.1.1" },
89                         { DynamicList,  "HNA",                                  "192.168.80.0/24", IpMask2Cidr, Cidr2IpMask }
90                 },
91
92                 ["olsrd_httpinfo.so.0.1"] = {
93                         { Value,                "port",                                 "80" },
94                         { DynamicList,  "Host",                                 "163.24.87.3" },
95                         { DynamicList,  "Net",                                  "0.0.0.0/0", IpMask2Cidr, Cidr2IpMask }
96                 },
97
98                 ["olsrd_nameservice.so.0.3"] = {
99                         { DynamicList,  "name",                                 "my-name.mesh" },
100                         { DynamicList,  "hosts",                                "1.2.3.4 name-for-other-interface.mesh" },
101                         { Value,                "suffix",                               ".olsr" },
102                         { Value,                "hosts_file",                   "/path/to/hosts_file" },
103                         { Value,                "add_hosts",                    "/path/to/file" },
104                         { Value,                "dns_server",                   "141.1.1.1" },
105                         { Value,                "resolv_file",                  "/path/to/resolv.conf" },
106                         { Value,                "interval",                             "120" },
107                         { Value,                "timeout",                              "240" },
108                         { Value,                "lat",                                  "12.123" },
109                         { Value,                "lon",                                  "12.123" },
110                         { Value,                "latlon_file",                  "/var/run/latlon.js" },
111                         { Value,                "latlon_infile",                "/var/run/gps.txt" },
112                         { Value,                "sighup_pid_file",              "/var/run/dnsmasq.pid" },
113                         { Value,                "name_change_script",   "/usr/local/bin/announce_new_hosts.sh" },
114                         { Value,                "services_change_script",       "/usr/local/bin/announce_new_services.sh" }
115                 },
116
117                 ["olsrd_quagga.so.0.2.2"] = {
118                         { StaticList,   "redistribute",                 {
119                                 "system", "kernel", "connect", "static", "rip", "ripng", "ospf",
120                                 "ospf6", "isis", "bgp", "hsls"
121                         } },
122                         { ListValue,    "ExportRoutes",                 { "only", "both" } },
123                         { Flag,                 "LocalPref",                    "true" },
124                         { Value,                "Distance",                             Range(0,255) }
125                 },
126
127                 ["olsrd_secure.so.0.5"] = {
128                         { Value,                "Keyfile",                              "/etc/private-olsr.key" }
129                 },
130
131                 ["olsrd_txtinfo.so.0.1"] = {
132                         { Value,                "accept",                               "10.247.200.4" }
133                 },
134
135                 ["olsrd_arprefresh.so.0.1"]             = {},
136                 ["olsrd_dot_draw.so.0.3"]               = {},
137                 ["olsrd_dyn_gw_plain.so.0.4"]   = {},
138                 ["olsrd_pgraph.so.1.1"]                 = {},
139                 ["olsrd_tas.so.0.1"]                    = {}
140         }
141
142
143         -- build plugin options with dependencies
144         if knownPlParams[arg[1]] then
145                 for _, option in ipairs(knownPlParams[arg[1]]) do
146                         local otype, name, default, uci2cbi, cbi2uci = unpack(option)
147                         local values
148
149                         if type(default) == "table" then
150                                 values  = default
151                                 default = default[1]
152                         end
153
154                         if otype == Flag then
155                                 local bool = p:option( Flag, name )
156                                 if default == "yes" or default == "no" then
157                                         bool.enabled  = "yes"
158                                         bool.disabled = "no"
159                                 elseif default == "on" or default == "off" then
160                                         bool.enabled  = "on"
161                                         bool.disabled = "off"
162                                 elseif default == "1" or default == "0" then
163                                         bool.enabled  = "1"
164                                         bool.disabled = "0"
165                                 else
166                                         bool.enabled  = "true"
167                                         bool.disabled = "false"
168                                 end
169                                 bool.optional = true
170                                 bool.default = default
171                                 bool:depends({ library = plugin })
172                         else
173                                 local field = p:option( otype, name )
174                                 if values then
175                                         for _, value in ipairs(values) do
176                                                 field:value( value )
177                                         end
178                                 end
179                                 if type(uci2cbi) == "function" then
180                                         function field.cfgvalue(self, section)
181                                                 return uci2cbi(otype.cfgvalue(self, section))
182                                         end
183                                 end
184                                 if type(cbi2uci) == "function" then
185                                         function field.formvalue(self, section)
186                                                 return cbi2uci(otype.formvalue(self, section))
187                                         end
188                                 end
189                                 field.optional = true
190                                 field.default = default
191                                 --field:depends({ library = arg[1] })
192                         end
193                 end
194         end
195
196         return mp
197
198 else
199
200         mpi = Map("olsrd", "OLSR - Plugins")
201
202         local plugins = {}
203         mpi.uci:foreach("olsrd", "LoadPlugin",
204                 function(section)
205                         if section.library and not plugins[section.library] then
206                                 plugins[section.library] = true
207                         end
208                 end
209         )
210
211         -- create a loadplugin section for each found plugin
212         for k, v in pairs(luci.fs.dir("/usr/lib")) do
213                 if v:sub(1, 6) == "olsrd_" then
214                         if not plugins[v] then
215                                 mpi.uci:section(
216                                         "olsrd", "LoadPlugin", nil,
217                                         { library = v, ignore = 1 }
218                                 )
219                         end
220                 end
221         end
222
223         t = mpi:section( TypedSection, "LoadPlugin", "Plugins" )
224         t.anonymous = true
225         t.template  = "cbi/tblsection"
226         t.override_scheme = true
227         function t.extedit(self, section)
228                 local lib = self.map:get(section, "library") or ""
229                 return luci.dispatcher.build_url("admin", "services", "olsrd", "plugins") .. "/" .. lib
230         end
231
232         ign = t:option( Flag, "ignore", "Enabled" )
233         ign.enabled = "0"
234         ign.disabled = "1"
235         function ign.cfgvalue(self, section)
236                 return Flag.cfgvalue(self, section) or "0"
237         end
238
239         t:option( DummyValue, "library", "Library" )
240
241         return mpi
242 end