summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2016-01-12 17:23:05 (GMT)
committerKevin Smith <kevin.smith@isode.com>2016-02-02 11:33:06 (GMT)
commit1b9ccc1fef6104eaf951153ddccdc6bb15899e9a (patch)
tree428232448e9846265605820db6f380a5b98c018a
parent3afd061b713ce5fff604dee62dec8410a1de6a9c (diff)
downloadswift-1b9ccc1fef6104eaf951153ddccdc6bb15899e9a.zip
swift-1b9ccc1fef6104eaf951153ddccdc6bb15899e9a.tar.bz2
Change stanza body to boost::optional<std::string> type
Changed MUCController to only handle message stanzas as subject change if <subject/> is present and neither <body/> nor <thread/> is present in the message stanza. Test-Information: Added unit tests verifying behavior described in XEP-0045 section 8.1. Unit tests pass on OS X 10.11.2. Change-Id: I1d22272da1675176be131ab360b214a98f20533f
-rw-r--r--Swift/Controllers/Chat/ChatController.cpp2
-rw-r--r--Swift/Controllers/Chat/ChatControllerBase.cpp8
-rw-r--r--Swift/Controllers/Chat/ChatsManager.cpp2
-rw-r--r--Swift/Controllers/Chat/MUCController.cpp9
-rw-r--r--Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp115
-rw-r--r--Swift/Controllers/EventNotifier.cpp36
-rw-r--r--Swift/Controllers/UnitTest/MockChatWindow.h4
-rw-r--r--Swift/Controllers/XMPPEvents/MessageEvent.h8
-rw-r--r--Swift/QtUI/EventViewer/QtEvent.cpp4
-rw-r--r--Swiften/Elements/Message.h29
-rw-r--r--Swiften/Elements/Stanza.cpp16
-rw-r--r--Swiften/Elements/Stanza.h10
-rw-r--r--Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp4
-rw-r--r--Swiften/Parser/PayloadParsers/UnitTest/ForwardedParserTest.cpp10
-rw-r--r--Swiften/Parser/PayloadParsers/UnitTest/MAMResultParserTest.cpp8
-rw-r--r--Swiftob/Commands.cpp9
-rw-r--r--Swiftob/LuaCommands.cpp6
-rw-r--r--Swiftob/Swiftob.cpp12
18 files changed, 215 insertions, 77 deletions
diff --git a/Swift/Controllers/Chat/ChatController.cpp b/Swift/Controllers/Chat/ChatController.cpp
index eb86766..11ba89e 100644
--- a/Swift/Controllers/Chat/ChatController.cpp
+++ b/Swift/Controllers/Chat/ChatController.cpp
@@ -177,7 +177,7 @@ void ChatController::preHandleIncomingMessage(boost::shared_ptr<MessageEvent> me
if (toJID_.equals(from, JID::WithoutResource) && toJID_.isBare()){
// Bind controller to a full JID if message contains body text or is a typing chat state.
ChatState::ref chatState = message->getPayload<ChatState>();
- if (!message->getBody().empty() || (chatState && chatState->getChatState() == ChatState::Composing)) {
+ if (!message->getBody().get_value_or("").empty() || (chatState && chatState->getChatState() == ChatState::Composing)) {
setToJID(from);
}
}
diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp
index 4a84a6e..fef3e7a 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.cpp
+++ b/Swift/Controllers/Chat/ChatControllerBase.cpp
@@ -172,8 +172,8 @@ void ChatControllerBase::handleSendMessageRequest(const std::string &body, bool
}
message->setID(lastSentMessageStanzaID_ = idGenerator_.generateID());
stanzaChannel_->sendMessage(message);
- postSendMessage(message->getBody(), boost::dynamic_pointer_cast<Stanza>(message));
- onActivity(message->getBody());
+ postSendMessage(message->getBody().get(), boost::dynamic_pointer_cast<Stanza>(message));
+ onActivity(message->getBody().get());
#ifdef SWIFT_EXPERIMENTAL_HISTORY
logMessage(body, selfJID_, toJID_, now, false);
@@ -237,7 +237,7 @@ void ChatControllerBase::handleIncomingMessage(boost::shared_ptr<MessageEvent> m
}
}
boost::shared_ptr<Message> message = messageEvent->getStanza();
- std::string body = message->getBody();
+ std::string body = message->getBody().get_value_or("");
HighlightAction highlight;
if (message->isError()) {
if (!message->getTo().getResource().empty()) {
@@ -286,7 +286,7 @@ void ChatControllerBase::handleIncomingMessage(boost::shared_ptr<MessageEvent> m
boost::shared_ptr<Replace> replace = message->getPayload<Replace>();
if (replace) {
- std::string body = message->getBody();
+ std::string body = message->getBody().get_value_or("");
// Should check if the user has a previous message
std::map<JID, std::string>::iterator lastMessage;
lastMessage = lastMessagesUIID_.find(from);
diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp
index 1a69982..49caee4 100644
--- a/Swift/Controllers/Chat/ChatsManager.cpp
+++ b/Swift/Controllers/Chat/ChatsManager.cpp
@@ -917,7 +917,7 @@ void ChatsManager::handleIncomingMessage(boost::shared_ptr<Message> message) {
// Do not bind a controller to a full JID, for delivery receipts or chat state notifications.
bool bindControllerToJID = false;
ChatState::ref chatState = message->getPayload<ChatState>();
- if (!message->getBody().empty() || (chatState && chatState->getChatState() == ChatState::Composing)) {
+ if (!message->getBody().get_value_or("").empty() || (chatState && chatState->getChatState() == ChatState::Composing)) {
bindControllerToJID = true;
}
diff --git a/Swift/Controllers/Chat/MUCController.cpp b/Swift/Controllers/Chat/MUCController.cpp
index e6c16b7..409fe1f 100644
--- a/Swift/Controllers/Chat/MUCController.cpp
+++ b/Swift/Controllers/Chat/MUCController.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2015 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -22,6 +22,7 @@
#include <Swiften/Client/StanzaChannel.h>
#include <Swiften/Disco/EntityCapsProvider.h>
#include <Swiften/Elements/Delay.h>
+#include <Swiften/Elements/Thread.h>
#include <Swiften/MUC/MUC.h>
#include <Swiften/MUC/MUCBookmark.h>
#include <Swiften/MUC/MUCBookmarkManager.h>
@@ -531,7 +532,7 @@ JID MUCController::nickToJID(const std::string& nick) {
bool MUCController::messageTargetsMe(boost::shared_ptr<Message> message) {
std::string stringRegexp(".*\\b" + boost::to_lower_copy(nick_) + "\\b.*");
boost::regex myRegexp(stringRegexp);
- return boost::regex_match(boost::to_lower_copy(message->getBody()), myRegexp);
+ return boost::regex_match(boost::to_lower_copy(message->getBody().get_value_or("")), myRegexp);
}
void MUCController::preHandleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent) {
@@ -559,7 +560,7 @@ void MUCController::preHandleIncomingMessage(boost::shared_ptr<MessageEvent> mes
receivedActivity();
joined_ = true;
- if (message->hasSubject() && message->getBody().empty()) {
+ if (message->hasSubject() && !message->getPayload<Body>() && !message->getPayload<Thread>()) {
chatWindow_->addSystemMessage(chatMessageParser_->parseMessageBody(str(format(QT_TRANSLATE_NOOP("", "The room subject is now: %1%")) % message->getSubject())), ChatWindow::DefaultDirection);
chatWindow_->setSubject(message->getSubject());
doneGettingHistory_ = true;
@@ -1078,7 +1079,7 @@ void MUCController::addRecentLogs() {
}
void MUCController::checkDuplicates(boost::shared_ptr<Message> newMessage) {
- std::string body = newMessage->getBody();
+ std::string body = newMessage->getBody().get_value_or("");
JID jid = newMessage->getFrom();
boost::optional<boost::posix_time::ptime> time = newMessage->getTimestamp();
diff --git a/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp b/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp
index bc6ada2..e8fc41d 100644
--- a/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp
+++ b/Swift/Controllers/Chat/UnitTest/MUCControllerTest.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2015 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -19,6 +19,7 @@
#include <Swiften/Crypto/PlatformCryptoProvider.h>
#include <Swiften/Disco/DummyEntityCapsProvider.h>
#include <Swiften/Elements/MUCUserPayload.h>
+#include <Swiften/Elements/Thread.h>
#include <Swiften/MUC/MUCBookmarkManager.h>
#include <Swiften/MUC/UnitTest/MockMUC.h>
#include <Swiften/Network/TimerFactory.h>
@@ -58,6 +59,10 @@ class MUCControllerTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testMessageWithLabelItem);
CPPUNIT_TEST(testCorrectMessageWithLabelItem);
CPPUNIT_TEST(testRoleAffiliationStates);
+ CPPUNIT_TEST(testSubjectChangeCorrect);
+ CPPUNIT_TEST(testSubjectChangeIncorrectA);
+ CPPUNIT_TEST(testSubjectChangeIncorrectB);
+ CPPUNIT_TEST(testSubjectChangeIncorrectC);
CPPUNIT_TEST_SUITE_END();
public:
@@ -215,7 +220,7 @@ public:
CPPUNIT_ASSERT(window_->labelsEnabled_);
CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */
CPPUNIT_ASSERT(message);
- CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody());
+ CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody().get());
CPPUNIT_ASSERT(!message->getPayload<SecurityLabel>());
}
@@ -242,7 +247,7 @@ public:
CPPUNIT_ASSERT(window_->labelsEnabled_);
CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */
CPPUNIT_ASSERT(message);
- CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody());
+ CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody().get());
CPPUNIT_ASSERT_EQUAL(label, message->getPayload<SecurityLabel>());
}
@@ -274,13 +279,13 @@ public:
CPPUNIT_ASSERT(window_->labelsEnabled_);
CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */
CPPUNIT_ASSERT(message);
- CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody());
+ CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody().get());
CPPUNIT_ASSERT_EQUAL(label, message->getPayload<SecurityLabel>());
window_->label_ = labelItem2;
window_->onSendMessageRequest(messageBody, true);
rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1];
message = boost::dynamic_pointer_cast<Message>(rawStanza);
- CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody());
+ CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody().get());
CPPUNIT_ASSERT_EQUAL(label, message->getPayload<SecurityLabel>());
}
@@ -398,6 +403,106 @@ public:
}
}
+ void testSubjectChangeCorrect() {
+ std::string messageBody("test message");
+ window_->onSendMessageRequest(messageBody, false);
+ boost::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1];
+ Message::ref message = boost::dynamic_pointer_cast<Message>(rawStanza);
+ CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */
+ CPPUNIT_ASSERT(message);
+ CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody().get_value_or(""));
+
+ {
+ Message::ref message = boost::make_shared<Message>();
+ message->setType(Message::Groupchat);
+ message->setTo(self_);
+ message->setFrom(mucJID_.withResource("SomeNickname"));
+ message->setID(iqChannel_->getNewIQID());
+ message->setSubject("New Room Subject");
+
+ controller_->handleIncomingMessage(boost::make_shared<MessageEvent>(message));
+ CPPUNIT_ASSERT_EQUAL(std::string("The room subject is now: New Room Subject"), boost::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(window_->lastAddedSystemMessage_.getParts()[0])->text);
+ }
+ }
+
+ /*
+ * Test that message stanzas with subject element and non-empty body element do not cause a subject change.
+ */
+ void testSubjectChangeIncorrectA() {
+ std::string messageBody("test message");
+ window_->onSendMessageRequest(messageBody, false);
+ boost::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1];
+ Message::ref message = boost::dynamic_pointer_cast<Message>(rawStanza);
+ CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */
+ CPPUNIT_ASSERT(message);
+ CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody().get_value_or(""));
+
+ {
+ Message::ref message = boost::make_shared<Message>();
+ message->setType(Message::Groupchat);
+ message->setTo(self_);
+ message->setFrom(mucJID_.withResource("SomeNickname"));
+ message->setID(iqChannel_->getNewIQID());
+ message->setSubject("New Room Subject");
+ message->setBody("Some body text that prevents this stanza from being a subject change.");
+
+ controller_->handleIncomingMessage(boost::make_shared<MessageEvent>(message));
+ CPPUNIT_ASSERT_EQUAL(std::string("Trying to enter room teaparty@rooms.wonderland.lit"), boost::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(window_->lastAddedSystemMessage_.getParts()[0])->text);
+ }
+ }
+
+ /*
+ * Test that message stanzas with subject element and thread element do not cause a subject change.
+ */
+ void testSubjectChangeIncorrectB() {
+ std::string messageBody("test message");
+ window_->onSendMessageRequest(messageBody, false);
+ boost::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1];
+ Message::ref message = boost::dynamic_pointer_cast<Message>(rawStanza);
+ CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */
+ CPPUNIT_ASSERT(message);
+ CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody().get_value_or(""));
+
+ {
+ Message::ref message = boost::make_shared<Message>();
+ message->setType(Message::Groupchat);
+ message->setTo(self_);
+ message->setFrom(mucJID_.withResource("SomeNickname"));
+ message->setID(iqChannel_->getNewIQID());
+ message->setSubject("New Room Subject");
+ message->addPayload(boost::make_shared<Thread>("Thread that prevents the subject change."));
+
+ controller_->handleIncomingMessage(boost::make_shared<MessageEvent>(message));
+ CPPUNIT_ASSERT_EQUAL(std::string("Trying to enter room teaparty@rooms.wonderland.lit"), boost::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(window_->lastAddedSystemMessage_.getParts()[0])->text);
+ }
+ }
+
+ /*
+ * Test that message stanzas with subject element and empty body element do not cause a subject change.
+ */
+ void testSubjectChangeIncorrectC() {
+ std::string messageBody("test message");
+ window_->onSendMessageRequest(messageBody, false);
+ boost::shared_ptr<Stanza> rawStanza = stanzaChannel_->sentStanzas[stanzaChannel_->sentStanzas.size() - 1];
+ Message::ref message = boost::dynamic_pointer_cast<Message>(rawStanza);
+ CPPUNIT_ASSERT(stanzaChannel_->isAvailable()); /* Otherwise will prevent sends. */
+ CPPUNIT_ASSERT(message);
+ CPPUNIT_ASSERT_EQUAL(messageBody, message->getBody().get_value_or(""));
+
+ {
+ Message::ref message = boost::make_shared<Message>();
+ message->setType(Message::Groupchat);
+ message->setTo(self_);
+ message->setFrom(mucJID_.withResource("SomeNickname"));
+ message->setID(iqChannel_->getNewIQID());
+ message->setSubject("New Room Subject");
+ message->setBody("");
+
+ controller_->handleIncomingMessage(boost::make_shared<MessageEvent>(message));
+ CPPUNIT_ASSERT_EQUAL(std::string("Trying to enter room teaparty@rooms.wonderland.lit"), boost::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(window_->lastAddedSystemMessage_.getParts()[0])->text);
+ }
+ }
+
void testRoleAffiliationStatesVerify(const std::map<std::string, MUCOccupant> &occupants) {
/* verify that the roster is in sync */
GroupRosterItem* group = window_->getRosterModel()->getRoot();
diff --git a/Swift/Controllers/EventNotifier.cpp b/Swift/Controllers/EventNotifier.cpp
index 47bb888..626fd40 100644
--- a/Swift/Controllers/EventNotifier.cpp
+++ b/Swift/Controllers/EventNotifier.cpp
@@ -1,27 +1,29 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
-#include "Swift/Controllers/EventNotifier.h"
+#include <Swift/Controllers/EventNotifier.h>
-#include <boost/bind.hpp>
#include <boost/algorithm/string.hpp>
+#include <boost/bind.hpp>
-#include <Swift/Controllers/Intl.h>
-#include <Swiften/Base/format.h>
+#include <Swiften/Avatars/AvatarManager.h>
+#include <Swiften/Client/NickResolver.h>
+#include <Swiften/JID/JID.h>
#include <Swiften/Base/String.h>
-#include "Swift/Controllers/XMPPEvents/EventController.h"
-#include "SwifTools/Notifier/Notifier.h"
-#include "Swiften/Avatars/AvatarManager.h"
-#include "Swiften/Client/NickResolver.h"
-#include "Swiften/JID/JID.h"
-#include "Swift/Controllers/XMPPEvents/MessageEvent.h"
-#include "Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h"
-#include "Swift/Controllers/XMPPEvents/ErrorEvent.h"
-#include "Swift/Controllers/XMPPEvents/MUCInviteEvent.h"
-#include "Swift/Controllers/Settings/SettingsProvider.h"
+#include <Swiften/Base/format.h>
+
+#include <Swift/Controllers/Settings/SettingsProvider.h>
+#include <Swift/Controllers/XMPPEvents/ErrorEvent.h>
+#include <Swift/Controllers/XMPPEvents/EventController.h>
+#include <Swift/Controllers/XMPPEvents/MUCInviteEvent.h>
+#include <Swift/Controllers/XMPPEvents/MessageEvent.h>
+#include <Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h>
+#include <Swift/Controllers/Intl.h>
+
+#include <SwifTools/Notifier/Notifier.h>
namespace Swift {
@@ -41,12 +43,12 @@ void EventNotifier::handleEventAdded(boost::shared_ptr<StanzaEvent> event) {
if (boost::shared_ptr<MessageEvent> messageEvent = boost::dynamic_pointer_cast<MessageEvent>(event)) {
JID jid = messageEvent->getStanza()->getFrom();
std::string title = nickResolver->jidToNick(jid);
- if (!messageEvent->getStanza()->isError() && !messageEvent->getStanza()->getBody().empty()) {
+ if (!messageEvent->getStanza()->isError() && !messageEvent->getStanza()->getBody().get_value_or("").empty()) {
JID activationJID = jid;
if (messageEvent->getStanza()->getType() == Message::Groupchat) {
activationJID = jid.toBare();
}
- std::string messageText = messageEvent->getStanza()->getBody();
+ std::string messageText = messageEvent->getStanza()->getBody().get_value_or("");
if (boost::starts_with(messageText, "/me ")) {
messageText = "*" + String::getSplittedAtFirst(messageText, ' ').second + "*";
}
diff --git a/Swift/Controllers/UnitTest/MockChatWindow.h b/Swift/Controllers/UnitTest/MockChatWindow.h
index 4523d29..dddea6c 100644
--- a/Swift/Controllers/UnitTest/MockChatWindow.h
+++ b/Swift/Controllers/UnitTest/MockChatWindow.h
@@ -25,7 +25,8 @@ namespace Swift {
virtual std::string addAction(const ChatMessage& /*message*/, const std::string& /*senderName*/, bool /*senderIsSelf*/, boost::shared_ptr<SecurityLabel> /*label*/, const std::string& /*avatarPath*/, const boost::posix_time::ptime& /*time*/, const HighlightAction& /*highlight*/) {return "id";}
- virtual std::string addSystemMessage(const ChatMessage& /*message*/, Direction /*direction*/) {
+ virtual std::string addSystemMessage(const ChatMessage& message, Direction /*direction*/) {
+ lastAddedSystemMessage_ = message;
return "id";
}
@@ -103,6 +104,7 @@ namespace Swift {
std::string lastMessageBody_;
ChatMessage lastAddedPresence_;
ChatMessage lastReplacedMessage_;
+ ChatMessage lastAddedSystemMessage_;
std::vector<SecurityLabelsCatalog::Item> labels_;
bool labelsEnabled_;
bool impromptuMUCSupported_;
diff --git a/Swift/Controllers/XMPPEvents/MessageEvent.h b/Swift/Controllers/XMPPEvents/MessageEvent.h
index be8b1b0..b5b1215 100644
--- a/Swift/Controllers/XMPPEvents/MessageEvent.h
+++ b/Swift/Controllers/XMPPEvents/MessageEvent.h
@@ -1,17 +1,19 @@
/*
- * Copyright (c) 2010-2012 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
+
#include <cassert>
#include <boost/shared_ptr.hpp>
-#include <Swift/Controllers/XMPPEvents/StanzaEvent.h>
#include <Swiften/Elements/Message.h>
+#include <Swift/Controllers/XMPPEvents/StanzaEvent.h>
+
namespace Swift {
class MessageEvent : public StanzaEvent {
public:
@@ -22,7 +24,7 @@ namespace Swift {
boost::shared_ptr<Message> getStanza() {return stanza_;}
bool isReadable() {
- return getStanza()->isError() || !getStanza()->getBody().empty();
+ return getStanza()->isError() || !getStanza()->getBody().get_value_or("").empty();
}
void read() {
diff --git a/Swift/QtUI/EventViewer/QtEvent.cpp b/Swift/QtUI/EventViewer/QtEvent.cpp
index 4d90bd9..4830aec 100644
--- a/Swift/QtUI/EventViewer/QtEvent.cpp
+++ b/Swift/QtUI/EventViewer/QtEvent.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2015 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -64,7 +64,7 @@ QString QtEvent::sender() {
QString QtEvent::text() {
boost::shared_ptr<MessageEvent> messageEvent = boost::dynamic_pointer_cast<MessageEvent>(event_);
if (messageEvent) {
- return P2QSTRING(messageEvent->getStanza()->getBody());
+ return P2QSTRING(messageEvent->getStanza()->getBody().get_value_or(""));
}
boost::shared_ptr<SubscriptionRequestEvent> subscriptionRequestEvent = boost::dynamic_pointer_cast<SubscriptionRequestEvent>(event_);
if (subscriptionRequestEvent) {
diff --git a/Swiften/Elements/Message.h b/Swiften/Elements/Message.h
index f6c16e4..0f0d380 100644
--- a/Swiften/Elements/Message.h
+++ b/Swiften/Elements/Message.h
@@ -1,22 +1,23 @@
/*
- * Copyright (c) 2010-2015 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
+#include <string>
+
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
-#include <string>
#include <Swiften/Base/API.h>
#include <Swiften/Elements/Body.h>
-#include <Swiften/Elements/Subject.h>
#include <Swiften/Elements/ErrorPayload.h>
-#include <Swiften/Elements/Stanza.h>
#include <Swiften/Elements/Replace.h>
+#include <Swiften/Elements/Stanza.h>
+#include <Swiften/Elements/Subject.h>
namespace Swift {
class SWIFTEN_API Message : public Stanza {
@@ -45,16 +46,26 @@ namespace Swift {
return static_cast<bool>(getPayload<Subject>());
}
- std::string getBody() const {
+ boost::optional<std::string> getBody() const {
boost::shared_ptr<Body> body(getPayload<Body>());
+ boost::optional<std::string> bodyData;
if (body) {
- return body->getText();
+ bodyData = body->getText();
}
- return "";
+ return bodyData;
+ }
+
+ void setBody(const std::string& body) {
+ setBody(boost::optional<std::string>(body));
}
- void setBody(const std::string& body) {
- updatePayload(boost::make_shared<Body>(body));
+ void setBody(const boost::optional<std::string>& body) {
+ if (body) {
+ updatePayload(boost::make_shared<Body>(body.get()));
+ }
+ else {
+ removePayloadOfSameType(boost::make_shared<Body>());
+ }
}
bool isError() {
diff --git a/Swiften/Elements/Stanza.cpp b/Swiften/Elements/Stanza.cpp
index 234878c..f385e1c 100644
--- a/Swiften/Elements/Stanza.cpp
+++ b/Swiften/Elements/Stanza.cpp
@@ -1,15 +1,17 @@
/*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/Elements/Stanza.h>
-#include <Swiften/Elements/Delay.h>
#include <typeinfo>
+#include <boost/bind.hpp>
+
#include <Swiften/Base/foreach.h>
+#include <Swiften/Elements/Delay.h>
namespace Swift {
@@ -30,6 +32,16 @@ void Stanza::updatePayload(boost::shared_ptr<Payload> payload) {
addPayload(payload);
}
+static bool sameType(boost::shared_ptr<Payload> a, boost::shared_ptr<Payload> b) {
+ return typeid(*a.get()) == typeid(*b.get());
+}
+
+void Stanza::removePayloadOfSameType(boost::shared_ptr<Payload> payload) {
+ payloads_.erase(std::remove_if(payloads_.begin(), payloads_.end(),
+ boost::bind<bool>(&sameType, payload, _1)),
+ payloads_.end());
+}
+
boost::shared_ptr<Payload> Stanza::getPayloadOfSameType(boost::shared_ptr<Payload> payload) const {
foreach (const boost::shared_ptr<Payload>& i, payloads_) {
if (typeid(*i.get()) == typeid(*payload.get())) {
diff --git a/Swiften/Elements/Stanza.h b/Swiften/Elements/Stanza.h
index 41df894..8da6280 100644
--- a/Swiften/Elements/Stanza.h
+++ b/Swiften/Elements/Stanza.h
@@ -1,16 +1,17 @@
/*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
-#include <vector>
#include <string>
-#include <boost/shared_ptr.hpp>
-#include <boost/optional/optional_fwd.hpp>
+#include <vector>
+
#include <boost/date_time/posix_time/ptime.hpp>
+#include <boost/optional/optional_fwd.hpp>
+#include <boost/shared_ptr.hpp>
#include <Swiften/Base/API.h>
#include <Swiften/Elements/ToplevelElement.h>
@@ -69,6 +70,7 @@ namespace Swift {
void updatePayload(boost::shared_ptr<Payload> payload);
+ void removePayloadOfSameType(boost::shared_ptr<Payload>);
boost::shared_ptr<Payload> getPayloadOfSameType(boost::shared_ptr<Payload>) const;
const JID& getFrom() const { return from_; }
diff --git a/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp b/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp
index 2e2503b..216d16d 100644
--- a/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp
+++ b/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014 Isode Limited.
+ * Copyright (c) 2014-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -75,7 +75,7 @@ static void handleDisconnected(const boost::optional<ClientError>&) {
static void handleIncomingMessage(boost::shared_ptr<Message> message) {
if (message->getFrom().toBare() == roomJID) {
- cout << "[ " << roomJID << " ] " << message->getFrom().getResource() << ": " << message->getBody() << endl;
+ cout << "[ " << roomJID << " ] " << message->getFrom().getResource() << ": " << message->getBody().get_value_or("") << endl;
}
}
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/ForwardedParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/ForwardedParserTest.cpp
index e77e821..fae259f 100644
--- a/Swiften/Parser/PayloadParsers/UnitTest/ForwardedParserTest.cpp
+++ b/Swiften/Parser/PayloadParsers/UnitTest/ForwardedParserTest.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014 Isode Limited.
+ * Copyright (c) 2014-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -8,12 +8,12 @@
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <Swiften/Base/DateTime.h>
-#include <Swiften/Parser/PayloadParsers/ForwardedParser.h>
-#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>
#include <Swiften/Elements/Delay.h>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/Presence.h>
+#include <Swiften/Parser/PayloadParsers/ForwardedParser.h>
+#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>
using namespace Swift;
@@ -66,7 +66,7 @@ class ForwardedParserTest : public CppUnit::TestFixture
boost::shared_ptr<Message> message = boost::dynamic_pointer_cast<Message>(payload->getStanza());
CPPUNIT_ASSERT(!!message);
const std::string expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.";
- CPPUNIT_ASSERT_EQUAL(expectedBody, message->getBody());
+ CPPUNIT_ASSERT_EQUAL(expectedBody, message->getBody().get());
CPPUNIT_ASSERT_EQUAL(Message::Chat, message->getType());
CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), message->getTo());
CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), message->getFrom());
@@ -88,7 +88,7 @@ class ForwardedParserTest : public CppUnit::TestFixture
boost::shared_ptr<Message> message = boost::dynamic_pointer_cast<Message>(payload->getStanza());
CPPUNIT_ASSERT(!!message);
const std::string expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.";
- CPPUNIT_ASSERT_EQUAL(expectedBody, message->getBody());
+ CPPUNIT_ASSERT_EQUAL(expectedBody, message->getBody().get());
CPPUNIT_ASSERT_EQUAL(Message::Chat, message->getType());
CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), message->getTo());
CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), message->getFrom());
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MAMResultParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MAMResultParserTest.cpp
index c67f7f8..a4c2f08 100644
--- a/Swiften/Parser/PayloadParsers/UnitTest/MAMResultParserTest.cpp
+++ b/Swiften/Parser/PayloadParsers/UnitTest/MAMResultParserTest.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014 Isode Limited.
+ * Copyright (c) 2014-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -8,11 +8,11 @@
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <Swiften/Base/DateTime.h>
-#include <Swiften/Parser/PayloadParsers/MAMResultParser.h>
-#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>
#include <Swiften/Elements/Delay.h>
#include <Swiften/Elements/Forwarded.h>
#include <Swiften/Elements/Message.h>
+#include <Swiften/Parser/PayloadParsers/MAMResultParser.h>
+#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>
using namespace Swift;
@@ -48,7 +48,7 @@ class MAMResultParserTest : public CppUnit::TestFixture
boost::shared_ptr<Message> message = boost::dynamic_pointer_cast<Message>(forwarded->getStanza());
CPPUNIT_ASSERT(!!message);
const std::string expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.";
- CPPUNIT_ASSERT_EQUAL(expectedBody, message->getBody());
+ CPPUNIT_ASSERT_EQUAL(expectedBody, message->getBody().get());
CPPUNIT_ASSERT_EQUAL(Message::Chat, message->getType());
CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), message->getTo());
CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), message->getFrom());
diff --git a/Swiftob/Commands.cpp b/Swiftob/Commands.cpp
index 4e31212..9212aaf 100644
--- a/Swiftob/Commands.cpp
+++ b/Swiftob/Commands.cpp
@@ -1,16 +1,17 @@
/*
- * Copyright (c) 2011 Isode Limited.
+ * Copyright (c) 2011-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiftob/Commands.h>
-#include <Swiften/Base/foreach.h>
#include <iostream>
-#include <boost/bind.hpp>
+
#include <boost/algorithm/string.hpp>
+#include <boost/bind.hpp>
+#include <Swiften/Base/foreach.h>
#include <Swiften/Client/Client.h>
typedef std::pair<std::string, Commands::Command*> NamedCommand;
@@ -211,6 +212,6 @@ void Commands::replyTo(Swift::Message::ref source, std::string replyBody, bool o
if (client_->isAvailable()) {
client_->sendMessage(reply);
} else {
- std::cout << "Dropping '" + reply->getBody() + "' -> " + reply->getTo().toString() + " on the floor due to missing connection." << std::endl;
+ std::cout << "Dropping '" + reply->getBody().get_value_or("") + "' -> " + reply->getTo().toString() + " on the floor due to missing connection." << std::endl;
}
}
diff --git a/Swiftob/LuaCommands.cpp b/Swiftob/LuaCommands.cpp
index 1192452..18535f3 100644
--- a/Swiftob/LuaCommands.cpp
+++ b/Swiftob/LuaCommands.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2011 Isode Limited.
+ * Copyright (c) 2011-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -380,7 +380,7 @@ int LuaCommands::get_setting(lua_State *L) {
void LuaCommands::handleLuaListener(int callbackIndex, lua_State* L, Swift::Message::ref message) {
lua_rawgeti(L, LUA_REGISTRYINDEX, callbackIndex);
- lua_pushstring(L, message->getBody().c_str());
+ lua_pushstring(L, message->getBody().get_value_or("").c_str());
lua_pushstring(L, message->getFrom().toBare().toString().c_str());
lua_pushstring(L, message->getFrom().getResource().c_str());
messageOntoStack(message, L);
@@ -426,7 +426,7 @@ void LuaCommands::messageOntoStack(Swift::Message::ref message, lua_State* L) {
lua_setfield(L, -2, "frombare");
lua_pushstring(L, message->getTo().toString().c_str());
lua_setfield(L, -2, "to");
- lua_pushstring(L, message->getBody().c_str());
+ lua_pushstring(L, message->getBody().get_value_or("").c_str());
lua_setfield(L, -2, "body");
}
diff --git a/Swiftob/Swiftob.cpp b/Swiftob/Swiftob.cpp
index d26ea23..d534479 100644
--- a/Swiftob/Swiftob.cpp
+++ b/Swiftob/Swiftob.cpp
@@ -1,22 +1,22 @@
/*
- * Copyright (c) 2011 Isode Limited.
+ * Copyright (c) 2011-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiftob/Swiftob.h>
-#include <string>
#include <iostream>
+#include <string>
+
#include <boost/bind.hpp>
-#include <Swiften/JID/JID.h>
#include <Swiften/Base/String.h>
+#include <Swiften/JID/JID.h>
#include <Swiften/Presence/PresenceSender.h>
-#include <Swiftob/Users.h>
#include <Swiftob/Storage.h>
-
+#include <Swiftob/Users.h>
po::options_description Swiftob::getOptionsDescription() {
po::options_description result("Options");
@@ -98,7 +98,7 @@ void Swiftob::handleMessageReceived(Swift::Message::ref message) {
std::cout << "Ignoring typed message" << std::endl;
return;
}
- std::string body = message->getBody();
+ std::string body = message->getBody().get_value_or("");
std::cout << "Got message with body " << body << std::endl;
if (body.size() == 0) {
std::cout << "Not handling empty body" << std::endl;