Changes done on the Mac.
[cocanwiki.git] / scripts / mailing_list_send.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: mailing_list_send.ml,v 1.8 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 open Cocanwiki_strings
31
32 let run r (q : cgi) dbh hostid { hostname = hostname } _ =
33   let template = get_template dbh hostid "mailing_list_send.txt" in
34
35   let email = trim (q#param "email") in
36   let name = trim (q#param "name") in
37
38   if email = "" then (
39     error ~title:"No email address" ~back_button:true
40       dbh hostid q "You must give an email address.";
41     return ()
42   );
43
44   (* Good a place as any to delete old, unconfirmed emails. *)
45   PGSQL(dbh) "delete from mailing_lists
46                where pending is not null
47                  and entry_date < current_date - 7";
48   PGOCaml.commit dbh;
49   PGOCaml.begin_work dbh;  (* We do some more writes below. *)
50
51   (* Is that email address already registered in the database? *)
52   let rows = PGSQL(dbh)
53     "select 1 from mailing_lists where hostid = $hostid and email = $email" in
54   let registered = rows = [Some 1l] in
55
56   if registered then (
57     error ~title:"Email address already used" ~back_button:true
58       dbh hostid q
59       ("That email address is already on our mailing list. "^
60        "If you are not receiving mailing list messages, then you will " ^
61        "need to confirm that address. If you continue to have problems " ^
62        "please contact the site administrator.");
63     return ()
64   );
65
66   (* Create the unique pending and opt_out fields.  The pending field
67    * allows the user to register.  The opt_out field allows the user
68    * to unsubscribe.
69    *)
70   let pending = random_sessionid () in
71   let opt_out = random_sessionid () in
72
73   (* Insert into the database. *)
74   PGSQL(dbh) "insert into mailing_lists (hostid, email, name,
75                 pending, opt_out)
76               values ($hostid, $email, $name, $pending, $opt_out)";
77
78   PGOCaml.commit dbh;
79
80   (* Send the initial email to the user. *)
81   template#set "hostname" hostname;
82   template#set "pending" pending;
83   template#set "opt_out" opt_out;
84
85   let body = template#to_string in
86   let subject = "Site notice: " ^ hostname ^ ": Confirm your email address" in
87   Sendmail.send_mail ~subject ~to_addr:[email] body;
88
89   (* Finish up. *)
90   let buttons = [ ok_button "/" ] in
91   ok ~buttons ~title:"Confirmation email sent"
92     dbh hostid q
93     ("Please check your email now.  You have been sent a confirmation " ^
94      "email so we can verify the email address is yours.  Click on the " ^
95      "first link in that email to confirm.")
96
97 let () =
98   register_script ~restrict:[CanView] run