Changeset - c8006a0ebab6
[Not reviewed]
0 11 1
HanzZ - 15 years ago 2011-04-02 21:51:51
hanzz.k@gmail.com
Working message forwarding in both sides :)
12 files changed with 102 insertions and 6 deletions:
0 comments (0 inline, 0 general)
examples/usermanager/main.cpp
Show inline comments
 
@@ -14,13 +14,13 @@ int main(void)
 
	if (!config.load("sample.cfg")) {
 
		std::cout << "Can't open sample.cfg configuration file.\n";
 
		return 1;
 
	}
 
 
	Swift::SimpleEventLoop eventLoop;
 
	Component transport(&eventLoop, &config);
 
	Component transport(&eventLoop, &config, NULL);
 
	Logger logger(&transport);
 
 
	SQLite3Backend sql(&config);
 
	logger.setStorageBackend(&sql);
 
	if (!sql.connect()) {
 
		std::cout << "Can't connect to database.\n";
include/transport/conversationmanager.h
Show inline comments
 
@@ -49,13 +49,16 @@ class ConversationManager {
 

	
 
		void setConversation(AbstractConversation *conv);
 

	
 
		void unsetConversation(AbstractConversation *conv);
 

	
 
	private:
 
		void handleMessageReceived(Swift::Message::ref message);
 

	
 
		Component *m_component;
 
		User *m_user;
 

	
 
		std::map<std::string, AbstractConversation *> m_convs;
 
		friend class UserManager;
 
};
 

	
 
}
include/transport/factory.h
Show inline comments
 
new file 100644
 
/**
 
 * XMPP - libpurple transport
 
 *
 
 * Copyright (C) 2009, Jan Kaluza <hanzz@soc.pidgin.im>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * 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
 
 */
 

	
 
#pragma once
 

	
 
#include <string>
 
#include <algorithm>
 
#include "transport/transport.h"
 

	
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Elements/Message.h"
 
#include "transport/abstractconversation.h"
 

	
 
namespace Transport {
 

	
 
class AbstractConversation;
 
class ConversationManager;
 

	
 
class Factory {
 
	public:
 
		
 
		virtual AbstractConversation *createConversation(ConversationManager *conversationManager, const std::string &legacyName) = 0;
 
};
 

	
 
}
include/transport/transport.h
Show inline comments
 
@@ -30,12 +30,13 @@
 
#include "Swiften/Presence/PresenceOracle.h"
 
#include "Swiften/Network/BoostTimerFactory.h"
 
#include "Swiften/Network/BoostIOServiceThread.h"
 
#include "Swiften/Server/UserRegistry.h"
 
#include <boost/bind.hpp>
 
#include "transport/config.h"
 
#include "transport/factory.h"
 

	
 
#define tr(lang,STRING)    (STRING)
 
#define _(STRING)    (STRING)
 

	
 
namespace Transport {
 
