(* COCANWIKI scripts. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: edit_emails.ml,v 1.3 2004/09/07 14:58:34 rich Exp $ *) 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_stuff _ = let hostid = int_of_string (q#param "hostid") in if q#param_true "cancel" then ( let _, hostname, _ = host_stuff in q#redirect ("http://" ^ hostname ^ "/_bin/admin/host.cmo?hostid=" ^ string_of_int hostid); raise CgiExit ); 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