Reports for figuring out which accounts to freeze or boot; most of new SSL request...
[bpt/portal.git] / util.sml
CommitLineData
208e2cbc
AC
1structure Util :> UTIL =
2struct
3
b340786b
AC
4datatype 'a flat_element =
5 BEGIN
6 | END
7 | ITEM of 'a
8type 'a flat_tree = 'a flat_element list
9
208e2cbc
AC
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
f49e1088 30fun neg (r : real) = ~r
9bda1e7f 31fun add (r1 : real, r2) = r1 + r2
466c5944 32fun sub (r1 : real, r2) = r1 - r2
aaa50197 33fun mult (r1, r2) = real r1 * r2
f49e1088 34
a4ccdb5e 35fun isIdent ch = Char.isLower ch orelse Char.isDigit ch orelse ch = #"-"
5da9f4a9
AC
36
37fun validHost s =
a570bbd5 38 size s > 0 andalso size s < 40 andalso List.all isIdent (String.explode s)
5da9f4a9
AC
39
40fun validDomain s =
41 size s > 0 andalso size s < 100 andalso List.all validHost (String.fields (fn ch => ch = #".") s)
42
9d1c0e98
AC
43fun validUser s =
44 size s > 0 andalso size s < 50 andalso List.all
45 (fn ch => isIdent ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+")
46 (String.explode s)
47
48fun validEmailUser s =
49 size s > 0 andalso size s < 50 andalso List.all
50 (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+")
51 (String.explode s)
52
53fun validEmail s =
54 (case String.fields (fn ch => ch = #"@") s of
55 [user, host] => validEmailUser user andalso validDomain host
56 | _ => false)
57
5da9f4a9
AC
58fun whoisUrl dom = String.concat ["http://reports.internic.net/cgi/whois?whois_nic=", dom, "&type=domain"]
59
9d1c0e98
AC
60val rnd = ref (Random.rand (0, 0))
61
62fun init () = rnd := Random.rand (SysWord.toInt (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())),
63 SysWord.toInt (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())))
64
65fun randomPassword () = Int.toString (Int.abs (Random.randInt (!rnd)))
66
37cec107 67fun domainDir dom =
acb31ead 68 String.concatWith "/" ("/afs/hcoop.net/common/etc/domtool/nodes/deleuze" :: List.rev (String.fields (fn ch => ch = #".") dom))
37cec107 69
b90b0980
AC
70fun readFile fname =
71 let
72 val inf = TextIO.openIn fname
73
74 fun readLines lines =
75 case TextIO.inputLine inf of
76 NONE => String.concat (List.rev lines)
77 | SOME line => readLines (line :: lines)
78 in
79 readLines []
80 before TextIO.closeIn inf
81 end
82
dfb0d0d7
AC
83fun mem (x, ls) = List.exists (fn y => y = x) ls
84
d5f8418b
AC
85val allLower = CharVector.map Char.toLower
86
9953bee7
AC
87fun normEmail s = case String.tokens Char.isSpace (allLower s) of
88 s :: _ => s
89 | [] => ""
90
6b8b767b
AC
91val s_cutoff = LargeInt.fromInt 60
92val m_cutoff = LargeInt.fromInt (60 * 60)
93val h_cutoff = LargeInt.fromInt (60 * 60 * 24)
94
95fun diffFromNow t =
96 let
97 val secs = Time.toSeconds (Time.- (Time.now (), t))
98 in
99 if LargeInt.< (secs, s_cutoff) then
100 LargeInt.toString secs ^ " seconds"
101 else if LargeInt.< (secs, m_cutoff) then
102 LargeInt.toString (LargeInt.div (secs, s_cutoff)) ^ " minutes"
103 else if LargeInt.< (secs, h_cutoff) then
104 LargeInt.toString (LargeInt.div (secs, m_cutoff)) ^ " hours"
105 else
106 LargeInt.toString (LargeInt.div (secs, h_cutoff)) ^ " days"
107 end
108
37cec107 109end