Changeset - b0bcade44c94
[Not reviewed]
0 11 0
HanzZ - 14 years ago 2011-06-17 13:43:31
hanzz.k@gmail.com
Hopefully working authorizations. Tested it only once :)
11 files changed with 238 insertions and 10 deletions:
0 comments (0 inline, 0 general)
backends/libpurple/main.cpp
Show inline comments
 
@@ -36,24 +36,33 @@ static gboolean list_purple_settings = FALSE;
 

	
 
static GOptionEntry options_entries[] = {
 
	{ "nodaemon", 'n', 0, G_OPTION_ARG_NONE, &nodaemon, "Disable background daemon mode", NULL },
 
	{ "logfile", 'l', 0, G_OPTION_ARG_STRING, &logfile, "Set file to log", NULL },
 
	{ "pidfile", 'p', 0, G_OPTION_ARG_STRING, &lock_file, "File where to write transport PID", NULL },
 
	{ "version", 'v', 0, G_OPTION_ARG_NONE, &ver, "Shows Spectrum version", NULL },
 
	{ "list-purple-settings", 's', 0, G_OPTION_ARG_NONE, &list_purple_settings, "Lists purple settings which can be used in config file", NULL },
 
	{ "host", 'h', 0, G_OPTION_ARG_STRING, &host, "Host to connect to", NULL },
 
	{ "port", 'p', 0, G_OPTION_ARG_INT, &port, "Port to connect to", NULL },
 
	{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, "", NULL }
 
};
 

	
 
struct authRequest {
 
	PurpleAccountRequestAuthorizationCb authorize_cb;
 
	PurpleAccountRequestAuthorizationCb deny_cb;
 
	void *user_data;
 
	std::string who;
 
	PurpleAccount *account;
 
	std::string mainJID;	// JID of user connected with this request
 
};
 

	
 
static void * requestInput(const char *title, const char *primary,const char *secondary, const char *default_value, gboolean multiline, gboolean masked, gchar *hint,const char *ok_text, GCallback ok_cb,const char *cancel_text, GCallback cancel_cb, PurpleAccount *account, const char *who,PurpleConversation *conv, void *user_data) {
 
	std::cout << "REQUEST INPUT\n";
 
	if (primary) {
 
		std::string primaryString(primary);
 
		std::cout << "REQUEST INPUT " << primaryString << "\n";
 
		if (primaryString == "Authorization Request Message:") {
 
			std::cout << "AUTHORIZING\n";
 
			((PurpleRequestInputCb) ok_cb)(user_data, "Please authorize me.");
 
			return NULL;
 
		}
 
	}
 
	return NULL;
 
@@ -285,35 +294,45 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
				}
 
#endif /* WITH_IMAGEMAGICK */
 

	
 
				if (!photo)
 
					return;
 
				purple_buddy_icons_set_account_icon(account, photo, size);
 
			}
 
		}
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::string &groups) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				if (m_authRequests.find(user + buddyName) != m_authRequests.end()) {
 
					m_authRequests[user + buddyName]->deny_cb(m_authRequests[user + buddyName]->user_data);
 
					m_authRequests.erase(user + buddyName);
 
				}
 
				PurpleBuddy *buddy = purple_find_buddy(account, buddyName.c_str());
 
				if (buddy) {
 
					purple_account_remove_buddy(account, buddy, purple_buddy_get_group(buddy));
 
					purple_blist_remove_buddy(buddy);
 
				}
 
			}
 
		}
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::string &groups) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 

	
 
				if (m_authRequests.find(user + buddyName) != m_authRequests.end()) {
 
					m_authRequests[user + buddyName]->authorize_cb(m_authRequests[user + buddyName]->user_data);
 
					m_authRequests.erase(user + buddyName);
 
				}
 

	
 
				PurpleBuddy *buddy = purple_find_buddy(account, buddyName.c_str());
 
				if (buddy) {
 
					purple_blist_alias_buddy(buddy, alias.c_str());
 
					purple_blist_server_alias_buddy(buddy, alias.c_str());
 
					serv_alias_buddy(buddy);
 
				}
 
				else {
 
					std::cout << "ADDING NEW BUDDY\n";
 
					PurpleBuddy *buddy = purple_buddy_new(account, buddyName.c_str(), alias.c_str());
 

	
 
					// Add newly created buddy to legacy network roster.
 
					PurpleGroup *group = purple_find_group(groups.c_str());
 
@@ -338,24 +357,25 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
		}
 

	
 
		void handleStoppedTypingRequest(const std::string &user, const std::string &buddyName) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				serv_send_typing(purple_account_get_connection(account), buddyName.c_str(), PURPLE_NOT_TYPING);
 
			}
 
		}
 

	
 
		std::map<std::string, PurpleAccount *> m_sessions;
 
		std::map<PurpleAccount *, std::string> m_accounts;
 
		std::map<std::string, unsigned int> m_vcards;
 
		std::map<std::string, authRequest *> m_authRequests;
 
	private:
 
		Config *config;
 
};
 

	
 
