Merged with master, fixing conflicts for postgres fix

This commit is contained in:
Daniel Henninger 2013-01-11 13:57:58 -05:00
commit 8c78fb8f12
4 changed files with 106 additions and 31 deletions

View file

@ -71,6 +71,7 @@ class UserRegistration : public Swift::Responder<Swift::InBandRegistrationPayloa
virtual bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload);
virtual bool handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload);
void handleRegisterRemoteRosterResponse(boost::shared_ptr<Swift::RosterPayload> payload, Swift::ErrorPayload::ref error, const UserInfo &row);
void handleUnregisterRemoteRosterResponse(boost::shared_ptr<Swift::RosterPayload> payload, Swift::ErrorPayload::ref error, const std::string &barejid);
Component *m_component;

View file

@ -48,32 +48,32 @@ void PQXXBackend::disconnect() {
}
bool PQXXBackend::connect() {
std::string connection_str;
connection_str = CONFIG_STRING(m_config, "database.connectionstring");
if (!connection_str) {
if (connection_str.empty()) {
LOG4CXX_INFO(logger, "Connecting PostgreSQL server " << CONFIG_STRING(m_config, "database.server") << ", user " <<
CONFIG_STRING(m_config, "database.user") << ", database " << CONFIG_STRING(m_config, "database.database") <<
", port " << CONFIG_INT(m_config, "database.port")
);
connection_str = "dbname=";
connection_str += CONFIG_STRING(m_config, "database.database");
if (CONFIG_STRING(m_config, "database.server")) {
if (!CONFIG_STRING(m_config, "database.server").empty()) {
connection_str += " host=" + CONFIG_STRING(m_config, "database.server");
}
if (CONFIG_STRING(m_config, "database.user")) {
if (!CONFIG_STRING(m_config, "database.user").empty()) {
connection_str += " user=" + CONFIG_STRING(m_config, "database.user");
}
if (CONFIG_STRING(m_config, "database.password")) {
if (!CONFIG_STRING(m_config, "database.password").empty()) {
connection_str += " password=" + CONFIG_STRING(m_config, "database.password");
}
if (CONFIG_STRING(m_config, "database.port")) {
if (!CONFIG_STRING(m_config, "database.port").empty()) {
connection_str += " port=" + CONFIG_STRING(m_config, "database.password");
}
}
else {
LOG4CXX_INFO(logger, "Connecting PostgreSQL server via provided connection string.");
}
try {
m_conn = new pqxx::connection(connection_str);
}

View file

@ -26,6 +26,7 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
CPPUNIT_TEST(getForm);
CPPUNIT_TEST(getFormRegistered);
CPPUNIT_TEST(registerUser);
CPPUNIT_TEST(registerUserWithoutRR);
CPPUNIT_TEST(unregisterUser);
CPPUNIT_TEST(unregisterEmptyPayload);
CPPUNIT_TEST(registerUserNotify);
@ -96,12 +97,23 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[1])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[1]))->getType());
iq = Swift::IQ::createResult(Swift::JID("localhost"), getStanza(received[0])->getTo(), getStanza(received[0])->getID(), boost::shared_ptr<Swift::Payload>(new Swift::RosterPayload()));
received.clear();
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Set, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), getStanza(received[0])->getPayload<Swift::RosterPayload>()->getItems()[0].getJID().toString());
UserInfo user;
CPPUNIT_ASSERT_EQUAL(true, storage->getUser("user@localhost", user));
@ -109,6 +121,30 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
CPPUNIT_ASSERT_EQUAL(std::string("password"), user.password);
}
void registerUserWithoutRR(){
Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload();
reg->setUsername("legacyname");
reg->setPassword("password");
boost::shared_ptr<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr<Swift::Payload>(reg));
iq->setFrom("user@localhost");
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[1])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[1]))->getType());
iq = Swift::IQ::createError(Swift::JID("localhost"), getStanza(received[0])->getTo(), getStanza(received[0])->getID());
received.clear();
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
}
void unregisterUser() {
registerUser();
received.clear();
@ -153,17 +189,27 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(3, (int) received.size());
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[1])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[1]))->getType());
iq = Swift::IQ::createResult(Swift::JID("localhost"), getStanza(received[0])->getTo(), getStanza(received[0])->getID(), boost::shared_ptr<Swift::Payload>(new Swift::RosterPayload()));
received.clear();
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Set, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), getStanza(received[0])->getPayload<Swift::RosterPayload>()->getItems()[0].getJID().toString());
CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[1])));
CPPUNIT_ASSERT_EQUAL(std::string("registered: user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getBody());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[2])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[2]))->getType());
UserInfo user;
CPPUNIT_ASSERT_EQUAL(true, storage->getUser("user@localhost", user));
@ -293,12 +339,22 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[1])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[1]))->getType());
iq = Swift::IQ::createResult(Swift::JID("localhost"), getStanza(received[0])->getTo(), getStanza(received[0])->getID(), boost::shared_ptr<Swift::Payload>(new Swift::RosterPayload()));
received.clear();
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Set, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::RosterPayload>());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), getStanza(received[0])->getPayload<Swift::RosterPayload>()->getItems()[0].getJID().toString());
UserInfo user;
CPPUNIT_ASSERT_EQUAL(true, storage->getUser("user@localhost", user));

