From 87aaacdb420341bf3619922332d58b95249971bc Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 3 Nov 2013 00:22:32 +0100 Subject: Rename libirc and libxmpp to irc and xmpp --- src/irc/irc_client.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/irc/irc_client.cpp (limited to 'src/irc/irc_client.cpp') 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 +#include + +#include +#include + +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})); +} -- cgit v1.2.3 From d834d6ed0647ba7e51e81f600fe259156e2b8070 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 3 Nov 2013 17:54:07 +0100 Subject: Exit the poller when it handles no connection at all --- src/irc/irc_client.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 83d1b0d..80f36ee 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -21,7 +21,6 @@ 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() -- cgit v1.2.3 From bf7b05ef72bbdac97704d262ddfe418908267535 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 6 Nov 2013 20:51:05 +0100 Subject: Implement the Bridge class to translate between the two protocols MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add all useful classes as well: Jid, Iid, IrcChannel, IrcUser etc to properly keep the informations about what we receive from the IRC server. Only handle the MUC join stanza, and send the list of users in the IRC channel to the XMPP user, and the IRC channel’s topic, for now. --- src/irc/irc_client.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 3 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 80f36ee..7875b1c 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -1,10 +1,18 @@ -#include #include +#include +#include +#include + +#include +#include #include #include -IrcClient::IrcClient() +IrcClient::IrcClient(const std::string& hostname, const std::string& username, Bridge* bridge): + hostname(hostname), + username(username), + bridge(bridge) { std::cout << "IrcClient()" << std::endl; } @@ -14,8 +22,15 @@ IrcClient::~IrcClient() std::cout << "~IrcClient()" << std::endl; } +void IrcClient::start() +{ + this->connect(this->hostname, "6667"); +} + void IrcClient::on_connected() { + this->send_nick_command(this->username); + this->send_user_command(this->username, this->username); } void IrcClient::on_connection_close() @@ -23,6 +38,19 @@ void IrcClient::on_connection_close() std::cout << "Connection closed by remote server." << std::endl; } +IrcChannel* IrcClient::get_channel(const std::string& name) +{ + try + { + return this->channels.at(name).get(); + } + catch (const std::out_of_range& exception) + { + this->channels.emplace(name, std::make_unique()); + return this->channels.at(name).get(); + } +} + void IrcClient::parse_in_buffer() { while (true) @@ -33,6 +61,21 @@ void IrcClient::parse_in_buffer() 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; + // TODO map function and command name properly + if (message.command == "PING") + this->send_pong_command(); + else if (message.command == "NOTICE" || + message.command == "375" || + message.command == "372") + this->forward_server_message(message); + else if (message.command == "JOIN") + this->on_self_channel_join(message); + else if (message.command == "353") + this->set_and_forward_user_list(message); + else if (message.command == "332") + this->on_topic_received(message); + else if (message.command == "366") + this->on_channel_completely_joined(message); } } @@ -52,6 +95,8 @@ void IrcClient::send_message(IrcMessage&& message) res += " " + arg; } res += "\r\n"; + std::cout << "=== IRC SENDING ===" << std::endl; + std::cout << res << std::endl; this->send_data(std::move(res)); } @@ -67,5 +112,59 @@ void IrcClient::send_nick_command(const std::string& nick) void IrcClient::send_join_command(const std::string& chan_name) { - this->send_message(IrcMessage("JOIN", {chan_name})); + IrcChannel* channel = this->get_channel(chan_name); + if (channel->joined == false) + this->send_message(IrcMessage("JOIN", {chan_name})); +} + +void IrcClient::send_pong_command() +{ + this->send_message(IrcMessage("PONG", {})); +} + +void IrcClient::forward_server_message(const IrcMessage& message) +{ + const std::string from = message.prefix; + const std::string body = message.arguments[1]; + + this->bridge->send_xmpp_message(this->hostname, from, body); +} + +void IrcClient::set_and_forward_user_list(const IrcMessage& message) +{ + const std::string chan_name = message.arguments[2]; + IrcChannel* channel = this->get_channel(chan_name); + std::vector nicks = utils::split(message.arguments[3], ' '); + for (const std::string& nick: nicks) + { + IrcUser* user = channel->add_user(nick); + if (user->nick != channel->get_self()->nick) + { + std::cout << "Adding user [" << nick << "] to chan " << chan_name << std::endl; + this->bridge->send_user_join(this->hostname, chan_name, user->nick); + } + } +} + +void IrcClient::on_self_channel_join(const IrcMessage& message) +{ + const std::string chan_name = message.arguments[0]; + IrcChannel* channel = this->get_channel(chan_name); + channel->joined = true; + channel->set_self(message.prefix); +} + +void IrcClient::on_topic_received(const IrcMessage& message) +{ + const std::string chan_name = message.arguments[1]; + IrcChannel* channel = this->get_channel(chan_name); + channel->topic = message.arguments[2]; +} + +void IrcClient::on_channel_completely_joined(const IrcMessage& message) +{ + const std::string chan_name = message.arguments[1]; + IrcChannel* channel = this->get_channel(chan_name); + this->bridge->send_self_join(this->hostname, chan_name, channel->get_self()->nick); + this->bridge->send_topic(this->hostname, chan_name, channel->topic); } -- cgit v1.2.3 From a418b6ed5d70f0e61e71bb1adce2a693ade89e30 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 7 Nov 2013 01:53:09 +0100 Subject: Send and receive messages Also correctly respond to PING with the id, escape some XML content, but not always --- src/irc/irc_client.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 5 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 7875b1c..cf57bd7 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -12,7 +12,9 @@ IrcClient::IrcClient(const std::string& hostname, const std::string& username, Bridge* bridge): hostname(hostname), username(username), - bridge(bridge) + current_nick(username), + bridge(bridge), + welcomed(false) { std::cout << "IrcClient()" << std::endl; } @@ -51,6 +53,11 @@ IrcChannel* IrcClient::get_channel(const std::string& name) } } +std::string IrcClient::get_own_nick() const +{ + return this->current_nick; +} + void IrcClient::parse_in_buffer() { while (true) @@ -63,19 +70,23 @@ void IrcClient::parse_in_buffer() std::cout << message << std::endl; // TODO map function and command name properly if (message.command == "PING") - this->send_pong_command(); + this->send_pong_command(message); else if (message.command == "NOTICE" || message.command == "375" || message.command == "372") this->forward_server_message(message); else if (message.command == "JOIN") this->on_self_channel_join(message); + else if (message.command == "PRIVMSG") + this->on_channel_message(message); else if (message.command == "353") this->set_and_forward_user_list(message); else if (message.command == "332") this->on_topic_received(message); else if (message.command == "366") this->on_channel_completely_joined(message); + else if (message.command == "001") + this->on_welcome_message(message); } } @@ -102,7 +113,7 @@ void IrcClient::send_message(IrcMessage&& message) void IrcClient::send_user_command(const std::string& username, const std::string& realname) { - this->send_message(IrcMessage("USER", {username, "NONE", "NONE", realname})); + this->send_message(IrcMessage("USER", {username, "ignored", "ignored", realname})); } void IrcClient::send_nick_command(const std::string& nick) @@ -112,14 +123,32 @@ void IrcClient::send_nick_command(const std::string& nick) void IrcClient::send_join_command(const std::string& chan_name) { + if (this->welcomed == false) + { + this->channels_to_join.push_back(chan_name); + return ; + } IrcChannel* channel = this->get_channel(chan_name); if (channel->joined == false) this->send_message(IrcMessage("JOIN", {chan_name})); } -void IrcClient::send_pong_command() +bool IrcClient::send_channel_message(const std::string& chan_name, const std::string& body) { - this->send_message(IrcMessage("PONG", {})); + IrcChannel* channel = this->get_channel(chan_name); + if (channel->joined == false) + { + std::cout << "Cannot send message to channel " << chan_name << ", it is not joined" << std::endl; + return false; + } + this->send_message(IrcMessage("PRIVMSG", {chan_name, body})); + return true; +} + +void IrcClient::send_pong_command(const IrcMessage& message) +{ + const std::string id = message.arguments[0]; + this->send_message(IrcMessage("PONG", {id})); } void IrcClient::forward_server_message(const IrcMessage& message) @@ -154,6 +183,17 @@ void IrcClient::on_self_channel_join(const IrcMessage& message) channel->set_self(message.prefix); } +void IrcClient::on_channel_message(const IrcMessage& message) +{ + const IrcUser user(message.prefix); + const std::string nick = user.nick; + Iid iid; + iid.chan = message.arguments[0]; + iid.server = this->hostname; + const std::string body = message.arguments[1]; + this->bridge->send_muc_message(iid, nick, body); +} + void IrcClient::on_topic_received(const IrcMessage& message) { const std::string chan_name = message.arguments[1]; @@ -168,3 +208,12 @@ void IrcClient::on_channel_completely_joined(const IrcMessage& message) this->bridge->send_self_join(this->hostname, chan_name, channel->get_self()->nick); this->bridge->send_topic(this->hostname, chan_name, channel->topic); } + +void IrcClient::on_welcome_message(const IrcMessage& message) +{ + this->current_nick = message.arguments[0]; + this->welcomed = true; + for (const std::string& chan_name: this->channels_to_join) + this->send_join_command(chan_name); + this->channels_to_join.clear(); +} -- cgit v1.2.3 From 7c671499350e22f8bfba2f72b9827aa5b200f7b0 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 9 Nov 2013 23:17:48 +0100 Subject: Implement part and join, both ways --- src/irc/irc_client.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 7 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index cf57bd7..e3d7653 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -76,7 +76,7 @@ void IrcClient::parse_in_buffer() message.command == "372") this->forward_server_message(message); else if (message.command == "JOIN") - this->on_self_channel_join(message); + this->on_channel_join(message); else if (message.command == "PRIVMSG") this->on_channel_message(message); else if (message.command == "353") @@ -87,6 +87,8 @@ void IrcClient::parse_in_buffer() this->on_channel_completely_joined(message); else if (message.command == "001") this->on_welcome_message(message); + else if (message.command == "PART") + this->on_part(message); } } @@ -128,9 +130,7 @@ void IrcClient::send_join_command(const std::string& chan_name) this->channels_to_join.push_back(chan_name); return ; } - IrcChannel* channel = this->get_channel(chan_name); - if (channel->joined == false) - this->send_message(IrcMessage("JOIN", {chan_name})); + this->send_message(IrcMessage("JOIN", {chan_name})); } bool IrcClient::send_channel_message(const std::string& chan_name, const std::string& body) @@ -145,6 +145,15 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st return true; } +void IrcClient::send_part_command(const std::string& chan_name, const std::string& status_message) +{ + IrcChannel* channel = this->get_channel(chan_name); + if (channel->joined == true) + { + this->send_message(IrcMessage("PART", {chan_name, status_message})); + } +} + void IrcClient::send_pong_command(const IrcMessage& message) { const std::string id = message.arguments[0]; @@ -175,12 +184,21 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) } } -void IrcClient::on_self_channel_join(const IrcMessage& message) +void IrcClient::on_channel_join(const IrcMessage& message) { const std::string chan_name = message.arguments[0]; IrcChannel* channel = this->get_channel(chan_name); - channel->joined = true; - channel->set_self(message.prefix); + const std::string nick = message.prefix; + if (channel->joined == false) + { + channel->joined = true; + channel->set_self(nick); + } + else + { + IrcUser* user = channel->add_user(nick); + this->bridge->send_user_join(this->hostname, chan_name, user->nick); + } } void IrcClient::on_channel_message(const IrcMessage& message) @@ -217,3 +235,25 @@ void IrcClient::on_welcome_message(const IrcMessage& message) this->send_join_command(chan_name); this->channels_to_join.clear(); } + +void IrcClient::on_part(const IrcMessage& message) +{ + const std::string chan_name = message.arguments[0]; + IrcChannel* channel = this->get_channel(chan_name); + std::string txt; + if (message.arguments.size() >= 2) + txt = message.arguments[1]; + const IrcUser* user = channel->find_user(message.prefix); + if (user) + { + std::string nick = user->nick; + channel->remove_user(user); + Iid iid; + iid.chan = chan_name; + iid.server = this->hostname; + bool self = channel->get_self()->nick == nick; + this->bridge->send_muc_leave(std::move(iid), std::move(nick), std::move(txt), self); + if (self) + channel->joined = false; + } +} -- cgit v1.2.3 From 0bb7ee0127a625ca8b6c25d9f593bfaa3d5af84b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 10 Nov 2013 05:08:50 +0100 Subject: Handle IRC QUIT command --- src/irc/irc_client.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e3d7653..8de7c8f 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -89,6 +89,8 @@ void IrcClient::parse_in_buffer() this->on_welcome_message(message); else if (message.command == "PART") this->on_part(message); + else if (message.command == "QUIT") + this->on_quit(message); } } @@ -257,3 +259,25 @@ void IrcClient::on_part(const IrcMessage& message) channel->joined = false; } } + +void IrcClient::on_quit(const IrcMessage& message) +{ + std::string txt; + if (message.arguments.size() >= 1) + txt = message.arguments[0]; + for (auto it = this->channels.begin(); it != this->channels.end(); ++it) + { + const std::string chan_name = it->first; + IrcChannel* channel = it->second.get(); + const IrcUser* user = channel->find_user(message.prefix); + if (user) + { + std::string nick = user->nick; + channel->remove_user(user); + Iid iid; + iid.chan = chan_name; + iid.server = this->hostname; + this->bridge->send_muc_leave(std::move(iid), std::move(nick), std::move(txt), false); + } + } +} -- cgit v1.2.3 From 10d528717723a72dd3240c634980a461cf9fa2df Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 10 Nov 2013 06:33:04 +0100 Subject: Handle private messages, both ways --- src/irc/irc_client.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 8de7c8f..71b0fe2 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -147,6 +147,11 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st return true; } +void IrcClient::send_private_message(const std::string& username, const std::string& body) +{ + this->send_message(IrcMessage("PRIVMSG", {username, body})); +} + void IrcClient::send_part_command(const std::string& chan_name, const std::string& status_message) { IrcChannel* channel = this->get_channel(chan_name); @@ -211,7 +216,13 @@ void IrcClient::on_channel_message(const IrcMessage& message) iid.chan = message.arguments[0]; iid.server = this->hostname; const std::string body = message.arguments[1]; - this->bridge->send_muc_message(iid, nick, body); + bool muc = true; + if (!this->get_channel(iid.chan)->joined) + { + iid.chan = nick; + muc = false; + } + this->bridge->send_message(iid, nick, body, muc); } void IrcClient::on_topic_received(const IrcMessage& message) -- cgit v1.2.3 From 096a4e3bafe6e2d238e4592f57f22f19f363fcbd Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 11 Nov 2013 00:24:34 +0100 Subject: Handle nick changes, both ways --- src/irc/irc_client.cpp | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 71b0fe2..30b1204 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -53,6 +53,12 @@ IrcChannel* IrcClient::get_channel(const std::string& name) } } +bool IrcClient::is_channel_joined(const std::string& name) +{ + IrcChannel* client = this->get_channel(name); + return client->joined; +} + std::string IrcClient::get_own_nick() const { return this->current_nick; @@ -91,6 +97,8 @@ void IrcClient::parse_in_buffer() this->on_part(message); else if (message.command == "QUIT") this->on_quit(message); + else if (message.command == "NICK") + this->on_nick(message); } } @@ -128,11 +136,9 @@ void IrcClient::send_nick_command(const std::string& nick) void IrcClient::send_join_command(const std::string& chan_name) { if (this->welcomed == false) - { - this->channels_to_join.push_back(chan_name); - return ; - } - this->send_message(IrcMessage("JOIN", {chan_name})); + this->channels_to_join.push_back(chan_name); + else + this->send_message(IrcMessage("JOIN", {chan_name})); } bool IrcClient::send_channel_message(const std::string& chan_name, const std::string& body) @@ -288,7 +294,34 @@ void IrcClient::on_quit(const IrcMessage& message) Iid iid; iid.chan = chan_name; iid.server = this->hostname; - this->bridge->send_muc_leave(std::move(iid), std::move(nick), std::move(txt), false); + this->bridge->send_muc_leave(std::move(iid), std::move(nick), txt, false); } } } + +void IrcClient::on_nick(const IrcMessage& message) +{ + const std::string new_nick = message.arguments[0]; + for (auto it = this->channels.begin(); it != this->channels.end(); ++it) + { + const std::string chan_name = it->first; + IrcChannel* channel = it->second.get(); + IrcUser* user = channel->find_user(message.prefix); + if (user) + { + std::string old_nick = user->nick; + Iid iid; + iid.chan = chan_name; + iid.server = this->hostname; + bool self = channel->get_self()->nick == old_nick; + this->bridge->send_nick_change(std::move(iid), old_nick, new_nick, self); + user->nick = new_nick; + if (self) + { + channel->get_self()->nick = new_nick; + this->current_nick = new_nick; + } + } + } +} + -- cgit v1.2.3 From 5817a95b5ee89480788832be35679dfcd2ed833b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 12 Nov 2013 23:43:43 +0100 Subject: Basic handling of modes, both ways --- src/irc/irc_client.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 30b1204..0d8c614 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -162,9 +162,15 @@ void IrcClient::send_part_command(const std::string& chan_name, const std::strin { IrcChannel* channel = this->get_channel(chan_name); if (channel->joined == true) - { - this->send_message(IrcMessage("PART", {chan_name, status_message})); - } + this->send_message(IrcMessage("PART", {chan_name, status_message})); +} + +void IrcClient::send_mode_command(const std::string& chan_name, const std::vector& arguments) +{ + std::vector args(arguments); + args.insert(args.begin(), chan_name); + IrcMessage m("MODE", std::move(args)); + this->send_message(std::move(m)); } void IrcClient::send_pong_command(const IrcMessage& message) @@ -325,3 +331,34 @@ void IrcClient::on_nick(const IrcMessage& message) } } +void IrcClient::on_mode(const IrcMessage& message) +{ + const std::string target = message.arguments[0]; + if (target[0] == '&' || target[0] == '#' || + target[0] == '!' || target[0] == '+') + this->on_channel_mode(message); + else + this->on_user_mode(message); +} + +void IrcClient::on_channel_mode(const IrcMessage& message) +{ + // For now, just transmit the modes so the user can know what happens + // TODO, actually interprete the mode. + Iid iid; + iid.chan = message.arguments[0]; + iid.server = this->hostname; + IrcUser user(message.prefix); + this->bridge->send_message(iid, "", std::string("Mode ") + iid.chan + + " [" + message.arguments[1] + + (message.arguments.size() > 2 ? (" " + message.arguments[2]): "") + + "] by " + user.nick, + true); +} + +void IrcClient::on_user_mode(const IrcMessage& message) +{ + this->bridge->send_xmpp_message(this->hostname, "", + std::string("User mode for ") + message.arguments[0] + + " is [" + message.arguments[1] + "]"); +} -- cgit v1.2.3 From 3cfaab9a2debe03829b1ff26fe94e775e1d18e0a Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 12 Nov 2013 23:47:00 +0100 Subject: Map irc commands to callbacks, in a clean way --- src/irc/irc_client.cpp | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0d8c614..1d2e487 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -74,31 +74,11 @@ void IrcClient::parse_in_buffer() 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; - // TODO map function and command name properly - if (message.command == "PING") - this->send_pong_command(message); - else if (message.command == "NOTICE" || - message.command == "375" || - message.command == "372") - this->forward_server_message(message); - else if (message.command == "JOIN") - this->on_channel_join(message); - else if (message.command == "PRIVMSG") - this->on_channel_message(message); - else if (message.command == "353") - this->set_and_forward_user_list(message); - else if (message.command == "332") - this->on_topic_received(message); - else if (message.command == "366") - this->on_channel_completely_joined(message); - else if (message.command == "001") - this->on_welcome_message(message); - else if (message.command == "PART") - this->on_part(message); - else if (message.command == "QUIT") - this->on_quit(message); - else if (message.command == "NICK") - this->on_nick(message); + auto cb = irc_callbacks.find(message.command); + if (cb != irc_callbacks.end()) + (this->*(cb->second))(message); + else + std::cout << "No handler for command " << message.command << std::endl; } } -- cgit v1.2.3 From 0859801230f999889d0f7356864888e8c5936cda Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 13 Nov 2013 01:24:36 +0100 Subject: Handle KICK in irc channel, both ways --- src/irc/irc_client.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 1d2e487..82abbd9 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -113,6 +113,11 @@ void IrcClient::send_nick_command(const std::string& nick) this->send_message(IrcMessage("NICK", {nick})); } +void IrcClient::send_kick_command(const std::string& chan_name, const std::string& target, const std::string& reason) +{ + this->send_message(IrcMessage("KICK", {chan_name, target, reason})); +} + void IrcClient::send_join_command(const std::string& chan_name) { if (this->welcomed == false) @@ -311,6 +316,21 @@ void IrcClient::on_nick(const IrcMessage& message) } } +void IrcClient::on_kick(const IrcMessage& message) +{ + const std::string target = message.arguments[1]; + const std::string reason = message.arguments[2]; + const std::string chan_name = message.arguments[0]; + IrcChannel* channel = this->get_channel(chan_name); + if (channel->get_self()->nick == target) + channel->joined = false; + IrcUser author(message.prefix); + Iid iid; + iid.chan = chan_name; + iid.server = this->hostname; + this->bridge->kick_muc_user(std::move(iid), target, reason, author.nick); +} + void IrcClient::on_mode(const IrcMessage& message) { const std::string target = message.arguments[0]; -- cgit v1.2.3 From 1e122d3342ef4336f17bd5606be7101748627415 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 17 Nov 2013 11:35:43 +0100 Subject: Send the motd as one single big message We append each line to a string, and when the MOTD is complete, we send that string at once. --- src/irc/irc_client.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 82abbd9..f339580 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -222,6 +222,27 @@ void IrcClient::on_channel_message(const IrcMessage& message) this->bridge->send_message(iid, nick, body, muc); } +void IrcClient::empty_motd(const IrcMessage& message) +{ + (void)message; + this->motd.erase(); +} + +void IrcClient::on_motd_line(const IrcMessage& message) +{ + const std::string body = message.arguments[1]; + // We could send the MOTD without a line break between each IRC-message, + // but sometimes it contains some ASCII art, we use line breaks to keep + // them intact. + this->motd += body+"\n"; +} + +void IrcClient::send_motd(const IrcMessage& message) +{ + (void)message; + this->bridge->send_xmpp_message(this->hostname, "", this->motd); +} + void IrcClient::on_topic_received(const IrcMessage& message) { const std::string chan_name = message.arguments[1]; -- cgit v1.2.3 From 61ecaab40078901800e3e20282f1ae7852a7c938 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 28 Nov 2013 00:55:20 +0100 Subject: Re-add support for /me messages from IRC It was recently removed because it was handled in the old "convert irc colors" code. It now is in the right place. --- src/irc/irc_client.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index f339580..72eec02 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -219,7 +219,14 @@ void IrcClient::on_channel_message(const IrcMessage& message) iid.chan = nick; muc = false; } - this->bridge->send_message(iid, nick, body, muc); + if (!body.empty() && body[0] == '\01') + { + if (body.substr(1, 6) == "ACTION") + this->bridge->send_message(iid, nick, + std::string("/me") + body.substr(7, body.size() - 8), muc); + } + else + this->bridge->send_message(iid, nick, body, muc); } void IrcClient::empty_motd(const IrcMessage& message) -- cgit v1.2.3 From 1151c26c363e736a98c5fcb723c753658fe35b9b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 28 Nov 2013 02:14:42 +0100 Subject: Channel names are case insensitive But some servers (epiknet for example) send channel names with an uppercase --- src/irc/irc_client.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 72eec02..dc0986f 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -174,7 +175,7 @@ void IrcClient::forward_server_message(const IrcMessage& message) void IrcClient::set_and_forward_user_list(const IrcMessage& message) { - const std::string chan_name = message.arguments[2]; + const std::string chan_name = utils::tolower(message.arguments[2]); IrcChannel* channel = this->get_channel(chan_name); std::vector nicks = utils::split(message.arguments[3], ' '); for (const std::string& nick: nicks) @@ -190,7 +191,7 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) void IrcClient::on_channel_join(const IrcMessage& message) { - const std::string chan_name = message.arguments[0]; + const std::string chan_name = utils::tolower(message.arguments[0]); IrcChannel* channel = this->get_channel(chan_name); const std::string nick = message.prefix; if (channel->joined == false) @@ -252,14 +253,14 @@ void IrcClient::send_motd(const IrcMessage& message) void IrcClient::on_topic_received(const IrcMessage& message) { - const std::string chan_name = message.arguments[1]; + const std::string chan_name = utils::tolower(message.arguments[1]); IrcChannel* channel = this->get_channel(chan_name); channel->topic = message.arguments[2]; } void IrcClient::on_channel_completely_joined(const IrcMessage& message) { - const std::string chan_name = message.arguments[1]; + const std::string chan_name = utils::tolower(message.arguments[1]); IrcChannel* channel = this->get_channel(chan_name); this->bridge->send_self_join(this->hostname, chan_name, channel->get_self()->nick); this->bridge->send_topic(this->hostname, chan_name, channel->topic); @@ -276,7 +277,7 @@ void IrcClient::on_welcome_message(const IrcMessage& message) void IrcClient::on_part(const IrcMessage& message) { - const std::string chan_name = message.arguments[0]; + const std::string chan_name = utils::tolower(message.arguments[0]); IrcChannel* channel = this->get_channel(chan_name); std::string txt; if (message.arguments.size() >= 2) @@ -348,7 +349,7 @@ void IrcClient::on_kick(const IrcMessage& message) { const std::string target = message.arguments[1]; const std::string reason = message.arguments[2]; - const std::string chan_name = message.arguments[0]; + const std::string chan_name = utils::tolower(message.arguments[0]); IrcChannel* channel = this->get_channel(chan_name); if (channel->get_self()->nick == target) channel->joined = false; -- cgit v1.2.3 From d6832fbcc98557952387f3ce2899bbebacd7c204 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 28 Nov 2013 21:09:27 +0100 Subject: :3 --- src/irc/irc_client.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index dc0986f..2d158fb 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -91,7 +91,8 @@ void IrcClient::send_message(IrcMessage&& message) res += std::move(message.command); for (const std::string& arg: message.arguments) { - if (arg.find(" ") != std::string::npos) + if (arg.find(" ") != std::string::npos || + (!arg.empty()) && arg[0] == ':') { res += " :" + arg; break; -- cgit v1.2.3 From 6bd176f15ebf146874bc7f4525870e52921cc2fe Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 30 Nov 2013 18:53:45 +0100 Subject: Fix a parenthesis ambiguity --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 2d158fb..0061561 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -92,7 +92,7 @@ void IrcClient::send_message(IrcMessage&& message) for (const std::string& arg: message.arguments) { if (arg.find(" ") != std::string::npos || - (!arg.empty()) && arg[0] == ':') + (!arg.empty() && arg[0] == ':')) { res += " :" + arg; break; -- cgit v1.2.3 From 2662ed89e2cd41477582140e482f1ddbbfdb235e Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 3 Dec 2013 18:27:20 +0100 Subject: Add a logger class --- src/irc/irc_client.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0061561..4e5efe1 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -4,6 +4,7 @@ #include #include +// #include #include #include -- cgit v1.2.3 From a4c845ab6c54172ea305f33734c83238c75d421a Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 3 Dec 2013 19:10:44 +0100 Subject: Use the logger everywhere --- src/irc/irc_client.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 4e5efe1..1a9e61b 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -4,7 +4,7 @@ #include #include -// #include +#include #include #include @@ -18,12 +18,10 @@ IrcClient::IrcClient(const std::string& hostname, const std::string& username, B bridge(bridge), welcomed(false) { - std::cout << "IrcClient()" << std::endl; } IrcClient::~IrcClient() { - std::cout << "~IrcClient()" << std::endl; } void IrcClient::start() @@ -39,7 +37,7 @@ void IrcClient::on_connected() void IrcClient::on_connection_close() { - std::cout << "Connection closed by remote server." << std::endl; + log_warning("Connection closed by remote server."); } IrcChannel* IrcClient::get_channel(const std::string& name) @@ -74,18 +72,19 @@ void IrcClient::parse_in_buffer() if (pos == std::string::npos) break ; IrcMessage message(this->in_buf.substr(0, pos)); + log_debug("IRC RECEIVING: " << message); this->in_buf = this->in_buf.substr(pos + 2, std::string::npos); - std::cout << message << std::endl; auto cb = irc_callbacks.find(message.command); if (cb != irc_callbacks.end()) (this->*(cb->second))(message); else - std::cout << "No handler for command " << message.command << std::endl; + log_info("No handler for command " << message.command); } } void IrcClient::send_message(IrcMessage&& message) { + log_debug("IRC SENDING: " << message); std::string res; if (!message.prefix.empty()) res += ":" + std::move(message.prefix) + " "; @@ -101,8 +100,6 @@ void IrcClient::send_message(IrcMessage&& message) res += " " + arg; } res += "\r\n"; - std::cout << "=== IRC SENDING ===" << std::endl; - std::cout << res << std::endl; this->send_data(std::move(res)); } @@ -134,7 +131,7 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st IrcChannel* channel = this->get_channel(chan_name); if (channel->joined == false) { - std::cout << "Cannot send message to channel " << chan_name << ", it is not joined" << std::endl; + log_warning("Cannot send message to channel " << chan_name << ", it is not joined"); return false; } this->send_message(IrcMessage("PRIVMSG", {chan_name, body})); @@ -185,7 +182,7 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) IrcUser* user = channel->add_user(nick); if (user->nick != channel->get_self()->nick) { - std::cout << "Adding user [" << nick << "] to chan " << chan_name << std::endl; + log_debug("Adding user [" << nick << "] to chan " << chan_name); this->bridge->send_user_join(this->hostname, chan_name, user->nick); } } -- cgit v1.2.3 From 6fa548a194082648724078cc777da34c30dd40a1 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 3 Dec 2013 19:56:57 +0100 Subject: Display all the MODE arguments in the message --- src/irc/irc_client.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 1a9e61b..af3df62 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -377,10 +377,18 @@ void IrcClient::on_channel_mode(const IrcMessage& message) iid.chan = message.arguments[0]; iid.server = this->hostname; IrcUser user(message.prefix); + std::string mode_arguments; + for (size_t i = 1; i < message.arguments.size(); ++i) + { + if (!message.arguments[i].empty()) + { + if (i != 1) + mode_arguments += " "; + mode_arguments += message.arguments[i]; + } + } this->bridge->send_message(iid, "", std::string("Mode ") + iid.chan + - " [" + message.arguments[1] + - (message.arguments.size() > 2 ? (" " + message.arguments[2]): "") - + "] by " + user.nick, + " [" + mode_arguments + "] by " + user.nick, true); } -- cgit v1.2.3 From e2117bcb0abfde30ad503b99da58699cf0f2a95b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 8 Dec 2013 04:44:46 +0100 Subject: Enforce a simple limit of 400 bytes for IRC messages body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The limit for the whole message is 512 bytes, we limit the body to 400 (instead of doing a calculation based on the command name and the other parameters), because it's simple, easy and that’s enough. fixes #2416 --- src/irc/irc_client.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index af3df62..0d4d102 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -134,13 +134,27 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st log_warning("Cannot send message to channel " << chan_name << ", it is not joined"); return false; } - this->send_message(IrcMessage("PRIVMSG", {chan_name, body})); + // Cut the message body into 400-bytes parts (because the whole command + // must fit into 512 bytes, that's an easy way to make sure the chan name + // + body fits. I’m lazy.) + std::string::size_type pos = 0; + while (pos < body.size()) + { + this->send_message(IrcMessage("PRIVMSG", {chan_name, body.substr(pos, 400)})); + pos += 400; + } return true; } void IrcClient::send_private_message(const std::string& username, const std::string& body) { - this->send_message(IrcMessage("PRIVMSG", {username, body})); + std::string::size_type pos = 0; + while (pos < body.size()) + { + this->send_message(IrcMessage("PRIVMSG", {username, body.substr(pos, 400)})); + pos += 400; + } + } void IrcClient::send_part_command(const std::string& chan_name, const std::string& status_message) -- cgit v1.2.3 From b11126a19dbaadf4c32fb8dbec22754ad0712c26 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 8 Dec 2013 20:14:12 +0100 Subject: Provide a JID for IRC users, and add a stringprep dependency for this --- src/irc/irc_client.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0d4d102..0f29a2b 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -193,11 +193,11 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) std::vector nicks = utils::split(message.arguments[3], ' '); for (const std::string& nick: nicks) { - IrcUser* user = channel->add_user(nick); + const IrcUser* user = channel->add_user(nick); if (user->nick != channel->get_self()->nick) { log_debug("Adding user [" << nick << "] to chan " << chan_name); - this->bridge->send_user_join(this->hostname, chan_name, user->nick); + this->bridge->send_user_join(this->hostname, chan_name, user); } } } @@ -214,8 +214,8 @@ void IrcClient::on_channel_join(const IrcMessage& message) } else { - IrcUser* user = channel->add_user(nick); - this->bridge->send_user_join(this->hostname, chan_name, user->nick); + const IrcUser* user = channel->add_user(nick); + this->bridge->send_user_join(this->hostname, chan_name, user); } } -- cgit v1.2.3 From b29290f73a24f2d5af7bde45c9ff5332c7a1f5a6 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 9 Dec 2013 03:53:01 +0100 Subject: Lowercase the chan names in two missing cases --- src/irc/irc_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0f29a2b..ed98653 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -224,7 +224,7 @@ void IrcClient::on_channel_message(const IrcMessage& message) const IrcUser user(message.prefix); const std::string nick = user.nick; Iid iid; - iid.chan = message.arguments[0]; + iid.chan = utils::tolower(message.arguments[0]); iid.server = this->hostname; const std::string body = message.arguments[1]; bool muc = true; @@ -388,7 +388,7 @@ void IrcClient::on_channel_mode(const IrcMessage& message) // For now, just transmit the modes so the user can know what happens // TODO, actually interprete the mode. Iid iid; - iid.chan = message.arguments[0]; + iid.chan = utils::tolower(message.arguments[0]); iid.server = this->hostname; IrcUser user(message.prefix); std::string mode_arguments; -- cgit v1.2.3 From 3afb63a650b8b925ce1ba722dd42b7418f623713 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 21 Dec 2013 21:04:41 +0100 Subject: Shutdown cleanly on SIGINT --- src/irc/irc_client.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index ed98653..2115bdc 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -118,6 +118,11 @@ void IrcClient::send_kick_command(const std::string& chan_name, const std::strin this->send_message(IrcMessage("KICK", {chan_name, target, reason})); } +void IrcClient::send_quit_command() +{ + this->send_message(IrcMessage("QUIT", {"gateway shutdown"})); +} + void IrcClient::send_join_command(const std::string& chan_name) { if (this->welcomed == false) @@ -310,6 +315,21 @@ void IrcClient::on_part(const IrcMessage& message) } } +void IrcClient::on_error(const IrcMessage& message) +{ + const std::string leave_message = message.arguments[0]; + // The user is out of all the channels + for (auto it = this->channels.begin(); it != this->channels.end(); ++it) + { + Iid iid; + iid.chan = it->first; + iid.server = this->hostname; + IrcChannel* channel = it->second.get(); + std::string own_nick = channel->get_self()->nick; + this->bridge->send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true); + } +} + void IrcClient::on_quit(const IrcMessage& message) { std::string txt; -- cgit v1.2.3 From b8ce9ed43d809daefe17714b36aa0874ca5f6cc8 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 26 Dec 2013 14:30:07 +0100 Subject: Check that channels are joined before acting on objects in it --- src/irc/irc_client.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 2115bdc..bde9973 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -297,6 +297,8 @@ void IrcClient::on_part(const IrcMessage& message) { const std::string chan_name = utils::tolower(message.arguments[0]); IrcChannel* channel = this->get_channel(chan_name); + if (!channel->joined) + return ; std::string txt; if (message.arguments.size() >= 2) txt = message.arguments[1]; @@ -325,6 +327,8 @@ void IrcClient::on_error(const IrcMessage& message) iid.chan = it->first; iid.server = this->hostname; IrcChannel* channel = it->second.get(); + if (!channel->joined) + continue; std::string own_nick = channel->get_self()->nick; this->bridge->send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true); } @@ -384,6 +388,8 @@ void IrcClient::on_kick(const IrcMessage& message) const std::string reason = message.arguments[2]; const std::string chan_name = utils::tolower(message.arguments[0]); IrcChannel* channel = this->get_channel(chan_name); + if (!channel->joined) + return ; if (channel->get_self()->nick == target) channel->joined = false; IrcUser author(message.prefix); -- cgit v1.2.3 From 9df757fc6737b59f56d5b808ef48baba760b142e Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 26 Dec 2013 14:57:13 +0100 Subject: Handle topic changes --- src/irc/irc_client.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index bde9973..bdeb2f8 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -274,6 +274,8 @@ void IrcClient::on_topic_received(const IrcMessage& message) const std::string chan_name = utils::tolower(message.arguments[1]); IrcChannel* channel = this->get_channel(chan_name); channel->topic = message.arguments[2]; + if (channel->joined) + this->bridge->send_topic(this->hostname, chan_name, channel->topic); } void IrcClient::on_channel_completely_joined(const IrcMessage& message) -- cgit v1.2.3 From 483e17020dc4f19223001ab36da0dc48a15a0d3e Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 27 Dec 2013 11:49:28 +0100 Subject: Be verbose about the connection status, and some errors --- src/irc/irc_client.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index bdeb2f8..881de96 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -33,11 +33,14 @@ void IrcClient::on_connected() { this->send_nick_command(this->username); this->send_user_command(this->username, this->username); + this->send_gateway_message("Connected to IRC server."); } void IrcClient::on_connection_close() { - log_warning("Connection closed by remote server."); + static const std::string message = "Connection closed by remote server."; + this->send_gateway_message(message); + log_warning(message); } IrcChannel* IrcClient::get_channel(const std::string& name) @@ -191,6 +194,11 @@ void IrcClient::forward_server_message(const IrcMessage& message) this->bridge->send_xmpp_message(this->hostname, from, body); } +void IrcClient::send_gateway_message(const std::string& message, const std::string& from) +{ + this->bridge->send_xmpp_message(this->hostname, from, message); +} + void IrcClient::set_and_forward_user_list(const IrcMessage& message) { const std::string chan_name = utils::tolower(message.arguments[2]); @@ -286,6 +294,20 @@ void IrcClient::on_channel_completely_joined(const IrcMessage& message) this->bridge->send_topic(this->hostname, chan_name, channel->topic); } +void IrcClient::on_erroneous_nickname(const IrcMessage& message) +{ + const std::string error_msg = message.arguments.size() >= 3 ? + message.arguments[2]: "Erroneous nickname"; + this->send_gateway_message(error_msg + ": " + message.arguments[1], message.prefix); +} + +void IrcClient::on_generic_error(const IrcMessage& message) +{ + const std::string error_msg = message.arguments.size() >= 3 ? + message.arguments[2]: "Unspecified error"; + this->send_gateway_message(message.arguments[1] + ": " + error_msg, message.prefix); +} + void IrcClient::on_welcome_message(const IrcMessage& message) { this->current_nick = message.arguments[0]; @@ -334,6 +356,7 @@ void IrcClient::on_error(const IrcMessage& message) std::string own_nick = channel->get_self()->nick; this->bridge->send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true); } + this->send_gateway_message(std::string("ERROR: ") + leave_message); } void IrcClient::on_quit(const IrcMessage& message) -- cgit v1.2.3 From aa53276859a5fc80071d24111a311297e058e603 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 27 Dec 2013 11:53:29 +0100 Subject: Keep a "connected" state in the SocketHandler class --- src/irc/irc_client.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 881de96..4e37a63 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -128,6 +128,8 @@ void IrcClient::send_quit_command() void IrcClient::send_join_command(const std::string& chan_name) { + if (!this->connected) + this->start(); if (this->welcomed == false) this->channels_to_join.push_back(chan_name); else -- cgit v1.2.3 From e8e592d1ace5413a1e7d8b59b9467c78d8d68ea9 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 27 Dec 2013 12:01:26 +0100 Subject: Remove disconnected IrcClients --- src/irc/irc_client.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 4e37a63..afdc629 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -58,8 +58,8 @@ IrcChannel* IrcClient::get_channel(const std::string& name) bool IrcClient::is_channel_joined(const std::string& name) { - IrcChannel* client = this->get_channel(name); - return client->joined; + IrcChannel* channel = this->get_channel(name); + return channel->joined; } std::string IrcClient::get_own_nick() const @@ -339,7 +339,12 @@ void IrcClient::on_part(const IrcMessage& message) bool self = channel->get_self()->nick == nick; this->bridge->send_muc_leave(std::move(iid), std::move(nick), std::move(txt), self); if (self) + { channel->joined = false; + this->channels.erase(chan_name); + // channel pointer is now invalid + channel = nullptr; + } } } @@ -358,6 +363,7 @@ void IrcClient::on_error(const IrcMessage& message) std::string own_nick = channel->get_self()->nick; this->bridge->send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true); } + this->channels.clear(); this->send_gateway_message(std::string("ERROR: ") + leave_message); } -- cgit v1.2.3 From 43cc60e4a9e2859fdf67c89e58ee18cf7571f186 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 27 Dec 2013 15:34:58 +0100 Subject: Handle nickname conflicts by sending the correct XMPP error presence --- src/irc/irc_client.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index afdc629..10a5b12 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -303,6 +303,19 @@ void IrcClient::on_erroneous_nickname(const IrcMessage& message) this->send_gateway_message(error_msg + ": " + message.arguments[1], message.prefix); } +void IrcClient::on_nickname_conflict(const IrcMessage& message) +{ + const std::string nickname = message.arguments[1]; + this->on_generic_error(message); + for (auto it = this->channels.begin(); it != this->channels.end(); ++it) + { + Iid iid; + iid.chan = it->first; + iid.server = this->hostname; + this->bridge->send_nickname_conflict_error(iid, nickname); + } +} + void IrcClient::on_generic_error(const IrcMessage& message) { const std::string error_msg = message.arguments.size() >= 3 ? -- cgit v1.2.3 From a075517824da7e82e1be7b67d615834851482861 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 27 Dec 2013 17:42:48 +0100 Subject: Basic isupport support CHANMODES and PREFIX only --- src/irc/irc_client.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 10a5b12..912ee45 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -16,7 +16,8 @@ IrcClient::IrcClient(const std::string& hostname, const std::string& username, B username(username), current_nick(username), bridge(bridge), - welcomed(false) + welcomed(false), + chanmodes({"", "", "", ""}) { } @@ -196,6 +197,32 @@ void IrcClient::forward_server_message(const IrcMessage& message) this->bridge->send_xmpp_message(this->hostname, from, body); } +void IrcClient::on_isupport_message(const IrcMessage& message) +{ + const size_t len = message.arguments.size(); + for (size_t i = 1; i < len; ++i) + { + const std::string token = message.arguments[i]; + if (token.substr(0, 10) == "CHANMODES=") + { + this->chanmodes = utils::split(token.substr(11), ','); + // make sure we have 4 strings + this->chanmodes.resize(4); + } + else if (token.substr(0, 7) == "PREFIX=") + { + size_t i = 8; // jump PREFIX=( + size_t j = 9; + // Find the ) char + while (j < token.size() && token[j] != ')') + j++; + j++; + while (j < token.size() && token[i] != ')') + this->prefix_to_mode[token[j++]] = token[i++]; + } + } +} + void IrcClient::send_gateway_message(const std::string& message, const std::string& from) { this->bridge->send_xmpp_message(this->hostname, from, message); -- cgit v1.2.3 From acf769d83a40e971ccc1346225688841465b36ee Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 29 Dec 2013 19:57:43 +0100 Subject: Use isupport informations to know the user modes when joining Also remove the duplicate send_self_join methods, user only send_user_join --- src/irc/irc_client.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 912ee45..cdda2b5 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -235,11 +235,16 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) std::vector nicks = utils::split(message.arguments[3], ' '); for (const std::string& nick: nicks) { - const IrcUser* user = channel->add_user(nick); + const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); if (user->nick != channel->get_self()->nick) { log_debug("Adding user [" << nick << "] to chan " << chan_name); - this->bridge->send_user_join(this->hostname, chan_name, user); + this->bridge->send_user_join(this->hostname, chan_name, user, false); + } + else + { + // we now know the modes of self, so copy the modes into self + channel->get_self()->modes = user->modes; } } } @@ -256,8 +261,8 @@ void IrcClient::on_channel_join(const IrcMessage& message) } else { - const IrcUser* user = channel->add_user(nick); - this->bridge->send_user_join(this->hostname, chan_name, user); + const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); + this->bridge->send_user_join(this->hostname, chan_name, user, false); } } @@ -319,7 +324,7 @@ void IrcClient::on_channel_completely_joined(const IrcMessage& message) { const std::string chan_name = utils::tolower(message.arguments[1]); IrcChannel* channel = this->get_channel(chan_name); - this->bridge->send_self_join(this->hostname, chan_name, channel->get_self()->nick); + this->bridge->send_user_join(this->hostname, chan_name, channel->get_self(), true); this->bridge->send_topic(this->hostname, chan_name, channel->topic); } -- cgit v1.2.3 From e840704b58a984351971e8034e74f5e9fdfaf114 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 4 Jan 2014 01:30:03 +0100 Subject: Convert received modes into roles and affiliations --- src/irc/irc_client.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index cdda2b5..8a57371 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -218,7 +218,10 @@ void IrcClient::on_isupport_message(const IrcMessage& message) j++; j++; while (j < token.size() && token[i] != ')') - this->prefix_to_mode[token[j++]] = token[i++]; + { + this->sorted_user_modes.push_back(token[i]); + this->prefix_to_mode[token[j++]] = token[i++]; + } } } } @@ -508,6 +511,76 @@ void IrcClient::on_channel_mode(const IrcMessage& message) this->bridge->send_message(iid, "", std::string("Mode ") + iid.chan + " [" + mode_arguments + "] by " + user.nick, true); + const IrcChannel* channel = this->get_channel(iid.chan); + if (!channel) + return; + + // parse the received modes, we need to handle things like "+m-oo coucou toutou" + const std::string modes = message.arguments[1]; + // a list of modified IrcUsers. When we applied all modes, we check the + // modes that now applies to each of them, and send a notification for + // each one. This is to disallow sending two notifications or more when a + // single MODE command changes two or more modes on the same participant + std::set modified_users; + // If it is true, the modes are added, if it’s false they are + // removed. When we encounter the '+' char, the value is changed to true, + // and with '-' it is changed to false. + bool add = true; + bool use_arg; + size_t arg_pos = 2; + for (const char c: modes) + { + if (c == '+') + add = true; + else if (c == '-') + add = false; + else + { // lookup the mode symbol in the 4 chanmodes lists, depending on + // the list where it is found, it takes an argument or not + size_t type; + for (type = 0; type < 4; ++type) + if (this->chanmodes[type].find(c) != std::string::npos) + break; + if (type == 4) // if mode was not found + { + // That mode can also be of type B if it is present in the + // prefix_to_mode map + for (const std::pair& pair: this->prefix_to_mode) + if (pair.second == c) + { + type = 1; + break; + } + } + // modes of type A, B or C (but only with add == true) + if (type == 0 || type == 1 || + (type == 2 && add == true)) + use_arg = true; + else // modes of type C (but only with add == false), D, or unknown + use_arg = false; + if (use_arg == true && message.arguments.size() > arg_pos) + { + const std::string target = message.arguments[arg_pos++]; + IrcUser* user = channel->find_user(target); + if (!user) + { + log_warning("Trying to set mode for non-existing user '" << target + << "' in channel" << iid.chan); + return; + } + if (add) + user->add_mode(c); + else + user->remove_mode(c); + modified_users.insert(user); + } + } + } + for (const IrcUser* u: modified_users) + { + char most_significant_mode = u->get_most_significant_mode(this->sorted_user_modes); + this->bridge->send_affiliation_role_change(iid, u->nick, most_significant_mode); + } } void IrcClient::on_user_mode(const IrcMessage& message) -- cgit v1.2.3 From fbec16f1a208881ea49923287aae27978d79681e Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 4 Jan 2014 01:53:50 +0100 Subject: Possibility to change a channel's topic --- src/irc/irc_client.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 8a57371..644cfd1 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -122,6 +122,11 @@ void IrcClient::send_kick_command(const std::string& chan_name, const std::strin this->send_message(IrcMessage("KICK", {chan_name, target, reason})); } +void IrcClient::send_topic_command(const std::string& chan_name, const std::string& topic) +{ + this->send_message(IrcMessage("TOPIC", {chan_name, topic})); +} + void IrcClient::send_quit_command() { this->send_message(IrcMessage("QUIT", {"gateway shutdown"})); -- cgit v1.2.3 From d90c1978def803390203f0fada3621de8abba0e5 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 4 Jan 2014 01:54:09 +0100 Subject: Fix a bug when receiving a topic change The number of arguments is not always the same --- src/irc/irc_client.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 644cfd1..1e8e326 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -321,9 +321,11 @@ void IrcClient::send_motd(const IrcMessage& message) void IrcClient::on_topic_received(const IrcMessage& message) { - const std::string chan_name = utils::tolower(message.arguments[1]); + if (message.arguments.size() < 2) + return; + const std::string chan_name = utils::tolower(message.arguments[message.arguments.size() - 2]); IrcChannel* channel = this->get_channel(chan_name); - channel->topic = message.arguments[2]; + channel->topic = message.arguments[message.arguments.size() - 1]; if (channel->joined) this->bridge->send_topic(this->hostname, chan_name, channel->topic); } -- cgit v1.2.3 From fef9c8193ddf8cdf81978874be788af9441e2286 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 6 Jan 2014 01:22:43 +0100 Subject: Also set the role and affiliation of users already in the chan --- src/irc/irc_client.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 1e8e326..644351e 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -247,7 +247,9 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) if (user->nick != channel->get_self()->nick) { log_debug("Adding user [" << nick << "] to chan " << chan_name); - this->bridge->send_user_join(this->hostname, chan_name, user, false); + this->bridge->send_user_join(this->hostname, chan_name, user, + user->get_most_significant_mode(this->sorted_user_modes), + false); } else { @@ -270,7 +272,9 @@ void IrcClient::on_channel_join(const IrcMessage& message) else { const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); - this->bridge->send_user_join(this->hostname, chan_name, user, false); + this->bridge->send_user_join(this->hostname, chan_name, user, + user->get_most_significant_mode(this->sorted_user_modes), + false); } } @@ -334,7 +338,9 @@ void IrcClient::on_channel_completely_joined(const IrcMessage& message) { const std::string chan_name = utils::tolower(message.arguments[1]); IrcChannel* channel = this->get_channel(chan_name); - this->bridge->send_user_join(this->hostname, chan_name, channel->get_self(), true); + this->bridge->send_user_join(this->hostname, chan_name, channel->get_self(), + channel->get_self()->get_most_significant_mode(this->sorted_user_modes), + true); this->bridge->send_topic(this->hostname, chan_name, channel->topic); } -- cgit v1.2.3 From c8b41d5e350354881caa933ab363a2e90f991524 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 8 Feb 2014 07:52:22 +0100 Subject: Be verbose about IRC server connection failures, and handle them properly --- src/irc/irc_client.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 644351e..a84f96e 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -27,7 +27,12 @@ IrcClient::~IrcClient() void IrcClient::start() { - this->connect(this->hostname, "6667"); + this->bridge->send_xmpp_message(this->hostname, "", std::string("Connecting to ") + + this->hostname + ":" + "6667"); + std::pair res = this->connect(this->hostname, "6667"); + if (!res.first) + this->bridge->send_xmpp_message(this->hostname, "", + std::string("Connection failed: ") + res.second); } void IrcClient::on_connected() -- cgit v1.2.3 From cf9f3a1f2855b358aa9bbc31f234801e9e1efc28 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 17 Feb 2014 01:43:58 +0100 Subject: Include role and affiliation in the join presence of the nick change process --- src/irc/irc_client.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index a84f96e..bb2fc75 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -469,8 +469,9 @@ void IrcClient::on_nick(const IrcMessage& message) Iid iid; iid.chan = chan_name; iid.server = this->hostname; - bool self = channel->get_self()->nick == old_nick; - this->bridge->send_nick_change(std::move(iid), old_nick, new_nick, self); + const bool self = channel->get_self()->nick == old_nick; + const char user_mode = user->get_most_significant_mode(this->sorted_user_modes); + this->bridge->send_nick_change(std::move(iid), old_nick, new_nick, user_mode, self); user->nick = new_nick; if (self) { -- cgit v1.2.3 From 112197f6ab8ec392aecfccd10af5547633fe7482 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 19 Feb 2014 02:38:22 +0100 Subject: Fix the joined flag on channels, avoid sending the topic twice when joining --- src/irc/irc_client.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index bb2fc75..d65da87 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -270,10 +270,7 @@ void IrcClient::on_channel_join(const IrcMessage& message) IrcChannel* channel = this->get_channel(chan_name); const std::string nick = message.prefix; if (channel->joined == false) - { - channel->joined = true; - channel->set_self(nick); - } + channel->set_self(nick); else { const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); @@ -343,6 +340,7 @@ void IrcClient::on_channel_completely_joined(const IrcMessage& message) { const std::string chan_name = utils::tolower(message.arguments[1]); IrcChannel* channel = this->get_channel(chan_name); + channel->joined = true; this->bridge->send_user_join(this->hostname, chan_name, channel->get_self(), channel->get_self()->get_most_significant_mode(this->sorted_user_modes), true); -- cgit v1.2.3 From 16be9af3fcd23fa8590692cbe84af20cfe6a60b2 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 19 Feb 2014 23:59:43 +0100 Subject: Send unavailable presence to all muc when the IRC server closes the connection --- src/irc/irc_client.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index d65da87..e134bea 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -46,6 +46,8 @@ void IrcClient::on_connection_close() { static const std::string message = "Connection closed by remote server."; this->send_gateway_message(message); + const IrcMessage error{"ERROR", {message}}; + this->on_error(error); log_warning(message); } -- cgit v1.2.3 From 190c4ff1762e5e762e913f98033369ed75ed5291 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 20 Feb 2014 02:19:50 +0100 Subject: QUIT the irc server when the last channel is left --- src/irc/irc_client.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e134bea..0d5c4e3 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -134,9 +134,9 @@ void IrcClient::send_topic_command(const std::string& chan_name, const std::stri this->send_message(IrcMessage("TOPIC", {chan_name, topic})); } -void IrcClient::send_quit_command() +void IrcClient::send_quit_command(const std::string& reason) { - this->send_message(IrcMessage("QUIT", {"gateway shutdown"})); + this->send_message(IrcMessage("QUIT", {reason})); } void IrcClient::send_join_command(const std::string& chan_name) @@ -403,7 +403,6 @@ void IrcClient::on_part(const IrcMessage& message) iid.chan = chan_name; iid.server = this->hostname; bool self = channel->get_self()->nick == nick; - this->bridge->send_muc_leave(std::move(iid), std::move(nick), std::move(txt), self); if (self) { channel->joined = false; @@ -411,6 +410,7 @@ void IrcClient::on_part(const IrcMessage& message) // channel pointer is now invalid channel = nullptr; } + this->bridge->send_muc_leave(std::move(iid), std::move(nick), std::move(txt), self); } } @@ -608,3 +608,8 @@ void IrcClient::on_user_mode(const IrcMessage& message) std::string("User mode for ") + message.arguments[0] + " is [" + message.arguments[1] + "]"); } + +size_t IrcClient::number_of_joined_channels() const +{ + return this->channels.size(); +} -- cgit v1.2.3 From 99aba5667d0d7ba6657f9c175a9342126bc4b0f2 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 22 Feb 2014 21:42:24 +0100 Subject: Connection to servers does not block the process anymore --- src/irc/irc_client.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0d5c4e3..d35437c 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -29,10 +29,13 @@ void IrcClient::start() { this->bridge->send_xmpp_message(this->hostname, "", std::string("Connecting to ") + this->hostname + ":" + "6667"); - std::pair res = this->connect(this->hostname, "6667"); - if (!res.first) - this->bridge->send_xmpp_message(this->hostname, "", - std::string("Connection failed: ") + res.second); + this->connect(this->hostname, "6667"); +} + +void IrcClient::on_connection_failed(const std::string& reason) +{ + this->bridge->send_xmpp_message(this->hostname, "", + std::string("Connection failed: ") + reason); } void IrcClient::on_connected() -- cgit v1.2.3 From 76b131e13a842657b899d8852efb0f92b2d32145 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 25 Feb 2014 02:10:45 +0100 Subject: Do not try to connect to an irc server if we are connected or connecting --- src/irc/irc_client.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index d35437c..232b87f 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -27,6 +27,8 @@ IrcClient::~IrcClient() void IrcClient::start() { + if (this->connected || this->connecting) + return ; this->bridge->send_xmpp_message(this->hostname, "", std::string("Connecting to ") + this->hostname + ":" + "6667"); this->connect(this->hostname, "6667"); @@ -144,8 +146,7 @@ void IrcClient::send_quit_command(const std::string& reason) void IrcClient::send_join_command(const std::string& chan_name) { - if (!this->connected) - this->start(); + this->start(); if (this->welcomed == false) this->channels_to_join.push_back(chan_name); else -- cgit v1.2.3 From 86d4347af8532ef85472e47c01d645fa5ad1b3b1 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 28 Feb 2014 01:14:38 +0100 Subject: Avoid unnecessary copies by recv()ing data directly into the expat buffer --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 232b87f..0c36372 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -80,7 +80,7 @@ std::string IrcClient::get_own_nick() const return this->current_nick; } -void IrcClient::parse_in_buffer() +void IrcClient::parse_in_buffer(const size_t) { while (true) { -- cgit v1.2.3 From e196d2f1f400f5c9634ed42fcbdf5c5fb63a13fb Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 24 Mar 2014 18:37:31 +0100 Subject: Do not send data if we are connected, send it only once we actually are --- src/irc/irc_client.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0c36372..3737b91 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -45,6 +45,7 @@ void IrcClient::on_connected() this->send_nick_command(this->username); this->send_user_command(this->username, this->username); this->send_gateway_message("Connected to IRC server."); + this->send_pending_data(); } void IrcClient::on_connection_close() -- cgit v1.2.3 From e971f64f92c640d3ee634b01eeba7fbf056fdaac Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 28 Mar 2014 01:47:48 +0100 Subject: Catch all exception produced by a received message (irc or xmpp) --- src/irc/irc_client.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 3737b91..884f214 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -93,7 +93,13 @@ void IrcClient::parse_in_buffer(const size_t) this->in_buf = this->in_buf.substr(pos + 2, std::string::npos); auto cb = irc_callbacks.find(message.command); if (cb != irc_callbacks.end()) - (this->*(cb->second))(message); + { + try { + (this->*(cb->second))(message); + } catch (const std::exception& e) { + log_error("Unhandled exception: " << e.what()); + } + } else log_info("No handler for command " << message.command); } -- cgit v1.2.3 From 020325dbb071f1735bceb80de9f982aefcd2de47 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 13 Apr 2014 15:37:56 +0200 Subject: [WIP] DummyIrcChannel --- src/irc/irc_client.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 884f214..e565658 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -280,7 +280,11 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) void IrcClient::on_channel_join(const IrcMessage& message) { const std::string chan_name = utils::tolower(message.arguments[0]); - IrcChannel* channel = this->get_channel(chan_name); + IrcChannel* channel; + if (chan_name.empty()) + channel = &this->dummy_channel; + else + channel = this->get_channel(chan_name); const std::string nick = message.prefix; if (channel->joined == false) channel->set_self(nick); @@ -394,6 +398,18 @@ void IrcClient::on_welcome_message(const IrcMessage& message) for (const std::string& chan_name: this->channels_to_join) this->send_join_command(chan_name); this->channels_to_join.clear(); + // Indicate that the dummy channel is joined as well, if needed + if (this->dummy_channel.joining) + { + // Simulate a message coming from the IRC server saying that we joined + // the channel + const IrcMessage join_message(this->get_nick(), "JOIN", {""}); + this->on_channel_join(join_message); + const IrcMessage end_join_message(std::string(this->hostname), "366", + {this->get_nick(), + "", "End of NAMES list"}); + this->on_channel_completely_joined(end_join_message); + } } void IrcClient::on_part(const IrcMessage& message) @@ -624,3 +640,8 @@ size_t IrcClient::number_of_joined_channels() const { return this->channels.size(); } + +DummyIrcChannel& IrcClient::get_dummy_channel() +{ + return this->dummy_channel; +} -- cgit v1.2.3 From 576fb3d132a11ca787f98da67889690b03c7ba8d Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 15 Apr 2014 04:44:09 +0200 Subject: Correctly use the dummy channel whenever we interract with an empty-string chan --- src/irc/irc_client.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e565658..7be7cc8 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -59,6 +59,8 @@ void IrcClient::on_connection_close() IrcChannel* IrcClient::get_channel(const std::string& name) { + if (name.empty()) + return &this->dummy_channel; try { return this->channels.at(name).get(); -- cgit v1.2.3 From cfca16bbecac9f14812db6d06588aae60cc55649 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 15 Apr 2014 04:44:51 +0200 Subject: Ability to leave the dummy channel --- src/irc/irc_client.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 7be7cc8..78acce5 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -197,7 +197,12 @@ void IrcClient::send_part_command(const std::string& chan_name, const std::strin { IrcChannel* channel = this->get_channel(chan_name); if (channel->joined == true) - this->send_message(IrcMessage("PART", {chan_name, status_message})); + { + if (chan_name.empty()) + this->bridge->send_muc_leave(Iid(std::string("%") + this->hostname), std::string(this->current_nick), "", true); + else + this->send_message(IrcMessage("PART", {chan_name, status_message})); + } } void IrcClient::send_mode_command(const std::string& chan_name, const std::vector& arguments) -- cgit v1.2.3 From 5739d418e2b35dfc038fe1a12f8b5c7eeeed6868 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 15 Apr 2014 04:56:50 +0200 Subject: Better way to leave the dummy room --- src/irc/irc_client.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 78acce5..f44821e 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -199,7 +199,7 @@ void IrcClient::send_part_command(const std::string& chan_name, const std::strin if (channel->joined == true) { if (chan_name.empty()) - this->bridge->send_muc_leave(Iid(std::string("%") + this->hostname), std::string(this->current_nick), "", true); + this->leave_dummy_channel(status_message); else this->send_message(IrcMessage("PART", {chan_name, status_message})); } @@ -652,3 +652,13 @@ DummyIrcChannel& IrcClient::get_dummy_channel() { return this->dummy_channel; } + +void IrcClient::leave_dummy_channel(const std::string& exit_message) +{ + if (!this->dummy_channel.joined) + return; + this->dummy_channel.joined = false; + this->dummy_channel.joining = false; + this->dummy_channel.remove_all_users(); + this->bridge->send_muc_leave(Iid(std::string("%") + this->hostname), std::string(this->current_nick), exit_message, true); +} -- cgit v1.2.3 From 28065d3d52cc48abd39a8556a0c282014b18f25a Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 15 Apr 2014 05:08:07 +0200 Subject: Do not disconnect from the IRC server if the dummy channel is joined --- src/irc/irc_client.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index f44821e..55c442b 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -645,7 +645,10 @@ void IrcClient::on_user_mode(const IrcMessage& message) size_t IrcClient::number_of_joined_channels() const { - return this->channels.size(); + if (this->dummy_channel.joined) + return this->channels.size() + 1; + else + return this->channels.size(); } DummyIrcChannel& IrcClient::get_dummy_channel() -- cgit v1.2.3 From 3bc6f1834738a1ee5a97580678ce204c0c215b71 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 15 Apr 2014 05:22:32 +0200 Subject: Add an explanatory topic on the dummy channel --- src/irc/irc_client.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 55c442b..537b738 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -19,6 +19,14 @@ IrcClient::IrcClient(const std::string& hostname, const std::string& username, B welcomed(false), chanmodes({"", "", "", ""}) { + this->dummy_channel.topic = "This is a virtual channel provided for " + "convenience by biboumi, it is not connected " + "to any actual IRC channel of the server '" + this->hostname + + "', and sending messages in it has no effect. " + "Its main goal is to keep the connection to the IRC server " + "alive without having to join a real channel of that server. " + "To disconnect from the IRC server, leave this room and all " + "other IRC channels of that server."; } IrcClient::~IrcClient() -- cgit v1.2.3 From 77a84fd2d99bcffd562f09c8235e5bcd365accb1 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 25 Apr 2014 00:35:57 +0200 Subject: NOTICE from channels are displayed in the channel, with a green "[notice]" --- src/irc/irc_client.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 537b738..f077e37 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -235,6 +235,21 @@ void IrcClient::forward_server_message(const IrcMessage& message) this->bridge->send_xmpp_message(this->hostname, from, body); } +void IrcClient::on_notice(const IrcMessage& message) +{ + std::string from = message.prefix; + const std::string to = message.arguments[0]; + const std::string body = message.arguments[1]; + + if (to == this->current_nick) + this->bridge->send_xmpp_message(this->hostname, from, body); + else + { + IrcMessage modified_message(std::move(from), "PRIVMSG", {to, std::string("\u000303[notice]\u0003 ") + body}); + this->on_channel_message(modified_message); + } +} + void IrcClient::on_isupport_message(const IrcMessage& message) { const size_t len = message.arguments.size(); -- cgit v1.2.3 From c6059e5a215624e205cae401183f3a8bb1bf87d0 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 28 Apr 2014 18:46:25 +0200 Subject: Upgrade to C++14 --- src/irc/irc_client.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index f077e37..e70dbe4 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -3,7 +3,6 @@ #include #include -#include #include #include #include -- cgit v1.2.3 From f6e6b8905be010d7329316cea4546900ad8a2d19 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 29 Apr 2014 21:50:13 +0200 Subject: Use C++14 string_literals --- src/irc/irc_client.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e70dbe4..da5a947 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -10,6 +10,9 @@ #include #include +#include +using namespace std::string_literals; + IrcClient::IrcClient(const std::string& hostname, const std::string& username, Bridge* bridge): hostname(hostname), username(username), @@ -36,7 +39,7 @@ void IrcClient::start() { if (this->connected || this->connecting) return ; - this->bridge->send_xmpp_message(this->hostname, "", std::string("Connecting to ") + + this->bridge->send_xmpp_message(this->hostname, "", "Connecting to "s + this->hostname + ":" + "6667"); this->connect(this->hostname, "6667"); } @@ -44,7 +47,7 @@ void IrcClient::start() void IrcClient::on_connection_failed(const std::string& reason) { this->bridge->send_xmpp_message(this->hostname, "", - std::string("Connection failed: ") + reason); + "Connection failed: "s + reason); } void IrcClient::on_connected() @@ -244,7 +247,7 @@ void IrcClient::on_notice(const IrcMessage& message) this->bridge->send_xmpp_message(this->hostname, from, body); else { - IrcMessage modified_message(std::move(from), "PRIVMSG", {to, std::string("\u000303[notice]\u0003 ") + body}); + IrcMessage modified_message(std::move(from), "PRIVMSG", {to, "\u000303[notice]\u0003 "s + body}); this->on_channel_message(modified_message); } } @@ -344,7 +347,7 @@ void IrcClient::on_channel_message(const IrcMessage& message) { if (body.substr(1, 6) == "ACTION") this->bridge->send_message(iid, nick, - std::string("/me") + body.substr(7, body.size() - 8), muc); + "/me"s + body.substr(7, body.size() - 8), muc); } else this->bridge->send_message(iid, nick, body, muc); @@ -486,7 +489,7 @@ void IrcClient::on_error(const IrcMessage& message) this->bridge->send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true); } this->channels.clear(); - this->send_gateway_message(std::string("ERROR: ") + leave_message); + this->send_gateway_message("ERROR: "s + leave_message); } void IrcClient::on_quit(const IrcMessage& message) @@ -583,7 +586,7 @@ void IrcClient::on_channel_mode(const IrcMessage& message) mode_arguments += message.arguments[i]; } } - this->bridge->send_message(iid, "", std::string("Mode ") + iid.chan + + this->bridge->send_message(iid, "", "Mode "s + iid.chan + " [" + mode_arguments + "] by " + user.nick, true); const IrcChannel* channel = this->get_channel(iid.chan); @@ -661,7 +664,7 @@ void IrcClient::on_channel_mode(const IrcMessage& message) void IrcClient::on_user_mode(const IrcMessage& message) { this->bridge->send_xmpp_message(this->hostname, "", - std::string("User mode for ") + message.arguments[0] + + "User mode for "s + message.arguments[0] + " is [" + message.arguments[1] + "]"); } @@ -685,5 +688,5 @@ void IrcClient::leave_dummy_channel(const std::string& exit_message) this->dummy_channel.joined = false; this->dummy_channel.joining = false; this->dummy_channel.remove_all_users(); - this->bridge->send_muc_leave(Iid(std::string("%") + this->hostname), std::string(this->current_nick), exit_message, true); + this->bridge->send_muc_leave(Iid("%"s + this->hostname), std::string(this->current_nick), exit_message, true); } -- cgit v1.2.3 From 5ec05cb0edda6b01ff5c21a42edf9142b90399e5 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 6 May 2014 22:50:37 +0200 Subject: Forward CTCP version request to XMPP --- src/irc/irc_client.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index da5a947..5f70efb 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -348,6 +348,8 @@ void IrcClient::on_channel_message(const IrcMessage& message) if (body.substr(1, 6) == "ACTION") this->bridge->send_message(iid, nick, "/me"s + body.substr(7, body.size() - 8), muc); + else if (body.substr(1, 8) == "VERSION\01") + this->bridge->send_iq_version_request(nick, this->hostname); } else this->bridge->send_message(iid, nick, body, muc); -- cgit v1.2.3 From 579ca4bdb6b8806d821daa2ee47d60260b64f0f8 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 7 May 2014 02:02:47 +0200 Subject: Forward iq version results to IRC --- src/irc/irc_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 5f70efb..c149311 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -192,12 +192,12 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st return true; } -void IrcClient::send_private_message(const std::string& username, const std::string& body) +void IrcClient::send_private_message(const std::string& username, const std::string& body, const std::string& type) { std::string::size_type pos = 0; while (pos < body.size()) { - this->send_message(IrcMessage("PRIVMSG", {username, body.substr(pos, 400)})); + this->send_message(IrcMessage(std::string(type), {username, body.substr(pos, 400)})); pos += 400; } -- cgit v1.2.3 From 5507adbe9473f4b41e52d16498f14850773e5e45 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 27 May 2014 01:01:44 +0200 Subject: SocketHandlers own the poller and add themself into it only when the socket is created MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to call socket() with the parameters provided by getaddrinfo, so we can’t addd the fd into the poller immediately. We need to wait the connection attempt, and then the SocketHandler can call add_socket_handler itself, if the connection succeeds, or is in progress. --- src/irc/irc_client.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index c149311..cea08f2 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -13,7 +13,8 @@ #include using namespace std::string_literals; -IrcClient::IrcClient(const std::string& hostname, const std::string& username, Bridge* bridge): +IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, const std::string& username, Bridge* bridge): + SocketHandler(poller), hostname(hostname), username(username), current_nick(username), -- cgit v1.2.3 From 796af0531e0f5eb5fa2b800370338ede120a867d Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 27 May 2014 02:17:25 +0200 Subject: Add support for CHANTYPES isupport element, to know the prefixes of channels --- src/irc/irc_client.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index cea08f2..03402e7 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -20,7 +20,8 @@ IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname current_nick(username), bridge(bridge), welcomed(false), - chanmodes({"", "", "", ""}) + chanmodes({"", "", "", ""}), + chantypes({'#', '&'}) { this->dummy_channel.topic = "This is a virtual channel provided for " "convenience by biboumi, it is not connected " @@ -279,6 +280,15 @@ void IrcClient::on_isupport_message(const IrcMessage& message) this->prefix_to_mode[token[j++]] = token[i++]; } } + else if (token.substr(0, 10) == "CHANTYPES=") + { + // Remove the default types, they apply only if no other value is + // specified. + this->chantypes.clear(); + size_t i = 11; + while (i < token.size()) + this->chantypes.insert(token[i++]); + } } } @@ -564,8 +574,7 @@ void IrcClient::on_kick(const IrcMessage& message) void IrcClient::on_mode(const IrcMessage& message) { const std::string target = message.arguments[0]; - if (target[0] == '&' || target[0] == '#' || - target[0] == '!' || target[0] == '+') + if (this->chantypes.find(target[0]) != this->chantypes.end()) this->on_channel_mode(message); else this->on_user_mode(message); -- cgit v1.2.3 From aea923bcb99f8a7dee83202b035bc377cf835c65 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 27 May 2014 02:30:11 +0200 Subject: Use the CHANTYPES values to differentiate channel or user notices It also happens to fix #2517 because this used to create buggy channels named "auth" and stuf like that. --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 03402e7..1411689 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -245,7 +245,7 @@ void IrcClient::on_notice(const IrcMessage& message) const std::string to = message.arguments[0]; const std::string body = message.arguments[1]; - if (to == this->current_nick) + if (!to.empty() && this->chantypes.find(to[0]) == this->chantypes.end()) this->bridge->send_xmpp_message(this->hostname, from, body); else { -- cgit v1.2.3 From 34739728f930f461ae6763a5f144f709a9919e59 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 26 May 2014 18:34:56 +0200 Subject: Fix a by-one error in the CHANTYPES parsing --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 1411689..270e0ed 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -285,7 +285,7 @@ void IrcClient::on_isupport_message(const IrcMessage& message) // Remove the default types, they apply only if no other value is // specified. this->chantypes.clear(); - size_t i = 11; + size_t i = 10; while (i < token.size()) this->chantypes.insert(token[i++]); } -- cgit v1.2.3 From 6c2d03da4ea0443624b6bf434b6a654c12e48438 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 28 May 2014 01:27:41 +0200 Subject: Send an error presence when the connection to the IRC server fails --- src/irc/irc_client.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 270e0ed..f08e9d6 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -50,6 +50,13 @@ void IrcClient::on_connection_failed(const std::string& reason) { this->bridge->send_xmpp_message(this->hostname, "", "Connection failed: "s + reason); + // Send an error message for all room that the user wanted to join + for (const std::string& channel: this->channels_to_join) + { + Iid iid(channel + "%" + this->hostname); + this->bridge->send_join_failed(iid, this->current_nick, + "cancel", "item-not-found", reason); + } } void IrcClient::on_connected() @@ -167,11 +174,11 @@ void IrcClient::send_quit_command(const std::string& reason) void IrcClient::send_join_command(const std::string& chan_name) { - this->start(); if (this->welcomed == false) this->channels_to_join.push_back(chan_name); else this->send_message(IrcMessage("JOIN", {chan_name})); + this->start(); } bool IrcClient::send_channel_message(const std::string& chan_name, const std::string& body) -- cgit v1.2.3 From c2311b2893f3db755b67c43e5ad60cef66b10ab2 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 30 May 2014 16:08:23 +0200 Subject: Send (every 240s) a PING command to all connected irc servers fix #2452 --- src/irc/irc_client.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index f08e9d6..01bf569 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -10,8 +11,11 @@ #include #include +#include #include + using namespace std::string_literals; +using namespace std::chrono_literals; IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, const std::string& username, Bridge* bridge): SocketHandler(poller), @@ -35,6 +39,9 @@ IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname IrcClient::~IrcClient() { + // This event may or may not exist (if we never got connected, it + // doesn't), but it's ok + TimedEventsManager::instance().cancel("PING"s + this->hostname + this->bridge->get_jid()); } void IrcClient::start() @@ -238,6 +245,11 @@ void IrcClient::send_pong_command(const IrcMessage& message) this->send_message(IrcMessage("PONG", {id})); } +void IrcClient::send_ping_command() +{ + this->send_message(IrcMessage("PING", {"biboumi"})); +} + void IrcClient::forward_server_message(const IrcMessage& message) { const std::string from = message.prefix; @@ -447,6 +459,9 @@ void IrcClient::on_welcome_message(const IrcMessage& message) { this->current_nick = message.arguments[0]; this->welcomed = true; + // Install a repeated events to regularly send a PING + TimedEventsManager::instance().add_event(TimedEvent(240s, std::bind(&IrcClient::send_ping_command, this), + "PING"s + this->hostname + this->bridge->get_jid())); for (const std::string& chan_name: this->channels_to_join) this->send_join_command(chan_name); this->channels_to_join.clear(); -- cgit v1.2.3 From 23f32ba39ebe5e9bbdfc4dd00d9914c0f0447ef4 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 18 May 2014 20:23:08 +0200 Subject: Implement TLS support using Botan For now, it tries two TLS ports and then connects to the non-tls port. In the future we would like the user to be able to configure that. fix #2435 --- src/irc/irc_client.cpp | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 01bf569..c3765a6 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -14,6 +14,8 @@ #include #include +#include "config.h" + using namespace std::string_literals; using namespace std::chrono_literals; @@ -35,6 +37,13 @@ IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname "alive without having to join a real channel of that server. " "To disconnect from the IRC server, leave this room and all " "other IRC channels of that server."; + // TODO: get the values from the preferences of the user, and only use the + // list of default ports if the user didn't specify anything + this->ports_to_try.emplace("6667", false); // standard non-encrypted port +#ifdef BOTAN_FOUND + this->ports_to_try.emplace("6670", true); // non-standard but I want it for some servers + this->ports_to_try.emplace("6697", true); // standard encrypted port +#endif // BOTAN_FOUND } IrcClient::~IrcClient() @@ -48,29 +57,39 @@ void IrcClient::start() { if (this->connected || this->connecting) return ; + std::string port; + bool tls; + std::tie(port, tls) = this->ports_to_try.top(); + this->ports_to_try.pop(); this->bridge->send_xmpp_message(this->hostname, "", "Connecting to "s + - this->hostname + ":" + "6667"); - this->connect(this->hostname, "6667"); + this->hostname + ":" + port + " (" + + (tls ? "encrypted" : "not encrypted") + ")"); + this->connect(this->hostname, port, tls); } void IrcClient::on_connection_failed(const std::string& reason) { this->bridge->send_xmpp_message(this->hostname, "", "Connection failed: "s + reason); - // Send an error message for all room that the user wanted to join - for (const std::string& channel: this->channels_to_join) + if (this->ports_to_try.empty()) { - Iid iid(channel + "%" + this->hostname); - this->bridge->send_join_failed(iid, this->current_nick, - "cancel", "item-not-found", reason); + // Send an error message for all room that the user wanted to join + for (const std::string& channel: this->channels_to_join) + { + Iid iid(channel + "%" + this->hostname); + this->bridge->send_join_failed(iid, this->current_nick, + "cancel", "item-not-found", reason); + } } + else // try the next port + this->start(); } void IrcClient::on_connected() { this->send_nick_command(this->username); this->send_user_command(this->username, this->username); - this->send_gateway_message("Connected to IRC server."); + this->send_gateway_message("Connected to IRC server"s + (this->use_tls ? " (encrypted)": "") + "."); this->send_pending_data(); } @@ -326,7 +345,6 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); if (user->nick != channel->get_self()->nick) { - log_debug("Adding user [" << nick << "] to chan " << chan_name); this->bridge->send_user_join(this->hostname, chan_name, user, user->get_most_significant_mode(this->sorted_user_modes), false); -- cgit v1.2.3 From 7c1a38999c2eebfbd0939c9f8f8f864f10d9bc9a Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 11 Jun 2014 02:37:11 +0200 Subject: Rewrite the whole IID usage IRC users and channels are now distinguished by the separator used in the IID (% or !). ref #2468 --- src/irc/irc_client.cpp | 61 +++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 25 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index c3765a6..6e22980 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -102,10 +102,11 @@ void IrcClient::on_connection_close() log_warning(message); } -IrcChannel* IrcClient::get_channel(const std::string& name) +IrcChannel* IrcClient::get_channel(const std::string& n) { - if (name.empty()) + if (n.empty()) return &this->dummy_channel; + const std::string name = utils::tolower(n); try { return this->channels.at(name).get(); @@ -382,15 +383,18 @@ void IrcClient::on_channel_message(const IrcMessage& message) const IrcUser user(message.prefix); const std::string nick = user.nick; Iid iid; - iid.chan = utils::tolower(message.arguments[0]); - iid.server = this->hostname; + iid.set_local(message.arguments[0]); + iid.set_server(this->hostname); const std::string body = message.arguments[1]; bool muc = true; - if (!this->get_channel(iid.chan)->joined) + if (!this->get_channel(iid.get_local())->joined) { - iid.chan = nick; + iid.is_user = true; + iid.set_local(nick); muc = false; } + else + iid.is_channel = true; if (!body.empty() && body[0] == '\01') { if (body.substr(1, 6) == "ACTION") @@ -460,8 +464,9 @@ void IrcClient::on_nickname_conflict(const IrcMessage& message) for (auto it = this->channels.begin(); it != this->channels.end(); ++it) { Iid iid; - iid.chan = it->first; - iid.server = this->hostname; + iid.set_local(it->first); + iid.set_server(this->hostname); + iid.is_channel = true; this->bridge->send_nickname_conflict_error(iid, nickname); } } @@ -499,7 +504,7 @@ void IrcClient::on_welcome_message(const IrcMessage& message) void IrcClient::on_part(const IrcMessage& message) { - const std::string chan_name = utils::tolower(message.arguments[0]); + const std::string chan_name = message.arguments[0]; IrcChannel* channel = this->get_channel(chan_name); if (!channel->joined) return ; @@ -512,13 +517,14 @@ void IrcClient::on_part(const IrcMessage& message) std::string nick = user->nick; channel->remove_user(user); Iid iid; - iid.chan = chan_name; - iid.server = this->hostname; + iid.set_local(chan_name); + iid.set_server(this->hostname); + iid.is_channel = true; bool self = channel->get_self()->nick == nick; if (self) { channel->joined = false; - this->channels.erase(chan_name); + this->channels.erase(utils::tolower(chan_name)); // channel pointer is now invalid channel = nullptr; } @@ -533,8 +539,9 @@ void IrcClient::on_error(const IrcMessage& message) for (auto it = this->channels.begin(); it != this->channels.end(); ++it) { Iid iid; - iid.chan = it->first; - iid.server = this->hostname; + iid.set_local(it->first); + iid.set_server(this->hostname); + iid.is_channel = true; IrcChannel* channel = it->second.get(); if (!channel->joined) continue; @@ -560,8 +567,9 @@ void IrcClient::on_quit(const IrcMessage& message) std::string nick = user->nick; channel->remove_user(user); Iid iid; - iid.chan = chan_name; - iid.server = this->hostname; + iid.set_local(chan_name); + iid.set_server(this->hostname); + iid.is_channel = true; this->bridge->send_muc_leave(std::move(iid), std::move(nick), txt, false); } } @@ -579,8 +587,9 @@ void IrcClient::on_nick(const IrcMessage& message) { std::string old_nick = user->nick; Iid iid; - iid.chan = chan_name; - iid.server = this->hostname; + iid.set_local(chan_name); + iid.set_server(this->hostname); + iid.is_channel = true; const bool self = channel->get_self()->nick == old_nick; const char user_mode = user->get_most_significant_mode(this->sorted_user_modes); this->bridge->send_nick_change(std::move(iid), old_nick, new_nick, user_mode, self); @@ -606,8 +615,9 @@ void IrcClient::on_kick(const IrcMessage& message) channel->joined = false; IrcUser author(message.prefix); Iid iid; - iid.chan = chan_name; - iid.server = this->hostname; + iid.set_local(chan_name); + iid.set_server(this->hostname); + iid.is_channel = true; this->bridge->kick_muc_user(std::move(iid), target, reason, author.nick); } @@ -625,8 +635,9 @@ void IrcClient::on_channel_mode(const IrcMessage& message) // For now, just transmit the modes so the user can know what happens // TODO, actually interprete the mode. Iid iid; - iid.chan = utils::tolower(message.arguments[0]); - iid.server = this->hostname; + iid.set_local(message.arguments[0]); + iid.set_server(this->hostname); + iid.is_channel = true; IrcUser user(message.prefix); std::string mode_arguments; for (size_t i = 1; i < message.arguments.size(); ++i) @@ -638,10 +649,10 @@ void IrcClient::on_channel_mode(const IrcMessage& message) mode_arguments += message.arguments[i]; } } - this->bridge->send_message(iid, "", "Mode "s + iid.chan + + this->bridge->send_message(iid, "", "Mode "s + iid.get_local() + " [" + mode_arguments + "] by " + user.nick, true); - const IrcChannel* channel = this->get_channel(iid.chan); + const IrcChannel* channel = this->get_channel(iid.get_local()); if (!channel) return; @@ -695,7 +706,7 @@ void IrcClient::on_channel_mode(const IrcMessage& message) if (!user) { log_warning("Trying to set mode for non-existing user '" << target - << "' in channel" << iid.chan); + << "' in channel" << iid.get_local()); return; } if (add) -- cgit v1.2.3 From a705b9af7b1bbce6b6c94788398a4cff9cad9ec9 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 19 Jun 2014 01:10:13 +0200 Subject: =?UTF-8?q?Remove=20a=20duplicate=20=E2=80=9Cconnection=20closed?= =?UTF-8?q?=E2=80=9D=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/irc/irc_client.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 6e22980..cc54971 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -96,7 +96,6 @@ void IrcClient::on_connected() void IrcClient::on_connection_close() { static const std::string message = "Connection closed by remote server."; - this->send_gateway_message(message); const IrcMessage error{"ERROR", {message}}; this->on_error(error); log_warning(message); -- cgit v1.2.3 From 26ffc8fe121e03e1b663aa0015a71b0fc914f95e Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 19 Jun 2014 22:21:49 +0200 Subject: Implement a way to add callbacks, waiting for an IRC event to return an iq --- src/irc/irc_client.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index cc54971..d179aaa 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -136,8 +136,11 @@ void IrcClient::parse_in_buffer(const size_t) if (pos == std::string::npos) break ; IrcMessage message(this->in_buf.substr(0, pos)); - log_debug("IRC RECEIVING: " << message); this->in_buf = this->in_buf.substr(pos + 2, std::string::npos); + log_debug("IRC RECEIVING: " << message); + + // Call the standard callback (if any), associated with the command + // name that we just received. auto cb = irc_callbacks.find(message.command); if (cb != irc_callbacks.end()) { @@ -149,6 +152,8 @@ void IrcClient::parse_in_buffer(const size_t) } else log_info("No handler for command " << message.command); + // Try to find a waiting_iq, which response will be triggered by this IrcMessage + this->bridge->trigger_response_iq(this->hostname, message); } } -- cgit v1.2.3 From 04d999168ac4629f5e49939f3659b32b2da2563d Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 2 Jul 2014 03:01:09 +0200 Subject: Add a level of inheritance above SocketHandler SocketHandler has been renamed to TCPSocketHandler SocketHandler is now a simple interface with a few methods, used only by Poller. This way we can inherite from the new SocketHandler class, to handle other types of sockets, and still make them manageable by the poller without any change in the Poller class. --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index d179aaa..6468094 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -20,7 +20,7 @@ using namespace std::string_literals; using namespace std::chrono_literals; IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, const std::string& username, Bridge* bridge): - SocketHandler(poller), + TCPSocketHandler(poller), hostname(hostname), username(username), current_nick(username), -- cgit v1.2.3 From 11a31db2d5bcc158bb8902e74f192dbc82827f53 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 15 Jul 2014 15:39:25 +0200 Subject: Send the reason of the connection close to the user --- src/irc/irc_client.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 6468094..e518ffc 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -93,9 +93,11 @@ void IrcClient::on_connected() this->send_pending_data(); } -void IrcClient::on_connection_close() +void IrcClient::on_connection_close(const std::string& error_msg) { - static const std::string message = "Connection closed by remote server."; + std::string message = "Connection closed by remote server."; + if (!error_msg.empty()) + message += ": " + error_msg; const IrcMessage error{"ERROR", {message}}; this->on_error(error); log_warning(message); -- cgit v1.2.3 From e3ea0d62c79cf74e154f24d7f5a0fa8c7d26d73b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 8 Aug 2014 00:46:27 +0200 Subject: Use generic send_presence_error() instead of almost identical specializations --- src/irc/irc_client.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e518ffc..678cac8 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -77,8 +77,9 @@ void IrcClient::on_connection_failed(const std::string& reason) for (const std::string& channel: this->channels_to_join) { Iid iid(channel + "%" + this->hostname); - this->bridge->send_join_failed(iid, this->current_nick, - "cancel", "item-not-found", reason); + this->bridge->send_presence_error(iid, this->current_nick, + "cancel", "item-not-found", + "", reason); } } else // try the next port -- cgit v1.2.3 From 41e8a3ba9b57e67aec5d0d30112338664afbd6e4 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 8 Aug 2014 00:47:05 +0200 Subject: Send a proper error on IRC message 438 (nickname change too fast) fix #2576 --- src/irc/irc_client.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 678cac8..f9ebfa0 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -478,6 +478,25 @@ void IrcClient::on_nickname_conflict(const IrcMessage& message) } } +void IrcClient::on_nickname_change_too_fast(const IrcMessage& message) +{ + const std::string nickname = message.arguments[1]; + std::string txt; + if (message.arguments.size() >= 3) + txt = message.arguments[2]; + this->on_generic_error(message); + for (auto it = this->channels.begin(); it != this->channels.end(); ++it) + { + Iid iid; + iid.set_local(it->first); + iid.set_server(this->hostname); + iid.is_channel = true; + this->bridge->send_presence_error(iid, nickname, + "cancel", "not-acceptable", + "", txt); + } +} + void IrcClient::on_generic_error(const IrcMessage& message) { const std::string error_msg = message.arguments.size() >= 3 ? -- cgit v1.2.3 From e1d69806ed7c92bdfe1bf632064bf68b3d1d266b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 12 Nov 2014 04:05:56 +0100 Subject: =?UTF-8?q?Rename=20iq=5Fresponder=5Fcallback=5Ft=20to=20irc=5F?= =?UTF-8?q?=E2=80=A6=20and=20add=20the=20equivalent=20to=20wait=20for=20iq?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index f9ebfa0..90f0644 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -156,7 +156,7 @@ void IrcClient::parse_in_buffer(const size_t) else log_info("No handler for command " << message.command); // Try to find a waiting_iq, which response will be triggered by this IrcMessage - this->bridge->trigger_response_iq(this->hostname, message); + this->bridge->trigger_on_irc_message(this->hostname, message); } } -- cgit v1.2.3 From d8da7984b23bf8f30b5f8f53895cbae5be50347a Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 12 Nov 2014 06:16:10 +0100 Subject: Implement PING, user to user only (XMPP and IRC side, using CTCP PING) ref #2757 --- src/irc/irc_client.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 90f0644..707593f 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -409,6 +409,9 @@ void IrcClient::on_channel_message(const IrcMessage& message) "/me"s + body.substr(7, body.size() - 8), muc); else if (body.substr(1, 8) == "VERSION\01") this->bridge->send_iq_version_request(nick, this->hostname); + else if (body.substr(1, 5) == "PING ") + this->bridge->send_xmpp_ping_request(nick, this->hostname, + body.substr(6, body.size() - 7)); } else this->bridge->send_message(iid, nick, body, muc); -- cgit v1.2.3 From b79dbefbe71824d8d42c5034a6900644a0850c4c Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 12 Jan 2015 22:50:06 +0100 Subject: If we sent a message to a user, their notices are considered private messages fix #2882 --- src/irc/irc_client.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 707593f..228fc6e 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -243,7 +243,9 @@ void IrcClient::send_private_message(const std::string& username, const std::str this->send_message(IrcMessage(std::string(type), {username, body.substr(pos, 400)})); pos += 400; } - + // We always try to insert and we don't care if the username was already + // in the set. + this->nicks_to_treat_as_private.insert(username); } void IrcClient::send_part_command(const std::string& chan_name, const std::string& status_message) @@ -292,9 +294,30 @@ void IrcClient::on_notice(const IrcMessage& message) const std::string body = message.arguments[1]; if (!to.empty() && this->chantypes.find(to[0]) == this->chantypes.end()) - this->bridge->send_xmpp_message(this->hostname, from, body); + { + // The notice is for the us precisely. + + // Find out if we already sent a private message to this user. If yes + // we treat that message as a private message coming from + // it. Otherwise we treat it as a notice coming from the server. + IrcUser user(from); + std::string nick = utils::tolower(user.nick); + log_debug("received notice from nick: " << nick); + if (this->nicks_to_treat_as_private.find(nick) != + this->nicks_to_treat_as_private.end()) + { // We previously sent a message to that nick) + // this->bridge->send_message(iid, nick, body, muc); + this->bridge->send_message({nick + "!" + this->hostname}, nick, body, + false); + } + else + this->bridge->send_xmpp_message(this->hostname, from, body); + } else { + // The notice was directed at a channel we are in. Modify the message + // to indicate that it is a notice, and make it a MUC message coming + // from the MUC JID IrcMessage modified_message(std::move(from), "PRIVMSG", {to, "\u000303[notice]\u0003 "s + body}); this->on_channel_message(modified_message); } -- cgit v1.2.3 From e2e2f3089469e3e2acbdf1ac6902241d994057c6 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 12 Jan 2015 22:57:08 +0100 Subject: Remove a dummy commented line --- src/irc/irc_client.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 228fc6e..b892684 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -306,7 +306,6 @@ void IrcClient::on_notice(const IrcMessage& message) if (this->nicks_to_treat_as_private.find(nick) != this->nicks_to_treat_as_private.end()) { // We previously sent a message to that nick) - // this->bridge->send_message(iid, nick, body, muc); this->bridge->send_message({nick + "!" + this->hostname}, nick, body, false); } -- cgit v1.2.3 From e4fcbd3030f033c24102db9f6b6abfb540332c9d Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 14 Jan 2015 12:38:46 +0100 Subject: Add support for password-protected IRC rooms. --- src/irc/irc_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index b892684..a29fb0a 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -206,12 +206,12 @@ void IrcClient::send_quit_command(const std::string& reason) this->send_message(IrcMessage("QUIT", {reason})); } -void IrcClient::send_join_command(const std::string& chan_name) +void IrcClient::send_join_command(const std::string& chan_name, const std::string& password) { if (this->welcomed == false) this->channels_to_join.push_back(chan_name); else - this->send_message(IrcMessage("JOIN", {chan_name})); + this->send_message(IrcMessage("JOIN", {chan_name, password})); this->start(); } -- cgit v1.2.3 From 60569993b4532caffd8b1a6646292efbbd933585 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 14 Jan 2015 13:48:34 +0100 Subject: Make the password work when we join our first channel on that server Because we need to wait for the welcome message, when we connect to the server, before sending the JOIN command, we need to also save the value of the password to reuse it when we actually send the JOIN command --- src/irc/irc_client.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index a29fb0a..12f39d6 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -74,9 +74,9 @@ void IrcClient::on_connection_failed(const std::string& reason) if (this->ports_to_try.empty()) { // Send an error message for all room that the user wanted to join - for (const std::string& channel: this->channels_to_join) + for (const auto& tuple: this->channels_to_join) { - Iid iid(channel + "%" + this->hostname); + Iid iid(std::get<0>(tuple) + "%" + this->hostname); this->bridge->send_presence_error(iid, this->current_nick, "cancel", "item-not-found", "", reason); @@ -209,7 +209,7 @@ void IrcClient::send_quit_command(const std::string& reason) void IrcClient::send_join_command(const std::string& chan_name, const std::string& password) { if (this->welcomed == false) - this->channels_to_join.push_back(chan_name); + this->channels_to_join.emplace_back(chan_name, password); else this->send_message(IrcMessage("JOIN", {chan_name, password})); this->start(); @@ -536,8 +536,8 @@ void IrcClient::on_welcome_message(const IrcMessage& message) // Install a repeated events to regularly send a PING TimedEventsManager::instance().add_event(TimedEvent(240s, std::bind(&IrcClient::send_ping_command, this), "PING"s + this->hostname + this->bridge->get_jid())); - for (const std::string& chan_name: this->channels_to_join) - this->send_join_command(chan_name); + for (const auto& tuple: this->channels_to_join) + this->send_join_command(std::get<0>(tuple), std::get<1>(tuple)); this->channels_to_join.clear(); // Indicate that the dummy channel is joined as well, if needed if (this->dummy_channel.joining) -- cgit v1.2.3 From 4203384e2adcc57ddaf0ede6ca7745fb4a828c44 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 14 Jan 2015 13:53:25 +0100 Subject: Do not send an empty password at all, if the user didn't provide one --- src/irc/irc_client.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 12f39d6..2549138 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -210,6 +210,8 @@ void IrcClient::send_join_command(const std::string& chan_name, const std::strin { if (this->welcomed == false) this->channels_to_join.emplace_back(chan_name, password); + else if (password.empty()) + this->send_message(IrcMessage("JOIN", {chan_name})); else this->send_message(IrcMessage("JOIN", {chan_name, password})); this->start(); -- cgit v1.2.3 From a447214f10e894a7de827c3ff10c185280ed2538 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 14 Jan 2015 13:56:27 +0100 Subject: Remove a recent debug line that should not be there --- src/irc/irc_client.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 2549138..ee8d2d8 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -304,7 +304,6 @@ void IrcClient::on_notice(const IrcMessage& message) // it. Otherwise we treat it as a notice coming from the server. IrcUser user(from); std::string nick = utils::tolower(user.nick); - log_debug("received notice from nick: " << nick); if (this->nicks_to_treat_as_private.find(nick) != this->nicks_to_treat_as_private.end()) { // We previously sent a message to that nick) -- cgit v1.2.3 From 1c43c3af4cbefcd482f4145ee3d7553631a7485d Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 25 Feb 2015 16:37:31 +0100 Subject: Include the IRC hostname in the IRC RECEIVING and SENDING debug messages fix #2715 --- src/irc/irc_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index ee8d2d8..f83b48c 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -140,7 +140,7 @@ void IrcClient::parse_in_buffer(const size_t) break ; IrcMessage message(this->in_buf.substr(0, pos)); this->in_buf = this->in_buf.substr(pos + 2, std::string::npos); - log_debug("IRC RECEIVING: " << message); + log_debug("IRC RECEIVING: (" << this->get_hostname() << ") " << message); // Call the standard callback (if any), associated with the command // name that we just received. @@ -162,7 +162,7 @@ void IrcClient::parse_in_buffer(const size_t) void IrcClient::send_message(IrcMessage&& message) { - log_debug("IRC SENDING: " << message); + log_debug("IRC SENDING: (" << this->get_hostname() << ") " << message); std::string res; if (!message.prefix.empty()) res += ":" + std::move(message.prefix) + " "; -- cgit v1.2.3 From c01befb054075ab414fd602859e5999a138aa5bf Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 26 Feb 2015 05:02:08 +0100 Subject: Implement room discovery using the LIST irc command ref #2472 --- src/irc/irc_client.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index f83b48c..dedb5de 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -196,6 +196,11 @@ void IrcClient::send_kick_command(const std::string& chan_name, const std::strin this->send_message(IrcMessage("KICK", {chan_name, target, reason})); } +void IrcClient::send_list_command() +{ + this->send_message(IrcMessage("LIST", {})); +} + void IrcClient::send_topic_command(const std::string& chan_name, const std::string& topic) { this->send_message(IrcMessage("TOPIC", {chan_name, topic})); -- cgit v1.2.3 From d600a2843f1dbe3b1ba2dead9a020cc73d7d10ae Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 27 Feb 2015 12:16:09 +0100 Subject: Remove all the libs that are now in louloulibs --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index dedb5de..cbbd4ea 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -14,7 +14,7 @@ #include #include -#include "config.h" +#include "louloulibs.h" using namespace std::string_literals; using namespace std::chrono_literals; -- cgit v1.2.3 From ffcce28c7711ff69e46445c466bd439362e3d0d4 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 24 Mar 2015 04:34:05 +0100 Subject: Do not log a warning when we receive a PONG command --- src/irc/irc_client.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index cbbd4ea..694baf8 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -281,6 +281,10 @@ void IrcClient::send_pong_command(const IrcMessage& message) this->send_message(IrcMessage("PONG", {id})); } +void IrcClient::on_pong(const IrcMessage& message) +{ +} + void IrcClient::send_ping_command() { this->send_message(IrcMessage("PING", {"biboumi"})); -- cgit v1.2.3 From a8225dc54c019788722bda3bda8d55151c1ccdef Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 21 Apr 2015 15:35:10 +0200 Subject: Properly check for connecting or connected status before reconnecting Note, in our context, is_connecting() includes the resolving part as well as the actual connection (if we are using c-ares) fix #3048 --- src/irc/irc_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 694baf8..b0ce93a 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -55,8 +55,8 @@ IrcClient::~IrcClient() void IrcClient::start() { - if (this->connected || this->connecting) - return ; + if (this->is_connecting() || this->is_connected()) + return; std::string port; bool tls; std::tie(port, tls) = this->ports_to_try.top(); -- cgit v1.2.3 From 0a6b673b14efc4f623ea445045e6fc60e9842a25 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 7 May 2015 17:01:17 +0200 Subject: Support raw IRC messages Messages received on an IRC server JID are forwarded as raw IRC messages. fix #2486 --- src/irc/irc_client.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index b0ce93a..717f7e3 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -181,6 +181,11 @@ void IrcClient::send_message(IrcMessage&& message) this->send_data(std::move(res)); } +void IrcClient::send_raw(const std::string& txt) +{ + this->send_data(txt + "\r\n"); +} + void IrcClient::send_user_command(const std::string& username, const std::string& realname) { this->send_message(IrcMessage("USER", {username, "ignored", "ignored", realname})); -- cgit v1.2.3 From 163ace553f3e65dcf9f97070faa5dbab0d31bac0 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 7 May 2015 17:17:01 +0200 Subject: Handle all unknown IRC command by forwarding the arguments as a message body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This way, the users can receive the result of any IRC command (although not parsed nor formatted in anyway) when biboumi doesn’t support it fix #2884 --- src/irc/irc_client.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 717f7e3..677f6be 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -154,7 +155,11 @@ void IrcClient::parse_in_buffer(const size_t) } } else - log_info("No handler for command " << message.command); + { + log_info("No handler for command " << message.command << + ", forwarding the arguments to the user"); + this->on_unknown_message(message); + } // Try to find a waiting_iq, which response will be triggered by this IrcMessage this->bridge->trigger_on_irc_message(this->hostname, message); } @@ -797,6 +802,22 @@ void IrcClient::on_user_mode(const IrcMessage& message) " is [" + message.arguments[1] + "]"); } +void IrcClient::on_unknown_message(const IrcMessage& message) +{ + if (message.arguments.size() < 2) + return ; + std::string from = message.prefix; + const std::string to = message.arguments[0]; + std::stringstream ss; + for (auto it = message.arguments.begin() + 1; it != message.arguments.end(); ++it) + { + ss << *it; + if (it + 1 != message.arguments.end()) + ss << " "; + } + this->bridge->send_xmpp_message(this->hostname, from, ss.str()); +} + size_t IrcClient::number_of_joined_channels() const { if (this->dummy_channel.joined) -- cgit v1.2.3 From 221ed2553dcc5c43bfeb71f97e86147735d77856 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 7 May 2015 17:18:40 +0200 Subject: Two trivial fixes --- src/irc/irc_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 677f6be..7dcda8e 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -291,7 +291,7 @@ void IrcClient::send_pong_command(const IrcMessage& message) this->send_message(IrcMessage("PONG", {id})); } -void IrcClient::on_pong(const IrcMessage& message) +void IrcClient::on_pong(const IrcMessage&) { } @@ -316,7 +316,7 @@ void IrcClient::on_notice(const IrcMessage& message) if (!to.empty() && this->chantypes.find(to[0]) == this->chantypes.end()) { - // The notice is for the us precisely. + // The notice is for us precisely. // Find out if we already sent a private message to this user. If yes // we treat that message as a private message coming from -- cgit v1.2.3 From 7fe0dc2667229287ab9f1825e84403a25bc863fe Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 11 May 2015 05:31:11 +0200 Subject: Fix the case of the nick for ping requests fix #3041 --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 7dcda8e..5f6aaaf 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -452,7 +452,7 @@ void IrcClient::on_channel_message(const IrcMessage& message) else if (body.substr(1, 8) == "VERSION\01") this->bridge->send_iq_version_request(nick, this->hostname); else if (body.substr(1, 5) == "PING ") - this->bridge->send_xmpp_ping_request(nick, this->hostname, + this->bridge->send_xmpp_ping_request(utils::tolower(nick), this->hostname, body.substr(6, body.size() - 7)); } else -- cgit v1.2.3 From fa466f33f4c8009e69fe0ebf31c7eef1c394377b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 19 May 2015 06:00:07 +0200 Subject: Ignore commands that flood the user with private messages when listing chans ref #2472 --- src/irc/irc_client.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 5f6aaaf..d048e47 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -459,6 +459,18 @@ void IrcClient::on_channel_message(const IrcMessage& message) this->bridge->send_message(iid, nick, body, muc); } +void IrcClient::on_rpl_liststart(const IrcMessage&) +{ +} + +void IrcClient::on_rpl_list(const IrcMessage&) +{ +} + +void IrcClient::on_rpl_listend(const IrcMessage&) +{ +} + void IrcClient::empty_motd(const IrcMessage& message) { (void)message; -- cgit v1.2.3 From e454c12bd7f695d6450dc804c07aa5637a8b1c00 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 19 May 2015 06:43:38 +0200 Subject: =?UTF-8?q?Do=20not=20forward=20CTCP=20commands=20(PING,=20VERSION?= =?UTF-8?q?=E2=80=A6)=20to=20the=20user=20as=20private=20messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some XMPP client ping themselves inside MUCs, to know if they are still in there, this created a flood of PING message in private. If the user is interested in knowing when they receive a ping or version request, they can still read their XML logs --- src/irc/irc_client.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index d048e47..905a336 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -314,6 +314,10 @@ void IrcClient::on_notice(const IrcMessage& message) const std::string to = message.arguments[0]; const std::string body = message.arguments[1]; + if (!body.empty() && body[0] == '\01' && body[body.size() - 1] == '\01') + // Do not forward the notice to the user if it's a CTCP command + return ; + if (!to.empty() && this->chantypes.find(to[0]) == this->chantypes.end()) { // The notice is for us precisely. -- cgit v1.2.3 From ece4b4969b296a3da010fb22768348650e70962d Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 9 Jul 2015 15:30:24 +0200 Subject: If hostname resolution fails, do not try all possible ports --- src/irc/irc_client.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 905a336..4e8385c 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -72,6 +72,11 @@ void IrcClient::on_connection_failed(const std::string& reason) { this->bridge->send_xmpp_message(this->hostname, "", "Connection failed: "s + reason); + + if (this->hostname_resolution_failed) + while (!this->ports_to_try.empty()) + this->ports_to_try.pop(); + if (this->ports_to_try.empty()) { // Send an error message for all room that the user wanted to join -- cgit v1.2.3 From 4cfcc79114d89096219039104674d35ca1aba5ca Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 6 Sep 2015 18:55:48 +0200 Subject: Check the number of argument of every IRC command received from the server Each IrcClient callback has a max and min size of argument, we call the callback only if the parsed message has a correct number of arguments, otherwise it is ignored (with a warning logged). --- src/irc/irc_client.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 4e8385c..6ab19b7 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -150,14 +150,26 @@ void IrcClient::parse_in_buffer(const size_t) // Call the standard callback (if any), associated with the command // name that we just received. - auto cb = irc_callbacks.find(message.command); - if (cb != irc_callbacks.end()) + auto it = irc_callbacks.find(message.command); + if (it != irc_callbacks.end()) { - try { - (this->*(cb->second))(message); - } catch (const std::exception& e) { - log_error("Unhandled exception: " << e.what()); - } + const auto& limits = it->second.second; + // Check that the Message is well formed before actually calling + // the callback. limits.first is the min number of arguments, + // second is the max + if (message.arguments.size() < limits.first || + (limits.second > 0 && message.arguments.size() > limits.second)) + log_warning("Invalid number of arguments for IRC command “" << message.command << + "”: " << message.arguments.size()); + else + { + const auto& cb = it->second.first; + try { + (this->*(cb))(message); + } catch (const std::exception& e) { + log_error("Unhandled exception: " << e.what()); + } + } } else { -- cgit v1.2.3 From 73573ebb2abb2aea119a6c99e2bf4a302f2ba834 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 6 Sep 2015 18:57:35 +0200 Subject: Trivial cleanup in irc_client.cpp --- src/irc/irc_client.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 6ab19b7..29f0b54 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -492,9 +492,8 @@ void IrcClient::on_rpl_listend(const IrcMessage&) { } -void IrcClient::empty_motd(const IrcMessage& message) +void IrcClient::empty_motd(const IrcMessage&) { - (void)message; this->motd.erase(); } @@ -507,16 +506,13 @@ void IrcClient::on_motd_line(const IrcMessage& message) this->motd += body+"\n"; } -void IrcClient::send_motd(const IrcMessage& message) +void IrcClient::send_motd(const IrcMessage&) { - (void)message; this->bridge->send_xmpp_message(this->hostname, "", this->motd); } void IrcClient::on_topic_received(const IrcMessage& message) { - if (message.arguments.size() < 2) - return; const std::string chan_name = utils::tolower(message.arguments[message.arguments.size() - 2]); IrcChannel* channel = this->get_channel(chan_name); channel->topic = message.arguments[message.arguments.size() - 1]; @@ -709,9 +705,9 @@ void IrcClient::on_nick(const IrcMessage& message) void IrcClient::on_kick(const IrcMessage& message) { + const std::string chan_name = utils::tolower(message.arguments[0]); const std::string target = message.arguments[1]; const std::string reason = message.arguments[2]; - const std::string chan_name = utils::tolower(message.arguments[0]); IrcChannel* channel = this->get_channel(chan_name); if (!channel->joined) return ; -- cgit v1.2.3 From 532228a3cefd92fe43ad0f52149b7f0f5ab5cb79 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 18 Sep 2015 22:02:01 +0200 Subject: =?UTF-8?q?Send=20a=20PASS=20IRC=20command=20if=20the=20=E2=80=9Cp?= =?UTF-8?q?ass=E2=80=9D=20config=20is=20sot=20by=20a=20user,=20on=20an=20I?= =?UTF-8?q?RC=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #3068 --- src/irc/irc_client.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 29f0b54..bac3e34 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -94,6 +95,12 @@ void IrcClient::on_connection_failed(const std::string& reason) void IrcClient::on_connected() { +#ifdef USE_DATABASE + auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), + this->get_hostname()); + if (!options.pass.value().empty()) + this->send_pass_command(options.pass.value()); +#endif this->send_nick_command(this->username); this->send_user_command(this->username, this->username); this->send_gateway_message("Connected to IRC server"s + (this->use_tls ? " (encrypted)": "") + "."); @@ -218,6 +225,11 @@ void IrcClient::send_nick_command(const std::string& nick) this->send_message(IrcMessage("NICK", {nick})); } +void IrcClient::send_pass_command(const std::string& password) +{ + this->send_message(IrcMessage("PASS", {password})); +} + void IrcClient::send_kick_command(const std::string& chan_name, const std::string& target, const std::string& reason) { this->send_message(IrcMessage("KICK", {chan_name, target, reason})); -- cgit v1.2.3 From 890cfe90996ac4c3916c84d178049d9b3b23465b Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 21 Sep 2015 04:27:23 +0200 Subject: Provide Ports and TLS Ports IRC-server ad-hoc options This let any user choose which ports to use when connecting to the IRC server. This also lets the user choose whether or not to force TLS usage (by setting no non-TLS port). fix #2731 --- src/irc/irc_client.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index bac3e34..c6174bf 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -16,11 +16,13 @@ #include #include +#include "biboumi.h" #include "louloulibs.h" using namespace std::string_literals; using namespace std::chrono_literals; + IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, const std::string& username, Bridge* bridge): TCPSocketHandler(poller), hostname(hostname), @@ -39,13 +41,25 @@ IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname "alive without having to join a real channel of that server. " "To disconnect from the IRC server, leave this room and all " "other IRC channels of that server."; - // TODO: get the values from the preferences of the user, and only use the - // list of default ports if the user didn't specify anything +#ifdef USE_DATABASE + auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), + this->get_hostname()); + std::vector ports = utils::split(options.ports, ';', false); + for (auto it = ports.rbegin(); it != ports.rend(); ++it) + this->ports_to_try.emplace(*it, false); +# ifdef BOTAN_FOUND + ports = utils::split(options.tlsPorts, ';', false); + for (auto it = ports.rbegin(); it != ports.rend(); ++it) + this->ports_to_try.emplace(*it, true); +# endif // BOTAN_FOUND + +#else // not USE_DATABASE this->ports_to_try.emplace("6667", false); // standard non-encrypted port -#ifdef BOTAN_FOUND +# ifdef BOTAN_FOUND this->ports_to_try.emplace("6670", true); // non-standard but I want it for some servers this->ports_to_try.emplace("6697", true); // standard encrypted port -#endif // BOTAN_FOUND +# endif // BOTAN_FOUND +#endif // USE_DATABASE } IrcClient::~IrcClient() -- cgit v1.2.3 From ceec98907776dcd73b0c02a46ca135196e5f223e Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 21 Sep 2015 18:56:37 +0200 Subject: Add a field (in the configure form) to specifiy an after-connect IRC command --- src/irc/irc_client.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index c6174bf..26f9a46 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -226,6 +226,7 @@ void IrcClient::send_message(IrcMessage&& message) void IrcClient::send_raw(const std::string& txt) { + log_debug("IRC SENDING (raw): (" << this->get_hostname() << ") " << txt); this->send_data(txt + "\r\n"); } @@ -608,6 +609,12 @@ void IrcClient::on_welcome_message(const IrcMessage& message) { this->current_nick = message.arguments[0]; this->welcomed = true; +#ifdef USE_DATABASE + auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), + this->get_hostname()); + if (!options.afterConnectionCommand.value().empty()) + this->send_raw(options.afterConnectionCommand.value()); +#endif // Install a repeated events to regularly send a PING TimedEventsManager::instance().add_event(TimedEvent(240s, std::bind(&IrcClient::send_ping_command, this), "PING"s + this->hostname + this->bridge->get_jid())); -- cgit v1.2.3 From 7247228e9785fd7c8d796d4aa0eb3b9c6fc8f221 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 22 Sep 2015 03:50:08 +0200 Subject: Connection may be closed from our side too --- src/irc/irc_client.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 26f9a46..b4df7dd 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -123,9 +123,11 @@ void IrcClient::on_connected() void IrcClient::on_connection_close(const std::string& error_msg) { - std::string message = "Connection closed by remote server."; + std::string message = "Connection closed"; if (!error_msg.empty()) message += ": " + error_msg; + else + message += "."; const IrcMessage error{"ERROR", {message}}; this->on_error(error); log_warning(message); -- cgit v1.2.3 From f904d57940699e332f75a77062912431c6e51188 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 23 Sep 2015 18:24:06 +0200 Subject: Provide username and realname IRC server options Used in the USER command when connecting to the IRC server, instead of the first nick. fix #3028 --- src/irc/irc_client.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index b4df7dd..00d9f43 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -116,7 +116,17 @@ void IrcClient::on_connected() this->send_pass_command(options.pass.value()); #endif this->send_nick_command(this->username); +#ifdef USE_DATABASE + std::string username = this->username; + if (!options.username.value().empty()) + username = options.username.value(); + std::string realname = this->username; + if (!options.realname.value().empty()) + realname = options.realname.value(); + this->send_user_command(username, realname); +#else this->send_user_command(this->username, this->username); +#endif // USE_DATABASE this->send_gateway_message("Connected to IRC server"s + (this->use_tls ? " (encrypted)": "") + "."); this->send_pending_data(); } -- cgit v1.2.3 From 6512f830fa2baa85a7c688843ebd42eb528e2ad6 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 12 Oct 2015 16:35:50 +0200 Subject: The realname is also saved as an IrcClient member --- src/irc/irc_client.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 00d9f43..4692eef 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -27,6 +27,7 @@ IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname TCPSocketHandler(poller), hostname(hostname), username(username), + realname(username), current_nick(username), bridge(bridge), welcomed(false), @@ -115,18 +116,18 @@ void IrcClient::on_connected() if (!options.pass.value().empty()) this->send_pass_command(options.pass.value()); #endif + this->send_nick_command(this->username); + #ifdef USE_DATABASE - std::string username = this->username; if (!options.username.value().empty()) - username = options.username.value(); - std::string realname = this->username; + this->username = options.username.value(); if (!options.realname.value().empty()) - realname = options.realname.value(); + this->realname = options.realname.value(); this->send_user_command(username, realname); -#else - this->send_user_command(this->username, this->username); -#endif // USE_DATABASE +#endif + this->send_user_command(this->username, this->realname); + this->send_gateway_message("Connected to IRC server"s + (this->use_tls ? " (encrypted)": "") + "."); this->send_pending_data(); } -- cgit v1.2.3 From 84aafab6040f8fd126e6c4941631d44f122c4b9a Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 12 Oct 2015 16:47:27 +0200 Subject: =?UTF-8?q?Provide=20the=20=E2=80=9Crealname=5Fcustomization?= =?UTF-8?q?=E2=80=9D=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ref #3136 --- src/irc/irc_client.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 4692eef..d6c7021 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -120,11 +121,14 @@ void IrcClient::on_connected() this->send_nick_command(this->username); #ifdef USE_DATABASE - if (!options.username.value().empty()) - this->username = options.username.value(); - if (!options.realname.value().empty()) - this->realname = options.realname.value(); - this->send_user_command(username, realname); + if (Config::get("realname_customization", "true") == "true") + { + if (!options.username.value().empty()) + this->username = options.username.value(); + if (!options.realname.value().empty()) + this->realname = options.realname.value(); + this->send_user_command(username, realname); + } #endif this->send_user_command(this->username, this->realname); -- cgit v1.2.3 From 1aa2c2d857037f3274297527ca3971a75203d39c Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 12 Oct 2015 17:14:29 +0200 Subject: Introduce the realname_from_jid option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When set to true, the realname and username are extracted (by default) from the user’s JID fix #3136 --- src/irc/irc_client.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index d6c7021..0289d72 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -24,12 +24,14 @@ using namespace std::string_literals; using namespace std::chrono_literals; -IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, const std::string& username, Bridge* bridge): +IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, + const std::string& nickname, const std::string& username, + const std::string& realname, Bridge* bridge): TCPSocketHandler(poller), hostname(hostname), username(username), - realname(username), - current_nick(username), + realname(realname), + current_nick(nickname), bridge(bridge), welcomed(false), chanmodes({"", "", "", ""}), -- cgit v1.2.3 From 142516a69bb000ce80cbb6509d1f407438a94663 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 26 Oct 2015 20:09:39 +0100 Subject: Fix some trivial issues reported by cppcheck --- src/irc/irc_client.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0289d72..8f92f9b 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -888,7 +888,6 @@ void IrcClient::on_unknown_message(const IrcMessage& message) if (message.arguments.size() < 2) return ; std::string from = message.prefix; - const std::string to = message.arguments[0]; std::stringstream ss; for (auto it = message.arguments.begin() + 1; it != message.arguments.end(); ++it) { -- cgit v1.2.3 From 2c932cf0f7ca9bc82430c1da5097653f6a4d0bf4 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 31 Oct 2015 05:36:33 +0100 Subject: Fix the double sending of the USER command --- src/irc/irc_client.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 8f92f9b..f6b8f9c 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -131,9 +131,11 @@ void IrcClient::on_connected() this->realname = options.realname.value(); this->send_user_command(username, realname); } -#endif + else + this->send_user_command(this->username, this->realname); +#else this->send_user_command(this->username, this->realname); - +#endif this->send_gateway_message("Connected to IRC server"s + (this->use_tls ? " (encrypted)": "") + "."); this->send_pending_data(); } -- cgit v1.2.3 From 34fc1d4010d23be947c00fc956f2bdded2374cee Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sat, 31 Oct 2015 06:17:35 +0100 Subject: Implement a basic webirc support See https://kiwiirc.com/docs/webirc fix #3135 --- src/irc/irc_client.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index f6b8f9c..3161b85 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -26,9 +26,11 @@ using namespace std::chrono_literals; IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, const std::string& nickname, const std::string& username, - const std::string& realname, Bridge* bridge): + const std::string& realname, const std::string& user_hostname, + Bridge* bridge): TCPSocketHandler(poller), hostname(hostname), + user_hostname(user_hostname), username(username), realname(realname), current_nick(nickname), @@ -113,6 +115,39 @@ void IrcClient::on_connection_failed(const std::string& reason) void IrcClient::on_connected() { + const auto webirc_password = Config::get("webirc_password", ""); + static std::string resolved_ip; + + if (!webirc_password.empty()) + { + if (!resolved_ip.empty()) + this->send_webirc_command(webirc_password, resolved_ip); + else + { // Start resolving the hostname of the user, and call + // on_connected again when it’s done + this->dns_resolver.resolve(this->user_hostname, "5222", + [this](const struct addrinfo* addr) + { + resolved_ip = addr_to_string(addr); + // Only continue the process if we + // didn’t get connected while we were + // resolving + if (this->is_connected()) + this->on_connected(); + }, + [this](const char* error_msg) + { + if (this->is_connected()) + { + this->on_connection_close("Could not resolve hostname "s + this->user_hostname + + ": " + error_msg); + this->send_quit_command(""); + } + }); + return; + } + } + #ifdef USE_DATABASE auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), this->get_hostname()); @@ -253,7 +288,7 @@ void IrcClient::send_raw(const std::string& txt) void IrcClient::send_user_command(const std::string& username, const std::string& realname) { - this->send_message(IrcMessage("USER", {username, "ignored", "ignored", realname})); + this->send_message(IrcMessage("USER", {username, this->user_hostname, "ignored", realname})); } void IrcClient::send_nick_command(const std::string& nick) @@ -266,6 +301,11 @@ void IrcClient::send_pass_command(const std::string& password) this->send_message(IrcMessage("PASS", {password})); } +void IrcClient::send_webirc_command(const std::string& password, const std::string& user_ip) +{ + this->send_message(IrcMessage("WEBIRC", {password, "biboumi", this->user_hostname, user_ip})); +} + void IrcClient::send_kick_command(const std::string& chan_name, const std::string& target, const std::string& reason) { this->send_message(IrcMessage("KICK", {chan_name, target, reason})); -- cgit v1.2.3 From 0a4041f7e9d30fae9b2bfcfaaf1dc3efb368f86f Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 2 Nov 2015 17:14:21 +0100 Subject: Fix the initial IRC nickname (was using realname, by mistake) --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 3161b85..7caf443 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -155,7 +155,7 @@ void IrcClient::on_connected() this->send_pass_command(options.pass.value()); #endif - this->send_nick_command(this->username); + this->send_nick_command(this->current_nick); #ifdef USE_DATABASE if (Config::get("realname_customization", "true") == "true") -- cgit v1.2.3 From e8386bd14e9783f0bef39bdf577545522e33e719 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 3 Nov 2015 16:56:38 +0100 Subject: Provide an adhoc option to let user pass the cert verif for some IRC servers --- src/irc/irc_client.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 7caf443..93ea2ae 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -962,3 +962,14 @@ void IrcClient::leave_dummy_channel(const std::string& exit_message) this->dummy_channel.remove_all_users(); this->bridge->send_muc_leave(Iid("%"s + this->hostname), std::string(this->current_nick), exit_message, true); } + +#ifdef BOTAN_FOUND +bool IrcClient::abort_on_invalid_cert() const +{ +#ifdef USE_DATABASE + auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), this->hostname); + return options.verifyCert.value(); +#endif + return true; +} +#endif -- cgit v1.2.3 From 241768836ddfb9e3609f987224cd821058fcc948 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 1 Dec 2015 16:08:40 +0100 Subject: Add the outgoing_bind option Lets the admin choose a local address to bind each outgoing (IRC) socket. --- src/irc/irc_client.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 93ea2ae..e53c540 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -86,6 +86,9 @@ void IrcClient::start() this->bridge->send_xmpp_message(this->hostname, "", "Connecting to "s + this->hostname + ":" + port + " (" + (tls ? "encrypted" : "not encrypted") + ")"); + + this->bind_addr = Config::get("outgoing_bind", ""); + this->connect(this->hostname, port, tls); } -- cgit v1.2.3 From 7e2427148e9023483f266cd3ac4e167d50320796 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 21 Dec 2015 20:45:40 +0100 Subject: =?UTF-8?q?Use=20references=20instead=20of=20raw=20pointer,=20to?= =?UTF-8?q?=20store=20the=20=E2=80=9Cparent=E2=80=9D=20object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In Bridge and IrcClient --- src/irc/irc_client.cpp | 74 +++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e53c540..4aa45ac 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -27,7 +27,7 @@ using namespace std::chrono_literals; IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, const std::string& nickname, const std::string& username, const std::string& realname, const std::string& user_hostname, - Bridge* bridge): + Bridge& bridge): TCPSocketHandler(poller), hostname(hostname), user_hostname(user_hostname), @@ -48,7 +48,7 @@ IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname "To disconnect from the IRC server, leave this room and all " "other IRC channels of that server."; #ifdef USE_DATABASE - auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), + auto options = Database::get_irc_server_options(this->bridge.get_bare_jid(), this->get_hostname()); std::vector ports = utils::split(options.ports, ';', false); for (auto it = ports.rbegin(); it != ports.rend(); ++it) @@ -72,7 +72,7 @@ IrcClient::~IrcClient() { // This event may or may not exist (if we never got connected, it // doesn't), but it's ok - TimedEventsManager::instance().cancel("PING"s + this->hostname + this->bridge->get_jid()); + TimedEventsManager::instance().cancel("PING"s + this->hostname + this->bridge.get_jid()); } void IrcClient::start() @@ -83,7 +83,7 @@ void IrcClient::start() bool tls; std::tie(port, tls) = this->ports_to_try.top(); this->ports_to_try.pop(); - this->bridge->send_xmpp_message(this->hostname, "", "Connecting to "s + + this->bridge.send_xmpp_message(this->hostname, "", "Connecting to "s + this->hostname + ":" + port + " (" + (tls ? "encrypted" : "not encrypted") + ")"); @@ -94,7 +94,7 @@ void IrcClient::start() void IrcClient::on_connection_failed(const std::string& reason) { - this->bridge->send_xmpp_message(this->hostname, "", + this->bridge.send_xmpp_message(this->hostname, "", "Connection failed: "s + reason); if (this->hostname_resolution_failed) @@ -107,7 +107,7 @@ void IrcClient::on_connection_failed(const std::string& reason) for (const auto& tuple: this->channels_to_join) { Iid iid(std::get<0>(tuple) + "%" + this->hostname); - this->bridge->send_presence_error(iid, this->current_nick, + this->bridge.send_presence_error(iid, this->current_nick, "cancel", "item-not-found", "", reason); } @@ -152,7 +152,7 @@ void IrcClient::on_connected() } #ifdef USE_DATABASE - auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), + auto options = Database::get_irc_server_options(this->bridge.get_bare_jid(), this->get_hostname()); if (!options.pass.value().empty()) this->send_pass_command(options.pass.value()); @@ -258,7 +258,7 @@ void IrcClient::parse_in_buffer(const size_t) this->on_unknown_message(message); } // Try to find a waiting_iq, which response will be triggered by this IrcMessage - this->bridge->trigger_on_irc_message(this->hostname, message); + this->bridge.trigger_on_irc_message(this->hostname, message); } } @@ -413,7 +413,7 @@ void IrcClient::forward_server_message(const IrcMessage& message) const std::string from = message.prefix; const std::string body = message.arguments[1]; - this->bridge->send_xmpp_message(this->hostname, from, body); + this->bridge.send_xmpp_message(this->hostname, from, body); } void IrcClient::on_notice(const IrcMessage& message) @@ -438,11 +438,11 @@ void IrcClient::on_notice(const IrcMessage& message) if (this->nicks_to_treat_as_private.find(nick) != this->nicks_to_treat_as_private.end()) { // We previously sent a message to that nick) - this->bridge->send_message({nick + "!" + this->hostname}, nick, body, + this->bridge.send_message({nick + "!" + this->hostname}, nick, body, false); } else - this->bridge->send_xmpp_message(this->hostname, from, body); + this->bridge.send_xmpp_message(this->hostname, from, body); } else { @@ -494,7 +494,7 @@ void IrcClient::on_isupport_message(const IrcMessage& message) void IrcClient::send_gateway_message(const std::string& message, const std::string& from) { - this->bridge->send_xmpp_message(this->hostname, from, message); + this->bridge.send_xmpp_message(this->hostname, from, message); } void IrcClient::set_and_forward_user_list(const IrcMessage& message) @@ -507,7 +507,7 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); if (user->nick != channel->get_self()->nick) { - this->bridge->send_user_join(this->hostname, chan_name, user, + this->bridge.send_user_join(this->hostname, chan_name, user, user->get_most_significant_mode(this->sorted_user_modes), false); } @@ -533,7 +533,7 @@ void IrcClient::on_channel_join(const IrcMessage& message) else { const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); - this->bridge->send_user_join(this->hostname, chan_name, user, + this->bridge.send_user_join(this->hostname, chan_name, user, user->get_most_significant_mode(this->sorted_user_modes), false); } @@ -559,16 +559,16 @@ void IrcClient::on_channel_message(const IrcMessage& message) if (!body.empty() && body[0] == '\01') { if (body.substr(1, 6) == "ACTION") - this->bridge->send_message(iid, nick, + this->bridge.send_message(iid, nick, "/me"s + body.substr(7, body.size() - 8), muc); else if (body.substr(1, 8) == "VERSION\01") - this->bridge->send_iq_version_request(nick, this->hostname); + this->bridge.send_iq_version_request(nick, this->hostname); else if (body.substr(1, 5) == "PING ") - this->bridge->send_xmpp_ping_request(utils::tolower(nick), this->hostname, + this->bridge.send_xmpp_ping_request(utils::tolower(nick), this->hostname, body.substr(6, body.size() - 7)); } else - this->bridge->send_message(iid, nick, body, muc); + this->bridge.send_message(iid, nick, body, muc); } void IrcClient::on_rpl_liststart(const IrcMessage&) @@ -599,7 +599,7 @@ void IrcClient::on_motd_line(const IrcMessage& message) void IrcClient::send_motd(const IrcMessage&) { - this->bridge->send_xmpp_message(this->hostname, "", this->motd); + this->bridge.send_xmpp_message(this->hostname, "", this->motd); } void IrcClient::on_topic_received(const IrcMessage& message) @@ -608,7 +608,7 @@ void IrcClient::on_topic_received(const IrcMessage& message) IrcChannel* channel = this->get_channel(chan_name); channel->topic = message.arguments[message.arguments.size() - 1]; if (channel->joined) - this->bridge->send_topic(this->hostname, chan_name, channel->topic); + this->bridge.send_topic(this->hostname, chan_name, channel->topic); } void IrcClient::on_channel_completely_joined(const IrcMessage& message) @@ -616,10 +616,10 @@ void IrcClient::on_channel_completely_joined(const IrcMessage& message) const std::string chan_name = utils::tolower(message.arguments[1]); IrcChannel* channel = this->get_channel(chan_name); channel->joined = true; - this->bridge->send_user_join(this->hostname, chan_name, channel->get_self(), + this->bridge.send_user_join(this->hostname, chan_name, channel->get_self(), channel->get_self()->get_most_significant_mode(this->sorted_user_modes), true); - this->bridge->send_topic(this->hostname, chan_name, channel->topic); + this->bridge.send_topic(this->hostname, chan_name, channel->topic); } void IrcClient::on_erroneous_nickname(const IrcMessage& message) @@ -639,7 +639,7 @@ void IrcClient::on_nickname_conflict(const IrcMessage& message) iid.set_local(it->first); iid.set_server(this->hostname); iid.is_channel = true; - this->bridge->send_nickname_conflict_error(iid, nickname); + this->bridge.send_nickname_conflict_error(iid, nickname); } } @@ -656,7 +656,7 @@ void IrcClient::on_nickname_change_too_fast(const IrcMessage& message) iid.set_local(it->first); iid.set_server(this->hostname); iid.is_channel = true; - this->bridge->send_presence_error(iid, nickname, + this->bridge.send_presence_error(iid, nickname, "cancel", "not-acceptable", "", txt); } @@ -674,14 +674,14 @@ void IrcClient::on_welcome_message(const IrcMessage& message) this->current_nick = message.arguments[0]; this->welcomed = true; #ifdef USE_DATABASE - auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), + auto options = Database::get_irc_server_options(this->bridge.get_bare_jid(), this->get_hostname()); if (!options.afterConnectionCommand.value().empty()) this->send_raw(options.afterConnectionCommand.value()); #endif // Install a repeated events to regularly send a PING TimedEventsManager::instance().add_event(TimedEvent(240s, std::bind(&IrcClient::send_ping_command, this), - "PING"s + this->hostname + this->bridge->get_jid())); + "PING"s + this->hostname + this->bridge.get_jid())); for (const auto& tuple: this->channels_to_join) this->send_join_command(std::get<0>(tuple), std::get<1>(tuple)); this->channels_to_join.clear(); @@ -725,7 +725,7 @@ void IrcClient::on_part(const IrcMessage& message) // channel pointer is now invalid channel = nullptr; } - this->bridge->send_muc_leave(std::move(iid), std::move(nick), std::move(txt), self); + this->bridge.send_muc_leave(std::move(iid), std::move(nick), std::move(txt), self); } } @@ -743,7 +743,7 @@ void IrcClient::on_error(const IrcMessage& message) if (!channel->joined) continue; std::string own_nick = channel->get_self()->nick; - this->bridge->send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true); + this->bridge.send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true); } this->channels.clear(); this->send_gateway_message("ERROR: "s + leave_message); @@ -767,7 +767,7 @@ void IrcClient::on_quit(const IrcMessage& message) iid.set_local(chan_name); iid.set_server(this->hostname); iid.is_channel = true; - this->bridge->send_muc_leave(std::move(iid), std::move(nick), txt, false); + this->bridge.send_muc_leave(std::move(iid), std::move(nick), txt, false); } } } @@ -789,7 +789,7 @@ void IrcClient::on_nick(const IrcMessage& message) iid.is_channel = true; const bool self = channel->get_self()->nick == old_nick; const char user_mode = user->get_most_significant_mode(this->sorted_user_modes); - this->bridge->send_nick_change(std::move(iid), old_nick, new_nick, user_mode, self); + this->bridge.send_nick_change(std::move(iid), old_nick, new_nick, user_mode, self); user->nick = new_nick; if (self) { @@ -815,7 +815,7 @@ void IrcClient::on_kick(const IrcMessage& message) iid.set_local(chan_name); iid.set_server(this->hostname); iid.is_channel = true; - this->bridge->kick_muc_user(std::move(iid), target, reason, author.nick); + this->bridge.kick_muc_user(std::move(iid), target, reason, author.nick); } void IrcClient::on_mode(const IrcMessage& message) @@ -846,7 +846,7 @@ void IrcClient::on_channel_mode(const IrcMessage& message) mode_arguments += message.arguments[i]; } } - this->bridge->send_message(iid, "", "Mode "s + iid.get_local() + + this->bridge.send_message(iid, "", "Mode "s + iid.get_local() + " [" + mode_arguments + "] by " + user.nick, true); const IrcChannel* channel = this->get_channel(iid.get_local()); @@ -917,13 +917,13 @@ void IrcClient::on_channel_mode(const IrcMessage& message) for (const IrcUser* u: modified_users) { char most_significant_mode = u->get_most_significant_mode(this->sorted_user_modes); - this->bridge->send_affiliation_role_change(iid, u->nick, most_significant_mode); + this->bridge.send_affiliation_role_change(iid, u->nick, most_significant_mode); } } void IrcClient::on_user_mode(const IrcMessage& message) { - this->bridge->send_xmpp_message(this->hostname, "", + this->bridge.send_xmpp_message(this->hostname, "", "User mode for "s + message.arguments[0] + " is [" + message.arguments[1] + "]"); } @@ -940,7 +940,7 @@ void IrcClient::on_unknown_message(const IrcMessage& message) if (it + 1 != message.arguments.end()) ss << " "; } - this->bridge->send_xmpp_message(this->hostname, from, ss.str()); + this->bridge.send_xmpp_message(this->hostname, from, ss.str()); } size_t IrcClient::number_of_joined_channels() const @@ -963,14 +963,14 @@ void IrcClient::leave_dummy_channel(const std::string& exit_message) this->dummy_channel.joined = false; this->dummy_channel.joining = false; this->dummy_channel.remove_all_users(); - this->bridge->send_muc_leave(Iid("%"s + this->hostname), std::string(this->current_nick), exit_message, true); + this->bridge.send_muc_leave(Iid("%"s + this->hostname), std::string(this->current_nick), exit_message, true); } #ifdef BOTAN_FOUND bool IrcClient::abort_on_invalid_cert() const { #ifdef USE_DATABASE - auto options = Database::get_irc_server_options(this->bridge->get_bare_jid(), this->hostname); + auto options = Database::get_irc_server_options(this->bridge.get_bare_jid(), this->hostname); return options.verifyCert.value(); #endif return true; -- cgit v1.2.3 From a38b17692e0297cbd5d719f059bd0a1b6ef39fe4 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 29 Dec 2015 11:19:39 +0100 Subject: Support multi-prefix See http://ircv3.net/specs/extensions/multi-prefix-3.1.html ref #3103 --- src/irc/irc_client.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 4aa45ac..e71d38c 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -151,6 +151,9 @@ void IrcClient::on_connected() } } + this->send_message({"CAP", {"REQ", "multi-prefix"}}); + this->send_message({"CAP", {"END"}}); + #ifdef USE_DATABASE auto options = Database::get_irc_server_options(this->bridge.get_bare_jid(), this->get_hostname()); -- cgit v1.2.3 From 1f8333f23f060750673d0b7c573f2e2d12142adf Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 30 Dec 2015 21:34:11 +0100 Subject: Support a trusted SHA1 fingerprint to be configured for each IRC server --- src/irc/irc_client.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e71d38c..1a83446 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -89,6 +89,13 @@ void IrcClient::start() this->bind_addr = Config::get("outgoing_bind", ""); +#ifdef BOTAN_FOUND +# ifdef USE_DATABASE + auto options = Database::get_irc_server_options(this->bridge.get_bare_jid(), + this->get_hostname()); + this->credential_manager.set_trusted_fingerprint(options.trustedFingerprint); +# endif +#endif this->connect(this->hostname, port, tls); } -- cgit v1.2.3 From 0a352e557d2a0760601847a97c562759fb04d529 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 2 Feb 2016 17:41:38 +0100 Subject: Move the irc callbacks into the cpp file --- src/irc/irc_client.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 1a83446..0efae04 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -23,6 +23,98 @@ using namespace std::string_literals; using namespace std::chrono_literals; +/** + * Define a map of functions to be called for each IRC command we can + * handle. + */ +typedef void (IrcClient::*irc_callback_t)(const IrcMessage&); + +static const std::unordered_map>> irc_callbacks = { + {"NOTICE", {&IrcClient::on_notice, {2, 0}}}, + {"002", {&IrcClient::forward_server_message, {2, 0}}}, + {"003", {&IrcClient::forward_server_message, {2, 0}}}, + {"005", {&IrcClient::on_isupport_message, {0, 0}}}, + {"RPL_LISTSTART", {&IrcClient::on_rpl_liststart, {0, 0}}}, + {"321", {&IrcClient::on_rpl_liststart, {0, 0}}}, + {"RPL_LIST", {&IrcClient::on_rpl_list, {0, 0}}}, + {"322", {&IrcClient::on_rpl_list, {0, 0}}}, + {"RPL_LISTEND", {&IrcClient::on_rpl_listend, {0, 0}}}, + {"323", {&IrcClient::on_rpl_listend, {0, 0}}}, + {"RPL_MOTDSTART", {&IrcClient::empty_motd, {0, 0}}}, + {"375", {&IrcClient::empty_motd, {0, 0}}}, + {"RPL_MOTD", {&IrcClient::on_motd_line, {2, 0}}}, + {"372", {&IrcClient::on_motd_line, {2, 0}}}, + {"RPL_MOTDEND", {&IrcClient::send_motd, {0, 0}}}, + {"376", {&IrcClient::send_motd, {0, 0}}}, + {"JOIN", {&IrcClient::on_channel_join, {1, 0}}}, + {"PRIVMSG", {&IrcClient::on_channel_message, {2, 0}}}, + {"353", {&IrcClient::set_and_forward_user_list, {4, 0}}}, + {"332", {&IrcClient::on_topic_received, {2, 0}}}, + {"TOPIC", {&IrcClient::on_topic_received, {2, 0}}}, + {"366", {&IrcClient::on_channel_completely_joined, {2, 0}}}, + {"432", {&IrcClient::on_erroneous_nickname, {2, 0}}}, + {"433", {&IrcClient::on_nickname_conflict, {2, 0}}}, + {"438", {&IrcClient::on_nickname_change_too_fast, {2, 0}}}, + {"001", {&IrcClient::on_welcome_message, {1, 0}}}, + {"PART", {&IrcClient::on_part, {1, 0}}}, + {"ERROR", {&IrcClient::on_error, {1, 0}}}, + {"QUIT", {&IrcClient::on_quit, {0, 0}}}, + {"NICK", {&IrcClient::on_nick, {1, 0}}}, + {"MODE", {&IrcClient::on_mode, {1, 0}}}, + {"PING", {&IrcClient::send_pong_command, {1, 0}}}, + {"PONG", {&IrcClient::on_pong, {0, 0}}}, + {"KICK", {&IrcClient::on_kick, {3, 0}}}, + + {"401", {&IrcClient::on_generic_error, {2, 0}}}, + {"402", {&IrcClient::on_generic_error, {2, 0}}}, + {"403", {&IrcClient::on_generic_error, {2, 0}}}, + {"404", {&IrcClient::on_generic_error, {2, 0}}}, + {"405", {&IrcClient::on_generic_error, {2, 0}}}, + {"406", {&IrcClient::on_generic_error, {2, 0}}}, + {"407", {&IrcClient::on_generic_error, {2, 0}}}, + {"408", {&IrcClient::on_generic_error, {2, 0}}}, + {"409", {&IrcClient::on_generic_error, {2, 0}}}, + {"410", {&IrcClient::on_generic_error, {2, 0}}}, + {"411", {&IrcClient::on_generic_error, {2, 0}}}, + {"412", {&IrcClient::on_generic_error, {2, 0}}}, + {"414", {&IrcClient::on_generic_error, {2, 0}}}, + {"421", {&IrcClient::on_generic_error, {2, 0}}}, + {"422", {&IrcClient::on_generic_error, {2, 0}}}, + {"423", {&IrcClient::on_generic_error, {2, 0}}}, + {"424", {&IrcClient::on_generic_error, {2, 0}}}, + {"431", {&IrcClient::on_generic_error, {2, 0}}}, + {"436", {&IrcClient::on_generic_error, {2, 0}}}, + {"441", {&IrcClient::on_generic_error, {2, 0}}}, + {"442", {&IrcClient::on_generic_error, {2, 0}}}, + {"443", {&IrcClient::on_generic_error, {2, 0}}}, + {"444", {&IrcClient::on_generic_error, {2, 0}}}, + {"446", {&IrcClient::on_generic_error, {2, 0}}}, + {"451", {&IrcClient::on_generic_error, {2, 0}}}, + {"461", {&IrcClient::on_generic_error, {2, 0}}}, + {"462", {&IrcClient::on_generic_error, {2, 0}}}, + {"463", {&IrcClient::on_generic_error, {2, 0}}}, + {"464", {&IrcClient::on_generic_error, {2, 0}}}, + {"465", {&IrcClient::on_generic_error, {2, 0}}}, + {"467", {&IrcClient::on_generic_error, {2, 0}}}, + {"470", {&IrcClient::on_generic_error, {2, 0}}}, + {"471", {&IrcClient::on_generic_error, {2, 0}}}, + {"472", {&IrcClient::on_generic_error, {2, 0}}}, + {"473", {&IrcClient::on_generic_error, {2, 0}}}, + {"474", {&IrcClient::on_generic_error, {2, 0}}}, + {"475", {&IrcClient::on_generic_error, {2, 0}}}, + {"476", {&IrcClient::on_generic_error, {2, 0}}}, + {"477", {&IrcClient::on_generic_error, {2, 0}}}, + {"481", {&IrcClient::on_generic_error, {2, 0}}}, + {"482", {&IrcClient::on_generic_error, {2, 0}}}, + {"483", {&IrcClient::on_generic_error, {2, 0}}}, + {"484", {&IrcClient::on_generic_error, {2, 0}}}, + {"485", {&IrcClient::on_generic_error, {2, 0}}}, + {"487", {&IrcClient::on_generic_error, {2, 0}}}, + {"491", {&IrcClient::on_generic_error, {2, 0}}}, + {"501", {&IrcClient::on_generic_error, {2, 0}}}, + {"502", {&IrcClient::on_generic_error, {2, 0}}}, +}; IrcClient::IrcClient(std::shared_ptr poller, const std::string& hostname, const std::string& nickname, const std::string& username, -- cgit v1.2.3 From 464261d4ed462f5d74507fe4d17bc5f76b5f726d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 11 Apr 2016 17:14:39 +0200 Subject: Support RPL_NOTPIC and 005 --- src/irc/irc_client.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0efae04..1150155 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -34,6 +34,7 @@ static const std::unordered_mapbridge.send_xmpp_message(this->hostname, from, message); @@ -690,6 +697,15 @@ void IrcClient::empty_motd(const IrcMessage&) this->motd.erase(); } +void IrcClient::on_empty_topic(const IrcMessage& message) +{ + const std::string chan_name = utils::tolower(message.arguments[message.arguments.size() - 1]); + log_debug("empty topic for " << chan_name); + IrcChannel* channel = this->get_channel(chan_name); + if (channel) + channel->topic.clear(); +} + void IrcClient::on_motd_line(const IrcMessage& message) { const std::string body = message.arguments[1]; -- cgit v1.2.3 From 04d28f968b227067e77e365d317fc251d3c965f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 19 Apr 2016 02:43:26 +0200 Subject: Forward the topic authors, handle the author from 333 messages fix #2 --- src/irc/irc_client.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 1150155..327b170 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -55,6 +55,8 @@ static const std::unordered_mapget_channel(chan_name); channel->topic = message.arguments[message.arguments.size() - 1]; + channel->topic_author = author.nick; if (channel->joined) - this->bridge.send_topic(this->hostname, chan_name, channel->topic); + this->bridge.send_topic(this->hostname, chan_name, channel->topic, channel->topic_author); +} + +void IrcClient::on_topic_who_time_received(const IrcMessage& message) +{ + IrcUser author(message.arguments[2]); + const std::string chan_name = utils::tolower(message.arguments[1]); + IrcChannel* channel = this->get_channel(chan_name); + channel->topic_author = author.nick; } void IrcClient::on_channel_completely_joined(const IrcMessage& message) @@ -737,7 +749,7 @@ void IrcClient::on_channel_completely_joined(const IrcMessage& message) this->bridge.send_user_join(this->hostname, chan_name, channel->get_self(), channel->get_self()->get_most_significant_mode(this->sorted_user_modes), true); - this->bridge.send_topic(this->hostname, chan_name, channel->topic); + this->bridge.send_topic(this->hostname, chan_name, channel->topic, channel->topic_author); } void IrcClient::on_erroneous_nickname(const IrcMessage& message) -- cgit v1.2.3 From fdddd447b3976f26a4146ebf91abfc04736a006b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 13 May 2016 01:22:49 +0200 Subject: =?UTF-8?q?Use=20=E2=80=9Cusing=E2=80=9D=20instead=20of=20typedef?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/irc/irc_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 327b170..0b1b079 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -27,10 +27,10 @@ using namespace std::chrono_literals; * Define a map of functions to be called for each IRC command we can * handle. */ -typedef void (IrcClient::*irc_callback_t)(const IrcMessage&); +using IrcCallback = void (IrcClient::*)(const IrcMessage&); static const std::unordered_map>> irc_callbacks = { + std::pair>> irc_callbacks = { {"NOTICE", {&IrcClient::on_notice, {2, 0}}}, {"002", {&IrcClient::forward_server_message, {2, 0}}}, {"003", {&IrcClient::forward_server_message, {2, 0}}}, -- cgit v1.2.3 From 507d0c2cbe3c41e3d8e6d38862fe418cb551adf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 2 Jun 2016 01:35:15 +0200 Subject: Forward everything to all concerned XMPP resources --- src/irc/irc_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 0b1b079..ae68528 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -701,7 +701,7 @@ void IrcClient::empty_motd(const IrcMessage&) void IrcClient::on_empty_topic(const IrcMessage& message) { - const std::string chan_name = utils::tolower(message.arguments[message.arguments.size() - 1]); + const std::string chan_name = utils::tolower(message.arguments[1]); log_debug("empty topic for " << chan_name); IrcChannel* channel = this->get_channel(chan_name); if (channel) -- cgit v1.2.3 From 2d11a5f49454717c404b25825f18e696281207d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 8 Jun 2016 01:32:39 +0200 Subject: Support multiple nick session, except for IQs ref #2556 --- src/irc/irc_client.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index ae68528..d16ffc7 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -618,9 +618,7 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message) const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); if (user->nick != channel->get_self()->nick) { - this->bridge.send_user_join(this->hostname, chan_name, user, - user->get_most_significant_mode(this->sorted_user_modes), - false); + this->bridge.send_user_join(this->hostname, chan_name, user, user->get_most_significant_mode(this->sorted_user_modes), false); } else { @@ -644,9 +642,7 @@ void IrcClient::on_channel_join(const IrcMessage& message) else { const IrcUser* user = channel->add_user(nick, this->prefix_to_mode); - this->bridge.send_user_join(this->hostname, chan_name, user, - user->get_most_significant_mode(this->sorted_user_modes), - false); + this->bridge.send_user_join(this->hostname, chan_name, user, user->get_most_significant_mode(this->sorted_user_modes), false); } } @@ -746,9 +742,7 @@ void IrcClient::on_channel_completely_joined(const IrcMessage& message) const std::string chan_name = utils::tolower(message.arguments[1]); IrcChannel* channel = this->get_channel(chan_name); channel->joined = true; - this->bridge.send_user_join(this->hostname, chan_name, channel->get_self(), - channel->get_self()->get_most_significant_mode(this->sorted_user_modes), - true); + this->bridge.send_user_join(this->hostname, chan_name, channel->get_self(), channel->get_self()->get_most_significant_mode(this->sorted_user_modes), true); this->bridge.send_topic(this->hostname, chan_name, channel->topic, channel->topic_author); } -- cgit v1.2.3 From 5a2e61161792cf51209f240e40e28036195f35be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 13 Jun 2016 19:59:17 +0200 Subject: Show off, with some variadic templates, for the logger module --- src/irc/irc_client.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index d16ffc7..e320db9 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -333,7 +333,7 @@ void IrcClient::parse_in_buffer(const size_t) break ; IrcMessage message(this->in_buf.substr(0, pos)); this->in_buf = this->in_buf.substr(pos + 2, std::string::npos); - log_debug("IRC RECEIVING: (" << this->get_hostname() << ") " << message); + log_debug("IRC RECEIVING: (", this->get_hostname(), ") ", message); // Call the standard callback (if any), associated with the command // name that we just received. @@ -346,21 +346,21 @@ void IrcClient::parse_in_buffer(const size_t) // second is the max if (message.arguments.size() < limits.first || (limits.second > 0 && message.arguments.size() > limits.second)) - log_warning("Invalid number of arguments for IRC command “" << message.command << - "”: " << message.arguments.size()); + log_warning("Invalid number of arguments for IRC command “", message.command, + "”: ", message.arguments.size()); else { const auto& cb = it->second.first; try { (this->*(cb))(message); } catch (const std::exception& e) { - log_error("Unhandled exception: " << e.what()); + log_error("Unhandled exception: ", e.what()); } } } else { - log_info("No handler for command " << message.command << + log_info("No handler for command ", message.command, ", forwarding the arguments to the user"); this->on_unknown_message(message); } @@ -371,7 +371,7 @@ void IrcClient::parse_in_buffer(const size_t) void IrcClient::send_message(IrcMessage&& message) { - log_debug("IRC SENDING: (" << this->get_hostname() << ") " << message); + log_debug("IRC SENDING: (", this->get_hostname(), ") ", message); std::string res; if (!message.prefix.empty()) res += ":" + std::move(message.prefix) + " "; @@ -392,7 +392,7 @@ void IrcClient::send_message(IrcMessage&& message) void IrcClient::send_raw(const std::string& txt) { - log_debug("IRC SENDING (raw): (" << this->get_hostname() << ") " << txt); + log_debug("IRC SENDING (raw): (", this->get_hostname(), ") ", txt); this->send_data(txt + "\r\n"); } @@ -452,7 +452,7 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st IrcChannel* channel = this->get_channel(chan_name); if (channel->joined == false) { - log_warning("Cannot send message to channel " << chan_name << ", it is not joined"); + log_warning("Cannot send message to channel ", chan_name, ", it is not joined"); return false; } // Cut the message body into 400-bytes parts (because the whole command @@ -698,7 +698,7 @@ void IrcClient::empty_motd(const IrcMessage&) void IrcClient::on_empty_topic(const IrcMessage& message) { const std::string chan_name = utils::tolower(message.arguments[1]); - log_debug("empty topic for " << chan_name); + log_debug("empty topic for ", chan_name); IrcChannel* channel = this->get_channel(chan_name); if (channel) channel->topic.clear(); @@ -1026,8 +1026,8 @@ void IrcClient::on_channel_mode(const IrcMessage& message) IrcUser* user = channel->find_user(target); if (!user) { - log_warning("Trying to set mode for non-existing user '" << target - << "' in channel" << iid.get_local()); + log_warning("Trying to set mode for non-existing user '", target + , "' in channel", iid.get_local()); return; } if (add) -- cgit v1.2.3 From 80d0c19c5a8d548a8c6019033bf574ff2be4c0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 14 Jun 2016 20:14:36 +0200 Subject: Refactor, test and improve the way we cut too-long messages for IRC --- src/irc/irc_client.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e320db9..2cf0840 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -455,15 +456,13 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st log_warning("Cannot send message to channel ", chan_name, ", it is not joined"); return false; } - // Cut the message body into 400-bytes parts (because the whole command - // must fit into 512 bytes, that's an easy way to make sure the chan name - // + body fits. I’m lazy.) - std::string::size_type pos = 0; - while (pos < body.size()) - { - this->send_message(IrcMessage("PRIVMSG", {chan_name, body.substr(pos, 400)})); - pos += 400; - } + // Cut the message body into 512-bytes parts, because the whole command + // must fit into 512 bytes. + // Count the ':' at the start of the text, and two spaces + const auto line_size = 512 - ::strlen("PRIVMSG") - chan_name.length() - 3; + const auto lines = cut(body, line_size); + for (const auto& line: lines) + this->send_message(IrcMessage("PRIVMSG", {chan_name, line})); return true; } -- cgit v1.2.3 From 4b1c580bb9bc03d656e59d702c72c3e793a1bbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 15 Jun 2016 12:19:19 +0200 Subject: cut messages at 512 bytes, taking into account the UTF-8 codepoints ref #3067 --- src/irc/irc_client.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 2cf0840..1d56361 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -388,6 +388,8 @@ void IrcClient::send_message(IrcMessage&& message) res += " " + arg; } res += "\r\n"; + log_debug("Effective size: ", res.size()); + log_debug(res); this->send_data(std::move(res)); } @@ -458,8 +460,7 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st } // Cut the message body into 512-bytes parts, because the whole command // must fit into 512 bytes. - // Count the ':' at the start of the text, and two spaces - const auto line_size = 512 - ::strlen("PRIVMSG") - chan_name.length() - 3; + const auto line_size = 500 - ::strlen("PRIVMSG ") - chan_name.length() - ::strlen(" :\r\n"); const auto lines = cut(body, line_size); for (const auto& line: lines) this->send_message(IrcMessage("PRIVMSG", {chan_name, line})); -- cgit v1.2.3 From 849c50f9f33d5a391f623272b007810c816aca68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 15 Jun 2016 15:47:05 +0200 Subject: Save our own host, as reported by the server --- src/irc/irc_client.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 1d56361..324f725 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -59,6 +59,7 @@ static const std::unordered_mapbridge.send_topic(this->hostname, chan_name, channel->topic, channel->topic_author); } +void IrcClient::on_own_host_received(const IrcMessage& message) +{ + this->own_host = message.arguments[1]; + const std::string from = message.prefix; + if (message.arguments.size() >= 3) + this->bridge.send_xmpp_message(this->hostname, from, + this->own_host + " " + message.arguments[2]); + else + this->bridge.send_xmpp_message(this->hostname, from, this->own_host + + " is now your displayed host"); +} + void IrcClient::on_erroneous_nickname(const IrcMessage& message) { const std::string error_msg = message.arguments.size() >= 3 ? -- cgit v1.2.3 From 430bf3a6610da0f44bdeb0ea37bdf9ceabb8ed01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 15 Jun 2016 15:47:43 +0200 Subject: Properly calculate the maximum size of each message line, before cutting fix #3067 --- src/irc/irc_client.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 324f725..4492176 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -389,8 +389,6 @@ void IrcClient::send_message(IrcMessage&& message) res += " " + arg; } res += "\r\n"; - log_debug("Effective size: ", res.size()); - log_debug(res); this->send_data(std::move(res)); } @@ -459,9 +457,15 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st log_warning("Cannot send message to channel ", chan_name, ", it is not joined"); return false; } - // Cut the message body into 512-bytes parts, because the whole command - // must fit into 512 bytes. - const auto line_size = 500 - ::strlen("PRIVMSG ") - chan_name.length() - ::strlen(" :\r\n"); + // The max size is 512, taking into account the whole message, not just + // the text we send. + // This includes our own nick, username and host (because this will be + // added by the server into our message), in addition to the basic + // components of the message we send (command name, chan name, \r\n et) + // : + NICK + ! + USER + @ + HOST + + PRIVMSG + + CHAN + + : + \r\n + const auto line_size = 512 - + this->current_nick.size() - this->username.size() - this->own_host.size() - + ::strlen(":!@ PRIVMSG ") - chan_name.length() - ::strlen(" :\r\n"); const auto lines = cut(body, line_size); for (const auto& line: lines) this->send_message(IrcMessage("PRIVMSG", {chan_name, line})); -- cgit v1.2.3 From c1f678e4503083e56d0bef0386657c083c0117ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 15 Jun 2016 16:47:11 +0200 Subject: Fix a missing include for strlen --- src/irc/irc_client.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 4492176..dd83307 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include -- cgit v1.2.3