8c114bfb899dc2deb035cf4544e4307e25473cf0
[lede-routing/.git] / main.lua
1 --[[
2     Copyright (C) 2011 Pau Escrich <pau@dabax.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18     The full GNU General Public License is included in this distribution in
19     the file called "COPYING".
20 --]]
21
22 local sys = require("luci.sys")
23 local bmx6json = require("luci.model.bmx6json")
24
25 m = Map("bmx6", "bmx6")
26
27 -- Getting json and Checking if bmx6-json is avaiable
28 local options = bmx6json.get("options")
29 if options == nil or options.OPTIONS == nil then
30          m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
31 else
32         options = options.OPTIONS
33 end
34
35 -- Getting a list of interfaces
36 local eth_int = luci.sys.net.devices()
37
38 -- Getting the most important options from general
39 local general = m:section(NamedSection,"general","general","General")
40 general.addremove = false
41 general:option(Value,"globalPrefix","Global ip prefix","Specify global prefix for interfaces: NETADDR/LENGTH. If you are using IPv6 leave blank to let bmx6 autoassign an ULA IPv6 address.")
42
43 if m:get("ipVersion","ipVersion") == "6" then
44         general:option(Value,"tun4Address","IPv4 address or range","specify default IPv4 tunnel address and announced range")
45 end
46
47 -- IP section
48 -- ipVersion section is important, we are allways showing it
49 local ipV = m:section(NamedSection,"ipVersion","ipVersion","IP options")
50 ipV.addremove = false
51 local lipv = ipV:option(ListValue,"ipVersion","IP version")
52 lipv:value("4","4")
53 lipv:value("6","6")
54 lipv.default = "6"
55
56 -- rest of ip options are optional, getting them from json
57 local ipoptions = {}
58 for _,o in ipairs(options) do
59         if o.name == "ipVersion" and o.CHILD_OPTIONS ~= nil then
60                 ipoptions = o.CHILD_OPTIONS
61                 break
62         end
63 end
64
65 local help = ""
66 local name = ""
67 local value = nil
68
69 for _,o in ipairs(ipoptions) do
70         if o.name ~= nil then
71                 help = ""
72                 name = o.name
73                 if o.help ~= nil then
74                         help = bmx6json.text2html(o.help)
75                 end
76
77                 if o.syntax ~= nil then
78                         help = help .. "<br/><strong>Syntax: </strong>" .. bmx6json.text2html(o.syntax)
79                 end
80
81                 if o.def ~= nil then
82                         help = help .. "<br/><strong> Default: </strong>" .. bmx6json.text2html(o.def)
83                 end
84
85                 value = ipV:option(Value,name,name,help)
86                 value.optional = true
87         end
88 end
89
90 -- Interfaces section
91 local interfaces = m:section(TypedSection,"dev","Devices","")
92 interfaces.addremove = true
93 interfaces.anonymous = true
94 local intlv = interfaces:option(ListValue,"dev","Device")
95
96 for _,i in ipairs(eth_int) do
97         intlv:value(i,i)
98 end
99
100 function m.on_commit(self,map)
101     local err = sys.call('bmx6 -c --configReload > /tmp/bmx6-luci.err.tmp')
102     if err ~= 0 then
103         m.message = sys.exec("cat /tmp/bmx6-luci.err.tmp")
104     end
105 end
106
107 return m
108