1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
4 * This program is free software
; you can redistribute it
and/or
5 * modify it under the terms
of the GNU General Public License
6 * as published by the Free Software Foundation
; either version
2
7 * of the License
, or (at your option
) any later version
.
9 * This program is distributed
in the hope that it will be useful
,
10 * but WITHOUT ANY WARRANTY
; without even the implied warranty
of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
. See the
12 * GNU General Public License for more details
.
14 * You should have received a copy
of the GNU General Public License
15 * along
with this program
; if not
, write to the Free Software
16 * Foundation
, Inc
., 51 Franklin Street
, Fifth Floor
, Boston
, MA
02110-1301, USA
.
19 (* Configuring e
-mail aliases
*)
21 structure Alias
:> ALIAS
= struct
25 val aliases
: TextIO.outstream option ref
= ref NONE
26 fun aliasesF () = valOf (!aliases
)
28 val aliasesD
: TextIO.outstream option ref
= ref NONE
29 fun aliasesDF () = valOf (!aliasesD
)
31 fun write s
= TextIO.output (aliasesF (), s
)
32 fun writeD s
= TextIO.output (aliasesDF (), s
)
34 val _
= Domain
.registerBefore
35 (fn _
=> (aliases
:= SOME (Domain
.domainFile
"aliases");
36 aliasesD
:= SOME (Domain
.domainFile
"aliases.default")))
38 val _
= Domain
.registerAfter
39 (fn _
=> (TextIO.closeOut (aliasesF ());
40 TextIO.closeOut (aliasesDF ())))
42 fun validEmailUser s
=
43 size s
> 0 andalso size s
< 50
44 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
50 val _
= Env
.type_one
"emailUser"
55 case String.fields (fn ch
=> ch
= #
"@") s
of
56 [user
] => validEmailUser user
57 |
[user
, host
] => validEmailUser user
andalso Domain
.validDomain host
60 val _
= Env
.type_one
"email"
64 datatype aliasSource
=
69 val source
= fn (EApp ((EVar
"userSource", _
), e
), _
) =>
70 Option
.map
User (Env
.string e
)
71 |
(EVar
"defaultSource", _
) => SOME Default
72 |
(EVar
"catchAllSource", _
) => SOME CatchAll
75 datatype aliasTarget
=
77 | Addresses
of string list
80 val target
= fn (EApp ((EVar
"addressTarget", _
), e
), _
) =>
81 Option
.map
Address (Env
.string e
)
82 |
(EApp ((EVar
"addressesTarget", _
), e
), _
) =>
83 Option
.map
Addresses (Env
.list Env
.string e
)
84 |
(EVar
"dropTarget", _
) => SOME Drop
89 val (prefix
, suffix
) = Substring
.splitl (fn ch
=> ch
<> #
"@") (Substring
.full s
)
91 if Substring
.size suffix
= 0 then
97 fun writeTarget (outf
, t
) =
99 Address s
=> TextIO.output (outf
, localhostify s
)
100 | Addresses
[] => TextIO.output (outf
, "!")
101 | Addresses ss
=> TextIO.output (outf
, String.concatWith
"," (map localhostify ss
))
102 | Drop
=> TextIO.output (outf
, "!")
104 fun writeSource (s
, t
) =
108 write (Domain
.currentDomain ());
110 writeTarget (aliasesF (), t
);
112 | Default
=> (write
"*@";
113 write (Domain
.currentDomain ());
115 writeTarget (aliasesF (), t
);
117 | CatchAll
=> (writeD
"*@";
118 writeD (Domain
.currentDomain ());
120 writeTarget (aliasesDF (), t
);
123 val _
= Env
.action_two
"aliasPrim"
124 ("from", source
, "to", target
)