Refactor the channel::self to point at the existing user
This way, the user is always up to date, instead of being a duplicate out of sync. fix #3258
This commit is contained in:
+17
-5
@@ -1,21 +1,25 @@
|
|||||||
#include <irc/irc_channel.hpp>
|
#include <irc/irc_channel.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
void IrcChannel::set_self(const std::string& name)
|
void IrcChannel::set_self(IrcUser* user)
|
||||||
{
|
{
|
||||||
this->self = std::make_unique<IrcUser>(name);
|
this->self = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
IrcUser* IrcChannel::add_user(const std::string& name,
|
IrcUser* IrcChannel::add_user(const std::string& name,
|
||||||
const std::map<char, char>& prefix_to_mode)
|
const std::map<char, char>& prefix_to_mode)
|
||||||
{
|
{
|
||||||
this->users.emplace_back(std::make_unique<IrcUser>(name, prefix_to_mode));
|
auto new_user = std::make_unique<IrcUser>(name, prefix_to_mode);
|
||||||
|
auto old_user = this->find_user(new_user->nick);
|
||||||
|
if (old_user)
|
||||||
|
return old_user;
|
||||||
|
this->users.emplace_back(std::move(new_user));
|
||||||
return this->users.back().get();
|
return this->users.back().get();
|
||||||
}
|
}
|
||||||
|
|
||||||
IrcUser* IrcChannel::get_self() const
|
IrcUser* IrcChannel::get_self() const
|
||||||
{
|
{
|
||||||
return this->self.get();
|
return this->self;
|
||||||
}
|
}
|
||||||
|
|
||||||
IrcUser* IrcChannel::find_user(const std::string& name) const
|
IrcUser* IrcChannel::find_user(const std::string& name) const
|
||||||
@@ -32,19 +36,27 @@ IrcUser* IrcChannel::find_user(const std::string& name) const
|
|||||||
void IrcChannel::remove_user(const IrcUser* user)
|
void IrcChannel::remove_user(const IrcUser* user)
|
||||||
{
|
{
|
||||||
const auto nick = user->nick;
|
const auto nick = user->nick;
|
||||||
|
const bool is_self = (user == this->self);
|
||||||
const auto it = std::find_if(this->users.begin(), this->users.end(),
|
const auto it = std::find_if(this->users.begin(), this->users.end(),
|
||||||
[nick](const std::unique_ptr<IrcUser>& u)
|
[nick](const std::unique_ptr<IrcUser>& u)
|
||||||
{
|
{
|
||||||
return nick == u->nick;
|
return nick == u->nick;
|
||||||
});
|
});
|
||||||
if (it != this->users.end())
|
if (it != this->users.end())
|
||||||
|
{
|
||||||
this->users.erase(it);
|
this->users.erase(it);
|
||||||
|
if (is_self)
|
||||||
|
{
|
||||||
|
this->self = nullptr;
|
||||||
|
this->joined = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IrcChannel::remove_all_users()
|
void IrcChannel::remove_all_users()
|
||||||
{
|
{
|
||||||
this->users.clear();
|
this->users.clear();
|
||||||
this->self.reset();
|
this->self = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
DummyIrcChannel::DummyIrcChannel():
|
DummyIrcChannel::DummyIrcChannel():
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public:
|
|||||||
bool parting{false};
|
bool parting{false};
|
||||||
std::string topic{};
|
std::string topic{};
|
||||||
std::string topic_author{};
|
std::string topic_author{};
|
||||||
void set_self(const std::string& name);
|
void set_self(IrcUser* user);
|
||||||
IrcUser* get_self() const;
|
IrcUser* get_self() const;
|
||||||
IrcUser* add_user(const std::string& name,
|
IrcUser* add_user(const std::string& name,
|
||||||
const std::map<char, char>& prefix_to_mode);
|
const std::map<char, char>& prefix_to_mode);
|
||||||
@@ -38,7 +38,8 @@ public:
|
|||||||
{ return this->users; }
|
{ return this->users; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::unique_ptr<IrcUser> self{};
|
// Pointer to one IrcUser stored in users
|
||||||
|
IrcUser* self{nullptr};
|
||||||
std::vector<std::unique_ptr<IrcUser>> users{};
|
std::vector<std::unique_ptr<IrcUser>> users{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+12
-12
@@ -642,15 +642,18 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message)
|
|||||||
std::vector<std::string> nicks = utils::split(message.arguments[3], ' ');
|
std::vector<std::string> nicks = utils::split(message.arguments[3], ' ');
|
||||||
for (const std::string& nick: nicks)
|
for (const std::string& nick: nicks)
|
||||||
{
|
{
|
||||||
const IrcUser* user = channel->add_user(nick, this->prefix_to_mode);
|
// Just create this dummy user to parse and get its modes
|
||||||
if (user->nick != channel->get_self()->nick)
|
IrcUser tmp_user{nick, this->prefix_to_mode};
|
||||||
|
// Does this concern ourself
|
||||||
|
if (channel->get_self() && channel->find_user(tmp_user.nick) == channel->get_self())
|
||||||
{
|
{
|
||||||
this->bridge.send_user_join(this->hostname, chan_name, user, user->get_most_significant_mode(this->sorted_user_modes), false);
|
// We now know our own modes, that’s all.
|
||||||
|
channel->get_self()->modes = tmp_user.modes;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{ // Otherwise this is a new user
|
||||||
// we now know the modes of self, so copy the modes into self
|
const IrcUser *user = channel->add_user(nick, this->prefix_to_mode);
|
||||||
channel->get_self()->modes = user->modes;
|
this->bridge.send_user_join(this->hostname, chan_name, user, user->get_most_significant_mode(this->sorted_user_modes), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -664,14 +667,12 @@ void IrcClient::on_channel_join(const IrcMessage& message)
|
|||||||
else
|
else
|
||||||
channel = this->get_channel(chan_name);
|
channel = this->get_channel(chan_name);
|
||||||
const std::string nick = message.prefix;
|
const std::string nick = message.prefix;
|
||||||
|
IrcUser* user = channel->add_user(nick, this->prefix_to_mode);
|
||||||
if (channel->joined == false)
|
if (channel->joined == false)
|
||||||
channel->set_self(nick);
|
channel->set_self(user);
|
||||||
else
|
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);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void IrcClient::on_channel_message(const IrcMessage& message)
|
void IrcClient::on_channel_message(const IrcMessage& message)
|
||||||
{
|
{
|
||||||
@@ -929,15 +930,14 @@ void IrcClient::on_part(const IrcMessage& message)
|
|||||||
if (user)
|
if (user)
|
||||||
{
|
{
|
||||||
std::string nick = user->nick;
|
std::string nick = user->nick;
|
||||||
|
bool self = channel->get_self() && channel->get_self()->nick == nick;
|
||||||
channel->remove_user(user);
|
channel->remove_user(user);
|
||||||
Iid iid;
|
Iid iid;
|
||||||
iid.set_local(chan_name);
|
iid.set_local(chan_name);
|
||||||
iid.set_server(this->hostname);
|
iid.set_server(this->hostname);
|
||||||
iid.type = Iid::Type::Channel;
|
iid.type = Iid::Type::Channel;
|
||||||
bool self = channel->get_self()->nick == nick;
|
|
||||||
if (self)
|
if (self)
|
||||||
{
|
{
|
||||||
channel->joined = false;
|
|
||||||
this->channels.erase(utils::tolower(chan_name));
|
this->channels.erase(utils::tolower(chan_name));
|
||||||
// channel pointer is now invalid
|
// channel pointer is now invalid
|
||||||
channel = nullptr;
|
channel = nullptr;
|
||||||
|
|||||||
Reference in New Issue
Block a user