aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp173
1 files changed, 20 insertions, 153 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 6e39f1c..72fd9a0 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -3,63 +3,32 @@
#include <unordered_map>
#include "crow.h"
+#include "sqdb.hpp"
-#include "AnnotationTypes.hpp"
+#include "Handler.hpp"
+#include "Response.hpp"
-#define NULL_STRING std::string("null")
-#define EMPTY_STRING std::string("")
-#define NO_SUCH_COMMAND std::string("no_such_command")
+/* BEGIN Handlers */
+#include "GetEssen.hpp"
+#include "SimpleHandlers.hpp"
+#include "RelationshipHandler.hpp"
-typedef crow::json::wvalue json;
-typedef std::function<json(std::string const &arguments, std::string const &session, void *payload)> handler_function;
-
-struct CommandHandler {
- [[nodiscard]] json exec(std::string const &arguments, std::string const &session) const {
- return func(arguments, session, payload);
- }
-
- handler_function func{nullptr};
- std::string description;
- void *payload{nullptr};
-};
-
-/* ------------------------------------------------------------------------------------------------------------------ */
-
-json helpHandler(std::string const &arguments, std::string const &session, void *payload);
-
-json stopHandler(std::string const &arguments, std::string const &session, void *payload);
-
-json wikiHandler(std::string const &arguments, std::string const &session, void *payload);
-
-json klingerHandler(std::string const &arguments, std::string const &session, void *payload);
-
-/* ------------------------------------------------------------------------------------------------------------------ */
-
-json simple_response(std::string const &text, std::string const &session = NULL_STRING, bool success = true);
-
-json create_annotation(Reply::AnnotationType annotation = Reply::AnnotationType::none,
- std::string const &extra = EMPTY_STRING);
-
-json create_text(std::string const &text);
-
-json create_text(std::string const &text, std::vector<json> &&annotations);
-
-json create_response(std::vector<json> &&reply, std::string const &session = NULL_STRING, bool success = true);
-
-/* ------------------------------------------------------------------------------------------------------------------ */
+/* END Handlers */
int main() {
crow::SimpleApp app;
// command --> handler
- std::unordered_map<std::string, CommandHandler> commands{
- {"stop", {.func = stopHandler, .description = "Stops the bot", .payload = &app}},
- {"wiki", {.func = wikiHandler, .description = "Send you to wikipedia"}},
- {"klinger", {.func = klingerHandler, .description = "Greats in french. Bonjour!"}},
+ 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 = helpHandler, .description = "This is my holy manual.", .payload = &commands}});
+ {.func = Handler::helpHandler, .description = "This is my holy manual.", .payload = &commands}});
CROW_ROUTE(app, "/")
.methods("POST"_method)
@@ -72,122 +41,20 @@ int main() {
if (!data.count("command"))
return crow::response(400, "malformed request: missing `command` field\n");
- std::string command = (data.count("command") ? data["command"].s() : NO_SUCH_COMMAND);
- std::string arguments = (data.count("arguments") ? data["arguments"].s() : EMPTY_STRING);
- std::string session = (data.count("session") ? data["session"].s() : NULL_STRING);
+ 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{simple_response("No such command!", NULL_STRING, false)};
+ return crow::response{Response::simple_response("No such command!", "null", false)};
});
app.port(18080).multithreaded().run();
std::cout << "Stopped successfully" << std::endl;
}
-
-/* ------------------------------------------------------------------------------------------------------------------ */
-
-json klingerHandler(std::string const &arguments, std::string const &session, void *payload) {
- (void) payload;
- return simple_response("Bonjour!");
-}
-
-json wikiHandler(std::string const &arguments, std::string const &session, void *payload) {
- (void) payload;
-
- std::vector<json> linkAnnotations;
- linkAnnotations.emplace_back(create_annotation(Reply::AnnotationType::link, "Wikipedia"));
- linkAnnotations.emplace_back(create_annotation(Reply::AnnotationType::underline));
-
- std::vector<json> reply_vec;
- reply_vec.emplace_back(create_text("Visit "));
- reply_vec.emplace_back(create_text("https://eo.wikipedia.org/", std::move(linkAnnotations)));
- reply_vec.emplace_back(create_text(" today!"));
-
- return create_response(std::move(reply_vec), session, true);
-}
-
-json helpHandler(std::string const &arguments, std::string const &session, void *payload) {
- auto commands = *(std::unordered_map<std::string, CommandHandler> *) payload;
-
- std::vector<json> reply_vec;
- reply_vec.emplace_back(create_text(
- "Reading from the gospel according to saint N,\n"
- "And you shall feast upon the fruits and vegetables as I do in the offering for they will make you strong.\n"
- "\n"
- "Dear my fellow users, these are my official commands, which are offered to my believers.\n"
- "\n"));
-
- for (auto const &itor : commands) {
- std::vector<json> commandAnnotations;
- commandAnnotations.emplace_back(create_annotation(Reply::AnnotationType::command));
-
- reply_vec.emplace_back(create_text("- "));
- reply_vec.emplace_back(create_text(itor.first, std::move(commandAnnotations)));
- reply_vec.emplace_back(create_text(" " + itor.second.description + "\n"));
- }
-
- reply_vec.emplace_back(create_text(
- "\n"
- "Thanks for using me. Takk skal du ha.\n"
- "Bussy\n"
- "N"));
-
- return create_response(std::move(reply_vec), session, true);
-}
-
-json stopHandler(std::string const &arguments, std::string const &session, void *payload) {
- auto app = (crow::SimpleApp *) payload;
- app->stop();
-
- std::vector<json> annotations;
- annotations.emplace_back(create_annotation(Reply::AnnotationType::bold));
-
- std::vector<json> reply_vec;
- reply_vec.emplace_back(create_text("stopped", std::move(annotations)));
-
- return create_response(std::move(reply_vec), session, true);
-}
-
-/* ------------------------------------------------------------------------------------------------------------------ */
-
-json simple_response(std::string const &text, std::string const &session, bool success) {
- std::vector<json> reply_vec;
- reply_vec.emplace_back(create_text(text));
-
- return create_response(std::move(reply_vec), session, success);
-}
-
-json create_annotation(Reply::AnnotationType annotationType, std::string const &extra) {
- json annotation(crow::json::type::Object);
- annotation["type"] = Reply::GetStringAnnotationType(annotationType);
- annotation["extra"] = extra;
- return annotation;
-}
-
-json create_text(std::string const &text) {
- std::vector<json> annotations;
- annotations.emplace_back(create_annotation());
- return create_text(text, std::move(annotations));
-}
-
-json create_text(std::string const &text, std::vector<json> &&annotations) {
- json reply(crow::json::type::Object);
- reply["text"] = text;
- reply["annotations"] = std::move(annotations);
- return reply;
-}
-
-json create_response(std::vector<json> &&reply, std::string const &session, bool success) {
- json response(crow::json::type::Object);
- response["success"] = success;
- response["session"] = session;
- response["reply"] = std::move(reply);
- return response;
-}
-
-// curl -d '{"command":"klinger", "b":"12"}' -H "Content-Type: application/json" -X POST http://localhost:18080/