Changes done on the Mac.
[cocanwiki.git] / scripts / invite_user.ml
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.7 2006/03/28 13:20:00 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_template
30
31 let split_re = Pcre.regexp "[\\s,;]+"
32
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
36
37   (* Get the email addresses. *)
38   let emails = q#param "emails" in
39   let emails = Pcre.split ~rex:split_re emails in
40
41   (* This guy's got no friends ... *)
42   if emails = [] then (
43     q#redirect ("http://" ^ hostname ^ "/");
44     return ()
45   );
46
47   let userid, username, email = match user with
48       User (userid, username, _, prefs) -> userid, username, prefs.email
49     | _ -> assert false in
50
51   let subject = username ^ " has invited you to join " ^ hostname in
52
53   (* Get a suitable return address for the email. *)
54   let from =
55     match email with
56       | Some email -> email
57       | None -> "service@merjis.com" in
58
59   (* Add user accounts for these new users.  For users who are already
60    * registered, we'll send reminder emails.
61    *)
62   List.iter
63     (fun email ->
64        let rows = PGSQL(dbh) "select id, invite from users
65                                where hostid = $hostid and
66                                (email = $email or name = $email)" in
67
68        let body =
69          match rows with
70          | [ userid, None ] ->
71              (* Existing user account - send reminder. *)
72              template_exists#set "username" username;
73              template_exists#set "hostname" hostname;
74              template_exists#to_string
75
76          | [ userid, Some invite ] ->
77              (* Existing user account - resend the invite. *)
78              template#set "username" username;
79              template#set "hostname" hostname;
80              template#set "invite" invite;
81              template#to_string
82
83          | [] ->
84              (* Add user account. *)
85              let invite = random_sessionid () in
86              PGSQL(dbh)
87                "insert into users (hostid, name,
88                   password, email, invite) values ($hostid, $email, $invite,
89                   $email, $invite)";
90
91              template#set "username" username;
92              template#set "hostname" hostname;
93              template#set "invite" invite;
94              template#to_string
95
96          | _ -> assert false in
97
98        (* Send the email. *)
99        Sendmail.send_mail ~subject ~to_addr:[email] ~from body
100     ) emails;
101
102   (* Finish off. *)
103   PGOCaml.commit dbh;
104
105   let buttons = [ ok_button "/_users" ] in
106   ok ~buttons ~title:"Invitation emails sent"
107     dbh hostid q "We sent invitations emails to those address(es)."
108
109 let () =
110   register_script ~restrict:[CanManageUsers] ~anonymous:false run