static std::string getAlias(PurpleBuddy *m_buddy) {
 
	std::string alias;
 
	if (purple_buddy_get_server_alias(m_buddy))
 
		alias = (std::string) purple_buddy_get_server_alias(m_buddy);
 
	else
 
		alias = (std::string) purple_buddy_get_alias(m_buddy);
 
	return alias;
 
}
 
@@ -684,28 +704,62 @@ static PurpleRequestUiOps requestUiOps =
 
	NULL,
 
	requestAction,
 
	NULL,
 
	NULL,
 
	NULL, //requestClose,
 
	NULL,
 
	NULL,
 
	NULL,
 
	NULL,
 
	NULL
 
};
 

	
 
static void * accountRequestAuth(PurpleAccount *account, const char *remote_user, const char *id, const char *alias, const char *message, gboolean on_list, PurpleAccountRequestAuthorizationCb authorize_cb, PurpleAccountRequestAuthorizationCb deny_cb, void *user_data) {
 
	authRequest *req = new authRequest;
 
	req->authorize_cb = authorize_cb;
 
	req->deny_cb = deny_cb;
 
	req->user_data = user_data;
 
	req->account = account;
 
	req->who = remote_user;
 
	req->mainJID = np->m_accounts[account];
 
	np->m_authRequests[req->mainJID + req->who] = req;
 

	
 
	np->handleAuthorization(req->mainJID, req->who);
 

	
 
	return req;
 
}
 

	
 
static void accountRequestClose(void *data){
 
	authRequest *req = (authRequest *) data;
 
	np->m_authRequests.erase(req->mainJID + req->who);
 
}
 

	
 

	
 
static PurpleAccountUiOps accountUiOps =
 
{
 
	NULL,
 
	NULL,
 
	NULL,
 
	accountRequestAuth,
 
	accountRequestClose,
 
	NULL,
 
	NULL,
 
	NULL,
 
	NULL
 
};
 

	
 
static void transport_core_ui_init(void)
 
{
 
	purple_blist_set_ui_ops(&blistUiOps);
 
// 	purple_accounts_set_ui_ops(&accountUiOps);
 
	purple_accounts_set_ui_ops(&accountUiOps);
 
	purple_notify_set_ui_ops(&notifyUiOps);
 
	purple_request_set_ui_ops(&requestUiOps);
 
// 	purple_xfers_set_ui_ops(getXferUiOps());
 
	purple_connections_set_ui_ops(&conn_ui_ops);
 
	purple_conversations_set_ui_ops(&conversation_ui_ops);
 
// #ifndef WIN32
 
// 	purple_dnsquery_set_ui_ops(getDNSUiOps());
 
// #endif
 
}
 

	
 
static PurpleCoreUiOps coreUiOps =
 
