Changeset - cbcf2f145298
CMakeLists.txt
Show inline comments
 
@@ -105,70 +105,71 @@ if (PROTOBUF_FOUND)
 
	message("Frotz plugin      : yes")
 

	
 
else()
 
	message("Network plugins   : no (install libprotobuf-dev)")
 
	message("Libpurple plugin  : no (install libpurple and libprotobuf-dev)")
 
	message("IRC plugin        : no (install libircclient-qt and libprotobuf-dev)")
 
	message("Frotz plugin      : no (install libprotobuf-dev)")
 
endif()
 

	
 
if (LOG4CXX_FOUND)
 
	message("Logging           : yes")
 
	include_directories(${LOG4CXX_INCLUDE_DIR})
 
else()
 
	message(FATAL_ERROR "Logging           : no (install log4cxx-devel)")
 
endif()
 

	
 
if(CMAKE_BUILD_TYPE MATCHES Debug)
 
	ADD_DEFINITIONS(-O3)
 
	ADD_DEFINITIONS(-ggdb)
 
	ADD_DEFINITIONS(-DDEBUG)
 
	ADD_DEFINITIONS(-Wall)
 
	ADD_DEFINITIONS(-W)
 
	ADD_DEFINITIONS(-Wcast-align)
 
	ADD_DEFINITIONS(-Wextra -Wno-sign-compare -Wno-unused-parameter)
 
	ADD_DEFINITIONS(-Winit-self)
 
	ADD_DEFINITIONS(-Wmissing-declarations)
 
	ADD_DEFINITIONS(-Wpointer-arith)
 
	ADD_DEFINITIONS(-Wreorder)
 
	ADD_DEFINITIONS(-Woverloaded-virtual)
 
	ADD_DEFINITIONS(-Wsign-promo)
 
	ADD_DEFINITIONS(-Wundef -Wunused)
 
	message("Debug             : yes")
 
else(CMAKE_BUILD_TYPE MATCHES Debug)
 
	message("Debug             : no (run \"cmake . -DCMAKE_BUILD_TYPE=Debug\")")
 
endif(CMAKE_BUILD_TYPE MATCHES Debug)
 

	
 

	
 
SET(TRANSPORT_VERSION 2.0)
 
SET(PROJECT_VERSION 2.0)
 
include_directories(include)
 

	
 

	
 
include_directories(${EVENT_INCLUDE_DIRS})
 
include_directories(${SWIFTEN_INCLUDE_DIR})
 
include_directories(${Boost_INCLUDE_DIRS})
 

	
 

	
 
ADD_SUBDIRECTORY(src)
 
ADD_SUBDIRECTORY(plugin)
 
ADD_SUBDIRECTORY(include)
 
#ADD_SUBDIRECTORY(examples)
 
ADD_SUBDIRECTORY(spectrum)
 
ADD_SUBDIRECTORY(backends)
 
#ADD_SUBDIRECTORY(tests)
 
ADD_SUBDIRECTORY(spectrum_manager)
 

	
 
if (CPPUNIT_FOUND)
 
	message("tests             : yes")
 
	include_directories(${CPPUNIT_INCLUDE_DIR})
 
else()
 
	message("tests             : no (install CPPUnit)")
 
endif()
 

	
 
if(DOXYGEN_FOUND)
 
	message("Docs              : yes")
 
	ADD_SUBDIRECTORY(docs)
 
else(DOXYGEN_FOUND)
 
	message("Docs              : no (install doxygen)")
 
endif(DOXYGEN_FOUND)
 

	
 
message("----------------------")
backends/frotz/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
 
ADD_SUBDIRECTORY(dfrotz)
 
 
FILE(GLOB SRC *.c *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_frotz_backend ${SRC})
 
 
target_link_libraries(spectrum2_frotz_backend transport pthread)
 
target_link_libraries(spectrum2_frotz_backend transport pthread transport-plugin)
 
 
INSTALL(TARGETS spectrum2_frotz_backend RUNTIME DESTINATION bin)
 
backends/frotz/main.cpp
Show inline comments
 
/*
 
 * Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
 
 *
 
 * This example is free, and not covered by LGPL license. There is no
 
 * restriction applied to their modification, redistribution, using and so on.
 
 * You can study them, modify them, use them in your own program - either
 
 * completely or partially. By using it you may give me some credits in your
 
 * program, but you don't have to.
 
 */
 

	
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "Swiften/Swiften.h"
 
#include <boost/filesystem.hpp>
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
Swift::SimpleEventLoop *loop_;
 

	
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
class FrotzNetworkPlugin;
 
FrotzNetworkPlugin * np = NULL;
 

	
 
#define	PARENT_READ	p.readpipe[0]
 
#define	CHILD_WRITE	p.readpipe[1]
 
#define CHILD_READ	p.writepipe[0]
 
#define PARENT_WRITE	p.writepipe[1]
 

	
 
typedef struct dfrotz_ {
 
	pid_t pid;
 
	std::string game;
 
	int readpipe[2];
 
	int writepipe[2];
 
} dfrotz;
 

	
 
using namespace boost::filesystem;
 

	
 
static const char *howtoplay = "To move around, just type the direction you want to go.  Directions can be\n"
 
"abbreviated:  NORTH to N, SOUTH to S, EAST to E, WEST to W, NORTHEAST to\n"
 
"NE, NORTHWEST to NW, SOUTHEAST to SE, SOUTHWEST to SW, UP to U, and DOWN\n"
 
"to D.  IN and OUT will also work in certain places.\n"
 
"\n"
 
"There are many differnet kinds of sentences used in Interactive Fiction.\n"
 
"Here are some examples:\n"
 
"\n"
 
"> WALK TO THE NORTH\n"
 
"> WEST\n"
 
"> NE\n"
 
"> DOWN\n"
 
"> TAKE THE BIRDCAGE\n"
 
"> READ ABOUT DIMWIT FLATHEAD\n"
 
"> LOOK UP MEGABOZ IN THE ENCYCLOPEDIA\n"
 
"> LIE DOWN IN THE PINK SOFA\n"
 
"> EXAMINE THE SHINY COIN\n"
 
"> PUT THE RUSTY KEY IN THE CARDBOARD BOX\n"
 
"> SHOW MY BOW TIE TO THE BOUNCER\n"
 
"> HIT THE CRAWLING CRAB WITH THE GIANT NUTCRACKER\n"
 
"> ASK THE COWARDLY KING ABOUT THE CROWN JEWELS\n"
 
@@ -100,104 +101,103 @@ static const char *howtoplay = "To move around, just type the direction you want
 
"> WHERE IS EVERYBODY?\n"
 
"\n"
 
"When you meet intelligent creatures, you can talk to them by typing their\n"
 
"name, then a comma, then whatever you want to say to them.  Here are some\n"
 
"examples:\n"
 
"\n"
 
"> JESTER, HELLO\n"
 
"> GUSTAR WOOMAX, TELL ME ABOUT THE COCONUT\n"
 
"> UNCLE OTTO, GIVE ME YOUR WALLET\n"
 
"> HORSE, WHERE IS YOUR SADDLE?\n"
 
"> BOY, RUN HOME THEN CALL THE POLICE\n"
 
"> MIGHTY WIZARD, TAKE THIS POISONED APPLE.  EAT IT\n"
 
"\n"
 
"Notice that in the last two examples, you are giving the characters more\n"
 
"than one command on the same input line.  Keep in mind, however, that many\n"
 
"creatures don't care for idle chatter; your actions will speak louder than\n"
 
"your words.  \n";
 

	
 

	
 
static void start_dfrotz(dfrotz &p, const std::string &game) {
 
// 	p.writepipe[0] = -1;
 

	
 
	if (pipe(p.readpipe) < 0 || pipe(p.writepipe) < 0) {
 
	}
 

	
 
	std::cout << "dfrotz -p " << game << "\n";
 

	
 
	if ((p.pid = fork()) < 0) {
 
		/* FATAL: cannot fork child */
 
	}
 
	else if (p.pid == 0) {
 
		close(PARENT_WRITE);
 
		close(PARENT_READ);
 

	
 
		dup2(CHILD_READ,  0);  close(CHILD_READ);
 
		dup2(CHILD_WRITE, 1);  close(CHILD_WRITE);
 

	
 
		execlp("dfrotz", "-p", game.c_str(), NULL);
 

	
 
	}
 
	else {
 
		close(CHILD_READ);
 
		close(CHILD_WRITE);
 
	}
 
}
 

	
 
class FrotzNetworkPlugin : public NetworkPlugin {
 
	public:
 
		FrotzNetworkPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin(loop, host, port) {
 
		FrotzNetworkPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
		}
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			np->handleConnected(user);
 
			Swift::StatusShow status;
 
			np->handleBuddyChanged(user, "zcode", "ZCode", "ZCode", status.getType());
 
			np->handleBuddyChanged(user, "zcode", "ZCode", "ZCode", pbnetwork::STATUS_ONLINE);
 
// 			sleep(1);
 
// 			np->handleMessage(np->m_user, "zork", first_msg);
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
			if (games.find(user) != games.end()) {
 
				kill(games[user].pid, SIGTERM);
 
				games.erase(user);
 
			}
 
// 			exit(0);
 
		}
 

	
 
		void readMessage(const std::string &user) {
 
			static char buf[15000];
 
			buf[0] = 0;
 
			int repeated = 0;
 
			while (strlen(buf) == 0) {
 
				ssize_t len = read(games[user].readpipe[0], buf, 15000);
 
				if (len > 0) {
 
					buf[len] = 0;
 
				}
 
				usleep(1000);
 
				repeated++;
 
				if (repeated > 30)
 
					return;
 
			}
 
			np->handleMessage(user, "zcode", buf);
 

	
 
			std::string msg = "save\n";
 
			write(games[user].writepipe[1], msg.c_str(), msg.size());
 

	
 
			msg = user + "_" + games[user].game + ".save\n";
 
			write(games[user].writepipe[1], msg.c_str(), msg.size());
 
			ignoreMessage(user);
 
		}
 

	
 
		void ignoreMessage(const std::string &user) {
 
			usleep(1000000);
 
			static char buf[15000];
 
			buf[0] = 0;
 
			int repeated = 0;
 
			while (strlen(buf) == 0) {
 
				ssize_t len = read(games[user].readpipe[0], buf, 15000);
 
				if (len > 0) {
 
					buf[len] = 0;
 
				}
 
				usleep(1000);
 
				repeated++;
backends/libircclient-qt/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 
FILE(GLOB HEADERS *.h)
 
QT4_WRAP_CPP(SRC ${HEADERS})
 
ADD_EXECUTABLE(spectrum2_libircclient-qt_backend ${SRC})
 
 
target_link_libraries(spectrum2_libircclient-qt_backend ${IRC_LIBRARY} ${QT_LIBRARIES} transport pthread)
 
target_link_libraries(spectrum2_libircclient-qt_backend ${IRC_LIBRARY} ${QT_LIBRARIES} transport-plugin transport pthread)
 
 
INSTALL(TARGETS spectrum2_libircclient-qt_backend RUNTIME DESTINATION bin)
 
backends/libircclient-qt/main.cpp
Show inline comments
 
/*
 
 * Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
 
 *
 
 * This example is free, and not covered by LGPL license. There is no
 
 * restriction applied to their modification, redistribution, using and so on.
 
 * You can study them, modify them, use them in your own program - either
 
 * completely or partially. By using it you may give me some credits in your
 
 * program, but you don't have to.
 
 */
 

	
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "session.h"
 
#include <QtCore>
 
#include "Swiften/EventLoop/Qt/QtEventLoop.h"
 

	
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
class IRCNetworkPlugin;
 
IRCNetworkPlugin * np = NULL;
 

	
 
class IRCNetworkPlugin : public NetworkPlugin {
 
	public:
 
		IRCNetworkPlugin(Config *config, Swift::QtEventLoop *loop, const std::string &host, int port) : NetworkPlugin(loop, host, port) {
 
		IRCNetworkPlugin(Config *config, Swift::QtEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
		}
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			Swift::JID jid(legacyName);
 
			MyIrcSession *session = new MyIrcSession(user, this);
 
			session->setNick(QString::fromStdString(jid.getNode()));
 
			session->connectToServer(QString::fromStdString(jid.getDomain()), 6667);
 
			std::cout << "CONNECTING IRC NETWORK " << jid.getNode() << " " << jid.getDomain() << "\n";
 
			session->setNick(QString::fromStdString(user.substr(0, user.find("@"))));
 
			session->connectToServer(QString::fromStdString(user.substr(user.find("@") + 1)), 6667);
 
// 			std::cout << "CONNECTING IRC NETWORK " << jid.getNode() << " " << jid.getDomain() << "\n";
 
			m_sessions[user] = session;
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
			if (m_sessions[user] == NULL)
 
				return;
 
			m_sessions[user]->disconnectFromServer();
 
			m_sessions[user]->deleteLater();
 
		}
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &/*xhtml*/) {
 
			std::cout << "MESSAGE " << user << " " << legacyName << "\n";
 
			if (m_sessions[user] == NULL)
 
				return;
 
			m_sessions[user]->message(QString::fromStdString(legacyName), QString::fromStdString(message));
 
			std::cout << "SENT\n";
 
		}
 

	
 
		void handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password) {
 
			std::cout << "JOIN\n";
 
			if (m_sessions[user] == NULL)
 
				return;
 
			m_sessions[user]->addAutoJoinChannel(QString::fromStdString(room));
 
			m_sessions[user]->join(QString::fromStdString(room), QString::fromStdString(password));
 
			// update nickname, because we have nickname per session, no nickname per room.
 
			handleRoomNicknameChanged(user, room, m_sessions[user]->nick().toStdString());
 
		}
 

	
 
		void handleLeaveRoomRequest(const std::string &user, const std::string &room) {
 
			std::cout << "PART\n";
 
			if (m_sessions[user] == NULL)
 
				return;
 
			m_sessions[user]->part(QString::fromStdString(room));
 
			m_sessions[user]->removeAutoJoinChannel(QString::fromStdString(room));
 
		}
 

	
 
		std::map<std::string, MyIrcSession *> m_sessions;
 

	
 
	private:
 
		Config *config;
 
};
 

	
 
