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 structure SM
= DataStructures
.StringMap
27 val aliases
: Domain
.files SM
.map ref
= ref SM
.empty
28 fun aliasesF node
= valOf (SM
.find (!aliases
, node
))
30 val aliasesD
: Domain
.files SM
.map ref
= ref SM
.empty
31 fun aliasesDF node
= valOf (SM
.find (!aliasesD
, node
))
35 val files
= map (fn node
=> aliasesF node
) nodes
37 (fn s
=> app (fn file
=> #write file s
) files
,
38 fn () => app (fn file
=> #writeDom
file ()) files
)
43 val files
= map (fn node
=> aliasesDF node
) nodes
45 (fn s
=> app (fn file
=> #write file s
) files
,
46 fn () => app (fn file
=> #writeDom
file ()) files
)
49 fun openInAll base
= foldl (fn (node
, r
) =>
52 Domain
.domainsFile
{node
= node
, name
= base
}))
55 val _
= Domain
.registerBefore
56 (fn _
=> (aliases
:= openInAll
"aliases";
57 aliasesD
:= openInAll
"aliases.default"))
59 val _
= Domain
.registerAfter
60 (fn _
=> (SM
.app (fn file
=> #close
file ()) (!aliases
);
61 SM
.app (fn file
=> #close
file ()) (!aliasesD
)))
63 fun validEmailUser s
=
64 size s
> 0 andalso size s
< 50
65 andalso CharVector
.all (fn ch
=> Char.isAlphaNum ch
71 val _
= Env
.type_one
"emailUser"
76 case String.fields (fn ch
=> ch
= #
"@") s
of
77 [user
] => validEmailUser user
78 |
[user
, host
] => validEmailUser user
andalso Domain
.validDomain host
81 val _
= Env
.type_one
"email"
85 datatype aliasSource
=
90 val source
= fn (EApp ((EVar
"userSource", _
), e
), _
) =>
91 Option
.map
User (Env
.string e
)
92 |
(EVar
"defaultSource", _
) => SOME Default
93 |
(EVar
"catchAllSource", _
) => SOME CatchAll
96 datatype aliasTarget
=
98 | Addresses
of string list
101 val target
= fn (EApp ((EVar
"addressTarget", _
), e
), _
) =>
102 Option
.map
Address (Env
.string e
)
103 |
(EApp ((EVar
"addressesTarget", _
), e
), _
) =>
104 Option
.map
Addresses (Env
.list Env
.string e
)
105 |
(EVar
"dropTarget", _
) => SOME Drop
110 val (prefix
, suffix
) = Substring
.splitl (fn ch
=> ch
<> #
"@") (Substring
.full s
)
112 if Substring
.size suffix
= 0 then
118 fun writeSource (env
, s
, t
) =
120 val nodes
= Env
.env (Env
.list Env
.string) (env
, "MailNodes")
122 val (write
, writeDom
) = write nodes
123 val (writeD
, writeDomD
) = writeD nodes
125 fun writeTarget (writer
, t
) =
127 Address s
=> writer (localhostify s
)
128 | Addresses
[] => writer
"!"
129 | Addresses ss
=> writer (String.concatWith
"," (map localhostify ss
))
137 writeTarget (write
, t
);
139 | Default
=> (write
"*@";
142 writeTarget (write
, t
);
144 | CatchAll
=> (writeD
"*@";
147 writeTarget (writeD
, t
);
151 val _
= Env
.actionV_two
"aliasPrim"
152 ("from", source
, "to", target
)