(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: invite_user.ml,v 1.3 2004/10/30 10:16:10 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_template let split_re = Pcre.regexp "[\\s,;]+" let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user = let template = _get_template "invite_user.txt" in let template_exists = _get_template "invite_user_exists.txt" in (* Get the email addresses. *) let emails = q#param "emails" in let emails = Pcre.split ~rex:split_re emails in (* This guy's got no friends ... *) if emails = [] then ( q#redirect ("http://" ^ hostname ^ "/"); return () ); let userid, username, email = match user with User (userid, username, _, prefs) -> userid, username, prefs.email | _ -> assert false in let subject = username ^ " has invited you to join " ^ hostname in (* Get a suitable return address for the email. *) let from = match email with | Some email -> email | None -> "service@merjis.com" in (* Add user accounts for these new users. For users who are already * registered, we'll send reminder emails. *) List.iter (fun email -> let sth = dbh#prepare_cached "select id, invite from users where hostid = ? and (email = ? or name = ?)" in sth#execute [`Int hostid; `String email; `String email]; let body = try (match sth#fetch1 () with [ `Int userid; `Null ] -> (* Existing user account - send reminder. *) template_exists#set "username" username; template_exists#set "hostname" hostname; template_exists#to_string | [ `Int userid; `String invite ] -> (* Existing user account - resend the invite. *) template#set "username" username; template#set "hostname" hostname; template#set "invite" invite; template#to_string | _ -> assert false) with Not_found -> (* Add user account. *) let invite = random_sessionid () in let sth = dbh#prepare_cached "insert into users (hostid, name, password, email, invite) values (?, ?, ?, ?, ?)" in sth#execute [`Int hostid; `String email; `String invite; `String email; `String invite]; template#set "username" username; template#set "hostname" hostname; template#set "invite" invite; template#to_string in (* Send the email. *) Sendmail.send_mail ~subject ~to_addr:[email] ~from ~body () ) emails; (* Finish off. *) dbh#commit (); let buttons = [ ok_button "/_users" ] in ok ~buttons ~title:"Invitation emails sent" q "We sent invitations emails to those address(es)." let () = register_script ~restrict:[CanManageUsers] ~anonymous:false run