{
include/transport/networkplugin.h
Show inline comments
 
@@ -55,24 +55,26 @@ class NetworkPlugin {
 
		void handleSubject(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &nickname = "");
 

	
 
		void handleRoomChanged(const std::string &user, const std::string &room, const std::string &nickname);
 

	
 
		void handleVCard(const std::string &user, unsigned int id, const std::string &legacyName, const std::string &fullName, const std::string &nickname, const std::string &photo);
 

	
 
		void handleBuddyTyping(const std::string &user, const std::string &buddyName);
 
		
 
		void handleBuddyTyped(const std::string &user, const std::string &buddyName);
 

	
 
		void handleBuddyStoppedTyping(const std::string &user, const std::string &buddyName);
 

	
 
		void handleAuthorization(const std::string &user, const std::string &buddyName);
 

	
 
		virtual void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) = 0;
 
		virtual void handleLogoutRequest(const std::string &user, const std::string &legacyName) = 0;
 
		virtual void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message) = 0;
 
		virtual void handleVCardRequest(const std::string &/*user*/, const std::string &/*legacyName*/, unsigned int /*id*/) {}
 
		virtual void handleVCardUpdatedRequest(const std::string &/*user*/, const std::string &/*photo*/) {}
 
		virtual void handleJoinRoomRequest(const std::string &/*user*/, const std::string &/*room*/, const std::string &/*nickname*/, const std::string &/*pasword*/) {}
 
		virtual void handleLeaveRoomRequest(const std::string &/*user*/, const std::string &/*room*/) {}
 
		virtual void handleStatusChangeRequest(const std::string &/*user*/, int status, const std::string &statusMessage) {}
 
		virtual void handleBuddyUpdatedRequest(const std::string &/*user*/, const std::string &/*buddyName*/, const std::string &/*alias*/, const std::string &/*groups*/) {}
 
		virtual void handleBuddyRemovedRequest(const std::string &/*user*/, const std::string &/*buddyName*/, const std::string &/*groups*/) {}
 

	
 
		virtual void handleTypingRequest(const std::string &/*user*/, const std::string &/*buddyName*/) {}
include/transport/networkpluginserver.h
Show inline comments
 
