Changeset - 2add374fbd61
[Not reviewed]
0 8 0
Jan Kaluza - 14 years ago 2011-05-18 16:25:52
hanzz.k@gmail.com
working messaging and PM (only if you send it to user@server.tld, so normal PMs doesn't work yet)
8 files changed with 53 insertions and 13 deletions:
0 comments (0 inline, 0 general)
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) {
 
			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";
 
			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) {
 
			
 
			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.
 
			handleRoomChanged(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));
 
		}
 

	
 
		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");
 
	desc.add_options()
 
		("host,h", value<std::string>(&host), "host")
 
		("port,p", value<int>(&port), "port")
 
		;
 
	try
 
	{
 
		boost::program_options::variables_map vm;
 
		boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
 
		boost::program_options::notify(vm);
 
	}
 
	catch (std::runtime_error& e)
 
	{
 
		std::cout << desc << "\n";
 
		exit(1);
 
	}
 
	catch (...)
 
	{
backends/libircclient-qt/session.cpp
Show inline comments
 
@@ -71,97 +71,98 @@ MyIrcBuffer::MyIrcBuffer(const QString& receiver, const std::string &user, Netwo
 
    connect(this, SIGNAL(unknownMessageReceived(QString, QStringList)), SLOT(on_unknownMessageReceived(QString, QStringList)));
 
}
 

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

	
 
void MyIrcBuffer::on_joined(const QString& origin)
 
{
 
    qDebug() << "joined:" << receiver() << origin;
 
}
 

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

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

	
 
void MyIrcBuffer::on_nickChanged(const QString& origin, const QString& nick)
 
{
 
    qDebug() << "nick changed:" << receiver() << origin << nick;
 
}
 

	
 
void MyIrcBuffer::on_modeChanged(const QString& origin, const QString& mode, const QString& args)
 
{
 
    qDebug() << "mode changed:" << receiver() << origin << mode << args;
 
}
 

	
 
void MyIrcBuffer::on_topicChanged(const QString& origin, const QString& topic)
 
{
 
    qDebug() << "topic changed:" << receiver() << origin << topic;
 
}
 

	
 
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)");
 
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 353:
 
			QString channel = params.value(2);
 
			QStringList members = params.value(3).split(" ");
 

	
 
			for (int i = 0; i < members.size(); i++) {
 
				np->handleParticipantChanged(user, members.at(i).toStdString(), channel.toStdString(), 0);
 
			}
 
			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;
 
}
include/transport/conversation.h
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
 
 */
 

	
 
#pragma once
 

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

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

	
 
namespace Transport {
 

	
 
class ConversationManager;
 

	
 
class Conversation {
 
	public:
 
		/// Constructor.
 
		Conversation(ConversationManager *conversationManager, const std::string &legacyName);
 

	
 
		/// Destructor
 
		virtual ~Conversation();
 

	
 
		const std::string &getLegacyName() { return m_legacyName; }
 

	
 
		void handleMessage(boost::shared_ptr<Swift::Message> &message);
 
		void handleMessage(boost::shared_ptr<Swift::Message> &message, const std::string &nickname = "");
 
		void handleParticipantChanged(const std::string &nickname, int flag);
 
		void setNickname(const std::string &nickname) {
 
			m_nickname = nickname;
 
		}
 

	
 
		virtual void sendMessage(boost::shared_ptr<Swift::Message> &message) = 0;
 

	
 
		ConversationManager *getConversationManager() {
 
			return m_conversationManager;
 
		}
 

	
 
	private:
 
		ConversationManager *m_conversationManager;
 
		std::string m_legacyName;
 
		std::string m_nickname;
 
		int m_muc;
 
};
 

	
 
}
include/transport/networkplugin.h
Show inline comments
 
@@ -3,84 +3,84 @@
 
 *
 
 * 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"
 

	
 
namespace Transport {
 

	
 
class NetworkPlugin {
 
	public:
 
		NetworkPlugin(Swift::EventLoop *loop, const std::string &host, int port);
 

	
 
		virtual ~NetworkPlugin();
 

	
 
		void handleBuddyChanged(const std::string &user, const std::string &buddyName, const std::string &alias,
 
			const std::string &groups, int status, const std::string &statusMessage = "", const std::string &iconHash = ""
 
		);
 

	
 
		void handleParticipantChanged(const std::string &user, const std::string &nickname, const std::string &room, int flags);
 

	
 
