Commit e1d69806 authored by louiz’'s avatar louiz’

Rename iq_responder_callback_t to irc_… and add the equivalent to wait for iqs

parent dccd8039
#include <bridge/bridge.hpp> #include <bridge/bridge.hpp>
#include <bridge/colors.hpp> #include <bridge/colors.hpp>
#include <xmpp/xmpp_component.hpp> #include <xmpp/xmpp_component.hpp>
#include <xmpp/xmpp_stanza.hpp>
#include <irc/irc_message.hpp> #include <irc/irc_message.hpp>
#include <network/poller.hpp> #include <network/poller.hpp>
#include <utils/encoding.hpp> #include <utils/encoding.hpp>
...@@ -220,10 +221,14 @@ void Bridge::send_irc_kick(const Iid& iid, const std::string& target, const std: ...@@ -220,10 +221,14 @@ void Bridge::send_irc_kick(const Iid& iid, const std::string& target, const std:
const std::string& iq_id, const std::string& to_jid) const std::string& iq_id, const std::string& to_jid)
{ {
IrcClient* irc = this->get_irc_client(iid.get_server()); IrcClient* irc = this->get_irc_client(iid.get_server());
if (irc)
{ if (!irc)
return;
irc->send_kick_command(iid.get_local(), target, reason); irc->send_kick_command(iid.get_local(), target, reason);
this->add_waiting_iq([this, target, iq_id, to_jid, iid](const std::string& irc_hostname, const IrcMessage& message){ irc_responder_callback_t cb = [this, target, iq_id, to_jid, iid](const std::string& irc_hostname,
const IrcMessage& message) -> bool
{
if (irc_hostname != iid.get_server()) if (irc_hostname != iid.get_server())
return false; return false;
if (message.command == "KICK" && message.arguments.size() >= 2) if (message.command == "KICK" && message.arguments.size() >= 2)
...@@ -257,8 +262,8 @@ void Bridge::send_irc_kick(const Iid& iid, const std::string& target, const std: ...@@ -257,8 +262,8 @@ void Bridge::send_irc_kick(const Iid& iid, const std::string& target, const std:
error_message, false); error_message, false);
} }
return true; return true;
}); };
} this->add_waiting_irc(std::move(cb));
} }
void Bridge::set_channel_topic(const Iid& iid, const std::string& subject) void Bridge::set_channel_topic(const Iid& iid, const std::string& subject)
...@@ -284,7 +289,8 @@ void Bridge::send_irc_version_request(const std::string& irc_hostname, const std ...@@ -284,7 +289,8 @@ void Bridge::send_irc_version_request(const std::string& irc_hostname, const std
// TODO, add a timer to remove that waiting iq if the server does not // TODO, add a timer to remove that waiting iq if the server does not
// respond with a matching command before n seconds // respond with a matching command before n seconds
this->add_waiting_iq([this, target, iq_id, to_jid, irc_hostname, from_jid](const std::string& hostname, const IrcMessage& message){ irc_responder_callback_t cb = [this, target, iq_id, to_jid, irc_hostname, from_jid](const std::string& hostname, const IrcMessage& message) -> bool
{
if (irc_hostname != hostname) if (irc_hostname != hostname)
return false; return false;
IrcUser user(message.prefix); IrcUser user(message.prefix);
...@@ -307,10 +313,10 @@ void Bridge::send_irc_version_request(const std::string& irc_hostname, const std ...@@ -307,10 +313,10 @@ void Bridge::send_irc_version_request(const std::string& irc_hostname, const std
return true; return true;
} }
return false; return false;
}); };
this->add_waiting_irc(std::move(cb));
} }
void Bridge::send_message(const Iid& iid, const std::string& nick, const std::string& body, const bool muc) void Bridge::send_message(const Iid& iid, const std::string& nick, const std::string& body, const bool muc)
{ {
if (muc) if (muc)
...@@ -447,18 +453,18 @@ void Bridge::remove_preferred_from_jid(const std::string& nick) ...@@ -447,18 +453,18 @@ void Bridge::remove_preferred_from_jid(const std::string& nick)
this->preferred_user_from.erase(it); this->preferred_user_from.erase(it);
} }
void Bridge::add_waiting_iq(iq_responder_callback_t&& callback) void Bridge::add_waiting_irc(irc_responder_callback_t&& callback)
{ {
this->waiting_iq.emplace_back(std::move(callback)); this->waiting_irc.emplace_back(std::move(callback));
} }
void Bridge::trigger_response_iq(const std::string& irc_hostname, const IrcMessage& message) void Bridge::trigger_on_irc_message(const std::string& irc_hostname, const IrcMessage& message)
{ {
auto it = this->waiting_iq.begin(); auto it = this->waiting_irc.begin();
while (it != this->waiting_iq.end()) while (it != this->waiting_irc.end())
{ {
if ((*it)(irc_hostname, message) == true) if ((*it)(irc_hostname, message) == true)
it = this->waiting_iq.erase(it); it = this->waiting_irc.erase(it);
else else
++it; ++it;
} }
......
...@@ -17,11 +17,11 @@ class Poller; ...@@ -17,11 +17,11 @@ class Poller;
/** /**
* A callback called for each IrcMessage we receive. If the message triggers * A callback called for each IrcMessage we receive. If the message triggers
* a response, it must send an iq and return true (in that case it is * a response, it must send ore or more iq and return true (in that case it
* removed from the list), otherwise it must do nothing and just return * is removed from the list), otherwise it must do nothing and just return
* false. * false.
*/ */
typedef std::function<bool(const std::string& irc_hostname, const IrcMessage& message)> iq_responder_callback_t; using irc_responder_callback_t = std::function<bool(const std::string& irc_hostname, const IrcMessage& message)>;
/** /**
* One bridge is spawned for each XMPP user that uses the component. The * One bridge is spawned for each XMPP user that uses the component. The
...@@ -146,15 +146,15 @@ public: ...@@ -146,15 +146,15 @@ public:
*/ */
void remove_preferred_from_jid(const std::string& nick); void remove_preferred_from_jid(const std::string& nick);
/** /**
* Add a callback to the waiting iq list. * Add a callback to the waiting list of irc callbacks.
*/ */
void add_waiting_iq(iq_responder_callback_t&& callback); void add_waiting_irc(irc_responder_callback_t&& callback);
/** /**
* Iter over all the waiting_iq, call the iq_responder_filter_t for each, * Iter over all the waiting_iq, call the iq_responder_filter_t for each,
* whenever one of them returns true: call the corresponding * whenever one of them returns true: call the corresponding
* iq_responder_callback_t and remove the callback from the list. * iq_responder_callback_t and remove the callback from the list.
*/ */
void trigger_response_iq(const std::string& irc_hostname, const IrcMessage& message); void trigger_on_irc_message(const std::string& irc_hostname, const IrcMessage& message);
private: private:
/** /**
...@@ -203,7 +203,7 @@ private: ...@@ -203,7 +203,7 @@ private:
* request and we need a response from IRC to be able to provide the * request and we need a response from IRC to be able to provide the
* response iq. * response iq.
*/ */
std::list<iq_responder_callback_t> waiting_iq; std::list<irc_responder_callback_t> waiting_irc;
Bridge(const Bridge&) = delete; Bridge(const Bridge&) = delete;
Bridge(Bridge&& other) = delete; Bridge(Bridge&& other) = delete;
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
class Iid class Iid
{ {
public: public:
explicit Iid(const std::string& iid); Iid(const std::string& iid);
explicit Iid(const Iid&); explicit Iid(const Iid&);
explicit Iid(); explicit Iid();
......
...@@ -156,7 +156,7 @@ void IrcClient::parse_in_buffer(const size_t) ...@@ -156,7 +156,7 @@ void IrcClient::parse_in_buffer(const size_t)
else else
log_info("No handler for command " << message.command); log_info("No handler for command " << message.command);
// Try to find a waiting_iq, which response will be triggered by this IrcMessage // Try to find a waiting_iq, which response will be triggered by this IrcMessage
this->bridge->trigger_response_iq(this->hostname, message); this->bridge->trigger_on_irc_message(this->hostname, message);
} }
} }
......
...@@ -577,6 +577,15 @@ void XmppComponent::handle_iq(const Stanza& stanza) ...@@ -577,6 +577,15 @@ void XmppComponent::handle_iq(const Stanza& stanza)
const Iid iid(to.local); const Iid iid(to.local);
bridge->send_xmpp_version_to_irc(iid, name, version, os); bridge->send_xmpp_version_to_irc(iid, name, version, os);
} }
else
{
const auto it = this->waiting_iq.find(id);
if (it != this->waiting_iq.end())
{
it->second(bridge, stanza);
this->waiting_iq.erase(it);
}
}
} }
error_type = "cancel"; error_type = "cancel";
error_name = "feature-not-implemented"; error_name = "feature-not-implemented";
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <unordered_map> #include <unordered_map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <map>
#define STREAM_NS "http://etherx.jabber.org/streams" #define STREAM_NS "http://etherx.jabber.org/streams"
#define COMPONENT_NS "jabber:component:accept" #define COMPONENT_NS "jabber:component:accept"
...@@ -23,6 +24,11 @@ ...@@ -23,6 +24,11 @@
#define STREAMS_NS "urn:ietf:params:xml:ns:xmpp-streams" #define STREAMS_NS "urn:ietf:params:xml:ns:xmpp-streams"
#define VERSION_NS "jabber:iq:version" #define VERSION_NS "jabber:iq:version"
#define ADHOC_NS "http://jabber.org/protocol/commands" #define ADHOC_NS "http://jabber.org/protocol/commands"
/**
* A callback called when the waited iq result is received (it is matched
* against the iq id)
*/
using iq_responder_callback_t = std::function<void(Bridge* bridge, const Stanza& stanza)>;
/** /**
* An XMPP component, communicating with an XMPP server using the protocole * An XMPP component, communicating with an XMPP server using the protocole
...@@ -256,6 +262,14 @@ private: ...@@ -256,6 +262,14 @@ private:
std::unordered_map<std::string, std::function<void(const Stanza&)>> stanza_handlers; std::unordered_map<std::string, std::function<void(const Stanza&)>> stanza_handlers;
AdhocCommandsHandler adhoc_commands_handler; AdhocCommandsHandler adhoc_commands_handler;
/**
* A map of id -> callback. When we want to wait for an iq result, we add
* the callback to this map, with the iq id as the key. When an iq result
* is received, we look for a corresponding callback in this map. If
* found, we call it and remove it.
*/
std::map<std::string, iq_responder_callback_t> waiting_iq;
/** /**
* One bridge for each user of the component. Indexed by the user's full * One bridge for each user of the component. Indexed by the user's full
* jid * jid
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment