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