From 94b7aaf53c8b20e03c8081cce49cda14cd5c01da Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Mon, 6 Feb 2012 12:59:12 +0000
Subject: Display mediated invites


diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp
index ecb9288..f8f30ff 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.cpp
+++ b/Swift/Controllers/Chat/ChatControllerBase.cpp
@@ -20,6 +20,7 @@
 #include <Swiften/Client/StanzaChannel.h>
 #include <Swiften/Elements/Delay.h>
 #include <Swiften/Elements/MUCInvitationPayload.h>
+#include <Swiften/Elements/MUCUserPayload.h>
 #include <Swiften/Base/foreach.h>
 #include <Swift/Controllers/XMPPEvents/EventController.h>
 #include <Swiften/Disco/EntityCapsProvider.h>
@@ -181,6 +182,10 @@ void ChatControllerBase::handleIncomingMessage(boost::shared_ptr<MessageEvent> m
 		handleMUCInvitation(messageEvent->getStanza());
 		return;
 	}
+	else if (messageEvent->getStanza()->getPayload<MUCUserPayload>() && messageEvent->getStanza()->getPayload<MUCUserPayload>()->getInvite()) {
+		handleMediatedMUCInvitation(messageEvent->getStanza());
+		return;
+	}
 	else {
 		if (!messageEvent->isReadable()) {
 			return;
@@ -266,4 +271,23 @@ void ChatControllerBase::handleMUCInvitation(Message::ref message) {
 	chatWindow_->addMUCInvitation(invite->getJID(), invite->getReason(), invite->getPassword());
 }
 
+void ChatControllerBase::handleMediatedMUCInvitation(Message::ref message) {
+	MUCUserPayload::Invite invite = *message->getPayload<MUCUserPayload>()->getInvite();
+	JID from;
+	if (invite.from.isValid()) {
+		from = invite.from;
+	}
+	std::string reason;
+	if (!invite.reason.empty()) {
+		reason = invite.reason;
+	}
+	std::string password;
+	if (message->getPayload<MUCUserPayload>()->getPassword()) {
+		password = *message->getPayload<MUCUserPayload>()->getPassword();
+	}
+	chatWindow_->addMUCInvitation(from, reason, password, false);
+}
+
+
+
 }
diff --git a/Swift/Controllers/Chat/ChatControllerBase.h b/Swift/Controllers/Chat/ChatControllerBase.h
index f1ecfe8..e1034d6 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.h
+++ b/Swift/Controllers/Chat/ChatControllerBase.h
@@ -83,6 +83,7 @@ namespace Swift {
 			void handleSecurityLabelsCatalogResponse(boost::shared_ptr<SecurityLabelsCatalog>, ErrorPayload::ref error);
 			void handleDayChangeTick();
 			void handleMUCInvitation(Message::ref message);
+			void handleMediatedMUCInvitation(Message::ref message);
 
 		protected:
 			JID selfJID_;
diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp
index 61c3208..ff00bc1 100644
--- a/Swift/Controllers/Chat/ChatsManager.cpp
+++ b/Swift/Controllers/Chat/ChatsManager.cpp
@@ -29,6 +29,7 @@
 #include <Swiften/Client/NickResolver.h>
 #include <Swiften/MUC/MUCManager.h>
 #include <Swiften/Elements/ChatState.h>
+#include <Swiften/Elements/MUCUserPayload.h>
 #include <Swiften/Elements/DeliveryReceipt.h>
 #include <Swiften/Elements/DeliveryReceiptRequest.h>
 #include <Swiften/MUC/MUCBookmarkManager.h>
@@ -574,7 +575,7 @@ void ChatsManager::handleSearchMUCRequest() {
 void ChatsManager::handleIncomingMessage(boost::shared_ptr<Message> message) {
 	JID jid = message->getFrom();
 	boost::shared_ptr<MessageEvent> event(new MessageEvent(message));
-	bool isInvite = message->getPayload<MUCInvitationPayload>();
+	bool isInvite = message->getPayload<MUCInvitationPayload>() || (message->getPayload<MUCUserPayload>() && message->getPayload<MUCUserPayload>()->getInvite());
 	if (!event->isReadable() && !message->getPayload<ChatState>() && !message->getPayload<DeliveryReceipt>() && !message->getPayload<DeliveryReceiptRequest>() && !isInvite && !message->hasSubject()) {
 		return;
 	}
diff --git a/Swift/Controllers/UIInterfaces/ChatWindow.h b/Swift/Controllers/UIInterfaces/ChatWindow.h
index fd99514..fe7b6bf 100644
--- a/Swift/Controllers/UIInterfaces/ChatWindow.h
+++ b/Swift/Controllers/UIInterfaces/ChatWindow.h
@@ -56,7 +56,7 @@ namespace Swift {
 			virtual std::string addFileTransfer(const std::string& senderName, bool senderIsSelf, const std::string& filename, const boost::uintmax_t sizeInBytes) = 0;
 			virtual void setFileTransferProgress(std::string, const int percentageDone) = 0;
 			virtual void setFileTransferStatus(std::string, const FileTransferState state, const std::string& msg = "") = 0;
-			virtual void addMUCInvitation(const JID& jid, const std::string& reason, const std::string& password) = 0;
+			virtual void addMUCInvitation(const JID& jid, const std::string& reason, const std::string& password, bool direct = true) = 0;
 
 			// message receipts
 			virtual void setMessageReceiptState(const std::string& id, ChatWindow::ReceiptState state) = 0;
diff --git a/Swift/Controllers/UnitTest/MockChatWindow.h b/Swift/Controllers/UnitTest/MockChatWindow.h
index 5e43549..cc54763 100644
--- a/Swift/Controllers/UnitTest/MockChatWindow.h
+++ b/Swift/Controllers/UnitTest/MockChatWindow.h
@@ -50,7 +50,7 @@ namespace Swift {
 			void setAvailableOccupantActions(const std::vector<OccupantAction>&/* actions*/) {}
 			void setSubject(const std::string& /*subject*/) {}
 			virtual void showRoomConfigurationForm(Form::ref) {}
-			virtual void addMUCInvitation(const JID& /*jid*/, const std::string& /*reason*/, const std::string& /*password*/) {};
+			virtual void addMUCInvitation(const JID& /*jid*/, const std::string& /*reason*/, const std::string& /*password*/, bool direct = true) {};
 			virtual void setAffiliations(MUCOccupant::Affiliation, const std::vector<JID>&) {}
 
 			std::string name_;
diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp
index 88f326f..ed8e632 100644
--- a/Swift/QtUI/QtChatWindow.cpp
+++ b/Swift/QtUI/QtChatWindow.cpp
@@ -795,14 +795,18 @@ void QtChatWindow::showRoomConfigurationForm(Form::ref form) {
 	mucConfigurationWindow_->onFormCancelled.connect(boost::bind(boost::ref(onConfigurationFormCancelled)));
 }
 
-void QtChatWindow::addMUCInvitation(const JID& jid, const std::string& reason, const std::string& /*password*/) {
+void QtChatWindow::addMUCInvitation(const JID& jid, const std::string& reason, const std::string& /*password*/, bool direct) {
 	bool accepted = false;
 	QMessageBox msgBox;
+	//FIXME: horrid modal untranslated popup. Fix before release.
 	msgBox.setText(QString("You have been invited to the room %1 by %2.").arg(P2QSTRING(jid.toString())).arg(contact_));
 	QString reasonString;
 	if (!reason.empty()) {
 		reasonString = QString("\"%1\"").arg(P2QSTRING(reason));
 	}
+	if (!direct) {
+		reasonString += QString("(%1 may not have really sent this invitation)").arg(P2QSTRING(jid.toString()));
+	}
 	msgBox.setInformativeText(QString("Accept invitation from %1 to enter %2?\n%3").arg(contact_).arg(P2QSTRING(jid.toString())).arg(reasonString));
 	msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
 	msgBox.setDefaultButton(QMessageBox::Yes);
diff --git a/Swift/QtUI/QtChatWindow.h b/Swift/QtUI/QtChatWindow.h
index 55e929d..4f997c0 100644
--- a/Swift/QtUI/QtChatWindow.h
+++ b/Swift/QtUI/QtChatWindow.h
@@ -77,7 +77,7 @@ namespace Swift {
 			virtual void setAvailableOccupantActions(const std::vector<OccupantAction>& actions);
 			void setSubject(const std::string& subject);
 			void showRoomConfigurationForm(Form::ref);
-			void addMUCInvitation(const JID& jid, const std::string& reason, const std::string& password);
+			void addMUCInvitation(const JID& jid, const std::string& reason, const std::string& password, bool direct = true);
 			void setAffiliations(MUCOccupant::Affiliation, const std::vector<JID>&);
 
 		public slots:
-- 
cgit v0.10.2-6-g49f6