int main (int argc, char* argv[]) {
 
	std::string host;
 
	int port;
 

	
 

	
 
	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <config_file.cfg>\nAllowed options");
backends/libircclient-qt/session.cpp
Show inline comments
 
@@ -34,184 +34,184 @@ void MyIrcSession::on_bufferAdded(Irc::Buffer* buffer)
 
{
 
    qDebug() << "buffer added:" << buffer->receiver();
 
}
 

	
 
void MyIrcSession::on_bufferRemoved(Irc::Buffer* buffer)
 
{
 
    qDebug() << "buffer removed:" << buffer->receiver();
 
}
 

	
 
Irc::Buffer* MyIrcSession::createBuffer(const QString& receiver)
 
{
 
    return new MyIrcBuffer(receiver, user, np, this);
 
}
 

	
 
MyIrcBuffer::MyIrcBuffer(const QString& receiver, const std::string &user, NetworkPlugin *np, Irc::Session* parent)
 
    : Irc::Buffer(receiver, parent)
 
{
 
	this->np = np;
 
	this->user = user;
 
	p = (MyIrcSession *) parent;
 
    connect(this, SIGNAL(receiverChanged(QString)), SLOT(on_receiverChanged(QString)));
 
    connect(this, SIGNAL(joined(QString)), SLOT(on_joined(QString)));
 
    connect(this, SIGNAL(parted(QString, QString)), SLOT(on_parted(QString, QString)));
 
    connect(this, SIGNAL(quit(QString, QString)), SLOT(on_quit(QString, QString)));
 
    connect(this, SIGNAL(nickChanged(QString, QString)), SLOT(on_nickChanged(QString, QString)));
 
    connect(this, SIGNAL(modeChanged(QString, QString, QString)), SLOT(on_modeChanged(QString, QString, QString)));
 
    connect(this, SIGNAL(topicChanged(QString, QString)), SLOT(on_topicChanged(QString, QString)));
 
    connect(this, SIGNAL(invited(QString, QString, QString)), SLOT(on_invited(QString, QString, QString)));
 
    connect(this, SIGNAL(kicked(QString, QString, QString)), SLOT(on_kicked(QString, QString, QString)));
 
    connect(this, SIGNAL(messageReceived(QString, QString, Irc::Buffer::MessageFlags)),
 
                  SLOT(on_messageReceived(QString, QString, Irc::Buffer::MessageFlags)));
 
    connect(this, SIGNAL(noticeReceived(QString, QString, Irc::Buffer::MessageFlags)),
 
                  SLOT(on_noticeReceived(QString, QString, Irc::Buffer::MessageFlags)));
 
    connect(this, SIGNAL(ctcpRequestReceived(QString, QString, Irc::Buffer::MessageFlags)),
 
                  SLOT(on_ctcpRequestReceived(QString, QString, Irc::Buffer::MessageFlags)));
 
    connect(this, SIGNAL(ctcpReplyReceived(QString, QString, Irc::Buffer::MessageFlags)),
 
                  SLOT(on_ctcpReplyReceived(QString, QString, Irc::Buffer::MessageFlags)));
 
    connect(this, SIGNAL(ctcpActionReceived(QString, QString, Irc::Buffer::MessageFlags)),
 
                  SLOT(on_ctcpActionReceived(QString, QString, Irc::Buffer::MessageFlags)));
 
    connect(this, SIGNAL(numericMessageReceived(QString, uint, QStringList)), SLOT(on_numericMessageReceived(QString, uint, QStringList)));
 
    connect(this, SIGNAL(unknownMessageReceived(QString, QStringList)), SLOT(on_unknownMessageReceived(QString, QStringList)));
 
}
 

	
 
void MyIrcBuffer::on_receiverChanged(const QString& receiver)
 
{
 
    qDebug() << "receiver changed:" << receiver;
 
}
 

	
 
Conversation::ParticipantFlag MyIrcBuffer::correctNickname(std::string &nickname) {
 
	Conversation::ParticipantFlag flags = Conversation::None;
 
bool MyIrcBuffer::correctNickname(std::string &nickname) {
 
	bool flags = 0;
 
	switch(nickname.at(0)) {
 
		case '@': nickname = nickname.substr(1); flags = Conversation::Moderator; break;
 
		case '@': nickname = nickname.substr(1); flags = 1; break;
 
		case '+': nickname = nickname.substr(1); break;
 
		default: break;
 
	}
 
	return flags;
 
}
 

	
 
void MyIrcBuffer::on_joined(const QString& origin) {
 
	qDebug() << "joined:" << receiver() << origin;
 
	Conversation::ParticipantFlag flags = Conversation::None;
 
	bool flags = 0;
 
	std::string nickname = origin.toStdString();
 
	flags = correctNickname(nickname);
 
	np->handleParticipantChanged(user, origin.toStdString(), receiver().toStdString(), flags, Swift::StatusShow::Online);
 
	np->handleParticipantChanged(user, origin.toStdString(), receiver().toStdString(), (int) flags, pbnetwork::STATUS_ONLINE);
 
}
 

	
 
void MyIrcBuffer::on_parted(const QString& origin, const QString& message) {
 
	qDebug() << "parted:" << receiver() << origin << message;
 
	Conversation::ParticipantFlag flags = Conversation::None;
 
	bool flags = 0;
 
	std::string nickname = origin.toStdString();
 
	flags = correctNickname(nickname);
 
	np->handleParticipantChanged(user, nickname, receiver().toStdString(), flags, Swift::StatusShow::None, message.toStdString());
 
	np->handleParticipantChanged(user, nickname, receiver().toStdString(),(int) flags, pbnetwork::STATUS_NONE, message.toStdString());
 
}
 

	
 
void MyIrcBuffer::on_quit(const QString& origin, const QString& message)
 
{
 
    qDebug() << "quit:" << receiver() << origin << message;
 
	on_parted(origin, message);
 
}
 

	
 
void MyIrcBuffer::on_nickChanged(const QString& origin, const QString& nick) {
 
	qDebug() << "nick changed:" << receiver() << origin << nick;
 
	std::string nickname = origin.toStdString();
 
	Conversation::ParticipantFlag flags = p->m_modes[receiver().toStdString() + nickname];
 
	bool flags = p->m_modes[receiver().toStdString() + nickname];
 
// 	std::cout << receiver().toStdString() + nickname << " " << flags <<  "\n";
 
	np->handleParticipantChanged(user, nickname, receiver().toStdString(), flags, Swift::StatusShow::Online, "", nick.toStdString());
 
	np->handleParticipantChanged(user, nickname, receiver().toStdString(),(int) flags, pbnetwork::STATUS_ONLINE, "", nick.toStdString());
 
}
 

	
 
void MyIrcBuffer::on_modeChanged(const QString& origin, const QString& mode, const QString& args) {
 
	// mode changed: "#testik" "HanzZ" "+o" "hanzz_k" 
 
	qDebug() << "mode changed:" << receiver() << origin << mode << args;
 
	std::string nickname = args.toStdString();
 
	if (nickname.empty())
 
		return;
 
	if (mode == "+o") {
 
		p->m_modes[receiver().toStdString() + nickname] = Conversation::Moderator;
 
		p->m_modes[receiver().toStdString() + nickname] = 1;
 
	}
 
	else {
 
		p->m_modes[receiver().toStdString() + nickname] = Conversation::None;
 
		p->m_modes[receiver().toStdString() + nickname] = 0;
 
	}
 
	Conversation::ParticipantFlag flags = p->m_modes[receiver().toStdString() + nickname];
 
	np->handleParticipantChanged(user, nickname, receiver().toStdString(), flags, Swift::StatusShow::Online, "");
 
	bool flags = p->m_modes[receiver().toStdString() + nickname];
 
	np->handleParticipantChanged(user, nickname, receiver().toStdString(),(int) flags, pbnetwork::STATUS_ONLINE, "");
 
}
 

	
 
void MyIrcBuffer::on_topicChanged(const QString& origin, const QString& topic) {
 
	//topic changed: "#testik" "HanzZ" "test"
 
	qDebug() << "topic changed:" << receiver() << origin << topic;
 
	np->handleSubject(user, receiver().toStdString(), topic.toStdString(), origin.toStdString());
 
}
 

	
 
void MyIrcBuffer::on_invited(const QString& origin, const QString& receiver, const QString& channel)
 
{
 
    qDebug() << "invited:" << Irc::Buffer::receiver() << origin << receiver << channel;
 
}
 

	
 
void MyIrcBuffer::on_kicked(const QString& origin, const QString& nick, const QString& message)
 
{
 
    qDebug() << "kicked:" << receiver() << origin << nick << message;
 
}
 

	
 
void MyIrcBuffer::on_messageReceived(const QString& origin, const QString& message, Irc::Buffer::MessageFlags flags) {
 
	qDebug() << "message received:" << receiver() << origin << message << (flags & Irc::Buffer::IdentifiedFlag ? "(identified!)" : "(not identified)");
 
	if (!receiver().startsWith("#") && (flags & Irc::Buffer::EchoFlag))
 
		return;
 
	np->handleMessage(user, receiver().toStdString(), message.toStdString(), origin.toStdString());
 
}
 

	
 
void MyIrcBuffer::on_noticeReceived(const QString& origin, const QString& notice, Irc::Buffer::MessageFlags flags)
 
{
 
    qDebug() << "notice received:" << receiver() << origin << notice
 
             << (flags & Irc::Buffer::IdentifiedFlag ? "(identified!)" : "(not identified)");
 
}
 

	
 
void MyIrcBuffer::on_ctcpRequestReceived(const QString& origin, const QString& request, Irc::Buffer::MessageFlags flags)
 
{
 
    qDebug() << "ctcp request received:" << receiver() << origin << request
 
             << (flags & Irc::Buffer::IdentifiedFlag ? "(identified!)" : "(not identified)");
 
}
 

	
 
void MyIrcBuffer::on_ctcpReplyReceived(const QString& origin, const QString& reply, Irc::Buffer::MessageFlags flags)
 
{
 
    qDebug() << "ctcp reply received:" << receiver() << origin << reply
 
             << (flags & Irc::Buffer::IdentifiedFlag ? "(identified!)" : "(not identified)");
 
}
 

	
 
void MyIrcBuffer::on_ctcpActionReceived(const QString& origin, const QString& action, Irc::Buffer::MessageFlags flags)
 
{
 
    qDebug() << "ctcp action received:" << receiver() << origin << action
 
             << (flags & Irc::Buffer::IdentifiedFlag ? "(identified!)" : "(not identified)");
 
}
 

	
 
void MyIrcBuffer::on_numericMessageReceived(const QString& origin, uint code, const QStringList& params)
 
{
 
	switch (code) {
 
		case 251:
 
			np->handleConnected(user);
 
			break;
 
		case 332:
 
			m_topicData = params.value(2).toStdString();
 
			break;
 
		case 333:
 
			np->handleSubject(user, params.value(1).toStdString(), m_topicData, params.value(2).toStdString());
 
			break;
 
		case 353:
 
			QString channel = params.value(2);
 
			QStringList members = params.value(3).split(" ");
 

	
 
			for (int i = 0; i < members.size(); i++) {
 
				Conversation::ParticipantFlag flags = Conversation::None;
 
				bool flags = 0;
 
				std::string nickname = members.at(i).toStdString();
 
				flags = correctNickname(nickname);
 
				p->m_modes[channel.toStdString() + nickname] = flags;
 
// 				std::cout << channel.toStdString() + nickname << " " << flags << "\n";
 
				np->handleParticipantChanged(user, nickname, channel.toStdString(), flags, Swift::StatusShow::Online);
 
				np->handleParticipantChanged(user, nickname, channel.toStdString(),(int) flags, pbnetwork::STATUS_ONLINE);
 
			}
 
			break;
 
	}
 
    qDebug() << "numeric message received:" << receiver() << origin << code << params;
 
}
 

	
 
void MyIrcBuffer::on_unknownMessageReceived(const QString& origin, const QStringList& params)
 
{
 
    qDebug() << "unknown message received:" << receiver() << origin << params;
 
}
backends/libircclient-qt/session.h
Show inline comments
 
/*
 
 * Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
 
 *
 
 * This example is free, and not covered by LGPL license. There is no
 
 * restriction applied to their modification, redistribution, using and so on.
 
 * You can study them, modify them, use them in your own program - either
 
 * completely or partially. By using it you may give me some credits in your
 
 * program, but you don't have to.
 
 */
 

	
 
#ifndef SESSION_H
 
#define SESSION_H
 

	
 
#include <IrcSession>
 
#include <IrcBuffer>
 
#include <transport/networkplugin.h>
 

	
 
using namespace Transport;
 

	
 
class MyIrcSession : public Irc::Session
 
{
 
    Q_OBJECT
 

	
 
public:
 
    MyIrcSession(const std::string &user, NetworkPlugin *np, QObject* parent = 0);
 
	std::map<std::string, Conversation::ParticipantFlag> m_modes;
 
	std::map<std::string, bool> m_modes;
 

	
 
protected Q_SLOTS:
 
    void on_connected();
 
    void on_disconnected();
 

	
 
    void on_bufferAdded(Irc::Buffer* buffer);
 
    void on_bufferRemoved(Irc::Buffer* buffer);
 

	
 
protected:
 
	NetworkPlugin *np;
 
	std::string user;
 
    virtual Irc::Buffer* createBuffer(const QString& receiver);
 
};
 

	
 
class MyIrcBuffer : public Irc::Buffer
 
{
 
    Q_OBJECT
 

	
 
public:
 
    MyIrcBuffer(const QString& receiver, const std::string &user, NetworkPlugin *np, Irc::Session* parent);
 
	NetworkPlugin *np;
 
	std::string user;
 
	MyIrcSession *p;
 
	std::string m_topicData;
 

	
 
protected Q_SLOTS:
 
    void on_receiverChanged(const QString& receiver);
 
    void on_joined(const QString& origin);
 
    void on_parted(const QString& origin, const QString& message);
 
    void on_quit(const QString& origin, const QString& message);
 
    void on_nickChanged(const QString& origin, const QString& nick);
 
    void on_modeChanged(const QString& origin, const QString& mode, const QString& args);
 
    void on_topicChanged(const QString& origin, const QString& topic);
 
    void on_invited(const QString& origin, const QString& receiver, const QString& channel);
 
    void on_kicked(const QString& origin, const QString& nick, const QString& message);
 
    void on_messageReceived(const QString& origin, const QString& message, Irc::Buffer::MessageFlags flags);
 
    void on_noticeReceived(const QString& origin, const QString& notice, Irc::Buffer::MessageFlags flags);
 
    void on_ctcpRequestReceived(const QString& origin, const QString& request, Irc::Buffer::MessageFlags flags);
 
    void on_ctcpReplyReceived(const QString& origin, const QString& reply, Irc::Buffer::MessageFlags flags);
 
    void on_ctcpActionReceived(const QString& origin, const QString& action, Irc::Buffer::MessageFlags flags);
 
    void on_numericMessageReceived(const QString& origin, uint code, const QStringList& params);
 
    void on_unknownMessageReceived(const QString& origin, const QStringList& params);
 

	
 
	Conversation::ParticipantFlag correctNickname(std::string &nickname);
 
	bool correctNickname(std::string &nickname);
 
};
 

	
 
#endif // SESSION_H
backends/libpurple/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_libpurple_backend ${SRC})
 
 
target_link_libraries(spectrum2_libpurple_backend ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport pthread)
 
target_link_libraries(spectrum2_libpurple_backend ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin pthread)
 
 
INSTALL(TARGETS spectrum2_libpurple_backend RUNTIME DESTINATION bin)
 
backends/libpurple/main.cpp
Show inline comments
 
#include "glib.h"
 
#include "purple.h"
 
#include <iostream>
 

	
 
#include "transport/config.h"
 
#include "transport/transport.h"
 
#include "transport/usermanager.h"
 
#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/conversation.h"
 
#include "transport/networkplugin.h"
 
#include "spectrumeventloop.h"
 
#include "geventloop.h"
 
#include "log4cxx/logger.h"
 
#include "log4cxx/consoleappender.h"
 
#include "log4cxx/patternlayout.h"
 
#include "log4cxx/propertyconfigurator.h"
 
#include "log4cxx/helpers/properties.h"
 
#include "log4cxx/helpers/fileinputstream.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
// #include "valgrind/memcheck.h"
 
#include "malloc.h"
 

	
 
#include <algorithm>
 
#include "errno.h"
 

	
 
#include <netinet/if_ether.h>
 
#include <netinet/ip.h>
 
#include <netinet/ip6.h>
 
#include <netinet/udp.h>
 
#include <netinet/tcp.h>
 
#include <netinet/ether.h>
 
#include "sys/socket.h"
 
#include <netdb.h>
 
#include <unistd.h>
 
#include <fcntl.h>
 

	
 
#ifdef WITH_LIBEVENT
 
#include <event.h>
 
#endif
 

	
 
using namespace log4cxx;
 

	
 
static LoggerPtr logger_libpurple = Logger::getLogger("backend.libpurple");
 
static LoggerPtr logger = Logger::getLogger("backend");
 
static LoggerPtr logger_libpurple = log4cxx::Logger::getLogger("libpurple");
 
static LoggerPtr logger = log4cxx::Logger::getLogger("backend");
 
int m_sock;
 

	
 
using namespace Transport;
 

	
 
class SpectrumNetworkPlugin;
 
template <class T> T fromString(const std::string &str) {
 
	T i;
 
	std::istringstream os(str);
 
	os >> i;
 
	return i;
 
}
 

	
 
template <class T> std::string stringOf(T object) {
 
	std::ostringstream os;
 
	os << object;
 
	return (os.str());
 
}
 

	
 
class SpectrumNetworkPlugin;
 

	
 
GKeyFile *keyfile;
 
SpectrumNetworkPlugin *np;
 

	
 
static std::string KEYFILE_STRING(const std::string &cat, const std::string &key, const std::string &def = "") {
 
	gchar *str = g_key_file_get_string(keyfile, cat.c_str(), key.c_str(), 0);
 
	if (!str) {
 
		return def;
 
	}
 
	std::string ret(str);
 
	free(str);
 
	return ret;
 
}
 

	
 
#define KEYFILE_BOOL(CAT, KEY) g_key_file_get_boolean(keyfile, CAT, KEY, 0)
 

	
 
static gboolean nodaemon = FALSE;
 
static gchar *logfile = NULL;
 
static gchar *lock_file = NULL;
 
static gchar *host = NULL;
 
static int port = 10000;
 
static gboolean ver = FALSE;
 
static gboolean list_purple_settings = FALSE;
 

	
 
struct FTData {
 
	unsigned long id;
 
	unsigned long timer;
 
	bool paused;
 
};
 

	
 
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 }
 
};
 

	
 
static void *notify_user_info(PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info);
 
static GHashTable *ui_info = NULL;
 

	
 
static GHashTable *spectrum_ui_get_info(void)
 
{
 
	if(NULL == ui_info) {
 
		ui_info = g_hash_table_new(g_str_hash, g_str_equal);
 

	
 
		g_hash_table_insert(ui_info, g_strdup("name"), g_strdup("Spectrum"));
 
		g_hash_table_insert(ui_info, g_strdup("version"), g_strdup("0.5"));
 
		g_hash_table_insert(ui_info, g_strdup("website"), g_strdup("http://spectrum.im"));
 
		g_hash_table_insert(ui_info, g_strdup("dev_website"), g_strdup("http://spectrum.im"));
 
		g_hash_table_insert(ui_info, g_strdup("client_type"), g_strdup("pc"));
 

	
 
		/*
 
		 * This is the client key for "Pidgin."  It is owned by the AIM
 
		 * account "markdoliner."  Please don't use this key for other
 
		 * applications.  You can either not specify a client key, in
 
		 * which case the default "libpurple" key will be used, or you
 
		 * can register for your own client key at
 
		 * http://developer.aim.com/manageKeys.jsp
 
		 */
 
		g_hash_table_insert(ui_info, g_strdup("prpl-aim-clientkey"), g_strdup("ma1cSASNCKFtrdv9"));
 
@@ -387,694 +417,693 @@ static void * requestInput(const char *title, const char *primary,const char *se
 
		}
 
		else {
 
			LOG4CXX_WARN(logger, "Unhandled request input. title=" << titleString);
 
		}
 
	}
 
	else {
 
		LOG4CXX_WARN(logger, "Request input without primary string");
 
	}
 
	return NULL;
 
}
 

	
 
