PayPal link mentioned in reminder e-mails
[hcoop/zz_old/portal.git] / util.sml
CommitLineData
8d347a33 1structure Util :> UTIL =
2struct
3
51520441 4datatype 'a flat_element =
5 BEGIN
6 | END
7 | ITEM of 'a
8type 'a flat_tree = 'a flat_element list
9
8d347a33 10fun printInt n =
11 Web.print (if n < 0 then
12 "-" ^ Int.toString (~n)
13 else
14 Int.toString n)
15
16fun 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
22fun id x = x
23
24fun 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
f1ea3762 30fun neg (r : real) = ~r
f98251aa 31fun add (r1 : real, r2) = r1 + r2
f1ea3762 32
add44c00 33fun isIdent ch = Char.isLower ch orelse Char.isDigit ch orelse ch = #"-"
ff2b7604 34
35fun validHost s =
36 size s > 0 andalso size s < 20 andalso List.all isIdent (String.explode s)
37
38fun validDomain s =
39 size s > 0 andalso size s < 100 andalso List.all validHost (String.fields (fn ch => ch = #".") s)
40
78304862 41fun validUser s =
42 size s > 0 andalso size s < 50 andalso List.all
43 (fn ch => isIdent ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+")
44 (String.explode s)
45
46fun validEmailUser s =
47 size s > 0 andalso size s < 50 andalso List.all
48 (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+")
49 (String.explode s)
50
51fun validEmail s =
52 (case String.fields (fn ch => ch = #"@") s of
53 [user, host] => validEmailUser user andalso validDomain host
54 | _ => false)
55
ff2b7604 56fun whoisUrl dom = String.concat ["http://reports.internic.net/cgi/whois?whois_nic=", dom, "&type=domain"]
57
78304862 58val rnd = ref (Random.rand (0, 0))
59
60fun init () = rnd := Random.rand (SysWord.toInt (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())),
61 SysWord.toInt (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())))
62
63fun randomPassword () = Int.toString (Int.abs (Random.randInt (!rnd)))
64
f038f26c 65fun domainDir dom =
66 String.concatWith "/" ("/etc/domains" :: String.fields (fn ch => ch = #".") dom)
67
ce7b516a 68fun readFile fname =
69 let
70 val inf = TextIO.openIn fname
71
72 fun readLines lines =
73 case TextIO.inputLine inf of
74 NONE => String.concat (List.rev lines)
75 | SOME line => readLines (line :: lines)
76 in
77 readLines []
78 before TextIO.closeIn inf
79 end
80
3ad30cf6 81fun mem (x, ls) = List.exists (fn y => y = x) ls
82
f038f26c 83end