From 2b630292e8e26e07a77e350024ba45c625f8dd95 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Tue, 14 Sep 2010 22:05:27 +0200
Subject: Print warning on non-validating hashes.


diff --git a/Swiften/Disco/CapsManager.cpp b/Swiften/Disco/CapsManager.cpp
index 185f8e6..a5023d3 100644
--- a/Swiften/Disco/CapsManager.cpp
+++ b/Swiften/Disco/CapsManager.cpp
@@ -16,7 +16,7 @@
 
 namespace Swift {
 
-CapsManager::CapsManager(CapsStorage* capsStorage, StanzaChannel* stanzaChannel, IQRouter* iqRouter) : iqRouter(iqRouter), capsStorage(capsStorage) {
+CapsManager::CapsManager(CapsStorage* capsStorage, StanzaChannel* stanzaChannel, IQRouter* iqRouter) : iqRouter(iqRouter), capsStorage(capsStorage), warnOnInvalidHash(true) {
 	stanzaChannel->onPresenceReceived.connect(boost::bind(&CapsManager::handlePresenceReceived, this, _1));
 	stanzaChannel->onAvailableChanged.connect(boost::bind(&CapsManager::handleStanzaChannelAvailableChanged, this, _1));
 }
@@ -51,6 +51,9 @@ void CapsManager::handleStanzaChannelAvailableChanged(bool available) {
 void CapsManager::handleDiscoInfoReceived(const JID& from, const String& hash, DiscoInfo::ref discoInfo, const boost::optional<ErrorPayload>& error) {
 	requestedDiscoInfos.erase(hash);
 	if (error || CapsInfoGenerator("").generateCapsInfo(*discoInfo.get()).getVersion() != hash) {
+		if (warnOnInvalidHash && !error) {
+			std::cerr << "Warning: Caps from " << from.toString() << " do not verify" << std::endl;
+		}
 		failingCaps.insert(std::make_pair(from, hash));
 		std::map<String, std::set< std::pair<JID, String> > >::iterator i = fallbacks.find(hash);
 		if (i != fallbacks.end() && !i->second.empty()) {
diff --git a/Swiften/Disco/CapsManager.h b/Swiften/Disco/CapsManager.h
index 3188a07..e2a8901 100644
--- a/Swiften/Disco/CapsManager.h
+++ b/Swiften/Disco/CapsManager.h
@@ -26,6 +26,11 @@ namespace Swift {
 
 			DiscoInfo::ref getCaps(const String&) const;
 
+			// Mainly for testing purposes
+			void setWarnOnInvalidHash(bool b) {
+				warnOnInvalidHash = b;
+			}
+
 		private:
 			void handlePresenceReceived(boost::shared_ptr<Presence>);
 			void handleStanzaChannelAvailableChanged(bool);
@@ -35,6 +40,7 @@ namespace Swift {
 		private:
 			IQRouter* iqRouter;
 			CapsStorage* capsStorage;
+			bool warnOnInvalidHash;
 			std::set<String> requestedDiscoInfos;
 			std::set< std::pair<JID, String> > failingCaps;
 			std::map<String, std::set< std::pair<JID, String> > > fallbacks;
diff --git a/Swiften/Disco/UnitTest/CapsManagerTest.cpp b/Swiften/Disco/UnitTest/CapsManagerTest.cpp
index 7925ae7..d5b9896 100644
--- a/Swiften/Disco/UnitTest/CapsManagerTest.cpp
+++ b/Swiften/Disco/UnitTest/CapsManagerTest.cpp
@@ -234,6 +234,7 @@ class CapsManagerTest : public CppUnit::TestFixture {
 	private:
 		std::auto_ptr<CapsManager> createManager() {
 			std::auto_ptr<CapsManager> manager(new CapsManager(storage, stanzaChannel, iqRouter));
+			manager->setWarnOnInvalidHash(false);
 			//manager->onCapsChanged.connect(boost::bind(&CapsManagerTest::handleCapsChanged, this, _1));
 			return manager;
 		}
-- 
cgit v0.10.2-6-g49f6