summaryrefslogtreecommitdiff
path: root/src/bridge
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2013-11-07 01:53:09 +0100
committerFlorent Le Coz <louiz@louiz.org>2013-11-07 01:53:09 +0100
commita418b6ed5d70f0e61e71bb1adce2a693ade89e30 (patch)
treef4d979e1dae286df4faa6de3660288495d4ced77 /src/bridge
parent4b76a30d0479f366374c7dcf99ac211038722503 (diff)
downloadbiboumi-a418b6ed5d70f0e61e71bb1adce2a693ade89e30.tar.gz
biboumi-a418b6ed5d70f0e61e71bb1adce2a693ade89e30.tar.bz2
biboumi-a418b6ed5d70f0e61e71bb1adce2a693ade89e30.tar.xz
biboumi-a418b6ed5d70f0e61e71bb1adce2a693ade89e30.zip
Send and receive messages
Also correctly respond to PING with the id, escape some XML content, but not always
Diffstat (limited to 'src/bridge')
-rw-r--r--src/bridge/bridge.cpp43
-rw-r--r--src/bridge/bridge.hpp11
2 files changed, 52 insertions, 2 deletions
diff --git a/src/bridge/bridge.cpp b/src/bridge/bridge.cpp
index 638777d..5047a78 100644
--- a/src/bridge/bridge.cpp
+++ b/src/bridge/bridge.cpp
@@ -2,6 +2,8 @@
#include <xmpp/xmpp_component.hpp>
#include <network/poller.hpp>
+#include <iostream>
+
Bridge::Bridge(const std::string& user_jid, XmppComponent* xmpp, Poller* poller):
user_jid(user_jid),
xmpp(xmpp),
@@ -29,15 +31,54 @@ IrcClient* Bridge::get_irc_client(const std::string& hostname, const std::string
}
}
+IrcClient* Bridge::get_irc_client(const std::string& hostname)
+{
+ try
+ {
+ return this->irc_clients.at(hostname).get();
+ }
+ catch (const std::out_of_range& exception)
+ {
+ return nullptr;
+ }
+}
+
+
void Bridge::join_irc_channel(const Iid& iid, const std::string& username)
{
IrcClient* irc = this->get_irc_client(iid.server, username);
irc->send_join_command(iid.chan);
}
+void Bridge::send_channel_message(const Iid& iid, const std::string& body)
+{
+ if (iid.chan.empty() || iid.server.empty())
+ {
+ std::cout << "Cannot send message to channel: [" << iid.chan << "] on server [" << iid.server << "]" << std::endl;
+ return;
+ }
+ IrcClient* irc = this->get_irc_client(iid.server);
+ if (!irc)
+ {
+ std::cout << "Cannot send message: no client exist for server " << iid.server << std::endl;
+ return;
+ }
+ irc->send_channel_message(iid.chan, body);
+ this->xmpp->send_muc_message(iid.chan + "%" + iid.server, irc->get_own_nick(), body, this->user_jid);
+}
+
+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, body, this->user_jid);
+}
+
void Bridge::send_xmpp_message(const std::string& from, const std::string& author, const std::string& msg)
{
- const std::string body = std::string("[") + author + std::string("] ") + msg;
+ std::string body;
+ if (!author.empty())
+ body = std::string("[") + author + std::string("] ") + msg;
+ else
+ body = msg;
this->xmpp->send_message(from, body, this->user_jid);
}
diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp
index f9ddcca..38cf565 100644
--- a/src/bridge/bridge.hpp
+++ b/src/bridge/bridge.hpp
@@ -30,6 +30,7 @@ public:
**/
void join_irc_channel(const Iid& iid, const std::string& username);
+ void send_channel_message(const Iid& iid, const std::string& body);
/***
**
@@ -54,7 +55,10 @@ public:
* Send the topic of the MUC to the user
*/
void send_topic(const std::string& hostname, const std::string& chan_name, const std::string topic);
-
+ /**
+ * Send a MUC message from some participant
+ */
+ void send_muc_message(const Iid& iid, const std::string& nick, const std::string& body);
private:
/**
* Returns the client for the given hostname, create one (and use the
@@ -63,6 +67,11 @@ private:
*/
IrcClient* get_irc_client(const std::string& hostname, const std::string& username);
/**
+ * This version does not create the IrcClient if it does not exist, and
+ * returns nullptr in that case
+ */
+ IrcClient* get_irc_client(const std::string& hostname);
+ /**
* The JID of the user associated with this bridge. Messages from/to this
* JID are only managed by this bridge.
*/