summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/commands/command.cpp20
-rw-r--r--src/commands/command.hpp8
-rw-r--r--src/commands/request.cpp62
-rw-r--r--src/commands/request.hpp42
-rw-r--r--src/commands/request_util.cpp58
-rw-r--r--src/commands/request_util.hpp11
-rw-r--r--src/modules/ping.hpp2
7 files changed, 91 insertions, 112 deletions
diff --git a/src/commands/command.cpp b/src/commands/command.cpp
index 9d6ec6a..bf482f5 100644
--- a/src/commands/command.cpp
+++ b/src/commands/command.cpp
@@ -1,11 +1,12 @@
#include "command.hpp"
-#include <iostream>
+#include <algorithm>
#include <memory>
#include <optional>
#include "../bundle.hpp"
#include "../modules/ping.hpp"
+#include "request.hpp"
namespace bot {
namespace command {
@@ -18,16 +19,17 @@ namespace bot {
}
std::optional<std::variant<std::vector<std::string>, std::string>>
- CommandLoader::run(
- const InstanceBundle &bundle,
- const irc::Message<irc::MessageType::Privmsg> &msg) const {
- for (const std::unique_ptr<Command> &command : this->commands) {
- if (command->get_name() == msg.message) {
- return command->run(bundle, msg);
- }
+ CommandLoader::run(const InstanceBundle &bundle,
+ const Request &request) const {
+ auto command = std::find_if(
+ this->commands.begin(), this->commands.end(),
+ [&](const auto &x) { return x->get_name() == request.command_id; });
+
+ if (command == this->commands.end()) {
+ return std::nullopt;
}
- return std::nullopt;
+ return (*command)->run(bundle, request);
}
}
}
diff --git a/src/commands/command.hpp b/src/commands/command.hpp
index 5f69788..498a1f2 100644
--- a/src/commands/command.hpp
+++ b/src/commands/command.hpp
@@ -7,7 +7,7 @@
#include <vector>
#include "../bundle.hpp"
-#include "../irc/message.hpp"
+#include "request.hpp"
namespace bot {
namespace command {
@@ -15,8 +15,7 @@ namespace bot {
public:
virtual std::string get_name() const = 0;
virtual std::variant<std::vector<std::string>, std::string> run(
- const InstanceBundle &bundle,
- const irc::Message<irc::MessageType::Privmsg> &msg) const = 0;
+ const InstanceBundle &bundle, const Request &request) const = 0;
};
class CommandLoader {
@@ -26,8 +25,7 @@ namespace bot {
void add_command(std::unique_ptr<Command> cmd);
std::optional<std::variant<std::vector<std::string>, std::string>> run(
- const InstanceBundle &bundle,
- const irc::Message<irc::MessageType::Privmsg> &msg) const;
+ const InstanceBundle &bundle, const Request &msg) const;
const std::vector<std::unique_ptr<Command>> &get_commands() const {
return this->commands;
diff --git a/src/commands/request.cpp b/src/commands/request.cpp
deleted file mode 100644
index 72b04c7..0000000
--- a/src/commands/request.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-#include "request.hpp"
-
-#include <algorithm>
-#include <optional>
-#include <string>
-#include <vector>
-
-#include "../constants.hpp"
-
-namespace bot {
- namespace command {
- bool Request::fill_request() {
- std::vector<std::string> parts =
- utils::string::split_text(irc_message.message, ' ');
-
- std::string command_id = parts[0];
-
- if (command_id.substr(0, DEFAULT_PREFIX.length()) != DEFAULT_PREFIX) {
- return false;
- }
-
- command_id =
- command_id.substr(DEFAULT_PREFIX.length(), command_id.length());
-
- bool found = std::any_of(this->command_loader.get_commands().begin(),
- this->command_loader.get_commands().end(),
- [&](const auto &command) {
- return command->get_name() == command_id;
- });
-
- if (!found) {
- return false;
- }
-
- this->command_id = command_id;
-
- parts.erase(parts.begin());
-
- if (parts.empty()) {
- return true;
- }
-
- std::string subcommand_id = parts[0];
- if (subcommand_id.empty()) {
- this->subcommand_id = std::nullopt;
- } else {
- this->subcommand_id = subcommand_id;
- }
- parts.erase(parts.begin());
-
- std::string message = utils::string::join_vector(parts, ' ');
-
- if (message.empty()) {
- this->message = std::nullopt;
- } else {
- this->message = message;
- }
-
- return true;
- }
- }
-}
diff --git a/src/commands/request.hpp b/src/commands/request.hpp
index 71f2f04..9742cbc 100644
--- a/src/commands/request.hpp
+++ b/src/commands/request.hpp
@@ -4,40 +4,12 @@
#include <string>
#include "../irc/message.hpp"
-#include "command.hpp"
-
-namespace bot {
- namespace command {
- class Request {
- public:
- Request(const command::CommandLoader &command_loader,
- const irc::Message<irc::MessageType::Privmsg> &irc_message)
- : irc_message(irc_message), command_loader(command_loader){};
- ~Request() = default;
-
- bool fill_request();
-
- const std::string &get_command_id() const { return this->command_id; };
- const std::optional<std::string> &get_subcommand_id() const {
- return this->subcommand_id;
- };
- const std::optional<std::string> &get_message() const {
- return this->message;
- };
-
- const irc::Message<irc::MessageType::Privmsg> &get_irc_message() const {
- return this->irc_message;
- };
-
- private:
- std::string command_id;
- std::optional<std::string> subcommand_id;
- std::optional<std::string> message;
-
- const irc::Message<irc::MessageType::Privmsg> &irc_message;
- const command::CommandLoader &command_loader;
- };
-
- }
+namespace bot::command {
+ struct Request {
+ std::string command_id;
+ std::optional<std::string> subcommand_id;
+ std::optional<std::string> message;
+ const irc::Message<irc::MessageType::Privmsg> &irc_message;
+ };
}
diff --git a/src/commands/request_util.cpp b/src/commands/request_util.cpp
new file mode 100644
index 0000000..af17623
--- /dev/null
+++ b/src/commands/request_util.cpp
@@ -0,0 +1,58 @@
+#include "request_util.hpp"
+
+#include <optional>
+
+#include "../constants.hpp"
+#include "../irc/message.hpp"
+#include "command.hpp"
+#include "request.hpp"
+
+namespace bot::command {
+ std::optional<Request> generate_request(
+ const command::CommandLoader &command_loader,
+ const irc::Message<irc::MessageType::Privmsg> &irc_message) {
+ std::vector<std::string> parts =
+ utils::string::split_text(irc_message.message, ' ');
+
+ std::string command_id = parts[0];
+
+ if (command_id.substr(0, DEFAULT_PREFIX.length()) != DEFAULT_PREFIX) {
+ return std::nullopt;
+ }
+
+ command_id =
+ command_id.substr(DEFAULT_PREFIX.length(), command_id.length());
+
+ bool found = std::any_of(
+ command_loader.get_commands().begin(),
+ command_loader.get_commands().end(),
+ [&](const auto &command) { return command->get_name() == command_id; });
+
+ if (!found) {
+ return std::nullopt;
+ }
+
+ parts.erase(parts.begin());
+
+ if (parts.empty()) {
+ Request req{command_id, std::nullopt, std::nullopt, irc_message};
+
+ return req;
+ }
+
+ std::optional<std::string> subcommand_id = parts[0];
+ if (subcommand_id->empty()) {
+ subcommand_id = std::nullopt;
+ }
+ parts.erase(parts.begin());
+
+ std::optional<std::string> message = utils::string::join_vector(parts, ' ');
+
+ if (message->empty()) {
+ message = std::nullopt;
+ }
+
+ Request req{command_id, subcommand_id, message, irc_message};
+ return req;
+ }
+}
diff --git a/src/commands/request_util.hpp b/src/commands/request_util.hpp
new file mode 100644
index 0000000..df61ba5
--- /dev/null
+++ b/src/commands/request_util.hpp
@@ -0,0 +1,11 @@
+#include <optional>
+
+#include "../irc/message.hpp"
+#include "command.hpp"
+#include "request.hpp"
+
+namespace bot::command {
+ std::optional<Request> generate_request(
+ const command::CommandLoader &command_loader,
+ const irc::Message<irc::MessageType::Privmsg> &irc_message);
+}
diff --git a/src/modules/ping.hpp b/src/modules/ping.hpp
index 033f93a..724beab 100644
--- a/src/modules/ping.hpp
+++ b/src/modules/ping.hpp
@@ -14,7 +14,7 @@ namespace bot {
std::variant<std::vector<std::string>, std::string> run(
const InstanceBundle &bundle,
- const irc::Message<irc::MessageType::Privmsg> &msg) const override {
+ const command::Request &request) const override {
return "pong";
}
};