@@ -59,24 +59,25 @@ class NetworkPluginServer {
 
		void handleNewClientConnection(boost::shared_ptr<Swift::Connection> c);
 
		void handleSessionFinished(Client *c);
 
		void handleDataRead(Client *c, const Swift::SafeByteArray&);
 

	
 
		void handleConnectedPayload(const std::string &payload);
 
		void handleDisconnectedPayload(const std::string &payload);
 
		void handleBuddyChangedPayload(const std::string &payload);
 
		void handleConvMessagePayload(const std::string &payload, bool subject = false);
 
		void handleParticipantChangedPayload(const std::string &payload);
 
		void handleRoomChangedPayload(const std::string &payload);
 
		void handleVCardPayload(const std::string &payload);
 
		void handleChatStatePayload(const std::string &payload, Swift::ChatState::ChatStateType type);
 
		void handleAuthorizationPayload(const std::string &payload);
 

	
 
		void handleUserCreated(User *user);
 
		void handleRoomJoined(User *user, const std::string &room, const std::string &nickname, const std::string &password);
 
		void handleRoomLeft(User *user, const std::string &room);
 
		void handleUserReadyToConnect(User *user);
 
		void handleUserPresenceChanged(User *user, Swift::Presence::ref presence);
 
		void handleUserDestroyed(User *user);
 

	
 
		void handleBuddyUpdated(Buddy *buddy, const Swift::RosterItemPayload &item);
 
		void handleBuddyRemoved(Buddy *buddy);
 
		void handleBuddyAdded(Buddy *buddy, const Swift::RosterItemPayload &item);
 

	
include/transport/rostermanager.h
Show inline comments
 
@@ -67,24 +67,28 @@ class RosterManager {
 
		/// Returns user associated with this roster.
 
		/// \return User
 
		User *getUser() { return m_user; }
 

	
 
		/// Called when new Buddy is added to this roster.
 
		/// \param buddy newly added Buddy
 
		boost::signal<void (Buddy *buddy)> onBuddySet;
 

	
 
		/// Called when Buddy has been removed from this roster.
 
		/// \param buddy removed Buddy
 
		boost::signal<void (Buddy *buddy)> onBuddyUnset;
 

	
 
		boost::signal<void (Buddy *buddy)> onBuddyAdded;
 
		
 
		boost::signal<void (Buddy *buddy)> onBuddyRemoved;
 

	
 
		void handleSubscription(Swift::Presence::ref presence);
 

	
 
	private:
 
		void setBuddyCallback(Buddy *buddy);
 

	
 
		void sendBuddyRosterPush(Buddy *buddy);
 
		void sendRIE();
 
		void handleBuddyRosterPushResponse(Swift::ErrorPayload::ref error, const std::string &key);
 

	
 

	
 
		std::map<std::string, Buddy *> m_buddies;
 
		Component *m_component;
spectrum/src/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 
 
ADD_EXECUTABLE(spectrum2 ${SRC})
 
 
ADD_DEPENDENCIES(spectrum2 libpurple_backend)
 
ADD_DEPENDENCIES(spectrum2 libircclient-qt_backend)
 
ADD_DEPENDENCIES(spectrum2 spectrum_libpurple_backend)
 
ADD_DEPENDENCIES(spectrum2 spectrum_libircclient-qt_backend)
 
 
target_link_libraries(spectrum2 transport)
 
 
INSTALL(TARGETS spectrum2 RUNTIME DESTINATION bin)
 
 
INSTALL(FILES
 
	sample2.cfg
 
	RENAME spectrum.cfg.example
 
	DESTINATION /etc/spectrum2
 
	)
spectrum/src/sample.cfg
Show inline comments
 
[service]
 
jid = localhost
 
password = secret
 
server = 127.0.0.1
 
port = 5222
 
server_mode = 1
 
backend_host=localhost # < this option doesn't work yet
 
backend_port=10001
 
#cert= #patch to PKCS#12 certificate
 
#cert_password= #password to that certificate if any
 
users_per_backend=2
 
backend=../../backends/libpurple/libpurple_backend
 
#backend=../../backends/libircclient-qt/libircclient-qt_backend
 
backend=../../backends/libpurple/spectrum_libpurple_backend
 
#backend=../../backends/libircclient-qt/spectrum_libircclient-qt_backend
 
protocol=prpl-jabber
 
#protocol=prpl-msn
 
#protocol=prpl-icq
 

	
 
[database]
 
type = sqlite3 # or "none" without database backend
 
database = test.sql
 
prefix=icq
src/buddy.cpp
Show inline comments
 
@@ -160,17 +160,21 @@ void Buddy::handleVCardReceived(const std::string &id, Swift::VCard::ref vcard)
 
}
 

	
 
std::string Buddy::JIDToLegacyName(const Swift::JID &jid) {
 
	std::string name;
 
	if (jid.getUnescapedNode() == jid.getNode()) {
 
		name = jid.getNode();
 
		if (name.find_last_of("%") != std::string::npos) {
 
			name.replace(name.find_last_of("%"), 1, "@");
 
		}
 
	}
 
	else {
 
		name = jid.getUnescapedNode();
 
		// Psi sucks...
 
		if (name.find_last_of("\\40") != std::string::npos) {
 
			name.replace(name.find_last_of("\\40"), 1, "@");
 
		}
 
	}
 
	return name;
 
}
 

	
 
}
src/networkplugin.cpp
Show inline comments
 
