aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 8e0b44249053098748b32fe8a80bea017b247be6 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <queue>
#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>

#include "crow.h"

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

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

/* END Handlers */

int main() {
    crow::SimpleApp app;

    auto createPlainDescriptionFromText = [](std::string const &text) -> std::queue<crow::json::wvalue> * {
        auto reply_queue = new std::queue<crow::json::wvalue>;
        reply_queue->emplace(Response::create_text(text));
        return reply_queue;
    };

    // command --> handler
    std::vector<Handler::CommandHandler> commands;
    commands.emplace_back(
            Handler::CommandHandler{"wiki", Handler::wikiHandler, createPlainDescriptionFromText(
                    "Sends you to Wikipedia!")});
    commands.emplace_back(
            Handler::CommandHandler{"stop", Handler::stopHandler, createPlainDescriptionFromText(
                    "Stops the bot"), &app});
    commands.emplace_back(
            Handler::CommandHandler{"mensa", Handler::mensaHandler, createPlainDescriptionFromText(
                    "{adlershof, nord, sued} Shows the daily menu of the mensa at various places.")});
    commands.emplace_back(
            Handler::CommandHandler{"klinger", Handler::klingerHandler, createPlainDescriptionFromText(
                    "Greats in french. Bonjour!")});
    commands.emplace_back(
            Handler::CommandHandler{"relation", Handler::relationShipHandler, createPlainDescriptionFromText(
                    "[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.emplace_back(Handler::CommandHandler{"help", Handler::helpHandler, createPlainDescriptionFromText(
            "This is my holy manual."), &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"));

                        auto itor = std::find_if(
                                commands.begin(),
                                commands.end(),
                                [command](Handler::CommandHandler const &handler) {
                                    return handler.command == command;
                                });
                        if (itor != commands.end()) {
                            return crow::response{itor->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;
}