~singpolyma/biboumi

7c671499350e22f8bfba2f72b9827aa5b200f7b0 — Florent Le Coz 9 years ago f38b31a
Implement part and join, both ways
M src/bridge/bridge.cpp => src/bridge/bridge.cpp +12 -0
@@ 82,11 82,23 @@ void Bridge::send_channel_message(const Iid& iid, const std::string& body)
  this->xmpp->send_muc_message(iid.chan + "%" + iid.server, irc->get_own_nick(), body, this->user_jid);
}

void Bridge::leave_irc_channel(Iid&& iid, std::string&& status_message)
{
  IrcClient* irc = this->get_irc_client(iid.server);
  if (irc)
    irc->send_part_command(iid.chan, status_message);
}

void Bridge::send_muc_message(const Iid& iid, const std::string& nick, const std::string& body)
{
  this->xmpp->send_muc_message(iid.chan + "%" + iid.server, nick, this->sanitize_for_xmpp(body), this->user_jid);
}

void Bridge::send_muc_leave(Iid&& iid, std::string&& nick, std::string&& message, const bool self)
{
  this->xmpp->send_muc_leave(std::move(iid.chan) + "%" + std::move(iid.server), std::move(nick), this->sanitize_for_xmpp(message), this->user_jid, self);
}

void Bridge::send_xmpp_message(const std::string& from, const std::string& author, const std::string& msg)
{
  std::string body;

M src/bridge/bridge.hpp => src/bridge/bridge.hpp +6 -0
@@ 32,6 32,7 @@ public:

  void join_irc_channel(const Iid& iid, const std::string& username);
  void send_channel_message(const Iid& iid, const std::string& body);
  void leave_irc_channel(Iid&& iid, std::string&& status_message);

  /***
   **


@@ 60,6 61,11 @@ public:
   * Send a MUC message from some participant
   */
  void send_muc_message(const Iid& iid, const std::string& nick, const std::string& body);
  /**
   * Send an unavailable presence from this participant
   */
  void send_muc_leave(Iid&& iid, std::string&& nick, std::string&& message, const bool self);

private:
  /**
   * Returns the client for the given hostname, create one (and use the

M src/irc/irc_channel.cpp => src/irc/irc_channel.cpp +24 -0
@@ 22,3 22,27 @@ IrcUser* IrcChannel::get_self() const
{
  return this->self.get();
}

IrcUser* IrcChannel::find_user(const std::string& name)
{
  IrcUser user(name);
  for (const auto& u: this->users)
    {
      if (u->nick == user.nick)
        return u.get();
    }
  return nullptr;
}

void IrcChannel::remove_user(const IrcUser* user)
{
  for (auto it = this->users.begin(); it != this->users.end(); ++it)
    {
      IrcUser* u = it->get();
      if (u->nick == user->nick)
        {
          this->users.erase(it);
          break ;
        }
    }
}

M src/irc/irc_channel.hpp => src/irc/irc_channel.hpp +2 -0
@@ 20,6 20,8 @@ public:
  void set_self(const std::string& name);
  IrcUser* get_self() const;
  IrcUser* add_user(const std::string& name);
  IrcUser* find_user(const std::string& name);
  void remove_user(const IrcUser* user);

private:
  std::unique_ptr<IrcUser> self;

M src/irc/irc_client.cpp => src/irc/irc_client.cpp +47 -7
@@ 76,7 76,7 @@ void IrcClient::parse_in_buffer()
               message.command == "372")
        this->forward_server_message(message);
      else if (message.command == "JOIN")
        this->on_self_channel_join(message);
        this->on_channel_join(message);
      else if (message.command == "PRIVMSG")
        this->on_channel_message(message);
      else if (message.command == "353")


@@ 87,6 87,8 @@ void IrcClient::parse_in_buffer()
        this->on_channel_completely_joined(message);
      else if (message.command == "001")
        this->on_welcome_message(message);
      else if (message.command == "PART")
        this->on_part(message);
    }
}



@@ 128,9 130,7 @@ void IrcClient::send_join_command(const std::string& chan_name)
      this->channels_to_join.push_back(chan_name);
      return ;
    }
  IrcChannel* channel = this->get_channel(chan_name);
  if (channel->joined == false)
    this->send_message(IrcMessage("JOIN", {chan_name}));
  this->send_message(IrcMessage("JOIN", {chan_name}));
}

bool IrcClient::send_channel_message(const std::string& chan_name, const std::string& body)


@@ 145,6 145,15 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st
  return true;
}

void IrcClient::send_part_command(const std::string& chan_name, const std::string& status_message)
{
  IrcChannel* channel = this->get_channel(chan_name);
  if (channel->joined == true)
    {
      this->send_message(IrcMessage("PART", {chan_name, status_message}));
    }
}

void IrcClient::send_pong_command(const IrcMessage& message)
{
  const std::string id = message.arguments[0];


@@ 175,12 184,21 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message)
    }
}

void IrcClient::on_self_channel_join(const IrcMessage& message)
void IrcClient::on_channel_join(const IrcMessage& message)
{
  const std::string chan_name = message.arguments[0];
  IrcChannel* channel = this->get_channel(chan_name);
  channel->joined = true;
  channel->set_self(message.prefix);
  const std::string nick = message.prefix;
  if (channel->joined == false)
    {
      channel->joined = true;
      channel->set_self(nick);
    }
  else
    {
      IrcUser* user = channel->add_user(nick);
      this->bridge->send_user_join(this->hostname, chan_name, user->nick);
    }
}

void IrcClient::on_channel_message(const IrcMessage& message)


@@ 217,3 235,25 @@ void IrcClient::on_welcome_message(const IrcMessage& message)
    this->send_join_command(chan_name);
  this->channels_to_join.clear();
}

void IrcClient::on_part(const IrcMessage& message)
{
  const std::string chan_name = message.arguments[0];
  IrcChannel* channel = this->get_channel(chan_name);
  std::string txt;
  if (message.arguments.size() >= 2)
    txt = message.arguments[1];
  const IrcUser* user = channel->find_user(message.prefix);
  if (user)
    {
      std::string nick = user->nick;
      channel->remove_user(user);
      Iid iid;
      iid.chan = chan_name;
      iid.server = this->hostname;
      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;
    }
}

M src/irc/irc_client.hpp => src/irc/irc_client.hpp +9 -1
@@ 76,6 76,10 @@ public:
   */
  bool send_channel_message(const std::string& chan_name, const std::string& body);
  /**
   * Send the PART irc command
   */
  void send_part_command(const std::string& chan_name, const std::string& status_message);
  /**
   * Forward the server message received from IRC to the XMPP component
   */
  void forward_server_message(const IrcMessage& message);


@@ 88,7 92,7 @@ public:
   * Remember our nick and host, when we are joined to the channel. The list
   * of user comes after so we do not send the self-presence over XMPP yet.
   */
  void on_self_channel_join(const IrcMessage& message);
  void on_channel_join(const IrcMessage& message);
  /**
   * When a channel message is received
   */


@@ 106,6 110,10 @@ public:
   * When a message 001 is received, join the rooms we wanted to join, and set our actual nickname
   */
  void on_welcome_message(const IrcMessage& message);
  /**
   * When a PART message is received
   */
  void on_part(const IrcMessage& message);

private:
  /**

M src/irc/irc_user.cpp => src/irc/irc_user.cpp +6 -2
@@ 7,14 7,18 @@ IrcUser::IrcUser(const std::string& name)
  const std::string::size_type sep = name.find("!");
  if (sep == std::string::npos)
    {
      if (name[0] == '@' || name[0] == '+')
      if (name[0] == '~' || name[0] == '&'
          || name[0] == '@' || name[0] == '%'
          || name[0] == '+')
        this->nick = name.substr(1);
      else
        this->nick = name;
    }
  else
    {
      if (name[0] == '@' || name[0] == '+')
      if (name[0] == '~' || name[0] == '&'
          || name[0] == '@' || name[0] == '%'
          || name[0] == '+')
        this->nick = name.substr(1, sep);
      else
        this->nick = name.substr(0, sep);

M src/xmpp/xmpp_component.cpp => src/xmpp/xmpp_component.cpp +36 -2
@@ 149,8 149,22 @@ void XmppComponent::handle_presence(const Stanza& stanza)
  Bridge* bridge = this->get_user_bridge(stanza["from"]);
  Jid to(stanza["to"]);
  Iid iid(to.local);
  if (!iid.chan.empty() && !iid.server.empty())
    bridge->join_irc_channel(iid, to.resource);
  std::string type;
  try {
    type = stanza["type"];
  }
  catch (const AttributeNotFound&) {}

  if (!iid.chan.empty() && !iid.chan.empty())
    { // presence toward a MUC that corresponds to an irc channel
      if (type.empty())
        bridge->join_irc_channel(iid, to.resource);
      else if (type == "unavailable")
        {
          XmlNode* status = stanza.get_child("status");
          bridge->leave_irc_channel(std::move(iid), status ? std::move(status->get_inner()) : "");
        }
    }
}

void XmppComponent::handle_message(const Stanza& stanza)


@@ 269,3 283,23 @@ void XmppComponent::send_muc_message(const std::string& muc_name, const std::str
  message.close();
  this->send_stanza(message);
}

void XmppComponent::send_muc_leave(std::string&& muc_name, std::string&& nick, std::string&& message, const std::string& jid_to, const bool self)
{
  Stanza presence("presence");
  presence["to"] = jid_to;
  presence["from"] = muc_name + "@" + this->served_hostname + "/" + nick;
  presence["type"] = "unavailable";
  if (!message.empty() || self)
    {
      XmlNode status("status");
      if (!message.empty())
        status.set_inner(std::move(message));
      if (self)
        status["code"] = "110";
      status.close();
      presence.add_child(std::move(status));
    }
  presence.close();
  this->send_stanza(presence);
}

M src/xmpp/xmpp_component.hpp => src/xmpp/xmpp_component.hpp +4 -0
@@ 76,6 76,10 @@ public:
   */
  void send_muc_message(const std::string& muc_name, const std::string& nick, const std::string body_str, const std::string& jid_to);
  /**
   * Send an unavailable presence for this nick
   */
  void send_muc_leave(std::string&& muc_name, std::string&& nick, std::string&& message, const std::string& jid_to, const bool self);
  /**
   * Handle the various stanza types
   */
  void handle_handshake(const Stanza& stanza);

M src/xmpp/xmpp_stanza.cpp => src/xmpp/xmpp_stanza.cpp +2 -0
@@ 1,5 1,7 @@
#include <xmpp/xmpp_stanza.hpp>

#include <utils/encoding.hpp>

#include <iostream>

std::string xml_escape(const std::string& data)