Commit | Line | Data |
---|---|---|
208e2cbc AC |
1 | structure Util :> UTIL = |
2 | struct | |
3 | ||
b340786b AC |
4 | datatype 'a flat_element = |
5 | BEGIN | |
6 | | END | |
7 | | ITEM of 'a | |
8 | type 'a flat_tree = 'a flat_element list | |
9 | ||
208e2cbc AC |
10 | fun printInt n = |
11 | Web.print (if n < 0 then | |
12 | "-" ^ Int.toString (~n) | |
13 | else | |
14 | Int.toString n) | |
15 | ||
16 | fun printReal n = | |
17 | Web.print (if n < 0.0 then | |
18 | "-" ^ Real.fmt (StringCvt.FIX (SOME 2)) (~n) | |
19 | else | |
20 | Real.fmt (StringCvt.FIX (SOME 2)) n) | |
21 | ||
22 | fun id x = x | |
23 | ||
24 | fun makeSet f items = | |
25 | case items of | |
26 | [] => "()" | |
27 | | [usr] => "(" ^ f usr ^ ")" | |
28 | | usr::rest => foldl (fn (usr, s) => s ^ ", " ^ f usr) ("(" ^ f usr) rest ^ ")" | |
29 | ||
f49e1088 | 30 | fun neg (r : real) = ~r |
9bda1e7f | 31 | fun add (r1 : real, r2) = r1 + r2 |
aaa50197 | 32 | fun mult (r1, r2) = real r1 * r2 |
f49e1088 | 33 | |
a4ccdb5e | 34 | fun isIdent ch = Char.isLower ch orelse Char.isDigit ch orelse ch = #"-" |
5da9f4a9 AC |
35 | |
36 | fun validHost s = | |
a570bbd5 | 37 | size s > 0 andalso size s < 40 andalso List.all isIdent (String.explode s) |
5da9f4a9 AC |
38 | |
39 | fun validDomain s = | |
40 | size s > 0 andalso size s < 100 andalso List.all validHost (String.fields (fn ch => ch = #".") s) | |
41 | ||
9d1c0e98 AC |
42 | fun validUser s = |
43 | size s > 0 andalso size s < 50 andalso List.all | |
44 | (fn ch => isIdent ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+") | |
45 | (String.explode s) | |
46 | ||
47 | fun validEmailUser s = | |
48 | size s > 0 andalso size s < 50 andalso List.all | |
49 | (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+") | |
50 | (String.explode s) | |
51 | ||
52 | fun validEmail s = | |
53 | (case String.fields (fn ch => ch = #"@") s of | |
54 | [user, host] => validEmailUser user andalso validDomain host | |
55 | | _ => false) | |
56 | ||
5da9f4a9 AC |
57 | fun whoisUrl dom = String.concat ["http://reports.internic.net/cgi/whois?whois_nic=", dom, "&type=domain"] |
58 | ||
9d1c0e98 AC |
59 | val rnd = ref (Random.rand (0, 0)) |
60 | ||
61 | fun init () = rnd := Random.rand (SysWord.toInt (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())), | |
62 | SysWord.toInt (Posix.Process.pidToWord (Posix.ProcEnv.getpid ()))) | |
63 | ||
64 | fun randomPassword () = Int.toString (Int.abs (Random.randInt (!rnd))) | |
65 | ||
37cec107 | 66 | fun domainDir dom = |
acb31ead | 67 | String.concatWith "/" ("/afs/hcoop.net/common/etc/domtool/nodes/deleuze" :: List.rev (String.fields (fn ch => ch = #".") dom)) |
37cec107 | 68 | |
b90b0980 AC |
69 | fun readFile fname = |
70 | let | |
71 | val inf = TextIO.openIn fname | |
72 | ||
73 | fun readLines lines = | |
74 | case TextIO.inputLine inf of | |
75 | NONE => String.concat (List.rev lines) | |
76 | | SOME line => readLines (line :: lines) | |
77 | in | |
78 | readLines [] | |
79 | before TextIO.closeIn inf | |
80 | end | |
81 | ||
dfb0d0d7 AC |
82 | fun mem (x, ls) = List.exists (fn y => y = x) ls |
83 | ||
d5f8418b AC |
84 | val allLower = CharVector.map Char.toLower |
85 | ||
9953bee7 AC |
86 | fun normEmail s = case String.tokens Char.isSpace (allLower s) of |
87 | s :: _ => s | |
88 | | [] => "" | |
89 | ||
37cec107 | 90 | end |