e6a4c16887463728800535a0cf90f29dbdf60f41
[project/luci.git] / libs / uvl / luasrc / uvl / datatypes.lua
1 --[[
2
3 UCI Validation Layer - Datatype Tests
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.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
17 module( "luci.uvl.datatypes", package.seeall )
18
19 require("luci.fs")
20 require("luci.ip")
21 require("luci.util")
22
23
24 function boolean( val )
25         if val == "1" or val == "yes" or val == "on" or val == "true" then
26                 return true
27         elseif val == "0" or val == "no" or val == "off" or val == "false" then
28                 return true
29         end
30
31         return false
32 end
33
34 function uint( val )
35         local n = tonumber(val)
36         if n ~= nil and math.floor(n) == n and n >= 0 then
37                 return true
38         end
39
40         return false
41 end
42
43 function integer( val )
44         local n = tonumber(val)
45         if n ~= nil and math.floor(n) == n then
46                 return true
47         end
48
49         return false
50 end
51
52 function float( val )
53         return ( tonumber(val) ~= nil )
54 end
55
56 function ipaddr( val )
57         return ip4addr(val) or ip6addr(val)
58 end
59
60 function ip4addr( val )
61         if val then
62                 return luci.ip.IPv4(val) and true or false
63         end
64
65         return false
66 end
67
68 function ip4prefix( val )
69         val = tonumber(val)
70         return ( val and val >= 0 and val <= 32 )
71 end
72
73 function ip6addr( val )
74         if val then
75                 return luci.ip.IPv6(val) and true or false
76         end
77
78         return false
79 end
80
81 function ip6prefix( val )
82         val = tonumber(val)
83         return ( val and val >= 0 and val <= 128 )
84 end
85
86 function port( val )
87         val = tonumber(val)
88         return ( val and val >= 1 and val <= 65535 )
89 end
90
91 function portrange( val )
92         local p1, p2 = val:match("^(%d+)%-(%d+)$")
93         if p1 and p2 and port(p1) and port(p2) then
94                 return true
95         else
96                 return port(val)
97         end
98 end
99
100 function macaddr( val )
101         if val and val:match(
102                 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
103                  "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
104         ) then
105                 local parts = luci.util.split( val, ":" )
106
107                 for i = 1,6 do
108                         parts[i] = tonumber( parts[i], 16 )
109                         if parts[i] < 0 or parts[i] > 255 then
110                                 return false
111                         end
112                 end
113
114                 return true
115         end
116
117         return false
118 end
119
120 function hostname( val )
121         if val and val:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then
122                 return true     -- XXX: ToDo: need better solution
123         end
124
125         return false
126 end
127
128 function host( val )
129         return hostname(val) or ipaddr(val)
130 end
131
132 function string( val )
133         return true             -- Everything qualifies as valid string
134 end
135
136 function directory( val, seen )
137         local s = luci.fs.stat( val )
138         seen = seen or { }
139
140         if s and not seen[s.ino] then
141                 seen[s.ino] = true
142                 if s.type == "directory" then
143                         return true
144                 elseif s.type == "link" then
145                         return directory( luci.fs.readlink(val), seen )
146                 end
147         end
148
149         return false
150 end
151
152 function file( val, seen )
153         local s = luci.fs.stat( val )
154         seen = seen or { }
155
156         if s and not seen[s.ino] then
157                 seen[s.ino] = true
158                 if s.type == "regular" then
159                         return true
160                 elseif s.type == "link" then
161                         return file( luci.fs.readlink(val), seen )
162                 end
163         end
164
165         return false
166 end