Commit 020325db authored by louiz’'s avatar louiz’

[WIP] DummyIrcChannel

parent 65601a96
#include <bridge/bridge.hpp>
#include <bridge/colors.hpp>
#include <xmpp/xmpp_component.hpp>
#include <irc/irc_message.hpp>
#include <network/poller.hpp>
#include <utils/encoding.hpp>
#include <logger/logger.hpp>
......@@ -100,6 +101,25 @@ IrcClient* Bridge::get_irc_client(const std::string& hostname)
bool Bridge::join_irc_channel(const Iid& iid, const std::string& username)
{
IrcClient* irc = this->get_irc_client(iid.server, username);
if (iid.chan.empty())
{ // Join the dummy channel
if (irc->is_welcomed())
{
if (irc->get_dummy_channel().joined)
return false;
// Immediately simulate a message coming from the IRC server saying that we
// joined the channel
const IrcMessage join_message(irc->get_nick(), "JOIN", {""});
irc->on_channel_join(join_message);
const IrcMessage end_join_message(std::string(iid.server), "366",
{irc->get_nick(),
"", "End of NAMES list"});
irc->on_channel_completely_joined(end_join_message);
}
else
irc->get_dummy_channel().joining = true;
return true;
}
if (irc->is_channel_joined(iid.chan) == false)
{
irc->send_join_command(iid.chan);
......
......@@ -47,3 +47,9 @@ void IrcChannel::remove_user(const IrcUser* user)
}
}
}
DummyIrcChannel::DummyIrcChannel():
IrcChannel(),
joining(false)
{
}
......@@ -25,13 +25,44 @@ public:
IrcUser* find_user(const std::string& name) const;
void remove_user(const IrcUser* user);
private:
protected:
std::unique_ptr<IrcUser> self;
std::vector<std::unique_ptr<IrcUser>> users;
private:
IrcChannel(const IrcChannel&) = delete;
IrcChannel(IrcChannel&&) = delete;
IrcChannel& operator=(const IrcChannel&) = delete;
IrcChannel& operator=(IrcChannel&&) = delete;
};
/**
* A special channel that is not actually linked to any real irc
* channel. This is just a channel representing a connection to the
* server. If an user wants to maintain the connection to the server without
* having to be on any irc channel of that server, he can just join this
* dummy channel.
* It’s not actually dummy because it’s useful and it does things, but well.
*/
class DummyIrcChannel: public IrcChannel
{
public:
explicit DummyIrcChannel();
/**
* This flag is at true whenever the user wants to join this channel, but
* he is not yet connected to the server. When the connection is made, we
* check that flag and if it’s true, we inform the user that he has just
* joined that channel.
* If the user is already connected to the server when he tries to join
* the channel, we don’t use that flag, we just join it immediately.
*/
bool joining;
private:
DummyIrcChannel(const DummyIrcChannel&) = delete;
DummyIrcChannel(DummyIrcChannel&&) = delete;
DummyIrcChannel& operator=(const DummyIrcChannel&) = delete;
DummyIrcChannel& operator=(DummyIrcChannel&&) = delete;
};
#endif // IRC_CHANNEL_INCLUDED
......@@ -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;
}
......@@ -194,6 +194,14 @@ public:
* Return the number of joined channels
*/
size_t number_of_joined_channels() const;
/**
* Get a reference to the unique dummy channel
*/
DummyIrcChannel& get_dummy_channel();
const std::string& get_hostname() const { return this->hostname; }
std::string get_nick() const { return this->current_nick; }
bool is_welcomed() const { return this->welcomed; }
private:
/**
......@@ -216,6 +224,11 @@ private:
* The list of joined channels, indexed by name
*/
std::unordered_map<std::string, std::unique_ptr<IrcChannel>> channels;
/**
* A single channel with a iid of the form "hostname" (normal channel have
* an iid of the form "chan%hostname".
*/
DummyIrcChannel dummy_channel;
/**
* A list of chan we want to join, but we need a response 001 from
* the server before sending the actual JOIN commands. So we just keep the
......@@ -223,6 +236,10 @@ private:
* whenever the WELCOME message is received.
*/
std::vector<std::string> channels_to_join;
/**
* This flag indicates that the server is completely joined (connection
* has been established, we are authentified and we have a nick)
*/
bool welcomed;
/**
* See http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt section 3.3
......
......@@ -10,6 +10,7 @@
#include <utils/tolower.hpp>
#include <utils/split.hpp>
#include <xmpp/jid.hpp>
#include <irc/iid.hpp>
#include <string.h>
#include <iostream>
......
......@@ -290,8 +290,9 @@ void XmppComponent::handle_presence(const Stanza& stanza)
error_type, error_name, "");
});
if (!iid.chan.empty() && !iid.server.empty())
{ // presence toward a MUC that corresponds to an irc channel
if (!iid.server.empty())
{ // presence toward a MUC that corresponds to an irc channel, or a
// dummy channel if iid.chan is empty
if (type.empty())
{
const std::string own_nick = bridge->get_own_nick(iid);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment