2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: edit_emails.ml,v 1.3 2004/09/07 14:58:34 rich Exp $
14 open Cocanwiki_strings
16 let split_re = Pcre.regexp "[\\r\\n,;]+"
17 let email_re = Pcre.regexp "(.*)<(.*)>"
19 let run r (q : cgi) (dbh : Dbi.connection) host_stuff _ =
20 let hostid = int_of_string (q#param "hostid") in
22 if q#param_true "cancel" then (
23 let _, hostname, _ = host_stuff in
24 q#redirect ("http://" ^ hostname ^ "/_bin/admin/host.cmo?hostid=" ^
25 string_of_int hostid);
29 let emails = try q#param "emails" with Not_found -> "" in
31 (* It's very hard to verify email addresses. Thus this script
32 * should not be exposed to untrusted users.
37 let subs = Pcre.exec ~rex:email_re str in
38 Pcre.get_substring subs 1, Pcre.get_substring subs 2
43 (* Trim whitespace. *)
47 let emails = Pcre.split ~rex:split_re emails in
48 let emails = List.map check_email emails in
49 let emails = List.filter ((<>) ("","")) emails in
51 (* Update the database. *)
52 let sth = dbh#prepare_cached
53 "delete from email_notify where hostid = ?" in
54 sth#execute [`Int hostid];
55 let sth = dbh#prepare_cached "insert into email_notify (hostid, email, name)
57 List.iter (fun (name, email) ->
59 sth#execute [`Int hostid; `String email; `Null]
61 sth#execute [`Int hostid; `String email; `String name])
64 (* Commit to the database. *)
67 (* Print confirmation page. *)
69 { StdPages.label = "OK";
70 StdPages.link = "/_bin/admin/host.cmo";
71 StdPages.method_ = None;
72 StdPages.params = [ "hostid", string_of_int hostid ] }
75 ok ~title:"Saved" ~buttons
76 q "Email notifications updated."