	// typedef enum { 	CLIENT_FEATURE_ROSTERX = 2,
 
@@ -45,12 +46,13 @@ namespace Transport {
 
	// 				} SpectrumImportantFeatures;
 
	// 
 
	class StorageBackend;
 
	class DiscoInfoResponder;
 
	class DiscoItemsResponder;
 
	class RosterResponder;
 
	class Factory;
 

	
 
	/// Represents one transport instance.
 

	
 
	/// It's used to connect
 
	/// the Jabber server and provides transaction layer between Jabber server
 
	/// and other classes.
 
@@ -60,13 +62,13 @@ namespace Transport {
 
			/// \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(Swift::EventLoop *loop, Config *config, Factory *factory);
 

	
 
			/// Component destructor.
 
			~Component();
 

	
 
			/// Returns Swift::Component associated with this Transport::Component.
 
			/// You can use it to send presences and other stanzas.
 
@@ -100,12 +102,14 @@ namespace Transport {
 
			/// Returns Jabber ID of this transport.
 
			/// \return Jabber ID of this transport
 
			Swift::JID &getJID() { return m_jid; }
 

	
 
			Swift::BoostNetworkFactories *getFactories() { return m_factories; }
 

	
 
			Factory *getFactory() { return m_factory; }
 

	
 
			/// This signal is emitted when server disconnects the transport because of some error.
 
			/// \param error disconnection error
 
			boost::signal<void (const Swift::ComponentError &error)> onConnectionError;
 

	
 
			/// This signal is emitted when transport successfully connects the server.
 
			boost::signal<void ()> onConnected;
 
@@ -157,11 +161,12 @@ namespace Transport {
 
			DiscoItemsResponder *m_discoItemsResponder;
 
			RosterResponder *m_rosterResponder;
 
			int m_reconnectCount;
 
			Config* m_config;
 
			std::string m_protocol;
 
			Swift::JID m_jid;
 
			Factory *m_factory;
 

	
 
		friend class User;
 
		friend class UserRegistration;
 
	};
 
}
include/transport/usermanager.h
Show inline comments
 
@@ -65,12 +65,13 @@ class UserManager {
 
		/// 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 handleMessageReceived(Swift::Message::ref message);
 
// 		void handleDiscoInfoResponse(boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error, const Swift::JID& jid);
 
		void addUser(User *user);
 

	
 
		long m_onlineBuddies;
 
		User *m_cachedUser;
 
		std::map<std::string, User *> m_users;
spectrum/src/main.cpp
Show inline comments
 
@@ -8,12 +8,13 @@
 
#include "transport/logger.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/userregistration.h"
 
#include "transport/user.h"
 
#include "transport/storagebackend.h"
 
#include "transport/rostermanager.h"
 
#include "transport/abstractconversation.h"
 
#include "spectrumeventloop.h"
 
#include "spectrumbuddy.h"
 
#include "spectrumconversation.h"
 
#include "geventloop.h"
 

	
 
#define Log(X, STRING) std::cout << "[SPECTRUM] " << X << " " << STRING << "\n";
 
@@ -161,12 +162,15 @@ static void conv_destroy(PurpleConversation *conv) {
 
	if (s_conv) {
 
		delete s_conv;
 
	}
 
}
 

	
 
static void conv_write_im(PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime) {
 
	// Don't forwards our own messages.
 
	if (flags & PURPLE_MESSAGE_SEND || flags & PURPLE_MESSAGE_SYSTEM)
 
		return;
 
	SpectrumConversation *s_conv = (SpectrumConversation *) conv->ui_data;
 
	if (!s_conv)
 
		return;
 

	
 
	boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
	msg->setBody(message);
 
@@ -420,12 +424,20 @@ static void handleUserDestroyed(User *user, UserManager *userManager, Config *co
 

	
 
		account->ui_data = NULL;
 
// 		Transport::instance()->collector()->collect(m_account);
 
	}
 
}
 

	
 
class SpectrumFactory : public Factory {
 
	public:
 
		AbstractConversation *createConversation(ConversationManager *conversationManager, const std::string &legacyName) {
 
			PurpleConversation *conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, (PurpleAccount *) conversationManager->getUser()->getData() , legacyName.c_str());
 
			return (AbstractConversation *) conv->ui_data;
 
		}
 
};
 

	
 
int main(int argc, char **argv) {
 
	GError *error = NULL;
 
	GOptionContext *context;
 
	context = g_option_context_new("config_file_name or profile name");
 
	g_option_context_add_main_entries(context, options_entries, "");
 
	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 
@@ -491,13 +503,14 @@ int main(int argc, char **argv) {
 
			return 1;
 
		}
 

	
 
		initPurple(config);
 

	
 
		SpectrumEventLoop eventLoop;
 
		Component transport(&eventLoop, &config);
 
		SpectrumFactory factory;
 
		Component transport(&eventLoop, &config, &factory);
 
		Logger logger(&transport);
 
		_logger = &logger;
 

	
 
		SQLite3Backend sql(&config);
 
		logger.setStorageBackend(&sql);
 
		if (!sql.connect()) {
spectrum/src/spectrumconversation.cpp
Show inline comments
 
@@ -27,10 +27,15 @@ SpectrumConversation::SpectrumConversation(ConversationManager *conversationMana
 
}
 

	
 
SpectrumConversation::~SpectrumConversation() {
 
}
 

	
 
void SpectrumConversation::sendMessage(boost::shared_ptr<Swift::Message> &message) {
 
	
 
	// escape and send
 
	gchar *_markup = purple_markup_escape_text(message->getBody().c_str(), -1);
 
	if (purple_conversation_get_type(m_conv) == PURPLE_CONV_TYPE_IM) {
 
		purple_conv_im_send(PURPLE_CONV_IM(m_conv), _markup);
 
	}
 
	g_free(_markup);
 
}
 

	
 

	
spectrum/src/spectrumconversation.h
Show inline comments
 
@@ -24,12 +24,13 @@
 
#include "purple.h"
 
#include "account.h"
 
#include "glib.h"
 
#include <algorithm>
 
#include "transport/abstractconversation.h"
 
#include "transport/conversationmanager.h"
 
#include "transport/user.h"
 

	
 
using namespace Transport;
 

	
 
// Wrapper for PurpleBuddy
 
class SpectrumConversation : public AbstractConversation {
 
	public:
src/abstractconversation.cpp
Show inline comments
 
@@ -26,14 +26,14 @@
 
#include "transport/abstractbuddy.h"
 
#include "transport/rostermanager.h"
 

	
 
namespace Transport {
 

	
 
AbstractConversation::AbstractConversation(ConversationManager *conversationManager, const std::string &legacyName) : m_conversationManager(conversationManager) {
 
	m_conversationManager->setConversation(this);
 
	m_legacyName = legacyName;
 
	m_conversationManager->setConversation(this);
 
}
 

	
 
AbstractConversation::~AbstractConversation() {
 
	m_conversationManager->unsetConversation(this);
 
}
 

	
src/conversationmanager.cpp
Show inline comments
 
@@ -19,12 +19,13 @@
 
 */
 

	
 
#include "transport/conversationmanager.h"
 
#include "transport/abstractconversation.h"
 
#include "transport/usermanager.h"
 
#include "transport/abstractbuddy.h"
 
#include "transport/factory.h"
 
#include "transport/user.h"
 
#include "Swiften/Roster/SetRosterRequest.h"
 
#include "Swiften/Elements/RosterPayload.h"
 
#include "Swiften/Elements/RosterItemPayload.h"
 

	
 
namespace Transport {
 
@@ -42,7 +43,19 @@ void ConversationManager::setConversation(AbstractConversation *conv) {
 
}
 

	
 
void ConversationManager::unsetConversation(AbstractConversation *conv) {
 
	m_convs.erase(conv->getLegacyName());
 
}
 

	
 
void ConversationManager::handleMessageReceived(Swift::Message::ref message) {
 
	std::string name = message->getTo().getUnescapedNode();
 
	if (name.find_last_of("%") != std::string::npos) {
 
		name.replace(name.find_last_of("%"), 1, "@");
 
	}
 

	
 
	if (!m_convs[name]) {
 
		m_convs[name] = m_component->getFactory()->createConversation(this, name);
 
	}
 
	m_convs[name]->sendMessage(message);
 
}
 

	
 
}
 
\ No newline at end of file
src/transport.cpp
Show inline comments
 
@@ -18,12 +18,13 @@
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "transport/transport.h"
 
#include <boost/bind.hpp>
 
#include "transport/storagebackend.h"
 
#include "transport/factory.h"
 
#include "discoinforesponder.h"
 
#include "discoitemsresponder.h"
 
#include "rosterresponder.h"
 

	
 
using namespace Swift;
 
using namespace boost;
 
@@ -39,18 +40,19 @@ class MyUserRegistry : public Swift::UserRegistry {
 
			users[user.toBare().toString()] = password;
 
			return true;
 
		}
 
		mutable std::map<std::string, std::string> users;
 
};
 

	
 
Component::Component(Swift::EventLoop *loop, Config *config) {
 
Component::Component(Swift::EventLoop *loop, Config *config, Factory *factory) {
 
	m_component = NULL;
 
	m_userRegistry = NULL;
 
	m_server = NULL;
 
	m_reconnectCount = 0;
 
	m_config = config;
 
	m_factory = factory;
 

	
 
	m_jid = Swift::JID(CONFIG_STRING(m_config, "service.jid"));
 

	
 
	m_factories = new BoostNetworkFactories(loop);
 

	
 
	m_reconnectTimer = m_factories->getTimerFactory()->createTimer(1000);
src/usermanager.cpp
Show inline comments
 
@@ -19,22 +19,24 @@
 
 */
 

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

	
 
namespace Transport {
 

	
 
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));
 
	m_component->getStanzaChannel()->onMessageReceived.connect(bind(&UserManager::handleMessageReceived, this, _1));
 
// 	component->onDiscoInfoResponse.connect(bind(&UserManager::handleDiscoInfoResponse, this, _1, _2, _3));
 
}
 

	
 
UserManager::~UserManager(){
 
}
 

	
 
@@ -141,7 +143,16 @@ void UserManager::handlePresence(Swift::Presence::ref presence) {
 
// 		else if (user && Transport::instance()->protocol()->tempAccountsAllowed() && !((User *) user)->hasOpenedMUC()) {
 
// 			Transport::instance()->userManager()->removeUser(user);
 
// 		}
 
	}
 
}
 

	
 
void UserManager::handleMessageReceived(Swift::Message::ref message) {
 
	User *user = getUser(message->getFrom().toBare().toString());
 
	if (!user ){
 
		return;
 
	}
 

	
 
	user->getConversationManager()->handleMessageReceived(message);
 
}
 

	
 
}
0 comments (0 inline, 0 general)