1 (* COCANWIKI - a wiki written in Objective CAML.
2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: edit_emails.ml,v 1.5 2004/09/09 12:21:22 rich Exp $
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 open Cocanwiki_strings
31 let split_re = Pcre.regexp "[\\r\\n,;]+"
32 let email_re = Pcre.regexp "(.*)<(.*)>"
34 let run r (q : cgi) (dbh : Dbi.connection) _ host' _ =
35 let hostid = int_of_string (q#param "hostid") in
37 if q#param_true "cancel" then (
38 let { hostname = hostname } = host' in
39 q#redirect ("http://" ^ hostname ^ "/_bin/admin/host.cmo?hostid=" ^
40 string_of_int hostid);
44 let emails = try q#param "emails" with Not_found -> "" in
46 (* It's very hard to verify email addresses. Thus this script
47 * should not be exposed to untrusted users.
52 let subs = Pcre.exec ~rex:email_re str in
53 Pcre.get_substring subs 1, Pcre.get_substring subs 2
58 (* Trim whitespace. *)
62 let emails = Pcre.split ~rex:split_re emails in
63 let emails = List.map check_email emails in
64 let emails = List.filter ((<>) ("","")) emails in
66 (* Update the database. *)
67 let sth = dbh#prepare_cached
68 "delete from email_notify where hostid = ?" in
69 sth#execute [`Int hostid];
70 let sth = dbh#prepare_cached "insert into email_notify (hostid, email, name)
72 List.iter (fun (name, email) ->
74 sth#execute [`Int hostid; `String email; `Null]
76 sth#execute [`Int hostid; `String email; `String name])
79 (* Commit to the database. *)
82 (* Print confirmation page. *)
84 { StdPages.label = "OK";
85 StdPages.link = "/_bin/admin/host.cmo";
86 StdPages.method_ = None;
87 StdPages.params = [ "hostid", string_of_int hostid ] }
90 ok ~title:"Saved" ~buttons
91 q "Email notifications updated."