aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 72fd9a0059cea64ff3a52ed846c522f44eb230f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <functional>
#include <unordered_map>

#include "crow.h"
#include "sqdb.hpp"

#include "Handler.hpp"
#include "Response.hpp"

/* BEGIN Handlers */
#include "GetEssen.hpp"
#include "SimpleHandlers.hpp"
#include "RelationshipHandler.hpp"

/* END Handlers */

int main() {
    crow::SimpleApp app;

    // command --> handler
    std::unordered_map<std::string, Handler::CommandHandler> commands{
            {"wiki",     {.func = Handler::wikiHandler, .description = "Send you to wikipedia"}},
            {"stop",     {.func = Handler::stopHandler, .description = "Stops the bot", .payload = &app}},
            {"mensa",    {.func = Handler::mensaHandler, .description = "{adlershof, nord, sued} Shows the daily menu of the mensa at various places."}},
            {"klinger",  {.func = Handler::klingerHandler, .description = "Greats in french. Bonjour!"}},
            {"relation", {.func = Handler::relationShipHandler, .description = "[name1] [name2] Shows the result of an odd astrological religious pseudo-algorithm, based on the fact how much blessing a relationship receives by the glorious N."}},
    };

    commands.insert({"help",
                     {.func = Handler::helpHandler, .description = "This is my holy manual.", .payload = &commands}});

    CROW_ROUTE(app, "/")
            .methods("POST"_method)
                    ([&commands](crow::request const &request) {
                        auto data = crow::json::load(request.body);

                        if (!data)
                            return crow::response(400);

                        if (!data.count("command"))
                            return crow::response(400, "malformed request: missing `command` field\n");

                        std::string command = (data.count("command") ? data["command"].s() : std::string(
                                "No such command!"));
                        std::string arguments = (data.count("arguments") ? data["arguments"].s() : std::string(""));
                        std::string session = (data.count("session") ? data["session"].s() : std::string("null"));

                        if (auto itor = commands.find(command); itor != commands.end()) {
                            auto handler = itor->second;
                            return crow::response{handler.exec(arguments, session)};
                        }

                        return crow::response{Response::simple_response("No such command!", "null", false)};
                    });

    app.port(18080).multithreaded().run();

    std::cout << "Stopped successfully" << std::endl;
}