mirror of
https://gitlab.com/niansa/asbots.git
synced 2025-03-06 20:48:25 +01:00
Added InsufficientArgsError exception
This commit is contained in:
parent
315b65f0c3
commit
29de614df2
4 changed files with 24 additions and 25 deletions
|
@ -1,5 +1,7 @@
|
||||||
#ifndef __EXCEPTIONS_HPP
|
#ifndef __EXCEPTIONS_HPP
|
||||||
#define __EXCEPTIONS_HPP
|
#define __EXCEPTIONS_HPP
|
||||||
|
#include <fmt/format.h>
|
||||||
|
#include <string_view>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,8 +16,12 @@ struct DesyncError : public std::exception {
|
||||||
const char *what() const throw() {
|
const char *what() const throw() {
|
||||||
return "Server has desynced!!!";
|
return "Server has desynced!!!";
|
||||||
}
|
}
|
||||||
DesyncError() {
|
};
|
||||||
std::cout << "DESCONACCREKO" << std::endl;
|
|
||||||
}
|
struct InsufficientArgsError : public ParseError {
|
||||||
|
using ParseError::ParseError;
|
||||||
|
|
||||||
|
InsufficientArgsError(std::string_view where, size_t expected, size_t given)
|
||||||
|
: ParseError::ParseError(fmt::format("In {} event: Insufficient arguments expected. Given {} but expected {}", where, given, expected)) {}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
29
instance.cpp
29
instance.cpp
|
@ -20,9 +20,7 @@ using namespace Utility;
|
||||||
void Event::parse(std::string_view str) {
|
void Event::parse(std::string_view str) {
|
||||||
auto split = strSplit(str, ' ', 2);
|
auto split = strSplit(str, ' ', 2);
|
||||||
// Check split size
|
// Check split size
|
||||||
if (split.size() < 2) {
|
argsSizeCheck("basic", split, 2);
|
||||||
throw ParseError("In event parser: Events are expected to have both a sender and name");
|
|
||||||
}
|
|
||||||
// Move values
|
// Move values
|
||||||
split[0] = {split[0].data()+1, split[0].size()-1}; // Erase leading ':'
|
split[0] = {split[0].data()+1, split[0].size()-1}; // Erase leading ':'
|
||||||
auto id_len = split[0].size();
|
auto id_len = split[0].size();
|
||||||
|
@ -113,9 +111,7 @@ void ModeSet::parse(std::string_view in, NetworkInfo &netInfo) {
|
||||||
void User::parse_euid(const Event& event, NetworkInfo& netInfo) {
|
void User::parse_euid(const Event& event, NetworkInfo& netInfo) {
|
||||||
this->server = std::get<SUID>(event.sender.id);
|
this->server = std::get<SUID>(event.sender.id);
|
||||||
// Check size
|
// Check size
|
||||||
if (event.args.size() < 10) {
|
argsSizeCheck("EUID", event.args, 10);
|
||||||
throw ParseError("In euid parser: This euid event does not have enough arguments");
|
|
||||||
}
|
|
||||||
// Move values
|
// Move values
|
||||||
nick = event.args[0];
|
nick = event.args[0];
|
||||||
hops = std::stoull(std::string(event.args[1]));
|
hops = std::stoull(std::string(event.args[1]));
|
||||||
|
@ -140,9 +136,7 @@ void User::removeChannel(const u_Channel& channel) {
|
||||||
void Channel::parse_sjoin(const Event& event, Cache& cache, NetworkInfo& netInfo) {
|
void Channel::parse_sjoin(const Event& event, Cache& cache, NetworkInfo& netInfo) {
|
||||||
this->server = std::get<SUID>(event.sender.id);
|
this->server = std::get<SUID>(event.sender.id);
|
||||||
// Check size
|
// Check size
|
||||||
if (event.args.size() < 3) {
|
argsSizeCheck("SJOIN", event.args, 3);
|
||||||
throw ParseError("In euid parser: This euid event does not have enough arguments");
|
|
||||||
}
|
|
||||||
// Move values
|
// Move values
|
||||||
name = std::move(event.args[1]);
|
name = std::move(event.args[1]);
|
||||||
mode.parse<true>(event.args[2], netInfo);
|
mode.parse<true>(event.args[2], netInfo);
|
||||||
|
@ -401,15 +395,11 @@ async::result<void> Instance::process(Event event) {
|
||||||
res->get()->umode.parse<false>(event.text, netInfo);
|
res->get()->umode.parse<false>(event.text, netInfo);
|
||||||
} else if (event.name == "ENCAP") {
|
} else if (event.name == "ENCAP") {
|
||||||
// Get args
|
// Get args
|
||||||
if (event.args.size() < 3) {
|
argsSizeCheck("ENCAP", event.args, 3);
|
||||||
throw ParseError("In encap event parser: at least * and encap name need to be passed");
|
|
||||||
}
|
|
||||||
if (event.args[1] == "SU") {
|
if (event.args[1] == "SU") {
|
||||||
// User logged in
|
// User logged in
|
||||||
// Check args
|
// Check args
|
||||||
if (event.args.size() < 5) {
|
argsSizeCheck("ENCAP (SU)", event.args, 5);
|
||||||
throw ParseError("In encap su event parser: this encap requires UID as first argument and optionally login name as second one");
|
|
||||||
}
|
|
||||||
// Find user in cache
|
// Find user in cache
|
||||||
auto res = cache.find_user_by_uid(event.args[2]);
|
auto res = cache.find_user_by_uid(event.args[2]);
|
||||||
if (res == cache.users.end()) {
|
if (res == cache.users.end()) {
|
||||||
|
@ -447,10 +437,7 @@ async::result<void> Instance::process(Event event) {
|
||||||
res->get()->topic = event.text;
|
res->get()->topic = event.text;
|
||||||
} else if (event.name == "JOIN") {
|
} else if (event.name == "JOIN") {
|
||||||
// User joined existing channel
|
// User joined existing channel
|
||||||
// Split args
|
argsSizeCheck("JOIN", event.args, 3);
|
||||||
if (event.args.size() < 3) {
|
|
||||||
throw ParseError("In join event parser: join even did not receive enough arguments (expected 3)");
|
|
||||||
}
|
|
||||||
// Get channel from cache
|
// Get channel from cache
|
||||||
auto c_res = cache.find_channel_by_name(event.args[1]);
|
auto c_res = cache.find_channel_by_name(event.args[1]);
|
||||||
if (c_res == cache.channels.end()) {
|
if (c_res == cache.channels.end()) {
|
||||||
|
@ -483,12 +470,10 @@ async::result<void> Instance::process(Event event) {
|
||||||
u_res->get()->removeChannel(*c_res);
|
u_res->get()->removeChannel(*c_res);
|
||||||
} else if (event.name == "TMODE" || event.name == "BMASK") {
|
} else if (event.name == "TMODE" || event.name == "BMASK") {
|
||||||
// Channel modes changed
|
// Channel modes changed
|
||||||
|
argsSizeCheck(event.name, event.args, 3);
|
||||||
// Split args
|
// Split args
|
||||||
std::string_view modes, channelName;
|
std::string_view modes, channelName;
|
||||||
{
|
{
|
||||||
if (event.args.size() < 3) {
|
|
||||||
throw ParseError("In MODE/BMASK event parser: did not receive enough arguments (expected 3)");
|
|
||||||
}
|
|
||||||
channelName = event.args[1];
|
channelName = event.args[1];
|
||||||
modes = event.args[2];
|
modes = event.args[2];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "utility.hpp"
|
#include "utility.hpp"
|
||||||
#include "uid.hpp"
|
#include "uid.hpp"
|
||||||
#include "instance.hpp"
|
#include "instance.hpp"
|
||||||
|
#include "exceptions.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
@ -31,6 +32,12 @@ std::tuple<std::string_view, std::string_view> colonSplit(std::string_view s) {
|
||||||
// Split there
|
// Split there
|
||||||
return {s.substr(0, colonPos), s.substr(colonPos+2, s.size()-1)};
|
return {s.substr(0, colonPos), s.substr(colonPos+2, s.size()-1)};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void argsSizeCheck(std::string_view where, std::vector<std::string_view> args, size_t expected) {
|
||||||
|
if (args.size() < expected) {
|
||||||
|
throw InsufficientArgsError(where, expected, args.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char UUIDChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
static const char UUIDChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||||
|
|
|
@ -12,5 +12,6 @@ constexpr size_t strSplitInf = 0;
|
||||||
|
|
||||||
std::vector<std::string_view> strSplit(std::string_view s, char delimiter, size_t times = strSplitInf);
|
std::vector<std::string_view> strSplit(std::string_view s, char delimiter, size_t times = strSplitInf);
|
||||||
std::tuple<std::string_view, std::string_view> colonSplit(std::string_view s);
|
std::tuple<std::string_view, std::string_view> colonSplit(std::string_view s);
|
||||||
|
void argsSizeCheck(std::string_view where, std::vector<std::string_view> args, size_t expected);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue