Merge pull request #20 from darchigh/master
If server supports remote-roster protoXEP, send initial subscribe presence from transport contact using it
This commit is contained in:
commit
7cd30707da
3 changed files with 100 additions and 25 deletions
|
@ -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;
|
||||
|
|
|
@ -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));
|
||||
|
||||
|
|
|
@ -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 ¬ify_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 ¬ify_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);
|
||||
|
|
Loading…
Add table
Reference in a new issue