summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2014-06-19 22:21:49 +0200
committerFlorent Le Coz <louiz@louiz.org>2014-06-19 22:21:49 +0200
commit26ffc8fe121e03e1b663aa0015a71b0fc914f95e (patch)
tree098ca791e05911ecdc7ae22620c263669e37f7d3 /src
parenta705b9af7b1bbce6b6c94788398a4cff9cad9ec9 (diff)
downloadbiboumi-26ffc8fe121e03e1b663aa0015a71b0fc914f95e.tar.gz
biboumi-26ffc8fe121e03e1b663aa0015a71b0fc914f95e.tar.bz2
biboumi-26ffc8fe121e03e1b663aa0015a71b0fc914f95e.tar.xz
biboumi-26ffc8fe121e03e1b663aa0015a71b0fc914f95e.zip
Implement a way to add callbacks, waiting for an IRC event to return an iq
Diffstat (limited to 'src')
-rw-r--r--src/bridge/bridge.cpp18
-rw-r--r--src/bridge/bridge.hpp26
-rw-r--r--src/irc/iid.cpp8
-rw-r--r--src/irc/iid.hpp2
-rw-r--r--src/irc/irc_client.cpp7
-rw-r--r--src/xmpp/xmpp_component.cpp12
6 files changed, 68 insertions, 5 deletions
diff --git a/src/bridge/bridge.cpp b/src/bridge/bridge.cpp
index a0964df..c2ac11f 100644
--- a/src/bridge/bridge.cpp
+++ b/src/bridge/bridge.cpp
@@ -4,6 +4,7 @@
#include <irc/irc_message.hpp>
#include <network/poller.hpp>
#include <utils/encoding.hpp>
+#include <utils/tolower.hpp>
#include <logger/logger.hpp>
#include <utils/split.hpp>
#include <stdexcept>
@@ -369,3 +370,20 @@ void Bridge::remove_preferred_from_jid(const std::string& nick)
if (it != this->preferred_user_from.end())
this->preferred_user_from.erase(it);
}
+
+void Bridge::add_waiting_iq(iq_responder_callback_t&& callback)
+{
+ this->waiting_iq.emplace_back(std::move(callback));
+}
+
+void Bridge::trigger_response_iq(const std::string& irc_hostname, const IrcMessage& message)
+{
+ auto it = this->waiting_iq.begin();
+ while (it != this->waiting_iq.end())
+ {
+ if ((*it)(irc_hostname, message) == true)
+ it = this->waiting_iq.erase(it);
+ else
+ ++it;
+ }
+}
diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp
index 8711829..0983595 100644
--- a/src/bridge/bridge.hpp
+++ b/src/bridge/bridge.hpp
@@ -7,6 +7,7 @@
#include <irc/iid.hpp>
#include <unordered_map>
+#include <functional>
#include <string>
#include <memory>
@@ -14,6 +15,14 @@ class XmppComponent;
class Poller;
/**
+ * 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
+ * removed from the list), otherwise it must do nothing and just return
+ * false.
+ */
+typedef std::function<bool(const std::string& irc_hostname, const IrcMessage& message)> iq_responder_callback_t;
+
+/**
* One bridge is spawned for each XMPP user that uses the component. The
* bridge spawns IrcClients when needed (when the user wants to join a
* channel on a new server) and does the translation between the two
@@ -131,6 +140,16 @@ public:
* Remove the preferred jid for the given IRC nick
*/
void remove_preferred_from_jid(const std::string& nick);
+ /**
+ * Add a callback to the waiting iq list.
+ */
+ void add_waiting_iq(iq_responder_callback_t&& callback);
+ /**
+ * Iter over all the waiting_iq, call the iq_responder_filter_t for each,
+ * whenever one of them returns true: call the corresponding
+ * iq_responder_callback_t and remove the callback from the list.
+ */
+ void trigger_response_iq(const std::string& irc_hostname, const IrcMessage& message);
private:
/**
@@ -173,6 +192,13 @@ private:
* from='#somechan%server@biboumi/ToTo'
*/
std::unordered_map<std::string, std::string> preferred_user_from;
+ /**
+ * A list of callbacks that are waiting for some IrcMessage to trigger a
+ * response. We add callbacks in this list whenever we received an IQ
+ * request and we need a response from IRC to be able to provide the
+ * response iq.
+ */
+ std::list<iq_responder_callback_t> waiting_iq;
Bridge(const Bridge&) = delete;
Bridge(Bridge&& other) = delete;
diff --git a/src/irc/iid.cpp b/src/irc/iid.cpp
index 4893e9e..0bb991f 100644
--- a/src/irc/iid.cpp
+++ b/src/irc/iid.cpp
@@ -20,6 +20,14 @@ Iid::Iid(const std::string& iid):
this->set_server(iid);
}
+Iid::Iid(const Iid& other):
+ is_channel(other.is_channel),
+ is_user(other.is_user),
+ local(other.local),
+ server(other.server)
+{
+}
+
Iid::Iid():
is_channel(false),
is_user(false)
diff --git a/src/irc/iid.hpp b/src/irc/iid.hpp
index 2302a18..c547dea 100644
--- a/src/irc/iid.hpp
+++ b/src/irc/iid.hpp
@@ -43,6 +43,7 @@ class Iid
{
public:
explicit Iid(const std::string& iid);
+ explicit Iid(const Iid&);
explicit Iid();
void set_local(const std::string& loc);
@@ -59,7 +60,6 @@ private:
std::string local;
std::string server;
- Iid(const Iid&) = delete;
Iid(Iid&&) = delete;
Iid& operator=(const Iid&) = delete;
Iid& operator=(Iid&&) = delete;
diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp
index cc54971..d179aaa 100644
--- a/src/irc/irc_client.cpp
+++ b/src/irc/irc_client.cpp
@@ -136,8 +136,11 @@ void IrcClient::parse_in_buffer(const size_t)
if (pos == std::string::npos)
break ;
IrcMessage message(this->in_buf.substr(0, pos));
- log_debug("IRC RECEIVING: " << message);
this->in_buf = this->in_buf.substr(pos + 2, std::string::npos);
+ log_debug("IRC RECEIVING: " << message);
+
+ // Call the standard callback (if any), associated with the command
+ // name that we just received.
auto cb = irc_callbacks.find(message.command);
if (cb != irc_callbacks.end())
{
@@ -149,6 +152,8 @@ void IrcClient::parse_in_buffer(const size_t)
}
else
log_info("No handler for command " << message.command);
+ // Try to find a waiting_iq, which response will be triggered by this IrcMessage
+ this->bridge->trigger_response_iq(this->hostname, message);
}
}
diff --git a/src/xmpp/xmpp_component.cpp b/src/xmpp/xmpp_component.cpp
index da9cded..901e168 100644
--- a/src/xmpp/xmpp_component.cpp
+++ b/src/xmpp/xmpp_component.cpp
@@ -220,14 +220,20 @@ void XmppComponent::send_stream_error(const std::string& name, const std::string
}
void XmppComponent::send_stanza_error(const std::string& kind, const std::string& to, const std::string& from,
- const std::string& id, const std::string& error_type,
- const std::string& defined_condition, const std::string& text)
+ const std::string& id, const std::string& error_type,
+ const std::string& defined_condition, const std::string& text,
+ const bool fulljid)
{
Stanza node(kind);
if (!to.empty())
node["to"] = to;
if (!from.empty())
- node["from"] = from;
+ {
+ if (fulljid)
+ node["from"] = from;
+ else
+ node["from"] = from + "@" + this->served_hostname;
+ }
if (!id.empty())
node["id"] = id;
node["type"] = "error";