aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 2e215cd656394421dfd8e3e3e773cb297bdb9e26 (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
91
92
93
94
95
96
97
98
#include <queue>
#include <string>
#include <vector>
#include <iostream>

#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::vector<crow::json::wvalue> {
        std::vector<crow::json::wvalue> reply_vector;
        reply_vector.emplace_back(Response::create_text(text));
        return reply_vector;
    };

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

    CROW_ROUTE(app, "/")
            .methods("POST"_method)
                    ([&regular_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(
                                regular_commands.begin(),
                                regular_commands.end(),
                                [command](Handler::CommandHandler const &handler) {
                                    return handler.command == command;
                                });

                        if (itor != regular_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;
}