@@ -153,24 +153,37 @@ void NetworkPlugin::handleBuddyStoppedTyping(const std::string &user, const std:
 
	pbnetwork::Buddy buddy;
 
	buddy.set_username(user);
 
	buddy.set_buddyname(buddyName);
 

	
 
	std::string message;
 
	buddy.SerializeToString(&message);
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BUDDY_STOPPED_TYPING);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleAuthorization(const std::string &user, const std::string &buddyName) {
 
	pbnetwork::Buddy buddy;
 
	buddy.set_username(user);
 
	buddy.set_buddyname(buddyName);
 

	
 
	std::string message;
 
	buddy.SerializeToString(&message);
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_AUTH_REQUEST);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleConnected(const std::string &user) {
 
	std::cout << "LOGIN SENT\n";
 
	pbnetwork::Connected d;
 
	d.set_user(user);
 

	
 
	std::string message;
 
	d.SerializeToString(&message);
 

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_CONNECTED);
 

	
 
	send(message);	
 
}
src/networkpluginserver.cpp
Show inline comments
 
@@ -215,46 +215,71 @@ void NetworkPluginServer::handleVCardPayload(const std::string &data) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	boost::shared_ptr<Swift::VCard> vcard(new Swift::VCard());
 
	vcard->setFullName(payload.fullname());
 
	vcard->setPhoto(Swift::createByteArray(payload.photo()));
 
	vcard->setNickname(payload.nickname());
 

	
 
	m_vcardResponder->sendVCard(payload.id(), vcard);
 
}
 

	
 
void NetworkPluginServer::handleAuthorizationPayload(const std::string &data) {
 
	pbnetwork::Buddy payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	User *user = m_userManager->getUser(payload.username());
 
	if (!user)
 
		return;
 

	
 
	Swift::Presence::ref response = Swift::Presence::create();
 
	response->setTo(user->getJID());
 
	std::string name = payload.buddyname();
 

	
 
// 	name = Swift::JID::getEscapedNode(name)
 

	
 
	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);
 
	m_component->getStanzaChannel()->sendPresence(response);
 
}
 

	
 
void NetworkPluginServer::handleChatStatePayload(const std::string &data, Swift::ChatState::ChatStateType type) {
 
	pbnetwork::Buddy payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	User *user = m_userManager->getUser(payload.username());
 
	if (!user)
 
		return;
 

	
 
	NetworkConversation *conv = (NetworkConversation *) user->getConversationManager()->getConversation(payload.buddyname());
 
	if (!conv) {
 
		std::cout << "handling chatstate: NO conv with buddyname=" << payload.buddyname() << "\n";
 
		return;
 
	}
 

	
 
	boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
	msg->addPayload(boost::make_shared<Swift::ChatState>(type));
 

	
 
	conv->handleMessage(msg);
 
	
 
}
 

	
 
void NetworkPluginServer::handleBuddyChangedPayload(const std::string &data) {
 
	pbnetwork::Buddy payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	User *user = m_userManager->getUser(payload.username());
 
	if (!user)
 
		return;
 
@@ -400,24 +425,27 @@ void NetworkPluginServer::handleDataRead(Client *c, const Swift::SafeByteArray &
 
			case pbnetwork::WrapperMessage_Type_TYPE_VCARD:
 
				handleVCardPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPING:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Composing);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPED:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Paused);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_STOPPED_TYPING:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Active);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_AUTH_REQUEST:
 
				handleAuthorizationPayload(wrapper.payload());
 
				break;
 
			default:
 
				return;
 
		}
 
	}
 
}
 

	
 
void NetworkPluginServer::send(boost::shared_ptr<Swift::Connection> &c, const std::string &data) {
 
	char header[4];
 
	*((int*)(header)) = htonl(data.size());
 
	c->write(Swift::createSafeByteArray(std::string(header, 4) + data));
 
}
 

	
src/pbnetwork.proto
Show inline comments
 
