1
0
Fork 0
mirror of https://gitlab.com/niansa/asbots.git synced 2025-03-06 20:48:25 +01:00
asbots/services/nickserv.cpp

60 lines
2.3 KiB
C++

#include "nickserv.hpp"
#include "../instance.hpp"
#include <async/result.hpp>
#include <string>
#include <string_view>
#include <vector>
#include <memory>
async::result<void> NickServ::intitialize() {
co_await i->netInfo.wait_ready();
user = {
.server = i->config.server.uid,
.nick = std::string(getConfig("NickServ", "nickname").value_or("NickServ")),
.realhost = i->config.server.name,
.uid = uuid,
.realname = i->netInfo.name
};
co_await mark_ready(user);
using namespace sqlite_orm;
storage = std::make_unique<Storage>(make_storage(
std::string(getConfig("NickServ", "database").value_or("nickserv.sqlite")),
make_table("users",
make_column("id", &Account::id, autoincrement(), primary_key()),
make_column("name", &Account::name),
make_column("password_hash", &Account::password_hash))
));
storage->sync_schema();
commands = {
{"test", {[this] (u_User& sender, std::vector<std::string_view> args) -> async::result<void> {
co_await i->send_event(user.get_notice("Hello world! Works", sender->uid));
}, "", 0}
},
{"identify", {[this] (u_User& sender, const std::vector<std::string_view>& args) -> async::result<void> {
// Get user from storage
auto accounts_found = storage->get_all<Account>(where(c(&Account::name) == sender->nick));
if (accounts_found.empty()) {
co_await i->send_event(user.get_notice("Error: Your nick is not registered!", sender->uid));
co_return;
}
auto& account = accounts_found[0];
// Check password
if (account.password_hash != args[0]) {
co_await i->send_event(user.get_notice("Error: Invalid password!", sender->uid));
co_return;
}
// Login
co_await i->send_event(sender->get_encap_su(account.name));
}, "<password>", 1}
}
};
}
async::result<void> NickServ::on_event(const Event& event) {
co_return;
}