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