(* COCANWIKI - a wiki written in Objective CAML.
* Written by Richard W.M. Jones <rich@merjis.com>.
* Copyright (C) 2004 Merjis Ltd.
- * $Id: invite_user.ml,v 1.1 2004/10/14 15:57:15 rich Exp $
+ * $Id: invite_user.ml,v 1.7 2006/03/28 13:20:00 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
let split_re = Pcre.regexp "[\\s,;]+"
-let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
+let run r (q : cgi) dbh hostid {hostname = hostname} user =
let template = _get_template "invite_user.txt" in
let template_exists = _get_template "invite_user_exists.txt" in
return ()
);
- let userid, username = match user with
- User (userid, username, _) -> userid, username
+ 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 user's own email address, which will be the return address
- * for the email.
- *)
- let sth = dbh#prepare_cached "select email from users
- where hostid = ? and id = ?" in
- sth#execute [`Int hostid; `Int userid];
-
+ (* Get a suitable return address for the email. *)
let from =
- match sth#fetch1 () with
- [ `String email ] -> email
- | [ `Null ] -> "service@merjis.com"
- | _ -> assert false in
+ 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 from users
- where hostid = ? and
- (email = ? or name = ?)" in
- sth#execute [`Int hostid; `String email; `String email];
-
- let userid = try Some (sth#fetch1int ()) with Not_found -> None in
+ let rows = PGSQL(dbh) "select id, invite from users
+ where hostid = $hostid and
+ (email = $email or name = $email)" in
let body =
- match userid with
- Some userid ->
- (* Existing user account - send reminder. *)
- template_exists#set "username" username;
- template_exists#set "hostname" hostname;
- template_exists#to_string
-
- | None ->
- (* Add user account. *)
- let password = random_sessionid () in
- let sth = dbh#prepare_cached "insert into users (hostid, name,
- password, email) values (?, ?, ?, ?)" in
- sth#execute [`Int hostid; `String email; `String password;
- `String email];
-
- template#set "username" username;
- template#set "hostname" hostname;
- template#set "password" password;
- template#to_string in
+ match rows with
+ | [ userid, None ] ->
+ (* Existing user account - send reminder. *)
+ template_exists#set "username" username;
+ template_exists#set "hostname" hostname;
+ template_exists#to_string
+
+ | [ userid, Some invite ] ->
+ (* Existing user account - resend the invite. *)
+ template#set "username" username;
+ template#set "hostname" hostname;
+ template#set "invite" invite;
+ template#to_string
+
+ | [] ->
+ (* Add user account. *)
+ let invite = random_sessionid () in
+ PGSQL(dbh)
+ "insert into users (hostid, name,
+ password, email, invite) values ($hostid, $email, $invite,
+ $email, $invite)";
+
+ template#set "username" username;
+ template#set "hostname" hostname;
+ template#set "invite" invite;
+ template#to_string
+
+ | _ -> assert false in
(* Send the email. *)
- Sendmail.send_mail ~subject ~to_addr:[email] ~from ~body ()
+ Sendmail.send_mail ~subject ~to_addr:[email] ~from body
) emails;
(* Finish off. *)
- dbh#commit ();
+ PGOCaml.commit dbh;
let buttons = [ ok_button "/_users" ] in
ok ~buttons ~title:"Invitation emails sent"
- q "We sent invitations emails to those address(es)."
+ dbh hostid q "We sent invitations emails to those address(es)."
let () =
register_script ~restrict:[CanManageUsers] ~anonymous:false run