~singpolyma/biboumi

bb596582bd2d8b9aab3fe08e76a7d24d82bf614a — louiz’ 5 years ago 158d743
Add a <item/> node in the presence of a leaving participant

fix #3339
M src/bridge/bridge.cpp => src/bridge/bridge.cpp +17 -7
@@ 419,7 419,7 @@ void Bridge::leave_irc_channel(Iid&& iid, const std::string& status_message, con
        }
      else if (channel->joined)
        {
          this->send_muc_leave(iid, channel->get_self()->nick, "", true, true, resource);
          this->send_muc_leave(iid, *channel->get_self(), "", true, true, resource);
        }
      if (persistent)
        this->remove_resource_from_chan(key, resource);


@@ 430,7 430,7 @@ void Bridge::leave_irc_channel(Iid&& iid, const std::string& status_message, con
  else
    {
      if (channel && channel->joined)
        this->send_muc_leave(iid, channel->get_self()->nick,
        this->send_muc_leave(iid, *channel->get_self(),
                             "Biboumi note: " + std::to_string(resources - 1) + " resources are still in this channel.",
                             true, true, resource);
      this->remove_resource_from_chan(key, resource);


@@ 847,19 847,29 @@ void Bridge::send_presence_error(const Iid& iid, const std::string& nick,
  this->xmpp.send_presence_error(std::to_string(iid), nick, this->user_jid, type, condition, error_code, text);
}

void Bridge::send_muc_leave(const Iid& iid, const std::string& nick,
void Bridge::send_muc_leave(const Iid& iid, const IrcUser& user,
                            const std::string& message, const bool self,
                            const bool user_requested,
                            const std::string& resource)
{
  const IrcClient* client = this->find_irc_client(iid.get_server());
  if (!client)
    {
      log_error("Tried to send an unavailable presence for non existant client: ", std::to_string(iid));
      return;
    }
  std::string affiliation;
  std::string role;
  std::tie(role, affiliation) = get_role_affiliation_from_irc_mode(user.get_most_significant_mode(client->get_sorted_user_modes()));

  if (!resource.empty())
    this->xmpp.send_muc_leave(std::to_string(iid), nick, this->make_xmpp_body(message),
                              this->user_jid + "/" + resource, self, user_requested);
    this->xmpp.send_muc_leave(std::to_string(iid), user.nick, this->make_xmpp_body(message),
                              this->user_jid + "/" + resource, self, user_requested, affiliation, role);
  else
    {
      for (const auto &res: this->resources_in_chan[iid.to_tuple()])
        this->xmpp.send_muc_leave(std::to_string(iid), nick, this->make_xmpp_body(message),
                                  this->user_jid + "/" + res, self, user_requested);
        this->xmpp.send_muc_leave(std::to_string(iid), user.nick, this->make_xmpp_body(message),
                                  this->user_jid + "/" + res, self, user_requested, affiliation, role);
      if (self)
        {
          // Copy the resources currently in that channel

M src/bridge/bridge.hpp => src/bridge/bridge.hpp +1 -1
@@ 170,7 170,7 @@ public:
  /**
   * Send an unavailable presence from this participant
   */
  void send_muc_leave(const Iid& iid, const std::string& nick,
  void send_muc_leave(const Iid& iid, const IrcUser& nick,
                      const std::string& message, const bool self,
                      const bool user_requested,
                      const std::string& resource="");

M src/irc/irc_channel.cpp => src/irc/irc_channel.cpp +4 -1
@@ 33,8 33,9 @@ IrcUser* IrcChannel::find_user(const std::string& name) const
  return nullptr;
}

void IrcChannel::remove_user(const IrcUser* user)
std::unique_ptr<IrcUser> IrcChannel::remove_user(const IrcUser* user)
{
  std::unique_ptr<IrcUser> result{};
  const auto nick = user->nick;
  const bool is_self = (user == this->self);
  const auto it = std::find_if(this->users.begin(), this->users.end(),


@@ 44,6 45,7 @@ void IrcChannel::remove_user(const IrcUser* user)
                               });
  if (it != this->users.end())
    {
      result = std::move(*it);
      this->users.erase(it);
      if (is_self)
        {


@@ 57,4 59,5 @@ void IrcChannel::remove_all_users()
{
  this->users.clear();
  this->self = nullptr;
  return result;
}

M src/irc/irc_channel.hpp => src/irc/irc_channel.hpp +1 -1
@@ 32,8 32,8 @@ public:
  IrcUser* add_user(const std::string& name,
                    const std::map<char, char>& prefix_to_mode);
  IrcUser* find_user(const std::string& name) const;
  void remove_user(const IrcUser* user);
  void remove_all_users();
  std::unique_ptr<IrcUser> remove_user(const IrcUser* user);
  const std::vector<std::unique_ptr<IrcUser>>& get_users() const
  { return this->users; }


M src/irc/irc_client.cpp => src/irc/irc_client.cpp +8 -9
@@ 951,18 951,18 @@ void IrcClient::on_part(const IrcMessage& message)
    {
      std::string nick = user->nick;
      bool self = channel->get_self() && channel->get_self()->nick == nick;
      channel->remove_user(user);
      Iid iid;
      iid.set_local(chan_name);
      iid.set_server(this->hostname);
      iid.type = Iid::Type::Channel;
      auto user_ptr = channel->remove_user(user);
      if (self)
      {
        this->channels.erase(utils::tolower(chan_name));
        // channel pointer is now invalid
        channel = nullptr;
      }
      this->bridge.send_muc_leave(iid, std::move(nick), txt, self, true);
      Iid iid;
      iid.set_local(chan_name);
      iid.set_server(this->hostname);
      iid.type = Iid::Type::Channel;
      this->bridge.send_muc_leave(iid, *user_ptr, txt, self, true);
    }
}



@@ 979,8 979,7 @@ void IrcClient::on_error(const IrcMessage& message)
    IrcChannel* channel = pair.second.get();
    if (!channel->joined)
      continue;
    std::string own_nick = channel->get_self()->nick;
      this->bridge.send_muc_leave(iid, std::move(own_nick), leave_message, true, false);
    this->bridge.send_muc_leave(iid, *channel->get_self(), leave_message, true, false);
  }
  this->channels.clear();
  this->send_gateway_message("ERROR: " + leave_message);


@@ 1005,7 1004,7 @@ void IrcClient::on_quit(const IrcMessage& message)
      iid.set_local(chan_name);
      iid.set_server(this->hostname);
      iid.type = Iid::Type::Channel;
      this->bridge.send_muc_leave(iid, user->nick, txt, self, false);
      this->bridge.send_muc_leave(iid, *user, txt, self, false);
      channel->remove_user(user);
    }
}

M src/xmpp/xmpp_component.cpp => src/xmpp/xmpp_component.cpp +5 -1
@@ 425,7 425,8 @@ void XmppComponent::send_history_message(const std::string& muc_name, const std:
#endif

void XmppComponent::send_muc_leave(const std::string& muc_name, const std::string& nick, Xmpp::body&& message,
                                   const std::string& jid_to, const bool self, const bool user_requested)
                                   const std::string& jid_to, const bool self, const bool user_requested,
                                   const std::string& affiliation, const std::string& role)
{
  Stanza presence("presence");
  {


@@ 447,6 448,9 @@ void XmppComponent::send_muc_leave(const std::string& muc_name, const std::strin
            status["code"] = "332";
          }
      }
    XmlSubNode item(x, "item");
    item["affiliation"] = affiliation;
    item["role"] = role;
    if (!message_str.empty())
      {
        XmlSubNode status(presence, "status");

M src/xmpp/xmpp_component.hpp => src/xmpp/xmpp_component.hpp +2 -1
@@ 150,7 150,8 @@ public:
                      Xmpp::body&& message,
                      const std::string& jid_to,
                      const bool self,
                      const bool user_requested);
                      const bool user_requested,
                      const std::string& affiliation, const std::string& role);
  /**
   * Indicate that a participant changed his nick
   */

M tests/end_to_end/__main__.py => tests/end_to_end/__main__.py +2 -1
@@ 1149,7 1149,8 @@ if __name__ == '__main__':
                     "<presence from='{jid_one}/{resource_one}' to='#foo%{irc_server_one}/{nick_one}' type='unavailable' />"),
                     # Only user 1 receives the unavailable presence
                     partial(expect_stanza,
                             "/presence[@from='#foo%{irc_server_one}/{nick_one}'][@to='{jid_one}/{resource_one}'][@type='unavailable']/muc_user:x/muc_user:status[@code='110']"),
                             ("/presence[@from='#foo%{irc_server_one}/{nick_one}'][@to='{jid_one}/{resource_one}'][@type='unavailable']/muc_user:x/muc_user:status[@code='110']",
                              "/presence/muc_user:x/muc_user:item[@affiliation='admin'][@role='moderator']")),

                     # Second user sends a channel message
                     partial(send_stanza, "<message type='groupchat' from='{jid_two}/{resource_one}' to='#foo%{irc_server_one}'><body>coucou</body></message>"),