		void handleDisconnected(const std::string &user, const std::string &legacyName, int error, const std::string &message);
 

	
 
		void handleMessage(const std::string &user, const std::string &legacyName, const std::string &message);
 
		void handleMessage(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &nickname = "");
 

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

	
 
		virtual void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) = 0;
 
		virtual void handleLogoutRequest(const std::string &user, const std::string &legacyName) = 0;
 
		virtual void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message) = 0;
 
		virtual void 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*/) {}
 
		
 

	
 
	private:
 
		void connect();
 
		void handleLoginPayload(const std::string &payload);
 
		void handleLogoutPayload(const std::string &payload);
 
		void handleConvMessagePayload(const std::string &payload);
 
		void handleJoinRoomPayload(const std::string &payload);
 
		void handleLeaveRoomPayload(const std::string &payload);
 
		void handleDataRead(const Swift::ByteArray&);
 
		void handleConnected(bool error);
 
		void handleDisconnected();
 

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

	
 
		std::string 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::Timer::ref m_reconnectTimer;
 

	
 
};
 

	
 
}
src/conversation.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 <iostream>
 
#include "transport/conversation.h"
 
#include "transport/conversationmanager.h"
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/buddy.h"
 
#include "transport/rostermanager.h"
 

	
 
namespace Transport {
 

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

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

	
 
void Conversation::handleMessage(boost::shared_ptr<Swift::Message> &message) {
 
void Conversation::handleMessage(boost::shared_ptr<Swift::Message> &message, const std::string &nickname) {
 
	if (m_muc == -1)
 
		m_muc = message->getType() != Swift::Message::Groupchat;
 
	if (m_muc == 0) {
 
		message->setType(Swift::Message::Groupchat);
 
	}
 
	else {
 
		message->setType(Swift::Message::Chat);
 
	}
 
	if (message->getType() != Swift::Message::Groupchat) {
 
		
 
		message->setTo(m_conversationManager->getUser()->getJID().toBare());
 
		// normal message
 
		if (nickname.empty()) {
 
			Buddy *buddy = m_conversationManager->getUser()->getRosterManager()->getBuddy(m_legacyName);
 
			if (buddy) {
 
				std::cout << m_legacyName << " 222222\n";
 
				message->setFrom(buddy->getJID());
 
			}
 
			else {
 
				std::cout << m_legacyName << " 1111111\n";
 
				// TODO: escape from and setFrom
 
			}
 
		}
 
		// PM message
 
		else {
 
			message->setFrom(Swift::JID(nickname, m_conversationManager->getComponent()->getJID().toBare(), "user"));
 
		}
 
		m_conversationManager->getComponent()->getStanzaChannel()->sendMessage(message);
 
	}
 
	else {
 
		message->setTo(m_conversationManager->getUser()->getJID().toString());
 
		message->setFrom(Swift::JID(m_legacyName, m_conversationManager->getComponent()->getJID().toBare(), nickname));
 
		m_conversationManager->getComponent()->getStanzaChannel()->sendMessage(message);
 
	}
 
}
 

	
 
void Conversation::handleParticipantChanged(const std::string &nick, int flag) {
 
	std::string nickname = nick;
 
	if (nickname.find("@") == 0) {
 
		nickname = nickname.substr(1);
 
	}
 
	Swift::Presence::ref presence = Swift::Presence::create();
 
 	presence->setFrom(Swift::JID(m_legacyName, m_conversationManager->getComponent()->getJID().toBare(), nickname));
 
	presence->setTo(m_conversationManager->getUser()->getJID().toString());
 
	presence->setType(Swift::Presence::Available);
 

	
 
	Swift::MUCUserPayload *p = new Swift::MUCUserPayload ();
 
	if (m_nickname == nickname) {
 
		Swift::MUCUserPayload::StatusCode c;
 
		c.code = 110;
 
		p->addStatusCode(c);
 
	}
 
	p->addItem(Swift::MUCUserPayload::Item(Swift::MUCOccupant::Member, Swift::MUCOccupant::Participant));
 

	
 
	presence->addPayload(boost::shared_ptr<Swift::Payload>(p));
 
	m_conversationManager->getComponent()->getStanzaChannel()->sendPresence(presence);
 
}
 

	
 
}
src/networkplugin.cpp
Show inline comments
 
@@ -10,101 +10,102 @@
 
 *
 
 * 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"
 

	
 
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;
 
	m_conn = m_factories->getConnectionFactory()->createConnection();
 
