~singpolyma/biboumi

e8e592d1ace5413a1e7d8b59b9467c78d8d68ea9 — Florent Le Coz 10 years ago aa53276
Remove disconnected IrcClients
M src/bridge/bridge.cpp => src/bridge/bridge.cpp +13 -0
@@ 29,6 29,19 @@ void Bridge::shutdown()
  }
}

void Bridge::clean()
{
  auto it = this->irc_clients.begin();
  while (it != this->irc_clients.end())
  {
    IrcClient* client = it->second.get();
    if (!client->is_connected())
      it = this->irc_clients.erase(it);
    else
      ++it;
  }
}

Xmpp::body Bridge::make_xmpp_body(const std::string& str)
{
  std::string res;

M src/bridge/bridge.hpp => src/bridge/bridge.hpp +4 -1
@@ 28,7 28,10 @@ public:
   * QUIT all connected IRC servers.
   */
  void shutdown();

  /**
   * Remove all inactive IrcClients
   */
  void clean();
  static Xmpp::body make_xmpp_body(const std::string& str);
  /***
   **

M src/irc/irc_client.cpp => src/irc/irc_client.cpp +8 -2
@@ 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);
}


M src/main.cpp => src/main.cpp +3 -0
@@ 81,6 81,9 @@ int main(int ac, char** av)
  const std::chrono::milliseconds timeout(-1);
  while (p.poll(timeout) != -1 || !exiting)
  {
    // Check for empty irc_clients (not connected, or with no joined
    // channel) and remove them
    xmpp_component->clean();
    if (stop)
    {
      log_info("Signal received, exiting...");

M src/xmpp/xmpp_component.cpp => src/xmpp/xmpp_component.cpp +8 -0
@@ 94,6 94,14 @@ void XmppComponent::shutdown()
  }
}

void XmppComponent::clean()
{
  for (auto it = this->bridges.begin(); it != this->bridges.end(); ++it)
  {
    it->second->clean();
  }
}

void XmppComponent::on_remote_stream_open(const XmlNode& node)
{
  log_debug("XMPP DOCUMENT OPEN: " << node.to_string());

M src/xmpp/xmpp_component.hpp => src/xmpp/xmpp_component.hpp +5 -0
@@ 32,6 32,11 @@ public:
  void shutdown();
  bool is_document_open() const;
  /**
   * Run a check on all bridges, to remove all disconnected (socket is
   * closed, or no channel is joined) IrcClients. Some kind of garbage collector.
   */
  void clean();
  /**
   * Connect to the XMPP server.
   * Returns false if we failed to connect
   */