root/http/moderate.cc

Revision 2114058982a08f59f0f312544a16250eceab0098, 4.3 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 17 months ago)

#14: Support moderators with kill/mark-as-spam and resurrection powers

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 © 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 "resource.hh"
24#include "request.hh"
25#include "../db/db.hh"
26#include "../db/msg.hh"
27
28namespace http
29{
30        class moderate : public resource
31        {
32                server::conn_cb cb;
33                class noauth {};
34        public:
35                moderate(server::conn_cb cb)
36                        : cb(cb)
37                        {}
38
39                void action(boost::shared_ptr<request> req);
40
41                boost::shared_ptr<response> operator()
42                (boost::shared_ptr<request> req, response::factory);
43        };
44
45        void moderate::action(boost::shared_ptr<request> req)
46        {
47                db::user::ptr u = req->get_user();
48                if (!u) throw noauth();
49
50                std::string action = req->get_form_field("action");
51                std::string msgid = req->get_form_field("msgid");
52                std::string reason = req->get_form_field("reason");
53
54                size_t re = reason.find_first_of("\r\n");
55                if (re != std::string::npos) reason = reason.substr(0, re);
56
57                db::msg::ptr m = cb.dbase().lookup_msgid(msgid);
58
59                if (!u->is_moderator(m))
60                {
61                        boost::shared_ptr<error_resource> r
62                                (new error_resource(cb, "403 Not a moderator"));
63                        throw resource_exception(r);
64                }
65
66                if (action != "KILL" && action != "SPAM" && action != "CLEAR")
67                        return;
68
69                cb.dbase().add_record("MODERATION " + u->get_userid() + "\n" +
70                                      action + " " + m->msgid() + " " + reason);
71        }
72
73        boost::shared_ptr<response> moderate::operator()
74        (boost::shared_ptr<request> req, response::factory)
75        {
76                if (req->get_path() == "/mod")
77                {
78                        try
79                        {
80                                if (req->get_method() == "POST") action(req);
81                        }
82                        catch (noauth)
83                        {
84                                boost::shared_ptr<resource> r
85                                        (new reauthn_resource(cb));
86                                throw resource_exception(r);
87                        }
88                        catch (db::no_such_article)
89                        {
90                                ;
91                        }
92                        ::uri redir = req->get_form_field("redir");
93                        boost::shared_ptr<redir_resource> r
94                                (new redir_resource(cb, redir.to_string(),
95                                                    "303 See other"));
96                        throw resource_exception(r);
97                }
98                else
99                {
100                        boost::shared_ptr<error_resource> r
101                                (new error_resource(cb, "404 Not found"));
102                        throw resource_exception(r);
103                }
104        }
105}
106
107namespace
108{
109        class factory : public server::http_resource_factory
110        {
111        public:
112                factory() {
113                        server::register_http_resource("/mod", this);
114                }
115                boost::shared_ptr<http::resource> operator()
116                (server::conn_cb cb, std::string) {
117                        boost::shared_ptr<http::resource> rv
118                                (new http::moderate(cb));
119                        return rv;
120                }
121        };
122        factory f;
123}
Note: See TracBrowser for help on using the browser.