applications/firewall: fix display of enabled/disabled rules, #594
[project/luci.git] / applications / luci-firewall / luasrc / tools / firewall.lua
index a2e3bce..07162cb 100644 (file)
@@ -1,7 +1,7 @@
 --[[
 LuCI - Lua Configuration Interface
 
-Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
+Copyright 2011-2012 Jo-Philipp Wich <xm@subsignal.org>
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -9,7 +9,6 @@ You may obtain a copy of the License at
 
        http://www.apache.org/licenses/LICENSE-2.0
 
-$Id$
 ]]--
 
 module("luci.tools.firewall", package.seeall)
@@ -18,7 +17,11 @@ local ut = require "luci.util"
 local ip = require "luci.ip"
 local nx = require "nixio"
 
-local tr, trf = luci.i18n.translate, luci.i18n.translatef
+local translate, translatef = luci.i18n.translate, luci.i18n.translatef
+
+local function tr(...)
+       return tostring(translate(...))
+end
 
 function fmt_neg(x)
        if type(x) == "string" then
@@ -51,7 +54,7 @@ function fmt_mac(x)
        end
 end
 
-function fmt_port(x)
+function fmt_port(x, d)
        if x and #x > 0 then
                local p, n
                local l = { tr("port"), " " }
@@ -74,9 +77,10 @@ function fmt_port(x)
                        return table.concat(l, "")
                end
        end
+       return d and "<var>%s</var>" % d
 end
 
-function fmt_ip(x)
+function fmt_ip(x, d)
        if x and #x > 0 then
                local l = { tr("IP"), " " }
                local v, a, n
@@ -85,7 +89,7 @@ function fmt_ip(x)
                        a, m = v:match("(%S+)/(%d+%.%S+)")
                        a = a or v
                        a = a:match(":") and ip.IPv6(a, m) or ip.IPv4(a, m)
-                       if a and (a:is6() or a:prefix() < 32) then
+                       if a and (a:is6() and a:prefix() < 128 or a:prefix() < 32) then
                                l[1] = tr("IP range")
                                l[#l+1] = "<var title='%s - %s'>%s%s</var>" %{
                                        a:minhost():string(),
@@ -108,13 +112,16 @@ function fmt_ip(x)
                        return table.concat(l, "")
                end
        end
+       return d and "<var>%s</var>" % d
 end
 
-function fmt_zone(x)
+function fmt_zone(x, d)
        if x == "*" then
                return "<var>%s</var>" % tr("any zone")
        elseif x and #x > 0 then
                return "<var>%s</var>" % x
+       elseif d then
+               return "<var>%s</var>" % d
        end
 end
 
@@ -146,6 +153,7 @@ function fmt_proto(x, icmp_types)
                        v, n = fmt_neg(v)
                        if v == "tcpudp" then
                                l[#l+1] = "TCP"
+                               l[#l+1] = ", "
                                l[#l+1] = "UDP"
                                l[#l+1] = ", "
                        elseif v ~= "all" then
@@ -153,7 +161,7 @@ function fmt_proto(x, icmp_types)
                                if p then
                                        -- ICMP
                                        if (p.proto == 1 or p.proto == 58) and t then
-                                               l[#l+1] = trf(
+                                               l[#l+1] = translatef(
                                                        "%s%s with %s",
                                                        n, p.aliases[1] or p.name, t
                                                )
@@ -191,23 +199,91 @@ function fmt_limit(limit, burst)
                                u = tr("day")
                        end
                        if burst and burst > 0 then
-                               return trf("<var>%d</var> pkts. per <var>%s</var>, \
+                               return translatef("<var>%d</var> pkts. per <var>%s</var>, \
                                    burst <var>%d</var> pkts.", l, u, burst)
                        else
-                               return trf("<var>%d</var> pkts. per <var>%s</var>", l, u)
+                               return translatef("<var>%d</var> pkts. per <var>%s</var>", l, u)
+                       end
+               end
+       end
+end
+
+function fmt_target(x, dest)
+       if dest and #dest > 0 then
+               if x == "ACCEPT" then
+                       return tr("Accept forward")
+               elseif x == "REJECT" then
+                       return tr("Refuse forward")
+               elseif x == "NOTRACK" then
+                       return tr("Do not track forward")
+               else --if x == "DROP" then
+                       return tr("Discard forward")
+               end
+       else
+               if x == "ACCEPT" then
+                       return tr("Accept input")
+               elseif x == "REJECT" then
+                       return tr("Refuse input")
+               elseif x == "NOTRACK" then
+                       return tr("Do not track input")
+               else --if x == "DROP" then
+                       return tr("Discard input")
+               end
+       end
+end
+
+
+function opt_enabled(s, t, ...)
+       if t == luci.cbi.Button then
+               local o = s:option(t, "__enabled")
+               function o.render(self, section)
+                       if self.map:get(section, "enabled") ~= "0" then
+                               self.title      = tr("Rule is enabled")
+                               self.inputtitle = tr("Disable")
+                               self.inputstyle = "reset"
+                       else
+                               self.title      = tr("Rule is disabled")
+                               self.inputtitle = tr("Enable")
+                               self.inputstyle = "apply"
                        end
+                       t.render(self, section)
                end
+               function o.write(self, section, value)
+                       if self.map:get(section, "enabled") ~= "0" then
+                               self.map:set(section, "enabled", "0")
+                       else
+                               self.map:del(section, "enabled")
+                       end
+               end
+               return o
+       else
+               local o = s:option(t, "enabled", ...)
+               o.default = "1"
+               return o
        end
 end
 
-function fmt_target(x)
-       if x == "ACCEPT" then
-               return tr("Accept")
-       elseif x == "REJECT" then
-               return tr("Refuse")
-       elseif x == "NOTRACK" then
-               return tr("Do not track")
-       else --if x == "DROP" then
-               return tr("Discard")
+function opt_name(s, t, ...)
+       local o = s:option(t, "name", ...)
+
+       function o.cfgvalue(self, section)
+               return self.map:get(section, "name") or
+                       self.map:get(section, "_name") or "-"
        end
+
+       function o.write(self, section, value)
+               if value ~= "-" then
+                       self.map:set(section, "name", value)
+                       self.map:del(section, "_name")
+               else
+                       self:remove(section)
+               end
+       end
+
+       function o.remove(self, section)
+               self.map:del(section, "name")
+               self.map:del(section, "_name")
+       end
+
+       return o
 end