@@ -83,17 +83,18 @@ message WrapperMessage {
 
		TYPE_PING					= 9;
 
		TYPE_PONG					= 10;
 
		TYPE_JOIN_ROOM				= 11;
 
		TYPE_LEAVE_ROOM				= 12;
 
		TYPE_PARTICIPANT_CHANGED	= 13;
 
		TYPE_ROOM_NICKNAME_CHANGED	= 14;
 
		TYPE_ROOM_SUBJECT_CHANGED	= 15;
 
		TYPE_VCARD					= 16;
 
		TYPE_STATUS_CHANGED			= 17;
 
		TYPE_BUDDY_TYPING			= 18;
 
		TYPE_BUDDY_STOPPED_TYPING	= 19;
 
		TYPE_BUDDY_TYPED			= 20;
 
		TYPE_AUTH_REQUEST			= 21;
 
	}
 
	required Type type = 1;
 
	optional bytes payload = 2;
 
}
 
;
 
\ No newline at end of file
src/rostermanager.cpp
Show inline comments
 
@@ -30,27 +30,24 @@
 
#include "Swiften/Elements/RosterItemPayload.h"
 
#include "Swiften/Elements/RosterItemExchangePayload.h"
 

	
 
namespace Transport {
 

	
 
RosterManager::RosterManager(User *user, Component *component){
 
	m_rosterStorage = NULL;
 
	m_user = user;
 
	m_component = component;
 
	m_setBuddyTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(1000);
 
	m_RIETimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(5000);
 
	m_RIETimer->onTick.connect(boost::bind(&RosterManager::sendRIE, this));
 

	
 
	
 

	
 
}
 

	
 
RosterManager::~RosterManager() {
 
	m_setBuddyTimer->stop();
 
	m_RIETimer->stop();
 
	if (m_rosterStorage) {
 
// 		for (std::map<std::string, Buddy *>::const_iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
 
// 			Buddy *buddy = (*it).second;
 
// 			m_rosterStorage->storeBuddy(buddy);
 
// 		}
 
		m_rosterStorage->storeBuddies();
 
	}
 
@@ -148,25 +145,149 @@ void RosterManager::sendRIE() {
 
		item.setAction(Swift::RosterItemExchangePayload::Item::Add);
 
		item.setGroups(buddy->getGroups());
 

	
 
		payload->addItem(item);
 
	}
 

	
 
	boost::shared_ptr<Swift::GenericRequest<Swift::RosterItemExchangePayload> > request(new Swift::GenericRequest<Swift::RosterItemExchangePayload>(Swift::IQ::Set, m_user->getJID(), payload, m_component->getIQRouter()));
 
	request->send();
 
}
 

	
 
void RosterManager::handleSubscription(Swift::Presence::ref presence) {
 
	std::string legacyName = Buddy::JIDToLegacyName(presence->getTo());
 
	
 
	// For server mode the subscription changes are handler in rosterresponder.cpp
 
	// using roster pushes.
 
	if (m_component->inServerMode()) {
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setTo(presence->getFrom());
 
		response->setFrom(presence->getTo());
 
		Buddy *buddy = getBuddy(Buddy::JIDToLegacyName(presence->getTo()));
 
		if (buddy) {
 
			switch (presence->getType()) {
 
				case Swift::Presence::Subscribe:
 
					response->setType(Swift::Presence::Subscribed);
 
					break;
 
				case Swift::Presence::Unsubscribe:
 
					response->setType(Swift::Presence::Unsubscribed);
 
					break;
 
				default:
 
					return;
 
			}
 
			m_component->getStanzaChannel()->sendPresence(response);
 
			
 
		}
 
		else {
 
			BuddyInfo buddyInfo;
 
			switch (presence->getType()) {
 
				// buddy is not in roster, so add him
 
				case Swift::Presence::Subscribe:
 
					buddyInfo.id = -1;
 
					buddyInfo.alias = "";
 
					buddyInfo.legacyName = Buddy::JIDToLegacyName(presence->getTo());
 
					buddyInfo.subscription = "both";
 
					buddyInfo.flags = 0;
 

	
 
					buddy = m_component->getFactory()->createBuddy(this, buddyInfo);
 
					setBuddy(buddy);
 
					onBuddyAdded(buddy);
 
					response->setType(Swift::Presence::Subscribed);
 
					break;
 
				// buddy is already there, so nothing to do, just answer
 
				case Swift::Presence::Unsubscribe:
 
					response->setType(Swift::Presence::Unsubscribed);
 
					break;
 
				default:
 
					return;
 
			}
 
			m_component->getStanzaChannel()->sendPresence(response);
 
		}
 
	}
 
	else {
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setTo(presence->getFrom());
 
		response->setFrom(presence->getTo());
 

	
 
		Buddy *buddy = getBuddy(Buddy::JIDToLegacyName(presence->getTo()));
 
		if (buddy) {
 
			switch (presence->getType()) {
 
				// buddy is already there, so nothing to do, just answer
 
				case Swift::Presence::Subscribe:
 
					response->setType(Swift::Presence::Subscribed);
 
					break;
 
				// remove buddy
 
				case Swift::Presence::Unsubscribe:
 
					response->setType(Swift::Presence::Unsubscribed);
 
					onBuddyRemoved(buddy);
 
					break;
 
				// just send response
 
				case Swift::Presence::Unsubscribed:
 
					response->setType(Swift::Presence::Unsubscribe);
 
					break;
 
				// just send response
 
				case Swift::Presence::Subscribed:
 
					response->setType(Swift::Presence::Subscribe);
 
					break;
 
				default:
 
					return;
 
			}
 
		}
 
		else {
 
			BuddyInfo buddyInfo;
 
			switch (presence->getType()) {
 
				// buddy is not in roster, so add him
 
				case Swift::Presence::Subscribe:
 
					buddyInfo.id = -1;
 
					buddyInfo.alias = "";
 
					buddyInfo.legacyName = Buddy::JIDToLegacyName(presence->getTo());
 
					buddyInfo.subscription = "both";
 
					buddyInfo.flags = 0;
 

	
 
					buddy = m_component->getFactory()->createBuddy(this, buddyInfo);
 
					setBuddy(buddy);
 
					onBuddyAdded(buddy);
 
					response->setType(Swift::Presence::Subscribed);
 
					break;
 
				// buddy is already there, so nothing to do, just answer
 
				case Swift::Presence::Unsubscribe:
 
					response->setType(Swift::Presence::Unsubscribed);
 
					break;
 
				// just send response
 
				case Swift::Presence::Unsubscribed:
 
					response->setType(Swift::Presence::Unsubscribe);
 
					break;
 
				// just send response
 
				case Swift::Presence::Subscribed:
 
					response->setType(Swift::Presence::Subscribe);
 
					break;
 
				default:
 
					return;
 
			}
 
		}
 

	
 
		m_component->getStanzaChannel()->sendPresence(response);
 

	
 
		// We have to act as buddy and send its subscribe/unsubscribe just to be sure...
 
		switch (response->getType()) {
 
			case Swift::Presence::Unsubscribed:
 
				response->setType(Swift::Presence::Unsubscribe);
 
				m_component->getStanzaChannel()->sendPresence(response);
 
				break;
 
			case Swift::Presence::Subscribed:
 
				response->setType(Swift::Presence::Subscribe);
 
				m_component->getStanzaChannel()->sendPresence(response);
 
				break;
 
			default:
 
				break;
 
		}
 
	}
 
}
 

	
 
void RosterManager::setStorageBackend(StorageBackend *storageBackend) {
 
	if (m_rosterStorage || !storageBackend) {
 
		return;
 
	}
 
	m_rosterStorage = new RosterStorage(m_user, storageBackend);
 

	
 
	std::list<BuddyInfo> roster;
 
	storageBackend->getBuddies(m_user->getUserInfo().id, roster);
 

	
 
	for (std::list<BuddyInfo>::const_iterator it = roster.begin(); it != roster.end(); it++) {
0 comments (0 inline, 0 general)