summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-10-21 20:21:45 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-10-21 20:21:45 (GMT)
commitaf61db1e108d17afcf84a97492dd6b79426688ec (patch)
treefd765135ebbb67be56ea4eefbfed73c05c00d214 /Swiften/Queries
parentd0c2985c34b65f0d85d35948bb0cfadd3b2af7bd (diff)
downloadswift-contrib-af61db1e108d17afcf84a97492dd6b79426688ec.zip
swift-contrib-af61db1e108d17afcf84a97492dd6b79426688ec.tar.bz2
Make sure Component always sets 'from' on outgoing IQ stanzas.
Diffstat (limited to 'Swiften/Queries')
-rw-r--r--Swiften/Queries/IQRouter.cpp5
-rw-r--r--Swiften/Queries/IQRouter.h24
-rw-r--r--Swiften/Queries/UnitTest/IQRouterTest.cpp30
3 files changed, 54 insertions, 5 deletions
diff --git a/Swiften/Queries/IQRouter.cpp b/Swiften/Queries/IQRouter.cpp
index f40d248..a1689e9 100644
--- a/Swiften/Queries/IQRouter.cpp
+++ b/Swiften/Queries/IQRouter.cpp
@@ -40,7 +40,7 @@ void IQRouter::handleIQ(boost::shared_ptr<IQ> iq) {
}
}
if (!handled && (iq->getType() == IQ::Get || iq->getType() == IQ::Set) ) {
- channel_->sendIQ(IQ::createError(iq->getFrom(), iq->getID(), ErrorPayload::FeatureNotImplemented, ErrorPayload::Cancel));
+ sendIQ(IQ::createError(iq->getFrom(), iq->getID(), ErrorPayload::FeatureNotImplemented, ErrorPayload::Cancel));
}
processPendingRemoves();
@@ -77,6 +77,9 @@ void IQRouter::removeHandler(boost::shared_ptr<IQHandler> handler) {
}
void IQRouter::sendIQ(boost::shared_ptr<IQ> iq) {
+ if (from_.isValid()) {
+ iq->setFrom(from_);
+ }
channel_->sendIQ(iq);
}
diff --git a/Swiften/Queries/IQRouter.h b/Swiften/Queries/IQRouter.h
index eab2848..42fa6e9 100644
--- a/Swiften/Queries/IQRouter.h
+++ b/Swiften/Queries/IQRouter.h
@@ -4,8 +4,7 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_IQRouter_H
-#define SWIFTEN_IQRouter_H
+#pragma once
#include <boost/shared_ptr.hpp>
#include <vector>
@@ -22,11 +21,29 @@ namespace Swift {
IQRouter(IQChannel* channel);
~IQRouter();
+ /**
+ * Sets the 'from' JID for all outgoing IQ stanzas.
+ *
+ * By default, IQRouter does not add a from to IQ stanzas, since
+ * this is automatically added by the server. This overrides this
+ * default behavior, which is necessary for e.g. components.
+ */
+ void setFrom(const JID& from) {
+ from_ = from;
+ }
+
void addHandler(IQHandler* handler);
void removeHandler(IQHandler* handler);
void addHandler(boost::shared_ptr<IQHandler> handler);
void removeHandler(boost::shared_ptr<IQHandler> handler);
+ /**
+ * Sends an IQ stanza.
+ *
+ * If a JID was specified using setFrom, the JID will
+ * be set as the 'from' address on iq before sending
+ * it.
+ */
void sendIQ(boost::shared_ptr<IQ> iq);
String getNewIQID();
@@ -38,10 +55,9 @@ namespace Swift {
private:
IQChannel* channel_;
+ JID from_;
std::vector< boost::shared_ptr<IQHandler> > handlers_;
std::vector< boost::shared_ptr<IQHandler> > queuedRemoves_;
bool queueRemoves_;
};
}
-
-#endif
diff --git a/Swiften/Queries/UnitTest/IQRouterTest.cpp b/Swiften/Queries/UnitTest/IQRouterTest.cpp
index 919711e..2d6b2cb 100644
--- a/Swiften/Queries/UnitTest/IQRouterTest.cpp
+++ b/Swiften/Queries/UnitTest/IQRouterTest.cpp
@@ -24,6 +24,9 @@ class IQRouterTest : public CppUnit::TestFixture
CPPUNIT_TEST(testHandleIQ_SuccesfulHandlerLast);
CPPUNIT_TEST(testHandleIQ_NoSuccesfulHandler);
CPPUNIT_TEST(testHandleIQ_HandlerRemovedDuringHandle);
+ CPPUNIT_TEST(testSendIQ_WithFrom);
+ CPPUNIT_TEST(testSendIQ_WithoutFrom);
+ CPPUNIT_TEST(testHandleIQ_WithFrom);
CPPUNIT_TEST_SUITE_END();
public:
@@ -109,6 +112,33 @@ class IQRouterTest : public CppUnit::TestFixture
CPPUNIT_ASSERT_EQUAL(2, handler2.called);
}
+ void testSendIQ_WithFrom() {
+ IQRouter testling(channel_);
+ testling.setFrom(JID("foo@bar.com/baz"));
+
+ testling.sendIQ(boost::shared_ptr<IQ>(new IQ()));
+
+ CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/baz"), channel_->iqs_[0]->getFrom());
+ }
+
+ void testSendIQ_WithoutFrom() {
+ IQRouter testling(channel_);
+
+ testling.sendIQ(boost::shared_ptr<IQ>(new IQ()));
+
+ CPPUNIT_ASSERT_EQUAL(JID(), channel_->iqs_[0]->getFrom());
+ }
+
+ void testHandleIQ_WithFrom() {
+ IQRouter testling(channel_);
+ testling.setFrom(JID("foo@bar.com/baz"));
+ DummyIQHandler handler(false, &testling);
+
+ channel_->onIQReceived(boost::shared_ptr<IQ>(new IQ()));
+
+ CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/baz"), channel_->iqs_[0]->getFrom());
+ }
+
private:
struct DummyIQHandler : public IQHandler {
DummyIQHandler(bool handle, IQRouter* router) : handle(handle), router(router), called(0) {