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: invite_user.ml,v 1.10 2006/12/06 09:46:57 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_template
31 let split_re = Pcre.regexp "[\\s,;]+"
33 let run r (q : cgi) dbh hostid {hostname = hostname} user =
34 let template = _get_template "invite_user.txt" in
35 let template_exists = _get_template "invite_user_exists.txt" in
37 (* Get the email addresses. *)
38 let emails = q#param "emails" in
39 let emails = Pcre.split ~rex:split_re emails in
41 (* This guy's got no friends ... *)
43 q#redirect ("http://" ^ hostname ^ "/");
45 let userid, username, email = match user with
46 User (userid, username, _, prefs) -> userid, username, prefs.email
47 | _ -> assert false in
49 let subject = username ^ " has invited you to join " ^ hostname in
51 (* Get a suitable return address for the email. *)
54 | Some email -> username, email
55 | None -> username, "service@merjis.com" in
57 (* Add user accounts for these new users. For users who are already
58 * registered, we'll send reminder emails.
62 let rows = PGSQL(dbh) "select id, invite from users
63 where hostid = $hostid and
64 (email = $email or name = $email)" in
69 (* Existing user account - send reminder. *)
70 template_exists#set "username" username;
71 template_exists#set "hostname" hostname;
72 template_exists#to_string
74 | [ userid, Some invite ] ->
75 (* Existing user account - resend the invite. *)
76 template#set "username" username;
77 template#set "hostname" hostname;
78 template#set "invite" invite;
82 (* Add user account. *)
83 let invite = random_sessionid () in
85 "insert into users (hostid, name,
86 password, email, invite) values ($hostid, $email, $invite,
89 template#set "username" username;
90 template#set "hostname" hostname;
91 template#set "invite" invite;
94 | _ -> assert false in
97 let to_addrs = [ "", email ] in
99 "text/plain", ["charset", Mimestring.mk_param "UTF-8"] in
102 Netsendmail.compose ~subject
103 ~to_addrs ~from_addr ~content_type body in
104 Netsendmail.sendmail msg
110 let buttons = [ ok_button "/_users" ] in
111 ok ~buttons ~title:"Invitation emails sent"
112 r dbh hostid q "We sent invitations emails to those address(es)."
115 register_script ~restrict:[CanManageUsers] ~anonymous:false run