| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 28 | namespace 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 | |
|---|
| 107 | namespace |
|---|
| 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 | } |
|---|