Global replace 'raise CgiExit' -> 'return ()' to match a future version of mod_caml.
[cocanwiki.git] / scripts / admin / edit_emails.ml
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.6 2004/09/23 11:56:47 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_strings
30
31 let split_re = Pcre.regexp "[\\r\\n,;]+"
32 let email_re = Pcre.regexp "(.*)<(.*)>"
33
34 let run r (q : cgi) (dbh : Dbi.connection) _ host' _ =
35   let hostid = int_of_string (q#param "hostid") in
36
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);
41     return ()
42   );
43
44   let emails = try q#param "emails" with Not_found -> "" in
45
46   (* It's very hard to verify email addresses.  Thus this script
47    * should not be exposed to untrusted users.
48    *)
49   let check_email str =
50     let name, email =
51       try
52         let subs = Pcre.exec ~rex:email_re str in
53         Pcre.get_substring subs 1, Pcre.get_substring subs 2
54       with
55           Not_found ->
56             "", str in
57
58     (* Trim whitespace. *)
59     trim name, trim email
60   in
61
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
65
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)
71                                 values (?, ?, ?)" in
72   List.iter (fun (name, email) ->
73                if name = "" then
74                  sth#execute [`Int hostid; `String email; `Null]
75                else
76                  sth#execute [`Int hostid; `String email; `String name])
77     emails;
78
79   (* Commit to the database. *)
80   dbh#commit ();
81
82   (* Print confirmation page. *)
83   let buttons = [
84     { StdPages.label = "OK";
85       StdPages.link = "/_bin/admin/host.cmo";
86       StdPages.method_ = None;
87       StdPages.params = [ "hostid", string_of_int hostid ] }
88   ] in
89
90   ok ~title:"Saved" ~buttons
91     q "Email notifications updated."
92
93 let () =
94   register_script run