static void *requestAction(const char *title, const char *primary, const char *secondary, int default_action, PurpleAccount *account, const char *who,PurpleConversation *conv, void *user_data, size_t action_count, va_list actions){
 
	std::string t(title ? title : "NULL");
 
	if (t == "SSL Certificate Verification") {
 
		LOG4CXX_INFO(logger,  "accepting SSL certificate");
 
		va_arg(actions, char *);
 
		((PurpleRequestActionCb) va_arg(actions, GCallback)) (user_data, 2);
 
	}
 
	else {
 
		if (title) {
 
			std::string headerString(title);
 
			LOG4CXX_INFO(logger,  "header string: " << headerString);
 
			if (headerString == "SSL Certificate Verification") {
 
				va_arg(actions, char *);
 
				((PurpleRequestActionCb) va_arg(actions, GCallback)) (user_data, 2);
 
			}
 
		}
 
	}
 
	return NULL;
 
}
 

	
 
static std::string getAlias(PurpleBuddy *m_buddy) {
 
	std::string alias;
 
	PurpleContact *contact = PURPLE_CONTACT(PURPLE_BLIST_NODE(m_buddy)->parent);
 
	if (contact && contact->alias) {
 
		alias = contact->alias;
 
	}
 
	else if (purple_buddy_get_alias(m_buddy)) {
 
		alias = (std::string) purple_buddy_get_alias(m_buddy);
 
	}
 
	else {
 
		alias = (std::string) purple_buddy_get_server_alias(m_buddy);
 
	}
 
	return alias;
 
}
 

	
 
class SpectrumNetworkPlugin : public NetworkPlugin {
 
	public:
 
		SpectrumEventLoop *m_loop;
 
		SpectrumNetworkPlugin(Config *config, SpectrumEventLoop *loop, const std::string &host, int port) : NetworkPlugin(loop, host, port) {
 
			this->config = config;
 
			m_loop = loop;
 
		SpectrumNetworkPlugin(const std::string &host, int port) : NetworkPlugin() {
 

	
 
		}
 

	
 
		void handleExit() {
 
			m_loop->stop();
 
		void handleExitRequest() {
 
			LOG4CXX_INFO(logger, "Exiting...");
 
			exit(1);
 
		}
 

	
 
		void getProtocolAndName(const std::string &legacyName, std::string &name, std::string &protocol) {
 
			name = legacyName;
 
			protocol = CONFIG_STRING(config, "service.protocol");
 
			protocol = KEYFILE_STRING("service", "protocol");
 
			if (protocol == "any") {
 
				protocol = name.substr(0, name.find("."));
 
				name = name.substr(name.find(".") + 1);
 
			}
 
		}
 

	
 
		void setDefaultAvatar(PurpleAccount *account, const std::string &legacyName) {
 
			char* contents;
 
			gsize length;
 
			gboolean ret = false;
 
			if (!CONFIG_STRING(config, "backend.avatars_directory").empty()) {
 
				std::string f = CONFIG_STRING(config, "backend.avatars_directory") + "/" + legacyName;
 
			if (!KEYFILE_STRING("backend", "avatars_directory").empty()) {
 
				std::string f = KEYFILE_STRING("backend", "avatars_directory") + "/" + legacyName;
 
				ret = g_file_get_contents (f.c_str(), &contents, &length, NULL);
 
			}
 

	
 
			if (!CONFIG_STRING(config, "backend.default_avatar").empty() && !ret) {
 
				ret = g_file_get_contents (CONFIG_STRING(config, "backend.default_avatar").c_str(),
 
			if (!KEYFILE_STRING("backend", "default_avatar").empty() && !ret) {
 
				ret = g_file_get_contents (KEYFILE_STRING("backend", "default_avatar").c_str(),
 
											&contents, &length, NULL);
 
			}
 

	
 
			if (ret) {
 
				purple_buddy_icons_set_account_icon(account, (guchar *) contents, length);
 
			}
 
		}
 

	
 
		void setDefaultAccountOptions(PurpleAccount *account) {
 
			for (std::map<std::string,std::string>::const_iterator it = config->getUnregistered().begin();
 
				it != config->getUnregistered().end(); it++) {
 
				if ((*it).first.find("purple.") == 0) {
 
					std::string key = (*it).first.substr((*it).first.find(".") + 1);
 

	
 
					PurplePlugin *plugin = purple_find_prpl(purple_account_get_protocol_id(account));
 
					PurplePluginProtocolInfo *prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(plugin);
 
					bool found = false;
 
					for (GList *l = prpl_info->protocol_options; l != NULL; l = l->next) {
 
						PurpleAccountOption *option = (PurpleAccountOption *) l->data;
 
						PurplePrefType type = purple_account_option_get_type(option);
 
						std::string key2(purple_account_option_get_setting(option));
 
						std::cout << key << " " << key2 << " " << (*it).second << "\n";
 
						if (key != key2)
 
							continue;
 
						
 
						found = true;
 
						switch (type) {
 
							case PURPLE_PREF_BOOLEAN:
 
								purple_account_set_bool(account, key.c_str(), boost::lexical_cast<bool>((*it).second));
 
								break;
 

	
 
							case PURPLE_PREF_INT:
 
								purple_account_set_int(account, key.c_str(), boost::lexical_cast<int>((*it).second));
 
								break;
 

	
 
							case PURPLE_PREF_STRING:
 
							case PURPLE_PREF_STRING_LIST:
 
								purple_account_set_string(account, key.c_str(), (*it).second.c_str());
 
								break;
 
							default:
 
								continue;
 
						}
 
						break;
 
					}
 

	
 
					if (!found) {
 
						purple_account_set_string(account, key.c_str(), (*it).second.c_str());
 
					}
 
				}
 
			}
 
// 			for (std::map<std::string,std::string>::const_iterator it = config->getUnregistered().begin();
 
// 				it != config->getUnregistered().end(); it++) {
 
// 				if ((*it).first.find("purple.") == 0) {
 
// 					std::string key = (*it).first.substr((*it).first.find(".") + 1);
 
// 
 
// 					PurplePlugin *plugin = purple_find_prpl(purple_account_get_protocol_id(account));
 
// 					PurplePluginProtocolInfo *prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(plugin);
 
// 					bool found = false;
 
// 					for (GList *l = prpl_info->protocol_options; l != NULL; l = l->next) {
 
// 						PurpleAccountOption *option = (PurpleAccountOption *) l->data;
 
// 						PurplePrefType type = purple_account_option_get_type(option);
 
// 						std::string key2(purple_account_option_get_setting(option));
 
// 						std::cout << key << " " << key2 << " " << (*it).second << "\n";
 
// 						if (key != key2)
 
// 							continue;
 
// 						
 
// 						found = true;
 
// 						switch (type) {
 
// 							case PURPLE_PREF_BOOLEAN:
 
// 								purple_account_set_bool(account, key.c_str(), fromString<bool>((*it).second));
 
// 								break;
 
// 
 
// 							case PURPLE_PREF_INT:
 
// 								purple_account_set_int(account, key.c_str(), fromString<int>((*it).second));
 
// 								break;
 
// 
 
// 							case PURPLE_PREF_STRING:
 
// 							case PURPLE_PREF_STRING_LIST:
 
// 								purple_account_set_string(account, key.c_str(), (*it).second.c_str());
 
// 								break;
 
// 							default:
 
// 								continue;
 
// 						}
 
// 						break;
 
// 					}
 
// 
 
// 					if (!found) {
 
// 						purple_account_set_string(account, key.c_str(), (*it).second.c_str());
 
// 					}
 
// 				}
 
// 			}
 
		}
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			PurpleAccount *account = NULL;
 
			
 
			std::string name;
 
			std::string protocol;
 
			getProtocolAndName(legacyName, name, protocol);
 

	
 
			if (password.empty()) {
 
				np->handleDisconnected(user, 0, "Empty password.");
 
				return;
 
			}
 

	
 
			if (!purple_find_prpl(protocol.c_str())) {
 
				np->handleDisconnected(user, 0, "Invalid protocol " + protocol);
 
				return;
 
			}
 

	
 
			LOG4CXX_INFO(logger,  "Creating account with name '" << name.c_str() << "' and protocol '" << protocol << "'");
 
			if (purple_accounts_find(name.c_str(), protocol.c_str()) != NULL){
 
				account = purple_accounts_find(name.c_str(), protocol.c_str());
 
			}
 
			else {
 
				account = purple_account_new(name.c_str(), protocol.c_str());
 
				purple_accounts_add(account);
 
			}
 

	
 
			m_sessions[user] = account;
 
			m_accounts[account] = user;
 

	
 
			// Default avatar
 
			setDefaultAvatar(account, legacyName);
 

	
 
			purple_account_set_password(account, password.c_str());
 
			purple_account_set_bool(account, "custom_smileys", FALSE);
 
			purple_account_set_bool(account, "direct_connect", FALSE);
 

	
 
			setDefaultAccountOptions(account);
 

	
 
			purple_account_set_enabled(account, "spectrum", TRUE);
 
			if (CONFIG_BOOL(np->config, "service.enable_privacy_lists")) {
 
			if (KEYFILE_BOOL("service", "enable_privacy_lists")) {
 
				purple_account_set_privacy_type(account, PURPLE_PRIVACY_DENY_USERS);
 
			}
 

	
 
			const PurpleStatusType *status_type = purple_account_get_status_type_with_primitive(account, PURPLE_STATUS_AVAILABLE);
 
			if (status_type != NULL) {
 
				purple_account_set_status(account, purple_status_type_get_id(status_type), TRUE, NULL);
 
			}
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
// 				VALGRIND_DO_LEAK_CHECK;
 
				m_sessions.erase(user);
 
				purple_account_disconnect(account);
 
				purple_account_set_enabled(account, "spectrum", FALSE);
 

	
 
				g_free(account->ui_data);
 
				account->ui_data = NULL;
 
				m_accounts.erase(account);
 

	
 
				purple_accounts_delete(account);
 
// 
 
// 				// Remove conversations.
 
// 				// This has to be called before m_account->ui_data = NULL;, because it uses
 
// 				// ui_data to call SpectrumMessageHandler::purpleConversationDestroyed() callback.
 
// 				GList *iter;
 
// 				for (iter = purple_get_conversations(); iter; ) {
 
// 					PurpleConversation *conv = (PurpleConversation*) iter->data;
 
// 					iter = iter->next;
 
// 					if (purple_conversation_get_account(conv) == account)
 
// 						purple_conversation_destroy(conv);
 
// 				}
 
// 
 
// 				g_free(account->ui_data);
 
// 				account->ui_data = NULL;
 
// 				m_accounts.erase(account);
 
// 
 
// 				purple_notify_close_with_handle(account);
 
// 				purple_request_close_with_handle(account);
 
// 
 
// 				purple_accounts_remove(account);
 
// 
 
// 				GSList *buddies = purple_find_buddies(account, NULL);
 
// 				while(buddies) {
 
// 					PurpleBuddy *b = (PurpleBuddy *) buddies->data;
 
// 					purple_blist_remove_buddy(b);
 
// 					buddies = g_slist_delete_link(buddies, buddies);
 
// 				}
 
// 
 
// 				/* Remove any open conversation for this account */
 
// 				for (GList *it = purple_get_conversations(); it; ) {
 
// 					PurpleConversation *conv = (PurpleConversation *) it->data;
 
// 					it = it->next;
 
// 					if (purple_conversation_get_account(conv) == account)
 
// 						purple_conversation_destroy(conv);
 
// 				}
 
// 
 
// 				/* Remove this account's pounces */
 
// 					// purple_pounce_destroy_all_by_account(account);
 
// 
 
// 				/* This will cause the deletion of an old buddy icon. */
 
// 				purple_buddy_icons_set_account_icon(account, NULL, 0);
 
// 
 
// 				purple_account_destroy(account);
 
				// force returning of memory chunks allocated by libxml2 to kernel
 
				malloc_trim(0);
 
// 				VALGRIND_DO_LEAK_CHECK;
 
			}
 
		}
 

	
 
		void handleStatusChangeRequest(const std::string &user, int status, const std::string &statusMessage) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				int st;
 
				switch(status) {
 
					case Swift::StatusShow::Away: {
 
					case pbnetwork::STATUS_AWAY: {
 
						st = PURPLE_STATUS_AWAY;
 
						if (!purple_account_get_status_type_with_primitive(account, PURPLE_STATUS_AWAY))
 
							st = PURPLE_STATUS_EXTENDED_AWAY;
 
						else
 
							st = PURPLE_STATUS_AWAY;
 
						break;
 
					}
 
					case Swift::StatusShow::DND: {
 
					case pbnetwork::STATUS_DND: {
 
						st = PURPLE_STATUS_UNAVAILABLE;
 
						break;
 
					}
 
					case Swift::StatusShow::XA: {
 
					case pbnetwork::STATUS_XA: {
 
						if (!purple_account_get_status_type_with_primitive(account, PURPLE_STATUS_EXTENDED_AWAY))
 
							st = PURPLE_STATUS_AWAY;
 
						else
 
							st = PURPLE_STATUS_EXTENDED_AWAY;
 
						break;
 
					}
 
					case Swift::StatusShow::None: {
 
					case pbnetwork::STATUS_NONE: {
 
						st = PURPLE_STATUS_OFFLINE;
 
						break;
 
					}
 
					case 6:
 
					case pbnetwork::STATUS_INVISIBLE:
 
						st = PURPLE_STATUS_INVISIBLE;
 
						break;
 
					default:
 
						st = PURPLE_STATUS_AVAILABLE;
 
						break;
 
				}
 
				gchar *_markup = purple_markup_escape_text(statusMessage.c_str(), -1);
 
				std::string markup(_markup);
 
				g_free(_markup);
 

	
 
				// we are already connected so we have to change status
 
				const PurpleStatusType *status_type = purple_account_get_status_type_with_primitive(account, (PurpleStatusPrimitive) st);
 
				if (status_type != NULL) {
 
					// send presence to legacy network
 
					if (!markup.empty()) {
 
						purple_account_set_status(account, purple_status_type_get_id(status_type), TRUE, "message", markup.c_str(), NULL);
 
					}
 
					else {
 
						purple_account_set_status(account, purple_status_type_get_id(status_type), TRUE, NULL);
 
					}
 
				}
 
			}
 
		}
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				PurpleConversation *conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, legacyName.c_str(), account);
 
				if (!conv) {
 
					conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, legacyName.c_str());
 
				}
 
				if (xhtml.empty()) {
 
					gchar *_markup = purple_markup_escape_text(message.c_str(), -1);
 
					purple_conv_im_send(PURPLE_CONV_IM(conv), _markup);
 
					g_free(_markup);
 
				}
 
				else {
 
					purple_conv_im_send(PURPLE_CONV_IM(conv), xhtml.c_str());
 
				}
 
			}
 
		}
 

	
 
		void handleVCardRequest(const std::string &user, const std::string &legacyName, unsigned int id) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				std::string name = legacyName;
 
				if (CONFIG_STRING(config, "service.protocol") == "any" && legacyName.find("prpl-") == 0) {
 
				if (KEYFILE_STRING("service", "protocol") == "any" && legacyName.find("prpl-") == 0) {
 
					name = name.substr(name.find(".") + 1);
 
				}
 
				m_vcards[user + name] = id;
 

	
 
				std::cout << name << " " << purple_account_get_username(account) << "\n";
 
				if (CONFIG_BOOL(config, "backend.no_vcard_fetch") && name != purple_account_get_username(account)) {
 
				if (KEYFILE_BOOL("backend", "no_vcard_fetch") && name != purple_account_get_username(account)) {
 
					PurpleNotifyUserInfo *user_info = purple_notify_user_info_new();
 
					notify_user_info(purple_account_get_connection(account), name.c_str(), user_info);
 
					purple_notify_user_info_destroy(user_info);
 
				}
 
				else {
 
					serv_get_info(purple_account_get_connection(account), name.c_str());
 
				}
 
				
 
			}
 
		}
 

	
 
		void handleVCardUpdatedRequest(const std::string &user, const std::string &image, const std::string &nickname) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				purple_account_set_alias(account, nickname.c_str());
 
				purple_account_set_public_alias(account, nickname.c_str(), NULL, NULL);
 
				gssize size = image.size();
 
				// this will be freed by libpurple
 
				guchar *photo = (guchar *) g_malloc(size * sizeof(guchar));
 
				memcpy(photo, image.c_str(), size);
 

	
 
				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) {
 
					if (getAlias(buddy) != alias) {
 
						purple_blist_alias_buddy(buddy, alias.c_str());
 
						purple_blist_server_alias_buddy(buddy, alias.c_str());
 
						serv_alias_buddy(buddy);
 
					}
 

	
 
					PurpleGroup *group = purple_find_group(groups.c_str());
 
					if (!group) {
 
						group = purple_group_new(groups.c_str());
 
					}
 
					purple_blist_add_contact(purple_buddy_get_contact(buddy), group ,NULL);
 
				}
 
				else {
 
					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());
 
					if (!group) {
 
						group = purple_group_new(groups.c_str());
 
					}
 
					purple_blist_add_buddy(buddy, NULL, group ,NULL);
 
					purple_account_add_buddy(account, buddy);
 
				}
 
			}
 
		}
 

	
 
		void handleBuddyBlockToggled(const std::string &user, const std::string &buddyName, bool blocked) {
 
			if (CONFIG_BOOL(np->config, "service.enable_privacy_lists")) {
 
			if (KEYFILE_BOOL("service", "enable_privacy_lists")) {
 
				PurpleAccount *account = m_sessions[user];
 
				if (account) {
 
					if (blocked) {
 
						purple_privacy_deny(account, buddyName.c_str(), FALSE, FALSE);
 
					}
 
					else {
 
						purple_privacy_allow(account, buddyName.c_str(), FALSE, FALSE);
 
					}
 
				}
 
			}
 
		}
 

	
 
		void handleTypingRequest(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_TYPING);
 
			}
 
		}
 

	
 
		void handleTypedRequest(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_TYPED);
 
			}
 
		}
 

	
 
		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);
 
			}
 
		}
 

	
 
		void handleAttentionRequest(const std::string &user, const std::string &buddyName, const std::string &message) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				purple_prpl_send_attention(purple_account_get_connection(account), buddyName.c_str(), 0);
 
			}
 
		}
 

	
 
		void handleFTStartRequest(const std::string &user, const std::string &buddyName, const std::string &fileName, unsigned long size, unsigned long ftID) {
 
			PurpleXfer *xfer = m_unhandledXfers[user + fileName + buddyName];
 
			if (xfer) {
 
				m_unhandledXfers.erase(user + fileName + buddyName);
 
				FTData *ftData = (FTData *) xfer->ui_data;
 
				
 
				ftData->id = ftID;
 
				m_xfers[ftID] = xfer;
 
				purple_xfer_request_accepted(xfer, fileName.c_str());
 
				purple_xfer_ui_ready(xfer);
 
			}
 
		}
 

	
 
		void handleFTFinishRequest(const std::string &user, const std::string &buddyName, const std::string &fileName, unsigned long size, unsigned long ftID) {
 
			PurpleXfer *xfer = m_unhandledXfers[user + fileName + buddyName];
 
			if (xfer) {
 
				m_unhandledXfers.erase(user + fileName + buddyName);
 
				purple_xfer_request_denied(xfer);
 
			}
 
		}
 

	
 
		void handleFTPauseRequest(unsigned long ftID) {
 
			PurpleXfer *xfer = m_xfers[ftID];
 
			if (!xfer)
 
				return;
 
			FTData *ftData = (FTData *) xfer->ui_data;
 
			ftData->paused = true;
 
		}
 

	
 
		void handleFTContinueRequest(unsigned long ftID) {
 
			PurpleXfer *xfer = m_xfers[ftID];
 
			if (!xfer)
 
				return;
 
			FTData *ftData = (FTData *) xfer->ui_data;
 
			ftData->paused = false;
 
			purple_xfer_ui_ready(xfer);
 
		}
 

	
 
		void sendData(const std::string &string) {
 
			write(m_sock, string.c_str(), string.size());
 
		}
 

	
 
		void readyForData() {
 
			if (m_waitingXfers.empty())
 
				return;
 
			std::vector<PurpleXfer *> tmp;
 
			tmp.swap(m_waitingXfers);
 

	
 
			BOOST_FOREACH(PurpleXfer *xfer, tmp) {
 
				FTData *ftData = (FTData *) xfer->ui_data;
 
			for (std::vector<PurpleXfer *>::const_iterator it = tmp.begin(); it != tmp.end(); it++) {
 
				FTData *ftData = (FTData *) (*it)->ui_data;
 
				if (ftData->timer == 0) {
 
					ftData->timer = purple_timeout_add(1, ft_ui_ready, xfer);
 
					ftData->timer = purple_timeout_add(1, ft_ui_ready, *it);
 
				}
 
// 				purple_xfer_ui_ready(xfer);
 
			}
 
		}
 

	
 
		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;
 
		std::map<unsigned long, PurpleXfer *> m_xfers;
 
		std::map<std::string, PurpleXfer *> m_unhandledXfers;
 
		std::vector<PurpleXfer *> m_waitingXfers;
 
		Config *config;
 
};
 

	
 
