Added support for service.jid_escaping config
This commit is contained in:
parent
5257bb4304
commit
df32761194
10 changed files with 188 additions and 36 deletions
|
@ -65,6 +65,8 @@ class NetworkPluginServer {
|
|||
|
||||
virtual ~NetworkPluginServer();
|
||||
|
||||
void start();
|
||||
|
||||
void setAdminInterface(AdminInterface *adminInterface) {
|
||||
m_adminInterface = adminInterface;
|
||||
}
|
||||
|
@ -87,7 +89,7 @@ class NetworkPluginServer {
|
|||
|
||||
void handleMessageReceived(NetworkConversation *conv, boost::shared_ptr<Swift::Message> &message);
|
||||
|
||||
private:
|
||||
public:
|
||||
void handleNewClientConnection(boost::shared_ptr<Swift::Connection> c);
|
||||
void handleSessionFinished(Backend *c);
|
||||
void handlePongReceived(Backend *c);
|
||||
|
@ -133,6 +135,7 @@ class NetworkPluginServer {
|
|||
void handleFTRejected(User *user, const std::string &buddyName, const std::string &fileName, unsigned long size);
|
||||
void handleFTDataNeeded(Backend *b, unsigned long ftid);
|
||||
|
||||
private:
|
||||
void send(boost::shared_ptr<Swift::Connection> &, const std::string &data);
|
||||
|
||||
void pingTimeout();
|
||||
|
|
|
@ -402,6 +402,7 @@ int main(int argc, char **argv)
|
|||
FileTransferManager ftManager(&transport, &userManager);
|
||||
|
||||
NetworkPluginServer plugin(&transport, &config, &userManager, &ftManager, &discoItemsResponder);
|
||||
plugin.start();
|
||||
|
||||
AdminInterface adminInterface(&transport, &userManager, &plugin, storageBackend, userRegistration);
|
||||
plugin.setAdminInterface(&adminInterface);
|
||||
|
|
|
@ -15,6 +15,7 @@ if (CPPUNIT_FOUND)
|
|||
FILE(GLOB SRC_TEST tests/*.cpp)
|
||||
|
||||
ADD_EXECUTABLE(libtransport_test ${SRC_TEST})
|
||||
set_target_properties(libtransport_test PROPERTIES COMPILE_DEFINITIONS LIBTRANSPORT_TEST=1)
|
||||
|
||||
target_link_libraries(libtransport_test transport ${CPPUNIT_LIBRARY} ${Boost_LIBRARIES})
|
||||
endif()
|
||||
|
|
|
@ -98,6 +98,7 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
|
|||
("service.enable_privacy_lists", value<bool>()->default_value(true), "")
|
||||
("service.enable_xhtml", value<bool>()->default_value(true), "")
|
||||
("service.max_room_list_size", value<int>()->default_value(100), "")
|
||||
("service.jid_escaping", value<bool>()->default_value(true), "")
|
||||
("vhosts.vhost", value<std::vector<std::string> >()->multitoken(), "")
|
||||
("identity.name", value<std::string>()->default_value("Spectrum 2 Transport"), "Name showed in service discovery.")
|
||||
("identity.category", value<std::string>()->default_value("gateway"), "Disco#info identity category. 'gateway' by default.")
|
||||
|
|
|
@ -91,7 +91,17 @@ void Conversation::handleMessage(boost::shared_ptr<Swift::Message> &message, con
|
|||
message->setFrom(buddy->getJID());
|
||||
}
|
||||
else {
|
||||
message->setFrom(Swift::JID(Swift::JID::getEscapedNode(m_legacyName), m_conversationManager->getComponent()->getJID().toBare()));
|
||||
std::string name = m_legacyName;
|
||||
if (CONFIG_BOOL_DEFAULTED(m_conversationManager->getComponent()->getConfig(), "service.jid_escaping", true)) {
|
||||
name = Swift::JID::getEscapedNode(m_legacyName);
|
||||
}
|
||||
else {
|
||||
if (name.find_last_of("@") != std::string::npos) {
|
||||
name.replace(name.find_last_of("@"), 1, "%");
|
||||
}
|
||||
}
|
||||
|
||||
message->setFrom(Swift::JID(name, m_conversationManager->getComponent()->getJID().toBare(), "bot"));
|
||||
}
|
||||
}
|
||||
// PM message
|
||||
|
|
|
@ -285,6 +285,30 @@ NetworkPluginServer::NetworkPluginServer(Component *component, Config *config, U
|
|||
|
||||
m_server = component->getNetworkFactories()->getConnectionServerFactory()->createConnectionServer(Swift::HostAddress(CONFIG_STRING(m_config, "service.backend_host")), boost::lexical_cast<int>(CONFIG_STRING(m_config, "service.backend_port")));
|
||||
m_server->onNewConnection.connect(boost::bind(&NetworkPluginServer::handleNewClientConnection, this, _1));
|
||||
}
|
||||
|
||||
NetworkPluginServer::~NetworkPluginServer() {
|
||||
for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
|
||||
LOG4CXX_INFO(logger, "Stopping backend " << *it);
|
||||
std::string message;
|
||||
pbnetwork::WrapperMessage wrap;
|
||||
wrap.set_type(pbnetwork::WrapperMessage_Type_TYPE_EXIT);
|
||||
wrap.SerializeToString(&message);
|
||||
|
||||
Backend *c = (Backend *) *it;
|
||||
send(c->connection, message);
|
||||
}
|
||||
|
||||
m_pingTimer->stop();
|
||||
m_server->stop();
|
||||
m_server.reset();
|
||||
delete m_component->m_factory;
|
||||
delete m_vcardResponder;
|
||||
delete m_rosterResponder;
|
||||
delete m_blockResponder;
|
||||
}
|
||||
|
||||
void NetworkPluginServer::start() {
|
||||
m_server->start();
|
||||
|
||||
LOG4CXX_INFO(logger, "Listening on host " << CONFIG_STRING(m_config, "service.backend_host") << " port " << CONFIG_STRING(m_config, "service.backend_port"));
|
||||
|
@ -318,28 +342,6 @@ NetworkPluginServer::NetworkPluginServer(Component *component, Config *config, U
|
|||
// quit the while loop
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
NetworkPluginServer::~NetworkPluginServer() {
|
||||
for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
|
||||
LOG4CXX_INFO(logger, "Stopping backend " << *it);
|
||||
std::string message;
|
||||
pbnetwork::WrapperMessage wrap;
|
||||
wrap.set_type(pbnetwork::WrapperMessage_Type_TYPE_EXIT);
|
||||
wrap.SerializeToString(&message);
|
||||
|
||||
Backend *c = (Backend *) *it;
|
||||
send(c->connection, message);
|
||||
}
|
||||
|
||||
m_pingTimer->stop();
|
||||
m_server->stop();
|
||||
m_server.reset();
|
||||
delete m_component->m_factory;
|
||||
delete m_vcardResponder;
|
||||
delete m_rosterResponder;
|
||||
delete m_blockResponder;
|
||||
}
|
||||
|
||||
void NetworkPluginServer::handleNewClientConnection(boost::shared_ptr<Swift::Connection> c) {
|
||||
|
@ -479,11 +481,14 @@ void NetworkPluginServer::handleAuthorizationPayload(const std::string &data) {
|
|||
response->setTo(user->getJID());
|
||||
std::string name = payload.buddyname();
|
||||
|
||||
name = Swift::JID::getEscapedNode(name);
|
||||
|
||||
// if (name.find_last_of("@") != std::string::npos) { // OK when commented
|
||||
// name.replace(name.find_last_of("@"), 1, "%"); // OK when commented
|
||||
// }
|
||||
if (CONFIG_BOOL_DEFAULTED(m_config, "service.jid_escaping", true)) {
|
||||
name = Swift::JID::getEscapedNode(name);
|
||||
}
|
||||
else {
|
||||
if (name.find_last_of("@") != std::string::npos) {
|
||||
name.replace(name.find_last_of("@"), 1, "%");
|
||||
}
|
||||
}
|
||||
|
||||
response->setFrom(Swift::JID(name, m_component->getJID().toString()));
|
||||
response->setType(Swift::Presence::Subscribe);
|
||||
|
@ -536,7 +541,12 @@ void NetworkPluginServer::handleBuddyChangedPayload(const std::string &data) {
|
|||
for (int i = 0; i < payload.group_size(); i++) {
|
||||
groups.push_back(payload.group(i));
|
||||
}
|
||||
buddy = new LocalBuddy(user->getRosterManager(), -1, payload.buddyname(), payload.alias(), groups, BUDDY_JID_ESCAPING);
|
||||
if (CONFIG_BOOL_DEFAULTED(m_config, "service.jid_escaping", true)) {
|
||||
buddy = new LocalBuddy(user->getRosterManager(), -1, payload.buddyname(), payload.alias(), groups, BUDDY_JID_ESCAPING);
|
||||
}
|
||||
else {
|
||||
buddy = new LocalBuddy(user->getRosterManager(), -1, payload.buddyname(), payload.alias(), groups, BUDDY_NO_FLAG);
|
||||
}
|
||||
if (!buddy->isValid()) {
|
||||
delete buddy;
|
||||
return;
|
||||
|
|
|
@ -127,7 +127,7 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
|
|||
void handleNormalMessages() {
|
||||
User *user = userManager->getUser("user@localhost");
|
||||
|
||||
TestingConversation *conv = new TestingConversation(user->getConversationManager(), "buddy1");
|
||||
TestingConversation *conv = new TestingConversation(user->getConversationManager(), "buddy1@test");
|
||||
user->getConversationManager()->addConversation(conv);
|
||||
conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
|
||||
|
||||
|
@ -142,13 +142,13 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
|
|||
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("user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
|
||||
|
||||
received.clear();
|
||||
|
||||
// send response
|
||||
msg->setFrom("user@localhost/resource");
|
||||
msg->setTo("buddy1@localhost/bot");
|
||||
msg->setTo("buddy1\\40test@localhost/bot");
|
||||
msg->setBody("response!");
|
||||
injectMessage(msg);
|
||||
loop->processEvents();
|
||||
|
@ -169,20 +169,28 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
|
|||
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("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
|
||||
|
||||
received.clear();
|
||||
|
||||
// disable jid_escaping
|
||||
std::istringstream ifs("service.server_mode = 1\nservice.jid_escaping=0\nservice.jid=localhost\nservice.more_resources=1\n");
|
||||
cfg->load(ifs);
|
||||
|
||||
// and now to bare JID again...
|
||||
user->getConversationManager()->resetResources();
|
||||
conv->handleMessage(msg2);
|
||||
loop->processEvents();
|
||||
|
||||
// enable jid_escaping again
|
||||
std::istringstream ifs2("service.server_mode = 1\nservice.jid_escaping=1\nservice.jid=localhost\nservice.more_resources=1\n");
|
||||
cfg->load(ifs2);
|
||||
|
||||
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("user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1%test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
|
||||
|
||||
received.clear();
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ class LocalBuddyTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
|
|||
CPPUNIT_TEST(createWithInvalidName);
|
||||
CPPUNIT_TEST(buddyFlagsFromJID);
|
||||
CPPUNIT_TEST(JIDToLegacyName);
|
||||
CPPUNIT_TEST(getSafeName);
|
||||
CPPUNIT_TEST(handleBuddyChanged);
|
||||
CPPUNIT_TEST(setAlias);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
@ -70,6 +71,20 @@ class LocalBuddyTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
|
|||
CPPUNIT_ASSERT_EQUAL(std::string("hanzz@test"), Buddy::JIDToLegacyName("hanzz%test@localhost/bot"));
|
||||
}
|
||||
|
||||
void getSafeName() {
|
||||
User *user = userManager->getUser("user@localhost");
|
||||
CPPUNIT_ASSERT(user);
|
||||
|
||||
std::vector<std::string> grp;
|
||||
grp.push_back("group1");
|
||||
LocalBuddy *buddy = new LocalBuddy(user->getRosterManager(), -1, "buddy1@test", "Buddy 1", grp, BUDDY_JID_ESCAPING);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test"), buddy->getSafeName());
|
||||
|
||||
buddy->setFlags(BUDDY_NO_FLAG);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1%test"), buddy->getSafeName());
|
||||
}
|
||||
|
||||
void buddyFlagsFromJID() {
|
||||
CPPUNIT_ASSERT_EQUAL(BUDDY_JID_ESCAPING, Buddy::buddyFlagsFromJID("hanzz\\40test@localhost/bot"));
|
||||
CPPUNIT_ASSERT_EQUAL(BUDDY_NO_FLAG, Buddy::buddyFlagsFromJID("hanzz%test@localhost/bot"));
|
||||
|
|
95
src/tests/networkpluginserver.cpp
Normal file
95
src/tests/networkpluginserver.cpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
#include "transport/userregistry.h"
|
||||
#include "transport/userregistration.h"
|
||||
#include "transport/config.h"
|
||||
#include "transport/storagebackend.h"
|
||||
#include "transport/user.h"
|
||||
#include "transport/transport.h"
|
||||
#include "transport/conversation.h"
|
||||
#include "transport/usermanager.h"
|
||||
#include "transport/localbuddy.h"
|
||||
#include "transport/settingsadhoccommand.h"
|
||||
#include "transport/adhocmanager.h"
|
||||
#include "transport/protocol.pb.h"
|
||||
#include "transport/networkpluginserver.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/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"
|
||||
|
||||
using namespace Transport;
|
||||
|
||||
class NetworkPluginServerTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
|
||||
CPPUNIT_TEST_SUITE(NetworkPluginServerTest);
|
||||
CPPUNIT_TEST(handleBuddyChangedPayload);
|
||||
CPPUNIT_TEST(handleBuddyChangedPayloadNoEscaping);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
NetworkPluginServer *serv;
|
||||
|
||||
void setUp (void) {
|
||||
setMeUp();
|
||||
|
||||
serv = new NetworkPluginServer(component, cfg, userManager, NULL, NULL);
|
||||
connectUser();
|
||||
received.clear();
|
||||
}
|
||||
|
||||
void tearDown (void) {
|
||||
received.clear();
|
||||
disconnectUser();
|
||||
delete serv;
|
||||
tearMeDown();
|
||||
}
|
||||
|
||||
void handleBuddyChangedPayload() {
|
||||
User *user = userManager->getUser("user@localhost");
|
||||
|
||||
pbnetwork::Buddy buddy;
|
||||
buddy.set_username("user@localhost");
|
||||
buddy.set_buddyname("buddy1@test");
|
||||
|
||||
std::string message;
|
||||
buddy.SerializeToString(&message);
|
||||
|
||||
serv->handleBuddyChangedPayload(message);
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
Swift::RosterPayload::ref payload1 = getStanza(received[0])->getPayload<Swift::RosterPayload>();
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) payload1->getItems().size());
|
||||
Swift::RosterItemPayload item = payload1->getItems()[0];
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost"), item.getJID().toString());
|
||||
}
|
||||
|
||||
void handleBuddyChangedPayloadNoEscaping() {
|
||||
std::istringstream ifs("service.server_mode = 1\nservice.jid_escaping=0\nservice.jid=localhost\nservice.more_resources=1\n");
|
||||
cfg->load(ifs);
|
||||
User *user = userManager->getUser("user@localhost");
|
||||
|
||||
pbnetwork::Buddy buddy;
|
||||
buddy.set_username("user@localhost");
|
||||
buddy.set_buddyname("buddy1@test");
|
||||
|
||||
std::string message;
|
||||
buddy.SerializeToString(&message);
|
||||
|
||||
serv->handleBuddyChangedPayload(message);
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
|
||||
Swift::RosterPayload::ref payload1 = getStanza(received[0])->getPayload<Swift::RosterPayload>();
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int) payload1->getItems().size());
|
||||
Swift::RosterItemPayload item = payload1->getItems()[0];
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("buddy1%test@localhost"), item.getJID().toString());
|
||||
|
||||
std::istringstream ifs2("service.server_mode = 1\nservice.jid_escaping=1\nservice.jid=localhost\nservice.more_resources=1\n");
|
||||
cfg->load(ifs2);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION (NetworkPluginServerTest);
|
|
@ -94,7 +94,15 @@ void UserRegistration::handleUnregisterRemoteRosterResponse(boost::shared_ptr<Sw
|
|||
std::list <BuddyInfo> roster;
|
||||
m_storageBackend->getBuddies(userInfo.id, roster);
|
||||
for(std::list<BuddyInfo>::iterator u = roster.begin(); u != roster.end() ; u++){
|
||||
std::string name = Swift::JID::getEscapedNode((*u).legacyName);
|
||||
std::string name = (*u).legacyName;
|
||||
if ((*u).flags & BUDDY_JID_ESCAPING) {
|
||||
name = Swift::JID::getEscapedNode((*u).legacyName);
|
||||
}
|
||||
else {
|
||||
if (name.find_last_of("@") != std::string::npos) {
|
||||
name.replace(name.find_last_of("@"), 1, "%");
|
||||
}
|
||||
}
|
||||
|
||||
Swift::Presence::ref response;
|
||||
response = Swift::Presence::create();
|
||||
|
|
Loading…
Add table
Reference in a new issue