Changeset - 2ae880ff94b5
[Not reviewed]
0 12 0
HanzZ - 14 years ago 2011-03-17 20:31:00
hanzz.k@gmail.com
Basic User and UserManager classes
12 files changed with 213 insertions and 116 deletions:
0 comments (0 inline, 0 general)
examples/usermanager/main.cpp
Show inline comments
 
@@ -17,19 +17,20 @@ int main(void)
 
	}
 
 
	Swift::SimpleEventLoop eventLoop;
 
	Component transport(&eventLoop, &config);
 
	Logger logger(&transport);
 
 
	SQLite3Backend sql(&config);
 
	logger.setStorageBackend(&sql);
 
	if (!sql.connect()) {
 
		std::cout << "Can't connect to database.\n";
 
	}
 
 
	UserManager userManager(&transport);
 
	UserManager userManager(&transport, &sql);
 
	UserRegistration userRegistration(&transport, &userManager, &sql);
 
	logger.setUserRegistration(&userRegistration);
 
	logger.setUserManager(&userManager);
 
 
	transport.connect();
 
	eventLoop.run();
 
}
include/transport/config.h
Show inline comments
 
@@ -27,24 +27,25 @@
 
#include <boost/assign.hpp>
 
#include <boost/bind.hpp>
 
#include <boost/signal.hpp>
 

	
 

	
 
#define CONFIG_STRING(PTR, KEY) (*PTR)[KEY].as<std::string>()
 
#define CONFIG_INT(PTR, KEY) (*PTR)[KEY].as<int>()
 
#define CONFIG_BOOL(PTR, KEY) (*PTR)[KEY].as<bool>()
 
#define CONFIG_LIST(PTR, KEY) (*PTR)[KEY].as<std::list<std::string> >()
 

	
 
namespace Transport {
 

	
 
/// Represents variable:value pairs.
 
typedef boost::program_options::variables_map Variables;
 

	
 
/// Represents config file.
 

	
 
/// It's used to load config file and allows others parts of libtransport to be configured
 
/// properly. Config files are text files which use "ini" format. Variables are divided into multiple
 
/// sections. Every class is configurable with some variables which change its behavior. Check particular
 
/// class documentation to get a list of all relevant variables for that class.
 
class Config {
 
	public:
 
		/// Constructor.
 
		Config() {}
include/transport/logger.h
Show inline comments
 
@@ -43,29 +43,37 @@ class Logger
 

	
 
		/// Logger destructor.
 
		~Logger();
 

	
 
		/// Starts logging data related to StorageBackend class.
 
		/// \param storage storage class
 
		void setStorageBackend(StorageBackend *storage);
 

	
 
		/// Starts logging data related to UserRegistration class.
 
		/// \param userRegistration userRegistration class
 
		void setUserRegistration(UserRegistration *userRegistration);
 

	
 
		/// Starts logging data related to UserManager class.
 
		/// \param userManager userManager class
 
		void setUserManager(UserManager *userManager);
 

	
 
	private:
 
		// Component
 
		void handleConnected();
 
		void handleConnectionError(const Swift::ComponentError &error);
 
		void handleXMLIn(const std::string &data);
 
		void handleXMLOut(const std::string &data);
 

	
 
		// StorageBackend
 
		void handleStorageError(const std::string &statement, const std::string &error);
 

	
 
		// UserRegistration
 
		void handleUserRegistered(const UserInfo &user);
 
		void handleUserUnregistered(const UserInfo &user);
 
		void handleUserUpdated(const UserInfo &user);
 

	
 
		// UserManager
 
		void handleUserCreated(User *user);
 
		void handleUserDestroyed(User *user);
 
};
 

	
 
}
include/transport/transport.h
Show inline comments
 