	m_conn->onDataRead.connect(boost::bind(&NetworkPlugin::handleDataRead, this, _1));
 
	m_conn->onConnectFinished.connect(boost::bind(&NetworkPlugin::handleConnected, this, _1));
 
	m_conn->onDisconnected.connect(boost::bind(&NetworkPlugin::handleDisconnected, this));
 

	
 
	m_reconnectTimer = m_factories->getTimerFactory()->createTimer(1000);
 
	m_reconnectTimer->onTick.connect(boost::bind(&NetworkPlugin::connect, this)); 
 
	connect();
 
}
 

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

	
 
void NetworkPlugin::handleMessage(const std::string &user, const std::string &legacyName, const std::string &msg) {
 
void NetworkPlugin::handleMessage(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_CONV_MESSAGE);
 
// 	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, int status, const std::string &statusMessage, const std::string &iconHash) {
 
	pbnetwork::Buddy buddy;
 
	buddy.set_username(user);
 
	buddy.set_buddyname(buddyName);
 
	buddy.set_alias(alias);
 
	buddy.set_groups(groups);
 
	buddy.set_status(status);
 
	buddy.set_statusmessage(statusMessage);
 
	buddy.set_iconhash(iconHash);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED);
 

	
 
	send(message);
 
}
 

	
 
void NetworkPlugin::handleDisconnected(const std::string &user, const std::string &legacyName, int error, const std::string &msg) {
 
	pbnetwork::Disconnected d;
 
	d.set_user(user);
 
	d.set_name(legacyName);
 
	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, int flags) {
 
	pbnetwork::Participant d;
 
	d.set_username(user);
 
	d.set_nickname(nickname);
src/networkpluginserver.cpp
Show inline comments
 
@@ -210,104 +210,114 @@ void NetworkPluginServer::handleParticipantChangedPayload(const std::string &dat
 
		return;
 
	}
 

	
 
	conv->handleParticipantChanged(payload.nickname(), payload.flag());
 

	
 
// 	LocalBuddy *buddy = (LocalBuddy *) user->getRosterManager()->getBuddy(payload.buddyname());
 
// 	if (buddy) {
 
// 		handleBuddyPayload(buddy, payload);
 
// 		buddy->buddyChanged();
 
// 	}
 
// 	else {
 
// 		buddy = new LocalBuddy(user->getRosterManager(), -1);
 
// 		handleBuddyPayload(buddy, payload);
 
// 		user->getRosterManager()->setBuddy(buddy);
 
// 	}
 
	std::cout << payload.nickname() << "\n";
 
}
 

	
 
void NetworkPluginServer::handleRoomChangedPayload(const std::string &data) {
 
	pbnetwork::Room payload;
 
	if (payload.ParseFromString(data) == false) {
 
		return;
 
	}
 

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

	
 
	NetworkConversation *conv = (NetworkConversation *) user->getConversationManager()->getConversation(payload.room());
 
	if (!conv) {
 
		return;
 
	}
 

	
 
	conv->setNickname(payload.nickname());
 
}
 

	
 
void NetworkPluginServer::handleConvMessagePayload(const std::string &data) {
 
	pbnetwork::ConversationMessage payload;
 
// 	std::cout << "payload...\n";
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 
// 	std::cout << "payload 2...\n";
 
	User *user = m_userManager->getUser(payload.username());
 
	if (!user)
 
		return;
 

	
 
	boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
	msg->setBody(payload.message());
 

	
 
	NetworkConversation *conv = (NetworkConversation *) user->getConversationManager()->getConversation(payload.buddyname());
 
	if (!conv) {
 
		conv = new NetworkConversation(user->getConversationManager(), payload.buddyname());
 
		conv->onMessageToSend.connect(boost::bind(&NetworkPluginServer::handleMessageReceived, this, _1, _2));
 
	}
 
	boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
	msg->setBody(payload.message());
 
	conv->handleMessage(msg);
 
	else {
 
		// groupchat messages can be created only for conversations initiated from XMPP side, not from legacy network side.
 
		// ie. you can't create Groupchat conversation from legacy network side.
 
		if (!payload.nickname().empty()) {
 
			msg->setType(Swift::Message::Groupchat);
 
		}
 
	}
 

	
 
	
 
	conv->handleMessage(msg, payload.nickname());
 
}
 

	
 
