root/http/sub.cc

Revision f2c6ab73bbeb02eb513a3952a380305eeeae60ff, 4.3 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 "authn.hh"
21#include "compose.hh"
22#include "error_resource.hh"
23#include "reauthn_resource.hh"
24#include "redir_resource.hh"
25#include "request.hh"
26#include "resource.hh"
27#include "session.hh"
28
29#include "../config.hh"
30#include "../db/db.hh"
31#include "../db/group.hh"
32#include "../db/user.hh"
33#include "../logger/logline.hh"
34
35namespace http
36{
37        class sub : public resource
38        {
39                server::conn_cb cb;
40        public:
41                sub(server::conn_cb cb)
42                        : cb(cb)
43                        {}
44        protected:
45                boost::shared_ptr<response> operator()
46                (boost::shared_ptr<request>, response::factory);
47        };
48
49        boost::shared_ptr<response> sub::operator()
50        (boost::shared_ptr<request> req, response::factory)
51        {
52                std::string redir = req->get_form_field("redir");
53                if (redir.empty())
54                {
55                        redir = "https://" +
56                                config["canonical-name"].as<std::string>() +
57                                (config["https-port"].as<std::string>() == "443"
58                                 ? ""
59                                 : ":" +config["https-port"].as<std::string>())+
60                                "/";
61                }
62                boost::shared_ptr<resource> resp(new redir_resource
63                                                 (cb, redir, "303 See other"));
64                bool sub;
65                if (req->get_path() == "/sub")
66                {
67                        sub = true;
68                }
69                else if (req->get_path() == "/unsub")
70                {
71                        sub = false;
72                }
73                else
74                {
75                        resp.reset(new error_resource(cb, "404 Not found"));
76                        throw resource_exception(resp);
77                }
78                db::group::ptr gr;
79                try
80                {
81                        gr = cb.dbase().lookup_group(req->get_form_field("group"));
82                }
83                catch (db::no_such_group)
84                {
85                        logger::logline ll;
86                        ll << "no such group " << req->get_form_field("group");
87                        throw resource_exception(resp);
88                }
89                if (!req->is_authenticated())
90                {
91                        logger::logline ll;
92                        ll << "not logged in ";
93                        throw resource_exception(resp);
94                }
95                if (req->get_method() != "POST") throw resource_exception(resp);
96                db::user::ptr u = req->get_user();
97                if (!u) throw resource_exception(resp);
98                if (sub)
99                {
100                        gr->add_subscriber(u, cb, req->get_peer());
101                }
102                else
103                {
104                        gr->delete_subscriber(u, cb);
105                }
106                throw resource_exception(resp);
107       }
108}
109
110namespace
111{
112        class factory : public server::http_resource_factory
113        {
114        public:
115                factory() {
116                        server::register_http_resource("/sub", this);
117                        server::register_http_resource("/unsub", this);
118                }
119                boost::shared_ptr<http::resource> operator()
120                (server::conn_cb cb,std::string) {
121                        boost::shared_ptr<http::resource> rv
122                                (new http::sub(cb));
123                        return rv;
124                }
125        };
126        factory f;
127}
Note: See TracBrowser for help on using the browser.