+datatype listing =
+ Error of string
+ | Listing of {user : string, mailbox : string} list
+
+fun list domain =
+ let
+ val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.Courier.userdbDir,
+ file = domain})
+
+ fun loop users =
+ case TextIO.inputLine inf of
+ NONE => Listing (rev users)
+ | SOME line =>
+ case String.tokens Char.isSpace line of
+ [addr, fields] =>
+ (case String.fields (fn ch => ch = #"@") addr of
+ [user, _] =>
+ let
+ fun parseFields fields =
+ case fields of
+ "mail" :: mailbox :: _ => loop ({user = user, mailbox = mailbox} :: users)
+ | _ :: _ :: rest => parseFields rest
+ | _ => Error "Invalid fields in database"
+ in
+ parseFields (String.fields (fn ch => ch = #"|" orelse ch = #"=") fields)
+ end
+ | _ => Error "Invalid e-mail address format in database")
+ | _ => Error "Invalid entry in database"
+ in
+ loop []
+ before TextIO.closeIn inf
+ end
+ handle IO.Io _ => Listing []
+
+fun mailboxExists {domain, user} =
+ let
+ val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.Courier.userdbDir,
+ file = domain})
+
+ fun loop () =
+ case TextIO.inputLine inf of
+ NONE => false
+ | SOME line =>
+ case String.tokens Char.isSpace line of
+ [addr, _] =>
+ (case String.fields (fn ch => ch = #"@") addr of
+ [user', _] =>
+ user' = user orelse loop ()
+ | _ => false)
+ | _ => false
+ in
+ loop ()
+ before TextIO.closeIn inf
+ end
+ handle IO.Io _ => false
+