applications/luci-firewall: make rule descriptions fully translateable
[project/luci.git] / applications / luci-firewall / luasrc / model / cbi / firewall / forwards.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 local ds = require "luci.dispatcher"
16 local ft = require "luci.tools.firewall"
17
18 m = Map("firewall", translate("Firewall - Port Forwards"),
19         translate("Port forwarding allows remote computers on the Internet to \
20                    connect to a specific computer or service within the \
21                    private LAN."))
22
23 --
24 -- Port Forwards
25 --
26
27 s = m:section(TypedSection, "redirect", translate("Port Forwards"))
28 s.template  = "cbi/tblsection"
29 s.addremove = true
30 s.anonymous = true
31 s.sortable  = true
32 s.extedit   = ds.build_url("admin/network/firewall/forwards/%s")
33 s.template_addremove = "firewall/cbi_addforward"
34
35 function s.create(self, section)
36         local n = m:formvalue("_newfwd.name")
37         local p = m:formvalue("_newfwd.proto")
38         local e = m:formvalue("_newfwd.extport")
39         local a = m:formvalue("_newfwd.intaddr")
40         local i = m:formvalue("_newfwd.intport")
41
42         if p == "other" or (p and a) then
43                 created = TypedSection.create(self, section)
44
45                 self.map:set(created, "target",    "DNAT")
46                 self.map:set(created, "src",       "wan")
47                 self.map:set(created, "dest",      "lan")
48                 self.map:set(created, "proto",     (p ~= "other") and p or "all")
49                 self.map:set(created, "src_dport", e)
50                 self.map:set(created, "dest_ip",   a)
51                 self.map:set(created, "dest_port", i)
52                 self.map:set(created, "_name",     n)
53         end
54
55         if p ~= "other" then
56                 created = nil
57         end
58 end
59
60 function s.parse(self, ...)
61         TypedSection.parse(self, ...)
62         if created then
63                 m.uci:save("firewall")
64                 luci.http.redirect(ds.build_url(
65                         "admin/network/firewall/redirect", created
66                 ))
67         end
68 end
69
70 function s.filter(self, sid)
71         return (self.map:get(sid, "target") ~= "SNAT")
72 end
73
74 name = s:option(DummyValue, "_name", translate("Name"))
75 function name.cfgvalue(self, s)
76         return self.map:get(s, "_name") or "-"
77 end
78
79 proto = s:option(DummyValue, "proto", translate("Protocol"))
80 proto.rawhtml = true
81 function proto.cfgvalue(self, s)
82         return ft.fmt_proto(self.map:get(s, "proto")) or "Any"
83 end
84
85
86 src = s:option(DummyValue, "src", translate("Source"))
87 src.rawhtml = true
88 src.width   = "20%"
89 function src.cfgvalue(self, s)
90         local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
91         local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
92         local p = ft.fmt_port(self.map:get(s, "src_port"))
93         local m = ft.fmt_mac(self.map:get(s, "src_mac"))
94
95         if p and m then
96                 return translatef("From %s in %s with source %s and %s", a, z, p, m)
97         elseif p or m then
98                 return translatef("From %s in %s with source %s", a, z, p or m)
99         else
100                 return translatef("From %s in %s", a, z)
101         end
102 end
103
104 via = s:option(DummyValue, "via", translate("Via"))
105 via.rawhtml = true
106 via.width   = "20%"
107 function via.cfgvalue(self, s)
108         local a = ft.fmt_ip(self.map:get(s, "src_dip"), translate("any router IP"))
109         local p = ft.fmt_port(self.map:get(s, "src_dport"))
110
111         if p then
112                 return translatef("To %s at %s", a, p)
113         else
114                 return translatef("To %s", a)
115         end
116 end
117
118 dest = s:option(DummyValue, "dest", translate("Destination"))
119 dest.rawhtml = true
120 dest.width   = "30%"
121 function dest.cfgvalue(self, s)
122         local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
123         local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
124         local p = ft.fmt_port(self.map:get(s, "dest_port")) or
125                 ft.fmt_port(self.map:get(s, "src_dport"))
126
127         if p then
128                 return translatef("Forward to %s, %s in %s", a, p, z)
129         else
130                 return translatef("Forward to %s in %s", a, z)
131         end
132 end
133
134 return m