static bool getStatus(PurpleBuddy *m_buddy, Swift::StatusShow &status, std::string &statusMessage) {
 
static bool getStatus(PurpleBuddy *m_buddy, pbnetwork::StatusType &status, std::string &statusMessage) {
 
	PurplePresence *pres = purple_buddy_get_presence(m_buddy);
 
	if (pres == NULL)
 
		return false;
 
	PurpleStatus *stat = purple_presence_get_active_status(pres);
 
	if (stat == NULL)
 
		return false;
 
	int st = purple_status_type_get_primitive(purple_status_get_type(stat));
 

	
 
	switch(st) {
 
		case PURPLE_STATUS_AVAILABLE: {
 
			status = pbnetwork::STATUS_ONLINE;
 
			break;
 
		}
 
		case PURPLE_STATUS_AWAY: {
 
			status = Swift::StatusShow::Away;
 
			status = pbnetwork::STATUS_AWAY;
 
			break;
 
		}
 
		case PURPLE_STATUS_UNAVAILABLE: {
 
			status = Swift::StatusShow::DND;
 
			status = pbnetwork::STATUS_DND;
 
			break;
 
		}
 
		case PURPLE_STATUS_EXTENDED_AWAY: {
 
			status = Swift::StatusShow::XA;
 
			status = pbnetwork::STATUS_XA;
 
			break;
 
		}
 
		case PURPLE_STATUS_OFFLINE: {
 
			status = Swift::StatusShow::None;
 
			status = pbnetwork::STATUS_NONE;
 
			break;
 
		}
 
		default:
 
			status = pbnetwork::STATUS_ONLINE;
 
			break;
 
	}
 

	
 
	const char *message = purple_status_get_attr_string(stat, "message");
 

	
 
	if (message != NULL) {
 
		char *stripped = purple_markup_strip_html(message);
 
		statusMessage = std::string(stripped);
 
		g_free(stripped);
 
	}
 
	else
 
		statusMessage = "";
 
	return true;
 
}
 

	
 
static std::string getIconHash(PurpleBuddy *m_buddy) {
 
	char *avatarHash = NULL;
 
	PurpleBuddyIcon *icon = purple_buddy_icons_find(purple_buddy_get_account(m_buddy), purple_buddy_get_name(m_buddy));
 
	if (icon) {
 
		avatarHash = purple_buddy_icon_get_full_path(icon);
 
		purple_buddy_icon_unref(icon);
 
	}
 

	
 
	if (avatarHash) {
 
		// Check if it's patched libpurple which saves icons to directories
 
		char *hash = strrchr(avatarHash,'/');
 
		std::string h;
 
		if (hash) {
 
			char *dot;
 
			hash++;
 
			dot = strchr(hash, '.');
 
			if (dot)
 
				*dot = '\0';
 

	
 
			std::string ret(hash);
 
			g_free(avatarHash);
 
			return ret;
 
		}
 
		else {
 
			std::string ret(avatarHash);
 
			g_free(avatarHash);
 
			return ret;
 
		}
 
	}
 

	
 
	return "";
 
}
 

	
 
static std::vector<std::string> getGroups(PurpleBuddy *m_buddy) {
 
	std::vector<std::string> groups;
 
	groups.push_back((purple_buddy_get_group(m_buddy) && purple_group_get_name(purple_buddy_get_group(m_buddy))) ? std::string(purple_group_get_name(purple_buddy_get_group(m_buddy))) : std::string("Buddies"));
 
	return groups;
 
}
 

	
 
static void buddyListNewNode(PurpleBlistNode *node) {
 
	if (!PURPLE_BLIST_NODE_IS_BUDDY(node))
 
		return;
 
	PurpleBuddy *buddy = (PurpleBuddy *) node;
 
	PurpleAccount *account = purple_buddy_get_account(buddy);
 

	
 
	// Status
 
	Swift::StatusShow status;
 
	pbnetwork::StatusType status;
 
	std::string message;
 
	getStatus(buddy, status, message);
 

	
 
	// Tooltip
 
	PurplePlugin *prpl = purple_find_prpl(purple_account_get_protocol_id(account));
 
	PurplePluginProtocolInfo *prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
 

	
 
	bool blocked = false;
 
	if (CONFIG_BOOL(np->config, "service.enable_privacy_lists")) {
 
	if (KEYFILE_BOOL("service", "enable_privacy_lists")) {
 
		if (prpl_info && prpl_info->tooltip_text) {
 
			PurpleNotifyUserInfo *user_info = purple_notify_user_info_new();
 
			prpl_info->tooltip_text(buddy, user_info, true);
 
			GList *entries = purple_notify_user_info_get_entries(user_info);
 

	
 
			while (entries) {
 
				PurpleNotifyUserInfoEntry *entry = (PurpleNotifyUserInfoEntry *)(entries->data);
 
				if (purple_notify_user_info_entry_get_label(entry) && purple_notify_user_info_entry_get_value(entry)) {
 
					std::string label = purple_notify_user_info_entry_get_label(entry);
 
					if (label == "Blocked" ) {
 
						if (std::string(purple_notify_user_info_entry_get_value(entry)) == "Yes") {
 
							blocked = true;
 
							break;
 
						}
 
					}
 
				}
 
				entries = entries->next;
 
			}
 
			purple_notify_user_info_destroy(user_info);
 
		}
 

	
 
		if (!blocked) {
 
			blocked = purple_privacy_check(account, purple_buddy_get_name(buddy)) == false;
 
		}
 
		else {
 
			bool purpleBlocked = purple_privacy_check(account, purple_buddy_get_name(buddy)) == false;
 
			if (blocked != purpleBlocked) {
 
				purple_privacy_deny(account, purple_buddy_get_name(buddy), FALSE, FALSE);
 
			}
 
		}
 
	}
 

	
 
	std::cout << "BLOCKED?" << (purple_privacy_check(account, purple_buddy_get_name(buddy)) == false) << "\n";
 
	np->handleBuddyChanged(np->m_accounts[account], purple_buddy_get_name(buddy), getAlias(buddy), getGroups(buddy)[0], status.getType(), message, getIconHash(buddy),
 
	np->handleBuddyChanged(np->m_accounts[account], purple_buddy_get_name(buddy), getAlias(buddy), getGroups(buddy)[0], status, message, getIconHash(buddy),
 
		blocked
 
	);
 
}
 

	
 
static void buddyListUpdate(PurpleBuddyList *list, PurpleBlistNode *node) {
 
	if (!PURPLE_BLIST_NODE_IS_BUDDY(node))
 
		return;
 
	buddyListNewNode(node);
 
}
 

	
 
static void buddyPrivacyChanged(PurpleBlistNode *node, void *data) {
 
	std::cout << "PRIVACY CHANGED\n";
 
	if (!PURPLE_BLIST_NODE_IS_BUDDY(node))
 
		return;
 
	buddyListUpdate(NULL, node);
 
}
 

	
 
static void NodeRemoved(PurpleBlistNode *node, void *data) {
 
	if (!PURPLE_BLIST_NODE_IS_BUDDY(node))
 
		return;
 
// 	PurpleBuddy *buddy = (PurpleBuddy *) node;
 
}
 

	
 
static PurpleBlistUiOps blistUiOps =
 
{
 
	NULL,
 
	buddyListNewNode,
 
	NULL,
 
	buddyListUpdate,
 
	NULL, //NodeRemoved,
 
	NULL,
 
	NULL,
 
	NULL, // buddyListAddBuddy,
 
	NULL,
 
	NULL,
 
	NULL, //buddyListSaveNode,
 
	NULL, //buddyListRemoveNode,
 
	NULL, //buddyListSaveAccount,
 
	NULL
 
};
 

	
 
static void conv_write_im(PurpleConversation *conv, const char *who, const char *msg, PurpleMessageFlags flags, time_t mtime) {
 
	// Don't forwards our own messages.
 
	if (flags & PURPLE_MESSAGE_SEND || flags & PURPLE_MESSAGE_SYSTEM)
 
		return;
 
	PurpleAccount *account = purple_conversation_get_account(conv);
 

	
 
// 	char *striped = purple_markup_strip_html(message);
 
@@ -1152,179 +1181,179 @@ static gboolean disconnectMe(void *data) {
 
	if (account) {
 
		np->handleLogoutRequest(np->m_accounts[account], purple_account_get_username(account));
 
	}
 
	return FALSE;
 
}
 

	
 
static void connection_report_disconnect(PurpleConnection *gc, PurpleConnectionError reason, const char *text){
 
	PurpleAccount *account = purple_connection_get_account(gc);
 
	np->handleDisconnected(np->m_accounts[account], (int) reason, text ? text : "");
 
	Dis *d = new Dis;
 
	d->name = purple_account_get_username(account);
 
	d->protocol = purple_account_get_protocol_id(account);
 
	purple_timeout_add_seconds(10, disconnectMe, d);
 
}
 

	
 
static PurpleConnectionUiOps conn_ui_ops =
 
{
 
	NULL,
 
	NULL,
 
	NULL,//connection_disconnected,
 
	NULL,
 
	NULL,
 
	NULL,
 
	NULL,
 
	connection_report_disconnect,
 
	NULL,
 
	NULL,
 
	NULL
 
};
 

	
 
static void *notify_user_info(PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info) {
 
	PurpleAccount *account = purple_connection_get_account(gc);
 
	std::string name(purple_normalize(account, who));
 
	std::transform(name.begin(), name.end(), name.begin(),(int(*)(int)) std::tolower);
 

	
 
	size_t pos = name.find("/");
 
	if (pos != std::string::npos)
 
		name.erase((int) pos, name.length() - (int) pos);
 

	
 
	
 
	GList *vcardEntries = purple_notify_user_info_get_entries(user_info);
 
	PurpleNotifyUserInfoEntry *vcardEntry;
 
	std::string firstName;
 
	std::string lastName;
 
	std::string fullName;
 
	std::string nickname;
 
	std::string header;
 
	std::string label;
 
	Swift::ByteArray photo;
 
	std::string photo;
 

	
 
	while (vcardEntries) {
 
		vcardEntry = (PurpleNotifyUserInfoEntry *)(vcardEntries->data);
 
		if (purple_notify_user_info_entry_get_label(vcardEntry) && purple_notify_user_info_entry_get_value(vcardEntry)){
 
			label = purple_notify_user_info_entry_get_label(vcardEntry);
 
			if (label == "Given Name" || label == "First Name") {
 
				firstName = purple_notify_user_info_entry_get_value(vcardEntry);
 
			}
 
			else if (label == "Family Name" || label == "Last Name") {
 
				lastName = purple_notify_user_info_entry_get_value(vcardEntry);
 
			}
 
			else if (label=="Nickname" || label == "Nick") {
 
				nickname = purple_notify_user_info_entry_get_value(vcardEntry);
 
			}
 
			else if (label=="Full Name") {
 
				fullName = purple_notify_user_info_entry_get_value(vcardEntry);
 
			}
 
			else {
 
				LOG4CXX_WARN(logger, "Unhandled VCard Label '" << purple_notify_user_info_entry_get_label(vcardEntry) << "' " << purple_notify_user_info_entry_get_value(vcardEntry));
 
			}
 
		}
 
		vcardEntries = vcardEntries->next;
 
	}
 

	
 
	if ((!firstName.empty() || !lastName.empty()) && fullName.empty())
 
		fullName = firstName + " " + lastName;
 

	
 
	if (nickname.empty() && !fullName.empty()) {
 
		nickname = fullName;
 
	}
 

	
 
	bool ownInfo = name == purple_account_get_username(account);
 
	std::cout << "RECEIVED " << name << " " << purple_account_get_username(account) << "\n";
 

	
 
	if (ownInfo) {
 
		const gchar *displayname = purple_connection_get_display_name(gc);
 
		if (!displayname) {
 
			displayname = purple_account_get_name_for_display(account);
 
		}
 

	
 
		if (displayname && nickname.empty()) {
 
			nickname = displayname;
 
		}
 

	
 
		// avatar
 
		PurpleStoredImage *avatar = purple_buddy_icons_find_account_icon(account);
 
		if (avatar) {
 
			const gchar * data = (const gchar *) purple_imgstore_get_data(avatar);
 
			size_t len = purple_imgstore_get_size(avatar);
 
			if (len < 300000 && data) {
 
				photo = Swift::createByteArray(data, len);
 
				photo = std::string(data, len);
 
			}
 
			purple_imgstore_unref(avatar);
 
		}
 
	}
 

	
 
	PurpleBuddy *buddy = purple_find_buddy(purple_connection_get_account(gc), who);
 
	if (buddy && photo.size() == 0) {
 
		gsize len;
 
		PurpleBuddyIcon *icon = NULL;
 
		icon = purple_buddy_icons_find(purple_connection_get_account(gc), name.c_str());
 
		if (icon) {
 
			if (true) {
 
				gchar *data;
 
				gchar *path = purple_buddy_icon_get_full_path(icon);
 
				if (g_file_get_contents (path, &data, &len, NULL)) {
 
					photo = Swift::createByteArray(data, len);
 
					photo = std::string(data, len);
 
					free(data);
 
				}
 
				free(path);
 
			}
 
			else {
 
				const gchar * data = (gchar*)purple_buddy_icon_get_data(icon, &len);
 
				if (len < 300000 && data) {
 
					photo = Swift::createByteArray(data, len);
 
					photo = std::string(data, len);
 
				}
 
			}
 
			purple_buddy_icon_unref(icon);
 
		}
 
	}
 

	
 
	np->handleVCard(np->m_accounts[account], np->m_vcards[np->m_accounts[account] + name], name, fullName, nickname, Swift::byteArrayToString(photo));
 
	np->handleVCard(np->m_accounts[account], np->m_vcards[np->m_accounts[account] + name], name, fullName, nickname, photo);
 
	np->m_vcards.erase(np->m_accounts[account] + name);
 

	
 
	return NULL;
 
}
 

	
 
static PurpleNotifyUiOps notifyUiOps =
 
{
 
		NULL,
 
		NULL,
 
		NULL,
 
		NULL,
 
		NULL,
 
		NULL,
 
		notify_user_info,
 
		NULL,
 
		NULL,
 
		NULL,
 
		NULL,
 
		NULL,
 
		NULL
 
};
 

	
 
static PurpleRequestUiOps requestUiOps =
 
{
 
	requestInput,
 
	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);
 
@@ -1535,273 +1564,288 @@ static void printDebug(PurpleDebugLevel level, const char *category, const char
 
	LOG4CXX_INFO(logger_libpurple, c << args);
 
}
 

	
 
/*
 
 * Ops....
 
 */
 
static PurpleDebugUiOps debugUiOps =
 
{
 
	printDebug,
 
	NULL,
 
	NULL,
 
	NULL,
 
	NULL,
 
	NULL
 
};
 

	
 
static void buddyTyping(PurpleAccount *account, const char *who, gpointer null) {
 
	std::string w = purple_normalize(account, who);
 
	size_t pos = w.find("/");
 
	if (pos != std::string::npos)
 
		w.erase((int) pos, w.length() - (int) pos);
 
	np->handleBuddyTyping(np->m_accounts[account], w);
 
}
 

	
 
static void buddyTyped(PurpleAccount *account, const char *who, gpointer null) {
 
	std::string w = purple_normalize(account, who);
 
	size_t pos = w.find("/");
 
	if (pos != std::string::npos)
 
		w.erase((int) pos, w.length() - (int) pos);
 
	np->handleBuddyTyped(np->m_accounts[account], w);
 
}
 

	
 
static void buddyTypingStopped(PurpleAccount *account, const char *who, gpointer null){
 
	std::string w = purple_normalize(account, who);
 
	size_t pos = w.find("/");
 
	if (pos != std::string::npos)
 
		w.erase((int) pos, w.length() - (int) pos);
 
	np->handleBuddyStoppedTyping(np->m_accounts[account], w);
 
}
 

	
 
static void gotAttention(PurpleAccount *account, const char *who, PurpleConversation *conv, guint type) {
 
	std::string w = purple_normalize(account, who);
 
	size_t pos = w.find("/");
 
	if (pos != std::string::npos)
 
		w.erase((int) pos, w.length() - (int) pos);
 
	np->handleAttention(np->m_accounts[account], w, "");
 
}
 

	
 
static bool initPurple(Config &cfg) {
 
static bool initPurple() {
 
	bool ret;
 

	
 
	purple_util_set_user_dir("./");
 
	remove("./accounts.xml");
 
	remove("./blist.xml");
 

	
 
// 	if (m_configuration.logAreas & LOG_AREA_PURPLE)
 
		purple_debug_set_ui_ops(&debugUiOps);
 
		purple_debug_set_verbose(true);
 

	
 
	purple_core_set_ui_ops(&coreUiOps);
 
	std::map<std::string, std::string> unregistered = cfg.getUnregistered();
 
	if (unregistered.find("service.eventloop") != unregistered.end()) {
 
		if (unregistered["service.eventloop"] == "libev") {
 
	if (KEYFILE_STRING("service", "eventloop") == "libev") {
 
		LOG4CXX_INFO(logger, "Will use libev based event loop");
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, "Will use glib based event loop");
 
	}
 
		purple_eventloop_set_ui_ops(getEventLoopUiOps(unregistered["service.eventloop"] == "libev"));
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, "Will use glib based event loop");
 
		purple_eventloop_set_ui_ops(getEventLoopUiOps(false));
 
	}
 
	purple_eventloop_set_ui_ops(getEventLoopUiOps(KEYFILE_STRING("service", "eventloop") == "libev"));
 

	
 
	ret = purple_core_init("spectrum");
 
	if (ret) {
 
		static int blist_handle;
 
		static int conversation_handle;
 

	
 
		purple_set_blist(purple_blist_new());
 
		purple_blist_load();
 

	
 
		purple_prefs_load();
 

	
 
		/* Good default preferences */
 
		/* The combination of these two settings mean that libpurple will never
 
		 * (of its own accord) set all the user accounts idle.
 
		 */
 
		purple_prefs_set_bool("/purple/away/away_when_idle", false);
 
		/*
 
		 * This must be set to something not "none" for idle reporting to work
 
		 * for, e.g., the OSCAR prpl. We don't implement the UI ops, so this is
 
		 * okay for now.
 
		 */
 
		purple_prefs_set_string("/purple/away/idle_reporting", "system");
 

	
 
		/* Disable all logging */
 
		purple_prefs_set_bool("/purple/logging/log_ims", false);
 
		purple_prefs_set_bool("/purple/logging/log_chats", false);
 
		purple_prefs_set_bool("/purple/logging/log_system", false);
 

	
 

	
 
// 		purple_signal_connect(purple_conversations_get_handle(), "received-im-msg", &conversation_handle, PURPLE_CALLBACK(newMessageReceived), NULL);
 
		purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", &conversation_handle, PURPLE_CALLBACK(buddyTyping), NULL);
 
		purple_signal_connect(purple_conversations_get_handle(), "buddy-typed", &conversation_handle, PURPLE_CALLBACK(buddyTyped), NULL);
 
		purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", &conversation_handle, PURPLE_CALLBACK(buddyTypingStopped), NULL);
 
		purple_signal_connect(purple_blist_get_handle(), "buddy-privacy-changed", &conversation_handle, PURPLE_CALLBACK(buddyPrivacyChanged), NULL);
 
		purple_signal_connect(purple_conversations_get_handle(), "got-attention", &conversation_handle, PURPLE_CALLBACK(gotAttention), NULL);
 
		purple_signal_connect(purple_connections_get_handle(), "signed-on", &blist_handle,PURPLE_CALLBACK(signed_on), NULL);
 
// 		purple_signal_connect(purple_blist_get_handle(), "buddy-removed", &blist_handle,PURPLE_CALLBACK(buddyRemoved), NULL);
 
// 		purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", &blist_handle,PURPLE_CALLBACK(buddySignedOn), NULL);
 
// 		purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", &blist_handle,PURPLE_CALLBACK(buddySignedOff), NULL);
 
// 		purple_signal_connect(purple_blist_get_handle(), "buddy-status-changed", &blist_handle,PURPLE_CALLBACK(buddyStatusChanged), NULL);
 
		purple_signal_connect(purple_blist_get_handle(), "blist-node-removed", &blist_handle,PURPLE_CALLBACK(NodeRemoved), NULL);
 
// 		purple_signal_connect(purple_conversations_get_handle(), "chat-topic-changed", &conversation_handle, PURPLE_CALLBACK(conv_chat_topic_changed), NULL);
 
		static int xfer_handle;
 
		purple_signal_connect(purple_xfers_get_handle(), "file-send-start", &xfer_handle, PURPLE_CALLBACK(fileSendStart), NULL);
 
		purple_signal_connect(purple_xfers_get_handle(), "file-recv-start", &xfer_handle, PURPLE_CALLBACK(fileRecvStart), NULL);
 
		purple_signal_connect(purple_xfers_get_handle(), "file-recv-request", &xfer_handle, PURPLE_CALLBACK(newXfer), NULL);
 
		purple_signal_connect(purple_xfers_get_handle(), "file-recv-complete", &xfer_handle, PURPLE_CALLBACK(XferReceiveComplete), NULL);
 
		purple_signal_connect(purple_xfers_get_handle(), "file-send-complete", &xfer_handle, PURPLE_CALLBACK(XferSendComplete), NULL);
 
// 
 
// 		purple_commands_init();
 

	
 
	}
 
	return ret;
 
}
 

	
 
static void spectrum_sigchld_handler(int sig)
 
{
 
	int status;
 
	pid_t pid;
 

	
 
	do {
 
		pid = waitpid(-1, &status, WNOHANG);
 
	} while (pid != 0 && pid != (pid_t)-1);
 

	
 
	if ((pid == (pid_t) - 1) && (errno != ECHILD)) {
 
		char errmsg[BUFSIZ];
 
		snprintf(errmsg, BUFSIZ, "Warning: waitpid() returned %d", pid);
 
		perror(errmsg);
 
	}
 
}
 

	
 
static void transportDataReceived(gpointer data, gint source, PurpleInputCondition cond) {
 
	char buffer[65535];
 
	char *ptr = buffer;
 
	ssize_t n = read(source, ptr, sizeof(buffer));
 
	Swift::SafeByteArray d = Swift::createSafeByteArray(buffer, n);
 
	if (n <= 0) {
 
		LOG4CXX_INFO(logger, "Diconnecting from spectrum2 server");
 
		exit(errno);
 
	}
 
	std::string d = std::string(buffer, n);
 
	np->handleDataRead(d);
 
}
 

	
 
int main(int argc, char **argv) {
 
	Swift::logging = true;
 
	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)) {
 
		std::cout << "option parsing failed: " << error->message << "\n";
 
		return -1;
 
	}
 

	
 
	if (ver) {
 
// 		std::cout << VERSION << "\n";
 
		std::cout << "verze\n";
 
		g_option_context_free(context);
 
		return 0;
 
	}
 

	
 
	if (argc != 2) {
 
#ifdef WIN32
 
		std::cout << "Usage: spectrum.exe <configuration_file.cfg>\n";
 
#else
 

	
 
#if GLIB_CHECK_VERSION(2,14,0)
 
	std::cout << g_option_context_get_help(context, FALSE, NULL);
 
#else
 
	std::cout << "Usage: spectrum <configuration_file.cfg>\n";
 
	std::cout << "See \"man spectrum\" for more info.\n";
 
#endif
 
		
 
#endif
 
	}
 
	else {
 
#ifndef WIN32
 
		signal(SIGPIPE, SIG_IGN);
 

	
 
		if (signal(SIGCHLD, spectrum_sigchld_handler) == SIG_ERR) {
 
			std::cout << "SIGCHLD handler can't be set\n";
 
			g_option_context_free(context);
 
			return -1;
 
		}
 
// 
 
// 		if (signal(SIGINT, spectrum_sigint_handler) == SIG_ERR) {
 
// 			std::cout << "SIGINT handler can't be set\n";
 
// 			g_option_context_free(context);
 
// 			return -1;
 
// 		}
 
// 
 
// 		if (signal(SIGTERM, spectrum_sigterm_handler) == SIG_ERR) {
 
// 			std::cout << "SIGTERM handler can't be set\n";
 
// 			g_option_context_free(context);
 
// 			return -1;
 
// 		}
 
// 
 
// 		struct sigaction sa;
 
// 		memset(&sa, 0, sizeof(sa)); 
 
// 		sa.sa_handler = spectrum_sighup_handler;
 
// 		if (sigaction(SIGHUP, &sa, NULL)) {
 
// 			std::cout << "SIGHUP handler can't be set\n";
 
// 			g_option_context_free(context);
 
// 			return -1;
 
//		}
 
#endif
 
		Config config;
 
		if (!config.load(argv[1])) {
 
		keyfile = g_key_file_new ();
 
		if (!g_key_file_load_from_file (keyfile, argv[1], (GKeyFileFlags) 0, 0)) {
 
			std::cout << "Can't open " << argv[1] << " configuration file.\n";
 
			return 1;
 
		}
 

	
 
		if (CONFIG_STRING(&config, "logging.backend_config").empty()) {
 
		if (KEYFILE_STRING("logging", "backend_config").empty()) {
 
			LoggerPtr root = log4cxx::Logger::getRootLogger();
 
			root->addAppender(new ConsoleAppender(new PatternLayout("%d %-5p %c: %m%n")));
 
		}
 
		else {
 
			log4cxx::helpers::Properties p;
 
			log4cxx::helpers::FileInputStream *istream = new log4cxx::helpers::FileInputStream(CONFIG_STRING(&config, "logging.backend_config"));
 
			log4cxx::helpers::FileInputStream *istream = new log4cxx::helpers::FileInputStream(KEYFILE_STRING("logging", "backend_config"));
 

	
 
			p.load(istream);
 
			p.setProperty("pid", boost::lexical_cast<std::string>(getpid()));
 
			p.setProperty("jid", CONFIG_STRING(&config, "service.jid"));
 
			p.setProperty("pid", stringOf(getpid()));
 
			p.setProperty("jid", KEYFILE_STRING("service", "jid"));
 
			log4cxx::PropertyConfigurator::configure(p);
 
		}
 

	
 
		initPurple(config);
 
		initPurple();
 

	
 
		int portno = port;
 
		struct sockaddr_in serv_addr;
 

	
 
		m_sock = socket(AF_INET, SOCK_STREAM, 0);
 
		memset((char *) &serv_addr, 0, sizeof(serv_addr));
 
		serv_addr.sin_family = AF_INET;
 
		serv_addr.sin_port = htons(portno);
 

	
 
		hostent *hos;  // Resolve name
 
		if ((hos = gethostbyname(host)) == NULL) {
 
			// strerror() will not work for gethostbyname() and hstrerror() 
 
			// is supposedly obsolete
 
			exit(1);
 
		}
 
		serv_addr.sin_addr.s_addr = *((unsigned long *) hos->h_addr_list[0]);
 

	
 
		if (connect(m_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
 
			close(m_sock);
 
			m_sock = 0;
 
		}
 

	
 
		int flags = fcntl(m_sock, F_GETFL);
 
		flags |= O_NONBLOCK;
 
		fcntl(m_sock, F_SETFL, flags);
 

	
 
		purple_input_add(m_sock, PURPLE_INPUT_READ, &transportDataReceived, NULL);
 

	
 
		std::map<std::string, std::string> unregistered = config.getUnregistered();
 
		SpectrumEventLoop eventLoop(unregistered["service.eventloop"] == "libev");
 
		np = new SpectrumNetworkPlugin(&config, &eventLoop, host, port);
 
		eventLoop.run();
 
		np = new SpectrumNetworkPlugin(host, port);
 
		bool libev = KEYFILE_STRING("service", "eventloop") == "libev";
 

	
 
		GMainLoop *m_loop;
 
#ifdef WITH_LIBEVENT
 
		if (!libev) {
 
			m_loop = g_main_loop_new(NULL, FALSE);
 
		}
 
		else {
 
			event_init();
 
		}
 
#endif
 
		m_loop = g_main_loop_new(NULL, FALSE);
 

	
 
		if (m_loop) {
 
			g_main_loop_run(m_loop);
 
		}
 
#ifdef WITH_LIBEVENT
 
		else {
 
			event_loop(0);
 
		}
 
#endif
 
	}
 

	
 
	g_option_context_free(context);
 
	return 0;
 
}
backends/libpurple/spectrumeventloop.cpp
Show inline comments
 
deleted file
backends/libpurple/spectrumeventloop.h
Show inline comments
 
deleted file
cmake_modules/ProtobufConfig.cmake
Show inline comments
 
@@ -18,102 +18,102 @@
 
#   include_directories(${PROTOBUF_INCLUDE_DIRS})
 
#
 
#   include_directories(${CMAKE_CURRENT_BINARY_DIR})
 
#   PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
 
#   add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
 
#   target_link_libraries(bar ${PROTOBUF_LIBRARY})
 
#
 
# NOTE: You may need to link against pthreads, depending
 
# on the platform.
 
#  ====================================================================
 
#
 
# PROTOBUF_GENERATE_CPP (public function)
 
#   SRCS = Variable to define with autogenerated
 
#          source files
 
#   HDRS = Variable to define with autogenerated
 
#          header files
 
#   ARGN = proto files
 
#
 
#  ====================================================================
 

	
 

	
 
#=============================================================================
 
# Copyright 2009 Kitware, Inc.
 
# Copyright 2009 Philip Lowman <philip@yhbt.com>
 
# Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
 
#
 
# Distributed under the OSI-approved BSD License (the "License");
 
# see accompanying file Copyright.txt for details.
 
#
 
# This software is distributed WITHOUT ANY WARRANTY; without even the
 
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
# See the License for more information.
 
#=============================================================================
 
# (To distributed this file outside of CMake, substitute the full
 
#  License text for the above reference.)
 

	
 
function(PROTOBUF_GENERATE_CPP SRCS HDRS)
 
  if(NOT ARGN)
 
    message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
 
    return()
 
  endif(NOT ARGN)
 

	
 
  set(${SRCS})
 
  set(${HDRS})
 
  foreach(FIL ${ARGN})
 
    get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
 
    get_filename_component(FIL_WE ${FIL} NAME_WE)
 
    
 
    list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
 
    list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
 
    list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.cpp")
 
    list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.h")
 

	
 
    add_custom_command(
 
      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
 
             "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
 
    add_custom_target(pb
 
      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.cpp"
 
             "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.h"
 
      COMMAND  ${PROTOBUF_PROTOC_EXECUTABLE}
 
      ARGS --cpp_out  ${CMAKE_CURRENT_BINARY_DIR} --proto_path ${CMAKE_CURRENT_SOURCE_DIR} ${ABS_FIL}
 
      DEPENDS ${ABS_FIL}
 
      COMMENT "Running C++ protocol buffer compiler on ${FIL}"
 
      VERBATIM )
 
  endforeach()
 

	
 
  set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
 
  set(${SRCS} ${${SRCS}} PARENT_SCOPE)
 
  set(${HDRS} ${${HDRS}} PARENT_SCOPE)
 
endfunction()
 

	
 
function(PROTOBUF_GENERATE_PY SRCS HDRS)
 
  if(NOT ARGN)
 
    message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
 
    return()
 
  endif(NOT ARGN)
 

	
 
  set(${SRCS})
 
  set(${HDRS})
 
  foreach(FIL ${ARGN})
 
    get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
 
    get_filename_component(FIL_WE ${FIL} NAME_WE)
 
    
 
    list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
 
    list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
 

	
 
    add_custom_command(
 
      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
 
             "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
 
      COMMAND  ${PROTOBUF_PROTOC_EXECUTABLE}
 
      ARGS --python_out  ${CMAKE_CURRENT_BINARY_DIR} --proto_path ${CMAKE_CURRENT_SOURCE_DIR} ${ABS_FIL}
 
      DEPENDS ${ABS_FIL}
 
      COMMENT "Running Py protocol buffer compiler on ${FIL}"
 
      VERBATIM )
 
  endforeach()
 

	
 
  set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
 
  set(${SRCS} ${${SRCS}} PARENT_SCOPE)
 
  set(${HDRS} ${${HDRS}} PARENT_SCOPE)
 
endfunction()
 

	
 

	
 
find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h)
 

	
 
# Google's provided vcproj files generate libraries with a "lib"
 
# prefix on Windows
 
if(WIN32)
include/transport/CMakeLists.txt
Show inline comments
 
FILE(GLOB HEADERS *.h)
 
if (PROTOBUF_FOUND)
 
	add_custom_target(pb
 
		${PROTOBUF_PROTOC_EXECUTABLE}
 
		--cpp_out  ${CMAKE_CURRENT_BINARY_DIR} --proto_path ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/protocol.proto
 
		COMMENT "Running C++ protocol buffer compiler on protocol.proto"
 
		DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/protocol.proto
 
		VERBATIM )
 
endif()
 

	
 
FILE(GLOB HEADERS *.h protocol.h)
 

	
 
INSTALL(FILES ${HEADERS} DESTINATION include/transport COMPONENT headers)
 
\ No newline at end of file
include/transport/memoryusage.h
Show inline comments
 
file renamed from src/memoryusage.h to include/transport/memoryusage.h
include/transport/networkplugin.h
Show inline comments
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * 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 <time.h>
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Presence/PresenceOracle.h"
 
#include "Swiften/Disco/EntityCapsManager.h"
 
#include "Swiften/Network/ConnectionServer.h"
 
#include "Swiften/Network/Connection.h"
 
#include "Swiften/Network/BoostTimerFactory.h"
 
#include "Swiften/Network/BoostNetworkFactories.h"
 
#include "Swiften/Network/BoostIOServiceThread.h"
 
#include "Swiften/Network/Connection.h"
 
#include "storagebackend.h"
 
#include "conversation.h"
 
#include "transport/protocol.pb.h"
 
// #include "conversation.h"
 
#include <iostream>
 

	
 
namespace Transport {
 

	
 
/// Represents Spectrum2 legacy network plugin.
 

	
 
/// This class is base class for all C++ legacy network plugins. It provides a way to connect 
 
/// Spectrum2 NetworkPluginServer and allows to use high-level API for legacy network plugins
 
/// development.
 
class NetworkPlugin {
 
	public:
 
		/// Creates new NetworkPlugin and connects the Spectrum2 NetworkPluginServer.
 
		/// \param loop Event loop.
 
		/// \param host Host where Spectrum2 NetworkPluginServer runs.
 
		/// \param port Port.
 
		NetworkPlugin(Swift::EventLoop *loop, const std::string &host, int port);
 
		NetworkPlugin();
 

	
 
		/// Destructor.
 
		virtual ~NetworkPlugin();
 

	
 
		virtual void readyForData() {}
 

	
 
		/// Call this function when legacy network buddy changed.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		/// \param buddyName Name of legacy network buddy. (eg. "user2@gmail.com")
 
		/// \param alias Alias of legacy network buddy. If empty, then it's not changed on XMPP side.
 
		/// \param groups Groups in which buddy currently is. If empty, then it's not changed on XMPP side.
 
		/// \param status Status of this buddy.
 
		/// \param statusMessage Status message of this buddy.
 
		/// \param iconHash MD5 hash of buddy icon. Empty if none buddy icon.
 
		/// \param blocked True if this buddy is blocked in privacy lists in legacy network.
 
		void handleBuddyChanged(const std::string &user, const std::string &buddyName, const std::string &alias,
 
			const std::string &groups, Swift::StatusShow::Type status, const std::string &statusMessage = "", const std::string &iconHash = "",
 
			const std::string &groups, pbnetwork::StatusType status, const std::string &statusMessage = "", const std::string &iconHash = "",
 
			bool blocked = false
 
		);
 

	
 
		/// Call this function when participant in room changed.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		/// \param nickname Nickname of participant. If participant renamed, this is old name of participant. (eg. "HanzZ")
 
		/// \param room Room in which participant changed. (eg. #spectrum)
 
		/// \param flags Participant flags.
 
		/// \param status Current status of participant. Swift::StatusShow::None if participant left the room.
 
		/// \param statusMessage Current status message of participant.
 
		/// \param newname New name of participant if he changed the nickname. Otherwise empty.
 
		void handleParticipantChanged(const std::string &user, const std::string &nickname, const std::string &room, Conversation::ParticipantFlag flags = Conversation::None,
 
			Swift::StatusShow::Type status = Swift::StatusShow::None, const std::string &statusMessage = "", const std::string &newname = "");
 
		void handleParticipantChanged(const std::string &user, const std::string &nickname, const std::string &room, int flags,
 
			pbnetwork::StatusType = pbnetwork::STATUS_NONE, const std::string &statusMessage = "", const std::string &newname = "");
 

	
 
		/// Call this function when user disconnected the legacy network because of some legacy network error.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		/// \param error Reserved for future use, currently keep it on 0.
 
		/// \param message XMPP message which is sent to XMPP user.
 
		void handleDisconnected(const std::string &user, int error = 0, const std::string &message = "");
 

	
 
		/// Call this function when user connected the legacy network and is logged in.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		void handleConnected(const std::string &user);
 

	
 
		/// Call this function when new message is received from legacy network for user.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		/// \param legacyName Name of legacy network buddy or name of room. (eg. "user2@gmail.com")
 
		/// \param message Plain text message.
 
		/// \param nickname Nickname of buddy in room. Empty if it's normal chat message.
 
		/// \param xhtml XHTML message.
 
		void handleMessage(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &nickname = "", const std::string &xhtml = "");
 

	
 
		/// Call this function when subject in room changed.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		/// \param legacyName Name of room. (eg. "#spectrum")
 
		/// \param message Subject message.
 
		/// \param nickname Nickname of user who changed subject.
 
		void handleSubject(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &nickname = "");
 

	
 
		/// Call this function XMPP user's nickname changed.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		/// \param room Room in which participant changed. (eg. #spectrum)
 
		/// \param nickname New nickname.
 
		void handleRoomNicknameChanged(const std::string &user, const std::string &room, const std::string &nickname);
 

	
 
		/// Call this function when requested VCard arrived.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		/// \param id VCard ID.
 
		/// \param legacyName Name of legacy network buddy. (eg. "user2@gmail.com")
 
		/// \param fullName Name of legacy network buddy. (eg. "Monty Python")
 
		/// \param nickname Nickname.
 
		/// \param photo Raw photo.
 
		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);
 

	
 
		/// Call this function when buddy started typing.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
		/// \param buddyName Name of legacy network buddy. (eg. "user2@gmail.com")
 
		void handleBuddyTyping(const std::string &user, const std::string &buddyName);
 

	
 
		/// Call this function when buddy typed, but is not typing anymore.
 
		/// \param user XMPP JID of user for which this event occurs. You can get it from NetworkPlugin::handleLoginRequest(). (eg. "user%gmail.com@xmpp.domain.tld")
 
@@ -176,92 +166,82 @@ class NetworkPlugin {
 
		virtual void handleLogoutRequest(const std::string &user, const std::string &legacyName) = 0;
 

	
 
		/// Called when XMPP user sends message to legacy network.
 
		/// \param user XMPP JID of user for which this event occurs.
 
		/// \param legacyName Legacy network name of buddy or room.
 
		/// \param message Plain text message.
 
		/// \param xhtml XHTML message.
 
		virtual void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "") = 0;
 

	
 
		/// Called when XMPP user requests VCard of buddy.
 
		/// \param user XMPP JID of user for which this event occurs.
 
		/// \param legacyName Legacy network name of buddy whose VCard is requested.
 
		/// \param id ID which is associated with this request. You have to pass it to handleVCard function when you receive VCard.
 
		/**
 
			\msc
 
			NetworkPlugin,YourNetworkPlugin,LegacyNetwork;
 
			NetworkPlugin->YourNetworkPlugin [label="handleVCardRequest(...)", URL="\ref NetworkPlugin::handleVCardRequest()"];
 
			YourNetworkPlugin->LegacyNetwork [label="start VCard fetching"];
 
			YourNetworkPlugin<-LegacyNetwork [label="VCard fetched"];
 
			YourNetworkPlugin->NetworkPlugin [label="handleVCard()", URL="\ref NetworkPlugin::handleVCard()"];
 
			\endmsc
 
		*/
 
		virtual void handleVCardRequest(const std::string &/*user*/, const std::string &/*legacyName*/, unsigned int /*id*/) {}
 

	
 
		/// Called when XMPP user updates his own VCard.
 
		/// You should update the VCard in legacy network too.
 
		/// \param user XMPP JID of user for which this event occurs.
 
		/// \param photo Raw photo data.
 
		virtual void handleVCardUpdatedRequest(const std::string &/*user*/, const std::string &/*photo*/, const std::string &nickname) {}
 

	
 

	
 
		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 handleBuddyBlockToggled(const std::string &/*user*/, const std::string &/*buddyName*/, bool /*blocked*/) {}
 

	
 
		virtual void handleTypingRequest(const std::string &/*user*/, const std::string &/*buddyName*/) {}
 
		virtual void handleTypedRequest(const std::string &/*user*/, const std::string &/*buddyName*/) {}
 
		virtual void handleStoppedTypingRequest(const std::string &/*user*/, const std::string &/*buddyName*/) {}
 
		virtual void handleAttentionRequest(const std::string &/*user*/, const std::string &/*buddyName*/, const std::string &/*message*/) {}
 

	
 
		virtual void handleFTStartRequest(const std::string &/*user*/, const std::string &/*buddyName*/, const std::string &/*fileName*/, unsigned long size, unsigned long ftID) {}
 
		virtual void handleFTFinishRequest(const std::string &/*user*/, const std::string &/*buddyName*/, const std::string &/*fileName*/, unsigned long size, unsigned long ftID) {}
 
		virtual void handleFTPauseRequest(unsigned long ftID) {}
 
		virtual void handleFTContinueRequest(unsigned long ftID) {}
 

	
 
		virtual void handleExit() { std::cout << "EXITING\n"; exit(1); }
 
		void handleDataRead(Swift::SafeByteArray &data);
 
		virtual void handleExitRequest() { exit(1); }
 
		void handleDataRead(std::string &data);
 
		virtual void sendData(const std::string &string) {}
 
		
 

	
 
	private:
 
		void connect();
 
		void handleLoginPayload(const std::string &payload);
 
		void handleLogoutPayload(const std::string &payload);
 
		void handleStatusChangedPayload(const std::string &payload);
 
		void handleConvMessagePayload(const std::string &payload);
 
		void handleJoinRoomPayload(const std::string &payload);
 
		void handleLeaveRoomPayload(const std::string &payload);
 
		void handleVCardPayload(const std::string &payload);
 
		void handleBuddyChangedPayload(const std::string &payload);
 
		void handleBuddyRemovedPayload(const std::string &payload);
 
		void handleChatStatePayload(const std::string &payload, Swift::ChatState::ChatStateType type);
 
		void handleChatStatePayload(const std::string &payload, int type);
 
		void handleAttentionPayload(const std::string &payload);
 
		void handleFTStartPayload(const std::string &payload);
 
		void handleFTFinishPayload(const std::string &payload);
 
		void handleFTPausePayload(const std::string &payload);
 
		void handleFTContinuePayload(const std::string &payload);
 
		void _handleConnected(bool error);
 
		void handleDisconnected();
 

	
 
		void send(const std::string &data);
 
		void sendPong();
 
		void sendMemoryUsage();
 
		void pingTimeout();
 

	
 
		Swift::SafeByteArray m_data;
 
		std::string m_host;
 
		int m_port;
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
		Swift::EventLoop *m_loop;
 
		Swift::Timer::ref m_pingTimer;
 

	
 
		std::string m_data;
 
		bool m_pingReceived;
 
		double m_init_res;
 

	
 
};
 

	
 
}
include/transport/protocol.proto
Show inline comments
 
new file 100644
 
package pbnetwork;
 

	
 
enum StatusType {
 
	STATUS_ONLINE		= 0;
 
	STATUS_AWAY			= 1;
 
	STATUS_FFC			= 2;
 
	STATUS_XA			= 3;
 
	STATUS_DND			= 4;
 
	STATUS_NONE			= 5;
 
	STATUS_INVISIBLE	= 6;
 
}
 

	
 
message Connected {
 
	required string user = 1;
 
}
 

	
 
message Disconnected {
 
	required string user = 1;
 
	required int32 error = 2;
 
	optional string message = 3;
 
}
 

	
 
message Login {
 
	required string user = 1;
 
	required string legacyName = 2;
 
	required string password = 3;
 
}
 

	
 
message Logout {
 
	required string user = 1;
 
	required string legacyName = 2;
 
}
 

	
 
message Buddy {
 
	required string userName = 1;
 
	required string buddyName = 2;
 
	optional string alias = 3;
 
	optional string groups = 4;
 
	optional StatusType status = 5;
 
	optional string statusMessage = 6;
 
	optional string iconHash = 7;
 
	optional bool blocked = 8;
 
}
 

	
 
message ConversationMessage {
 
	required string userName = 1;
 
	required string buddyName = 2;
 
	required string message = 3;
 
	optional string nickname = 4;
 
	optional string xhtml = 5;
 
}
 

	
 
message Room {
 
	required string userName = 1;
 
	required string nickname = 2;
 
	required string room = 3;
 
	optional string password = 4;
 
}
 

	
 
message Participant {
 
	required string userName = 1;
 
	required string room = 2;
 
	required string nickname = 3;
 
	required int32 flag = 4;
 
	required StatusType status = 5;
 
	optional string statusMessage = 6;
 
	optional string newname = 7;
 
}
 

	
 
message VCard {
 
	required string userName = 1;
 
	required string buddyName = 2;
 
	required int32 id = 3;
 
	optional string fullname = 4;
 
	optional string nickname = 5;
 
	optional bytes photo = 6;
 
}
 

	
 
message Status {
 
	required string userName = 1;
 
	required StatusType status = 3;
 
	optional string statusMessage = 4;
 
}
 

	
 
message Stats {
 
	required int32 res = 1;
 
	required int32 init_res = 2;
 
	required int32 shared = 3;
 
}
 

	
 
message File {
 
	required string userName = 1;
 
	required string buddyName = 2;
 
	required string fileName = 3;
 
	required int32 size = 4;
 
	optional int32 ftID = 5;
 
}
 

	
 
message FileTransferData {
 
	required int32 ftID = 1;
 
	required bytes data = 2;
 
}
 

	
 
message WrapperMessage {
 
	enum Type { 
 
		TYPE_CONNECTED 				= 1;
 
		TYPE_DISCONNECTED 			= 2;
 
		TYPE_LOGIN 					= 3;
 
		TYPE_LOGOUT 				= 4;
 
		TYPE_BUDDY_CHANGED			= 6;
 
		TYPE_BUDDY_REMOVED			= 7;
 
		TYPE_CONV_MESSAGE			= 8;
 
		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;
 
		TYPE_ATTENTION				= 22;
 
		TYPE_STATS					= 23;
 
		TYPE_FT_START				= 24;
 
		TYPE_FT_FINISH				= 25;
 
		TYPE_FT_DATA				= 26;
 
		TYPE_FT_PAUSE				= 27;
 
		TYPE_FT_CONTINUE			= 28;
 
		TYPE_EXIT					= 29;
 
	}
 
	required Type type = 1;
 
	optional bytes payload = 2;
 
}
 
;
 
\ No newline at end of file
include/transport/storagebackend.h
Show inline comments
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * 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 <map>
 
#include <list>
 
#include <vector>
 
#include <boost/bind.hpp>
 
// #include <boost/signal.hpp>
 

	
 
namespace Transport {
 

	
 
/// Represents all data needed to be stored in database.
 
struct UserInfo {
 
	int id;				///< id of user used as primary key in database
 
	std::string jid;		///< barejid of XMPP user
 
	std::string uin;		///< legacy network username
 
	std::string password;	///< password for legacy network
 
	std::string language;	///< user's preferred language
 
	std::string encoding;	///< user's preferred encoding
 
	bool vip;				///< true if user is VIP
 
};
 

	
 
typedef enum
 
{
 
	TYPE_UNKNOWN = 0,  /**< Unknown type.                     */
 
	TYPE_SUBTYPE,      /**< Subtype.                          */
 
	TYPE_CHAR,         /**< Character.                        */
 
	TYPE_UCHAR,        /**< Unsigned character.               */
 
	TYPE_BOOLEAN,      /**< Boolean.                          */
 
	TYPE_SHORT,        /**< Short integer.                    */
 
	TYPE_USHORT,       /**< Unsigned short integer.           */
 
	TYPE_INT,          /**< Integer.                          */
 
	TYPE_UINT,         /**< Unsigned integer.                 */
 
	TYPE_LONG,         /**< Long integer.                     */
 
	TYPE_ULONG,        /**< Unsigned long integer.            */
 
	TYPE_INT64,        /**< 64-bit integer.                   */
 
	TYPE_UINT64,       /**< 64-bit unsigned integer.          */
 
	TYPE_STRING,       /**< String.                           */
 
	TYPE_OBJECT,       /**< Object pointer.                   */
 
	TYPE_POINTER,      /**< Generic pointer.                  */
 
	TYPE_ENUM,         /**< Enum.                             */
 
	TYPE_BOXED         /**< Boxed pointer with specific type. */
 

	
 
} SettingType;
 

	
 
struct SettingVariableInfo {
 
	int type;
 
	std::string s;
 
	int i;
 
	bool b;
 
};
 

	
 
struct BuddyInfo {
 
	long id;
 
	std::string alias;
 
	std::string legacyName;
 
	std::string subscription;
 
	std::vector<std::string> groups;
 
	std::map<std::string, SettingVariableInfo> settings;
 
	int flags;
 
};
 

	
 
/// Abstract class defining storage backends.
 
class StorageBackend
 
{
 
	public:
 
		/// Virtual desctructor.
 
		virtual ~StorageBackend() {}
 

	
 
		/// connect
 
		virtual bool connect() = 0;
 

	
 
		/// createDatabase
 
		virtual bool createDatabase() = 0;
 

	
 
		/// setUser
 
		virtual void setUser(const UserInfo &user) = 0;
 

	
 
		/// getuser
 
		virtual bool getUser(const std::string &barejid, UserInfo &user) = 0;
 

	
 
		/// setUserOnline
 
		virtual void setUserOnline(long id, bool online) = 0;
 

	
 
		/// removeUser
 
		virtual bool removeUser(long id) = 0;
 

	
 
		/// getBuddies
 
		virtual bool getBuddies(long id, std::list<BuddyInfo> &roster) = 0;
 

	
 
		virtual long addBuddy(long userId, const BuddyInfo &buddyInfo) = 0;
 
		virtual void updateBuddy(long userId, const BuddyInfo &buddyInfo) = 0;
 
		virtual void removeBuddy(long id) = 0;
 

	
 
		virtual void getUserSetting(long userId, const std::string &variable, int &type, std::string &value) = 0;
 
		virtual void updateUserSetting(long userId, const std::string &variable, const std::string &value) = 0;
 

	
 
		virtual void beginTransaction() = 0;
 
		virtual void commitTransaction() = 0;
 

	
 
		/// onStorageError
 
		boost::signal<void (const std::string &statement, const std::string &error)> onStorageError;
 
// 		boost::signal<void (const std::string &statement, const std::string &error)> onStorageError;
 

	
 
};
 

	
 
}
plugin/src/networkplugin.cpp
Show inline comments
 
file renamed from src/networkplugin.cpp to plugin/src/networkplugin.cpp
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * 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
 
 */
 

	
 
#include "transport/networkplugin.h"
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 
#include "transport/rostermanager.h"
 
#include "transport/usermanager.h"
 
#include "transport/conversationmanager.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Elements/StreamError.h"
 
#include "pbnetwork.pb.h"
 
#include "log4cxx/logger.h"
 
#include "log4cxx/basicconfigurator.h"
 
#include "memoryusage.h"
 
#include "transport/memoryusage.h"
 

	
 
#include <arpa/inet.h>
 

	
 
using namespace log4cxx;
 

	
 
static LoggerPtr logger = Logger::getLogger("NetworkPlugin");
 

	
 
namespace Transport {
 

	
 
#define WRAP(MESSAGE, TYPE) 	pbnetwork::WrapperMessage wrap; \
 
	wrap.set_type(TYPE); \
 
	wrap.set_payload(MESSAGE); \
 
	wrap.SerializeToString(&MESSAGE);
 

	
 
NetworkPlugin::NetworkPlugin(Swift::EventLoop *loop, const std::string &host, int port) {
 
// 	m_factories = new Swift::BoostNetworkFactories(loop);
 
	m_host = host;
 
	m_port = port;
 
NetworkPlugin::NetworkPlugin() {
 
	m_pingReceived = false;
 
	m_loop = loop;
 
// 	m_conn = m_factories->getConnectionFactory()->createConnection();
 
// 	m_conn->onDataRead.connect(boost::bind(&NetworkPlugin::handleDataRead, this, _1));
 
// 	m_conn->onDataWritten.connect(boost::bind(&NetworkPlugin::readyForData, this));
 
// 	m_conn->onConnectFinished.connect(boost::bind(&NetworkPlugin::_handleConnected, this, _1));
 
// 	m_conn->onDisconnected.connect(boost::bind(&NetworkPlugin::handleDisconnected, this));
 

	
 
// 	m_pingTimer = m_factories->getTimerFactory()->createTimer(30000);
 
// 	m_pingTimer->onTick.connect(boost::bind(&NetworkPlugin::pingTimeout, this)); 
 
	connect();
 

	
 
	double shared;
 
#ifndef WIN32
 
	process_mem_usage(shared, m_init_res);
 
#endif
 
}
 

	
 
NetworkPlugin::~NetworkPlugin() {
 
	delete m_factories;
 
}
 

	
 
void NetworkPlugin::handleMessage(const std::string &user, const std::string &legacyName, const std::string &msg, const std::string &nickname, const std::string &xhtml) {
 
	pbnetwork::ConversationMessage m;
 
	m.set_username(user);
 
	m.set_buddyname(legacyName);
 
	m.set_message(msg);
 
	m.set_nickname(nickname);
 
	m.set_xhtml(xhtml);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_CONV_MESSAGE);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleAttention(const std::string &user, const std::string &buddyName, const std::string &msg) {
 
	pbnetwork::ConversationMessage m;
 
	m.set_username(user);
 
	m.set_buddyname(buddyName);
 
	m.set_message(msg);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_ATTENTION);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleVCard(const std::string &user, unsigned int id, const std::string &legacyName, const std::string &fullName, const std::string &nickname, const std::string &photo) {
 
	pbnetwork::VCard vcard;
 
	vcard.set_username(user);
 
	vcard.set_buddyname(legacyName);
 
	vcard.set_id(id);
 
	vcard.set_fullname(fullName);
 
	vcard.set_nickname(nickname);
 
	vcard.set_photo(photo);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_VCARD);
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleSubject(const std::string &user, const std::string &legacyName, const std::string &msg, const std::string &nickname) {
 
	pbnetwork::ConversationMessage m;
 
	m.set_username(user);
 
	m.set_buddyname(legacyName);
 
	m.set_message(msg);
 
	m.set_nickname(nickname);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_ROOM_SUBJECT_CHANGED);
 
// 	std::cout << "SENDING MESSAGE\n";
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleBuddyChanged(const std::string &user, const std::string &buddyName, const std::string &alias,
 
			const std::string &groups, Swift::StatusShow::Type status, const std::string &statusMessage, const std::string &iconHash, bool blocked) {
 
			const std::string &groups, pbnetwork::StatusType status, const std::string &statusMessage, const std::string &iconHash, bool blocked) {
 
	pbnetwork::Buddy buddy;
 
	buddy.set_username(user);
 
	buddy.set_buddyname(buddyName);
 
	buddy.set_alias(alias);
 
	buddy.set_groups(groups);
 
	buddy.set_status((pbnetwork::StatusType) status);
 
	buddy.set_statusmessage(statusMessage);
 
	buddy.set_iconhash(iconHash);
 
	buddy.set_blocked(blocked);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleBuddyTyping(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_BUDDY_TYPING);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleBuddyTyped(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_BUDDY_TYPED);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleBuddyStoppedTyping(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_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);	
 
}
 

	
 
void NetworkPlugin::handleDisconnected(const std::string &user, int error, const std::string &msg) {
 
	pbnetwork::Disconnected d;
 
	d.set_user(user);
 
	d.set_error(error);
 
	d.set_message(msg);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_DISCONNECTED);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleParticipantChanged(const std::string &user, const std::string &nickname, const std::string &room, Conversation::ParticipantFlag flags, Swift::StatusShow::Type status, const std::string &statusMessage, const std::string &newname) {
 
void NetworkPlugin::handleParticipantChanged(const std::string &user, const std::string &nickname, const std::string &room, int flags, pbnetwork::StatusType status, const std::string &statusMessage, const std::string &newname) {
 
	pbnetwork::Participant d;
 
	d.set_username(user);
 
	d.set_nickname(nickname);
 
	d.set_room(room);
 
	d.set_flag(flags);
 
	d.set_newname(newname);
 
	d.set_status((pbnetwork::StatusType) status);
 
	d.set_statusmessage(statusMessage);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_PARTICIPANT_CHANGED);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleRoomNicknameChanged(const std::string &user, const std::string &r, const std::string &nickname) {
 
	pbnetwork::Room room;
 
	room.set_username(user);
 
	room.set_nickname(nickname);
 
	room.set_room(r);
 
	room.set_password("");
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_ROOM_NICKNAME_CHANGED);
 
 
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleFTStart(const std::string &user, const std::string &buddyName, const std::string fileName, unsigned long size) {
 
	pbnetwork::File room;
 
	room.set_username(user);
 
	room.set_buddyname(buddyName);
 
	room.set_filename(fileName);
 
	room.set_size(size);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_FT_START);
 
 
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleFTFinish(const std::string &user, const std::string &buddyName, const std::string fileName, unsigned long size, unsigned long ftid) {
 
	pbnetwork::File room;
 
	room.set_username(user);
 
	room.set_buddyname(buddyName);
 
	room.set_filename(fileName);
 
	room.set_size(size);
 
	if (ftid) {
 
		room.set_ftid(ftid);
 
	}
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_FT_FINISH);
 
 
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleFTData(unsigned long ftID, const std::string &data) {
 
	pbnetwork::FileTransferData d;
 
	d.set_ftid(ftID);
 
	d.set_data(data);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_FT_DATA);
 
 
 
	send(message);
 
}
 

	
 
void NetworkPlugin::_handleConnected(bool error) {
 
	if (error) {
 
// 		LOG4CXX_ERROR(logger, "Connecting error. Exiting");
 
// 		m_pingTimer->stop();
 
		handleExit();
 
	}
 
	else {
 
// 		LOG4CXX_INFO(logger, "Connected to NetworkPluginServer");
 
// 		m_pingTimer->start();
 
	}
 
}
 

	
 
void NetworkPlugin::handleDisconnected() {
 
// 	LOG4CXX_INFO(logger, "Disconnected from NetworkPluginServer. Exiting.");
 
// 	m_pingTimer->stop();
 
	handleExit();
 
}
 

	
 
void NetworkPlugin::connect() {
 
	LOG4CXX_INFO(logger, "Connecting NetworkPluginServer host " << m_host << " port " << m_port);
 
// 	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(m_host), m_port));
 
}
 

	
 
void NetworkPlugin::handleLoginPayload(const std::string &data) {
 
	pbnetwork::Login payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 
	handleLoginRequest(payload.user(), payload.legacyname(), payload.password());
 
}
 

	
 
void NetworkPlugin::handleLogoutPayload(const std::string &data) {
 
	pbnetwork::Logout payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 
	handleLogoutRequest(payload.user(), payload.legacyname());
 
}
 

	
 
void NetworkPlugin::handleStatusChangedPayload(const std::string &data) {
 
	pbnetwork::Status payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	handleStatusChangeRequest(payload.username(), payload.status(), payload.statusmessage());
 
}
 

	
 
void NetworkPlugin::handleConvMessagePayload(const std::string &data) {
 
	pbnetwork::ConversationMessage payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	handleMessageSendRequest(payload.username(), payload.buddyname(), payload.message(), payload.xhtml());
 
}
 

	
 
void NetworkPlugin::handleAttentionPayload(const std::string &data) {
 
	pbnetwork::ConversationMessage payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	handleAttentionRequest(payload.username(), payload.buddyname(), payload.message());
 
}
 

	
 
@@ -435,203 +389,197 @@ void NetworkPlugin::handleLeaveRoomPayload(const std::string &data) {
 
	pbnetwork::Room payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	handleLeaveRoomRequest(payload.username(), payload.room());
 
}
 

	
 
void NetworkPlugin::handleVCardPayload(const std::string &data) {
 
	pbnetwork::VCard payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	if (payload.has_photo()) {
 
		handleVCardUpdatedRequest(payload.username(), payload.photo(), payload.nickname());
 
	}
 
	else if (!payload.buddyname().empty()) {
 
		handleVCardRequest(payload.username(), payload.buddyname(), payload.id());
 
	}
 
}
 

	
 
void NetworkPlugin::handleBuddyChangedPayload(const std::string &data) {
 
	pbnetwork::Buddy payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 
	if (payload.has_blocked()) {
 
		handleBuddyBlockToggled(payload.username(), payload.buddyname(), payload.blocked());
 
	}
 
	else {
 
		handleBuddyUpdatedRequest(payload.username(), payload.buddyname(), payload.alias(), payload.groups());
 
	}
 
}
 

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

	
 
	handleBuddyRemovedRequest(payload.username(), payload.buddyname(), payload.groups());
 
}
 

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

	
 
	switch(type) {
 
		case Swift::ChatState::Composing:
 
		case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPING:
 
			handleTypingRequest(payload.username(), payload.buddyname());
 
			break;
 
		case Swift::ChatState::Paused:
 
		case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPED:
 
			handleTypedRequest(payload.username(), payload.buddyname());
 
			break;
 
		case Swift::ChatState::Active:
 
		case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_STOPPED_TYPING:
 
			handleStoppedTypingRequest(payload.username(), payload.buddyname());
 
			break;
 
		default:
 
			break;
 
	}
 
}
 

	
 
void NetworkPlugin::handleDataRead(Swift::SafeByteArray &data) {
 
void NetworkPlugin::handleDataRead(std::string &data) {
 
	m_data.insert(m_data.end(), data.begin(), data.end());
 

	
 
	while (m_data.size() != 0) {
 
		unsigned int expected_size;
 

	
 
		if (m_data.size() >= 4) {
 
			expected_size = *((unsigned int*) &m_data[0]);
 
			expected_size = ntohl(expected_size);
 
			if (m_data.size() - 4 < expected_size)
 
				return;
 
		}
 
		else {
 
			return;
 
		}
 

	
 
		pbnetwork::WrapperMessage wrapper;
 
		if (wrapper.ParseFromArray(&m_data[4], expected_size) == false) {
 
			m_data.erase(m_data.begin(), m_data.begin() + 4 + expected_size);
 
			return;
 
		}
 
		m_data.erase(m_data.begin(), m_data.begin() + 4 + expected_size);
 

	
 
		switch(wrapper.type()) {
 
			case pbnetwork::WrapperMessage_Type_TYPE_LOGIN:
 
				handleLoginPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_LOGOUT:
 
				handleLogoutPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_PING:
 
				LOG4CXX_INFO(logger, "PING RECEIVED");
 
				sendPong();
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_CONV_MESSAGE:
 
				handleConvMessagePayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_JOIN_ROOM:
 
				handleJoinRoomPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_LEAVE_ROOM:
 
				handleLeaveRoomPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_VCARD:
 
				handleVCardPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED:
 
				handleBuddyChangedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_REMOVED:
 
				handleBuddyRemovedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_STATUS_CHANGED:
 
				handleStatusChangedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPING:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Composing);
 
				handleChatStatePayload(wrapper.payload(), pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPING);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPED:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Paused);
 
				handleChatStatePayload(wrapper.payload(), pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPED);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_STOPPED_TYPING:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Active);
 
				handleChatStatePayload(wrapper.payload(), pbnetwork::WrapperMessage_Type_TYPE_BUDDY_STOPPED_TYPING);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_ATTENTION:
 
				handleAttentionPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_FT_START:
 
				handleFTStartPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_FT_FINISH:
 
				handleFTFinishPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_FT_PAUSE:
 
				handleFTPausePayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_FT_CONTINUE:
 
				handleFTContinuePayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_EXIT:
 
				handleExitRequest();
 
				break;
 
			default:
 
				return;
 
		}
 
	}
 
}
 

	
 
void NetworkPlugin::send(const std::string &data) {
 
	char header[4];
 
	*((int*)(header)) = htonl(data.size());
 
	sendData(std::string(header, 4) + data);
 
}
 

	
 
void NetworkPlugin::sendPong() {
 
	m_pingReceived = true;
 
	std::string message;
 
	pbnetwork::WrapperMessage wrap;
 
	wrap.set_type(pbnetwork::WrapperMessage_Type_TYPE_PONG);
 
	wrap.SerializeToString(&message);
 

	
 
	send(message);
 
	LOG4CXX_INFO(logger, "PONG");
 
	sendMemoryUsage();
 
}
 

	
 
void NetworkPlugin::sendMemoryUsage() {
 
	pbnetwork::Stats stats;
 

	
 
	stats.set_init_res(m_init_res);
 
	double res;
 
	double shared;
 
#ifndef WIN32
 
	process_mem_usage(shared, res);
 
#endif
 
	stats.set_res(res);
 
	stats.set_shared(shared);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_STATS);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::pingTimeout() {
 
	if (m_pingReceived == false) {
 
// 		LOG4CXX_ERROR(logger, "No PING received for long time. Exiting");
 
		handleExit();
 
	}
 
	m_pingReceived = false;
 
// 	m_pingTimer->start();
 
}
 

	
 
}
plugin/src/pbnetwork.proto
Show inline comments
 
file renamed from src/pbnetwork.proto to plugin/src/pbnetwork.proto
src/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp *.h)
 
