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.2 2004/10/23 15:00:15 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 : Dbi.connection) 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 ^ "/");
47 let userid, username = match user with
48 User (userid, username, _) -> userid, username
49 | _ -> assert false in
51 let subject = username ^ " has invited you to join " ^ hostname in
53 (* Get user's own email address, which will be the return address
56 let sth = dbh#prepare_cached "select email from users
57 where hostid = ? and id = ?" in
58 sth#execute [`Int hostid; `Int userid];
61 match sth#fetch1 () with
62 [ `String email ] -> email
63 | [ `Null ] -> "service@merjis.com"
64 | _ -> assert false in
66 (* Add user accounts for these new users. For users who are already
67 * registered, we'll send reminder emails.
71 let sth = dbh#prepare_cached "select id, invite from users
73 (email = ? or name = ?)" in
74 sth#execute [`Int hostid; `String email; `String email];
78 (match sth#fetch1 () with
79 [ `Int userid; `Null ] ->
80 (* Existing user account - send reminder. *)
81 template_exists#set "username" username;
82 template_exists#set "hostname" hostname;
83 template_exists#to_string
85 | [ `Int userid; `String invite ] ->
86 (* Existing user account - resend the invite. *)
87 template#set "username" username;
88 template#set "hostname" hostname;
89 template#set "invite" invite;
95 (* Add user account. *)
96 let invite = random_sessionid () in
97 let sth = dbh#prepare_cached "insert into users (hostid, name,
98 password, email, invite) values (?, ?, ?, ?, ?)" in
99 sth#execute [`Int hostid; `String email; `String invite;
100 `String email; `String invite];
102 template#set "username" username;
103 template#set "hostname" hostname;
104 template#set "invite" invite;
105 template#to_string in
107 (* Send the email. *)
108 Sendmail.send_mail ~subject ~to_addr:[email] ~from ~body ()
114 let buttons = [ ok_button "/_users" ] in
115 ok ~buttons ~title:"Invitation emails sent"
116 q "We sent invitations emails to those address(es)."
119 register_script ~restrict:[CanManageUsers] ~anonymous:false run