void NetworkPluginServer::handleDataRead(boost::shared_ptr<Swift::Connection> c, const Swift::ByteArray &data) {
 
	long expected_size = 0;
 
	m_data += data.toString();
 
// 	std::cout << "received data; size = " << m_data.size() << "\n";
 
	while (m_data.size() != 0) {
 
		if (m_data.size() >= 4) {
 
			unsigned char * head = (unsigned char*) m_data.c_str();
 
			expected_size = (((((*head << 8) | *(head + 1)) << 8) | *(head + 2)) << 8) | *(head + 3);
 
			//expected_size = m_data[0];
 
// 			std::cout << "expected_size=" << expected_size << "\n";
 
			if (m_data.size() - 4 < expected_size)
 
				return;
 
		}
 
		else {
 
			return;
 
		}
 

	
 
		std::string msg = m_data.substr(4, expected_size);
 
		m_data.erase(0, 4 + expected_size);
 

	
 
		pbnetwork::WrapperMessage wrapper;
 
		if (wrapper.ParseFromString(msg) == false) {
 
			// TODO: ERROR
 
			return;
 
		}
 

	
 
		switch(wrapper.type()) {
 
			case pbnetwork::WrapperMessage_Type_TYPE_CONNECTED:
 
				handleConnectedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_DISCONNECTED:
 
				handleDisconnectedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED:
 
				handleBuddyChangedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_CONV_MESSAGE:
 
				handleConvMessagePayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_PONG:
 
				m_pongReceived = true;
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_PARTICIPANT_CHANGED:
 
				handleParticipantChangedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_ROOM_NICKNAME_CHANGED:
 
@@ -378,73 +388,73 @@ void NetworkPluginServer::handleRoomJoined(User *user, const std::string &r, con
 
	send(m_client, message);
 

	
 
	NetworkConversation *conv = new NetworkConversation(user->getConversationManager(), r);
 
	conv->onMessageToSend.connect(boost::bind(&NetworkPluginServer::handleMessageReceived, this, _1, _2));
 
	conv->setNickname(nickname);
 
}
 

	
 
void NetworkPluginServer::handleRoomLeft(User *user, const std::string &r) {
 
	UserInfo userInfo = user->getUserInfo();
 

	
 
	pbnetwork::Room room;
 
	room.set_username(user->getJID().toBare());
 
	room.set_nickname("");
 
	room.set_room(r);
 
	room.set_password("");
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_LEAVE_ROOM);
 
 
 
	send(m_client, message);
 

	
 
	NetworkConversation *conv = (NetworkConversation *) user->getConversationManager()->getConversation(r);
 
	if (!conv) {
 
		return;
 
	}
 

	
 
	delete conv;
 
}
 

	
 
void NetworkPluginServer::handleUserDestroyed(User *user) {
 
	UserInfo userInfo = user->getUserInfo();
 

	
 
	pbnetwork::Logout logout;
 
	logout.set_user(user->getJID().toBare());
 
	logout.set_legacyname(userInfo.uin);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_LOGOUT);
 
 
 
	send(m_client, message);
 
}
 

	
 
void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost::shared_ptr<Swift::Message> &msg) {
 
	pbnetwork::ConversationMessage m;
 
	m.set_username(conv->getConversationManager()->getUser()->getUserInfo().uin);
 
	m.set_username(conv->getConversationManager()->getUser()->getJID().toBare());
 
	m.set_buddyname(conv->getLegacyName());
 
	m.set_message(msg->getBody());
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_CONV_MESSAGE);
 

	
 
	send(m_client, message);
 
}
 

	
 
void NetworkPluginServer::sendPing() {
 

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

	
 
	send(m_client, message);
 
	m_pongReceived = false;
 
	std::cout << "SENDING PING\n";
 
}
 

	
 
}
src/pbnetwork.proto
Show inline comments
 
package pbnetwork;
 

	
 
message Connected {
 
	required string user = 1;
 
	required string name = 2;
 
}
 

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

	
 
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;
 
	required string alias = 3;
 
	required string groups = 4;
 
	required int32 status = 5;
 
	optional string statusMessage = 6;
 
	optional string iconHash = 7;
 
}
 

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

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

	
 
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_PARTICIPANT_LEFT		= 14;
 
		TYPE_ROOM_NICKNAME_CHANGED	= 15;
 
	}
 
	required Type type = 1;
 
	optional bytes payload = 2;
 
}
 
;
 
\ No newline at end of file
0 comments (0 inline, 0 general)