FILE(GLOB_RECURSE SWIFTEN_SRC ../include/Swiften/*.cpp)
 
FILE(GLOB HEADERS ../include/transport/*.h)
 
 
if (PROTOBUF_FOUND)
 
	PROTOBUF_GENERATE_CPP(PROTOBUF_SRC PROTOBUF_HDRS "pbnetwork.proto")
 
endif()
 
 
if (CPPUNIT_FOUND)
 
	FILE(GLOB SRC_TEST tests/*.cpp)
 
 
	ADD_EXECUTABLE(libtransport_test ${SRC_TEST})
 
 
	target_link_libraries(libtransport_test transport ${CPPUNIT_LIBRARIES} ${Boost_LIBRARIES})
 
endif()
 
 
include_directories(${POPT_INCLUDE_DIR})
 
 
# SOURCE_GROUP(headers FILES ${HEADERS})
 
 
ADD_LIBRARY(transport SHARED ${HEADERS} ${SRC} ${SWIFTEN_SRC} ${PROTOBUF_SRC} ${PROTOBUF_HDRS})
 
 
 
 
if (PROTOBUF_FOUND)
 
	ADD_LIBRARY(transport SHARED ${HEADERS} ${SRC} ${SWIFTEN_SRC} ../include/transport/protocol.pb.cc)
 
	ADD_DEPENDENCIES(transport pb)
 
else()
 
	ADD_LIBRARY(transport SHARED ${HEADERS} ${SRC} ${SWIFTEN_SRC})
 
endif()
 
 
ADD_DEFINITIONS(-fPIC)
 
 
TARGET_LINK_LIBRARIES(transport ${Boost_LIBRARIES} ${SQLITE3_LIBRARIES} ${MYSQL_LIBRARIES} ${SWIFTEN_LIBRARY} ${PROTOBUF_LIBRARIES} ${LOG4CXX_LIBRARIES} ${POPT_LIBRARY})
 
 
SET_TARGET_PROPERTIES(transport PROPERTIES
 
      VERSION ${TRANSPORT_VERSION} SOVERSION ${TRANSPORT_VERSION}
 
)
 
 
INSTALL(TARGETS transport LIBRARY DESTINATION lib ARCHIVE DESTINATION lib COMPONENT libraries)
 
 
#CONFIGURE_FILE(transport.pc.in "${CMAKE_CURRENT_BINARY_DIR}/transport.pc")
 
#INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/transport.pc" DESTINATION lib/pkgconfig)
src/admininterface.cpp
Show inline comments
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * 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
 
 */
 

	
 
#include "transport/admininterface.h"
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 
#include "transport/conversationmanager.h"
 
#include "transport/rostermanager.h"
 
#include "transport/usermanager.h"
 
#include "transport/networkpluginserver.h"
 
#include "storageresponder.h"
 
#include "log4cxx/logger.h"
 
#include "memoryusage.h"
 
#include "transport/memoryusage.h"
 
#include <boost/foreach.hpp>
 

	
 
using namespace log4cxx;
 

	
 
namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("AdminInterface");
 

	
 
static std::string getArg(const std::string &body) {
 
	std::string ret;
 
	if (body.find(" ") == std::string::npos)
 
		return ret;
 

	
 
	return body.substr(body.find(" ") + 1);
 
}
 

	
 
AdminInterface::AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server, StorageBackend *storageBackend) {
 
	m_component = component;
 
	m_storageBackend = storageBackend;
 
	m_userManager = userManager;
 
	m_server = server;
 

	
 
	m_component->getStanzaChannel()->onMessageReceived.connect(bind(&AdminInterface::handleMessageReceived, this, _1));
 
}
 

	
 
