From 7f76ef28bd610caee72609c5e798403d1d7872b3 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Thu, 6 Dec 2012 14:57:38 +0100 Subject: [PATCH] added [registration] notify_jid option to send notification about user registration/unregistration --- src/config.cpp | 1 + src/tests/userregistration.cpp | 68 ++++++++++++++++++++++++++++++++++ src/userregistration.cpp | 16 ++++++++ 3 files changed, 85 insertions(+) diff --git a/src/config.cpp b/src/config.cpp index 6bf3a4b2..c37048fd 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -114,6 +114,7 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description ("registration.auto_register", value()->default_value(false), "Register new user automatically when the presence arrives.") ("registration.encoding", value()->default_value("utf8"), "Default encoding in registration form") ("registration.require_local_account", value()->default_value(false), "True if users have to have a local account to register to this transport from remote servers.") + ("registration.notify_jid", value >()->multitoken(), "Send message to this JID if user registers/unregisters") ("registration.local_username_label", value()->default_value("Local username:"), "Label for local usernme field") ("registration.local_account_server", value()->default_value("localhost"), "The server on which the local accounts will be checked for validity") ("registration.local_account_server_timeout", value()->default_value(10000), "Timeout when checking local user on local_account_server (msecs)") diff --git a/src/tests/userregistration.cpp b/src/tests/userregistration.cpp index d97db6db..1c81aa5b 100644 --- a/src/tests/userregistration.cpp +++ b/src/tests/userregistration.cpp @@ -28,6 +28,8 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest CPPUNIT_TEST(registerUser); CPPUNIT_TEST(unregisterUser); CPPUNIT_TEST(unregisterEmptyPayload); + CPPUNIT_TEST(registerUserNotify); + CPPUNIT_TEST(unregisterUserNotify); CPPUNIT_TEST(changePassword); CPPUNIT_TEST(registerUserEmpty); CPPUNIT_TEST(registerUserNoNeedPassword); @@ -139,6 +141,72 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest CPPUNIT_ASSERT_EQUAL(false, storage->getUser("user@localhost", user)); } + void registerUserNotify() { + std::istringstream ifs("service.server_mode = 1\nregistration.notify_jid=user@localhost\nservice.jid=localhost\nservice.more_resources=1\n"); + cfg->load(ifs); + + Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload(); + reg->setUsername("legacyname"); + reg->setPassword("password"); + boost::shared_ptr iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr(reg)); + iq->setFrom("user@localhost"); + injectIQ(iq); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(3, (int) received.size()); + + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[0]))); + CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, dynamic_cast(getStanza(received[0]))->getType()); + + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[1]))); + CPPUNIT_ASSERT_EQUAL(std::string("registered: user@localhost"), dynamic_cast(getStanza(received[1]))->getBody()); + + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[2]))); + CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast(getStanza(received[2]))->getType()); + + UserInfo user; + CPPUNIT_ASSERT_EQUAL(true, storage->getUser("user@localhost", user)); + + CPPUNIT_ASSERT_EQUAL(std::string("legacyname"), user.uin); + CPPUNIT_ASSERT_EQUAL(std::string("password"), user.password); + } + + void unregisterUserNotify() { + registerUserNotify(); + received.clear(); + + Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload(); + reg->setRemove(true); + boost::shared_ptr iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr(reg)); + iq->setFrom("user@localhost"); + injectIQ(iq); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(2, (int) received.size()); + + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[1]))); + CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast(getStanza(received[1]))->getType()); + + iq = Swift::IQ::createResult(Swift::JID("localhost"), getStanza(received[0])->getTo(), getStanza(received[0])->getID(), boost::shared_ptr(new Swift::RosterPayload())); + received.clear(); + injectIQ(iq); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(2, (int) received.size()); + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[0]))); + CPPUNIT_ASSERT_EQUAL(Swift::IQ::Set, dynamic_cast(getStanza(received[0]))->getType()); + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[1]))); + CPPUNIT_ASSERT_EQUAL(std::string("unregistered: user@localhost"), dynamic_cast(getStanza(received[1]))->getBody()); + + + UserInfo user; + CPPUNIT_ASSERT_EQUAL(false, storage->getUser("user@localhost", user)); + } + void changePassword() { registerUser(); received.clear(); diff --git a/src/userregistration.cpp b/src/userregistration.cpp index 4160f9e0..363812ce 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -63,6 +63,14 @@ bool UserRegistration::registerUser(const UserInfo &row) { m_component->getStanzaChannel()->sendPresence(response); onUserRegistered(row); + + BOOST_FOREACH(const std::string ¬ify_jid, CONFIG_VECTOR(m_component->getConfig(),"registration.notify_jid")) { + boost::shared_ptr 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; } @@ -165,6 +173,14 @@ void UserRegistration::handleUnregisterRemoteRosterResponse(boost::shared_ptrgetIQRouter()); request->send(); } + + BOOST_FOREACH(const std::string ¬ify_jid, CONFIG_VECTOR(m_component->getConfig(),"registration.notify_jid")) { + boost::shared_ptr msg(new Swift::Message()); + msg->setBody(std::string("unregistered: ") + barejid); + msg->setTo(notify_jid); + msg->setFrom(m_component->getJID()); + m_component->getStanzaChannel()->sendMessage(msg); + } } bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr payload) {