1
0
Fork 0
mirror of https://gitlab.com/niansa/asbots.git synced 2025-03-06 20:48:25 +01:00
asbots/instance.hpp
2021-06-18 15:25:14 +02:00

96 lines
2.2 KiB
C++

#ifndef _INSTANCE_HPP
#define _INSTANCE_HPP
#include "config.hpp"
#include <uvpp.hpp>
#include <async/result.hpp>
#include <fmt/format.h>
#include <string>
#include <string_view>
#include <unordered_map>
#include <stdexcept>
#include <ctime>
struct ParseError : public std::runtime_error {
using std::runtime_error::runtime_error;
};
struct ConnectionError : public std::runtime_error {
using std::runtime_error::runtime_error;
};
struct Event {
AnyUID sender;
std::string name, args, text;
std::string dump() const {
return fmt::format(":{} {} {}{}\n", sender.str(), name, args, (text.empty()?"":":"+text));
}
void parse(std::string_view str);
};
struct Command {
std::string name, args, text;
std::string dump() const {
return fmt::format("{} {}{}\n", name, args, (text.empty()?"":":"+text));
}
void parse(std::string_view str);
};
struct User {
SUID server;
// ... == "EUID"
std::string nick;
size_t hops;
time_t ts;
std::string umode;
std::string ident;
std::string host;
std::string realhost;
UUID id;
std::string realname;
User() {
ts = time(nullptr);
}
Event dump() const {
return Event{
.sender = SUID(server),
.name = "EUID",
.args = fmt::format("{} 1 {} {} {} {} 0 {} * * :{}", nick, ts, umode, ident, host, id.str(), realname)
};
}
void parse(Event event);
};
class Instance {
uvpp::loop_service &s;
const Config& config;
uvpp::Addr addr;
uvpp::tcp *socket = nullptr;
Config::Server connected_server;
bool server_bursting = true, client_bursting = true;
bool authed = false;
std::unordered_map<std::string_view, User> users;
public:
Instance(uvpp::loop_service &s, const Config& config) : s(s), config(config) {
addr = uvpp::make_ipv4(config.connection.addr, config.connection.port);
}
~Instance() {
if (socket) delete socket;
}
async::result<void> run();
async::result<void> login();
async::result<void> burst();
async::result<void> process(const Command);
async::result<void> process(const Event);
async::result<void> send_ping();
};
#endif