root/http/confirm.cc

Revision f2c6ab73bbeb02eb513a3952a380305eeeae60ff, 4.2 KB (checked in by ajk <ajk@…>, 16 months ago)

#82: Integrate sub/unsub messages and email confirmation messages

Signed-off-by: ajk <ajk@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009, 2010, 2011 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20#include "error_resource.hh"
21#include "reauthn_resource.hh"
22#include "redir_resource.hh"
23#include "request.hh"
24#include "templated_resource.hh"
25#include "token.hh"
26
27#include "../config.hh"
28#include "../db/db.hh"
29#include "../db/user.hh"
30#include "../html/util.hh"
31#include "../msg/lexutils.hh"
32#include "../tlate/tlate.hh"
33#include "../smtp_client/smtp_client.hh"
34
35#define REQPATH "/confirm"
36#define REQPREFIX REQPATH "/"
37
38namespace http
39{
40        class confirm : public templated_resource
41        {
42        public:
43                confirm(server::conn_cb cb)
44                        : templated_resource(cb, "confirm.html")
45                        {}
46                void set_attributes(boost::shared_ptr<request>,
47                                    tlate::data_model::ptr);
48        };
49
50        void confirm::set_attributes(boost::shared_ptr<request> req,
51                                     tlate::data_model::ptr am)
52        {
53                std::string (*const q)(std::string,bool) = html::quote;
54                std::string (*const p)(std::string) = uri::percent_encode;
55                std::string (*const dep)(std::string) = uri::percent_decode;
56
57                if (req->get_path().length() <= sizeof REQPREFIX - 1)
58                {
59                        boost::shared_ptr<resource> er
60                                (new error_resource(cb, "404 Not found"));
61                        throw resource_exception(er);
62                }
63
64                if (!req->is_authenticated())
65                {
66                        boost::shared_ptr<resource> er
67                                (new reauthn_resource(cb));
68                        throw resource_exception(er);
69                }
70
71                boost::shared_ptr<db::user> u = req->get_user();
72
73                std::string tokstr =
74                        dep(req->get_path().substr(sizeof REQPREFIX - 1));
75
76                if (req->get_method() == "POST")
77                {
78                        boost::shared_ptr<resource> er;
79                        if (u->verify_delivery_email(tokstr, cb))
80                                er.reset(new redir_resource(cb,
81                                                            "/confirmed.html",
82                                                            "303 See other"));
83                        else
84                                er.reset(new error_resource
85                                         (cb, "410 Invalid or expired link"));
86                        throw resource_exception(er);
87                }
88                am->insert("userid", q(u->get_userid(), false));
89                am->insert("username", q(u->get_display_name(), false));
90                am->insert("diaddr", q(u->get_delivery_email(), false));
91                am->insert("action", q(REQPREFIX + p(tokstr), false));
92                am->insert("method", "post");
93                am->insert("enctype", "application/x-www-form-urlencoded");
94                am->insert("accept_charlist", q("utf8",false));
95        }
96};
97
98namespace
99{
100        class factory : public server::http_resource_factory
101        {
102        public:
103                factory() {
104                        server::register_http_resource(REQPATH, this);
105                }
106                boost::shared_ptr<http::resource> operator()
107                (server::conn_cb cb, std::string) {
108                        boost::shared_ptr<http::resource> rv
109                                (new http::confirm(cb));
110                        return rv;
111                }
112        };
113        factory f;
114}
Note: See TracBrowser for help on using the browser.