xbot/ircmsg.hpp

75 lines
1.5 KiB
C++
Raw Normal View History

2023-11-22 19:59:34 -08:00
#pragma once
#include <iostream>
#include <string_view>
#include <vector>
struct irctag
{
std::string_view key;
std::string_view val;
2025-01-25 15:45:31 -08:00
irctag(std::string_view key, std::string_view val)
: key{key}
, val{val}
{
}
2023-11-22 19:59:34 -08:00
2025-01-25 15:45:31 -08:00
friend auto operator==(const irctag &, const irctag &) -> bool = default;
2023-11-22 19:59:34 -08:00
};
2023-11-25 09:22:55 -08:00
struct IrcMsg
2023-11-22 19:59:34 -08:00
{
std::vector<irctag> tags;
std::vector<std::string_view> args;
std::string_view source;
std::string_view command;
2023-11-25 09:22:55 -08:00
IrcMsg() = default;
2023-11-22 19:59:34 -08:00
2023-11-25 09:22:55 -08:00
IrcMsg(
2025-01-25 15:45:31 -08:00
std::vector<irctag> &&tags,
2023-11-22 19:59:34 -08:00
std::string_view source,
std::string_view command,
2025-01-25 15:45:31 -08:00
std::vector<std::string_view> &&args
)
: tags(std::move(tags))
, args(std::move(args))
, source{source}
, command{command}
{
}
2023-11-22 19:59:34 -08:00
2025-01-25 15:45:31 -08:00
bool hassource() const;
2023-11-22 19:59:34 -08:00
2025-01-25 15:45:31 -08:00
friend bool operator==(const IrcMsg &, const IrcMsg &) = default;
2023-11-22 19:59:34 -08:00
};
2025-01-25 15:45:31 -08:00
enum class irc_error_code
{
2023-11-22 19:59:34 -08:00
MISSING_TAG,
MISSING_COMMAND,
};
2025-01-25 15:45:31 -08:00
auto operator<<(std::ostream &out, irc_error_code) -> std::ostream &;
2023-11-22 19:59:34 -08:00
2025-01-25 15:45:31 -08:00
struct irc_parse_error : public std::exception
{
2023-11-22 19:59:34 -08:00
irc_error_code code;
2025-01-25 15:45:31 -08:00
irc_parse_error(irc_error_code code)
: code(code)
{
}
2023-11-22 19:59:34 -08:00
};
/**
* Parses the given IRC message into a structured format.
* The original message is mangled to store string fragments
* that are pointed to by the structured message type.
*
* Returns zero for success, non-zero for parse error.
*/
2025-01-25 15:45:31 -08:00
auto parse_irc_message(char *msg) -> IrcMsg;
2023-11-22 19:59:34 -08:00
2025-01-25 15:45:31 -08:00
auto parse_irc_tags(char *msg) -> std::vector<irctag>;