~singpolyma/biboumi

14fe971183e5a281525827c8135965f57e15f73b — Emmanuel Gil Peyrot 4 years ago 440e04c
Don’t treat presence updates as MUC joins

If the user sends a directed presence to an unjoined MUC without a <x/>
element, send a presence error back instead of attempting to join it
again, as this is almost never what the user wants.

Fixes #3415.
3 files changed, 42 insertions(+), 35 deletions(-)

M src/bridge/bridge.cpp
M src/bridge/bridge.hpp
M src/xmpp/biboumi_component.cpp
M src/bridge/bridge.cpp => src/bridge/bridge.cpp +3 -4
@@ 170,8 170,7 @@ IrcClient* Bridge::find_irc_client(const std::string& hostname) const
bool Bridge::join_irc_channel(const Iid& iid, std::string nickname,
                              const std::string& password,
                              const std::string& resource,
                              HistoryLimit history_limit,
                              const bool force_join)
                              HistoryLimit history_limit)
{
  const auto& hostname = iid.get_server();
#ifdef USE_DATABASE


@@ 189,8 188,8 @@ bool Bridge::join_irc_channel(const Iid& iid, std::string nickname,
    {
      irc->send_join_command(iid.get_local(), password);
      return true;
    } else if (!res_in_chan || force_join) {
      // See https://github.com/xsf/xeps/pull/499 for the force_join argument
    } else {
      // See https://github.com/xsf/xeps/pull/499
      this->generate_channel_join_for_resource(iid, resource);
    }
  return false;

M src/bridge/bridge.hpp => src/bridge/bridge.hpp +2 -4
@@ 72,14 72,12 @@ public:
   **/

  /**
   * Try to join an irc_channel, does nothing and return true if the channel
   * was already joined.
   * Try to join an irc_channel.
   */
  bool join_irc_channel(const Iid& iid, std::string nickname,
                        const std::string& password,
                        const std::string& resource,
                        HistoryLimit history_limit,
                        const bool force_join);
                        HistoryLimit history_limit);

  void send_channel_message(const Iid& iid, const std::string& body, std::string id);
  void send_private_message(const Iid& iid, const std::string& body, const std::string& type="PRIVMSG");

M src/xmpp/biboumi_component.cpp => src/xmpp/biboumi_component.cpp +37 -27
@@ 158,39 158,49 @@ void BiboumiComponent::handle_presence(const Stanza& stanza)
        {
          const std::string own_nick = bridge->get_own_nick(iid);
          const XmlNode* x = stanza.get_child("x", MUC_NS);
          const XmlNode* password = x ? x->get_child("password", MUC_NS): nullptr;
          const XmlNode* history = x ? x->get_child("history", MUC_NS): nullptr;
          HistoryLimit history_limit;
          if (history)
            {
              const auto seconds = history->get_tag("seconds");
              if (!seconds.empty())
                {
                  const auto now = std::chrono::system_clock::now();
                  std::time_t timestamp = std::chrono::system_clock::to_time_t(now);
                  int int_seconds = std::atoi(seconds.data());
                  timestamp -= int_seconds;
                  history_limit.since = utils::to_string(timestamp);
                }
              const auto since = history->get_tag("since");
              if (!since.empty())
                history_limit.since = since;
              const auto maxstanzas = history->get_tag("maxstanzas");
              if (!maxstanzas.empty())
                history_limit.stanzas = std::atoi(maxstanzas.data());
              // Ignore any other value, because this is too complex to implement,
              // so I won’t do it.
              if (history->get_tag("maxchars") == "0")
                history_limit.stanzas = 0;
            }
          bridge->join_irc_channel(iid, to.resource, password ? password->get_inner(): "",
                                   from.resource, history_limit, x != nullptr);
          const IrcClient* irc = bridge->find_irc_client(iid.get_server());
          if (irc)
            {
              const auto chan = irc->find_channel(iid.get_local());
              if (chan->joined)
                bridge->send_irc_nick_change(iid, to.resource, from.resource);
              else if (!x)
                { // send an error if we are not joined yet, instead of treating it as a join
                  this->send_stanza_error("presence", from_str, to_str, id,
                                          "modify", "not-acceptable",
                                          "You are not joined to this MUC.");
                }
            }
          // if there is no <x/>, this is a presence status update, we don’t care about those
          if (x)
            {
              const XmlNode* password = x->get_child("password", MUC_NS);
              const XmlNode* history = x->get_child("history", MUC_NS);
              HistoryLimit history_limit;
              if (history)
                {
                  const auto seconds = history->get_tag("seconds");
                  if (!seconds.empty())
                    {
                      const auto now = std::chrono::system_clock::now();
                      std::time_t timestamp = std::chrono::system_clock::to_time_t(now);
                      int int_seconds = std::atoi(seconds.data());
                      timestamp -= int_seconds;
                      history_limit.since = utils::to_string(timestamp);
                    }
                  const auto since = history->get_tag("since");
                  if (!since.empty())
                    history_limit.since = since;
                  const auto maxstanzas = history->get_tag("maxstanzas");
                  if (!maxstanzas.empty())
                    history_limit.stanzas = std::atoi(maxstanzas.data());
                  // Ignore any other value, because this is too complex to implement,
                  // so I won’t do it.
                  if (history->get_tag("maxchars") == "0")
                    history_limit.stanzas = 0;
                }
              bridge->join_irc_channel(iid, to.resource, password ? password->get_inner(): "",
                                       from.resource, history_limit);
            }
        }
      else if (type == "unavailable")