AdminInterface::~AdminInterface() {
 
}
 

	
 
void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
 
	if (!message->getTo().getNode().empty())
 
		return;
 

	
 
	if (message->getFrom().getNode() != CONFIG_STRING(m_component->getConfig(), "service.admin_username")) {
 
		LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().getNode());
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Message from admin received");
 
	message->setTo(message->getFrom());
 
	message->setFrom(m_component->getJID());
 

	
 
	if (message->getBody() == "status") {
 
		int users = m_userManager->getUserCount();
 
		int backends = m_server->getBackendCount();
 
		message->setBody("Running (" + boost::lexical_cast<std::string>(users) + " users connected using " + boost::lexical_cast<std::string>(backends) + " backends)");
 
	}
 
	else if (message->getBody() == "online_users") {
 
		std::string lst;
src/logger.cpp
Show inline comments
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * 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
 
 */
 

	
 
#include "transport/logger.h"
 
#include "transport/usermanager.h"
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 
#include "transport/userregistration.h"
 
#include "transport/buddy.h"
 
#include "transport/rostermanager.h"
 
#include <boost/bind.hpp>
 

	
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
Logger::Logger(Component *component) {
 
	component->onConnected.connect(boost::bind(&Logger::handleConnected, this));
 
	component->onConnectionError.connect(boost::bind(&Logger::handleConnectionError, this, _1));
 
	component->onXMLIn.connect(boost::bind(&Logger::handleXMLIn, this, _1));
 
	component->onXMLOut.connect(boost::bind(&Logger::handleXMLOut, this, _1));
 
}
 

	
 
Logger::~Logger(){
 
}
 

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

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

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

	
 
void Logger::setRosterManager(RosterManager *rosterManager) {
 
	rosterManager->onBuddySet.connect(boost::bind(&Logger::handleBuddySet, this, _1));
 
	rosterManager->onBuddyUnset.connect(boost::bind(&Logger::handleBuddyUnset, 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;
 
		case Swift::ComponentError::XMLError: std::cout << "[COMPONENT] Disconnect reason: XMLError\n"; break;
 
		case Swift::ComponentError::AuthenticationFailedError: std::cout << "[COMPONENT] Disconnect reason: AuthenticationFailedError\n"; break;
 
		case Swift::ComponentError::UnexpectedElementError: std::cout << "[COMPONENT] Disconnect reason: UnexpectedElementError\n"; break; 
 
	};
 
}
 

	
 
void Logger::handleXMLIn(const std::string &data) {
 
	std::cout << "[XML IN] " << data << "\n";
 
}
 

	
 
void Logger::handleXMLOut(const std::string &data) {
 
	std::cout << "[XML OUT] " << data << "\n";
 
}
 

	
 
void Logger::handleStorageError(const std::string &statement, const std::string &error) {
 
	std::cout << "[SQL ERROR] \"" << error << "\" during statement \"" << statement << "\"\n";
 
}
 

	
 
void Logger::handleUserRegistered(const UserInfo &user) {
src/memoryreadbytestream.cpp
Show inline comments
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * 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
 
 */
 

	
 
#include "transport/memoryreadbytestream.h"
 
#include "log4cxx/logger.h"
 
#include "memoryusage.h"
 
#include <boost/foreach.hpp>
 

	
 
using namespace log4cxx;
 

	
 
namespace Transport {
 
	
 
MemoryReadBytestream::MemoryReadBytestream(unsigned long size) {
 
	neededData = false;
 
	m_finished = false;
 
	m_sent = 0;
 
	m_size = size;
 
}
 

	
 
MemoryReadBytestream::~MemoryReadBytestream() {
 
	
 
}
 

	
 
unsigned long MemoryReadBytestream::appendData(const std::string &data) {
 
	m_data += data;
 
	onDataAvailable();
 
	neededData = false;
 
	return m_data.size();
 
}
 

	
 
boost::shared_ptr<std::vector<unsigned char> > MemoryReadBytestream::read(size_t size) {
 
	if (m_data.empty()) {
 
		onDataNeeded();
 
		return boost::shared_ptr<std::vector<unsigned char> >(new std::vector<unsigned char>());
 
	}
 

	
 
	if (m_data.size() < size) {
 
		boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.end()));
 
		m_sent += m_data.size();
 
		m_data.clear();
 
		if (m_sent == m_size)
 
			m_finished = true;
 
		onDataNeeded();
 
		return ptr;
 
	}
 
	boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.begin() + size));
 
	m_data.erase(m_data.begin(), m_data.begin() + size);
 
	m_sent += size;
 
	if (m_sent == m_size)
 
		m_finished = true;
 
	if (m_data.size() < 500000 && !neededData) {
 
		neededData = true;
 
		onDataNeeded();
 
	}
src/memoryusage.cpp
Show inline comments
 
/**
 
 * 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
 
 */
 

	
 
#include "memoryusage.h"
 
#include "transport/memoryusage.h"
 

	
 
#include <iostream>
 
#include <cstring>
 
#include <sstream>
 
#include <fstream>
 
#include <algorithm>
 
#ifndef WIN32
 
#include <sys/param.h>
 
#endif
 
#ifdef BSD
 
#include <sys/types.h>
 
#include <sys/sysctl.h>
 
#include <sys/param.h>
 
#include <sys/sysctl.h>
 
#include <sys/user.h>
 

	
 
#endif
 

	
 
namespace Transport {
 

	
 
#ifndef WIN32
 
#ifdef BSD
 
void process_mem_usage(double& vm_usage, double& resident_set) {
 
	int mib[4];
 
	size_t size;
 
	mib[0] = CTL_KERN;
 
	mib[1] = KERN_PROC;
 
	mib[2] = KERN_PROC_PID;
 
	mib[3] = getpid();
 
	struct kinfo_proc proc;
 

	
 
	size = sizeof(struct kinfo_proc);
 

	
 
	if (sysctl((int*)mib, 4, &proc, &size, NULL, 0) == -1) {
 
		vm_usage = 0;
 
		resident_set = 0;
 
		return;
 
	}
 

	
 
	// sysctl stores 0 in the size if we can't find the process information.
 
	// Set errno to ESRCH which will be translated in NoSuchProcess later on.
 
	if (size == 0) {
 
		vm_usage = 0;
 
		resident_set = 0;
 
		return;
 
	}
 

	
 
	int pagesize,cnt;
src/networkpluginserver.cpp
Show inline comments
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * 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
 
 */
 

	
 
#include "transport/networkpluginserver.h"
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 
#include "transport/rostermanager.h"
 
#include "transport/usermanager.h"
 
#include "transport/conversationmanager.h"
 
#include "transport/localbuddy.h"
 
#include "transport/config.h"
 
#include "transport/conversation.h"
 
#include "transport/vcardresponder.h"
 
#include "transport/rosterresponder.h"
 
#include "transport/memoryreadbytestream.h"
 
#include "blockresponder.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Elements/StreamError.h"
 
#include "Swiften/Network/BoostConnectionServer.h"
 
#include "Swiften/Elements/AttentionPayload.h"
 
#include "Swiften/Elements/XHTMLIMPayload.h"
 
#include "Swiften/Elements/InvisiblePayload.h"
 
#include "pbnetwork.pb.h"
 
#include "transport/protocol.pb.h"
 
#include "log4cxx/logger.h"
 

	
 
#include <Swiften/FileTransfer/ReadBytestream.h>
 
#include <Swiften/Elements/StreamInitiationFileInfo.h>
 

	
 
#ifdef _WIN32
 
#include "windows.h"
 
#else
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
#include "popt.h"
 
#endif
 

	
 
using namespace log4cxx;
 

	
 
namespace Transport {
 

	
 
static unsigned long backend_id;
 
static unsigned long bytestream_id;
 

	
 
static LoggerPtr logger = Logger::getLogger("NetworkPluginServer");
 

	
 
class NetworkConversation : public Conversation {
 
	public:
 
		NetworkConversation(ConversationManager *conversationManager, const std::string &legacyName, bool muc = false) : Conversation(conversationManager, legacyName, muc) {
 
		}
 

	
 
		// Called when there's new message to legacy network from XMPP network
 
		void sendMessage(boost::shared_ptr<Swift::Message> &message) {
 
			onMessageToSend(this, message);
 
		}
 

	
 
		boost::signal<void (NetworkConversation *, boost::shared_ptr<Swift::Message> &)> onMessageToSend;
 
};
 

	
 
class NetworkFactory : public Factory {
 
	public:
 
		NetworkFactory(NetworkPluginServer *nps) {
 
			m_nps = nps;
 
		}
 

	
 
		// Creates new conversation (NetworkConversation in this case)
 
		Conversation *createConversation(ConversationManager *conversationManager, const std::string &legacyName) {
 
			NetworkConversation *nc = new NetworkConversation(conversationManager, legacyName);
 
			nc->onMessageToSend.connect(boost::bind(&NetworkPluginServer::handleMessageReceived, m_nps, _1, _2));
 
			return nc;
 
		}
 

	
 
@@ -285,96 +285,103 @@ void NetworkPluginServer::handleNewClientConnection(boost::shared_ptr<Swift::Con
 
		m_component->start();
 
	}
 

	
 
	m_clients.push_front(client);
 

	
 
	c->onDisconnected.connect(boost::bind(&NetworkPluginServer::handleSessionFinished, this, client));
 
	c->onDataRead.connect(boost::bind(&NetworkPluginServer::handleDataRead, this, client, _1));
 
	sendPing(client);
 

	
 
	// sendPing sets pongReceived to 0, but we want to have it -1 to ignore this backend
 
	// in first ::pingTimeout call, because it can be called right after this function
 
	// and backend wouldn't have any time to response to ping.
 
	client->pongReceived = -1;
 

	
 
	// some users are in queue waiting for this backend
 
	while(!m_waitingUsers.empty()) {
 
		// There's no new backend, so stop associating users and wait for new backend,
 
		// which has been already spawned in getFreeClient() call.
 
		if (getFreeClient() == NULL)
 
			break;
 

	
 
		User *u = m_waitingUsers.front();
 
		m_waitingUsers.pop_front();
 

	
 
		LOG4CXX_INFO(logger, "Associating " << u->getJID().toString() << " with this backend");
 

	
 
		// associate backend with user
 
		handleUserCreated(u);
 

	
 
		// connect user if it's ready
 
		if (u->isReadyToConnect()) {
 
			handleUserReadyToConnect(u);
 
		}
 

	
 
	}
 
}
 

	
 
void NetworkPluginServer::handleSessionFinished(Backend *c) {
 
	LOG4CXX_INFO(logger, "Backend " << c << " disconnected. Current backend count=" << (m_clients.size() - 1));
 

	
 
	// If there are users associated with this backend, it must have crashed, so print error output
 
	// and disconnect users
 
	for (std::list<User *>::const_iterator it = c->users.begin(); it != c->users.end(); it++) {
 
		LOG4CXX_ERROR(logger, "Backend " << c << " disconnected (probably crashed) with active user " << (*it)->getJID().toString());
 
		(*it)->setData(NULL);
 
		(*it)->handleDisconnected("Internal Server Error, please reconnect.");
 
	}
 

	
 
	std::string message;
 
	pbnetwork::WrapperMessage wrap;
 
	wrap.set_type(pbnetwork::WrapperMessage_Type_TYPE_EXIT);
 
	wrap.SerializeToString(&message);
 

	
 
	send(c->connection, message);
 

	
 
	c->connection->onDisconnected.disconnect_all_slots();
 
	c->connection->onDataRead.disconnect_all_slots();
 
	c->connection->disconnect();
 
	c->connection.reset();
 

	
 
	m_clients.remove(c);
 
	delete c;
 
}
 

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

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

	
 
	user->setConnected(true);
 
	m_component->m_userRegistry->onPasswordValid(payload.user());
 
}
 

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

	
 
	m_component->m_userRegistry->onPasswordInvalid(payload.user());
 

	
 
	User *user = m_userManager->getUser(payload.user());
 
	if (!user) {
 
		return;
 
	}
 
	user->handleDisconnected(payload.message());
 
}
 

	
 
void NetworkPluginServer::handleVCardPayload(const std::string &data) {
 
	pbnetwork::VCard payload;
 
	if (payload.ParseFromString(data) == false) {
 
		std::cout << "PARSING ERROR\n";
 
		// TODO: ERROR
 
		return;
 
	}
0 comments (0 inline, 0 general)