(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: edit_emails.ml,v 1.6 2004/09/23 11:56:47 rich Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Apache open Registry open Cgi open Printf open Cocanwiki open Cocanwiki_ok open Cocanwiki_strings let split_re = Pcre.regexp "[\\r\\n,;]+" let email_re = Pcre.regexp "(.*)<(.*)>" let run r (q : cgi) (dbh : Dbi.connection) _ host' _ = let hostid = int_of_string (q#param "hostid") in if q#param_true "cancel" then ( let { hostname = hostname } = host' in q#redirect ("http://" ^ hostname ^ "/_bin/admin/host.cmo?hostid=" ^ string_of_int hostid); return () ); let emails = try q#param "emails" with Not_found -> "" in (* It's very hard to verify email addresses. Thus this script * should not be exposed to untrusted users. *) let check_email str = let name, email = try let subs = Pcre.exec ~rex:email_re str in Pcre.get_substring subs 1, Pcre.get_substring subs 2 with Not_found -> "", str in (* Trim whitespace. *) trim name, trim email in let emails = Pcre.split ~rex:split_re emails in let emails = List.map check_email emails in let emails = List.filter ((<>) ("","")) emails in (* Update the database. *) let sth = dbh#prepare_cached "delete from email_notify where hostid = ?" in sth#execute [`Int hostid]; let sth = dbh#prepare_cached "insert into email_notify (hostid, email, name) values (?, ?, ?)" in List.iter (fun (name, email) -> if name = "" then sth#execute [`Int hostid; `String email; `Null] else sth#execute [`Int hostid; `String email; `String name]) emails; (* Commit to the database. *) dbh#commit (); (* Print confirmation page. *) let buttons = [ { StdPages.label = "OK"; StdPages.link = "/_bin/admin/host.cmo"; StdPages.method_ = None; StdPages.params = [ "hostid", string_of_int hostid ] } ] in ok ~title:"Saved" ~buttons q "Email notifications updated." let () = register_script run