root/http/recovery.cc

Revision 92c18c14e163e2ab3200ccad6c6c0e6ee5c3e1a6, 6.1 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 21 months ago)

Make Alue self-identity in emails configurable

Fixes #52

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009, 2010 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 "authn.hh"
21#include "error_resource.hh"
22#include "redir_resource.hh"
23#include "request.hh"
24#include "templated_resource.hh"
25#include "response.hh"
26#include "token.hh"
27
28#include "../config.hh"
29#include "../db/db.hh"
30#include "../db/user.hh"
31#include "../html/util.hh"
32#include "../msg/lexutils.hh"
33#include "../server.hh"
34#include "../smtp_client/smtp_client.hh"
35#include "../tlate/tlate.hh"
36#include "../util.hh"
37
38#include <boost/shared_ptr.hpp>
39
40namespace http
41{
42        class recovery : public templated_resource
43        {
44                void do_post(boost::shared_ptr<request>);
45        public:
46                recovery(server::conn_cb cb)
47                        : templated_resource(cb, "recovery.html")
48                        {}
49        protected:
50                void set_attributes(boost::shared_ptr<request>,
51                                    tlate::data_model::ptr);
52        };
53
54        void recovery::set_attributes(boost::shared_ptr<request> req,
55                                      tlate::data_model::ptr am)
56        {
57                if (req->get_method() == "POST") return do_post(req);
58
59                if (req->get_path() != "/recovery")
60                {
61                        boost::shared_ptr<resource> er
62                                (new error_resource(cb, "404 Not found"));
63                        throw resource_exception(er);
64                }
65               
66                std::string (*const q)(std::string,bool) = html::quote;
67//              std::string (*const p)(std::string) = uri::percent_encode;
68//              std::string (*const dep)(std::string) = uri::percent_decode;
69
70                am->insert("action", q("/recovery", false));
71                am->insert("method", "post");
72                am->insert("enctype", "application/x-www-form-urlencoded");
73                am->insert("accept_charlist", "utf8");
74        }
75
76        void recovery::do_post(boost::shared_ptr<request> req)
77        {
78                std::string (*const q)(std::string,bool) = html::quote;
79//              std::string (*const p)(std::string) = uri::percent_encode;
80//              std::string (*const dep)(std::string) = uri::percent_decode;
81
82                std::string user = req->get_form_field("userid");
83                boost::shared_ptr<db::user> u = cb.dbase().lookup_user(user);
84                if (u)
85                {
86                        boost::random_device rand;
87                        std::string ton = u->get_display_name();
88                        std::string to = u->get_delivery_email();
89                        std::string from =
90                                config["alue-name"].as<std::string>() +
91                                " <" +
92                                config["operator-email"].as<std::string>() +
93                                ">";
94                               
95                        std::string token;
96                        for (size_t i = 0; i < 30; i++)
97                                token += util::b64_char(rand() % 64);
98                        token += "42"; /* just in case the last char
99                                          is punctuation, in order to
100                                          not confuse users */
101                        cb.get_http_token(token).reset
102                                (new http::token(u, http::token::PASSWD));
103
104                        std::ostringstream link;
105                        link << "https://" << req->get_host();
106                        if (req->get_port() != -1)
107                                link << ':' << req->get_port();
108                        link << "/passwd/" << uri::percent_encode(token);
109
110                        tlate::data_model::ptr mam(new tlate::data_model);
111
112                        mam->insert("from", from);
113                        mam->insert("to",
114                                    msg::make_phrase(ton) + " <" + to + ">");
115                        mam->insert("userid", u->get_userid());
116                        mam->insert("link", link.str());
117                        mam->insert("request_ip", req->get_peer().to_string());
118
119                        boost::shared_ptr<tlate::tlate> tl =
120                                tlate::tlate::parse("recovery.msg");
121                        std::ostringstream mos;
122                        mos << tl->eval(mam);
123                       
124                        std::list<std::string> recv;
125                        recv.push_back(to);
126                               
127                        cb.smtpc().send_mail(recv, mos.str());
128                }
129
130                boost::shared_ptr<resource> rr
131                        (new redir_resource(cb,
132                                            std::string("/recovered?userid=") +
133                                            q(user, false),
134                                            "303 See other"));
135                throw resource_exception(rr);
136        }
137}
138
139namespace
140{
141        class factory : public server::http_resource_factory
142        {
143        public:
144                factory() {
145                        server::register_http_resource("/recovery", this);
146                }
147                boost::shared_ptr<http::resource> operator()
148                (server::conn_cb cb,std::string) {
149                        boost::shared_ptr<http::resource> rv
150                                (new http::recovery(cb));
151                        return rv;
152                }
153        };
154        factory f;
155}
Note: See TracBrowser for help on using the browser.