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