diff options
author | Florent Le Coz <louiz@louiz.org> | 2013-11-03 00:22:32 +0100 |
---|---|---|
committer | Florent Le Coz <louiz@louiz.org> | 2013-11-03 00:22:32 +0100 |
commit | 87aaacdb420341bf3619922332d58b95249971bc (patch) | |
tree | 8f819241c8e78b23f5e2036c22ac637092e4293c /src/irc | |
parent | e332d7a2c0900aec488ab508c3e9e389d2a71e32 (diff) | |
download | biboumi-87aaacdb420341bf3619922332d58b95249971bc.tar.gz biboumi-87aaacdb420341bf3619922332d58b95249971bc.tar.bz2 biboumi-87aaacdb420341bf3619922332d58b95249971bc.tar.xz biboumi-87aaacdb420341bf3619922332d58b95249971bc.zip |
Rename libirc and libxmpp to irc and xmpp
Diffstat (limited to 'src/irc')
-rw-r--r-- | src/irc/irc_client.cpp | 72 | ||||
-rw-r--r-- | src/irc/irc_client.hpp | 60 | ||||
-rw-r--r-- | src/irc/irc_message.cpp | 65 | ||||
-rw-r--r-- | src/irc/irc_message.hpp | 28 |
4 files changed, 225 insertions, 0 deletions
diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp new file mode 100644 index 0000000..83d1b0d --- /dev/null +++ b/src/irc/irc_client.cpp @@ -0,0 +1,72 @@ +#include <irc/irc_client.hpp> +#include <irc/irc_message.hpp> + +#include <iostream> +#include <stdexcept> + +IrcClient::IrcClient() +{ + std::cout << "IrcClient()" << std::endl; +} + +IrcClient::~IrcClient() +{ + std::cout << "~IrcClient()" << std::endl; +} + +void IrcClient::on_connected() +{ +} + +void IrcClient::on_connection_close() +{ + std::cout << "Connection closed by remote server." << std::endl; + this->close(); +} + +void IrcClient::parse_in_buffer() +{ + while (true) + { + auto pos = this->in_buf.find("\r\n"); + if (pos == std::string::npos) + break ; + IrcMessage message(this->in_buf.substr(0, pos)); + this->in_buf = this->in_buf.substr(pos + 2, std::string::npos); + std::cout << message << std::endl; + } +} + +void IrcClient::send_message(IrcMessage&& message) +{ + std::string res; + if (!message.prefix.empty()) + res += ":" + std::move(message.prefix) + " "; + res += std::move(message.command); + for (const std::string& arg: message.arguments) + { + if (arg.find(" ") != std::string::npos) + { + res += " :" + arg; + break; + } + res += " " + arg; + } + res += "\r\n"; + this->send_data(std::move(res)); +} + +void IrcClient::send_user_command(const std::string& username, const std::string& realname) +{ + this->send_message(IrcMessage("USER", {username, "NONE", "NONE", realname})); +} + +void IrcClient::send_nick_command(const std::string& nick) +{ + this->send_message(IrcMessage("NICK", {nick})); +} + +void IrcClient::send_join_command(const std::string& chan_name) +{ + this->send_message(IrcMessage("JOIN", {chan_name})); +} diff --git a/src/irc/irc_client.hpp b/src/irc/irc_client.hpp new file mode 100644 index 0000000..e380f5b --- /dev/null +++ b/src/irc/irc_client.hpp @@ -0,0 +1,60 @@ +#ifndef IRC_CLIENT_INCLUDED +# define IRC_CLIENT_INCLUDED + +#include <irc/irc_message.hpp> + +#include <network/socket_handler.hpp> + +#include <string> + +/** + * Represent one IRC client, i.e. an endpoint connected to a single IRC + * server, through a TCP socket, receiving and sending commands to it. + * + * TODO: TLS support, maybe, but that's not high priority + */ +class IrcClient: public SocketHandler +{ +public: + explicit IrcClient(); + ~IrcClient(); + /** + * Called when successfully connected to the server + */ + void on_connected(); + /** + * Close the connection, remove us from the poller + */ + void on_connection_close(); + /** + * Parse the data we have received so far and try to get one or more + * complete messages from it. + */ + void parse_in_buffer(); + /** + * Serialize the given message into a line, and send that into the socket + * (actually, into our out_buf and signal the poller that we want to wach + * for send events to be ready) + */ + void send_message(IrcMessage&& message); + /** + * Send the USER irc command + */ + void send_user_command(const std::string& username, const std::string& realname); + /** + * Send the NICK irc command + */ + void send_nick_command(const std::string& username); + /** + * Send the JOIN irc command + */ + void send_join_command(const std::string& chan_name); + +private: + IrcClient(const IrcClient&) = delete; + IrcClient(IrcClient&&) = delete; + IrcClient& operator=(const IrcClient&) = delete; + IrcClient& operator=(IrcClient&&) = delete; +}; + +#endif // IRC_CLIENT_INCLUDED diff --git a/src/irc/irc_message.cpp b/src/irc/irc_message.cpp new file mode 100644 index 0000000..18fa3ec --- /dev/null +++ b/src/irc/irc_message.cpp @@ -0,0 +1,65 @@ +#include <irc/irc_message.hpp> +#include <iostream> + +IrcMessage::IrcMessage(std::string&& line) +{ + std::string::size_type pos; + + // optional prefix + if (line[0] == ':') + { + pos = line.find(" "); + this->prefix = line.substr(1, pos); + line = line.substr(pos + 1, std::string::npos); + } + // command + pos = line.find(" "); + this->command = line.substr(0, pos); + line = line.substr(pos + 1, std::string::npos); + // arguments + do + { + if (line[0] == ':') + { + this->arguments.emplace_back(line.substr(1, std::string::npos)); + break ; + } + pos = line.find(" "); + this->arguments.emplace_back(line.substr(0, pos)); + line = line.substr(pos + 1, std::string::npos); + } while (pos != std::string::npos); +} + +IrcMessage::IrcMessage(std::string&& prefix, + std::string&& command, + std::vector<std::string>&& args): + prefix(std::move(prefix)), + command(std::move(command)), + arguments(std::move(args)) +{ +} + +IrcMessage::IrcMessage(std::string&& command, + std::vector<std::string>&& args): + prefix(), + command(std::move(command)), + arguments(std::move(args)) +{ +} + +IrcMessage::~IrcMessage() +{ +} + +std::ostream& operator<<(std::ostream& os, const IrcMessage& message) +{ + os << "IrcMessage"; + os << "[" << message.command << "]"; + for (const std::string& arg: message.arguments) + { + os << "{" << arg << "}"; + } + if (!message.prefix.empty()) + os << "(from: " << message.prefix << ")"; + return os; +} diff --git a/src/irc/irc_message.hpp b/src/irc/irc_message.hpp new file mode 100644 index 0000000..a0bd772 --- /dev/null +++ b/src/irc/irc_message.hpp @@ -0,0 +1,28 @@ +#ifndef IRC_MESSAGE_INCLUDED +# define IRC_MESSAGE_INCLUDED + +#include <vector> +#include <string> +#include <ostream> + +class IrcMessage +{ +public: + explicit IrcMessage(std::string&& line); + explicit IrcMessage(std::string&& prefix, std::string&& command, std::vector<std::string>&& args); + explicit IrcMessage(std::string&& command, std::vector<std::string>&& args); + ~IrcMessage(); + + std::string prefix; + std::string command; + std::vector<std::string> arguments; + + IrcMessage(const IrcMessage&) = delete; + IrcMessage(IrcMessage&&) = delete; + IrcMessage& operator=(const IrcMessage&) = delete; + IrcMessage& operator=(IrcMessage&&) = delete; +}; + +std::ostream& operator<<(std::ostream& os, const IrcMessage& message); + +#endif // IRC_MESSAGE_INCLUDED |