@@ -56,24 +56,34 @@ namespace Transport {
 
			/// Creates new Component instance.
 
			/// \param loop main event loop 
 
			/// \param config cofiguration, this class uses following Config values:
 
			/// 	- service.jid
 
			/// 	- service.password
 
			/// 	- service.server
 
			/// 	- service.port
 
			Component(Swift::EventLoop *loop, Config *config);
 

	
 
			/// Component destructor.
 
			~Component();
 

	
 
			/// Returns Swift::Component associated with this Transport::Component.
 
			/// You can use it to send presences and other stanzas.
 
			/// \return Swift::Component associated with this Transport::Component
 
			Swift::Component *getComponent();
 

	
 
			/// Returns Swift::PresenceOracle associated with this Transport::Component.
 
			/// You can use it to check current resource connected for particular user.
 
			/// \return Swift::PresenceOracle associated with this Transport::Component
 
			Swift::PresenceOracle *getPresenceOracle();
 

	
 
			/// Connects the Jabber server.
 
			/// \see Component()
 
			void connect();
 

	
 
			/// Sets disco#info features which are sent as answer to
 
			/// disco#info IQ-get. This sets features of transport contact (For example "j2j.domain.tld").
 
			/// \param features list of features as sent in disco#info response
 
			void setTransportFeatures(std::list<std::string> &features);
 

	
 
			/// Sets disco#info features which are sent as answer to
 
			/// disco#info IQ-get. This sets features of legacy network buddies (For example "me\40gmail.com@j2j.domain.tld").
 
			/// \param features list of features as sent in disco#info response
include/transport/user.h
Show inline comments
 
@@ -15,35 +15,57 @@
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#pragma once
 

	
 
#include <time.h>
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Presence/PresenceOracle.h"
 
#include "Swiften/Disco/EntityCapsManager.h"
 
#include "storagebackend.h"
 

	
 
namespace Transport {
 

	
 
class Component;
 
struct UserInfo;
 

	
 
// Representation of XMPP User
 
/// Represents online XMPP user.
 
class User {
 
	public:
 
		User(const Swift::JID &jid, const std::string &username, const std::string &password, Component * component);
 
		/// Creates new User class.
 
		/// \param jid XMPP JID associated with this user
 
		/// \param userInfo UserInfo struct with informations needed to connect
 
		/// this user to legacy network
 
		/// \param component Component associated with this user
 
		User(const Swift::JID &jid, UserInfo &userInfo, Component * component);
 

	
 
		/// Destroyes User.
 
		virtual ~User();
 

	
 
		/// Returns JID of XMPP user who is currently connected using this User class.
 
		/// \return full JID
 
		const Swift::JID &getJID();
 

	
 
		/// Returns UserInfo struct with informations needed to connect the legacy network.
 
		/// \return UserInfo struct
 
		UserInfo &getUserInfo() { return m_userInfo; }
 

	
 
		/// Handles presence from XMPP JID associated with this user.
 
		/// \param presence Swift::Presence.
 
		void handlePresence(Swift::Presence::ref presence);
 

	
 
		/// Returns language.
 
		/// \return language
 
		const char *getLang() { return "en"; }
 

	
 
	private:
 
		Swift::JID m_jid;
 
		Swift::Component *m_component;		
 
		Component *m_component;		
 
		Swift::EntityCapsManager *m_entityCapsManager;
 
		Swift::PresenceOracle *m_presenceOracle;
 
		UserInfo m_userInfo;
 
};
 

	
 
}
include/transport/usermanager.h
Show inline comments
 
@@ -19,37 +19,62 @@
 
 */
 

	
 
#pragma once
 

	
 
#include <string>
 
#include <map>
 
#include "Swiften/Swiften.h"
 

	
 
namespace Transport {
 

	
 
class User;
 
class Component;
 
class StorageBackend;
 

	
 
// Class for managing online XMPP users.
 
class UserManager
 
{
 
/// Manages online XMPP Users.
 

	
 
/// This class handles presences and creates User classes when new user connects.
 
/// It also removes the User class once the last user's resource disconnected.
 
class UserManager {
 
	public:
 
		UserManager(Component *component);
 
		/// Creates new UserManager.
 
		/// \param component Component which's presence will be handled
 
		/// \param storageBackend Storage backend used to fetch UserInfos
 
		UserManager(Component *component, StorageBackend *storageBackend);
 

	
 
		/// Destroys UserManager.
 
		~UserManager();
 

	
 
		// User *
 
		User *getUserByJID(const std::string &barejid);
 
		/// Returns user according to his bare JID.
 
		/// \param barejid bare JID of user
 
		/// \return User class associated with this user
 
		User *getUser(const std::string &barejid);
 

	
 
		/// Returns number of online users.
 
		/// \return number of online users
 
		int getUserCount();
 

	
 
		/// Removes user. This function disconnects user and safely removes
 
		/// User class. This does *not* remove user from database.
 
		/// \param user User class to remove
 
		void removeUser(User *user);
 

	
 
		// Returns count of online users;
 
		int userCount();
 
		/// Called when new User class is created.
 
		/// \param user newly created User class
 
		boost::signal<void (User *user)> onUserCreated;
 

	
 
		void removeUser(User *user) {}
 
		/// Called when User class is going to be removed
 
		/// \param user removed User class
 
		boost::signal<void (User *user)> onUserDestroyed;
 

	
 
	private:
 
		void handlePresence(Swift::Presence::ref presence);
 
		void addUser(User *user);
 

	
 
		long m_onlineBuddies;
 
		User *m_cachedUser;
 
		std::map<std::string, User *> m_users;
 
		Component *m_component;
 
		StorageBackend *m_storageBackend;
 
};
 

	
 
}
include/transport/userregistration.h
Show inline comments
 
@@ -24,38 +24,58 @@
 
#include "Swiften/Queries/GetResponder.h"
 
#include "Swiften/Queries/SetResponder.h"
 
#include "Swiften/Elements/InBandRegistrationPayload.h"
 

	
 
namespace Transport {
 

	
 
struct UserInfo;
 
class Component;
 
class StorageBackend;
 
class UserManager;
 
class Config;
 

	
 
/// Allows users to register the transport using service discovery.
 
class UserRegistration : Swift::GetResponder<Swift::InBandRegistrationPayload>, Swift::SetResponder<Swift::InBandRegistrationPayload> {
 
	public:
 
		/// Creates new UserRegistration handler.
 
		/// \param component Component associated with this class
 
		/// \param userManager UserManager associated with this class
 
		/// \param storageBackend StorageBackend where the registered users will be stored
 
		UserRegistration(Component *component, UserManager *userManager, StorageBackend *storageBackend);
 

	
 
		/// Destroys UserRegistration.
 
		~UserRegistration();
 

	
 
		// Registers new user, returns false if user was already registered.
 
		bool registerUser(const UserInfo &user);
 
		/// Registers new user. This function stores user into database and subscribe user to transport.
 
		/// \param userInfo UserInfo struct with informations about registered user
 
		/// \return false if user is already registered
 
		bool registerUser(const UserInfo &userInfo);
 

	
 
		// Unregisters user, returns true if user was successfully unregistered.
 
		/// Unregisters user. This function removes all data about user from databa, unsubscribe all buddies
 
		/// managed by this transport and disconnects user if he's connected.
 
		/// \param barejid bare JID of user to unregister
 
		/// \return false if there is no such user registered
 
		bool unregisterUser(const std::string &barejid);
 

	
 
		boost::signal<void (const UserInfo &user)> onUserRegistered;
 
		boost::signal<void (const UserInfo &user)> onUserUnregistered;
 
		boost::signal<void (const UserInfo &user)> onUserUpdated;
 
		/// Called when new user has been registered.
 
		/// \param userInfo UserInfo struct with informations about user
 
		boost::signal<void (const UserInfo &userInfo)> onUserRegistered;
 

	
 
		/// Called when user has been unregistered.
 
		/// \param userInfo UserInfo struct with informations about user
 
		boost::signal<void (const UserInfo &userInfo)> onUserUnregistered;
 

	
 
		/// Called when user's registration has been updated.
 
		/// \param userInfo UserInfo struct with informations about user
 
		boost::signal<void (const UserInfo &userInfo)> onUserUpdated;
 

	
 
	private:
 
		bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload);
 
		bool handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::InBandRegistrationPayload> payload);
 
		
 
		Component *m_component;
 
		StorageBackend *m_storageBackend;
 
		UserManager *m_userManager;
 
		Config *m_config;
 

	
 
};
 

	
src/logger.cpp
Show inline comments
 
@@ -41,24 +41,28 @@ Logger::~Logger(){
 
}
 

	
 
void Logger::setStorageBackend(StorageBackend *storage) {
 
	storage->onStorageError.connect(bind(&Logger::handleStorageError, this, _1, _2));
 
}
 

	
 
void Logger::setUserRegistration(UserRegistration *userRegistration) {
 
	userRegistration->onUserRegistered.connect(bind(&Logger::handleUserRegistered, this, _1));
 
	userRegistration->onUserUnregistered.connect(bind(&Logger::handleUserUnregistered, this, _1));
 
	userRegistration->onUserUpdated.connect(bind(&Logger::handleUserUpdated, this, _1));
 
}
 

	
 
void Logger::setUserManager(UserManager *userManager) {
 
	userManager->onUserCreated.connect(bind(&Logger::handleUserCreated, this, _1));
 
	userManager->onUserDestroyed.connect(bind(&Logger::handleUserDestroyed, this, _1));
 
}
 

	
 
void Logger::handleConnected() {
 
	std::cout << "[COMPONENT] Connected to Jabber Server!\n";
 
}
 

	
 
void Logger::handleConnectionError(const Swift::ComponentError &error) {
 
	std::cout << "[COMPONENT] Connection Error!\n";
 
	switch (error.getType()) {
 
		case Swift::ComponentError::UnknownError: std::cout << "[COMPONENT] Disconnect reason: UnknownError\n"; break;
 
		case Swift::ComponentError::ConnectionError: std::cout << "[COMPONENT] Disconnect reason: ConnectionError\n"; break;
 
		case Swift::ComponentError::ConnectionReadError: std::cout << "[COMPONENT] Disconnect reason: ConnectionReadError\n"; break;
 
		case Swift::ComponentError::ConnectionWriteError: std::cout << "[COMPONENT] Disconnect reason: ConnectionWriteError\n"; break;
 
@@ -83,13 +87,21 @@ void Logger::handleStorageError(const std::string &statement, const std::string
 
void Logger::handleUserRegistered(const UserInfo &user) {
 
	std::cout << "[REGISTRATION] User \"" << user.jid << "\" registered as \"" << user.uin << "\"\n";
 
}
 

	
 
void Logger::handleUserUnregistered(const UserInfo &user) {
 
	std::cout << "[REGISTRATION] User \"" << user.jid << "\" unregistered \"" << user.uin << "\"\n";
 
}
 

	
 
void Logger::handleUserUpdated(const UserInfo &user) {
 
	std::cout << "[REGISTRATION] User \"" << user.jid << "\" updated \"" << user.uin << "\"\n";
 
}
 

	
 
void Logger::handleUserCreated(User *user) {
 
	std::cout << "[USERMANAGER] User \"" << user->getJID().toString() << "\" (UIN: \"" << user->getUserInfo().uin << "\") connected and User class has been created\n";
 
}
 

	
 
void Logger::handleUserDestroyed(User *user) {
 
	std::cout << "[USERMANAGER] User \"" << user->getJID().toBare().toString() << "\" (UIN: \"" << user->getUserInfo().uin << "\") disconnected and User class is going to be destroyed\n";
 
}
 

	
 
}
src/transport.cpp
Show inline comments
 
@@ -69,24 +69,32 @@ Component::Component(Swift::EventLoop *loop, Config *config) {
 

	
 
Component::~Component() {
 
	delete m_presenceOracle;
 
	delete m_entityCapsManager;
 
	delete m_capsManager;
 
	delete m_capsMemoryStorage;
 
// 	delete m_discoInfoResponder;
 
// 	delete m_registerHandler;
 
	delete m_component;
 
	delete m_factories;
 
}
 

	
 
Swift::Component *Component::getComponent() {
 
	return m_component;
 
}
 

	
 
Swift::PresenceOracle *Component::getPresenceOracle() {
 
	return m_presenceOracle;
 
}
 

	
 
void Component::setTransportFeatures(std::list<std::string> &features) {
 
	m_discoInfoResponder->setTransportFeatures(features);
 
}
 

	
 
void Component::setBuddyFeatures(std::list<std::string> &features) {
 
	// TODO: handle caps change
 
	m_discoInfoResponder->setBuddyFeatures(features);
 
}
 

	
 
void Component::connect() {
 
	m_reconnectCount++;
 
	m_component->connect(CONFIG_STRING(m_config, "service.server"), CONFIG_INT(m_config, "service.port"));
src/user.cpp
Show inline comments
 
@@ -11,34 +11,51 @@
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 
#include "Swiften/Swiften.h"
 

	
 
namespace Transport {
 

	
 
User::User(const Swift::JID &jid, const std::string &username, const std::string &password, Component *component) {
 
User::User(const Swift::JID &jid, UserInfo &userInfo, Component *component) {
 
	m_jid = jid;
 

	
 
	m_component = component->m_component;
 
	m_component = component;
 
	m_presenceOracle = component->m_presenceOracle;
 
	m_entityCapsManager = component->m_entityCapsManager;
 
// 	m_activeResource = m_jid.getResource();
 

	
 
	m_userInfo = userInfo;
 
}
 

	
 
User::~User(){
 

	
 
}
 

	
 
const Swift::JID &User::getJID() {
 
	return m_jid;
 
}
 

	
 
void User::handlePresence(Swift::Presence::ref presence) {
 
	Swift::Presence::ref highest = m_presenceOracle->getHighestPriorityPresence(m_jid.toBare());
 
	if (highest) {
 
		highest->setTo(presence->getFrom());
 
		highest->setFrom(m_component->getJID());
 
		m_component->getComponent()->sendPresence(highest);
 
	}
 
	else {
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setTo(m_jid.toBare());
 
		response->setFrom(m_component->getJID());
 
		response->setType(Swift::Presence::Unavailable);
 
		m_component->getComponent()->sendPresence(response);
 
	}
 
}
 

	
 
}
src/usermanager.cpp
Show inline comments
 
@@ -12,151 +12,124 @@
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "transport/usermanager.h"
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 

	
 
namespace Transport {
 

	
 
UserManager::UserManager(Component *component) {
 
UserManager::UserManager(Component *component, StorageBackend *storageBackend) {
 
	m_cachedUser = NULL;
 
	m_onlineBuddies = 0;
 
	m_component = component;
 
	m_storageBackend = storageBackend;
 

	
 
	component->onUserPresenceReceived.connect(bind(&UserManager::handlePresence, this, _1));
 
}
 

	
 
UserManager::~UserManager(){
 
}
 

	
 
User *UserManager::getUserByJID(const std::string &barejid){
 
void UserManager::addUser(User *user) {
 
	m_users[user->getJID().toBare().toString()] = user;
 
	onUserCreated(user);
 
}
 

	
 
User *UserManager::getUser(const std::string &barejid){
 
	if (m_cachedUser && barejid == m_cachedUser->getJID().toBare().toString()) {
 
		return m_cachedUser;
 
	}
 

	
 
	if (m_users.find(barejid) != m_users.end()) {
 
		User *user = m_users[barejid];
 
		m_cachedUser = user;
 
		return user;
 
	}
 
	return NULL;
 
}
 

	
 
int UserManager::userCount() {
 
void UserManager::removeUser(User *user) {
 
	m_users.erase(user->getJID().toBare().toString());
 
	if (m_cachedUser == user)
 
		m_cachedUser = NULL;
 
	onUserDestroyed(user);
 
	delete user;
 
}
 

	
 
int UserManager::getUserCount() {
 
	return m_users.size();
 
}
 

	
 
void UserManager::handlePresence(Swift::Presence::ref presence) {
 
// 	std::string barejid = presence->getTo().toBare().toString().getUTF8String();
 
// 	std::string userkey = presence->getFrom().toBare().toString().getUTF8String();
 
// // 	if (Transport::instance()->protocol()->tempAccountsAllowed()) {
 
// // 		std::string server = barejid.substr(barejid.find("%") + 1, barejid.length() - barejid.find("%"));
 
// // 		userkey += server;
 
// // 	}
 
// 
 
// 	User *user = getUserByJID(userkey);
 
// 	if (user ) {
 
// 		user->handlePresence(presence);
 
// 	}
 
// 	else {
 
// // 		// No user, unavailable presence... nothing to do
 
// // 		if (presence->getType() == Swift::Presence::Unavailable) {
 
// // 			Swift::Presence::ref response = Swift::Presence::create();
 
// // 			response->setTo(presence->getFrom());
 
// // 			response->setFrom(Swift::JID(Transport::instance()->jid()));
 
// // 			response->setType(Swift::Presence::Unavailable);
 
// // 			m_component->sendPresence(response);
 
// // 
 
// // // 			UserRow res = Transport::instance()->sql()->getUserByJid(userkey);
 
// // // 			if (res.id != -1) {
 
// // // 				Transport::instance()->sql()->setUserOnline(res.id, false);
 
// // // 			}
 
// // 			return;
 
// // 		}
 
// // 		UserRow res = Transport::instance()->sql()->getUserByJid(userkey);
 
// // 		if (res.id == -1 && !Transport::instance()->protocol()->tempAccountsAllowed()) {
 
// // 			// presence from unregistered user
 
// // 			Log(presence->getFrom().toString().getUTF8String(), "This user is not registered");
 
// // 			return;
 
// // 		}
 
// // 		else {
 
// // 			if (res.id == -1 && Transport::instance()->protocol()->tempAccountsAllowed()) {
 
// // 				res.jid = userkey;
 
// // 				res.uin = presence->getFrom().toBare().toString().getUTF8String();
 
// // 				res.password = "";
 
// // 				res.language = "en";
 
// // 				res.encoding = CONFIG().encoding;
 
// // 				res.vip = 0;
 
// // 				Transport::instance()->sql()->addUser(res);
 
// // 				res = Transport::instance()->sql()->getUserByJid(userkey);
 
// // 			}
 
// // 
 
	std::string barejid = presence->getTo().toBare().toString();
 
	std::string userkey = presence->getFrom().toBare().toString();
 

	
 
	User *user = getUser(userkey);
 
	if (!user ) {
 
		// No user and unavailable presence -> answer with unavailable
 
		if (presence->getType() == Swift::Presence::Unavailable) {
 
			Swift::Presence::ref response = Swift::Presence::create();
 
			response->setTo(presence->getFrom());
 
			response->setFrom(presence->getTo());
 
			response->setType(Swift::Presence::Unavailable);
 
			m_component->getComponent()->sendPresence(response);
 

	
 
			UserInfo res;
 
			bool registered = m_storageBackend->getUser(userkey, res);
 
			if (registered) {
 
				m_storageBackend->setUserOnline(res.id, false);
 
			}
 
			return;
 
		}
 

	
 
		UserInfo res;
 
		bool registered = m_storageBackend->getUser(userkey, res);
 

	
 
		if (!registered) {
 
			// TODO: logging
 
			return;
 
		}
 

	
 
		// TODO: isVIP
 
// // 			bool isVip = res.vip;
 
// // 			std::list<std::string> const &x = CONFIG().allowedServers;
 
// // 			if (CONFIG().onlyForVIP && !isVip && std::find(x.begin(), x.end(), presence->getFrom().getDomain().getUTF8String()) == x.end()) {
 
// // 				Log(presence->getFrom().toString().getUTF8String(), "This user is not VIP, can't login...");
 
// // 				return;
 
// // 			}
 
// // 
 
// // 			Log(presence->getFrom().toString().getUTF8String(), "Creating new User instance");
 
// // 
 
// // 			if (Transport::instance()->protocol()->tempAccountsAllowed()) {
 
// // 				std::string server = barejid.substr(barejid.find("%") + 1, barejid.length() - barejid.find("%"));
 
// // 				res.uin = presence->getTo().getResource().getUTF8String() + "@" + server;
 
// // 			}
 
// // 			else {
 
// // 				if (purple_accounts_find(res.uin.c_str(), Transport::instance()->protocol()->protocol().c_str()) != NULL) {
 
// // 					PurpleAccount *act = purple_accounts_find(res.uin.c_str(), Transport::instance()->protocol()->protocol().c_str());
 
// // 					user = Transport::instance()->userManager()->getUserByAccount(act);
 
// // 					if (user) {
 
// // 						Log(presence->getFrom().toString().getUTF8String(), "This account is already connected by another jid " << user->jid());
 
// // 						return;
 
// // 					}
 
// // 				}
 
// // 			}
 
// // 			user = (AbstractUser *) new User(res, userkey, m_component, m_presenceOracle, m_entityCapsManager);
 
				user = new User(presence->getFrom(), res, m_component);
 
				// TODO: handle features somehow
 
// // 			user->setFeatures(isVip ? CONFIG().VIPFeatures : CONFIG().transportFeatures);
 
// // // 				if (c != NULL)
 
// // // 					if (Transport::instance()->hasClientCapabilities(c->findAttribute("ver")))
 
// // // 						user->setResource(stanza.from().resource(), stanza.priority(), Transport::instance()->getCapabilities(c->findAttribute("ver")));
 
// // // 
 
// // 			Transport::instance()->userManager()->addUser(user);
 
// // 			user->receivedPresence(presence);
 
// // // 				if (protocol()->tempAccountsAllowed()) {
 
// // // 					std::string server = stanza.to().username().substr(stanza.to().username().find("%") + 1, stanza.to().username().length() - stanza.to().username().find("%"));
 
// // // 					server = stanza.from().bare() + server;
 
// // // 					purple_timeout_add_seconds(15, &connectUser, g_strdup(server.c_str()));
 
// // // 				}
 
// // // 				else
 
// // // 					purple_timeout_add_seconds(15, &connectUser, g_strdup(stanza.from().bare().c_str()));
 
// // // 			}
 
// // 		}
 
// // // 		if (stanza.presence() == Presence::Unavailable && stanza.to().username() == ""){
 
// // // 			Log(stanza.from().full(), "User is already logged out => sending unavailable presence");
 
// // // 			Tag *tag = new Tag("presence");
 
// // // 			tag->addAttribute( "to", stanza.from().bare() );
 
// // // 			tag->addAttribute( "type", "unavailable" );
 
// // // 			tag->addAttribute( "from", jid() );
 
// // // 			j->send( tag );
 
// // // 		}
 
// 	}
 
// 
 
// 	if (presence->getType() == Swift::Presence::Unavailable) {
 
// 		if (user) {
 
// 			Swift::Presence::ref highest = m_presenceOracle->getHighestPriorityPresence(presence->getFrom().toBare());
 
// 			if (presence->getType() == Swift::Presence::Unavailable && (!highest || (highest && highest->getType() == Swift::Presence::Unavailable))) {
 
// 				Transport::instance()->userManager()->removeUser(user);
 
// 			}
 
// 		}
 
			addUser(user);
 
	}
 
	user->handlePresence(presence);
 

	
 
	if (presence->getType() == Swift::Presence::Unavailable) {
 
		if (user) {
 
			Swift::Presence::ref highest = m_component->getPresenceOracle()->getHighestPriorityPresence(presence->getFrom().toBare());
 
			// There's no presence for this user, so disconnect
 
			if (!highest || (highest && highest->getType() == Swift::Presence::Unavailable)) {
 
				removeUser(user);
 
			}
 
		}
 
		// TODO: HANDLE MUC SOMEHOW
 
// 		else if (user && Transport::instance()->protocol()->tempAccountsAllowed() && !((User *) user)->hasOpenedMUC()) {
 
// 			Transport::instance()->userManager()->removeUser(user);
 
// 		}
 
// 	}
 
	}
 
}
 

	
 
}
src/userregistration.cpp
Show inline comments
 
@@ -65,25 +65,25 @@ bool UserRegistration::registerUser(const UserInfo &row) {
 

	
 
bool UserRegistration::unregisterUser(const std::string &barejid) {
 
	UserInfo userInfo;
 
	bool registered = m_storageBackend->getUser(barejid, userInfo);
 
	// This user is not registered
 
	if (!registered)
 
		return false;
 

	
 
	onUserUnregistered(userInfo);
 

	
 
	Swift::Presence::ref response;
 

	
 
	User *user = m_userManager->getUserByJID(barejid);
 
	User *user = m_userManager->getUser(barejid);
 

	
 
	// roster contains already escaped jids
 
	std::list <std::string> roster;
 
	m_storageBackend->getBuddies(userInfo.id, roster);
 

	
 
	for(std::list<std::string>::iterator u = roster.begin(); u != roster.end() ; u++){
 
		std::string name = *u;
 

	
 
		response = Swift::Presence::create();
 
		response->setTo(Swift::JID(barejid));
 
		response->setFrom(Swift::JID(name + "@" + m_component->getJID().toString()));
 
		response->setType(Swift::Presence::Unsubscribe);
0 comments (0 inline, 0 general)