Unescape double-escaped messages from twitter

This commit is contained in:
HanzZ 2012-11-28 18:52:58 +01:00
parent 4eeeb84652
commit 9469109d37
3 changed files with 65 additions and 7 deletions

View file

@ -1,5 +1,6 @@
#include "TwitterResponseParser.h"
#include "transport/logging.h"
#include "boost/algorithm/string.hpp"
#include <cctype>
DEFINE_LOGGER(logger, "TwitterResponseParser")
@ -11,6 +12,16 @@ static std::string tolowercase(std::string inp)
return out;
}
static std::string unescape(std::string data) {
using boost::algorithm::replace_all;
replace_all(data, "&amp;", "&");
replace_all(data, "&quot;", "\"");
replace_all(data, "&apos;", "\'");
replace_all(data, "&lt;", "<");
replace_all(data, "&gt;", ">");
return data;
}
static std::string toIsoTime(std::string in) {
time_t now = time(0);
struct tm *mtime = gmtime(&now);
@ -31,7 +42,7 @@ EmbeddedStatus getEmbeddedStatus(const Swift::ParserElement::ref &element, const
status.setCreationTime( toIsoTime(std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
status.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
status.setTweet( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) );
status.setTweet( unescape (std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) ) );
status.setTruncated( std::string( element->getChild(TwitterReponseTypes::truncated, xmlns)->getText() )=="true" );
status.setReplyToStatusID( std::string( element->getChild(TwitterReponseTypes::in_reply_to_status_id, xmlns)->getText() ) );
status.setReplyToUserID( std::string( element->getChild(TwitterReponseTypes::in_reply_to_user_id, xmlns)->getText() ) );
@ -72,7 +83,7 @@ Status getStatus(const Swift::ParserElement::ref &element, const std::string xml
status.setCreationTime( toIsoTime ( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
status.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
status.setTweet( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) );
status.setTweet( unescape ( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) ) );
status.setTruncated( std::string( element->getChild(TwitterReponseTypes::truncated, xmlns)->getText() )=="true" );
status.setReplyToStatusID( std::string( element->getChild(TwitterReponseTypes::in_reply_to_status_id, xmlns)->getText() ) );
status.setReplyToUserID( std::string( element->getChild(TwitterReponseTypes::in_reply_to_user_id, xmlns)->getText() ) );
@ -94,7 +105,7 @@ DirectMessage getDirectMessage(const Swift::ParserElement::ref &element, const s
DM.setCreationTime( toIsoTime ( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
DM.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
DM.setMessage( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) );
DM.setMessage( unescape ( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) ) );
DM.setSenderID( std::string( element->getChild(TwitterReponseTypes::sender_id, xmlns)->getText() ) );
DM.setRecipientID( std::string( element->getChild(TwitterReponseTypes::recipient_id, xmlns)->getText() ) );
DM.setSenderScreenName( std::string( element->getChild(TwitterReponseTypes::sender_screen_name, xmlns)->getText() ) );

View file

@ -141,7 +141,7 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
boost::shared_ptr<Swift::Message> msg(new Swift::Message());
msg->setBody("hi there!");
msg->setBody("hi there<>!");
// Forward it
conv->handleMessage(msg);
@ -149,7 +149,7 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
CPPUNIT_ASSERT_EQUAL(std::string("hi there<>!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
CPPUNIT_ASSERT_EQUAL(std::string("user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
@ -158,13 +158,13 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
// send response
msg->setFrom("user@localhost/resource");
msg->setTo("buddy1\\40test@localhost/bot");
msg->setBody("response!");
msg->setBody("response<>!");
injectMessage(msg);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
CPPUNIT_ASSERT(m_msg);
CPPUNIT_ASSERT_EQUAL(std::string("response!"), m_msg->getBody());
CPPUNIT_ASSERT_EQUAL(std::string("response<>!"), m_msg->getBody());
// send another message from legacy network, should be sent to user@localhost/resource now
boost::shared_ptr<Swift::Message> msg2(new Swift::Message());

View file

@ -0,0 +1,47 @@
#include "transport/userregistry.h"
#include "transport/config.h"
#include "transport/storagebackend.h"
#include "transport/user.h"
#include "transport/transport.h"
#include "transport/storagebackend.h"
#include "transport/conversation.h"
#include "transport/usermanager.h"
#include "transport/localbuddy.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <Swiften/Swiften.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Server/Server.h>
#include <Swiften/Parser/StringTreeParser.h>
#include <Swiften/Network/DummyNetworkFactories.h>
#include <Swiften/Network/DummyConnectionServer.h>
#include "Swiften/Server/ServerStanzaChannel.h"
#include "Swiften/Server/ServerFromClientSession.h"
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
#include "basictest.h"
#include "transport/util.h"
using namespace Transport;
class StringTreeParserTest : public CPPUNIT_NS :: TestFixture{
CPPUNIT_TEST_SUITE(StringTreeParserTest);
CPPUNIT_TEST(parseEscapedCharacters);
CPPUNIT_TEST_SUITE_END();
public:
void setUp (void) {
}
void tearDown (void) {
}
void parseEscapedCharacters() {
Swift::ParserElement::ref root = Swift::StringTreeParser::parse("<body>&lt;test&gt;</body>");
CPPUNIT_ASSERT_EQUAL(std::string("<test>"), root->getText());
}
};
CPPUNIT_TEST_SUITE_REGISTRATION (StringTreeParserTest);