View file

@ -56,21 +56,11 @@ bool UserRegistration::registerUser(const UserInfo &row) {
m_storageBackend->setUser(row);
Swift::Presence::ref response = Swift::Presence::create();
response->setFrom(m_component->getJID());
response->setTo(Swift::JID(row.jid));
response->setType(Swift::Presence::Subscribe);
m_component->getStanzaChannel()->sendPresence(response);
//same as in unregisterUser but here we have to pass UserInfo to handleRegisterRRResponse
AddressedRosterRequest::ref request = AddressedRosterRequest::ref(new AddressedRosterRequest(m_component->getIQRouter(),row.jid));
request->onResponse.connect(boost::bind(&UserRegistration::handleRegisterRemoteRosterResponse, this, _1, _2, row));
request->send();
onUserRegistered(row);
BOOST_FOREACH(const std::string &notify_jid, CONFIG_VECTOR(m_component->getConfig(),"registration.notify_jid")) {
boost::shared_ptr<Swift::Message> msg(new Swift::Message());
msg->setBody(std::string("registered: ") + row.jid);
msg->setTo(notify_jid);
msg->setFrom(m_component->getJID());
m_component->getStanzaChannel()->sendMessage(msg);
}
return true;
}
@ -91,6 +81,34 @@ bool UserRegistration::unregisterUser(const std::string &barejid) {
return true;
}
void UserRegistration::handleRegisterRemoteRosterResponse(boost::shared_ptr<Swift::RosterPayload> payload, Swift::ErrorPayload::ref remoteRosterNotSupported /*error*/, const UserInfo &row){
if (remoteRosterNotSupported || !payload) {
Swift::Presence::ref response = Swift::Presence::create();
response->setFrom(m_component->getJID());
response->setTo(Swift::JID(row.jid));
response->setType(Swift::Presence::Subscribe);
m_component->getStanzaChannel()->sendPresence(response);
}
else{
Swift::RosterPayload::ref payload = Swift::RosterPayload::ref(new Swift::RosterPayload());
Swift::RosterItemPayload item;
item.setJID(m_component->getJID());
item.setSubscription(Swift::RosterItemPayload::Both);
payload->addItem(item);
Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(payload, row.jid, m_component->getIQRouter());
request->send();
}
onUserRegistered(row);
BOOST_FOREACH(const std::string &notify_jid, CONFIG_VECTOR(m_component->getConfig(),"registration.notify_jid")) {
boost::shared_ptr<Swift::Message> msg(new Swift::Message());
msg->setBody(std::string("registered: ") + row.jid);
msg->setTo(notify_jid);
msg->setFrom(m_component->getJID());
m_component->getStanzaChannel()->sendMessage(msg);
}
}
void UserRegistration::handleUnregisterRemoteRosterResponse(boost::shared_ptr<Swift::RosterPayload> payload, Swift::ErrorPayload::ref remoteRosterNotSupported /*error*/, const std::string &barejid) {
UserInfo userInfo;
bool registered = m_storageBackend->getUser(barejid, userInfo);