Changeset - eabd4012f4c5
[Not reviewed]
Merge
1 62 191
Vitaly Takmazov - 13 years ago 2012-08-01 11:26:13
vitalyster@gmail.com
Merge branch 'master' of https://github.com/hanzz/libtransport
76 files changed with 18951 insertions and 333 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
project(libtransport)
 

	
 
if(NOT LIB_INSTALL_DIR)
 
	set(LIB_INSTALL_DIR "lib")
 
endif()
 

	
 
set(CMAKE_MODULE_PATH "cmake_modules")
 

	
 
set(cppunit_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(cppunit)
 

	
 
if (NOT CMAKE_COMPILER_IS_GNUCXX)
 
ADD_SUBDIRECTORY(msvc-deps)
 
else()
 
if (WIN32)
 
ADD_SUBDIRECTORY(msvc-deps/sqlite3)
 
else()
 
set(sqlite3_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(sqlite3)
 
endif()
 
endif()
 

	
 
set(mysql_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(mysql)
 

	
 
set(purple_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(purple)
 

	
 
set(glib_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(glib)
 

	
 
set(libxml2_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(libxml2)
 

	
 

	
 
if (NOT WIN32)
 
	set(popt_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(popt)
 
endif()
 

	
 
set(event_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(event)
 

	
 
set(Swiften_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(Swiften)
 

	
 
if (CMAKE_COMPILER_IS_GNUCXX)
 
set(openssl_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(openssl)
 
endif()
 

	
 
set(Boost_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
if (WIN32)
 
set(Boost_USE_STATIC_LIBS      ON)
 
set(Boost_USE_MULTITHREADED      ON)
 
set(Boost_USE_STATIC_RUNTIME    OFF)
 
endif()
 
find_package(Boost COMPONENTS program_options date_time system filesystem regex  signals REQUIRED)
 
message( STATUS "Found Boost: ${Boost_LIBRARIES}, ${Boost_INCLUDE_DIR}")
 

	
 
set(Protobuf_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(Protobuf REQUIRED)
 

	
 
set(Communi_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(Communi)
 

	
 
set(log4cxx_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(log4cxx)
 

	
 
set(event_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(event)
 

	
 
set(pqxx_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(pqxx)
 

	
 
if (NOT WIN32)
 
set(dbus_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(dbus)
 
endif()
 

	
 
set(yahoo2_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(yahoo2)
 

	
 
find_package(Doxygen)
 

	
 
INCLUDE(FindQt4)
 
FIND_PACKAGE(Qt4 COMPONENTS QtCore QtNetwork)
 

	
 
# ADD_DEFINITIONS(${SWIFTEN_CFLAGS})
 
ADD_DEFINITIONS(-DSUPPORT_LEGACY_CAPS)
 
ADD_DEFINITIONS(-DBOOST_FILESYSTEM_VERSION=2)
 

	
 
message("  Supported features")
 
message("-----------------------")
 

	
 
if (SPECTRUM_VERSION)
 
	ADD_DEFINITIONS(-DSPECTRUM_VERSION="${SPECTRUM_VERSION}")
 
else (SPECTRUM_VERSION)
 
	if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
 
		if (NOT GIT_EXECUTABLE)
 
		set (GIT_EXECUTABLE git)
 
		endif()
 
		execute_process(COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git" rev-parse --short HEAD
 
						OUTPUT_VARIABLE GIT_REVISION
 
						OUTPUT_STRIP_TRAILING_WHITESPACE
 
		)
 
		set(SPECTRUM_VERSION 2.0.0-beta-git-${GIT_REVISION})
 
		ADD_DEFINITIONS(-DSPECTRUM_VERSION="${SPECTRUM_VERSION}")
 
	else (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
 
		set(SPECTRUM_VERSION 2.0.0-alpha)
 
		ADD_DEFINITIONS(-DSPECTRUM_VERSION="${SPECTRUM_VERSION}")
 
	endif (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
 
endif (SPECTRUM_VERSION)
 

	
 
message("Version           : " ${SPECTRUM_VERSION})
 

	
 
if (SQLITE3_FOUND)
 
	ADD_DEFINITIONS(-DWITH_SQLITE)
 
	include_directories(${SQLITE3_INCLUDE_DIR})
 
	message("SQLite3           : yes")
 
else (SQLITE3_FOUND)
 
if (WIN32)
 
	ADD_DEFINITIONS(-DWITH_SQLITE)
 
	include_directories(msvc-deps/sqlite3)
 
	message("SQLite3           : bundled")
 
else()
 
	set(SQLITE3_LIBRARIES "")
 
	message("SQLite3           : no")
 
endif()
 
endif (SQLITE3_FOUND)
 

	
 
if (MYSQL_FOUND)
 
	ADD_DEFINITIONS(-DWITH_MYSQL)
 
	include_directories(${MYSQL_INCLUDE_DIR})
 
	message("MySQL             : yes")
 
else (MYSQL_FOUND)
 
	set(MYSQL_LIBRARIES "")
 
	message("MySQL             : no (install mysql-devel)")
 
endif (MYSQL_FOUND)
 

	
 
if (PQXX_FOUND)
 
	ADD_DEFINITIONS(-DWITH_PQXX)
 
	include_directories(${PQXX_INCLUDE_DIR})
 
	message("PostgreSQL        : yes")
 
else (PQXX_FOUND)
 
	set(PQXX_LIBRARY "")
 
	set(PQ_LIBRARY "")
 
	message("PostgreSQL        : no (install libpqxx-devel)")
 
endif (PQXX_FOUND)
 

	
 
if (PROTOBUF_FOUND)
 
	ADD_DEFINITIONS(-DWITH_PROTOBUF)
 
	include_directories(${PROTOBUF_INCLUDE_DIRS})
 
	message("Network plugins   : yes")
 

	
 
	if(PURPLE_LIBRARY AND PURPLE_INCLUDE_DIR)
 
		message("Libpurple plugin  : yes")
 
		include_directories(${PURPLE_INCLUDE_DIR})
 
		include_directories(${GLIB2_INCLUDE_DIR})
 
	else()
 
		message("Libpurple plugin  : no (install libpurple)")
 
	endif()
 

	
 
	if (HAVE_EVENT)
 
		ADD_DEFINITIONS(-DWITH_LIBEVENT)
 
		include_directories(${EVENT_INCLUDE_DIRS})
 
		message("  libev eventloop : yes")
 
	else()
 
		message("  libev eventloop : no (install libev-devel)")
 
	endif()
 

	
 
	if(IRC_FOUND)
 
		ADD_DEFINITIONS(-DCOMMUNI_SHARED)
 
		message("IRC plugin        : yes")
 
		include_directories(${QT_QTNETWORK_INCLUDE_DIR})
 
		include_directories(${IRC_INCLUDE_DIR})
 
		include(${QT_USE_FILE})
 
	else()
 
		message("IRC plugin        : no (install libCommuni and libprotobuf-dev)")
 
	endif()
 

	
 
if (NOT WIN32)
 
	message("Frotz plugin      : yes")
 
	message("SMSTools3 plugin  : yes")
 
else()
 
	message("Frotz plugin      : no")
 
	message("SMSTools3 plugin  : no")
 
	if(${LIBDBUSGLIB_FOUND})
 
		message("Skype plugin      : yes")
 
		include_directories(${LIBDBUSGLIB_INCLUDE_DIRS})
 
	else()
 
		message("Skype plugin      : no (install dbus-glib-devel)")
 
	endif()
 
endif()
 

	
 
#	We have our own copy now...
 
# 	if(YAHOO2_FOUND)
 
		message("Libyahoo2 plugin  : yes")
 
# 		include_directories(${YAHOO2_INCLUDE_DIR})
 
# 	else()
 
# 		message("Libyahoo2 plugin  : no (install libyahoo2-devel)")
 
# 	endif()
 

	
 
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)")
 
	message("SMSTools3 plugin  : no (install libprotobuf-dev)")
 
endif()
 

	
 
if (LOG4CXX_FOUND)
 
	message("Log4cxx           : yes")
 
	include_directories(${LOG4CXX_INCLUDE_DIR})
 
	ADD_DEFINITIONS(-DWITH_LOG4CXX)
 
else()
 
	set(LOG4CXX_LIBRARIES "")
 
	message("Log4cxx           : no (install log4cxx-devel)")
 
endif()
 

	
 
if (WIN32)
 
ADD_DEFINITIONS(-DLOG4CXX_STATIC)
 
ADD_DEFINITIONS(-D_WIN32_WINNT=0x501)
 
ADD_DEFINITIONS(-DWIN32_LEAN_AND_MEAN)
 
ADD_DEFINITIONS(-DBOOST_USE_WINDOWS_H)
 
ADD_DEFINITIONS(-DBOOST_THREAD_USE_LIB)
 
endif()
 

	
 
if(CMAKE_BUILD_TYPE MATCHES Debug)
 
if (CMAKE_COMPILER_IS_GNUCXX)
 
	ADD_DEFINITIONS(-O3)
 
	ADD_DEFINITIONS(-ggdb)
 
endif()
 
	ADD_DEFINITIONS(-DDEBUG)
 
	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})
 

	
 
if (CMAKE_COMPILER_IS_GNUCXX)
 
include_directories(${OPENSSL_INCLUDE_DIR})
 
endif()
 

	
 
ADD_SUBDIRECTORY(src)
 
ADD_SUBDIRECTORY(plugin)
 
ADD_SUBDIRECTORY(include)
 
ADD_SUBDIRECTORY(spectrum)
 
ADD_SUBDIRECTORY(backends)
 
if (NOT WIN32)
 
	ADD_SUBDIRECTORY(spectrum_manager)
 
#	ADD_SUBDIRECTORY(spectrum2_send_message)
 
endif()
 

	
 
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("----------------------")
ChangeLog
Show inline comments
 
Version 2.0.0-beta3 (2012-XX-XX):
 
	General:
 
	* Log errors related to backend spawning (Show proper log message for
 
	  example when path to backend binary is not found).
 
	* Update buddies in database only when it's needed and do not execute
 
	  useless database statements.
 
	* Send roster changes also when buddy's group change.
 
	* Fixed bug when transport contact we in all groups.
 
	* Answer to disco#info IQ with proper node (#206).
 
	* Set SQLite3 as default database backend.
 
	* Fixed disconnecting from server caused by sending VCard response
 
	  with bad "from" attribute.
 
	* Added Munin plugin (Thanks to Askovpen).
 
	* Added support for more admin_jid JIDs (Thanks to Askovpen).
 
	* Fixed allowed_servers option.
 
	* Show error in server-mode when server port is already used.
 
	* Fixed bug when backend could freeze on exit.
 
	* Options from config file can now be set also using command line like
 
	  --service.jid=domain.tld .
 
	* Do not send password in IQ-get registration response.
 

	
 
	Libpurple:
 
	* prpl-gg: Fetch the contact list properly (#252).
 
	* Added support for prpl-novell as it was in spectrum1.
 

	
 
	Skype:
 
	* Log more errors.
 

	
 
	Libyahoo2:
 
	* Added new Yahoo backend based on libyahoo2.
 

	
 
	Swiften:
 
	* Added new XMPP backend based on Swiften library.
 

	
 
	Backend API:
 
	* Added Python NetworkPlugin class, so it is now easier to write backends
 
	  in Python (Thanks to Sarang).
 

	
 
Version 2.0.0-beta2 (2012-03-28):
 
	General:
 
	* Fixed bug when Roster Item Exchange and subscribe stanzas were sent
 
	  repeatedly.
 
	* Backends related logs now contain the backend PID.
 
	* Fixed username_mask setting.
 
	* Added new fields into statistics (backends_crashed, messages related
 
	  stats).
 
	* Chatstates are now not counted as incoming messages in stats.
 
	* Log4cxx is now optional dependency. Without Log4cxx, Spectrum 2 logs
 
	  to standard output.
 
	* Fixed crash when Log4cxx configuration file didn't exist.
 
	* Admin can now see "Admin" contact in server-mode.
 

	
 
	libpurple:
 
	* Added initial support for MUC for prpl-jabber protocol.
 

	
 
	LibCommuni IRC backend:
 
	* Fixed sending/receiving UTF8 messages.
 
	* Using the [registration] auto_register=1 config option, users don't
 
	  have to register manually when connecting IRC network.
 

	
 
	Skype:
 
	* Memory usage statistic now includes the Skype client.
 
	* Fixed logging issue when the logs were not stored in the proper instance
 
	  directory.
 
	* Skype backend includes also Skype client memory usage into the account.
 
	* Working buddies adding/removing.
 
	* Information about missed call is now forwarded to XMPP user.
 
	* Fixed bug when Skype client instance wasn't killed by backend.
 

	
 
Version 2.0.0-beta (2012-02-28):
 
	General:
 
	* Added PostreSQL support (thanks to Jadestorm).
 
	* Added XEP-0100 (Gateway interaction) support.
 
	* Send presences only "from" bare JID (fixed bug with buddies appearing
 
	  twice in the roster and potential unregistering issues).
 
	* Fixed potential MySQL/SQLite3 deadlocks.
 
	* Fixed disconnecting in server-mode when client does not send unavailable
 
	  presence before disconnection.
 
	* Fixed crash in server-mode when client send its custom jabber:iq:storage
 
	  payload.
 
	* Fixed registration from Pidgin.
 
	* Unsubscribe presence sent to some buddy doesn't disconnect the account.
 
	* Remote Roster requests are not sent to resources, but to bare JID.
 
	* Added automatic reconnection in case of non-fatal error.
 
	* Added more error messages.
 

	
 
	Skype:
 
	* Initial support for Skype added, read more on
 
	  http://spectrum.im/projects/spectrum/wiki/Spectrum_2_Admin_-_Skype_backend
 

	
 
	SMSTools3:
 
	* Initial support for SMSTools3, read more on
 
	http://spectrum.im/projects/spectrum/wiki/Spectrum_2_Admin_-_SMSTools3_backend
 

	
 
version 2.0.0 alpha (2011-12-06):
 
	General:
 
	* First Spectrum 2.0.0 alpha release, check more on
 
	  http://spectrum.im/projects/spectrum/wiki/Spectrum_200_alpha
backends/CMakeLists.txt
Show inline comments
 
if (PROTOBUF_FOUND)
 
	if ( PURPLE_LIBRARY AND PURPLE_INCLUDE_DIR )
 
		ADD_SUBDIRECTORY(libpurple)
 
	endif()
 

	
 
	if (IRC_FOUND)
 
		ADD_SUBDIRECTORY(libcommuni)
 
	endif()
 

	
 
	ADD_SUBDIRECTORY(swiften)
 

	
 
# 	if(YAHOO2_FOUND)
 
		ADD_SUBDIRECTORY(libyahoo2)
 
# 	endif()
 

	
 
	ADD_SUBDIRECTORY(template)
 

	
 
if (NOT WIN32)
 
	ADD_SUBDIRECTORY(smstools3)
 
	ADD_SUBDIRECTORY(frotz)
 
	if (${LIBDBUSGLIB_FOUND})
 
		ADD_SUBDIRECTORY(skype)
 
	endif()
 
endif()
 

	
 
endif()
backends/libcommuni/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 "transport/logging.h"
 
#include "session.h"
 
#include <QtCore>
 
#include <QtNetwork>
 
#include "Swiften/EventLoop/Qt/QtEventLoop.h"
 
#include "ircnetworkplugin.h"
 
#include "singleircnetworkplugin.h"
 

	
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
NetworkPlugin * np = NULL;
 

	
 
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 (...)
 
	{
 
		std::cout << desc << "\n";
 
		exit(1);
 
	}
 

	
 

	
 
	if (argc < 5) {
 
		qDebug("Usage: %s <config>", argv[0]);
 
		return 1;
 
	}
 

	
 
// 	QStringList channels;
 
// 	for (int i = 3; i < argc; ++i)
 
// 	{
 
// 		channels.append(argv[i]);
 
// 	}
 
// 
 
// 	MyIrcSession session;
 
// 	session.setNick(argv[2]);
 
// 	session.setAutoJoinChannels(channels);
 
// 	session.connectToServer(argv[1], 6667);
 

	
 
	Config config;
 
	if (!config.load(argv[5])) {
 
		std::cerr << "Can't open " << argv[1] << " configuration file.\n";
 
		return 1;
 
	}
 
	QCoreApplication app(argc, argv);
 

	
 
	Logging::initBackendLogging(&config);
 

	
 
	Swift::QtEventLoop eventLoop;
 

	
 
	if (config.getUnregistered().find("service.irc_server") == config.getUnregistered().end()) {
 
	if (!CONFIG_HAS_KEY(&config, "service.irc_server")) {
 
		np = new IRCNetworkPlugin(&config, &eventLoop, host, port);
 
	}
 
	else {
 
		np = new SingleIRCNetworkPlugin(&config, &eventLoop, host, port);
 
	}
 

	
 
	return app.exec();
 
}
backends/libcommuni/singleircnetworkplugin.cpp
Show inline comments
 
#include "singleircnetworkplugin.h"
 
#include "transport/logging.h"
 
#include <IrcCommand>
 
#include <IrcMessage>
 

	
 
#define FROM_UTF8(WHAT) QString::fromUtf8((WHAT).c_str(), (WHAT).size())
 
#define TO_UTF8(WHAT) std::string((WHAT).toUtf8().data(), (WHAT).toUtf8().size())
 

	
 
DEFINE_LOGGER(logger, "SingleIRCNetworkPlugin");
 

	
 
SingleIRCNetworkPlugin::SingleIRCNetworkPlugin(Config *config, Swift::QtEventLoop *loop, const std::string &host, int port) {
 
	this->config = config;
 
	m_server = config->getUnregistered().find("service.irc_server")->second;
 
	if (CONFIG_HAS_KEY(config, "service.irc_server")) {
 
		m_server = CONFIG_STRING(config, "service.irc_server");
 
	}
 
	else {
 
		LOG4CXX_ERROR(logger, "No [service] irc_server defined, exiting...");
 
		exit(-1);
 
	}
 
	m_socket = new QTcpSocket();
 
	m_socket->connectToHost(FROM_UTF8(host), port);
 
	connect(m_socket, SIGNAL(readyRead()), this, SLOT(readData()));
 

	
 
	if (config->getUnregistered().find("service.irc_identify") != config->getUnregistered().end()) {
 
		m_identify = config->getUnregistered().find("service.irc_identify")->second;
 
	if (CONFIG_HAS_KEY(config, "service.irc_identify")) {
 
		m_identify = CONFIG_STRING(config, "service.irc_identify");
 
	}
 
	else {
 
		m_identify = "NickServ identify $name $password";
 
	}
 

	
 
	LOG4CXX_INFO(logger, "SingleIRCNetworkPlugin for server " << m_server << " initialized.");
 
}
 

	
 
void SingleIRCNetworkPlugin::readData() {
 
	size_t availableBytes = m_socket->bytesAvailable();
 
	if (availableBytes == 0)
 
		return;
 

	
 
	std::string d = std::string(m_socket->readAll().data(), availableBytes);
 
	handleDataRead(d);
 
}
 

	
 
void SingleIRCNetworkPlugin::sendData(const std::string &string) {
 
	m_socket->write(string.c_str(), string.size());
 
}
 

	
 
void SingleIRCNetworkPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
	// legacy name is users nickname
 
	if (m_sessions[user] != NULL) {
 
		LOG4CXX_WARN(logger, user << ": Already logged in.");
 
		return;
 
	}
 
	LOG4CXX_INFO(logger, user << ": Connecting " << m_server << " as " << legacyName);
 

	
 
	MyIrcSession *session = new MyIrcSession(user, this);
 
	session->setUserName(FROM_UTF8(legacyName));
 
	session->setNickName(FROM_UTF8(legacyName));
 
	session->setRealName(FROM_UTF8(legacyName));
 
	session->setHost(FROM_UTF8(m_server));
 
	session->setPort(6667);
 
	session->setEncoding( "utf-8" );
 

	
 
	if (!password.empty()) {
 
		std::string identify = m_identify;
 
		boost::replace_all(identify, "$password", password);
 
		boost::replace_all(identify, "$name", legacyName);
 
		session->setIdentify(identify);
 
	}
 

	
 
	session->open();
 

	
 
	m_sessions[user] = session;
 
}
 

	
 
void SingleIRCNetworkPlugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
	if (m_sessions[user] == NULL) {
 
		LOG4CXX_WARN(logger, user << ": Already disconnected.");
 
		return;
 
	}
 
	LOG4CXX_INFO(logger, user << ": Disconnecting.");
 

	
 
	m_sessions[user]->close();
 
	m_sessions[user]->deleteLater();
 
	m_sessions.erase(user);
 
}
 

	
 
void SingleIRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &/*xhtml*/) {
 
	if (m_sessions[user] == NULL) {
 
		LOG4CXX_WARN(logger, user << ": Message received for unconnected user");
 
		return;
 
	}
 

	
 
	// handle PMs
 
	std::string r = legacyName;
 
	if (legacyName.find("/") == std::string::npos) {
 
		r = legacyName.substr(0, r.find("@"));
 
	}
 
	else {
 
		r = legacyName.substr(legacyName.find("/") + 1);
 
	}
 

	
 
	LOG4CXX_INFO(logger, user << ": Forwarding message to " << r);
 
	m_sessions[user]->sendCommand(IrcCommand::createMessage(FROM_UTF8(r), FROM_UTF8(message)));
 

	
 
	if (r.find("#") == 0) {
 
		handleMessage(user, legacyName, message, TO_UTF8(m_sessions[user]->nickName()));
 
	}
 
}
 

	
 
void SingleIRCNetworkPlugin::handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password) {
 
	if (m_sessions[user] == NULL) {
 
		LOG4CXX_WARN(logger, user << ": Join room requested for unconnected user");
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, user << ": Joining " << room);
 
	m_sessions[user]->addAutoJoinChannel(room);
 
	m_sessions[user]->sendCommand(IrcCommand::createJoin(FROM_UTF8(room), FROM_UTF8(password)));
 
	m_sessions[user]->rooms += 1;
 

	
 
	// update nickname, because we have nickname per session, no nickname per room.
 
	handleRoomNicknameChanged(user, room, TO_UTF8(m_sessions[user]->userName()));
 
}
 

	
 
void SingleIRCNetworkPlugin::handleLeaveRoomRequest(const std::string &user, const std::string &room) {
 
	std::string r = room;
 
	std::string u = user;
 

	
 
	if (m_sessions[u] == NULL) {
 
		LOG4CXX_WARN(logger, user << ": Leave room requested for unconnected user");
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, user << ": Leaving " << room);
 
	m_sessions[u]->sendCommand(IrcCommand::createPart(FROM_UTF8(r)));
 
	m_sessions[u]->removeAutoJoinChannel(r);
 
	m_sessions[u]->rooms -= 1;
 
}
backends/libpurple/main.cpp
Show inline comments
 
#include "utils.h"
 

	
 
#include "glib.h"
 
#include "purple.h"
 
#include <algorithm>
 
#include <iostream>
 

	
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 
#include "transport/config.h"
 
#include "transport/logging.h"
 
#include "geventloop.h"
 

	
 
// #include "valgrind/memcheck.h"
 
#include "malloc.h"
 
#include <algorithm>
 
#include "errno.h"
 

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

	
 
#ifdef WIN32
 
#include "win32/win32dep.h"
 
#define ssize_t SSIZE_T
 
#include <process.h>
 
#define getpid _getpid
 
#endif
 

	
 
#include "purple_defs.h"
 

	
 
DEFINE_LOGGER(logger_libpurple, "libpurple");
 
DEFINE_LOGGER(logger, "backend");
 

	
 
int main_socket;
 
static int writeInput;
 

	
 
using namespace Transport;
 

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

	
 
static std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
 
    std::stringstream ss(s);
 
    std::string item;
 
    while(std::getline(ss, item, delim)) {
 
        elems.push_back(item);
 
    }
 
    return elems;
 
}
 

	
 

	
 
static std::vector<std::string> split(const std::string &s, char delim) {
 
    std::vector<std::string> elems;
 
    return split(s, delim, elems);
 
}
 

	
 
static void transportDataReceived(gpointer data, gint source, PurpleInputCondition cond);
 

	
 
class SpectrumNetworkPlugin;
 

	
 
GKeyFile *keyfile;
 
SpectrumNetworkPlugin *np;
 

	
 
static std::string replaceAll(
 
  std::string result,
 
  const std::string& replaceWhat,
 
  const std::string& replaceWithWhat)
 
{
 
  while(1)
 
  {
 
	const int pos = result.find(replaceWhat);
 
	if (pos==-1) break;
 
	result.replace(pos,replaceWhat.size(),replaceWithWhat);
 
  }
 
  return result;
 
}
 

	
 
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);
 
	g_free(str);
 

	
 
	if (ret.find("#") != std::string::npos) {
 
		ret = ret.substr(0, ret.find("#"));
 
		while(*(ret.end() - 1) == ' ') {
 
			ret.erase(ret.end() - 1);
 
		}
 
	}
 

	
 
	if (ret.find("$jid") != std::string::npos) {
 
		std::string jid = KEYFILE_STRING("service", "jid");
 
		ret = replaceAll(ret, "$jid", jid);
 
	}
 
	return ret;
 
}
 

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

	
 
static gchar *host = NULL;
 
static int port = 10000;
 

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

	
 
static GOptionEntry options_entries[] = {
 
	{ "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 gboolean ft_ui_ready(void *data) {
 
	PurpleXfer *xfer = (PurpleXfer *) data;
 
	FTData *ftdata = (FTData *) xfer->ui_data;
 
	ftdata->timer = 0;
 
	purple_xfer_ui_ready((PurpleXfer *) data);
 
	return FALSE;
 
}
 

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

	
 
static void * requestInput(const char *title, const char *primary,const char *secondary, const char *default_value, gboolean multiline, gboolean masked, gchar *hint,const char *ok_text, GCallback ok_cb,const char *cancel_text, GCallback cancel_cb, PurpleAccount *account, const char *who,PurpleConversation *conv, void *user_data) {
 
	if (primary) {
 
		std::string primaryString(primary);
 
		if (primaryString == "Authorization Request Message:") {
 
			LOG4CXX_INFO(logger, "Authorization Request Message: calling ok_cb(...)");
 
			((PurpleRequestInputCb) ok_cb)(user_data, "Please authorize me.");
 
			return NULL;
 
		}
 
		else if (primaryString == "Authorization Request Message:") {
 
			LOG4CXX_INFO(logger, "Authorization Request Message: calling ok_cb(...)");
 
			((PurpleRequestInputCb) ok_cb)(user_data, "Please authorize me.");
 
			return NULL;
 
		}
 
		else if (primaryString == "Authorization Denied Message:") {
 
			LOG4CXX_INFO(logger, "Authorization Deined Message: calling ok_cb(...)");
 
			((PurpleRequestInputCb) ok_cb)(user_data, "Authorization denied.");
 
			return NULL;
 
		}
 
		else {
 
			LOG4CXX_WARN(logger, "Unhandled request input. primary=" << primaryString);
 
		}
 
	}
 
	else if (title) {
 
		std::string titleString(title);
 
		if (titleString == "Xfire Invitation Message") {
 
			LOG4CXX_INFO(logger, "Authorization Request Message: calling ok_cb(...)");
 
			((PurpleRequestInputCb) ok_cb)(user_data, "Please authorize me.");
 
			return NULL;
 
		}
 
		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:
 
		SpectrumNetworkPlugin() : NetworkPlugin() {
 

	
 
		}
 

	
 
		void handleExitRequest() {
 
			LOG4CXX_INFO(logger, "Exiting...");
 
			exit(0);
 
		}
 

	
 
		void getProtocolAndName(const std::string &legacyName, std::string &name, std::string &protocol) {
 
			name = legacyName;
 
			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 (!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 (!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) {
 
			int i = 0;
 
			gchar **keys = g_key_file_get_keys (keyfile, "purple", NULL, NULL);
 
			while (keys && keys[i] != NULL) {
 
				std::string key = keys[i];
 

	
 
				if (key == "fb_api_key" || key == "fb_api_secret") {
 
					purple_account_set_bool(account, "auth_fb", TRUE);
 
				}
 

	
 
				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));
 
					if (key != key2) {
 
						continue;
 
					}
 
					
 
					found = true;
 
					switch (type) {
 
						case PURPLE_PREF_BOOLEAN:
 
							purple_account_set_bool(account, key.c_str(), fromString<bool>(KEYFILE_STRING("purple", key)));
 
							break;
 

	
 
						case PURPLE_PREF_INT:
 
							purple_account_set_int(account, key.c_str(), fromString<int>(KEYFILE_STRING("purple", key)));
 
							break;
 

	
 
						case PURPLE_PREF_STRING:
 
						case PURPLE_PREF_STRING_LIST:
 
							purple_account_set_string(account, key.c_str(), KEYFILE_STRING("purple", key).c_str());
 
							break;
 
						default:
 
							continue;
 
					}
 
					break;
 
				}
 

	
 
				if (!found) {
 
					purple_account_set_string(account, key.c_str(), KEYFILE_STRING("purple", key).c_str());
 
				}
 
				i++;
 
			}
 
			g_strfreev (keys);
 

	
 
			char* contents;
 
			gsize length;
 
			gboolean ret = g_file_get_contents ("gfire.cfg", &contents, &length, NULL);
 
			if (ret) {
 
				purple_account_set_int(account, "version", fromString<int>(std::string(contents, length)));
 
			}
 

	
 

	
 
			if (KEYFILE_STRING("service", "protocol") == "prpl-novell") {
 
				std::string username(purple_account_get_username(account));
 
				std::vector <std::string> u = split(username, '@');
 
				purple_account_set_username(account, (const char*) u.front().c_str());
 
				std::vector <std::string> s = split(u.back(), ':'); 
 
				purple_account_set_string(account, "server", s.front().c_str());
 
				purple_account_set_int(account, "port", atoi(s.back().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()) {
 
				LOG4CXX_INFO(logger,  name.c_str() << ": Empty password");
 
				np->handleDisconnected(user, 0, "Empty password.");
 
				return;
 
			}
 

	
 
			if (!purple_find_prpl(protocol.c_str())) {
 
				LOG4CXX_INFO(logger,  name.c_str() << ": Invalid protocol '" << protocol << "'");
 
				np->handleDisconnected(user, 0, "Invalid protocol " + protocol);
 
				return;
 
			}
 

	
 
			if (purple_accounts_find(name.c_str(), protocol.c_str()) != NULL) {
 
				LOG4CXX_INFO(logger, "Using previously created account with name '" << name.c_str() << "' and protocol '" << protocol << "'");
 
				account = purple_accounts_find(name.c_str(), protocol.c_str());
 
				if (m_accounts.find(account) != m_accounts.end() && m_accounts[account] != user) {
 
					LOG4CXX_INFO(logger, "Account '" << name << "' is already used by '" << m_accounts[account] << "'");
 
					np->handleDisconnected(user, 0, "Account '" + name + "' is already used by '" + m_accounts[account] + "'");
 
					return;
 
				}
 
				LOG4CXX_INFO(logger, "Using previously created account with name '" << name.c_str() << "' and protocol '" << protocol << "'");
 
			}
 
			else {
 
				LOG4CXX_INFO(logger, "Creating account with name '" << name.c_str() << "' and protocol '" << protocol << "'");
 
				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);
 

	
 
			// Enable account + privacy lists
 
			purple_account_set_enabled(account, "spectrum", TRUE);
 
			if (KEYFILE_BOOL("service", "enable_privacy_lists")) {
 
				purple_account_set_privacy_type(account, PURPLE_PRIVACY_DENY_USERS);
 
			}
 

	
 
			// Set the status
 
			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) {
 
				if (purple_account_get_int(account, "version", 0) != 0) {
 
					std::string data = stringOf(purple_account_get_int(account, "version", 0));
 
					g_file_set_contents ("gfire.cfg", data.c_str(), data.size(), NULL);
 
				}
 
// 				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);
 
#ifndef WIN32
 
				malloc_trim(0);
 
#endif
 
// 				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 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 pbnetwork::STATUS_DND: {
 
						st = PURPLE_STATUS_UNAVAILABLE;
 
						break;
 
					}
 
					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 pbnetwork::STATUS_NONE: {
 
						st = PURPLE_STATUS_OFFLINE;
 
						break;
 
					}
 
					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_CHAT, legacyName.c_str(), account);
 
				if (!conv) {
 
					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);
 
					if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) {
 
						purple_conv_im_send(PURPLE_CONV_IM(conv), _markup);
 
					}
 
					else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
 
						purple_conv_chat_send(PURPLE_CONV_CHAT(conv), _markup);
 
					}
 
					g_free(_markup);
 
				}
 
				else {
 
					if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) {
 
						purple_conv_im_send(PURPLE_CONV_IM(conv), xhtml.c_str());
 
					}
 
					else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
 
						purple_conv_chat_send(PURPLE_CONV_CHAT(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 (KEYFILE_STRING("service", "protocol") == "any" && legacyName.find("prpl-") == 0) {
 
					name = name.substr(name.find(".") + 1);
 
				}
 
				m_vcards[user + name] = id;
 

	
 
				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::vector<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::vector<std::string> &groups_) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (account) {
 
				std::string groups = groups_.empty() ? "" : groups_[0];
 

	
 
				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 (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 handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &pasword) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (!account) {
 
				return;
 
			}
 

	
 
			PurpleConnection *gc = purple_account_get_connection(account);
 
			GHashTable *comps = NULL;
 

	
 
			// Check if the PurpleChat is not stored in buddy list
 
			PurpleChat *chat = purple_blist_find_chat(account, room.c_str());
 
			if (chat) {
 
				comps = purple_chat_get_components(chat);
 
			}
 
			else if (PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl)->chat_info_defaults != NULL) {
 
				comps = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl)->chat_info_defaults(gc, room.c_str());
 
			}
 

	
 
			LOG4CXX_INFO(logger, user << ": Joining the room " << room);
 
			if (comps) {
 
				serv_join_chat(gc, comps);
 
				g_hash_table_destroy(comps);
 
			}
 
		}
 

	
 
		void handleLeaveRoomRequest(const std::string &user, const std::string &room) {
 
			PurpleAccount *account = m_sessions[user];
 
			if (!account) {
 
				return;
 
			}
 

	
 
			PurpleConversation *conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, room.c_str(), account);
 
			purple_conversation_destroy(conv);
 
		}
 

	
 
		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) {
 
#ifdef WIN32
 
			::send(main_socket, string.c_str(), string.size(), 0);
 
#else
 
			write(main_socket, string.c_str(), string.size());
 
#endif
 
			if (writeInput == 0)
 
				writeInput = purple_input_add(main_socket, PURPLE_INPUT_WRITE, &transportDataReceived, NULL);
 
		}
 

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

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

	
 
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 = pbnetwork::STATUS_AWAY;
 
			break;
 
		}
 
		case PURPLE_STATUS_UNAVAILABLE: {
 
			status = pbnetwork::STATUS_DND;
 
			break;
 
		}
 
		case PURPLE_STATUS_EXTENDED_AWAY: {
 
			status = pbnetwork::STATUS_XA;
 
			break;
 
		}
 
		case PURPLE_STATUS_OFFLINE: {
 
			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;
 
	if (purple_buddy_get_name(m_buddy)) {
 
		GSList *buddies = purple_find_buddies(purple_buddy_get_account(m_buddy), purple_buddy_get_name(m_buddy));
 
		while(buddies) {
 
			PurpleGroup *g = purple_buddy_get_group((PurpleBuddy *) buddies->data);
 
			buddies = g_slist_delete_link(buddies, buddies);
 

	
 
			if(g && purple_group_get_name(g)) {
 
				groups.push_back(purple_group_get_name(g));
 
			}
 
		}
 
	}
 

	
 
	if (groups.empty()) {
 
		groups.push_back("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);
 

	
 
	std::vector<std::string> groups = getGroups(buddy);
 
	LOG4CXX_INFO(logger, "Buddy updated " << np->m_accounts[account] << " " << purple_buddy_get_name(buddy) << " " << getAlias(buddy) << " group (" << groups.size() << ")=" << groups[0]);
 

	
 
	// Status
 
	pbnetwork::StatusType status = pbnetwork::STATUS_NONE;
 
	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 (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);
 
			}
 
		}
 
	}
 

	
 
	np->handleBuddyChanged(np->m_accounts[account], purple_buddy_get_name(buddy), getAlias(buddy), getGroups(buddy), 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) {
 
	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 (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM && (flags & PURPLE_MESSAGE_SEND || flags & PURPLE_MESSAGE_SYSTEM)) {
 
		return;
 
	}
 
	PurpleAccount *account = purple_conversation_get_account(conv);
 

	
 
// 	char *striped = purple_markup_strip_html(message);
 
// 	std::string msg = striped;
 
// 	g_free(striped);
 

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

	
 
	// Escape HTML characters.
 
	char *newline = purple_strdup_withhtml(msg);
 
	char *strip, *xhtml;
 
	purple_markup_html_to_xhtml(newline, &xhtml, &strip);
 
// 	xhtml_linkified = spectrum_markup_linkify(xhtml);
 
	std::string message_(strip);
 

	
 
	std::string xhtml_(xhtml);
 
	g_free(newline);
 
	g_free(xhtml);
 
// 	g_free(xhtml_linkified);
 
	g_free(strip);
 

	
 
	// AIM and XMPP adds <body>...</body> here...
 
	if (xhtml_.find("<body>") == 0) {
 
		xhtml_ = xhtml_.substr(6);
 
		if (xhtml_.find("</body>") != std::string::npos) {
 
			xhtml_ = xhtml_.substr(0, xhtml_.find("</body>"));
 
		}
 
	}
 

	
 
	if (xhtml_ == message_) {
 
		xhtml_ = "";
 
	}
 

	
 
// 	LOG4CXX_INFO(logger, "Received message body='" << message_ << "' xhtml='" << xhtml_ << "'");
 

	
 
	if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) {
 
		np->handleMessage(np->m_accounts[account], w, message_, "", xhtml_);
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, "Received message body='" << message_ << "' name='" << purple_conversation_get_name(conv) << "' " << w);
 
		np->handleMessage(np->m_accounts[account], purple_conversation_get_name(conv), message_, w, xhtml_);
 
	}
 
}
 

	
 
static void conv_chat_add_users(PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals) {
 
	PurpleAccount *account = purple_conversation_get_account(conv);
 

	
 
	GList *l = cbuddies;
 
	while (l != NULL) {
 
		PurpleConvChatBuddy *cb = (PurpleConvChatBuddy *)l->data;
 
		std::string name(cb->name);
 
		int flags = GPOINTER_TO_INT(cb->flags);
 
		if (flags & PURPLE_CBFLAGS_OP || flags & PURPLE_CBFLAGS_HALFOP) {
 
// 			item->addAttribute("affiliation", "admin");
 
// 			item->addAttribute("role", "moderator");
 
			flags = 1;
 
		}
 
		else if (flags & PURPLE_CBFLAGS_FOUNDER) {
 
// 			item->addAttribute("affiliation", "owner");
 
// 			item->addAttribute("role", "moderator");
 
			flags = 1;
 
		}
 
		else {
 
			flags = 0;
 
// 			item->addAttribute("affiliation", "member");
 
// 			item->addAttribute("role", "participant");
 
		}
 

	
 
		np->handleParticipantChanged(np->m_accounts[account], name, purple_conversation_get_name(conv), (int) flags, pbnetwork::STATUS_ONLINE);
 

	
 
		l = l->next;
 
	}
 
}
 

	
 
static void conv_chat_remove_users(PurpleConversation *conv, GList *users) {
 
	PurpleAccount *account = purple_conversation_get_account(conv);
 

	
 
	GList *l = users;
 
	while (l != NULL) {
 
		std::string name((char *) l->data);
 
		np->handleParticipantChanged(np->m_accounts[account], name, purple_conversation_get_name(conv), 0, pbnetwork::STATUS_NONE);
 

	
 
		l = l->next;
 
	}
 
}
 

	
 
static PurpleConversationUiOps conversation_ui_ops =
 
{
 
	NULL,
 
	NULL,
 
	conv_write_im,//conv_write_chat,                              /* write_chat           */
 
	conv_write_im,             /* write_im             */
 
	NULL,//conv_write_conv,           /* write_conv           */
 
	conv_chat_add_users,       /* chat_add_users       */
 
	NULL,//conv_chat_rename_user,     /* chat_rename_user     */
 
	conv_chat_remove_users,    /* chat_remove_users    */
 
	NULL,//pidgin_conv_chat_update_user,     /* chat_update_user     */
 
	NULL,//pidgin_conv_present_conversation, /* present              */
 
	NULL,//pidgin_conv_has_focus,            /* has_focus            */
 
	NULL,//pidgin_conv_custom_smiley_add,    /* custom_smiley_add    */
 
	NULL,//pidgin_conv_custom_smiley_write,  /* custom_smiley_write  */
 
	NULL,//pidgin_conv_custom_smiley_close,  /* custom_smiley_close  */
 
	NULL,//pidgin_conv_send_confirm,         /* send_confirm         */
 
	NULL,
 
	NULL,
 
	NULL,
 
	NULL
 
};
 

	
 
struct Dis {
 
	std::string name;
 
	std::string protocol;
 
};
 

	
 
static gboolean disconnectMe(void *data) {
 
	Dis *d = (Dis *) data;
 
	PurpleAccount *account = purple_accounts_find(d->name.c_str(), d->protocol.c_str());
 
	delete d;
 

	
 
	if (account) {
 
		np->handleLogoutRequest(np->m_accounts[account], purple_account_get_username(account));
 
	}
 
	return FALSE;
 
}
 

	
 
static gboolean pingTimeout(void *data) {
 
	np->checkPing();
 
	return TRUE;
 
}
 

	
 
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(), ::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;
 
	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);
 

	
 
	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 = 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 (path) {
 
					if (g_file_get_contents(path, &data, &len, NULL)) {
 
						photo = std::string(data, len);
 
						g_free(data);
 
					}
 
					g_free(path);
 
				}
 
			}
 
			else {
 
				const gchar * data = (gchar*)purple_buddy_icon_get_data(icon, &len);
 
				if (len < 300000 && data) {
 
					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, 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);
 

	
 
	return req;
 
}
 

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

	
 

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

	
 
static void XferCreated(PurpleXfer *xfer) {
 
	if (!xfer) {
 
		return;
 
	}
 

	
 
// 	PurpleAccount *account = purple_xfer_get_account(xfer);
 
// 	np->handleFTStart(np->m_accounts[account], xfer->who, xfer, "", xhtml_);
 
}
 

	
 
static void XferDestroyed(PurpleXfer *xfer) {
 
	std::remove(np->m_waitingXfers.begin(), np->m_waitingXfers.end(), xfer);
 
	FTData *ftdata = (FTData *) xfer->ui_data;
 
	if (ftdata && ftdata->timer) {
 
		purple_timeout_remove(ftdata->timer);
 
	}
 
	if (ftdata) {
 
		np->m_xfers.erase(ftdata->id);
 
	}
 
}
 

	
 
static void xferCanceled(PurpleXfer *xfer) {
 
	PurpleAccount *account = purple_xfer_get_account(xfer);
 
	std::string filename(xfer ? purple_xfer_get_filename(xfer) : "");
 
	std::string w = xfer->who;
 
	size_t pos = w.find("/");
 
	if (pos != std::string::npos)
 
		w.erase((int) pos, w.length() - (int) pos);
 

	
 
	FTData *ftdata = (FTData *) xfer->ui_data;
 

	
 
	np->handleFTFinish(np->m_accounts[account], w, filename, purple_xfer_get_size(xfer), ftdata ? ftdata->id : 0);
 
	std::remove(np->m_waitingXfers.begin(), np->m_waitingXfers.end(), xfer);
 
	if (ftdata && ftdata->timer) {
 
		purple_timeout_remove(ftdata->timer);
 
	}
 
	purple_xfer_unref(xfer);
 
}
 

	
 
static void fileSendStart(PurpleXfer *xfer) {
 
// 	FiletransferRepeater *repeater = (FiletransferRepeater *) xfer->ui_data;
 
// 	repeater->fileSendStart();
 
}
 

	
 
static void fileRecvStart(PurpleXfer *xfer) {
 
// 	FiletransferRepeater *repeater = (FiletransferRepeater *) xfer->ui_data;
 
// 	repeater->fileRecvStart();
 
	FTData *ftData = (FTData *) xfer->ui_data;
 
	if (ftData->timer == 0) {
 
		ftData->timer = purple_timeout_add(1, ft_ui_ready, xfer);
 
	}
 
}
 

	
 
static void newXfer(PurpleXfer *xfer) {
 
	PurpleAccount *account = purple_xfer_get_account(xfer);
 
	std::string filename(xfer ? purple_xfer_get_filename(xfer) : "");
 
	purple_xfer_ref(xfer);
 
	std::string w = xfer->who;
 
	size_t pos = w.find("/");
 
	if (pos != std::string::npos)
 
		w.erase((int) pos, w.length() - (int) pos);
 

	
 
	FTData *ftdata = new FTData;
 
	ftdata->paused = false;
 
	ftdata->id = 0;
 
	ftdata->timer = 0;
 
	xfer->ui_data = (void *) ftdata;
 

	
 
	np->m_unhandledXfers[np->m_accounts[account] + filename + w] = xfer;
 

	
 
	np->handleFTStart(np->m_accounts[account], w, filename, purple_xfer_get_size(xfer));
 
}
 

	
 
static void XferReceiveComplete(PurpleXfer *xfer) {
 
// 	FiletransferRepeater *repeater = (FiletransferRepeater *) xfer->ui_data;
 
// 	repeater->_tryToDeleteMe();
 
// 	GlooxMessageHandler::instance()->ftManager->handleXferFileReceiveComplete(xfer);
 
	std::remove(np->m_waitingXfers.begin(), np->m_waitingXfers.end(), xfer);
 
	FTData *ftdata = (FTData *) xfer->ui_data;
 
	if (ftdata && ftdata->timer) {
 
		purple_timeout_remove(ftdata->timer);
 
	}
 
	purple_xfer_unref(xfer);
 
}
 

	
 
static void XferSendComplete(PurpleXfer *xfer) {
 
// 	FiletransferRepeater *repeater = (FiletransferRepeater *) xfer->ui_data;
 
// 	repeater->_tryToDeleteMe();
 
	std::remove(np->m_waitingXfers.begin(), np->m_waitingXfers.end(), xfer);
 
	FTData *ftdata = (FTData *) xfer->ui_data;
 
	if (ftdata && ftdata->timer) {
 
		purple_timeout_remove(ftdata->timer);
 
	}
 
	purple_xfer_unref(xfer);
 
}
 

	
 
static gssize XferWrite(PurpleXfer *xfer, const guchar *buffer, gssize size) {
 
	FTData *ftData = (FTData *) xfer->ui_data;
 
	std::string data((const char *) buffer, (size_t) size);
 
// 	std::cout << "xferwrite\n";
 
	if (!ftData->paused) {
 
// 		std::cout << "adding xfer to waitingXfers queue\n";
 
		np->m_waitingXfers.push_back(xfer);
 
	}
 
	np->handleFTData(ftData->id, data);
 
	return size;
 
}
 

	
 
static void XferNotSent(PurpleXfer *xfer, const guchar *buffer, gsize size) {
 
// 	FiletransferRepeater *repeater = (FiletransferRepeater *) xfer->ui_data;
 
// 	repeater->handleDataNotSent(buffer, size);
 
}
 

	
 
static gssize XferRead(PurpleXfer *xfer, guchar **buffer, gssize size) {
 
// 	FiletransferRepeater *repeater = (FiletransferRepeater *) xfer->ui_data;
 
// 	int data_size = repeater->getDataToSend(buffer, size);
 
// 	if (data_size == 0)
 
// 		return 0;
 
// 	
 
// 	return data_size;
 
	return 0;
 
}
 

	
 
static PurpleXferUiOps xferUiOps =
 
{
 
	XferCreated,
 
	XferDestroyed,
 
	NULL,
 
	NULL,
 
	xferCanceled,
 
	xferCanceled,
 
	XferWrite,
 
	XferRead,
 
	XferNotSent,
 
	NULL
 
};
 

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

	
 
/***** Core Ui Ops *****/
 
static void
 
spectrum_glib_log_handler(const gchar *domain,
 
						  GLogLevelFlags flags,
 
						  const gchar *message,
 
						  gpointer user_data)
 
{
 
	const char *level;
 
	char *new_msg = NULL;
 
	char *new_domain = NULL;
 

	
 
	if ((flags & G_LOG_LEVEL_ERROR) == G_LOG_LEVEL_ERROR)
 
		level = "ERROR";
 
	else if ((flags & G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL)
 
		level = "CRITICAL";
 
	else if ((flags & G_LOG_LEVEL_WARNING) == G_LOG_LEVEL_WARNING)
 
		level = "WARNING";
 
	else if ((flags & G_LOG_LEVEL_MESSAGE) == G_LOG_LEVEL_MESSAGE)
 
		level = "MESSAGE";
 
	else if ((flags & G_LOG_LEVEL_INFO) == G_LOG_LEVEL_INFO)
 
		level = "INFO";
 
	else if ((flags & G_LOG_LEVEL_DEBUG) == G_LOG_LEVEL_DEBUG)
 
		level = "DEBUG";
 
	else {
 
		LOG4CXX_ERROR(logger, "Unknown glib logging level in " << (guint)flags);
 
		level = "UNKNOWN"; /* This will never happen. */
 
	}
 

	
 
	if (message != NULL)
 
		new_msg = purple_utf8_try_convert(message);
 

	
 
	if (domain != NULL)
 
		new_domain = purple_utf8_try_convert(domain);
 

	
 
	if (new_msg != NULL) {
 
		std::string area("glib");
 
		area.push_back('/');
 
		area.append(level);
 

	
 
		std::string message(new_domain ? new_domain : "g_log");
 
		message.push_back(' ');
 
		message.append(new_msg);
 

	
 
		LOG4CXX_ERROR(logger, message);
 
		g_free(new_msg);
 
	}
 

	
 
	g_free(new_domain);
 
}
 

	
 
static void
 
debug_init(void)
 
{
 
#define REGISTER_G_LOG_HANDLER(name) \
 
	g_log_set_handler((name), \
 
		(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL \
 
										  | G_LOG_FLAG_RECURSION), \
 
					  spectrum_glib_log_handler, NULL)
 

	
 
	REGISTER_G_LOG_HANDLER(NULL);
 
	REGISTER_G_LOG_HANDLER("GLib");
 
	REGISTER_G_LOG_HANDLER("GModule");
 
	REGISTER_G_LOG_HANDLER("GLib-GObject");
 
	REGISTER_G_LOG_HANDLER("GThread");
 
	REGISTER_G_LOG_HANDLER("GConf");
 
	
 

	
 
#undef REGISTER_G_LOD_HANDLER
 
}
 

	
 
static PurpleCoreUiOps coreUiOps =
 
{
 
	NULL,
 
	debug_init,
 
	transport_core_ui_init,
 
	NULL,
 
	spectrum_ui_get_info,
 
	NULL,
 
	NULL,
 
	NULL
 
};
 

	
 
static void signed_on(PurpleConnection *gc, gpointer unused) {
 
	PurpleAccount *account = purple_connection_get_account(gc);
 
	np->handleConnected(np->m_accounts[account]);
 
#ifndef WIN32
 
	// force returning of memory chunks allocated by libxml2 to kernel
 
	malloc_trim(0);
 
#endif
 

	
 
	// For prpl-gg
 
	execute_purple_plugin_action(gc, "Download buddylist from Server");
 
}
 

	
 
static void printDebug(PurpleDebugLevel level, const char *category, const char *arg_s) {
 
	std::string c("");
 
	std::string args(arg_s);
 
	args.erase(args.size() - 1);
 

	
 
	if (category) {
 
		c.append(category);
 
	}
 

	
 
	c.push_back(':');
 

	
 
	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() {
 
	bool ret;
 

	
 
	if (!resolvePurpleFunctions()) {
 
		LOG4CXX_ERROR(logger, "Unable to load libpurple.dll or some of the needed methods");
 
		return false;
 
	}
 

	
 
	purple_plugins_add_search_path("./plugins");
 

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

	
 
	purple_debug_set_ui_ops(&debugUiOps);
 
	purple_debug_set_verbose(true);
 

	
 
	purple_core_set_ui_ops(&coreUiOps);
 
	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(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 transportDataReceived(gpointer data, gint source, PurpleInputCondition cond) {
 
	if (cond & PURPLE_INPUT_READ) {
 
		char buffer[65535];
 
		char *ptr = buffer;
 
#ifdef WIN32
 
		ssize_t n = recv(source, ptr, sizeof(buffer), 0);
 
#else
 
		ssize_t n = read(source, ptr, sizeof(buffer));
 
#endif
 
		if (n <= 0) {
 
			LOG4CXX_INFO(logger, "Diconnecting from spectrum2 server");
 
			exit(errno);
 
		}
 
		std::string d = std::string(buffer, n);
 
		np->handleDataRead(d);
 
	}
 
	else {
 
		if (writeInput != 0) {
 
			purple_input_remove(writeInput);
 
			writeInput = 0;
 
		}
 
		np->readyForData();
 
	}
 
}
 

	
 
int main(int argc, char **argv) {
 
	GError *error = NULL;
 
	GOptionContext *context;
 
	context = g_option_context_new("config_file_name or profile name");
 
	g_option_context_add_main_entries(context, options_entries, "");
 
	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 
		std::cerr << "option parsing failed: " << error->message << "\n";
 
		return -1;
 
	}
 

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

	
 
		Config config;
 
		if (!config.load(argv[1])) {
 
			std::cerr << "Can't open " << argv[1] << " configuration file.\n";
 
			return 1;
 
		}
 
		Logging::initBackendLogging(&config);
 

	
 
		initPurple();
 

	
 
		main_socket = create_socket(host, port);
 
		purple_input_add(main_socket, PURPLE_INPUT_READ, &transportDataReceived, NULL);
 
		purple_timeout_add_seconds(30, pingTimeout, NULL);
 

	
 
		np = new SpectrumNetworkPlugin();
 
		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/libyahoo2/CMakeLists.txt
Show inline comments
 
new file 100644
 
cmake_minimum_required(VERSION 2.6)
 
 
FILE(GLOB_RECURSE SRC *.c *.cpp)
 
 
ADD_DEFINITIONS(-DHAVE_STDINT_H=1)
 
 
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/yahoo)
 
 
ADD_EXECUTABLE(spectrum2_libyahoo2_backend ${SRC})
 
 
target_link_libraries(spectrum2_libyahoo2_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
 
INSTALL(TARGETS spectrum2_libyahoo2_backend RUNTIME DESTINATION bin)
 
backends/libyahoo2/httpfetch.cpp
Show inline comments
 
new file 100644
 

	
 
#include "httpfetch.h"
 
#include "transport/logging.h"
 

	
 
DEFINE_LOGGER(logger, "HTTPFetch");
 

	
 
static int url_to_host_port_path(const char *url,
 
	char *host, int *port, char *path, int *ssl)
 
{
 
	char *urlcopy = NULL;
 
	char *slash = NULL;
 
	char *colon = NULL;
 

	
 
	/*
 
	 * http://hostname
 
	 * http://hostname/
 
	 * http://hostname/path
 
	 * http://hostname/path:foo
 
	 * http://hostname:port
 
	 * http://hostname:port/
 
	 * http://hostname:port/path
 
	 * http://hostname:port/path:foo
 
	 * and https:// variants of the above
 
	 */
 

	
 
	if (strstr(url, "http://") == url) {
 
		urlcopy = strdup(url + 7);
 
	} else if (strstr(url, "https://") == url) {
 
		urlcopy = strdup(url + 8);
 
		*ssl = 1;
 
	} else {
 
		return 0;
 
	}
 

	
 
	slash = strchr(urlcopy, '/');
 
	colon = strchr(urlcopy, ':');
 

	
 
	if (!colon || (slash && slash < colon)) {
 
		if (*ssl)
 
			*port = 443;
 
		else
 
			*port = 80;
 
	} else {
 
		*colon = 0;
 
		*port = atoi(colon + 1);
 
	}
 

	
 
	if (!slash) {
 
		strcpy(path, "/");
 
	} else {
 
		strcpy(path, slash);
 
		*slash = 0;
 
	}
 

	
 
	strcpy(host, urlcopy);
 

	
 
	free(urlcopy);
 

	
 
	return 1;
 
}
 

	
 
HTTPFetch::HTTPFetch(Swift::BoostIOServiceThread *ioService, Swift::ConnectionFactory *factory) : m_ioService(ioService), m_factory(factory) {
 
	m_afterHeader = false;
 
}
 

	
 
HTTPFetch::~HTTPFetch() {
 
}
 

	
 
void HTTPFetch::_connected(boost::shared_ptr<Swift::Connection> conn, const std::string url, bool error) {
 
	if (error) {
 
		_disconnected(conn);
 
	}
 
	else {
 
		char host[255];
 
		int port = 80;
 
		char path[255];
 
		int ssl = 0;
 
		if (!url_to_host_port_path(url.c_str(), host, &port, path, &ssl))
 
			return;
 

	
 
		static char buff[2048];
 
		snprintf(buff, sizeof(buff),
 
			"GET %s HTTP/1.1\r\n"
 
			"Host: %s\r\n"
 
			"User-Agent: Mozilla/4.5 [en] (1/1)\r\n"
 
			"Accept: */*\r\n"
 
			"%s" "\r\n", path, host,
 
			"Connection: close\r\n");
 
		LOG4CXX_INFO(logger, "Sending " << buff << "\n");
 
		conn->write(Swift::createSafeByteArray(buff));
 
	}
 
}
 

	
 
void HTTPFetch::_disconnected(boost::shared_ptr<Swift::Connection> conn) {
 
	conn->onConnectFinished.disconnect_all_slots();
 
	conn->onDisconnected.disconnect_all_slots();
 
	conn->onDataRead.disconnect_all_slots();
 

	
 
	if (m_buffer.size() == 0) {
 
		onURLFetched("");
 
	}
 
	else {
 
		std::string img = m_buffer.substr(m_buffer.find("\r\n\r\n") + 4);
 
		onURLFetched(img);
 
	}
 
}
 

	
 
void HTTPFetch::_read(boost::shared_ptr<Swift::Connection> conn, boost::shared_ptr<Swift::SafeByteArray> data) {
 
	std::string d(data->begin(), data->end());
 
// 			std::cout << d << "\n";
 
	std::string img = d.substr(d.find("\r\n\r\n") + 4);
 
	if (d.find("Location: ") == std::string::npos) {
 
		m_buffer += d;
 
	}
 
	else {
 
		d = d.substr(d.find("Location: ") + 10);
 
		if (d.find("\r") == std::string::npos) {
 
			d = d.substr(0, d.find("\n"));
 
		}
 
		else {
 
			d = d.substr(0, d.find("\r"));
 
		}
 
		LOG4CXX_INFO(logger, "Next url is '" << d << "'");
 
		fetchURL(d);
 
		conn->onConnectFinished.disconnect_all_slots();
 
		conn->onDisconnected.disconnect_all_slots();
 
		conn->onDataRead.disconnect_all_slots();
 
	}
 
}
 

	
 
bool HTTPFetch::fetchURL(const std::string &url) {
 
	char host[255];
 
	int port = 80;
 
	char path[255];
 
	char buff[1024];
 
	int ssl = 0;
 
	if (!url_to_host_port_path(url.c_str(), host, &port, path, &ssl)) {
 
		LOG4CXX_ERROR(logger, "Invalid URL " << url);
 
		return false;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Connecting to " << host << ":" << port);
 

	
 
	boost::asio::ip::tcp::resolver resolver(*m_ioService->getIOService());
 
	boost::asio::ip::tcp::resolver::query query(host, "");
 
	boost::asio::ip::address address;
 
	for(boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query); i != boost::asio::ip::tcp::resolver::iterator(); ++i) {
 
		boost::asio::ip::tcp::endpoint end = *i;
 
		address = end.address();
 
		break;
 
	}
 

	
 
	boost::shared_ptr<Swift::Connection> conn = m_factory->createConnection();
 
	conn->onConnectFinished.connect(boost::bind(&HTTPFetch::_connected, this, conn, url, _1));
 
	conn->onDisconnected.connect(boost::bind(&HTTPFetch::_disconnected, this, conn));
 
	conn->onDataRead.connect(boost::bind(&HTTPFetch::_read, this, conn, _1));
 
	conn->connect(Swift::HostAddressPort(Swift::HostAddress(address), port));
 
	return true;
 
}
backends/libyahoo2/httpfetch.h
Show inline comments
 
new file 100644
 
#pragma once
 

	
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 

	
 
// Swiften
 
#include "Swiften/Swiften.h"
 
#include "Swiften/TLS/OpenSSL/OpenSSLContextFactory.h"
 

	
 
// for signal handler
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 

	
 
// Boost
 
#include <boost/algorithm/string.hpp>
 

	
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
class HTTPFetch {
 
	public:
 
		HTTPFetch(Swift::BoostIOServiceThread *ioSerice, Swift::ConnectionFactory *factory);
 
		virtual ~HTTPFetch();
 

	
 
		bool fetchURL(const std::string &url);
 

	
 
		boost::signal<void (const std::string &data)> onURLFetched;
 

	
 
	private:
 
		void _connected(boost::shared_ptr<Swift::Connection> conn, const std::string url, bool error);
 
		void _disconnected(boost::shared_ptr<Swift::Connection> conn);
 
		void _read(boost::shared_ptr<Swift::Connection> conn, boost::shared_ptr<Swift::SafeByteArray> data);
 

	
 
		Swift::BoostIOServiceThread *m_ioService;
 
		Swift::ConnectionFactory *m_factory;
 
		std::string m_buffer;
 
		bool m_afterHeader;
 
};
backends/libyahoo2/main.cpp
Show inline comments
 
new file 100644
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 

	
 
// Yahoo2
 
#include <yahoo2.h>
 
#include <yahoo2_callbacks.h>
 
#include <stdio.h>
 
#include <stdarg.h>
 
#include <stdlib.h>
 

	
 
#include "yahoohandler.h"
 
#include "yahoolocalaccount.h"
 
#include "httpfetch.h"
 

	
 
// Swiften
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Network/TLSConnectionFactory.h"
 
#include "Swiften/TLS/OpenSSL/OpenSSLContextFactory.h"
 

	
 
// for signal handler
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 

	
 
// Boost
 
#include <boost/algorithm/string.hpp>
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
class YahooHandler;
 
class YahooLocalAccount;
 

	
 
static std::string *currently_read_data;
 
static YahooLocalAccount *currently_writting_account;
 

	
 
YahooHandler::YahooHandler(YahooLocalAccount *account, int conn_tag, int handler_tag, void *data, yahoo_input_condition cond) :
 
	handler_tag(handler_tag), conn_tag(conn_tag), data(data), cond(cond), remove_later(false), account(account) {}
 

	
 
YahooHandler::~YahooHandler() {}
 

	
 
void YahooHandler::ready(std::string *buffer) {
 
	if (cond == YAHOO_INPUT_WRITE) {
 
		YahooLocalAccount *old = currently_writting_account;
 
		currently_writting_account = account;
 
		yahoo_write_ready(account->id, (void *) conn_tag, data);
 
		currently_writting_account = old;
 
	}
 
	else {
 
		if (!buffer) {
 
			return;
 
		}
 
		// yahoo_read_ready calls ext_yahoo_read(...) in a loop, so we just have to choose proper buffer from which will
 
		// that method read. We do that by static currently_read_data pointer.
 
		currently_read_data = buffer;
 
		// libyahoo2 reads data per 1024 bytes, so if we still have some data after the first ext_yahoo_read call,
 
		// we have to call yahoo_read_ready again...
 
		do {
 
			yahoo_read_ready(account->id, (void *) conn_tag, data);
 
		} while (currently_read_data->size() != 0);
 
	}
 
}
 

	
 

	
 
typedef struct {
 
	std::string yahoo_id;
 
	std::string name;
 
	int status;
 
	int away;
 
	std::string msg;
 
	std::string group;
 
} yahoo_account;
 

	
 
typedef struct {
 
	int id;
 
	char *label;
 
} yahoo_idlabel;
 

	
 
typedef struct {
 
	int id;
 
	char *who;
 
} yahoo_authorize_data;
 

	
 
DEFINE_LOGGER(logger, "Yahoo2");
 

	
 
// eventloop
 
Swift::SimpleEventLoop *loop_;
 

	
 
// Plugin
 
class YahooPlugin;
 
YahooPlugin * np = NULL;
 

	
 
class YahooPlugin : public NetworkPlugin {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::OpenSSLContextFactory *m_sslFactory;
 
		Swift::TLSConnectionFactory *m_tlsFactory;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 

	
 
		YahooPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
			m_factories = new Swift::BoostNetworkFactories(loop);
 
			m_sslFactory = new Swift::OpenSSLContextFactory();
 
			m_tlsFactory = new Swift::TLSConnectionFactory(m_sslFactory, m_factories->getConnectionFactory());
 
			m_conn = m_factories->getConnectionFactory()->createConnection();
 
			m_conn->onDataRead.connect(boost::bind(&YahooPlugin::_handleDataRead, this, _1));
 
			m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
			LOG4CXX_INFO(logger, "Starting the plugin.");
 
		}
 

	
 
		// NetworkPlugin uses this method to send the data to networkplugin server
 
		void sendData(const std::string &string) {
 
			m_conn->write(Swift::createSafeByteArray(string));
 
		}
 

	
 
		// This method has to call handleDataRead with all received data from network plugin server
 
		void _handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) {
 
			std::string d(data->begin(), data->end());
 
			handleDataRead(d);
 
		}
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			YahooLocalAccount *account = new YahooLocalAccount(user, legacyName, password);
 
			m_users[user] = account;
 
			m_ids[account->id] = user;
 

	
 
			LOG4CXX_INFO(logger, user << ": Logging in the user as " << legacyName << " with id=" << account->id);
 
			account->login();
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
			YahooLocalAccount *account = m_users[user];
 
			if (account) {
 
				yahoo_logoff(account->id);
 
				m_ids.erase(account->id);
 
				m_users.erase(user);
 
				delete account;
 
			}
 
		}
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "") {
 
			YahooLocalAccount *account = m_users[user];
 
			if (account) {
 
				LOG4CXX_INFO(logger, "Sending message from " << user << " to " << legacyName << ": " << message << ".");
 
				yahoo_send_im(account->id, NULL, legacyName.c_str(), message.c_str(), 0, 0);
 
				_yahoo_write_ready(account);
 
			}
 
		}
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) {
 
			LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
 
			handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
 
		}
 

	
 
		void _avatar_fetched(HTTPFetch *fetch, int account_id, unsigned int id, const std::string &img) {
 
			handleVCard(m_ids[account_id], id, "", "", "", img);
 
			delete fetch;
 
		}
 

	
 
		void handleVCardRequest(const std::string &user, const std::string &legacyName, unsigned int id) {
 
			YahooLocalAccount *account = m_users[user];
 
			if (!account) {
 
				return;
 
			}
 

	
 
			if (account->urls.find(legacyName) == account->urls.end()) {
 
				return;
 
			}
 

	
 
			HTTPFetch *fetch = new HTTPFetch(&m_boostIOServiceThread, m_factories->getConnectionFactory());
 
			fetch->onURLFetched.connect(boost::bind(&YahooPlugin::_avatar_fetched, this, fetch, account->id, id, _1));
 
			fetch->fetchURL(account->urls[legacyName]);
 
		}
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) {
 

	
 
		}
 

	
 
		YahooLocalAccount *getAccount(int id) {
 
			return m_users[m_ids[id]];
 
		}
 

	
 
		void _yahoo_remove_account(YahooLocalAccount *account) {
 
			m_ids.erase(account->id);
 
			m_users.erase(account->user);
 
			delete account;
 
		}
 

	
 
		void _yahoo_connect_finished(YahooLocalAccount *account, yahoo_connect_callback callback, void *data, int conn_tag, bool error) {
 
			currently_writting_account = account;
 
			if (error) {
 
				LOG4CXX_ERROR(logger, account->user << ": Connection error!");
 
				callback(NULL, 0, data);
 
// 				np->handleDisconnected(user, 0, "Connection error.");
 
			}
 
			else {
 
				LOG4CXX_INFO(logger, account->user << ": Connected");
 
				// We will have dangling pointer here, but we can't pass boost::shared_ptr here...
 
				callback((void *) conn_tag, 0, data);
 
			}
 
		}
 

	
 
		void _yahoo_write_ready(YahooLocalAccount *account) {
 
			// Find all WRITE handlers and inform that they really can write.
 
			for (std::map<int, YahooHandler *>::iterator it = account->handlers.begin(); it != account->handlers.end(); it++) {
 
				if (it->second->cond == YAHOO_INPUT_WRITE && !it->second->remove_later) {
 
					it->second->ready();
 
				}
 
			}
 
		}
 

	
 
		void _yahoo_data_read(YahooLocalAccount *account, int conn_tag, boost::shared_ptr<Swift::SafeByteArray> data) {
 
			std::string d(data->begin(), data->end());
 

	
 
			// Find the handler that handles READing for this conn_tag
 
			for (std::map<int, YahooHandler *>::iterator it = account->handlers_per_conn[conn_tag].begin(); it != account->handlers_per_conn[conn_tag].end(); it++) {
 
				if (it->second->cond == YAHOO_INPUT_READ && !it->second->remove_later) {
 
					std::string cpy(d);
 
					it->second->ready(&cpy);
 

	
 
					// Look like libyahoo2 needs to be informed it can write to socket after the read
 
					// even we have informed it before...
 
					_yahoo_write_ready(account);
 
					break;
 
				}
 
			}
 

	
 
			account->removeOldHandlers();
 
		}
 

	
 
		void _yahoo_data_written(YahooLocalAccount *account, int conn_tag) {
 
			LOG4CXX_INFO(logger, "data written");
 
			for (std::map<int, YahooHandler *>::iterator it = account->handlers_per_conn[conn_tag].begin(); it != account->handlers_per_conn[conn_tag].end(); it++) {
 
				if (it->second->cond == YAHOO_INPUT_WRITE) {
 
					it->second->ready();
 
				}
 
			}
 

	
 
			account->removeOldHandlers();
 
		}
 

	
 
		void _yahoo_disconnected(YahooLocalAccount *account, int conn_tag, const boost::optional<Swift::Connection::Error> &error) {
 
			for (std::map<int, YahooHandler *>::iterator it = account->handlers_per_conn[conn_tag].begin(); it != account->handlers_per_conn[conn_tag].end(); it++) {
 
				if (it->second->cond == YAHOO_INPUT_READ && !it->second->remove_later) {
 
					std::string cpy;
 
					it->second->ready(&cpy);
 
					_yahoo_write_ready(account);
 
					break;
 
				}
 
			}
 

	
 
			account->removeConn(conn_tag);
 
		}
 

	
 
		int _yahoo_connect_async(int id, const char *host, int port, yahoo_connect_callback callback, void *data, int use_ssl) {
 
			YahooLocalAccount *account = getAccount(id);
 
			if (!account) {
 
				LOG4CXX_ERROR(logger, "Unknown account id=" << id);
 
				return -1;
 
			}
 

	
 
			boost::asio::ip::tcp::resolver resolver(*m_boostIOServiceThread.getIOService());
 
			boost::asio::ip::tcp::resolver::query query(host, "");
 
			boost::asio::ip::address address;
 
			for(boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query); i != boost::asio::ip::tcp::resolver::iterator(); ++i) {
 
				boost::asio::ip::tcp::endpoint end = *i;
 
				address = end.address();
 
				break;
 
			}
 

	
 
			LOG4CXX_INFO(logger, m_ids[id] << ": Connecting " << host << ":" << port);
 
			int tag = account->conn_tag++;
 
			if (use_ssl) {
 
				account->conns[tag] = m_tlsFactory->createConnection();
 
			}
 
			else {
 
				account->conns[tag] = m_factories->getConnectionFactory()->createConnection();
 
			}
 
			account->conns[tag]->onConnectFinished.connect(boost::bind(&YahooPlugin::_yahoo_connect_finished, this, account, callback, data, tag, _1));
 
			account->conns[tag]->onDisconnected.connect(boost::bind(&YahooPlugin::_yahoo_disconnected, this, account, tag, _1));
 
			account->conns[tag]->onDataRead.connect(boost::bind(&YahooPlugin::_yahoo_data_read, this, account, tag, _1));
 
			account->conns[tag]->onDataWritten.connect(boost::bind(&YahooPlugin::_yahoo_data_written, this, account, tag));
 
			account->conns[tag]->connect(Swift::HostAddressPort(Swift::HostAddress(address), port));
 
			return tag;
 
		}
 

	
 
	private:
 
		Config *config;
 
		std::map<std::string, YahooLocalAccount *> m_users;
 
		std::map<int, std::string> m_ids;
 
};
 

	
 
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 ext_yahoo_got_conf_invite(int id, const char *me, const char *who, const char *room, const char *msg, YList *members) {
 
}
 

	
 
static void ext_yahoo_conf_userdecline(int id, const char *me, const char *who, const char *room, const char *msg) {
 
}
 

	
 
static void ext_yahoo_conf_userjoin(int id, const char *me, const char *who, const char *room) {
 
}
 

	
 
static void ext_yahoo_conf_userleave(int id, const char *me, const char *who, const char *room) {
 
}
 

	
 
static void ext_yahoo_conf_message(int id, const char *me, const char *who, const char *room, const char *msg, int utf8) {
 
}
 

	
 
static void ext_yahoo_chat_cat_xml(int id, const char *xml)  {
 
}
 

	
 
static void ext_yahoo_chat_join(int id, const char *me, const char *room, const char * topic, YList *members, void *fd) {
 
}
 

	
 
static void ext_yahoo_chat_userjoin(int id, const char *me, const char *room, struct yahoo_chat_member *who) {
 
}
 

	
 
static void ext_yahoo_chat_userleave(int id, const char *me, const char *room, const char *who) {
 
}
 

	
 
static void ext_yahoo_chat_message(int id, const char *me, const char *who, const char *room, const char *msg, int msgtype, int utf8) {
 
}
 

	
 
static void ext_yahoo_status_changed(int id, const char *who, int stat, const char *msg, int away, int idle, int mobile) {
 
	YahooLocalAccount *account = np->getAccount(id);
 
	if (!account) {
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, account->user << ": " << who << " status changed");
 

	
 
	pbnetwork::StatusType status = pbnetwork::STATUS_NONE;
 
	switch (stat) {
 
		case YAHOO_STATUS_AVAILABLE:
 
			status = pbnetwork::STATUS_ONLINE;
 
			break;
 
		case YAHOO_STATUS_NOTATHOME:
 
		case YAHOO_STATUS_NOTATDESK:
 
		case YAHOO_STATUS_NOTINOFFICE:
 
		case YAHOO_STATUS_ONPHONE:
 
		case YAHOO_STATUS_ONVACATION:
 
		case YAHOO_STATUS_OUTTOLUNCH:
 
		case YAHOO_STATUS_STEPPEDOUT:
 
			status = pbnetwork::STATUS_AWAY;
 
			break;
 
		case YAHOO_STATUS_BRB:
 
			status = pbnetwork::STATUS_XA;
 
			break;
 
		case YAHOO_STATUS_BUSY:
 
			status = pbnetwork::STATUS_DND;
 
			break;
 
		case YAHOO_STATUS_OFFLINE:
 
			status = pbnetwork::STATUS_NONE;
 
			break;
 
		default:
 
			status = pbnetwork::STATUS_ONLINE;
 
			break;
 
	}
 

	
 
	yahoo_buddyicon_request(id, who);
 
	np->_yahoo_write_ready(account);
 

	
 
	np->handleBuddyChanged(account->user, who, "", std::vector<std::string>(), status, msg ? msg : "");
 
}
 

	
 
static void ext_yahoo_got_buddies(int id, YList * buds) {
 
	YahooLocalAccount *account = np->getAccount(id);
 
	if (!account) {
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, account->user << ": Got buddy list");
 
	for(; buds; buds = buds->next) {
 
		struct yahoo_buddy *bud = (struct yahoo_buddy *) buds->data;
 

	
 
		std::vector<std::string> groups;
 
		groups.push_back(bud->group);
 
		np->handleBuddyChanged(account->user, bud->id, bud->real_name ? bud->real_name : "", groups, pbnetwork::STATUS_NONE);
 
	}
 

	
 
// 	yahoo_set_away(id, YAHOO_STATUS_AVAILABLE, "", 1);
 
	np->_yahoo_write_ready(account);
 
	np->handleConnected(account->user);
 
}
 

	
 
static void ext_yahoo_got_ignore(int id, YList * igns)
 
{
 
}
 

	
 
static void ext_yahoo_got_buzz(int id, const char *me, const char *who, long tm) {
 
}
 

	
 
static void ext_yahoo_got_im(int id, const char *me, const char *who, const char *msg, long tm, int stat, int utf8) {
 
	YahooLocalAccount *account = np->getAccount(id);
 
	if (!account) {
 
		return;
 
	}
 

	
 
	np->handleMessage(account->user, who, msg);
 
}
 

	
 
static void ext_yahoo_rejected(int id, const char *who, const char *msg) {
 
}
 

	
 
static void ext_yahoo_contact_added(int id, const char *myid, const char *who, const char *msg) {
 
}
 

	
 
static void ext_yahoo_typing_notify(int id, const char* me, const char *who, int stat) {
 
}
 

	
 
static void ext_yahoo_game_notify(int id, const char *me, const char *who, int stat, const char *msg)
 
{
 
}
 

	
 
static void ext_yahoo_mail_notify(int id, const char *from, const char *subj, int cnt) {
 
}
 

	
 
static void ext_yahoo_got_webcam_image(int id, const char *who, const unsigned char *image, unsigned int image_size, unsigned int real_size, unsigned int timestamp) {
 
}
 

	
 
static void ext_yahoo_webcam_viewer(int id, const char *who, int connect) {
 
}
 

	
 
static void ext_yahoo_webcam_closed(int id, const char *who, int reason) {
 
}
 

	
 
static void ext_yahoo_webcam_data_request(int id, int send) {
 
}
 

	
 
static void ext_yahoo_webcam_invite(int id, const char *me, const char *from) {
 
}
 

	
 
static void ext_yahoo_webcam_invite_reply(int id, const char *me, const char *from, int accept) {
 
}
 

	
 
static void ext_yahoo_system_message(int id, const char *me, const char *who, const char *msg) {
 
}
 

	
 
static void ext_yahoo_got_cookies(int id) {
 
}
 

	
 
static void ext_yahoo_login_response(int id, int succ, const char *url) {
 
	YahooLocalAccount *account = np->getAccount(id);
 
	if (!account) {
 
		return;
 
	}
 

	
 
	if (succ == YAHOO_LOGIN_OK) {
 
		account->status = yahoo_current_status(id);
 
		// We will fire handleConnected in Got Buddy List.
 
		return;
 
	}
 
	else if (succ == YAHOO_LOGIN_UNAME) {
 
		np->handleDisconnected(account->user, 0, "Could not log into Yahoo service - username not recognised.  Please verify that your username is correctly typed.");
 
	}
 
	else if (succ == YAHOO_LOGIN_PASSWD) {
 
		np->handleDisconnected(account->user, 0, "Could not log into Yahoo service - password incorrect.  Please verify that your password is correctly typed.");
 
	}
 
	else if (succ == YAHOO_LOGIN_LOCK) {
 
		np->handleDisconnected(account->user, 0, std::string("Could not log into Yahoo service.  Your account has been locked. Visit ") + url + " to reactivate it.");
 
	}
 
	else if (succ == YAHOO_LOGIN_DUPL) {
 
		np->handleDisconnected(account->user, 0, "You have been logged out of the yahoo service, possibly due to a duplicate login.");
 
	}
 
	else if (succ == YAHOO_LOGIN_SOCK) {
 
		np->handleDisconnected(account->user, 0, "The server closed the socket.");
 
	}
 
	else {
 
		np->handleDisconnected(account->user, 0, "Could not log in, unknown reason.");
 
	}
 

	
 
	np->handleLogoutRequest(account->user, "");
 
}
 

	
 
static void ext_yahoo_error(int id, const char *_err, int fatal, int num) {
 
	std::string err(_err);
 
	std::string msg("Yahoo Error: ");
 
	msg += err + " - ";
 
	switch(num) {
 
		case E_UNKNOWN:
 
			msg += "unknown error " + err;
 
			break;
 
		case E_CUSTOM:
 
			msg += "custom error " + err;
 
			break;
 
		case E_CONFNOTAVAIL:
 
			msg += err + " is not available for the conference";
 
			break;
 
		case E_IGNOREDUP:
 
			msg += err + " is already ignored";
 
			break;
 
		case E_IGNORENONE:
 
			msg += err +" is not in the ignore list";
 
			break;
 
		case E_IGNORECONF:
 
			msg += err + " is in buddy list - cannot ignore ";
 
			break;
 
		case E_SYSTEM:
 
			msg += "system error " + err;
 
			break;
 
		case E_CONNECTION:
 
			msg += err + "server connection error %s";
 
			break;
 
	}
 
	LOG4CXX_ERROR(logger, msg);
 

	
 
	YahooLocalAccount *account = np->getAccount(id);
 
	if (!account) {
 
		return;
 
	}
 

	
 
	if(fatal) {
 
		np->handleDisconnected(account->user, 0, msg);
 
		np->handleLogoutRequest(account->user, "");
 
	}
 
}
 

	
 
static int ext_yahoo_connect(const char *host, int port) {
 
	return -1;
 
}
 

	
 
static int ext_yahoo_add_handler(int id, void *fd, yahoo_input_condition cond, void *data) {
 
	YahooLocalAccount *account = np->getAccount(id);
 
	if (!account) {
 
		return -1;
 
	}
 

	
 
	int conn_tag = (unsigned long) fd;
 
	YahooHandler *handler = new YahooHandler(account, conn_tag, account->handler_tag++, data, cond);
 
	account->addHandler(handler);
 

	
 
	// We are ready to write right now, so why not...
 
	handler->ready();
 

	
 
	return handler->handler_tag;
 
}
 

	
 
static void ext_yahoo_remove_handler(int id, int tag) {
 
	YahooLocalAccount *account = np->getAccount(id);
 
	if (!account) {
 
		return;
 
	}
 

	
 
	if (account->handlers.find(tag) == account->handlers.end()) {
 
		return;
 
	}
 

	
 
	YahooHandler *handler = account->handlers[tag];
 
	handler->remove_later = true;
 
}
 

	
 
static int ext_yahoo_write(void *fd, char *buf, int len) {
 
	int conn_tag = (unsigned long) fd;
 
	YahooLocalAccount *account = currently_writting_account;
 

	
 
	std::string string(buf, len);
 
	account->conns[conn_tag]->write(Swift::createSafeByteArray(string));
 

	
 
	return len;
 
}
 

	
 
static int ext_yahoo_read(void *fd, char *buf, int len) {
 
	if (currently_read_data->size() < len) {
 
		len = currently_read_data->size();
 
	}
 
	memcpy(buf, currently_read_data->c_str(), len);
 
	currently_read_data->erase(0, len);
 
	return len;
 
}
 

	
 
static void ext_yahoo_close(void *fd) {
 
	// No need to do anything here. We close it properly in _yahoo_disconnected(...);
 
}
 

	
 
static int ext_yahoo_connect_async(int id, const char *host, int port, yahoo_connect_callback callback, void *data, int use_ssl) {
 
	return np->_yahoo_connect_async(id, host, port, callback, data, use_ssl);
 
}
 

	
 
static void ext_yahoo_got_file(int id, const char *me, const char *who, const char *msg, const char *fname, unsigned long fesize, char *trid) {
 
}
 

	
 
static void ext_yahoo_file_transfer_done(int id, int response, void *data) {
 
}
 

	
 
static char *ext_yahoo_get_ip_addr(const char *domain) {
 
	return NULL;
 
}
 

	
 
static void ext_yahoo_got_ft_data(int id, const unsigned char *in, int count, void *data) {
 
}
 

	
 
static void ext_yahoo_got_identities(int id, YList * ids) {
 
}
 

	
 
static void ext_yahoo_chat_yahoologout(int id, const char *me) {
 
}
 

	
 
static void ext_yahoo_chat_yahooerror(int id, const char *me) {
 
}
 

	
 
static void ext_yahoo_got_ping(int id, const char *errormsg){
 
}
 

	
 
static void ext_yahoo_got_search_result(int id, int found, int start, int total, YList *contacts) {
 
}
 

	
 
static void ext_yahoo_got_buddyicon_checksum(int id, const char *a, const char *b, int checksum) {
 
	LOG4CXX_INFO(logger, "got buddyicon_checksum");
 
}
 

	
 
static void ext_yahoo_got_buddy_change_group(int id, const char *me, const char *who, const char *old_group, const char *new_group) {
 
}
 

	
 
static void ext_yahoo_got_buddyicon(int id, const char *me, const char *who, const char *url, int checksum) {
 
	YahooLocalAccount *account = np->getAccount(id);
 
	if (!account) {
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, account->user << ": got buddyicon of " << who);
 
	account->urls[who] = url;
 
}
 

	
 
static void ext_yahoo_buddyicon_uploaded(int id, const char *url) {
 
}
 

	
 
static void ext_yahoo_got_buddyicon_request(int id, const char *me, const char *who) {
 
	LOG4CXX_INFO(logger, "got buddyicon_request");
 
}
 

	
 
static int ext_yahoo_log(const char *fmt,...)
 
{
 
	static char log[8192];
 
	static std::string buffered;
 
	va_list ap;
 

	
 
	va_start(ap, fmt);
 

	
 
	vsnprintf(log, 8191, fmt, ap);
 
	buffered += log;
 
	if (buffered.find('\n') != std::string::npos) {
 
		buffered.erase(buffered.find('\n'), 1);
 
		LOG4CXX_INFO(logger, buffered);
 
		buffered.clear();
 
	}
 
	fflush(stderr);
 
	va_end(ap);
 
	return 0;
 
}
 

	
 
static void register_callbacks()
 
{
 
	static struct yahoo_callbacks yc;
 

	
 
	yc.ext_yahoo_login_response = ext_yahoo_login_response;
 
	yc.ext_yahoo_got_buddies = ext_yahoo_got_buddies;
 
	yc.ext_yahoo_got_ignore = ext_yahoo_got_ignore;
 
	yc.ext_yahoo_got_identities = ext_yahoo_got_identities;
 
	yc.ext_yahoo_got_cookies = ext_yahoo_got_cookies;
 
	yc.ext_yahoo_status_changed = ext_yahoo_status_changed;
 
	yc.ext_yahoo_got_im = ext_yahoo_got_im;
 
	yc.ext_yahoo_got_buzz = ext_yahoo_got_buzz;
 
	yc.ext_yahoo_got_conf_invite = ext_yahoo_got_conf_invite;
 
	yc.ext_yahoo_conf_userdecline = ext_yahoo_conf_userdecline;
 
	yc.ext_yahoo_conf_userjoin = ext_yahoo_conf_userjoin;
 
	yc.ext_yahoo_conf_userleave = ext_yahoo_conf_userleave;
 
	yc.ext_yahoo_conf_message = ext_yahoo_conf_message;
 
	yc.ext_yahoo_chat_cat_xml = ext_yahoo_chat_cat_xml;
 
	yc.ext_yahoo_chat_join = ext_yahoo_chat_join;
 
	yc.ext_yahoo_chat_userjoin = ext_yahoo_chat_userjoin;
 
	yc.ext_yahoo_chat_userleave = ext_yahoo_chat_userleave;
 
	yc.ext_yahoo_chat_message = ext_yahoo_chat_message;
 
	yc.ext_yahoo_chat_yahoologout = ext_yahoo_chat_yahoologout;
 
	yc.ext_yahoo_chat_yahooerror = ext_yahoo_chat_yahooerror;
 
	yc.ext_yahoo_got_webcam_image = ext_yahoo_got_webcam_image;
 
	yc.ext_yahoo_webcam_invite = ext_yahoo_webcam_invite;
 
	yc.ext_yahoo_webcam_invite_reply = ext_yahoo_webcam_invite_reply;
 
	yc.ext_yahoo_webcam_closed = ext_yahoo_webcam_closed;
 
	yc.ext_yahoo_webcam_viewer = ext_yahoo_webcam_viewer;
 
	yc.ext_yahoo_webcam_data_request = ext_yahoo_webcam_data_request;
 
	yc.ext_yahoo_got_file = ext_yahoo_got_file;
 
	yc.ext_yahoo_got_ft_data = ext_yahoo_got_ft_data;
 
	yc.ext_yahoo_get_ip_addr = ext_yahoo_get_ip_addr;
 
	yc.ext_yahoo_file_transfer_done = ext_yahoo_file_transfer_done;
 
	yc.ext_yahoo_contact_added = ext_yahoo_contact_added;
 
	yc.ext_yahoo_rejected = ext_yahoo_rejected;
 
	yc.ext_yahoo_typing_notify = ext_yahoo_typing_notify;
 
	yc.ext_yahoo_game_notify = ext_yahoo_game_notify;
 
	yc.ext_yahoo_mail_notify = ext_yahoo_mail_notify;
 
	yc.ext_yahoo_got_search_result = ext_yahoo_got_search_result;
 
	yc.ext_yahoo_system_message = ext_yahoo_system_message;
 
	yc.ext_yahoo_error = ext_yahoo_error;
 
	yc.ext_yahoo_log = ext_yahoo_log;
 
	yc.ext_yahoo_add_handler = ext_yahoo_add_handler;
 
	yc.ext_yahoo_remove_handler = ext_yahoo_remove_handler;
 
	yc.ext_yahoo_connect = ext_yahoo_connect;
 
	yc.ext_yahoo_connect_async = ext_yahoo_connect_async;
 
	yc.ext_yahoo_read = ext_yahoo_read;
 
	yc.ext_yahoo_write = ext_yahoo_write;
 
	yc.ext_yahoo_close = ext_yahoo_close;
 
	yc.ext_yahoo_got_buddyicon = ext_yahoo_got_buddyicon;
 
	yc.ext_yahoo_got_buddyicon_checksum = ext_yahoo_got_buddyicon_checksum;
 
	yc.ext_yahoo_buddyicon_uploaded = ext_yahoo_buddyicon_uploaded;
 
	yc.ext_yahoo_got_buddyicon_request = ext_yahoo_got_buddyicon_request;
 
	yc.ext_yahoo_got_ping = ext_yahoo_got_ping;
 
	yc.ext_yahoo_got_buddy_change_group = ext_yahoo_got_buddy_change_group;
 

	
 
	yahoo_register_callbacks(&yc);
 
}
 

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

	
 
	if (signal(SIGCHLD, spectrum_sigchld_handler) == SIG_ERR) {
 
		std::cout << "SIGCHLD handler can't be set\n";
 
		return -1;
 
	}
 

	
 
	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 (...)
 
	{
 
		std::cout << desc << "\n";
 
		exit(1);
 
	}
 

	
 

	
 
	if (argc < 5) {
 
		return 1;
 
	}
 

	
 
	Config config;
 
	if (!config.load(argv[5])) {
 
		std::cerr << "Can't open " << argv[1] << " configuration file.\n";
 
		return 1;
 
	}
 

	
 
	Logging::initBackendLogging(&config);
 

	
 
	register_callbacks();
 
	yahoo_set_log_level(YAHOO_LOG_DEBUG);
 

	
 
	Swift::SimpleEventLoop eventLoop;
 
	loop_ = &eventLoop;
 
	np = new YahooPlugin(&config, &eventLoop, host, port);
 
	loop_->run();
 

	
 
	return 0;
 
}
backends/libyahoo2/sample_client.example
Show inline comments
 
new file 100644
 
/*
 
 * sample yahoo client based on libyahoo2
 
 *
 
 * libyahoo2 is available at http://libyahoo2.sourceforge.net/
 
 *
 
 * $Revision: 373 $
 
 * $Date: 2010-06-02 13:55:25 -0700 (Wed, 02 Jun 2010) $
 
 * 
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 * Copyright (C) 2009, Siddhesh Poyarekar <siddhesh.poyarekar@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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#if HAVE_CONFIG_H
 
# include <config.h>
 
#endif
 
#ifndef _WIN32
 
#include <netdb.h>
 
#include <sys/time.h>
 
#endif
 
#include <sys/types.h>
 
#ifndef _WIN32
 
#include <sys/socket.h>
 
#include <netinet/in.h>
 
#include <arpa/inet.h>
 

	
 
#include <termios.h>
 
#endif
 
#include <stdio.h>
 
#include <stdarg.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include <ctype.h>
 
#include <time.h>
 
#include <sys/stat.h>
 
#include <fcntl.h>
 
#include <errno.h>
 
#include <openssl/ssl.h>
 
#if HAVE_UNISTD_H
 
#include <unistd.h>
 
#endif
 

	
 
/* Get these from http://libyahoo2.sourceforge.net/ */
 
#include <yahoo2.h>
 
#include <yahoo2_callbacks.h>
 
#include "yahoo_util.h"
 

	
 
#ifndef _WIN32
 
int fileno(FILE * stream);
 
#endif
 

	
 
#define MAX_PREF_LEN 255
 

	
 
static char *local_host = NULL;
 

	
 
static int do_mail_notify = 0;
 
static int do_yahoo_debug = 0;
 
static int ignore_system = 0;
 
static int do_typing_notify = 1;
 
static int accept_webcam_viewers = 1;
 
static int send_webcam_images = 0;
 
static int webcam_direction = YAHOO_WEBCAM_DOWNLOAD;
 
static time_t curTime = 0;
 
static time_t pingTimer = 0;
 
static time_t webcamTimer = 0;
 
static double webcamStart = 0;
 

	
 
/* id of the webcam connection (needed for uploading) */
 
static int webcam_id = 0;
 

	
 
static int poll_loop=1;
 

	
 
static void register_callbacks();
 

	
 
typedef struct {
 
	char yahoo_id[255];
 
	char password[255];
 
	int id;
 
	int fd;
 
	int status;
 
	char *msg;
 
} yahoo_local_account;
 

	
 
typedef struct {
 
	char yahoo_id[255];
 
	char name[255];
 
	int status;
 
	int away;
 
	char *msg;
 
	char group[255];
 
} yahoo_account;
 

	
 
typedef struct {
 
	int id;
 
	char *label;
 
} yahoo_idlabel;
 

	
 
typedef struct {
 
	int id;
 
	char *who;
 
} yahoo_authorize_data;
 

	
 
yahoo_idlabel yahoo_status_codes[] = {
 
	{YAHOO_STATUS_AVAILABLE, "Available"},
 
	{YAHOO_STATUS_BRB, "BRB"},
 
	{YAHOO_STATUS_BUSY, "Busy"},
 
	{YAHOO_STATUS_NOTATHOME, "Not Home"},
 
	{YAHOO_STATUS_NOTATDESK, "Not at Desk"},
 
	{YAHOO_STATUS_NOTINOFFICE, "Not in Office"},
 
	{YAHOO_STATUS_ONPHONE, "On Phone"},
 
	{YAHOO_STATUS_ONVACATION, "On Vacation"},
 
	{YAHOO_STATUS_OUTTOLUNCH, "Out to Lunch"},
 
	{YAHOO_STATUS_STEPPEDOUT, "Stepped Out"},
 
	{YAHOO_STATUS_INVISIBLE, "Invisible"},
 
	{YAHOO_STATUS_IDLE, "Idle"},
 
	{YAHOO_STATUS_OFFLINE, "Offline"},
 
	{YAHOO_STATUS_CUSTOM, "[Custom]"},
 
	{YPACKET_STATUS_NOTIFY, "Notify"},
 
	{0, NULL}
 
};
 

	
 
static void ext_yahoo_close(void *fd);
 

	
 
char * yahoo_status_code(enum yahoo_status s)
 
{
 
	int i;
 
	for(i=0; yahoo_status_codes[i].label; i++)
 
		if(yahoo_status_codes[i].id == s)
 
			return yahoo_status_codes[i].label;
 
	return "Unknown";
 
}
 

	
 
#define YAHOO_DEBUGLOG ext_yahoo_log
 

	
 
static int ext_yahoo_log(const char *fmt,...)
 
{
 
	va_list ap;
 

	
 
	va_start(ap, fmt);
 

	
 
	vfprintf(stderr, fmt, ap);
 
	fflush(stderr);
 
	va_end(ap);
 
	return 0;
 
}
 

	
 
#define LOG(x) if(do_yahoo_debug) { YAHOO_DEBUGLOG("%s:%d: ", __FILE__, __LINE__); \
 
	YAHOO_DEBUGLOG x; \
 
	YAHOO_DEBUGLOG("\n"); }
 

	
 
#define WARNING(x) if(do_yahoo_debug) { YAHOO_DEBUGLOG("%s:%d: warning: ", __FILE__, __LINE__); \
 
	YAHOO_DEBUGLOG x; \
 
	YAHOO_DEBUGLOG("\n"); }
 

	
 
#define print_message(x) { printf x; printf("\n"); }
 

	
 
static yahoo_local_account * ylad = NULL;
 
static YList * buddies = NULL;
 

	
 
static int expired(time_t timer)
 
{
 
	if (timer && curTime >= timer)
 
		return 1;
 

	
 
	return 0;
 
}
 

	
 
static void rearm(time_t *timer, int seconds)
 
{
 
	time(timer);
 
	*timer += seconds;
 
}
 

	
 
#ifndef _WIN32
 
FILE *popen(const char *command, const char *type);
 
int pclose(FILE *stream);
 
int gethostname(char *name, size_t len);
 
#endif
 

	
 
static char * get_local_addresses()
 
{
 
	static char addresses[1024];
 
	char buff[1024];
 
	struct hostent * hn;
 
#ifndef _WIN32
 
	char gateway[16];
 
	char  * c;
 
	int i;
 
	FILE * f;
 
	f = popen("netstat -nr", "r");
 
	if(!f)
 
			goto IP_TEST_2;
 
	while( fgets(buff, sizeof(buff), f)  != NULL ) {
 
			c = strtok( buff, " " );
 
			if( (strstr(c, "default") || strstr(c,"0.0.0.0") ) &&
 
							!strstr(c, "127.0.0" ) )
 
					break;
 
	}
 
	c = strtok( NULL, " " );
 
	pclose(f);
 

	
 
	strncpy(gateway,c, 16);
 

	
 

	
 

	
 
	for(i = strlen(gateway); gateway[i] != '.'; i-- )
 
		gateway[i] = 0;
 

	
 
	gateway[i] = 0;
 

	
 
	for(i = strlen(gateway); gateway[i] != '.'; i-- )
 
		gateway[i] = 0;
 

	
 
	f = popen("/sbin/ifconfig -a", "r");
 
	if(!f)
 
		goto IP_TEST_2;
 

	
 
	while( fgets(buff, sizeof(buff), f) != NULL ) {
 
		if( strstr(buff, "inet") && strstr(buff,gateway) )
 
			break;
 
	}
 
	pclose(f);
 

	
 
	c = strtok( buff, " " );
 
	c = strtok( NULL, " " );
 

	
 
	strncpy ( addresses, c, sizeof(addresses) );
 
	c = strtok(addresses, ":" );
 
	strncpy ( buff, c, sizeof(buff) );
 
	if((c=strtok(NULL, ":")))
 
		strncpy( buff, c, sizeof(buff) );
 

	
 
	strncpy(addresses, buff, sizeof(addresses));
 

	
 
	return addresses;
 
		
 
		
 
IP_TEST_2:
 
#endif /* _WIN32 */
 

	
 
	gethostname(buff,sizeof(buff));
 

	
 
	hn = gethostbyname(buff);
 
	if(hn)
 
		strncpy(addresses, inet_ntoa( *((struct in_addr*)hn->h_addr)), sizeof(addresses) );
 
	else
 
		addresses[0] = 0;
 

	
 
	return addresses;
 
}
 

	
 
static double get_time()
 
{
 
#ifndef _WIN32
 
	struct timeval ct;
 
	gettimeofday(&ct, 0);
 

	
 
	/* return time in milliseconds */
 
	return (ct.tv_sec * 1E3 + ct.tv_usec / 1E3);
 
#else
 
	return timeGetTime();
 
#endif
 
}
 

	
 
static int yahoo_ping_timeout_callback()
 
{
 
	print_message(("Sending a keep alive message"));
 
	yahoo_keepalive(ylad->id);
 
	rearm(&pingTimer, 600);
 
	return 1;
 
}
 

	
 
static int yahoo_webcam_timeout_callback(int id)
 
{
 
	static unsigned image_num = 0;
 
	unsigned char *image = NULL;
 
	unsigned int length = 0;
 
	unsigned int timestamp = get_time() - webcamStart;
 
	char fname[1024];
 
	FILE *f_image = NULL;
 
	struct stat s_image;
 

	
 
	if (send_webcam_images)
 
	{
 
		sprintf(fname, "images/image_%.3d.jpc", image_num++);
 
		if (image_num > 999) image_num = 0;
 
		if (stat(fname, &s_image) == -1)
 
			return -1;
 
		length = s_image.st_size;
 
		image = y_new0(unsigned char, length);
 

	
 
		if ((f_image = fopen(fname, "r")) != NULL) {
 
			fread(image, length, 1, f_image);
 
			fclose(f_image);
 
		} else {
 
			printf("Error reading from %s\n", fname);
 
		}
 
	}
 

	
 
	print_message(("Sending a webcam image (%d bytes)", length));
 
	yahoo_webcam_send_image(id, image, length, timestamp);
 
	FREE(image);
 
	rearm(&webcamTimer, 2);
 
	return 1;
 
}
 

	
 
YList * conferences = NULL;
 
typedef struct {
 
	int id;
 
	char * me;
 
	char * room_name;
 
	char * host;
 
	YList * members;
 
	int joined;
 
} conf_room;
 

	
 
static const char * get_buddy_name(const char * yid)
 
{
 
	YList * b;
 
	for (b = buddies; b; b = b->next) {
 
		yahoo_account * ya = b->data;
 
		if(!strcmp(yid, ya->yahoo_id))
 
			return ya->name&&*ya->name?ya->name:ya->yahoo_id;
 
	}
 

	
 
	return yid;
 
}
 

	
 
static conf_room * find_conf_room_by_name_and_id(int id, const char * me, const char * name)
 
{
 
	YList * l;
 
	for(l = conferences; l; l=l->next) {
 
		conf_room * cr = l->data;
 
		if(cr->id == id && !strcmp(name, cr->room_name) && (me == NULL || cr->me == NULL || !strcmp(me, cr->me))) {
 
			return cr;
 
		}
 
	}
 

	
 
	return NULL;
 
}
 

	
 
static void ext_yahoo_got_conf_invite(int id, const char *me, const char *who, const char *room, const char *msg, YList *members)
 
{
 
	conf_room * cr = y_new0(conf_room, 1);
 
	cr->room_name = strdup(room);
 
	cr->me = strdup(me);
 
	cr->host = strdup(who);
 
	cr->members = members;
 
	cr->id = id;
 

	
 
	conferences = y_list_append(conferences, cr);
 

	
 
	print_message(("%s has invited you [%s] to a conference: %s\n"
 
				"with the message: %s",
 
				who, me, room, msg));
 

	
 
	for(; members; members=members->next)
 
		print_message(("\t%s", (char *)members->data));
 
	
 
}
 
static void ext_yahoo_conf_userdecline(int id, const char *me, const char *who, const char *room, const char *msg)
 
{
 
	YList * l;
 
	/* TODO: probably have to use the me arg to find the room */
 
	conf_room * cr = find_conf_room_by_name_and_id(id, me, room);
 
	
 
	if(cr)
 
	for(l = cr->members; l; l=l->next) {
 
		char * w = l->data;
 
		if(!strcmp(w, who)) {
 
			FREE(l->data);
 
			cr->members = y_list_remove_link(cr->members, l);
 
			y_list_free_1(l);
 
			break;
 
		}
 
	}
 

	
 
	print_message(("%s declined the invitation from %s to %s\n"
 
				"with the message: %s", who, me, room, msg));
 
}
 

	
 
static void ext_yahoo_conf_userjoin(int id, const char *me, const char *who, const char *room)
 
{
 
	conf_room * cr = find_conf_room_by_name_and_id(id, me, room);
 
	if(cr) {
 
	YList * l = NULL;
 
	for(l = cr->members; l; l=l->next) {
 
		char * w = l->data;
 
		if(!strcmp(w, who))
 
			break;
 
	}
 
	if(!l)
 
		cr->members = y_list_append(cr->members, strdup(who));
 
	}
 

	
 
	print_message(("%s joined the conference %s [%s]", who, room, me));
 

	
 
}
 

	
 
static void ext_yahoo_conf_userleave(int id, const char *me, const char *who, const char *room)
 
{
 
	YList * l;
 
	conf_room * cr = find_conf_room_by_name_and_id(id, me, room);
 
	
 
	if(cr)
 
	for(l = cr->members; l; l=l->next) {
 
		char * w = l->data;
 
		if(!strcmp(w, who)) {
 
			FREE(l->data);
 
			cr->members = y_list_remove_link(cr->members, l);
 
			y_list_free_1(l);
 
			break;
 
		}
 
	}
 

	
 
	print_message(("%s left the conference %s [%s]", who, room, me));
 
}
 

	
 
static void ext_yahoo_conf_message(int id, const char *me, const char *who, const char *room, const char *msg, int utf8)
 
{
 
	char * umsg = (char *)msg;
 

	
 
	if(utf8)
 
		umsg = y_utf8_to_str(msg);
 

	
 
	who = get_buddy_name(who);
 

	
 
	print_message(("%s (in %s [%s]): %s", who, room, me, umsg));
 

	
 
	if(utf8)
 
		FREE(umsg);
 
}
 

	
 
static void print_chat_member(struct yahoo_chat_member *ycm) 
 
{
 
	printf("%s (%s) ", ycm->id, ycm->alias);
 
	printf(" Age: %d Sex: ", ycm->age);
 
	if (ycm->attribs & YAHOO_CHAT_MALE) {
 
		printf("Male");
 
	} else if (ycm->attribs & YAHOO_CHAT_FEMALE) {
 
		printf("Female");
 
	} else {
 
		printf("Unknown");
 
	}
 
	if (ycm->attribs & YAHOO_CHAT_WEBCAM) {
 
		printf(" with webcam");
 
	}
 

	
 
	printf("  Location: %s", ycm->location);
 
}
 

	
 
static void ext_yahoo_chat_cat_xml(int id, const char *xml) 
 
{
 
	print_message(("%s", xml));
 
}
 

	
 
static void ext_yahoo_chat_join(int id, const char *me, const char *room, const char * topic, YList *members, void *fd)
 
{
 
	print_message(("You [%s] have joined the chatroom %s with topic %s", me, room, topic));
 

	
 
	while(members) {
 
		YList *n = members->next;
 

	
 
		printf("\t");
 
		print_chat_member(members->data);
 
		printf("\n");
 
		FREE(((struct yahoo_chat_member *)members->data)->id);
 
		FREE(((struct yahoo_chat_member *)members->data)->alias);
 
		FREE(((struct yahoo_chat_member *)members->data)->location);
 
		FREE(members->data);
 
		FREE(members);
 
		members=n;
 
	}
 
}
 

	
 
static void ext_yahoo_chat_userjoin(int id, const char *me, const char *room, struct yahoo_chat_member *who)
 
{
 
	print_chat_member(who);
 
	print_message((" joined the chatroom %s [%s]", room, me));
 
	FREE(who->id);
 
	FREE(who->alias);
 
	FREE(who->location);
 
	FREE(who);
 
}
 

	
 
static void ext_yahoo_chat_userleave(int id, const char *me, const char *room, const char *who)
 
{
 
	print_message(("%s left the chatroom %s [%s]", who, room, me));
 
}
 

	
 
static void ext_yahoo_chat_message(int id, const char *me, const char *who, const char *room, const char *msg, int msgtype, int utf8)
 
{
 
	char * umsg = (char *)msg;
 
	char * charpos;
 

	
 
	if(utf8)
 
		umsg = y_utf8_to_str(msg);
 
	/* Remove escapes */
 
	charpos = umsg;
 
	while(*charpos) {
 
		if (*charpos == 0x1b) {
 
			*charpos = ' ';
 
		}
 
		charpos++;
 
	}
 

	
 
	if (msgtype == 2) {
 
		print_message(("(in %s [%s]) %s %s", room, me, who, umsg));
 
	} else {
 
		print_message(("(in %s [%s]) %s: %s", room, me, who, umsg));
 
	}
 

	
 
	if(utf8)
 
		FREE(umsg);
 
}
 

	
 
static void ext_yahoo_status_changed(int id, const char *who, int stat, const char *msg, int away, int idle, int mobile)
 
{
 
	yahoo_account * ya=NULL;
 
	YList * b;
 
	char buf[1024];
 

	
 
	for(b = buddies; b; b = b->next) {
 
		if(!strcmp(((yahoo_account *)b->data)->yahoo_id, who)) {
 
			ya = b->data;
 
			break;
 
		}
 
	}
 
	
 
	if (msg == NULL) {
 
		sprintf(buf, "%s", yahoo_status_code(stat));
 
	}
 
	else if (stat == YAHOO_STATUS_CUSTOM) {
 
		sprintf(buf, "%s", msg);
 
	} else {
 
		sprintf(buf, "%s: %s", yahoo_status_code(stat), msg);
 
	}
 

	
 
	if (away > 0) {
 
		char away_buf[32];
 
		sprintf(away_buf, " away[%d]", away);
 
		strcat(buf, away_buf);
 
	}
 

	
 
	if (mobile > 0) {
 
		char mobile_buf[32];
 
		sprintf(mobile_buf, " mobile[%d]", mobile);
 
		strcat(buf, mobile_buf);
 
	}
 

	
 
	if (idle > 0) {
 
		char time_buf[32];
 
		sprintf(time_buf, " idle for %d:%02d:%02d", idle/3600, (idle/60)%60, idle%60);
 
		strcat(buf, time_buf);
 
	}
 

	
 
	print_message(("%s (%s) is now %s", ya?ya->name:who, who, buf))
 

	
 
	if(ya) {
 
		ya->status = stat;
 
		ya->away = away;
 
		if(msg) {
 
			FREE(ya->msg);
 
			ya->msg = strdup(msg);
 
		}
 
	}
 
}
 

	
 
static void ext_yahoo_got_buddies(int id, YList * buds)
 
{
 
	while(buddies) {
 
		FREE(buddies->data);
 
		buddies = buddies->next;
 
		if(buddies)
 
			FREE(buddies->prev);
 
	}
 
	for(; buds; buds = buds->next) {
 
		yahoo_account *ya = y_new0(yahoo_account, 1);
 
		struct yahoo_buddy *bud = buds->data;
 
		strncpy(ya->yahoo_id, bud->id, 255);
 
		if(bud->real_name)
 
			strncpy(ya->name, bud->real_name, 255);
 
		strncpy(ya->group, bud->group, 255);
 
		ya->status = YAHOO_STATUS_OFFLINE;
 
		buddies = y_list_append(buddies, ya);
 

	
 
/*		print_message(("%s is %s", bud->id, bud->real_name));*/
 
	}
 
}
 

	
 
static void ext_yahoo_got_ignore(int id, YList * igns)
 
{
 
}
 

	
 
static void ext_yahoo_got_buzz(int id, const char *me, const char *who, long tm)
 
{
 
	who = get_buddy_name(who);
 

	
 
	printf("\a");
 
	if(tm) {
 
		char timestr[255];
 

	
 
		strncpy(timestr, ctime((time_t *)&tm), sizeof(timestr));
 
		timestr[strlen(timestr) - 1] = '\0';
 

	
 
		print_message(("[Offline message at %s to %s from %s]: **DING**",
 
				timestr, me, who))
 
	} else
 
		print_message(("[%s]%s: **DING**", me, who))
 
}
 

	
 
static void ext_yahoo_got_im(int id, const char *me, const char *who, const char *msg, long tm, int stat, int utf8)
 
{
 
	char *umsg = (char *)msg;
 

	
 
	if(stat == 2) {
 
		LOG(("Error sending message from %s to %s", me, who));
 
		return;
 
	}
 

	
 
	if(!msg)
 
		return;
 

	
 
	if(utf8)
 
		umsg = y_utf8_to_str(msg);
 
	
 
	who = get_buddy_name(who);
 

	
 
	if(tm) {
 
		char timestr[255];
 

	
 
		strncpy(timestr, ctime((time_t *)&tm), sizeof(timestr));
 
		timestr[strlen(timestr) - 1] = '\0';
 

	
 
		print_message(("[Offline message at %s to %s from %s]: %s", 
 
				timestr, me, who, umsg))
 
	} else
 
		print_message(("[%s]%s: %s", me, who, umsg))
 

	
 
	if(utf8)
 
		FREE(umsg);
 
}
 

	
 
static void ext_yahoo_rejected(int id, const char *who, const char *msg)
 
{
 
	print_message(("%s has rejected you%s%s", who, 
 
				(msg?" with the message:\n":"."), 
 
				(msg?msg:"")));
 
}
 

	
 
static void ext_yahoo_contact_added(int id, const char *myid, const char *who, const char *msg)
 
{
 
	char buff[1024];
 

	
 
	snprintf(buff, sizeof(buff), "%s, the yahoo user %s has added you to their contact list", myid, who);
 
	if(msg) {
 
		strcat(buff, " with the following message:\n");
 
		strcat(buff, msg);
 
		strcat(buff, "\n");
 
	} else {
 
		strcat(buff, ".  ");
 
	}
 
	strcat(buff, "Do you want to allow this [Y/N]?");
 

	
 
/*	print_message((buff));
 
	scanf("%c", &choice);
 
	if(choice != 'y' && choice != 'Y')
 
		yahoo_reject_buddy(id, who, "Thanks, but no thanks.");
 
*/
 
}
 

	
 
static void ext_yahoo_typing_notify(int id, const char* me, const char *who, int stat)
 
{
 
	if(stat && do_typing_notify)
 
		print_message(("[%s]%s is typing...", me, who));
 
}
 

	
 
static void ext_yahoo_game_notify(int id, const char *me, const char *who, int stat, const char *msg)
 
{
 
}
 

	
 
static void ext_yahoo_mail_notify(int id, const char *from, const char *subj, int cnt)
 
{
 
	char buff[1024] = {0};
 
	
 
	if(!do_mail_notify)
 
		return;
 

	
 
	if(from && subj)
 
		snprintf(buff, sizeof(buff), 
 
				"You have new mail from %s about %s\n", 
 
				from, subj);
 
	if(cnt) {
 
		char buff2[100];
 
		snprintf(buff2, sizeof(buff2), 
 
				"You have %d message%s\n", 
 
				cnt, cnt==1?"":"s");
 
		strcat(buff, buff2);
 
	}
 

	
 
	if(buff[0])
 
		print_message((buff));
 
}
 

	
 
static void ext_yahoo_got_webcam_image(int id, const char *who,
 
		const unsigned char *image, unsigned int image_size, unsigned int real_size,
 
		unsigned int timestamp)
 
{
 
	static unsigned char *cur_image = NULL;
 
	static unsigned int cur_image_len = 0;
 
	static unsigned int image_num = 0;
 
	FILE* f_image;
 
	char fname[1024];
 

	
 
	/* copy image part to cur_image */
 
	if (real_size)
 
	{
 
		if (!cur_image) cur_image = y_new0(unsigned char, image_size);
 
		memcpy(cur_image + cur_image_len, image, real_size);
 
		cur_image_len += real_size;
 
	}
 

	
 
	if (image_size == cur_image_len)
 
	{
 
		print_message(("Received a image update at %d (%d bytes)",
 
			 timestamp, image_size));
 

	
 
		/* if we recieved an image then write it to file */
 
		if (image_size)
 
		{
 
			sprintf(fname, "images/%s_%.3d.jpc", who, image_num++);
 

	
 
			if ((f_image = fopen(fname, "w")) != NULL) {
 
				fwrite(cur_image, image_size, 1, f_image);
 
				fclose(f_image);
 
			} else {
 
				printf("Error writing to %s\n", fname);
 
			}
 
			FREE(cur_image);
 
			cur_image_len = 0;
 
			if (image_num > 999) image_num = 0;
 
		}
 
	}
 
}
 

	
 
static void ext_yahoo_webcam_viewer(int id, const char *who, int connect)
 
{
 
	switch (connect)
 
	{
 
		case 0:
 
			print_message(("%s has stopped viewing your webcam", who));
 
			break;
 
		case 1:
 
			print_message(("%s has started viewing your webcam", who));
 
			break;
 
		case 2:
 
			print_message(("%s is trying to view your webcam", who));
 
			yahoo_webcam_accept_viewer(id, who, accept_webcam_viewers);
 
			break;
 
	}
 
}
 

	
 
static void ext_yahoo_webcam_closed(int id, const char *who, int reason)
 
{
 
	switch(reason)
 
	{
 
		case 1:
 
			print_message(("%s stopped broadcasting", who));
 
			break;
 
		case 2:
 
			print_message(("%s cancelled viewing permission", who));
 
			break;
 
		case 3:
 
			print_message(("%s declines permission to view his/her webcam", who));
 
			break;
 
		case 4:
 
			print_message(("%s does not have his/her webcam online", who));
 
			break;
 
	}
 
}
 

	
 
static void ext_yahoo_webcam_data_request(int id, int send)
 
{
 
	webcam_id = id;
 

	
 
	if (send) {
 
		print_message(("Got request to start sending images"));
 
		if (!webcamTimer)
 
			rearm(&webcamTimer, 2);
 
	} else {
 
		print_message(("Got request to stop sending images"));
 
	}
 
	send_webcam_images = send;
 
}
 

	
 
static void ext_yahoo_webcam_invite(int id, const char *me, const char *from)
 
{
 
	print_message(("Got a webcam invitation to %s from %s", me, from));
 
}
 

	
 
static void ext_yahoo_webcam_invite_reply(int id, const char *me, const char *from, int accept)
 
{
 
	if(accept) {
 
		print_message(("[%s]%s accepted webcam invitation...", me, from));
 
	} else {
 
		print_message(("[%s]%s declined webcam invitation...", me, from));
 
	}
 
}
 

	
 
static void ext_yahoo_system_message(int id, const char *me, const char *who, const char *msg)
 
{
 
	if(ignore_system)
 
		return;
 

	
 
	print_message(("Yahoo System Message: %s", msg));
 
}
 

	
 
void yahoo_logout()
 
{
 
	if (ylad->id <= 0) {
 
		return;
 
	}
 

	
 
	pingTimer=0;
 

	
 
	while(conferences) {
 
		YList * n = conferences->next;
 
		conf_room * cr = conferences->data;
 
		if(cr->joined)
 
			yahoo_conference_logoff(ylad->id, NULL, cr->members, cr->room_name);
 
		FREE(cr->me);
 
		FREE(cr->room_name);
 
		FREE(cr->host);
 
		while(cr->members) {
 
			YList *n = cr->members->next;
 
			FREE(cr->members->data);
 
			FREE(cr->members);
 
			cr->members=n;
 
		}
 
		FREE(cr);
 
		FREE(conferences);
 
		conferences = n;
 
	}
 
	
 
	yahoo_logoff(ylad->id);
 
	yahoo_close(ylad->id);
 

	
 
	ylad->status = YAHOO_STATUS_OFFLINE;
 
	ylad->id = 0;
 

	
 
	poll_loop=0;
 

	
 
	print_message(("logged_out"));
 
}
 

	
 
static void ext_yahoo_login(yahoo_local_account * ylad, int login_mode)
 
{
 
	LOG(("ext_yahoo_login"));
 

	
 
	ylad->id = yahoo_init_with_attributes(ylad->yahoo_id, ylad->password, 
 
			"local_host", local_host,
 
			"pager_port", 5050,
 
			NULL);
 
	ylad->status = YAHOO_STATUS_OFFLINE;
 
	yahoo_login(ylad->id, login_mode);
 

	
 
/*	if (ylad->id <= 0) {
 
		print_message(("Could not connect to Yahoo server.  Please verify that you are connected to the net and the pager host and port are correctly entered."));
 
		return;
 
	}
 
*/
 
	rearm(&pingTimer, 600);
 
}
 

	
 
static void ext_yahoo_got_cookies(int id)
 
{
 
	/*yahoo_get_yab(id);*/
 
}
 

	
 
static void ext_yahoo_login_response(int id, int succ, const char *url)
 
{
 
	char buff[1024];
 

	
 
	if(succ == YAHOO_LOGIN_OK) {
 
		ylad->status = yahoo_current_status(id);
 
		print_message(("logged in"));
 
		return;
 
		
 
	} else if(succ == YAHOO_LOGIN_UNAME) {
 

	
 
		snprintf(buff, sizeof(buff), "Could not log into Yahoo service - username not recognised.  Please verify that your username is correctly typed.");
 
	} else if(succ == YAHOO_LOGIN_PASSWD) {
 

	
 
		snprintf(buff, sizeof(buff), "Could not log into Yahoo service - password incorrect.  Please verify that your password is correctly typed.");
 

	
 
	} else if(succ == YAHOO_LOGIN_LOCK) {
 
		
 
		snprintf(buff, sizeof(buff), "Could not log into Yahoo service.  Your account has been locked.\nVisit %s to reactivate it.", url);
 

	
 
	} else if(succ == YAHOO_LOGIN_DUPL) {
 

	
 
		snprintf(buff, sizeof(buff), "You have been logged out of the yahoo service, possibly due to a duplicate login.");
 
	} else if(succ == YAHOO_LOGIN_SOCK) {
 

	
 
		snprintf(buff, sizeof(buff), "The server closed the socket.");
 
	} else {
 
		snprintf(buff, sizeof(buff), "Could not log in, unknown reason: %d.", succ);
 
	}
 

	
 
	ylad->status = YAHOO_STATUS_OFFLINE;
 
	print_message((buff));
 
	yahoo_logout();
 
	poll_loop=0;
 
}
 

	
 
static void ext_yahoo_error(int id, const char *err, int fatal, int num)
 
{
 
	fprintf(stdout, "Yahoo Error: ");
 
	fprintf(stdout, "%s", err);
 
	switch(num) {
 
		case E_UNKNOWN:
 
			fprintf(stdout, "unknown error %s", err);
 
			break;
 
		case E_CUSTOM:
 
			fprintf(stdout, "custom error %s", err);
 
			break;
 
		case E_CONFNOTAVAIL:
 
			fprintf(stdout, "%s is not available for the conference", err);
 
			break;
 
		case E_IGNOREDUP:
 
			fprintf(stdout, "%s is already ignored", err);
 
			break;
 
		case E_IGNORENONE:
 
			fprintf(stdout, "%s is not in the ignore list", err);
 
			break;
 
		case E_IGNORECONF:
 
			fprintf(stdout, "%s is in buddy list - cannot ignore ", err);
 
			break;
 
		case E_SYSTEM:
 
			fprintf(stdout, "system error %s", err);
 
			break;
 
		case E_CONNECTION:
 
			fprintf(stdout, "server connection error %s", err);
 
			break;
 
	}
 
	fprintf(stdout, "\n");
 
	if(fatal)
 
		yahoo_logout();
 
}
 

	
 
void yahoo_set_current_state(int yahoo_state)
 
{
 
	if (ylad->status == YAHOO_STATUS_OFFLINE && yahoo_state != YAHOO_STATUS_OFFLINE) {
 
		ext_yahoo_login(ylad, yahoo_state);
 
		return;
 
	} else if (ylad->status != YAHOO_STATUS_OFFLINE && yahoo_state == YAHOO_STATUS_OFFLINE) {
 
		yahoo_logout();
 
		return;
 
	}
 

	
 
	ylad->status = yahoo_state;
 
	if(yahoo_state == YAHOO_STATUS_CUSTOM) {
 
		if(ylad->msg)
 
			yahoo_set_away(ylad->id, yahoo_state, ylad->msg, 1);
 
		else
 
			yahoo_set_away(ylad->id, yahoo_state, "delta p * delta x too large", 1);
 
	} else
 
		yahoo_set_away(ylad->id, yahoo_state, NULL, 1);
 
}
 

	
 
static int ext_yahoo_connect(const char *host, int port)
 
{
 
	WARNING(("This should not be used anymore. File a bug report."));
 
	return -1;
 
}
 

	
 
/*************************************
 
 * Callback handling code starts here
 
 */
 
YList *connections = NULL;
 
struct _conn {
 
	int fd;
 
	SSL *ssl;
 
	int use_ssl;
 
	int remove;
 
};
 

	
 
struct conn_handler {
 
	struct _conn *con;
 
	int id;
 
	int tag;
 
	yahoo_input_condition cond;
 
	int remove;
 
	void *data;
 
};
 

	
 
static int connection_tags=0;
 

	
 
static int ext_yahoo_add_handler(int id, void *d, yahoo_input_condition cond, void *data)
 
{
 
	struct conn_handler *h = y_new0(struct conn_handler, 1);
 

	
 
	h->id = id;
 
	h->tag = ++connection_tags;
 
	h->con = d;
 
	h->cond = cond;
 
	h->data = data;
 

	
 
	LOG(("Add %d(%d) for %d, tag %d", h->con->fd, cond, id, h->tag));
 

	
 
	connections = y_list_prepend(connections, h);
 

	
 
	return h->tag;
 
}
 

	
 
static void ext_yahoo_remove_handler(int id, int tag)
 
{
 
	YList *l;
 
	if (!tag)
 
		return;
 

	
 
	for(l = connections; l; l = y_list_next(l)) {
 
		struct conn_handler *c = l->data;
 
		if(c->tag == tag) {
 
			/* don't actually remove it, just mark it for removal */
 
			/* we'll remove when we start the next poll cycle */
 
			LOG(("Marking id:%d fd:%p tag:%d for removal",
 
				c->id, c->con, c->tag));
 
			c->remove = 1;
 
			return;
 
		}
 
	}
 
}
 

	
 
static SSL *do_ssl_connect(int fd)
 
{
 
	SSL *ssl;
 
	SSL_CTX *ctx;
 

	
 
	LOG(("SSL Handshake"));
 

	
 
	SSL_library_init ();
 
	ctx = SSL_CTX_new(SSLv23_client_method());
 
	ssl = SSL_new(ctx);
 
	SSL_CTX_free(ctx);
 
	SSL_set_fd(ssl, fd);
 

	
 
	if (SSL_connect(ssl) == 1)
 
		return ssl;
 

	
 
	return NULL;
 
}
 

	
 
struct connect_callback_data {
 
	yahoo_connect_callback callback;
 
	void * callback_data;
 
	int id;
 
	int tag;
 
};
 

	
 
static void connect_complete(void *data, struct _conn *source, yahoo_input_condition condition)
 
{
 
	struct connect_callback_data *ccd = data;
 
	int error, err_size = sizeof(error);
 

	
 
	ext_yahoo_remove_handler(0, ccd->tag);
 
	getsockopt(source->fd, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&err_size);
 

	
 
	if(error)
 
		goto err;
 

	
 
	LOG(("Connected fd: %d, error: %d", source->fd, error));
 

	
 
	if (source->use_ssl) {
 
		source->ssl = do_ssl_connect(source->fd);
 

	
 
		if (!source->ssl) {
 
err:
 
			LOG(("SSL Handshake Failed!"));
 
			ext_yahoo_close(source);
 

	
 
			ccd->callback(NULL, 0, ccd->callback_data);
 
			FREE(ccd);
 
			return;
 
		}
 
	}
 

	
 
	fcntl(source->fd, F_SETFL, O_NONBLOCK);
 

	
 
	ccd->callback(source, error, ccd->callback_data);
 
	FREE(ccd);
 
}
 

	
 
void yahoo_callback(struct conn_handler *c, yahoo_input_condition cond)
 
{
 
	int ret=1;
 
	char buff[1024]={0};
 

	
 
	if(c->id < 0) {
 
		connect_complete(c->data, c->con, cond);
 
	} else {
 
		if(cond & YAHOO_INPUT_READ)
 
			ret = yahoo_read_ready(c->id, c->con, c->data);
 
		if(ret>0 && cond & YAHOO_INPUT_WRITE)
 
			ret = yahoo_write_ready(c->id, c->con, c->data);
 

	
 
		if(ret == -1)
 
			snprintf(buff, sizeof(buff), 
 
				"Yahoo read error (%d): %s", errno, strerror(errno));
 
		else if(ret == 0)
 
			snprintf(buff, sizeof(buff), 
 
				"Yahoo read error: Server closed socket");
 

	
 
		if(buff[0])
 
			print_message((buff));
 
	}
 
}
 

	
 
static int ext_yahoo_write(void *fd, char *buf, int len)
 
{
 
	struct _conn *c = fd;
 

	
 
	if (c->use_ssl)
 
		return SSL_write(c->ssl, buf, len);
 
	else
 
		return write(c->fd, buf, len);
 
}
 

	
 
static int ext_yahoo_read(void *fd, char *buf, int len)
 
{
 
	struct _conn *c = fd;
 

	
 
	if (c->use_ssl)
 
		return SSL_read(c->ssl, buf, len);
 
	else
 
		return read(c->fd, buf, len);
 
}
 

	
 
static void ext_yahoo_close(void *fd)
 
{
 
	struct _conn *c = fd;
 
	YList *l;
 

	
 
	if (c->use_ssl)
 
		SSL_free(c->ssl);
 

	
 
	close(c->fd);
 
	c->fd = 0;
 

	
 
	/* Remove all handlers */
 
	for (l = connections; l; l = y_list_next(l)) {
 
		struct conn_handler *h = l->data;
 

	
 
		if (h->con == c)
 
			h->remove = 1;
 
	}
 

	
 
	c->remove = 1;
 
}
 

	
 
static int ext_yahoo_connect_async(int id, const char *host, int port, 
 
		yahoo_connect_callback callback, void *data, int use_ssl)
 
{
 
	struct sockaddr_in serv_addr;
 
	static struct hostent *server;
 
	int servfd;
 
	struct connect_callback_data * ccd;
 
	int error;
 
	SSL *ssl = NULL;
 

	
 
	struct _conn *c;
 

	
 
	LOG(("Connecting to %s:%d", host, port));
 
	
 
	if(!(server = gethostbyname(host))) {
 
		errno=h_errno;
 
		return -1;
 
	}
 

	
 
	if((servfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 
		return -1;
 
	}
 

	
 
	memset(&serv_addr, 0, sizeof(serv_addr));
 
	serv_addr.sin_family = AF_INET;
 
	memcpy(&serv_addr.sin_addr.s_addr, *server->h_addr_list, server->h_length);
 
	serv_addr.sin_port = htons(port);
 

	
 
	c = y_new0(struct _conn, 1);
 
	c->fd = servfd;
 
	c->use_ssl = use_ssl;
 

	
 
	error = connect(servfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
 

	
 
	LOG(("Trying to connect: fd:%d error:%d", servfd, error));
 
	if(!error) {
 
		LOG(("Connected"));
 
		if (use_ssl) {
 
			ssl = do_ssl_connect(servfd);
 

	
 
			if (!ssl) {
 
				LOG(("SSL Handshake Failed!"));
 
				ext_yahoo_close(c);
 

	
 
				callback(NULL, 0, data);
 
				return -1;
 
			}
 
		}
 

	
 
		c->ssl = ssl;
 
		fcntl(c->fd, F_SETFL, O_NONBLOCK);
 

	
 
		callback(c, 0, data);
 
		return 0;
 
	} else if(error == -1 && errno == EINPROGRESS) {
 
		ccd = calloc(1, sizeof(struct connect_callback_data));
 
		ccd->callback = callback;
 
		ccd->callback_data = data;
 
		ccd->id = id;
 

	
 
		ccd->tag = ext_yahoo_add_handler(-1, c, YAHOO_INPUT_WRITE, ccd);
 
		return ccd->tag;
 
	} else {
 
		if(error == -1)
 
			LOG(("Connection failure: %s", strerror(errno)));
 

	
 
		ext_yahoo_close(c);
 

	
 
		callback(NULL, errno, data);
 
		return -1;
 
	}
 
}
 
/*
 
 * Callback handling code ends here
 
 ***********************************/
 

	
 
static void process_commands(char *line)
 
{
 
	char *cmd, *to, *msg;
 

	
 
	char *tmp, *start;
 
	char *copy = strdup(line);
 
	int yid = 0;
 

	
 
	enum yahoo_status state;
 

	
 
	start = cmd = copy;
 
	tmp = strchr(copy, ' ');
 
	if(tmp) {
 
		*tmp = '\0';
 
		copy = tmp+1;
 
	} else {
 
		copy = NULL;
 
	}
 

	
 
	if(!strncasecmp(cmd, "MSG", strlen("MSG"))) {
 
		/* send a message */
 
		to = copy;
 
		tmp = strchr(copy, ' ');
 
		if(tmp) {
 
			*tmp = '\0';
 
			copy = tmp+1;
 
		}
 
		msg = copy;
 
		if(to && msg) {
 
			if(!strcmp(msg, "\a"))
 
				yahoo_send_im(ylad->id, NULL, to, "<ding>", 0,0);
 
			else {
 
				msg = y_str_to_utf8(msg);
 
				yahoo_send_im(ylad->id, NULL, to, msg, 1,0);
 
				FREE(msg)
 
			}
 
		}
 
	} else if(!strncasecmp(cmd, "CMS", strlen("CMS"))) {
 
		/* send a message */
 
		conf_room * cr;
 
		to = copy;
 
		tmp = strchr(copy, ' ');
 
		if(tmp) {
 
			*tmp = '\0';
 
			copy = tmp+1;
 
		}
 
		msg = copy;
 
		cr = find_conf_room_by_name_and_id(ylad->id, NULL, to);
 
		if(!cr) {
 
			print_message(("no such room: %s", copy));
 
			goto end_parse;
 
		}
 
		if(msg)
 
			yahoo_conference_message(ylad->id, NULL, cr->members, to, msg, 0);
 
	} else if(!strncasecmp(cmd, "CLS", strlen("CLS"))) {
 
		YList * l;
 
		if(copy) {
 
			conf_room * cr = find_conf_room_by_name_and_id(ylad->id, NULL, copy);
 
			if(!cr) {
 
				print_message(("no such room: %s", copy));
 
				goto end_parse;
 
			}
 
			print_message(("Room: %s", copy));
 
			for(l = cr->members; l; l=l->next) {
 
				print_message(("%s", (char *)l->data))
 
			}
 
		} else {
 
			print_message(("All Rooms:"));
 
			for(l = conferences; l; l=l->next) {
 
				conf_room * cr = l->data;
 
				print_message(("%s", cr->room_name));
 
			}
 
		}
 

	
 
	} else if(!strncasecmp(cmd, "CCR", strlen("CCR"))) {
 
		conf_room * cr = y_new0(conf_room, 1);
 
		while((tmp = strchr(copy, ' ')) != NULL) {
 
			*tmp = '\0';
 
			if(!cr->room_name)
 
				cr->room_name = strdup(copy);
 
			else
 
				cr->members = y_list_append(cr->members,
 
						strdup(copy));
 
			copy = tmp+1;
 
		}
 
		cr->members = y_list_append(cr->members, strdup(copy));
 

	
 
		if(!cr->room_name || !cr->members) {
 
			FREE(cr);
 
		} else {
 
			cr->id = ylad->id;
 
			cr->joined = 1;
 
			conferences = y_list_append(conferences, cr);
 
			yahoo_conference_invite(ylad->id, NULL, cr->members, cr->room_name, "Join my conference");
 
			cr->members = y_list_append(cr->members,strdup(ylad->yahoo_id));
 
		}
 
	} else if(!strncasecmp(cmd, "CIN", strlen("CIN"))) {
 
		conf_room * cr;
 
		char * room=copy;
 
		YList * l1, *l = NULL;
 

	
 
		while((tmp = strchr(copy, ' ')) != NULL) {
 
			*tmp = '\0';
 
			copy = tmp+1;
 
			l = y_list_append(l, copy);
 
		}
 

	
 
		cr = find_conf_room_by_name_and_id(ylad->id, NULL, room);
 
		if(!cr) {
 
			print_message(("no such room: %s", room));
 
			y_list_free(l);
 
			goto end_parse;
 
		}
 

	
 
		for(l1 = l; l1; l1=l1->next) {
 
			char * w = l1->data;
 
			yahoo_conference_addinvite(ylad->id, NULL, w, room, cr->members, "Join my conference");
 
			cr->members = y_list_append(cr->members, strdup(w));
 
		}
 
		y_list_free(l);
 

	
 
	} else if(!strncasecmp(cmd, "CLN", strlen("CLN"))) {
 
		conf_room * cr = find_conf_room_by_name_and_id(ylad->id, NULL, copy);
 
		YList * l;
 
		if(!cr) {
 
			print_message(("no such room: %s", copy));
 
			goto end_parse;
 
		}
 

	
 
		cr->joined = 1;
 
		for(l = cr->members; l; l=l->next) {
 
			char * w = l->data;
 
			if(!strcmp(w, ylad->yahoo_id))
 
				break;
 
		}
 
		if(!l)
 
			cr->members = y_list_append(cr->members, strdup(ylad->yahoo_id));
 
		yahoo_conference_logon(ylad->id, NULL, cr->members, copy);
 

	
 
	} else if(!strncasecmp(cmd, "CLF", strlen("CLF"))) {
 
		conf_room * cr = find_conf_room_by_name_and_id(ylad->id, NULL, copy);
 
		
 
		if(!cr) {
 
			print_message(("no such room: %s", copy));
 
			goto end_parse;
 
		}
 

	
 
		yahoo_conference_logoff(ylad->id, NULL, cr->members, copy);
 

	
 
		conferences = y_list_remove(conferences, cr);
 
		FREE(cr->room_name);
 
		FREE(cr->host);
 
		while(cr->members) {
 
			YList *n = cr->members->next;
 
			FREE(cr->members->data);
 
			FREE(cr->members);
 
			cr->members=n;
 
		}
 
		FREE(cr);
 

	
 
	} else if(!strncasecmp(cmd, "CDC", strlen("CDC"))) {
 
		conf_room * cr;
 
		char * room = copy;
 
		tmp = strchr(copy, ' ');
 
		if(tmp) {
 
			*tmp = '\0';
 
			copy = tmp+1;
 
			msg = copy;
 
		} else {
 
			msg = "Thanks, but no thanks!";
 
		}
 
		
 
		cr = find_conf_room_by_name_and_id(ylad->id, NULL, room);
 
		if(!cr) {
 
			print_message(("no such room: %s", room));
 
			goto end_parse;
 
		}
 

	
 
		yahoo_conference_decline(ylad->id, NULL, cr->members, room,msg);
 

	
 
		conferences = y_list_remove(conferences, cr);
 
		FREE(cr->room_name);
 
		FREE(cr->host);
 
		while(cr->members) {
 
			YList *n = cr->members->next;
 
			FREE(cr->members->data);
 
			FREE(cr->members);
 
			cr->members=n;
 
		}
 
		FREE(cr);
 

	
 

	
 
	} else if(!strncasecmp(cmd, "CHL", strlen("CHL"))) {
 
		int roomid;
 
		roomid = atoi(copy);
 
		yahoo_get_chatrooms(ylad->id, roomid);
 
	} else if(!strncasecmp(cmd, "CHJ", strlen("CHJ"))) {
 
		char *roomid, *roomname;
 
/* Linux, FreeBSD, Solaris:1 */
 
/* 1600326591 */
 
		roomid = copy;
 
		tmp = strchr(copy, ' ');
 
		if(tmp) {
 
			*tmp = '\0';
 
			copy = tmp+1;
 
		}
 
		roomname = copy;
 
		if(roomid && roomname) {
 
			yahoo_chat_logon(ylad->id, NULL, roomname, roomid);
 
		}
 

	
 
	} else if(!strncasecmp(cmd, "CHM", strlen("CHM"))) {
 
		char *msg, *roomname;
 
		roomname = copy;
 
		tmp = strstr(copy, "  ");
 
		if(tmp) {
 
			*tmp = '\0';
 
			copy = tmp+2;
 
		}
 
		msg = copy;
 
		if(roomname && msg) {
 
			yahoo_chat_message(ylad->id, NULL, roomname, msg, 1, 0);
 
		}
 

	
 
	} else if(!strncasecmp(cmd, "CHX", strlen("CHX"))) {
 
		yahoo_chat_logoff(ylad->id, NULL);
 
	} else if(!strncasecmp(cmd, "STA", strlen("STA"))) {
 
		if(isdigit(copy[0])) {
 
			state = (enum yahoo_status)atoi(copy);
 
			copy = strchr(copy, ' ');
 
			if(state == 99) {
 
				if(copy)
 
					msg = copy;
 
				else
 
					msg = "delta x * delta p too large";
 
			} else
 
				msg = NULL;
 
		} else {
 
			state = YAHOO_STATUS_CUSTOM;
 
			msg = copy;
 
		}
 

	
 
		yahoo_set_away(ylad->id, state, msg, 1);
 

	
 
	} else if(!strncasecmp(cmd, "OFF", strlen("OFF"))) {
 
		/* go offline */
 
		printf("Going offline\n");
 
		poll_loop=0;
 
	} else if(!strncasecmp(cmd, "IDS", strlen("IDS"))) {
 
		/* print identities */
 
		const YList * ids = yahoo_get_identities(ylad->id);
 
		printf("Identities: ");
 
		for(; ids; ids = ids->next)
 
			printf("%s, ", (char *)ids->data);
 
		printf("\n");
 
	} else if(!strncasecmp(cmd, "AID", strlen("AID"))) {
 
		/* activate identity */
 
		yahoo_set_identity_status(ylad->id, copy, 1);
 
	} else if(!strncasecmp(cmd, "DID", strlen("DID"))) {
 
		/* deactivate identity */
 
		yahoo_set_identity_status(ylad->id, copy, 0);
 
	} else if(!strncasecmp(cmd, "LST", strlen("LST"))) {
 
		YList * b = buddies;
 
		for(; b; b=b->next) {
 
			yahoo_account * ya = b->data;
 
			if(ya->status == YAHOO_STATUS_OFFLINE)
 
				continue;
 
			if(ya->msg)
 
				print_message(("%s (%s) is now %s", ya->name, ya->yahoo_id, 
 
							ya->msg))
 
			else
 
				print_message(("%s (%s) is now %s", ya->name, ya->yahoo_id, 
 
						yahoo_status_code(ya->status)))
 
		}
 
	} else if(!strncasecmp(cmd, "NAM", strlen("NAM"))) {
 
		struct yab * yab;
 
		
 
		to = copy;
 
		tmp = strchr(copy, ' ');
 
		if(tmp) {
 
			*tmp = '\0';
 
			copy = tmp+1;
 
		}
 
		yid = atoi(copy);
 

	
 
		tmp = strchr(copy, ' ');
 
		if(tmp) {
 
			*tmp = '\0';
 
			copy = tmp+1;
 
		}
 
		msg = copy;
 

	
 
		if(to && msg) {
 
			yab = y_new0(struct yab, 1);
 
			yab->id = to;
 
			yab->yid = yid;	/* Only do this if you have got it from the server */
 
			yab->nname = msg;
 
			yahoo_set_yab(ylad->id, yab);
 
			FREE(yab);
 
		}
 
	} else if(!strncasecmp(cmd, "WCAM", strlen("WCAM"))) {
 
		if (copy)
 
		{
 
			printf("Viewing webcam (%s)\n", copy);
 
			webcam_direction = YAHOO_WEBCAM_DOWNLOAD;
 
			yahoo_webcam_get_feed(ylad->id, copy);
 
		} else {
 
			printf("Starting webcam\n");
 
			webcam_direction = YAHOO_WEBCAM_UPLOAD;
 
			yahoo_webcam_get_feed(ylad->id, NULL);
 
		}
 
	} else if(!strncasecmp(cmd, "WINV", strlen("WINV"))) {
 
		printf("Inviting %s to view webcam\n", copy);
 
		yahoo_webcam_invite(ylad->id, copy);
 
	} else {
 
		fprintf(stderr, "Unknown command: %s\n", cmd);
 
	}
 

	
 
end_parse:
 
	FREE(start);
 
}
 

	
 
#ifndef _WIN32
 
static void local_input_callback(int source)
 
{
 
	char line[1024] = {0};
 
	int i;
 
	char c;
 
	i=0; c=0;
 
	do {
 
		if(read(source, &c, 1) <= 0)
 
			c='\0';
 
		if(c == '\r')
 
			continue;
 
		if(c == '\n')
 
			break;
 
		if(c == '\b') {
 
			if(!i)
 
				continue;
 
			c = '\0';
 
			i--;
 
		}
 
		if(c) {
 
			line[i++] = c;
 
			line[i]='\0';
 
		}
 
	} while(i<1023 && c != '\n');
 

	
 
	if(line[0])
 
		process_commands(line);
 
}
 
#else
 
#include <conio.h>
 
static void local_input_callback(char c)
 
{
 
	static char line[1024] = {0};
 
	static int line_length = 0;
 

	
 
	if (c == '\b' || (int)c == 127) {
 
		if (line_length > 0) {
 
			_cputs("\b \b");
 
			line_length--;
 
		}
 
		return;
 
	}
 

	
 
	if (c == '\n' || c == '\r' || c == 3) {
 
		_cputs("\n");
 
		line[line_length] = 0;
 
		process_commands(line);
 
		line_length = 0;
 
		line[0] = 0;
 
		return;
 
	}
 

	
 
	_putch(c);
 
	line[line_length++] = c;
 
}
 
#endif
 

	
 
int main(int argc, char * argv[])
 
{
 
	int status;
 
	int log_level;
 
	int lfd=0;
 

	
 
	fd_set inp, outp;
 
	struct timeval tv;
 

	
 
#ifndef _WIN32
 
	int fd_stdin = fileno(stdin);
 
#endif
 
	YList *l=connections;
 

	
 
#ifdef _WIN32
 
	WSADATA wsa;	
 
	if (WSAStartup(MAKEWORD(2,2), &wsa))
 
		return -1;
 
#endif
 

	
 
	ylad = y_new0(yahoo_local_account, 1);
 

	
 
	local_host = strdup(get_local_addresses());
 

	
 
	printf("Yahoo Id: ");
 
	scanf("%s", ylad->yahoo_id);
 
	printf("Password: ");
 
#ifdef _WIN32
 
	scanf("%s", ylad->password);
 
#else
 
	{
 
		tcflag_t oflags;
 
		struct termios term;
 
		tcgetattr(fd_stdin, &term);
 
		oflags = term.c_lflag;
 
		term.c_lflag = oflags & ~(ECHO | ECHOK | ICANON);
 
		term.c_cc[VTIME] = 1;
 
		tcsetattr(fd_stdin, TCSANOW, &term);
 
		
 
		scanf("%s", ylad->password);
 

	
 
		term.c_lflag = oflags;
 
		term.c_cc[VTIME] = 0;
 
		tcsetattr(fd_stdin, TCSANOW, &term);
 
	}
 
	printf("\n");
 
#endif
 

	
 
	printf("Initial Status: ");
 
	scanf("%d", &status);
 

	
 
	printf("Log Level: ");
 
	scanf("%d", &log_level);
 
	do_yahoo_debug=log_level;
 

	
 
	register_callbacks();
 
	yahoo_set_log_level(log_level);
 

	
 
	ext_yahoo_login(ylad, status);
 

	
 
	while(poll_loop) {
 
		FD_ZERO(&inp);
 
		FD_ZERO(&outp);
 
#ifndef _WIN32
 
		FD_SET(fd_stdin, &inp);
 
		tv.tv_sec=1;
 
		tv.tv_usec=0;
 
#else
 
		tv.tv_sec=0;
 
		tv.tv_usec=1E4;
 
#endif
 
		lfd=0;
 

	
 
		for(l=connections; l; ) {
 
			struct conn_handler *c = l->data;
 
			if(c->remove) {
 
				YList *n = y_list_next(l);
 
				LOG(("Removing id:%d fd:%d", c->id, c->con->fd));
 
				connections = y_list_remove_link(connections, l);
 
				y_list_free_1(l);
 
				FREE(c);
 
				l=n;
 
			} else {
 
				if(c->cond & YAHOO_INPUT_READ)
 
					FD_SET(c->con->fd, &inp);
 
				if(c->cond & YAHOO_INPUT_WRITE)
 
					FD_SET(c->con->fd, &outp);
 
				if(lfd < c->con->fd)
 
					lfd = c->con->fd;
 
				l = y_list_next(l);
 
			}
 
		}
 

	
 
		select(lfd + 1, &inp, &outp, NULL, &tv);
 
		time(&curTime);
 

	
 
#ifndef _WIN32
 
		if(FD_ISSET(fd_stdin, &inp))	local_input_callback(0);
 
#else
 
		if (_kbhit()) local_input_callback(_getch());
 
#endif
 

	
 
		for(l = connections; l; l = y_list_next(l)) {
 
			struct conn_handler *c = l->data;
 
			if(c->con->remove) {
 
				FREE(c->con);
 
				c->con = NULL;
 
				continue;
 
			}
 
			if(c->remove)
 
				continue;
 
			if(FD_ISSET(c->con->fd, &inp))
 
				yahoo_callback(c, YAHOO_INPUT_READ);
 
			if(FD_ISSET(c->con->fd, &outp))
 
				yahoo_callback(c, YAHOO_INPUT_WRITE);
 
		}
 

	
 
		if(expired(pingTimer))		yahoo_ping_timeout_callback();
 
		if(expired(webcamTimer))	yahoo_webcam_timeout_callback(webcam_id);
 
	}
 
	LOG(("Exited loop"));
 

	
 
	while(connections) {
 
		YList *tmp = connections;
 
		struct conn_handler *c = connections->data;
 
		FREE(c);
 
		connections = y_list_remove_link(connections, connections);
 
		y_list_free_1(tmp);
 
	}
 

	
 
	yahoo_logout();
 

	
 
	FREE(ylad);
 

	
 
#ifdef _WIN32
 
	WSACleanup();
 
#endif
 
	return 0;
 
}
 

	
 
static void ext_yahoo_got_file(int id, const char *me, const char *who, const char *msg, const char *fname, 
 
	unsigned long fesize, char *trid)
 
{
 
	LOG(("Got a File transfer request (%s, %ld bytes) from %s", fname, fesize, who));
 
}
 

	
 
static void ext_yahoo_file_transfer_done(int id, int response, void *data)
 
{
 
}
 

	
 
static char *ext_yahoo_get_ip_addr(const char *domain)
 
{
 
	return NULL;
 
}
 

	
 
static void ext_yahoo_got_ft_data(int id, const unsigned char *in, int count, void *data)
 
{
 
}
 

	
 
static void ext_yahoo_got_identities(int id, YList * ids)
 
{
 
}
 

	
 
static void ext_yahoo_chat_yahoologout(int id, const char *me)
 
{ 
 
 	LOG(("got chat logout for %s", me));
 
}
 

	
 
static void ext_yahoo_chat_yahooerror(int id, const char *me)
 
{ 
 
 	LOG(("got chat error for %s", me));
 
}
 

	
 
static void ext_yahoo_got_ping(int id, const char *errormsg)
 
{ 
 
 	LOG(("got ping errormsg %s", errormsg));
 
}
 

	
 
static void ext_yahoo_got_search_result(int id, int found, int start, int total, YList *contacts)
 
{
 
	LOG(("got search result"));
 
}
 

	
 
static void ext_yahoo_got_buddyicon_checksum(int id, const char *a, const char *b, int checksum)
 
{
 
	LOG(("got buddy icon checksum"));
 
}
 

	
 
static void ext_yahoo_got_buddy_change_group(int id, const char *me, const char *who, 
 
	const char *old_group, const char *new_group)
 
{
 
}
 

	
 
static void ext_yahoo_got_buddyicon(int id, const char *a, const char *b, const char *c, int checksum)
 
{
 
	LOG(("got buddy icon"));
 
}
 

	
 
static void ext_yahoo_buddyicon_uploaded(int id, const char *url)
 
{
 
	LOG(("buddy icon uploaded"));
 
}
 

	
 
static void ext_yahoo_got_buddyicon_request(int id, const char *me, const char *who)
 
{
 
	LOG(("got buddy icon request from %s",who));
 
}
 

	
 
static void register_callbacks()
 
{
 
	static struct yahoo_callbacks yc;
 

	
 
	yc.ext_yahoo_login_response = ext_yahoo_login_response;
 
	yc.ext_yahoo_got_buddies = ext_yahoo_got_buddies;
 
	yc.ext_yahoo_got_ignore = ext_yahoo_got_ignore;
 
	yc.ext_yahoo_got_identities = ext_yahoo_got_identities;
 
	yc.ext_yahoo_got_cookies = ext_yahoo_got_cookies;
 
	yc.ext_yahoo_status_changed = ext_yahoo_status_changed;
 
	yc.ext_yahoo_got_im = ext_yahoo_got_im;
 
	yc.ext_yahoo_got_buzz = ext_yahoo_got_buzz;
 
	yc.ext_yahoo_got_conf_invite = ext_yahoo_got_conf_invite;
 
	yc.ext_yahoo_conf_userdecline = ext_yahoo_conf_userdecline;
 
	yc.ext_yahoo_conf_userjoin = ext_yahoo_conf_userjoin;
 
	yc.ext_yahoo_conf_userleave = ext_yahoo_conf_userleave;
 
	yc.ext_yahoo_conf_message = ext_yahoo_conf_message;
 
	yc.ext_yahoo_chat_cat_xml = ext_yahoo_chat_cat_xml;
 
	yc.ext_yahoo_chat_join = ext_yahoo_chat_join;
 
	yc.ext_yahoo_chat_userjoin = ext_yahoo_chat_userjoin;
 
	yc.ext_yahoo_chat_userleave = ext_yahoo_chat_userleave;
 
	yc.ext_yahoo_chat_message = ext_yahoo_chat_message;
 
	yc.ext_yahoo_chat_yahoologout = ext_yahoo_chat_yahoologout;
 
	yc.ext_yahoo_chat_yahooerror = ext_yahoo_chat_yahooerror;
 
	yc.ext_yahoo_got_webcam_image = ext_yahoo_got_webcam_image;
 
	yc.ext_yahoo_webcam_invite = ext_yahoo_webcam_invite;
 
	yc.ext_yahoo_webcam_invite_reply = ext_yahoo_webcam_invite_reply;
 
	yc.ext_yahoo_webcam_closed = ext_yahoo_webcam_closed;
 
	yc.ext_yahoo_webcam_viewer = ext_yahoo_webcam_viewer;
 
	yc.ext_yahoo_webcam_data_request = ext_yahoo_webcam_data_request;
 
	yc.ext_yahoo_got_file = ext_yahoo_got_file;
 
	yc.ext_yahoo_got_ft_data = ext_yahoo_got_ft_data;
 
	yc.ext_yahoo_get_ip_addr = ext_yahoo_get_ip_addr;
 
	yc.ext_yahoo_file_transfer_done = ext_yahoo_file_transfer_done;
 
	yc.ext_yahoo_contact_added = ext_yahoo_contact_added;
 
	yc.ext_yahoo_rejected = ext_yahoo_rejected;
 
	yc.ext_yahoo_typing_notify = ext_yahoo_typing_notify;
 
	yc.ext_yahoo_game_notify = ext_yahoo_game_notify;
 
	yc.ext_yahoo_mail_notify = ext_yahoo_mail_notify;
 
	yc.ext_yahoo_got_search_result = ext_yahoo_got_search_result;
 
	yc.ext_yahoo_system_message = ext_yahoo_system_message;
 
	yc.ext_yahoo_error = ext_yahoo_error;
 
	yc.ext_yahoo_log = ext_yahoo_log;
 
	yc.ext_yahoo_add_handler = ext_yahoo_add_handler;
 
	yc.ext_yahoo_remove_handler = ext_yahoo_remove_handler;
 
	yc.ext_yahoo_connect = ext_yahoo_connect;
 
	yc.ext_yahoo_connect_async = ext_yahoo_connect_async;
 
	yc.ext_yahoo_read = ext_yahoo_read;
 
	yc.ext_yahoo_write = ext_yahoo_write;
 
	yc.ext_yahoo_close = ext_yahoo_close;
 
	yc.ext_yahoo_got_buddyicon = ext_yahoo_got_buddyicon;
 
	yc.ext_yahoo_got_buddyicon_checksum = ext_yahoo_got_buddyicon_checksum;
 
	yc.ext_yahoo_buddyicon_uploaded = ext_yahoo_buddyicon_uploaded;
 
	yc.ext_yahoo_got_buddyicon_request = ext_yahoo_got_buddyicon_request;
 
	yc.ext_yahoo_got_ping = ext_yahoo_got_ping;
 
	yc.ext_yahoo_got_buddy_change_group = ext_yahoo_got_buddy_change_group;
 

	
 
	yahoo_register_callbacks(&yc);
 
}
 

	
backends/libyahoo2/yahoo/crypt.c
Show inline comments
 
new file 100644
 
/* One way encryption based on MD5 sum.
 
   Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
 
   This file is part of the GNU C Library.
 
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 

	
 
   The GNU C Library is free software; you can redistribute it and/or
 
   modify it under the terms of the GNU Lesser General Public
 
   License as published by the Free Software Foundation; either
 
   version 2.1 of the License, or (at your option) any later version.
 

	
 
   The GNU C Library 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
 
   Lesser General Public License for more details.
 

	
 
   You should have received a copy of the GNU Lesser General Public
 
   License along with the GNU C Library; if not, write to the Free
 
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
   02111-1307 USA.  */
 

	
 
/* warmenhoven took this file and made it work with the md5.[ch] we
 
 * already had. isn't that lovely. people should just use linux or
 
 * freebsd, crypt works properly on those systems. i hate solaris */
 

	
 
#if HAVE_CONFIG_H
 
#  include <config.h>
 
#endif
 

	
 
#if HAVE_STRING_H
 
#  include <string.h>
 
#elif HAVE_STRINGS_H
 
#  include <strings.h>
 
#endif
 

	
 
#include <stdlib.h>
 
#include "yahoo_util.h"
 

	
 
#include "md5.h"
 

	
 
/* Define our magic string to mark salt for MD5 "encryption"
 
   replacement.  This is meant to be the same as for other MD5 based
 
   encryption implementations.  */
 
static const char md5_salt_prefix[] = "$1$";
 

	
 
/* Table with characters for base64 transformation.  */
 
static const char b64t[64] =
 
	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 

	
 
char *yahoo_crypt(char *key, char *salt)
 
{
 
	char *buffer = NULL;
 
	int buflen = 0;
 
	int needed = 3 + strlen(salt) + 1 + 26 + 1;
 

	
 
	md5_byte_t alt_result[16];
 
	md5_state_t ctx;
 
	md5_state_t alt_ctx;
 
	size_t salt_len;
 
	size_t key_len;
 
	size_t cnt;
 
	char *cp;
 

	
 
	if (buflen < needed) {
 
		buflen = needed;
 
		if ((buffer = realloc(buffer, buflen)) == NULL)
 
			return NULL;
 
	}
 

	
 
	/* Find beginning of salt string.  The prefix should normally always
 
	   be present.  Just in case it is not.  */
 
	if (strncmp(md5_salt_prefix, salt, sizeof(md5_salt_prefix) - 1) == 0)
 
		/* Skip salt prefix.  */
 
		salt += sizeof(md5_salt_prefix) - 1;
 

	
 
	salt_len = MIN(strcspn(salt, "$"), 8);
 
	key_len = strlen(key);
 

	
 
	/* Prepare for the real work.  */
 
	md5_init(&ctx);
 

	
 
	/* Add the key string.  */
 
	md5_append(&ctx, (md5_byte_t *)key, key_len);
 

	
 
	/* Because the SALT argument need not always have the salt prefix we
 
	   add it separately.  */
 
	md5_append(&ctx, (md5_byte_t *)md5_salt_prefix,
 
		sizeof(md5_salt_prefix) - 1);
 

	
 
	/* The last part is the salt string.  This must be at most 8
 
	   characters and it ends at the first `$' character (for
 
	   compatibility which existing solutions).  */
 
	md5_append(&ctx, (md5_byte_t *)salt, salt_len);
 

	
 
	/* Compute alternate MD5 sum with input KEY, SALT, and KEY.  The
 
	   final result will be added to the first context.  */
 
	md5_init(&alt_ctx);
 

	
 
	/* Add key.  */
 
	md5_append(&alt_ctx, (md5_byte_t *)key, key_len);
 

	
 
	/* Add salt.  */
 
	md5_append(&alt_ctx, (md5_byte_t *)salt, salt_len);
 

	
 
	/* Add key again.  */
 
	md5_append(&alt_ctx, (md5_byte_t *)key, key_len);
 

	
 
	/* Now get result of this (16 bytes) and add it to the other
 
	   context.  */
 
	md5_finish(&alt_ctx, alt_result);
 

	
 
	/* Add for any character in the key one byte of the alternate sum.  */
 
	for (cnt = key_len; cnt > 16; cnt -= 16)
 
		md5_append(&ctx, alt_result, 16);
 
	md5_append(&ctx, alt_result, cnt);
 

	
 
	/* For the following code we need a NUL byte.  */
 
	alt_result[0] = '\0';
 

	
 
	/* The original implementation now does something weird: for every 1
 
	   bit in the key the first 0 is added to the buffer, for every 0
 
	   bit the first character of the key.  This does not seem to be
 
	   what was intended but we have to follow this to be compatible.  */
 
	for (cnt = key_len; cnt > 0; cnt >>= 1)
 
		md5_append(&ctx,
 
			(cnt & 1) != 0 ? alt_result : (md5_byte_t *)key, 1);
 

	
 
	/* Create intermediate result.  */
 
	md5_finish(&ctx, alt_result);
 

	
 
	/* Now comes another weirdness.  In fear of password crackers here
 
	   comes a quite long loop which just processes the output of the
 
	   previous round again.  We cannot ignore this here.  */
 
	for (cnt = 0; cnt < 1000; ++cnt) {
 
		/* New context.  */
 
		md5_init(&ctx);
 

	
 
		/* Add key or last result.  */
 
		if ((cnt & 1) != 0)
 
			md5_append(&ctx, (md5_byte_t *)key, key_len);
 
		else
 
			md5_append(&ctx, alt_result, 16);
 

	
 
		/* Add salt for numbers not divisible by 3.  */
 
		if (cnt % 3 != 0)
 
			md5_append(&ctx, (md5_byte_t *)salt, salt_len);
 

	
 
		/* Add key for numbers not divisible by 7.  */
 
		if (cnt % 7 != 0)
 
			md5_append(&ctx, (md5_byte_t *)key, key_len);
 

	
 
		/* Add key or last result.  */
 
		if ((cnt & 1) != 0)
 
			md5_append(&ctx, alt_result, 16);
 
		else
 
			md5_append(&ctx, (md5_byte_t *)key, key_len);
 

	
 
		/* Create intermediate result.  */
 
		md5_finish(&ctx, alt_result);
 
	}
 

	
 
	/* Now we can construct the result string.  It consists of three
 
	   parts.  */
 

	
 
	strncpy(buffer, md5_salt_prefix, MAX(0, buflen));
 
	cp = buffer + strlen(buffer);
 
	buflen -= sizeof(md5_salt_prefix);
 

	
 
	strncpy(cp, salt, MIN((size_t) buflen, salt_len));
 
	cp = cp + strlen(cp);
 
	buflen -= MIN((size_t) buflen, salt_len);
 

	
 
	if (buflen > 0) {
 
		*cp++ = '$';
 
		--buflen;
 
	}
 
#define b64_from_24bit(B2, B1, B0, N) \
 
	do { \
 
		unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
 
		int n = (N); \
 
		while (n-- > 0 && buflen > 0) { \
 
			*cp++ = b64t[w & 0x3f]; \
 
			--buflen; \
 
			w >>= 6; \
 
		}\
 
	} while (0)
 

	
 
	b64_from_24bit(alt_result[0], alt_result[6], alt_result[12], 4);
 
	b64_from_24bit(alt_result[1], alt_result[7], alt_result[13], 4);
 
	b64_from_24bit(alt_result[2], alt_result[8], alt_result[14], 4);
 
	b64_from_24bit(alt_result[3], alt_result[9], alt_result[15], 4);
 
	b64_from_24bit(alt_result[4], alt_result[10], alt_result[5], 4);
 
	b64_from_24bit(0, 0, alt_result[11], 2);
 
	if (buflen <= 0) {
 
		FREE(buffer);
 
	} else
 
		*cp = '\0';	/* Terminate the string.  */
 

	
 
	/* Clear the buffer for the intermediate result so that people
 
	   attaching to processes or reading core dumps cannot get any
 
	   information.  We do it in this way to clear correct_words[]
 
	   inside the MD5 implementation as well.  */
 
	md5_init(&ctx);
 
	md5_finish(&ctx, alt_result);
 
	memset(&ctx, '\0', sizeof(ctx));
 
	memset(&alt_ctx, '\0', sizeof(alt_ctx));
 

	
 
	return buffer;
 
}
backends/libyahoo2/yahoo/libyahoo2.c
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: libyahoo2.c
 
 *
 
 * Some code copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 * YMSG16 code copyright (C) 2009, 
 
 * 		Siddhesh Poyarekar <siddhesh dot poyarekar at gmail dot com>
 
 *
 
 * Yahoo Search copyright (C) 2003, Konstantin Klyagin <konst AT konst.org.ua>
 
 *
 
 * Much of this code was taken and adapted from the yahoo module for
 
 * gaim released under the GNU GPL.  This code is also released under the 
 
 * GNU GPL.
 
 *
 
 * This code is derivitive of Gaim <http://gaim.sourceforge.net>
 
 * copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
 
 *	       1998-1999, Adam Fritzler <afritz@marko.net>
 
 *	       1998-2002, Rob Flynn <rob@marko.net>
 
 *	       2000-2002, Eric Warmenhoven <eric@warmenhoven.org>
 
 *	       2001-2002, Brian Macke <macke@strangelove.net>
 
 *		    2001, Anand Biligiri S <abiligiri@users.sf.net>
 
 *		    2001, Valdis Kletnieks
 
 *		    2002, Sean Egan <bj91704@binghamton.edu>
 
 *		    2002, Toby Gray <toby.gray@ntlworld.com>
 
 *
 
 * This library also uses code from other libraries, namely:
 
 *     Portions from libfaim copyright 1998, 1999 Adam Fritzler
 
 *     <afritz@auk.cx>
 
 *     Portions of Sylpheed copyright 2000-2002 Hiroyuki Yamamoto
 
 *     <hiro-y@kcn.ne.jp>
 
 *
 
 * YMSG16 authentication code based mostly on write-up at:
 
 * 	http://www.carbonize.co.uk/ymsg16.html
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#if HAVE_CONFIG_H
 
# include <config.h>
 
#endif
 

	
 
#if HAVE_UNISTD_H
 
#include <unistd.h>
 
#endif
 
#include <errno.h>
 
#include <stdio.h>
 
#include <stdarg.h>
 

	
 
#if STDC_HEADERS
 
# include <string.h>
 
#else
 
# if !HAVE_STRCHR
 
#  define strchr index
 
#  define strrchr rindex
 
# endif
 
char *strchr(), *strrchr();
 
# if !HAVE_MEMCPY
 
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
 
#  define memmove(d, s, n) bcopy ((s), (d), (n))
 
# endif
 
#endif
 

	
 
#include <sys/types.h>
 

	
 
#include <stdlib.h>
 
#include <ctype.h>
 

	
 
#include "sha1.h"
 
#include "md5.h"
 
#include "yahoo2.h"
 
#include "yahoo_httplib.h"
 
#include "yahoo_util.h"
 
#include "yahoo_fn.h"
 

	
 
#include "yahoo2_callbacks.h"
 
#include "yahoo_debug.h"
 
#ifdef __MINGW32__
 
#define snprintf _snprintf
 
#define vsnprintf _vsnprintf
 
#endif
 

	
 
struct yahoo_callbacks *yc = NULL;
 

	
 
void yahoo_register_callbacks(struct yahoo_callbacks *tyc)
 
{
 
	yc = tyc;
 
}
 

	
 
#define YAHOO_CALLBACK(x)	yc->x
 

	
 
static int yahoo_send_data(void *fd, void *data, int len);
 
static void _yahoo_http_connected(int id, void *fd, int error, void *data);
 
static void yahoo_connected(void *fd, int error, void *data);
 

	
 
int yahoo_log_message(char *fmt, ...)
 
{
 
	char out[1024];
 
	va_list ap;
 
	va_start(ap, fmt);
 
	vsnprintf(out, sizeof(out), fmt, ap);
 
	va_end(ap);
 
	return YAHOO_CALLBACK(ext_yahoo_log) ("%s", out);
 
}
 

	
 
int yahoo_connect(char *host, int port)
 
{
 
	return YAHOO_CALLBACK(ext_yahoo_connect) (host, port);
 
}
 

	
 
static enum yahoo_log_level log_level = YAHOO_LOG_NONE;
 

	
 
enum yahoo_log_level yahoo_get_log_level()
 
{
 
	return log_level;
 
}
 

	
 
int yahoo_set_log_level(enum yahoo_log_level level)
 
{
 
	enum yahoo_log_level l = log_level;
 
	log_level = level;
 
	return l;
 
}
 

	
 
/* default values for servers */
 
static char *default_pager_hosts[] = {	"scs.msg.yahoo.com",
 
					"scsa.msg.yahoo.com",
 
					"scsb.msg.yahoo.com",
 
					"scsc.msg.yahoo.com",
 
					NULL};
 

	
 
static int pager_port = 5050;
 
static int fallback_ports[] = { 23, 25, 80, 20, 119, 8001, 8002, 5050, 0 };
 

	
 
static char filetransfer_host[] = "filetransfer.msg.yahoo.com";
 
static int filetransfer_port = 80;
 
static char webcam_host[] = "webcam.yahoo.com";
 
static int webcam_port = 5100;
 
static char webcam_description[] = "";
 
static char local_host[] = "";
 
static int conn_type = Y_WCM_DSL;
 

	
 
static char profile_url[] = "http://profiles.yahoo.com/";
 

	
 
struct connect_callback_data {
 
	struct yahoo_data *yd;
 
	int tag;
 
	int i;
 
	int server_i;
 
};
 

	
 
struct yahoo_pair {
 
	int key;
 
	char *value;
 
};
 

	
 
struct yahoo_packet {
 
	unsigned short int service;
 
	unsigned int status;
 
	unsigned int id;
 
	YList *hash;
 
};
 

	
 
struct yahoo_search_state {
 
	int lsearch_type;
 
	char *lsearch_text;
 
	int lsearch_gender;
 
	int lsearch_agerange;
 
	int lsearch_photo;
 
	int lsearch_yahoo_only;
 
	int lsearch_nstart;
 
	int lsearch_nfound;
 
	int lsearch_ntotal;
 
};
 

	
 
struct data_queue {
 
	unsigned char *queue;
 
	int len;
 
};
 

	
 
struct yahoo_input_data {
 
	struct yahoo_data *yd;
 
	struct yahoo_webcam *wcm;
 
	struct yahoo_webcam_data *wcd;
 
	struct yahoo_search_state *ys;
 

	
 
	void *fd;
 
	enum yahoo_connection_type type;
 

	
 
	unsigned char *rxqueue;
 
	int rxlen;
 
	int read_tag;
 

	
 
	YList *txqueues;
 
	int write_tag;
 
};
 

	
 
struct yahoo_server_settings {
 
	char *pager_host;
 
	int pager_port;
 
	char *filetransfer_host;
 
	int filetransfer_port;
 
	char *webcam_host;
 
	int webcam_port;
 
	char *webcam_description;
 
	char *local_host;
 
	int conn_type;
 
	char **pager_host_list;
 
};
 

	
 
static void yahoo_process_ft_connection(struct yahoo_input_data *yid, int over);
 

	
 
static void yahoo_process_filetransfer(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt);
 
static void yahoo_process_filetransferinfo(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt);
 
static void yahoo_process_filetransferaccept(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt);
 

	
 
static void *_yahoo_default_server_settings()
 
{
 
	struct yahoo_server_settings *yss =
 
		y_new0(struct yahoo_server_settings, 1);
 

	
 
	/* Give preference to the default host list
 
	 * Make sure that only one of the two is set at any time 
 
	 */
 
	yss->pager_host = NULL;
 
	yss->pager_host_list = default_pager_hosts;
 

	
 
	yss->pager_port = pager_port;
 
	yss->filetransfer_host = strdup(filetransfer_host);
 
	yss->filetransfer_port = filetransfer_port;
 
	yss->webcam_host = strdup(webcam_host);
 
	yss->webcam_port = webcam_port;
 
	yss->webcam_description = strdup(webcam_description);
 
	yss->local_host = strdup(local_host);
 
	yss->conn_type = conn_type;
 

	
 
	return yss;
 
}
 

	
 
static void *_yahoo_assign_server_settings(va_list ap)
 
{
 
	struct yahoo_server_settings *yss = _yahoo_default_server_settings();
 
	char *key;
 
	char *svalue;
 
	int nvalue;
 
	char **pvalue;
 

	
 
	while (1) {
 
		key = va_arg(ap, char *);
 
		if (key == NULL)
 
			break;
 

	
 
		if (!strcmp(key, "pager_host")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->pager_host);
 
			yss->pager_host = strdup(svalue);
 
			yss->pager_host_list = NULL;
 
		} else if (!strcmp(key, "pager_host_list")) {
 
			pvalue = va_arg(ap, char **);
 
			yss->pager_host_list = pvalue;
 
			free(yss->pager_host);
 
			yss->pager_host = NULL;
 
		} else if (!strcmp(key, "pager_port")) {
 
			nvalue = va_arg(ap, int);
 
			yss->pager_port = nvalue;
 
		} else if (!strcmp(key, "filetransfer_host")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->filetransfer_host);
 
			yss->filetransfer_host = strdup(svalue);
 
		} else if (!strcmp(key, "filetransfer_port")) {
 
			nvalue = va_arg(ap, int);
 
			yss->filetransfer_port = nvalue;
 
		} else if (!strcmp(key, "webcam_host")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->webcam_host);
 
			yss->webcam_host = strdup(svalue);
 
		} else if (!strcmp(key, "webcam_port")) {
 
			nvalue = va_arg(ap, int);
 
			yss->webcam_port = nvalue;
 
		} else if (!strcmp(key, "webcam_description")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->webcam_description);
 
			yss->webcam_description = strdup(svalue);
 
		} else if (!strcmp(key, "local_host")) {
 
			svalue = va_arg(ap, char *);
 
			free(yss->local_host);
 
			yss->local_host = strdup(svalue);
 
		} else if (!strcmp(key, "conn_type")) {
 
			nvalue = va_arg(ap, int);
 
			yss->conn_type = nvalue;
 
		} else {
 
			WARNING(("Unknown key passed to yahoo_init, "
 
					"perhaps you didn't terminate the list "
 
					"with NULL"));
 
		}
 
	}
 

	
 
	return yss;
 
}
 

	
 
static void yahoo_free_server_settings(struct yahoo_server_settings *yss)
 
{
 
	if (!yss)
 
		return;
 

	
 
	free(yss->pager_host);
 
	free(yss->filetransfer_host);
 
	free(yss->webcam_host);
 
	free(yss->webcam_description);
 
	free(yss->local_host);
 

	
 
	free(yss);
 
}
 

	
 
static YList *conns = NULL;
 
static YList *inputs = NULL;
 
static int last_id = 0;
 

	
 
static void add_to_list(struct yahoo_data *yd)
 
{
 
	conns = y_list_prepend(conns, yd);
 
}
 

	
 
static struct yahoo_data *find_conn_by_id(int id)
 
{
 
	YList *l;
 
	for (l = conns; l; l = y_list_next(l)) {
 
		struct yahoo_data *yd = l->data;
 
		if (yd->client_id == id)
 
			return yd;
 
	}
 
	return NULL;
 
}
 

	
 
static void del_from_list(struct yahoo_data *yd)
 
{
 
	conns = y_list_remove(conns, yd);
 
}
 

	
 
/* call repeatedly to get the next one */
 
/*
 
static struct yahoo_input_data * find_input_by_id(int id)
 
{
 
	YList *l;
 
	for(l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if(yid->yd->client_id == id)
 
			return yid;
 
	}
 
	return NULL;
 
}
 
*/
 

	
 
static struct yahoo_input_data *find_input_by_id_and_webcam_user(int id,
 
	const char *who)
 
{
 
	YList *l;
 
	LOG(("find_input_by_id_and_webcam_user"));
 
	for (l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if (yid->type == YAHOO_CONNECTION_WEBCAM
 
			&& yid->yd->client_id == id && yid->wcm && ((who
 
					&& yid->wcm->user
 
					&& !strcmp(who, yid->wcm->user))
 
				|| !(yid->wcm->user && !who)))
 
			return yid;
 
	}
 
	return NULL;
 
}
 

	
 
static struct yahoo_input_data *find_input_by_id_and_type(int id,
 
	enum yahoo_connection_type type)
 
{
 
	YList *l;
 
	LOG(("find_input_by_id_and_type"));
 
	for (l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if (yid->type == type && yid->yd->client_id == id)
 
			return yid;
 
	}
 
	return NULL;
 
}
 

	
 
static struct yahoo_input_data *find_input_by_id_and_fd(int id, void *fd)
 
{
 
	YList *l;
 
	LOG(("find_input_by_id_and_fd"));
 
	for (l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if (yid->fd == fd && yid->yd->client_id == id)
 
			return yid;
 
	}
 
	return NULL;
 
}
 

	
 
static int count_inputs_with_id(int id)
 
{
 
	int c = 0;
 
	YList *l;
 
	LOG(("counting %d", id));
 
	for (l = inputs; l; l = y_list_next(l)) {
 
		struct yahoo_input_data *yid = l->data;
 
		if (yid->yd->client_id == id)
 
			c++;
 
	}
 
	LOG(("%d", c));
 
	return c;
 
}
 

	
 
extern char *yahoo_crypt(char *, char *);
 

	
 
/* Free a buddy list */
 
static void yahoo_free_buddies(YList *list)
 
{
 
	YList *l;
 

	
 
	for (l = list; l; l = l->next) {
 
		struct yahoo_buddy *bud = l->data;
 
		if (!bud)
 
			continue;
 

	
 
		FREE(bud->group);
 
		FREE(bud->id);
 
		FREE(bud->real_name);
 
		if (bud->yab_entry) {
 
			FREE(bud->yab_entry->fname);
 
			FREE(bud->yab_entry->lname);
 
			FREE(bud->yab_entry->nname);
 
			FREE(bud->yab_entry->id);
 
			FREE(bud->yab_entry->email);
 
			FREE(bud->yab_entry->hphone);
 
			FREE(bud->yab_entry->wphone);
 
			FREE(bud->yab_entry->mphone);
 
			FREE(bud->yab_entry);
 
		}
 
		FREE(bud);
 
		l->data = bud = NULL;
 
	}
 

	
 
	y_list_free(list);
 
}
 

	
 
/* Free an identities list */
 
static void yahoo_free_identities(YList *list)
 
{
 
	while (list) {
 
		YList *n = list;
 
		FREE(list->data);
 
		list = y_list_remove_link(list, list);
 
		y_list_free_1(n);
 
	}
 
}
 

	
 
/* Free webcam data */
 
static void yahoo_free_webcam(struct yahoo_webcam *wcm)
 
{
 
	if (wcm) {
 
		FREE(wcm->user);
 
		FREE(wcm->server);
 
		FREE(wcm->key);
 
		FREE(wcm->description);
 
		FREE(wcm->my_ip);
 
	}
 
	FREE(wcm);
 
}
 

	
 
static void yahoo_free_data(struct yahoo_data *yd)
 
{
 
	FREE(yd->user);
 
	FREE(yd->password);
 
	FREE(yd->cookie_y);
 
	FREE(yd->cookie_t);
 
	FREE(yd->cookie_b);
 
	FREE(yd->cookie_c);
 
	FREE(yd->login_cookie);
 
	FREE(yd->login_id);
 

	
 
	yahoo_free_buddies(yd->buddies);
 
	yahoo_free_buddies(yd->ignore);
 
	yahoo_free_identities(yd->identities);
 

	
 
	yahoo_free_server_settings(yd->server_settings);
 

	
 
	FREE(yd);
 
}
 

	
 
#define YAHOO_PACKET_HDRLEN (4 + 2 + 2 + 2 + 2 + 4 + 4)
 

	
 
static struct yahoo_packet *yahoo_packet_new(enum yahoo_service service,
 
	enum ypacket_status status, int id)
 
{
 
	struct yahoo_packet *pkt = y_new0(struct yahoo_packet, 1);
 

	
 
	pkt->service = service;
 
	pkt->status = status;
 
	pkt->id = id;
 

	
 
	return pkt;
 
}
 

	
 
static void yahoo_packet_hash(struct yahoo_packet *pkt, int key,
 
	const char *value)
 
{
 
	struct yahoo_pair *pair = y_new0(struct yahoo_pair, 1);
 
	pair->key = key;
 
	pair->value = strdup(value);
 
	pkt->hash = y_list_append(pkt->hash, pair);
 
}
 

	
 
static int yahoo_packet_length(struct yahoo_packet *pkt)
 
{
 
	YList *l;
 

	
 
	int len = 0;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		int tmp = pair->key;
 
		do {
 
			tmp /= 10;
 
			len++;
 
		} while (tmp);
 
		len += 2;
 
		len += strlen(pair->value);
 
		len += 2;
 
	}
 

	
 
	return len;
 
}
 

	
 
#define yahoo_put16(buf, data) ( \
 
		(*(buf) = (unsigned char)((data)>>8)&0xff), \
 
		(*((buf)+1) = (unsigned char)(data)&0xff),  \
 
		2)
 
#define yahoo_get16(buf) ((((*(buf))&0xff)<<8) + ((*((buf)+1)) & 0xff))
 
#define yahoo_put32(buf, data) ( \
 
		(*((buf)) = (unsigned char)((data)>>24)&0xff), \
 
		(*((buf)+1) = (unsigned char)((data)>>16)&0xff), \
 
		(*((buf)+2) = (unsigned char)((data)>>8)&0xff), \
 
		(*((buf)+3) = (unsigned char)(data)&0xff), \
 
		4)
 
#define yahoo_get32(buf) ((((*(buf)   )&0xff)<<24) + \
 
			 (((*((buf)+1))&0xff)<<16) + \
 
			 (((*((buf)+2))&0xff)<< 8) + \
 
			 (((*((buf)+3))&0xff)))
 

	
 
static void yahoo_packet_read(struct yahoo_packet *pkt, unsigned char *data,
 
	int len)
 
{
 
	int pos = 0;
 

	
 
	while (pos + 1 < len) {
 
		char *key, *value = NULL;
 
		int accept;
 
		int x;
 

	
 
		struct yahoo_pair *pair = y_new0(struct yahoo_pair, 1);
 

	
 
		key = malloc(len + 1);
 
		x = 0;
 
		while (pos + 1 < len) {
 
			if (data[pos] == 0xc0 && data[pos + 1] == 0x80)
 
				break;
 
			key[x++] = data[pos++];
 
		}
 
		key[x] = 0;
 
		pos += 2;
 
		pair->key = strtol(key, NULL, 10);
 
		free(key);
 

	
 
		accept = x;
 

	
 
		if (pos + 1 > len) {
 
			/* Malformed packet! (Truncated--garbage or something) */
 
			accept = 0;
 
		}
 

	
 
		/* if x is 0 there was no key, so don't accept it */
 
		if (accept)
 
			value = malloc(len - pos + 1);
 
		x = 0;
 
		while (pos + 1 < len) {
 
			if (data[pos] == 0xc0 && data[pos + 1] == 0x80)
 
				break;
 
			if (accept)
 
				value[x++] = data[pos++];
 
		}
 
		if (accept)
 
			value[x] = 0;
 
		pos += 2;
 
		if (accept) {
 
			pair->value = strdup(value);
 
			FREE(value);
 
			pkt->hash = y_list_append(pkt->hash, pair);
 
			DEBUG_MSG(("Key: %d  \tValue: %s", pair->key,
 
					pair->value));
 
		} else {
 
			FREE(pair);
 
		}
 
	}
 
}
 

	
 
static void yahoo_packet_write(struct yahoo_packet *pkt, unsigned char *data)
 
{
 
	YList *l;
 
	int pos = 0;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		unsigned char buf[100];
 

	
 
		snprintf((char *)buf, sizeof(buf), "%d", pair->key);
 
		strcpy((char *)data + pos, (char *)buf);
 
		pos += strlen((char *)buf);
 
		data[pos++] = 0xc0;
 
		data[pos++] = 0x80;
 

	
 
		strcpy((char *)data + pos, pair->value);
 
		pos += strlen(pair->value);
 
		data[pos++] = 0xc0;
 
		data[pos++] = 0x80;
 
	}
 
}
 

	
 
static void yahoo_dump_unhandled(struct yahoo_packet *pkt)
 
{
 
	YList *l;
 

	
 
	NOTICE(("Service: 0x%02x\tStatus: %d", pkt->service, pkt->status));
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		NOTICE(("\t%d => %s", pair->key, pair->value));
 
	}
 
}
 

	
 
static void yahoo_packet_dump(unsigned char *data, int len)
 
{
 
	if (yahoo_get_log_level() >= YAHOO_LOG_DEBUG) {
 
		int i;
 
		for (i = 0; i < len; i++) {
 
			if ((i % 8 == 0) && i)
 
				YAHOO_CALLBACK(ext_yahoo_log) (" ");
 
			if ((i % 16 == 0) && i)
 
				YAHOO_CALLBACK(ext_yahoo_log) ("\n");
 
			YAHOO_CALLBACK(ext_yahoo_log) ("%02x ", data[i]);
 
		}
 
		YAHOO_CALLBACK(ext_yahoo_log) ("\n");
 
		for (i = 0; i < len; i++) {
 
			if ((i % 8 == 0) && i)
 
				YAHOO_CALLBACK(ext_yahoo_log) (" ");
 
			if ((i % 16 == 0) && i)
 
				YAHOO_CALLBACK(ext_yahoo_log) ("\n");
 
			if (isprint(data[i]))
 
				YAHOO_CALLBACK(ext_yahoo_log) (" %c ", data[i]);
 
			else
 
				YAHOO_CALLBACK(ext_yahoo_log) (" . ");
 
		}
 
		YAHOO_CALLBACK(ext_yahoo_log) ("\n");
 
	}
 
}
 

	
 
static const char base64digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
	"abcdefghijklmnopqrstuvwxyz" "0123456789._";
 
static void to_y64(unsigned char *out, const unsigned char *in, int inlen)
 
/* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
 
{
 
	for (; inlen >= 3; inlen -= 3) {
 
		*out++ = base64digits[in[0] >> 2];
 
		*out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
 
		*out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
 
		*out++ = base64digits[in[2] & 0x3f];
 
		in += 3;
 
	}
 
	if (inlen > 0) {
 
		unsigned char fragment;
 

	
 
		*out++ = base64digits[in[0] >> 2];
 
		fragment = (in[0] << 4) & 0x30;
 
		if (inlen > 1)
 
			fragment |= in[1] >> 4;
 
		*out++ = base64digits[fragment];
 
		*out++ = (inlen < 2) ? '-' : base64digits[(in[1] << 2) & 0x3c];
 
		*out++ = '-';
 
	}
 
	*out = '\0';
 
}
 

	
 
static void yahoo_add_to_send_queue(struct yahoo_input_data *yid, void *data,
 
	int length)
 
{
 
	struct data_queue *tx = y_new0(struct data_queue, 1);
 
	tx->queue = y_new0(unsigned char, length);
 
	tx->len = length;
 
	memcpy(tx->queue, data, length);
 

	
 
	yid->txqueues = y_list_append(yid->txqueues, tx);
 

	
 
	if (!yid->write_tag)
 
		yid->write_tag =
 
			YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->
 
			client_id, yid->fd, YAHOO_INPUT_WRITE, yid);
 
}
 

	
 
static void yahoo_send_packet(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt, int extra_pad)
 
{
 
	int pktlen = yahoo_packet_length(pkt);
 
	int len = YAHOO_PACKET_HDRLEN + pktlen;
 
	unsigned char *data;
 
	int pos = 0;
 

	
 
	if (!yid->fd)
 
		return;
 

	
 
	data = y_new0(unsigned char, len + 1);
 

	
 
	memcpy(data + pos, "YMSG", 4);
 
	pos += 4;
 
	pos += yahoo_put16(data + pos, YAHOO_PROTO_VER);	/* version [latest 12 0x000c] */
 
	pos += yahoo_put16(data + pos, 0x0000);	/* HIWORD pkt length??? */
 
	pos += yahoo_put16(data + pos, pktlen + extra_pad);	/* LOWORD pkt length? */
 
	pos += yahoo_put16(data + pos, pkt->service);	/* service */
 
	pos += yahoo_put32(data + pos, pkt->status);	/* status [4bytes] */
 
	pos += yahoo_put32(data + pos, pkt->id);	/* session [4bytes] */
 

	
 
	yahoo_packet_write(pkt, data + pos);
 

	
 
	yahoo_packet_dump(data, len);
 

	
 
	if (yid->type == YAHOO_CONNECTION_FT)
 
		yahoo_send_data(yid->fd, data, len);
 
	else
 
		yahoo_add_to_send_queue(yid, data, len);
 
	FREE(data);
 
}
 

	
 
static void yahoo_packet_free(struct yahoo_packet *pkt)
 
{
 
	while (pkt->hash) {
 
		struct yahoo_pair *pair = pkt->hash->data;
 
		YList *tmp;
 
		FREE(pair->value);
 
		FREE(pair);
 
		tmp = pkt->hash;
 
		pkt->hash = y_list_remove_link(pkt->hash, pkt->hash);
 
		y_list_free_1(tmp);
 
	}
 
	FREE(pkt);
 
}
 

	
 
static int yahoo_send_data(void *fd, void *data, int len)
 
{
 
	int ret;
 
	int e;
 

	
 
	if (fd == NULL)
 
		return -1;
 

	
 
	yahoo_packet_dump(data, len);
 

	
 
	do {
 
		ret = YAHOO_CALLBACK(ext_yahoo_write) (fd, data, len);
 
	} while (ret == -1 && errno == EINTR);
 
	e = errno;
 

	
 
	if (ret == -1) {
 
		LOG(("wrote data: ERR %s", strerror(errno)));
 
	} else {
 
		LOG(("wrote data: OK"));
 
	}
 

	
 
	errno = e;
 
	return ret;
 
}
 

	
 
void yahoo_close(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return;
 

	
 
	del_from_list(yd);
 

	
 
	yahoo_free_data(yd);
 
	if (id == last_id)
 
		last_id--;
 
}
 

	
 
static void yahoo_input_close(struct yahoo_input_data *yid)
 
{
 
	inputs = y_list_remove(inputs, yid);
 

	
 
	LOG(("yahoo_input_close(read)"));
 
	YAHOO_CALLBACK(ext_yahoo_remove_handler) (yid->yd->client_id,
 
		yid->read_tag);
 
	LOG(("yahoo_input_close(write)"));
 
	YAHOO_CALLBACK(ext_yahoo_remove_handler) (yid->yd->client_id,
 
		yid->write_tag);
 
	yid->read_tag = yid->write_tag = 0;
 
	if (yid->fd)
 
		YAHOO_CALLBACK(ext_yahoo_close) (yid->fd);
 
	yid->fd = 0;
 
	FREE(yid->rxqueue);
 
	if (count_inputs_with_id(yid->yd->client_id) == 0) {
 
		LOG(("closing %d", yid->yd->client_id));
 
		yahoo_close(yid->yd->client_id);
 
	}
 
	yahoo_free_webcam(yid->wcm);
 
	if (yid->wcd)
 
		FREE(yid->wcd);
 
	if (yid->ys) {
 
		FREE(yid->ys->lsearch_text);
 
		FREE(yid->ys);
 
	}
 
	FREE(yid);
 
}
 

	
 
static int is_same_bud(const void *a, const void *b)
 
{
 
	const struct yahoo_buddy *subject = a;
 
	const struct yahoo_buddy *object = b;
 

	
 
	return strcmp(subject->id, object->id);
 
}
 

	
 
static char *getcookie(char *rawcookie)
 
{
 
	char *cookie = NULL;
 
	char *tmpcookie;
 
	char *cookieend;
 

	
 
	if (strlen(rawcookie) < 2)
 
		return NULL;
 

	
 
	tmpcookie = strdup(rawcookie + 2);
 
	cookieend = strchr(tmpcookie, ';');
 

	
 
	if (cookieend)
 
		*cookieend = '\0';
 

	
 
	cookie = strdup(tmpcookie);
 
	FREE(tmpcookie);
 
	/* cookieend=NULL;  not sure why this was there since the value is not preserved in the stack -dd */
 

	
 
	return cookie;
 
}
 

	
 
static char *getlcookie(char *cookie)
 
{
 
	char *tmp;
 
	char *tmpend;
 
	char *login_cookie = NULL;
 

	
 
	tmpend = strstr(cookie, "n=");
 
	if (tmpend) {
 
		tmp = strdup(tmpend + 2);
 
		tmpend = strchr(tmp, '&');
 
		if (tmpend)
 
			*tmpend = '\0';
 
		login_cookie = strdup(tmp);
 
		FREE(tmp);
 
	}
 

	
 
	return login_cookie;
 
}
 

	
 
static void yahoo_process_notify(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *msg = NULL;
 
	char *from = NULL;
 
	char *to = NULL;
 
	int stat = 0;
 
	int accept = 0;
 
	char *ind = NULL;
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 4)
 
			from = pair->value;
 
		if (pair->key == 5)
 
			to = pair->value;
 
		if (pair->key == 49)
 
			msg = pair->value;
 
		if (pair->key == 13)
 
			stat = atoi(pair->value);
 
		if (pair->key == 14)
 
			ind = pair->value;
 
		if (pair->key == 16) {	/* status == -1 */
 
			NOTICE((pair->value));
 
			return;
 
		}
 

	
 
	}
 

	
 
	if (!msg)
 
		return;
 

	
 
	if (!strncasecmp(msg, "TYPING", strlen("TYPING")))
 
		YAHOO_CALLBACK(ext_yahoo_typing_notify) (yd->client_id, to,
 
			from, stat);
 
	else if (!strncasecmp(msg, "GAME", strlen("GAME")))
 
		YAHOO_CALLBACK(ext_yahoo_game_notify) (yd->client_id, to, from,
 
			stat, ind);
 
	else if (!strncasecmp(msg, "WEBCAMINVITE", strlen("WEBCAMINVITE"))) {
 
		if (!strcmp(ind, " ")) {
 
			YAHOO_CALLBACK(ext_yahoo_webcam_invite) (yd->client_id,
 
				to, from);
 
		} else {
 
			accept = atoi(ind);
 
			/* accept the invitation (-1 = deny 1 = accept) */
 
			if (accept < 0)
 
				accept = 0;
 
			YAHOO_CALLBACK(ext_yahoo_webcam_invite_reply) (yd->
 
				client_id, to, from, accept);
 
		}
 
	} else
 
		LOG(("Got unknown notification: %s", msg));
 
}
 

	
 
static void yahoo_process_conference(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *msg = NULL;
 
	char *host = NULL;
 
	char *who = NULL;
 
	char *room = NULL;
 
	char *id = NULL;
 
	int utf8 = 0;
 
	YList *members = NULL;
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 50)
 
			host = pair->value;
 

	
 
		if (pair->key == 52) {	/* invite */
 
			members = y_list_append(members, strdup(pair->value));
 
		}
 
		if (pair->key == 53)	/* logon */
 
			who = pair->value;
 
		if (pair->key == 54)	/* decline */
 
			who = pair->value;
 
		if (pair->key == 55)	/* unavailable (status == 2) */
 
			who = pair->value;
 
		if (pair->key == 56)	/* logoff */
 
			who = pair->value;
 

	
 
		if (pair->key == 57)
 
			room = pair->value;
 

	
 
		if (pair->key == 58)	/* join message */
 
			msg = pair->value;
 
		if (pair->key == 14)	/* decline/conf message */
 
			msg = pair->value;
 

	
 
		if (pair->key == 13) ;
 
		if (pair->key == 16)	/* error */
 
			msg = pair->value;
 

	
 
		if (pair->key == 1)	/* my id */
 
			id = pair->value;
 
		if (pair->key == 3)	/* message sender */
 
			who = pair->value;
 

	
 
		if (pair->key == 97)
 
			utf8 = atoi(pair->value);
 
	}
 

	
 
	if (!room)
 
		return;
 

	
 
	if (host) {
 
		for (l = members; l; l = l->next) {
 
			char *w = l->data;
 
			if (!strcmp(w, host))
 
				break;
 
		}
 
		if (!l)
 
			members = y_list_append(members, strdup(host));
 
	}
 
	/* invite, decline, join, left, message -> status == 1 */
 

	
 
	switch (pkt->service) {
 
	case YAHOO_SERVICE_CONFINVITE:
 
		if (pkt->status == 2) ;
 
		else if (members)
 
			YAHOO_CALLBACK(ext_yahoo_got_conf_invite) (yd->
 
				client_id, id, host, room, msg, members);
 
		else if (msg)
 
			YAHOO_CALLBACK(ext_yahoo_error) (yd->client_id, msg, 0,
 
				E_CONFNOTAVAIL);
 
		break;
 
	case YAHOO_SERVICE_CONFADDINVITE:
 
		if (pkt->status == 1)
 
			YAHOO_CALLBACK(ext_yahoo_got_conf_invite) (yd->
 
				client_id, id, host, room, msg, members);
 
		break;
 
	case YAHOO_SERVICE_CONFDECLINE:
 
		if (who)
 
			YAHOO_CALLBACK(ext_yahoo_conf_userdecline) (yd->
 
				client_id, id, who, room, msg);
 
		break;
 
	case YAHOO_SERVICE_CONFLOGON:
 
		if (who)
 
			YAHOO_CALLBACK(ext_yahoo_conf_userjoin) (yd->client_id,
 
				id, who, room);
 
		break;
 
	case YAHOO_SERVICE_CONFLOGOFF:
 
		if (who)
 
			YAHOO_CALLBACK(ext_yahoo_conf_userleave) (yd->client_id,
 
				id, who, room);
 
		break;
 
	case YAHOO_SERVICE_CONFMSG:
 
		if (who)
 
			YAHOO_CALLBACK(ext_yahoo_conf_message) (yd->client_id,
 
				id, who, room, msg, utf8);
 
		break;
 
	}
 
}
 

	
 
static void yahoo_process_chat(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *msg = NULL;
 
	char *id = NULL;
 
	char *who = NULL;
 
	char *room = NULL;
 
	char *topic = NULL;
 
	YList *members = NULL;
 
	struct yahoo_chat_member *currentmember = NULL;
 
	int msgtype = 1;
 
	int utf8 = 0;
 
	int firstjoin = 0;
 
	int membercount = 0;
 
	int chaterr = 0;
 
	YList *l;
 

	
 
	yahoo_dump_unhandled(pkt);
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		if (pair->key == 1) {
 
			/* My identity */
 
			id = pair->value;
 
		}
 

	
 
		if (pair->key == 104) {
 
			/* Room name */
 
			room = pair->value;
 
		}
 

	
 
		if (pair->key == 105) {
 
			/* Room topic */
 
			topic = pair->value;
 
		}
 

	
 
		if (pair->key == 108) {
 
			/* Number of members in this packet */
 
			membercount = atoi(pair->value);
 
		}
 

	
 
		if (pair->key == 109) {
 
			/* message sender */
 
			who = pair->value;
 

	
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN) {
 
				currentmember =
 
					y_new0(struct yahoo_chat_member, 1);
 
				currentmember->id = strdup(pair->value);
 
				members = y_list_append(members, currentmember);
 
			}
 
		}
 

	
 
		if (pair->key == 110) {
 
			/* age */
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN)
 
				currentmember->age = atoi(pair->value);
 
		}
 

	
 
		if (pair->key == 113) {
 
			/* attribs */
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN)
 
				currentmember->attribs = atoi(pair->value);
 
		}
 

	
 
		if (pair->key == 141) {
 
			/* alias */
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN)
 
				currentmember->alias = strdup(pair->value);
 
		}
 

	
 
		if (pair->key == 142) {
 
			/* location */
 
			if (pkt->service == YAHOO_SERVICE_CHATJOIN)
 
				currentmember->location = strdup(pair->value);
 
		}
 

	
 
		if (pair->key == 130) {
 
			/* first join */
 
			firstjoin = 1;
 
		}
 

	
 
		if (pair->key == 117) {
 
			/* message */
 
			msg = pair->value;
 
		}
 

	
 
		if (pair->key == 124) {
 
			/* Message type */
 
			msgtype = atoi(pair->value);
 
		}
 
		if (pair->key == 114) {
 
			/* message error not sure what all the pair values mean */
 
			/* but -1 means no session in room */
 
			chaterr = atoi(pair->value);
 
		}
 
	}
 

	
 
	if (!room) {
 
		if (pkt->service == YAHOO_SERVICE_CHATLOGOUT) {	/* yahoo originated chat logout */
 
			YAHOO_CALLBACK(ext_yahoo_chat_yahoologout) (yid->yd->
 
				client_id, id);
 
			return;
 
		}
 
		if (pkt->service == YAHOO_SERVICE_COMMENT && chaterr) {
 
			YAHOO_CALLBACK(ext_yahoo_chat_yahooerror) (yid->yd->
 
				client_id, id);
 
			return;
 
		}
 

	
 
		WARNING(("We didn't get a room name, ignoring packet"));
 
		return;
 
	}
 

	
 
	switch (pkt->service) {
 
	case YAHOO_SERVICE_CHATJOIN:
 
		if (y_list_length(members) != membercount) {
 
			WARNING(("Count of members doesn't match No. of members we got"));
 
		}
 
		if (firstjoin && members) {
 
			YAHOO_CALLBACK(ext_yahoo_chat_join) (yid->yd->client_id,
 
				id, room, topic, members, yid->fd);
 
		} else if (who) {
 
			if (y_list_length(members) != 1) {
 
				WARNING(("Got more than 1 member on a normal join"));
 
			}
 
			/* this should only ever have one, but just in case */
 
			while (members) {
 
				YList *n = members->next;
 
				currentmember = members->data;
 
				YAHOO_CALLBACK(ext_yahoo_chat_userjoin) (yid->
 
					yd->client_id, id, room, currentmember);
 
				y_list_free_1(members);
 
				members = n;
 
			}
 
		}
 
		break;
 
	case YAHOO_SERVICE_CHATEXIT:
 
		if (who) {
 
			YAHOO_CALLBACK(ext_yahoo_chat_userleave) (yid->yd->
 
				client_id, id, room, who);
 
		}
 
		break;
 
	case YAHOO_SERVICE_COMMENT:
 
		if (who) {
 
			YAHOO_CALLBACK(ext_yahoo_chat_message) (yid->yd->
 
				client_id, id, who, room, msg, msgtype, utf8);
 
		}
 
		break;
 
	}
 
}
 

	
 
static void yahoo_process_message(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	YList *l;
 
	YList *messages = NULL;
 

	
 
	struct m {
 
		int i_31;
 
		int i_32;
 
		char *to;
 
		char *from;
 
		long tm;
 
		char *msg;
 
		int utf8;
 
		char *gunk;
 
	} *message = y_new0(struct m, 1);
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1 || pair->key == 4) {
 
			if (!message->from)
 
				message->from = pair->value;
 
		} else if (pair->key == 5)
 
			message->to = pair->value;
 
		else if (pair->key == 15)
 
			message->tm = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 97)
 
			message->utf8 = atoi(pair->value);
 
		/* This comes when the official client sends us a message */
 
		else if (pair->key == 429)
 
			message->gunk = pair->value;
 
		/* user message *//* sys message */
 
		else if (pair->key == 14 || pair->key == 16)
 
			message->msg = pair->value;
 
		else if (pair->key == 31) {
 
			if (message->i_31) {
 
				messages = y_list_append(messages, message);
 
				message = y_new0(struct m, 1);
 
			}
 
			message->i_31 = atoi(pair->value);
 
		} else if (pair->key == 32)
 
			message->i_32 = atoi(pair->value);
 
		else
 
			LOG(("yahoo_process_message: status: %d, key: %d, value: %s", pkt->status, pair->key, pair->value));
 
	}
 

	
 
	messages = y_list_append(messages, message);
 

	
 
	for (l = messages; l; l = l->next) {
 
		message = l->data;
 
		if (pkt->service == YAHOO_SERVICE_SYSMESSAGE) {
 
			YAHOO_CALLBACK(ext_yahoo_system_message) (yd->client_id,
 
				message->to, message->from, message->msg);
 
		} else if (pkt->status <= 2 || pkt->status == 5) {
 
			/* Confirm message receipt if we got the gunk */
 
			if(message->gunk) {
 
				struct yahoo_packet *outpkt;
 
                        
 
				outpkt = yahoo_packet_new(YAHOO_SERVICE_MESSAGE_CONFIRM,
 
					YPACKET_STATUS_DEFAULT, 0);
 
				yahoo_packet_hash(outpkt, 1, yd->user);
 
				yahoo_packet_hash(outpkt, 5, message->from);
 
				yahoo_packet_hash(outpkt, 302, "430");
 
				yahoo_packet_hash(outpkt, 430, message->gunk);
 
				yahoo_packet_hash(outpkt, 303, "430");
 
				yahoo_packet_hash(outpkt, 450, "0");
 
				yahoo_send_packet(yid, outpkt, 0);
 
                        
 
				yahoo_packet_free(outpkt);
 
			}
 

	
 
			if (!strcmp(message->msg, "<ding>"))
 
				YAHOO_CALLBACK(ext_yahoo_got_buzz) (yd->client_id,
 
					message->to, message->from, message->tm);
 
			else
 
				YAHOO_CALLBACK(ext_yahoo_got_im) (yd->client_id,
 
					message->to, message->from, message->msg,
 
					message->tm, pkt->status, message->utf8);
 
		} else if (pkt->status == 0xffffffff) {
 
			YAHOO_CALLBACK(ext_yahoo_error) (yd->client_id,
 
				message->msg, 0, E_SYSTEM);
 
		}
 
		FREE(message);
 
	}
 

	
 
	y_list_free(messages);
 
}
 

	
 
/*
 
 * Here's what multi-level packets look like. Data in brackets is the value.
 
 *
 
 * 3 level:
 
 * =======
 
 * 
 
 * 302 (318) - Beginning level 1
 
 * 	300 (318) - Begin level 2
 
 * 	302 (319) - End level 2 header
 
 * 		300 (319) - Begin level 3
 
 * 		301 (319) - End level 3
 
 * 	303 (319) - End level 2
 
 * 303 (318) - End level 1
 
 *
 
 * 2 level:
 
 * =======
 
 *
 
 * 302 (315) - Beginning level 1
 
 * 	300 (315) - Begin level 2
 
 * 	301 (315) - End level 2
 
 * 303 (315) - End level 1
 
 *
 
 */
 
static void yahoo_process_status(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	struct yahoo_process_status_entry *u;
 

	
 
	YList *users = 0;
 

	
 
	if (pkt->service == YAHOO_SERVICE_LOGOFF && pkt->status == -1) {
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_DUPL, NULL);
 
		return;
 
	}
 

	
 
	/* 
 
	 * Status updates may be spread accross multiple packets and not 
 
	 * even on buddy boundaries, so keeping some state is important. 
 
	 * So, continue where we left off, and only add a user entry to 
 
	 * the list once it's complete (301-315 End buddy).
 
	 */
 
	u = yd->half_user;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 300:	/* Begin buddy */
 
			if (!strcmp(pair->value, "315") && !u) {
 
				u = yd->half_user = y_new0(struct yahoo_process_status_entry, 1);
 
			}
 
			break;
 
		case 301:	/* End buddy */
 
			if (!strcmp(pair->value, "315") && u) {
 
				users = y_list_prepend(users, u);
 
				u = yd->half_user = NULL;
 
			}
 
			break;
 
		case 0:	/* we won't actually do anything with this */
 
			NOTICE(("key %d:%s", pair->key, pair->value));
 
			break;
 
		case 1:	/* we don't get the full buddy list here. */
 
			if (!yd->logged_in) {
 
				yd->logged_in = 1;
 
				if (yd->current_status < 0)
 
					yd->current_status = yd->initial_status;
 
				YAHOO_CALLBACK(ext_yahoo_login_response) (yd->
 
					client_id, YAHOO_LOGIN_OK, NULL);
 
			}
 
			break;
 
		case 8:	/* how many online buddies we have */
 
			NOTICE(("key %d:%s", pair->key, pair->value));
 
			break;
 
		case 7:	/* the current buddy */
 
			if (!u) {
 
				/* This will only happen in case of a single level message */
 
				u = y_new0(struct yahoo_process_status_entry, 1);
 
				users = y_list_prepend(users, u);
 
			}
 
			u->name = pair->value;
 
			break;
 
		case 10:	/* state */
 
			u->state = strtol(pair->value, NULL, 10);
 
			break;
 
		case 19:	/* custom status message */
 
			u->msg = pair->value;
 
			break;
 
		case 47:	/* is it an away message or not. Not applicable for YMSG16 anymore */
 
			u->away = atoi(pair->value);
 
			break;
 
		case 137:	/* seconds idle */
 
			u->idle = atoi(pair->value);
 
			break;
 
		case 11:	/* this is the buddy's session id */
 
			u->buddy_session = atoi(pair->value);
 
			break;
 
		case 17:	/* in chat? */
 
			u->f17 = atoi(pair->value);
 
			break;
 
		case 13:	/* bitmask, bit 0 = pager, bit 1 = chat, bit 2 = game */
 
			u->flags = atoi(pair->value);
 
			break;
 
		case 60:	/* SMS -> 1 MOBILE USER */
 
			/* sometimes going offline makes this 2, but invisible never sends it */
 
			u->mobile = atoi(pair->value);
 
			break;
 
		case 138:
 
			u->f138 = atoi(pair->value);
 
			break;
 
		case 184:
 
			u->f184 = pair->value;
 
			break;
 
		case 192:
 
			u->f192 = atoi(pair->value);
 
			break;
 
		case 10001:
 
			u->f10001 = atoi(pair->value);
 
			break;
 
		case 10002:
 
			u->f10002 = atoi(pair->value);
 
			break;
 
		case 198:
 
			u->f198 = atoi(pair->value);
 
			break;
 
		case 197:
 
			u->f197 = pair->value;
 
			break;
 
		case 205:
 
			u->f205 = pair->value;
 
			break;
 
		case 213:
 
			u->f213 = atoi(pair->value);
 
			break;
 
		case 16:	/* Custom error message */
 
			YAHOO_CALLBACK(ext_yahoo_error) (yd->client_id,
 
				pair->value, 0, E_CUSTOM);
 
			break;
 
		default:
 
			WARNING(("unknown status key %d:%s", pair->key,
 
					pair->value));
 
			break;
 
		}
 
	}
 

	
 
	while (users) {
 
		YList *t = users;
 
		struct yahoo_process_status_entry *u = users->data;
 

	
 
		if (u->name != NULL) {
 
			if (pkt->service ==
 
				YAHOO_SERVICE_LOGOFF
 
				/*|| u->flags == 0 No flags for YMSG16 */ ) {
 
				YAHOO_CALLBACK(ext_yahoo_status_changed) (yd->
 
					client_id, u->name,
 
					YAHOO_STATUS_OFFLINE, NULL, 1, 0, 0);
 
			} else {
 
				/* Key 47 always seems to be 1 for YMSG16 */
 
				if (!u->state)
 
					u->away = 0;
 
				else
 
					u->away = 1;
 

	
 
				YAHOO_CALLBACK(ext_yahoo_status_changed) (yd->
 
					client_id, u->name, u->state, u->msg,
 
					u->away, u->idle, u->mobile);
 
			}
 
		}
 

	
 
		users = y_list_remove_link(users, users);
 
		y_list_free_1(t);
 
		FREE(u);
 
	}
 
}
 

	
 
static void yahoo_process_buddy_list(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	YList *l;
 
	int last_packet = 0;
 
	char *cur_group = NULL;
 
	struct yahoo_buddy *newbud = NULL;
 

	
 
	/* we could be getting multiple packets here */
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 300:
 
		case 301:
 
		case 302:
 
			break;	/* Separators. Our logic does not need them */
 
		case 303:
 
			if (318 == atoi(pair->value))
 
				last_packet = 1;
 
			break;
 
		case 65:
 
			cur_group = strdup(pair->value);
 
			break;
 
		case 7:
 
			newbud = y_new0(struct yahoo_buddy, 1);
 
			newbud->id = strdup(pair->value);
 
			if (cur_group)
 
				newbud->group = strdup(cur_group);
 
			else if (yd->buddies) {
 
				struct yahoo_buddy *lastbud =
 
					(struct yahoo_buddy *)y_list_nth(yd->
 
					buddies,
 
					y_list_length(yd->buddies) - 1)->data;
 
				newbud->group = strdup(lastbud->group);
 
			} else
 
				newbud->group = strdup("Buddies");
 

	
 
			yd->buddies = y_list_append(yd->buddies, newbud);
 

	
 
			break;
 
		}
 
	}
 

	
 
	/* we could be getting multiple packets here */
 
	if (pkt->hash && !last_packet)
 
		return;
 

	
 
	/* Logged in */
 
	if (!yd->logged_in) {
 
		yd->logged_in = 1;
 
		if (yd->current_status < 0)
 
			yd->current_status = yd->initial_status;
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_OK, NULL);
 

	
 
		yahoo_set_away(yd->client_id, yd->initial_status, NULL,
 
			(yd->initial_status == YAHOO_STATUS_AVAILABLE) ? 0 : 1);
 

	
 
		yahoo_get_yab(yd->client_id);
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_got_buddies) (yd->client_id, yd->buddies);
 

	
 
}
 

	
 
static void yahoo_process_list(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	YList *l;
 

	
 
	/* we could be getting multiple packets here */
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 89:	/* identities */
 
			{
 
				char **identities =
 
					y_strsplit(pair->value, ",", -1);
 
				int i;
 
				for (i = 0; identities[i]; i++)
 
					yd->identities =
 
						y_list_append(yd->identities,
 
						strdup(identities[i]));
 
				y_strfreev(identities);
 
			}
 
			YAHOO_CALLBACK(ext_yahoo_got_identities) (yd->client_id,
 
				yd->identities);
 
			break;
 
		case 59:	/* cookies */
 
			if (pair->value[0] == 'Y') {
 
				FREE(yd->cookie_y);
 
				FREE(yd->login_cookie);
 

	
 
				yd->cookie_y = getcookie(pair->value);
 
				yd->login_cookie = getlcookie(yd->cookie_y);
 

	
 
			} else if (pair->value[0] == 'T') {
 
				FREE(yd->cookie_t);
 
				yd->cookie_t = getcookie(pair->value);
 

	
 
			} else if (pair->value[0] == 'C') {
 
				FREE(yd->cookie_c);
 
				yd->cookie_c = getcookie(pair->value);
 
			}
 

	
 
			break;
 
		case 3:	/* my id */
 
		case 90:	/* 1 */
 
		case 100:	/* 0 */
 
		case 101:	/* NULL */
 
		case 102:	/* NULL */
 
		case 93:	/* 86400/1440 */
 
			break;
 
		}
 
	}
 

	
 
	if (yd->cookie_y && yd->cookie_t)	/* We don't get cookie_c anymore */
 
		YAHOO_CALLBACK(ext_yahoo_got_cookies) (yd->client_id);
 
}
 

	
 
static void yahoo_process_verify(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	if (pkt->status != 0x01) {
 
		DEBUG_MSG(("expected status: 0x01, got: %d", pkt->status));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_LOCK, "");
 
		return;
 
	}
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_AUTH, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
}
 

	
 
static void yahoo_process_picture_checksum(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *from;
 
	char *to;
 
	int checksum = 0;
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 1:
 
		case 4:
 
			from = pair->value;
 
		case 5:
 
			to = pair->value;
 
			break;
 
		case 212:
 
			break;
 
		case 192:
 
			checksum = atoi(pair->value);
 
			break;
 
		}
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_got_buddyicon_checksum) (yd->client_id, to,
 
		from, checksum);
 
}
 

	
 
static void yahoo_process_picture(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *url;
 
	char *from;
 
	char *to;
 
	int status = 0;
 
	int checksum = 0;
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 1:
 
		case 4:	/* sender */
 
			from = pair->value;
 
			break;
 
		case 5:	/* we */
 
			to = pair->value;
 
			break;
 
		case 13:	/* request / sending */
 
			status = atoi(pair->value);
 
			break;
 
		case 20:	/* url */
 
			url = pair->value;
 
			break;
 
		case 192:	/*checksum */
 
			checksum = atoi(pair->value);
 
			break;
 
		}
 
	}
 

	
 
	switch (status) {
 
	case 1:		/* this is a request, ignore for now */
 
		YAHOO_CALLBACK(ext_yahoo_got_buddyicon_request) (yd->client_id,
 
			to, from);
 
		break;
 
	case 2:		/* this is cool - we get a picture :) */
 
		YAHOO_CALLBACK(ext_yahoo_got_buddyicon) (yd->client_id, to,
 
			from, url, checksum);
 
		break;
 
	}
 
}
 

	
 
static void yahoo_process_picture_upload(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	YList *l;
 
	char *url;
 

	
 
	if (pkt->status != 1)
 
		return;		/* something went wrong */
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 5:	/* we */
 
			break;
 
		case 20:	/* url */
 
			url = pair->value;
 
			break;
 
		case 27:	/* local filename */
 
			break;
 
		case 38:	/* time */
 
			break;
 
		}
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_buddyicon_uploaded) (yd->client_id, url);
 
}
 

	
 
void yahoo_login(int id, int initial)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct connect_callback_data *ccd;
 
	struct yahoo_server_settings *yss;
 
	int tag;
 

	
 
	char *host;
 

	
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_PAGER;
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	yd->initial_status = initial;
 
	yss = yd->server_settings;
 

	
 
	ccd = y_new0(struct connect_callback_data, 1);
 
	ccd->yd = yd;
 

	
 
	host = yss->pager_host;
 

	
 
	if (!host)
 
		host = yss->pager_host_list[0];
 

	
 
	tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->client_id,
 
		host, yss->pager_port, yahoo_connected, ccd, 0);
 

	
 
	/*
 
	 * if tag <= 0, then callback has already been called
 
	 * so ccd will have been freed
 
	 */
 
	if (tag > 0)
 
		ccd->tag = tag;
 
	else if (tag < 0)
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_SOCK, NULL);
 
}
 

	
 
static void yahoo_auth_https(struct yahoo_data *yd)
 
{
 
	char url[256];
 
	char *user_encoded, *pass_encoded, *seed_encoded;
 

	
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 

	
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_AUTH;
 

	
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	user_encoded = yahoo_urlencode(yid->yd->user);
 
	pass_encoded = yahoo_urlencode(yid->yd->password);
 
	seed_encoded = yahoo_urlencode(yid->yd->seed);
 

	
 
	snprintf(url, sizeof(url),
 
		"https://login.yahoo.com/config/pwtoken_get?src=ymsgr&ts="
 
		"&login=%s&passwd=%s&chal=%s", user_encoded, pass_encoded,
 
		seed_encoded);
 

	
 
	yahoo_http_get(yid->yd->client_id, url, NULL, 1, 0,
 
		_yahoo_http_connected, yid);
 

	
 
	FREE(user_encoded);
 
	FREE(pass_encoded);
 
	FREE(seed_encoded);
 
}
 

	
 
static void yahoo_process_auth(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *seed = NULL;
 
	char *sn = NULL;
 
	YList *l = pkt->hash;
 
	int m = 0;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	while (l) {
 
		struct yahoo_pair *pair = l->data;
 

	
 
		switch (pair->key) {
 
		case 94:
 
			seed = pair->value;
 
			break;
 
		case 1:
 
			sn = pair->value;
 
			break;
 
		case 13:
 
			m = atoi(pair->value);
 
			break;
 
		}
 
		l = l->next;
 
	}
 

	
 
	if (!seed)
 
		return;
 

	
 
	yd->seed = strdup(seed);
 

	
 
	if (m==2)
 
		yahoo_auth_https(yd);
 
	else {
 
		/* call error */
 
		WARNING(("unknown auth type %d", m));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
	}
 
}
 

	
 
static void yahoo_send_auth(struct yahoo_data *yd)
 
{
 

	
 
	struct yahoo_packet *packet;
 

	
 
	unsigned char crypt_hash[25];
 

	
 
	md5_byte_t result[16];
 
	md5_state_t ctx;
 

	
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(yd->client_id,
 
			YAHOO_CONNECTION_PAGER);
 

	
 
	/* HTTPS */
 
	md5_init(&ctx);
 
	md5_append(&ctx, (md5_byte_t *)yd->crumb, strlen(yd->crumb));
 
	md5_append(&ctx, (md5_byte_t *)yd->seed, strlen(yd->seed));
 
	md5_finish(&ctx, result);
 

	
 
	to_y64(crypt_hash, result, 16);
 

	
 
	packet = yahoo_packet_new(YAHOO_SERVICE_AUTHRESP,
 
		yd->initial_status, yd->session_id);
 
	yahoo_packet_hash(packet, 1, yd->user);
 
	yahoo_packet_hash(packet, 0, yd->user);
 
	yahoo_packet_hash(packet, 277, yd->cookie_y);
 
	yahoo_packet_hash(packet, 278, yd->cookie_t);
 
	yahoo_packet_hash(packet, 307, (char *)crypt_hash);
 
	yahoo_packet_hash(packet, 244, "4194239");	/* Rekkanoryo says this is the build number */
 
	yahoo_packet_hash(packet, 2, yd->user);
 
	yahoo_packet_hash(packet, 2, "1");
 
	yahoo_packet_hash(packet, 59, yd->cookie_b);
 
	yahoo_packet_hash(packet, 98, "us");	/* TODO Put country code */
 
	yahoo_packet_hash(packet, 135, "9.0.0.2152");
 

	
 
	yahoo_send_packet(yid, packet, 0);
 

	
 
	yahoo_packet_free(packet);
 
}
 

	
 
static void yahoo_process_auth_resp(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *login_id;
 
	char *handle;
 
	char *url = NULL;
 
	int login_status = -1;
 

	
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 0)
 
			login_id = pair->value;
 
		else if (pair->key == 1)
 
			handle = pair->value;
 
		else if (pair->key == 20)
 
			url = pair->value;
 
		else if (pair->key == 66)
 
			login_status = atoi(pair->value);
 
	}
 

	
 
	if (pkt->status == YPACKET_STATUS_DISCONNECTED) {
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
			login_status, url);
 
		/*      yahoo_logoff(yd->client_id); */
 
	}
 
}
 

	
 
static void yahoo_process_mail(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *who = NULL;
 
	char *email = NULL;
 
	char *subj = NULL;
 
	int count = 0;
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 9)
 
			count = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 43)
 
			who = pair->value;
 
		else if (pair->key == 42)
 
			email = pair->value;
 
		else if (pair->key == 18)
 
			subj = pair->value;
 
		else
 
			LOG(("key: %d => value: %s", pair->key, pair->value));
 
	}
 

	
 
	if (who && email && subj) {
 
		char from[1024];
 
		snprintf(from, sizeof(from), "%s (%s)", who, email);
 
		YAHOO_CALLBACK(ext_yahoo_mail_notify) (yd->client_id, from,
 
			subj, count);
 
	} else if (count > 0)
 
		YAHOO_CALLBACK(ext_yahoo_mail_notify) (yd->client_id, NULL,
 
			NULL, count);
 
}
 

	
 
static void yahoo_process_new_contact(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *me = NULL;
 
	char *who = NULL;
 
	char *msg = NULL;
 
	int online = -1;
 

	
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 4)
 
			who = pair->value;
 
		else if (pair->key == 5)
 
			me = pair->value;
 
		else if (pair->key == 14)
 
			msg = pair->value;
 
		else if (pair->key == 13)
 
			online = strtol(pair->value, NULL, 10);
 
	}
 

	
 
	if (who && online < 0)
 
		YAHOO_CALLBACK(ext_yahoo_contact_added) (yd->client_id, me, who,
 
			msg);
 
	else if (online == 2)
 
		YAHOO_CALLBACK(ext_yahoo_rejected) (yd->client_id, who, msg);
 
}
 

	
 
/* UNUSED? */
 
static void yahoo_process_contact(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *id = NULL;
 
	char *who = NULL;
 
	char *msg = NULL;
 
	char *name = NULL;
 
	long tm = 0L;
 
	int state = YAHOO_STATUS_AVAILABLE;
 
	int online = 0;
 
	int away = 0;
 
	int idle = 0;
 
	int mobile = 0;
 

	
 
	YList *l;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1)
 
			id = pair->value;
 
		else if (pair->key == 3)
 
			who = pair->value;
 
		else if (pair->key == 14)
 
			msg = pair->value;
 
		else if (pair->key == 7)
 
			name = pair->value;
 
		else if (pair->key == 10)
 
			state = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 15)
 
			tm = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 13)
 
			online = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 47)
 
			away = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 137)
 
			idle = strtol(pair->value, NULL, 10);
 
		else if (pair->key == 60)
 
			mobile = strtol(pair->value, NULL, 10);
 

	
 
	}
 

	
 
	if (id)
 
		YAHOO_CALLBACK(ext_yahoo_contact_added) (yd->client_id, id, who,
 
			msg);
 
	else if (name)
 
		YAHOO_CALLBACK(ext_yahoo_status_changed) (yd->client_id, name,
 
			state, msg, away, idle, mobile);
 
	else if (pkt->status == 0x07)
 
		YAHOO_CALLBACK(ext_yahoo_rejected) (yd->client_id, who, msg);
 
}
 

	
 
static void yahoo_process_buddyadd(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *who = NULL;
 
	char *where = NULL;
 
	int status = 0;
 
	char *me = NULL;
 

	
 
	struct yahoo_buddy *bud = NULL;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1)
 
			me = pair->value;
 
		if (pair->key == 7)
 
			who = pair->value;
 
		if (pair->key == 65)
 
			where = pair->value;
 
		if (pair->key == 66)
 
			status = strtol(pair->value, NULL, 10);
 
	}
 

	
 
	if (!who)
 
		return;
 
	if (!where)
 
		where = "Unknown";
 

	
 
	bud = y_new0(struct yahoo_buddy, 1);
 
	bud->id = strdup(who);
 
	bud->group = strdup(where);
 
	bud->real_name = NULL;
 

	
 
	yd->buddies = y_list_append(yd->buddies, bud);
 

	
 
	/* A non-zero status (i've seen 2) seems to mean the buddy is already 
 
	 * added and is online */
 
	if (status) {
 
		LOG(("Setting online see packet for info"));
 
		yahoo_dump_unhandled(pkt);
 
		YAHOO_CALLBACK(ext_yahoo_status_changed) (yd->client_id, who,
 
			YAHOO_STATUS_AVAILABLE, NULL, 0, 0, 0);
 
	}
 
}
 

	
 
static void yahoo_process_buddydel(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	char *who = NULL;
 
	char *where = NULL;
 
	int unk_66 = 0;
 
	char *me = NULL;
 
	struct yahoo_buddy *bud;
 

	
 
	YList *buddy;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1)
 
			me = pair->value;
 
		else if (pair->key == 7)
 
			who = pair->value;
 
		else if (pair->key == 65)
 
			where = pair->value;
 
		else if (pair->key == 66)
 
			unk_66 = strtol(pair->value, NULL, 10);
 
		else
 
			DEBUG_MSG(("unknown key: %d = %s", pair->key,
 
					pair->value));
 
	}
 

	
 
	if (!who || !where)
 
		return;
 

	
 
	bud = y_new0(struct yahoo_buddy, 1);
 
	bud->id = strdup(who);
 
	bud->group = strdup(where);
 

	
 
	buddy = y_list_find_custom(yd->buddies, bud, is_same_bud);
 

	
 
	FREE(bud->id);
 
	FREE(bud->group);
 
	FREE(bud);
 

	
 
	if (buddy) {
 
		bud = buddy->data;
 
		yd->buddies = y_list_remove_link(yd->buddies, buddy);
 
		y_list_free_1(buddy);
 

	
 
		FREE(bud->id);
 
		FREE(bud->group);
 
		FREE(bud->real_name);
 
		FREE(bud);
 

	
 
		bud = NULL;
 
	}
 
}
 

	
 
static void yahoo_process_ignore(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *who = NULL;
 
	int status = 0;
 
	char *me = NULL;
 
	int un_ignore = 0;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 0)
 
			who = pair->value;
 
		if (pair->key == 1)
 
			me = pair->value;
 
		if (pair->key == 13)	/* 1 == ignore, 2 == unignore */
 
			un_ignore = strtol(pair->value, NULL, 10);
 
		if (pair->key == 66)
 
			status = strtol(pair->value, NULL, 10);
 
	}
 

	
 
	/*
 
	 * status
 
	 *      0  - ok
 
	 *      2  - already in ignore list, could not add
 
	 *      3  - not in ignore list, could not delete
 
	 *      12 - is a buddy, could not add
 
	 */
 

	
 
/*	if(status)
 
		YAHOO_CALLBACK(ext_yahoo_error)(yd->client_id, who, 0, status);
 
*/
 
}
 

	
 
static void yahoo_process_voicechat(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *who = NULL;
 
	char *me = NULL;
 
	char *room = NULL;
 
	char *voice_room = NULL;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 4)
 
			who = pair->value;
 
		if (pair->key == 5)
 
			me = pair->value;
 
		if (pair->key == 13)
 
			voice_room = pair->value;
 
		if (pair->key == 57)
 
			room = pair->value;
 
	}
 

	
 
	NOTICE(("got voice chat invite from %s in %s to identity %s", who, room,
 
			me));
 
	/* 
 
	 * send: s:0 1:me 5:who 57:room 13:1
 
	 * ????  s:4 5:who 10:99 19:-1615114531
 
	 * gotr: s:4 5:who 10:99 19:-1615114615
 
	 * ????  s:1 5:me 4:who 57:room 13:3room
 
	 * got:  s:1 5:me 4:who 57:room 13:1room
 
	 * rej:  s:0 1:me 5:who 57:room 13:3
 
	 * rejr: s:4 5:who 10:99 19:-1617114599
 
	 */
 
}
 

	
 
static void yahoo_process_ping(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *errormsg = NULL;
 

	
 
	YList *l;
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 16)
 
			errormsg = pair->value;
 
	}
 

	
 
	NOTICE(("got ping packet"));
 
	YAHOO_CALLBACK(ext_yahoo_got_ping) (yid->yd->client_id, errormsg);
 
}
 

	
 
static void yahoo_process_buddy_change_group(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	char *me = NULL;
 
	char *who = NULL;
 
	char *old_group = NULL;
 
	char *new_group = NULL;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 1)
 
			me = pair->value;
 
		if (pair->key == 7)
 
			who = pair->value;
 
		if (pair->key == 224)
 
			old_group = pair->value;
 
		if (pair->key == 264)
 
			new_group = pair->value;
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_got_buddy_change_group) (yid->yd->client_id,
 
		me, who, old_group, new_group);
 
}
 

	
 
static void _yahoo_webcam_get_server_connected(void *fd, int error, void *d)
 
{
 
	struct yahoo_input_data *yid = d;
 
	char *who = yid->wcm->user;
 
	char *data = NULL;
 
	char *packet = NULL;
 
	unsigned char magic_nr[] = { 0, 1, 0 };
 
	unsigned char header_len = 8;
 
	unsigned int len = 0;
 
	unsigned int pos = 0;
 

	
 
	if (error || !fd) {
 
		FREE(who);
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	yid->fd = fd;
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	/* send initial packet */
 
	if (who)
 
		data = strdup("<RVWCFG>");
 
	else
 
		data = strdup("<RUPCFG>");
 
	yahoo_add_to_send_queue(yid, data, strlen(data));
 
	FREE(data);
 

	
 
	/* send data */
 
	if (who) {
 
		data = strdup("g=");
 
		data = y_string_append(data, who);
 
		data = y_string_append(data, "\r\n");
 
	} else {
 
		data = strdup("f=1\r\n");
 
	}
 
	len = strlen(data);
 
	packet = y_new0(char, header_len + len);
 
	packet[pos++] = header_len;
 
	memcpy(packet + pos, magic_nr, sizeof(magic_nr));
 
	pos += sizeof(magic_nr);
 
	pos += yahoo_put32(packet + pos, len);
 
	memcpy(packet + pos, data, len);
 
	pos += len;
 
	yahoo_add_to_send_queue(yid, packet, pos);
 
	FREE(packet);
 
	FREE(data);
 

	
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id, fd,
 
		YAHOO_INPUT_READ, yid);
 
}
 

	
 
static void yahoo_webcam_get_server(struct yahoo_input_data *y, char *who,
 
	char *key)
 
{
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 
	struct yahoo_server_settings *yss = y->yd->server_settings;
 

	
 
	yid->type = YAHOO_CONNECTION_WEBCAM_MASTER;
 
	yid->yd = y->yd;
 
	yid->wcm = y_new0(struct yahoo_webcam, 1);
 
	yid->wcm->user = who ? strdup(who) : NULL;
 
	yid->wcm->direction = who ? YAHOO_WEBCAM_DOWNLOAD : YAHOO_WEBCAM_UPLOAD;
 
	yid->wcm->key = strdup(key);
 

	
 
	YAHOO_CALLBACK(ext_yahoo_connect_async) (yid->yd->client_id,
 
		yss->webcam_host, yss->webcam_port,
 
		_yahoo_webcam_get_server_connected, yid, 0);
 

	
 
}
 

	
 
static YList *webcam_queue = NULL;
 
static void yahoo_process_webcam_key(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	char *me = NULL;
 
	char *key = NULL;
 
	char *who = NULL;
 

	
 
	YList *l;
 
	yahoo_dump_unhandled(pkt);
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		if (pair->key == 5)
 
			me = pair->value;
 
		if (pair->key == 61)
 
			key = pair->value;
 
	}
 

	
 
	l = webcam_queue;
 
	if (!l)
 
		return;
 
	who = l->data;
 
	webcam_queue = y_list_remove_link(webcam_queue, webcam_queue);
 
	y_list_free_1(l);
 
	yahoo_webcam_get_server(yid, who, key);
 
	FREE(who);
 
}
 

	
 
static void yahoo_packet_process(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	DEBUG_MSG(("yahoo_packet_process: 0x%02x", pkt->service));
 
	switch (pkt->service) {
 
	case YAHOO_SERVICE_USERSTAT:
 
	case YAHOO_SERVICE_LOGON:
 
	case YAHOO_SERVICE_LOGOFF:
 
	case YAHOO_SERVICE_ISAWAY:
 
	case YAHOO_SERVICE_ISBACK:
 
	case YAHOO_SERVICE_GAMELOGON:
 
	case YAHOO_SERVICE_GAMELOGOFF:
 
	case YAHOO_SERVICE_IDACT:
 
	case YAHOO_SERVICE_IDDEACT:
 
	case YAHOO_SERVICE_Y6_STATUS_UPDATE:
 
	case YAHOO_SERVICE_Y8_STATUS:
 
		yahoo_process_status(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_NOTIFY:
 
		yahoo_process_notify(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_MESSAGE:
 
	case YAHOO_SERVICE_GAMEMSG:
 
	case YAHOO_SERVICE_SYSMESSAGE:
 
		yahoo_process_message(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_NEWMAIL:
 
		yahoo_process_mail(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y7_AUTHORIZATION:
 
		yahoo_process_new_contact(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_NEWCONTACT:
 
		yahoo_process_contact(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_LIST:
 
		yahoo_process_list(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_VERIFY:
 
		yahoo_process_verify(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_AUTH:
 
		yahoo_process_auth(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_AUTHRESP:
 
		yahoo_process_auth_resp(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_CONFINVITE:
 
	case YAHOO_SERVICE_CONFADDINVITE:
 
	case YAHOO_SERVICE_CONFDECLINE:
 
	case YAHOO_SERVICE_CONFLOGON:
 
	case YAHOO_SERVICE_CONFLOGOFF:
 
	case YAHOO_SERVICE_CONFMSG:
 
		yahoo_process_conference(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_CHATONLINE:
 
	case YAHOO_SERVICE_CHATGOTO:
 
	case YAHOO_SERVICE_CHATJOIN:
 
	case YAHOO_SERVICE_CHATLEAVE:
 
	case YAHOO_SERVICE_CHATEXIT:
 
	case YAHOO_SERVICE_CHATLOGOUT:
 
	case YAHOO_SERVICE_CHATPING:
 
	case YAHOO_SERVICE_COMMENT:
 
		yahoo_process_chat(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_P2PFILEXFER:
 
	case YAHOO_SERVICE_Y7_FILETRANSFER:
 
		yahoo_process_filetransfer(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y7_FILETRANSFERINFO:
 
		yahoo_process_filetransferinfo(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y7_FILETRANSFERACCEPT:
 
		yahoo_process_filetransferaccept(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_ADDBUDDY:
 
		yahoo_process_buddyadd(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_REMBUDDY:
 
		yahoo_process_buddydel(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_IGNORECONTACT:
 
		yahoo_process_ignore(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_VOICECHAT:
 
		yahoo_process_voicechat(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_WEBCAM:
 
		yahoo_process_webcam_key(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_PING:
 
		yahoo_process_ping(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y7_CHANGE_GROUP:
 
		yahoo_process_buddy_change_group(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_IDLE:
 
	case YAHOO_SERVICE_MAILSTAT:
 
	case YAHOO_SERVICE_CHATINVITE:
 
	case YAHOO_SERVICE_CALENDAR:
 
	case YAHOO_SERVICE_NEWPERSONALMAIL:
 
	case YAHOO_SERVICE_ADDIDENT:
 
	case YAHOO_SERVICE_ADDIGNORE:
 
	case YAHOO_SERVICE_GOTGROUPRENAME:
 
	case YAHOO_SERVICE_GROUPRENAME:
 
	case YAHOO_SERVICE_PASSTHROUGH2:
 
	case YAHOO_SERVICE_CHATLOGON:
 
	case YAHOO_SERVICE_CHATLOGOFF:
 
	case YAHOO_SERVICE_CHATMSG:
 
	case YAHOO_SERVICE_REJECTCONTACT:
 
	case YAHOO_SERVICE_PEERTOPEER:
 
		WARNING(("unhandled service 0x%02x", pkt->service));
 
		yahoo_dump_unhandled(pkt);
 
		break;
 
	case YAHOO_SERVICE_PICTURE:
 
		yahoo_process_picture(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_PICTURE_CHECKSUM:
 
		yahoo_process_picture_checksum(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_PICTURE_UPLOAD:
 
		yahoo_process_picture_upload(yid, pkt);
 
		break;
 
	case YAHOO_SERVICE_Y8_LIST:	/* Buddy List */
 
		yahoo_process_buddy_list(yid, pkt);
 
		break;
 
	default:
 
		WARNING(("unknown service 0x%02x", pkt->service));
 
		yahoo_dump_unhandled(pkt);
 
		break;
 
	}
 
}
 

	
 
static struct yahoo_packet *yahoo_getdata(struct yahoo_input_data *yid)
 
{
 
	struct yahoo_packet *pkt;
 
	struct yahoo_data *yd = yid->yd;
 
	int pos = 0;
 
	int pktlen;
 

	
 
	if (!yd)
 
		return NULL;
 

	
 
	DEBUG_MSG(("rxlen is %d", yid->rxlen));
 
	if (yid->rxlen < YAHOO_PACKET_HDRLEN) {
 
		DEBUG_MSG(("len < YAHOO_PACKET_HDRLEN"));
 
		return NULL;
 
	}
 

	
 
	pos += 4;		/* YMSG */
 
	pos += 2;
 
	pos += 2;
 

	
 
	pktlen = yahoo_get16(yid->rxqueue + pos);
 
	pos += 2;
 
	DEBUG_MSG(("%d bytes to read, rxlen is %d", pktlen, yid->rxlen));
 

	
 
	if (yid->rxlen < (YAHOO_PACKET_HDRLEN + pktlen)) {
 
		DEBUG_MSG(("len < YAHOO_PACKET_HDRLEN + pktlen"));
 
		return NULL;
 
	}
 

	
 
	LOG(("reading packet"));
 
	yahoo_packet_dump(yid->rxqueue, YAHOO_PACKET_HDRLEN + pktlen);
 

	
 
	pkt = yahoo_packet_new(0, 0, 0);
 

	
 
	pkt->service = yahoo_get16(yid->rxqueue + pos);
 
	pos += 2;
 
	pkt->status = yahoo_get32(yid->rxqueue + pos);
 
	pos += 4;
 
	DEBUG_MSG(("Yahoo Service: 0x%02x Status: %d", pkt->service,
 
			pkt->status));
 
	pkt->id = yahoo_get32(yid->rxqueue + pos);
 
	pos += 4;
 

	
 
	yd->session_id = pkt->id;
 

	
 
	yahoo_packet_read(pkt, yid->rxqueue + pos, pktlen);
 

	
 
	yid->rxlen -= YAHOO_PACKET_HDRLEN + pktlen;
 
	DEBUG_MSG(("rxlen == %d, rxqueue == %p", yid->rxlen, yid->rxqueue));
 
	if (yid->rxlen > 0) {
 
		unsigned char *tmp = y_memdup(yid->rxqueue + YAHOO_PACKET_HDRLEN
 
			+ pktlen, yid->rxlen);
 
		FREE(yid->rxqueue);
 
		yid->rxqueue = tmp;
 
		DEBUG_MSG(("new rxlen == %d, rxqueue == %p", yid->rxlen,
 
				yid->rxqueue));
 
	} else {
 
		DEBUG_MSG(("freed rxqueue == %p", yid->rxqueue));
 
		FREE(yid->rxqueue);
 
	}
 

	
 
	return pkt;
 
}
 

	
 
static struct yab *yahoo_yab_read(unsigned char *d, int len)
 
{
 
	char *st, *en;
 
	char *data = (char *)d;
 
	struct yab *yab = NULL;
 

	
 
	data[len] = '\0';
 

	
 
	DEBUG_MSG(("Got yab: %s", data));
 
	st = en = strstr(data, "e0=\"");
 
	if (st) {
 
		yab = y_new0(struct yab, 1);
 

	
 
		st += strlen("e0=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->email = yahoo_xmldecode(st);
 
	}
 

	
 
	if (!en)
 
		return NULL;
 

	
 
	st = strstr(en, "id=\"");
 
	if (st) {
 
		st += strlen("id=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->yid = atoi(yahoo_xmldecode(st));
 
	}
 

	
 
	st = strstr(en, "fn=\"");
 
	if (st) {
 
		st += strlen("fn=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->fname = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "ln=\"");
 
	if (st) {
 
		st += strlen("ln=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->lname = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "nn=\"");
 
	if (st) {
 
		st += strlen("nn=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->nname = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "yi=\"");
 
	if (st) {
 
		st += strlen("yi=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->id = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "hphone=\"");
 
	if (st) {
 
		st += strlen("hphone=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->hphone = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "wphone=\"");
 
	if (st) {
 
		st += strlen("wphone=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->wphone = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "mphone=\"");
 
	if (st) {
 
		st += strlen("mphone=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->mphone = yahoo_xmldecode(st);
 
	}
 

	
 
	st = strstr(en, "dbid=\"");
 
	if (st) {
 
		st += strlen("dbid=\"");
 
		en = strchr(st, '"');
 
		*en++ = '\0';
 
		yab->dbid = atoi(st);
 
	}
 

	
 
	return yab;
 
}
 

	
 
static struct yab *yahoo_getyab(struct yahoo_input_data *yid)
 
{
 
	struct yab *yab = NULL;
 
	int pos = 0, end = 0;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	if (!yd)
 
		return NULL;
 

	
 
	do {
 
		DEBUG_MSG(("rxlen is %d", yid->rxlen));
 

	
 
		if (yid->rxlen <= strlen("<ct"))
 
			return NULL;
 

	
 
		/* start with <ct */
 
		while (pos < yid->rxlen - strlen("<ct") + 1
 
			&& memcmp(yid->rxqueue + pos, "<ct", strlen("<ct")))
 
			pos++;
 

	
 
		if (pos >= yid->rxlen - 1)
 
			return NULL;
 

	
 
		end = pos + 2;
 
		/* end with > */
 
		while (end < yid->rxlen - strlen(">")
 
			&& memcmp(yid->rxqueue + end, ">", strlen(">")))
 
			end++;
 

	
 
		if (end >= yid->rxlen - 1)
 
			return NULL;
 

	
 
		yab = yahoo_yab_read(yid->rxqueue + pos, end + 2 - pos);
 

	
 
		yid->rxlen -= end + 1;
 
		DEBUG_MSG(("rxlen == %d, rxqueue == %p", yid->rxlen,
 
				yid->rxqueue));
 
		if (yid->rxlen > 0) {
 
			unsigned char *tmp =
 
				y_memdup(yid->rxqueue + end + 1, yid->rxlen);
 
			FREE(yid->rxqueue);
 
			yid->rxqueue = tmp;
 
			DEBUG_MSG(("new rxlen == %d, rxqueue == %p", yid->rxlen,
 
					yid->rxqueue));
 
		} else {
 
			DEBUG_MSG(("freed rxqueue == %p", yid->rxqueue));
 
			FREE(yid->rxqueue);
 
		}
 

	
 
	} while (!yab && end < yid->rxlen - 1);
 

	
 
	return yab;
 
}
 

	
 
static char *yahoo_getwebcam_master(struct yahoo_input_data *yid)
 
{
 
	unsigned int pos = 0;
 
	unsigned int len = 0;
 
	unsigned int status = 0;
 
	char *server = NULL;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	if (!yid || !yd)
 
		return NULL;
 

	
 
	DEBUG_MSG(("rxlen is %d", yid->rxlen));
 

	
 
	len = yid->rxqueue[pos++];
 
	if (yid->rxlen < len)
 
		return NULL;
 

	
 
	/* extract status (0 = ok, 6 = webcam not online) */
 
	status = yid->rxqueue[pos++];
 

	
 
	if (status == 0) {
 
		pos += 2;	/* skip next 2 bytes */
 
		server = y_memdup(yid->rxqueue + pos, 16);
 
		pos += 16;
 
	} else if (status == 6) {
 
		YAHOO_CALLBACK(ext_yahoo_webcam_closed)
 
			(yd->client_id, yid->wcm->user, 4);
 
	}
 

	
 
	/* skip rest of the data */
 

	
 
	yid->rxlen -= len;
 
	DEBUG_MSG(("rxlen == %d, rxqueue == %p", yid->rxlen, yid->rxqueue));
 
	if (yid->rxlen > 0) {
 
		unsigned char *tmp = y_memdup(yid->rxqueue + pos, yid->rxlen);
 
		FREE(yid->rxqueue);
 
		yid->rxqueue = tmp;
 
		DEBUG_MSG(("new rxlen == %d, rxqueue == %p", yid->rxlen,
 
				yid->rxqueue));
 
	} else {
 
		DEBUG_MSG(("freed rxqueue == %p", yid->rxqueue));
 
		FREE(yid->rxqueue);
 
	}
 

	
 
	return server;
 
}
 

	
 
static int yahoo_get_webcam_data(struct yahoo_input_data *yid)
 
{
 
	unsigned char reason = 0;
 
	unsigned int pos = 0;
 
	unsigned int begin = 0;
 
	unsigned int end = 0;
 
	unsigned int closed = 0;
 
	unsigned char header_len = 0;
 
	char *who;
 
	int connect = 0;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	if (!yd)
 
		return -1;
 

	
 
	if (!yid->wcm || !yid->wcd || !yid->rxlen)
 
		return -1;
 

	
 
	DEBUG_MSG(("rxlen is %d", yid->rxlen));
 

	
 
	/* if we are not reading part of image then read header */
 
	if (!yid->wcd->to_read) {
 
		header_len = yid->rxqueue[pos++];
 
		yid->wcd->packet_type = 0;
 

	
 
		if (yid->rxlen < header_len)
 
			return 0;
 

	
 
		if (header_len >= 8) {
 
			reason = yid->rxqueue[pos++];
 
			/* next 2 bytes should always be 05 00 */
 
			pos += 2;
 
			yid->wcd->data_size = yahoo_get32(yid->rxqueue + pos);
 
			pos += 4;
 
			yid->wcd->to_read = yid->wcd->data_size;
 
		}
 
		if (header_len >= 13) {
 
			yid->wcd->packet_type = yid->rxqueue[pos++];
 
			yid->wcd->timestamp = yahoo_get32(yid->rxqueue + pos);
 
			pos += 4;
 
		}
 

	
 
		/* skip rest of header */
 
		pos = header_len;
 
	}
 

	
 
	begin = pos;
 
	pos += yid->wcd->to_read;
 
	if (pos > yid->rxlen)
 
		pos = yid->rxlen;
 

	
 
	/* if it is not an image then make sure we have the whole packet */
 
	if (yid->wcd->packet_type != 0x02) {
 
		if ((pos - begin) != yid->wcd->data_size) {
 
			yid->wcd->to_read = 0;
 
			return 0;
 
		} else {
 
			yahoo_packet_dump(yid->rxqueue + begin, pos - begin);
 
		}
 
	}
 

	
 
	DEBUG_MSG(("packet type %.2X, data length %d", yid->wcd->packet_type,
 
			yid->wcd->data_size));
 

	
 
	/* find out what kind of packet we got */
 
	switch (yid->wcd->packet_type) {
 
	case 0x00:
 
		/* user requests to view webcam (uploading) */
 
		if (yid->wcd->data_size &&
 
			yid->wcm->direction == YAHOO_WEBCAM_UPLOAD) {
 
			end = begin;
 
			while (end <= yid->rxlen && yid->rxqueue[end++] != 13) ;
 
			if (end > begin) {
 
				who = y_memdup(yid->rxqueue + begin,
 
					end - begin);
 
				who[end - begin - 1] = 0;
 
				YAHOO_CALLBACK(ext_yahoo_webcam_viewer) (yd->
 
					client_id, who + 2, 2);
 
				FREE(who);
 
			}
 
		}
 

	
 
		if (yid->wcm->direction == YAHOO_WEBCAM_DOWNLOAD) {
 
			/* timestamp/status field */
 
			/* 0 = declined viewing permission */
 
			/* 1 = accepted viewing permission */
 
			if (yid->wcd->timestamp == 0) {
 
				YAHOO_CALLBACK(ext_yahoo_webcam_closed) (yd->
 
					client_id, yid->wcm->user, 3);
 
			}
 
		}
 
		break;
 
	case 0x01:		/* status packets?? */
 
		/* timestamp contains status info */
 
		/* 00 00 00 01 = we have data?? */
 
		break;
 
	case 0x02:		/* image data */
 
		YAHOO_CALLBACK(ext_yahoo_got_webcam_image) (yd->client_id,
 
			yid->wcm->user, yid->rxqueue + begin,
 
			yid->wcd->data_size, pos - begin, yid->wcd->timestamp);
 
		break;
 
	case 0x05:		/* response packets when uploading */
 
		if (!yid->wcd->data_size) {
 
			YAHOO_CALLBACK(ext_yahoo_webcam_data_request) (yd->
 
				client_id, yid->wcd->timestamp);
 
		}
 
		break;
 
	case 0x07:		/* connection is closing */
 
		switch (reason) {
 
		case 0x01:	/* user closed connection */
 
			closed = 1;
 
			break;
 
		case 0x0F:	/* user cancelled permission */
 
			closed = 2;
 
			break;
 
		}
 
		YAHOO_CALLBACK(ext_yahoo_webcam_closed) (yd->client_id,
 
			yid->wcm->user, closed);
 
		break;
 
	case 0x0C:		/* user connected */
 
	case 0x0D:		/* user disconnected */
 
		if (yid->wcd->data_size) {
 
			who = y_memdup(yid->rxqueue + begin, pos - begin + 1);
 
			who[pos - begin] = 0;
 
			if (yid->wcd->packet_type == 0x0C)
 
				connect = 1;
 
			else
 
				connect = 0;
 
			YAHOO_CALLBACK(ext_yahoo_webcam_viewer) (yd->client_id,
 
				who, connect);
 
			FREE(who);
 
		}
 
		break;
 
	case 0x13:		/* user data */
 
		/* i=user_ip (ip of the user we are viewing) */
 
		/* j=user_ext_ip (external ip of the user we */
 
		/*                are viewing) */
 
		break;
 
	case 0x17:		/* ?? */
 
		break;
 
	}
 
	yid->wcd->to_read -= pos - begin;
 

	
 
	yid->rxlen -= pos;
 
	DEBUG_MSG(("rxlen == %d, rxqueue == %p", yid->rxlen, yid->rxqueue));
 
	if (yid->rxlen > 0) {
 
		unsigned char *tmp = y_memdup(yid->rxqueue + pos, yid->rxlen);
 
		FREE(yid->rxqueue);
 
		yid->rxqueue = tmp;
 
		DEBUG_MSG(("new rxlen == %d, rxqueue == %p", yid->rxlen,
 
				yid->rxqueue));
 
	} else {
 
		DEBUG_MSG(("freed rxqueue == %p", yid->rxqueue));
 
		FREE(yid->rxqueue);
 
	}
 

	
 
	/* If we read a complete packet return success */
 
	if (!yid->wcd->to_read)
 
		return 1;
 

	
 
	return 0;
 
}
 

	
 
int yahoo_write_ready(int id, void *fd, void *data)
 
{
 
	struct yahoo_input_data *yid = data;
 
	int len;
 
	struct data_queue *tx;
 

	
 
	LOG(("write callback: id=%d fd=%p data=%p", id, fd, data));
 
	if (!yid || !yid->txqueues)
 
		return -2;
 

	
 
	tx = yid->txqueues->data;
 
	LOG(("writing %d bytes", tx->len));
 
	len = yahoo_send_data(fd, tx->queue, MIN(1024, tx->len));
 

	
 
	if (len == -1 && errno == EAGAIN)
 
		return 1;
 

	
 
	if (len <= 0) {
 
		int e = errno;
 
		DEBUG_MSG(("len == %d (<= 0)", len));
 
		while (yid->txqueues) {
 
			YList *l = yid->txqueues;
 
			tx = l->data;
 
			free(tx->queue);
 
			free(tx);
 
			yid->txqueues =
 
				y_list_remove_link(yid->txqueues,
 
				yid->txqueues);
 
			y_list_free_1(l);
 
		}
 
		LOG(("yahoo_write_ready(%d, %p) len < 0", id, fd));
 
		YAHOO_CALLBACK(ext_yahoo_remove_handler) (id, yid->write_tag);
 
		yid->write_tag = 0;
 
		errno = e;
 
		return 0;
 
	}
 

	
 
	tx->len -= len;
 
	if (tx->len > 0) {
 
		unsigned char *tmp = y_memdup(tx->queue + len, tx->len);
 
		FREE(tx->queue);
 
		tx->queue = tmp;
 
	} else {
 
		YList *l = yid->txqueues;
 
		free(tx->queue);
 
		free(tx);
 
		yid->txqueues =
 
			y_list_remove_link(yid->txqueues, yid->txqueues);
 
		y_list_free_1(l);
 
		/*
 
		   if(!yid->txqueues) 
 
		   LOG(("yahoo_write_ready(%d, %d) !yxqueues", id, fd));
 
		 */
 
		if (!yid->txqueues) {
 
			LOG(("yahoo_write_ready(%d, %p) !txqueues", id, fd));
 
			YAHOO_CALLBACK(ext_yahoo_remove_handler) (id,
 
				yid->write_tag);
 
			yid->write_tag = 0;
 
		}
 
	}
 

	
 
	return 1;
 
}
 

	
 
static void yahoo_process_pager_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	struct yahoo_packet *pkt;
 
	struct yahoo_data *yd = yid->yd;
 
	int id = yd->client_id;
 

	
 
	if (over)
 
		return;
 

	
 
	while (find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER)
 
		&& (pkt = yahoo_getdata(yid)) != NULL) {
 

	
 
		yahoo_packet_process(yid, pkt);
 

	
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
static void yahoo_process_chatcat_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	if (over)
 
		return;
 

	
 
	if (strstr((char *)yid->rxqueue + (yid->rxlen - 20), "</content>")) {
 
		YAHOO_CALLBACK(ext_yahoo_chat_cat_xml) (yid->yd->client_id,
 
			(char *)yid->rxqueue);
 
	}
 
}
 

	
 
static void yahoo_process_yab_connection(struct yahoo_input_data *yid, int over)
 
{
 
	struct yahoo_data *yd = yid->yd;
 
	struct yab *yab;
 
	YList *buds;
 
	int changed = 0;
 
	int id = yd->client_id;
 
	int yab_used = 0;
 

	
 
	LOG(("Got data for YAB"));
 

	
 
	if (over)
 
		return;
 

	
 
	while (find_input_by_id_and_type(id, YAHOO_CONNECTION_YAB)
 
		&& (yab = yahoo_getyab(yid)) != NULL) {
 
		if (!yab->id)
 
			continue;
 

	
 
		changed = 1;
 
		yab_used = 0;
 
		for (buds = yd->buddies; buds; buds = buds->next) {
 
			struct yahoo_buddy *bud = buds->data;
 
			if (!strcmp(bud->id, yab->id)) {
 
				yab_used = 1;
 
				bud->yab_entry = yab;
 
				if (yab->nname) {
 
					bud->real_name = strdup(yab->nname);
 
				} else if (yab->fname && yab->lname) {
 
					bud->real_name = y_new0(char,
 
						strlen(yab->fname) +
 
						strlen(yab->lname) + 2);
 
					sprintf(bud->real_name, "%s %s",
 
						yab->fname, yab->lname);
 
				} else if (yab->fname) {
 
					bud->real_name = strdup(yab->fname);
 
				}
 
				break;	/* for */
 
			}
 
		}
 

	
 
		if (!yab_used) {
 
			FREE(yab->fname);
 
			FREE(yab->lname);
 
			FREE(yab->nname);
 
			FREE(yab->id);
 
			FREE(yab->email);
 
			FREE(yab->hphone);
 
			FREE(yab->wphone);
 
			FREE(yab->mphone);
 
			FREE(yab);
 
		}
 

	
 
	}
 

	
 
	if (changed)
 
		YAHOO_CALLBACK(ext_yahoo_got_buddies) (yd->client_id,
 
			yd->buddies);
 
}
 

	
 
static void yahoo_process_search_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	struct yahoo_found_contact *yct = NULL;
 
	char *p = (char *)yid->rxqueue, *np, *cp;
 
	int k, n;
 
	int start = 0, found = 0, total = 0;
 
	YList *contacts = NULL;
 
	struct yahoo_input_data *pyid =
 
		find_input_by_id_and_type(yid->yd->client_id,
 
		YAHOO_CONNECTION_PAGER);
 

	
 
	if (!over || !pyid)
 
		return;
 

	
 
	if (p && (p = strstr(p, "\r\n\r\n"))) {
 
		p += 4;
 

	
 
		for (k = 0; (p = strchr(p, 4)) && (k < 4); k++) {
 
			p++;
 
			n = atoi(p);
 
			switch (k) {
 
			case 0:
 
				found = pyid->ys->lsearch_nfound = n;
 
				break;
 
			case 2:
 
				start = pyid->ys->lsearch_nstart = n;
 
				break;
 
			case 3:
 
				total = pyid->ys->lsearch_ntotal = n;
 
				break;
 
			}
 
		}
 

	
 
		if (p)
 
			p++;
 

	
 
		k = 0;
 
		while (p && *p) {
 
			cp = p;
 
			np = strchr(p, 4);
 

	
 
			if (!np)
 
				break;
 
			*np = 0;
 
			p = np + 1;
 

	
 
			switch (k++) {
 
			case 1:
 
				if (strlen(cp) > 2
 
					&& y_list_length(contacts) < total) {
 
					yct = y_new0(struct yahoo_found_contact,
 
						1);
 
					contacts = y_list_append(contacts, yct);
 
					yct->id = cp + 2;
 
				} else {
 
					*p = 0;
 
				}
 
				break;
 
			case 2:
 
				yct->online = !strcmp(cp, "2") ? 1 : 0;
 
				break;
 
			case 3:
 
				yct->gender = cp;
 
				break;
 
			case 4:
 
				yct->age = atoi(cp);
 
				break;
 
			case 5:
 
				/* not worth the context switch for strcmp */
 
				if (cp[0] != '\005' || cp[1] != '\000')
 
					yct->location = cp;
 
				k = 0;
 
				break;
 
			}
 
		}
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_got_search_result) (yid->yd->client_id, found,
 
		start, total, contacts);
 

	
 
	while (contacts) {
 
		YList *node = contacts;
 
		contacts = y_list_remove_link(contacts, node);
 
		free(node->data);
 
		y_list_free_1(node);
 
	}
 
}
 

	
 
static void _yahoo_webcam_connected(void *fd, int error, void *d)
 
{
 
	struct yahoo_input_data *yid = d;
 
	struct yahoo_webcam *wcm = yid->wcm;
 
	struct yahoo_data *yd = yid->yd;
 
	char conn_type[100];
 
	char *data = NULL;
 
	char *packet = NULL;
 
	unsigned char magic_nr[] = { 1, 0, 0, 0, 1 };
 
	unsigned header_len = 0;
 
	unsigned int len = 0;
 
	unsigned int pos = 0;
 

	
 
	if (error || !fd) {
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	yid->fd = fd;
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	LOG(("Connected"));
 
	/* send initial packet */
 
	switch (wcm->direction) {
 
	case YAHOO_WEBCAM_DOWNLOAD:
 
		data = strdup("<REQIMG>");
 
		break;
 
	case YAHOO_WEBCAM_UPLOAD:
 
		data = strdup("<SNDIMG>");
 
		break;
 
	default:
 
		return;
 
	}
 
	yahoo_add_to_send_queue(yid, data, strlen(data));
 
	FREE(data);
 

	
 
	/* send data */
 
	switch (wcm->direction) {
 
	case YAHOO_WEBCAM_DOWNLOAD:
 
		header_len = 8;
 
		data = strdup("a=2\r\nc=us\r\ne=21\r\nu=");
 
		data = y_string_append(data, yd->user);
 
		data = y_string_append(data, "\r\nt=");
 
		data = y_string_append(data, wcm->key);
 
		data = y_string_append(data, "\r\ni=");
 
		data = y_string_append(data, wcm->my_ip);
 
		data = y_string_append(data, "\r\ng=");
 
		data = y_string_append(data, wcm->user);
 
		data = y_string_append(data, "\r\no=w-2-5-1\r\np=");
 
		snprintf(conn_type, sizeof(conn_type), "%d", wcm->conn_type);
 
		data = y_string_append(data, conn_type);
 
		data = y_string_append(data, "\r\n");
 
		break;
 
	case YAHOO_WEBCAM_UPLOAD:
 
		header_len = 13;
 
		data = strdup("a=2\r\nc=us\r\nu=");
 
		data = y_string_append(data, yd->user);
 
		data = y_string_append(data, "\r\nt=");
 
		data = y_string_append(data, wcm->key);
 
		data = y_string_append(data, "\r\ni=");
 
		data = y_string_append(data, wcm->my_ip);
 
		data = y_string_append(data, "\r\no=w-2-5-1\r\np=");
 
		snprintf(conn_type, sizeof(conn_type), "%d", wcm->conn_type);
 
		data = y_string_append(data, conn_type);
 
		data = y_string_append(data, "\r\nb=");
 
		data = y_string_append(data, wcm->description);
 
		data = y_string_append(data, "\r\n");
 
		break;
 
	}
 

	
 
	len = strlen(data);
 
	packet = y_new0(char, header_len + len);
 
	packet[pos++] = header_len;
 
	packet[pos++] = 0;
 
	switch (wcm->direction) {
 
	case YAHOO_WEBCAM_DOWNLOAD:
 
		packet[pos++] = 1;
 
		packet[pos++] = 0;
 
		break;
 
	case YAHOO_WEBCAM_UPLOAD:
 
		packet[pos++] = 5;
 
		packet[pos++] = 0;
 
		break;
 
	}
 

	
 
	pos += yahoo_put32(packet + pos, len);
 
	if (wcm->direction == YAHOO_WEBCAM_UPLOAD) {
 
		memcpy(packet + pos, magic_nr, sizeof(magic_nr));
 
		pos += sizeof(magic_nr);
 
	}
 
	memcpy(packet + pos, data, len);
 
	yahoo_add_to_send_queue(yid, packet, header_len + len);
 
	FREE(packet);
 
	FREE(data);
 

	
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id,
 
		yid->fd, YAHOO_INPUT_READ, yid);
 
}
 

	
 
static void yahoo_webcam_connect(struct yahoo_input_data *y)
 
{
 
	struct yahoo_webcam *wcm = y->wcm;
 
	struct yahoo_input_data *yid;
 
	struct yahoo_server_settings *yss;
 

	
 
	if (!wcm || !wcm->server || !wcm->key)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->type = YAHOO_CONNECTION_WEBCAM;
 
	yid->yd = y->yd;
 

	
 
	/* copy webcam data to new connection */
 
	yid->wcm = y->wcm;
 
	y->wcm = NULL;
 

	
 
	yss = y->yd->server_settings;
 

	
 
	yid->wcd = y_new0(struct yahoo_webcam_data, 1);
 

	
 
	LOG(("Connecting to: %s:%d", wcm->server, wcm->port));
 
	YAHOO_CALLBACK(ext_yahoo_connect_async) (y->yd->client_id, wcm->server,
 
		wcm->port, _yahoo_webcam_connected, yid, 0);
 

	
 
}
 

	
 
static void yahoo_process_webcam_master_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	char *server;
 
	struct yahoo_server_settings *yss;
 

	
 
	if (over)
 
		return;
 

	
 
	server = yahoo_getwebcam_master(yid);
 

	
 
	if (server) {
 
		yss = yid->yd->server_settings;
 
		yid->wcm->server = strdup(server);
 
		yid->wcm->port = yss->webcam_port;
 
		yid->wcm->conn_type = yss->conn_type;
 
		yid->wcm->my_ip = strdup(yss->local_host);
 
		if (yid->wcm->direction == YAHOO_WEBCAM_UPLOAD)
 
			yid->wcm->description = strdup(yss->webcam_description);
 
		yahoo_webcam_connect(yid);
 
		FREE(server);
 
	}
 
}
 

	
 
static void yahoo_process_webcam_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	int id = yid->yd->client_id;
 
	void *fd = yid->fd;
 

	
 
	if (over)
 
		return;
 

	
 
	/* as long as we still have packets available keep processing them */
 
	while (find_input_by_id_and_fd(id, fd)
 
		&& yahoo_get_webcam_data(yid) == 1) ;
 
}
 

	
 
static void yahoo_process_auth_connection(struct yahoo_input_data *yid,
 
	int over)
 
{
 
	char *line_end;
 
	char *token;
 
	char *cookie;
 

	
 
	int error_code = 0;
 
	int is_ymsgr = 0;
 

	
 
	/* Wait till we get everything */
 
	if (!over)
 
		return;
 

	
 
	if (!yid->rxqueue) {
 
		LOG(("Whoops! Closed it just like that??\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	token = strstr((char *)yid->rxqueue, "\r\n\r\n");
 

	
 
	if (!token) {
 
		LOG(("Could not find anything after the HTTP headers? huh?\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	*token = '\0';
 
	token += 4;
 

	
 
	/* Skip the first number */
 
	token = strstr(token, "\r\n");
 

	
 
	if (!token) {
 
		LOG(("Well I tried to skip the first number and found... nothing\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	token += 2;
 

	
 
	line_end = strstr(token, "\r\n");
 

	
 
	if (line_end) {
 
		*line_end = '\0';
 

	
 
		line_end += 2;
 
	}
 

	
 
	error_code = atoi((char *)token);
 

	
 
	switch (error_code) {
 
	case 0:
 
		/* successful */
 
		break;
 
	case 1212:
 
		/* Incorrect ID or password */
 
		LOG(("Incorrect ID or password\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_PASSWD, NULL);
 

	
 
		return;
 
	case 1213:
 
		/* Security lock from too many failed login attempts */
 
		LOG(("Security lock from too many failed login attempts\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_LOCK, "");
 

	
 
		return;
 

	
 
	case 1214:
 
		/* Security lock */
 
		LOG(("Security lock\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_LOCK, "");
 

	
 
		return;
 

	
 
	case 1235:
 
		/* User ID not taken yet */
 
		LOG(("User ID not taken yet\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNAME, NULL);
 

	
 
		return;
 

	
 
	case 1216:
 
		/* Seems to be a lock, but shows the same generic User ID/Password failure */
 
		LOG(("Seems to be a lock, but shows the same generic User ID/Password failure\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_PASSWD, NULL);
 

	
 
		return;
 
	case 100:
 
		/* Username and password cannot be blank */
 
		LOG(("Username and password cannot be blank\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_PASSWD, NULL);
 

	
 
		return;
 
	default:
 
		/* Unknown error code */
 
		LOG(("Unknown Error\n"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 

	
 
		return;
 
	}
 

	
 
	if (line_end && !strncmp(line_end, "ymsgr=", 6)) {
 
		is_ymsgr = 1;
 
	} else if (strncmp(line_end, "crumb=", 6)) {
 
		LOG(("Oops! There was no ymsgr=. Where do I get my token from now :("));
 
		LOG(("I got this:\n\n%s\n", line_end));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
		/* Error */
 
	}
 

	
 
	token = line_end + 6;
 

	
 
	line_end = strstr(token, "\r\n");
 

	
 
	if (line_end) {
 
		*line_end = '\0';
 
		line_end += 2;
 
	}
 

	
 
	/* Go for the crumb */
 
	if (is_ymsgr) {
 
		char url[256];
 
		char *token_enc;
 
		struct yahoo_input_data *crumb_yid =
 
			y_new0(struct yahoo_input_data, 1);
 

	
 
		crumb_yid->yd = yid->yd;
 
		crumb_yid->type = YAHOO_CONNECTION_AUTH;
 

	
 
		inputs = y_list_prepend(inputs, crumb_yid);
 

	
 
		token_enc = yahoo_urlencode(token);
 

	
 
		snprintf(url, sizeof(url),
 
			"https://login.yahoo.com/config/pwtoken_login?src=ymsgr&ts="
 
			"&token=%s", token_enc);
 

	
 
		yahoo_http_get(crumb_yid->yd->client_id, url, NULL, 1, 0,
 
			_yahoo_http_connected, crumb_yid);
 

	
 
		FREE(token_enc);
 

	
 
		return;
 
	}
 

	
 
	/* token is actually crumb */
 

	
 
	if (!line_end) {
 
		/* We did not get our cookies. Cry. */
 
		LOG(("NO Cookies!"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	cookie = strstr((char *)yid->rxqueue, "Set-Cookie: Y=");
 

	
 
	if (!cookie) {
 
		/* Cry. */
 
		LOG(("NO Y Cookie!"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	cookie += 14;
 

	
 
	line_end = strstr(cookie, "\r\n");
 
	*line_end = '\0';
 

	
 
	yid->yd->cookie_y = strdup(cookie);
 
	*line_end = ';';
 

	
 
	cookie = strstr((char *)yid->rxqueue, "Set-Cookie: T=");
 
	if (!cookie) {
 
		/* Cry. */
 
		LOG(("NO T Cookie!"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	cookie += 14;
 

	
 
	line_end = strstr(cookie, "\r\n");
 
	*line_end = '\0';
 

	
 
	yid->yd->cookie_t = strdup(cookie);
 
	*line_end = ';';
 

	
 
	cookie = strstr((char *)yid->rxqueue, "Set-Cookie: B=");
 
	if (!cookie) {
 
		/* Cry. */
 
		LOG(("NO B Cookie!"));
 
		YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->client_id,
 
			YAHOO_LOGIN_UNKNOWN, NULL);
 
		return;
 
	}
 

	
 
	cookie += 14;
 

	
 
	line_end = strstr(cookie, "\r\n");
 
	*line_end = '\0';
 

	
 
	yid->yd->cookie_b = strdup(cookie);
 

	
 
	yid->yd->crumb = strdup(token);
 

	
 
	yahoo_send_auth(yid->yd);
 
}
 

	
 
static void (*yahoo_process_connection[]) (struct yahoo_input_data *,
 
	int over) = {
 
yahoo_process_pager_connection, yahoo_process_ft_connection,
 
		yahoo_process_yab_connection,
 
		yahoo_process_webcam_master_connection,
 
		yahoo_process_webcam_connection,
 
		yahoo_process_chatcat_connection,
 
		yahoo_process_search_connection, yahoo_process_auth_connection};
 

	
 
int yahoo_read_ready(int id, void *fd, void *data)
 
{
 
	struct yahoo_input_data *yid = data;
 
	char buf[1024];
 
	int len;
 

	
 
	LOG(("read callback: id=%d fd=%p data=%p", id, fd, data));
 
	if (!yid)
 
		return -2;
 

	
 
	do {
 
		len = YAHOO_CALLBACK(ext_yahoo_read) (fd, buf, sizeof(buf));
 
	} while (len == -1 && errno == EINTR);
 

	
 
	if (len == -1 && (errno == EAGAIN || errno == EINTR))	/* we'll try again later */
 
		return 1;
 

	
 
	if (len <= 0) {
 
		int e = errno;
 
		DEBUG_MSG(("len == %d (<= 0)", len));
 

	
 
		if (yid->type == YAHOO_CONNECTION_PAGER) {
 
			YAHOO_CALLBACK(ext_yahoo_login_response) (yid->yd->
 
				client_id, YAHOO_LOGIN_SOCK, NULL);
 
		}
 

	
 
		yahoo_process_connection[yid->type] (yid, 1);
 
		yahoo_input_close(yid);
 

	
 
		/* no need to return an error, because we've already fixed it */
 
		if (len == 0)
 
			return 1;
 

	
 
		errno = e;
 
		LOG(("read error: %s", strerror(errno)));
 
		return -1;
 
	}
 

	
 
	yid->rxqueue =
 
		y_renew(unsigned char, yid->rxqueue, len + yid->rxlen + 1);
 
	memcpy(yid->rxqueue + yid->rxlen, buf, len);
 
	yid->rxlen += len;
 
	yid->rxqueue[yid->rxlen] = 0;
 

	
 
	yahoo_process_connection[yid->type] (yid, 0);
 

	
 
	return len;
 
}
 

	
 
int yahoo_init_with_attributes(const char *username, const char *password, ...)
 
{
 
	va_list ap;
 
	struct yahoo_data *yd;
 

	
 
	yd = y_new0(struct yahoo_data, 1);
 

	
 
	if (!yd)
 
		return 0;
 

	
 
	yd->user = strdup(username);
 
	yd->password = strdup(password);
 

	
 
	yd->initial_status = -1;
 
	yd->current_status = -1;
 

	
 
	yd->client_id = ++last_id;
 

	
 
	add_to_list(yd);
 

	
 
	va_start(ap, password);
 
	yd->server_settings = _yahoo_assign_server_settings(ap);
 
	va_end(ap);
 

	
 
	return yd->client_id;
 
}
 

	
 
int yahoo_init(const char *username, const char *password)
 
{
 
	return yahoo_init_with_attributes(username, password, NULL);
 
}
 

	
 
static void yahoo_connected(void *fd, int error, void *data)
 
{
 
	struct connect_callback_data *ccd = data;
 
	struct yahoo_data *yd = ccd->yd;
 
	struct yahoo_packet *pkt;
 
	struct yahoo_input_data *yid;
 
	struct yahoo_server_settings *yss = yd->server_settings;
 

	
 
	if (error) {
 
		int tag;
 
		if (fallback_ports[ccd->i]) {
 
			char *host = yss->pager_host;
 

	
 
			if (!host)
 
				host = yss->pager_host_list[ccd->server_i];
 

	
 
			yss->pager_port = fallback_ports[ccd->i++];
 
			tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->
 
				client_id, host, yss->pager_port,
 
				yahoo_connected, ccd, 0);
 

	
 
			if (tag > 0)
 
				ccd->tag = tag;
 
		} else if (yss->pager_host_list
 
				&& yss->pager_host_list[ccd->server_i]) {
 

	
 
			/* Get back to the default port */
 
			yss->pager_port = pager_port;
 
			ccd->server_i++;
 
			LOG(("Fallback: Connecting to %s:%d", yss->pager_host_list[ccd->server_i], yss->pager_port));
 

	
 
			ccd->i = 0;
 
			tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->client_id,
 
				yss->pager_host_list[ccd->server_i], yss->pager_port,
 
				yahoo_connected, ccd, 0);
 
		} else {
 
			FREE(ccd);
 
			YAHOO_CALLBACK(ext_yahoo_login_response) (yd->client_id,
 
				YAHOO_LOGIN_SOCK, NULL);
 
		}
 
		return;
 
	}
 

	
 
	FREE(ccd);
 

	
 
	/* fd == NULL && error == 0 means connect was cancelled */
 
	if (!fd)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_AUTH, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	NOTICE(("Sending initial packet"));
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 

	
 
	yid = find_input_by_id_and_type(yd->client_id, YAHOO_CONNECTION_PAGER);
 
	yid->fd = fd;
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id,
 
		yid->fd, YAHOO_INPUT_READ, yid);
 
}
 

	
 
void *yahoo_get_fd(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	if (!yid)
 
		return 0;
 
	else
 
		return yid->fd;
 
}
 

	
 
void yahoo_send_buzz(int id, const char *from, const char *who)
 
{
 
	yahoo_send_im(id, from, who, "<ding>", 1, 0);
 
}
 

	
 
void yahoo_send_im(int id, const char *from, const char *who, const char *what,
 
	int utf8, int picture)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_packet *pkt = NULL;
 
	struct yahoo_data *yd;
 
	char pic_str[10];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_MESSAGE, YAHOO_STATUS_OFFLINE,
 
		yd->session_id);
 

	
 
	snprintf(pic_str, sizeof(pic_str), "%d", picture);
 

	
 
	if (from && strcmp(from, yd->user))
 
		yahoo_packet_hash(pkt, 0, yd->user);
 
	yahoo_packet_hash(pkt, 1, from ? from : yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 14, what);
 

	
 
	if (utf8)
 
		yahoo_packet_hash(pkt, 97, "1");
 

	
 
	yahoo_packet_hash(pkt, 63, ";0");	/* imvironment name; or ;0 */
 
	yahoo_packet_hash(pkt, 64, "0");
 
	yahoo_packet_hash(pkt, 206, pic_str);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_typing(int id, const char *from, const char *who, int typ)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_NOTIFY, YPACKET_STATUS_NOTIFY,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 1, from ? from : yd->user);
 
	yahoo_packet_hash(pkt, 14, " ");
 
	yahoo_packet_hash(pkt, 13, typ ? "1" : "0");
 
	yahoo_packet_hash(pkt, 49, "TYPING");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_set_away(int id, enum yahoo_status state, const char *msg, int away)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 
	int old_status;
 
	char s[4];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	old_status = yd->current_status;
 

	
 
	if (msg) {
 
		yd->current_status = YAHOO_STATUS_CUSTOM;
 
	} else {
 
		yd->current_status = state;
 
	}
 

	
 
	/* Thank you libpurple :) */
 
	if (yd->current_status == YAHOO_STATUS_INVISIBLE) {
 
		pkt = yahoo_packet_new(YAHOO_SERVICE_Y6_VISIBLE_TOGGLE,
 
			YAHOO_STATUS_AVAILABLE, 0);
 
		yahoo_packet_hash(pkt, 13, "2");
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 

	
 
		return;
 
	}
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y6_STATUS_UPDATE,
 
		yd->current_status, yd->session_id);
 
	snprintf(s, sizeof(s), "%d", yd->current_status);
 
	yahoo_packet_hash(pkt, 10, s);
 

	
 
	if (yd->current_status == YAHOO_STATUS_CUSTOM) {
 
		yahoo_packet_hash(pkt, 19, msg);
 
	} else {
 
		yahoo_packet_hash(pkt, 19, "");
 
	}
 

	
 
	yahoo_packet_hash(pkt, 47, (away == 2) ? "2" : (away) ? "1" : "0");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 

	
 
	if (old_status == YAHOO_STATUS_INVISIBLE) {
 
		pkt = yahoo_packet_new(YAHOO_SERVICE_Y6_VISIBLE_TOGGLE,
 
			YAHOO_STATUS_AVAILABLE, 0);
 
		yahoo_packet_hash(pkt, 13, "1");
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
void yahoo_logoff(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	LOG(("yahoo_logoff: current status: %d", yd->current_status));
 

	
 
	if (yd->current_status != -1) {
 
		pkt = yahoo_packet_new(YAHOO_SERVICE_LOGOFF,
 
			YPACKET_STATUS_DEFAULT, yd->session_id);
 
		yd->current_status = -1;
 

	
 
		if (pkt) {
 
			yahoo_send_packet(yid, pkt, 0);
 
			yahoo_packet_free(pkt);
 
		}
 
	}
 

	
 
/*	do {
 
		yahoo_input_close(yid);
 
	} while((yid = find_input_by_id(id)));*/
 

	
 
}
 

	
 
void yahoo_get_list(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_LIST, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	if (pkt) {
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
static void _yahoo_http_connected(int id, void *fd, int error, void *data)
 
{
 
	struct yahoo_input_data *yid = data;
 
	if (fd == NULL || error) {
 
		inputs = y_list_remove(inputs, yid);
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	yid->fd = fd;
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id, fd,
 
		YAHOO_INPUT_READ, yid);
 
}
 

	
 
/* FIXME Get address book from address.yahoo.com instead */
 
void yahoo_get_yab(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct yahoo_input_data *yid;
 
	char url[1024];
 
	char buff[2048];
 

	
 
	if (!yd)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_YAB;
 

	
 
	LOG(("Sending request for Address Book"));
 

	
 
	snprintf(url, 1024,
 
		"http://address.yahoo.com/yab/us?v=XM&prog=ymsgr&.intl=us"
 
		"&diffs=1&t=0&tags=short&rt=0&prog-ver=8.1.0.249&useutf8=1&legenc=codepage-1252");
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	yahoo_http_get(yid->yd->client_id, url, buff, 0, 0,
 
		_yahoo_http_connected, yid);
 
}
 

	
 
struct yahoo_post_data {
 
	struct yahoo_input_data *yid;
 
	char *data;
 
};
 

	
 
static void _yahoo_http_post_connected(int id, void *fd, int error, void *data)
 
{
 
	struct yahoo_post_data *yad = data;
 
	struct yahoo_input_data *yid = yad->yid;
 
	char *buff = yad->data;
 

	
 
	if (!fd) {
 
		inputs = y_list_remove(inputs, yid);
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	YAHOO_CALLBACK(ext_yahoo_write) (fd, buff, strlen(buff));
 

	
 
	yid->fd = fd;
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id, fd,
 
		YAHOO_INPUT_READ, yid);
 

	
 
	FREE(buff);
 
	FREE(yad);
 
}
 

	
 
/* FIXME This is also likely affected */
 
void yahoo_set_yab(int id, struct yab *yab)
 
{
 
	struct yahoo_post_data *yad = y_new0(struct yahoo_post_data, 1);
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct yahoo_input_data *yid;
 
	char url[1024];
 
	char buff[1024];
 
	char post[1024];
 
	int size = 0;
 

	
 
	if (!yd)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->type = YAHOO_CONNECTION_YAB;
 
	yid->yd = yd;
 

	
 
	if(yab->yid)
 
		size = snprintf(post, sizeof(post), "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 
			"<ab k=\"%s\" cc=\"%d\">"
 
			"<ct id=\"%d\" e=\"1\" yi=\"%s\" nn=\"%s\" />"
 
			"</ab>", yd->user, 9, yab->yid,	/* Don't know why */
 
			yab->id, yab->nname?yab->nname:"");
 
	else
 
		size = snprintf(post, sizeof(post), "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 
			"<ab k=\"%s\" cc=\"%d\">"
 
			"<ct a=\"1\" yi=\"%s\" nn=\"%s\" />"
 
			"</ab>", yd->user, 1,	/* Don't know why */
 
			yab->id, yab->nname?yab->nname:"");
 

	
 
	yad->yid = yid;
 
	yad->data = strdup(post);
 

	
 
	strcpy(url, "http://address.yahoo.com/yab/us?v=XM&prog=ymsgr&.intl=us"
 
		"&sync=1&tags=short&noclear=1&useutf8=1&legenc=codepage-1252");
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	yahoo_http_post(yid->yd->client_id, url, buff, size,
 
		_yahoo_http_post_connected, yad);
 
}
 

	
 
void yahoo_set_identity_status(int id, const char *identity, int active)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(active ? YAHOO_SERVICE_IDACT :
 
		YAHOO_SERVICE_IDDEACT, YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 3, identity);
 
	if (pkt) {
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
void yahoo_refresh(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_USERSTAT, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	if (pkt) {
 
		yahoo_send_packet(yid, pkt, 0);
 
		yahoo_packet_free(pkt);
 
	}
 
}
 

	
 
void yahoo_keepalive(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PING, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_chat_keepalive(int id)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CHATPING, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_add_buddy(int id, const char *who, const char *group,
 
	const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	if (!yd->logged_in)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_ADDBUDDY, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	if (msg != NULL)	/* add message/request "it's me add me" */
 
		yahoo_packet_hash(pkt, 14, msg);
 
	else
 
		yahoo_packet_hash(pkt, 14, "");
 
	yahoo_packet_hash(pkt, 65, group);
 
	yahoo_packet_hash(pkt, 97, "1");
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 302, "319");
 
	yahoo_packet_hash(pkt, 300, "319");
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 334, "0");
 
	yahoo_packet_hash(pkt, 301, "319");
 
	yahoo_packet_hash(pkt, 303, "319");
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_remove_buddy(int id, const char *who, const char *group)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_REMBUDDY, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 65, group);
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_confirm_buddy(int id, const char *who, int reject, const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	if (!yd->logged_in)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_AUTHORIZATION,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	if (reject)
 
		yahoo_packet_hash(pkt, 13, "2");
 
	else {
 
		yahoo_packet_hash(pkt, 241, "0");
 
		yahoo_packet_hash(pkt, 13, "1");
 
	}
 

	
 
	yahoo_packet_hash(pkt, 334, "0");
 

	
 
	if (reject) {
 
		yahoo_packet_hash(pkt, 14, msg ? msg : "");
 
		yahoo_packet_hash(pkt, 97, "1");
 
	}
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_ignore_buddy(int id, const char *who, int unignore)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	if (!yd->logged_in)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_IGNORECONTACT,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 13, unignore ? "2" : "1");
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_stealth_buddy(int id, const char *who, int unstealth)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	if (!yd->logged_in)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_STEALTH_PERM,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 31, unstealth ? "2" : "1");
 
	yahoo_packet_hash(pkt, 13, "2");
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_change_buddy_group(int id, const char *who, const char *old_group,
 
	const char *new_group)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_CHANGE_GROUP,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 302, "240");
 
	yahoo_packet_hash(pkt, 300, "240");
 
	yahoo_packet_hash(pkt, 7, who);
 
	yahoo_packet_hash(pkt, 224, old_group);
 
	yahoo_packet_hash(pkt, 264, new_group);
 
	yahoo_packet_hash(pkt, 301, "240");
 
	yahoo_packet_hash(pkt, 303, "240");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_group_rename(int id, const char *old_group, const char *new_group)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt = NULL;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_GROUPRENAME,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 65, old_group);
 
	yahoo_packet_hash(pkt, 67, new_group);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_addinvite(int id, const char *from, const char *who,
 
	const char *room, const YList *members, const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFADDINVITE,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 51, who);
 
	yahoo_packet_hash(pkt, 57, room);
 
	yahoo_packet_hash(pkt, 58, msg);
 
	yahoo_packet_hash(pkt, 13, "0");
 
	for (; members; members = members->next) {
 
		yahoo_packet_hash(pkt, 52, (char *)members->data);
 
		yahoo_packet_hash(pkt, 53, (char *)members->data);
 
	}
 
	/* 52, 53 -> other members? */
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_invite(int id, const char *from, YList *who,
 
	const char *room, const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFINVITE, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 50, yd->user);
 
	for (; who; who = who->next) {
 
		yahoo_packet_hash(pkt, 52, (char *)who->data);
 
	}
 
	yahoo_packet_hash(pkt, 57, room);
 
	yahoo_packet_hash(pkt, 58, msg);
 
	yahoo_packet_hash(pkt, 13, "0");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_logon(int id, const char *from, YList *who,
 
	const char *room)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFLOGON, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 3, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 57, room);
 
	for (; who; who = who->next)
 
		yahoo_packet_hash(pkt, 3, (char *)who->data);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_decline(int id, const char *from, YList *who,
 
	const char *room, const char *msg)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFDECLINE,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 3, (from ? from : yd->user));
 
	for (; who; who = who->next)
 
		yahoo_packet_hash(pkt, 3, (char *)who->data);
 
	yahoo_packet_hash(pkt, 57, room);
 
	yahoo_packet_hash(pkt, 14, msg);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_logoff(int id, const char *from, YList *who,
 
	const char *room)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFLOGOFF, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 3, (from ? from : yd->user));
 
	for (; who; who = who->next)
 
		yahoo_packet_hash(pkt, 3, (char *)who->data);
 

	
 
	yahoo_packet_hash(pkt, 57, room);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_conference_message(int id, const char *from, YList *who,
 
	const char *room, const char *msg, int utf8)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CONFMSG, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 53, (from ? from : yd->user));
 
	for (; who; who = who->next)
 
		yahoo_packet_hash(pkt, 53, (char *)who->data);
 

	
 
	yahoo_packet_hash(pkt, 57, room);
 
	yahoo_packet_hash(pkt, 14, msg);
 

	
 
	if (utf8)
 
		yahoo_packet_hash(pkt, 97, "1");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_get_chatrooms(int id, int chatroomid)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct yahoo_input_data *yid;
 
	char url[1024];
 
	char buff[1024];
 

	
 
	if (!yd)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_CHATCAT;
 

	
 
	if (chatroomid == 0) {
 
		snprintf(url, 1024,
 
			"http://insider.msg.yahoo.com/ycontent/?chatcat=0");
 
	} else {
 
		snprintf(url, 1024,
 
			"http://insider.msg.yahoo.com/ycontent/?chatroom_%d=0",
 
			chatroomid);
 
	}
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	inputs = y_list_prepend(inputs, yid);
 

	
 
	yahoo_http_get(yid->yd->client_id, url, buff, 0, 0,
 
		_yahoo_http_connected, yid);
 
}
 

	
 
void yahoo_chat_logon(int id, const char *from, const char *room,
 
	const char *roomid)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CHATONLINE, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 109, yd->user);
 
	yahoo_packet_hash(pkt, 6, "abcde");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CHATJOIN, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 104, room);
 
	yahoo_packet_hash(pkt, 129, roomid);
 
	yahoo_packet_hash(pkt, 62, "2");	/* ??? */
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_chat_message(int id, const char *from, const char *room,
 
	const char *msg, const int msgtype, const int utf8)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 
	char buf[2];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_COMMENT, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 
	yahoo_packet_hash(pkt, 104, room);
 
	yahoo_packet_hash(pkt, 117, msg);
 

	
 
	snprintf(buf, sizeof(buf), "%d", msgtype);
 
	yahoo_packet_hash(pkt, 124, buf);
 

	
 
	if (utf8)
 
		yahoo_packet_hash(pkt, 97, "1");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_chat_logoff(int id, const char *from)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_CHATLOGOUT, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, (from ? from : yd->user));
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_buddyicon_request(int id, const char *who)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 13, "1");
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_picture_info(int id, const char *who, const char *url,
 
	int checksum)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 
	char checksum_str[10];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	snprintf(checksum_str, sizeof(checksum_str), "%d", checksum);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YPACKET_STATUS_DEFAULT,
 
		0);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 4, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 13, "2");
 
	yahoo_packet_hash(pkt, 20, url);
 
	yahoo_packet_hash(pkt, 192, checksum_str);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_picture_update(int id, const char *who, int type)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 
	char type_str[10];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	snprintf(type_str, sizeof(type_str), "%d", type);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE_UPDATE,
 
		YPACKET_STATUS_DEFAULT, 0);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 206, type_str);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_picture_checksum(int id, const char *who, int checksum)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 
	char checksum_str[10];
 

	
 
	if (!yid)
 
		return;
 

	
 
	yd = yid->yd;
 

	
 
	snprintf(checksum_str, sizeof(checksum_str), "%d", checksum);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE_CHECKSUM,
 
		YPACKET_STATUS_DEFAULT, 0);
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	if (who != 0)
 
		yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 192, checksum_str);
 
	yahoo_packet_hash(pkt, 212, "1");
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_webcam_close_feed(int id, const char *who)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_webcam_user(id, who);
 

	
 
	if (yid)
 
		yahoo_input_close(yid);
 
}
 

	
 
void yahoo_webcam_get_feed(int id, const char *who)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_data *yd;
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	/* 
 
	 * add the user to the queue.  this is a dirty hack, since
 
	 * the yahoo server doesn't tell us who's key it's returning,
 
	 * we have to just hope that it sends back keys in the same 
 
	 * order that we request them.
 
	 * The queue is popped in yahoo_process_webcam_key
 
	 */
 
	webcam_queue = y_list_append(webcam_queue, who ? strdup(who) : NULL);
 

	
 
	yd = yid->yd;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_WEBCAM, YPACKET_STATUS_DEFAULT,
 
		yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	if (who != NULL)
 
		yahoo_packet_hash(pkt, 5, who);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_webcam_send_image(int id, unsigned char *image, unsigned int length,
 
	unsigned int timestamp)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_WEBCAM);
 
	unsigned char *packet;
 
	unsigned char header_len = 13;
 
	unsigned int pos = 0;
 

	
 
	if (!yid)
 
		return;
 

	
 
	packet = y_new0(unsigned char, header_len);
 

	
 
	packet[pos++] = header_len;
 
	packet[pos++] = 0;
 
	packet[pos++] = 5;	/* version byte?? */
 
	packet[pos++] = 0;
 
	pos += yahoo_put32(packet + pos, length);
 
	packet[pos++] = 2;	/* packet type, image */
 
	pos += yahoo_put32(packet + pos, timestamp);
 
	yahoo_add_to_send_queue(yid, packet, header_len);
 
	FREE(packet);
 

	
 
	if (length)
 
		yahoo_add_to_send_queue(yid, image, length);
 
}
 

	
 
void yahoo_webcam_accept_viewer(int id, const char *who, int accept)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_WEBCAM);
 
	char *packet = NULL;
 
	char *data = NULL;
 
	unsigned char header_len = 13;
 
	unsigned int pos = 0;
 
	unsigned int len = 0;
 

	
 
	if (!yid)
 
		return;
 

	
 
	data = strdup("u=");
 
	data = y_string_append(data, (char *)who);
 
	data = y_string_append(data, "\r\n");
 
	len = strlen(data);
 

	
 
	packet = y_new0(char, header_len + len);
 
	packet[pos++] = header_len;
 
	packet[pos++] = 0;
 
	packet[pos++] = 5;	/* version byte?? */
 
	packet[pos++] = 0;
 
	pos += yahoo_put32(packet + pos, len);
 
	packet[pos++] = 0;	/* packet type */
 
	pos += yahoo_put32(packet + pos, accept);
 
	memcpy(packet + pos, data, len);
 
	FREE(data);
 
	yahoo_add_to_send_queue(yid, packet, header_len + len);
 
	FREE(packet);
 
}
 

	
 
void yahoo_webcam_invite(int id, const char *who)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_packet *pkt;
 

	
 
	if (!yid)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_NOTIFY, YPACKET_STATUS_NOTIFY,
 
		yid->yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 49, "WEBCAMINVITE");
 
	yahoo_packet_hash(pkt, 14, " ");
 
	yahoo_packet_hash(pkt, 13, "0");
 
	yahoo_packet_hash(pkt, 1, yid->yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
static void yahoo_search_internal(int id, int t, const char *text, int g,
 
	int ar, int photo, int yahoo_only, int startpos, int total)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	struct yahoo_input_data *yid;
 
	char url[1024];
 
	char buff[1024];
 
	char *ctext, *p;
 

	
 
	if (!yd)
 
		return;
 

	
 
	yid = y_new0(struct yahoo_input_data, 1);
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_SEARCH;
 

	
 
	/*
 
	   age range
 
	   .ar=1 - 13-18, 2 - 18-25, 3 - 25-35, 4 - 35-50, 5 - 50-70, 6 - 70+
 
	 */
 

	
 
	snprintf(buff, sizeof(buff), "&.sq=%%20&.tt=%d&.ss=%d", total,
 
		startpos);
 

	
 
	ctext = strdup(text);
 
	while ((p = strchr(ctext, ' ')))
 
		*p = '+';
 

	
 
	snprintf(url, 1024,
 
		"http://members.yahoo.com/interests?.oc=m&.kw=%s&.sb=%d&.g=%d&.ar=0%s%s%s",
 
		ctext, t, g, photo ? "&.p=y" : "", yahoo_only ? "&.pg=y" : "",
 
		startpos ? buff : "");
 

	
 
	FREE(ctext);
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	inputs = y_list_prepend(inputs, yid);
 
	yahoo_http_get(yid->yd->client_id, url, buff, 0, 0,
 
		_yahoo_http_connected, yid);
 
}
 

	
 
void yahoo_search(int id, enum yahoo_search_type t, const char *text,
 
	enum yahoo_search_gender g, enum yahoo_search_agerange ar, int photo,
 
	int yahoo_only)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_search_state *yss;
 

	
 
	if (!yid)
 
		return;
 

	
 
	if (!yid->ys)
 
		yid->ys = y_new0(struct yahoo_search_state, 1);
 

	
 
	yss = yid->ys;
 

	
 
	FREE(yss->lsearch_text);
 
	yss->lsearch_type = t;
 
	yss->lsearch_text = strdup(text);
 
	yss->lsearch_gender = g;
 
	yss->lsearch_agerange = ar;
 
	yss->lsearch_photo = photo;
 
	yss->lsearch_yahoo_only = yahoo_only;
 

	
 
	yahoo_search_internal(id, t, text, g, ar, photo, yahoo_only, 0, 0);
 
}
 

	
 
void yahoo_search_again(int id, int start)
 
{
 
	struct yahoo_input_data *yid =
 
		find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	struct yahoo_search_state *yss;
 

	
 
	if (!yid || !yid->ys)
 
		return;
 

	
 
	yss = yid->ys;
 

	
 
	if (start == -1)
 
		start = yss->lsearch_nstart + yss->lsearch_nfound;
 

	
 
	yahoo_search_internal(id, yss->lsearch_type, yss->lsearch_text,
 
		yss->lsearch_gender, yss->lsearch_agerange,
 
		yss->lsearch_photo, yss->lsearch_yahoo_only,
 
		start, yss->lsearch_ntotal);
 
}
 

	
 
void yahoo_send_picture(int id, const char *name, unsigned long size,
 
	yahoo_get_fd_callback callback, void *data)
 
{
 
	/* Not Implemented */
 
}
 

	
 
/* File Transfer */
 
static YList *active_file_transfers = NULL;
 

	
 
enum {
 
	FT_STATE_HEAD = 1,
 
	FT_STATE_RECV,
 
	FT_STATE_RECV_START,
 
	FT_STATE_SEND
 
};
 

	
 
struct send_file_data {
 
	int client_id;
 
	char *id;
 
	char *who;
 
	char *filename;
 
	char *ip_addr;
 
	char *token;
 
	int size;
 

	
 
	struct yahoo_input_data *yid;
 
	int state;
 

	
 
	yahoo_get_fd_callback callback;
 
	void *data;
 
};
 

	
 
static char *yahoo_get_random(void)
 
{
 
	int i = 0;
 
	int r = 0;
 
	int c = 0;
 
	char out[25];
 

	
 
	out[24] = '\0';
 
	out[23] = '$';
 
	out[22] = '$';
 
	
 
	for (i = 0; i < 22; i++) {
 
		if(r == 0)
 
			r = rand();
 

	
 
		c = r%61;
 

	
 
		if(c<26)
 
			out[i] = c + 'a';
 
		else if (c<52)
 
			out[i] = c - 26 + 'A';
 
		else
 
			out[i] = c - 52 + '0';
 

	
 
		r /= 61;
 
	}
 

	
 
	return strdup(out);
 
}
 

	
 
static int _are_same_id(const void *sfd1, const void *id)
 
{
 
	return strcmp(((struct send_file_data *)sfd1)->id, (char *)id);
 
}
 

	
 
static int _are_same_yid(const void *sfd1, const void *yid)
 
{
 
	if(((struct send_file_data *)sfd1)->yid == yid)
 
		return 0;
 
	else
 
		return 1;
 
}
 

	
 
static struct send_file_data *yahoo_get_active_transfer(char *id)
 
{
 
	YList *l = y_list_find_custom(active_file_transfers, id,
 
		_are_same_id);
 

	
 
	if(l)
 
		return (struct send_file_data *)l->data;
 
	
 
	return NULL;
 
}
 

	
 
static struct send_file_data *yahoo_get_active_transfer_with_yid(void *yid)
 
{
 
	YList *l = y_list_find_custom(active_file_transfers, yid,
 
		_are_same_yid);
 

	
 
	if(l)
 
		return (struct send_file_data *)l->data;
 
	
 
	return NULL;
 
}
 

	
 
static void yahoo_add_active_transfer(struct send_file_data *sfd)
 
{
 
	active_file_transfers = y_list_prepend(active_file_transfers, sfd);
 
}
 

	
 
static void yahoo_remove_active_transfer(struct send_file_data *sfd)
 
{
 
	active_file_transfers = y_list_remove(active_file_transfers, sfd);
 
	free(sfd->id);
 
	free(sfd->who);
 
	free(sfd->filename);
 
	free(sfd->ip_addr);
 
	FREE(sfd);
 
}
 

	
 
static void _yahoo_ft_upload_connected(int id, void *fd, int error, void *data)
 
{
 
	struct send_file_data *sfd = data;
 
	struct yahoo_input_data *yid = sfd->yid;
 

	
 
	if (!fd) {
 
		inputs = y_list_remove(inputs, yid);
 
		FREE(yid);
 
		return;
 
	}
 

	
 
	sfd->callback(id, fd, error, sfd->data);
 

	
 
	yid->fd = fd;
 
	yid->read_tag =
 
		YAHOO_CALLBACK(ext_yahoo_add_handler) (yid->yd->client_id, fd,
 
		YAHOO_INPUT_READ, yid);
 
}
 

	
 
static void yahoo_file_transfer_upload(struct yahoo_data *yd,
 
	struct send_file_data *sfd)
 
{
 
	char url[256];
 
	char buff[4096];
 
	char *sender_enc = NULL, *recv_enc = NULL, *token_enc = NULL;
 

	
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 

	
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_FT;
 

	
 
	inputs = y_list_prepend(inputs, yid);
 
	sfd->yid = yid;
 
	sfd->state = FT_STATE_SEND;
 

	
 
	token_enc = yahoo_urlencode(sfd->token);
 
	sender_enc = yahoo_urlencode(yd->user);
 
	recv_enc = yahoo_urlencode(sfd->who);
 

	
 
	snprintf(url, sizeof(url), 
 
		"http://%s/relay?token=%s&sender=%s&recver=%s", sfd->ip_addr,
 
		token_enc, sender_enc, recv_enc);
 

	
 
	snprintf(buff, sizeof(buff), "T=%s; Y=%s", yd->cookie_t, yd->cookie_y);
 

	
 
	yahoo_http_post(yd->client_id, url, buff, sfd->size,
 
		_yahoo_ft_upload_connected, sfd);
 

	
 
	FREE(token_enc);
 
	FREE(sender_enc);
 
	FREE(recv_enc);
 
}
 

	
 
static void yahoo_init_ft_recv(struct yahoo_data *yd,
 
	struct send_file_data *sfd)
 
{
 
	char url[256];
 
	char buff[1024];
 
	char *sender_enc = NULL, *recv_enc = NULL, *token_enc = NULL;
 

	
 
	struct yahoo_input_data *yid = y_new0(struct yahoo_input_data, 1);
 

	
 
	yid->yd = yd;
 
	yid->type = YAHOO_CONNECTION_FT;
 

	
 
	inputs = y_list_prepend(inputs, yid);
 
	sfd->yid = yid;
 
	sfd->state = FT_STATE_HEAD;
 

	
 
	token_enc = yahoo_urlencode(sfd->token);
 
	sender_enc = yahoo_urlencode(sfd->who);
 
	recv_enc = yahoo_urlencode(yd->user);
 

	
 
	snprintf(url, sizeof(url), 
 
		"http://%s/relay?token=%s&sender=%s&recver=%s", sfd->ip_addr,
 
		token_enc, sender_enc, recv_enc);
 

	
 
	snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
 

	
 
	yahoo_http_head(yid->yd->client_id, url, buff, 0, NULL,
 
		_yahoo_http_connected, yid);
 

	
 
	FREE(token_enc);
 
	FREE(sender_enc);
 
	FREE(recv_enc);
 
}
 

	
 
static void yahoo_file_transfer_accept(struct yahoo_input_data *yid,
 
	struct send_file_data *sfd)
 
{
 
	struct yahoo_packet *pkt;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_FILETRANSFERACCEPT,
 
		YPACKET_STATUS_DEFAULT, yid->yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yid->yd->user);
 
	yahoo_packet_hash(pkt, 5, sfd->who);
 
	yahoo_packet_hash(pkt, 265, sfd->id);
 
	yahoo_packet_hash(pkt, 27, sfd->filename);
 
	yahoo_packet_hash(pkt, 249, "3");
 
	yahoo_packet_hash(pkt, 251, sfd->token);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
	yahoo_init_ft_recv(yid->yd, sfd);
 
}
 

	
 
static void yahoo_process_filetransferaccept(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	struct send_file_data *sfd;
 
	char *who = NULL;
 
	char *filename = NULL;
 
	char *id = NULL;
 
	char *token = NULL;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		switch (pair->key) {
 
		case 4:
 
			who = pair->value;
 
			break;
 
		case 5:
 
			/* Me... don't care */
 
			break;
 
		case 249:
 
			break;
 
		case 265:
 
			id = pair->value;
 
			break;
 
		case 251:
 
			token = pair->value;
 
			break;
 
		case 27:
 
			filename = pair->value;
 
			break;
 
		}
 
	}
 

	
 
	sfd = yahoo_get_active_transfer(id);
 

	
 
	if (sfd) {
 
		sfd->token = strdup(token);
 

	
 
		yahoo_file_transfer_upload(yid->yd, sfd);
 
	}
 
	else {
 
		YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
			(yid->yd->client_id, YAHOO_FILE_TRANSFER_UNKNOWN,
 
			sfd->data);
 

	
 
		yahoo_remove_active_transfer(sfd);
 
	}
 
}
 

	
 
static void yahoo_process_filetransferinfo(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	char *who = NULL;
 
	char *filename = NULL;
 
	char *id = NULL;
 
	char *token = NULL;
 
	char *ip_addr = NULL;
 

	
 
	struct send_file_data *sfd;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		switch (pair->key) {
 
		case 1:
 
		case 4:
 
			who = pair->value;
 
			break;
 
		case 5:
 
			/* Me... don't care */
 
			break;
 
		case 249:
 
			break;
 
		case 265:
 
			id = pair->value;
 
			break;
 
		case 250:
 
			ip_addr = pair->value;
 
			break;
 
		case 251:
 
			token = pair->value;
 
			break;
 
		case 27:
 
			filename = pair->value;
 
			break;
 
		}
 
	}
 

	
 
	sfd = yahoo_get_active_transfer(id);
 

	
 
	if (sfd) {
 
		sfd->token = strdup(token);
 
		sfd->ip_addr = strdup(ip_addr);
 

	
 
		yahoo_file_transfer_accept(yid, sfd);
 
	}
 
	else {
 
		YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
			(yid->yd->client_id, YAHOO_FILE_TRANSFER_UNKNOWN,
 
			sfd->data);
 

	
 
		yahoo_remove_active_transfer(sfd);
 
	}
 
}
 

	
 
static void yahoo_send_filetransferinfo(struct yahoo_data *yd,
 
	struct send_file_data *sfd)
 
{
 
	struct yahoo_input_data *yid;
 
	struct yahoo_packet *pkt;
 

	
 
	yid = find_input_by_id_and_type(yd->client_id, YAHOO_CONNECTION_PAGER);
 
	sfd->ip_addr = YAHOO_CALLBACK(ext_yahoo_get_ip_addr)("relay.yahoo.com");
 

	
 
	if (!sfd->ip_addr) {
 
		YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
			(yd->client_id, YAHOO_FILE_TRANSFER_RELAY, sfd->data);
 

	
 
		yahoo_remove_active_transfer(sfd);
 

	
 
		return;
 
	}
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_FILETRANSFERINFO,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, sfd->who);
 
	yahoo_packet_hash(pkt, 265, sfd->id);
 
	yahoo_packet_hash(pkt, 27, sfd->filename);
 
	yahoo_packet_hash(pkt, 249, "3");
 
	yahoo_packet_hash(pkt, 250, sfd->ip_addr);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
static void yahoo_process_filetransfer(struct yahoo_input_data *yid,
 
	struct yahoo_packet *pkt)
 
{
 
	YList *l;
 
	char *who = NULL;
 
	char *filename = NULL;
 
	char *msg = NULL;
 
	char *id = NULL;
 
	int action = 0;
 
	int size = 0;
 
	struct yahoo_data *yd = yid->yd;
 

	
 
	struct send_file_data *sfd;
 

	
 
	for (l = pkt->hash; l; l = l->next) {
 
		struct yahoo_pair *pair = l->data;
 
		switch (pair->key) {
 
		case 4:
 
			who = pair->value;
 
			break;
 
		case 5:
 
			/* Me... don't care */
 
			break;
 
		case 222:
 
			action = atoi(pair->value);
 
			break;
 
		case 265:
 
			id = pair->value;
 
			break;
 
		case 266: /* Don't know */
 
			break;
 
		case 302: /* Start Data? */
 
			break;
 
		case 300:
 
			break;
 
		case 27:
 
			filename = pair->value;
 
			break;
 
		case 28:
 
			size = atoi(pair->value);
 
			break;
 
		case 14:
 
			msg = pair->value;
 
		case 301: /* End Data? */
 
			break;
 
		case 303:
 
			break;
 

	
 
		}
 
	}
 

	
 
	if (action == YAHOO_FILE_TRANSFER_INIT) {
 
		/* Received a FT request from buddy */
 
		sfd = y_new0(struct send_file_data, 1);
 
        
 
		sfd->client_id = yd->client_id;
 
		sfd->id = strdup(id);
 
		sfd->who = strdup(who);
 
		sfd->filename = strdup(filename);
 
		sfd->size = size;
 
        
 
		yahoo_add_active_transfer(sfd);
 

	
 
		YAHOO_CALLBACK(ext_yahoo_got_file) (yd->client_id, yd->user,
 
			who, msg, filename, size, sfd->id);
 
	}
 
	else {
 
		/* Response to our request */
 
		sfd = yahoo_get_active_transfer(id);
 

	
 
		if (sfd && action == YAHOO_FILE_TRANSFER_ACCEPT) {
 
			yahoo_send_filetransferinfo(yd, sfd);
 
		}
 
		else if (!sfd || action == YAHOO_FILE_TRANSFER_REJECT) {
 
			YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
				(yd->client_id, YAHOO_FILE_TRANSFER_REJECT,
 
				sfd->data);
 

	
 
			yahoo_remove_active_transfer(sfd);
 
		}
 
	}
 
}
 

	
 
void yahoo_send_file(int id, const char *who, const char *msg,
 
	const char *name, unsigned long size,
 
	yahoo_get_fd_callback callback, void *data)
 
{
 
	struct yahoo_packet *pkt = NULL;
 
	char size_str[10];
 
	struct yahoo_input_data *yid;
 
	struct yahoo_data *yd;
 
	struct send_file_data *sfd;
 
	
 
	yid = find_input_by_id_and_type(id, YAHOO_CONNECTION_PAGER);
 
	yd = find_conn_by_id(id);
 
	sfd = y_new0(struct send_file_data, 1);
 

	
 
	sfd->client_id = id;
 
	sfd->id = yahoo_get_random();
 
	sfd->who = strdup(who);
 
	sfd->filename = strdup(name);
 
	sfd->size = size;
 
	sfd->callback = callback;
 
	sfd->data = data;
 

	
 
	yahoo_add_active_transfer(sfd);
 

	
 
	if (!yd)
 
		return;
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_FILETRANSFER,
 
		YPACKET_STATUS_DEFAULT, yd->session_id);
 

	
 
	snprintf(size_str, sizeof(size_str), "%ld", size);
 

	
 
	yahoo_packet_hash(pkt, 1, yd->user);
 
	yahoo_packet_hash(pkt, 5, who);
 
	yahoo_packet_hash(pkt, 265, sfd->id);
 
	yahoo_packet_hash(pkt, 222, "1");
 
	yahoo_packet_hash(pkt, 266, "1");
 
	yahoo_packet_hash(pkt, 302, "268");
 
	yahoo_packet_hash(pkt, 300, "268");
 
	yahoo_packet_hash(pkt, 27, name);
 
	yahoo_packet_hash(pkt, 28, size_str);
 
	yahoo_packet_hash(pkt, 301, "268");
 
	yahoo_packet_hash(pkt, 303, "268");
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 
}
 

	
 
void yahoo_send_file_transfer_response(int client_id, int response, char *id, void *data)
 
{
 
	struct yahoo_packet *pkt = NULL;
 
	char resp[2];
 
	struct yahoo_input_data *yid;
 
	
 
	struct send_file_data *sfd = yahoo_get_active_transfer(id);
 

	
 
	sfd->data = data;
 

	
 
	yid = find_input_by_id_and_type(client_id, YAHOO_CONNECTION_PAGER);
 

	
 
	pkt = yahoo_packet_new(YAHOO_SERVICE_Y7_FILETRANSFER,
 
		YPACKET_STATUS_DEFAULT, yid->yd->session_id);
 

	
 
	snprintf(resp, sizeof(resp), "%d", response);
 

	
 
	yahoo_packet_hash(pkt, 1, yid->yd->user);
 
	yahoo_packet_hash(pkt, 5, sfd->who);
 
	yahoo_packet_hash(pkt, 265, sfd->id);
 
	yahoo_packet_hash(pkt, 222, resp);
 

	
 
	yahoo_send_packet(yid, pkt, 0);
 

	
 
	yahoo_packet_free(pkt);
 

	
 
	if(response == YAHOO_FILE_TRANSFER_REJECT)
 
		yahoo_remove_active_transfer(sfd);
 
}
 

	
 
static void yahoo_process_ft_connection(struct yahoo_input_data *yid, int over)
 
{
 
	struct send_file_data *sfd;
 
	struct yahoo_data *yd = yid->yd;
 
	
 
	sfd = yahoo_get_active_transfer_with_yid(yid);
 

	
 
	if (!sfd) {
 
		LOG(("Something funny happened. yid %p has no sfd.\n", yid));
 
		return;
 
	}
 

	
 
	/* 
 
	 * We want to handle only the complete data with HEAD since we don't
 
	 * want a situation where both the GET and HEAD are active.
 
	 * With SEND, we really can't do much with partial response
 
	 */
 
	if ((sfd->state == FT_STATE_HEAD || sfd->state == FT_STATE_SEND) 
 
			&& !over)
 
		return;
 

	
 
	if (sfd->state == FT_STATE_HEAD) {
 
		/* Do a GET */
 
		char url[256];
 
		char buff[1024];
 
		char *sender_enc = NULL, *recv_enc = NULL, *token_enc = NULL;
 

	
 
		struct yahoo_input_data *yid_ft = 
 
			y_new0(struct yahoo_input_data, 1);
 
        
 
		yid_ft->yd = yid->yd;
 
		yid_ft->type = YAHOO_CONNECTION_FT;
 
        
 
		inputs = y_list_prepend(inputs, yid_ft);
 
		sfd->yid = yid_ft;
 
		sfd->state = FT_STATE_RECV;
 

	
 
		token_enc = yahoo_urlencode(sfd->token);
 
		sender_enc = yahoo_urlencode(sfd->who);
 
		recv_enc = yahoo_urlencode(yd->user);
 
        
 
		snprintf(url, sizeof(url), 
 
			"http://%s/relay?token=%s&sender=%s&recver=%s", sfd->ip_addr,
 
			token_enc, sender_enc, recv_enc);
 

	
 
		snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y,
 
			yd->cookie_t);
 

	
 

	
 
		yahoo_http_get(yd->client_id, url, buff, 1, 1,
 
			_yahoo_http_connected, yid_ft);
 

	
 
		FREE(token_enc);
 
		FREE(sender_enc);
 
		FREE(recv_enc);
 
	}
 
	else if (sfd->state == FT_STATE_RECV || 
 
		sfd->state == FT_STATE_RECV_START) {
 

	
 
		unsigned char *data_begin = NULL;
 

	
 
		if (yid->rxlen == 0)
 
			yahoo_remove_active_transfer(sfd);
 

	
 
		if (sfd->state != FT_STATE_RECV_START &&
 
			(data_begin = 
 
				(unsigned char *)strstr((char *)yid->rxqueue,
 
				"\r\n\r\n"))) {
 

	
 
			sfd->state = FT_STATE_RECV_START;
 

	
 
			yid->rxlen -= 4+(data_begin-yid->rxqueue)/sizeof(char);
 
			data_begin += 4;
 

	
 
			if (yid->rxlen > 0)
 
				YAHOO_CALLBACK(ext_yahoo_got_ft_data) 
 
					(yd->client_id, data_begin,
 
					yid->rxlen, sfd->data);
 
		}
 
		else if (sfd->state == FT_STATE_RECV_START)
 
			YAHOO_CALLBACK(ext_yahoo_got_ft_data) (yd->client_id,
 
				yid->rxqueue, yid->rxlen, sfd->data);
 

	
 
		FREE(yid->rxqueue);
 
		yid->rxqueue = NULL;
 
		yid->rxlen = 0;
 
	}
 
	else if (sfd->state == FT_STATE_SEND) {
 
		/* Sent file completed */
 
		int len = 0;
 
		char *off = strstr((char *)yid->rxqueue, "Content-Length: ");
 

	
 
		if (off) {
 
			off += 16;
 
			len = atoi(off);
 
		}
 

	
 
		if (len < sfd->size)
 
			YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
				(yd->client_id,
 
				YAHOO_FILE_TRANSFER_FAILED, sfd->data);
 
		else
 
			YAHOO_CALLBACK(ext_yahoo_file_transfer_done)
 
				(yd->client_id,
 
				YAHOO_FILE_TRANSFER_DONE, sfd->data);
 

	
 
		yahoo_remove_active_transfer(sfd);
 
	}
 
}
 

	
 
/* End File Transfer */
 

	
 
enum yahoo_status yahoo_current_status(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return YAHOO_STATUS_OFFLINE;
 
	return yd->current_status;
 
}
 

	
 
const YList *yahoo_get_buddylist(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return NULL;
 
	return yd->buddies;
 
}
 

	
 
const YList *yahoo_get_ignorelist(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return NULL;
 
	return yd->ignore;
 
}
 

	
 
const YList *yahoo_get_identities(int id)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return NULL;
 
	return yd->identities;
 
}
 

	
 
const char *yahoo_get_cookie(int id, const char *which)
 
{
 
	struct yahoo_data *yd = find_conn_by_id(id);
 
	if (!yd)
 
		return NULL;
 
	if (!strncasecmp(which, "y", 1))
 
		return yd->cookie_y;
 
	if (!strncasecmp(which, "b", 1))
 
		return yd->cookie_b;
 
	if (!strncasecmp(which, "t", 1))
 
		return yd->cookie_t;
 
	if (!strncasecmp(which, "c", 1))
 
		return yd->cookie_c;
 
	if (!strncasecmp(which, "login", 5))
 
		return yd->login_cookie;
 
	return NULL;
 
}
 

	
 
const char *yahoo_get_profile_url(void)
 
{
 
	return profile_url;
 
}
backends/libyahoo2/yahoo/md5.c
Show inline comments
 
new file 100644
 
/*
 
  Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
 

	
 
  This software is provided 'as-is', without any express or implied
 
  warranty.  In no event will the authors be held liable for any damages
 
  arising from the use of this software.
 

	
 
  Permission is granted to anyone to use this software for any purpose,
 
  including commercial applications, and to alter it and redistribute it
 
  freely, subject to the following restrictions:
 

	
 
  1. The origin of this software must not be misrepresented; you must not
 
     claim that you wrote the original software. If you use this software
 
     in a product, an acknowledgment in the product documentation would be
 
     appreciated but is not required.
 
  2. Altered source versions must be plainly marked as such, and must not be
 
     misrepresented as being the original software.
 
  3. This notice may not be removed or altered from any source distribution.
 

	
 
  L. Peter Deutsch
 
  ghost@aladdin.com
 

	
 
 */
 
/*
 
  Independent implementation of MD5 (RFC 1321).
 

	
 
  This code implements the MD5 Algorithm defined in RFC 1321.
 
  It is derived directly from the text of the RFC and not from the
 
  reference implementation.
 

	
 
  The original and principal author of md5.c is L. Peter Deutsch
 
  <ghost@aladdin.com>.  Other authors are noted in the change history
 
  that follows (in reverse chronological order):
 

	
 
  1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
 
  1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
 
  1999-05-03 lpd Original version.
 
 */
 

	
 
#if HAVE_CONFIG_H
 
# include <config.h>
 
#endif
 

	
 
#include "md5.h"
 

	
 
#if STDC_HEADERS
 
# include <string.h>
 
#else
 
# if !HAVE_STRCHR
 
#  define strchr index
 
#  define strrchr rindex
 
# endif
 
char *strchr(), *strrchr();
 
# if !HAVE_MEMCPY
 
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
 
#  define memmove(d, s, n) bcopy ((s), (d), (n))
 
# endif
 
#endif
 

	
 
#ifdef TEST
 
/*
 
 * Compile with -DTEST to create a self-contained executable test program.
 
 * The test program should print out the same values as given in section
 
 * A.5 of RFC 1321, reproduced below.
 
 */
 
main()
 
{
 
	static const char *const test[7] = {
 
		"",		/*d41d8cd98f00b204e9800998ecf8427e */
 
		"945399884.61923487334tuvga",	/*0cc175b9c0f1b6a831c399e269772661 */
 
		"abc",		/*900150983cd24fb0d6963f7d28e17f72 */
 
		"message digest",	/*f96b697d7cb7938d525a2f31aaf161d0 */
 
		"abcdefghijklmnopqrstuvwxyz",	/*c3fcd3d76192e4007dfb496cca67e13b */
 
		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
 
		/*d174ab98d277d9f5a5611c2c9f419d9f */
 
		"12345678901234567890123456789012345678901234567890123456789012345678901234567890"	/*57edf4a22be3c955ac49da2e2107b67a */
 
	};
 
	int i;
 

	
 
	for (i = 0; i < 7; ++i) {
 
		md5_state_t state;
 
		md5_byte_t digest[16];
 
		int di;
 

	
 
		md5_init(&state);
 
		md5_append(&state, (const md5_byte_t *)test[i],
 
			strlen(test[i]));
 
		md5_finish(&state, digest);
 
		printf("MD5 (\"%s\") = ", test[i]);
 
		for (di = 0; di < 16; ++di)
 
			printf("%02x", digest[di]);
 
		printf("\n");
 
	}
 
	return 0;
 
}
 
#endif				/* TEST */
 

	
 
/*
 
 * For reference, here is the program that computed the T values.
 
 */
 
#if 0
 
#include <math.h>
 
main()
 
{
 
	int i;
 
	for (i = 1; i <= 64; ++i) {
 
		unsigned long v =
 
			(unsigned long)(4294967296.0 * fabs(sin((double)i)));
 
		printf("#define T%d 0x%08lx\n", i, v);
 
	}
 
	return 0;
 
}
 
#endif
 
/*
 
 * End of T computation program.
 
 */
 
#define T1 0xd76aa478
 
#define T2 0xe8c7b756
 
#define T3 0x242070db
 
#define T4 0xc1bdceee
 
#define T5 0xf57c0faf
 
#define T6 0x4787c62a
 
#define T7 0xa8304613
 
#define T8 0xfd469501
 
#define T9 0x698098d8
 
#define T10 0x8b44f7af
 
#define T11 0xffff5bb1
 
#define T12 0x895cd7be
 
#define T13 0x6b901122
 
#define T14 0xfd987193
 
#define T15 0xa679438e
 
#define T16 0x49b40821
 
#define T17 0xf61e2562
 
#define T18 0xc040b340
 
#define T19 0x265e5a51
 
#define T20 0xe9b6c7aa
 
#define T21 0xd62f105d
 
#define T22 0x02441453
 
#define T23 0xd8a1e681
 
#define T24 0xe7d3fbc8
 
#define T25 0x21e1cde6
 
#define T26 0xc33707d6
 
#define T27 0xf4d50d87
 
#define T28 0x455a14ed
 
#define T29 0xa9e3e905
 
#define T30 0xfcefa3f8
 
#define T31 0x676f02d9
 
#define T32 0x8d2a4c8a
 
#define T33 0xfffa3942
 
#define T34 0x8771f681
 
#define T35 0x6d9d6122
 
#define T36 0xfde5380c
 
#define T37 0xa4beea44
 
#define T38 0x4bdecfa9
 
#define T39 0xf6bb4b60
 
#define T40 0xbebfbc70
 
#define T41 0x289b7ec6
 
#define T42 0xeaa127fa
 
#define T43 0xd4ef3085
 
#define T44 0x04881d05
 
#define T45 0xd9d4d039
 
#define T46 0xe6db99e5
 
#define T47 0x1fa27cf8
 
#define T48 0xc4ac5665
 
#define T49 0xf4292244
 
#define T50 0x432aff97
 
#define T51 0xab9423a7
 
#define T52 0xfc93a039
 
#define T53 0x655b59c3
 
#define T54 0x8f0ccc92
 
#define T55 0xffeff47d
 
#define T56 0x85845dd1
 
#define T57 0x6fa87e4f
 
#define T58 0xfe2ce6e0
 
#define T59 0xa3014314
 
#define T60 0x4e0811a1
 
#define T61 0xf7537e82
 
#define T62 0xbd3af235
 
#define T63 0x2ad7d2bb
 
#define T64 0xeb86d391
 

	
 
static void md5_process(md5_state_t *pms, const md5_byte_t *data /*[64] */ )
 
{
 
	md5_word_t
 
		a = pms->abcd[0], b = pms->abcd[1],
 
		c = pms->abcd[2], d = pms->abcd[3];
 
	md5_word_t t;
 

	
 
#ifndef ARCH_IS_BIG_ENDIAN
 
# define ARCH_IS_BIG_ENDIAN 1	/* slower, default implementation */
 
#endif
 
#if ARCH_IS_BIG_ENDIAN
 

	
 
	/*
 
	 * On big-endian machines, we must arrange the bytes in the right
 
	 * order.  (This also works on machines of unknown byte order.)
 
	 */
 
	md5_word_t X[16];
 
	const md5_byte_t *xp = data;
 
	int i;
 

	
 
	for (i = 0; i < 16; ++i, xp += 4)
 
		X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
 

	
 
#else				/* !ARCH_IS_BIG_ENDIAN */
 

	
 
	/*
 
	 * On little-endian machines, we can process properly aligned data
 
	 * without copying it.
 
	 */
 
	md5_word_t xbuf[16];
 
	const md5_word_t *X;
 

	
 
	if (!((data - (const md5_byte_t *)0) & 3)) {
 
		/* data are properly aligned */
 
		X = (const md5_word_t *)data;
 
	} else {
 
		/* not aligned */
 
		memcpy(xbuf, data, 64);
 
		X = xbuf;
 
	}
 
#endif
 

	
 
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
 

	
 
	/* Round 1. */
 
	/* Let [abcd k s i] denote the operation
 
	   a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
 
#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
 
#define SET(a, b, c, d, k, s, Ti)\
 
  t = a + F(b,c,d) + X[k] + Ti;\
 
  a = ROTATE_LEFT(t, s) + b
 
	/* Do the following 16 operations. */
 
	SET(a, b, c, d, 0, 7, T1);
 
	SET(d, a, b, c, 1, 12, T2);
 
	SET(c, d, a, b, 2, 17, T3);
 
	SET(b, c, d, a, 3, 22, T4);
 
	SET(a, b, c, d, 4, 7, T5);
 
	SET(d, a, b, c, 5, 12, T6);
 
	SET(c, d, a, b, 6, 17, T7);
 
	SET(b, c, d, a, 7, 22, T8);
 
	SET(a, b, c, d, 8, 7, T9);
 
	SET(d, a, b, c, 9, 12, T10);
 
	SET(c, d, a, b, 10, 17, T11);
 
	SET(b, c, d, a, 11, 22, T12);
 
	SET(a, b, c, d, 12, 7, T13);
 
	SET(d, a, b, c, 13, 12, T14);
 
	SET(c, d, a, b, 14, 17, T15);
 
	SET(b, c, d, a, 15, 22, T16);
 
#undef SET
 

	
 
	/* Round 2. */
 
	/* Let [abcd k s i] denote the operation
 
	   a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
 
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
 
#define SET(a, b, c, d, k, s, Ti)\
 
  t = a + G(b,c,d) + X[k] + Ti;\
 
  a = ROTATE_LEFT(t, s) + b
 
	/* Do the following 16 operations. */
 
	SET(a, b, c, d, 1, 5, T17);
 
	SET(d, a, b, c, 6, 9, T18);
 
	SET(c, d, a, b, 11, 14, T19);
 
	SET(b, c, d, a, 0, 20, T20);
 
	SET(a, b, c, d, 5, 5, T21);
 
	SET(d, a, b, c, 10, 9, T22);
 
	SET(c, d, a, b, 15, 14, T23);
 
	SET(b, c, d, a, 4, 20, T24);
 
	SET(a, b, c, d, 9, 5, T25);
 
	SET(d, a, b, c, 14, 9, T26);
 
	SET(c, d, a, b, 3, 14, T27);
 
	SET(b, c, d, a, 8, 20, T28);
 
	SET(a, b, c, d, 13, 5, T29);
 
	SET(d, a, b, c, 2, 9, T30);
 
	SET(c, d, a, b, 7, 14, T31);
 
	SET(b, c, d, a, 12, 20, T32);
 
#undef SET
 

	
 
	/* Round 3. */
 
	/* Let [abcd k s t] denote the operation
 
	   a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
 
#define H(x, y, z) ((x) ^ (y) ^ (z))
 
#define SET(a, b, c, d, k, s, Ti)\
 
  t = a + H(b,c,d) + X[k] + Ti;\
 
  a = ROTATE_LEFT(t, s) + b
 
	/* Do the following 16 operations. */
 
	SET(a, b, c, d, 5, 4, T33);
 
	SET(d, a, b, c, 8, 11, T34);
 
	SET(c, d, a, b, 11, 16, T35);
 
	SET(b, c, d, a, 14, 23, T36);
 
	SET(a, b, c, d, 1, 4, T37);
 
	SET(d, a, b, c, 4, 11, T38);
 
	SET(c, d, a, b, 7, 16, T39);
 
	SET(b, c, d, a, 10, 23, T40);
 
	SET(a, b, c, d, 13, 4, T41);
 
	SET(d, a, b, c, 0, 11, T42);
 
	SET(c, d, a, b, 3, 16, T43);
 
	SET(b, c, d, a, 6, 23, T44);
 
	SET(a, b, c, d, 9, 4, T45);
 
	SET(d, a, b, c, 12, 11, T46);
 
	SET(c, d, a, b, 15, 16, T47);
 
	SET(b, c, d, a, 2, 23, T48);
 
#undef SET
 

	
 
	/* Round 4. */
 
	/* Let [abcd k s t] denote the operation
 
	   a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
 
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
 
#define SET(a, b, c, d, k, s, Ti)\
 
  t = a + I(b,c,d) + X[k] + Ti;\
 
  a = ROTATE_LEFT(t, s) + b
 
	/* Do the following 16 operations. */
 
	SET(a, b, c, d, 0, 6, T49);
 
	SET(d, a, b, c, 7, 10, T50);
 
	SET(c, d, a, b, 14, 15, T51);
 
	SET(b, c, d, a, 5, 21, T52);
 
	SET(a, b, c, d, 12, 6, T53);
 
	SET(d, a, b, c, 3, 10, T54);
 
	SET(c, d, a, b, 10, 15, T55);
 
	SET(b, c, d, a, 1, 21, T56);
 
	SET(a, b, c, d, 8, 6, T57);
 
	SET(d, a, b, c, 15, 10, T58);
 
	SET(c, d, a, b, 6, 15, T59);
 
	SET(b, c, d, a, 13, 21, T60);
 
	SET(a, b, c, d, 4, 6, T61);
 
	SET(d, a, b, c, 11, 10, T62);
 
	SET(c, d, a, b, 2, 15, T63);
 
	SET(b, c, d, a, 9, 21, T64);
 
#undef SET
 

	
 
	/* Then perform the following additions. (That is increment each
 
	   of the four registers by the value it had before this block
 
	   was started.) */
 
	pms->abcd[0] += a;
 
	pms->abcd[1] += b;
 
	pms->abcd[2] += c;
 
	pms->abcd[3] += d;
 
}
 

	
 
void md5_init(md5_state_t *pms)
 
{
 
	pms->count[0] = pms->count[1] = 0;
 
	pms->abcd[0] = 0x67452301;
 
	pms->abcd[1] = 0xefcdab89;
 
	pms->abcd[2] = 0x98badcfe;
 
	pms->abcd[3] = 0x10325476;
 
}
 

	
 
void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
 
{
 
	const md5_byte_t *p = data;
 
	int left = nbytes;
 
	int offset = (pms->count[0] >> 3) & 63;
 
	md5_word_t nbits = (md5_word_t) (nbytes << 3);
 

	
 
	if (nbytes <= 0)
 
		return;
 

	
 
	/* Update the message length. */
 
	pms->count[1] += nbytes >> 29;
 
	pms->count[0] += nbits;
 
	if (pms->count[0] < nbits)
 
		pms->count[1]++;
 

	
 
	/* Process an initial partial block. */
 
	if (offset) {
 
		int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
 

	
 
		memcpy(pms->buf + offset, p, copy);
 
		if (offset + copy < 64)
 
			return;
 
		p += copy;
 
		left -= copy;
 
		md5_process(pms, pms->buf);
 
	}
 

	
 
	/* Process full blocks. */
 
	for (; left >= 64; p += 64, left -= 64)
 
		md5_process(pms, p);
 

	
 
	/* Process a final partial block. */
 
	if (left)
 
		memcpy(pms->buf, p, left);
 
}
 

	
 
void md5_finish(md5_state_t *pms, md5_byte_t digest[16])
 
{
 
	static const md5_byte_t pad[64] = {
 
		0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 
	};
 
	md5_byte_t data[8];
 
	int i;
 

	
 
	/* Save the length before padding. */
 
	for (i = 0; i < 8; ++i)
 
		data[i] = (md5_byte_t) (pms->count[i >> 2] >> ((i & 3) << 3));
 
	/* Pad to 56 bytes mod 64. */
 
	md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
 
	/* Append the length. */
 
	md5_append(pms, data, 8);
 
	for (i = 0; i < 16; ++i)
 
		digest[i] = (md5_byte_t) (pms->abcd[i >> 2] >> ((i & 3) << 3));
 
}
backends/libyahoo2/yahoo/md5.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
 

	
 
  This software is provided 'as-is', without any express or implied
 
  warranty.  In no event will the authors be held liable for any damages
 
  arising from the use of this software.
 

	
 
  Permission is granted to anyone to use this software for any purpose,
 
  including commercial applications, and to alter it and redistribute it
 
  freely, subject to the following restrictions:
 

	
 
  1. The origin of this software must not be misrepresented; you must not
 
     claim that you wrote the original software. If you use this software
 
     in a product, an acknowledgment in the product documentation would be
 
     appreciated but is not required.
 
  2. Altered source versions must be plainly marked as such, and must not be
 
     misrepresented as being the original software.
 
  3. This notice may not be removed or altered from any source distribution.
 

	
 
  L. Peter Deutsch
 
  ghost@aladdin.com
 

	
 
 */
 
/*
 
  Independent implementation of MD5 (RFC 1321).
 

	
 
  This code implements the MD5 Algorithm defined in RFC 1321.
 
  It is derived directly from the text of the RFC and not from the
 
  reference implementation.
 

	
 
  The original and principal author of md5.h is L. Peter Deutsch
 
  <ghost@aladdin.com>.  Other authors are noted in the change history
 
  that follows (in reverse chronological order):
 

	
 
  1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
 
  1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
 
	added conditionalization for C++ compilation from Martin
 
	Purschke <purschke@bnl.gov>.
 
  1999-05-03 lpd Original version.
 
 */
 

	
 
#ifndef md5_INCLUDED
 
#  define md5_INCLUDED
 

	
 
/*
 
 * This code has some adaptations for the Ghostscript environment, but it
 
 * will compile and run correctly in any environment with 8-bit chars and
 
 * 32-bit ints.  Specifically, it assumes that if the following are
 
 * defined, they have the same meaning as in Ghostscript: P1, P2, P3,
 
 * ARCH_IS_BIG_ENDIAN.
 
 */
 

	
 
typedef unsigned char md5_byte_t;	/* 8-bit byte */
 
typedef unsigned int md5_word_t;	/* 32-bit word */
 

	
 
/* Define the state of the MD5 Algorithm. */
 
typedef struct md5_state_s {
 
	md5_word_t count[2];	/* message length in bits, lsw first */
 
	md5_word_t abcd[4];	/* digest buffer */
 
	md5_byte_t buf[64];	/* accumulate block */
 
} md5_state_t;
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
/* Initialize the algorithm. */
 
#ifdef P1
 
	void md5_init(P1(md5_state_t *pms));
 
#else
 
	void md5_init(md5_state_t *pms);
 
#endif
 

	
 
/* Append a string to the message. */
 
#ifdef P3
 
	void md5_append(P3(md5_state_t *pms, const md5_byte_t *data,
 
			int nbytes));
 
#else
 
	void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
 
#endif
 

	
 
/* Finish the message and return the digest. */
 
#ifdef P2
 
	void md5_finish(P2(md5_state_t *pms, md5_byte_t digest[16]));
 
#else
 
	void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
 
#endif
 

	
 
#ifdef __cplusplus
 
}				/* end extern "C" */
 
#endif
 
#endif				/* md5_INCLUDED */
backends/libyahoo2/yahoo/sha1.c
Show inline comments
 
new file 100644
 
/*-
 
 * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com>
 
 * All rights reserved.
 
 *
 
 * Redistribution and use in source and binary forms, with or without
 
 * modification, are permitted provided that the following conditions
 
 * are met:
 
 * 1. Redistributions of source code must retain the above copyright
 
 *    notice, this list of conditions and the following disclaimer.
 
 * 2. Redistributions in binary form must reproduce the above copyright
 
 *    notice, this list of conditions and the following disclaimer in the
 
 *    documentation and/or other materials provided with the distribution.
 
 *
 
 * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS''
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
 * POSSIBILITY OF SUCH DAMAGE.
 
 */
 

	
 
/*
 
 * Define WORDS_BIGENDIAN if compiling on a big-endian architecture.
 
 *
 
 * Define SHA1_TEST to test the implementation using the NIST's
 
 * sample messages. The output should be:
 
 *
 
 *   a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
 
 *   84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1
 
 *   34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f
 
 */
 

	
 
#ifdef HAVE_CONFIG_H
 
#include <config.h>
 
#endif				/* HAVE_CONFIG_H */
 

	
 
#if HAVE_INTTYPES_H
 
# include <inttypes.h>
 
#else
 
# if HAVE_STDINT_H
 
#  include <stdint.h>
 
# endif
 
#endif
 

	
 
#include <string.h>
 

	
 
#include "sha1.h"
 

	
 
#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
 
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
 

	
 
#define F_0_19(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
 
#define F_20_39(x, y, z) ((x) ^ (y) ^ (z))
 
#define F_40_59(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
 
#define F_60_79(x, y, z) ((x) ^ (y) ^ (z))
 

	
 
#define DO_ROUND(F, K) { \
 
  temp = ROTL(a, 5) + F(b, c, d) + e + *(W++) + K; \
 
  e = d; \
 
  d = c; \
 
  c = ROTL(b, 30); \
 
  b = a; \
 
  a = temp; \
 
}
 

	
 
#define K_0_19 0x5a827999L
 
#define K_20_39 0x6ed9eba1L
 
#define K_40_59 0x8f1bbcdcL
 
#define K_60_79 0xca62c1d6L
 

	
 
#ifndef RUNTIME_ENDIAN
 

	
 
#ifdef WORDS_BIGENDIAN
 

	
 
#define BYTESWAP(x) (x)
 
#define BYTESWAP64(x) (x)
 

	
 
#else				/* WORDS_BIGENDIAN */
 

	
 
#define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | (ROTL((x), 8) & 0x00ff00ffL))
 

	
 
static uint64_t _byteswap64(uint64_t x)
 
{
 
	uint32_t a = x >> 32;
 
	uint32_t b = (uint32_t) x;
 
	return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a);
 
}
 

	
 
#define BYTESWAP64(x) _byteswap64(x)
 

	
 
#endif				/* WORDS_BIGENDIAN */
 

	
 
#else				/* !RUNTIME_ENDIAN */
 

	
 
#define BYTESWAP(x) _byteswap(sc->littleEndian, x)
 
#define BYTESWAP64(x) _byteswap64(sc->littleEndian, x)
 

	
 
#define _BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \
 
		      (ROTL((x), 8) & 0x00ff00ffL))
 
#define _BYTESWAP64(x) __byteswap64(x)
 

	
 
static uint64_t __byteswap64(uint64_t x)
 
{
 
	uint32_t a = x >> 32;
 
	uint32_t b = (uint32_t) x;
 
	return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a);
 
}
 

	
 
static uint32_t _byteswap(int littleEndian, uint32_t x)
 
{
 
	if (!littleEndian)
 
		return x;
 
	else
 
		return _BYTESWAP(x);
 
}
 

	
 
static uint64_t _byteswap64(int littleEndian, uint64_t x)
 
{
 
	if (!littleEndian)
 
		return x;
 
	else
 
		return _BYTESWAP64(x);
 
}
 

	
 
static void setEndian(int *littleEndianp)
 
{
 
	union {
 
		uint32_t w;
 
		uint8_t b[4];
 
	} endian;
 

	
 
	endian.w = 1L;
 
	*littleEndianp = endian.b[0] != 0;
 
}
 

	
 
#endif				/* !RUNTIME_ENDIAN */
 

	
 
static const uint8_t padding[64] = {
 
	0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
 
};
 

	
 
void SHA1Init(SHA1Context *sc)
 
{
 
#ifdef RUNTIME_ENDIAN
 
	setEndian(&sc->littleEndian);
 
#endif				/* RUNTIME_ENDIAN */
 

	
 
	sc->totalLength = 0LL;
 
	sc->hash[0] = 0x67452301L;
 
	sc->hash[1] = 0xefcdab89L;
 
	sc->hash[2] = 0x98badcfeL;
 
	sc->hash[3] = 0x10325476L;
 
	sc->hash[4] = 0xc3d2e1f0L;
 
	sc->bufferLength = 0L;
 
}
 

	
 
static void burnStack(int size)
 
{
 
	char buf[128];
 

	
 
	memset(buf, 0, sizeof(buf));
 
	size -= sizeof(buf);
 
	if (size > 0)
 
		burnStack(size);
 
}
 

	
 
static void SHA1Guts(SHA1Context *sc, const uint32_t *cbuf)
 
{
 
	uint32_t buf[80];
 
	uint32_t *W, *W3, *W8, *W14, *W16;
 
	uint32_t a, b, c, d, e, temp;
 
	int i;
 

	
 
	W = buf;
 

	
 
	for (i = 15; i >= 0; i--) {
 
		*(W++) = BYTESWAP(*cbuf);
 
		cbuf++;
 
	}
 

	
 
	W16 = &buf[0];
 
	W14 = &buf[2];
 
	W8 = &buf[8];
 
	W3 = &buf[13];
 

	
 
	for (i = 63; i >= 0; i--) {
 
		*W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++);
 
		*W = ROTL(*W, 1);
 
		W++;
 
	}
 

	
 
	a = sc->hash[0];
 
	b = sc->hash[1];
 
	c = sc->hash[2];
 
	d = sc->hash[3];
 
	e = sc->hash[4];
 

	
 
	W = buf;
 

	
 
#ifndef SHA1_UNROLL
 
#define SHA1_UNROLL 20
 
#endif				/* !SHA1_UNROLL */
 

	
 
#if SHA1_UNROLL == 1
 
	for (i = 19; i >= 0; i--)
 
		DO_ROUND(F_0_19, K_0_19);
 

	
 
	for (i = 19; i >= 0; i--)
 
		DO_ROUND(F_20_39, K_20_39);
 

	
 
	for (i = 19; i >= 0; i--)
 
		DO_ROUND(F_40_59, K_40_59);
 

	
 
	for (i = 19; i >= 0; i--)
 
		DO_ROUND(F_60_79, K_60_79);
 
#elif SHA1_UNROLL == 2
 
	for (i = 9; i >= 0; i--) {
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
	}
 

	
 
	for (i = 9; i >= 0; i--) {
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
	}
 

	
 
	for (i = 9; i >= 0; i--) {
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
	}
 

	
 
	for (i = 9; i >= 0; i--) {
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
	}
 
#elif SHA1_UNROLL == 4
 
	for (i = 4; i >= 0; i--) {
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
	}
 

	
 
	for (i = 4; i >= 0; i--) {
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
	}
 

	
 
	for (i = 4; i >= 0; i--) {
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
	}
 

	
 
	for (i = 4; i >= 0; i--) {
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
	}
 
#elif SHA1_UNROLL == 5
 
	for (i = 3; i >= 0; i--) {
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
	}
 

	
 
	for (i = 3; i >= 0; i--) {
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
	}
 

	
 
	for (i = 3; i >= 0; i--) {
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
	}
 

	
 
	for (i = 3; i >= 0; i--) {
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
	}
 
#elif SHA1_UNROLL == 10
 
	for (i = 1; i >= 0; i--) {
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
		DO_ROUND(F_0_19, K_0_19);
 
	}
 

	
 
	for (i = 1; i >= 0; i--) {
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
		DO_ROUND(F_20_39, K_20_39);
 
	}
 

	
 
	for (i = 1; i >= 0; i--) {
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
		DO_ROUND(F_40_59, K_40_59);
 
	}
 

	
 
	for (i = 1; i >= 0; i--) {
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
		DO_ROUND(F_60_79, K_60_79);
 
	}
 
#elif SHA1_UNROLL == 20
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 
	DO_ROUND(F_0_19, K_0_19);
 

	
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 
	DO_ROUND(F_20_39, K_20_39);
 

	
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 
	DO_ROUND(F_40_59, K_40_59);
 

	
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
	DO_ROUND(F_60_79, K_60_79);
 
#else				/* SHA1_UNROLL */
 
#error SHA1_UNROLL must be 1, 2, 4, 5, 10 or 20!
 
#endif
 

	
 
	sc->hash[0] += a;
 
	sc->hash[1] += b;
 
	sc->hash[2] += c;
 
	sc->hash[3] += d;
 
	sc->hash[4] += e;
 
}
 

	
 
void SHA1Update(SHA1Context *sc, const void *vdata, uint32_t len)
 
{
 
	const uint8_t *data = vdata;
 
	uint32_t bufferBytesLeft;
 
	uint32_t bytesToCopy;
 
	int needBurn = 0;
 

	
 
#ifdef SHA1_FAST_COPY
 
	if (sc->bufferLength) {
 
		bufferBytesLeft = 64L - sc->bufferLength;
 

	
 
		bytesToCopy = bufferBytesLeft;
 
		if (bytesToCopy > len)
 
			bytesToCopy = len;
 

	
 
		memcpy(&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
 

	
 
		sc->totalLength += bytesToCopy * 8L;
 

	
 
		sc->bufferLength += bytesToCopy;
 
		data += bytesToCopy;
 
		len -= bytesToCopy;
 

	
 
		if (sc->bufferLength == 64L) {
 
			SHA1Guts(sc, sc->buffer.words);
 
			needBurn = 1;
 
			sc->bufferLength = 0L;
 
		}
 
	}
 

	
 
	while (len > 63) {
 
		sc->totalLength += 512L;
 

	
 
		SHA1Guts(sc, data);
 
		needBurn = 1;
 

	
 
		data += 64L;
 
		len -= 64L;
 
	}
 

	
 
	if (len) {
 
		memcpy(&sc->buffer.bytes[sc->bufferLength], data, len);
 

	
 
		sc->totalLength += len * 8L;
 

	
 
		sc->bufferLength += len;
 
	}
 
#else				/* SHA1_FAST_COPY */
 
	while (len) {
 
		bufferBytesLeft = 64L - sc->bufferLength;
 

	
 
		bytesToCopy = bufferBytesLeft;
 
		if (bytesToCopy > len)
 
			bytesToCopy = len;
 

	
 
		memcpy(&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
 

	
 
		sc->totalLength += bytesToCopy * 8L;
 

	
 
		sc->bufferLength += bytesToCopy;
 
		data += bytesToCopy;
 
		len -= bytesToCopy;
 

	
 
		if (sc->bufferLength == 64L) {
 
			SHA1Guts(sc, sc->buffer.words);
 
			needBurn = 1;
 
			sc->bufferLength = 0L;
 
		}
 
	}
 
#endif				/* SHA1_FAST_COPY */
 

	
 
	if (needBurn)
 
		burnStack(sizeof(uint32_t[86]) + sizeof(uint32_t *[5]) +
 
			sizeof(int));
 
}
 

	
 
void SHA1Final(SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE])
 
{
 
	uint32_t bytesToPad;
 
	uint64_t lengthPad;
 
	int i;
 

	
 
	bytesToPad = 120L - sc->bufferLength;
 
	if (bytesToPad > 64L)
 
		bytesToPad -= 64L;
 

	
 
	lengthPad = BYTESWAP64(sc->totalLength);
 

	
 
	SHA1Update(sc, padding, bytesToPad);
 
	SHA1Update(sc, &lengthPad, 8L);
 

	
 
	if (hash) {
 
		for (i = 0; i < SHA1_HASH_WORDS; i++) {
 
#ifdef SHA1_FAST_COPY
 
			*((uint32_t *)hash) = BYTESWAP(sc->hash[i]);
 
#else				/* SHA1_FAST_COPY */
 
			hash[0] = (uint8_t) (sc->hash[i] >> 24);
 
			hash[1] = (uint8_t) (sc->hash[i] >> 16);
 
			hash[2] = (uint8_t) (sc->hash[i] >> 8);
 
			hash[3] = (uint8_t) sc->hash[i];
 
#endif				/* SHA1_FAST_COPY */
 
			hash += 4;
 
		}
 
	}
 
}
 

	
 
#ifdef SHA1_TEST
 

	
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 

	
 
int main(int argc, char *argv[])
 
{
 
	SHA1Context foo;
 
	uint8_t hash[SHA1_HASH_SIZE];
 
	char buf[1000];
 
	int i;
 

	
 
	SHA1Init(&foo);
 
	SHA1Update(&foo, "abc", 3);
 
	SHA1Final(&foo, hash);
 

	
 
	for (i = 0; i < SHA1_HASH_SIZE;) {
 
		printf("%02x", hash[i++]);
 
		if (!(i % 4))
 
			printf(" ");
 
	}
 
	printf("\n");
 

	
 
	SHA1Init(&foo);
 
	SHA1Update(&foo,
 
		"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56);
 
	SHA1Final(&foo, hash);
 

	
 
	for (i = 0; i < SHA1_HASH_SIZE;) {
 
		printf("%02x", hash[i++]);
 
		if (!(i % 4))
 
			printf(" ");
 
	}
 
	printf("\n");
 

	
 
	SHA1Init(&foo);
 
	memset(buf, 'a', sizeof(buf));
 
	for (i = 0; i < 1000; i++)
 
		SHA1Update(&foo, buf, sizeof(buf));
 
	SHA1Final(&foo, hash);
 

	
 
	for (i = 0; i < SHA1_HASH_SIZE;) {
 
		printf("%02x", hash[i++]);
 
		if (!(i % 4))
 
			printf(" ");
 
	}
 
	printf("\n");
 

	
 
	exit(0);
 
}
 

	
 
#endif				/* SHA1_TEST */
backends/libyahoo2/yahoo/sha1.h
Show inline comments
 
new file 100644
 
/*-
 
 * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com>
 
 * All rights reserved.
 
 *
 
 * Redistribution and use in source and binary forms, with or without
 
 * modification, are permitted provided that the following conditions
 
 * are met:
 
 * 1. Redistributions of source code must retain the above copyright
 
 *    notice, this list of conditions and the following disclaimer.
 
 * 2. Redistributions in binary form must reproduce the above copyright
 
 *    notice, this list of conditions and the following disclaimer in the
 
 *    documentation and/or other materials provided with the distribution.
 
 *
 
 * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS''
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
 * POSSIBILITY OF SUCH DAMAGE.
 
 */
 

	
 
#ifndef _SHA1_H
 
#define _SHA1_H
 

	
 
#if HAVE_INTTYPES_H
 
# include <inttypes.h>
 
#else
 
# if HAVE_STDINT_H
 
#  include <stdint.h>
 
# endif
 
#endif
 

	
 
#define SHA1_HASH_SIZE 20
 

	
 
/* Hash size in 32-bit words */
 
#define SHA1_HASH_WORDS 5
 

	
 
struct _SHA1Context {
 
	uint64_t totalLength;
 
	uint32_t hash[SHA1_HASH_WORDS];
 
	uint32_t bufferLength;
 
	union {
 
		uint32_t words[16];
 
		uint8_t bytes[64];
 
	} buffer;
 
#ifdef RUNTIME_ENDIAN
 
	int littleEndian;
 
#endif				/* RUNTIME_ENDIAN */
 
};
 

	
 
typedef struct _SHA1Context SHA1Context;
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
	void SHA1Init(SHA1Context *sc);
 
	void SHA1Update(SHA1Context *sc, const void *data, uint32_t len);
 
	void SHA1Final(SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE]);
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 
#endif				/* _SHA1_H */
backends/libyahoo2/yahoo/yahoo2.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo2.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#ifndef YAHOO2_H
 
#define YAHOO2_H
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
#include "yahoo2_types.h"
 

	
 
/* returns the socket descriptor object for a given pager connection. shouldn't be needed */
 
	void *yahoo_get_fd(int id);
 

	
 
/* says how much logging to do */
 
/* see yahoo2_types.h for the different values */
 
	int yahoo_set_log_level(enum yahoo_log_level level);
 
	enum yahoo_log_level yahoo_get_log_level(void);
 

	
 
/* these functions should be self explanatory */
 
/* who always means the buddy you're acting on */
 
/* id is the successful value returned by yahoo_init */
 

	
 
/* init returns a connection id used to identify the connection hereon */
 
/* or 0 on failure */
 
/* you must call init before calling any other function */
 
/*
 
 * The optional parameters to init are key/value pairs that specify 
 
 * server settings to use.  This list must be NULL terminated - even
 
 * if the list is empty.  If a parameter isn't set, a default value
 
 * will be used.  Parameter keys are strings, parameter values are
 
 * either strings or ints, depending on the key.  Values passed in
 
 * are copied, so you can use const/auto/static/pointers/whatever
 
 * you want.  Parameters are:
 
 * 	NAME			TYPE		DEFAULT
 
 *	pager_host		char *		scs.msg.yahoo.com
 
 *	pager_port		int		5050
 
 *	filetransfer_host	char *		filetransfer.msg.yahoo.com
 
 *	filetransfer_port	int		80
 
 *	webcam_host		char *		webcam.yahoo.com
 
 *	webcam_port		int		5100
 
 *	webcam_description	char *		""
 
 *	local_host		char *		""
 
 *	conn_type		int		Y_WCM_DSL
 
 *
 
 * You should set at least local_host if you intend to use webcams
 
 */
 
	int yahoo_init_with_attributes(const char *username,
 
		const char *password, ...);
 

	
 
/* yahoo_init does the same as yahoo_init_with_attributes, assuming defaults
 
 * for all attributes */
 
	int yahoo_init(const char *username, const char *password);
 

	
 
/* release all resources held by this session */
 
/* you need to call yahoo_close for a session only if
 
 * yahoo_logoff is never called for it (ie, it was never logged in) */
 
	void yahoo_close(int id);
 
/* login logs in to the server */
 
/* initial is of type enum yahoo_status.  see yahoo2_types.h */
 
	void yahoo_login(int id, int initial);
 
	void yahoo_logoff(int id);
 
/* reloads status of all buddies */
 
	void yahoo_refresh(int id);
 
/* activates/deactivates an identity */
 
	void yahoo_set_identity_status(int id, const char *identity,
 
		int active);
 
/* regets the entire buddy list from the server */
 
	void yahoo_get_list(int id);
 
/* download buddy contact information from your yahoo addressbook */
 
	void yahoo_get_yab(int id);
 
/* add/modify an address book entry.  if yab->dbid is set, it will */
 
/* modify that entry else it creates a new entry */
 
	void yahoo_set_yab(int id, struct yab *yab);
 
	void yahoo_keepalive(int id);
 
	void yahoo_chat_keepalive(int id);
 

	
 
/* from is the identity you're sending from.  if NULL, the default is used */
 
/* utf8 is whether msg is a utf8 string or not. */
 
	void yahoo_send_im(int id, const char *from, const char *who,
 
		const char *msg, int utf8, int picture);
 
	void yahoo_send_buzz(int id, const char *from, const char *who);
 
/* if type is true, send typing notice, else send stopped typing notice */
 
	void yahoo_send_typing(int id, const char *from, const char *who,
 
		int typ);
 

	
 
/* used to set away/back status. */
 
/* away says whether the custom message is an away message or a sig */
 
	void yahoo_set_away(int id, enum yahoo_status state, const char *msg,
 
		int away);
 

	
 
	void yahoo_add_buddy(int id, const char *who, const char *group,
 
		const char *msg);
 
	void yahoo_remove_buddy(int id, const char *who, const char *group);
 
	void yahoo_confirm_buddy(int id, const char *who, int reject,
 
		const char *msg);
 
	void yahoo_stealth_buddy(int id, const char *who, int unstealth);
 
/* if unignore is true, unignore, else ignore */
 
	void yahoo_ignore_buddy(int id, const char *who, int unignore);
 
	void yahoo_change_buddy_group(int id, const char *who,
 
		const char *old_group, const char *new_group);
 
	void yahoo_group_rename(int id, const char *old_group,
 
		const char *new_group);
 

	
 
	void yahoo_conference_invite(int id, const char *from, YList *who,
 
		const char *room, const char *msg);
 
	void yahoo_conference_addinvite(int id, const char *from,
 
		const char *who, const char *room, const YList *members,
 
		const char *msg);
 
	void yahoo_conference_decline(int id, const char *from, YList *who,
 
		const char *room, const char *msg);
 
	void yahoo_conference_message(int id, const char *from, YList *who,
 
		const char *room, const char *msg, int utf8);
 
	void yahoo_conference_logon(int id, const char *from, YList *who,
 
		const char *room);
 
	void yahoo_conference_logoff(int id, const char *from, YList *who,
 
		const char *room);
 

	
 
/* Get a list of chatrooms */
 
	void yahoo_get_chatrooms(int id, int chatroomid);
 
/* join room with specified roomname and roomid */
 
	void yahoo_chat_logon(int id, const char *from, const char *room,
 
		const char *roomid);
 
/* Send message "msg" to room with specified roomname, msgtype is 1-normal message or 2-/me mesage */
 
	void yahoo_chat_message(int id, const char *from, const char *room,
 
		const char *msg, const int msgtype, const int utf8);
 
/* Log off chat */
 
	void yahoo_chat_logoff(int id, const char *from);
 

	
 
/* requests a webcam feed */
 
/* who is the person who's webcam you would like to view */
 
/* if who is null, then you're the broadcaster */
 
	void yahoo_webcam_get_feed(int id, const char *who);
 
	void yahoo_webcam_close_feed(int id, const char *who);
 

	
 
/* sends an image when uploading */
 
/* image points to a JPEG-2000 image, length is the length of the image */
 
/* in bytes. The timestamp is the time in milliseconds since we started the */
 
/* webcam. */
 
	void yahoo_webcam_send_image(int id, unsigned char *image,
 
		unsigned int length, unsigned int timestamp);
 

	
 
/* this function should be called if we want to allow a user to watch the */
 
/* webcam. Who is the user we want to accept. */
 
/* Accept user (accept = 1), decline user (accept = 0) */
 
	void yahoo_webcam_accept_viewer(int id, const char *who, int accept);
 

	
 
/* send an invitation to a user to view your webcam */
 
	void yahoo_webcam_invite(int id, const char *who);
 

	
 
/* will set up a connection and initiate file transfer.
 
 * callback will be called with the fd that you should write
 
 * the file data to
 
 */
 
	void yahoo_send_file(int id, const char *who, const char *msg,
 
		const char *name, unsigned long size,
 
		yahoo_get_fd_callback callback, void *data);
 

	
 
/*
 
 * Respond to a file transfer request. Be sure to provide the callback data
 
 * since that is your only chance to recognize future callbacks
 
 */
 
	void yahoo_send_file_transfer_response(int client_id, int response,
 
		char *id, void *data);
 

	
 

	
 
/* send a search request
 
 */
 
	void yahoo_search(int id, enum yahoo_search_type t, const char *text,
 
		enum yahoo_search_gender g, enum yahoo_search_agerange ar,
 
		int photo, int yahoo_only);
 

	
 
/* continue last search
 
 * should be called if only (start+found >= total)
 
 *
 
 * where the above three are passed to ext_yahoo_got_search_result
 
 */
 
	void yahoo_search_again(int id, int start);
 

	
 
/* these should be called when input is available on a fd */
 
/* registered by ext_yahoo_add_handler */
 
/* if these return negative values, errno may be set */
 
	int yahoo_read_ready(int id, void *fd, void *data);
 
	int yahoo_write_ready(int id, void *fd, void *data);
 

	
 
/* utility functions. these do not hit the server */
 
	enum yahoo_status yahoo_current_status(int id);
 
	const YList *yahoo_get_buddylist(int id);
 
	const YList *yahoo_get_ignorelist(int id);
 
	const YList *yahoo_get_identities(int id);
 
/* 'which' could be y, t, c or login.  This may change in later versions. */
 
	const char *yahoo_get_cookie(int id, const char *which);
 

	
 
/* returns the url used to get user profiles - you must append the user id */
 
/* as of now this is http://profiles.yahoo.com/ */
 
/* You'll have to do urlencoding yourself, but see yahoo_httplib.h first */
 
	const char *yahoo_get_profile_url(void);
 

	
 
	void yahoo_buddyicon_request(int id, const char *who);
 

	
 
#include "yahoo_httplib.h"
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 
#endif
backends/libyahoo2/yahoo/yahoo2_callbacks.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo2_callbacks.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
/*
 
 * The functions in this file *must* be defined in your client program
 
 * If you want to use a callback structure instead of direct functions,
 
 * then you must define USE_STRUCT_CALLBACKS in all files that #include
 
 * this one.
 
 *
 
 * Register the callback structure by calling yahoo_register_callbacks -
 
 * declared in this file and defined in libyahoo2.c
 
 */
 

	
 
#ifndef YAHOO2_CALLBACKS_H
 
#define YAHOO2_CALLBACKS_H
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
#include "yahoo2_types.h"
 

	
 
/*
 
 * yahoo2_callbacks.h
 
 *
 
 * Callback interface for libyahoo2
 
 */
 

	
 
	typedef enum {
 
		YAHOO_INPUT_READ = 1 << 0,
 
		YAHOO_INPUT_WRITE = 1 << 1,
 
		YAHOO_INPUT_EXCEPTION = 1 << 2
 
	} yahoo_input_condition;
 

	
 
/*
 
 * A callback function called when an asynchronous connect completes.
 
 * 
 
 * Params:
 
 *     fd    - The file descriptor object that has been connected, or NULL on 
 
 *             error
 
 *     error - The value of errno set by the call to connect or 0 if no error
 
 *	       Set both fd and error to 0 if the connect was cancelled by the
 
 *	       user
 
 *     callback_data - the callback_data passed to the ext_yahoo_connect_async
 
 *	       function
 
 */
 
	typedef void (*yahoo_connect_callback) (void *fd, int error,
 
		void *callback_data);
 

	
 
/*
 
 * The following functions need to be implemented in the client
 
 * interface.  They will be called by the library when each
 
 * event occurs.
 
 */
 

	
 
/* 
 
 * should we use a callback structure or directly call functions
 
 * if you want the structure, you *must* define USE_STRUCT_CALLBACKS
 
 * both when you compile the library, and when you compile your code
 
 * that uses the library
 
 */
 

	
 
#define YAHOO_CALLBACK_TYPE(x)	(*x)
 
	struct yahoo_callbacks {
 
		
 
/*
 
 * Name: ext_yahoo_login_response
 
 * 	Called when the login process is complete
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	succ - enum yahoo_login_status
 
 * 	url  - url to reactivate account if locked
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_login_response) (int id, int succ,
 
		const char *url);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddies
 
 * 	Called when the contact list is got from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	buds - the buddy list
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddies) (int id, YList *buds);
 

	
 
/*
 
 * Name: ext_yahoo_got_ignore
 
 * 	Called when the ignore list is got from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	igns - the ignore list
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_ignore) (int id, YList *igns);
 

	
 
/*
 
 * Name: ext_yahoo_got_identities
 
 * 	Called when the contact list is got from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	ids  - the identity list
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_identities) (int id, YList *ids);
 

	
 
/*
 
 * Name: ext_yahoo_got_cookies
 
 * 	Called when the cookie list is got from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_cookies) (int id);
 

	
 
/*
 
 * Name: ext_yahoo_got_ping
 
 * 	Called when the ping packet is received from the server
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 *  errormsg - optional error message
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_ping) (int id,
 
		const char *errormsg);
 

	
 
/*
 
 * Name: ext_yahoo_status_changed
 
 * 	Called when remote user's status changes.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	who  - the handle of the remote user
 
 * 	stat - status code (enum yahoo_status)
 
 * 	msg  - the message if stat == YAHOO_STATUS_CUSTOM
 
 * 	away - whether the contact is away or not (YAHOO_STATUS_CUSTOM)
 
 * 	idle - this is the number of seconds he is idle [if he is idle]
 
 *	mobile - this is set for mobile users/buddies
 
 *	TODO: add support for pager, chat, and game states
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_status_changed) (int id,
 
		const char *who, int stat, const char *msg, int away, int idle,
 
		int mobile);
 

	
 
/*
 
 * Name: ext_yahoo_got_buzz
 
 * 	Called when remote user sends you a buzz.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the message was sent to
 
 * 	who  - the handle of the remote user
 
 * 	tm   - timestamp of message if offline
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buzz) (int id, const char *me,
 
		const char *who, long tm);
 

	
 
/*
 
 * Name: ext_yahoo_got_im
 
 * 	Called when remote user sends you a message.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the message was sent to
 
 * 	who  - the handle of the remote user
 
 * 	msg  - the message - NULL if stat == 2
 
 * 	tm   - timestamp of message if offline
 
 * 	stat - message status - 0
 
 * 				1
 
 * 				2 == error sending message
 
 * 				5
 
 * 	utf8 - whether the message is encoded as utf8 or not
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_im) (int id, const char *me,
 
		const char *who, const char *msg, long tm, int stat, int utf8);
 

	
 
/*
 
 * Name: ext_yahoo_got_conf_invite
 
 * 	Called when remote user sends you a conference invitation.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the invitation was sent to
 
 * 	who  - the user inviting you
 
 * 	room - the room to join
 
 * 	msg  - the message
 
 *	members - the initial members of the conference (null terminated list)
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_conf_invite) (int id,
 
		const char *me, const char *who, const char *room,
 
		const char *msg, YList *members);
 

	
 
/*
 
 * Name: ext_yahoo_conf_userdecline
 
 * 	Called when someone declines to join the conference.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the conference
 
 * 	who  - the user who has declined
 
 * 	room - the room
 
 * 	msg  - the declining message
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_conf_userdecline) (int id,
 
		const char *me, const char *who, const char *room,
 
		const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_conf_userjoin
 
 * 	Called when someone joins the conference.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the conference
 
 * 	who  - the user who has joined
 
 * 	room - the room joined
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_conf_userjoin) (int id,
 
		const char *me, const char *who, const char *room);
 

	
 
/*
 
 * Name: ext_yahoo_conf_userleave
 
 * 	Called when someone leaves the conference.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the conference
 
 * 	who  - the user who has left
 
 * 	room - the room left
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_conf_userleave) (int id,
 
		const char *me, const char *who, const char *room);
 

	
 
/*
 
 * Name: ext_yahoo_chat_cat_xml
 
 * 	Called when ?
 
 * Params:
 
 * 	id      - the id that identifies the server connection
 
 * 	xml     - ?
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_cat_xml) (int id,
 
		const char *xml);
 

	
 
/*
 
 * Name: ext_yahoo_chat_join
 
 * 	Called when joining the chatroom.
 
 * Params:
 
 * 	id      - the id that identifies the server connection
 
 * 	me   - the identity in the chatroom
 
 * 	room    - the room joined, used in all other chat calls, freed by 
 
 * 	          library after call
 
 * 	topic   - the topic of the room, freed by library after call
 
 *	members - the initial members of the chatroom (null terminated YList 
 
 *	          of yahoo_chat_member's) Must be freed by the client
 
 *	fd	- the object where the connection is coming from (for tracking)
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_join) (int id, const char *me,
 
		const char *room, const char *topic, YList *members, void *fd);
 

	
 
/*
 
 * Name: ext_yahoo_chat_userjoin
 
 * 	Called when someone joins the chatroom.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the chatroom
 
 * 	room - the room joined
 
 * 	who  - the user who has joined, Must be freed by the client
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_userjoin) (int id,
 
		const char *me, const char *room,
 
		struct yahoo_chat_member *who);
 

	
 
/*
 
 * Name: ext_yahoo_chat_userleave
 
 * 	Called when someone leaves the chatroom.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the chatroom
 
 * 	room - the room left
 
 * 	who  - the user who has left (Just the User ID)
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_userleave) (int id,
 
		const char *me, const char *room, const char *who);
 

	
 
/*
 
 * Name: ext_yahoo_chat_message
 
 * 	Called when someone messages in the chatroom.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity in the chatroom
 
 * 	room - the room
 
 * 	who  - the user who messaged (Just the user id)
 
 * 	msg  - the message
 
 * 	msgtype  - 1 = Normal message
 
 * 		   2 = /me type message
 
 * 	utf8 - whether the message is utf8 encoded or not
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_message) (int id,
 
		const char *me, const char *who, const char *room,
 
		const char *msg, int msgtype, int utf8);
 

	
 
/*
 
 *
 
 * Name: ext_yahoo_chat_yahoologout
 
 *	called when yahoo disconnects your chat session
 
 *	Note this is called whenver a disconnect happens, client or server
 
 *	requested. Care should be taken to make sure you know the origin 
 
 *	of the disconnect request before doing anything here (auto-join's etc)
 
 * Params:
 
 *	id   - the id that identifies this connection
 
 * 	me   - the identity in the chatroom
 
 * Returns:
 
 *	nothing.
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_yahoologout) (int id,
 
		const char *me);
 

	
 
/*
 
 *
 
 * Name: ext_yahoo_chat_yahooerror
 
 *	called when yahoo sends back an error to you
 
 *	Note this is called whenver chat message is sent into a room
 
 *	in error (fd not connected, room doesn't exists etc)
 
 *	Care should be taken to make sure you know the origin 
 
 *	of the error before doing anything about it.
 
 * Params:
 
 *	id   - the id that identifies this connection
 
 * 	me   - the identity in the chatroom
 
 * Returns:
 
 *	nothing.
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_yahooerror) (int id,
 
		const char *me);
 

	
 
/*
 
 * Name: ext_yahoo_conf_message
 
 * 	Called when someone messages in the conference.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the conf message was sent to
 
 * 	who  - the user who messaged
 
 * 	room - the room
 
 * 	msg  - the message
 
 * 	utf8 - whether the message is utf8 encoded or not
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_conf_message) (int id,
 
		const char *me, const char *who, const char *room,
 
		const char *msg, int utf8);
 

	
 
/*
 
 * Name: ext_yahoo_got_file
 
 * 	Called when someone sends you a file
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the identity the file was sent to
 
 * 	who  - the user who sent the file
 
 * 	msg  - the message
 
 * 	fname- the file name if direct transfer
 
 * 	fsize- the file size if direct transfer
 
 * 	trid - transfer id. Unique for this transfer
 
 *
 
 * NOTE: Subsequent callbacks for file transfer do not send all of this
 
 * information again since it is wasteful. Implementations are expected to
 
 * save this information and supply it as callback data when the file or
 
 * confirmation is sent
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_file) (int id, const char *me,
 
		const char *who, const char *msg, const char *fname,
 
		unsigned long fesize, char *trid);
 

	
 
/*
 
 * Name: ext_yahoo_got_ft_data
 
 * 	Called multiple times when parts of the file are received
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	in   - The data
 
 * 	len  - Length of the data
 
 * 	data - callback data
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_ft_data) (int id,
 
		const unsigned char *in, int len, void *data);
 

	
 
/*
 
 * Name: ext_yahoo_file_transfer_done
 
 * 	File transfer is done
 
 * Params:
 
 * 	id     - the id that identifies the server connection
 
 * 	result - To notify if it finished successfully or with a failure
 
 * 	data   - callback data
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_file_transfer_done) (int id,
 
		int result, void *data);
 

	
 
/*
 
 * Name: ext_yahoo_contact_added
 
 * 	Called when a contact is added to your list
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	myid - the identity he was added to
 
 * 	who  - who was added
 
 * 	msg  - any message sent
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_contact_added) (int id,
 
		const char *myid, const char *who, const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_rejected
 
 * 	Called when a contact rejects your add
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	who  - who rejected you
 
 * 	msg  - any message sent
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_rejected) (int id, const char *who,
 
		const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_typing_notify
 
 * 	Called when remote user starts or stops typing.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the handle of the identity the notification is sent to
 
 * 	who  - the handle of the remote user
 
 * 	stat - 1 if typing, 0 if stopped typing
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_typing_notify) (int id,
 
		const char *me, const char *who, int stat);
 

	
 
/*
 
 * Name: ext_yahoo_game_notify
 
 * 	Called when remote user starts or stops a game.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the handle of the identity the notification is sent to
 
 * 	who  - the handle of the remote user
 
 * 	stat - 1 if game, 0 if stopped gaming
 
 * 	msg  - game description and/or other text
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_game_notify) (int id, const char *me,
 
		const char *who, int stat, const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_mail_notify
 
 * 	Called when you receive mail, or with number of messages
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	from - who the mail is from - NULL if only mail count
 
 * 	subj - the subject of the mail - NULL if only mail count
 
 * 	cnt  - mail count - 0 if new mail notification
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_mail_notify) (int id,
 
		const char *from, const char *subj, int cnt);
 

	
 
/*
 
 * Name: ext_yahoo_system_message
 
 * 	System message
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - the handle of the identity the notification is sent to
 
 * 	who  - the source of the system message (there are different types)
 
 * 	msg  - the message
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_system_message) (int id,
 
		const char *me, const char *who, const char *msg);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddyicon
 
 * 	Buddy icon received
 
 * Params:
 
 * 	id - the id that identifies the server connection
 
 * 	me - the handle of the identity the notification is sent to
 
 * 	who - the person the buddy icon is for
 
 * 	url - the url to use to load the icon
 
 * 	checksum - the checksum of the icon content
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyicon) (int id,
 
		const char *me, const char *who, const char *url, int checksum);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddyicon_checksum
 
 * 	Buddy icon checksum received
 
 * Params:
 
 * 	id - the id that identifies the server connection
 
 * 	me - the handle of the identity the notification is sent to
 
 * 	who - the yahoo id of the buddy icon checksum is for
 
 * 	checksum - the checksum of the icon content
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyicon_checksum) (int id,
 
		const char *me, const char *who, int checksum);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddyicon_request
 
 * 	Buddy icon request received
 
 * Params:
 
 * 	id - the id that identifies the server connection
 
 * 	me - the handle of the identity the notification is sent to
 
 * 	who - the yahoo id of the buddy that requested the buddy icon
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyicon_request) (int id,
 
		const char *me, const char *who);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddyicon_request
 
 * 	Buddy icon request received
 
 * Params:
 
 * 	id - the id that identifies the server connection
 
 * 	url - remote url, the uploaded buddy icon can be fetched from
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_buddyicon_uploaded) (int id,
 
		const char *url);
 

	
 
/*
 
 * Name: ext_yahoo_got_webcam_image
 
 * 	Called when you get a webcam update
 
 *	An update can either be receiving an image, a part of an image or
 
 *	just an update with a timestamp
 
 * Params:
 
 * 	id         - the id that identifies the server connection
 
 * 	who        - the user who's webcam we're viewing
 
 *	image      - image data
 
 *	image_size - length of the image in bytes
 
 *	real_size  - actual length of image data
 
 *	timestamp  - milliseconds since the webcam started
 
 *
 
 *	If the real_size is smaller then the image_size then only part of
 
 *	the image has been read. This function will keep being called till
 
 *	the total amount of bytes in image_size has been read. The image
 
 *	received is in JPEG-2000 Code Stream Syntax (ISO/IEC 15444-1).
 
 *	The size of the image will be either 160x120 or 320x240.
 
 *	Each webcam image contains a timestamp. This timestamp should be
 
 *	used to keep the image in sync since some images can take longer
 
 *	to transport then others. When image_size is 0 we can still receive
 
 *	a timestamp to stay in sync
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_webcam_image) (int id,
 
		const char *who, const unsigned char *image,
 
		unsigned int image_size, unsigned int real_size,
 
		unsigned int timestamp);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_invite
 
 * 	Called when you get a webcam invitation
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - identity the invitation is to
 
 * 	from - who the invitation is from
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_invite) (int id,
 
		const char *me, const char *from);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_invite_reply
 
 * 	Called when you get a response to a webcam invitation
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	me   - identity the invitation response is to
 
 * 	from - who the invitation response is from
 
 *	accept - 0 (decline), 1 (accept)
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_invite_reply) (int id,
 
		const char *me, const char *from, int accept);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_closed
 
 * 	Called when the webcam connection closed
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	who  - the user who we where connected to
 
 *	reason - reason why the connection closed
 
 *	         1 = user stopped broadcasting
 
 *	         2 = user cancelled viewing permission
 
 *	         3 = user declines permission
 
 *	         4 = user does not have webcam online
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_closed) (int id,
 
		const char *who, int reason);
 

	
 
/*
 
 * Name: ext_yahoo_got_search_result
 
 *      Called when the search result received from server
 
 * Params:
 
 *      id  	 - the id that identifies the server connection
 
 * 	found	 - total number of results returned in the current result set
 
 * 	start	 - offset from where the current result set starts
 
 * 	total	 - total number of results available (start + found <= total)
 
 * 	contacts - the list of results as a YList of yahoo_found_contact
 
 * 		   these will be freed after this function returns, so
 
 * 		   if you need to use the information, make a copy
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_search_result) (int id,
 
		int found, int start, int total, YList *contacts);
 

	
 
/*
 
 * Name: ext_yahoo_error
 
 * 	Called on error.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	err  - the error message
 
 * 	fatal- whether this error is fatal to the connection or not
 
 * 	num  - Which error is this
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_error) (int id, const char *err,
 
		int fatal, int num);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_viewer
 
 *	Called when a viewer disconnects/connects/requests to connect
 
 * Params:
 
 *	id  - the id that identifies the server connection
 
 *	who - the viewer
 
 *	connect - 0=disconnect 1=connect 2=request
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_viewer) (int id,
 
		const char *who, int connect);
 

	
 
/*
 
 * Name: ext_yahoo_webcam_data_request
 
 *	Called when you get a request for webcam images
 
 * Params:
 
 *	id   - the id that identifies the server connection
 
 *	send - whether to send images or not
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_webcam_data_request) (int id,
 
		int send);
 

	
 
/*
 
 * Name: ext_yahoo_log
 
 * 	Called to log a message.
 
 * Params:
 
 * 	fmt  - the printf formatted message
 
 * Returns:
 
 * 	0
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_log) (const char *fmt, ...);
 

	
 
/*
 
 * Name: ext_yahoo_add_handler
 
 * 	Add a listener for the fd.  Must call yahoo_read_ready
 
 * 	when a YAHOO_INPUT_READ fd is ready and yahoo_write_ready
 
 * 	when a YAHOO_INPUT_WRITE fd is ready.
 
 * Params:
 
 * 	id   - the id that identifies the server connection
 
 * 	fd   - the fd object on which to listen
 
 * 	cond - the condition on which to call the callback
 
 * 	data - callback data to pass to yahoo_*_ready
 
 * 	
 
 * Returns: a tag to be used when removing the handler
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_add_handler) (int id, void *fd,
 
		yahoo_input_condition cond, void *data);
 

	
 
/*
 
 * Name: ext_yahoo_remove_handler
 
 * 	Remove the listener for the fd.
 
 * Params:
 
 * 	id   - the id that identifies the connection
 
 * 	tag  - the handler tag to remove
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_remove_handler) (int id, int tag);
 

	
 
/*
 
 * Name: ext_yahoo_connect
 
 * 	Connect to a host:port
 
 * Params:
 
 * 	host - the host to connect to
 
 * 	port - the port to connect on
 
 * Returns:
 
 * 	a unix file descriptor to the socket
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_connect) (const char *host, int port);
 

	
 
/*
 
 * Name: ext_yahoo_connect_async
 
 * 	Connect to a host:port asynchronously. This function should return
 
 * 	immediately returing a tag used to identify the connection handler,
 
 * 	or a pre-connect error (eg: host name lookup failure).
 
 * 	Once the connect completes (successfully or unsuccessfully), callback
 
 * 	should be called (see the signature for yahoo_connect_callback).
 
 * 	The callback may safely be called before this function returns, but
 
 * 	it should not be called twice.
 
 * Params:
 
 * 	id   - the id that identifies this connection
 
 * 	host - the host to connect to
 
 * 	port - the port to connect on
 
 * 	callback - function to call when connect completes
 
 * 	callback_data - data to pass to the callback function
 
 * 	use_ssl - Whether we need an SSL connection
 
 * Returns:
 
 * 	a tag signifying the connection attempt
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_connect_async) (int id,
 
		const char *host, int port, yahoo_connect_callback callback,
 
		void *callback_data, int use_ssl);
 

	
 
/*
 
 * Name: ext_yahoo_get_ip_addr
 
 * 	get IP Address for a domain name
 
 * Params:
 
 * 	domain - Domain name
 
 * Returns:
 
 * 	Newly allocated string containing the IP Address in IPv4 notation
 
 */
 
	char *YAHOO_CALLBACK_TYPE(ext_yahoo_get_ip_addr) (const char *domain);
 

	
 
/*
 
 * Name: ext_yahoo_write
 
 * 	Write data from the buffer into the socket for the specified connection
 
 * Params:
 
 * 	fd  - the file descriptor object that identifies this connection
 
 * 	buf - Buffer to write the data from
 
 * 	len - Length of the data
 
 * Returns:
 
 * 	Number of bytes written or -1 for error
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_write) (void *fd, char *buf, int len);
 

	
 
/*
 
 * Name: ext_yahoo_read
 
 * 	Read data into a buffer from socket for the specified connection
 
 * Params:
 
 * 	fd  - the file descriptor object that identifies this connection
 
 * 	buf - Buffer to read the data into
 
 * 	len - Max length to read
 
 * Returns:
 
 * 	Number of bytes read or -1 for error
 
 */
 
	int YAHOO_CALLBACK_TYPE(ext_yahoo_read) (void *fd, char *buf, int len);
 

	
 
/*
 
 * Name: ext_yahoo_close
 
 * 	Close the file descriptor object and free its resources. Libyahoo2 will not
 
 * 	use this object again.
 
 * Params:
 
 * 	fd  - the file descriptor object that identifies this connection
 
 * Returns:
 
 * 	Nothing
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_close) (void *fd);
 

	
 
/*
 
 * Name: ext_yahoo_got_buddy_change_group
 
 * 	Acknowledgement of buddy changing group
 
 * Params:
 
 * 	id: client id
 
 * 	me: The user
 
 * 	who: Buddy name
 
 * 	old_group: Old group name
 
 * 	new_group: New group name
 
 * Returns:
 
 * 	Nothing
 
 */
 
	void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddy_change_group) (int id,
 
		const char *me, const char *who, const char *old_group,
 
		const char *new_group);
 

	
 
};
 

	
 
/*
 
 * if using a callback structure, call yahoo_register_callbacks
 
 * before doing anything else
 
 */
 
void yahoo_register_callbacks(struct yahoo_callbacks *tyc);
 

	
 
#undef YAHOO_CALLBACK_TYPE
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 

	
 
#endif
backends/libyahoo2/yahoo/yahoo2_types.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo2_types.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#ifndef YAHOO2_TYPES_H
 
#define YAHOO2_TYPES_H
 

	
 
#include "yahoo_list.h"
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
	enum yahoo_service {	/* these are easier to see in hex */
 
		YAHOO_SERVICE_LOGON = 1,
 
		YAHOO_SERVICE_LOGOFF,
 
		YAHOO_SERVICE_ISAWAY,
 
		YAHOO_SERVICE_ISBACK,
 
		YAHOO_SERVICE_IDLE,	/* 5 (placemarker) */
 
		YAHOO_SERVICE_MESSAGE,
 
		YAHOO_SERVICE_IDACT,
 
		YAHOO_SERVICE_IDDEACT,
 
		YAHOO_SERVICE_MAILSTAT,
 
		YAHOO_SERVICE_USERSTAT,	/* 0xa */
 
		YAHOO_SERVICE_NEWMAIL,
 
		YAHOO_SERVICE_CHATINVITE,
 
		YAHOO_SERVICE_CALENDAR,
 
		YAHOO_SERVICE_NEWPERSONALMAIL,
 
		YAHOO_SERVICE_NEWCONTACT,
 
		YAHOO_SERVICE_ADDIDENT,	/* 0x10 */
 
		YAHOO_SERVICE_ADDIGNORE,
 
		YAHOO_SERVICE_PING,
 
		YAHOO_SERVICE_GOTGROUPRENAME,	/* < 1, 36(old), 37(new) */
 
		YAHOO_SERVICE_SYSMESSAGE = 0x14,
 
		YAHOO_SERVICE_SKINNAME = 0x15,
 
		YAHOO_SERVICE_PASSTHROUGH2 = 0x16,
 
		YAHOO_SERVICE_CONFINVITE = 0x18,
 
		YAHOO_SERVICE_CONFLOGON,
 
		YAHOO_SERVICE_CONFDECLINE,
 
		YAHOO_SERVICE_CONFLOGOFF,
 
		YAHOO_SERVICE_CONFADDINVITE,
 
		YAHOO_SERVICE_CONFMSG,
 
		YAHOO_SERVICE_CHATLOGON,
 
		YAHOO_SERVICE_CHATLOGOFF,
 
		YAHOO_SERVICE_CHATMSG = 0x20,
 
		YAHOO_SERVICE_GAMELOGON = 0x28,
 
		YAHOO_SERVICE_GAMELOGOFF,
 
		YAHOO_SERVICE_GAMEMSG = 0x2a,
 
		YAHOO_SERVICE_FILETRANSFER = 0x46,
 
		YAHOO_SERVICE_VOICECHAT = 0x4A,
 
		YAHOO_SERVICE_NOTIFY,
 
		YAHOO_SERVICE_VERIFY,
 
		YAHOO_SERVICE_P2PFILEXFER,
 
		YAHOO_SERVICE_PEERTOPEER = 0x4F,	/* Checks if P2P possible */
 
		YAHOO_SERVICE_WEBCAM,
 
		YAHOO_SERVICE_AUTHRESP = 0x54,
 
		YAHOO_SERVICE_LIST,
 
		YAHOO_SERVICE_AUTH = 0x57,
 
		YAHOO_SERVICE_AUTHBUDDY = 0x6d,
 
		YAHOO_SERVICE_ADDBUDDY = 0x83,
 
		YAHOO_SERVICE_REMBUDDY,
 
		YAHOO_SERVICE_IGNORECONTACT,	/* > 1, 7, 13 < 1, 66, 13, 0 */
 
		YAHOO_SERVICE_REJECTCONTACT,
 
		YAHOO_SERVICE_GROUPRENAME = 0x89,	/* > 1, 65(new), 66(0), 67(old) */
 
		YAHOO_SERVICE_Y7_PING = 0x8A,
 
		YAHOO_SERVICE_CHATONLINE = 0x96,	/* > 109(id), 1, 6(abcde) < 0,1 */
 
		YAHOO_SERVICE_CHATGOTO,
 
		YAHOO_SERVICE_CHATJOIN,	/* > 1 104-room 129-1600326591 62-2 */
 
		YAHOO_SERVICE_CHATLEAVE,
 
		YAHOO_SERVICE_CHATEXIT = 0x9b,
 
		YAHOO_SERVICE_CHATADDINVITE = 0x9d,
 
		YAHOO_SERVICE_CHATLOGOUT = 0xa0,
 
		YAHOO_SERVICE_CHATPING,
 
		YAHOO_SERVICE_COMMENT = 0xa8,
 
		YAHOO_SERVICE_GAME_INVITE = 0xb7,
 
		YAHOO_SERVICE_STEALTH_PERM = 0xb9,
 
		YAHOO_SERVICE_STEALTH_SESSION = 0xba,
 
		YAHOO_SERVICE_AVATAR = 0xbc,
 
		YAHOO_SERVICE_PICTURE_CHECKSUM = 0xbd,
 
		YAHOO_SERVICE_PICTURE = 0xbe,
 
		YAHOO_SERVICE_PICTURE_UPDATE = 0xc1,
 
		YAHOO_SERVICE_PICTURE_UPLOAD = 0xc2,
 
		YAHOO_SERVICE_YAB_UPDATE = 0xc4,
 
		YAHOO_SERVICE_Y6_VISIBLE_TOGGLE = 0xc5,	/* YMSG13, key 13: 2 = invisible, 1 = visible */
 
		YAHOO_SERVICE_Y6_STATUS_UPDATE = 0xc6,	/* YMSG13 */
 
		YAHOO_SERVICE_PICTURE_STATUS = 0xc7,	/* YMSG13, key 213: 0 = none, 1 = avatar, 2 = picture */
 
		YAHOO_SERVICE_VERIFY_ID_EXISTS = 0xc8,
 
		YAHOO_SERVICE_AUDIBLE = 0xd0,
 
		YAHOO_SERVICE_Y7_PHOTO_SHARING = 0xd2,
 
		YAHOO_SERVICE_Y7_CONTACT_DETAILS = 0xd3,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_CHAT_SESSION = 0xd4,
 
		YAHOO_SERVICE_Y7_AUTHORIZATION = 0xd6,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_FILETRANSFER = 0xdc,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_FILETRANSFERINFO,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_FILETRANSFERACCEPT,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_MINGLE = 0xe1,	/* YMSG13 */
 
		YAHOO_SERVICE_Y7_CHANGE_GROUP = 0xe7,	/* YMSG13 */
 
		YAHOO_SERVICE_MYSTERY = 0xef,	/* Don't know what this is for */
 
		YAHOO_SERVICE_Y8_STATUS = 0xf0,	/* YMSG15 */
 
		YAHOO_SERVICE_Y8_LIST = 0Xf1,	/* YMSG15 */
 
		YAHOO_SERVICE_MESSAGE_CONFIRM = 0xfb,
 
		YAHOO_SERVICE_WEBLOGIN = 0x0226,
 
		YAHOO_SERVICE_SMS_MSG = 0x02ea
 
	};
 

	
 
	enum yahoo_status {
 
		YAHOO_STATUS_AVAILABLE = 0,
 
		YAHOO_STATUS_BRB,
 
		YAHOO_STATUS_BUSY,
 
		YAHOO_STATUS_NOTATHOME,
 
		YAHOO_STATUS_NOTATDESK,
 
		YAHOO_STATUS_NOTINOFFICE,
 
		YAHOO_STATUS_ONPHONE,
 
		YAHOO_STATUS_ONVACATION,
 
		YAHOO_STATUS_OUTTOLUNCH,
 
		YAHOO_STATUS_STEPPEDOUT,
 
		YAHOO_STATUS_INVISIBLE = 12,
 
		YAHOO_STATUS_CUSTOM = 99,
 
		YAHOO_STATUS_IDLE = 999,
 
		YAHOO_STATUS_OFFLINE = 0x5a55aa56	/* don't ask */
 
	};
 

	
 
	enum ypacket_status {
 
		YPACKET_STATUS_DISCONNECTED = -1,
 
		YPACKET_STATUS_DEFAULT = 0,
 
		YPACKET_STATUS_SERVERACK = 1,
 
		YPACKET_STATUS_GAME = 0x2,
 
		YPACKET_STATUS_AWAY = 0x4,
 
		YPACKET_STATUS_CONTINUED = 0x5,
 
		YPACKET_STATUS_INVISIBLE = 12,
 
		YPACKET_STATUS_NOTIFY = 0x16,	/* TYPING */
 
		YPACKET_STATUS_WEBLOGIN = 0x5a55aa55,
 
		YPACKET_STATUS_OFFLINE = 0x5a55aa56
 
	};
 

	
 
#define YAHOO_STATUS_GAME	0x2	/* Games don't fit into the regular status model */
 

	
 
	enum yahoo_login_status {
 
		YAHOO_LOGIN_OK = 0,
 
		YAHOO_LOGIN_LOGOFF = 1,
 
		YAHOO_LOGIN_UNAME = 3,
 
		YAHOO_LOGIN_PASSWD = 13,
 
		YAHOO_LOGIN_LOCK = 14,
 
		YAHOO_LOGIN_DUPL = 99,
 
		YAHOO_LOGIN_SOCK = -1,
 
		YAHOO_LOGIN_UNKNOWN = 999
 
	};
 

	
 
	enum yahoo_error {
 
		E_UNKNOWN = -1,
 
		E_CONNECTION = -2,
 
		E_SYSTEM = -3,
 
		E_CUSTOM = 0,
 

	
 
		/* responses from ignore buddy */
 
		E_IGNOREDUP = 2,
 
		E_IGNORENONE = 3,
 
		E_IGNORECONF = 12,
 

	
 
		/* conference */
 
		E_CONFNOTAVAIL = 20
 
	};
 

	
 
	enum yahoo_log_level {
 
		YAHOO_LOG_NONE = 0,
 
		YAHOO_LOG_FATAL,
 
		YAHOO_LOG_ERR,
 
		YAHOO_LOG_WARNING,
 
		YAHOO_LOG_NOTICE,
 
		YAHOO_LOG_INFO,
 
		YAHOO_LOG_DEBUG
 
	};
 

	
 
	enum yahoo_file_transfer {
 
		YAHOO_FILE_TRANSFER_INIT = 1,
 
		YAHOO_FILE_TRANSFER_ACCEPT = 3,
 
		YAHOO_FILE_TRANSFER_REJECT = 4,
 
		YAHOO_FILE_TRANSFER_DONE = 5,
 
		YAHOO_FILE_TRANSFER_RELAY,
 
		YAHOO_FILE_TRANSFER_FAILED,
 
		YAHOO_FILE_TRANSFER_UNKNOWN
 
	};
 

	
 
#define YAHOO_PROTO_VER 0x0010
 

	
 
/* Yahoo style/color directives */
 
#define YAHOO_COLOR_BLACK "\033[30m"
 
#define YAHOO_COLOR_BLUE "\033[31m"
 
#define YAHOO_COLOR_LIGHTBLUE "\033[32m"
 
#define YAHOO_COLOR_GRAY "\033[33m"
 
#define YAHOO_COLOR_GREEN "\033[34m"
 
#define YAHOO_COLOR_PINK "\033[35m"
 
#define YAHOO_COLOR_PURPLE "\033[36m"
 
#define YAHOO_COLOR_ORANGE "\033[37m"
 
#define YAHOO_COLOR_RED "\033[38m"
 
#define YAHOO_COLOR_OLIVE "\033[39m"
 
#define YAHOO_COLOR_ANY "\033[#"
 
#define YAHOO_STYLE_ITALICON "\033[2m"
 
#define YAHOO_STYLE_ITALICOFF "\033[x2m"
 
#define YAHOO_STYLE_BOLDON "\033[1m"
 
#define YAHOO_STYLE_BOLDOFF "\033[x1m"
 
#define YAHOO_STYLE_UNDERLINEON "\033[4m"
 
#define YAHOO_STYLE_UNDERLINEOFF "\033[x4m"
 
#define YAHOO_STYLE_URLON "\033[lm"
 
#define YAHOO_STYLE_URLOFF "\033[xlm"
 

	
 
	enum yahoo_connection_type {
 
		YAHOO_CONNECTION_PAGER = 0,
 
		YAHOO_CONNECTION_FT,
 
		YAHOO_CONNECTION_YAB,
 
		YAHOO_CONNECTION_WEBCAM_MASTER,
 
		YAHOO_CONNECTION_WEBCAM,
 
		YAHOO_CONNECTION_CHATCAT,
 
		YAHOO_CONNECTION_SEARCH,
 
		YAHOO_CONNECTION_AUTH
 
	};
 

	
 
	enum yahoo_webcam_direction_type {
 
		YAHOO_WEBCAM_DOWNLOAD = 0,
 
		YAHOO_WEBCAM_UPLOAD
 
	};
 

	
 
	enum yahoo_stealth_visibility_type {
 
		YAHOO_STEALTH_DEFAULT = 0,
 
		YAHOO_STEALTH_ONLINE,
 
		YAHOO_STEALTH_PERM_OFFLINE
 
	};
 

	
 
/* chat member attribs */
 
#define YAHOO_CHAT_MALE 0x8000
 
#define YAHOO_CHAT_FEMALE 0x10000
 
#define YAHOO_CHAT_FEMALE 0x10000
 
#define YAHOO_CHAT_DUNNO 0x400
 
#define YAHOO_CHAT_WEBCAM 0x10
 

	
 
	enum yahoo_webcam_conn_type { Y_WCM_DIALUP, Y_WCM_DSL, Y_WCM_T1 };
 

	
 
	struct yahoo_webcam {
 
		int direction;	/* Uploading or downloading */
 
		int conn_type;	/* 0=Dialup, 1=DSL/Cable, 2=T1/Lan */
 

	
 
		char *user;	/* user we are viewing */
 
		char *server;	/* webcam server to connect to */
 
		int port;	/* webcam port to connect on */
 
		char *key;	/* key to connect to the server with */
 
		char *description;	/* webcam description */
 
		char *my_ip;	/* own ip number */
 
	};
 

	
 
	struct yahoo_webcam_data {
 
		unsigned int data_size;
 
		unsigned int to_read;
 
		unsigned int timestamp;
 
		unsigned char packet_type;
 
	};
 

	
 
	struct yahoo_data {
 
		char *user;
 
		char *password;
 

	
 
		char *cookie_y;
 
		char *cookie_t;
 
		char *cookie_c;
 
		char *cookie_b;
 
		char *login_cookie;
 
		char *crumb;
 
		char *seed;
 

	
 
		YList *buddies;
 
		YList *ignore;
 
		YList *identities;
 
		char *login_id;
 

	
 
		int current_status;
 
		int initial_status;
 
		int logged_in;
 

	
 
		int session_id;
 

	
 
		int client_id;
 

	
 
		char *rawbuddylist;
 
		char *ignorelist;
 

	
 
		void *server_settings;
 

	
 
		struct yahoo_process_status_entry *half_user;
 
	};
 

	
 
	struct yab {
 
		int yid;
 
		char *id;
 
		char *fname;
 
		char *lname;
 
		char *nname;
 
		char *email;
 
		char *hphone;
 
		char *wphone;
 
		char *mphone;
 
		int dbid;
 
	};
 

	
 
	struct yahoo_buddy {
 
		char *group;
 
		char *id;
 
		char *real_name;
 
		struct yab *yab_entry;
 
	};
 

	
 
	enum yahoo_search_type {
 
		YAHOO_SEARCH_KEYWORD = 0,
 
		YAHOO_SEARCH_YID,
 
		YAHOO_SEARCH_NAME
 
	};
 

	
 
	enum yahoo_search_gender {
 
		YAHOO_GENDER_NONE = 0,
 
		YAHOO_GENDER_MALE,
 
		YAHOO_GENDER_FEMALE
 
	};
 

	
 
	enum yahoo_search_agerange {
 
		YAHOO_AGERANGE_NONE = 0
 
	};
 

	
 
	struct yahoo_found_contact {
 
		char *id;
 
		char *gender;
 
		char *location;
 
		int age;
 
		int online;
 
	};
 

	
 
/*
 
 * Function pointer to be passed to http get/post and send file
 
 */
 
	typedef void (*yahoo_get_fd_callback) (int id, void *fd, int error,
 
		void *data);
 

	
 
/*
 
 * Function pointer to be passed to yahoo_get_url_handle
 
 */
 
	typedef void (*yahoo_get_url_handle_callback) (int id, void *fd,
 
		int error, const char *filename, unsigned long size,
 
		void *data);
 

	
 
	struct yahoo_chat_member {
 
		char *id;
 
		int age;
 
		int attribs;
 
		char *alias;
 
		char *location;
 
	};
 

	
 
	struct yahoo_process_status_entry { 
 
		char *name;     /* 7      name */ 
 
		int state;      /* 10     state */ 
 
		int flags;      /* 13     flags, bit 0 = pager, bit 1 = chat, bit 2 = game */ 
 
		int mobile;     /* 60     mobile */ 
 
		char *msg;      /* 19     custom status message */ 
 
		int away;       /* 47     away (or invisible) */ 
 
		int buddy_session; /* 11  state */ 
 
		int f17;        /* 17     in chat? then what about flags? */ 
 
		int idle;       /* 137    seconds idle */ 
 
		int f138;       /* 138    state */ 
 
		char *f184;     /* 184    state */ 
 
		int f192;       /* 192    state */ 
 
		int f10001;     /* 10001  state */ 
 
		int f10002;     /* 10002  state */ 
 
		int f198;       /* 198    state */ 
 
		char *f197;     /* 197    state */ 
 
		char *f205;     /* 205    state */ 
 
		int f213;       /* 213    state */ 
 
	};
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 
#endif
backends/libyahoo2/yahoo/yahoo_debug.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo_debug.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
extern int yahoo_log_message(char *fmt, ...);
 

	
 
#define NOTICE(x) if(yahoo_get_log_level() >= YAHOO_LOG_NOTICE) { yahoo_log_message x; yahoo_log_message("\n"); }
 

	
 
#define LOG(x) if(yahoo_get_log_level() >= YAHOO_LOG_INFO) { yahoo_log_message("%s:%d: ", __FILE__, __LINE__); \
 
	yahoo_log_message x; \
 
	yahoo_log_message("\n"); }
 

	
 
#define WARNING(x) if(yahoo_get_log_level() >= YAHOO_LOG_WARNING) { yahoo_log_message("%s:%d: warning: ", __FILE__, __LINE__); \
 
	yahoo_log_message x; \
 
	yahoo_log_message("\n"); }
 

	
 
#define DEBUG_MSG(x) if(yahoo_get_log_level() >= YAHOO_LOG_DEBUG) { yahoo_log_message("%s:%d: debug: ", __FILE__, __LINE__); \
 
	yahoo_log_message x; \
 
	yahoo_log_message("\n"); }
backends/libyahoo2/yahoo/yahoo_fn.c
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2 - originally from gaim patches by Amatus
 
 *
 
 * Copyright (C) 2003-2004
 
 *
 
 * Some code copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
 
 * libfaim code copyright 1998, 1999 Adam Fritzler <afritz@auk.cx>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#include "yahoo_fn.h"
 

	
 
unsigned char table_0[256] = {
 
	0x5A, 0x41, 0x11, 0x77, 0x29, 0x9C, 0x31, 0xAD,
 
	0x4A, 0x32, 0x1A, 0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, 0x8C, 0x7A, 0x98,
 
	0x21, 0x95, 0x10, 0x8D, 0xDF, 0xC0, 0x69, 0xC8,
 
	0x03, 0x6E, 0x4B, 0x36, 0xFC, 0x6F, 0xA9, 0x48,
 
	0x63, 0xE1, 0xB9, 0x24, 0x87, 0x13, 0xB2, 0xA4,
 
	0x84, 0x06, 0x14, 0x61, 0x3D, 0x92, 0xB1, 0x41,
 
	0xE2, 0x71, 0xAF, 0x16, 0xDE, 0x25, 0x82, 0xD9,
 
	0x2B, 0x33, 0x51, 0xA2, 0x4E, 0x7D, 0x94, 0xFF,
 
	0xFD, 0x5F, 0x80, 0xED, 0x64, 0xE7, 0x50, 0x6D,
 
	0xD0, 0x3C, 0x6B, 0x65, 0x77, 0x17, 0x1A, 0xEC,
 
	0xD5, 0xAA, 0xF9, 0xC4, 0x9C, 0x35, 0xE3, 0x42,
 
	0xE4, 0x19, 0x52, 0x67, 0xB7, 0x9D, 0x28, 0xC5,
 
	0x47, 0x38, 0x91, 0x57, 0xAE, 0x3F, 0x29, 0x9A,
 
	0x2F, 0xF7, 0x90, 0x04, 0xEE, 0xFA, 0x20, 0xB6,
 
	0xEA, 0x49, 0x23, 0x4D, 0xB3, 0x8E, 0xC3, 0x1F,
 
	0x7C, 0xEF, 0xE0, 0x99, 0x09, 0xA0, 0x01, 0x7E
 
};
 

	
 
unsigned char table_4[32] = {
 
	0x1F, 0x0B, 0x00, 0x1E, 0x03, 0x0E, 0x15, 0x01,
 
	0x1A, 0x17, 0x1D, 0x1B, 0x11, 0x0F, 0x0A, 0x12,
 
	0x13, 0x18, 0x02, 0x04, 0x09, 0x06, 0x0D, 0x07,
 
	0x08, 0x05, 0x10, 0x19, 0x0C, 0x14, 0x16, 0x1C
 
};
 

	
 
unsigned char table_5[256] = {
 
	0x9A, 0xAB, 0x61, 0x28, 0x0A, 0x23, 0xFC, 0xBA,
 
	0x90, 0x22, 0xB7, 0x62, 0xD9, 0x09, 0x91, 0xF4,
 
	0x7B, 0x5D, 0x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 0xC5, 0x9D,
 
	0x63, 0x19, 0x79, 0x57, 0x96, 0x30, 0x74, 0xBB,
 
	0xDA, 0x1A, 0x9F, 0x44, 0xC1, 0x98, 0xE5, 0x81,
 
	0xD6, 0x18, 0x8F, 0xFD, 0x8E, 0x06, 0x6F, 0xF6,
 
	0x2E, 0x3B, 0xB5, 0x85, 0x8A, 0x9C, 0x53, 0x4A,
 
	0xA9, 0x52, 0x3D, 0x4E, 0xBE, 0xAF, 0xBC, 0xA8,
 
	0x4F, 0x6D, 0x15, 0x35, 0x8C, 0xBD, 0x34, 0x8B,
 
	0xDC, 0x0B, 0xCF, 0x31, 0xEA, 0xB2, 0x70, 0x4B,
 
	0x46, 0x73, 0x69, 0xD5, 0x10, 0xEE, 0x02, 0xEF
 
};
 

	
 
unsigned char table_6[32] = {
 
	0x1A, 0x1C, 0x0F, 0x0C, 0x00, 0x02, 0x13, 0x09,
 
	0x11, 0x05, 0x0D, 0x12, 0x18, 0x0B, 0x04, 0x10,
 
	0x14, 0x1B, 0x1E, 0x16, 0x07, 0x08, 0x03, 0x17,
 
	0x19, 0x1F, 0x01, 0x0E, 0x15, 0x06, 0x0A, 0x1D
 
};
 

	
 
unsigned char table_7[256] = {
 
	0x52, 0x11, 0x72, 0xD0, 0x76, 0xD7, 0xAE, 0x03,
 
	0x7F, 0x19, 0xF4, 0xB8, 0xB3, 0x5D, 0xCA, 0x2D,
 
	0x5C, 0x30, 0x53, 0x1A, 0x57, 0xF6, 0xAD, 0x83,
 
	0x29, 0x79, 0xD5, 0xF0, 0x0F, 0xC3, 0x8B, 0xD3,
 
	0x8E, 0x37, 0x01, 0xA6, 0xF1, 0x10, 0x04, 0x71,
 
	0xCC, 0xC6, 0xE7, 0xC2, 0x85, 0x94, 0xBD, 0x6F,
 
	0xCB, 0xEA, 0xFC, 0xA1, 0x38, 0x5E, 0x08, 0x2E,
 
	0x35, 0x42, 0x67, 0xD4, 0x56, 0x6D, 0x7C, 0xE5,
 
	0x0E, 0x7D, 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 0x00, 0xB9,
 
	0x36, 0xE6, 0x78, 0x4F, 0xC9, 0xE9, 0x2C, 0x43,
 
	0x88, 0x9E, 0x9C, 0x5B, 0x4D, 0x3A, 0x39, 0xEF
 
};
 

	
 
unsigned char table_8[32] = {
 
	0x13, 0x08, 0x1E, 0x1D, 0x17, 0x16, 0x07, 0x1F,
 
	0x0E, 0x03, 0x1A, 0x19, 0x01, 0x12, 0x11, 0x10,
 
	0x09, 0x0C, 0x0F, 0x14, 0x0B, 0x05, 0x00, 0x04,
 
	0x1C, 0x18, 0x0A, 0x15, 0x02, 0x1B, 0x06, 0x0D
 
};
 

	
 
unsigned char table_9[256] = {
 
	0x20, 0x2A, 0xDA, 0xFE, 0x76, 0x0D, 0xED, 0x39,
 
	0x51, 0x4C, 0x46, 0x9A, 0xF1, 0xB0, 0x10, 0xC7,
 
	0xD1, 0x6F, 0x18, 0x24, 0xB9, 0x7A, 0x4F, 0x47,
 
	0xE0, 0x4E, 0x88, 0x09, 0x8A, 0xBA, 0x60, 0xBD,
 
	0xC2, 0x27, 0x93, 0x7D, 0x94, 0x40, 0xCB, 0x80,
 
	0xB8, 0x41, 0x84, 0x5D, 0xC1, 0x0F, 0x5E, 0x78,
 
	0x2B, 0x48, 0x28, 0x29, 0xEE, 0x81, 0x90, 0x86,
 
	0x50, 0x9C, 0xF3, 0xB2, 0x35, 0x52, 0x0C, 0x9D,
 
	0xFC, 0x69, 0xD6, 0xA6, 0x06, 0xD7, 0xC6, 0xFF,
 
	0x1C, 0x14, 0x57, 0x33, 0xE2, 0x1F, 0x83, 0xA8,
 
	0xF7, 0x99, 0xC5, 0xDC, 0x70, 0x9E, 0xF4, 0x6B,
 
	0x0A, 0x77, 0x95, 0x4A, 0x2E, 0x53, 0xF2, 0x62,
 
	0x98, 0xF8, 0x96, 0xDB, 0xE6, 0x32, 0x3C, 0x58,
 
	0xD5, 0x6D, 0xE7, 0x4B, 0xCE, 0x91, 0x43, 0xD8,
 
	0xFA, 0xE3, 0x4D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4C, 0xF5, 0x32, 0x23, 0x56, 0x5A, 0xEB, 0x97,
 
	0x34, 0x00, 0x77, 0x71, 0x4B, 0x70, 0xD5, 0x31,
 
	0x72, 0x05, 0xDF, 0xE8, 0x15, 0x3B, 0x54, 0x16,
 
	0x89, 0xE4, 0xF1, 0xD7, 0x80, 0x82, 0x4A, 0xE3,
 
	0x39, 0x06, 0x47, 0x28, 0xC2, 0x86, 0x87, 0xB1,
 
	0x62, 0x74, 0x53, 0x21, 0x67, 0x38, 0x42, 0xCA,
 
	0x9B, 0xC3, 0x51, 0x99, 0x8B, 0x1F, 0x24, 0x8D,
 
	0xF8, 0x68, 0x3E, 0x59, 0xBB, 0x61, 0x5F, 0xBC,
 
	0x09, 0x6E, 0x8F, 0x0F, 0x2D, 0xC0, 0xE0, 0x46,
 
	0x66, 0x69, 0xA5, 0xE9, 0x30, 0x9C, 0x5E, 0xAE,
 
	0xBF, 0xC7, 0x20, 0x7E, 0x6A, 0xC5, 0x88, 0xFC,
 
	0x64, 0x76, 0xFF, 0x9F, 0x2E, 0x02, 0xCC, 0x57
 
};
 

	
 
unsigned char table_12[32] = {
 
	0x14, 0x1B, 0x18, 0x00, 0x1F, 0x15, 0x17, 0x07,
 
	0x11, 0x1A, 0x0E, 0x13, 0x12, 0x06, 0x01, 0x03,
 
	0x1C, 0x0C, 0x0B, 0x1D, 0x10, 0x0F, 0x09, 0x19,
 
	0x0D, 0x1E, 0x04, 0x05, 0x08, 0x16, 0x0A, 0x02
 
};
 

	
 
unsigned char table_13[256] = {
 
	0x37, 0x8A, 0x1B, 0x91, 0xA5, 0x2B, 0x2D, 0x88,
 
	0x8E, 0xFE, 0x0E, 0xD3, 0xF3, 0xE9, 0x7D, 0xD1,
 
	0x24, 0xEA, 0xB1, 0x8B, 0x5C, 0xA4, 0x44, 0x7E,
 
	0x8C, 0x2C, 0x73, 0xD5, 0x50, 0x3E, 0xD7, 0x18,
 
	0xB9, 0xD6, 0xBA, 0x94, 0x0C, 0xFC, 0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0x64, 0xB6, 0x02, 0xDB, 0x55, 0x32,
 
	0xAF, 0x9A, 0xC0, 0x1C, 0x12, 0x29, 0x0B, 0x72,
 
	0x4F, 0xDA, 0xAB, 0x35, 0xF8, 0x22, 0xD9, 0x4E,
 
	0x3D, 0x1E, 0xDC, 0x58, 0x20, 0x34, 0xAE, 0x66,
 
	0x75, 0x93, 0x9F, 0x3A, 0x07, 0xE5, 0x89, 0xDF,
 
	0x97, 0x4A, 0xB8, 0x7A, 0xF4, 0xFB, 0x04, 0xA8
 
};
 

	
 
unsigned char table_14[32] = {
 
	0x04, 0x14, 0x13, 0x15, 0x1A, 0x1B, 0x0F, 0x16,
 
	0x02, 0x0D, 0x0C, 0x06, 0x10, 0x17, 0x01, 0x0B,
 
	0x1E, 0x08, 0x1C, 0x18, 0x19, 0x0A, 0x1F, 0x05,
 
	0x11, 0x09, 0x1D, 0x07, 0x0E, 0x12, 0x03, 0x00
 
};
 

	
 
unsigned char table_15[256] = {
 
	0x61, 0x48, 0x58, 0x41, 0x7F, 0x88, 0x43, 0x42,
 
	0xD9, 0x80, 0x81, 0xFE, 0xC6, 0x49, 0xD7, 0x2C,
 
	0xE6, 0x5B, 0xEE, 0xFF, 0x2A, 0x6F, 0xBF, 0x98,
 
	0xD6, 0x20, 0xB9, 0xB1, 0x5D, 0x95, 0x72, 0x1E,
 
	0x82, 0x96, 0xDE, 0xC1, 0x40, 0xD8, 0x70, 0xA3,
 
	0xD1, 0x1F, 0xF0, 0x9F, 0x2D, 0xDC, 0x3F, 0xF9,
 
	0x5E, 0x0D, 0x15, 0x2F, 0x67, 0x31, 0x9D, 0x84,
 
	0x97, 0x0C, 0xF6, 0x79, 0xC2, 0xA7, 0xC0, 0x32,
 
	0xB3, 0xEB, 0xED, 0x71, 0x30, 0xCC, 0x4B, 0xA0,
 
	0xF5, 0xC4, 0xCD, 0x27, 0xFA, 0x11, 0x25, 0xDB,
 
	0x4F, 0xE2, 0x7E, 0xA6, 0xAF, 0x34, 0x69, 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, 0xF6,
 
	0x1E, 0x0D, 0xAE, 0x7B, 0x5E, 0x61, 0xE9, 0x63
 
};
 

	
 
unsigned char table_20[32] = {
 
	0x0D, 0x0B, 0x11, 0x02, 0x05, 0x1B, 0x08, 0x1D,
 
	0x04, 0x14, 0x01, 0x09, 0x00, 0x19, 0x1E, 0x15,
 
	0x1F, 0x0A, 0x0F, 0x1C, 0x10, 0x16, 0x0C, 0x07,
 
	0x13, 0x1A, 0x06, 0x17, 0x0E, 0x12, 0x18, 0x03
 
};
 

	
 
unsigned char table_21[256] = {
 
	0x4C, 0x94, 0xAD, 0x66, 0x9E, 0x69, 0x04, 0xA8,
 
	0x61, 0xE0, 0xE1, 0x3D, 0xFD, 0x9C, 0xFB, 0x19,
 
	0x1E, 0x80, 0x8C, 0xA0, 0xFC, 0x27, 0x26, 0x3B,
 
	0x48, 0x6D, 0x07, 0xE4, 0xEA, 0x17, 0x64, 0x9B,
 
	0xD0, 0xE2, 0xD1, 0x13, 0x39, 0xF5, 0x73, 0xD3,
 
	0x0C, 0x3A, 0x6E, 0x77, 0xFA, 0xE3, 0x2F, 0x44,
 
	0x7E, 0x72, 0x30, 0x43, 0xD4, 0x7F, 0x36, 0xD9,
 
	0xBD, 0x3E, 0x3F, 0x91, 0xBE, 0x54, 0x79, 0xA6,
 
	0x7C, 0x0E, 0xC5, 0x7A, 0x70, 0xC4, 0xD7, 0xCE,
 
	0xDA, 0xAA, 0x68, 0x8F, 0xBC, 0x96, 0x1B, 0x16,
 
	0xA2, 0xC6, 0x67, 0x09, 0x45, 0x9F, 0xCF, 0x41,
 
	0xC8, 0x60, 0x74, 0x99, 0x5D, 0x85, 0x5F, 0x50,
 
	0x33, 0x52, 0x22, 0xA9, 0xB5, 0x2D, 0x98, 0x87,
 
	0x15, 0x9A, 0xAC, 0x2C, 0xDE, 0xC0, 0xB8, 0x37,
 
	0x88, 0x1F, 0xC1, 0x4F, 0x65, 0x0F, 0x3C, 0x84,
 
	0x4B, 0x1A, 0xAB, 0xA4, 0x23, 0xCB, 0xB1, 0xC7,
 
	0xDB, 0xEF, 0x40, 0x0D, 0x46, 0xE8, 0xF4, 0x71,
 
	0x38, 0x01, 0x5C, 0x0B, 0x5E, 0xC9, 0xAF, 0xC3,
 
	0xF6, 0xB6, 0x10, 0x1D, 0xE5, 0x8A, 0x90, 0xA7,
 
	0xA3, 0x05, 0x4E, 0x14, 0x63, 0x25, 0x34, 0xEC,
 
	0x6B, 0x95, 0x21, 0x55, 0xF2, 0xF0, 0x47, 0x9D,
 
	0xF8, 0x8E, 0x02, 0x0A, 0xED, 0x97, 0xAE, 0x00,
 
	0x2A, 0xEB, 0xB2, 0xA5, 0x32, 0x06, 0x2E, 0xFE,
 
	0x8D, 0x7B, 0x7D, 0x35, 0x5A, 0xD2, 0xF1, 0xE9,
 
	0xF9, 0x62, 0xB7, 0xB9, 0x53, 0x75, 0x5B, 0x8B,
 
	0xCC, 0x6C, 0x18, 0x49, 0x89, 0x31, 0xB0, 0x92,
 
	0x6F, 0xDF, 0x03, 0x57, 0xF3, 0x58, 0xCA, 0x2B,
 
	0x93, 0xA1, 0xD6, 0x24, 0x29, 0xCD, 0x59, 0x1C,
 
	0x83, 0xB3, 0x42, 0xBF, 0x82, 0xB4, 0x11, 0x4A,
 
	0x08, 0xEE, 0x76, 0x4D, 0x12, 0xDC, 0xE6, 0xC2,
 
	0x56, 0xBA, 0x86, 0x28, 0x6A, 0x20, 0x51, 0xF7,
 
	0xFF, 0xD8, 0xE7, 0xDD, 0xBB, 0x78, 0xD5, 0x81
 
};
 

	
 
unsigned char table_22[32] = {
 
	0x0B, 0x15, 0x1C, 0x0C, 0x06, 0x0A, 0x1D, 0x16,
 
	0x12, 0x0E, 0x04, 0x11, 0x1F, 0x0F, 0x07, 0x02,
 
	0x17, 0x13, 0x19, 0x18, 0x0D, 0x10, 0x1A, 0x05,
 
	0x03, 0x00, 0x01, 0x08, 0x09, 0x14, 0x1B, 0x1E
 
};
 

	
 
unsigned char table_23[256] = {
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, 0x80, 0x3A
 
};
 

	
 
unsigned char table_25[32] = {
 
	0x0A, 0x11, 0x17, 0x03, 0x05, 0x0B, 0x18, 0x13,
 
	0x09, 0x02, 0x00, 0x1C, 0x0C, 0x08, 0x1B, 0x14,
 
	0x06, 0x0E, 0x01, 0x0D, 0x16, 0x1E, 0x1D, 0x19,
 
	0x0F, 0x1A, 0x10, 0x04, 0x12, 0x15, 0x07, 0x1F
 
};
 

	
 
unsigned char table_26[32] = {
 
	0x19, 0x13, 0x1B, 0x01, 0x1C, 0x0D, 0x0C, 0x15,
 
	0x0B, 0x00, 0x1A, 0x0F, 0x12, 0x16, 0x08, 0x0A,
 
	0x03, 0x06, 0x14, 0x10, 0x18, 0x04, 0x11, 0x1D,
 
	0x1F, 0x07, 0x17, 0x05, 0x02, 0x0E, 0x1E, 0x09
 
};
 

	
 
unsigned char table_27[256] = {
 
	0x72, 0xF0, 0x14, 0xCB, 0x61, 0xA5, 0xB2, 0x02,
 
	0x75, 0x22, 0xC3, 0x9D, 0x5A, 0x63, 0xFA, 0x5F,
 
	0xD9, 0x55, 0x58, 0x43, 0x24, 0x7D, 0x77, 0x93,
 
	0xBA, 0x50, 0x1D, 0xF7, 0x49, 0x18, 0xB0, 0x42,
 
	0xBB, 0xEC, 0x52, 0x38, 0xDC, 0xC8, 0x16, 0x54,
 
	0x17, 0x19, 0x89, 0x67, 0x33, 0x3C, 0x0A, 0xAD,
 
	0xC9, 0xDE, 0x81, 0xED, 0xBD, 0x0E, 0x0B, 0x6D,
 
	0x46, 0x30, 0x35, 0x2B, 0x8C, 0xA0, 0x1C, 0x0D,
 
	0xFD, 0xA1, 0x70, 0xC6, 0xD8, 0x41, 0xB3, 0xC0,
 
	0x44, 0xEB, 0x92, 0xBE, 0x6B, 0x98, 0x1A, 0x76,
 
	0x71, 0xC5, 0x51, 0x56, 0x80, 0xFC, 0x01, 0x53,
 
	0x4B, 0xD0, 0x8B, 0xD2, 0x7B, 0xE7, 0x15, 0x5D,
 
	0xE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0x26, 0x98, 0x16, 0x7D, 0xD0, 0x2E, 0x8B, 0xD8,
 
	0xE6, 0xE8, 0x30, 0xFD, 0x7C, 0x64, 0x5A, 0xBD,
 
	0x87, 0xE2, 0xA1, 0x3F, 0xC3, 0x38, 0x96, 0xA3,
 
	0x2D, 0xF3, 0x3A, 0xEE, 0xC0, 0x10, 0xEA, 0x6F,
 
	0x8D, 0x03, 0xF4, 0x51, 0x97, 0x7E, 0x56, 0x42,
 
	0x3C, 0x5D, 0x5F, 0xF5, 0x6A, 0xAF, 0xE4, 0xBE,
 
	0xBA, 0x78, 0xA0, 0x5B, 0x49, 0xA7, 0xC7, 0x9C,
 
	0x63, 0x6B, 0x00, 0x17, 0x69, 0x75, 0x3B, 0x40,
 
	0xEF, 0x45, 0xB5, 0x2B, 0x2F, 0x02, 0xC6, 0x22,
 
	0x9F, 0xFC, 0x73, 0x08, 0x81, 0xB2, 0x2C, 0x71,
 
	0x35, 0xA2, 0xE3, 0xB3, 0x9D, 0xC5, 0x0A, 0xC2,
 
	0x25, 0x82, 0xDC, 0x88, 0xA9, 0xE5, 0xF7, 0xEB,
 
	0xD6, 0x60, 0x76, 0x55, 0x0B, 0x4E, 0xFF, 0x1A,
 
	0x46, 0x62, 0xB6, 0xB0, 0x15, 0x04, 0x95, 0x4D
 
};
 

	
 
unsigned char table_30[32] = {
 
	0x00, 0x1C, 0x0E, 0x0C, 0x06, 0x16, 0x09, 0x12,
 
	0x01, 0x13, 0x0B, 0x14, 0x11, 0x08, 0x04, 0x18,
 
	0x10, 0x1B, 0x15, 0x03, 0x02, 0x19, 0x1A, 0x17,
 
	0x1E, 0x1F, 0x0F, 0x07, 0x0D, 0x05, 0x1D, 0x0A
 
};
 

	
 
unsigned char table_31[256] = {
 
	0xDF, 0xD8, 0x3F, 0xBC, 0x5F, 0xC9, 0x8E, 0x4C,
 
	0x0B, 0x3C, 0xE5, 0xBF, 0x39, 0xD5, 0x30, 0xDD,
 
	0x23, 0xC7, 0x72, 0x63, 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 

	
 
unsigned char table_39[32] = {
 
	0x12, 0x18, 0x0E, 0x08, 0x16, 0x05, 0x06, 0x00,
 
	0x11, 0x17, 0x15, 0x1B, 0x14, 0x01, 0x1F, 0x19,
 
	0x04, 0x0D, 0x0A, 0x0F, 0x10, 0x07, 0x1D, 0x03,
 
	0x0B, 0x13, 0x0C, 0x09, 0x1E, 0x02, 0x1A, 0x1C
 
};
 

	
 
unsigned char table_40[32] = {
 
	0x16, 0x02, 0x06, 0x0E, 0x0D, 0x1C, 0x08, 0x0A,
 
	0x0F, 0x13, 0x0B, 0x18, 0x07, 0x04, 0x14, 0x01,
 
	0x1B, 0x05, 0x17, 0x1E, 0x11, 0x1A, 0x10, 0x1F,
 
	0x12, 0x19, 0x1D, 0x03, 0x0C, 0x00, 0x09, 0x15
 
};
 

	
 
unsigned char table_41[32] = {
 
	0x13, 0x18, 0x04, 0x1F, 0x1D, 0x11, 0x03, 0x00,
 
	0x10, 0x12, 0x06, 0x0A, 0x1C, 0x07, 0x15, 0x0E,
 
	0x08, 0x05, 0x0C, 0x09, 0x01, 0x02, 0x16, 0x0B,
 
	0x1A, 0x17, 0x14, 0x1E, 0x0D, 0x0F, 0x19, 0x1B
 
};
 

	
 
unsigned char table_42[32] = {
 
	0x00, 0x08, 0x15, 0x1D, 0x05, 0x18, 0x06, 0x07,
 
	0x1F, 0x01, 0x0B, 0x03, 0x19, 0x13, 0x02, 0x1C,
 
	0x17, 0x11, 0x0E, 0x1E, 0x0C, 0x0F, 0x09, 0x1A,
 
	0x1B, 0x16, 0x10, 0x0D, 0x0A, 0x14, 0x12, 0x04
 
};
 

	
 
unsigned char table_43[256] = {
 
	0x34, 0xB7, 0x36, 0x85, 0x5F, 0x93, 0x98, 0x70,
 
	0x1E, 0x59, 0x83, 0x60, 0x6F, 0xBF, 0xF9, 0xD0,
 
	0xB3, 0x22, 0x12, 0x38, 0xF5, 0x01, 0xC9, 0x5B,
 
	0xEF, 0x1D, 0x81, 0x64, 0xFA, 0x8F,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, 0xB0, 0x3E, 0x37, 0x39, 0x1C, 0x8D, 0x03,
 
	0x6E, 0xF6, 0x80, 0x6D, 0x8C, 0x9C, 0xB6, 0xCF,
 
	0xC3, 0x91, 0x63, 0xC0, 0x07, 0x67, 0xE6, 0xF4,
 
	0xCE, 0x3D, 0xDB, 0x16, 0xFD, 0xEA, 0xD6, 0x68,
 
	0xD5, 0xA6, 0x0F, 0x58, 0x44, 0x52, 0xB5, 0xDC,
 
	0x0A, 0x69, 0xC5, 0xA5, 0xC1, 0x8A, 0x2A, 0xEE,
 
	0x73, 0x76, 0x3A, 0x21, 0x53, 0xA4, 0x50, 0x6A
 
};
 

	
 
unsigned char table_44[32] = {
 
	0x1A, 0x0E, 0x0A, 0x17, 0x1F, 0x08, 0x10, 0x14,
 
	0x0C, 0x0F, 0x09, 0x1C, 0x06, 0x18, 0x1E, 0x12,
 
	0x15, 0x00, 0x11, 0x13, 0x0D, 0x01, 0x0B, 0x03,
 
	0x16, 0x19, 0x05, 0x1D, 0x02, 0x07, 0x04, 0x1B
 
};
 

	
 
unsigned char table_45[256] = {
 
	0x5E, 0xD6, 0xE2, 0x54, 0x35, 0xC2, 0xAC, 0x9D,
 
	0x92, 0x64, 0x57, 0x65, 0xC8, 0xAE, 0x21, 0xA9,
 
	0x89, 0x48, 0x12, 0x59, 0xEC, 0xEF, 0x9F, 0xF7,
 
	0x19, 0x03, 0x83, 0xC0, 0x79, 0x5D, 0x4A, 0x10,
 
	0x8C, 0xEB, 0xFF, 0xB5, 0x3B, 0x51, 0x2D, 0xD1,
 
	0x6B, 0xC5, 0x24, 0x5C, 0xE6, 0x11, 0x94, 0x3F,
 
	0xD0, 0x2F, 0x0E, 0x95, 0x3C, 0xFE, 0x5B, 0x20,
 
	0x23, 0xE0, 0x91, 0x6F, 0xCA, 0x56, 0x0C, 0x73,
 
	0xDA, 0x67, 0x37, 0xA3, 0xA5, 0x70, 0x93, 0x1C,
 
	0x18, 0xD9, 0x42, 0x5F, 0x44, 0xF0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	0x39, 0x6B, 0xA5, 0x2A, 0x09, 0xCA, 0x04, 0xEC,
 
	0xBA, 0x18, 0x31, 0x46, 0x20, 0xBE, 0x1F, 0x3C,
 
	0x6D, 0xAA, 0xF6, 0xDD, 0xF4, 0x96, 0x03, 0x0A,
 
	0x9E, 0x83, 0xA1, 0x9D, 0xD5, 0xB0, 0x17, 0xBF,
 
	0x56, 0xAB, 0xAE, 0x1B, 0x52, 0xC6, 0x81, 0x4B,
 
	0xDC, 0x90, 0x5A, 0x9B, 0xB6, 0x0F, 0xF3, 0x67,
 
	0x30, 0x63, 0x7C, 0x40, 0x0E, 0x7F, 0x95, 0x36,
 
	0xC4, 0x4E, 0x43, 0xCB, 0x15, 0xB8, 0x00, 0x91,
 
	0x8A, 0x4C, 0x8E, 0x14, 0x06, 0x6E, 0xA9, 0x2E,
 
	0x3F, 0x48, 0x2F, 0x0C, 0xB5, 0x21, 0xBB, 0xDA,
 
	0x8B, 0x42, 0x29, 0x8C, 0x33, 0x59, 0xE8, 0xF8,
 
	0xC7, 0xE4, 0x37, 0xE5, 0xFC, 0xBD, 0x99, 0x41
 
};
 

	
 
unsigned char table_47[32] = {
 
	0x18, 0x1D, 0x16, 0x10, 0x11, 0x04, 0x1E, 0x08,
 
	0x19, 0x0E, 0x0F, 0x02, 0x14, 0x1C, 0x07, 0x17,
 
	0x0D, 0x09, 0x12, 0x1A, 0x05, 0x01, 0x0B, 0x0A,
 
	0x13, 0x15, 0x0C, 0x00, 0x06, 0x1F, 0x03, 0x1B
 
};
 

	
 
unsigned char table_48[32] = {
 
	0x13, 0x08, 0x15, 0x01, 0x17, 0x10, 0x0F, 0x1F,
 
	0x1D, 0x0D, 0x12, 0x03, 0x06, 0x0A, 0x1C, 0x19,
 
	0x1A, 0x04, 0x1B, 0x02, 0x16, 0x1E, 0x11, 0x00,
 
	0x14, 0x09, 0x0C, 0x18, 0x05, 0x07, 0x0E, 0x0B
 
};
 

	
 
unsigned char table_49[32] = {
 
	0x1F, 0x0F, 0x19, 0x07, 0x18, 0x05, 0x1E, 0x1D,
 
	0x15, 0x08, 0x17, 0x10, 0x0A, 0x0E, 0x0C, 0x1B,
 
	0x02, 0x13, 0x03, 0x0D, 0x04, 0x1A, 0x06, 0x09,
 
	0x12, 0x1C, 0x0B, 0x16, 0x14, 0x01, 0x11, 0x00
 
};
 

	
 
unsigned char table_50[32] = {
 
	0x16, 0x18, 0x1C, 0x0E, 0x12, 0x00, 0x04, 0x1B,
 
	0x1F, 0x13, 0x17, 0x0A, 0x1E, 0x03, 0x0C, 0x01,
 
	0x0F, 0x10, 0x02, 0x08, 0x14, 0x09, 0x19, 0x15,
 
	0x06, 0x0D, 0x0B, 0x1D, 0x05, 0x07, 0x11, 0x1A
 
};
 

	
 
unsigned char table_51[32] = {
 
	0x1C, 0x0D, 0x1B, 0x07, 0x17, 0x0E, 0x06, 0x01,
 
	0x12, 0x19, 0x03, 0x0B, 0x10, 0x08, 0x00, 0x1E,
 
	0x0A, 0x04, 0x1A, 0x1D, 0x0C, 0x18, 0x02, 0x13,
 
	0x0F, 0x11, 0x05, 0x09, 0x15, 0x16, 0x1F, 0x14
 
};
 

	
 
unsigned char table_52[256] = {
 
	0x34, 0x0B, 0x47, 0xA3, 0x56, 0x30, 0x73, 0xD4,
 
	0x4B, 0xF6, 0xA6, 0x80, 0x22, 0x95, 0xA5, 0xBB,
 
	0xFE, 0xCD, 0x27, 0x88, 0x87, 0x18, 0x86, 0x6E,
 
	0xB9, 0x07, 0x37, 0x52, 0x0A, 0x28, 0x2C, 0xC4,
 
	0x75, 0xA1, 0x29, 0x54, 0x84, 0x08, 0x72, 0x51,
 
	0xDD, 0xF1, 0x4E, 0x1A, 0x90, 0x57, 0x20, 0xAD,
 
	0x68, 0x61, 0xAF, 0x50, 0x6B, 0x1B, 0x71, 0xEB,
 
	0x63, 0xC9, 0xB0, 0x58, 0x26, 0x40, 0xC7, 0xD9,
 
	0x70, 0xA2, 0x9A, 0x09, 0x3F, 0x92, 0x0D, 0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0, 0x01, 0x86, 0x82, 0xCC, 0x12, 0x8C, 0x06,
 
	0x30, 0xA8, 0x7A, 0x73, 0x66, 0x7C, 0xC6, 0xB6,
 
	0xF2, 0x13, 0xBF, 0x40, 0x85, 0x77, 0x09, 0x3D,
 
	0x67, 0x63, 0x3F, 0x7F, 0xF3, 0x87, 0x8F, 0xFF,
 
	0x92, 0xC7, 0x4C, 0x23, 0xBA, 0xCB, 0xB1, 0xED,
 
	0x0C, 0x60, 0x47, 0xFE, 0x38, 0x5D, 0xCF, 0x8B,
 
	0x4D, 0xA9, 0x2E, 0xE5, 0xA4, 0x1B, 0x88, 0x3E,
 
	0x7D, 0xF8, 0xC0, 0xD5, 0x6D, 0x6C, 0x48, 0xAC,
 
	0x9B, 0x51, 0x7E, 0x6E, 0x50, 0x0D, 0x9A, 0xB3,
 
	0xEE, 0x07, 0x4F, 0x69, 0x9C, 0x03, 0xD9, 0xD4,
 
	0xB4, 0xD2, 0xAE, 0x4E, 0x55, 0xB7, 0xC9, 0x41,
 
	0x39, 0x6A, 0xC8, 0xA0, 0xB2, 0xC1, 0x84, 0xFC,
 
	0xAB, 0x64, 0xE0, 0xBE, 0xDA, 0xBD, 0x96, 0x94
 
};
 

	
 
unsigned char table_54[32] = {
 
	0x01, 0x02, 0x1D, 0x10, 0x0E, 0x11, 0x08, 0x14,
 
	0x12, 0x09, 0x15, 0x17, 0x16, 0x04, 0x06, 0x1B,
 
	0x07, 0x1A, 0x18, 0x13, 0x0A, 0x1E, 0x1C, 0x1F,
 
	0x0C, 0x0B, 0x0D, 0x05, 0x0F, 0x00, 0x19, 0x03
 
};
 

	
 
unsigned char table_55[32] = {
 
	0x01, 0x12, 0x13, 0x09, 0x0B, 0x19, 0x03, 0x0E,
 
	0x02, 0x1F, 0x1D, 0x1B, 0x1E, 0x11, 0x06, 0x05,
 
	0x00, 0x16, 0x07, 0x0C, 0x15, 0x0D, 0x1A, 0x08,
 
	0x18, 0x10, 0x0F, 0x17, 0x1C, 0x0A,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0xBD, 0xE9, 0x68, 0x18, 0xAB, 0x2E,
 
	0x5D, 0x1A, 0x8D, 0xC1, 0x58, 0x48, 0xAD, 0x0F
 
};
 

	
 
unsigned char table_60[32] = {
 
	0x1C, 0x06, 0x1E, 0x10, 0x1D, 0x05, 0x00, 0x0E,
 
	0x0C, 0x02, 0x11, 0x19, 0x15, 0x18, 0x16, 0x07,
 
	0x1F, 0x0B, 0x14, 0x01, 0x0F, 0x09, 0x0D, 0x13,
 
	0x03, 0x08, 0x12, 0x04, 0x1B, 0x0A, 0x17, 0x1A
 
};
 

	
 
unsigned char table_61[256] = {
 
	0xC5, 0xA6, 0xF2, 0x6B, 0x4B, 0x58, 0xE0, 0x41,
 
	0xC6, 0x2F, 0x13, 0xFE, 0xC1, 0x34, 0x3F, 0x24,
 
	0x10, 0xBF, 0x8B, 0xC9, 0x26, 0x2E, 0x68, 0xBE,
 
	0x28, 0x54, 0x93, 0x11, 0x21, 0x03, 0xFF, 0x50,
 
	0x31, 0x71, 0x2C, 0x6C, 0x91, 0x8F, 0x3B, 0x40,
 
	0x3E, 0xE5, 0xA5, 0x80, 0xEA, 0x7C, 0x9D, 0x18,
 
	0x84, 0x5A, 0x73, 0x3A, 0x33, 0x43, 0xA1, 0x47,
 
	0xB1, 0xEE, 0xFB, 0x79, 0x5E, 0xAF, 0xB9, 0x48,
 
	0x0F, 0x88, 0x65, 0x67, 0x6F, 0xDB, 0x25, 0xE4,
 
	0xB0, 0x87, 0xD0, 0x46, 0xB5, 0xB7, 0x53, 0xD4,
 
	0x1E, 0x76, 0xB4, 0x90, 0xDD, 0xA3, 0xF7, 0x57,
 
	0xD2, 0xCC, 0x5D, 0xE3, 0xB3, 0xD8, 0x5F, 0x2B,
 
	0x69, 0x4A, 0x9B, 0x39, 0x1A, 0x8D, 0x05, 0x8A,
 
	0x44, 0x15, 0xAE, 0xF3, 0xA8, 0x92, 0x02, 0xAB,
 
	0xB8, 0xDA, 0x0A, 0x0C, 0xED, 0xD7, 0x77,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 0x00, 0x1D, 0x1F, 0x01, 0x0F, 0x07, 0x12
 
};
 

	
 
unsigned char table_65[32] = {
 
	0x01, 0x0A, 0x1E, 0x14, 0x10, 0x1D, 0x0D, 0x17,
 
	0x0E, 0x0C, 0x0F, 0x12, 0x04, 0x1A, 0x05, 0x02,
 
	0x08, 0x1C, 0x09, 0x1F, 0x0B, 0x13, 0x19, 0x1B,
 
	0x11, 0x00, 0x16, 0x06, 0x03, 0x18, 0x15, 0x07
 
};
 

	
 
unsigned char table_66[32] = {
 
	0x1C, 0x18, 0x0C, 0x09, 0x05, 0x03, 0x15, 0x12,
 
	0x0D, 0x02, 0x08, 0x0E, 0x19, 0x07, 0x13, 0x17,
 
	0x1E, 0x1D, 0x1F, 0x11, 0x06, 0x0A, 0x0B, 0x14,
 
	0x0F, 0x10, 0x01, 0x1B, 0x00, 0x04, 0x1A, 0x16
 
};
 

	
 
unsigned char table_67[256] = {
 
	0x6B, 0x49, 0xC8, 0x86, 0xFF, 0xC0, 0x5D, 0xEF,
 
	0xF7, 0x06, 0xE0, 0x98, 0xA9, 0x72, 0x71, 0xD5,
 
	0xBA, 0x7F, 0x10, 0xD1, 0xBE, 0x41, 0x9C, 0x40,
 
	0x28, 0x8E, 0xE5, 0x74, 0x47, 0x9E, 0x3E, 0x7C,
 
	0xB5, 0xCD, 0x3F, 0x20, 0xF2, 0xA6, 0xDC, 0x97,
 
	0x32, 0x6D, 0x52, 0xF5, 0x16, 0x05, 0xFE, 0x04,
 
	0x3D, 0x53, 0x50, 0x23, 0x39, 0x77, 0x08, 0x60,
 
	0x75, 0x18, 0x4A, 0xC6, 0xBB, 0xE7, 0xF1, 0xAB,
 
	0xEB, 0x88, 0xB6, 0x82, 0x6E, 0x91, 0xF3, 0x34,
 
	0x3A, 0x42, 0x1A, 0xDF, 0xA1, 0xB3, 0x92, 0xBF,
 
	0xB7, 0x00, 0xD4, 0xDE, 0x31, 0xF0, 0x1C, 0xDA,
 
	0x4F, 0x61, 0x67, 0x2C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
	0x55, 0xCB, 0x0E, 0x5A, 0x77, 0xFB, 0x8B, 0x86,
 
	0x75, 0x8A, 0x51, 0xEC, 0xDA, 0xC6, 0xA6, 0xCC,
 
	0x91, 0x4B, 0x11, 0xF6, 0xEA, 0xD1, 0xB6, 0x4E,
 
	0x82, 0x04, 0x92, 0x30, 0xF4, 0x25, 0x88, 0x1E,
 
	0x9C, 0xA0, 0xC8, 0x6A, 0x93, 0x87, 0x1F, 0xB4,
 
	0xB1, 0x8F, 0x65, 0xCA, 0xFE, 0xFF, 0x97, 0x15,
 
	0x99, 0x28, 0x80, 0x42, 0x70, 0x85, 0x0C, 0x3B,
 
	0xBD, 0xE1, 0xA7, 0x17, 0xC9, 0x3A, 0xBB, 0x6B,
 
	0x37, 0xF0, 0xC5, 0x39, 0x6F, 0x01, 0x83, 0x67,
 
	0x74, 0xCE, 0xDC, 0x90, 0x3E, 0xF3, 0x7F, 0xC4,
 
	0x49, 0x84, 0x38, 0xC7, 0xE3, 0xD4, 0x1A, 0xBF
 
};
 

	
 
unsigned char table_71[32] = {
 
	0x17, 0x13, 0x0E, 0x1A, 0x0D, 0x18, 0x19, 0x10,
 
	0x14, 0x11, 0x16, 0x05, 0x04, 0x00, 0x12, 0x0A,
 
	0x02, 0x07, 0x03, 0x0B, 0x09, 0x1F, 0x1C, 0x0F,
 
	0x0C, 0x06, 0x1B, 0x08, 0x1D, 0x01, 0x15, 0x1E
 
};
 

	
 
unsigned char table_72[256] = {
 
	0xC9, 0xA7, 0x1B, 0xEC, 0x2B, 0x8B, 0xB0, 0xEB,
 
	0x7F, 0x39, 0x25, 0xD9, 0x1D, 0xD5, 0x67, 0xA0,
 
	0xB3, 0xAC, 0x3B, 0xC8, 0x82, 0xC0, 0xE3, 0x9E,
 
	0x4C, 0x9B, 0xAF, 0xFD, 0x91, 0x86, 0x5F, 0x92,
 
	0xB4, 0x42, 0x3C, 0x45, 0x12, 0xC4, 0xE2, 0xE1,
 
	0x6C, 0x1F, 0xC6, 0x40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 0x29, 0x4D, 0xF8, 0xDF,
 
	0xC4, 0x72, 0x2F, 0xAE, 0x06, 0x51, 0x41, 0xAF,
 
	0xF3, 0xDD, 0x87, 0xB2, 0x9C, 0xC7, 0x12, 0x16,
 
	0x20, 0xA7, 0x21, 0x73, 0xF1, 0x58, 0xD7, 0x7B,
 
	0xB9, 0xB3, 0x32, 0x01, 0x80, 0x1C, 0x39, 0x0B,
 
	0x13, 0x56, 0x6C, 0x89, 0x33, 0x6E, 0x2A, 0xA5,
 
	0xD1, 0x95, 0xC3, 0xA0, 0x0F, 0xCA, 0xAC, 0xFC
 
};
 

	
 
unsigned char table_77[32] = {
 
	0x1C, 0x0D, 0x1E, 0x01, 0x06, 0x16, 0x18, 0x17,
 
	0x0B, 0x1F, 0x04, 0x0F, 0x00, 0x19, 0x08, 0x0A,
 
	0x11, 0x03, 0x05, 0x07, 0x09, 0x0C, 0x15, 0x14,
 
	0x1A, 0x12, 0x13, 0x0E, 0x1D, 0x10, 0x02, 0x1B
 
};
 

	
 
unsigned char table_78[32] = {
 
	0x0E, 0x02, 0x17, 0x12, 0x1E, 0x09, 0x15, 0x03,
 
	0x01, 0x0B, 0x0F, 0x11, 0x10, 0x0A, 0x16, 0x06,
 
	0x07, 0x00, 0x1C, 0x1D, 0x1F, 0x0C, 0x18, 0x04,
 
	0x13, 0x0D, 0x1B, 0x08, 0x19, 0x14, 0x05, 0x1A
 
};
 

	
 
unsigned char table_79[32] = {
 
	0x12, 0x0B, 0x11, 0x01, 0x07, 0x0E, 0x1A, 0x0D,
 
	0x1E, 0x18, 0x14, 0x1F, 0x0A, 0x17, 0x19, 0x1B,
 
	0x00, 0x10, 0x0C, 0x08, 0x13, 0x02, 0x0F, 0x1D,
 
	0x09, 0x06, 0x04, 0x16, 0x15, 0x1C, 0x05, 0x03
 
};
 

	
 
unsigned char table_80[256] = {
 
	0x14, 0xE7, 0x31, 0x0F, 0xD1, 0x5F, 0xED, 0x1E,
 
	0xA6, 0x77, 0x20, 0x57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0xDE,
 
	0xA7, 0x19, 0x09, 0x7F, 0x5B, 0x96, 0xBA, 0x0D,
 
	0xCF, 0xD2, 0x06, 0x1F, 0xD8, 0xDB, 0xEC, 0xA0,
 
	0xDD, 0x66, 0x10, 0xA2, 0xDF, 0x30, 0xF4, 0x88,
 
	0xCB, 0x36, 0x82, 0xE3, 0x73, 0x17, 0x55, 0x15,
 
	0xF5, 0xB7, 0x23, 0xB1, 0xD6, 0xE2, 0x47, 0x7E,
 
	0x67, 0xE8, 0x1D, 0x16, 0x8B, 0xEB, 0xD0, 0x3D,
 
	0x6A, 0x54, 0x2A, 0x4E, 0x93, 0xFA, 0x44, 0x05,
 
	0x2F, 0x50, 0x2E, 0x95, 0xAE, 0x1B, 0x56, 0x7B,
 
	0x39, 0xB9, 0xC0, 0x22, 0xF1, 0x4D, 0x90, 0xFC
 
};
 

	
 
unsigned char table_81[32] = {
 
	0x03, 0x02, 0x1D, 0x0E, 0x09, 0x1A, 0x0C, 0x11,
 
	0x1C, 0x0D, 0x08, 0x12, 0x19, 0x10, 0x04, 0x17,
 
	0x15, 0x05, 0x0A, 0x00, 0x13, 0x16, 0x1B, 0x18,
 
	0x1E, 0x0B, 0x0F, 0x01, 0x07, 0x14, 0x1F, 0x06
 
};
 

	
 
unsigned char table_82[256] = {
 
	0x53, 0xD3, 0x64, 0x89, 0x7D, 0xA5, 0x66, 0xA4,
 
	0x09, 0x46, 0x17, 0x2C, 0xAF, 0x8C, 0x21, 0x5F,
 
	0x3B, 0x22, 0xE3, 0x05, 0x07, 0x28, 0x2F, 0xAB,
 
	0xF4, 0x8E, 0x51, 0x31, 0x02, 0xC7, 0x48, 0x13,
 
	0x24, 0x12, 0xB8, 0xE5, 0xBD, 0xAE, 0x7E, 0xCC,
 
	0xC9, 0x98, 0x08, 0xEE, 0xDB, 0x1B, 0xE8, 0x3D,
 
	0x8F, 0xF2, 0xFB, 0x36, 0x4D, 0x94, 0x9C, 0x16,
 
	0xF7, 0x42, 0x9B, 0x2B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 0xC0,
 
	0x39, 0xD1, 0x43, 0xCE, 0x4F, 0x5C, 0xE7, 0x37,
 
	0x5E, 0x33, 0x5B, 0xA6, 0xC2, 0xB0, 0xBA, 0x30,
 
	0x6A, 0x78, 0xB5, 0x71, 0x56, 0x87, 0x7F, 0x86
 
};
 

	
 
unsigned char table_83[32] = {
 
	0x1B, 0x0A, 0x1F, 0x01, 0x10, 0x08, 0x0E, 0x18,
 
	0x06, 0x04, 0x00, 0x1C, 0x0C, 0x19, 0x0D, 0x16,
 
	0x02, 0x03, 0x09, 0x07, 0x13, 0x0F, 0x05, 0x12,
 
	0x17, 0x1E, 0x1A, 0x1D, 0x0B, 0x11, 0x14, 0x15
 
};
 

	
 
unsigned char table_84[32] = {
 
	0x02, 0x1A, 0x0D, 0x15, 0x01, 0x16, 0x1E, 0x00,
 
	0x08, 0x1B, 0x04, 0x10, 0x1C, 0x18, 0x19, 0x14,
 
	0x0C, 0x11, 0x0B, 0x0E, 0x03, 0x0A, 0x07, 0x12,
 
	0x1D, 0x17, 0x13, 0x06, 0x0F, 0x05, 0x09, 0x1F
 
};
 

	
 
unsigned char table_85[256] = {
 
	0xC6, 0x7C, 0xCE, 0xBD, 0x84, 0x3E, 0x0B, 0xD8,
 
	0xFE, 0xCC, 0x46, 0x50, 0xD1, 0xFB, 0xA0, 0x6D,
 
	0xEA, 0xE2, 0x40, 0x51, 0x13, 0xB0, 0xD6, 0xB1,
 
	0xA8, 0xDF, 0x61, 0xA4, 0x80, 0x21, 0xB3, 0x33,
 
	0x06, 0x6B, 0xE3, 0x8C, 0xA1, 0x18, 0xBA, 0x03,
 
	0xD7, 0x8D, 0x54, 0x12, 0x4C, 0xEE, 0x9E, 0xCF,
 
	0x04, 0x2A, 0x08, 0xBB, 0xC2, 0xD4, 0xC3, 0x4A,
 
	0xD5, 0xFA, 0x36, 0x2F, 0x14, 0x3F, 0xED, 0x05,
 
	0x17, 0x28, 0x75, 0xFC, 0xA2, 0x1F, 0x4B, 0x6F,
 
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 0x94, 0xC4, 0x0D, 0xB8,
 
	0x2D, 0x57, 0xE7, 0x82, 0x1E, 0x37, 0x63, 0x43
 
};
 

	
 
unsigned char table_86[32] = {
 
	0x11, 0x07, 0x0F, 0x0A, 0x19, 0x1D, 0x0B, 0x09,
 
	0x1C, 0x1E, 0x14, 0x06, 0x0C, 0x16, 0x13, 0x04,
 
	0x15, 0x18, 0x00, 0x0D, 0x12, 0x05, 0x08, 0x02,
 
	0x10, 0x1A, 0x1F, 0x01, 0x17, 0x0E, 0x03, 0x1B
 
};
 

	
 
unsigned char table_87[32] = {
 
	0x17, 0x0E, 0x1D, 0x13, 0x0B, 0x19, 0x03, 0x06,
 
	0x09, 0x01, 0x0D, 0x15, 0x1C, 0x16, 0x18, 0x1B,
 
	0x11, 0x10, 0x00, 0x1E, 0x1F, 0x08, 0x12, 0x0F,
 
	0x02, 0x04, 0x07, 0x1A, 0x14, 0x0A, 0x0C, 0x05
 
};
 

	
 
unsigned char table_88[32] = {
 
	0x09, 0x08, 0x17, 0x10, 0x0A, 0x07, 0x1C, 0x1F,
 
	0x04, 0x0E, 0x01, 0x0C, 0x0D, 0x1B, 0x03, 0x15,
 
	0x02, 0x1E, 0x18, 0x19, 0x0F, 0x06, 0x1A, 0x0B,
 
	0x05, 0x11, 0x14, 0x00, 0x16, 0x1D, 0x12, 0x13
 
};
 

	
 
unsigned char table_89[32] = {
 
	0x15, 0x1C, 0x1D, 0x14, 0x0F, 0x1A, 0x05, 0x02,
 
	0x07, 0x09, 0x06, 0x08, 0x1F, 0x00, 0x10, 0x13,
 
	0x0D, 0x03, 0x0C, 0x18, 0x0E, 0x16, 0x1B, 0x1E,
 
	0x12, 0x04, 0x11, 0x0A, 0x01, 0x0B, 0x17, 0x19
 
};
 

	
 
unsigned char table_90[256] = {
 
	0x62, 0x36, 0x64, 0x0E, 0x4C, 0x6C, 0xBE, 0xCF,
 
	0x25, 0x5A, 0x3D, 0x12, 0x54, 0x9F, 0xE7, 0xA5,
 
	0xDE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B7, 0xF6, 0x3E, 0x80,
 
	0xA9, 0xC6, 0x01, 0xD2, 0xEB, 0x9E, 0x4B, 0xBC,
 
	0xC8, 0xB5, 0x02, 0x5F, 0x98, 0x9D, 0x5D, 0x35,
 
	0xD0, 0x16, 0xB1, 0x23, 0x7D, 0xAF, 0x10, 0x3C,
 
	0xAB, 0x14, 0x09, 0x2D, 0x0D, 0xC5, 0x1D, 0xD6,
 
	0x42, 0xF2, 0x34, 0x73, 0xF4, 0xFD, 0xE0, 0x24,
 
	0x6F, 0xD3, 0x75, 0xD8, 0xCC, 0xB6, 0x99, 0x4F,
 
	0x29, 0x0A, 0x08, 0xE4, 0x27, 0x19, 0x31, 0xC9,
 
	0x20, 0x94, 0x45, 0xED, 0xDC, 0xBD, 0x7E, 0x50
 
};
 

	
 
unsigned char table_91[32] = {
 
	0x03, 0x04, 0x0C, 0x18, 0x10, 0x0D, 0x13, 0x1B,
 
	0x1F, 0x07, 0x11, 0x17, 0x1C, 0x1D, 0x05, 0x06,
 
	0x0A, 0x12, 0x02, 0x1A, 0x0B, 0x01, 0x0E, 0x08,
 
	0x14, 0x16, 0x00, 0x15, 0x19, 0x09, 0x0F, 0x1E
 
};
 

	
 
unsigned char table_92[32] = {
 
	0x1E, 0x10, 0x01, 0x07, 0x11, 0x16, 0x15, 0x17,
 
	0x1F, 0x14, 0x0C, 0x1C, 0x06, 0x03, 0x00, 0x18,
 
	0x08, 0x0E, 0x02, 0x1B, 0x09, 0x0D, 0x19, 0x05,
 
	0x0F, 0x12, 0x0B, 0x13, 0x0A, 0x04, 0x1D, 0x1A
 
};
 

	
 
unsigned char table_93[256] = {
 
	0x76, 0x78, 0xA2, 0x94, 0x0E, 0x7F, 0xDF, 0xC1,
 
	0xB9, 0xE1, 0x3D, 0x59, 0x6F, 0x1E, 0x53, 0x99,
 
	0x80, 0xE3, 0x21, 0xF8, 0x65, 0xB8, 0x08, 0xBC,
 
	0x29, 0x17, 0xFD, 0x33, 0x35, 0xF2, 0x70, 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A1, 0x3C, 0x97, 0xF9, 0x03, 0x40, 0x12,
 
	0xCF, 0x37, 0xE4, 0x10, 0x6A, 0xED, 0xFF, 0x62,
 
	0x42, 0x4C, 0xAF, 0x9F, 0xE5, 0xE8, 0xD8, 0xD1,
 
	0x28, 0x3F, 0x1B, 0xE9, 0xCE, 0x6C, 0x27, 0x88,
 
	0xEF, 0x2F, 0xE0, 0x30, 0x87, 0x5A, 0x73, 0xB3,
 
	0x6E, 0x3B, 0x7D, 0x77, 0x36, 0xAA, 0x39, 0xDE,
 
	0x24, 0x34, 0xE2, 0xEC, 0x85, 0x47, 0xF4, 0xB2
 
};
 

	
 
unsigned char table_94[32] = {
 
	0x1C, 0x07, 0x05, 0x1A, 0x10, 0x1D, 0x14, 0x12,
 
	0x08, 0x0F, 0x0C, 0x01, 0x04, 0x1B, 0x16, 0x0A,
 
	0x11, 0x02, 0x1F, 0x13, 0x0D, 0x1E, 0x17, 0x06,
 
	0x0E, 0x09, 0x15, 0x19, 0x03, 0x18, 0x00, 0x0B
 
};
 

	
 
unsigned char table_95[32] = {
 
	0x12, 0x10, 0x11, 0x15, 0x03, 0x0A, 0x14, 0x05,
 
	0x1D, 0x07, 0x17, 0x0D, 0x09, 0x08, 0x1B, 0x1F,
 
	0x0B, 0x06, 0x19, 0x0E, 0x18, 0x04, 0x00, 0x02,
 
	0x1E, 0x1C, 0x01, 0x0C, 0x1A, 0x0F, 0x13, 0x16
 
};
 

	
 
unsigned char table_96[256] = {
 
	0x1C, 0x6E, 0xCD, 0xB4, 0xB3, 0x93, 0xA8, 0x2E,
 
	0x4F, 0x09, 0xE3, 0x72, 0x64, 0x13, 0x21, 0xF5,
 
	0x89, 0xB2, 0xD2, 0x22, 0x5D, 0x63, 0x90, 0xC4,
 
	0x42, 0x9B, 0x07, 0xCA, 0x16, 0x19, 0x5C, 0x2B,
 
	0x3D, 0xA0, 0x69, 0x5F, 0x52, 0x41, 0x66, 0xC0,
 
	0x55, 0xDA, 0x82, 0x40, 0x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0x89, 0x49, 0x1A, 0xC4, 0x52, 0xEC, 0x8D, 0x73,
 
	0xD3, 0x7C, 0x79, 0xD2, 0x14, 0x4D, 0x84, 0xA2,
 
	0x0E, 0x70, 0x78, 0x72, 0xB7, 0xA0, 0xC5, 0x81,
 
	0x58, 0x0C, 0x68, 0x27, 0xFF, 0xF6, 0xAE, 0xCC,
 
	0x88, 0xFE, 0x24, 0x2F, 0x76, 0x3F, 0x59, 0x21,
 
	0x54, 0x3A, 0x13, 0x09, 0x2C, 0xB5, 0xC7, 0x63
 
};
 

	
 
unsigned char table_99[32] = {
 
	0x19, 0x00, 0x10, 0x18, 0x09, 0x11, 0x13, 0x1D,
 
	0x08, 0x1A, 0x02, 0x05, 0x03, 0x17, 0x12, 0x01,
 
	0x1F, 0x14, 0x06, 0x07, 0x15, 0x0D, 0x0F, 0x0B,
 
	0x0E, 0x16, 0x1E, 0x04, 0x1B, 0x0A, 0x0C, 0x1C
 
};
 

	
 
unsigned char table_100[256] = {
 
	0x9B, 0x3A, 0xAE, 0x60, 0x27, 0x67, 0x1E, 0x4E,
 
	0x91, 0xDA, 0x85, 0x43, 0x5C, 0xCC, 0x89, 0x55,
 
	0x75, 0x56, 0xF2, 0x86, 0xEB, 0xC4, 0x0D, 0xE6,
 
	0x63, 0x88, 0x38, 0x59, 0x68, 0xD0, 0x18, 0xF0,
 
	0xBA, 0x28, 0xF5, 0x80, 0x02, 0x5B, 0xE1, 0xA4,
 
	0x7A, 0x4B, 0x8E, 0xF7, 0x9E, 0x99, 0x70, 0xEF,
 
	0x66, 0x50, 0xB1, 0xCD, 0x9A, 0xAF, 0x5F, 0x21,
 
	0xE5, 0x5D, 0x14, 0xD4, 0x34, 0x22, 0xC3, 0x0F,
 
	0x44, 0xB6, 0x92, 0xCE, 0xB4, 0x6E, 0xB0, 0x00,
 
	0xF9, 0xB5, 0x10, 0xEA, 0x45, 0x2F, 0x2B, 0xF4,
 
	0xF6, 0xFE, 0xCB, 0x0A, 0x42, 0xF8,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 
unsigned char table_101[32] = {
 
	0x18, 0x08, 0x14, 0x17, 0x03, 0x10, 0x19, 0x04,
 
	0x0D, 0x1C, 0x06, 0x1D, 0x1E, 0x12, 0x11, 0x0B,
 
	0x0F, 0x02, 0x0E, 0x1B, 0x13, 0x05, 0x07, 0x16,
 
	0x15, 0x0A, 0x0C, 0x1A, 0x00, 0x01, 0x1F, 0x09
 
};
 

	
 
unsigned char table_102[32] = {
 
	0x17, 0x1F, 0x0E, 0x05, 0x13, 0x0C, 0x14, 0x1A,
 
	0x0F, 0x01, 0x12, 0x1C, 0x00, 0x07, 0x0D, 0x02,
 
	0x10, 0x16, 0x04, 0x11, 0x1D, 0x03, 0x1E, 0x18,
 
	0x06, 0x15, 0x0A, 0x19, 0x09, 0x08, 0x1B, 0x0B
 
};
 

	
 
unsigned char table_103[32] = {
 
	0x0F, 0x09, 0x1E, 0x11, 0x0D, 0x08, 0x10, 0x00,
 
	0x01, 0x1F, 0x1D, 0x1C, 0x12, 0x04, 0x07, 0x05,
 
	0x19, 0x14, 0x1B, 0x02, 0x1A, 0x15, 0x17, 0x16,
 
	0x18, 0x0B, 0x0A, 0x13, 0x0C, 0x0E, 0x03, 0x06
 
};
 

	
 
unsigned char table_104[256] = {
 
	0xA4, 0x9F, 0x78, 0x39, 0x3D, 0x81, 0x51, 0x24,
 
	0x46, 0x2A, 0x56, 0xE8, 0xDF, 0x73, 0xA8, 0xA2,
 
	0x0D, 0xDC, 0xA5, 0x4F, 0xF0, 0x93, 0xC0, 0x76,
 
	0x38, 0x70, 0xB0, 0x30, 0x98, 0x13, 0x8B, 0x14,
 
	0x26, 0x45, 0x0F, 0x7D, 0x34, 0x72, 0x6B, 0x89,
 
	0x43, 0xE2, 0x96, 0x5B, 0xEF, 0x2B, 0xF9, 0xDE,
 
	0x82, 0xB5, 0x61, 0x4A, 0x17, 0xC2, 0x5A, 0xCB,
 
	0xB2, 0x8D, 0xE4, 0xEC, 0xD9, 0x80, 0xBC, 0x62,
 
	0x67,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 0x97, 0xA2, 0x8B, 0xFC, 0x10,
 
	0x5F, 0xDC, 0xD5, 0xB0, 0xBD, 0x55, 0xC1, 0xE7,
 
	0x0C, 0x50, 0x43, 0x39, 0x71, 0x52, 0xE5, 0xAF,
 
	0x8A, 0x60, 0x92, 0x2D, 0xD8, 0x03, 0xF5, 0x28,
 
	0xCA, 0xEF, 0xD0, 0xC2, 0x53, 0x91, 0xA6, 0x73,
 
	0x56, 0xA5, 0xF1, 0x57, 0x42, 0xF4, 0xD4, 0x36,
 
	0x8D, 0xBC, 0xE9, 0x7E, 0x02, 0x76, 0x18, 0x0B,
 
	0x84, 0x5A, 0xE2, 0xBF, 0x68, 0x95, 0x29, 0x98,
 
	0xAD, 0x88, 0x1F, 0x81, 0x67, 0xA1, 0x3A, 0xA7,
 
	0x22, 0xF8, 0x01, 0xA0, 0xCE, 0x7A, 0xDA, 0x30,
 
	0xC4, 0xE4, 0xEE, 0x7C, 0x3B, 0x4D, 0x3D, 0xE3,
 
	0xFA, 0x6A, 0x7F, 0x99, 0x00, 0x93, 0x0E, 0xFF,
 
	0x90, 0x0A, 0x2A, 0x5D, 0x96, 0x08, 0x6B, 0x83,
 
	0xBA, 0x1E, 0x44, 0x87, 0x45, 0x9F, 0xC9, 0x94
 
};
 

	
 
unsigned char table_106[32] = {
 
	0x03, 0x11, 0x07, 0x1B, 0x0F, 0x14, 0x0C, 0x01,
 
	0x04, 0x02, 0x09, 0x0A, 0x05, 0x12, 0x06, 0x1F,
 
	0x1C, 0x0E, 0x0D, 0x15, 0x18, 0x08, 0x00, 0x10,
 
	0x1E, 0x1D, 0x17, 0x19, 0x13, 0x16, 0x0B, 0x1A
 
};
 

	
 
unsigned char table_107[32] = {
 
	0x13, 0x1B, 0x06, 0x11, 0x1C, 0x07, 0x08, 0x0E,
 
	0x10, 0x05, 0x09, 0x18, 0x04, 0x15, 0x1E, 0x0F,
 
	0x1F, 0x12, 0x02, 0x00, 0x17, 0x19, 0x1A, 0x0D,
 
	0x03,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0x03, 0x81, 0x1B, 0x05, 0xDF, 0x52,
 
	0xF1, 0x8A, 0xF9, 0xDD, 0x91, 0x3B, 0xD7, 0xE1,
 
	0x54, 0xAD, 0x90, 0x5A, 0x7B, 0xC7, 0x32, 0x62,
 
	0x16, 0x27, 0xB9, 0x66, 0x21, 0x88, 0xBD, 0x18,
 
	0x77, 0x8E, 0x94, 0x8C, 0x9B, 0x46, 0x9C, 0xB1,
 
	0xD3, 0x53, 0xB0, 0xBE, 0xAC, 0xAF, 0x73, 0x24,
 
	0xDA, 0x58, 0xE2, 0xFC, 0x78, 0xEA, 0xCD, 0xFA,
 
	0x37, 0xED, 0x13, 0x19, 0xC0, 0x59, 0x83, 0xBA,
 
	0x3F, 0x57, 0x00, 0x7E, 0xC9, 0x2E, 0x17, 0x5B,
 
	0x84, 0xF6, 0xE7, 0x22, 0xFB, 0x5F, 0x4C, 0x2C,
 
	0x61, 0x9F, 0x45, 0x39, 0xB3, 0xEC, 0x04, 0x87,
 
	0x67, 0xDC, 0x0B, 0xF4, 0x20, 0xAB, 0x6B, 0x9E,
 
	0x4B, 0xCF, 0xB4, 0x2F, 0xBB, 0xEF, 0xDB, 0x33
 
};
 

	
 
unsigned char table_111[32] = {
 
	0x09, 0x0F, 0x00, 0x15, 0x12, 0x17, 0x1A, 0x0D,
 
	0x1C, 0x0B, 0x01, 0x0A, 0x05, 0x1E, 0x1D, 0x0C,
 
	0x1B, 0x08, 0x19, 0x18, 0x14, 0x07, 0x0E, 0x03,
 
	0x10, 0x16, 0x11, 0x1F, 0x04, 0x06, 0x02, 0x13
 
};
 

	
 
unsigned char table_112[256] = {
 
	0xF9, 0x7D, 0xBE, 0xD5, 0x9F, 0xB8, 0x95, 0x43,
 
	0xDB, 0xAE, 0x7E, 0xEC, 0x5B, 0x58, 0x18, 0x49,
 
	0x4B, 0x9D, 0x1C, 0x3E, 0x61, 0xD1, 0xF6, 0x2F,
 
	0x41, 0x82, 0x51, 0x37, 0x72, 0x79, 0x05, 0x2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x34, 0x49, 0x95, 0xF1, 0x6F, 0x89, 0xA8,
 
	0xC0, 0x36, 0x0A, 0x3F, 0x60, 0x50, 0xE7, 0x08,
 
	0xCE, 0x25, 0xC1, 0x71, 0xF6, 0x59, 0x58, 0x56,
 
	0x4C, 0xAB, 0x27, 0xAC, 0x06, 0xCB, 0x00, 0x30,
 
	0x84, 0x3E, 0xC2, 0x1D, 0x02, 0xE0, 0xC5, 0xD6,
 
	0x18, 0x70, 0xA9, 0x88, 0xD9, 0x39, 0x8B, 0x6E,
 
	0xF4, 0x24, 0xA0, 0x48, 0x45, 0x21, 0x87, 0x78,
 
	0x38, 0x90, 0xE3, 0xCA, 0xF5, 0xD7, 0x2A, 0x53,
 
	0x9C, 0xCD, 0x31, 0x35, 0xAD, 0x74, 0xD8, 0x12,
 
	0xBC, 0x9E, 0x6B, 0x67, 0xB0, 0xBA, 0xE4, 0x10,
 
	0x5E, 0xFC, 0xC7, 0x0F, 0x2E, 0x81, 0x7E, 0xA1,
 
	0x8C, 0x17, 0xB5, 0xEB, 0xD5, 0xF3, 0x0B, 0x3C
 
};
 

	
 
unsigned char table_116[32] = {
 
	0x00, 0x05, 0x10, 0x1C, 0x0C, 0x1A, 0x04, 0x1B,
 
	0x0A, 0x0D, 0x14, 0x0B, 0x07, 0x03, 0x12, 0x1E,
 
	0x06, 0x11, 0x01, 0x08, 0x15, 0x09, 0x1F, 0x0F,
 
	0x19, 0x18, 0x16, 0x02, 0x13, 0x0E, 0x17, 0x1D
 
};
 

	
 
unsigned char table_117[256] = {
 
	0xD0, 0x9A, 0xAB, 0xA8, 0xA7, 0xDF, 0x28, 0xCE,
 
	0x3E, 0x51, 0xBF, 0x76, 0x03, 0xA0, 0x53, 0x3F,
 
	0x90, 0x93, 0x87, 0x67, 0x98, 0x3D, 0xEA, 0x8B,
 
	0x55, 0xCF, 0x10, 0xF3, 0x25, 0xFC, 0x9F, 0x41,
 
	0x6B, 0x54, 0x6E, 0x0B, 0x83, 0x35, 0x69, 0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xA0, 0x2A, 0x53, 0x13, 0xDD, 0xF8, 0xA8,
 
	0x0A, 0x6E, 0xCF, 0x6F, 0x7E, 0xE0, 0x3E, 0xE5,
 
	0x07, 0xCC, 0x38, 0xD1, 0xF2, 0x2C, 0x9A, 0xAC,
 
	0x88, 0x79, 0xB8, 0xC8, 0xBF, 0x63, 0x71, 0x69,
 
	0x52, 0x39, 0x9F, 0x22, 0x3F, 0x9E, 0x44, 0xFA,
 
	0x73, 0x6A, 0x8B, 0xA2, 0xD6, 0x1A, 0x9B, 0xB1,
 
	0x8A, 0x4D, 0x58, 0xA1, 0x46, 0x5F, 0x55, 0x56,
 
	0x21, 0x05, 0x15, 0x92, 0xAA, 0xEB, 0x31, 0x68,
 
	0xFB, 0x41, 0xC3, 0x4E, 0xB3, 0x40, 0x34, 0x17,
 
	0xD9, 0x29, 0x3C, 0x0C, 0xF1, 0x0B, 0x28, 0x84,
 
	0x5E, 0xCA, 0xFD, 0x11, 0xA3, 0xC7, 0xC0, 0x91
 
};
 

	
 
unsigned char table_121[32] = {
 
	0x1E, 0x12, 0x06, 0x1D, 0x15, 0x1F, 0x13, 0x0B,
 
	0x10, 0x0D, 0x1C, 0x01, 0x0A, 0x0E, 0x02, 0x19,
 
	0x04, 0x1A, 0x03, 0x11, 0x00, 0x16, 0x0C, 0x17,
 
	0x14, 0x08, 0x18, 0x05, 0x09, 0x0F, 0x1B, 0x07
 
};
 

	
 
unsigned char table_122[256] = {
 
	0x85, 0xDF, 0x7F, 0x7C, 0x56, 0xF0, 0x0C, 0x7D,
 
	0x76, 0xA8, 0x58, 0x31, 0x25, 0x8A, 0x0D, 0x23,
 
	0x05, 0x0F, 0x12, 0x64, 0x8E, 0x5D, 0xF4, 0x2C,
 
	0x18, 0xFA, 0x4B, 0xFE, 0x91, 0xBF, 0x95, 0x0B,
 
	0xF1, 0x88, 0x10, 0xD8, 0x3E, 0x53, 0x96, 0xB5,
 
	0x75, 0x24, 0x8F, 0xD6, 0x68, 0x5C, 0x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, 0xAC, 0x75, 0xB5, 0x78, 0x0A,
 
	0xA3, 0x69, 0x16, 0xBA, 0x50, 0x2A, 0x41, 0x83,
 
	0xF6, 0x64, 0x00, 0x65, 0x7E, 0xDD, 0x5B, 0xDA,
 
	0x14, 0xFD, 0x3E, 0x7F, 0xCA, 0x66, 0x4A, 0x1F,
 
	0xA2, 0xAD, 0xF2, 0x23, 0xBB, 0x72, 0xF3, 0x94,
 
	0x62, 0x1B, 0xDE, 0x91, 0x87, 0x97, 0x05, 0x2E
 
};
 

	
 
unsigned char table_125[32] = {
 
	0x1A, 0x18, 0x12, 0x15, 0x00, 0x1C, 0x01, 0x0B,
 
	0x19, 0x1B, 0x1F, 0x11, 0x07, 0x10, 0x1E, 0x06,
 
	0x17, 0x04, 0x0A, 0x0E, 0x0D, 0x0C, 0x16, 0x08,
 
	0x02, 0x03, 0x13, 0x14, 0x09, 0x1D, 0x05, 0x0F
 
};
 

	
 
unsigned char table_126[32] = {
 
	0x1C, 0x1D, 0x07, 0x12, 0x18, 0x1A, 0x19, 0x09,
 
	0x0F, 0x14, 0x1F, 0x0B, 0x13, 0x04, 0x0E, 0x1E,
 
	0x0C, 0x0D, 0x01, 0x17, 0x1B, 0x16, 0x0A, 0x05,
 
	0x15, 0x10, 0x11, 0x08, 0x00, 0x03, 0x06, 0x02
 
};
 

	
 
unsigned char table_127[256] = {
 
	0xA0, 0x66, 0xD8, 0x08, 0xEA, 0x39, 0x78, 0xAB,
 
	0x61, 0x4E, 0xC7, 0xD1, 0xA3, 0x1C, 0x9F, 0xCB,
 
	0x19, 0x51, 0x15, 0x92, 0x23, 0xFD, 0x7D, 0x1D,
 
	0x95, 0xAE, 0x0E, 0x8B, 0xE6, 0x7F, 0x86, 0x6D,
 
	0x06, 0xBD, 0x20, 0x1F, 0x3A, 0xE4, 0x54, 0x91,
 
	0x69, 0xD3, 0xE3, 0x3D, 0x4D, 0x31, 0x49, 0xA4,
 
	0x41, 0xF3, 0xE0, 0x11, 0x14, 0x9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C, 0x1D, 0x0E,
 
	0x12, 0x06, 0x18, 0x16, 0x1A, 0x09, 0x13, 0x19,
 
	0x1B, 0x00, 0x08, 0x0D, 0x0C, 0x1E, 0x04, 0x1F
 
};
 

	
 
unsigned char table_131[32] = {
 
	0x1D, 0x13, 0x1B, 0x10, 0x07, 0x03, 0x0A, 0x02,
 
	0x00, 0x0C, 0x0E, 0x0B, 0x0D, 0x18, 0x12, 0x1F,
 
	0x1A, 0x04, 0x15, 0x11, 0x1E, 0x08, 0x1C, 0x14,
 
	0x19, 0x05, 0x0F, 0x17, 0x06, 0x01, 0x09, 0x16
 
};
 

	
 
unsigned char table_132[256] = {
 
	0x33, 0x8D, 0x45, 0x6F, 0xFF, 0xF5, 0xB6, 0x53,
 
	0x3B, 0xF3, 0x07, 0xA4, 0x97, 0xEB, 0x6B, 0xA5,
 
	0xD3, 0xDC, 0x7B, 0x79, 0x93, 0xE7, 0xF7, 0x67,
 
	0x9C, 0x4F, 0x88, 0xF9, 0x3A, 0x2B, 0x27, 0x48,
 
	0x47, 0x18, 0xF4, 0xAD, 0xB4, 0x8F, 0x2A, 0x76,
 
	0x17, 0xE9, 0x1F, 0x40, 0x0C, 0x59, 0xD1, 0x4C,
 
	0x20, 0x31, 0x73, 0x54, 0xCD, 0x68, 0x08, 0x52,
 
	0x10, 0x62, 0x3D, 0xD2, 0x77, 0xF2, 0xD7, 0x30,
 
	0xCA, 0x16, 0x01, 0x50, 0x9F, 0x3F, 0x75, 0xED,
 
	0x90, 0x6A, 0x34, 0xCE, 0x05, 0x78, 0x5E, 0xD6,
 
	0x85, 0xCC, 0x29, 0xB8, 0xC1, 0x0D, 0xCB, 0x80,
 
	0x2E, 0x04, 0x00, 0x44, 0x32, 0x95, 0xBF, 0xFE,
 
	0x6E, 0x7C, 0xFD, 0xA7, 0x3C, 0x5C, 0xF0, 0xEC,
 
	0xAC, 0xF8, 0xB9, 0xC0, 0x1B, 0x3E, 0xE8, 0x66,
 
	0x5D, 0xDE, 0x49,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 0x48, 0x3C, 0x51,
 
	0x6D, 0x5B, 0x74, 0xC3, 0xC9, 0x09, 0x3D, 0x9F,
 
	0xDB, 0x32, 0x40, 0x18, 0xD7, 0xCE, 0x69, 0x49,
 
	0x3A, 0xF1, 0xB9, 0x56, 0x91, 0x99, 0x84, 0x24,
 
	0x7E, 0x34, 0x4B, 0xA6, 0x47, 0xB4, 0x6E, 0xDF,
 
	0x65, 0x3B, 0xAD, 0x45, 0x13, 0xC6, 0x81, 0xF8,
 
	0x4A, 0x2D, 0x8F, 0x4C, 0x97, 0x28, 0x3E, 0xE5,
 
	0x5A, 0x35, 0xB0, 0xAE, 0x82, 0x79, 0x1D, 0x52
 
};
 

	
 
unsigned char table_134[32] = {
 
	0x09, 0x0F, 0x10, 0x0C, 0x03, 0x15, 0x07, 0x17,
 
	0x0E, 0x0B, 0x1D, 0x08, 0x19, 0x11, 0x00, 0x0A,
 
	0x01, 0x06, 0x18, 0x16, 0x0D, 0x13, 0x14, 0x12,
 
	0x02, 0x1B, 0x1A, 0x04, 0x05, 0x1F, 0x1C, 0x1E
 
};
 

	
 
unsigned char table_135[256] = {
 
	0x14, 0x34, 0xEA, 0x02, 0x2B, 0x5A, 0x10, 0x51,
 
	0xF3, 0x8F, 0x28, 0xB2, 0x50, 0x8B, 0x01, 0xCC,
 
	0x80, 0x15, 0x29, 0x42, 0xF4, 0x1D, 0xFB, 0xBB,
 
	0x1F, 0x43, 0x8C, 0x17, 0x1E, 0x81, 0x04, 0x98,
 
	0x46, 0xD8, 0xD5, 0x65, 0x4C, 0x1C, 0xDB, 0x40,
 
	0x5F, 0x1A, 0x31, 0x74, 0xF1, 0x64, 0x19, 0x05,
 
	0xFC, 0xF0, 0x73, 0xB6, 0x23, 0x77, 0x9C, 0xCE,
 
	0x70, 0xEF, 0xDA, 0xE0, 0xA2, 0x78, 0x84, 0xEB,
 
	0x9E, 0xC5, 0x95, 0xA3, 0xF6, 0xCA, 0xAD, 0x52,
 
	0xD0, 0x3F, 0x54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, 0x2C, 0x02, 0x25,
 
	0xC0, 0x8F, 0x61, 0xA4, 0x39, 0x8C, 0x5D, 0xAE,
 
	0x22, 0x1C, 0x2F, 0xD4, 0x6C, 0xD1, 0x51, 0xEA,
 
	0x4F, 0x7E, 0xA0, 0xF5, 0x6A, 0x32, 0xA2, 0x01,
 
	0xB5, 0x10, 0x2A, 0xAC, 0xA9, 0x06, 0xC4, 0x91,
 
	0x68, 0xE1, 0xBD, 0x14, 0x38, 0xFA, 0x6E, 0x3F,
 
	0x37, 0x66, 0xDB, 0x57, 0x43, 0x1B, 0x67, 0xAF,
 
	0x1F, 0x0B, 0x6D, 0x2D, 0x89, 0x04, 0x4B, 0x52,
 
	0xC2, 0xBF, 0xA1, 0x92, 0x99, 0x6F, 0x63, 0x81,
 
	0x27, 0x05, 0x96, 0x3A, 0xEC, 0x0E, 0x97, 0xD9,
 
	0xDE, 0x46, 0x35, 0x8B, 0x8E, 0x8A, 0xF4, 0xFF,
 
	0x60, 0xD7, 0xE9, 0x17, 0xEB, 0x9C, 0x84, 0x0C,
 
	0x93, 0x1D, 0x9B, 0x5B, 0x40, 0xEE, 0x42, 0x19
 
};
 

	
 
unsigned char table_137[32] = {
 
	0x0F, 0x09, 0x02, 0x06, 0x18, 0x0B, 0x1E, 0x05,
 
	0x11, 0x1D, 0x16, 0x01, 0x13, 0x10, 0x0E, 0x1A,
 
	0x1B, 0x00, 0x0D, 0x08, 0x15, 0x14, 0x19, 0x17,
 
	0x03, 0x1F, 0x0A, 0x12, 0x0C, 0x07, 0x04, 0x1C
 
};
 

	
 
unsigned char table_138[32] = {
 
	0x0D, 0x1C, 0x1F, 0x15, 0x0F, 0x14, 0x1B, 0x12,
 
	0x09, 0x0B, 0x19, 0x07, 0x11, 0x16, 0x0C, 0x04,
 
	0x13, 0x05, 0x1D, 0x03, 0x0E, 0x0A, 0x08, 0x1E,
 
	0x01, 0x06, 0x18, 0x17, 0x10, 0x1A, 0x02, 0x00
 
};
 

	
 
unsigned char table_139[32] = {
 
	0x05, 0x15, 0x1D, 0x02, 0x0F, 0x03, 0x17, 0x1A,
 
	0x0A, 0x00, 0x1F, 0x12, 0x0E, 0x11, 0x1B, 0x13,
 
	0x0B, 0x0D, 0x09, 0x18, 0x1E, 0x08, 0x14, 0x07,
 
	0x0C, 0x04, 0x16, 0x19, 0x1C, 0x06, 0x10, 0x01
 
};
 

	
 
unsigned char table_140[32] = {
 
	0x06, 0x1E, 0x0C, 0x11, 0x13, 0x08, 0x15, 0x01,
 
	0x1D, 0x03, 0x0F, 0x19, 0x18, 0x04, 0x00, 0x14,
 
	0x12, 0x1A, 0x0B, 0x0E, 0x02, 0x1B, 0x07, 0x05,
 
	0x1F, 0x17, 0x09, 0x0A, 0x0D, 0x16, 0x10, 0x1C
 
};
 

	
 
unsigned char table_141[256] = {
 
	0xE1, 0x0A, 0x28, 0xCD, 0x8A, 0x1E, 0x26, 0x10,
 
	0xC0, 0x6F, 0x06, 0x2C, 0xF8, 0x51, 0x6C, 0x8F,
 
	0xA8, 0x8C, 0x41, 0xF4, 0xED, 0x36, 0xAC, 0x89,
 
	0xBD, 0x9D, 0x42, 0x50, 0x95, 0x07, 0x2A, 0x9B,
 
	0x7E, 0xA3, 0x6B, 0x30, 0x72, 0x4E, 0xBE, 0xD8,
 
	0x8B, 0x5B, 0x1A, 0x56, 0x05, 0xEF, 0xEE, 0x64,
 
	0xFF, 0xFD, 0x93, 0xB5, 0xD6, 0x04, 0x57, 0xAE,
 
	0x4D, 0x6D, 0x2F, 0xBA, 0x40, 0xE0, 0xDB, 0xF2,
 
	0xCC, 0x08, 0x35, 0x02, 0xC4, 0x65, 0x66, 0x76,
 
	0xA1, 0x97, 0x9F, 0x6A, 0x90, 0xA7, 0x34, 0x1B,
 
	0x18, 0xB9, 0xA2, 0xDE, 0x23, 0x1F, 0xCB, 0xE6,
 
	0xAB, 0xCF, 0xAD, 0x4A, 0xF7, 0x24, 0xD0, 0xE8,
 
	0x8D, 0x49, 0xEA, 0x0F, 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E7,
 
	0x10, 0x19, 0xD0, 0xEF, 0xBD, 0xC2, 0x44, 0xB4,
 
	0xF7, 0xA4, 0x53, 0x9F, 0x86, 0xFA, 0xE8, 0x4B,
 
	0x28, 0x3D, 0x9B, 0x56, 0x89, 0x6B, 0x25, 0x71,
 
	0x60, 0x11, 0x9A, 0x5E, 0x1A, 0x52, 0x08, 0x4F,
 
	0xB1, 0xDD, 0xBB, 0x98, 0xFB, 0x12, 0x3B, 0x0A,
 
	0x2E, 0xDB, 0x62, 0x8D, 0xC4, 0x75, 0xA9, 0x2B,
 
	0xE4, 0x97, 0x72, 0xF5, 0xEE, 0xF2, 0xB6, 0x21,
 
	0xBA, 0x7A, 0x76, 0x41, 0x50, 0x66, 0x05, 0x8E,
 
	0xCC, 0x1E, 0x87, 0xD7, 0x01, 0x1F, 0x51, 0x95
 
};
 

	
 
unsigned char table_143[32] = {
 
	0x0E, 0x16, 0x18, 0x11, 0x0C, 0x01, 0x12, 0x1F,
 
	0x08, 0x15, 0x0A, 0x06, 0x1C, 0x1E, 0x02, 0x1A,
 
	0x17, 0x03, 0x07, 0x13, 0x05, 0x19, 0x10, 0x0F,
 
	0x0D, 0x14, 0x09, 0x0B, 0x1B, 0x00, 0x1D, 0x04
 
};
 

	
 
unsigned char table_144[32] = {
 
	0x00, 0x1B, 0x17, 0x19, 0x1D, 0x11, 0x0D, 0x1A,
 
	0x13, 0x03, 0x1E, 0x09, 0x10, 0x0E, 0x15, 0x05,
 
	0x0B, 0x1C, 0x1F, 0x08, 0x0A, 0x06, 0x01, 0x0F,
 
	0x16, 0x14, 0x02, 0x04, 0x07, 0x18, 0x12, 0x0C
 
};
 

	
 
unsigned char table_145[256] = {
 
	0xF9, 0x2C, 0x38, 0x74, 0xDA, 0x65, 0x85, 0x0E,
 
	0xBA, 0x64, 0xDB, 0xE3, 0xB6, 0x8B, 0x0B, 0x5E,
 
	0x01, 0x0F, 0x12, 0x8C, 0xD4, 0xCC, 0xB1, 0x7B,
 
	0xE7,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4C, 0x49, 0xD4, 0x5D, 0x3E,
 
	0xBA, 0x1D, 0xD8, 0x91, 0x74, 0x10, 0xF8, 0xDE,
 
	0xEF, 0xF0, 0x6A, 0x04, 0x72, 0x08, 0x78, 0x3A,
 
	0x53, 0xC4, 0x34, 0xF2, 0x64, 0xAF, 0x86, 0xC3,
 
	0xF3, 0x73, 0x67, 0xCC, 0x58, 0xF4, 0x96, 0xAC,
 
	0x3D, 0xE7, 0x15, 0x8E, 0x19, 0x61, 0xF9, 0xB6,
 
	0xCD, 0x87, 0xAA, 0xB0, 0x1F, 0x6F, 0xAD, 0x28,
 
	0xC8, 0x69, 0x56, 0xC1, 0x71, 0xED, 0xE6, 0x98,
 
	0x6B, 0x59, 0xB7, 0xF5, 0x2C, 0xEC, 0xA8, 0x94,
 
	0x89, 0xBB, 0xA9, 0xD7, 0x2F, 0x8A, 0x4E, 0xD6,
 
	0x33, 0x16, 0x0D, 0x83, 0x5C, 0x52, 0x85, 0xA6,
 
	0x40, 0x45, 0x9F, 0x44, 0x63, 0x35, 0x77, 0xFF,
 
	0x09, 0x43, 0xBF, 0xD0, 0x55, 0xDD, 0x3F, 0x24
 
};
 

	
 
unsigned char table_149[32] = {
 
	0x1B, 0x0B, 0x0C, 0x06, 0x1F, 0x17, 0x04, 0x1A,
 
	0x1E, 0x02, 0x0F, 0x16, 0x0E, 0x09, 0x10, 0x01,
 
	0x13, 0x19, 0x11, 0x00, 0x0A, 0x05, 0x03, 0x1C,
 
	0x18, 0x1D, 0x14, 0x0D, 0x07, 0x08, 0x15, 0x12
 
};
 

	
 
unsigned char table_150[256] = {
 
	0x57, 0xBC, 0x9D, 0x46, 0x14, 0xD0, 0x94, 0x95,
 
	0x1B, 0x12, 0xB8, 0xD4, 0x53, 0x73, 0x83, 0xE6,
 
	0x75, 0xE1, 0xD1, 0x0D, 0xDF, 0x23, 0x13, 0x40,
 
	0xF1, 0x0C, 0xA0, 0xC1, 0x22, 0xDA, 0xE8, 0xFB,
 
	0xE5,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, 0x19, 0x11, 0x08, 0x15
 
};
 

	
 
unsigned char table_153[32] = {
 
	0x0E, 0x14, 0x12, 0x1E, 0x1C, 0x02, 0x06, 0x16,
 
	0x18, 0x0D, 0x17, 0x0C, 0x1D, 0x11, 0x08, 0x19,
 
	0x07, 0x0F, 0x13, 0x04, 0x03, 0x1B, 0x0B, 0x1F,
 
	0x1A, 0x0A, 0x05, 0x10, 0x00, 0x01, 0x15, 0x09
 
};
 

	
 
unsigned char table_154[256] = {
 
	0x27, 0x5A, 0x08, 0x5B, 0xF4, 0x39, 0x13, 0x6F,
 
	0x67, 0xEA, 0x22, 0xCA, 0x5C, 0xCF, 0x18, 0x7C,
 
	0x05, 0x87, 0x60, 0xCC, 0x40, 0xC6, 0xE8, 0x6D,
 
	0xF5, 0x2A, 0x2D, 0xA2, 0x8C, 0x82, 0xE9, 0xDC,
 
	0xD6, 0x65, 0x74, 0x8E, 0x42, 0x4F, 0x3E, 0x55,
 
	0xFF, 0xC7, 0x9D, 0x0F, 0x81, 0xE2, 0x4C, 0xE6,
 
	0xEB, 0x4D, 0x70, 0xD1, 0x49, 0x43, 0x3D, 0x69,
 
	0x0C, 0x45, 0x28, 0x00, 0x99, 0xAE, 0xEC, 0xB8,
 
	0xC3, 0x17, 0x93, 0x8D, 0x36, 0x3C, 0x46, 0x2B,
 
	0x29, 0xC5, 0xB4, 0xB1, 0xD0, 0x0D, 0xAD, 0xFE,
 
	0xE5, 0xA8, 0x3B, 0x1A, 0x2C, 0xDF, 0x07, 0x86,
 
	0xB0, 0xD3, 0x7A, 0x59, 0x79, 0x8B, 0xC1, 0x9A,
 
	0x30, 0xDB, 0x24, 0xF3, 0xD8, 0x04, 0x25, 0xC2,
 
	0xA3, 0x98, 0x96, 0x7B, 0x71, 0x4E, 0x5E, 0x58,
 
	0xA5, 0x51, 0x88, 0xDA, 0xF8, 0xC0, 0x7D, 0xF6,
 
	0x31, 0x5F, 0x09, 0x16, 0x21, 0x62, 0x01, 0x64,
 
	0x9B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D, 0xF8, 0xB2, 0xE6, 0xAB, 0xF4,
 
	0x23, 0x6E, 0xF0, 0x6B, 0x0B, 0x2E, 0xC8, 0xC4,
 
	0x4F, 0xA8, 0x6D, 0x26, 0xE9, 0x9C, 0x22, 0xB7,
 
	0x00, 0xB3, 0x0A, 0x7C, 0x44, 0x55, 0x75, 0xD5,
 
	0xAA, 0x66, 0x56, 0x24, 0x83, 0x90, 0xA4, 0xF5,
 
	0xCD, 0xEC, 0x18, 0xDC, 0xFE, 0x96, 0xA3, 0xF7,
 
	0xD2, 0xFB, 0xD1, 0x65, 0xC5, 0x08, 0x7B, 0x70,
 
	0x16, 0x9A, 0x20, 0x09, 0x29, 0xDA, 0x52, 0x5A,
 
	0x59, 0xB4, 0x77, 0x62, 0x9E, 0x19, 0x7F, 0x82,
 
	0x4C, 0xB6, 0x0D, 0x58, 0xEE, 0x1D, 0xB9, 0x93,
 
	0x50, 0xD9, 0x30, 0xE4, 0x13, 0x01, 0x36, 0x8F,
 
	0x53, 0x3F, 0x64, 0xA5, 0xB5, 0xD7, 0x81, 0x41,
 
	0x17, 0xE5, 0x94, 0xE3, 0xF9, 0x61, 0x76, 0xE1,
 
	0x9F, 0xFC, 0x1E, 0x12, 0xDB, 0x21, 0x79, 0x2A,
 
	0xAC, 0xF3, 0x6C, 0xC1, 0x95, 0x92, 0xEB, 0xA7,
 
	0x11, 0xC7, 0xB8, 0x4A, 0x33, 0xB0, 0x99, 0xE7,
 
	0xF1, 0x68, 0xBE, 0x35, 0x40, 0x8C, 0xD4, 0x47
 
};
 

	
 
unsigned char table_157[32] = {
 
	0x00, 0x0D, 0x03, 0x02, 0x11, 0x04, 0x18, 0x0B,
 
	0x14, 0x1D, 0x1C, 0x13, 0x1B, 0x17, 0x10, 0x15,
 
	0x01, 0x19, 0x07, 0x09, 0x1A, 0x16, 0x12, 0x1E,
 
	0x08, 0x06, 0x0C, 0x0E, 0x1F, 0x0F, 0x0A, 0x05
 
};
 

	
 
unsigned char table_158[256] = {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, 0xDE,
 
	0x90, 0x9E, 0x7D, 0x64, 0x2B, 0x79, 0xF5, 0xF3,
 
	0x13, 0x1C, 0x7A, 0x07, 0x4F, 0x78, 0x89, 0xB6,
 
	0x97, 0xF1, 0xD7, 0x7C, 0x48, 0xAE, 0x39, 0xA8,
 
	0xA6, 0x86, 0x3E, 0x27, 0x87, 0x73, 0x82, 0x24,
 
	0x30, 0x74, 0x5F, 0xD1, 0x9F, 0x9C, 0x1A, 0x8C,
 
	0x42, 0x6E, 0x28, 0xB9, 0xF0, 0xC4, 0x68, 0x25,
 
	0xC5, 0xDC, 0xB8, 0x29, 0xD6, 0x84, 0x3D, 0xBB,
 
	0x88, 0x76, 0xFD, 0x61, 0x94, 0x91, 0xDA, 0xB7,
 
	0x72, 0xBA, 0xC2, 0xDB, 0xB5, 0xA5, 0xE2, 0x18,
 
	0xF6, 0xAA, 0x8A, 0x19, 0x63, 0x9A, 0xA7, 0xC8,
 
	0xD4, 0x02, 0x8E, 0x37, 0xF4, 0xB3, 0xA2, 0x53,
 
	0x38, 0xCC, 0x58, 0x44, 0xBF, 0x93, 0x5D, 0xC7
 
};
 

	
 
unsigned char table_163[32] = {
 
	0x1B, 0x14, 0x12, 0x15, 0x11, 0x1D, 0x17, 0x19,
 
	0x10, 0x09, 0x08, 0x06, 0x1A, 0x16, 0x07, 0x13,
 
	0x1F, 0x0B, 0x1C, 0x05, 0x0E, 0x00, 0x18, 0x0A,
 
	0x04, 0x01, 0x03, 0x0C, 0x0D, 0x1E, 0x02, 0x0F
 
};
 

	
 
unsigned char table_164[32] = {
 
	0x15, 0x00, 0x10, 0x0B, 0x1D, 0x0A, 0x06, 0x1C,
 
	0x0D, 0x1F, 0x17, 0x0F, 0x03, 0x14, 0x13, 0x12,
 
	0x1B, 0x18, 0x08, 0x1E, 0x16, 0x09, 0x1A, 0x04,
 
	0x02, 0x0C, 0x0E, 0x01, 0x07, 0x19, 0x11, 0x05
 
};
 

	
 
unsigned char table_165[256] = {
 
	0x98,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x61, 0x6E, 0xE9, 0x63, 0xF9, 0xEC,
 
	0x48, 0x30, 0x87, 0x83, 0x12, 0x4D, 0x65, 0xDF
 
};
 

	
 
unsigned char table_171[32] = {
 
	0x07, 0x06, 0x11, 0x08, 0x0C, 0x1F, 0x19, 0x02,
 
	0x14, 0x04, 0x0D, 0x18, 0x1A, 0x05, 0x17, 0x13,
 
	0x1C, 0x1B, 0x15, 0x03, 0x01, 0x0F, 0x16, 0x1E,
 
	0x1D, 0x10, 0x00, 0x12, 0x0B, 0x0E, 0x09, 0x0A
 
};
 

	
 
unsigned char table_172[32] = {
 
	0x11, 0x01, 0x1F, 0x06, 0x1A, 0x04, 0x02, 0x09,
 
	0x05, 0x0D, 0x0B, 0x18, 0x0E, 0x12, 0x1B, 0x17,
 
	0x07, 0x08, 0x1D, 0x1E, 0x14, 0x19, 0x16, 0x15,
 
	0x03, 0x0C, 0x00, 0x10, 0x0A, 0x1C, 0x0F, 0x13
 
};
 

	
 
unsigned char table_173[32] = {
 
	0x1F, 0x0B, 0x13, 0x00, 0x16, 0x15, 0x14, 0x0A,
 
	0x1D, 0x05, 0x1E, 0x1A, 0x0F, 0x04, 0x0E, 0x01,
 
	0x19, 0x07, 0x02, 0x12, 0x0C, 0x17, 0x08, 0x09,
 
	0x03, 0x11, 0x18, 0x10, 0x1C, 0x1B, 0x06, 0x0D
 
};
 

	
 
unsigned char table_174[32] = {
 
	0x02, 0x1B, 0x0C, 0x17, 0x1F, 0x05, 0x15, 0x1E,
 
	0x16, 0x09, 0x1A, 0x12, 0x0F, 0x1C, 0x18, 0x0A,
 
	0x19, 0x10, 0x0D, 0x13, 0x04, 0x11, 0x08, 0x14,
 
	0x1D, 0x0E, 0x06, 0x00, 0x01, 0x07, 0x0B, 0x03
 
};
 

	
 
unsigned char table_175[32] = {
 
	0x00, 0x06, 0x0B, 0x08, 0x0C, 0x04, 0x1A, 0x1C,
 
	0x05, 0x1E, 0x14, 0x03, 0x0A, 0x18, 0x12, 0x1D,
 
	0x16, 0x1F, 0x07, 0x09, 0x0F, 0x0E, 0x17, 0x13,
 
	0x11, 0x19, 0x10, 0x0D, 0x1B, 0x02, 0x01, 0x15
 
};
 

	
 
unsigned char table_176[32] = {
 
	0x12, 0x03, 0x1A, 0x15, 0x04, 0x19, 0x0B, 0x1B,
 
	0x17, 0x1E, 0x0D, 0x05, 0x11, 0x14, 0x1C, 0x00,
 
	0x18, 0x10, 0x0A, 0x06, 0x0E, 0x08, 0x02, 0x07,
 
	0x13, 0x09, 0x16, 0x1D, 0x0F, 0x0C, 0x01, 0x1F
 
};
 

	
 
unsigned char table_177[256] = {
 
	0x5E, 0x4D, 0x76, 0xFE, 0xB5, 0x50, 0x83, 0x23,
 
	0x72, 0xDD, 0x93, 0x08, 0x69, 0xAD, 0xEC, 0x3B,
 
	0x0B, 0x9A, 0x36, 0xC9, 0xCA, 0xBE, 0xF7, 0x30,
 
	0x19, 0x39, 0x2C, 0xAB, 0xE3, 0x7B, 0xBC, 0x32,
 
	0xA0, 0xE4, 0xA6, 0xB6, 0xCB, 0xC8, 0x37, 0x07,
 
	0xD2, 0xA1, 0xD9, 0xF6, 0xBF, 0xF5, 0x88, 0x01,
 
	0x95, 0x0F, 0x03, 0xFD, 0xE6, 0x68, 0x90, 0x61,
 
	0x21, 0x6D, 0x3C, 0x62, 0x34, 0x2B, 0x71, 0x4B,
 
	0x44, 0x64, 0x75, 0xA2, 0x6A, 0xFF, 0x29, 0xBD,
 
	0x35, 0x15, 0xF9, 0xC1, 0x09, 0x45, 0xB2, 0xF2,
 
	0x3F, 0xCE, 0xB0, 0xC0, 0xB8, 0x00, 0x05, 0xD7,
 
	0x11, 0xC6, 0x78, 0x53, 0x9E, 0xB3, 0xED, 0x56,
 
	0x22, 0x5C, 0x9D, 0x6C, 0x99, 0x43, 0x2F, 0xAE,
 
	0xEB, 0x40, 0x8C, 0x1F, 0xC2, 0xDF, 0x92, 0x65,
 
	0x6F, 0x79, 0x5D, 0x5B, 0xAA, 0xDB, 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, 0xA9, 0xE1, 0x40,
 
	0xEB, 0xE8, 0x5F, 0xF5, 0xC3, 0xD1, 0x34, 0xFB,
 
	0xEC, 0xF7, 0x9C, 0xC7, 0xDD, 0x6C, 0x36, 0x9D,
 
	0x42, 0x59, 0x99, 0x5D, 0xD8, 0x82, 0x07, 0x24,
 
	0x6D, 0xAD, 0x13, 0x48, 0x6B, 0x6E, 0x75, 0x4D,
 
	0xD5, 0x02, 0xED, 0xFE, 0x91, 0xCD, 0x77, 0xB0,
 
	0xF6, 0xC8, 0x6F, 0x23, 0xAF, 0xB7, 0x2D, 0xD6,
 
	0xA4, 0xE2, 0x45, 0x8F, 0x21, 0xE0, 0x49, 0x22,
 
	0x7B, 0x17, 0x0B, 0x0A, 0x41, 0x03, 0xD4, 0x4B
 
};
 

	
 
unsigned char table_183[32] = {
 
	0x1E, 0x1B, 0x11, 0x07, 0x08, 0x06, 0x18, 0x17,
 
	0x0D, 0x0F, 0x12, 0x03, 0x1D, 0x04, 0x0A, 0x1A,
 
	0x0C, 0x13, 0x14, 0x1F, 0x0B, 0x19, 0x10, 0x01,
 
	0x16, 0x05, 0x1C, 0x0E, 0x02, 0x00, 0x09, 0x15
 
};
 

	
 
unsigned char table_184[32] = {
 
	0x0F, 0x1D, 0x17, 0x16, 0x0D, 0x05, 0x13, 0x1F,
 
	0x1B, 0x09, 0x1C, 0x1E, 0x15, 0x01, 0x06, 0x08,
 
	0x0C, 0x10, 0x0B, 0x02, 0x04, 0x0A, 0x07, 0x1A,
 
	0x18, 0x0E, 0x03, 0x11, 0x12, 0x14, 0x19, 0x00
 
};
 

	
 
unsigned char table_185[256] = {
 
	0xA5, 0xEE, 0x2E, 0x28, 0xA7, 0xAC, 0xD9, 0xB2,
 
	0x6E, 0x04, 0xB4, 0x03, 0xE8, 0x92, 0x5F, 0x4D,
 
	0x73, 0x20, 0x71, 0xE0, 0x43, 0x53, 0x3F, 0xF8,
 
	0x96, 0xA1, 0x24, 0x97, 0xAD, 0x7B, 0xE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0x18, 0x03, 0x1C, 0x12, 0x11, 0x0D, 0x02
 
};
 

	
 
struct yahoo_fn yahoo_fntable[5][96] = { {{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0},
 
	{IDENT, 0, 0}},
 
{{MULADD, 0x36056CD7, 0x4387},
 
	{LOOKUP, (long)table_0, 0},
 
	{LOOKUP, (long)table_1, 0},
 
	{BITFLD, (long)table_2, 0},
 
	{LOOKUP, (long)table_3, 0},
 
	{BITFLD, (long)table_4, 0},
 
	{MULADD, 0x4ABB534D, 0x3769},
 
	{XOR, 0x1D242DA5, 0},
 
	{MULADD, 0x3C23132D, 0x339B},
 
	{XOR, 0x0191265C, 0},
 
	{XOR, 0x3DB979DB, 0},
 
	{LOOKUP, (long)table_5, 0},
 
	{XOR, 0x1A550E1E, 0},
 
	{XOR, 0x2F140A2D, 0},
 
	{MULADD, 0x7C466A4B, 0x29BF},
 
	{XOR, 0x2D3F30D3, 0},
 
	{MULADD, 0x7E823B21, 0x6BB3},
 
	{BITFLD, (long)table_6, 0},
 
	{LOOKUP, (long)table_7, 0},
 
	{BITFLD, (long)table_8, 0},
 
	{LOOKUP, (long)table_9, 0},
 
	{BITFLD, (long)table_10, 0},
 
	{LOOKUP, (long)table_11, 0},
 
	{BITFLD, (long)table_12, 0},
 
	{LOOKUP, (long)table_13, 0},
 
	{BITFLD, (long)table_14, 0},
 
	{MULADD, 0x5B756AB9, 0x7E9B},
 
	{LOOKUP, (long)table_15, 0},
 
	{XOR, 0x1D1C4911, 0},
 
	{LOOKUP, (long)table_16, 0},
 
	{LOOKUP, (long)table_17, 0},
 
	{XOR, 0x46BD7771, 0},
 
	{XOR, 0x51AE2B42, 0},
 
	{MULADD, 0x2417591B, 0x177B},
 
	{MULADD, 0x57F27C5F, 0x2433},
 
	{LOOKUP, (long)table_18, 0},
 
	{LOOKUP, (long)table_19, 0},
 
	{XOR, 0x71422261, 0},
 
	{BITFLD, (long)table_20, 0},
 
	{MULADD, 0x58E937F9, 0x1075},
 
	{LOOKUP, (long)table_21, 0},
 
	{BITFLD, (long)table_22, 0},
 
	{LOOKUP, (long)table_23, 0},
 
	{LOOKUP, (long)table_24, 0},
 
	{MULADD, 0x0B4C3D13, 0x1597},
 
	{BITFLD, (long)table_25, 0},
 
	{XOR, 0x0FE07D38, 0},
 
	{MULADD, 0x689B4017, 0x3CFB},
 
	{BITFLD, (long)table_26, 0},
 
	{LOOKUP, (long)table_27, 0},
 
	{XOR, 0x35413DF3, 0},
 
	{MULADD, 0x05B611AB, 0x570B},
 
	{MULADD, 0x0DA5334F, 0x3AC7},
 
	{XOR, 0x47706008, 0},
 
	{BITFLD, (long)table_28, 0},
 
	{LOOKUP, (long)table_29, 0},
 
	{BITFLD, (long)table_30, 0},
 
	{XOR, 0x57611B36, 0},
 
	{MULADD, 0x314C2CD1, 0x2B5B},
 
	{XOR, 0x1EF33946, 0},
 
	{MULADD, 0x28EA041F, 0x638F},
 
	{LOOKUP, (long)table_31, 0},
 
	{LOOKUP, (long)table_32, 0},
 
	{LOOKUP, (long)table_33, 0},
 
	{MULADD, 0x511537CB, 0x7135},
 
	{MULADD, 0x1CF71007, 0x5E17},
 
	{XOR, 0x583D4BCF, 0},
 
	{LOOKUP, (long)table_34, 0},
 
	{XOR, 0x373E6856, 0},
 
	{MULADD, 0x4D595519, 0x1A7D},
 
	{LOOKUP, (long)table_35, 0},
 
	{LOOKUP, (long)table_36, 0},
 
	{XOR, 0x0E2A36A7, 0},
 
	{LOOKUP, (long)table_37, 0},
 
	{LOOKUP, (long)table_38, 0},
 
	{BITFLD, (long)table_39, 0},
 
	{BITFLD, (long)table_40, 0},
 
	{XOR, 0x53F3604F, 0},
 
	{BITFLD, (long)table_41, 0},
 
	{BITFLD, (long)table_42, 0},
 
	{MULADD, 0x1EDC0BA3, 0x7531},
 
	{LOOKUP, (long)table_43, 0},
 
	{XOR, 0x10DF1038, 0},
 
	{BITFLD, (long)table_44, 0},
 
	{LOOKUP, (long)table_45, 0},
 
	{XOR, 0x4EDE0CAC, 0},
 
	{MULADD, 0x2F076EEB, 0x5BCF},
 
	{XOR, 0x6D86030F, 0},
 
	{XOR, 0x3F331713, 0},
 
	{LOOKUP, (long)table_46, 0},
 
	{MULADD, 0x41CD726F, 0x3F79},
 
	{BITFLD, (long)table_47, 0},
 
	{XOR, 0x0ECE0054, 0},
 
	{MULADD, 0x19B32B03, 0x4AD1},
 
	{BITFLD, (long)table_48, 0},
 
	{BITFLD, (long)table_49, 0}},
 
{{MULADD, 0x39731111, 0x419B},
 
	{XOR, 0x54F7757A, 0},
 
	{BITFLD, (long)table_50, 0},
 
	{BITFLD, (long)table_51, 0},
 
	{LOOKUP, (long)table_52, 0},
 
	{LOOKUP, (long)table_53, 0},
 
	{MULADD, 0x3CC0256B, 0x7CE7},
 
	{XOR, 0x79991847, 0},
 
	{MULADD, 0x228F7FB5, 0x472D},
 
	{MULADD, 0x32DA290B, 0x7745},
 
	{XOR, 0x7A28180D, 0},
 
	{BITFLD, (long)table_54, 0},
 
	{BITFLD, (long)table_55, 0},
 
	{MULADD, 0x5C814F8B, 0x227F},
 
	{LOOKUP, (long)table_56, 0},
 
	{MULADD, 0x0B496F6D, 0x412D},
 
	{XOR, 0x6F4B62DA, 0},
 
	{LOOKUP, (long)table_57, 0},
 
	{XOR, 0x64973977, 0},
 
	{LOOKUP, (long)table_58, 0},
 
	{LOOKUP, (long)table_59, 0},
 
	{BITFLD, (long)table_60, 0},
 
	{LOOKUP, (long)table_61, 0},
 
	{LOOKUP, (long)table_62, 0},
 
	{XOR, 0x6DD14C92, 0},
 
	{LOOKUP, (long)table_63, 0},
 
	{BITFLD, (long)table_64, 0},
 
	{BITFLD, (long)table_65, 0},
 
	{BITFLD, (long)table_66, 0},
 
	{LOOKUP, (long)table_67, 0},
 
	{XOR, 0x5E6324D8, 0},
 
	{LOOKUP, (long)table_68, 0},
 
	{LOOKUP, (long)table_69, 0},
 
	{LOOKUP, (long)table_70, 0},
 
	{BITFLD, (long)table_71, 0},
 
	{XOR, 0x62745ED0, 0},
 
	{MULADD, 0x102C215B, 0x0581},
 
	{LOOKUP, (long)table_72, 0},
 
	{LOOKUP, (long)table_73, 0},
 
	{LOOKUP, (long)table_74, 0},
 
	{MULADD, 0x19511111, 0x12C1},
 
	{LOOKUP, (long)table_75, 0},
 
	{MULADD, 0x2A6E2953, 0x6977},
 
	{LOOKUP, (long)table_76, 0},
 
	{XOR, 0x55CD5445, 0},
 
	{BITFLD, (long)table_77, 0},
 
	{BITFLD, (long)table_78, 0},
 
	{MULADD, 0x646C21EB, 0x43E5},
 
	{XOR, 0x71DC4898, 0},
 
	{XOR, 0x167519CB, 0},
 
	{XOR, 0x6D3158F8, 0},
 
	{XOR, 0x7EA95BEA, 0},
 
	{BITFLD, (long)table_79, 0},
 
	{XOR, 0x47377587, 0},
 
	{XOR, 0x2D8B6E8F, 0},
 
	{MULADD, 0x5E6105DB, 0x1605},
 
	{XOR, 0x65B543C8, 0},
 
	{LOOKUP, (long)table_80, 0},
 
	{BITFLD, (long)table_81, 0},
 
	{MULADD, 0x48AF73CB, 0x0A67},
 
	{XOR, 0x4FB96154, 0},
 
	{LOOKUP, (long)table_82, 0},
 
	{BITFLD, (long)table_83, 0},
 
	{XOR, 0x622C4954, 0},
 
	{BITFLD, (long)table_84, 0},
 
	{XOR, 0x20D220F3, 0},
 
	{XOR, 0x361D4F0D, 0},
 
	{XOR, 0x2B2000D1, 0},
 
	{XOR, 0x6FB8593E, 0},
 
	{LOOKUP, (long)table_85, 0},
 
	{BITFLD, (long)table_86, 0},
 
	{XOR, 0x2B7F7DFC, 0},
 
	{MULADD, 0x5FC41A57, 0x0693},
 
	{MULADD, 0x17154387, 0x2489},
 
	{BITFLD, (long)table_87, 0},
 
	{BITFLD, (long)table_88, 0},
 
	{BITFLD, (long)table_89, 0},
 
	{LOOKUP, (long)table_90, 0},
 
	{XOR, 0x7E221470, 0},
 
	{XOR, 0x7A600061, 0},
 
	{BITFLD, (long)table_91, 0},
 
	{BITFLD, (long)table_92, 0},
 
	{LOOKUP, (long)table_93, 0},
 
	{BITFLD, (long)table_94, 0},
 
	{MULADD, 0x00E813A5, 0x2CE5},
 
	{MULADD, 0x3D707E25, 0x3827},
 
	{MULADD, 0x77A53E07, 0x6A5F},
 
	{BITFLD, (long)table_95, 0},
 
	{LOOKUP, (long)table_96, 0},
 
	{LOOKUP, (long)table_97, 0},
 
	{XOR, 0x43A73788, 0},
 
	{LOOKUP, (long)table_98, 0},
 
	{BITFLD, (long)table_99, 0},
 
	{LOOKUP, (long)table_100, 0},
 
	{XOR, 0x55F4606B, 0},
 
	{BITFLD, (long)table_101, 0}},
 
{{BITFLD, (long)table_102, 0},
 
	{MULADD, 0x32CA58E3, 0x04F9},
 
	{XOR, 0x11756B30, 0},
 
	{MULADD, 0x218B2569, 0x5DB1},
 
	{XOR, 0x77D64B90, 0},
 
	{BITFLD, (long)table_103, 0},
 
	{LOOKUP, (long)table_104, 0},
 
	{MULADD, 0x7D1428CB, 0x3D},
 
	{XOR, 0x6F872C49, 0},
 
	{XOR, 0x2E484655, 0},
 
	{MULADD, 0x1E3349F7, 0x41F5},
 
	{LOOKUP, (long)table_105, 0},
 
	{BITFLD, (long)table_106, 0},
 
	{XOR, 0x61640311, 0},
 
	{BITFLD, (long)table_107, 0},
 
	{LOOKUP, (long)table_108, 0},
 
	{LOOKUP, (long)table_109, 0},
 
	{LOOKUP, (long)table_110, 0},
 
	{XOR, 0x007044D3, 0},
 
	{BITFLD, (long)table_111, 0},
 
	{MULADD, 0x5C221625, 0x576F},
 
	{LOOKUP, (long)table_112, 0},
 
	{LOOKUP, (long)table_113, 0},
 
	{XOR, 0x2D406BB1, 0},
 
	{MULADD, 0x680B1F17, 0x12CD},
 
	{BITFLD, (long)table_114, 0},
 
	{MULADD, 0x12564D55, 0x32B9},
 
	{MULADD, 0x21A67897, 0x6BAB},
 
	{LOOKUP, (long)table_115, 0},
 
	{MULADD, 0x06405119, 0x7143},
 
	{XOR, 0x351D01ED, 0},
 
	{MULADD, 0x46356F6B, 0x0A49},
 
	{MULADD, 0x32C77969, 0x72F3},
 
	{BITFLD, (long)table_116, 0},
 
	{LOOKUP, (long)table_117, 0},
 
	{LOOKUP, (long)table_118, 0},
 
	{BITFLD, (long)table_119, 0},
 
	{LOOKUP, (long)table_120, 0},
 
	{BITFLD, (long)table_121, 0},
 
	{MULADD, 0x74D52C55, 0x5F43},
 
	{XOR, 0x26201CA8, 0},
 
	{XOR, 0x7AEB3255, 0},
 
	{LOOKUP, (long)table_122, 0},
 
	{MULADD, 0x578F1047, 0x640B},
 
	{LOOKUP, (long)table_123, 0},
 
	{LOOKUP, (long)table_124, 0},
 
	{BITFLD, (long)table_125, 0},
 
	{BITFLD, (long)table_126, 0},
 
	{XOR, 0x4A1352CF, 0},
 
	{MULADD, 0x4BFB6EF3, 0x704F},
 
	{MULADD, 0x1B4C7FE7, 0x5637},
 
	{MULADD, 0x04091A3B, 0x4917},
 
	{XOR, 0x270C2F52, 0},
 
	{LOOKUP, (long)table_127, 0},
 
	{BITFLD, (long)table_128, 0},
 
	{LOOKUP, (long)table_129, 0},
 
	{BITFLD, (long)table_130, 0},
 
	{MULADD, 0x127549D5, 0x579B},
 
	{MULADD, 0x0AB54121, 0x7A47},
 
	{BITFLD, (long)table_131, 0},
 
	{XOR, 0x751E6E49, 0},
 
	{LOOKUP, (long)table_132, 0},
 
	{LOOKUP, (long)table_133, 0},
 
	{XOR, 0x670C3F74, 0},
 
	{MULADD, 0x6B080851, 0x7E8B},
 
	{XOR, 0x71CD789E, 0},
 
	{XOR, 0x3EB20B7B, 0},
 
	{BITFLD, (long)table_134, 0},
 
	{LOOKUP, (long)table_135, 0},
 
	{MULADD, 0x58A67753, 0x272B},
 
	{MULADD, 0x1AB54AD7, 0x4D33},
 
	{MULADD, 0x07D30A45, 0x0569},
 
	{MULADD, 0x737616BF, 0x70C7},
 
	{LOOKUP, (long)table_136, 0},
 
	{MULADD, 0x45C4485D, 0x2063},
 
	{BITFLD, (long)table_137, 0},
 
	{XOR, 0x2598043D, 0},
 
	{MULADD, 0x223A4FE3, 0x49A7},
 
	{XOR, 0x1EED619F, 0},
 
	{BITFLD, (long)table_138, 0},
 
	{XOR, 0x6F477561, 0},
 
	{BITFLD, (long)table_139, 0},
 
	{BITFLD, (long)table_140, 0},
 
	{LOOKUP, (long)table_141, 0},
 
	{MULADD, 0x4BC13C4F, 0x45C1},
 
	{XOR, 0x3B547BFB, 0},
 
	{LOOKUP, (long)table_142, 0},
 
	{MULADD, 0x71406AB3, 0x7A5F},
 
	{XOR, 0x2F1467E9, 0},
 
	{MULADD, 0x009366D1, 0x22D1},
 
	{MULADD, 0x587D1B75, 0x2CA5},
 
	{MULADD, 0x213A4BE7, 0x4499},
 
	{MULADD, 0x62653E89, 0x2D5D},
 
	{BITFLD, (long)table_143, 0},
 
	{MULADD, 0x4F5F3257, 0x444F},
 
	{MULADD, 0x4C0E2B2B, 0x19D3}},
 
{{MULADD, 0x3F867B35, 0x7B3B},
 
	{MULADD, 0x32D25CB1, 0x3D6D},
 
	{BITFLD, (long)table_144, 0},
 
	{MULADD, 0x50FA1C51, 0x5F4F},
 
	{LOOKUP, (long)table_145, 0},
 
	{XOR, 0x05FE7AF1, 0},
 
	{MULADD, 0x14067C29, 0x10C5},
 
	{LOOKUP, (long)table_146, 0},
 
	{MULADD, 0x4A5558C5, 0x271F},
 
	{XOR, 0x3C0861B1, 0},
 
	{BITFLD, (long)table_147, 0},
 
	{LOOKUP, (long)table_148, 0},
 
	{MULADD, 0x18837C9D, 0x6335},
 
	{BITFLD, (long)table_149, 0},
 
	{XOR, 0x7DAB5033, 0},
 
	{LOOKUP, (long)table_150, 0},
 
	{MULADD, 0x03B87321, 0x7225},
 
	{XOR, 0x7F906745, 0},
 
	{LOOKUP, (long)table_151, 0},
 
	{BITFLD, (long)table_152, 0},
 
	{XOR, 0x21C46C2C, 0},
 
	{MULADD, 0x2B36757D, 0x028D},
 
	{BITFLD, (long)table_153, 0},
 
	{LOOKUP, (long)table_154, 0},
 
	{XOR, 0x106B4A85, 0},
 
	{XOR, 0x17640F11, 0},
 
	{LOOKUP, (long)table_155, 0},
 
	{XOR, 0x69E60486, 0},
 
	{LOOKUP, (long)table_156, 0},
 
	{MULADD, 0x3782017D, 0x05BF},
 
	{BITFLD, (long)table_157, 0},
 
	{LOOKUP, (long)table_158, 0},
 
	{XOR, 0x6BCA53B0, 0},
 
	{LOOKUP, (long)table_159, 0},
 
	{LOOKUP, (long)table_160, 0},
 
	{LOOKUP, (long)table_161, 0},
 
	{LOOKUP, (long)table_162, 0},
 
	{XOR, 0x0B8236E3, 0},
 
	{BITFLD, (long)table_163, 0},
 
	{MULADD, 0x5EE51C43, 0x4553},
 
	{BITFLD, (long)table_164, 0},
 
	{LOOKUP, (long)table_165, 0},
 
	{LOOKUP, (long)table_166, 0},
 
	{LOOKUP, (long)table_167, 0},
 
	{MULADD, 0x42B14C6F, 0x5531},
 
	{XOR, 0x4A2548E8, 0},
 
	{MULADD, 0x5C071D85, 0x2437},
 
	{LOOKUP, (long)table_168, 0},
 
	{MULADD, 0x29195861, 0x108B},
 
	{XOR, 0x24012258, 0},
 
	{LOOKUP, (long)table_169, 0},
 
	{XOR, 0x63CC2377, 0},
 
	{XOR, 0x08D04B59, 0},
 
	{MULADD, 0x3FD30CF5, 0x7027},
 
	{XOR, 0x7C3E0478, 0},
 
	{MULADD, 0x457776B7, 0x24B3},
 
	{XOR, 0x086652BC, 0},
 
	{MULADD, 0x302F5B13, 0x371D},
 
	{LOOKUP, (long)table_170, 0},
 
	{MULADD, 0x58692D47, 0x0671},
 
	{XOR, 0x6601178E, 0},
 
	{MULADD, 0x0F195B9B, 0x1369},
 
	{XOR, 0x07BA21D8, 0},
 
	{BITFLD, (long)table_171, 0},
 
	{BITFLD, (long)table_172, 0},
 
	{XOR, 0x13AC3D21, 0},
 
	{MULADD, 0x5BCF3275, 0x6E1B},
 
	{MULADD, 0x62725C5B, 0x16B9},
 
	{MULADD, 0x5B950FDF, 0x2D35},
 
	{BITFLD, (long)table_173, 0},
 
	{BITFLD, (long)table_174, 0},
 
	{MULADD, 0x73BA5335, 0x1C13},
 
	{BITFLD, (long)table_175, 0},
 
	{BITFLD, (long)table_176, 0},
 
	{XOR, 0x3E144154, 0},
 
	{MULADD, 0x4EED7B27, 0x38AB},
 
	{LOOKUP, (long)table_177, 0},
 
	{MULADD, 0x627C7E0F, 0x7F01},
 
	{MULADD, 0x5D7E1F73, 0x2C0F},
 
	{LOOKUP, (long)table_178, 0},
 
	{MULADD, 0x55C9525F, 0x4659},
 
	{XOR, 0x3765334C, 0},
 
	{MULADD, 0x5DF66DDF, 0x7C25},
 
	{LOOKUP, (long)table_179, 0},
 
	{LOOKUP, (long)table_180, 0},
 
	{XOR, 0x16AE5776, 0},
 
	{LOOKUP, (long)table_181, 0},
 
	{LOOKUP, (long)table_182, 0},
 
	{BITFLD, (long)table_183, 0},
 
	{BITFLD, (long)table_184, 0},
 
	{LOOKUP, (long)table_185, 0},
 
	{MULADD, 0x4392327B, 0x7E0D},
 
	{LOOKUP, (long)table_186, 0},
 
	{MULADD, 0x3D8B0CB5, 0x640D},
 
	{MULADD, 0x32865601, 0x4D43},
 
	{BITFLD, (long)table_187, 0}}
 
};
 

	
 
#define A( x ) (( x ) & 0xFF )
 
#define B( x ) (( x ) >> 8 & 0xFF )
 
#define C( x ) (( x ) >> 16 & 0xFF )
 
#define D( x ) (( x ) >> 24 & 0xFF )
 

	
 
int yahoo_xfrm(int table, int depth, int seed)
 
{
 
	struct yahoo_fn *xfrm;
 
	int i, j, z;
 
	unsigned int n = seed;
 
	unsigned char *arg;
 

	
 
	for (i = 0; i < depth; i++) {
 
		xfrm = &yahoo_fntable[table][n % 96];
 
		switch (xfrm->type) {
 
		case IDENT:
 
			return seed;
 
		case XOR:
 
			seed ^= xfrm->arg1;
 
			break;
 
		case MULADD:
 
			seed = seed * xfrm->arg1 + xfrm->arg2;
 
			break;
 
		case LOOKUP:
 
			arg = (unsigned char *)xfrm->arg1;
 
			seed = arg[A(seed)] | arg[B(seed)] << 8 | arg[C(seed)]
 
				<< 16 | arg[D(seed)] << 24;
 
			break;
 
		case BITFLD:
 
			arg = (unsigned char *)xfrm->arg1;
 
			for (j = 0, z = 0; j < 32; j++)
 
				z = (((seed >> j) & 1) << arg[j]) | (~(1 <<
 
						arg[j]) & z);
 
			seed = z;
 
			break;
 
		}
 
		if (depth - i == 1)
 
			return seed;
 
		z = ((((((A(seed) * 0x9E3779B1) ^ B(seed)) * 0x9E3779B1)
 
					^ C(seed)) * 0x9E3779B1) ^ D(seed)) *
 
			0x9E3779B1;
 
		n = ((((z ^ (z >> 8)) >> 16) ^ z) ^ (z >> 8)) & 0xFF;
 
		seed *= 0x00010DCD;
 
	}
 
	return seed;
 
}
backends/libyahoo2/yahoo/yahoo_fn.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2 - originally from gaim patches by Amatus
 
 *
 
 * Copyright (C) 2003-2004
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 */
 

	
 
#define IDENT  1		/* identify function */
 
#define XOR    2		/* xor with arg1 */
 
#define MULADD 3		/* multipy by arg1 then add arg2 */
 
#define LOOKUP 4		/* lookup each byte in the table pointed to by arg1 */
 
#define BITFLD 5		/* reorder bits according to table pointed to by arg1 */
 

	
 
struct yahoo_fn {
 
	int type;
 
	long arg1, arg2;
 
};
 

	
 
int yahoo_xfrm(int table, int depth, int seed);
backends/libyahoo2/yahoo/yahoo_httplib.c
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo_httplib.c
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#if HAVE_CONFIG_H
 
# include <config.h>
 
#endif
 

	
 
#include <stdio.h>
 
#include <stdlib.h>
 

	
 
#if STDC_HEADERS
 
# include <string.h>
 
#else
 
# if !HAVE_STRCHR
 
#  define strchr index
 
#  define strrchr rindex
 
# endif
 
char *strchr(), *strrchr();
 
# if !HAVE_MEMCPY
 
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
 
#  define memmove(d, s, n) bcopy ((s), (d), (n))
 
# endif
 
#endif
 

	
 
#include <errno.h>
 
#if HAVE_UNISTD_H
 
#include <unistd.h>
 
#endif
 
#include <ctype.h>
 
#include "yahoo2.h"
 
#include "yahoo2_callbacks.h"
 
#include "yahoo_httplib.h"
 
#include "yahoo_util.h"
 

	
 
#include "yahoo_debug.h"
 
#ifdef __MINGW32__
 
# include <winsock2.h>
 
# define snprintf _snprintf
 
#endif
 

	
 
extern struct yahoo_callbacks *yc;
 
#define YAHOO_CALLBACK(x)	yc->x
 

	
 
extern enum yahoo_log_level log_level;
 

	
 
int yahoo_tcp_readline(char *ptr, int maxlen, void *fd)
 
{
 
	int n, rc;
 
	char c;
 

	
 
	for (n = 1; n < maxlen; n++) {
 

	
 
		do {
 
			rc = YAHOO_CALLBACK(ext_yahoo_read) (fd, &c, 1);
 
		} while (rc == -1 && (errno == EINTR || errno == EAGAIN));	/* this is bad - it should be done asynchronously */
 

	
 
		if (rc == 1) {
 
			if (c == '\r')	/* get rid of \r */
 
				continue;
 
			*ptr = c;
 
			if (c == '\n')
 
				break;
 
			ptr++;
 
		} else if (rc == 0) {
 
			if (n == 1)
 
				return (0);	/* EOF, no data */
 
			else
 
				break;	/* EOF, w/ data */
 
		} else {
 
			return -1;
 
		}
 
	}
 

	
 
	*ptr = 0;
 
	return (n);
 
}
 

	
 
static int url_to_host_port_path(const char *url,
 
	char *host, int *port, char *path, int *ssl)
 
{
 
	char *urlcopy = NULL;
 
	char *slash = NULL;
 
	char *colon = NULL;
 

	
 
	/*
 
	 * http://hostname
 
	 * http://hostname/
 
	 * http://hostname/path
 
	 * http://hostname/path:foo
 
	 * http://hostname:port
 
	 * http://hostname:port/
 
	 * http://hostname:port/path
 
	 * http://hostname:port/path:foo
 
	 * and https:// variants of the above
 
	 */
 

	
 
	if (strstr(url, "http://") == url) {
 
		urlcopy = strdup(url + 7);
 
	} else if (strstr(url, "https://") == url) {
 
		urlcopy = strdup(url + 8);
 
		*ssl = 1;
 
	} else {
 
		WARNING(("Weird url - unknown protocol: %s", url));
 
		return 0;
 
	}
 

	
 
	slash = strchr(urlcopy, '/');
 
	colon = strchr(urlcopy, ':');
 

	
 
	if (!colon || (slash && slash < colon)) {
 
		if (*ssl)
 
			*port = 443;
 
		else
 
			*port = 80;
 
	} else {
 
		*colon = 0;
 
		*port = atoi(colon + 1);
 
	}
 

	
 
	if (!slash) {
 
		strcpy(path, "/");
 
	} else {
 
		strcpy(path, slash);
 
		*slash = 0;
 
	}
 

	
 
	strcpy(host, urlcopy);
 

	
 
	FREE(urlcopy);
 

	
 
	return 1;
 
}
 

	
 
static int isurlchar(unsigned char c)
 
{
 
	return (isalnum(c));
 
}
 

	
 
char *yahoo_urlencode(const char *instr)
 
{
 
	int ipos = 0, bpos = 0;
 
	char *str = NULL;
 
	int len = strlen(instr);
 

	
 
	if (!(str = y_new(char, 3 *len + 1)))
 
		 return "";
 

	
 
	while (instr[ipos]) {
 
		while (isurlchar(instr[ipos]))
 
			str[bpos++] = instr[ipos++];
 
		if (!instr[ipos])
 
			break;
 

	
 
		snprintf(&str[bpos], 4, "%%%02x", instr[ipos] & 0xff);
 
		bpos += 3;
 
		ipos++;
 
	}
 
	str[bpos] = '\0';
 

	
 
	/* free extra alloc'ed mem. */
 
	len = strlen(str);
 
	str = y_renew(char, str, len + 1);
 

	
 
	return (str);
 
}
 

	
 
char *yahoo_urldecode(const char *instr)
 
{
 
	int ipos = 0, bpos = 0;
 
	char *str = NULL;
 
	char entity[3] = { 0, 0, 0 };
 
	unsigned dec;
 
	int len = strlen(instr);
 

	
 
	if (!(str = y_new(char, len + 1)))
 
		 return "";
 

	
 
	while (instr[ipos]) {
 
		while (instr[ipos] && instr[ipos] != '%')
 
			if (instr[ipos] == '+') {
 
				str[bpos++] = ' ';
 
				ipos++;
 
			} else
 
				str[bpos++] = instr[ipos++];
 
		if (!instr[ipos])
 
			break;
 

	
 
		if (instr[ipos + 1] && instr[ipos + 2]) {
 
			ipos++;
 
			entity[0] = instr[ipos++];
 
			entity[1] = instr[ipos++];
 
			sscanf(entity, "%2x", &dec);
 
			str[bpos++] = (char)dec;
 
		} else {
 
			str[bpos++] = instr[ipos++];
 
		}
 
	}
 
	str[bpos] = '\0';
 

	
 
	/* free extra alloc'ed mem. */
 
	len = strlen(str);
 
	str = y_renew(char, str, len + 1);
 

	
 
	return (str);
 
}
 

	
 
char *yahoo_xmldecode(const char *instr)
 
{
 
	int ipos = 0, bpos = 0, epos = 0;
 
	char *str = NULL;
 
	char entity[4] = { 0, 0, 0, 0 };
 
	char *entitymap[5][2] = {
 
		{"amp;", "&"},
 
		{"quot;", "\""},
 
		{"lt;", "<"},
 
		{"gt;", "<"},
 
		{"nbsp;", " "}
 
	};
 
	unsigned dec;
 
	int len = strlen(instr);
 

	
 
	if (!(str = y_new(char, len + 1)))
 
		 return "";
 

	
 
	while (instr[ipos]) {
 
		while (instr[ipos] && instr[ipos] != '&')
 
			if (instr[ipos] == '+') {
 
				str[bpos++] = ' ';
 
				ipos++;
 
			} else
 
				str[bpos++] = instr[ipos++];
 
		if (!instr[ipos] || !instr[ipos + 1])
 
			break;
 
		ipos++;
 

	
 
		if (instr[ipos] == '#') {
 
			ipos++;
 
			epos = 0;
 
			while (instr[ipos] != ';')
 
				entity[epos++] = instr[ipos++];
 
			sscanf(entity, "%u", &dec);
 
			str[bpos++] = (char)dec;
 
			ipos++;
 
		} else {
 
			int i;
 
			for (i = 0; i < 5; i++)
 
				if (!strncmp(instr + ipos, entitymap[i][0],
 
						strlen(entitymap[i][0]))) {
 
					str[bpos++] = entitymap[i][1][0];
 
					ipos += strlen(entitymap[i][0]);
 
					break;
 
				}
 
		}
 
	}
 
	str[bpos] = '\0';
 

	
 
	/* free extra alloc'ed mem. */
 
	len = strlen(str);
 
	str = y_renew(char, str, len + 1);
 

	
 
	return (str);
 
}
 

	
 
typedef void (*http_connected) (int id, void *fd, int error);
 

	
 
struct callback_data {
 
	int id;
 
	yahoo_get_fd_callback callback;
 
	char *request;
 
	void *user_data;
 
};
 

	
 
static void connect_complete(void *fd, int error, void *data)
 
{
 
	struct callback_data *ccd = data;
 
	if (error == 0)
 
		YAHOO_CALLBACK(ext_yahoo_write) (fd, ccd->request,
 
			strlen(ccd->request));
 
	free(ccd->request);
 
	ccd->callback(ccd->id, fd, error, ccd->user_data);
 
	FREE(ccd);
 
}
 

	
 
static void yahoo_send_http_request(int id, char *host, int port, char *request,
 
	yahoo_get_fd_callback callback, void *data, int use_ssl)
 
{
 
	struct callback_data *ccd = y_new0(struct callback_data, 1);
 
	ccd->callback = callback;
 
	ccd->id = id;
 
	ccd->request = strdup(request);
 
	ccd->user_data = data;
 

	
 
	YAHOO_CALLBACK(ext_yahoo_connect_async) (id, host, port,
 
		connect_complete, ccd, use_ssl);
 
}
 

	
 
void yahoo_http_post(int id, const char *url, const char *cookies,
 
	long content_length, yahoo_get_fd_callback callback, void *data)
 
{
 
	char host[255];
 
	int port = 80;
 
	char path[255];
 
	char buff[1024];
 
	int ssl = 0;
 

	
 
	if (!url_to_host_port_path(url, host, &port, path, &ssl))
 
		return;
 

	
 
	/* thanks to kopete dumpcap */
 
	snprintf(buff, sizeof(buff),
 
		"POST %s HTTP/1.1\r\n"
 
		"Cookie: %s\r\n"
 
		"User-Agent: Mozilla/5.0\r\n"
 
		"Host: %s\r\n"
 
		"Content-Length: %ld\r\n"
 
		"Cache-Control: no-cache\r\n"
 
		"\r\n", path, cookies, host, content_length);
 

	
 
	yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
 
}
 

	
 
void yahoo_http_get(int id, const char *url, const char *cookies, int http11,
 
	int keepalive, yahoo_get_fd_callback callback, void *data)
 
{
 
	char host[255];
 
	int port = 80;
 
	char path[255];
 
	char buff[2048];
 
	char cookiebuff[1024];
 
	int ssl = 0;
 

	
 
	if (!url_to_host_port_path(url, host, &port, path, &ssl))
 
		return;
 

	
 
	/* Allow cases when we don't need to send a cookie */
 
	if (cookies)
 
		snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n",
 
			cookies);
 
	else
 
		cookiebuff[0] = '\0';
 

	
 
	snprintf(buff, sizeof(buff),
 
		"GET %s HTTP/1.%s\r\n"
 
		"%sHost: %s\r\n"
 
		"User-Agent: Mozilla/4.5 [en] (" "1" "/" "1" ")\r\n"
 
		"Accept: */*\r\n"
 
		"%s" "\r\n", path, http11?"1":"0", cookiebuff, host,
 
		keepalive? "Connection: Keep-Alive\r\n":"Connection: close\r\n");
 

	
 
	yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
 
}
 

	
 
void yahoo_http_head(int id, const char *url, const char *cookies, int len,
 
	char *payload, yahoo_get_fd_callback callback, void *data)
 
{
 
	char host[255];
 
	int port = 80;
 
	char path[255];
 
	char buff[2048];
 
	char cookiebuff[1024];
 
	int ssl = 0;
 

	
 
	if (!url_to_host_port_path(url, host, &port, path, &ssl))
 
		return;
 

	
 
	/* Allow cases when we don't need to send a cookie */
 
	if (cookies)
 
		snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n",
 
			cookies);
 
	else
 
		cookiebuff[0] = '\0';
 

	
 
	snprintf(buff, sizeof(buff),
 
		"HEAD %s HTTP/1.0\r\n"
 
		"Accept: */*\r\n"
 
		"Host: %s:%d\r\n"
 
		"User-Agent: Mozilla/4.5 [en] (" "1" "/" "1" ")\r\n"
 
		"%s"
 
		"Content-Length: %d\r\n"
 
		"Cache-Control: no-cache\r\n"
 
		"\r\n%s", path, host, port, cookiebuff, len,
 
		payload?payload:"");
 

	
 
	yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
 
}
 

	
backends/libyahoo2/yahoo/yahoo_httplib.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo_httplib.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#ifndef YAHOO_HTTPLIB_H
 
#define YAHOO_HTTPLIB_H
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
#include "yahoo2_types.h"
 

	
 
	char *yahoo_urlencode(const char *instr);
 
	char *yahoo_urldecode(const char *instr);
 
	char *yahoo_xmldecode(const char *instr);
 

	
 
	int yahoo_tcp_readline(char *ptr, int maxlen, void *fd);
 
	void yahoo_http_post(int id, const char *url, const char *cookies,
 
		long size, yahoo_get_fd_callback callback, void *data);
 
	void yahoo_http_get(int id, const char *url, const char *cookies,
 
		int http11, int keepalive, yahoo_get_fd_callback callback,
 
		void *data);
 
	void yahoo_http_head(int id, const char *url, const char *cookies,
 
		int size, char *payload, yahoo_get_fd_callback callback,
 
		void *data);
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 
#endif
backends/libyahoo2/yahoo/yahoo_list.c
Show inline comments
 
new file 100644
 
/*
 
 * yahoo_list.c: linked list routines
 
 *
 
 * Some code copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 * Other code copyright Meredydd Luff <meredydd AT everybuddy.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 * Some of this code was borrowed from elist.c in the eb-lite sources
 
 *
 
 */
 

	
 
#include <stdlib.h>
 

	
 
#include "yahoo_list.h"
 

	
 
YList *y_list_append(YList *list, void *data)
 
{
 
	YList *n;
 
	YList *new_list = malloc(sizeof(YList));
 
	YList *attach_to = NULL;
 

	
 
	new_list->next = NULL;
 
	new_list->data = data;
 

	
 
	for (n = list; n != NULL; n = n->next) {
 
		attach_to = n;
 
	}
 

	
 
	if (attach_to == NULL) {
 
		new_list->prev = NULL;
 
		return new_list;
 
	} else {
 
		new_list->prev = attach_to;
 
		attach_to->next = new_list;
 
		return list;
 
	}
 
}
 

	
 
YList *y_list_prepend(YList *list, void *data)
 
{
 
	YList *n = malloc(sizeof(YList));
 

	
 
	n->next = list;
 
	n->prev = NULL;
 
	n->data = data;
 
	if (list)
 
		list->prev = n;
 

	
 
	return n;
 
}
 

	
 
YList *y_list_concat(YList *list, YList *add)
 
{
 
	YList *l;
 

	
 
	if (!list)
 
		return add;
 

	
 
	if (!add)
 
		return list;
 

	
 
	for (l = list; l->next; l = l->next) ;
 

	
 
	l->next = add;
 
	add->prev = l;
 

	
 
	return list;
 
}
 

	
 
YList *y_list_remove(YList *list, void *data)
 
{
 
	YList *n;
 

	
 
	for (n = list; n != NULL; n = n->next) {
 
		if (n->data == data) {
 
			list = y_list_remove_link(list, n);
 
			y_list_free_1(n);
 
			break;
 
		}
 
	}
 

	
 
	return list;
 
}
 

	
 
/* Warning */
 
/* link MUST be part of list */
 
/* caller must free link using y_list_free_1 */
 
YList *y_list_remove_link(YList *list, const YList *link)
 
{
 
	if (!link)
 
		return list;
 

	
 
	if (link->next)
 
		link->next->prev = link->prev;
 
	if (link->prev)
 
		link->prev->next = link->next;
 

	
 
	if (link == list)
 
		list = link->next;
 

	
 
	return list;
 
}
 

	
 
int y_list_length(const YList *list)
 
{
 
	int retval = 0;
 
	const YList *n = list;
 

	
 
	for (n = list; n != NULL; n = n->next) {
 
		retval++;
 
	}
 

	
 
	return retval;
 
}
 

	
 
/* well, you could just check for list == NULL, but that would be
 
 * implementation dependent
 
 */
 
int y_list_empty(const YList *list)
 
{
 
	if (!list)
 
		return 1;
 
	else
 
		return 0;
 
}
 

	
 
int y_list_singleton(const YList *list)
 
{
 
	if (!list || list->next)
 
		return 0;
 
	return 1;
 
}
 

	
 
YList *y_list_copy(YList *list)
 
{
 
	YList *n;
 
	YList *copy = NULL;
 

	
 
	for (n = list; n != NULL; n = n->next) {
 
		copy = y_list_append(copy, n->data);
 
	}
 

	
 
	return copy;
 
}
 

	
 
void y_list_free_1(YList *list)
 
{
 
	free(list);
 
}
 

	
 
void y_list_free(YList *list)
 
{
 
	YList *n = list;
 

	
 
	while (n != NULL) {
 
		YList *next = n->next;
 
		free(n);
 
		n = next;
 
	}
 
}
 

	
 
YList *y_list_find(YList *list, const void *data)
 
{
 
	YList *l;
 
	for (l = list; l && l->data != data; l = l->next) ;
 

	
 
	return l;
 
}
 

	
 
void y_list_foreach(YList *list, YListFunc fn, void *user_data)
 
{
 
	for (; list; list = list->next)
 
		fn(list->data, user_data);
 
}
 

	
 
YList *y_list_find_custom(YList *list, const void *data, YListCompFunc comp)
 
{
 
	YList *l;
 
	for (l = list; l; l = l->next)
 
		if (comp(l->data, data) == 0)
 
			return l;
 

	
 
	return NULL;
 
}
 

	
 
YList *y_list_nth(YList *list, int n)
 
{
 
	int i = n;
 
	for (; list && i; list = list->next, i--) ;
 

	
 
	return list;
 
}
 

	
 
YList *y_list_insert_sorted(YList *list, void *data, YListCompFunc comp)
 
{
 
	YList *l, *n, *prev = NULL;
 
	if (!list)
 
		return y_list_append(list, data);
 

	
 
	n = malloc(sizeof(YList));
 
	n->data = data;
 
	for (l = list; l && comp(l->data, n->data) <= 0; l = l->next)
 
		prev = l;
 

	
 
	if (l) {
 
		n->prev = l->prev;
 
		l->prev = n;
 
	} else
 
		n->prev = prev;
 

	
 
	n->next = l;
 

	
 
	if (n->prev) {
 
		n->prev->next = n;
 
		return list;
 
	} else {
 
		return n;
 
	}
 

	
 
}
backends/libyahoo2/yahoo/yahoo_list.h
Show inline comments
 
new file 100644
 
/*
 
 * yahoo_list.h: linked list routines
 
 *
 
 * Some code copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 * Other code copyright Meredydd Luff <meredydd AT everybuddy.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
/*
 
 * This is a replacement for the GList.  It only provides functions that 
 
 * we use in Ayttm.  Thanks to Meredyyd from everybuddy dev for doing 
 
 * most of it.
 
 */
 

	
 
#ifndef __YLIST_H__
 
#define __YLIST_H__
 

	
 
#ifdef __cplusplus
 
extern "C" {
 
#endif
 

	
 
	typedef struct _YList {
 
		struct _YList *next;
 
		struct _YList *prev;
 
		void *data;
 
	} YList;
 

	
 
	typedef int (*YListCompFunc) (const void *, const void *);
 
	typedef void (*YListFunc) (void *, void *);
 

	
 
	YList *y_list_append(YList *list, void *data);
 
	YList *y_list_prepend(YList *list, void *data);
 
	YList *y_list_remove_link(YList *list, const YList *link);
 
	YList *y_list_remove(YList *list, void *data);
 

	
 
	YList *y_list_insert_sorted(YList *list, void *data,
 
		YListCompFunc comp);
 

	
 
	YList *y_list_copy(YList *list);
 

	
 
	YList *y_list_concat(YList *list, YList *add);
 

	
 
	YList *y_list_find(YList *list, const void *data);
 
	YList *y_list_find_custom(YList *list, const void *data,
 
		YListCompFunc comp);
 

	
 
	YList *y_list_nth(YList *list, int n);
 

	
 
	void y_list_foreach(YList *list, YListFunc fn, void *user_data);
 

	
 
	void y_list_free_1(YList *list);
 
	void y_list_free(YList *list);
 
	int y_list_length(const YList *list);
 
	int y_list_empty(const YList *list);
 
	int y_list_singleton(const YList *list);
 

	
 
#define y_list_next(list)	list->next
 

	
 
#ifdef __cplusplus
 
}
 
#endif
 
#endif
backends/libyahoo2/yahoo/yahoo_util.c
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo_util.c
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#if HAVE_CONFIG_H
 
# include <config.h>
 
#endif
 

	
 
#if STDC_HEADERS
 
# include <string.h>
 
#else
 
# if !HAVE_STRCHR
 
#  define strchr index
 
#  define strrchr rindex
 
# endif
 
char *strchr(), *strrchr();
 
# if !HAVE_MEMCPY
 
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
 
#  define memmove(d, s, n) bcopy ((s), (d), (n))
 
# endif
 
#endif
 

	
 
#include "yahoo_util.h"
 

	
 
char *y_string_append(char *string, char *append)
 
{
 
	int size = strlen(string) + strlen(append) + 1;
 
	char *new_string = y_renew(char, string, size);
 

	
 
	if (new_string == NULL) {
 
		new_string = y_new(char, size);
 
		strcpy(new_string, string);
 
		FREE(string);
 
	}
 

	
 
	strcat(new_string, append);
 

	
 
	return new_string;
 
}
 

	
 
char *y_str_to_utf8(const char *in)
 
{
 
	unsigned int n, i = 0;
 
	char *result = NULL;
 

	
 
	if (in == NULL || *in == '\0')
 
		return strdup("");
 

	
 
	result = y_new(char, strlen(in) * 2 + 1);
 

	
 
	/* convert a string to UTF-8 Format */
 
	for (n = 0; n < strlen(in); n++) {
 
		unsigned char c = (unsigned char)in[n];
 

	
 
		if (c < 128) {
 
			result[i++] = (char)c;
 
		} else {
 
			result[i++] = (char)((c >> 6) | 192);
 
			result[i++] = (char)((c & 63) | 128);
 
		}
 
	}
 
	result[i] = '\0';
 
	return result;
 
}
 

	
 
char *y_utf8_to_str(const char *in)
 
{
 
	int i = 0;
 
	unsigned int n;
 
	char *result = NULL;
 

	
 
	if (in == NULL || *in == '\0')
 
		return strdup("");
 

	
 
	result = y_new(char, strlen(in) + 1);
 

	
 
	/* convert a string from UTF-8 Format */
 
	for (n = 0; n < strlen(in); n++) {
 
		unsigned char c = in[n];
 

	
 
		if (c < 128) {
 
			result[i++] = (char)c;
 
		} else {
 
			result[i++] = (c << 6) | (in[++n] & 63);
 
		}
 
	}
 
	result[i] = '\0';
 
	return result;
 
}
 

	
 
#if !HAVE_GLIB
 

	
 
void y_strfreev(char **vector)
 
{
 
	char **v;
 
	for (v = vector; *v; v++) {
 
		FREE(*v);
 
	}
 
	FREE(vector);
 
}
 

	
 
char **y_strsplit(char *str, char *sep, int nelem)
 
{
 
	char **vector;
 
	char *s, *p;
 
	int i = 0;
 
	int l = strlen(sep);
 
	if (nelem <= 0) {
 
		char *s;
 
		nelem = 0;
 
		if (*str) {
 
			for (s = strstr(str, sep); s;
 
				s = strstr(s + l, sep), nelem++) ;
 
			if (strcmp(str + strlen(str) - l, sep))
 
				nelem++;
 
		}
 
	}
 

	
 
	vector = y_new(char *, nelem + 1);
 

	
 
	for (p = str, s = strstr(p, sep); i < nelem && s;
 
		p = s + l, s = strstr(p, sep), i++) {
 
		int len = s - p;
 
		vector[i] = y_new(char, len + 1);
 
		strncpy(vector[i], p, len);
 
		vector[i][len] = '\0';
 
	}
 

	
 
	if (i < nelem && *str)	/* str didn't end with sep, and str isn't empty */
 
		vector[i++] = strdup(p);
 

	
 
	vector[i] = NULL;
 

	
 
	return vector;
 
}
 

	
 
void *y_memdup(const void *addr, int n)
 
{
 
	void *new_chunk = malloc(n);
 
	if (new_chunk)
 
		memcpy(new_chunk, addr, n);
 
	return new_chunk;
 
}
 

	
 
#endif
backends/libyahoo2/yahoo/yahoo_util.h
Show inline comments
 
new file 100644
 
/*
 
 * libyahoo2: yahoo_util.h
 
 *
 
 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
 
 *
 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 *
 
 */
 

	
 
#ifndef __YAHOO_UTIL_H__
 
#define __YAHOO_UTIL_H__
 

	
 
#if HAVE_CONFIG_H
 
# include <config.h>
 
#endif
 

	
 
#if HAVE_GLIB
 
# include <glib.h>
 

	
 
# define FREE(x)	if(x) {g_free(x); x=NULL;}
 

	
 
# define y_new		g_new
 
# define y_new0		g_new0
 
# define y_renew	g_renew
 

	
 
# define y_memdup	g_memdup
 
# define y_strsplit	g_strsplit
 
# define y_strfreev	g_strfreev
 
# ifndef strdup
 
#  define strdup	g_strdup
 
# endif
 
# ifndef strncasecmp
 
#  define strncasecmp	g_strncasecmp
 
#  define strcasecmp	g_strcasecmp
 
# endif
 

	
 
# define snprintf	g_snprintf
 
# define vsnprintf	g_vsnprintf
 

	
 
#else
 

	
 
# include <stdlib.h>
 
# include <stdarg.h>
 

	
 
# define FREE(x)		if(x) {free(x); x=NULL;}
 

	
 
# define y_new(type, n)		(type *)malloc(sizeof(type) * (n))
 
# define y_new0(type, n)	(type *)calloc((n), sizeof(type))
 
# define y_renew(type, mem, n)	(type *)realloc(mem, n)
 

	
 
void *y_memdup(const void *addr, int n);
 
char **y_strsplit(char *str, char *sep, int nelem);
 
void y_strfreev(char **vector);
 

	
 
#ifndef _WIN32
 
int strncasecmp(const char *s1, const char *s2, size_t n);
 
int strcasecmp(const char *s1, const char *s2);
 

	
 
char *strdup(const char *s);
 

	
 
int snprintf(char *str, size_t size, const char *format, ...);
 
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
 
#endif
 

	
 
#endif
 

	
 
#ifndef TRUE
 
#define TRUE 1
 
#endif
 

	
 
#ifndef FALSE
 
#define FALSE 0
 
#endif
 

	
 
#ifndef MIN
 
#define MIN(x,y) ((x)<(y)?(x):(y))
 
#endif
 

	
 
#ifndef MAX
 
#define MAX(x,y) ((x)>(y)?(x):(y))
 
#endif
 

	
 
/* 
 
 * The following three functions return newly allocated memory.
 
 * You must free it yourself
 
 */
 
char *y_string_append(char *str, char *append);
 
char *y_str_to_utf8(const char *in);
 
char *y_utf8_to_str(const char *in);
 

	
 
#endif
backends/libyahoo2/yahoohandler.h
Show inline comments
 
new file 100644
 
#pragma once
 

	
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 

	
 
// Yahoo2
 
#include <yahoo2.h>
 
#include <yahoo2_callbacks.h>
 
#include <stdio.h>
 
#include <stdarg.h>
 
#include <stdlib.h>
 

	
 
// Swiften
 
#include "Swiften/Swiften.h"
 
#include "Swiften/TLS/OpenSSL/OpenSSLContextFactory.h"
 

	
 
// for signal handler
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 

	
 
// Boost
 
#include <boost/algorithm/string.hpp>
 

	
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
class YahooLocalAccount;
 

	
 
class YahooHandler {
 
	public:
 
		YahooHandler(YahooLocalAccount *account, int conn_tag, int handler_tag, void *data, yahoo_input_condition cond);
 
		virtual ~YahooHandler();
 

	
 
		void ready(std::string *buffer = NULL);
 

	
 
		int handler_tag;
 
		int conn_tag;
 
		void *data;
 
		yahoo_input_condition cond;
 
		bool remove_later;
 
		YahooLocalAccount *account;
 
};
 

	
backends/libyahoo2/yahoolocalaccount.cpp
Show inline comments
 
new file 100644
 

	
 
#include "yahoolocalaccount.h"
 
#include "yahoohandler.h"
 

	
 
YahooLocalAccount::YahooLocalAccount(const std::string &user, const std::string &legacyName, const std::string &password) : user(user), id(0), conn_tag(1), handler_tag(1), status(YAHOO_STATUS_OFFLINE), msg(""), buffer("") {
 
	id = yahoo_init_with_attributes(legacyName.c_str(), password.c_str(), 
 
			"local_host", "",
 
			"pager_port", 5050,
 
			NULL);
 
}
 

	
 
YahooLocalAccount::~YahooLocalAccount() {
 
	// remove handlers
 
	for (std::map<int, YahooHandler *>::iterator it = handlers.begin(); it != handlers.end(); it++) {
 
		delete it->second;
 
	}
 

	
 
	// remove conns
 
	for (std::map<int, boost::shared_ptr<Swift::Connection> >::iterator it = conns.begin(); it != conns.end(); it++) {
 
		it->second->onConnectFinished.disconnect_all_slots();
 
		it->second->onDisconnected.disconnect_all_slots();
 
		it->second->onDataRead.disconnect_all_slots();
 
		it->second->onDataWritten.disconnect_all_slots();
 
	}
 
}
 

	
 
void YahooLocalAccount::login() {
 
	yahoo_login(id, YAHOO_STATUS_AVAILABLE);
 
}
 

	
 
void YahooLocalAccount::addHandler(YahooHandler *handler) {
 
	handlers[handler->handler_tag] = handler;
 
	handlers_per_conn[handler->conn_tag][handler->handler_tag] = handler;
 
}
 

	
 
void YahooLocalAccount::removeOldHandlers() {
 
	std::vector<int> handlers_to_remove;
 
	for (std::map<int, YahooHandler *>::iterator it = handlers.begin(); it != handlers.end(); it++) {
 
		if (it->second->remove_later) {
 
			handlers_to_remove.push_back(it->first);
 
		}
 
	}
 

	
 
	BOOST_FOREACH(int tag, handlers_to_remove) {
 
		YahooHandler *handler = handlers[tag];
 
		handlers.erase(tag);
 
		handlers_per_conn[handler->conn_tag].erase(tag);
 
		delete handler;
 
	}
 
}
 

	
 
void YahooLocalAccount::removeConn(int conn_tag) {
 
	for (std::map<int, YahooHandler *>::iterator it = handlers_per_conn[conn_tag].begin(); it != handlers_per_conn[conn_tag].end(); it++) {
 
		it->second->remove_later = true;
 
	}
 

	
 
	removeOldHandlers();
 

	
 
	conns.erase(conn_tag);
 
}
backends/libyahoo2/yahoolocalaccount.h
Show inline comments
 
new file 100644
 
#pragma once
 

	
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 

	
 
// Yahoo2
 
#include <yahoo2.h>
 
#include <yahoo2_callbacks.h>
 
#include <stdio.h>
 
#include <stdarg.h>
 
#include <stdlib.h>
 

	
 
// Swiften
 
#include "Swiften/Swiften.h"
 
#include "Swiften/TLS/OpenSSL/OpenSSLContextFactory.h"
 

	
 
// for signal handler
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 

	
 
// Boost
 
#include <boost/algorithm/string.hpp>
 

	
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
class YahooHandler;
 

	
 
class YahooLocalAccount {
 
	public:
 
		YahooLocalAccount(const std::string &user, const std::string &legacyName, const std::string &password);
 
		virtual ~YahooLocalAccount();
 

	
 
		void login();
 

	
 
		void addHandler(YahooHandler *handler);
 
		void removeOldHandlers();
 
		void removeConn(int conn_tag);
 

	
 
		std::string user;
 
		int id;
 
		std::map<int, boost::shared_ptr<Swift::Connection> > conns;
 
		int conn_tag;
 
		std::map<int, YahooHandler *> handlers;
 
		std::map<int, std::map<int, YahooHandler *> > handlers_per_conn;
 
		std::map<std::string, std::string> urls;
 
		int handler_tag;
 
		int status;
 
		std::string msg;
 
		std::string buffer;
 
};
backends/smstools3/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/logging.h"
 
#include "transport/networkplugin.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/mysqlbackend.h"
 
#include "transport/pqxxbackend.h"
 
#include "transport/storagebackend.h"
 
#include "Swiften/Swiften.h"
 
#include <boost/filesystem.hpp>
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
#include <fstream>
 
#include <streambuf>
 

	
 
Swift::SimpleEventLoop *loop_;
 

	
 
#include <boost/filesystem.hpp>
 
#include <boost/algorithm/string.hpp>
 

	
 
using namespace boost::filesystem;
 

	
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
DEFINE_LOGGER(logger, "SMSNetworkPlugin");
 

	
 
#define INTERNAL_USER "/sms@backend@internal@user"
 

	
 
class SMSNetworkPlugin;
 
SMSNetworkPlugin * np = NULL;
 
StorageBackend *storageBackend;
 

	
 
class SMSNetworkPlugin : public NetworkPlugin {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
		Swift::Timer::ref m_timer;
 
		int m_internalUser;
 

	
 
		SMSNetworkPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
			m_factories = new Swift::BoostNetworkFactories(loop);
 
			m_conn = m_factories->getConnectionFactory()->createConnection();
 
			m_conn->onDataRead.connect(boost::bind(&SMSNetworkPlugin::_handleDataRead, this, _1));
 
			m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 
// 			m_conn->onConnectFinished.connect(boost::bind(&FrotzNetworkPlugin::_handleConnected, this, _1));
 
// 			m_conn->onDisconnected.connect(boost::bind(&FrotzNetworkPlugin::handleDisconnected, this));
 

	
 
			LOG4CXX_INFO(logger, "Starting the plugin.");
 

	
 
			m_timer = m_factories->getTimerFactory()->createTimer(5000);
 
			m_timer->onTick.connect(boost::bind(&SMSNetworkPlugin::handleSMSDir, this));
 
			m_timer->start();
 

	
 
			// We're reusing our database model here. Buddies of user with JID INTERNAL_USER are there
 
			// to match received GSM messages from number N with the XMPP users who sent message to number N.
 
			// BuddyName = GSM number
 
			// Alias = XMPP user JID to which the messages from this number is sent to.
 
			// TODO: This should be per Modem!!!
 
			UserInfo info;
 
			info.jid = INTERNAL_USER;
 
			info.password = "";
 
			storageBackend->setUser(info);
 
			storageBackend->getUser(INTERNAL_USER, info);
 
			m_internalUser = info.id;
 
		}
 

	
 

	
 
		void handleSMS(const std::string &sms) {
 
			LOG4CXX_INFO(logger, "Handling SMS " << sms << ".")
 
			std::ifstream t(sms.c_str());
 
			std::string str;
 

	
 
			t.seekg(0, std::ios::end);
 
			str.reserve(t.tellg());
 
			t.seekg(0, std::ios::beg);
 

	
 
			str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
 

	
 
			std::string from = "";
 
			std::string msg = "";
 
			while(str.find("\n") != std::string::npos) {
 
				std::string line = str.substr(0, str.find("\n"));
 
				if (line.find("From: ") == 0) {
 
					from = line.substr(strlen("From: "));
 
				}
 
				else if (line.empty()) {
 
					msg = str.substr(1);
 
					break;
 
				}
 
				str = str.substr(str.find("\n") + 1);
 
			}
 

	
 
			std::list<BuddyInfo> roster;
 
			storageBackend->getBuddies(m_internalUser, roster);
 

	
 
			std::string to;
 
			BOOST_FOREACH(BuddyInfo &b, roster) {
 
				if (b.legacyName == from) {
 
					to = b.alias;
 
				}
 
			}
 

	
 
			if (to.empty()) {
 
				LOG4CXX_WARN(logger, "Received SMS from " << from << ", but this number is not associated with any XMPP user.");
 
			}
 

	
 
			LOG4CXX_INFO(logger, "Forwarding SMS from " << from << " to " << to << ".");
 
			handleMessage(to, from, msg);
 
		}
 

	
 
		void handleSMSDir() {
 
			std::string dir = "/var/spool/sms/incoming/";
 
			if (config->getUnregistered().find("backend.incoming_dir") != config->getUnregistered().end()) {
 
				dir = config->getUnregistered().find("backend.incoming_dir")->second;
 
			if (CONFIG_HAS_KEY(config, "backend.incoming_dir")) {
 
				dir = CONFIG_STRING(config, "backend.incoming_dir");
 
			}
 
			LOG4CXX_INFO(logger, "Checking directory " << dir << " for incoming SMS.");
 

	
 
			path p(dir);
 
			directory_iterator end_itr;
 
			for (directory_iterator itr(p); itr != end_itr; ++itr) {
 

	
 
				try {
 
					if (is_regular(itr->path())) {
 
						handleSMS(itr->path().string());
 
						remove(itr->path());
 
					}
 
				}
 
				catch (const filesystem_error& ex) {
 
					LOG4CXX_ERROR(logger, "Error when removing the SMS: " << ex.what() << ".");
 
				}
 
			}
 
			m_timer->start();
 
		}
 

	
 
		void sendSMS(const std::string &to, const std::string &msg) {
 
			// TODO: Probably 
 
			std::string data = "To: " + to + "\n";
 
			data += "\n";
 
			data += msg;
 

	
 
			// generate random string here...
 
			std::string bucket = "abcdefghijklmnopqrstuvwxyz";
 
			std::string uuid;
 
			for (int i = 0; i < 10; i++) {
 
				uuid += bucket[rand() % bucket.size()];
 
			}
 
			std::ofstream myfile;
 
			myfile.open (std::string("/var/spool/sms/outgoing/spectrum." + uuid).c_str());
 
			myfile << data;
 
			myfile.close();
 
		}
 

	
 
		void sendData(const std::string &string) {
 
			m_conn->write(Swift::createSafeByteArray(string));
 
		}
 

	
 
		void _handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) {
 
			std::string d(data->begin(), data->end());
 
			handleDataRead(d);
 
		}
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			UserInfo info;
 
			if (!storageBackend->getUser(user, info)) {
 
				handleDisconnected(user, 0, "Not registered user.");
 
				return;
 
			}
 
			std::list<BuddyInfo> roster;
 
			storageBackend->getBuddies(info.id, roster);
 

	
 
			// Send available presence to every number in the roster.
 
			BOOST_FOREACH(BuddyInfo &b, roster) {
 
				handleBuddyChanged(user, b.legacyName, b.alias, b.groups, pbnetwork::STATUS_ONLINE);
 
			}
 

	
 
			np->handleConnected(user);
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
		}
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "") {
 
			// Remove trailing +, because smstools doesn't use it in "From: " field for received messages.
 
			std::string n = legacyName;
 
			if (n.find("+") == 0) {
 
				n = n.substr(1);
 
			}
 

	
 
			// Create GSM Number - XMPP user pair to match the potential response and send it to the proper JID.
 
			BuddyInfo info;
 
			info.legacyName = n;
 
			info.alias = user;
 
			info.id = -1;
 
			info.subscription = "both";
 
			info.flags = 0;
 
			storageBackend->addBuddy(m_internalUser, info);
 

	
 
			LOG4CXX_INFO(logger, "Sending SMS from " << user << " to " << n << ".");
 
			sendSMS(n, message);
 
		}
 

	
 
		void handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password) {
 
		}
 

	
 
		void handleLeaveRoomRequest(const std::string &user, const std::string &room) {
 
		}
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) {
 
			LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
 
			handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
 
		}
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) {
 

	
 
		}
 

	
 

	
 
	private:
 
		
 
		Config *config;
 
};
 

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

	
 

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

	
 
	if (signal(SIGCHLD, spectrum_sigchld_handler) == SIG_ERR) {
 
		std::cout << "SIGCHLD handler can't be set\n";
 
		return -1;
 
	}
 

	
 
	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 (...)
 
	{
 
		std::cout << desc << "\n";
 
		exit(1);
 
	}
 

	
 

	
 
	if (argc < 5) {
 
		return 1;
 
	}
 

	
 
// 	QStringList channels;
 
// 	for (int i = 3; i < argc; ++i)
 
// 	{
 
// 		channels.append(argv[i]);
 
// 	}
 
// 
 
// 	MyIrcSession session;
 
// 	session.setNick(argv[2]);
 
// 	session.setAutoJoinChannels(channels);
 
// 	session.connectToServer(argv[1], 6667);
 

	
 
	Config config;
 
	if (!config.load(argv[5])) {
 
		std::cerr << "Can't open " << argv[1] << " configuration file.\n";
 
		return 1;
 
	}
 

	
 
	Logging::initBackendLogging(&config);
 

	
 
	std::string error;
 
	StorageBackend *storageBackend = StorageBackend::createBackend(&config, error);
 
	if (storageBackend == NULL) {
 
		if (!error.empty()) {
 
			std::cerr << error << "\n";
 
			return -2;
 
		}
 
	}
 
	else if (!storageBackend->connect()) {
 
		std::cerr << "Can't connect to database. Check the log to find out the reason.\n";
 
		return -1;
 
	}
 

	
 
	Swift::SimpleEventLoop eventLoop;
 
	loop_ = &eventLoop;
 
	np = new SMSNetworkPlugin(&config, &eventLoop, host, port);
 
	loop_->run();
 

	
 
	return 0;
 
}
backends/swiften/main.cpp
Show inline comments
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 

	
 
// Swiften
 
#include "Swiften/Swiften.h"
 

	
 
#ifndef WIN32
 
// for signal handler
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
#endif
 

	
 
// malloc_trim
 
#include "malloc.h"
 

	
 
// Boost
 
#include <boost/algorithm/string.hpp>
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
DEFINE_LOGGER(logger, "Swiften");
 

	
 
// eventloop
 
Swift::SimpleEventLoop *loop_;
 

	
 
// Plugins
 
class SwiftenPlugin;
 
SwiftenPlugin *np = NULL;
 

	
 
class SwiftenPlugin : public NetworkPlugin {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 

	
 
		SwiftenPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
			m_factories = new Swift::BoostNetworkFactories(loop);
 
			m_conn = m_factories->getConnectionFactory()->createConnection();
 
			m_conn->onDataRead.connect(boost::bind(&SwiftenPlugin::_handleDataRead, this, _1));
 
			m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
			LOG4CXX_INFO(logger, "Starting the plugin.");
 
		}
 

	
 
		// NetworkPlugin uses this method to send the data to networkplugin server
 
		void sendData(const std::string &string) {
 
			m_conn->write(Swift::createSafeByteArray(string));
 
		}
 

	
 
		// This method has to call handleDataRead with all received data from network plugin server
 
		void _handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) {
 
			std::string d(data->begin(), data->end());
 
			handleDataRead(d);
 
		}
 

	
 
		void handleSwiftDisconnected(const std::string &user, const boost::optional<Swift::ClientError> &error) {
 
			std::string message = "";
 
			if (error) {
 
				switch(error->getType()) {
 
					case Swift::ClientError::UnknownError: message = ("Unknown Error"); break;
 
					case Swift::ClientError::DomainNameResolveError: message = ("Unable to find server"); break;
 
					case Swift::ClientError::ConnectionError: message = ("Error connecting to server"); break;
 
					case Swift::ClientError::ConnectionReadError: message = ("Error while receiving server data"); break;
 
					case Swift::ClientError::ConnectionWriteError: message = ("Error while sending data to the server"); break;
 
					case Swift::ClientError::XMLError: message = ("Error parsing server data"); break;
 
					case Swift::ClientError::AuthenticationFailedError: message = ("Login/password invalid"); break;
 
					case Swift::ClientError::CompressionFailedError: message = ("Error while compressing stream"); break;
 
					case Swift::ClientError::ServerVerificationFailedError: message = ("Server verification failed"); break;
 
					case Swift::ClientError::NoSupportedAuthMechanismsError: message = ("Authentication mechanisms not supported"); break;
 
					case Swift::ClientError::UnexpectedElementError: message = ("Unexpected response"); break;
 
					case Swift::ClientError::ResourceBindError: message = ("Error binding resource"); break;
 
					case Swift::ClientError::SessionStartError: message = ("Error starting session"); break;
 
					case Swift::ClientError::StreamError: message = ("Stream error"); break;
 
					case Swift::ClientError::TLSError: message = ("Encryption error"); break;
 
					case Swift::ClientError::ClientCertificateLoadError: message = ("Error loading certificate (Invalid password?)"); break;
 
					case Swift::ClientError::ClientCertificateError: message = ("Certificate not authorized"); break;
 

	
 
					case Swift::ClientError::UnknownCertificateError: message = ("Unknown certificate"); break;
 
					case Swift::ClientError::CertificateExpiredError: message = ("Certificate has expired"); break;
 
					case Swift::ClientError::CertificateNotYetValidError: message = ("Certificate is not yet valid"); break;
 
					case Swift::ClientError::CertificateSelfSignedError: message = ("Certificate is self-signed"); break;
 
					case Swift::ClientError::CertificateRejectedError: message = ("Certificate has been rejected"); break;
 
					case Swift::ClientError::CertificateUntrustedError: message = ("Certificate is not trusted"); break;
 
					case Swift::ClientError::InvalidCertificatePurposeError: message = ("Certificate cannot be used for encrypting your connection"); break;
 
					case Swift::ClientError::CertificatePathLengthExceededError: message = ("Certificate path length constraint exceeded"); break;
 
					case Swift::ClientError::InvalidCertificateSignatureError: message = ("Invalid certificate signature"); break;
 
					case Swift::ClientError::InvalidCAError: message = ("Invalid Certificate Authority"); break;
 
					case Swift::ClientError::InvalidServerIdentityError: message = ("Certificate does not match the host identity"); break;
 
				}
 
			}
 
			LOG4CXX_INFO(logger, user << ": Disconnected " << message);
 
			handleDisconnected(user, 3, message);
 

	
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				client->onConnected.disconnect(boost::bind(&SwiftenPlugin::handleSwiftConnected, this, user));
 
				client->onDisconnected.disconnect(boost::bind(&SwiftenPlugin::handleSwiftDisconnected, this, user, _1));
 
				client->onMessageReceived.disconnect(boost::bind(&SwiftenPlugin::handleSwiftMessageReceived, this, user, _1));
 
				m_users.erase(user);
 
			}
 

	
 
#ifndef WIN32
 
			// force returning of memory chunks allocated by libxml2 to kernel
 
			malloc_trim(0);
 
#endif
 
		}
 

	
 
		void handleSwiftConnected(const std::string &user) {
 
			LOG4CXX_INFO(logger, user << ": Connected to XMPP server.");
 
			handleConnected(user);
 
			m_users[user]->requestRoster();
 
			Swift::Presence::ref response = Swift::Presence::create();
 
			response->setFrom(m_users[user]->getJID());
 
			m_users[user]->sendPresence(response);
 
		}
 

	
 
		void handleSwiftRosterReceived(const std::string &user) {
 
			Swift::PresenceOracle *oracle = m_users[user]->getPresenceOracle();
 
			BOOST_FOREACH(const Swift::XMPPRosterItem &item, m_users[user]->getRoster()->getItems()) {
 
				Swift::Presence::ref lastPresence = oracle->getLastPresence(item.getJID());
 
				pbnetwork::StatusType status = lastPresence ? ((pbnetwork::StatusType) lastPresence->getShow()) : pbnetwork::STATUS_NONE;
 
				handleBuddyChanged(user, item.getJID().toBare().toString(),
 
								   item.getName(), item.getGroups(), status);
 
			}
 
		}
 

	
 
		void handleSwiftPresenceChanged(const std::string &user, Swift::Presence::ref presence) {
 
			LOG4CXX_INFO(logger, user << ": " << presence->getFrom().toBare().toString() << " presence changed");
 

	
 
			std::string message = presence->getStatus();
 
			std::string photo = "";
 

	
 
			boost::shared_ptr<Swift::VCardUpdate> update = presence->getPayload<Swift::VCardUpdate>();
 
			if (update) {
 
				photo = update->getPhotoHash();
 
			}
 

	
 
			boost::optional<Swift::XMPPRosterItem> item = m_users[user]->getRoster()->getItem(presence->getFrom());
 
			if (item) {
 
				handleBuddyChanged(user, presence->getFrom().toBare().toString(), item->getName(), item->getGroups(), (pbnetwork::StatusType) presence->getShow(), message, photo);
 
			}
 
			else {
 
				std::vector<std::string> groups;
 
				handleBuddyChanged(user, presence->getFrom().toBare().toString(), presence->getFrom().toBare(), groups, (pbnetwork::StatusType) presence->getShow(), message, photo);
 
			}
 
		}
 

	
 
		void handleSwiftMessageReceived(const std::string &user, Swift::Message::ref message) {
 
			std::string body = message->getBody();
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				handleMessage(user, message->getFrom().toBare().toString(), body, "", "");
 
			}
 
		}
 

	
 
		void handleSwiftVCardReceived(const std::string &user, unsigned int id, Swift::VCard::ref vcard, Swift::ErrorPayload::ref error) {
 
			if (error || !vcard) {
 
				LOG4CXX_INFO(logger, user << ": error fetching VCard with id=" << id);
 
				handleVCard(user, id, "", "", "", "");
 
				return;
 
			}
 
			LOG4CXX_INFO(logger, user << ": VCard fetched - id=" << id);
 
			std::string photo((const char *)&vcard->getPhoto()[0], vcard->getPhoto().size());
 
			handleVCard(user, id, vcard->getFullName(), vcard->getFullName(), vcard->getNickname(), photo);
 
		}
 

	
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
 
			LOG4CXX_INFO(logger, user << ": connecting as " << legacyName);
 
			boost::shared_ptr<Swift::Client> client = boost::make_shared<Swift::Client>(Swift::JID(legacyName), password, m_factories);
 
			m_users[user] = client;
 
			client->setAlwaysTrustCertificates();
 
			client->onConnected.connect(boost::bind(&SwiftenPlugin::handleSwiftConnected, this, user));
 
			client->onDisconnected.connect(boost::bind(&SwiftenPlugin::handleSwiftDisconnected, this, user, _1));
 
			client->onMessageReceived.connect(boost::bind(&SwiftenPlugin::handleSwiftMessageReceived, this, user, _1));
 
			client->getRoster()->onInitialRosterPopulated.connect(boost::bind(&SwiftenPlugin::handleSwiftRosterReceived, this, user));
 
			client->getPresenceOracle()->onPresenceChange.connect(boost::bind(&SwiftenPlugin::handleSwiftPresenceChanged, this, user, _1));
 
			Swift::ClientOptions opt;
 
			opt.allowPLAINWithoutTLS = true;
 
			client->connect(opt);
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				client->onConnected.disconnect(boost::bind(&SwiftenPlugin::handleSwiftConnected, this, user));
 
// 				client->onDisconnected.disconnect(boost::bind(&SwiftenPlugin::handleSwiftDisconnected, this, user, _1));
 
				client->onMessageReceived.disconnect(boost::bind(&SwiftenPlugin::handleSwiftMessageReceived, this, user, _1));
 
				client->getRoster()->onInitialRosterPopulated.disconnect(boost::bind(&SwiftenPlugin::handleSwiftRosterReceived, this, user));
 
				client->getPresenceOracle()->onPresenceChange.disconnect(boost::bind(&SwiftenPlugin::handleSwiftPresenceChanged, this, user, _1));
 
				client->disconnect();
 
			}
 
		}
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &msg, const std::string &xhtml = "") {
 
			LOG4CXX_INFO(logger, "Sending message from " << user << " to " << legacyName << ".");
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				boost::shared_ptr<Swift::Message> message(new Swift::Message());
 
				message->setTo(Swift::JID(legacyName));
 
				message->setFrom(client->getJID());
 
				message->setBody(msg);
 

	
 
				client->sendMessage(message);
 
			}
 
		}
 

	
 
		void handleVCardRequest(const std::string &user, const std::string &legacyName, unsigned int id) {
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				LOG4CXX_INFO(logger, user << ": fetching VCard of " << legacyName << " id=" << id);
 
				Swift::GetVCardRequest::ref request = Swift::GetVCardRequest::create(Swift::JID(legacyName), client->getIQRouter());
 
				request->onResponse.connect(boost::bind(&SwiftenPlugin::handleSwiftVCardReceived, this, user, id, _1, _2));
 
				request->send();
 
			}
 
		}
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) {
 
			LOG4CXX_INFO(logger, user << ": Added/Updated buddy " << buddyName << ".");
 
// 			handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
 
		}
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) {
 

	
 
		}
 

	
 
	private:
 
		Config *config;
 
		std::map<std::string, boost::shared_ptr<Swift::Client> > m_users;
 
};
 

	
 
#ifndef WIN32
 
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);
 
	}
 
}
 
#endif
 

	
 

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

	
 
#ifndef WIN32
 
	if (signal(SIGCHLD, spectrum_sigchld_handler) == SIG_ERR) {
 
		std::cout << "SIGCHLD handler can't be set\n";
 
		return -1;
 
	}
 
#endif
 

	
 
	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 (...)
 
	{
 
		std::cout << desc << "\n";
 
		exit(1);
 
	}
 

	
 

	
 
	if (argc < 5) {
 
		return 1;
 
	}
 

	
 
	Config config;
 
	if (!config.load(argv[5])) {
 
		std::cerr << "Can't open " << argv[1] << " configuration file.\n";
 
		return 1;
 
	}
 

	
 
	Logging::initBackendLogging(&config);
 

	
 
	Swift::SimpleEventLoop eventLoop;
 
	loop_ = &eventLoop;
 
	np = new SwiftenPlugin(&config, &eventLoop, host, port);
 
	loop_->run();
 

	
 
	return 0;
 
}
cmake_modules/BoostConfig.cmake
Show inline comments
 
# - Try to find Boost include dirs and libraries
 
# Usage of this module as follows:
 
#
 
# NOTE: Take note of the Boost_ADDITIONAL_VERSIONS variable below.
 
# Due to Boost naming conventions and limitations in CMake this find
 
# module is NOT future safe with respect to Boost version numbers,
 
# and may break.
 
#
 
# == Using Header-Only libraries from within Boost: ==
 
#
 
#   find_package( Boost 1.36.0 )
 
#   if(Boost_FOUND)
 
#      include_directories(${Boost_INCLUDE_DIRS})
 
#      add_executable(foo foo.cc)
 
#   endif()
 
#
 
#
 
# == Using actual libraries from within Boost: ==
 
#
 
#   set(Boost_USE_STATIC_LIBS   ON)
 
#   set(Boost_USE_MULTITHREADED ON)
 
#   find_package( Boost 1.36.0 COMPONENTS date_time filesystem system ... )
 
#
 
#   if(Boost_FOUND)
 
#      include_directories(${Boost_INCLUDE_DIRS})
 
#      add_executable(foo foo.cc)
 
#      target_link_libraries(foo ${Boost_LIBRARIES})
 
#   endif()
 
#
 
#
 
# The components list needs to contain actual names of boost libraries only,
 
# such as "date_time" for "libboost_date_time".  If you're using parts of
 
# Boost that contain header files only (e.g. foreach) you do not need to
 
# specify COMPONENTS.
 
#
 
# You should provide a minimum version number that should be used. If you provide this 
 
# version number and specify the REQUIRED attribute, this module will fail if it
 
# can't find the specified or a later version. If you specify a version number this is
 
# automatically put into the considered list of version numbers and thus doesn't need
 
# to be specified in the Boost_ADDITIONAL_VERSIONS variable (see below).
 
#
 
# NOTE for Visual Studio Users:
 
#     Automatic linking is used on MSVC & Borland compilers by default when
 
#     #including things in Boost.  It's important to note that setting
 
#     Boost_USE_STATIC_LIBS to OFF is NOT enough to get you dynamic linking,
 
#     should you need this feature.  Automatic linking typically uses static
 
#     libraries with a few exceptions (Boost.Python is one).
 
#
 
#     Please see the section below near Boost_LIB_DIAGNOSTIC_DEFINITIONS for
 
#     more details.  Adding a TARGET_LINK_LIBRARIES() as shown in the example
 
#     above appears to cause VS to link dynamically if Boost_USE_STATIC_LIBS
 
#     gets set to OFF.  It is suggested you avoid automatic linking since it
 
#     will make your application less portable.
 
#
 
# =========== The mess that is Boost_ADDITIONAL_VERSIONS (sorry?) ============
 
#
 
# OK, so the Boost_ADDITIONAL_VERSIONS variable can be used to specify a list of
 
# boost version numbers that should be taken into account when searching
 
# for Boost. Unfortunately boost puts the version number into the
 
# actual filename for the libraries, so this variable will certainly be needed
 
# in the future when new Boost versions are released.
 
#
 
# Currently this module searches for the following version numbers:
 
# 1.33, 1.33.0, 1.33.1, 1.34, 1.34.0, 1.34.1, 1.35, 1.35.0, 1.35.1,
 
# 1.36, 1.36.0, 1.36.1, 1.37, 1.37.0, 1.38, 1.38.0, 1.39, 1.39.0,
 
# 1.40, 1.40.0, 1.41, 1.41.0
 
#
 
# NOTE: If you add a new major 1.x version in Boost_ADDITIONAL_VERSIONS you should
 
# add both 1.x and 1.x.0 as shown above.  Official Boost include directories
 
# omit the 3rd version number from include paths if it is 0 although not all
 
# binary Boost releases do so.
 
#
 
# SET(Boost_ADDITIONAL_VERSIONS "1.78" "1.78.0" "1.79" "1.79.0")
 
#
 
# ===================================== ============= ========================
 
#
 
# Variables used by this module, they can change the default behaviour and
 
# need to be set before calling find_package:
 
#
 
#   Boost_USE_MULTITHREADED      Can be set to OFF to use the non-multithreaded
 
#                                boost libraries.  If not specified, defaults
 
#                                to ON.
 
#
 
#   Boost_USE_STATIC_LIBS        Can be set to ON to force the use of the static
 
#                                boost libraries. Defaults to OFF.
 
#
 
# Other Variables used by this module which you may want to set.
 
#
 
#   Boost_ADDITIONAL_VERSIONS    A list of version numbers to use for searching
 
#                                the boost include directory.  Please see
 
#                                the documentation above regarding this
 
#                                annoying, but necessary variable :(
 
#
 
#   Boost_DEBUG                  Set this to TRUE to enable debugging output
 
#                                of FindBoost.cmake if you are having problems.
 
#                                Please enable this before filing any bug
 
#                                reports.
 
#
 
#   Boost_DETAILED_FAILURE_MSG   FindBoost doesn't output detailed information
 
#                                about why it failed or how to fix the problem
 
#                                unless this is set to TRUE or the REQUIRED
 
#                                keyword is specified in find_package().
 
#                                  [Since CMake 2.8.0]
 
# 
 
#   Boost_COMPILER               Set this to the compiler suffix used by Boost
 
#                                (e.g. "-gcc43") if FindBoost has problems finding
 
#                                the proper Boost installation
 
#
 
# These last three variables are available also as environment variables:
 
#
 
#   BOOST_ROOT or BOOSTROOT      The preferred installation prefix for searching for
 
#                                Boost.  Set this if the module has problems finding
 
#                                the proper Boost installation.
 
#
 
#   BOOST_INCLUDEDIR             Set this to the include directory of Boost, if the
 
#                                module has problems finding the proper Boost installation
 
#
 
#   BOOST_LIBRARYDIR             Set this to the lib directory of Boost, if the
 
#                                module has problems finding the proper Boost installation
 
#
 
# Variables defined by this module:
 
#
 
#   Boost_FOUND                         System has Boost, this means the include dir was
 
#                                       found, as well as all the libraries specified in
 
#                                       the COMPONENTS list.
 
#
 
#   Boost_INCLUDE_DIRS                  Boost include directories: not cached
 
#
 
#   Boost_INCLUDE_DIR                   This is almost the same as above, but this one is
 
#                                       cached and may be modified by advanced users
 
#
 
#   Boost_LIBRARIES                     Link to these to use the Boost libraries that you
 
#                                       specified: not cached
 
#
 
#   Boost_LIBRARY_DIRS                  The path to where the Boost library files are.
 
#
 
#   Boost_VERSION                       The version number of the boost libraries that
 
#                                       have been found, same as in version.hpp from Boost
 
#
 
#   Boost_LIB_VERSION                   The version number in filename form as
 
#                                       it's appended to the library filenames
 
#
 
#   Boost_MAJOR_VERSION                 major version number of boost
 
#   Boost_MINOR_VERSION                 minor version number of boost
 
#   Boost_SUBMINOR_VERSION              subminor version number of boost
 
#
 
#   Boost_LIB_DIAGNOSTIC_DEFINITIONS    [WIN32 Only] You can call
 
#                                       add_definitions(${Boost_LIB_DIAGNOSTIC_DEFINITIONS})
 
#                                       to have diagnostic information about Boost's
 
#                                       automatic linking outputted during compilation time.
 
#
 
# For each component you specify in find_package(), the following (UPPER-CASE)
 
# variables are set.  You can use these variables if you would like to pick and
 
# choose components for your targets instead of just using Boost_LIBRARIES.
 
#
 
#   Boost_${COMPONENT}_FOUND            True IF the Boost library "component" was found.
 
#
 
#   Boost_${COMPONENT}_LIBRARY          Contains the libraries for the specified Boost
 
#                                       "component" (includes debug and optimized keywords
 
#                                       when needed).
 

	
 
#=============================================================================
 
# Copyright 2006-2009 Kitware, Inc.
 
# Copyright 2006-2008 Andreas Schneider <mail@cynapses.org>
 
# Copyright 2007      Wengo
 
# Copyright 2007      Mike Jackson
 
# Copyright 2008      Andreas Pakulat <apaku@gmx.de>
 
# Copyright 2008-2009 Philip Lowman <philip@yhbt.com>
 
#
 
# 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.)
 

	
 
#-------------------------------------------------------------------------------
 
#  FindBoost functions & macros
 
#
 
############################################
 
#
 
# Check the existence of the libraries.
 
#
 
############################################
 
# This macro was taken directly from the FindQt4.cmake file that is included
 
# with the CMake distribution. This is NOT my work. All work was done by the
 
# original authors of the FindQt4.cmake file. Only minor modifications were
 
# made to remove references to Qt and make this file more generally applicable
 
# And ELSE/ENDIF pairs were removed for readability.
 
#########################################################################
 

	
 
MACRO (_Boost_ADJUST_LIB_VARS basename)
 
  IF (Boost_INCLUDE_DIR )
 
    IF (Boost_${basename}_LIBRARY_DEBUG AND Boost_${basename}_LIBRARY_RELEASE)
 
      # if the generator supports configuration types then set
 
      # optimized and debug libraries, or if the CMAKE_BUILD_TYPE has a value
 
      IF (CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)
 
        SET(Boost_${basename}_LIBRARY optimized ${Boost_${basename}_LIBRARY_RELEASE} debug ${Boost_${basename}_LIBRARY_DEBUG})
 
      ELSE()
 
        # if there are no configuration types and CMAKE_BUILD_TYPE has no value
 
        # then just use the release libraries
 
        SET(Boost_${basename}_LIBRARY ${Boost_${basename}_LIBRARY_RELEASE} )
 
      ENDIF()
 
      # FIXME: This probably should be set for both cases
 
      SET(Boost_${basename}_LIBRARIES optimized ${Boost_${basename}_LIBRARY_RELEASE} debug ${Boost_${basename}_LIBRARY_DEBUG})
 
    ENDIF()
 

	
 
    # if only the release version was found, set the debug variable also to the release version
 
    IF (Boost_${basename}_LIBRARY_RELEASE AND NOT Boost_${basename}_LIBRARY_DEBUG)
 
      SET(Boost_${basename}_LIBRARY_DEBUG ${Boost_${basename}_LIBRARY_RELEASE})
 
      SET(Boost_${basename}_LIBRARY       ${Boost_${basename}_LIBRARY_RELEASE})
 
      SET(Boost_${basename}_LIBRARIES     ${Boost_${basename}_LIBRARY_RELEASE})
 
    ENDIF()
 

	
 
    # if only the debug version was found, set the release variable also to the debug version
 
    IF (Boost_${basename}_LIBRARY_DEBUG AND NOT Boost_${basename}_LIBRARY_RELEASE)
 
      SET(Boost_${basename}_LIBRARY_RELEASE ${Boost_${basename}_LIBRARY_DEBUG})
 
      SET(Boost_${basename}_LIBRARY         ${Boost_${basename}_LIBRARY_DEBUG})
 
      SET(Boost_${basename}_LIBRARIES       ${Boost_${basename}_LIBRARY_DEBUG})
 
    ENDIF()
 
    
 
    IF (Boost_${basename}_LIBRARY)
 
      set(Boost_${basename}_LIBRARY ${Boost_${basename}_LIBRARY} CACHE FILEPATH "The Boost ${basename} library")
 

	
 
      # Remove superfluous "debug" / "optimized" keywords from
 
      # Boost_LIBRARY_DIRS
 
      FOREACH(_boost_my_lib ${Boost_${basename}_LIBRARY})
 
        GET_FILENAME_COMPONENT(_boost_my_lib_path "${_boost_my_lib}" PATH)
 
        LIST(APPEND Boost_LIBRARY_DIRS ${_boost_my_lib_path})
 
      ENDFOREACH()
 
      LIST(REMOVE_DUPLICATES Boost_LIBRARY_DIRS)
 

	
 
      set(Boost_LIBRARY_DIRS ${Boost_LIBRARY_DIRS} CACHE FILEPATH "Boost library directory")
 
      SET(Boost_${basename}_FOUND ON CACHE INTERNAL "Whether the Boost ${basename} library found")
 
    ENDIF(Boost_${basename}_LIBRARY)
 

	
 
  ENDIF (Boost_INCLUDE_DIR )
 
  # Make variables changeble to the advanced user
 
  MARK_AS_ADVANCED(
 
      Boost_${basename}_LIBRARY
 
      Boost_${basename}_LIBRARY_RELEASE
 
      Boost_${basename}_LIBRARY_DEBUG
 
  )
 
ENDMACRO (_Boost_ADJUST_LIB_VARS)
 

	
 
#-------------------------------------------------------------------------------
 

	
 
#
 
# Runs compiler with "-dumpversion" and parses major/minor
 
# version with a regex.
 
#
 
FUNCTION(_Boost_COMPILER_DUMPVERSION _OUTPUT_VERSION)
 

	
 
  EXEC_PROGRAM(${CMAKE_CXX_COMPILER}
 
    ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
 
    OUTPUT_VARIABLE _boost_COMPILER_VERSION
 
  )
 
  STRING(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
 
    _boost_COMPILER_VERSION ${_boost_COMPILER_VERSION})
 

	
 
  SET(${_OUTPUT_VERSION} ${_boost_COMPILER_VERSION} PARENT_SCOPE)
 
ENDFUNCTION()
 

	
 
#
 
# A convenience function for marking desired components
 
# as found or not
 
#
 
function(_Boost_MARK_COMPONENTS_FOUND _yes_or_no)
 
  foreach(COMPONENT ${Boost_FIND_COMPONENTS})
 
    string(TOUPPER ${COMPONENT} UPPERCOMPONENT)
 
    set(Boost_${UPPERCOMPONENT}_FOUND ${_yes_or_no} CACHE INTERNAL "Whether the Boost ${COMPONENT} library found" FORCE)
 
  endforeach()
 
endfunction()
 

	
 
#
 
# End functions/macros
 
#  
 
#-------------------------------------------------------------------------------
 

	
 

	
 

	
 

	
 
IF(NOT DEFINED Boost_USE_MULTITHREADED)
 
    SET(Boost_USE_MULTITHREADED TRUE)
 
ENDIF()
 

	
 
if(Boost_FIND_VERSION_EXACT)
 
  # The version may appear in a directory with or without the patch
 
  # level, even when the patch level is non-zero.
 
  set(_boost_TEST_VERSIONS
 
    "${Boost_FIND_VERSION_MAJOR}.${Boost_FIND_VERSION_MINOR}.${Boost_FIND_VERSION_PATCH}"
 
    "${Boost_FIND_VERSION_MAJOR}.${Boost_FIND_VERSION_MINOR}")
 
else(Boost_FIND_VERSION_EXACT)
 
  # The user has not requested an exact version.  Among known
 
  # versions, find those that are acceptable to the user request.
 
  set(_Boost_KNOWN_VERSIONS ${Boost_ADDITIONAL_VERSIONS}
 
    "1.41.0" "1.41" "1.40.0" "1.40" "1.39.0" "1.39" "1.38.0" "1.38" "1.37.0" "1.37"
 
    "1.36.1" "1.36.0" "1.36" "1.35.1" "1.35.0" "1.35" "1.34.1" "1.34.0"
 
    "1.34" "1.33.1" "1.33.0" "1.33")
 
  set(_boost_TEST_VERSIONS)
 
  if(Boost_FIND_VERSION)
 
    set(_Boost_FIND_VERSION_SHORT "${Boost_FIND_VERSION_MAJOR}.${Boost_FIND_VERSION_MINOR}")
 
    # Select acceptable versions.
 
    foreach(version ${_Boost_KNOWN_VERSIONS})
 
      if(NOT "${version}" VERSION_LESS "${Boost_FIND_VERSION}")
 
        # This version is high enough.
 
        list(APPEND _boost_TEST_VERSIONS "${version}")
 
      elseif("${version}.99" VERSION_EQUAL "${_Boost_FIND_VERSION_SHORT}.99")
 
        # This version is a short-form for the requested version with
 
        # the patch level dropped.
 
        list(APPEND _boost_TEST_VERSIONS "${version}")
 
      endif()
 
    endforeach(version)
 
  else(Boost_FIND_VERSION)
 
    # Any version is acceptable.
 
    set(_boost_TEST_VERSIONS "${_Boost_KNOWN_VERSIONS}")
 
  endif(Boost_FIND_VERSION)
 
endif(Boost_FIND_VERSION_EXACT)
 

	
 
# The reason that we failed to find Boost. This will be set to a
 
# user-friendly message when we fail to find some necessary piece of
 
# Boost.
 
set(Boost_ERROR_REASON)
 

	
 
SET( _boost_IN_CACHE TRUE)
 
IF(Boost_INCLUDE_DIR)
 

	
 
  # On versions < 1.35, remove the System library from the considered list
 
  # since it wasn't added until 1.35.
 
  if(Boost_VERSION AND Boost_FIND_COMPONENTS)
 
     math(EXPR _boost_maj "${Boost_VERSION} / 100000")
 
     math(EXPR _boost_min "${Boost_VERSION} / 100 % 1000")
 
     if(${_boost_maj}.${_boost_min} VERSION_LESS 1.35)
 
       list(REMOVE_ITEM Boost_FIND_COMPONENTS system)
 
     endif()
 
  endif()
 

	
 
  FOREACH(COMPONENT ${Boost_FIND_COMPONENTS})
 
    STRING(TOUPPER ${COMPONENT} COMPONENT)
 
    IF(NOT Boost_${COMPONENT}_FOUND)
 
      SET( _boost_IN_CACHE FALSE)
 
    ENDIF(NOT Boost_${COMPONENT}_FOUND)
 
  ENDFOREACH(COMPONENT)
 
ELSE(Boost_INCLUDE_DIR)
 
  SET( _boost_IN_CACHE FALSE)
 
ENDIF(Boost_INCLUDE_DIR)
 

	
 
IF (_boost_IN_CACHE)
 
  # in cache already
 
  SET(Boost_FOUND TRUE)
 
  FOREACH(COMPONENT ${Boost_FIND_COMPONENTS})
 
    STRING(TOUPPER ${COMPONENT} COMPONENT)
 
    _Boost_ADJUST_LIB_VARS( ${COMPONENT} )
 
    SET(Boost_LIBRARIES ${Boost_LIBRARIES} ${Boost_${COMPONENT}_LIBRARY})
 
  ENDFOREACH(COMPONENT)
 
  SET(Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIR})
 
  IF(Boost_VERSION AND NOT "${Boost_VERSION}" STREQUAL "0")
 
    MATH(EXPR Boost_MAJOR_VERSION "${Boost_VERSION} / 100000")
 
    MATH(EXPR Boost_MINOR_VERSION "${Boost_VERSION} / 100 % 1000")
 
    MATH(EXPR Boost_SUBMINOR_VERSION "${Boost_VERSION} % 100")
 
  ENDIF(Boost_VERSION AND NOT "${Boost_VERSION}" STREQUAL "0")
 
  if(Boost_DEBUG)
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                     "boost ${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION}.${Boost_SUBMINOR_VERSION} "
 
                     "is already in the cache.  For debugging messages, please clear the cache.")
 
  endif()
 
ELSE (_boost_IN_CACHE)
 
  # Need to search for boost
 
  if(Boost_DEBUG)
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "Boost not in cache")
 
    # Output some of their choices
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "_boost_TEST_VERSIONS = ${_boost_TEST_VERSIONS}")
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "Boost_USE_MULTITHREADED = ${Boost_USE_MULTITHREADED}")
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "Boost_USE_STATIC_LIBS = ${Boost_USE_STATIC_LIBS}")
 
  endif()
 

	
 
  IF(WIN32)
 
    # In windows, automatic linking is performed, so you do not have
 
    # to specify the libraries.  If you are linking to a dynamic
 
    # runtime, then you can choose to link to either a static or a
 
    # dynamic Boost library, the default is to do a static link.  You
 
    # can alter this for a specific library "whatever" by defining
 
    # BOOST_WHATEVER_DYN_LINK to force Boost library "whatever" to be
 
    # linked dynamically.  Alternatively you can force all Boost
 
    # libraries to dynamic link by defining BOOST_ALL_DYN_LINK.
 
  
 
    # This feature can be disabled for Boost library "whatever" by
 
    # defining BOOST_WHATEVER_NO_LIB, or for all of Boost by defining
 
    # BOOST_ALL_NO_LIB.
 
  
 
    # If you want to observe which libraries are being linked against
 
    # then defining BOOST_LIB_DIAGNOSTIC will cause the auto-linking
 
    # code to emit a #pragma message each time a library is selected
 
    # for linking.
 
    SET(Boost_LIB_DIAGNOSTIC_DEFINITIONS 
 
      "-DBOOST_LIB_DIAGNOSTIC" CACHE STRING "Boost diagnostic define")
 
  ENDIF(WIN32)
 

	
 
  SET(_boost_INCLUDE_SEARCH_DIRS
 
    C:/boost/include
 
    C:/boost
 
    "$ENV{ProgramFiles}/boost/include"
 
    "$ENV{ProgramFiles}/boost"
 
    /sw/local/include
 
  )
 

	
 
  # If BOOST_ROOT was defined in the environment, use it.
 
  if (NOT BOOST_ROOT AND NOT $ENV{BOOST_ROOT} STREQUAL "")
 
    set(BOOST_ROOT $ENV{BOOST_ROOT})
 
  endif(NOT BOOST_ROOT AND NOT $ENV{BOOST_ROOT} STREQUAL "")
 

	
 
  # If BOOSTROOT was defined in the environment, use it.
 
  if (NOT BOOST_ROOT AND NOT $ENV{BOOSTROOT} STREQUAL "")
 
    set(BOOST_ROOT $ENV{BOOSTROOT})
 
  endif(NOT BOOST_ROOT AND NOT $ENV{BOOSTROOT} STREQUAL "")
 

	
 
  # If BOOST_INCLUDEDIR was defined in the environment, use it.
 
  IF( NOT $ENV{BOOST_INCLUDEDIR} STREQUAL "" )
 
    set(BOOST_INCLUDEDIR $ENV{BOOST_INCLUDEDIR})
 
  ENDIF( NOT $ENV{BOOST_INCLUDEDIR} STREQUAL "" )
 
  
 
  # If BOOST_LIBRARYDIR was defined in the environment, use it.
 
  IF( NOT $ENV{BOOST_LIBRARYDIR} STREQUAL "" )
 
    set(BOOST_LIBRARYDIR $ENV{BOOST_LIBRARYDIR})
 
  ENDIF( NOT $ENV{BOOST_LIBRARYDIR} STREQUAL "" )
 
  
 
  IF( BOOST_ROOT )
 
    file(TO_CMAKE_PATH ${BOOST_ROOT} BOOST_ROOT)
 
  ENDIF( BOOST_ROOT )
 

	
 
  if(Boost_DEBUG)
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "Declared as CMake or Environmental Variables:")
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "  BOOST_ROOT = ${BOOST_ROOT}")
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "  BOOST_INCLUDEDIR = ${BOOST_INCLUDEDIR}")
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "  BOOST_LIBRARYDIR = ${BOOST_LIBRARYDIR}")
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                   "_boost_TEST_VERSIONS = ${_boost_TEST_VERSIONS}")
 
  endif()
 

	
 
  IF( BOOST_ROOT )
 
    SET(_boost_INCLUDE_SEARCH_DIRS 
 
      ${BOOST_ROOT}/include 
 
      ${BOOST_ROOT}
 
      ${_boost_INCLUDE_SEARCH_DIRS})
 
  ENDIF( BOOST_ROOT )
 

	
 
  IF( BOOST_INCLUDEDIR )
 
    file(TO_CMAKE_PATH ${BOOST_INCLUDEDIR} BOOST_INCLUDEDIR)
 
    SET(_boost_INCLUDE_SEARCH_DIRS 
 
      ${BOOST_INCLUDEDIR} ${_boost_INCLUDE_SEARCH_DIRS})
 
  ENDIF( BOOST_INCLUDEDIR )
 

	
 
  # ------------------------------------------------------------------------
 
  #  Search for Boost include DIR 
 
  # ------------------------------------------------------------------------
 
  # Try to find Boost by stepping backwards through the Boost versions
 
  # we know about.
 
  IF( NOT Boost_INCLUDE_DIR )
 
    # Build a list of path suffixes for each version.
 
    SET(_boost_PATH_SUFFIXES)
 
    FOREACH(_boost_VER ${_boost_TEST_VERSIONS})
 
      # Add in a path suffix, based on the required version, ideally
 
      # we could read this from version.hpp, but for that to work we'd
 
      # need to know the include dir already
 
      set(_boost_BOOSTIFIED_VERSION)
 

	
 
      # Transform 1.35 => 1_35 and 1.36.0 => 1_36_0
 
      IF(_boost_VER MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
 
          STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\1_\\2_\\3" 
 
            _boost_BOOSTIFIED_VERSION ${_boost_VER})
 
      ELSEIF(_boost_VER MATCHES "[0-9]+\\.[0-9]+")
 
          STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)" "\\1_\\2" 
 
            _boost_BOOSTIFIED_VERSION ${_boost_VER})
 
      ENDIF()
 
      
 
      list(APPEND _boost_PATH_SUFFIXES "boost-${_boost_BOOSTIFIED_VERSION}")
 
      if(WIN32)
 
        # For BoostPro's underscores (and others?)
 
        list(APPEND _boost_PATH_SUFFIXES "boost_${_boost_BOOSTIFIED_VERSION}")
 
      endif()
 

	
 
    ENDFOREACH(_boost_VER)
 
      
 
    if(Boost_DEBUG)
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                     "Include debugging info:")
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                     "  _boost_INCLUDE_SEARCH_DIRS = ${_boost_INCLUDE_SEARCH_DIRS}")
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                     "  _boost_PATH_SUFFIXES = ${_boost_PATH_SUFFIXES}")
 
    endif()
 

	
 
    # Look for a standard boost header file.
 
    FIND_PATH(Boost_INCLUDE_DIR
 
      NAMES         boost/config.hpp
 
      HINTS         ${_boost_INCLUDE_SEARCH_DIRS}
 
      PATH_SUFFIXES ${_boost_PATH_SUFFIXES}
 
      )
 
  ENDIF( NOT Boost_INCLUDE_DIR )
 
  
 
  # ------------------------------------------------------------------------
 
  #  Extract version information from version.hpp
 
  # ------------------------------------------------------------------------
 

	
 
  IF(Boost_INCLUDE_DIR)
 
    # Extract Boost_VERSION and Boost_LIB_VERSION from version.hpp
 
    # Read the whole file:
 
    #
 
    SET(BOOST_VERSION 0)
 
    SET(BOOST_LIB_VERSION "")
 
    FILE(READ "${Boost_INCLUDE_DIR}/boost/version.hpp" _boost_VERSION_HPP_CONTENTS)
 
    if(Boost_DEBUG)
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                     "location of version.hpp: ${Boost_INCLUDE_DIR}/boost/version.hpp")
 
    endif()
 
  
 
    STRING(REGEX REPLACE ".*#define BOOST_VERSION ([0-9]+).*" "\\1" Boost_VERSION "${_boost_VERSION_HPP_CONTENTS}")
 
    STRING(REGEX REPLACE ".*#define BOOST_LIB_VERSION \"([0-9_]+)\".*" "\\1" Boost_LIB_VERSION "${_boost_VERSION_HPP_CONTENTS}")
 
  
 
    SET(Boost_LIB_VERSION ${Boost_LIB_VERSION} CACHE INTERNAL "The library version string for boost libraries")
 
    SET(Boost_VERSION ${Boost_VERSION} CACHE INTERNAL "The version number for boost libraries")
 
    
 
    IF(NOT "${Boost_VERSION}" STREQUAL "0")
 
      MATH(EXPR Boost_MAJOR_VERSION "${Boost_VERSION} / 100000")
 
      MATH(EXPR Boost_MINOR_VERSION "${Boost_VERSION} / 100 % 1000")
 
      MATH(EXPR Boost_SUBMINOR_VERSION "${Boost_VERSION} % 100")
 

	
 
      set(Boost_ERROR_REASON
 
          "${Boost_ERROR_REASON}Boost version: ${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION}.${Boost_SUBMINOR_VERSION}\nBoost include path: ${Boost_INCLUDE_DIR}")
 
    ENDIF(NOT "${Boost_VERSION}" STREQUAL "0")
 
    if(Boost_DEBUG)
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                     "version.hpp reveals boost "
 
                     "${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION}.${Boost_SUBMINOR_VERSION}")
 
    endif()
 
  ELSE(Boost_INCLUDE_DIR)
 
    set(Boost_ERROR_REASON
 
      "${Boost_ERROR_REASON}Unable to find the Boost header files. Please set BOOST_ROOT to the root directory containing Boost or BOOST_INCLUDEDIR to the directory containing Boost's headers.")
 
  ENDIF(Boost_INCLUDE_DIR)
 
  
 
  # ------------------------------------------------------------------------
 
  #  Suffix initialization and compiler suffix detection.
 
  # ------------------------------------------------------------------------
 

	
 
  # Setting some more suffixes for the library
 
  SET (Boost_LIB_PREFIX "")
 
  if ( WIN32 AND Boost_USE_STATIC_LIBS )
 
    SET (Boost_LIB_PREFIX "lib")
 
  endif()
 

	
 
  if (Boost_COMPILER)
 
    set(_boost_COMPILER ${Boost_COMPILER})
 
    if(Boost_DEBUG)
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
                     "using user-specified Boost_COMPILER = ${_boost_COMPILER}")
 
    endif()
 
  else(Boost_COMPILER)
 
    # Attempt to guess the compiler suffix
 
    # NOTE: this is not perfect yet, if you experience any issues
 
    # please report them and use the Boost_COMPILER variable
 
    # to work around the problems.
 
    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel"
 
        OR "${CMAKE_CXX_COMPILER}" MATCHES "icl" 
 
        OR "${CMAKE_CXX_COMPILER}" MATCHES "icpc")
 
      if(WIN32)
 
        set (_boost_COMPILER "-iw")
 
      else()
 
        set (_boost_COMPILER "-il")
 
      endif()
 
    elseif (MSVC90)
 
      SET (_boost_COMPILER "-vc90")
 
    elseif (MSVC10)
 
      SET (_boost_COMPILER "-vc100")
 
    elseif (MSVC80)
 
      SET (_boost_COMPILER "-vc80")
 
    elseif (MSVC71)
 
      SET (_boost_COMPILER "-vc71")
 
    elseif (MSVC70) # Good luck!
 
      SET (_boost_COMPILER "-vc7") # yes, this is correct
 
    elseif (MSVC60) # Good luck!
 
      SET (_boost_COMPILER "-vc6") # yes, this is correct
 
    elseif (BORLAND)
 
      SET (_boost_COMPILER "-bcb")
 
    elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "SunPro")
 
      set(_boost_COMPILER "-sw")
 
    elseif (MINGW)
 
      if(${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION} VERSION_LESS 1.34)
 
          SET(_boost_COMPILER "-mgw") # no GCC version encoding prior to 1.34
 
      else()
 
        _Boost_COMPILER_DUMPVERSION(_boost_COMPILER_VERSION)
 
        SET (_boost_COMPILER "-mgw${_boost_COMPILER_VERSION}")
 
      endif()
 
    elseif (UNIX)
 
      if (CMAKE_COMPILER_IS_GNUCXX)
 
        if(${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION} VERSION_LESS 1.34)
 
          SET(_boost_COMPILER "-gcc") # no GCC version encoding prior to 1.34
 
        else()
 
          _Boost_COMPILER_DUMPVERSION(_boost_COMPILER_VERSION)
 
          # Determine which version of GCC we have.
 
          IF(APPLE)
 
            IF(Boost_MINOR_VERSION)
 
              IF(${Boost_MINOR_VERSION} GREATER 35)
 
                # In Boost 1.36.0 and newer, the mangled compiler name used
 
                # on Mac OS X/Darwin is "xgcc".
 
                SET(_boost_COMPILER "-xgcc${_boost_COMPILER_VERSION}")
 
              ELSE(${Boost_MINOR_VERSION} GREATER 35)
 
                # In Boost <= 1.35.0, there is no mangled compiler name for
 
                # the Mac OS X/Darwin version of GCC.
 
                SET(_boost_COMPILER "")
 
              ENDIF(${Boost_MINOR_VERSION} GREATER 35)
 
            ELSE(Boost_MINOR_VERSION)
 
              # We don't know the Boost version, so assume it's
 
              # pre-1.36.0.
 
              SET(_boost_COMPILER "")
 
            ENDIF(Boost_MINOR_VERSION)
 
          ELSE()
 
            SET (_boost_COMPILER "-gcc${_boost_COMPILER_VERSION}")
 
          ENDIF()
 
        endif()
 
      endif (CMAKE_COMPILER_IS_GNUCXX)
 
    endif()
 
    if(Boost_DEBUG)
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
        "guessed _boost_COMPILER = ${_boost_COMPILER}")
 
    endif()
 
  endif(Boost_COMPILER)
 

	
 

	
 
  if(${Boost_MINOR_VERSION} GREATER 41 AND NOT WIN32 AND NOT APPLE)
 
      set (_boost_MULTITHREADED "")
 
  else()
 
#   if(${Boost_MINOR_VERSION} GREATER 41 AND NOT WIN32 AND NOT APPLE)
 
#       set (_boost_MULTITHREADED "")
 
#   else()
 
    SET (_boost_MULTITHREADED "-mt")
 
    if( NOT Boost_USE_MULTITHREADED )
 
      set (_boost_MULTITHREADED "")
 
    endif()
 
  endif()
 
#   endif()
 

	
 
  if(Boost_DEBUG)
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
      "_boost_MULTITHREADED = ${_boost_MULTITHREADED}")
 
  endif()
 

	
 
  SET( _boost_STATIC_TAG "")
 
  set( _boost_ABI_TAG "")
 
  IF (WIN32)
 
    IF(MSVC OR "${CMAKE_CXX_COMPILER}" MATCHES "icl"
 
            OR "${CMAKE_CXX_COMPILER}" MATCHES "icpc")
 
      SET (_boost_ABI_TAG "g")
 
    ENDIF()
 
    IF( Boost_USE_STATIC_LIBS )
 
      SET( _boost_STATIC_TAG "-s")
 
    ENDIF( Boost_USE_STATIC_LIBS )
 
  ENDIF(WIN32)
 
  SET (_boost_ABI_TAG "${_boost_ABI_TAG}d")
 
  if(Boost_DEBUG)
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
      "_boost_STATIC_TAG = ${_boost_STATIC_TAG}")
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
      "_boost_ABI_TAG = ${_boost_ABI_TAG}")
 
  endif()
 

	
 
  # ------------------------------------------------------------------------
 
  #  Begin finding boost libraries
 
  # ------------------------------------------------------------------------
 

	
 
  SET(_boost_LIBRARIES_SEARCH_DIRS
 
    ${Boost_INCLUDE_DIR}/lib
 
    ${Boost_INCLUDE_DIR}/../lib
 
    C:/boost/lib
 
    C:/boost
 
    "$ENV{ProgramFiles}/boost/boost_${Boost_MAJOR_VERSION}_${Boost_MINOR_VERSION}_${Boost_SUBMINOR_VERSION}/lib"
 
    "$ENV{ProgramFiles}/boost/boost_${Boost_MAJOR_VERSION}_${Boost_MINOR_VERSION}/lib"
 
    "$ENV{ProgramFiles}/boost/lib"
 
    "$ENV{ProgramFiles}/boost"
 
    /sw/local/lib
 
  )
 
  IF( BOOST_ROOT )
 
    SET(_boost_LIBRARIES_SEARCH_DIRS 
 
      ${BOOST_ROOT}/lib 
 
      ${BOOST_ROOT}/stage/lib 
 
      ${_boost_LIBRARIES_SEARCH_DIRS})
 
  ENDIF( BOOST_ROOT )
 

	
 
  IF( BOOST_LIBRARYDIR )
 
    file(TO_CMAKE_PATH ${BOOST_LIBRARYDIR} BOOST_LIBRARYDIR)
 
    SET(_boost_LIBRARIES_SEARCH_DIRS 
 
      ${BOOST_LIBRARYDIR} ${_boost_LIBRARIES_SEARCH_DIRS})
 
  ENDIF( BOOST_LIBRARYDIR )
 

	
 
  if(Boost_DEBUG)
 
    message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] "
 
      "_boost_LIBRARIES_SEARCH_DIRS = ${_boost_LIBRARIES_SEARCH_DIRS}")
 
  endif()
 

	
 
  FOREACH(COMPONENT ${Boost_FIND_COMPONENTS})
 
    STRING(TOUPPER ${COMPONENT} UPPERCOMPONENT)
 
    SET( Boost_${UPPERCOMPONENT}_LIBRARY "Boost_${UPPERCOMPONENT}_LIBRARY-NOTFOUND" )
 
    SET( Boost_${UPPERCOMPONENT}_LIBRARY_RELEASE "Boost_${UPPERCOMPONENT}_LIBRARY_RELEASE-NOTFOUND" )
 
    SET( Boost_${UPPERCOMPONENT}_LIBRARY_DEBUG "Boost_${UPPERCOMPONENT}_LIBRARY_DEBUG-NOTFOUND")
 

	
 
    # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
 
    IF( Boost_USE_STATIC_LIBS )
 
      SET( _boost_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
 
      IF(WIN32)
 
        SET(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
 
      ELSE(WIN32)
 
        SET(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
 
      ENDIF(WIN32)
 
    ENDIF( Boost_USE_STATIC_LIBS )
 

	
 
    FIND_LIBRARY(Boost_${UPPERCOMPONENT}_LIBRARY_RELEASE
 
        NAMES  ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_COMPILER}${_boost_MULTITHREADED}-${Boost_LIB_VERSION}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_COMPILER}${_boost_MULTITHREADED}${_boost_STATIC_TAG}-${Boost_LIB_VERSION}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_MULTITHREADED}-${Boost_LIB_VERSION}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_MULTITHREADED}${_boost_STATIC_TAG}-${Boost_LIB_VERSION}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_MULTITHREADED}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_MULTITHREADED}${_boost_STATIC_TAG}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}
 
        HINTS  ${_boost_LIBRARIES_SEARCH_DIRS}
 
    )
 

	
 
    FIND_LIBRARY(Boost_${UPPERCOMPONENT}_LIBRARY_DEBUG
 
        NAMES  ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_COMPILER}${_boost_MULTITHREADED}-${_boost_ABI_TAG}-${Boost_LIB_VERSION}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_COMPILER}${_boost_MULTITHREADED}${_boost_STATIC_TAG}${_boost_ABI_TAG}-${Boost_LIB_VERSION}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_MULTITHREADED}-${_boost_ABI_TAG}-${Boost_LIB_VERSION}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_MULTITHREADED}${_boost_STATIC_TAG}${_boost_ABI_TAG}-${Boost_LIB_VERSION}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_MULTITHREADED}-${_boost_ABI_TAG}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}${_boost_MULTITHREADED}${_boost_STATIC_TAG}${_boost_ABI_TAG}
 
               ${Boost_LIB_PREFIX}boost_${COMPONENT}-${_boost_ABI_TAG}
 
        HINTS  ${_boost_LIBRARIES_SEARCH_DIRS}
 
    )
 

	
 
    _Boost_ADJUST_LIB_VARS(${UPPERCOMPONENT})
 
    IF( Boost_USE_STATIC_LIBS )
 
      SET(CMAKE_FIND_LIBRARY_SUFFIXES ${_boost_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
 
    ENDIF( Boost_USE_STATIC_LIBS )
 
  ENDFOREACH(COMPONENT)
 
  # ------------------------------------------------------------------------
 
  #  End finding boost libraries
 
  # ------------------------------------------------------------------------
 

	
 
  SET(Boost_INCLUDE_DIRS
 
    ${Boost_INCLUDE_DIR}
 
  )
 

	
 
  SET(Boost_FOUND FALSE)
 
  IF(Boost_INCLUDE_DIR)
 
    SET( Boost_FOUND TRUE )
 

	
 
    # Check the version of Boost against the requested version.
 
    if (Boost_FIND_VERSION AND NOT Boost_FIND_VERSION_MINOR)
 
      message(SEND_ERROR "When requesting a specific version of Boost, you must provide at least the major and minor version numbers, e.g., 1.34")
 
    endif (Boost_FIND_VERSION AND NOT Boost_FIND_VERSION_MINOR)
 
    if(Boost_MAJOR_VERSION LESS "${Boost_FIND_VERSION_MAJOR}" )
 
      set( Boost_FOUND FALSE )
 
      set(_Boost_VERSION_AGE "old")
 
    elseif(Boost_MAJOR_VERSION EQUAL "${Boost_FIND_VERSION_MAJOR}" )
 
      if(Boost_MINOR_VERSION LESS "${Boost_FIND_VERSION_MINOR}" )
 
        set( Boost_FOUND FALSE )
 
        set(_Boost_VERSION_AGE "old")
 
      elseif(Boost_MINOR_VERSION EQUAL "${Boost_FIND_VERSION_MINOR}" )
 
        if( Boost_FIND_VERSION_PATCH AND Boost_SUBMINOR_VERSION LESS "${Boost_FIND_VERSION_PATCH}" )
 
          set( Boost_FOUND FALSE )
 
          set(_Boost_VERSION_AGE "old")
 
        endif( Boost_FIND_VERSION_PATCH AND Boost_SUBMINOR_VERSION LESS "${Boost_FIND_VERSION_PATCH}" )
 
      endif( Boost_MINOR_VERSION LESS "${Boost_FIND_VERSION_MINOR}" )
 
    endif( Boost_MAJOR_VERSION LESS "${Boost_FIND_VERSION_MAJOR}" )
 

	
 
    if (NOT Boost_FOUND)
 
      _Boost_MARK_COMPONENTS_FOUND(OFF)
 
    endif()
 

	
 
    if (Boost_FOUND AND Boost_FIND_VERSION_EXACT)
 
      # If the user requested an exact version of Boost, check
 
      # that. We already know that the Boost version we have is >= the
 
      # requested version.
 
      set(_Boost_VERSION_AGE "new")
 

	
 
      # If the user didn't specify a patchlevel, it's 0.
 
      if (NOT Boost_FIND_VERSION_PATCH)
 
        set(Boost_FIND_VERSION_PATCH 0)
 
      endif (NOT Boost_FIND_VERSION_PATCH)
 
      
 
      # We'll set Boost_FOUND true again if we have an exact version match.
 
      set(Boost_FOUND FALSE)
 
      _Boost_MARK_COMPONENTS_FOUND(OFF)
 
      if(Boost_MAJOR_VERSION EQUAL "${Boost_FIND_VERSION_MAJOR}" )
 
        if(Boost_MINOR_VERSION EQUAL "${Boost_FIND_VERSION_MINOR}" )
 
          if(Boost_SUBMINOR_VERSION EQUAL "${Boost_FIND_VERSION_PATCH}" )
 
            set( Boost_FOUND TRUE )
 
            _Boost_MARK_COMPONENTS_FOUND(ON)
 
          endif(Boost_SUBMINOR_VERSION EQUAL "${Boost_FIND_VERSION_PATCH}" )
 
        endif( Boost_MINOR_VERSION EQUAL "${Boost_FIND_VERSION_MINOR}" )
 
      endif( Boost_MAJOR_VERSION EQUAL "${Boost_FIND_VERSION_MAJOR}" )
 
    endif (Boost_FOUND AND Boost_FIND_VERSION_EXACT)
 

	
 
    if(NOT Boost_FOUND)
 
      # State that we found a version of Boost that is too new or too old.
 
      set(Boost_ERROR_REASON
 
        "${Boost_ERROR_REASON}\nDetected version of Boost is too ${_Boost_VERSION_AGE}. Requested version was ${Boost_FIND_VERSION_MAJOR}.${Boost_FIND_VERSION_MINOR}")
 
      if (Boost_FIND_VERSION_PATCH)
 
        set(Boost_ERROR_REASON 
 
          "${Boost_ERROR_REASON}.${Boost_FIND_VERSION_PATCH}")
 
      endif (Boost_FIND_VERSION_PATCH)
 
      if (NOT Boost_FIND_VERSION_EXACT)
 
        set(Boost_ERROR_REASON "${Boost_ERROR_REASON} (or newer)")
 
      endif (NOT Boost_FIND_VERSION_EXACT)
 
      set(Boost_ERROR_REASON "${Boost_ERROR_REASON}.")
 
    endif (NOT Boost_FOUND)
 

	
 
    # Always check for missing components
 
    set(_boost_CHECKED_COMPONENT FALSE)
 
    set(_Boost_MISSING_COMPONENTS "")
 
    foreach(COMPONENT ${Boost_FIND_COMPONENTS})
 
      string(TOUPPER ${COMPONENT} COMPONENT)
 
      set(_boost_CHECKED_COMPONENT TRUE)
 
      if(NOT Boost_${COMPONENT}_FOUND)
 
        string(TOLOWER ${COMPONENT} COMPONENT)
 
        list(APPEND _Boost_MISSING_COMPONENTS ${COMPONENT})
 
        set( Boost_FOUND FALSE)
 
      endif(NOT Boost_${COMPONENT}_FOUND)
 
    endforeach(COMPONENT)
 

	
 
    if(Boost_DEBUG)
 
      message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] Boost_FOUND = ${Boost_FOUND}")
 
    endif()
 

	
 
    if (_Boost_MISSING_COMPONENTS)
 
      # We were unable to find some libraries, so generate a sensible
 
      # error message that lists the libraries we were unable to find.
 
      set(Boost_ERROR_REASON
 
        "${Boost_ERROR_REASON}\nThe following Boost libraries could not be found:\n")
 
      foreach(COMPONENT ${_Boost_MISSING_COMPONENTS})
 
        set(Boost_ERROR_REASON
 
          "${Boost_ERROR_REASON}        boost_${COMPONENT}\n")
 
      endforeach(COMPONENT)
 

	
 
      list(LENGTH Boost_FIND_COMPONENTS Boost_NUM_COMPONENTS_WANTED)
 
      list(LENGTH _Boost_MISSING_COMPONENTS Boost_NUM_MISSING_COMPONENTS)
 
      if (${Boost_NUM_COMPONENTS_WANTED} EQUAL ${Boost_NUM_MISSING_COMPONENTS})
 
        set(Boost_ERROR_REASON
 
          "${Boost_ERROR_REASON}No Boost libraries were found. You may need to set Boost_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.")
 
      else (${Boost_NUM_COMPONENTS_WANTED} EQUAL ${Boost_NUM_MISSING_COMPONENTS})
 
        set(Boost_ERROR_REASON
 
          "${Boost_ERROR_REASON}Some (but not all) of the required Boost libraries were found. You may need to install these additional Boost libraries. Alternatively, set Boost_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.")
 
      endif (${Boost_NUM_COMPONENTS_WANTED} EQUAL ${Boost_NUM_MISSING_COMPONENTS})
 
    endif (_Boost_MISSING_COMPONENTS)
 

	
 
    IF( NOT Boost_LIBRARY_DIRS AND NOT _boost_CHECKED_COMPONENT )
 
      # Compatibility Code for backwards compatibility with CMake
 
      # 2.4's FindBoost module.
 

	
 
      # Look for the boost library path.
 
      # Note that the user may not have installed any libraries
 
      # so it is quite possible the Boost_LIBRARY_PATH may not exist.
 
      SET(_boost_LIB_DIR ${Boost_INCLUDE_DIR})
 
    
 
      IF("${_boost_LIB_DIR}" MATCHES "boost-[0-9]+")
 
        GET_FILENAME_COMPONENT(_boost_LIB_DIR ${_boost_LIB_DIR} PATH)
 
      ENDIF ("${_boost_LIB_DIR}" MATCHES "boost-[0-9]+")
 
    
 
      IF("${_boost_LIB_DIR}" MATCHES "/include$")
 
        # Strip off the trailing "/include" in the path.
 
        GET_FILENAME_COMPONENT(_boost_LIB_DIR ${_boost_LIB_DIR} PATH)
 
      ENDIF("${_boost_LIB_DIR}" MATCHES "/include$")
 
    
 
      IF(EXISTS "${_boost_LIB_DIR}/lib")
 
        SET (_boost_LIB_DIR ${_boost_LIB_DIR}/lib)
 
      ELSE(EXISTS "${_boost_LIB_DIR}/lib")
 
        IF(EXISTS "${_boost_LIB_DIR}/stage/lib")
 
          SET(_boost_LIB_DIR ${_boost_LIB_DIR}/stage/lib)
 
        ELSE(EXISTS "${_boost_LIB_DIR}/stage/lib")
 
          SET(_boost_LIB_DIR "")
 
        ENDIF(EXISTS "${_boost_LIB_DIR}/stage/lib")
 
      ENDIF(EXISTS "${_boost_LIB_DIR}/lib")
 
    
 
      IF(_boost_LIB_DIR AND EXISTS "${_boost_LIB_DIR}")
 
        SET(Boost_LIBRARY_DIRS ${_boost_LIB_DIR} CACHE FILEPATH "Boost library directory")
 
      ENDIF(_boost_LIB_DIR AND EXISTS "${_boost_LIB_DIR}")
 

	
 
    ENDIF( NOT Boost_LIBRARY_DIRS AND NOT _boost_CHECKED_COMPONENT )
 

	
 
  ELSE(Boost_INCLUDE_DIR)
 
    SET( Boost_FOUND FALSE)
 
  ENDIF(Boost_INCLUDE_DIR)
 

	
 
  IF (Boost_FOUND)
 
      IF (NOT Boost_FIND_QUIETLY)
 
        MESSAGE(STATUS "Boost version: ${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION}.${Boost_SUBMINOR_VERSION}")
 
#         if(Boost_FIND_COMPONENTS)
 
#           message(STATUS "Found the following Boost libraries:")
 
#         endif()
 
      ENDIF(NOT Boost_FIND_QUIETLY)
 
      FOREACH ( COMPONENT  ${Boost_FIND_COMPONENTS} )
 
        STRING( TOUPPER ${COMPONENT} UPPERCOMPONENT )
 
        IF ( Boost_${UPPERCOMPONENT}_FOUND )
 
          IF (NOT Boost_FIND_QUIETLY)
 
#             MESSAGE (STATUS "  ${COMPONENT}")
 
          ENDIF(NOT Boost_FIND_QUIETLY)
 
          SET(Boost_LIBRARIES ${Boost_LIBRARIES} ${Boost_${UPPERCOMPONENT}_LIBRARY})
 
        ENDIF ( Boost_${UPPERCOMPONENT}_FOUND )
 
      ENDFOREACH(COMPONENT)
 
  else()
 
    if(Boost_FIND_REQUIRED)
 
      message(SEND_ERROR "Unable to find the requested Boost libraries.\n${Boost_ERROR_REASON}")
 
    else()
 
      if(NOT Boost_FIND_QUIETLY)
 
        # we opt not to automatically output Boost_ERROR_REASON here as
 
        # it could be quite lengthy and somewhat imposing in it's requests
 
        # Since Boost is not always a required dependency we'll leave this
 
        # up to the end-user.
 
        if(Boost_DEBUG OR Boost_DETAILED_FAILURE_MSG)
 
          message(STATUS "Could NOT find Boost\n${Boost_ERROR_REASON}")
 
        else()
 
          message(STATUS "Could NOT find Boost")
 
        endif()
 
      endif()
 
    endif(Boost_FIND_REQUIRED)
 
  endif()
 

	
 
  # show the Boost_INCLUDE_DIRS AND Boost_LIBRARIES variables only in the advanced view
 
  MARK_AS_ADVANCED(Boost_INCLUDE_DIR
 
      Boost_INCLUDE_DIRS
 
      Boost_LIBRARY_DIRS
 
  )
 
ENDIF(_boost_IN_CACHE)
 

	
 

	
 
if (Boost_FOUND)
 
	message( STATUS "Found Boost: ${Boost_LIBRARIES}, ${Boost_INCLUDE_DIR}")
 
endif(Boost_FOUND)
cmake_modules/yahoo2Config.cmake
Show inline comments
 
new file 100644
 
FIND_PATH(YAHOO2_INCLUDE_DIR yahoo2.h PATH_SUFFIXES libyahoo2)
 
MARK_AS_ADVANCED(YAHOO2_INCLUDE_DIR)
 

	
 
FIND_LIBRARY(YAHOO2_LIBRARY yahoo2 )
 
MARK_AS_ADVANCED(YAHOO2_LIBRARY)
 

	
 
if(YAHOO2_LIBRARY AND YAHOO2_INCLUDE_DIR)
 
	set( YAHOO2_FOUND 1 )
 
	message(STATUS "Found libyahoo2: ${YAHOO2_LIBRARY}, ${YAHOO2_INCLUDE_DIR}")
 
else()
 
	message(STATUS "Could NOT find libyahoo2 library")
 
endif()
include/Swiften/Parser/StringTreeParser.cpp
Show inline comments
 
new file 100644
 
/*
 
 * Copyright (c) 2011 Jan Kaluza
 
 * Licensed under the Simplified BSD license.
 
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 
 */
 

	
 
#include <Swiften/Parser/StringTreeParser.h>
 
#include <Swiften/Parser/PlatformXMLParserFactory.h>
 
#include <Swiften/Parser/Tree/ParserElement.h>
 
#include <Swiften/Parser/XMLParser.h>
 

	
 
namespace Swift {
 

	
 
class DefaultStringTreeParser : public StringTreeParser {
 
	public:
 
		void handleTree(ParserElement::ref root) {
 
			root_ = root;
 
		}
 

	
 
		ParserElement::ref getRoot() {
 
			return root_;
 
		}
 

	
 
	private:
 
		ParserElement::ref root_;
 
};
 
	
 
ParserElement::ref StringTreeParser::parse(const std::string &xml) {
 
	PlatformXMLParserFactory factory;
 
	DefaultStringTreeParser client;
 
	XMLParser *parser = factory.createXMLParser(&client);
 
	
 
	parser->parse(xml);
 
	ParserElement::ref root = client.getRoot();
 
	delete parser;
 
	return root;
 
}
 

	
 
}
include/Swiften/Parser/StringTreeParser.h
Show inline comments
 
new file 100644
 
/*
 
 * Copyright (c) 2011 Jan Kaluza
 
 * Licensed under the Simplified BSD license.
 
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 
 */
 

	
 
#pragma once
 

	
 
#include <deque>
 

	
 
#include <boost/shared_ptr.hpp>
 
#include <boost/smart_ptr/make_shared.hpp>
 

	
 
#include <Swiften/Parser/Tree/ParserElement.h>
 
#include <Swiften/Parser/XMLParserClient.h>
 

	
 
namespace Swift {
 
	/**
 
	 * Generics parser offering something a bit like a DOM to work with.
 
	 */
 
	class StringTreeParser : public XMLParserClient {
 
		public:
 
			StringTreeParser() : XMLParserClient() {}
 
			
 
			virtual void handleStartElement(const std::string& element, const std::string& xmlns, const AttributeMap& attributes) {
 
				if (!root_) {
 
					root_ = boost::make_shared<ParserElement>(element, xmlns, attributes);
 
					elementStack_.push_back(root_);
 
				}
 
				else {
 
					ParserElement::ref current = *elementStack_.rbegin();
 
					elementStack_.push_back(current->addChild(element, xmlns, attributes));
 
				}
 
			}
 

	
 
			virtual void handleEndElement(const std::string& /*element*/, const std::string&) {
 
				elementStack_.pop_back();
 
				if (elementStack_.empty()) {
 
					handleTree(root_);
 
				}
 
			}
 

	
 
			virtual void handleCharacterData(const std::string& data) {
 
				ParserElement::ref current = *elementStack_.rbegin();
 
				current->appendCharacterData(data);
 
			}
 

	
 
			virtual void handleTree(ParserElement::ref root) = 0;
 

	
 
			static ParserElement::ref parse(const std::string &xml);
 

	
 
		private:
 
			std::deque<ParserElement::ref> elementStack_;
 
			ParserElement::ref root_;
 
	};
 
}
include/Swiften/Server/ServerStanzaChannel.h
Show inline comments
 
/*
 
 * Copyright (c) 2010 Remko Tronçon
 
 * Licensed under the GNU General Public License v3.
 
 * See Documentation/Licenses/GPLv3.txt for more information.
 
 */
 

	
 
#pragma once
 

	
 
#include <boost/shared_ptr.hpp>
 

	
 
#include "Swiften/Base/IDGenerator.h"
 
#include "Swiften/Server/ServerFromClientSession.h"
 
#include "Swiften/Client/StanzaChannel.h"
 
#include "Swiften/Elements/Message.h"
 
#include "Swiften/Elements/IQ.h"
 
#include "Swiften/Elements/Presence.h"
 
#include "Swiften/TLS/Certificate.h"
 

	
 
namespace Swift {
 
	class Error;
 
	class ServerStanzaChannel : public StanzaChannel {
 
		public:
 
			void addSession(boost::shared_ptr<ServerFromClientSession> session);
 
			void removeSession(boost::shared_ptr<ServerFromClientSession> session);
 

	
 
			void sendIQ(boost::shared_ptr<IQ> iq);
 
			void sendMessage(boost::shared_ptr<Message> message);
 
			void sendPresence(boost::shared_ptr<Presence> presence);
 

	
 
			void finishSession(const JID& to, boost::shared_ptr<Element> element, bool last = false);
 

	
 
			bool getStreamManagementEnabled() const {
 
				return false;
 
			}
 
		        virtual std::vector<Certificate::ref> getPeerCertificateChain() const;
 
	
 
			bool isAvailable() const {
 
				return true;
 
			}
 
			
 
			std::vector<Certificate::ref> getPeerCertificateChain() const {
 
				return std::vector<Certificate::ref>();
 
			}
 

	
 
		private:
 
			std::string getNewIQID();
 
			void send(boost::shared_ptr<Stanza> stanza);
 
			void handleSessionFinished(const boost::optional<Session::SessionError>&, const boost::shared_ptr<ServerFromClientSession> &session);
 
			void handleElement(boost::shared_ptr<Element> element, const boost::shared_ptr<ServerFromClientSession> &session);
 
			void handleDataRead(const SafeByteArray &data, const boost::shared_ptr<ServerFromClientSession> &session);
 
			void handleSessionInitialized();
 

	
 
		private:
 
			IDGenerator idGenerator;
 
			// [JID][resources][ServerFromClientSession]
 
			std::map<std::string, std::list<boost::shared_ptr<ServerFromClientSession> > > sessions;
 
	};
 

	
 
}
include/transport/adhoccommand.h
Show inline comments
 
new file 100644
 
/**
 
 * XMPP - libpurple transport
 
 *
 
 * Copyright (C) 2009, Jan Kaluza <hanzz@soc.pidgin.im>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#pragma once
 

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

	
 
namespace Transport {
 

	
 
class Component;
 

	
 
class AdHocCommand {
 
	public:
 
		/// Creates new AdHocManager.
 

	
 
		/// \param component Transport instance associated with this AdHocManager.
 
		AdHocCommand(Component *component, const Swift::JID &initiator, const Swift::JID &to);
 

	
 
		/// Destructor.
 
		virtual ~AdHocCommand();
 

	
 
		virtual boost::shared_ptr<Swift::Command> handleRequest(boost::shared_ptr<Swift::Command> payload) = 0;
 

	
 
		void addFormField(Swift::FormField::ref field);
 

	
 
		const std::string &getId() {
 
			return m_id;
 
		}
 

	
 
		void refreshLastActivity() {
 
			m_lastActivity = time(NULL);
 
		}
 

	
 
		time_t getLastActivity() {
 
			return m_lastActivity;
 
		}
 

	
 
	protected:
 
		Component *m_component;
 
		Swift::JID m_initiator;
 
		Swift::JID m_to;
 
		std::vector<Swift::FormField::ref> m_fields;
 
		std::string m_id;
 

	
 
	private:
 
		// This is used to remove AdHocCommand after long inactivity to prevent memory leaks
 
		// caused by users which disconnect before they finish the command.
 
		// AdHocManager uses this to garbage collect old AdHocCommands.
 
		time_t m_lastActivity;
 
};
 

	
 
}
include/transport/adhoccommandfactory.h
Show inline comments
 
new file 100644
 
/**
 
 * XMPP - libpurple transport
 
 *
 
 * Copyright (C) 2009, Jan Kaluza <hanzz@soc.pidgin.im>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#pragma once
 

	
 
#include <string>
 
#include <algorithm>
 
#include <map>
 
#include "transport/adhoccommand.h"
 
#include "Swiften/Swiften.h"
 

	
 
namespace Transport {
 

	
 
class Component;
 

	
 
class AdHocCommandFactory {
 
	public:
 
		AdHocCommandFactory() {}
 

	
 
		/// Destructor.
 
		virtual ~AdHocCommandFactory() {}
 

	
 
		virtual AdHocCommand *createAdHocCommand(Component *component, const Swift::JID &initiator, const Swift::JID &to) = 0;
 

	
 
		virtual std::string getNode() = 0;
 

	
 
		virtual std::string getName() = 0;
 
};
 

	
 
}
include/transport/adhocmanager.h
Show inline comments
 
new file 100644
 
/**
 
 * XMPP - libpurple transport
 
 *
 
 * Copyright (C) 2009, Jan Kaluza <hanzz@soc.pidgin.im>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#pragma once
 

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

	
 
namespace Transport {
 

	
 
class Conversation;
 
class User;
 
class Component;
 
class DiscoItemsResponder;
 
class AdHocCommandFactory;
 
class AdHocCommand;
 

	
 
/// Listens for AdHoc commands and manages all AdHoc commands sessions
 
class AdHocManager : public Swift::Responder<Swift::Command> {
 
	public:
 
		typedef std::map<std::string, AdHocCommand *> CommandsMap;
 
		typedef std::map<Swift::JID, CommandsMap> SessionsMap;
 
		/// Creates new AdHocManager.
 

	
 
		/// \param component Transport instance associated with this AdHocManager.
 
		AdHocManager(Component *component, DiscoItemsResponder *discoItemsResponder);
 

	
 
		/// Destructor.
 
		virtual ~AdHocManager();
 

	
 
		/// Starts handling AdHoc commands payloads.
 
		void start();
 

	
 
		/// Stops handling AdHoc commands payloads and destroys all existing
 
		/// AdHoc commands sessions.
 
		void stop();
 

	
 
		/// Adds factory to create new AdHoc commands sessions of particular type.
 
		void addAdHocCommand(AdHocCommandFactory *factory);
 

	
 
		/// Remove sessions older than N seconds.
 
		void removeOldSessions();
 

	
 

	
 
	private:
 
		virtual bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::Command> payload);
 
		virtual bool handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::Command> payload);
 

	
 
		Component *m_component;
 
		DiscoItemsResponder *m_discoItemsResponder;
 
		std::map<std::string, AdHocCommandFactory *> m_factories;
 
		SessionsMap m_sessions;
 
		Swift::Timer::ref m_collectTimer;
 
};
 

	
 
}
include/transport/admininterface.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 "Swiften/Swiften.h"
 

	
 
namespace Transport {
 

	
 
class Component;
 
class StorageBackend;
 
class UserManager;
 
class NetworkPluginServer;
 
class UserRegistration;
 

	
 
class AdminInterface {
 
	public:
 
		AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server = NULL, StorageBackend *storageBackend = NULL);
 
		AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server = NULL, StorageBackend *storageBackend = NULL, UserRegistration *userRegistration = NULL);
 

	
 
		~AdminInterface();
 

	
 
		void handleQuery(Swift::Message::ref message);
 

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

	
 
		Component *m_component;
 
		StorageBackend *m_storageBackend;
 
		UserManager *m_userManager;
 
		NetworkPluginServer *m_server;
 
		UserRegistration *m_userRegistration;
 
		time_t m_start;
 
};
 

	
 
}
include/transport/config.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 <boost/program_options.hpp>
 
#include <boost/foreach.hpp>
 
#include <boost/format.hpp>
 
#include <boost/algorithm/string.hpp>
 
#include <boost/assign.hpp>
 
#include <boost/bind.hpp>
 
#include <boost/signal.hpp>
 

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

	
 

	
 
namespace Transport {
 

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

	
 
/// Represents config file.
 

	
 
/// It's used to load config file and allows others parts of libtransport to be configured
 
/// properly. Config files are text files which use "ini" format. Variables are divided into multiple
 
/// sections. Every class is configurable with some variables which change its behavior. Check particular
 
/// class documentation to get a list of all relevant variables for that class.
 
class Config {
 
	public:
 
		/// Constructor.
 
		Config(int argc = 0, char **argv = NULL) : m_argc(argc), m_argv(argv) {}
 

	
 
		/// Destructor
 
		virtual ~Config() {}
 

	
 
		/// Loads data from config file.
 
		
 
		/// You can pass your extra options which will be recognized by
 
		/// the parser using opts parameter.
 
		/// \param configfile path to config file
 
		/// \param opts extra options which will be recognized by a parser
 
		bool load(const std::string &configfile, boost::program_options::options_description &opts, const std::string &jid = "");
 

	
 
		bool load(std::istream &ifs, boost::program_options::options_description &opts, const std::string &jid = "");
 

	
 
		bool load(std::istream &ifs);
 

	
 
		/// Loads data from config file.
 
		
 
		/// This function loads only config variables needed by libtransport.
 
		/// \see load(const std::string &, boost::program_options::options_description &)
 
		/// \param configfile path to config file
 
		bool load(const std::string &configfile, const std::string &jid = "");
 

	
 
		bool reload();
 

	
 
		bool hasKey(const std::string &key) {
 
			return m_variables.find(key) != m_variables.end();
 
			return m_variables.find(key) != m_variables.end() || m_unregistered.find(key) != m_unregistered.end();
 
		}
 

	
 
		/// Returns value of variable defined by key.
 
		
 
		/// For variables in sections you can use "section.variable" key format.
 
		/// \param key config variable name
 
		const boost::program_options::variable_value &operator[] (const std::string &key) {
 
			return m_variables[key];
 
			if (m_variables.find(key) != m_variables.end()) {
 
				return m_variables[key];
 
			}
 
			return m_unregistered[key];
 
		}
 

	
 
		/// Returns path to config file from which data were loaded.
 
		const std::string &getConfigFile() { return m_file; }
 

	
 
		const std::map<std::string, std::string> &getUnregistered() {
 
			return m_unregistered;
 
		}
 

	
 
		/// This signal is emitted when config is loaded/reloaded.
 
		boost::signal<void ()> onConfigReloaded;
 
	
 
	private:
 
		int m_argc;
 
		char **m_argv;
 
		Variables m_variables;
 
		std::map<std::string, std::string> m_unregistered;
 
		std::map<std::string, boost::program_options::variable_value> m_unregistered;
 
		std::string m_file;
 
};
 

	
 
}
include/transport/discoitemsresponder.h
Show inline comments
 
file renamed from src/discoitemsresponder.h to include/transport/discoitemsresponder.h
 
/**
 
 * 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 <vector>
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Queries/GetResponder.h"
 
#include "Swiften/Elements/DiscoItems.h"
 

	
 
namespace Transport {
 

	
 
class Component;
 

	
 
class DiscoItemsResponder : public Swift::GetResponder<Swift::DiscoItems> {
 
	public:
 
		DiscoItemsResponder(Swift::IQRouter *router);
 
		DiscoItemsResponder(Component *component);
 
		~DiscoItemsResponder();
 

	
 
		void addAdHocCommand(const std::string &node, const std::string &name);
 
// 		void removeAdHocCommand(const std::string &node);
 

	
 

	
 
	private:
 
		virtual bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::DiscoItems> payload);
 

	
 
		Component *m_component;
 
		boost::shared_ptr<Swift::DiscoItems> m_commands;
 
};
 

	
 
}
 
\ No newline at end of file
include/transport/mysqlbackend.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
 

	
 
#ifdef WITH_MYSQL
 

	
 
#include <string>
 
#include <map>
 
#include "Swiften/Swiften.h"
 
#include "transport/storagebackend.h"
 
#include "transport/config.h"
 
#include "mysql.h"
 

	
 
namespace Transport {
 

	
 
/// Used to store transport data into SQLite3 database.
 
class MySQLBackend : public StorageBackend
 
{
 
	public:
 
		/// Creates new MySQLBackend instance.
 
		/// \param config cofiguration, this class uses following Config values:
 
		/// 	- database.database - path to SQLite3 database file, database file is created automatically
 
		/// 	- service.prefix - prefix for tables created by createDatabase method
 
		MySQLBackend(Config *config);
 

	
 
		/// Destructor.
 
		~MySQLBackend();
 

	
 
		/// Connects to the database and creates it if it's needed. This method call createDatabase() function
 
		/// automatically.
 
		/// \return true if database is opened successfully.
 
		bool connect();
 
		void disconnect();
 

	
 
		/// Creates database structure.
 
		/// \see connect()
 
		/// \return true if database structure has been created successfully. Note that it returns True also if database structure
 
		/// already exists.
 
		bool createDatabase();
 

	
 
		/// Stores user into database.
 
		/// \param user user struct containing all information about user which have to be stored
 
		void setUser(const UserInfo &user);
 

	
 
		/// Gets user data from database and stores them into user reference.
 
		/// \param barejid barejid of user
 
		/// \param user UserInfo object where user data will be stored
 
		/// \return true if user has been found in database
 
		bool getUser(const std::string &barejid, UserInfo &user);
 

	
 
		/// Changes users online state variable in database.
 
		/// \param id id of user - UserInfo.id
 
		/// \param online online state
 
		void setUserOnline(long id, bool online);
 

	
 
		/// Removes user and all connected data from database.
 
		/// \param id id of user - UserInfo.id
 
		/// \return true if user has been found in database and removed
 
		bool removeUser(long id);
 

	
 
		/// Returns JIDs of all buddies in user's roster.
 
		/// \param id id of user - UserInfo.id
 
		/// \param roster string list used to store user's roster
 
		/// \return true if user has been found in database and roster has been fetched
 
		bool getBuddies(long id, std::list<BuddyInfo> &roster);
 

	
 
		bool getOnlineUsers(std::vector<std::string> &users);
 

	
 
		long addBuddy(long userId, const BuddyInfo &buddyInfo);
 

	
 
		void updateBuddy(long userId, const BuddyInfo &buddyInfo);
 
		void removeBuddy(long id) {}
 

	
 
		void getBuddySetting(long userId, long buddyId, const std::string &variable, int &type, std::string &value) {}
 
		void updateBuddySetting(long userId, long buddyId, const std::string &variable, int type, const std::string &value) {}
 

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

	
 
		void beginTransaction();
 
		void commitTransaction();
 

	
 
	private:
 
		bool exec(const std::string &query);
 

	
 
		class Statement {
 
			public:
 
				Statement(MYSQL *conn, const std::string &format, const std::string &statement);
 
				~Statement();
 

	
 
				int execute();
 

	
 
				int fetch();
 

	
 
				// Pushes new data used as input for the statement.
 
				template <typename T>
 
				Statement& operator << (const T& t);
 

	
 
				Statement& operator << (const std::string& str);
 

	
 
				// Pulls fetched data by previous execute(); call.
 
				template <typename T>
 
				Statement& operator >> (T& t);
 
				
 
				Statement& operator >> (std::string& t);
 
			private:
 
				MYSQL_STMT *m_stmt;
 
				MYSQL *m_conn;
 
				std::vector<MYSQL_BIND> m_params;
 
				std::vector<MYSQL_BIND> m_results;
 
				int m_resultOffset;
 
				int m_offset;
 
				int m_error;
 
				std::string m_string;
 
		};
 

	
 
		MYSQL m_conn;
 
		Config *m_config;
 
		std::string m_prefix;
 

	
 
		// statements
 
// 		MYSQL_STMT *m_setUser;
 
		Statement *m_setUser;
 
		Statement *m_getUser;
 
		Statement *m_getUserSetting;
 
		Statement *m_setUserSetting;
 
		Statement *m_updateUserSetting;
 
		Statement *m_removeUser;
 
		Statement *m_removeUserBuddies;
 
		Statement *m_removeUserSettings;
 
		Statement *m_removeUserBuddiesSettings;
 
		Statement *m_addBuddy;
 
		Statement *m_updateBuddy;
 
		Statement *m_updateBuddySetting;
 
		Statement *m_getBuddies;
 
		Statement *m_getBuddiesSettings;
 
		Statement *m_setUserOnline;
 
		Statement *m_getOnlineUsers;
 
};
 

	
 
}
 

	
 
#endif
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 "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:
 

	
 
		class PluginConfig {
 
			public:
 
				PluginConfig() : m_needPassword(true) {}
 
				virtual ~PluginConfig() {}
 

	
 
				void setNeedPassword(bool needPassword = true) { m_needPassword = needPassword; }
 
				void setExtraFields(const std::vector<std::string> &fields) { m_extraFields = fields; }
 

	
 
			private:
 
				bool m_needPassword;
 
				std::vector<std::string> m_extraFields;
 

	
 
				friend class NetworkPlugin;
 
		};
 

	
 
		/// Creates new NetworkPlugin and connects the Spectrum2 NetworkPluginServer.
 
		/// \param loop Event loop.
 
		/// \param host Host where Spectrum2 NetworkPluginServer runs.
 
		/// \param port Port.
 
		NetworkPlugin();
 

	
 
		/// Destructor.
 
		virtual ~NetworkPlugin();
 

	
 
		void sendConfig(const PluginConfig &cfg);
 

	
 
		/// 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::vector<std::string> &groups, pbnetwork::StatusType status, const std::string &statusMessage = "", const std::string &iconHash = "",
 
			bool blocked = false
 
		);
 

	
 
		/// Call this method when buddy is removed from legacy network contact list.
 
		/// \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 handleBuddyRemoved(const std::string &user, const std::string &buddyName);
 

	
 
		/// 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, 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")
 
		/// \param buddyName Name of legacy network buddy. (eg. "user2@gmail.com")
 
		void handleBuddyTyped(const std::string &user, const std::string &buddyName);
 

	
 
		/// Call this function when buddy has been typing, but paused for a while.
 
		/// \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 handleBuddyStoppedTyping(const std::string &user, const std::string &buddyName);
 

	
 
		/// Call this function when new authorization request arrived form legacy network
 
		/// \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 handleAuthorization(const std::string &user, const std::string &buddyName);
 

	
 
		/// Call this function when attention request arrived from legacy network.
 
		/// \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 message Message.
 
		void handleAttention(const std::string &user, const std::string &buddyName, const std::string &message);
 

	
 
		void handleFTStart(const std::string &user, const std::string &buddyName, const std::string fileName, unsigned long size);
 
		void handleFTFinish(const std::string &user, const std::string &buddyName, const std::string fileName, unsigned long size, unsigned long ftid);
 

	
 
		void handleFTData(unsigned long ftID, const std::string &data);
 

	
 
		/// Called when XMPP user wants to connect legacy network.
 
		/// You should connect him to legacy network and call handleConnected or handleDisconnected function later.
 
		/// \param user XMPP JID of user for which this event occurs.
 
		/// \param legacyName Legacy network name of this user used for login.
 
		/// \param password Legacy network password of this user.
 
		/**
 
			\msc
 
			NetworkPlugin,YourNetworkPlugin,LegacyNetwork;
 
			NetworkPlugin->YourNetworkPlugin [label="handleLoginRequest(...)", URL="\ref NetworkPlugin::handleLoginRequest()"];
 
			YourNetworkPlugin->LegacyNetwork [label="connect the legacy network"];
 
			--- [label="If password was valid and user is connected and logged in"];
 
			YourNetworkPlugin<-LegacyNetwork [label="connected"];
 
			YourNetworkPlugin->NetworkPlugin [label="handleConnected()", URL="\ref NetworkPlugin::handleConnected()"];
 
			--- [label="else"];
 
			YourNetworkPlugin<-LegacyNetwork [label="disconnected"];
 
			YourNetworkPlugin->NetworkPlugin [label="handleDisconnected()", URL="\ref NetworkPlugin::handleDisconnected()"];
 
			\endmsc
 
		*/
 
		virtual void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) = 0;
 

	
 
		/// Called when XMPP user wants to disconnect legacy network.
 
		/// You should disconnect him from legacy network.
 
		/// \param user XMPP JID of user for which this event occurs.
 
		/// \param legacyName Legacy network name of this user used for login.
 
		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::vector<std::string> &/*groups*/) {}
 
		virtual void handleBuddyRemovedRequest(const std::string &/*user*/, const std::string &/*buddyName*/, const std::vector<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 handleMemoryUsage(double &res, double &shared) {res = 0; shared = 0;}
 

	
 
		virtual void handleExitRequest() { exit(1); }
 
		void handleDataRead(std::string &data);
 
		virtual void sendData(const std::string &string) {}
 

	
 
		void checkPing();
 

	
 
	private:
 
		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, 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 send(const std::string &data);
 
		void sendPong();
 
		void sendMemoryUsage();
 

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

	
 
};
 

	
 
}
include/transport/networkpluginserver.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/BoostConnectionServer.h"
 
#include "Swiften/Network/Connection.h"
 
#include "storagebackend.h"
 
#include "transport/filetransfermanager.h"
 

	
 
namespace Transport {
 

	
 
class UserManager;
 
class User;
 
class Component;
 
class Buddy;
 
class LocalBuddy;
 
class Config;
 
class NetworkConversation;
 
class VCardResponder;
 
class RosterResponder;
 
class BlockResponder;
 
class DummyReadBytestream;
 
class AdminInterface;
 

	
 
class NetworkPluginServer {
 
	public:
 
		struct Backend {
 
			int pongReceived;
 
			std::list<User *> users;
 
			Swift::SafeByteArray data;
 
			boost::shared_ptr<Swift::Connection> connection;
 
			unsigned long res;
 
			unsigned long init_res;
 
			unsigned long shared;
 
			bool acceptUsers;
 
			bool longRun;
 
			bool willDie;
 
			std::string id;
 
		};
 

	
 
		NetworkPluginServer(Component *component, Config *config, UserManager *userManager, FileTransferManager *ftManager);
 

	
 
		virtual ~NetworkPluginServer();
 

	
 
		void setAdminInterface(AdminInterface *adminInterface) {
 
			m_adminInterface = adminInterface;
 
		}
 

	
 
		int getBackendCount() {
 
			return m_clients.size();
 
		}
 

	
 
		const std::list<Backend *> &getBackends() {
 
			return m_clients;
 
		}
 

	
 
		const std::vector<std::string> &getCrashedBackends() {
 
			return m_crashedBackends;
 
		}
 

	
 
		void collectBackend();
 

	
 
		bool moveToLongRunBackend(User *user);
 

	
 
		void handleMessageReceived(NetworkConversation *conv, boost::shared_ptr<Swift::Message> &message);
 

	
 
	private:
 
		void handleNewClientConnection(boost::shared_ptr<Swift::Connection> c);
 
		void handleSessionFinished(Backend *c);
 
		void handlePongReceived(Backend *c);
 
		void handleDataRead(Backend *c, boost::shared_ptr<Swift::SafeByteArray> data);
 

	
 
		void handleConnectedPayload(const std::string &payload);
 
		void handleDisconnectedPayload(const std::string &payload);
 
		void handleBuddyChangedPayload(const std::string &payload);
 
		void handleBuddyRemovedPayload(const std::string &payload);
 
		void handleConvMessagePayload(const std::string &payload, bool subject = false);
 
		void handleParticipantChangedPayload(const std::string &payload);
 
		void handleRoomChangedPayload(const std::string &payload);
 
		void handleVCardPayload(const std::string &payload);
 
		void handleChatStatePayload(const std::string &payload, Swift::ChatState::ChatStateType type);
 
		void handleAuthorizationPayload(const std::string &payload);
 
		void handleAttentionPayload(const std::string &payload);
 
		void handleStatsPayload(Backend *c, const std::string &payload);
 
		void handleFTStartPayload(const std::string &payload);
 
		void handleFTFinishPayload(const std::string &payload);
 
		void handleFTDataPayload(Backend *b ,const std::string &payload);
 
		void handleFTDataPayload(Backend *b, const std::string &payload);
 
		void handleQueryPayload(Backend *b, const std::string &payload);
 

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

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

	
 
		void handleBlockToggled(Buddy *buddy);
 

	
 
		void handleVCardUpdated(User *user, boost::shared_ptr<Swift::VCard> vcard);
 
		void handleVCardRequired(User *user, const std::string &name, unsigned int id);
 

	
 
		void handleFTStateChanged(Swift::FileTransfer::State state, const std::string &userName, const std::string &buddyName, const std::string &fileName, unsigned long size, unsigned long id);
 
		void handleFTAccepted(User *user, const std::string &buddyName, const std::string &fileName, unsigned long size, unsigned long ftID);
 
		void handleFTRejected(User *user, const std::string &buddyName, const std::string &fileName, unsigned long size);
 
		void handleFTDataNeeded(Backend *b, unsigned long ftid);
 

	
 
		void send(boost::shared_ptr<Swift::Connection> &, const std::string &data);
 

	
 
		void pingTimeout();
 
		void sendPing(Backend *c);
 
		Backend *getFreeClient(bool acceptUsers = true, bool longRun = false);
 

	
 
		UserManager *m_userManager;
 
		VCardResponder *m_vcardResponder;
 
		RosterResponder *m_rosterResponder;
 
		BlockResponder *m_blockResponder;
 
		Config *m_config;
 
		boost::shared_ptr<Swift::ConnectionServer> m_server;
 
		std::list<Backend *>  m_clients;
 
		Swift::Timer::ref m_pingTimer;
 
		Swift::Timer::ref m_collectTimer;
 
		Component *m_component;
 
		std::list<User *> m_waitingUsers;
 
		bool m_isNextLongRun;
 
		std::map<unsigned long, FileTransferManager::Transfer> m_filetransfers;
 
		FileTransferManager *m_ftManager;
 
		std::vector<std::string> m_crashedBackends;
 
		AdminInterface *m_adminInterface;
 
		bool m_startingBackend;
 
};
 

	
 
}
include/transport/pqxxbackend.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
 

	
 
#ifdef WITH_PQXX
 

	
 
#include <string>
 
#include <map>
 
#include "Swiften/Swiften.h"
 
#include "transport/storagebackend.h"
 
#include "transport/config.h"
 
#include <pqxx/pqxx>
 

	
 
namespace Transport {
 

	
 
/// Used to store transport data into SQLite3 database.
 
class PQXXBackend : public StorageBackend
 
{
 
	public:
 
		/// Creates new PQXXBackend instance.
 
		/// \param config cofiguration, this class uses following Config values:
 
		/// 	- database.database - path to SQLite3 database file, database file is created automatically
 
		/// 	- service.prefix - prefix for tables created by createDatabase method
 
		PQXXBackend(Config *config);
 

	
 
		/// Destructor.
 
		~PQXXBackend();
 

	
 
		/// Connects to the database and creates it if it's needed. This method call createDatabase() function
 
		/// automatically.
 
		/// \return true if database is opened successfully.
 
		bool connect();
 
		void disconnect();
 

	
 
		/// Creates database structure.
 
		/// \see connect()
 
		/// \return true if database structure has been created successfully. Note that it returns True also if database structure
 
		/// already exists.
 
		bool createDatabase();
 

	
 
		/// Stores user into database.
 
		/// \param user user struct containing all information about user which have to be stored
 
		void setUser(const UserInfo &user);
 

	
 
		/// Gets user data from database and stores them into user reference.
 
		/// \param barejid barejid of user
 
		/// \param user UserInfo object where user data will be stored
 
		/// \return true if user has been found in database
 
		bool getUser(const std::string &barejid, UserInfo &user);
 

	
 
		/// Changes users online state variable in database.
 
		/// \param id id of user - UserInfo.id
 
		/// \param online online state
 
		void setUserOnline(long id, bool online);
 

	
 
		/// Removes user and all connected data from database.
 
		/// \param id id of user - UserInfo.id
 
		/// \return true if user has been found in database and removed
 
		bool removeUser(long id);
 

	
 
		/// Returns JIDs of all buddies in user's roster.
 
		/// \param id id of user - UserInfo.id
 
		/// \param roster string list used to store user's roster
 
		/// \return true if user has been found in database and roster has been fetched
 
		bool getBuddies(long id, std::list<BuddyInfo> &roster);
 

	
 
		bool getOnlineUsers(std::vector<std::string> &users);
 

	
 
		long addBuddy(long userId, const BuddyInfo &buddyInfo);
 

	
 
		void updateBuddy(long userId, const BuddyInfo &buddyInfo);
 
		void removeBuddy(long id) {}
 

	
 
		void getBuddySetting(long userId, long buddyId, const std::string &variable, int &type, std::string &value) {}
 
		void updateBuddySetting(long userId, long buddyId, const std::string &variable, int type, const std::string &value) {}
 

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

	
 
		void beginTransaction();
 
		void commitTransaction();
 

	
 
	private:
 
		bool exec(const std::string &query, bool show_error = true);
 
		bool exec(pqxx::nontransaction &txn, const std::string &query, bool show_error = true);
 
		template<typename T>
 
		std::string quote(pqxx::nontransaction &txn, const T &t);
 

	
 
		Config *m_config;
 
		std::string m_prefix;
 

	
 
		pqxx::connection *m_conn;
 
};
 

	
 
}
 

	
 
#endif
include/transport/protocol.proto
Show inline comments
 
package pbnetwork;
 

	
 
enum ConnectionError {
 
	CONNECTION_ERROR_NETWORK_ERROR = 0;
 
	CONNECTION_ERROR_INVALID_USERNAME = 1;
 
	CONNECTION_ERROR_AUTHENTICATION_FAILED = 2;
 
	CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE = 3;
 
	CONNECTION_ERROR_NO_SSL_SUPPORT = 4;
 
	CONNECTION_ERROR_ENCRYPTION_ERROR = 5;
 
	CONNECTION_ERROR_NAME_IN_USE = 6;
 
	CONNECTION_ERROR_INVALID_SETTINGS = 7;
 
	CONNECTION_ERROR_CERT_NOT_PROVIDED = 8;
 
	CONNECTION_ERROR_CERT_UNTRUSTED = 9;
 
	CONNECTION_ERROR_CERT_EXPIRED = 10;
 
	CONNECTION_ERROR_CERT_NOT_ACTIVATED = 11;
 
	CONNECTION_ERROR_CERT_HOSTNAME_MISMATCH = 12;
 
	CONNECTION_ERROR_CERT_FINGERPRINT_MISMATCH = 13;
 
	CONNECTION_ERROR_CERT_SELF_SIGNED = 14;
 
	CONNECTION_ERROR_CERT_OTHER_ERROR = 15;
 
	CONNECTION_ERROR_OTHER_ERROR = 16;
 
}
 

	
 
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;
 
	repeated string extraFields = 4;
 
}
 

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

	
 
message Buddy {
 
	required string userName = 1;
 
	required string buddyName = 2;
 
	optional string alias = 3;
 
	repeated string group = 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;
 
	required string id = 4;
 
}
 

	
 
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 BackendConfig {
 
	required string config = 1;
 
}
 

	
 
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;
 
		TYPE_BACKEND_CONFIG			= 30;
 
		TYPE_QUERY					= 31;
 
	}
 
	required Type type = 1;
 
	optional bytes payload = 2;
 
}
 
;
include/transport/rostermanager.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 <map>
 
#include <boost/pool/pool_alloc.hpp>
 
#include <boost/pool/object_pool.hpp>
 
#include "Swiften/Swiften.h"
 
// #include "rosterstorage.h"
 

	
 
namespace Transport {
 

	
 
class Buddy;
 
class User;
 
class Component;
 
class StorageBackend;
 
class RosterStorage;
 

	
 
// TODO: Once Swiften GetRosterRequest will support setting to="", this can be removed
 
class AddressedRosterRequest : public Swift::GenericRequest<Swift::RosterPayload> {
 
	public:
 
		typedef boost::shared_ptr<AddressedRosterRequest> ref;
 

	
 
		AddressedRosterRequest(Swift::IQRouter* router, Swift::JID to) :
 
				Swift::GenericRequest<Swift::RosterPayload>(Swift::IQ::Get, to, boost::shared_ptr<Swift::Payload>(new Swift::RosterPayload()), router) {
 
		}
 
};
 

	
 
/// Manages roster of one XMPP user.
 
class RosterManager {
 
	public:
 
		typedef std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > > BuddiesMap;
 
		/// Creates new RosterManager.
 
		/// \param user User associated with this RosterManager.
 
		/// \param component Transport instance associated with this roster.
 
		RosterManager(User *user, Component *component);
 

	
 
		/// Destructor.
 
		virtual ~RosterManager();
 

	
 
		/// Associates the buddy with this roster,
 
		/// and if the buddy is not already in XMPP user's server-side roster, the proper requests
 
		/// are sent to XMPP user (subscribe presences, Roster Item Exchange stanza or
 
		/// the buddy is added to server-side roster using remote-roster protoXEP).
 
		/// \param buddy Buddy
 
		void setBuddy(Buddy *buddy);
 

	
 
		/// Deassociates the buddy with this roster.
 
		/// \param buddy Buddy.
 
		void unsetBuddy(Buddy *buddy);
 

	
 
		/// Removes buddy from this roster, sends proper XML to XMPP side and deletes it.
 
		/// \param name Buddy name.
 
		void removeBuddy(const std::string &name);
 

	
 
		Buddy *getBuddy(const std::string &name);
 

	
 
		void setStorageBackend(StorageBackend *storageBackend);
 

	
 
		void storeBuddy(Buddy *buddy);
 

	
 
		Swift::RosterPayload::ref generateRosterPayload();
 

	
 
		/// Returns user associated with this roster.
 
		/// \return User
 
		User *getUser() { return m_user; }
 

	
 
		const BuddiesMap &getBuddies() {
 
			return m_buddies;
 
		}
 

	
 
		bool isRemoteRosterSupported() {
 
			return m_supportRemoteRoster;
 
		}
 

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

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

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

	
 
		void handleBuddyChanged(Buddy *buddy);
 

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

	
 
		void sendBuddyRosterPush(Buddy *buddy);
 

	
 
		void sendBuddyRosterRemove(Buddy *buddy);
 

	
 
		void sendBuddySubscribePresence(Buddy *buddy);
 
		
 
		void sendBuddyUnsubscribePresence(Buddy *buddy);
 

	
 
		void sendCurrentPresences(const Swift::JID &to);
 

	
 
		void sendCurrentPresence(const Swift::JID &from, const Swift::JID &to);
 

	
 
		void sendUnavailablePresences(const Swift::JID &to);
 

	
 
	private:
 
		void setBuddyCallback(Buddy *buddy);
 

	
 
		void sendRIE();
 
		void handleBuddyRosterPushResponse(Swift::ErrorPayload::ref error, Swift::SetRosterRequest::ref request, const std::string &key);
 
		void handleRemoteRosterResponse(boost::shared_ptr<Swift::RosterPayload> roster, Swift::ErrorPayload::ref error);
 

	
 
		std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > > m_buddies;
 
		Component *m_component;
 
		RosterStorage *m_rosterStorage;
 
		User *m_user;
 
		Swift::Timer::ref m_setBuddyTimer;
 
		Swift::Timer::ref m_RIETimer;
 
		std::list <Swift::SetRosterRequest::ref> m_requests;
 
		bool m_supportRemoteRoster;
 
		AddressedRosterRequest::ref m_remoteRosterRequest;
 
};
 

	
 
}
include/transport/rosterstorage.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 "Swiften/Swiften.h"
 

	
 
namespace Transport {
 

	
 
class User;
 
class StorageBackend;
 
class Buddy;
 

	
 
// Stores buddies into DB Backend.
 
class RosterStorage {
 
	public:
 
		RosterStorage(User *user, StorageBackend *storageBackend);
 
		virtual ~RosterStorage();
 

	
 
		// Add buddy to store queue and store it in future. Nothing
 
		// will happen if buddy is already added.
 
		void storeBuddy(Buddy *buddy);
 

	
 
		void removeBuddy(Buddy *buddy);
 

	
 
		// Store all buddies from queue immediately. Returns true
 
		// if some buddies were stored.
 
		bool storeBuddies();
 

	
 
		// Remove buddy from storage queue.
 
		void removeBuddyFromQueue(Buddy *buddy);
 

	
 
	private:
 
		User *m_user;
 
		StorageBackend *m_storageBackend;
 
		std::map<std::string, Buddy *> m_buddies;
 
		Swift::Timer::ref m_storageTimer;
 
};
 

	
 
}
include/transport/settingsadhoccommand.h
Show inline comments
 
new file 100644
 
/**
 
 * XMPP - libpurple transport
 
 *
 
 * Copyright (C) 2009, Jan Kaluza <hanzz@soc.pidgin.im>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#pragma once
 

	
 
#include <string>
 
#include <algorithm>
 
#include <map>
 
#include "Swiften/Swiften.h"
 
#include "transport/adhoccommand.h"
 
#include "transport/adhoccommandfactory.h"
 

	
 

	
 
namespace Transport {
 

	
 
class Component;
 

	
 
class SettingsAdHocCommand : public AdHocCommand {
 
	public:
 
		typedef enum { Init, WaitingForResponse } State;
 

	
 
		SettingsAdHocCommand(Component *component, const Swift::JID &initiator, const Swift::JID &to);
 

	
 
		/// Destructor.
 
		virtual ~SettingsAdHocCommand();
 

	
 
		virtual boost::shared_ptr<Swift::Command> handleRequest(boost::shared_ptr<Swift::Command> payload);
 

	
 
	private:
 
		boost::shared_ptr<Swift::Command> getForm();
 
		boost::shared_ptr<Swift::Command> handleResponse(boost::shared_ptr<Swift::Command> payload);
 
		State m_state;
 
};
 

	
 
class SettingsAdHocCommandFactory : public AdHocCommandFactory {
 
	public:
 
		SettingsAdHocCommandFactory() {}
 
		virtual ~SettingsAdHocCommandFactory() {}
 

	
 
		AdHocCommand *createAdHocCommand(Component *component, const Swift::JID &initiator, const Swift::JID &to) {
 
			return new SettingsAdHocCommand(component, initiator, to);
 
		}
 

	
 
		std::string getNode() {
 
			return "settings";
 
		}
 

	
 
		std::string getName() {
 
			return "Transport settings";
 
		}
 
};
 

	
 
}
include/transport/sqlite3backend.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
 

	
 
#ifdef WITH_SQLITE
 

	
 
#include <string>
 
#include <map>
 
#include "Swiften/Swiften.h"
 
#include "transport/storagebackend.h"
 
#include "transport/config.h"
 
#include "sqlite3.h"
 

	
 
namespace Transport {
 

	
 
/// Used to store transport data into SQLite3 database.
 
class SQLite3Backend : public StorageBackend
 
{
 
	public:
 
		/// Creates new SQLite3Backend instance.
 
		/// \param config cofiguration, this class uses following Config values:
 
		/// 	- database.database - path to SQLite3 database file, database file is created automatically
 
		/// 	- service.prefix - prefix for tables created by createDatabase method
 
		SQLite3Backend(Config *config);
 

	
 
		/// Destructor.
 
		~SQLite3Backend();
 

	
 
		/// Connects to the database and creates it if it's needed. This method call createDatabase() function
 
		/// automatically.
 
		/// \return true if database is opened successfully.
 
		bool connect();
 

	
 
		/// Creates database structure.
 
		/// \see connect()
 
		/// \return true if database structure has been created successfully. Note that it returns True also if database structure
 
		/// already exists.
 
		bool createDatabase();
 

	
 
		/// Stores user into database.
 
		/// \param user user struct containing all information about user which have to be stored
 
		void setUser(const UserInfo &user);
 

	
 
		/// Gets user data from database and stores them into user reference.
 
		/// \param barejid barejid of user
 
		/// \param user UserInfo object where user data will be stored
 
		/// \return true if user has been found in database
 
		bool getUser(const std::string &barejid, UserInfo &user);
 

	
 
		/// Changes users online state variable in database.
 
		/// \param id id of user - UserInfo.id
 
		/// \param online online state
 
		void setUserOnline(long id, bool online);
 

	
 
		bool getOnlineUsers(std::vector<std::string> &users);
 

	
 
		/// Removes user and all connected data from database.
 
		/// \param id id of user - UserInfo.id
 
		/// \return true if user has been found in database and removed
 
		bool removeUser(long id);
 

	
 
		/// Returns JIDs of all buddies in user's roster.
 
		/// \param id id of user - UserInfo.id
 
		/// \param roster string list used to store user's roster
 
		/// \return true if user has been found in database and roster has been fetched
 
		bool getBuddies(long id, std::list<BuddyInfo> &roster);
 

	
 
		long addBuddy(long userId, const BuddyInfo &buddyInfo);
 

	
 
		void updateBuddy(long userId, const BuddyInfo &buddyInfo);
 
		void removeBuddy(long id) {}
 
		void removeBuddy(long id);
 

	
 
		void getBuddySetting(long userId, long buddyId, const std::string &variable, int &type, std::string &value);
 
		void updateBuddySetting(long userId, long buddyId, const std::string &variable, int type, const std::string &value);
 

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

	
 
		void beginTransaction();
 
		void commitTransaction();
 

	
 
	private:
 
		bool exec(const std::string &query);
 

	
 
		sqlite3 *m_db;
 
		Config *m_config;
 
		std::string m_prefix;
 

	
 
		// statements
 
		sqlite3_stmt *m_setUser;
 
		sqlite3_stmt *m_getUser;
 
		sqlite3_stmt *m_getUserSetting;
 
		sqlite3_stmt *m_setUserSetting;
 
		sqlite3_stmt *m_updateUserSetting;
 
		sqlite3_stmt *m_removeUser;
 
		sqlite3_stmt *m_removeUserBuddies;
 
		sqlite3_stmt *m_removeUserSettings;
 
		sqlite3_stmt *m_removeUserBuddiesSettings;
 
		sqlite3_stmt *m_removeBuddy;
 
		sqlite3_stmt *m_removeBuddySettings;
 
		sqlite3_stmt *m_addBuddy;
 
		sqlite3_stmt *m_updateBuddy;
 
		sqlite3_stmt *m_updateBuddySetting;
 
		sqlite3_stmt *m_getBuddySetting;
 
		sqlite3_stmt *m_getBuddies;
 
		sqlite3_stmt *m_getBuddiesSettings;
 
		sqlite3_stmt *m_setUserOnline;
 
		sqlite3_stmt *m_getOnlineUsers;
 
};
 

	
 
}
 

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

	
 
class Config;
 

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

	
 
		static StorageBackend *createBackend(Config *config, std::string &error);
 

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

	
 
		/// getOnlineUsers
 
		virtual bool getOnlineUsers(std::vector<std::string> &users) = 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 getBuddySetting(long userId, long buddyId, const std::string &variable, int &type, std::string &value) = 0;
 
		virtual void updateBuddySetting(long userId, long buddyId, const std::string &variable, int type, const std::string &value) = 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;
 

	
 
};
 

	
 
}
include/transport/transport.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 <vector>
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/Server.h"
 
#include "Swiften/Disco/GetDiscoInfoRequest.h"
 
#include "Swiften/Disco/EntityCapsManager.h"
 
#include "Swiften/Disco/CapsManager.h"
 
#include "Swiften/Disco/CapsMemoryStorage.h"
 
#include "Swiften/Network/BoostTimerFactory.h"
 
#include "Swiften/Network/BoostIOServiceThread.h"
 
#include "Swiften/Server/UserRegistry.h"
 
#include "Swiften/Base/SafeByteArray.h"
 
#include "Swiften/Jingle/JingleSessionManager.h"
 

	
 
#include <boost/bind.hpp>
 
#include "transport/config.h"
 
#include "transport/factory.h"
 
#include "transport/presenceoracle.h"
 
#include <Swiften/Network/BoostConnectionServer.h>
 

	
 
namespace Transport {
 
	// typedef enum { 	CLIENT_FEATURE_ROSTERX = 2,
 
	// 				CLIENT_FEATURE_XHTML_IM = 4,
 
	// 				CLIENT_FEATURE_FILETRANSFER = 8,
 
	// 				CLIENT_FEATURE_CHATSTATES = 16
 
	// 				} SpectrumImportantFeatures;
 
	// 
 
	class StorageBackend;
 
	class DiscoInfoResponder;
 
	class DiscoItemsResponder;
 
	class Factory;
 
	class UserRegistry;
 

	
 
	/// Represents one transport instance.
 

	
 
	/// It's used to connect the Jabber server and provides transaction layer
 
	/// between Jabber server and other classes.
 
	///
 
	/// In server mode it represents Jabber server to which users can connect and use
 
	/// it as transport.
 
	class Component {
 
		public:
 
			/// Creates new Component instance.
 

	
 
			/// \param loop Main event loop.
 
			/// \param config Cofiguration; this class uses following Config values:
 
			/// 	- service.jid
 
			/// 	- service.password
 
			/// 	- service.server
 
			/// 	- service.port
 
			/// 	- service.server_mode
 
			/// \param factory Transport Abstract factory used to create basic transport structures.
 
			Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, Config *config, Factory *factory, Transport::UserRegistry *userRegistry = NULL);
 

	
 
			/// Component destructor.
 
			~Component();
 

	
 
			/// Returns Swift::StanzaChannel associated with this Transport::Component.
 

	
 
			/// It can be used to send presences and other stanzas.
 
			/// \return Swift::StanzaChannel associated with this Transport::Component.
 
			Swift::StanzaChannel *getStanzaChannel();
 

	
 
			Swift::CapsInfo &getBuddyCapsInfo();
 

	
 
			/// Returns Swift::IQRouter associated with this Component.
 

	
 
			/// \return Swift::IQRouter associated with this Component.
 
			Swift::IQRouter *getIQRouter() { return m_iqRouter; }
 

	
 
			/// Returns Swift::PresenceOracle associated with this Transport::Component.
 

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

	
 
			/// Returns True if the component is in server mode.
 

	
 
			/// \return True if the component is in server mode.
 
			bool inServerMode() { return m_server != NULL; }
 

	
 
			/// Connects the Jabber server.
 

	
 
			void start();
 
			void stop();
 

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

	
 
			/// Sets disco#info features which are sent as answer to disco#info IQ-get.
 
			
 
			/// This sets features of legacy network buddies (For example "me\40gmail.com@j2j.domain.tld").
 
			/// \param features list of features as sent in disco#info response
 
			void setBuddyFeatures(std::list<std::string> &features);
 

	
 
			/// Returns Jabber ID of this transport.
 

	
 
			/// \return Jabber ID of this transport
 
			Swift::JID &getJID() { return m_jid; }
 

	
 
			/// Returns Swift::NetworkFactories which can be used to create new connections.
 

	
 
			/// \return Swift::NetworkFactories which can be used to create new connections.
 
			Swift::NetworkFactories *getNetworkFactories() { return m_factories; }
 

	
 
			/// Returns Transport Factory used to create basic Transport components.
 

	
 
			/// \return Transport Factory used to create basic Transport components.
 
			Factory *getFactory() { return m_factory; }
 

	
 
			/// This signal is emitted when server disconnects the transport because of some error.
 

	
 
			/// \param error disconnection error
 
			boost::signal<void (const Swift::ComponentError &error)> onConnectionError;
 

	
 
			/// This signal is emitted when transport successfully connects the server.
 
			boost::signal<void ()> onConnected;
 

	
 
			/// This signal is emitted when XML stanza is sent to server.
 

	
 
			/// \param xml xml stanza
 
			boost::signal<void (const std::string &xml)> onXMLOut;
 

	
 
			/// This signal is emitted when XML stanza is received from server.
 

	
 
			/// \param xml xml stanza
 
			boost::signal<void (const std::string &xml)> onXMLIn;
 

	
 
			Config *getConfig() { return m_config; }
 

	
 
			/// This signal is emitted when presence from XMPP user is received.
 

	
 
			/// It's emitted only for presences addressed to transport itself
 
			/// (for example to="j2j.domain.tld").
 
			/// \param presence presence data
 
			boost::signal<void (Swift::Presence::ref presence)> onUserPresenceReceived;
 

	
 
			boost::signal<void (const Swift::JID& jid, boost::shared_ptr<Swift::DiscoInfo> info)> onUserDiscoInfoReceived;
 

	
 
// 			boost::signal<void (boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error, const Swift::JID& jid)> onDiscoInfoResponse;
 

	
 
		private:
 
			void handleConnected();
 
			void handleConnectionError(const Swift::ComponentError &error);
 
			void handleServerStopped(boost::optional<Swift::BoostConnectionServer::Error> e);
 
			void handlePresence(Swift::Presence::ref presence);
 
			void handleDataRead(const Swift::SafeByteArray &data);
 
			void handleDataWritten(const Swift::SafeByteArray &data);
 

	
 
			void handleDiscoInfoResponse(boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error, const Swift::JID& jid);
 
			void handleCapsChanged(const Swift::JID& jid);
 

	
 
			Swift::NetworkFactories *m_factories;
 
			Swift::Component *m_component;
 
			Swift::Server *m_server;
 
			Swift::Timer::ref m_reconnectTimer;
 
			Swift::EntityCapsManager *m_entityCapsManager;
 
			Swift::CapsManager *m_capsManager;
 
			Swift::CapsMemoryStorage *m_capsMemoryStorage;
 
			PresenceOracle *m_presenceOracle;
 
			Swift::StanzaChannel *m_stanzaChannel;
 
			Swift::IQRouter *m_iqRouter;
 
			
 
			Transport::UserRegistry *m_userRegistry;
 
			StorageBackend *m_storageBackend;
 
 			DiscoInfoResponder *m_discoInfoResponder;
 
			DiscoItemsResponder *m_discoItemsResponder;
 
			int m_reconnectCount;
 
			Config* m_config;
 
			std::string m_protocol;
 
			Swift::JID m_jid;
 
			Factory *m_factory;
 
			Swift::EventLoop *m_loop;
 

	
 
		friend class User;
 
		friend class UserRegistration;
 
		friend class NetworkPluginServer;
 
	};
 
}
munin/spectrum2_
Show inline comments
 
#!/usr/bin/perl
 

	
 
# config:
 
# [spectrum2_*]
 
# env.admin_jid tradmin@host.org
 
# env.password jid_password
 
# env.transports icq.host.org xmpp.host.org
 
#!/usr/bin/python -Wignore::DeprecationWarning
 
# -*- coding: utf-8 -*-
 
#
 
# Wildcard-plugin to monitor spectrum transport usage through an XMPP-connection
 
# sending Statistics Gathering (XEP-0039 [1]) packets. Depending on the suffix,
 
# the plugin monitors one specific characteristic of one or more spectrum
 
# instances.
 
#
 
# Current suffixes are:
 
#     spectrum_uptime (monitor uptime of transports)
 
#     spectrum_registered (how many users are registered to the transport)
 
#     spectrum_online (how many users are online)
 
#     spectarm_contacts_registered
 
#     spectrum_contacts_online (same as above, only for the legacy network)
 
#     spectrum_messages (how many messages have been sent over this transport)
 
#     spectrum_memory (how much memory the transport consumes)
 
#
 
# Configuration:
 
#     You need to configure this plugin (just like any other plugin) in
 
#	     plugin-conf.d/munin-node.
 
#     You have to configure the plugin to run as user and group "spectrum".
 
#
 
#     By default, the plugin monitors all instances configured in a config-file
 
#     in /etc/spectrum. If you use a different directory, you can specify the
 
#     environment-variable "base". If you do not want to monitor all instances,
 
#     you can give an explicit listing of the corresponding configuration files
 
#     with the environment variable "cfgs".
 
#     
 
#     Here is an example of a configuration. Note again that you can ommit both
 
#     env.cfgs and env.base:
 
#
 
#     [spectrum_*]
 
#     user spectrum
 
#     group spectrum
 
#     env.cfgs xmpp.example.com.cfg,irc.example.com.cfg
 
#     env.base /etc/spectrum
 
#
 
# Author:
 
#     Mathias Ertl <mati@fsinf.at>
 
# 
 
# Changelog:
 
#     2.0: Port to config_interface local socket
 
#     1.1: Suffixes that aggregate multiple values no longer show the individual
 
#     	values by default. This can be overridden by setting the "verbose"
 
#     	env-variable to any non-empty string.
 
#     1.0: Initial version
 
#
 
# symlinks: 
 
#    spectrum2_backends  spectrum2_memory  spectrum2_users spectrum2_messages spectrum2_messages_sec
 
# [1] http://xmpp.org/extensions/xep-0039.html
 
#
 
# Copyright (c) 2009 Mathias Ertl.
 
#
 
use AnyEvent;
 
use AnyEvent::XMPP::Client;
 
use AnyEvent::XMPP::Component;
 
use AnyEvent::XMPP::Ext::Disco;
 
use AnyEvent::XMPP::Ext::Version;
 
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
 
use AnyEvent::XMPP::Util qw/simxml/;
 
use XML::Simple;
 
use Time::HiRes qw ( setitimer ITIMER_REAL time );
 
use strict;
 
$|=1;
 

	
 
$SIG{ALRM} = sub { exit; };
 
setitimer(ITIMER_REAL, 30, 1);
 

	
 

	
 
my %config=(
 
    users => {
 
	title=>'Buddies online',
 
	vlabel=>'users',
 
	info=>'Number of users that currently use the spectrum transports.',
 
	command=>'online_users_count',
 
	base=>'--base 1000',
 
	x=>'1',
 
    },
 
    backends => {
 
	title=>'Backends running',
 
	vlabel=>'backends',
 
	info=>'Number of backends that currently running.',
 
	command=>'backends_count',
 
	base=>'--base 1000',
 
	x=>'1',
 
    },
 
    memory => {
 
	title=>'Memory usage of transports',
 
	vlabel=>'megabytes',
 
	info=>'Memory usage of spectrum transports.',
 
	command=>'used_memory',
 
	base=>'--base 1024',
 
	x=>'1024',
 
    },
 
    messages => {
 
	title=>'Messages send over transport',
 
	vlabel=>'messages',
 
	info=>'Messages send over spectrum transports.',
 
	command=>'',
 
	base=>'--base 1000',
 
	x=>'1',
 
    },
 
    messages_sec => {
 
	title=>'Messages send over transport',
 
	vlabel=>'messages/sec',
 
	info=>'Messages send per second over spectrum transports.',
 
	command=>'',
 
	base=>'--base 1000',
 
	x=>'1',
 
    },
 
);
 
my @queue=('_out','_in');
 
$0 =~ /spectrum2_(.+)*$/;
 
my $func = $1;
 
exit 2 unless defined $func;
 
my %tr;
 
my $count=0;
 
    foreach (split(' ',$ENV{'transports'})){
 
        if ($func=~/messages/)
 
	{
 
	    $tr{$_."_in"}=$count;
 
	    $count++;
 
	    $tr{$_."_out"}=$count;
 
	    $count++;
 
	}
 
	else
 
	{
 
	$tr{$_}=$count;
 
	$count++;
 
	}
 
    }
 

	
 
if (exists $ARGV[0] and $ARGV[0] eq "config") 
 
{
 
    print "graph_title ".$config{$func}->{'title'}."\n";
 
    print "graph_vlabel ".$config{$func}->{'vlabel'}."\n";
 
    print "graph_category spectrum2\n";
 
    foreach (keys (%tr)){
 
	print "r".$tr{$_}.".label ".$_."\n";
 
	if ($func eq 'messages_sec')
 
	{
 
	    print "r".$tr{$_}.".type DERIVE\n";
 
	    print "r".$tr{$_}.".min 0\n";
 
	}
 
    }
 
    print "graph_args ".$config{$func}->{'base'}."\n";
 
    print "graph_info ".$config{$func}->{'info'}."\n";
 
    exit 0;
 
}
 

	
 
binmode( STDOUT);
 
my $xs=new XML::Simple;
 
my $cl=AnyEvent::XMPP::Client->new(debug=>0);
 
my $j=AnyEvent->condvar;
 
$cl->add_account($ENV{'admin_jid'}.'/'.time,$ENV{'password'});
 
$cl->reg_cb(
 
    session_ready => \&cl_sess,
 
    disconnect => \&cl_disc,
 
    message => \&cl_msg,
 
);
 
$cl->start;
 
$j->wait;
 

	
 

	
 
sub cl_disc
 
{
 
my ($cl,$acc,$h,$p,$reas)=@_;
 
    print "disc ($h:$p) $reas\n";
 
}
 
sub cl_sess
 
{
 
    my($cl,$acc)=@_;
 
    foreach (keys (%tr)){
 
        if ($func=~/messages/)
 
	{
 
	    if (s/_in$//)
 
	    {
 
		$cl->send_message("messages_from_xmpp",$_,undef,'chat');
 
	    };
 
	    if (s/_out$//)
 
	    {
 
		$cl->send_message("messages_to_xmpp",$_,undef,'chat');
 
	    }
 
	}
 
	else
 
	{
 
	    $cl->send_message($config{$func}->{'command'},$_,undef,'chat');
 
	}
 
    }
 
}
 
sub cl_msg
 
{
 
    my ($cl,$acc,$msg)=@_;
 
    if ($func=~/messages/)
 
    {
 
	print "r".$tr{$msg->from.$queue[-1]}.".value ".int($msg->any_body/$config{$func}->{'x'})."\n";
 
	delete( $tr{$msg->from.$queue[-1]});
 
	pop(@queue);
 
	if ($#queue==-1){@queue=("_out","_in");}
 
    }
 
    else
 
    {
 
	print "r".$tr{$msg->from}.".value ".int($msg->any_body/$config{$func}->{'x'})."\n";
 
	delete( $tr{$msg->from});
 
    }
 
    exit if (scalar(keys %tr)==0);
 
}
 
# Permission to use, copy, and modify this software with or without fee
 
# is hereby granted, provided that this entire notice is included in
 
# all source code copies of any software which is or includes a copy or
 
# modification of this software.
 
#
 
# THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
 
# IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
 
# REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
 
# MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
 
# PURPOSE.
 
#
 
# Magic markers
 
#%# family=auto
 
#%# capabilities=autoconf suggest
 

	
 
import sys
 
from subprocess import *
 

	
 
# autoconf and suggest handling:
 
if len( sys.argv ) > 1:
 
	if sys.argv[1] == 'autoconf':
 
		print( 'yes' )
 
		sys.exit( 0 )
 
	elif sys.argv[1] == 'suggest':
 
		print( """uptime
 
registered
 
online
 
contacts_total
 
contacts_online
 
messages
 
messages_sec
 
memory""" )
 
		sys.exit(0)
 

	
 
import os, re
 

	
 
# filter forbidden characters for munin fieldnames
 
def handle_field( string ):
 
	for regexp in [ '^[^A-Za-z_]', '[^A-Za-z0-9_]' ]:
 
		string = re.compile( regexp ).sub( '_', string )
 
	return string
 

	
 
# get runtime variables
 
suffix = sys.argv[0].partition('_')[2]
 
verbose = os.environ.get( 'verbose' )
 
proc = Popen(['spectrum2_manager', 'list'], stdout=PIPE, stderr=PIPE)
 
out, err = proc.communicate()
 
jids = out.split('\n')[:-1]
 

	
 
# set variables based on wildcard 
 
if suffix == 'uptime':
 
	stat = { 'uptime': None }
 
	title = "Uptime"
 
	vlabel = "days"
 
	info = ''
 
	transformer = lambda value: float(value)/60.0/60.0/24.0
 
elif suffix == 'backends_count':
 
	stat = { "backends_count": None }
 
	title = "Backends count"
 
	vlabel = "backends"
 
	info = 'Total number of backends.'
 
elif suffix == 'online':
 
	stat = { 'online_users_count': None }
 
	title = "Online users"
 
	vlabel = "users"
 
	info = 'Number of users that currently use the spectrum transports.'
 
elif suffix == 'messages':
 
	stat = { 'messages_from_xmpp': 'from_xmpp', 'messages_to_xmpp': 'to_xmpp' }
 
	title = "Messages send over transport"
 
	vlabel = "messages"
 
	info = 'Total messages send over spectrum since the last restart.'
 
elif suffix == 'messages_sec':
 
	stat = { 'messages_from_xmpp': 'from_xmpp', 'messages_to_xmpp': 'to_xmpp' }
 
	title = "Messages send over transport per second"
 
	vlabel = "messages/sec"
 
	info = 'Messages send per second over spectrum transports.'
 
elif suffix == 'memory':
 
	stat = { 'used_memory': None }
 
	title = "Memory usage of transports"
 
	vlabel = "megabytes"
 
	transformer = lambda value: float(value)/1024.0
 
	info = 'Memory usage of spectrum transports.'
 
elif suffix == 'average_memory_per_user':
 
	stat = { 'average_memory_per_user': None }
 
	title = "Average memory usage per user"
 
	vlabel = "kilobytes"
 
	#transformer = lambda value: float(value)/1024.0
 
	info = 'Memory usage of spectrum transports.'
 

	
 
# handle config
 
if len( sys.argv ) > 1 and sys.argv[1] == 'config':
 
	print( """graph_title %s
 
graph_args --base 1000 -l 0
 
graph_scale no
 
graph_vlabel %s
 
graph_category spectrum2
 
graph_info %s""" %(title, vlabel, info) )
 
	for jid in jids:
 
		if len(stat) > 1: 
 
			# plugin monitors more than one field
 
			label = jid + ' total'
 
			fieldname = handle_field( label )
 
			print( '%s.label %s' %(fieldname, label) )
 
			if suffix == 'messages_sec':
 
				print( '%s.type DERIVE' %(fieldname) )
 
				print( '%s.min 0' %(fieldname) )
 

	
 
			# to not print individual fields if verbose is not set:
 
			if not verbose: 
 
				continue
 

	
 
		for name, field_suffix in stat.iteritems():
 
			label = jid
 
			if field_suffix:
 
				label += ' ' + field_suffix
 
			fieldname = handle_field( label )
 
			print( '%s.label %s' %(fieldname, label) )
 
			if suffix == 'messages_sec':
 
				print( '%s.type DERIVE' %(fieldname) )
 
				print( '%s.min 0' %(fieldname) )
 
	sys.exit(0)
 

	
 
# callback to handle incoming packets
 
def handler_fetch( packet ):
 
	jid = str( packet.getFrom() )
 
	total = None
 

	
 
	for child in packet.getChildren()[0].getChildren():
 
		label = jid
 
		value = child.getAttr( 'value' )
 
		if len( stat ) > 1:
 
			if total == None:
 
				total = int( value )
 
			else:
 
				total += int( value )
 
			if not verbose:
 
				continue
 
		
 
		field_suffix = stat[ child.getAttr( 'name' ) ]
 
		if field_suffix:
 
			label += ' ' + field_suffix
 
		fieldname = handle_field( label )
 
		if 'transformer' in globals():
 
			value = transformer(value)
 

	
 
		print( '%s.value %s' %(fieldname, value) )
 

	
 
	if total != None:
 
		fieldname = handle_field( jid + ' total' )
 
		if 'transformer' in globals():
 
			total = transformer( total )
 
		print( '%s.value %s' %(fieldname, total) )
 

	
 
for jid in jids:
 
	total = None
 
	label = jid
 
	for name in stat.keys():
 
		proc = Popen(['spectrum2_manager', jid, name], stdout=PIPE, stderr=PIPE)
 
		out, err = proc.communicate()
 
		out = out.replace('\n', '')
 
		value = 0
 
		try:
 
			value = int(out)
 
		except:
 
			print( "Error: %s" % (value))
 
			continue
 

	
 
		if len( stat ) > 1:
 
			if total == None:
 
				total = int( value )
 
			else:
 
				total += int( value )
 
			if not verbose:
 
				continue
 
		
 
		field_suffix = stat[ name ]
 
		if field_suffix:
 
			label += ' ' + field_suffix
 
		fieldname = handle_field( label )
 
		if 'transformer' in globals():
 
			value = transformer(value)
 

	
 
		print( '%s.value %s' %(fieldname, value) )
 

	
 
	if total != None:
 
		fieldname = handle_field( jid + ' total' )
 
		if 'transformer' in globals():
 
			total = transformer( total )
 
		print( '%s.value %s' %(fieldname, total) )
plugin/cpp/networkplugin.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/networkplugin.h"
 
#include "transport/memoryusage.h"
 
#include "transport/logging.h"
 

	
 
#include <sstream>
 

	
 
#ifndef WIN32
 
#include <arpa/inet.h>
 
#include <sys/types.h>
 
#include <unistd.h>
 
#else 
 
#include <winsock2.h>
 
#include <stdint.h>
 
#include <process.h>
 
#define getpid _getpid
 
#endif
 

	
 
DEFINE_LOGGER(logger, "NetworkPlugin");
 

	
 
namespace Transport {
 

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

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

	
 
NetworkPlugin::NetworkPlugin() {
 
	m_pingReceived = false;
 

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

	
 
NetworkPlugin::~NetworkPlugin() {
 
}
 

	
 
void NetworkPlugin::sendConfig(const PluginConfig &cfg) {
 
	std::string data = "[registration]";
 
	data += std::string("needPassword=") + (cfg.m_needPassword ? "1" : "0") + "\n";
 

	
 
	for (std::vector<std::string>::const_iterator it = cfg.m_extraFields.begin(); it != cfg.m_extraFields.end(); it++) {
 
		data += std::string("extraField=") + (*it) + "\n";
 
	}
 

	
 
	pbnetwork::BackendConfig m;
 
	m.set_config(data);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BACKEND_CONFIG);
 

	
 
	send(message);
 
}
 

	
 
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::vector<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);
 
	for (std::vector<std::string>::const_iterator it = groups.begin(); it != groups.end(); it++) {
 
		buddy.add_group(*it);
 
	}
 
	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::handleBuddyRemoved(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_REMOVED);
 

	
 
	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) {
 
	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, 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::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());
 
}
 

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

	
 
	handleFTStartRequest(payload.username(), payload.buddyname(), payload.filename(), payload.size(), payload.ftid());
 
}
 

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

	
 
	handleFTFinishRequest(payload.username(), payload.buddyname(), payload.filename(), payload.size(), payload.ftid());
 
}
 

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

	
 
	handleFTPauseRequest(payload.ftid());
 
}
 

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

	
 
	handleFTContinueRequest(payload.ftid());
 
}
 

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

	
 
	handleJoinRoomRequest(payload.username(), payload.room(), payload.nickname(), payload.password());
 
}
 

	
 
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 {
 
		std::vector<std::string> groups;
 
		for (int i = 0; i < payload.group_size(); i++) {
 
			groups.push_back(payload.group(i));
 
		}
 
		handleBuddyUpdatedRequest(payload.username(), payload.buddyname(), payload.alias(), groups);
 
	}
 
}
 

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

	
 
	std::vector<std::string> groups;
 
	for (int i = 0; i < payload.group_size(); i++) {
 
		groups.push_back(payload.group(i));
 
	}
 

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

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

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

	
 
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:
 
				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(), pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPING);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPED:
 
				handleChatStatePayload(wrapper.payload(), pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPED);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_STOPPED_TYPING:
 
				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) {
 
	uint32_t size = htonl(data.size());
 
	char *header = (char *) &size;
 
	sendData(std::string(header, 4) + data);
 
}
 

	
 
void NetworkPlugin::checkPing() {
 
	if (m_pingReceived == false) {
 
		handleExitRequest();
 
	}
 
	m_pingReceived = false;
 
}
 

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

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

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

	
 
	double e_res;
 
	double e_shared;
 
	handleMemoryUsage(e_res, e_shared);
 

	
 
	stats.set_res(res + e_res);
 
	stats.set_shared(shared + e_shared);
 
	stats.set_id(stringOf(getpid()));
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_STATS);
 

	
 
	send(message);
 
}
 

	
 
}
spectrum/src/main.cpp
Show inline comments
 
#include "transport/config.h"
 
#include "transport/transport.h"
 
#include "transport/filetransfermanager.h"
 
#include "transport/usermanager.h"
 
#include "transport/logger.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/mysqlbackend.h"
 
#include "transport/pqxxbackend.h"
 
#include "transport/userregistration.h"
 
#include "transport/networkpluginserver.h"
 
#include "transport/admininterface.h"
 
#include "transport/statsresponder.h"
 
#include "transport/usersreconnecter.h"
 
#include "transport/util.h"
 
#include "transport/gatewayresponder.h"
 
#include "transport/logging.h"
 
#include "transport/discoitemsresponder.h"
 
#include "transport/adhocmanager.h"
 
#include "transport/settingsadhoccommand.h"
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 
#include <boost/filesystem.hpp>
 
#include <boost/algorithm/string.hpp>
 
#ifndef WIN32
 
#include "sys/signal.h"
 
#include <pwd.h>
 
#include <grp.h>
 
#include <sys/resource.h>
 
#include "libgen.h"
 
#else
 
#include <process.h>
 
#define getpid _getpid
 
#include "win32/SpectrumService.h"
 
#endif
 
#include <sys/stat.h>
 

	
 
using namespace Transport;
 

	
 
DEFINE_LOGGER(logger, "Spectrum");
 

	
 
Swift::SimpleEventLoop *eventLoop_ = NULL;
 
Component *component_ = NULL;
 
UserManager *userManager_ = NULL;
 

	
 
static void stop_spectrum() {
 
	userManager_->removeAllUsers(false);
 
	component_->stop();
 
	eventLoop_->stop();
 
}
 

	
 
static void spectrum_sigint_handler(int sig) {
 
	eventLoop_->postEvent(&stop_spectrum);
 
}
 

	
 
static void spectrum_sigterm_handler(int sig) {
 
	eventLoop_->postEvent(&stop_spectrum);
 
}
 

	
 
static void removeOldIcons(std::string iconDir) {
 
	std::vector<std::string> dirs;
 
	dirs.push_back(iconDir);
 

	
 
	boost::thread thread(boost::bind(Util::removeEverythingOlderThan, dirs, time(NULL) - 3600*24*14));
 
}
 

	
 
#ifndef WIN32
 
static void daemonize(const char *cwd, const char *lock_file) {
 
	pid_t pid, sid;
 
	FILE* lock_file_f;
 
	char process_pid[20];
 
	/* already a daemon */
 
	if ( getppid() == 1 ) return;
 

	
 
	/* Fork off the parent process */
 
	pid = fork();
 
	if (pid < 0) {
 
		exit(1);
 
	}
 
	/* If we got a good PID, then we can exit the parent process. */
 
	if (pid > 0) {
 
		if (lock_file) {
 
			/* write our pid into it & close the file. */
 
			lock_file_f = fopen(lock_file, "w+");
 
			if (lock_file_f == NULL) {
 
				std::cerr << "Cannot create lock file " << lock_file << ". Exiting\n";
 
				exit(1);
 
			}
 
			sprintf(process_pid,"%d\n",pid);
 
			if (fwrite(process_pid,1,strlen(process_pid),lock_file_f) < strlen(process_pid)) {
 
				std::cerr << "Cannot write to lock file " << lock_file << ". Exiting\n";
 
				exit(1);
 
			}
 
			fclose(lock_file_f);
 
		}
 
		exit(0);
 
	}
 

	
 
	/* Change the file mode mask */
 
	umask(0);
 

	
 
	/* Create a new SID for the child process */
 
	sid = setsid();
 
	if (sid < 0) {
 
		exit(1);
 
	}
 

	
 
	/* Change the current working directory.  This prevents the current
 
		directory from being locked; hence not being able to remove it. */
 
	if ((chdir(cwd)) < 0) {
 
		exit(1);
 
	}
 
	
 
	if (freopen( "/dev/null", "r", stdin) == NULL) {
 
		std::cout << "EE cannot open /dev/null. Exiting\n";
 
		exit(1);
 
	}
 
}
 

	
 
#endif
 

	
 
int main(int argc, char **argv)
 
{
 
	Config config(argc, argv);
 

	
 
	boost::program_options::variables_map vm;
 
	bool no_daemon = false;
 
	std::string config_file;
 
	std::string jid;
 
	
 

	
 
	setlocale(LC_ALL, "");
 

	
 
#ifndef WIN32
 
	if (signal(SIGINT, spectrum_sigint_handler) == SIG_ERR) {
 
		std::cout << "SIGINT handler can't be set\n";
 
		return -1;
 
	}
 

	
 
	if (signal(SIGTERM, spectrum_sigterm_handler) == SIG_ERR) {
 
		std::cout << "SIGTERM handler can't be set\n";
 
		return -1;
 
	}
 
#endif
 
	boost::program_options::options_description desc(std::string("Spectrum version: ") + SPECTRUM_VERSION + "\nUsage: spectrum [OPTIONS] <config_file.cfg>\nAllowed options");
 
	desc.add_options()
 
		("help,h", "help")
 
		("no-daemonize,n", "Do not run spectrum as daemon")
 
		("no-debug,d", "Create coredumps on crash")
 
		("jid,j", boost::program_options::value<std::string>(&jid)->default_value(""), "Specify JID of transport manually")
 
		("config", boost::program_options::value<std::string>(&config_file)->default_value(""), "Config file")
 
		("version,v", "Shows Spectrum version")
 
		;
 
#ifdef WIN32
 
	desc.add_options()
 
		("install-service,i", "Install spectrum as Windows service")
 
		("uninstall-service,u", "Uninstall Windows service");
 
#endif
 
	try
 
	{
 
		boost::program_options::positional_options_description p;
 
		p.add("config", -1);
 
		boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
 
          options(desc).positional(p).allow_unregistered().run(), vm);
 
		boost::program_options::notify(vm);
 

	
 
		if (vm.count("version")) {
 
			std::cout << SPECTRUM_VERSION << "\n";
 
			return 0;
 
		}
 

	
 
		if(vm.count("help"))
 
		{
 
			std::cout << desc << "\n";
 
			return 1;
 
		}
 

	
 
		if(vm.count("config") == 0) {
 
			std::cout << desc << "\n";
 
			return 1;
 
		}
 

	
 
		if(vm.count("no-daemonize")) {
 
			no_daemon = true;
 
		}
 
#ifdef WIN32
 
		if (vm.count("install-service")) {
 
			SpectrumService ntservice;
 
			if (!ntservice.IsInstalled()) {
 
					// determine the name of the currently executing file
 
				char szFilePath[MAX_PATH];
 
				GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));
 
				std::string exe_file(szFilePath);
 
				std::string config_file = exe_file.replace(exe_file.end() - 4, exe_file.end(), ".cfg");
 
				std::string service_path = std::string(szFilePath) + std::string(" --config ") + config_file;
 

	
 
				if (ntservice.Install(service_path.c_str())) {
 
					std::cout << "Successfully installed" << std::endl;
 
					return 0;
 
				} else {
 
					std::cout << "Error installing service, are you an Administrator?" << std::endl;
 
					return 1;
 
				}                				
 
			} else {
 
				std::cout << "Already installed" << std::endl;
 
				return 1;
 
			}
 
		}
 
		if (vm.count("uninstall-service")) {
 
			SpectrumService ntservice;
 
			if (ntservice.IsInstalled()) {
 
				if (ntservice.Remove()) {
 
					std::cout << "Successfully removed" << std::endl;
 
					return 0;
 
				} else {
 
					std::cout << "Error removing service, are you an Administrator?" << std::endl;
 
					return 1;
 
				}                               				
 
			} else {
 
				std::cout << "Service not installed" << std::endl;
 
				return 1;
 
			}
 
		}
 

	
 
#endif
 
	}
 
	catch (std::runtime_error& e)
 
	{
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 
	catch (...)
 
	{
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 

	
 
	if (!config.load(vm["config"].as<std::string>(), jid)) {
 
		std::cerr << "Can't load configuration file.\n";
 
		return 1;
 
	}
 

	
 
	// create directories
 
	try {
 
		boost::filesystem::create_directories(
 
			boost::filesystem::path(CONFIG_STRING(&config, "service.pidfile")).parent_path().string()
 
		);
 
	}
 
	catch (...) {
 
		std::cerr << "Can't create service.pidfile directory " << boost::filesystem::path(CONFIG_STRING(&config, "service.pidfile")).parent_path().string() << ".\n";
 
		return 1;
 
	}
 
	// create directories
 
	try {
 
		boost::filesystem::create_directories(CONFIG_STRING(&config, "service.working_dir"));
 
	}
 
	catch (...) {
 
		std::cerr << "Can't create service.working_dir directory " << CONFIG_STRING(&config, "service.working_dir") << ".\n";
 
		return 1;
 
	}
 
	// create directories
 
	try {
 
		boost::filesystem::create_directories(
 
			boost::filesystem::path(CONFIG_STRING(&config, "service.portfile")).parent_path().string()
 
		);
 
	}
 
	catch (...) {
 
		std::cerr << "Can't create service.portfile directory " << CONFIG_STRING(&config, "service.portfile") << ".\n";
 
		return 1;
 
	}
 

	
 
#ifndef WIN32
 
	if (!CONFIG_STRING(&config, "service.group").empty() ||!CONFIG_STRING(&config, "service.user").empty() ) {
 
		struct group *gr;
 
		if ((gr = getgrnam(CONFIG_STRING(&config, "service.group").c_str())) == NULL) {
 
			std::cerr << "Invalid service.group name " << CONFIG_STRING(&config, "service.group") << "\n";
 
			return 1;
 
		}
 
		struct passwd *pw;
 
		if ((pw = getpwnam(CONFIG_STRING(&config, "service.user").c_str())) == NULL) {
 
			std::cerr << "Invalid service.user name " << CONFIG_STRING(&config, "service.user") << "\n";
 
			return 1;
 
		}
 
		chown(CONFIG_STRING(&config, "service.working_dir").c_str(), pw->pw_uid, gr->gr_gid);
 
	}
 

	
 
	char backendport[20];
 
	FILE* port_file_f;
 
	port_file_f = fopen(CONFIG_STRING(&config, "service.portfile").c_str(), "w+");
 
	if (port_file_f == NULL) {
 
		std::cerr << "Cannot create port_file file " << CONFIG_STRING(&config, "service.portfile").c_str() << ". Exiting\n";
 
		exit(1);
 
	}
 
	sprintf(backendport,"%s\n",CONFIG_STRING(&config, "service.backend_port").c_str());
 
	if (fwrite(backendport,1,strlen(backendport),port_file_f) < strlen(backendport)) {
 
		std::cerr << "Cannot write to port file " << CONFIG_STRING(&config, "service.portfile") << ". Exiting\n";
 
		exit(1);
 
	}
 
	fclose(port_file_f);
 

	
 
	if (!no_daemon) {
 
		// daemonize
 
		daemonize(CONFIG_STRING(&config, "service.working_dir").c_str(), CONFIG_STRING(&config, "service.pidfile").c_str());
 
// 		removeOldIcons(CONFIG_STRING(&config, "service.working_dir") + "/icons");
 
    }
 
#endif
 

	
 
	Logging::initMainLogging(&config);
 

	
 
#ifndef WIN32
 
	if (!CONFIG_STRING(&config, "service.group").empty() ||!CONFIG_STRING(&config, "service.user").empty() ) {
 
		struct rlimit limit;
 
		getrlimit(RLIMIT_CORE, &limit);
 

	
 
		if (!CONFIG_STRING(&config, "service.group").empty()) {
 
			struct group *gr;
 
			if ((gr = getgrnam(CONFIG_STRING(&config, "service.group").c_str())) == NULL) {
 
				std::cerr << "Invalid service.group name " << CONFIG_STRING(&config, "service.group") << "\n";
 
				return 1;
 
			}
 

	
 
			if (((setgid(gr->gr_gid)) != 0) || (initgroups(CONFIG_STRING(&config, "service.user").c_str(), gr->gr_gid) != 0)) {
 
				std::cerr << "Failed to set service.group name " << CONFIG_STRING(&config, "service.group") << " - " << gr->gr_gid << ":" << strerror(errno) << "\n";
 
				return 1;
 
			}
 
		}
 

	
 
		if (!CONFIG_STRING(&config, "service.user").empty()) {
 
			struct passwd *pw;
 
			if ((pw = getpwnam(CONFIG_STRING(&config, "service.user").c_str())) == NULL) {
 
				std::cerr << "Invalid service.user name " << CONFIG_STRING(&config, "service.user") << "\n";
 
				return 1;
 
			}
 

	
 
			if ((setuid(pw->pw_uid)) != 0) {
 
				std::cerr << "Failed to set service.user name " << CONFIG_STRING(&config, "service.user") << " - " << pw->pw_uid << ":" << strerror(errno) << "\n";
 
				return 1;
 
			}
 
		}
 
		setrlimit(RLIMIT_CORE, &limit);
 
	}
 

	
 
	struct rlimit limit;
 
	limit.rlim_max = RLIM_INFINITY;
 
	limit.rlim_cur = RLIM_INFINITY;
 
	setrlimit(RLIMIT_CORE, &limit);
 
#endif
 

	
 
	Swift::SimpleEventLoop eventLoop;
 

	
 
	Swift::BoostNetworkFactories *factories = new Swift::BoostNetworkFactories(&eventLoop);
 
	UserRegistry userRegistry(&config, factories);
 

	
 
	Component transport(&eventLoop, factories, &config, NULL, &userRegistry);
 
	component_ = &transport;
 
// 	Logger logger(&transport);
 

	
 
	std::string error;
 
	StorageBackend *storageBackend = StorageBackend::createBackend(&config, error);
 
	if (storageBackend == NULL) {
 
		if (!error.empty()) {
 
			std::cerr << error << "\n";
 
			return -2;
 
		}
 
	}
 
	else if (!storageBackend->connect()) {        
 
		std::cerr << "Can't connect to database. Check the log to find out the reason.\n";
 
		return -1;
 
	}
 

	
 
	UserManager userManager(&transport, &userRegistry, storageBackend);
 
	userManager_ = &userManager;
 

	
 
	UserRegistration *userRegistration = NULL;
 
	UsersReconnecter *usersReconnecter = NULL;
 
	if (storageBackend) {
 
		userRegistration = new UserRegistration(&transport, &userManager, storageBackend);
 
		userRegistration->start();
 

	
 
		usersReconnecter = new UsersReconnecter(&transport, storageBackend);
 
	}
 

	
 
	FileTransferManager ftManager(&transport, &userManager);
 

	
 
	NetworkPluginServer plugin(&transport, &config, &userManager, &ftManager);
 

	
 
	AdminInterface adminInterface(&transport, &userManager, &plugin, storageBackend);
 
	AdminInterface adminInterface(&transport, &userManager, &plugin, storageBackend, userRegistration);
 
	plugin.setAdminInterface(&adminInterface);
 

	
 
	StatsResponder statsResponder(&transport, &userManager, &plugin, storageBackend);
 
	statsResponder.start();
 

	
 
	GatewayResponder gatewayResponder(transport.getIQRouter(), &userManager);
 
	gatewayResponder.start();
 

	
 
	DiscoItemsResponder discoItemsResponder(&transport);
 
	discoItemsResponder.start();
 

	
 
	AdHocManager adhocmanager(&transport, &discoItemsResponder);
 
	adhocmanager.start();
 

	
 
	SettingsAdHocCommandFactory settings;
 
	adhocmanager.addAdHocCommand(&settings);
 

	
 
	eventLoop_ = &eventLoop;
 

	
 
	eventLoop.run();
 

	
 
	if (userRegistration) {
 
		userRegistration->stop();
 
		delete userRegistration;
 
	}
 

	
 
	if (usersReconnecter) {
 
		delete usersReconnecter;
 
	}
 

	
 
	delete storageBackend;
 
	delete factories;
 
	return 0;
 
}
spectrum/src/sample.cfg
Show inline comments
 
[service]
 
jid = localhost
 
password = secret
 
server = 127.0.0.1
 
port = 5222
 
server_mode = 1
 
backend_host=localhost
 
pidfile=./test.pid
 
# < this option doesn't work yet
 
#backend_port=10001
 
#admin_jid=admin@localhost
 
admin_password=test
 
#cert=server.pfx #patch to PKCS#12 certificate
 
#cert_password=test #password to that certificate if any
 
users_per_backend=10
 
backend=../..//backends/swiften/spectrum2_swiften_backend
 
backend=../..//backends/libyahoo2/spectrum2_libyahoo2_backend
 
#backend=../../backends/template/template_backend.py
 
#protocol=prpl-jabber
 
protocol=prpl-msn
 
#protocol=any
 
#protocol=prpl-icq
 
working_dir=./
 
portfile=$jid.port
 

	
 
[backend]
 
#default_avatar=catmelonhead.jpg
 
#no_vcard_fetch=true
 

	
 
[logging]
 
#config=logging.cfg # log4cxx/log4j logging configuration file
 
#backend_config=/home/hanzz/code/libtransport/spectrum/src/backend-logging.cfg # log4cxx/log4j logging configuration file for backends
 

	
 
[database]
 
type = none # or "none" without database backend
 
database = test.sql
 
prefix=icq
 
#type = mysql # or "none" without database backend.......................................................................................................................
 
#database = test
 
#prefix=
 
#user=root
 
#password=yourrootsqlpassword
 
#encryption_key=hanzzik
spectrum_manager/src/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_manager ${SRC} ../../src/config.cpp ../../src/util.cpp)
 
 
target_link_libraries(spectrum2_manager ${SWIFTEN_LIBRARY})
 
ADD_EXECUTABLE(spectrum2_manager ${SRC} ../../src/config.cpp ../../src/util.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/transport/protocol.pb.cc)
 

	
 
ADD_DEPENDENCIES(spectrum2_manager pb)
 
SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/../../include/transport/protocol.pb.cc PROPERTIES GENERATED 1)
 

	
 
target_link_libraries(spectrum2_manager ${SWIFTEN_LIBRARY} ${PROTOBUF_LIBRARIES})
 
 
INSTALL(TARGETS spectrum2_manager RUNTIME DESTINATION bin)
 
 
INSTALL(FILES
 
	spectrum_manager.cfg
 
	DESTINATION /etc/spectrum2
 
	)
spectrum_manager/src/main.cpp
Show inline comments
 
#include "managerconfig.h"
 
#include "transport/config.h"
 
#include "transport/protocol.pb.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 
 
#include <boost/foreach.hpp>
 
#include <iostream>
 
#include <fstream>
 
#include <iterator>
 
#include <algorithm>
 
#include <boost/filesystem.hpp>
 
#include "signal.h"
 
#include "sys/wait.h"
 
 
#define WRAP(MESSAGE, TYPE) 	pbnetwork::WrapperMessage wrap; \
 
	wrap.set_type(TYPE); \
 
	wrap.set_payload(MESSAGE); \
 
	wrap.SerializeToString(&MESSAGE);
 
 
 
using namespace Transport;
 
 
using namespace boost::filesystem;
 
 
using namespace boost;
 
 
static int finished;
 
static std::string *m;
 
 
static void handleDisconnected(Swift::Client *client, const boost::optional<Swift::ClientError> &, const std::string &server) {
 
	std::cout << "[ DISCONNECTED ] " << server << "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
 
}
 
 
static void handleConnected(Swift::Client *client, const std::string &server) {
 
	boost::shared_ptr<Swift::Message> message(new Swift::Message());
 
	message->setTo(server);
 
	message->setFrom(client->getJID());
 
	message->setBody(*m);
 
 
	client->sendMessage(message);
 
}
 
 
static void handleMessageReceived(Swift::Client *client, Swift::Message::ref message, const std::string &server) {
 
	std::string body = message->getBody();
 
	boost::replace_all(body, "\n", "\n[      OK      ] " + server + ": ");
 
	std::cout << "[      OK      ] " << server << ": " << body <<  "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
 
}
 
std::string _data;
 
 
static std::string searchForBinary(const std::string &binary) {
 
	std::vector<std::string> path_list;
 
	char * env_path = getenv("PATH");
 
 
	if (env_path != NULL) {
 
		std::string buffer = "";
 
		for (int s = 0; s < strlen(env_path); s++) {
 
			if (env_path[s] == ':') {
 
				path_list.insert(path_list.end(), std::string(buffer));
 
				buffer = "";
 
			}
 
			else {
 
				buffer += env_path[s];
 
			}
 
		}
 
 
		if (buffer != "") {
 
			path_list.insert(path_list.end(), std::string(buffer));
 
			buffer = "";
 
		}
 
 
		for (std::vector<std::string>::iterator dit = path_list.begin(); dit < path_list.end(); dit++) {
 
			std::string bpath = *dit;
 
			bpath += "/";
 
			bpath += binary;
 
			path p(bpath);
 
			if (exists(p) && !is_directory(p)) {
 
				return bpath;
 
			}
 
		}
 
	}
 
	return "";
 
}
 
 
// Executes new backend
 
static unsigned long exec_(std::string path, std::string config, std::string jid = "") {
 
	// fork and exec
 
	pid_t pid = fork();
 
	if ( pid == 0 ) {
 
		// child process
 
		if (jid.empty()) {
 
			exit(execl(path.c_str(), path.c_str(), config.c_str(), NULL));
 
		}
 
		else {
 
			exit(execl(path.c_str(), path.c_str(), "-j", jid.c_str(), config.c_str(), NULL));
 
		}
 
	} else if ( pid < 0 ) {
 
		// fork failed
 
	}
 
	else {
 
		waitpid(pid, 0, 0);
 
	}
 
 
	return (unsigned long) pid;
 
}
 
 
static int getPort(const std::string &portfile) {
 
	path p(portfile);
 
	if (!exists(p) || is_directory(p)) {
 
		return 0;
 
	}
 
 
	std::ifstream f(p.string().c_str(), std::ios_base::in);
 
	std::string port;
 
	f >> port;
 
 
	if (port.empty())
 
		return 0;
 
 
	return boost::lexical_cast<int>(port);
 
}
 
 
static int isRunning(const std::string &pidfile) {
 
	path p(pidfile);
 
	if (!exists(p) || is_directory(p)) {
 
		return 0;
 
	}
 
 
	std::ifstream f(p.string().c_str(), std::ios_base::in);
 
	std::string pid;
 
	f >> pid;
 
 
	if (pid.empty())
 
		return 0;
 
 
	if (kill(boost::lexical_cast<int>(pid), 0) != 0)
 
		return 0;
 
 
	return boost::lexical_cast<int>(pid);
 
}
 
 
static void start_all_instances(ManagerConfig *config) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 
 
		std::string spectrum2_binary = searchForBinary("spectrum2");
 
		if (spectrum2_binary.empty()) {
 
			std::cerr << "spectrum2 binary not found in PATH\n";
 
			exit(8);
 
		}
 
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
					continue;
 
				}
 
 
				
 
				std::vector<std::string> vhosts;
 
				if (CONFIG_HAS_KEY(&cfg, "vhosts.vhost"))
 
					vhosts = CONFIG_VECTOR(&cfg, "vhosts.vhost");
 
				vhosts.push_back(CONFIG_STRING(&cfg, "service.jid"));
 
 
				BOOST_FOREACH(std::string &vhost, vhosts) {
 
					Config vhostCfg;
 
					if (vhostCfg.load(itr->path().string(), vhost) == false) {
 
						std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
						continue;
 
					}
 
 
					int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
 
					if (pid == 0) {
 
						std::cout << "Starting " << itr->path() << ": OK\n";
 
						exec_(spectrum2_binary, itr->path().string(), vhost);
 
					}
 
					else {
 
						std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 
 
static void stop_all_instances(ManagerConfig *config) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
				}
 
 
				std::vector<std::string> vhosts;
 
				if (CONFIG_HAS_KEY(&cfg, "vhosts.vhost"))
 
					vhosts = CONFIG_VECTOR(&cfg, "vhosts.vhost");
 
				vhosts.push_back(CONFIG_STRING(&cfg, "service.jid"));
 
 
				BOOST_FOREACH(std::string &vhost, vhosts) {
 
					Config vhostCfg;
 
					if (vhostCfg.load(itr->path().string(), vhost) == false) {
 
						std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
						continue;
 
					}
 
 
					int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
 
					if (pid) {
 
						std::cout << "Stopping " << itr->path() << ": OK\n";
 
						kill(pid, SIGTERM);
 
					}
 
					else {
 
						std::cout << "Stopping " << itr->path() << ": Not running\n";
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 
 
static int show_status(ManagerConfig *config) {
 
	int ret = 0;
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
				}
 
 
				std::vector<std::string> vhosts;
 
				if (CONFIG_HAS_KEY(&cfg, "vhosts.vhost"))
 
					vhosts = CONFIG_VECTOR(&cfg, "vhosts.vhost");
 
				vhosts.push_back(CONFIG_STRING(&cfg, "service.jid"));
 
 
				BOOST_FOREACH(std::string &vhost, vhosts) {
 
					Config vhostCfg;
 
					if (vhostCfg.load(itr->path().string(), vhost) == false) {
 
						std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
						continue;
 
					}
 
 
					int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
 
					if (pid) {
 
						std::cout << itr->path() << ": " << vhost << " Running\n";
 
					}
 
					else {
 
						ret = 3;
 
						std::cout << itr->path() << ": " << vhost << " Stopped\n";
 
					}
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
	return ret;
 
}
 
 
static void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &message) {
 
static void handleDataRead(boost::shared_ptr<Swift::Connection> m_conn, boost::shared_ptr<Swift::SafeByteArray> data) {
 
	_data += std::string(data->begin(), data->end());
 
 
	// Parse data while there are some
 
	while (_data.size() != 0) {
 
		// expected_size of wrapper message
 
		unsigned int expected_size;
 
 
		// if data is >= 4, we have whole header and we can
 
		// read expected_size.
 
		if (_data.size() >= 4) {
 
			expected_size = *((unsigned int*) &_data[0]);
 
			expected_size = ntohl(expected_size);
 
			// If we don't have whole wrapper message, wait for next
 
			// handleDataRead call.
 
			if (_data.size() - 4 < expected_size)
 
				return;
 
		}
 
		else {
 
			return;
 
		}
 
 
		// Parse wrapper message and erase it from buffer.
 
		pbnetwork::WrapperMessage wrapper;
 
		if (wrapper.ParseFromArray(&_data[4], expected_size) == false) {
 
			std::cout << "PARSING ERROR " << expected_size << "\n";
 
			_data.erase(_data.begin(), _data.begin() + 4 + expected_size);
 
			continue;
 
		}
 
		_data.erase(_data.begin(), _data.begin() + 4 + expected_size);
 
 
		if (wrapper.type() == pbnetwork::WrapperMessage_Type_TYPE_QUERY) {
 
			pbnetwork::BackendConfig payload;
 
			if (payload.ParseFromString(wrapper.payload()) == false) {
 
				std::cout << "PARSING ERROR\n";
 
				// TODO: ERROR
 
				continue;
 
			}
 
 
			std::cout << payload.config() << "\n";
 
			exit(0);
 
		}
 
	}
 
}
 
 
static void handleConnected(boost::shared_ptr<Swift::Connection> m_conn, const std::string &msg, bool error) {
 
	if (error) {
 
		std::cerr << "Can't connect the server\n";
 
		exit(50);
 
	}
 
	else {
 
		pbnetwork::BackendConfig m;
 
		m.set_config(msg);
 
 
		std::string message;
 
		m.SerializeToString(&message);
 
 
		WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_QUERY);
 
 
		uint32_t size = htonl(message.size());
 
		char *header = (char *) &size;
 
 
		
 
		// send header together with wrapper message
 
		m_conn->write(Swift::createSafeByteArray(std::string(header, 4) + message));
 
	}
 
}
 
 
static void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 
 
		bool found = false;
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
					continue;
 
				}
 
 
				if (CONFIG_VECTOR(&cfg, "service.admin_jid").empty() || CONFIG_STRING(&cfg, "service.admin_password").empty()) {
 
					std::cerr << itr->path().string() << ": service.admin_jid or service.admin_password empty. This server can't be queried over XMPP.\n";
 
				if (CONFIG_STRING(&cfg, "service.jid") != jid) {
 
					continue;
 
				}
 
 
				finished++;
 
				Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories);
 
				client->setAlwaysTrustCertificates();
 
				client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid")));
 
				client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
				client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
				Swift::ClientOptions opt;
 
				opt.allowPLAINWithoutTLS = true;
 
				client->connect(opt);
 
				found = true;
 
 
				boost::shared_ptr<Swift::Connection> m_conn;
 
				m_conn = networkFactories.getConnectionFactory()->createConnection();
 
				m_conn->onDataRead.connect(boost::bind(&handleDataRead, m_conn, _1));
 
				m_conn->onConnectFinished.connect(boost::bind(&handleConnected, m_conn, message, _1));
 
				m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(CONFIG_STRING(&cfg, "service.backend_host")), getPort(CONFIG_STRING(&cfg, "service.portfile"))));
 
 
// 				finished++;
 
// 				Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories);
 
// 				client->setAlwaysTrustCertificates();
 
// 				client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid")));
 
// 				client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
// 				client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
// 				Swift::ClientOptions opt;
 
// 				opt.allowPLAINWithoutTLS = true;
 
// 				client->connect(opt);
 
			}
 
		}
 
 
		if (!found) {
 
			std::cerr << "Config file for Spectrum instance with this JID was not found\n";
 
			exit(20);
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 
 
static void show_list(ManagerConfig *config) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 
 
		bool found = false;
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
					continue;
 
				}
 
 
				std::cout << CONFIG_STRING(&cfg, "service.jid") << "\n";
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 
 
// static void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &message) {
 
// 	path p(CONFIG_STRING(config, "service.config_directory"));
 
// 
 
// 	try {
 
// 		if (!exists(p)) {
 
// 			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
// 			exit(6);
 
// 		}
 
// 
 
// 		if (!is_directory(p)) {
 
// 			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
// 			exit(7);
 
// 		}
 
// 
 
// 		directory_iterator end_itr;
 
// 		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
// 			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
// 				Config cfg;
 
// 				if (cfg.load(itr->path().string()) == false) {
 
// 					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
// 					continue;
 
// 				}
 
// 
 
// 				if (CONFIG_VECTOR(&cfg, "service.admin_jid").empty() || CONFIG_STRING(&cfg, "service.admin_password").empty()) {
 
// 					std::cerr << itr->path().string() << ": service.admin_jid or service.admin_password empty. This server can't be queried over XMPP.\n";
 
// 					continue;
 
// 				}
 
// 
 
// 				finished++;
 
// 				Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories);
 
// 				client->setAlwaysTrustCertificates();
 
// 				client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid")));
 
// 				client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
// 				client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
// 				Swift::ClientOptions opt;
 
// 				opt.allowPLAINWithoutTLS = true;
 
// 				client->connect(opt);
 
// 			}
 
// 		}
 
// 	}
 
// 	catch (const filesystem_error& ex) {
 
// 		std::cerr << "boost filesystem error\n";
 
// 		exit(5);
 
// 	}
 
// }
 
 
 
int main(int argc, char **argv)
 
{
 
	ManagerConfig config;
 
	std::string config_file;
 
	std::string command;
 
	std::vector<std::string> command;
 
	boost::program_options::variables_map vm;
 
 
	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <COMMAND>\nCommands:\n"
 
	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <COMMAND>\n"
 
													 "       spectrum [OPTIONS] <instance_JID> <other>\nCommands:\n"
 
													 " start - start all local Spectrum2 instances\n"
 
													 " stop  - stop all  local Spectrum2 instances\n"
 
													 " stop  - stop all local Spectrum2 instances\n"
 
													 " status - status of local Spectrum2 instances\n"
 
													 " <other> - send command to all local + remote Spectrum2 instances and print output\n"
 
													 " <other> - send command to local Spectrum2 instance and print output\n"
 
													 "Allowed options");
 
	desc.add_options()
 
		("help,h", "Show help output")
 
		("config,c", boost::program_options::value<std::string>(&config_file)->default_value("/etc/spectrum2/spectrum_manager.cfg"), "Spectrum manager config file")
 
		("command", boost::program_options::value<std::string>(&command)->default_value(""), "Command")
 
		("command", boost::program_options::value<std::vector<std::string> >(&command), "Command")
 
		;
 
	try
 
	{
 
		boost::program_options::positional_options_description p;
 
		p.add("command", -1);
 
		boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
 
          options(desc).positional(p).run(), vm);
 
		boost::program_options::notify(vm);
 
 
		if(vm.count("help"))
 
		{
 
			std::cout << desc << "\n";
 
			return 1;
 
		}
 
	}
 
	catch (std::runtime_error& e)
 
	{
 
		std::cout << desc << "\n";
 
		return 2;
 
	}
 
	catch (...)
 
	{
 
		std::cout << desc << "\n";
 
		return 3;
 
	}
 
 
	if (!config.load(config_file)) {
 
		std::cerr << "Can't load configuration file.\n";
 
		return 4;
 
	}
 
 
	if (command.empty()) {
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 
 
	if (command == "start") {
 
	if (command[0] == "start") {
 
		start_all_instances(&config);
 
	}
 
	else if (command == "stop") {
 
	else if (command[0] == "stop") {
 
		stop_all_instances(&config);
 
	}
 
	else if (command == "status") {
 
	else if (command[0] == "status") {
 
		return show_status(&config);
 
	}
 
	else if (command[0] == "list") {
 
		show_list(&config);
 
	}
 
	else {
 
		if (command.size() < 2) {
 
			std::cout << desc << "\n";
 
			return 11;
 
		}
 
		Swift::SimpleEventLoop eventLoop;
 
		Swift::BoostNetworkFactories networkFactories(&eventLoop);
 
 
		std::string message = command;
 
		m = &message;
 
 
		ask_local_servers(&config, networkFactories, message);
 
 
		std::vector<std::string> servers = CONFIG_VECTOR(&config, "servers.server");
 
		for (std::vector<std::string>::const_iterator it = servers.begin(); it != servers.end(); it++) {
 
			finished++;
 
			Swift::Client *client = new Swift::Client(CONFIG_STRING(&config, "service.admin_username") + "@" + *it, CONFIG_STRING(&config, "service.admin_password"), &networkFactories);
 
			client->setAlwaysTrustCertificates();
 
			client->onConnected.connect(boost::bind(&handleConnected, client, *it));
 
			client->onDisconnected.connect(bind(&handleDisconnected, client, _1, *it));
 
			client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, *it));
 
			Swift::ClientOptions opt;
 
			opt.allowPLAINWithoutTLS = true;
 
			client->connect(opt);
 
	// 		std::cout << *it << "\n";
 
		}
 
		std::string jid = command[0];
 
		command.erase(command.begin());
 
		std::string cmd = boost::algorithm::join(command, " ");
 
 
		ask_local_server(&config, networkFactories, jid, cmd);
 
// 		std::string message = command;
 
// 		m = &message;
 
 
// 		ask_local_server(&config, networkFactories, message);
 
 
		eventLoop.run();
 
	}
 
}
src/adhoccommand.cpp
Show inline comments
 
new file 100644
 
/**
 
 * XMPP - libpurple transport
 
 *
 
 * Copyright (C) 2012, 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 "transport/adhoccommand.h"
 
#include "transport/adhoccommandfactory.h"
 
#include "transport/conversation.h"
 
#include "transport/usermanager.h"
 
#include "transport/buddy.h"
 
#include "transport/factory.h"
 
#include "transport/user.h"
 
#include "transport/logging.h"
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "AdHocCommand");
 

	
 
AdHocCommand::AdHocCommand(Component *component, const Swift::JID &initiator, const Swift::JID &to) {
 
	m_component = component;
 
	m_initiator = initiator;
 
	m_to = to;
 

	
 
	std::string bucket = "abcdefghijklmnopqrstuvwxyz";
 
	for (int i = 0; i < 32; i++) {
 
		m_id += bucket[rand() % bucket.size()];
 
	}
 
}
 

	
 
AdHocCommand::~AdHocCommand() {
 
}
 

	
 
void AdHocCommand::addFormField(Swift::FormField::ref field) {
 
	m_fields.push_back(field);
 
}
 

	
 
}
src/adhocmanager.cpp
Show inline comments
 
new file 100644
 
/**
 
 * XMPP - libpurple transport
 
 *
 
 * Copyright (C) 2012, 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 "transport/adhocmanager.h"
 
#include "transport/adhoccommandfactory.h"
 
#include "transport/discoitemsresponder.h"
 
#include "transport/conversation.h"
 
#include "transport/usermanager.h"
 
#include "transport/buddy.h"
 
#include "transport/factory.h"
 
#include "transport/user.h"
 
#include "transport/logging.h"
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "AdHocManager");
 

	
 
AdHocManager::AdHocManager(Component *component, DiscoItemsResponder *discoItemsResponder) : Swift::Responder<Swift::Command>(component->getIQRouter()){
 
	m_component = component;
 
	m_discoItemsResponder = discoItemsResponder;
 

	
 
	m_collectTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20);
 
	m_collectTimer->onTick.connect(boost::bind(&AdHocManager::removeOldSessions, this));
 
	m_collectTimer->start();
 
}
 

	
 
AdHocManager::~AdHocManager() {
 
	m_collectTimer->stop();
 
	stop();
 
}
 

	
 
void AdHocManager::start() {
 
	Swift::Responder<Swift::Command>::start();
 
}
 

	
 
void AdHocManager::stop() {
 
	Swift::Responder<Swift::Command>::stop();
 

	
 
	for (SessionsMap::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) {
 
		std::vector<std::string> candidates;
 
		for (CommandsMap::iterator ct = it->second.begin(); ct != it->second.end(); ct++) {
 
			delete ct->second;
 
		}
 
	}
 

	
 
	m_sessions.clear();
 
}
 

	
 
void AdHocManager::addAdHocCommand(AdHocCommandFactory *factory) {
 
	if (m_factories.find(factory->getNode()) != m_factories.end()) {
 
		LOG4CXX_ERROR(logger, "Command with node " << factory->getNode() << " is already registered. Ignoring this attempt.");
 
		return;
 
	}
 

	
 
	m_factories[factory->getNode()] = factory;
 
	m_discoItemsResponder->addAdHocCommand(factory->getNode(), factory->getName());
 
}
 

	
 
void AdHocManager::removeOldSessions() {
 
	unsigned long removedCommands = 0;
 
	time_t now = time(NULL);
 

	
 
	std::vector<std::string> toRemove;
 
	for (SessionsMap::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) {
 
		std::vector<std::string> candidates;
 
		for (CommandsMap::iterator ct = it->second.begin(); ct != it->second.end(); ct++) {
 
			if (now - ct->second->getLastActivity() > 15*60) {
 
				candidates.push_back(it->first);
 
				delete ct->second;
 
				removedCommands++;
 
			}
 
		}
 

	
 
		BOOST_FOREACH(std::string &key, candidates) {
 
			it->second.erase(key);
 
		}
 

	
 
		if (it->second.empty()) {
 
			toRemove.push_back(it->first);
 
		}
 
	}
 

	
 
	BOOST_FOREACH(std::string &key, toRemove) {
 
		m_sessions.erase(key);
 
	}
 

	
 
	if (removedCommands > 0) {
 
		LOG4CXX_INFO(logger, "Removed " << removedCommands << " old commands sessions.");
 
	}
 
}
 

	
 
bool AdHocManager::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::Command> payload) {
 
	return false;
 
}
 

	
 
bool AdHocManager::handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::Command> payload) {
 
	AdHocCommand *command = NULL;
 
	// Try to find AdHocCommand according to 'from' and session_id
 
	if (m_sessions.find(from) != m_sessions.end() && m_sessions[from].find(payload->getSessionID()) != m_sessions[from].end()) {
 
		command = m_sessions[from][payload->getSessionID()];
 
	}
 
	// Check if we can create command with this node
 
	else if (m_factories.find(payload->getNode()) != m_factories.end()) {
 
		command = m_factories[payload->getNode()]->createAdHocCommand(m_component, from, to);
 
		m_sessions[from][command->getId()] = command;
 
		LOG4CXX_INFO(logger, from.toString() << ": Started new AdHoc command session with node " << payload->getNode());
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, from.toString() << ": Unknown node " << payload->getNode() << ". Can't start AdHoc command session.");
 
		sendError(from, id, Swift::ErrorPayload::BadRequest, Swift::ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	if (!command) {
 
		LOG4CXX_ERROR(logger, from.toString() << ": createAdHocCommand for node " << payload->getNode() << " returned NULL pointer");
 
		sendError(from, id, Swift::ErrorPayload::BadRequest, Swift::ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	boost::shared_ptr<Swift::Command> response = command->handleRequest(payload);
 
	if (!response) {
 
		LOG4CXX_ERROR(logger, from.toString() << ": handleRequest for node " << payload->getNode() << " returned NULL pointer");
 
		sendError(from, id, Swift::ErrorPayload::BadRequest, Swift::ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	response->setSessionID(command->getId());
 

	
 
	sendResponse(from, id, response);
 

	
 
	command->refreshLastActivity();
 

	
 
	// Command completed, so we can remove it now
 
	if (response->getStatus() == Swift::Command::Completed) {
 
		m_sessions[from].erase(command->getId());
 
		if (m_sessions[from].empty()) {
 
			m_sessions.erase(from);
 
		}
 
		delete command;
 
	}
 
	
 

	
 
	return true;
 
}
 

	
 
}
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 "transport/logging.h"
 
#include "transport/userregistration.h"
 
#include "storageresponder.h"
 
#include "transport/memoryusage.h"
 
#include <boost/foreach.hpp>
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "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) {
 
AdminInterface::AdminInterface(Component *component, UserManager *userManager, NetworkPluginServer *server, StorageBackend *storageBackend, UserRegistration *userRegistration) {
 
	m_component = component;
 
	m_storageBackend = storageBackend;
 
	m_userManager = userManager;
 
	m_server = server;
 
	m_userRegistration = userRegistration;
 
	m_start = time(NULL);
 

	
 
	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;
 

	
 
	std::vector<std::string> const &x = CONFIG_VECTOR(m_component->getConfig(),"service.admin_jid");
 
	if (std::find(x.begin(), x.end(), message->getFrom().toBare().toString()) == x.end()) {
 
	    LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().toBare().toString());
 
	    return;
 
	
 
	}
 
	
 
	// Ignore empty messages
 
	if (message->getBody().empty()) {
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Message from admin received");
 
void AdminInterface::handleQuery(Swift::Message::ref message) {
 
	LOG4CXX_INFO(logger, "Message from admin received: '" << message->getBody() << "'");
 
	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() == "uptime") {
 
		message->setBody(boost::lexical_cast<std::string>(time(0) - m_start));
 
	}
 
	else if (message->getBody() == "online_users") {
 
		std::string lst;
 
		const std::map<std::string, User *> &users = m_userManager->getUsers();
 
		if (users.size() == 0)
 
			lst = "0";
 

	
 
		for (std::map<std::string, User *>::const_iterator it = users.begin(); it != users.end(); it ++) {
 
			lst += (*it).first + "\n";
 
		}
 

	
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody() == "online_users_count") {
 
		int users = m_userManager->getUserCount();
 
		message->setBody(boost::lexical_cast<std::string>(users));
 
	}
 
	else if (message->getBody() == "reload") {
 
		bool done = m_component->getConfig()->reload();
 
		if (done) {
 
			message->setBody("Config reloaded");
 
		}
 
		else {
 
			message->setBody("Error during config reload");
 
		}
 
	}
 
	else if (message->getBody() == "online_users_per_backend") {
 
		std::string lst;
 
		int id = 1;
 

	
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		for (std::list <NetworkPluginServer::Backend *>::const_iterator b = backends.begin(); b != backends.end(); b++) {
 
			NetworkPluginServer::Backend *backend = *b;
 
			lst += "Backend " + boost::lexical_cast<std::string>(id) + " (ID=" + backend->id + ")";
 
			lst += backend->acceptUsers ? "" : " - not-accepting";
 
			lst += backend->longRun ? " - long-running" : "";
 
			lst += ":\n";
 
			if (backend->users.size() == 0) {
 
				lst += "   waiting for users\n";
 
			}
 
			else {
 
				time_t now = time(NULL);
 
				for (std::list<User *>::const_iterator u = backend->users.begin(); u != backend->users.end(); u++) {
 
					User *user = *u;
 
					lst += "   " + user->getJID().toBare().toString();
 
					lst += " - non-active for " + boost::lexical_cast<std::string>(now - user->getLastActivity()) + " seconds";
 
					lst += "\n";
 
				}
 
			}
 
			id++;
 
		}
 

	
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody().find("has_online_user") == 0) {
 
		User *user = m_userManager->getUser(getArg(message->getBody()));
 
		std::cout << getArg(message->getBody()) << "\n";
 
		message->setBody(boost::lexical_cast<std::string>(user != NULL));
 
	}
 
	else if (message->getBody() == "backends_count") {
 
		int backends = m_server->getBackendCount();
 
		message->setBody(boost::lexical_cast<std::string>(backends));
 
	}
 
	else if (message->getBody() == "res_memory") {
 
		double shared = 0;
 
		double rss = 0;
 
#ifndef WIN32
 
		process_mem_usage(shared, rss);
 
#endif
 

	
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
			rss += backend->res;
 
		}
 

	
 
		message->setBody(boost::lexical_cast<std::string>(rss));
 
	}
 
	else if (message->getBody() == "shr_memory") {
 
		double shared = 0;
 
		double rss = 0;
 
#ifndef WIN32
 
		process_mem_usage(shared, rss);
 
#endif
 

	
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
			shared += backend->shared;
 
		}
 

	
 
		message->setBody(boost::lexical_cast<std::string>(shared));
 
	}
 
	else if (message->getBody() == "used_memory") {
 
		double shared = 0;
 
		double rss = 0;
 
#ifndef WIN32
 
		process_mem_usage(shared, rss);
 
#endif
 
		rss -= shared;
 

	
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
			rss += backend->res - backend->shared;
 
		}
 

	
 
		message->setBody(boost::lexical_cast<std::string>(rss));
 
	}
 
	else if (message->getBody() == "average_memory_per_user") {
 
		if (m_userManager->getUserCount() == 0) {
 
			message->setBody(boost::lexical_cast<std::string>(0));
 
		}
 
		else {
 
			unsigned long per_user = 0;
 
			const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
			BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
				per_user += (backend->res - backend->init_res);
 
			}
 

	
 
			message->setBody(boost::lexical_cast<std::string>(per_user / m_userManager->getUserCount()));
 
		}
 
	}
 
	else if (message->getBody() == "res_memory_per_backend") {
 
		std::string lst;
 
		int id = 1;
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
			lst += "Backend " + boost::lexical_cast<std::string>(id) + " (ID=" + backend->id + "): " + boost::lexical_cast<std::string>(backend->res) + "\n";
 
			id++;
 
		}
 

	
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody() == "shr_memory_per_backend") {
 
		std::string lst;
 
		int id = 1;
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
			lst += "Backend " + boost::lexical_cast<std::string>(id)  + " (ID=" + backend->id + "): " + boost::lexical_cast<std::string>(backend->shared) + "\n";
 
			id++;
 
		}
 

	
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody() == "used_memory_per_backend") {
 
		std::string lst;
 
		int id = 1;
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
			lst += "Backend " + boost::lexical_cast<std::string>(id)  + " (ID=" + backend->id + "): " + boost::lexical_cast<std::string>(backend->res - backend->shared) + "\n";
 
			id++;
 
		}
 

	
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody() == "average_memory_per_user_per_backend") {
 
		std::string lst;
 
		int id = 1;
 
		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
 
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
 
			if (backend->users.size() == 0) {
 
				lst += "Backend " + boost::lexical_cast<std::string>(id)  + " (ID=" + backend->id + "): 0\n";
 
			}
 
			else {
 
				lst += "Backend " + boost::lexical_cast<std::string>(id) + " (ID=" + backend->id + "): " + boost::lexical_cast<std::string>((backend->res - backend->init_res) / backend->users.size()) + "\n";
 
			}
 
			id++;
 
		}
 

	
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody() == "collect_backend") {
 
		m_server->collectBackend();
 
	}
 
	else if (message->getBody() == "crashed_backends") {
 
		std::string lst;
 
		const std::vector<std::string> &backends = m_server->getCrashedBackends();
 
		BOOST_FOREACH(const std::string &backend, backends) {
 
			lst += backend + "\n";
 
		}
 
		message->setBody(lst);
 
	}
 
	else if (message->getBody() == "crashed_backends_count") {
 
		message->setBody(boost::lexical_cast<std::string>(m_server->getCrashedBackends().size()));
 
	}
 
	else if (message->getBody() == "messages_from_xmpp") {
 
		int msgCount = m_userManager->getMessagesToBackend();
 
		message->setBody(boost::lexical_cast<std::string>(msgCount));
 
	}
 
	else if (message->getBody() == "messages_to_xmpp") {
 
		int msgCount = m_userManager->getMessagesToXMPP();
 
		message->setBody(boost::lexical_cast<std::string>(msgCount));
 
	}
 
	else if (message->getBody().find("register ") == 0 && m_userRegistration) {
 
		std::string body = message->getBody();
 
		std::vector<std::string> args;
 
		boost::split(args, body, boost::is_any_of(" "));
 
		if (args.size() == 4) {
 
			UserInfo res;
 
			res.jid = args[1];
 
			res.uin = args[2];
 
			res.password = args[3];
 
			res.language = "en";
 
			res.encoding = "utf-8";
 
			res.vip = 0;
 

	
 
			if (m_userRegistration->registerUser(res)) {
 
				message->setBody("User registered.");
 
			}
 
			else {
 
				message->setBody("Registration failed: User is already registered");
 
			}
 
		}
 
		else {
 
			message->setBody("Bad argument count. See 'help'.");
 
		}
 
	}
 
	else if (message->getBody().find("unregister ") == 0 && m_userRegistration) {
 
		std::string body = message->getBody();
 
		std::vector<std::string> args;
 
		boost::split(args, body, boost::is_any_of(" "));
 
		if (args.size() == 2) {
 
			if (m_userRegistration->unregisterUser(args[1])) {
 
				message->setBody("User '" + args[1] + "' unregistered.");
 
			}
 
			else {
 
				message->setBody("Unregistration failed: User '" + args[1] + "' is not registered");
 
			}
 
		}
 
		else {
 
			message->setBody("Bad argument count. See 'help'.");
 
		}
 
	}
 
	else if (message->getBody().find("help") == 0) {
 
		std::string help;
 
		help += "General:\n";
 
		help += "    status - shows instance status\n";
 
		help += "    reload - Reloads config file\n";
 
		help += "    uptime - returns ptime in seconds\n";
 
		help += "Users:\n";
 
		help += "    online_users - returns list of all online users\n";
 
		help += "    online_users_count - number of online users\n";
 
		help += "    online_users_per_backend - shows online users per backends\n";
 
		help += "    has_online_user <bare_JID> - returns 1 if user is online\n";
 
		if (m_userRegistration) {
 
			help += "    register <bare_JID> <legacyName> <password> - registers the new user\n";
 
			help += "    unregister <bare_JID> - unregisters existing user\n";
 
		}
 
		help += "Messages:\n";
 
		help += "    messages_from_xmpp - get number of messages received from XMPP users\n";
 
		help += "    messages_to_xmpp - get number of messages sent to XMPP users\n";
 
		help += "Backends:\n";
 
		help += "    backends_count - number of active backends\n";
 
		help += "    crashed_backends - returns IDs of crashed backends\n";
 
		help += "    crashed_backends_count - returns number of crashed backends\n";
 
		help += "Memory:\n";
 
		help += "    res_memory - Total RESident memory spectrum2 and its backends use in KB\n";
 
		help += "    shr_memory - Total SHaRed memory spectrum2 backends share together in KB\n";
 
		help += "    used_memory - (res_memory - shr_memory)\n";
 
		help += "    average_memory_per_user - (memory_used_without_any_user - res_memory)\n";
 
		help += "    res_memory_per_backend - RESident memory used by backends in KB\n";
 
		help += "    shr_memory_per_backend - SHaRed memory used by backends in KB\n";
 
		help += "    used_memory_per_backend - (res_memory - shr_memory) per backend\n";
 
		help += "    average_memory_per_user_per_backend - (memory_used_without_any_user - res_memory) per backend\n";
 
		
 
		
 
		message->setBody(help);
 
	}
 
	else {
 
		message->setBody("Unknown command. Try \"help\"");
 
	}
 
}
 

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

	
 
	std::vector<std::string> const &x = CONFIG_VECTOR(m_component->getConfig(),"service.admin_jid");
 
	if (std::find(x.begin(), x.end(), message->getFrom().toBare().toString()) == x.end()) {
 
	    LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().toBare().toString());
 
	    return;
 
	
 
	}
 
	
 
	// Ignore empty messages
 
	if (message->getBody().empty()) {
 
		return;
 
	}
 

	
 
	handleQuery(message);
 

	
 
	m_component->getStanzaChannel()->sendMessage(message);
 
}
 

	
 
}
src/config.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/config.h"
 
#include <fstream>
 
#ifdef _WIN32
 
#include <direct.h>
 
#define getcwd _getcwd
 
#include <windows.h>
 
#ifdef _MSC_VER
 
#define PATH_MAX MAX_PATH
 
#endif
 
#endif
 

	
 
#include "iostream"
 
#include "boost/version.hpp"
 

	
 
#define BOOST_MAJOR_VERSION BOOST_VERSION / 100000
 
#define BOOST_MINOR_VERSION BOOST_VERSION / 100 % 1000
 

	
 
using namespace boost::program_options;
 

	
 
namespace Transport {
 
static int getRandomPort(const std::string &s) {
 
	unsigned long r = 0;
 
	BOOST_FOREACH(char c, s) {
 
		r += (int) c;
 
	}
 
	srand(time(NULL) + r);
 
	return 30000 + rand() % 10000;
 
}
 

	
 
bool Config::load(const std::string &configfile, boost::program_options::options_description &opts, const std::string &jid) {
 
	std::ifstream ifs(configfile.c_str());
 
	if (!ifs.is_open())
 
		return false;
 

	
 
	m_file = configfile;
 
	bool ret = load(ifs, opts, jid);
 
	ifs.close();
 
#ifndef WIN32
 
	char path[PATH_MAX] = "";
 
	if (m_file.find_first_of("/") != 0) {
 
		getcwd(path, PATH_MAX);
 
		m_file = std::string(path) + "/" + m_file;
 
	}
 
#endif
 

	
 
	return ret;
 
}
 

	
 
bool Config::load(std::istream &ifs, boost::program_options::options_description &opts, const std::string &_jid) {
 
	m_unregistered.clear();
 
	opts.add_options()
 
		("service.jid", value<std::string>()->default_value(""), "Transport Jabber ID")
 
		("service.server", value<std::string>()->default_value(""), "Server to connect to")
 
		("service.password", value<std::string>()->default_value(""), "Password used to auth the server")
 
		("service.port", value<int>()->default_value(0), "Port the server is listening on")
 
		("service.user", value<std::string>()->default_value(""), "The name of user Spectrum runs as.")
 
		("service.group", value<std::string>()->default_value(""), "The name of group Spectrum runs as.")
 
		("service.backend", value<std::string>()->default_value("libpurple_backend"), "Backend")
 
		("service.protocol", value<std::string>()->default_value(""), "Protocol")
 
		("service.pidfile", value<std::string>()->default_value("/var/run/spectrum2/$jid.pid"), "Full path to pid file")
 
		("service.portfile", value<std::string>()->default_value("/var/run/spectrum2/$jid.port"), "File to store backend_port to. It's used by spectrum2_manager.")
 
		("service.working_dir", value<std::string>()->default_value("/var/lib/spectrum2/$jid"), "Working dir")
 
		("service.allowed_servers", value<std::vector<std::string> >()->multitoken(), "Only users from these servers can connect")
 
		("service.server_mode", value<bool>()->default_value(false), "True if Spectrum should behave as server")
 
		("service.users_per_backend", value<int>()->default_value(100), "Number of users per one legacy network backend")
 
		("service.backend_host", value<std::string>()->default_value("localhost"), "Host to bind backend server to")
 
		("service.backend_port", value<std::string>()->default_value("0"), "Port to bind backend server to")
 
		("service.cert", value<std::string>()->default_value(""), "PKCS#12 Certificate.")
 
		("service.cert_password", value<std::string>()->default_value(""), "PKCS#12 Certificate password.")
 
		("service.admin_jid", value<std::vector<std::string> >()->multitoken(), "Administrator jid.")
 
		("service.admin_password", value<std::string>()->default_value(""), "Administrator password.")
 
		("service.reuse_old_backends", value<bool>()->default_value(true), "True if Spectrum should use old backends which were full in the past.")
 
		("service.idle_reconnect_time", value<int>()->default_value(0), "Time in seconds after which idle users are reconnected to let their backend die.")
 
		("service.memory_collector_time", value<int>()->default_value(0), "Time in seconds after which backend with most memory is set to die.")
 
		("service.more_resources", value<bool>()->default_value(false), "Allow more resources to be connected in server mode at the same time.")
 
		("service.enable_privacy_lists", value<bool>()->default_value(true), "")
 
		("service.enable_xhtml", value<bool>()->default_value(true), "")
 
		("vhosts.vhost", value<std::vector<std::string> >()->multitoken(), "")
 
		("identity.name", value<std::string>()->default_value("Spectrum 2 Transport"), "Name showed in service discovery.")
 
		("identity.category", value<std::string>()->default_value("gateway"), "Disco#info identity category. 'gateway' by default.")
 
		("identity.type", value<std::string>()->default_value(""), "Type of transport ('icq','msn','gg','irc', ...)")
 
		("registration.enable_public_registration", value<bool>()->default_value(true), "True if users should be able to register.")
 
		("registration.language", value<std::string>()->default_value("en"), "Default language for registration form")
 
		("registration.instructions", value<std::string>()->default_value("Enter your legacy network username and password."), "Instructions showed to user in registration form")
 
		("registration.username_label", value<std::string>()->default_value("Legacy network username:"), "Label for username field")
 
		("registration.username_mask", value<std::string>()->default_value(""), "Username mask")
 
		("registration.auto_register", value<bool>()->default_value(false), "Register new user automatically when the presence arrives.")
 
		("registration.encoding", value<std::string>()->default_value("utf8"), "Default encoding in registration form")
 
		("registration.require_local_account", value<bool>()->default_value(false), "True if users have to have a local account to register to this transport from remote servers.")
 
		("registration.local_username_label", value<std::string>()->default_value("Local username:"), "Label for local usernme field")
 
		("registration.local_account_server", value<std::string>()->default_value("localhost"), "The server on which the local accounts will be checked for validity")
 
		("registration.local_account_server_timeout", value<int>()->default_value(10000), "Timeout when checking local user on local_account_server (msecs)")
 
		("gateway_responder.prompt", value<std::string>()->default_value("Contact ID"), "Value of <prompt> </promt> field")
 
		("gateway_responder.label", value<std::string>()->default_value("Enter legacy network contact ID."), "Label for add contact ID field")
 
		("database.type", value<std::string>()->default_value("none"), "Database type.")
 
		("database.database", value<std::string>()->default_value("/var/lib/spectrum2/$jid/database.sql"), "Database used to store data")
 
		("database.server", value<std::string>()->default_value("localhost"), "Database server.")
 
		("database.user", value<std::string>()->default_value(""), "Database user.")
 
		("database.password", value<std::string>()->default_value(""), "Database Password.")
 
		("database.port", value<int>()->default_value(0), "Database port.")
 
		("database.prefix", value<std::string>()->default_value(""), "Prefix of tables in database")
 
		("database.encryption_key", value<std::string>()->default_value(""), "Encryption key.")
 
		("logging.config", value<std::string>()->default_value(""), "Path to log4cxx config file which is used for Spectrum 2 instance")
 
		("logging.backend_config", value<std::string>()->default_value(""), "Path to log4cxx config file which is used for backends")
 
		("backend.default_avatar", value<std::string>()->default_value(""), "Full path to default avatar")
 
		("backend.avatars_directory", value<std::string>()->default_value(""), "Path to directory with avatars")
 
		("backend.no_vcard_fetch", value<bool>()->default_value(false), "True if VCards for buddies should not be fetched. Only avatars will be forwarded.")
 
	;
 

	
 
	// Load configs passed by command line
 
	if (m_argc != 0 && m_argv) {
 
		basic_command_line_parser<char> parser = command_line_parser(m_argc, m_argv).options(opts).allow_unregistered();
 
		parsed_options parsed = parser.run();
 
		store(parsed, m_variables);
 
	}
 

	
 
	parsed_options parsed = parse_config_file(ifs, opts, true);
 

	
 
	bool found_working = false;
 
	bool found_pidfile = false;
 
	bool found_portfile = false;
 
	bool found_backend_port = false;
 
	bool found_database = false;
 
	std::string jid = "";
 
	BOOST_FOREACH(option &opt, parsed.options) {
 
		if (opt.string_key == "service.jid") {
 
			if (_jid.empty()) {
 
				jid = opt.value[0];
 
			}
 
			else {
 
				opt.value[0] = _jid;
 
				jid = _jid;
 
			}
 
		}
 
		else if (opt.string_key == "service.backend_port") {
 
			found_backend_port = true;
 
			if (opt.value[0] == "0") {
 
				opt.value[0] = boost::lexical_cast<std::string>(getRandomPort(_jid.empty() ? jid : _jid));
 
			}
 
		}
 
		else if (opt.string_key == "service.working_dir") {
 
			found_working = true;
 
		}
 
		else if (opt.string_key == "service.pidfile") {
 
			found_pidfile = true;
 
		}
 
		else if (opt.string_key == "service.portfile") {
 
			found_portfile = true;
 
		}
 
		else if (opt.string_key == "database.database") {
 
			found_database = true;
 
		}
 
	}
 

	
 
	if (!found_working) {
 
		std::vector<std::string> value;
 
		value.push_back("/var/lib/spectrum2/$jid");
 
		parsed.options.push_back(boost::program_options::basic_option<char>("service.working_dir", value));
 
	}
 
	if (!found_pidfile) {
 
		std::vector<std::string> value;
 
		value.push_back("/var/run/spectrum2/$jid.pid");
 
		parsed.options.push_back(boost::program_options::basic_option<char>("service.pidfile", value));
 
	}
 
	if (!found_portfile) {
 
		std::vector<std::string> value;
 
		value.push_back("/var/run/spectrum2/$jid.port");
 
		parsed.options.push_back(boost::program_options::basic_option<char>("service.portfile", value));
 
	}
 
	if (!found_backend_port) {
 
		std::vector<std::string> value;
 
		std::string p = boost::lexical_cast<std::string>(getRandomPort(_jid.empty() ? jid : _jid));
 
		value.push_back(p);
 
		parsed.options.push_back(boost::program_options::basic_option<char>("service.backend_port", value));
 
	}
 
	if (!found_database) {
 
		std::vector<std::string> value;
 
		value.push_back("/var/lib/spectrum2/$jid/database.sql");
 
		parsed.options.push_back(boost::program_options::basic_option<char>("database.database", value));
 
	}
 

	
 
	BOOST_FOREACH(option &opt, parsed.options) {
 
		if (opt.unregistered) {
 
			m_unregistered[opt.string_key] = opt.value[0];
 
			m_unregistered[opt.string_key] = variable_value(opt.value[0], false);
 
		}
 
		else if (opt.value[0].find("$jid") != std::string::npos) {
 
			boost::replace_all(opt.value[0], "$jid", jid);
 
		}
 
	}
 

	
 
	store(parsed, m_variables);
 
	notify(m_variables);
 

	
 
	onConfigReloaded();
 

	
 
	return true;
 
}
 

	
 
bool Config::load(std::istream &ifs) {
 
	options_description opts("Transport options");
 
	return load(ifs, opts);
 
}
 

	
 
bool Config::load(const std::string &configfile, const std::string &jid) {
 
	try {
 
		options_description opts("Transport options");
 
		return load(configfile, opts, jid);
 
	} catch ( const boost::program_options::multiple_occurrences& e ) {
 
#if (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 42)
 
		std::cerr << configfile << " parsing error: " << e.what() << " from option: " << e.get_option_name() << std::endl;
 
#else
 
		std::cerr << configfile << " parsing error: " << e.what() << std::endl;
 
#endif
 
		return false;
 
	}
 
}
 

	
 
bool Config::reload() {
 
	if (m_file.empty()) {
 
		return false;
 
	}
 

	
 
	return load(m_file);
 
}
 

	
 
}
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, bool isMUC) : m_conversationManager(conversationManager) {
 
	m_legacyName = legacyName;
 
	m_conversationManager->addConversation(this);
 
	m_muc = isMUC;
 
	m_jid = m_conversationManager->getUser()->getJID().toBare();
 
}
 

	
 
Conversation::~Conversation() {
 
}
 

	
 
void Conversation::setRoom(const std::string &room) {
 
	m_room = room;
 
	m_legacyName = m_room + "/" + m_legacyName;
 
}
 

	
 
void Conversation::handleMessage(boost::shared_ptr<Swift::Message> &message, const std::string &nickname) {
 
	if (m_muc) {
 
		message->setType(Swift::Message::Groupchat);
 
	}
 
	else {
 
		message->setType(Swift::Message::Chat);
 
	}
 

	
 
	std::string n = nickname;
 
	if (n.empty() && !m_room.empty() && !m_muc) {
 
		n = m_nickname;
 
	}
 

	
 
	if (message->getType() != Swift::Message::Groupchat) {
 
		message->setTo(m_jid);
 
		// normal message
 
		if (n.empty()) {
 
			Buddy *buddy = m_conversationManager->getUser()->getRosterManager()->getBuddy(m_legacyName);
 
			if (buddy) {
 
				message->setFrom(buddy->getJID());
 
			}
 
			else {
 
				message->setFrom(Swift::JID(Swift::JID::getEscapedNode(m_legacyName), m_conversationManager->getComponent()->getJID().toBare()));
 
			}
 
		}
 
		// PM message
 
		else {
 
			if (m_room.empty()) {
 
				message->setFrom(Swift::JID(n, m_conversationManager->getComponent()->getJID().toBare(), "user"));
 
			}
 
			else {
 
				message->setFrom(Swift::JID(m_room, m_conversationManager->getComponent()->getJID().toBare(), n));
 
			}
 
		}
 
		m_conversationManager->getComponent()->getStanzaChannel()->sendMessage(message);
 
	}
 
	else {
 
		std::string legacyName = m_legacyName;
 
		if (legacyName.find_last_of("@") != std::string::npos) {
 
			legacyName.replace(legacyName.find_last_of("@"), 1, "%"); // OK
 
		}
 

	
 
		std::string n = nickname;
 
		if (n.empty()) {
 
			n = " ";
 
		}
 
		message->setTo(m_jid);
 
		message->setFrom(Swift::JID(legacyName, m_conversationManager->getComponent()->getJID().toBare(), nickname));
 
		message->setFrom(Swift::JID(legacyName, m_conversationManager->getComponent()->getJID().toBare(), n));
 
		m_conversationManager->getComponent()->getStanzaChannel()->sendMessage(message);
 
	}
 
}
 

	
 
void Conversation::handleParticipantChanged(const std::string &nick, int flag, int status, const std::string &statusMessage, const std::string &newname) {
 
	std::string nickname = nick;
 
	Swift::Presence::ref presence = Swift::Presence::create();
 
	std::string legacyName = m_legacyName;
 
	if (m_muc) {
 
		if (legacyName.find_last_of("@") != std::string::npos) {
 
			legacyName.replace(legacyName.find_last_of("@"), 1, "%"); // OK
 
		}
 
	}
 
	presence->setFrom(Swift::JID(legacyName, m_conversationManager->getComponent()->getJID().toBare(), nickname));
 
	presence->setTo(m_jid);
 
	presence->setType(Swift::Presence::Available);
 

	
 
	if (!statusMessage.empty())
 
		presence->setStatus(statusMessage);
 

	
 
	Swift::StatusShow s((Swift::StatusShow::Type) status);
 

	
 
	if (s.getType() == Swift::StatusShow::None) {
 
		presence->setType(Swift::Presence::Unavailable);
 
	}
 

	
 
	presence->setShow(s.getType());
 

	
 
	Swift::MUCUserPayload *p = new Swift::MUCUserPayload ();
 
	if (m_nickname == nickname) {
 
		Swift::MUCUserPayload::StatusCode c;
 
		c.code = 110;
 
		p->addStatusCode(c);
 
	}
 

	
 
	
 
	Swift::MUCItem item;
 
	
 
	item.affiliation = Swift::MUCOccupant::Member;
 
	item.role = Swift::MUCOccupant::Participant;
 

	
 
	if (flag & Moderator) {
 
		item.affiliation = Swift::MUCOccupant::Admin;
 
		item.role = Swift::MUCOccupant::Moderator;
 
	}
 

	
 
	if (!newname.empty()) {
 
		item.nick = newname;
 
		Swift::MUCUserPayload::StatusCode c;
 
		c.code = 303;
 
		p->addStatusCode(c);
 
		presence->setType(Swift::Presence::Unavailable);
 
	}
 

	
 
	p->addItem(item);
 

	
 
	presence->addPayload(boost::shared_ptr<Swift::Payload>(p));
 
	m_conversationManager->getComponent()->getStanzaChannel()->sendPresence(presence);
 
	if (!newname.empty()) {
 
		handleParticipantChanged(newname, flag, status, statusMessage);
 
	}
 
}
 

	
 
}
src/discoitemsresponder.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 "discoitemsresponder.h"
 
#include "transport/discoitemsresponder.h"
 

	
 
#include <iostream>
 
#include <boost/bind.hpp>
 
#include "Swiften/Queries/IQRouter.h"
 
#include "Swiften/Elements/DiscoItems.h"
 
#include "Swiften/Swiften.h"
 
#include "transport/transport.h"
 
#include "transport/logging.h"
 

	
 
using namespace Swift;
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
DiscoItemsResponder::DiscoItemsResponder(Swift::IQRouter *router) : Swift::GetResponder<DiscoItems>(router) {
 
DEFINE_LOGGER(logger, "DiscoItemsResponder");
 

	
 
DiscoItemsResponder::DiscoItemsResponder(Component *component) : Swift::GetResponder<DiscoItems>(component->getIQRouter()) {
 
	m_component = component;
 
	m_commands = boost::shared_ptr<DiscoItems>(new DiscoItems());
 
	m_commands->setNode("http://jabber.org/protocol/commands");
 
}
 

	
 
DiscoItemsResponder::~DiscoItemsResponder() {
 
	
 
}
 

	
 
void DiscoItemsResponder::addAdHocCommand(const std::string &node, const std::string &name) {
 
	m_commands->addItem(DiscoItems::Item(name, m_component->getJID(), node));
 
}
 

	
 

	
 
bool DiscoItemsResponder::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::DiscoItems> info) {
 
	// presence for transport
 
	if (to.getNode().empty()) {
 
	LOG4CXX_INFO(logger, "get request received with node " << info->getNode());
 
	if (info->getNode() == "http://jabber.org/protocol/commands") {
 
		sendResponse(from, id, m_commands);
 
	}
 
	else if (to.getNode().empty()) {
 
		sendResponse(from, id, boost::shared_ptr<DiscoItems>(new DiscoItems()));
 
	}
 
	// presence for buddy
 
	else {
 
		sendResponse(from, id, boost::shared_ptr<DiscoItems>(new DiscoItems()));
 
	}
 
	return true;
 
}
 

	
 
}
src/mysqlbackend.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
 
 */
 

	
 
#ifdef WITH_MYSQL
 

	
 
#include "transport/mysqlbackend.h"
 
#include "transport/util.h"
 
#include "transport/logging.h"
 
#include <boost/bind.hpp>
 

	
 
#define MYSQL_DB_VERSION 2
 
#define CHECK_DB_RESPONSE(stmt) \
 
	if(stmt) { \
 
		sqlite3_EXEC(m_db, "ROLLBACK;", NULL, NULL, NULL); \
 
		return 0; \
 
	}
 

	
 
// Prepare the SQL statement
 
#define PREP_STMT(sql, str) \
 
	sql = mysql_stmt_init(&m_conn);\
 
	if (mysql_stmt_prepare(sql, std::string(str).c_str(), std::string(str).size())) {\
 
		LOG4CXX_ERROR(logger, str << " " << mysql_error(&m_conn)); \
 
		return false; \
 
	}
 

	
 
// Finalize the prepared statement
 
#define FINALIZE_STMT(prep) \
 
	if(prep != NULL) { \
 
		mysql_stmt_close(prep); \
 
	}
 
	
 
#define BEGIN(STATEMENT, SIZE) 	MYSQL_BIND STATEMENT##_bind[SIZE]; \
 
							memset(STATEMENT##_bind, 0, sizeof(STATEMENT##_bind)); \
 
							int STATEMENT##_id = 1;\
 
							int STATEMENT##_id_get = 0;\
 
							(void)STATEMENT##_id_get;
 

	
 
#define BIND_INT(STATEMENT, VARIABLE) STATEMENT##_bind[STATEMENT##_id].buffer_type= MYSQL_TYPE_LONG;\
 
							STATEMENT##_bind[STATEMENT##_id].buffer= (char *)&VARIABLE;\
 
							STATEMENT##_bind[STATEMENT##_id].is_null= 0;\
 
							STATEMENT##_bind[STATEMENT##_id++].length= 0;
 
#define BIND_STR(STATEMENT, VARIABLE) STATEMENT##_bind[STATEMENT##_id].buffer_type= MYSQL_TYPE_STRING;\
 
							STATEMENT##_bind[STATEMENT##_id].buffer= VARIABLE.c_str();\
 
							STATEMENT##_bind[STATEMENT##_id].buffer_length= STRING_SIZE;\
 
							STATEMENT##_bind[STATEMENT##_id].is_null= 0;\
 
							STATEMENT##_bind[STATEMENT##_id++].length= VARIABLE.size();
 
#define RESET_GET_COUNTER(STATEMENT)	STATEMENT##_id_get = 0;
 
#define GET_INT(STATEMENT)	sqlite3_column_int(STATEMENT, STATEMENT##_id_get++)
 
#define GET_STR(STATEMENT)	(const char *) sqlite3_column_text(STATEMENT, STATEMENT##_id_get++)
 
#define EXECUTE_STATEMENT(STATEMENT, NAME) if (mysql_stmt_bind_param(STATEMENT, STATEMENT##_bind)) { \
 
		LOG4CXX_ERROR(logger, NAME << " " << mysql_error(&m_conn)); \
 
	} \
 
	if (mysql_stmt_execute(STATEMENT)) { \
 
		LOG4CXX_ERROR(logger, NAME << " " << mysql_error(&m_conn)); \
 
	}
 

	
 
#define EXEC(STMT, METHOD) \
 
	{\
 
	int ret = STMT->execute(); \
 
	if (ret == 0) \
 
		exec_ok = true; \
 
	else if (ret == 2013) { \
 
		LOG4CXX_INFO(logger, "MySQL connection lost. Reconnecting...");\
 
		disconnect(); \
 
		connect(); \
 
		return METHOD; \
 
	} \
 
	else \
 
		exec_ok = false; \
 
	}
 

	
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "MySQLBackend");
 
static bool exec_ok;
 

	
 
MySQLBackend::Statement::Statement(MYSQL *conn, const std::string &format, const std::string &statement) {
 
	m_resultOffset = -1;
 
	m_conn = conn;
 
	m_offset = 0;
 
	m_string = statement;
 
	m_stmt = mysql_stmt_init(conn);
 
	if (mysql_stmt_prepare(m_stmt, statement.c_str(), statement.size())) {
 
		LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
 
		return;
 
	}
 

	
 
	for (int i = 0; i < format.length() && m_resultOffset == -1; i++) {
 
		switch (format.at(i)) {
 
			case 's':
 
				m_params.resize(m_params.size() + 1);
 
				memset(&m_params.back(), 0, sizeof(MYSQL_BIND));
 

	
 
				m_params.back().buffer_type= MYSQL_TYPE_STRING;
 
				m_params.back().buffer= (char *) malloc(sizeof(char) * 4096);
 
				m_params.back().buffer_length= 4096;
 
				m_params.back().is_null= 0;
 
				m_params.back().length= (unsigned long *) malloc(sizeof(unsigned long));
 
				break;
 
			case 'i':
 
				m_params.resize(m_params.size() + 1);
 
				memset(&m_params.back(), 0, sizeof(MYSQL_BIND));
 

	
 
				m_params.back().buffer_type= MYSQL_TYPE_LONG;
 
				m_params.back().buffer= (int *) malloc(sizeof(int));
 
				m_params.back().is_null= 0;
 
				m_params.back().length= (unsigned long *) malloc(sizeof(unsigned long));
 
				break;
 
			case 'b':
 
				m_params.resize(m_params.size() + 1);
 
				memset(&m_params.back(), 0, sizeof(MYSQL_BIND));
 

	
 
				m_params.back().buffer_type= MYSQL_TYPE_TINY;
 
				m_params.back().buffer= (int *) malloc(sizeof(int));
 
				m_params.back().is_null= 0;
 
				m_params.back().length= (unsigned long *) malloc(sizeof(unsigned long));
 
				break;
 
			case '|':
 
				m_resultOffset = i;
 
				break;
 
		}
 
	}
 

	
 
	for (int i = m_resultOffset; i >= 0 && i < format.length(); i++) {
 
		switch (format.at(i)) {
 
			case 's':
 
				m_results.resize(m_results.size() + 1);
 
				memset(&m_results.back(), 0, sizeof(MYSQL_BIND));
 

	
 
				m_results.back().buffer_type= MYSQL_TYPE_STRING;
 
				m_results.back().buffer= (char *) malloc(sizeof(char) * 4096);
 
				m_results.back().buffer_length= 4096;
 
				m_results.back().is_null= 0;
 
				m_results.back().length= (unsigned long *) malloc(sizeof(unsigned long));
 
				break;
 
			case 'i':
 
				m_results.resize(m_results.size() + 1);
 
				memset(&m_results.back(), 0, sizeof(MYSQL_BIND));
 

	
 
				m_results.back().buffer_type= MYSQL_TYPE_LONG;
 
				m_results.back().buffer= (int *) malloc(sizeof(int));
 
				m_results.back().is_null= 0;
 
				m_results.back().length= (unsigned long *) malloc(sizeof(unsigned long));
 
				break;
 
			case 'b':
 
				m_results.resize(m_results.size() + 1);
 
				memset(&m_results.back(), 0, sizeof(MYSQL_BIND));
 

	
 
				m_results.back().buffer_type= MYSQL_TYPE_TINY;
 
				m_results.back().buffer= (int *) malloc(sizeof(int));
 
				m_results.back().is_null= 0;
 
				m_results.back().length= (unsigned long *) malloc(sizeof(unsigned long));
 
				break;
 
		}
 
	}
 

	
 
	if (mysql_stmt_bind_param(m_stmt, &m_params.front())) {
 
		LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
 
	}
 

	
 
	if (m_resultOffset < 0)
 
		m_resultOffset = format.size();
 
	else {
 
		if (mysql_stmt_bind_result(m_stmt, &m_results.front())) {
 
			LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
 
		}
 
	}
 
	m_resultOffset = 0;
 
}
 

	
 
MySQLBackend::Statement::~Statement() {
 
	for (int i = 0; i < m_params.size(); i++) {
 
		free(m_params[i].buffer);
 
		free(m_params[i].length);
 
	}
 
	for (int i = 0; i < m_results.size(); i++) {
 
		free(m_results[i].buffer);
 
		free(m_results[i].length);
 
	}
 
	FINALIZE_STMT(m_stmt);
 
}
 

	
 
int MySQLBackend::Statement::execute() {
 
	// If statement has some input and doesn't have any output, we have
 
	// to clear the offset now, because operator>> will not be called.
 
	m_offset = 0;
 
	m_resultOffset = 0;
 
	int ret;
 

	
 
	if ((ret = mysql_stmt_execute(m_stmt)) != 0) {
 
		LOG4CXX_ERROR(logger, m_string << " " << mysql_stmt_error(m_stmt) << "; " << mysql_error(m_conn));
 
		return mysql_stmt_errno(m_stmt);
 
	}
 
	return 0;
 
}
 

	
 
int MySQLBackend::Statement::fetch() {
 
	return mysql_stmt_fetch(m_stmt);
 
}
 

	
 
template <typename T>
 
MySQLBackend::Statement& MySQLBackend::Statement::operator << (const T& t) {
 
	if (m_offset >= m_params.size())
 
		return *this;
 
	int *data = (int *) m_params[m_offset].buffer;
 
	*data = (int) t;
 

	
 
// 	LOG4CXX_INFO(logger, "adding " << m_offset << ":" << (int) t);
 
	m_offset++;
 
	return *this;
 
}
 

	
 
MySQLBackend::Statement& MySQLBackend::Statement::operator << (const std::string& str) {
 
	if (m_offset >= m_params.size())
 
		return *this;
 
// 	LOG4CXX_INFO(logger, "adding " << m_offset << ":" << str << "(" << str.size() << ")");
 
	strncpy((char*) m_params[m_offset].buffer, str.c_str(), 4096);
 
	*m_params[m_offset].length = str.size();
 
	m_offset++;
 
	return *this;
 
}
 

	
 
template <typename T>
 
MySQLBackend::Statement& MySQLBackend::Statement::operator >> (T& t) {
 
	if (m_resultOffset > m_results.size())
 
		return *this;
 

	
 
	if (!m_results[m_resultOffset].is_null) {
 
		int *data = (int *) m_results[m_resultOffset].buffer;
 
		t = (int) *data;
 
// 		std::cout << "getting " << m_resultOffset << " " << (int) t << "\n";
 
	}
 

	
 
	if (++m_resultOffset == m_results.size())
 
		m_resultOffset = 0;
 
	return *this;
 
}
 

	
 
MySQLBackend::Statement& MySQLBackend::Statement::operator >> (std::string& t) {
 
// 	std::cout << "getting " << m_resultOffset << "\n";
 
	if (m_resultOffset > m_results.size())
 
		return *this;
 

	
 
	if (!m_results[m_resultOffset].is_null) {
 
		t = (char *) m_results[m_resultOffset].buffer;
 
	}
 

	
 
	if (++m_resultOffset == m_results.size())
 
		m_resultOffset = 0;
 
	return *this;
 
}
 

	
 
MySQLBackend::MySQLBackend(Config *config) {
 
	m_config = config;
 
	m_prefix = CONFIG_STRING(m_config, "database.prefix");
 
	mysql_init(&m_conn);
 
	my_bool my_true = 1;
 
	mysql_options(&m_conn, MYSQL_OPT_RECONNECT, &my_true);
 
}
 

	
 
MySQLBackend::~MySQLBackend(){
 
	disconnect();
 
}
 

	
 
void MySQLBackend::disconnect() {
 
	LOG4CXX_INFO(logger, "Disconnecting");
 
	delete m_setUser;
 
	delete m_getUser;
 
	delete m_removeUser;
 
	delete m_removeUserBuddies;
 
	delete m_removeUserSettings;
 
	delete m_removeUserBuddiesSettings;
 
	delete m_addBuddy;
 
	delete m_updateBuddy;
 
	delete m_getBuddies;
 
	delete m_getBuddiesSettings;
 
	delete m_getUserSetting;
 
	delete m_setUserSetting;
 
	delete m_updateUserSetting;
 
	delete m_updateBuddySetting;
 
	delete m_setUserOnline;
 
	delete m_getOnlineUsers;
 
	mysql_close(&m_conn);
 
}
 

	
 
bool MySQLBackend::connect() {
 
	LOG4CXX_INFO(logger, "Connecting MySQL server " << CONFIG_STRING(m_config, "database.server") << ", user " <<
 
		CONFIG_STRING(m_config, "database.user") << ", database " << CONFIG_STRING(m_config, "database.database") <<
 
		", port " << CONFIG_INT(m_config, "database.port")
 
	);
 

	
 
	if (!mysql_real_connect(&m_conn, CONFIG_STRING(m_config, "database.server").c_str(),
 
					   CONFIG_STRING(m_config, "database.user").c_str(),
 
					   CONFIG_STRING(m_config, "database.password").c_str(),
 
					   CONFIG_STRING(m_config, "database.database").c_str(),
 
					   CONFIG_INT(m_config, "database.port"), NULL, 0)) {
 
		LOG4CXX_ERROR(logger, "Can't connect database: " << mysql_error(&m_conn));
 
		return false;
 
	}
 

	
 
	if (!mysql_set_character_set(&m_conn, "utf8")) {
 
		LOG4CXX_INFO(logger, "New client character set: " << mysql_character_set_name(&m_conn));
 
	}
 

	
 
	createDatabase();
 

	
 
	m_setUser = new Statement(&m_conn, "sssssbss", "INSERT INTO " + m_prefix + "users (jid, uin, password, language, encoding, last_login, vip) VALUES (?, ?, ?, ?, ?, NOW(), ?) ON DUPLICATE KEY UPDATE uin=?, password=?");
 
	m_getUser = new Statement(&m_conn, "s|isssssb", "SELECT id, jid, uin, password, encoding, language, vip FROM " + m_prefix + "users WHERE jid=?");
 

	
 
	m_removeUser = new Statement(&m_conn, "i", "DELETE FROM " + m_prefix + "users WHERE id=?");
 
	m_removeUserBuddies = new Statement(&m_conn, "i", "DELETE FROM " + m_prefix + "buddies WHERE user_id=?");
 
	m_removeUserSettings = new Statement(&m_conn, "i", "DELETE FROM " + m_prefix + "users_settings WHERE user_id=?");
 
	m_removeUserBuddiesSettings = new Statement(&m_conn, "i", "DELETE FROM " + m_prefix + "buddies_settings WHERE user_id=?");
 

	
 
	m_addBuddy = new Statement(&m_conn, "issssi", "INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES (?, ?, ?, ?, ?, ?)");
 
	m_updateBuddy = new Statement(&m_conn, "ssisis", "UPDATE " + m_prefix + "buddies SET groups=?, nickname=?, flags=?, subscription=? WHERE user_id=? AND uin=?");
 
	m_getBuddies = new Statement(&m_conn, "i|issssi", "SELECT id, uin, subscription, nickname, groups, flags FROM " + m_prefix + "buddies WHERE user_id=? ORDER BY id ASC");
 
	m_getBuddiesSettings = new Statement(&m_conn, "i|iiss", "SELECT buddy_id, type, var, value FROM " + m_prefix + "buddies_settings WHERE user_id=? ORDER BY buddy_id ASC");
 
	m_updateBuddySetting = new Statement(&m_conn, "iisiss", "INSERT INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE value=?");
 
	
 
	m_getUserSetting = new Statement(&m_conn, "is|is", "SELECT type, value FROM " + m_prefix + "users_settings WHERE user_id=? AND var=?");
 
	m_setUserSetting = new Statement(&m_conn, "isis", "INSERT INTO " + m_prefix + "users_settings (user_id, var, type, value) VALUES (?,?,?,?)");
 
	m_updateUserSetting = new Statement(&m_conn, "sis", "UPDATE " + m_prefix + "users_settings SET value=? WHERE user_id=? AND var=?");
 

	
 
	m_setUserOnline = new Statement(&m_conn, "bi", "UPDATE " + m_prefix + "users SET online=?, last_login=NOW()  WHERE id=?");
 
	m_getOnlineUsers = new Statement(&m_conn, "|s", "SELECT jid FROM " + m_prefix + "users WHERE online=1");
 

	
 
	return true;
 
}
 

	
 
bool MySQLBackend::createDatabase() {
 
	int not_exist = exec("CREATE TABLE IF NOT EXISTS `" + m_prefix + "buddies` ("
 
							"`id` int(10) unsigned NOT NULL auto_increment,"
 
							"`user_id` int(10) unsigned NOT NULL,"
 
							"`uin` varchar(255) collate utf8_bin NOT NULL,"
 
							"`subscription` enum('to','from','both','ask','none') collate utf8_bin NOT NULL,"
 
							"`nickname` varchar(255) collate utf8_bin NOT NULL,"
 
							"`groups` varchar(255) collate utf8_bin NOT NULL,"
 
							"`flags` smallint(4) NOT NULL DEFAULT '0',"
 
							"PRIMARY KEY (`id`),"
 
							"UNIQUE KEY `user_id` (`user_id`,`uin`)"
 
						") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;");
 

	
 
	if (not_exist) {
 
		exec("CREATE TABLE IF NOT EXISTS `" + m_prefix + "buddies_settings` ("
 
				"`user_id` int(10) unsigned NOT NULL,"
 
				"`buddy_id` int(10) unsigned NOT NULL,"
 
				"`var` varchar(50) collate utf8_bin NOT NULL,"
 
				"`type` smallint(4) unsigned NOT NULL,"
 
				"`value` varchar(255) collate utf8_bin NOT NULL,"
 
				"PRIMARY KEY (`buddy_id`,`var`),"
 
				"KEY `user_id` (`user_id`)"
 
			") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;");
 
 
 
		exec("CREATE TABLE IF NOT EXISTS `" + m_prefix + "users` ("
 
				"`id` int(10) unsigned NOT NULL auto_increment,"
 
				"`jid` varchar(255) collate utf8_bin NOT NULL,"
 
				"`uin` varchar(4095) collate utf8_bin NOT NULL,"
 
				"`password` varchar(255) collate utf8_bin NOT NULL,"
 
				"`language` varchar(25) collate utf8_bin NOT NULL,"
 
				"`encoding` varchar(50) collate utf8_bin NOT NULL default 'utf8',"
 
				"`last_login` datetime,"
 
				"`vip` tinyint(1) NOT NULL  default '0',"
 
				"`online` tinyint(1) NOT NULL  default '0',"
 
				"PRIMARY KEY (`id`),"
 
				"UNIQUE KEY `jid` (`jid`)"
 
			") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;");
 

	
 
		exec("CREATE TABLE IF NOT EXISTS `" + m_prefix + "users_settings` ("
 
				"`user_id` int(10) unsigned NOT NULL,"
 
				"`var` varchar(50) collate utf8_bin NOT NULL,"
 
				"`type` smallint(4) unsigned NOT NULL,"
 
				"`value` varchar(255) collate utf8_bin NOT NULL,"
 
				"PRIMARY KEY (`user_id`,`var`),"
 
				"KEY `user_id` (`user_id`)"
 
			") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;");
 

	
 
		exec("CREATE TABLE IF NOT EXISTS `" + m_prefix + "db_version` ("
 
				"`ver` int(10) unsigned NOT NULL default '1',"
 
				"UNIQUE KEY `ver` (`ver`)"
 
			") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;");
 

	
 
		exec("INSERT IGNORE INTO db_version (ver) VALUES ('2');");
 
	}
 

	
 
	return true;
 
}
 

	
 
bool MySQLBackend::exec(const std::string &query) {
 
	if (mysql_query(&m_conn, query.c_str())) {
 
		LOG4CXX_ERROR(logger, query << " " << mysql_error(&m_conn));
 
		return false;
 
	}
 
	return true;
 
}
 

	
 
void MySQLBackend::setUser(const UserInfo &user) {
 
	std::string encrypted = user.password;
 
	if (!CONFIG_STRING(m_config, "database.encryption_key").empty()) {
 
		encrypted = Util::encryptPassword(encrypted, CONFIG_STRING(m_config, "database.encryption_key"));
 
	}
 
	*m_setUser << user.jid << user.uin << encrypted << user.language << user.encoding << user.vip << user.uin << encrypted;
 
	EXEC(m_setUser, setUser(user));
 
}
 

	
 
bool MySQLBackend::getUser(const std::string &barejid, UserInfo &user) {
 
	*m_getUser << barejid;
 
	EXEC(m_getUser, getUser(barejid, user));
 
	if (!exec_ok)
 
		return false;
 

	
 
	int ret = false;
 
	while (m_getUser->fetch() == 0) {
 
		ret = true;
 
		*m_getUser >> user.id >> user.jid >> user.uin >> user.password >> user.encoding >> user.language >> user.vip;
 

	
 
		if (!CONFIG_STRING(m_config, "database.encryption_key").empty()) {
 
			user.password = Util::decryptPassword(user.password, CONFIG_STRING(m_config, "database.encryption_key"));
 
		}
 
	}
 

	
 
	return ret;
 
}
 

	
 
void MySQLBackend::setUserOnline(long id, bool online) {
 
	*m_setUserOnline << online << id;
 
	EXEC(m_setUserOnline, setUserOnline(id, online));
 
}
 

	
 
bool MySQLBackend::getOnlineUsers(std::vector<std::string> &users) {
 
	EXEC(m_getOnlineUsers, getOnlineUsers(users));
 
	if (!exec_ok)
 
		return false;
 

	
 
	std::string jid;
 
	while (m_getOnlineUsers->fetch() == 0) {
 
		*m_getOnlineUsers >> jid;
 
		users.push_back(jid);
 
	}
 

	
 
	return true;
 
}
 

	
 
long MySQLBackend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
 
// 	"INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES (?, ?, ?, ?, ?, ?)"
 
	std::string groups = Util::serializeGroups(buddyInfo.groups);
 
	*m_addBuddy << userId << buddyInfo.legacyName << buddyInfo.subscription;
 
	*m_addBuddy << groups;
 
	*m_addBuddy << buddyInfo.alias << buddyInfo.flags;
 

	
 
	EXEC(m_addBuddy, addBuddy(userId, buddyInfo));
 

	
 
	long id = (long) mysql_insert_id(&m_conn);
 

	
 
// 	INSERT OR REPLACE INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES (?, ?, ?, ?, ?)
 
	if (buddyInfo.settings.find("icon_hash") != buddyInfo.settings.end() && !buddyInfo.settings.find("icon_hash")->second.s.empty()) {
 
		*m_updateBuddySetting << userId << id << buddyInfo.settings.find("icon_hash")->first << (int) TYPE_STRING << buddyInfo.settings.find("icon_hash")->second.s << buddyInfo.settings.find("icon_hash")->second.s;
 
		EXEC(m_updateBuddySetting, addBuddy(userId, buddyInfo));
 
	}
 

	
 
	return id;
 
}
 

	
 
void MySQLBackend::updateBuddy(long userId, const BuddyInfo &buddyInfo) {
 
// 	"UPDATE " + m_prefix + "buddies SET groups=?, nickname=?, flags=?, subscription=? WHERE user_id=? AND uin=?"
 
	std::string groups = Util::serializeGroups(buddyInfo.groups);
 
	*m_updateBuddy << groups;
 
	*m_updateBuddy << buddyInfo.alias << buddyInfo.flags << buddyInfo.subscription;
 
	*m_updateBuddy << userId << buddyInfo.legacyName;
 

	
 
	EXEC(m_updateBuddy, updateBuddy(userId, buddyInfo));
 
}
 

	
 
bool MySQLBackend::getBuddies(long id, std::list<BuddyInfo> &roster) {
 
//	SELECT id, uin, subscription, nickname, groups, flags FROM " + m_prefix + "buddies WHERE user_id=? ORDER BY id ASC
 
	*m_getBuddies << id;
 

	
 
// 	"SELECT buddy_id, type, var, value FROM " + m_prefix + "buddies_settings WHERE user_id=? ORDER BY buddy_id ASC"
 
	*m_getBuddiesSettings << id;
 

	
 
	SettingVariableInfo var;
 
	long buddy_id = -1;
 
	std::string key;
 

	
 
	EXEC(m_getBuddies, getBuddies(id, roster));
 
	if (!exec_ok)
 
		return false;
 

	
 
	while (m_getBuddies->fetch() == 0) {
 
		BuddyInfo b;
 

	
 
		std::string group;
 
		*m_getBuddies >> b.id >> b.legacyName >> b.subscription >> b.alias >> group >> b.flags;
 

	
 
		if (!group.empty()) {
 
			b.groups = Util::deserializeGroups(group);
 
		}
 

	
 
		roster.push_back(b);
 
	}
 

	
 
	EXEC(m_getBuddiesSettings, getBuddies(id, roster));
 
	if (!exec_ok)
 
		return false;
 

	
 
	BOOST_FOREACH(BuddyInfo &b, roster) {
 
		if (buddy_id == b.id) {
 
// 			std::cout << "Adding buddy info setting " << key << "\n";
 
			b.settings[key] = var;
 
			buddy_id = -1;
 
		}
 

	
 
		while(buddy_id == -1 && m_getBuddiesSettings->fetch() == 0) {
 
			std::string val;
 
			*m_getBuddiesSettings >> buddy_id >> var.type >> key >> val;
 

	
 
			switch (var.type) {
 
				case TYPE_BOOLEAN:
 
					var.b = atoi(val.c_str());
 
					break;
 
				case TYPE_STRING:
 
					var.s = val;
 
					break;
 
				default:
 
					if (buddy_id == b.id) {
 
						buddy_id = -1;
 
					}
 
					continue;
 
					break;
 
			}
 
			if (buddy_id == b.id) {
 
// 				std::cout << "Adding buddy info setting " << key << "=" << val << "\n";
 
				b.settings[key] = var;
 
				buddy_id = -1;
 
			}
 
		}
 
	}
 

	
 
	while(m_getBuddiesSettings->fetch() == 0) {
 
		// TODO: probably remove those settings, because there's no buddy for them.
 
		// It should not happend, but one never know...
 
	}
 
	
 
	return true;
 
}
 

	
 
bool MySQLBackend::removeUser(long id) {
 
	*m_removeUser << (int) id;
 
	EXEC(m_removeUser, removeUser(id));
 
	if (!exec_ok)
 
		return false;
 

	
 
	*m_removeUserSettings << (int) id;
 
	EXEC(m_removeUserSettings, removeUser(id));
 
	if (!exec_ok)
 
		return false;
 

	
 
	*m_removeUserBuddies << (int) id;
 
	EXEC(m_removeUserBuddies, removeUser(id));
 
	if (!exec_ok)
 
		return false;
 

	
 
	*m_removeUserBuddiesSettings << (int) id;
 
	EXEC(m_removeUserBuddiesSettings, removeUser(id));
 
	if (!exec_ok)
 
		return false;
 

	
 
	return true;
 
}
 

	
 
void MySQLBackend::getUserSetting(long id, const std::string &variable, int &type, std::string &value) {
 
// 	"SELECT type, value FROM " + m_prefix + "users_settings WHERE user_id=? AND var=?"
 
	*m_getUserSetting << id << variable;
 
	EXEC(m_getUserSetting, getUserSetting(id, variable, type, value));
 
	if (m_getUserSetting->fetch() != 0) {
 
// 		"INSERT INTO " + m_prefix + "users_settings (user_id, var, type, value) VALUES (?,?,?,?)"
 
		*m_setUserSetting << id << variable << type << value;
 
		EXEC(m_setUserSetting, getUserSetting(id, variable, type, value));
 
	}
 
	else {
 
		*m_getUserSetting >> type >> value;
 
	}
 

	
 
	while (m_getUserSetting->fetch() == 0) {
 

	
 
	}
 
}
 

	
 
void MySQLBackend::updateUserSetting(long id, const std::string &variable, const std::string &value) {
 
// 	"UPDATE " + m_prefix + "users_settings SET value=? WHERE user_id=? AND var=?"
 
	*m_updateUserSetting << value << id << variable;
 
	EXEC(m_updateUserSetting, updateUserSetting(id, variable, value));
 
}
 

	
 
void MySQLBackend::beginTransaction() {
 
	//exec("START TRANSACTION;");
 
	exec("START TRANSACTION;");
 
}
 

	
 
void MySQLBackend::commitTransaction() {
 
	//exec("COMMIT;");
 
	exec("COMMIT;");
 
}
 

	
 
}
 

	
 
#endif
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 "transport/logging.h"
 
#include "transport/admininterface.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 "Swiften/Elements/SpectrumErrorPayload.h"
 
#include "transport/protocol.pb.h"
 

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

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

	
 
namespace Transport {
 

	
 
static unsigned long backend_id;
 
static unsigned long bytestream_id;
 

	
 
DEFINE_LOGGER(logger, "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;
 
		}
 

	
 
		// Creates new LocalBuddy
 
		Buddy *createBuddy(RosterManager *rosterManager, const BuddyInfo &buddyInfo) {
 
			LocalBuddy *buddy = new LocalBuddy(rosterManager, buddyInfo.id);
 
			buddy->setAlias(buddyInfo.alias);
 
			buddy->setName(buddyInfo.legacyName);
 
			if (buddyInfo.subscription == "both") {
 
				buddy->setSubscription(Buddy::Both);
 
			}
 
			else {
 
				buddy->setSubscription(Buddy::Ask);
 
			}
 
			buddy->setGroups(buddyInfo.groups);
 
			buddy->setFlags((BuddyFlag) (buddyInfo.flags));
 
			if (buddyInfo.settings.find("icon_hash") != buddyInfo.settings.end())
 
				buddy->setIconHash(buddyInfo.settings.find("icon_hash")->second.s);
 
			return buddy;
 
		}
 

	
 
	private:
 
		NetworkPluginServer *m_nps;
 
};
 

	
 
// Wraps google protobuf payload into WrapperMessage and serialize it to string
 
#define WRAP(MESSAGE, TYPE) 	pbnetwork::WrapperMessage wrap; \
 
	wrap.set_type(TYPE); \
 
	wrap.set_payload(MESSAGE); \
 
	wrap.SerializeToString(&MESSAGE);
 

	
 
// Executes new backend
 
static unsigned long exec_(std::string path, const char *host, const char *port, const char *config) {
 
	std::string original_path = path;
 
	// BACKEND_ID is replaced with unique ID. The ID is increasing for every backend.
 
	boost::replace_all(path, "BACKEND_ID", boost::lexical_cast<std::string>(backend_id++));
 

	
 
	// Add host and port.
 
	path += std::string(" --host ") + host + " --port " + port + " " + config;
 
	LOG4CXX_INFO(logger, "Starting new backend " << path);
 

	
 
#ifdef _WIN32
 
	STARTUPINFO         si;
 
	PROCESS_INFORMATION pi;
 

	
 
	ZeroMemory (&si, sizeof(si));
 
	si.cb=sizeof (si);
 

	
 
	if (! CreateProcess(
 
	NULL,
 
	(LPSTR)path.c_str(),         // command line
 
	0,                    // process attributes
 
	0,                    // thread attributes
 
	0,                    // inherit handles
 
	0,                    // creation flags
 
	0,                    // environment
 
	0,                    // cwd
 
	&si,
 
	&pi
 
	)
 
	)  {
 
		LOG4CXX_ERROR(logger, "Could not start process");
 
	}
 

	
 
	return 0;
 
#else
 
	// Create array of char * from string using -lpopt library
 
	char *p = (char *) malloc(path.size() + 1);
 
	strcpy(p, path.c_str());
 
	int argc;
 
	char **argv;
 
	poptParseArgvString(p, &argc, (const char ***) &argv);
 

	
 
	// fork and exec
 
	pid_t pid = fork();
 
	if ( pid == 0 ) {
 
		setsid();
 
		// child process
 
		errno = 0;
 
		int ret = execv(argv[0], argv);
 
		if (ret == -1) {
 
			exit(errno);
 
		}
 
		exit(0);
 
	} else if ( pid < 0 ) {
 
		LOG4CXX_ERROR(logger, "Fork failed");
 
	}
 
	free(p);
 

	
 
	return (unsigned long) pid;
 
#endif
 
}
 

	
 
#ifndef _WIN32
 
static void SigCatcher(int n) {
 
	pid_t result;
 
	int status;
 
	// Read exit code from all children to not have zombies arround
 
	// WARNING: Do not put LOG4CXX_ here, because it can lead to deadlock
 
	while ((result = waitpid(-1, &status, WNOHANG)) > 0) {
 
		if (result != 0) {
 
			if (WIFEXITED(status)) {
 
				if (WEXITSTATUS(status) != 0) {
 
// 					LOG4CXX_ERROR(logger, "Backend can not be started, exit_code=" << WEXITSTATUS(status));
 
				}
 
			}
 
			else {
 
// 				LOG4CXX_ERROR(logger, "Backend can not be started");
 
			}
 
		}
 
	}
 
}
 
#endif
 

	
 
static void handleBuddyPayload(LocalBuddy *buddy, const pbnetwork::Buddy &payload) {
 
	buddy->setName(payload.buddyname());
 
	// Set alias only if it's not empty. Backends are allowed to send empty alias if it has
 
	// not changed.
 
	if (!payload.alias().empty()) {
 
		LOG4CXX_INFO(logger, "Setting alias to " << payload.alias() << " " << buddy->getAlias());
 
		buddy->setAlias(payload.alias());
 
	}
 

	
 
	// Change groups if it's not empty. The same as above...
 
	std::vector<std::string> groups;
 
	for (int i = 0; i < payload.group_size(); i++) {
 
		groups.push_back(payload.group(i));
 
	}
 
	if (!groups.empty()) {
 
		buddy->setGroups(groups);
 
	}
 

	
 
	buddy->setStatus(Swift::StatusShow((Swift::StatusShow::Type) payload.status()), payload.statusmessage());
 
	buddy->setIconHash(payload.iconhash());
 
	buddy->setBlocked(payload.blocked());
 
}
 

	
 
NetworkPluginServer::NetworkPluginServer(Component *component, Config *config, UserManager *userManager, FileTransferManager *ftManager) {
 
	m_ftManager = ftManager;
 
	m_userManager = userManager;
 
	m_config = config;
 
	m_component = component;
 
	m_isNextLongRun = false;
 
	m_adminInterface = NULL;
 
	m_startingBackend = false;
 
	m_component->m_factory = new NetworkFactory(this);
 
	m_userManager->onUserCreated.connect(boost::bind(&NetworkPluginServer::handleUserCreated, this, _1));
 
	m_userManager->onUserDestroyed.connect(boost::bind(&NetworkPluginServer::handleUserDestroyed, this, _1));
 

	
 
	m_pingTimer = component->getNetworkFactories()->getTimerFactory()->createTimer(20000);
 
	m_pingTimer->onTick.connect(boost::bind(&NetworkPluginServer::pingTimeout, this));
 
	m_pingTimer->start();
 

	
 
	if (CONFIG_INT(m_config, "service.memory_collector_time") != 0) {
 
		m_collectTimer = component->getNetworkFactories()->getTimerFactory()->createTimer(CONFIG_INT(m_config, "service.memory_collector_time"));
 
		m_collectTimer->onTick.connect(boost::bind(&NetworkPluginServer::collectBackend, this));
 
		m_collectTimer->start();
 
	}
 

	
 
	m_vcardResponder = new VCardResponder(component->getIQRouter(), component->getNetworkFactories(), userManager);
 
	m_vcardResponder->onVCardRequired.connect(boost::bind(&NetworkPluginServer::handleVCardRequired, this, _1, _2, _3));
 
	m_vcardResponder->onVCardUpdated.connect(boost::bind(&NetworkPluginServer::handleVCardUpdated, this, _1, _2));
 
	m_vcardResponder->start();
 

	
 
	m_rosterResponder = new RosterResponder(component->getIQRouter(), userManager);
 
	m_rosterResponder->onBuddyAdded.connect(boost::bind(&NetworkPluginServer::handleBuddyAdded, this, _1, _2));
 
	m_rosterResponder->onBuddyRemoved.connect(boost::bind(&NetworkPluginServer::handleBuddyRemoved, this, _1));
 
	m_rosterResponder->onBuddyUpdated.connect(boost::bind(&NetworkPluginServer::handleBuddyUpdated, this, _1, _2));
 
	m_rosterResponder->start();
 

	
 
	m_blockResponder = new BlockResponder(component->getIQRouter(), userManager);
 
	m_blockResponder->onBlockToggled.connect(boost::bind(&NetworkPluginServer::handleBlockToggled, this, _1));
 
	m_blockResponder->start();
 

	
 
	m_server = component->getNetworkFactories()->getConnectionServerFactory()->createConnectionServer(Swift::HostAddress(CONFIG_STRING(m_config, "service.backend_host")), boost::lexical_cast<int>(CONFIG_STRING(m_config, "service.backend_port")));
 
	m_server->onNewConnection.connect(boost::bind(&NetworkPluginServer::handleNewClientConnection, this, _1));
 
	m_server->start();
 

	
 
	LOG4CXX_INFO(logger, "Listening on host " << CONFIG_STRING(m_config, "service.backend_host") << " port " << CONFIG_STRING(m_config, "service.backend_port"));
 

	
 
	unsigned long pid = exec_(CONFIG_STRING(m_config, "service.backend"), CONFIG_STRING(m_config, "service.backend_host").c_str(), CONFIG_STRING(m_config, "service.backend_port").c_str(), m_config->getConfigFile().c_str());
 
	LOG4CXX_INFO(logger, "Tried to spawn first backend with pid " << pid);
 
	LOG4CXX_INFO(logger, "Backend should now connect to Spectrum2 instance. Spectrum2 won't accept any connection before backend connects");
 

	
 
#ifndef _WIN32
 
	// wait if the backend process will still be alive after 1 second
 
	sleep(1);
 
	pid_t result;
 
	int status;
 
	result = waitpid(-1, &status, WNOHANG);
 
	if (result != 0) {
 
		if (WIFEXITED(status)) {
 
			if (WEXITSTATUS(status) != 0) {
 
				LOG4CXX_ERROR(logger, "Backend can not be started, exit_code=" << WEXITSTATUS(status) << ", possible error: " << strerror(WEXITSTATUS(status)));
 
			}
 
		}
 
		else {
 
			LOG4CXX_ERROR(logger, "Backend can not be started");
 
		}
 
	}
 

	
 
	signal(SIGCHLD, SigCatcher);
 
#endif
 

	
 
}
 

	
 
NetworkPluginServer::~NetworkPluginServer() {
 
	for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
		LOG4CXX_INFO(logger, "Stopping backend " << *it);
 
		std::string message;
 
		pbnetwork::WrapperMessage wrap;
 
		wrap.set_type(pbnetwork::WrapperMessage_Type_TYPE_EXIT);
 
		wrap.SerializeToString(&message);
 

	
 
		Backend *c = (Backend *) *it;
 
		send(c->connection, message);
 
	}
 

	
 
	m_pingTimer->stop();
 
	m_server->stop();
 
	m_server.reset();
 
	delete m_component->m_factory;
 
	delete m_vcardResponder;
 
	delete m_rosterResponder;
 
	delete m_blockResponder;
 
}
 

	
 
void NetworkPluginServer::handleNewClientConnection(boost::shared_ptr<Swift::Connection> c) {
 
	// Create new Backend instance
 
	Backend *client = new Backend;
 
	client->pongReceived = -1;
 
	client->connection = c;
 
	client->res = 0;
 
	client->init_res = 0;
 
	client->shared = 0;
 
	client->willDie = 0;
 
	// Until we receive first PONG from backend, backend is in willDie state.
 
	client->willDie = true;
 
	// Backend does not accept new clients automatically if it's long-running
 
	client->acceptUsers = !m_isNextLongRun;
 
	client->longRun = m_isNextLongRun;
 

	
 
	LOG4CXX_INFO(logger, "New" + (client->longRun ? std::string(" long-running") : "") +  " backend " << client << " connected. Current backend count=" << (m_clients.size() + 1));
 
	m_startingBackend = false;
 

	
 
	if (m_clients.size() == 0) {
 
		// first backend connected, start the server, we're ready.
 
		m_component->start();
 
	}
 
	LOG4CXX_INFO(logger, "New" + (client->longRun ? std::string(" long-running") : "") +  " backend " << client << " connected. Current backend count=" << (m_clients.size() + 1));
 

	
 
	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 << " (ID=" << c->id << ") disconnected. Current backend count=" << (m_clients.size() - 1));
 

	
 
	// This backend will do, so we can't reconnect users to it in User::handleDisconnected call
 
	c->willDie = true;
 

	
 
	// If there are users associated with this backend, it must have crashed, so print error output
 
	// and disconnect users
 
	if (!c->users.empty()) {
 
		m_crashedBackends.push_back(c->id);
 
	}
 

	
 
	for (std::list<User *>::const_iterator it = c->users.begin(); it != c->users.end(); it++) {
 
		LOG4CXX_ERROR(logger, "Backend " << c << " (ID=" << c->id << ") 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) {
 
		LOG4CXX_ERROR(logger, "Connected payload received for unknown user " << payload.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(), payload.message());
 

	
 
	User *user = m_userManager->getUser(payload.user());
 
	if (!user) {
 
		return;
 
	}
 
	user->handleDisconnected(payload.message(), (Swift::SpectrumErrorPayload::Error) payload.error());
 
}
 

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

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

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

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

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

	
 
	// Create subscribe presence and forward it to XMPP side
 
	Swift::Presence::ref response = Swift::Presence::create();
 
	response->setTo(user->getJID());
 
	std::string name = payload.buddyname();
 

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

	
 
// 	if (name.find_last_of("@") != std::string::npos) { // OK when commented
 
// 		name.replace(name.find_last_of("@"), 1, "%"); // OK when commented
 
// 	}
 

	
 
	response->setFrom(Swift::JID(name, m_component->getJID().toString()));
 
	response->setType(Swift::Presence::Subscribe);
 
	m_component->getStanzaChannel()->sendPresence(response);
 
}
 

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

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

	
 
	// We're not creating new Conversation just because of chatstates.
 
	// Some networks/clients spams with chatstates a lot and it leads to bigger memory usage.
 
	NetworkConversation *conv = (NetworkConversation *) user->getConversationManager()->getConversation(payload.buddyname());
 
	if (!conv) {
 
		return;
 
	}
 

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

	
 
	conv->handleMessage(msg);
 
}
 

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

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

	
 
	LOG4CXX_INFO(logger, "HANDLE BUDDY CHANGED " << payload.buddyname() << "-" << payload.alias());
 

	
 
	LocalBuddy *buddy = (LocalBuddy *) user->getRosterManager()->getBuddy(payload.buddyname());
 
	if (buddy) {
 
		handleBuddyPayload(buddy, payload);
 
		buddy->handleBuddyChanged();
 
	}
 
	else {
 
		buddy = new LocalBuddy(user->getRosterManager(), -1);
 
		buddy->setFlags(BUDDY_JID_ESCAPING);
 
		handleBuddyPayload(buddy, payload);
 
		user->getRosterManager()->setBuddy(buddy);
 
	}
 
}
 

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

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

	
 
	user->getRosterManager()->removeBuddy(payload.buddyname());
 
}
 

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

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

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

	
 
	conv->handleParticipantChanged(payload.nickname(), payload.flag(), payload.status(), payload.statusmessage(), payload.newname());
 
}
 

	
 
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, bool subject) {
 
	pbnetwork::ConversationMessage payload;
 

	
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

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

	
 
	// Message from legacy network triggers network acticity
 
	user->updateLastActivity();
 

	
 
	// Set proper body.
 
	boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
	if (subject) {
 
		msg->setSubject(payload.message());
 
	}
 
	else {
 
		msg->setBody(payload.message());
 
	}
 

	
 
	// Add xhtml-im payload.
 
	if (!payload.xhtml().empty()) {
 
	if (CONFIG_BOOL(m_config, "service.enable_xhtml") && !payload.xhtml().empty()) {
 
		msg->addPayload(boost::make_shared<Swift::XHTMLIMPayload>(payload.xhtml()));
 
	}
 

	
 
	// Create new Conversation if it does not exist
 
	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));
 
	}
 

	
 
	// Forward it
 
	conv->handleMessage(msg, payload.nickname());
 
	m_userManager->messageToXMPPSent();
 
}
 

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

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

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

	
 
	// Attentions trigger new Conversation creation
 
	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));
 
	}
 

	
 
	conv->handleMessage(msg);
 
}
 

	
 
void NetworkPluginServer::handleStatsPayload(Backend *c, const std::string &data) {
 
	pbnetwork::Stats payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	c->res = payload.res();
 
	c->init_res = payload.init_res();
 
	c->shared = payload.shared();
 
	c->id = payload.id();
 
}
 

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

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

	
 
	LOG4CXX_INFO(logger, "handleFTStartPayload " << payload.filename() << " " << payload.buddyname());
 
	
 
	LocalBuddy *buddy = (LocalBuddy *) user->getRosterManager()->getBuddy(payload.buddyname());
 
	if (!buddy) {
 
		// TODO: escape? reject?
 
		return;
 
	}
 

	
 
	Swift::StreamInitiationFileInfo fileInfo;
 
	fileInfo.setSize(payload.size());
 
	fileInfo.setName(payload.filename());
 

	
 
	Backend *c = (Backend *) user->getData();
 
	boost::shared_ptr<MemoryReadBytestream> bytestream(new MemoryReadBytestream(payload.size()));
 
	bytestream->onDataNeeded.connect(boost::bind(&NetworkPluginServer::handleFTDataNeeded, this, c, bytestream_id + 1));
 

	
 
	LOG4CXX_INFO(logger, "jid=" << buddy->getJID());
 

	
 
	FileTransferManager::Transfer transfer = m_ftManager->sendFile(user, buddy, bytestream, fileInfo);
 
	if (!transfer.ft) {
 
		handleFTRejected(user, payload.buddyname(), payload.filename(), payload.size());
 
		return;
 
	}
 

	
 
	m_filetransfers[++bytestream_id] = transfer;
 
	transfer.ft->onStateChange.connect(boost::bind(&NetworkPluginServer::handleFTStateChanged, this, _1, payload.username(), payload.buddyname(), payload.filename(), payload.size(), bytestream_id));
 
	transfer.ft->start();
 
}
 

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

	
 
	if (payload.has_ftid()) {
 
		if (m_filetransfers.find(payload.ftid()) != m_filetransfers.end()) {
 
			FileTransferManager::Transfer &transfer = m_filetransfers[payload.ftid()];
 
			transfer.ft->cancel();
 
		}
 
		else {
 
			LOG4CXX_ERROR(logger, "FTFinishPayload for unknown ftid=" << payload.ftid());
 
		}
 
	}
 

	
 
}
 

	
 
void NetworkPluginServer::handleFTDataPayload(Backend *b, const std::string &data) {
 
	pbnetwork::FileTransferData payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

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

	
 
	FileTransferManager::Transfer &transfer = m_filetransfers[payload.ftid()];
 
	MemoryReadBytestream *bytestream = (MemoryReadBytestream *) transfer.readByteStream.get();
 

	
 
	if (bytestream->appendData(payload.data()) > 5000000) {
 
		pbnetwork::FileTransferData f;
 
		f.set_ftid(payload.ftid());
 
		f.set_data("");
 

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

	
 
		WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_FT_PAUSE);
 

	
 
		send(b->connection, message);
 
	}
 
}
 

	
 
void NetworkPluginServer::handleFTDataNeeded(Backend *b, unsigned long ftid) {
 
	pbnetwork::FileTransferData f;
 
	f.set_ftid(ftid);
 
	f.set_data("");
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_FT_CONTINUE);
 

	
 
	send(b->connection, message);
 
}
 

	
 
void NetworkPluginServer::handlePongReceived(Backend *c) {
 
	// This could be first PONG from the backend
 
	if (c->pongReceived == -1) {
 
		// Backend is fully ready to handle requests
 
		c->willDie = false;
 

	
 
		if (m_clients.size() == 1) {
 
			// first backend connected, start the server, we're ready.
 
			m_component->start();
 
		}
 

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

	
 
	c->pongReceived = true;
 
}
 

	
 
void NetworkPluginServer::handleQueryPayload(Backend *b, const std::string &data) {
 
	pbnetwork::BackendConfig payload;
 
	if (payload.ParseFromString(data) == false) {
 
		// TODO: ERROR
 
		return;
 
	}
 

	
 
	if (!m_adminInterface) {
 
		return;
 
	}
 

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

	
 
	pbnetwork::BackendConfig vcard;
 
	vcard.set_config(msg->getBody());
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_QUERY);
 

	
 
	send(b->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleDataRead(Backend *c, boost::shared_ptr<Swift::SafeByteArray> data) {
 
	// Append data to buffer
 
	c->data.insert(c->data.end(), data->begin(), data->end());
 

	
 
	// Parse data while there are some
 
	while (c->data.size() != 0) {
 
		// expected_size of wrapper message
 
		unsigned int expected_size;
 

	
 
		// if data is >= 4, we have whole header and we can
 
		// read expected_size.
 
		if (c->data.size() >= 4) {
 
			expected_size = *((unsigned int*) &c->data[0]);
 
			expected_size = ntohl(expected_size);
 
			// If we don't have whole wrapper message, wait for next
 
			// handleDataRead call.
 
			if (c->data.size() - 4 < expected_size)
 
				return;
 
		}
 
		else {
 
			return;
 
		}
 

	
 
		// Parse wrapper message and erase it from buffer.
 
		pbnetwork::WrapperMessage wrapper;
 
		if (wrapper.ParseFromArray(&c->data[4], expected_size) == false) {
 
			std::cout << "PARSING ERROR " << expected_size << "\n";
 
			c->data.erase(c->data.begin(), c->data.begin() + 4 + expected_size);
 
			continue;
 
		}
 
		c->data.erase(c->data.begin(), c->data.begin() + 4 + expected_size);
 

	
 
		// Handle payload in wrapper message
 
		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_ROOM_SUBJECT_CHANGED:
 
				handleConvMessagePayload(wrapper.payload(), true);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_PONG:
 
				c->pongReceived = true;
 
				handlePongReceived(c);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_PARTICIPANT_CHANGED:
 
				handleParticipantChangedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_ROOM_NICKNAME_CHANGED:
 
				handleRoomChangedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_VCARD:
 
				handleVCardPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPING:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Composing);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPED:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Paused);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_STOPPED_TYPING:
 
				handleChatStatePayload(wrapper.payload(), Swift::ChatState::Active);
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_AUTH_REQUEST:
 
				handleAuthorizationPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_ATTENTION:
 
				handleAttentionPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_STATS:
 
				handleStatsPayload(c, 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_DATA:
 
				handleFTDataPayload(c, wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_BUDDY_REMOVED:
 
				handleBuddyRemovedPayload(wrapper.payload());
 
				break;
 
			case pbnetwork::WrapperMessage_Type_TYPE_QUERY:
 
				handleQueryPayload(c, wrapper.payload());
 
				break;
 
			default:
 
				return;
 
		}
 
	}
 
}
 

	
 
void NetworkPluginServer::send(boost::shared_ptr<Swift::Connection> &c, const std::string &data) {
 
	// generate header - size of wrapper message
 
	uint32_t size = htonl(data.size());
 
	char *header = (char *) &size;
 

	
 
	// send header together with wrapper message
 
	c->write(Swift::createSafeByteArray(std::string(header, 4) + data));
 
}
 

	
 
void NetworkPluginServer::pingTimeout() {
 
	// TODO: move to separate timer, those 2 loops could be expensive
 
	// Some users are connected for weeks and they are blocking backend to be destroyed and its memory
 
	// to be freed. We are finding users who are inactive for more than "idle_reconnect_time" seconds and
 
	// reconnect them to long-running backend, where they can idle hapilly till the end of ages.
 
	time_t now = time(NULL);
 
	std::vector<User *> usersToMove;
 
	unsigned long diff = CONFIG_INT(m_config, "service.idle_reconnect_time");
 
	if (diff != 0) {
 
		for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
			// Users from long-running backends can't be moved
 
			if ((*it)->longRun) {
 
				continue;
 
			}
 

	
 
			// Find users which are inactive for more than 'diff'
 
			BOOST_FOREACH(User *u, (*it)->users) {
 
				if (now - u->getLastActivity() > diff) {
 
					usersToMove.push_back(u);
 
				}
 
			}
 
		}
 

	
 
		// Move inactive users to long-running backend.
 
		BOOST_FOREACH(User *u, usersToMove) {
 
			LOG4CXX_INFO(logger, "Moving user " << u->getJID().toString() << " to long-running backend");
 
			if (!moveToLongRunBackend(u))
 
				break;
 
		}
 
	}
 

	
 
	// We have to remove startingBackend flag otherwise 1 broken backend start could
 
	// block the backend.
 
	m_startingBackend = false;
 

	
 
	// check ping responses
 
	std::vector<Backend *> toRemove;
 
	for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
		// pong has been received OR backend just connected and did not have time to answer the ping
 
		// request.
 
		if ((*it)->pongReceived || (*it)->pongReceived == -1) {
 
			sendPing((*it));
 
			// Don't send another ping if pongReceived == -1, because we've already sent one
 
			// when registering backend.
 
			if ((*it)->pongReceived) {
 
				sendPing((*it));
 
			}
 
		}
 
		else {
 
			LOG4CXX_INFO(logger, "Disconnecting backend " << (*it) << " (ID=" << (*it)->id << "). PING response not received.");
 
			toRemove.push_back(*it);
 

	
 
#ifndef WIN32
 
			// generate coredump for this backend to find out why it wasn't able to respond to PING
 
			std::string pid = (*it)->id;
 
			if (!pid.empty()) {
 
				try {
 
					kill(boost::lexical_cast<int>(pid), SIGABRT);
 
				}
 
				catch (...) { }
 
			}
 
#endif
 
		}
 

	
 
		if ((*it)->users.size() == 0) {
 
			LOG4CXX_INFO(logger, "Disconnecting backend " << (*it) << " (ID=" << (*it)->id << "). There are no users.");
 
			toRemove.push_back(*it);
 
		}
 
	}
 

	
 
	BOOST_FOREACH(Backend *b, toRemove) {
 
		handleSessionFinished(b);
 
	}
 

	
 
	m_pingTimer->start();
 
}
 

	
 
void NetworkPluginServer::collectBackend() {
 
	// Stop accepting new users to backend with the biggest memory usage. This prevents backends
 
	// which are leaking to eat whole memory by connectin new users to legacy network.
 
	LOG4CXX_INFO(logger, "Collect backend called, finding backend which will be set to die");
 
	unsigned long max = 0;
 
	Backend *backend = NULL;
 
	for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
		if ((*it)->res > max) {
 
			max = (*it)->res;
 
			backend = (*it);
 
		}
 
	}
 

	
 
	if (backend) {
 
		if (m_collectTimer) {
 
			m_collectTimer->start();
 
		}
 
		LOG4CXX_INFO(logger, "Backend " << backend << " (ID=" << backend->id << ") is set to die");
 
		backend->acceptUsers = false;
 
	}
 
}
 

	
 
bool NetworkPluginServer::moveToLongRunBackend(User *user) {
 
	// Check if user has already some backend
 
	Backend *old = (Backend *) user->getData();
 
	if (!old) {
 
		LOG4CXX_INFO(logger, "User " << user->getJID().toString() << " does not have old backend. Not moving.");
 
		return true;
 
	}
 

	
 
	// if he's already on long run, do nothing
 
	if (old->longRun) {
 
		LOG4CXX_INFO(logger, "User " << user->getJID().toString() << " is already on long-running backend. Not moving.");
 
		return true;
 
	}
 

	
 
	// Get free longrun backend, if there's no longrun backend, create one and wait
 
	// for its connection
 
	Backend *backend = getFreeClient(false, true);
 
	if (!backend) {
 
		LOG4CXX_INFO(logger, "No free long-running backend for user " << user->getJID().toString() << ". Will try later");
 
		return false;
 
	}
 

	
 
	// old backend will trigger disconnection which has to be ignored to keep user online
 
	user->setIgnoreDisconnect(true);
 

	
 
	// remove user from the old backend
 
	// If backend is empty, it will be collected by pingTimeout
 
	old->users.remove(user);
 

	
 
	// switch to new backend and connect
 
	user->setData(backend);
 
	backend->users.push_back(user);
 

	
 
	// connect him
 
	handleUserReadyToConnect(user);
 
	return true;
 
}
 

	
 
void NetworkPluginServer::handleUserCreated(User *user) {
 
	// Get free backend to handle this user or spawn new one if there's no free one.
 
	Backend *c = getFreeClient();
 

	
 
	// Add user to queue if there's no free backend to handle him so far.
 
	if (!c) {
 
		LOG4CXX_INFO(logger, "There is no backend to handle user " << user->getJID().toString() << ". Adding him to queue.");
 
		m_waitingUsers.push_back(user);
 
		return;
 
	}
 

	
 
	// Associate users with backend
 
	user->setData(c);
 
	c->users.push_back(user);
 

	
 
	// Don't forget to disconnect these in handleUserDestroyed!!!
 
	user->onReadyToConnect.connect(boost::bind(&NetworkPluginServer::handleUserReadyToConnect, this, user));
 
	user->onPresenceChanged.connect(boost::bind(&NetworkPluginServer::handleUserPresenceChanged, this, user, _1));
 
	user->onRoomJoined.connect(boost::bind(&NetworkPluginServer::handleRoomJoined, this, user, _1, _2, _3, _4));
 
	user->onRoomLeft.connect(boost::bind(&NetworkPluginServer::handleRoomLeft, this, user, _1));
 
}
 

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

	
 
	pbnetwork::Login login;
 
	login.set_user(user->getJID().toBare());
 
	login.set_legacyname(userInfo.uin);
 
	login.set_password(userInfo.password);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_LOGIN);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleUserPresenceChanged(User *user, Swift::Presence::ref presence) {
 
	if (presence->getShow() == Swift::StatusShow::None)
 
		return;
 

	
 
	UserInfo userInfo = user->getUserInfo();
 

	
 
	pbnetwork::Status status;
 
	status.set_username(user->getJID().toBare());
 

	
 
	bool isInvisible = presence->getPayload<Swift::InvisiblePayload>() != NULL;
 
	if (isInvisible) {
 
		LOG4CXX_INFO(logger, "This presence is invisible");
 
		status.set_status((pbnetwork::STATUS_INVISIBLE));
 
	}
 
	else {
 
		status.set_status((pbnetwork::StatusType) presence->getShow());
 
	}
 

	
 
	status.set_statusmessage(presence->getStatus());
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_STATUS_CHANGED);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleRoomJoined(User *user, const Swift::JID &who, const std::string &r, const std::string &nickname, const std::string &password) {
 
	UserInfo userInfo = user->getUserInfo();
 

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

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_JOIN_ROOM);
 
 
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 

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

	
 
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);
 
 
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 

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

	
 
	user->getConversationManager()->removeConversation(conv);
 

	
 
	delete conv;
 
}
 

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

	
 
	user->onReadyToConnect.disconnect(boost::bind(&NetworkPluginServer::handleUserReadyToConnect, this, user));
 
	user->onPresenceChanged.disconnect(boost::bind(&NetworkPluginServer::handleUserPresenceChanged, this, user, _1));
 
	user->onRoomJoined.disconnect(boost::bind(&NetworkPluginServer::handleRoomJoined, this, user, _1, _2, _3, _4));
 
	user->onRoomLeft.disconnect(boost::bind(&NetworkPluginServer::handleRoomLeft, this, user, _1));
 

	
 
	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);
 
 
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
	c->users.remove(user);
 

	
 
	// If backend should handle only one user, it must not accept another one before 
 
	// we kill it, so set up willDie to true
 
	if (c->users.size() == 0 && CONFIG_INT(m_config, "service.users_per_backend") == 1) {
 
		LOG4CXX_INFO(logger, "Backend " << c->id << " will die, because the last user disconnected");
 
		c->willDie = true;
 
	}
 
}
 

	
 
void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost::shared_ptr<Swift::Message> &msg) {
 
	conv->getConversationManager()->getUser()->updateLastActivity();
 
	boost::shared_ptr<Swift::ChatState> statePayload = msg->getPayload<Swift::ChatState>();
 
	if (statePayload) {
 
		pbnetwork::WrapperMessage_Type type = pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED;
 
		switch (statePayload->getChatState()) {
 
			case Swift::ChatState::Active:
 
				type = pbnetwork::WrapperMessage_Type_TYPE_BUDDY_STOPPED_TYPING;
 
				break;
 
			case Swift::ChatState::Composing:
 
				type = pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPING;
 
				break;
 
			case Swift::ChatState::Paused:
 
				type = pbnetwork::WrapperMessage_Type_TYPE_BUDDY_TYPED;
 
				break;
 
			default:
 
				break;
 
		}
 
		if (type != pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED) {
 
			pbnetwork::Buddy buddy;
 
			buddy.set_username(conv->getConversationManager()->getUser()->getJID().toBare());
 
			buddy.set_buddyname(conv->getLegacyName());
 

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

	
 
			WRAP(message, type);
 

	
 
			Backend *c = (Backend *) conv->getConversationManager()->getUser()->getData();
 
			if (!c) {
 
				return;
 
			}
 
			send(c->connection, message);
 
		}
 
	}
 

	
 
	boost::shared_ptr<Swift::AttentionPayload> attentionPayload = msg->getPayload<Swift::AttentionPayload>();
 
	if (attentionPayload) {
 
		pbnetwork::ConversationMessage m;
 
		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_ATTENTION);
 

	
 
		Backend *c = (Backend *) conv->getConversationManager()->getUser()->getData();
 
		send(c->connection, message);
 
		return;
 
	}
 
	
 

	
 
	std::string xhtml;
 
	boost::shared_ptr<Swift::XHTMLIMPayload> xhtmlPayload = msg->getPayload<Swift::XHTMLIMPayload>();
 
	if (xhtmlPayload) {
 
		xhtml = xhtmlPayload->getBody();
 
	}
 

	
 
	// Send normal message
 
	if (!msg->getBody().empty() || !xhtml.empty()) {
 
		pbnetwork::ConversationMessage m;
 
		m.set_username(conv->getConversationManager()->getUser()->getJID().toBare());
 
		m.set_buddyname(conv->getLegacyName());
 
		m.set_message(msg->getBody());
 
		m.set_xhtml(xhtml);
 

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

	
 
		WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_CONV_MESSAGE);
 

	
 
		Backend *c = (Backend *) conv->getConversationManager()->getUser()->getData();
 
		if (!c) {
 
			return;
 
		}
 
		send(c->connection, message);
 
	}
 
}
 

	
 
void NetworkPluginServer::handleBuddyRemoved(Buddy *b) {
 
	User *user = b->getRosterManager()->getUser();
 

	
 
	pbnetwork::Buddy buddy;
 
	buddy.set_username(user->getJID().toBare());
 
	buddy.set_buddyname(b->getName());
 
	buddy.set_alias(b->getAlias());
 
	BOOST_FOREACH(const std::string &g, b->getGroups()) {
 
		buddy.add_group(g);
 
	}
 
	buddy.set_status(pbnetwork::STATUS_NONE);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BUDDY_REMOVED);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleBuddyUpdated(Buddy *b, const Swift::RosterItemPayload &item) {
 
	User *user = b->getRosterManager()->getUser();
 

	
 
	dynamic_cast<LocalBuddy *>(b)->setAlias(item.getName());
 
	dynamic_cast<LocalBuddy *>(b)->setGroups(item.getGroups());
 
	user->getRosterManager()->storeBuddy(b);
 

	
 
	pbnetwork::Buddy buddy;
 
	buddy.set_username(user->getJID().toBare());
 
	buddy.set_buddyname(b->getName());
 
	buddy.set_alias(b->getAlias());
 
	BOOST_FOREACH(const std::string &g, b->getGroups()) {
 
		buddy.add_group(g);
 
	}
 
	buddy.set_status(pbnetwork::STATUS_NONE);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

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

	
 
void NetworkPluginServer::handleBlockToggled(Buddy *b) {
 
	User *user = b->getRosterManager()->getUser();
 

	
 
	pbnetwork::Buddy buddy;
 
	buddy.set_username(user->getJID().toBare());
 
	buddy.set_buddyname(b->getName());
 
	buddy.set_alias(b->getAlias());
 
	BOOST_FOREACH(const std::string &g, b->getGroups()) {
 
		buddy.add_group(g);
 
	}
 
	buddy.set_status(pbnetwork::STATUS_NONE);
 
	buddy.set_blocked(!b->isBlocked());
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

	
 

	
 
void NetworkPluginServer::handleVCardUpdated(User *user, boost::shared_ptr<Swift::VCard> v) {
 
	pbnetwork::VCard vcard;
 
	vcard.set_username(user->getJID().toBare());
 
	vcard.set_buddyname("");
 
	vcard.set_id(0);
 
	vcard.set_photo(&v->getPhoto()[0], v->getPhoto().size());
 
	vcard.set_nickname(v->getNickname());
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_VCARD);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleVCardRequired(User *user, const std::string &name, unsigned int id) {
 
	pbnetwork::VCard vcard;
 
	vcard.set_username(user->getJID().toBare());
 
	vcard.set_buddyname(name);
 
	vcard.set_id(id);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_VCARD);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleFTAccepted(User *user, const std::string &buddyName, const std::string &fileName, unsigned long size, unsigned long ftID) {
 
	pbnetwork::File f;
 
	f.set_username(user->getJID().toBare());
 
	f.set_buddyname(buddyName);
 
	f.set_filename(fileName);
 
	f.set_size(size);
 
	f.set_ftid(ftID);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_FT_START);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleFTRejected(User *user, const std::string &buddyName, const std::string &fileName, unsigned long size) {
 
	pbnetwork::File f;
 
	f.set_username(user->getJID().toBare());
 
	f.set_buddyname(buddyName);
 
	f.set_filename(fileName);
 
	f.set_size(size);
 
	f.set_ftid(0);
 

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

	
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_FT_FINISH);
 

	
 
	Backend *c = (Backend *) user->getData();
 
	if (!c) {
 
		return;
 
	}
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleFTStateChanged(Swift::FileTransfer::State state, const std::string &userName, const std::string &buddyName, const std::string &fileName, unsigned long size, unsigned long id) {
 
	User *user = m_userManager->getUser(userName);
 
	if (!user) {
 
		// TODO: FIXME We have to remove filetransfer when use disconnects
 
		return;
 
	}
 
	if (state.state == Swift::FileTransfer::State::Transferring) {
 
		handleFTAccepted(user, buddyName, fileName, size, id);
 
	}
 
	else if (state.state == Swift::FileTransfer::State::Canceled) {
 
		handleFTRejected(user, buddyName, fileName, size);
 
	}
 
}
 

	
 
void NetworkPluginServer::sendPing(Backend *c) {
 

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

	
 
	if (c->connection) {
 
		LOG4CXX_INFO(logger, "PING to " << c << " (ID=" << c->id << ")");
 
		send(c->connection, message);
 
		c->pongReceived = false;
 
	}
 
// 	LOG4CXX_INFO(logger, "PING to " << c);
 
}
 

	
 
NetworkPluginServer::Backend *NetworkPluginServer::getFreeClient(bool acceptUsers, bool longRun) {
 
	NetworkPluginServer::Backend *c = NULL;
 

	
 
	// Check all backends and find free one
 
	for (std::list<Backend *>::const_iterator it = m_clients.begin(); it != m_clients.end(); it++) {
 
		if ((*it)->willDie == false && (*it)->acceptUsers == acceptUsers && (*it)->users.size() < CONFIG_INT(m_config, "service.users_per_backend") && (*it)->connection && (*it)->longRun == longRun) {
 
			c = *it;
 
			// if we're not reusing all backends and backend is full, stop accepting new users on this backend
 
			if (!CONFIG_BOOL(m_config, "service.reuse_old_backends")) {
 
				if (c->users.size() + 1 >= CONFIG_INT(m_config, "service.users_per_backend")) {
 
					c->acceptUsers = false;
 
				}
 
			}
 
			break;
 
		}
 
	}
 

	
 
	// there's no free backend, so spawn one.
 
	if (c == NULL) {
 
	if (c == NULL && !m_startingBackend) {
 
		m_isNextLongRun = longRun;
 
		m_startingBackend = true;
 
		exec_(CONFIG_STRING(m_config, "service.backend"), CONFIG_STRING(m_config, "service.backend_host").c_str(), CONFIG_STRING(m_config, "service.backend_port").c_str(), m_config->getConfigFile().c_str());
 
	}
 

	
 
	return c;
 
}
 

	
 
}
src/rostermanager.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 "transport/rostermanager.h"
 
#include "transport/rosterstorage.h"
 
#include "transport/storagebackend.h"
 
#include "transport/buddy.h"
 
#include "transport/usermanager.h"
 
#include "transport/buddy.h"
 
#include "transport/user.h"
 
#include "transport/logging.h"
 
#include "Swiften/Roster/SetRosterRequest.h"
 
#include "Swiften/Elements/RosterPayload.h"
 
#include "Swiften/Elements/RosterItemPayload.h"
 
#include "Swiften/Elements/RosterItemExchangePayload.h"
 
#include <boost/foreach.hpp>
 

	
 
#include <map>
 
#include <iterator>
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "RosterManager");
 

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

	
 
	m_supportRemoteRoster = false;
 

	
 
	if (!m_component->inServerMode()) {
 
		m_remoteRosterRequest = AddressedRosterRequest::ref(new AddressedRosterRequest(m_component->getIQRouter(), m_user->getJID().toBare()));
 
		m_remoteRosterRequest->onResponse.connect(boost::bind(&RosterManager::handleRemoteRosterResponse, this, _1, _2));
 
		m_remoteRosterRequest->send();
 
	}
 
}
 

	
 
RosterManager::~RosterManager() {
 
	m_setBuddyTimer->stop();
 
	m_RIETimer->stop();
 
	if (m_rosterStorage) {
 
		m_rosterStorage->storeBuddies();
 
	}
 

	
 
	sendUnavailablePresences(m_user->getJID().toBare());
 

	
 
	if (m_remoteRosterRequest) {
 
		m_remoteRosterRequest->onResponse.disconnect_all_slots();
 
		m_component->getIQRouter()->removeHandler(m_remoteRosterRequest);
 
	}
 

	
 
	for (std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > >::iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
 
		Buddy *buddy = (*it).second;
 
		if (!buddy) {
 
			continue;
 
		}
 
		delete buddy;
 
	}
 

	
 
	if (m_requests.size() != 0) {
 
		LOG4CXX_INFO(logger, m_user->getJID().toString() <<  ": Removing " << m_requests.size() << " unresponded IQs");
 
		BOOST_FOREACH(Swift::SetRosterRequest::ref request, m_requests) {
 
			request->onResponse.disconnect_all_slots();
 
			m_component->getIQRouter()->removeHandler(request);
 
		}
 
		m_requests.clear();
 
	}
 

	
 
	boost::singleton_pool<boost::pool_allocator_tag, sizeof(unsigned int)>::release_memory();
 

	
 
	if (m_rosterStorage)
 
		delete m_rosterStorage;
 
}
 

	
 
void RosterManager::setBuddy(Buddy *buddy) {
 
// 	m_setBuddyTimer->onTick.connect(boost::bind(&RosterManager::setBuddyCallback, this, buddy));
 
// 	m_setBuddyTimer->start();
 
	setBuddyCallback(buddy);
 
}
 

	
 
void RosterManager::removeBuddy(const std::string &name) {
 
	Buddy *buddy = getBuddy(name);
 
	if (!buddy) {
 
		LOG4CXX_WARN(logger, m_user->getJID().toString() << ": Tried to remove unknown buddy " << name);
 
		return;
 
	}
 

	
 
	if (m_component->inServerMode() || m_remoteRosterRequest) {
 
		sendBuddyRosterRemove(buddy);
 
	}
 
	else {
 
		sendBuddyUnsubscribePresence(buddy);
 
	}
 

	
 
	if (m_rosterStorage)
 
		m_rosterStorage->removeBuddy(buddy);
 

	
 
	unsetBuddy(buddy);
 
	delete buddy;
 
}
 

	
 
void RosterManager::sendBuddyRosterRemove(Buddy *buddy) {
 
	Swift::RosterPayload::ref p = Swift::RosterPayload::ref(new Swift::RosterPayload());
 
	Swift::RosterItemPayload item;
 
	item.setJID(buddy->getJID().toBare());
 
	item.setSubscription(Swift::RosterItemPayload::Remove);
 

	
 
	p->addItem(item);
 

	
 
	// In server mode we have to send pushes to all resources, but in gateway-mode we send it only to bare JID
 
	if (m_component->inServerMode()) {
 
		std::vector<Swift::Presence::ref> presences = m_component->getPresenceOracle()->getAllPresence(m_user->getJID().toBare());
 
		BOOST_FOREACH(Swift::Presence::ref presence, presences) {
 
			Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(p, presence->getFrom(), m_component->getIQRouter());
 
			request->send();
 
		}
 
	}
 
	else {
 
		Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(p, m_user->getJID().toBare(), m_component->getIQRouter());
 
		request->send();
 
	}
 
}
 

	
 
void RosterManager::sendBuddyRosterPush(Buddy *buddy) {
 
	// user can't receive anything in server mode if he's not logged in.
 
	// He will ask for roster later (handled in rosterreponsder.cpp)
 
	if (m_component->inServerMode() && !m_user->isConnected())
 
		return;
 

	
 
	Swift::RosterPayload::ref payload = Swift::RosterPayload::ref(new Swift::RosterPayload());
 
	Swift::RosterItemPayload item;
 
	item.setJID(buddy->getJID().toBare());
 
	if (buddy->getAlias().empty()) {
 
		item.setName(buddy->getJID().toBare().toString());
 
	}
 
	else {
 
		item.setName(buddy->getAlias());
 
	}
 
	item.setGroups(buddy->getGroups());
 
	item.setSubscription(Swift::RosterItemPayload::Both);
 

	
 
	payload->addItem(item);
 

	
 
	// In server mode we have to send pushes to all resources, but in gateway-mode we send it only to bare JID
 
	if (m_component->inServerMode()) {
 
		std::vector<Swift::Presence::ref> presences = m_component->getPresenceOracle()->getAllPresence(m_user->getJID().toBare());
 
		BOOST_FOREACH(Swift::Presence::ref presence, presences) {
 
			Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(payload, presence->getFrom(), m_component->getIQRouter());
 
			request->onResponse.connect(boost::bind(&RosterManager::handleBuddyRosterPushResponse, this, _1, request, buddy->getName()));
 
			request->send();
 
			m_requests.push_back(request);
 
		}
 
	}
 
	else {
 
		Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(payload, m_user->getJID().toBare(), m_component->getIQRouter());
 
		request->onResponse.connect(boost::bind(&RosterManager::handleBuddyRosterPushResponse, this, _1, request, buddy->getName()));
 
		request->send();
 
		m_requests.push_back(request);
 
	}
 

	
 
	if (buddy->getSubscription() != Buddy::Both) {
 
		buddy->setSubscription(Buddy::Both);
 
		handleBuddyChanged(buddy);
 
	}
 
}
 

	
 
void RosterManager::sendBuddyUnsubscribePresence(Buddy *buddy) {
 
	Swift::Presence::ref response = Swift::Presence::create();
 
	response->setTo(m_user->getJID());
 
	response->setFrom(buddy->getJID());
 
	response->setType(Swift::Presence::Unsubscribe);
 
	m_component->getStanzaChannel()->sendPresence(response);
 

	
 
	response = Swift::Presence::create();
 
	response->setTo(m_user->getJID());
 
	response->setFrom(buddy->getJID());
 
	response->setType(Swift::Presence::Unsubscribed);
 
	m_component->getStanzaChannel()->sendPresence(response);
 
}
 

	
 
void RosterManager::sendBuddySubscribePresence(Buddy *buddy) {
 
	Swift::Presence::ref response = Swift::Presence::create();
 
	response->setTo(m_user->getJID());
 
	response->setFrom(buddy->getJID());
 
	response->setType(Swift::Presence::Subscribe);
 
// 	TODO: NICKNAME
 
	m_component->getStanzaChannel()->sendPresence(response);
 
}
 

	
 
void RosterManager::handleBuddyChanged(Buddy *buddy) {
 
}
 

	
 
void RosterManager::setBuddyCallback(Buddy *buddy) {
 
	LOG4CXX_INFO(logger, "Associating buddy " << buddy->getName() << " with " << m_user->getJID().toString());
 
	m_buddies[buddy->getName()] = buddy;
 
	onBuddySet(buddy);
 

	
 
	// In server mode the only way is to send jabber:iq:roster push.
 
	// In component mode we send RIE or Subscribe presences, based on features.
 
	if (m_component->inServerMode()) {
 
		sendBuddyRosterPush(buddy);
 
	}
 
	else {
 
		if (buddy->getSubscription() == Buddy::Both) {
 
			LOG4CXX_INFO(logger, m_user->getJID().toString() << ": Not forwarding this buddy, because subscription=both");
 
			return;
 
		}
 

	
 
		if (m_supportRemoteRoster) {
 
			sendBuddyRosterPush(buddy);
 
		}
 
		else {
 
			// Send RIE only if there's resource which supports it.
 
			Swift::JID jidWithRIE = m_user->getJIDWithFeature("http://jabber.org/protocol/rosterx");
 
			if (jidWithRIE.isValid()) {
 
				m_RIETimer->start();
 
			}
 
			else {
 
				sendBuddySubscribePresence(buddy);
 
			}
 
		}
 
	}
 

	
 
	if (m_rosterStorage)
 
		m_rosterStorage->storeBuddy(buddy);
 
}
 

	
 
void RosterManager::unsetBuddy(Buddy *buddy) {
 
	m_buddies.erase(buddy->getName());
 
	if (m_rosterStorage)
 
		m_rosterStorage->removeBuddyFromQueue(buddy);
 
	onBuddyUnset(buddy);
 
}
 

	
 
void RosterManager::storeBuddy(Buddy *buddy) {
 
	if (m_rosterStorage) {
 
		m_rosterStorage->storeBuddy(buddy);
 
	}
 
}
 

	
 
void RosterManager::handleBuddyRosterPushResponse(Swift::ErrorPayload::ref error, Swift::SetRosterRequest::ref request, const std::string &key) {
 
	LOG4CXX_INFO(logger, "handleBuddyRosterPushResponse called for buddy " << key);
 
	if (m_buddies[key] != NULL) {
 
		m_buddies[key]->handleBuddyChanged();
 
	}
 
	else {
 
		LOG4CXX_WARN(logger, "handleBuddyRosterPushResponse called for unknown buddy " << key);
 
	}
 

	
 
	m_requests.remove(request);
 
	request->onResponse.disconnect_all_slots();
 
}
 

	
 
void RosterManager::handleRemoteRosterResponse(boost::shared_ptr<Swift::RosterPayload> payload, Swift::ErrorPayload::ref error) {
 
	m_remoteRosterRequest.reset();
 
	if (error) {
 
		m_supportRemoteRoster = false;
 
		LOG4CXX_INFO(logger, m_user->getJID().toString() << ": This server does not support remote roster protoXEP");
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, m_user->getJID().toString() << ": This server supports remote roster protoXEP");
 
	m_supportRemoteRoster = true;
 
	return;
 

	
 
	BOOST_FOREACH(const Swift::RosterItemPayload &item, payload->getItems()) {
 
		std::string legacyName = Buddy::JIDToLegacyName(item.getJID());
 
		if (m_buddies.find(legacyName) != m_buddies.end()) {
 
			continue;
 
		}
 

	
 
		if (legacyName.empty()) {
 
			continue;
 
		}
 

	
 
		BuddyInfo buddyInfo;
 
		buddyInfo.id = -1;
 
		buddyInfo.alias = item.getName();
 
		buddyInfo.legacyName = legacyName;
 
		buddyInfo.subscription = "both";
 
		buddyInfo.flags = Buddy::buddFlagsFromJID(item.getJID());
 
		buddyInfo.groups = item.getGroups();
 

	
 
		Buddy *buddy = m_component->getFactory()->createBuddy(this, buddyInfo);
 
		setBuddy(buddy);
 
	}
 
}
 

	
 
Buddy *RosterManager::getBuddy(const std::string &name) {
 
	return m_buddies[name];
 
}
 

	
 
void RosterManager::sendRIE() {
 
	m_RIETimer->stop();
 

	
 
	// Check the feature, because proper resource could logout during RIETimer.
 
	Swift::JID jidWithRIE = m_user->getJIDWithFeature("http://jabber.org/protocol/rosterx");
 

	
 
	// fallback to normal subscribe
 
	if (!jidWithRIE.isValid()) {
 
		for (std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > >::iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
 
			Buddy *buddy = (*it).second;
 
			if (!buddy) {
 
				continue;
 
			}
 
			sendBuddySubscribePresence(buddy);
 
		}
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Sending RIE stanza to " << jidWithRIE.toString());
 

	
 
	Swift::RosterItemExchangePayload::ref payload = Swift::RosterItemExchangePayload::ref(new Swift::RosterItemExchangePayload());
 
	for (std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > >::iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
 
		Buddy *buddy = (*it).second;
 
		if (!buddy) {
 
			continue;
 
		}
 
		Swift::RosterItemExchangePayload::Item item;
 
		item.setJID(buddy->getJID().toBare());
 
		item.setName(buddy->getAlias());
 
		item.setAction(Swift::RosterItemExchangePayload::Item::Add);
 
		item.setGroups(buddy->getGroups());
 

	
 
		payload->addItem(item);
 
	}
 

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

	
 
void RosterManager::handleSubscription(Swift::Presence::ref presence) {
 
	std::string legacyName = Buddy::JIDToLegacyName(presence->getTo());
 
	if (legacyName.empty()) {
 
		return;
 
	}
 
	
 
	// For server mode the subscription changes are handler in rosterresponder.cpp
 
	// using roster pushes.
 
	if (m_component->inServerMode()) {
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setTo(presence->getFrom().toBare());
 
		response->setFrom(presence->getTo().toBare());
 
		Buddy *buddy = getBuddy(Buddy::JIDToLegacyName(presence->getTo()));
 
		if (buddy) {
 
			LOG4CXX_INFO(logger, m_user->getJID().toString() << ": Subscription received and buddy " << Buddy::JIDToLegacyName(presence->getTo()) << " is already there => answering");
 
			switch (presence->getType()) {
 
				case Swift::Presence::Subscribe:
 
					onBuddyAdded(buddy);
 
					response->setType(Swift::Presence::Subscribed);
 
					break;
 
				case Swift::Presence::Unsubscribe:
 
					onBuddyRemoved(buddy);
 
					response->setType(Swift::Presence::Unsubscribed);
 
					break;
 
				case Swift::Presence::Subscribed:
 
					onBuddyAdded(buddy);
 
					break;
 
				default:
 
					return;
 
			}
 
			m_component->getStanzaChannel()->sendPresence(response);
 
			
 
		}
 
		else {
 
			BuddyInfo buddyInfo;
 
			switch (presence->getType()) {
 
				// buddy is not in roster, so add him
 
				case Swift::Presence::Subscribe:
 
					buddyInfo.id = -1;
 
					buddyInfo.alias = "";
 
					buddyInfo.legacyName = Buddy::JIDToLegacyName(presence->getTo());
 
					buddyInfo.subscription = "both";
 
					buddyInfo.flags = Buddy::buddFlagsFromJID(presence->getTo());
 
					LOG4CXX_INFO(logger, m_user->getJID().toString() << ": Subscription received for new buddy " << buddyInfo.legacyName << " => adding to legacy network");
 

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

	
 
		Buddy *buddy = getBuddy(Buddy::JIDToLegacyName(presence->getTo()));
 
		if (buddy) {
 
			switch (presence->getType()) {
 
				// buddy is already there, so nothing to do, just answer
 
				case Swift::Presence::Subscribe:
 
					onBuddyAdded(buddy);
 
					response->setType(Swift::Presence::Subscribed);
 
					currentPresence = buddy->generatePresenceStanza(255);
 
					if (currentPresence) {
 
						currentPresence->setTo(presence->getFrom());
 
						m_component->getStanzaChannel()->sendPresence(currentPresence);
 
					}
 
					if (buddy->getSubscription() != Buddy::Both) {
 
						buddy->setSubscription(Buddy::Both);
 
						handleBuddyChanged(buddy);
 
					}
 
					break;
 
				// remove buddy
 
				case Swift::Presence::Unsubscribe:
 
					response->setType(Swift::Presence::Unsubscribed);
 
					onBuddyRemoved(buddy);
 
					break;
 
				// just send response
 
				case Swift::Presence::Unsubscribed:
 
					response->setType(Swift::Presence::Unsubscribe);
 
					// We set both here, because this Unsubscribed can be response to
 
					// subscribe presence and we don't want that unsubscribe presence
 
					// to be send later again
 
					if (buddy->getSubscription() != Buddy::Both) {
 
						buddy->setSubscription(Buddy::Both);
 
						handleBuddyChanged(buddy);
 
					}
 
					break;
 
				case Swift::Presence::Subscribed:
 
					if (buddy->getSubscription() != Buddy::Both) {
 
						buddy->setSubscription(Buddy::Both);
 
						handleBuddyChanged(buddy);
 
					}
 
					return;
 
				default:
 
					return;
 
			}
 
		}
 
		else {
 
			BuddyInfo buddyInfo;
 
			switch (presence->getType()) {
 
				// buddy is not in roster, so add him
 
				case Swift::Presence::Subscribe:
 
					buddyInfo.id = -1;
 
					buddyInfo.alias = "";
 
					buddyInfo.legacyName = Buddy::JIDToLegacyName(presence->getTo());
 
					buddyInfo.subscription = "both";
 
					buddyInfo.flags = Buddy::buddFlagsFromJID(presence->getTo());
 

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

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

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

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

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

	
 
	for (std::list<BuddyInfo>::const_iterator it = roster.begin(); it != roster.end(); it++) {
 
		Buddy *buddy = m_component->getFactory()->createBuddy(this, *it);
 
		LOG4CXX_INFO(logger, m_user->getJID().toString() << ": Adding cached buddy " << buddy->getName() << " fom database");
 
		m_buddies[buddy->getName()] = buddy;
 
		onBuddySet(buddy);
 
	}
 

	
 
	m_rosterStorage = storage;
 
}
 

	
 
Swift::RosterPayload::ref RosterManager::generateRosterPayload() {
 
	Swift::RosterPayload::ref payload = Swift::RosterPayload::ref(new Swift::RosterPayload());
 

	
 
	for (std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > >::iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
 
		Buddy *buddy = (*it).second;
 
		if (!buddy) {
 
			continue;
 
		}
 
		Swift::RosterItemPayload item;
 
		item.setJID(buddy->getJID().toBare());
 
		item.setName(buddy->getAlias());
 
		item.setGroups(buddy->getGroups());
 
		item.setSubscription(Swift::RosterItemPayload::Both);
 
		payload->addItem(item);
 
	}
 
	return payload;
 
}
 

	
 
void RosterManager::sendCurrentPresences(const Swift::JID &to) {
 
	for (std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > >::iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
 
		Buddy *buddy = (*it).second;
 
		if (!buddy) {
 
			continue;
 
		}
 
		Swift::Presence::ref presence = buddy->generatePresenceStanza(255);
 
		if (presence) {
 
			presence->setTo(to);
 
			m_component->getStanzaChannel()->sendPresence(presence);
 
		}
 
	}
 
}
 

	
 
void RosterManager::sendCurrentPresence(const Swift::JID &from, const Swift::JID &to) {
 
	Buddy *buddy = getBuddy(Buddy::JIDToLegacyName(from));
 
	if (buddy) {
 
		Swift::Presence::ref presence = buddy->generatePresenceStanza(255);
 
		if (presence) {
 
			presence->setTo(to);
 
			m_component->getStanzaChannel()->sendPresence(presence);
 
		}
 
	}
 
	else {
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setTo(to);
 
		response->setFrom(from);
 
		response->setType(Swift::Presence::Unavailable);
 
		m_component->getStanzaChannel()->sendPresence(response);
 
	}
 
}
 

	
 
void RosterManager::sendUnavailablePresences(const Swift::JID &to) {
 
	for (std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > >::iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
 
		Buddy *buddy = (*it).second;
 
		if (!buddy) {
 
			continue;
 
		}
 
		Swift::Presence::ref presence = buddy->generatePresenceStanza(255);
 
		if (presence) {
 
			presence->setTo(to);
 
			presence->setType(Swift::Presence::Unavailable);
 
			m_component->getStanzaChannel()->sendPresence(presence);
 
		}
 
	}
 

	
 
	// in gateway mode, we have to send unavailable presence for transport
 
	// contact
 
	Swift::Presence::ref response = Swift::Presence::create();
 
	response->setTo(to);
 
	response->setFrom(m_component->getJID());
 
	response->setType(Swift::Presence::Unavailable);
 
	m_component->getStanzaChannel()->sendPresence(response);
 
}
 

	
 
}
src/rosterstorage.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 "transport/rosterstorage.h"
 
#include "transport/buddy.h"
 
#include "transport/user.h"
 
#include "transport/storagebackend.h"
 
#include "transport/logging.h"
 

	
 
DEFINE_LOGGER(logger, "RosterStorage");
 

	
 
namespace Transport {
 

	
 
// static void save_settings(gpointer k, gpointer v, gpointer data) {
 
// 	PurpleValue *value = (PurpleValue *) v;
 
// 	std::string key((char *) k);
 
// 	SaveData *s = (SaveData *) data;
 
// 	AbstractUser *user = s->user;
 
// 	long id = s->id;
 
// 	if (purple_value_get_type(value) == PURPLE_TYPE_BOOLEAN) {
 
// 		if (purple_value_get_boolean(value))
 
// 			Transport::instance()->sql()->addBuddySetting(user->storageId(), id, key, "1", purple_value_get_type(value));
 
// 		else
 
// 			Transport::instance()->sql()->addBuddySetting(user->storageId(), id, key, "0", purple_value_get_type(value));
 
// 	}
 
// 	else if (purple_value_get_type(value) == PURPLE_TYPE_STRING) {
 
// 		const char *str = purple_value_get_string(value);
 
// 		Transport::instance()->sql()->addBuddySetting(user->storageId(), id, key, str ? str : "", purple_value_get_type(value));
 
// 	}
 
// }
 
// 
 
// static gboolean storeAbstractSpectrumBuddy(gpointer key, gpointer v, gpointer data) {
 
// 	AbstractUser *user = (AbstractUser *) data;
 
// 	AbstractSpectrumBuddy *s_buddy = (AbstractSpectrumBuddy *) v;
 
// 	if (s_buddy->getFlags() & SPECTRUM_BUDDY_IGNORE)
 
// 		return TRUE;
 
// 	
 
// 	// save PurpleBuddy
 
// 	std::string alias = s_buddy->getAlias();
 
// 	std::string name = s_buddy->getName();
 
// 	long id = s_buddy->getId();
 
// 
 
// 	// Buddy is not in DB
 
// 	if (id != -1) {
 
// 		Transport::instance()->sql()->addBuddy(user->storageId(), name, s_buddy->getSubscription(), s_buddy->getGroup(), alias, s_buddy->getFlags());
 
// 	}
 
// 	else {
 
// 		id = Transport::instance()->sql()->addBuddy(user->storageId(), name, s_buddy->getSubscription(), s_buddy->getGroup(), alias, s_buddy->getFlags());
 
// 		s_buddy->setId(id);
 
// 	}
 
// 	Log("buddyListSaveNode", id << " " << name << " " << alias << " " << s_buddy->getSubscription());
 
// 	if (s_buddy->getBuddy() && id != -1) {
 
// 		PurpleBuddy *buddy = s_buddy->getBuddy();
 
// 		SaveData *s = new SaveData;
 
// 		s->user = user;
 
// 		s->id = id;
 
// 		g_hash_table_foreach(buddy->node.settings, save_settings, s);
 
// 		delete s;
 
// 	}
 
// 	return TRUE;
 
// }
 

	
 
RosterStorage::RosterStorage(User *user, StorageBackend *storageBackend) {
 
	m_user = user;
 
	m_storageBackend = storageBackend;
 
	m_storageTimer = m_user->getComponent()->getNetworkFactories()->getTimerFactory()->createTimer(5000);
 
	m_storageTimer->onTick.connect(boost::bind(&RosterStorage::storeBuddies, this));
 
}
 

	
 
RosterStorage::~RosterStorage() {
 
	m_storageTimer->stop();
 
}
 

	
 
void RosterStorage::removeBuddy(Buddy *buddy) {
 
	if (buddy->getID() != -1) {
 
		m_storageBackend->removeBuddy(buddy->getID());
 
	}
 
}
 

	
 
void RosterStorage::storeBuddy(Buddy *buddy) {
 
	if (!buddy) {
 
		return;
 
	}
 
	if (buddy->getName().empty()) {
 
		return;
 
	}
 

	
 
	m_buddies[buddy->getName()] = buddy;
 
	m_storageTimer->start();
 
}
 

	
 
bool RosterStorage::storeBuddies() {
 
	if (m_buddies.size() == 0) {
 
		return false;
 
	}
 
	
 
	m_storageBackend->beginTransaction();
 

	
 
	for (std::map<std::string, Buddy *>::const_iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
 
		Buddy *buddy = (*it).second;
 
		BuddyInfo buddyInfo;
 
		buddyInfo.alias = buddy->getAlias();
 
		buddyInfo.legacyName = buddy->getName();
 
		buddyInfo.groups = buddy->getGroups();
 
		buddyInfo.subscription = buddy->getSubscription() == Buddy::Ask ? "ask" : "both";
 
		buddyInfo.id = buddy->getID();
 
		buddyInfo.flags = buddy->getFlags();
 
		buddyInfo.settings["icon_hash"].s = buddy->getIconHash();
 
		buddyInfo.settings["icon_hash"].type = TYPE_STRING;
 

	
 
		// Buddy is in DB
 
		if (buddyInfo.id != -1) {
 
			m_storageBackend->updateBuddy(m_user->getUserInfo().id, buddyInfo);
 
		}
 
		else {
 
			buddyInfo.id = m_storageBackend->addBuddy(m_user->getUserInfo().id, buddyInfo);
 
			buddy->setID(buddyInfo.id);
 
		}
 

	
 
// 		Log("buddyListSaveNode", id << " " << name << " " << alias << " " << s_buddy->getSubscription());
 
// 		if (s_buddy->getBuddy() && id != -1) {
 
// 			PurpleBuddy *buddy = s_buddy->getBuddy();
 
// 			SaveData *s = new SaveData;
 
// 			s->user = user;
 
// 			s->id = id;
 
// 			g_hash_table_foreach(buddy->node.settings, save_settings, s);
 
// 			delete s;
 
// 		}
 
	}
 

	
 
	m_buddies.clear();
 
	m_storageBackend->commitTransaction();
 
	return true;
 
}
 

	
 
void RosterStorage::removeBuddyFromQueue(Buddy *buddy) {
 
	m_buddies.erase(buddy->getName());
 
}
 

	
 
}
src/settingsadhoccommand.cpp
Show inline comments
 
new file 100644
 
/**
 
 * XMPP - libpurple transport
 
 *
 
 * Copyright (C) 2012, 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 "transport/settingsadhoccommand.h"
 
#include "transport/conversation.h"
 
#include "transport/usermanager.h"
 
#include "transport/buddy.h"
 
#include "transport/factory.h"
 
#include "transport/user.h"
 
#include "transport/logging.h"
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "SettingsAdHocCommand");
 

	
 
SettingsAdHocCommand::SettingsAdHocCommand(Component *component, const Swift::JID &initiator, const Swift::JID &to) : AdHocCommand(component, initiator, to) {
 
	m_state = Init;
 
}
 

	
 
SettingsAdHocCommand::~SettingsAdHocCommand() {
 
}
 

	
 
boost::shared_ptr<Swift::Command> SettingsAdHocCommand::getForm() {
 
	boost::shared_ptr<Swift::Command> response(new Swift::Command("settings", m_id, Swift::Command::Executing));
 
	boost::shared_ptr<Swift::Form> form(new Swift::Form());
 

	
 
	BOOST_FOREACH(Swift::FormField::ref field, m_fields) {
 
		form->addField(field);
 
	}
 

	
 
	response->setForm(form);
 
	return response;
 
}
 

	
 
boost::shared_ptr<Swift::Command> SettingsAdHocCommand::handleResponse(boost::shared_ptr<Swift::Command> payload) {
 
	
 
	
 

	
 
	boost::shared_ptr<Swift::Command> response;
 
	response->setStatus(Swift::Command::Completed);
 
	return response;
 
}
 

	
 
boost::shared_ptr<Swift::Command> SettingsAdHocCommand::handleRequest(boost::shared_ptr<Swift::Command> payload) {
 
	boost::shared_ptr<Swift::Command> response;
 

	
 
	switch (m_state) {
 
		case Init:
 
			response = getForm();
 
			m_state = WaitingForResponse;
 
			break;
 
		case WaitingForResponse:
 
			response = handleResponse(payload);
 
			break;
 
		default:
 
			break;
 
	}
 
	
 
	return response;
 
}
 

	
 
}
src/sqlite3backend.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
 
 */
 

	
 
#ifdef WITH_SQLITE
 

	
 
#include "transport/sqlite3backend.h"
 
#include "transport/util.h"
 
#include "transport/logging.h"
 
#include <boost/bind.hpp>
 

	
 
#define SQLITE_DB_VERSION 3
 
#define CHECK_DB_RESPONSE(stmt) \
 
	if(stmt) { \
 
		sqlite3_exec(m_db, "ROLLBACK;", NULL, NULL, NULL); \
 
		return 0; \
 
	}
 

	
 
// Prepare the SQL statement
 
#define PREP_STMT(sql, str) \
 
	if(sqlite3_prepare_v2(m_db, std::string(str).c_str(), -1, &sql, NULL)) { \
 
		LOG4CXX_ERROR(logger, str<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db))); \
 
		return false; \
 
	}
 

	
 
// Finalize the prepared statement
 
#define FINALIZE_STMT(prep) \
 
	if(prep != NULL) { \
 
		sqlite3_finalize(prep); \
 
	}
 
	
 
#define BEGIN(STATEMENT) 	sqlite3_reset(STATEMENT);\
 
							int STATEMENT##_id = 1;\
 
							int STATEMENT##_id_get = 0;\
 
							(void)STATEMENT##_id_get;
 

	
 
#define BIND_INT(STATEMENT, VARIABLE) sqlite3_bind_int(STATEMENT, STATEMENT##_id++, VARIABLE)
 
#define BIND_STR(STATEMENT, VARIABLE) sqlite3_bind_text(STATEMENT, STATEMENT##_id++, VARIABLE.c_str(), -1, SQLITE_STATIC)
 
#define RESET_GET_COUNTER(STATEMENT)	STATEMENT##_id_get = 0;
 
#define GET_INT(STATEMENT)	sqlite3_column_int(STATEMENT, STATEMENT##_id_get++)
 
#define GET_STR(STATEMENT)	(const char *) sqlite3_column_text(STATEMENT, STATEMENT##_id_get++)
 
#define EXECUTE_STATEMENT(STATEMENT, NAME) 	if(sqlite3_step(STATEMENT) != SQLITE_DONE) {\
 
		LOG4CXX_ERROR(logger, NAME<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));\
 
			}
 

	
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "SQLite3Backend");
 

	
 
SQLite3Backend::SQLite3Backend(Config *config) {
 
	m_config = config;
 
	m_db = NULL;
 
	m_prefix = CONFIG_STRING(m_config, "database.prefix");
 
}
 

	
 
SQLite3Backend::~SQLite3Backend(){
 
	if (m_db) {
 
		// Would be nice to use this:
 
		//
 
		//   sqlite3_stmt *pStmt;
 
		//   while((pStmt = sqlite3_next_stmt(db, 0)) != 0 ) {
 
		//    sqlite3_finalize(pStmt);
 
		//   }
 
		//
 
		// But requires SQLite3 >= 3.6.0 beta
 
	
 
		FINALIZE_STMT(m_setUser);
 
		FINALIZE_STMT(m_getUser);
 
		FINALIZE_STMT(m_removeUser);
 
		FINALIZE_STMT(m_removeUserBuddies);
 
		FINALIZE_STMT(m_removeUserSettings);
 
		FINALIZE_STMT(m_removeUserBuddiesSettings);
 
		FINALIZE_STMT(m_removeBuddy);
 
		FINALIZE_STMT(m_removeBuddySettings);
 
		FINALIZE_STMT(m_addBuddy);
 
		FINALIZE_STMT(m_updateBuddy);
 
		FINALIZE_STMT(m_getBuddies);
 
		FINALIZE_STMT(m_getBuddiesSettings);
 
		FINALIZE_STMT(m_getUserSetting);
 
		FINALIZE_STMT(m_setUserSetting);
 
		FINALIZE_STMT(m_updateUserSetting);
 
		FINALIZE_STMT(m_updateBuddySetting);
 
		FINALIZE_STMT(m_getBuddySetting);
 
		FINALIZE_STMT(m_setUserOnline);
 
		FINALIZE_STMT(m_getOnlineUsers);
 
		sqlite3_close(m_db);
 
	}
 
}
 

	
 
bool SQLite3Backend::connect() {
 
	LOG4CXX_INFO(logger, "Opening database " << CONFIG_STRING(m_config, "database.database"));
 
	if (sqlite3_open(CONFIG_STRING(m_config, "database.database").c_str(), &m_db)) {
 
		sqlite3_close(m_db);
 
		return false;
 
	}
 

	
 
	sqlite3_busy_timeout(m_db, 1500);
 

	
 
	if (createDatabase() == false)
 
		return false;
 

	
 
	PREP_STMT(m_setUser, "INSERT OR REPLACE INTO " + m_prefix + "users (jid, uin, password, language, encoding, last_login, vip) VALUES (?, ?, ?, ?, ?, DATETIME('NOW'), ?)");
 
	PREP_STMT(m_getUser, "SELECT id, jid, uin, password, encoding, language, vip FROM " + m_prefix + "users WHERE jid=?");
 

	
 
	PREP_STMT(m_removeUser, "DELETE FROM " + m_prefix + "users WHERE id=?");
 
	PREP_STMT(m_removeUserBuddies, "DELETE FROM " + m_prefix + "buddies WHERE user_id=?");
 
	PREP_STMT(m_removeUserSettings, "DELETE FROM " + m_prefix + "users_settings WHERE user_id=?");
 
	PREP_STMT(m_removeUserBuddiesSettings, "DELETE FROM " + m_prefix + "buddies_settings WHERE user_id=?");
 

	
 
	PREP_STMT(m_removeBuddy, "DELETE FROM " + m_prefix + "buddies WHERE id=?");
 
	PREP_STMT(m_removeBuddySettings, "DELETE FROM " + m_prefix + "buddies_settings WHERE buddy_id=?");
 

	
 
	PREP_STMT(m_addBuddy, "INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES (?, ?, ?, ?, ?, ?)");
 
	PREP_STMT(m_updateBuddy, "UPDATE " + m_prefix + "buddies SET groups=?, nickname=?, flags=?, subscription=? WHERE user_id=? AND uin=?");
 
	PREP_STMT(m_getBuddies, "SELECT id, uin, subscription, nickname, groups, flags FROM " + m_prefix + "buddies WHERE user_id=? ORDER BY id ASC");
 
	PREP_STMT(m_getBuddiesSettings, "SELECT buddy_id, type, var, value FROM " + m_prefix + "buddies_settings WHERE user_id=? ORDER BY buddy_id ASC");
 
	PREP_STMT(m_updateBuddySetting, "INSERT OR REPLACE INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES (?, ?, ?, ?, ?)");
 
	PREP_STMT(m_getBuddySetting, "SELECT type, value FROM " + m_prefix + "buddies_settings WHERE user_id=? AND buddy_id=? AND var=?");
 
	
 
	PREP_STMT(m_getUserSetting, "SELECT type, value FROM " + m_prefix + "users_settings WHERE user_id=? AND var=?");
 
	PREP_STMT(m_setUserSetting, "INSERT INTO " + m_prefix + "users_settings (user_id, var, type, value) VALUES (?,?,?,?)");
 
	PREP_STMT(m_updateUserSetting, "UPDATE " + m_prefix + "users_settings SET value=? WHERE user_id=? AND var=?");
 

	
 
	PREP_STMT(m_setUserOnline, "UPDATE " + m_prefix + "users SET online=?, last_login=DATETIME('NOW') WHERE id=?");
 
	PREP_STMT(m_getOnlineUsers, "SELECT jid FROM " + m_prefix + "users WHERE online=1");
 

	
 
	return true;
 
}
 

	
 
bool SQLite3Backend::createDatabase() {
 
	int not_exist = exec("CREATE TABLE " + m_prefix + "buddies ("
 
				"  id INTEGER PRIMARY KEY NOT NULL,"
 
				"  user_id int(10) NOT NULL,"
 
				"  uin varchar(255) NOT NULL,"
 
				"  subscription varchar(20) NOT NULL,"
 
				"  nickname varchar(255) NOT NULL,"
 
				"  groups varchar(255) NOT NULL,"
 
				"  flags int(4) NOT NULL DEFAULT '0'"
 
				");");
 

	
 
	if (not_exist) {
 
		exec("CREATE UNIQUE INDEX IF NOT EXISTS user_id ON " + m_prefix + "buddies (user_id, uin);");
 

	
 
		exec("CREATE TABLE IF NOT EXISTS " + m_prefix + "buddies_settings ("
 
					"  user_id int(10) NOT NULL,"
 
					"  buddy_id int(10) NOT NULL,"
 
					"  var varchar(50) NOT NULL,"
 
					"  type int(4) NOT NULL,"
 
					"  value varchar(255) NOT NULL,"
 
					"  PRIMARY KEY (buddy_id, var)"
 
					");");
 

	
 
		exec("CREATE INDEX IF NOT EXISTS user_id02 ON " + m_prefix + "buddies_settings (user_id);");
 

	
 
		exec("CREATE TABLE IF NOT EXISTS " + m_prefix + "users ("
 
					"  id INTEGER PRIMARY KEY NOT NULL,"
 
					"  jid varchar(255) NOT NULL,"
 
					"  uin varchar(4095) NOT NULL,"
 
					"  password varchar(255) NOT NULL,"
 
					"  language varchar(25) NOT NULL,"
 
					"  encoding varchar(50) NOT NULL DEFAULT 'utf8',"
 
					"  last_login datetime,"
 
					"  vip int(1) NOT NULL DEFAULT '0',"
 
					"  online int(1) NOT NULL DEFAULT '0'"
 
					");");
 

	
 
		exec("CREATE UNIQUE INDEX IF NOT EXISTS jid ON " + m_prefix + "users (jid);");
 

	
 
		exec("CREATE TABLE " + m_prefix + "users_settings ("
 
					"  user_id int(10) NOT NULL,"
 
					"  var varchar(50) NOT NULL,"
 
					"  type int(4) NOT NULL,"
 
					"  value varchar(4095) NOT NULL,"
 
					"  PRIMARY KEY (user_id, var)"
 
					");");
 
					
 
		exec("CREATE INDEX IF NOT EXISTS user_id03 ON " + m_prefix + "users_settings (user_id);");
 

	
 
		exec("CREATE TABLE IF NOT EXISTS " + m_prefix + "db_version ("
 
			"  ver INTEGER NOT NULL DEFAULT '3'"
 
			");");
 
		exec("REPLACE INTO " + m_prefix + "db_version (ver) values(3)");
 
	}
 
	return true;
 
}
 

	
 
bool SQLite3Backend::exec(const std::string &query) {
 
	char *errMsg = 0;
 
	int rc = sqlite3_exec(m_db, query.c_str(), NULL, 0, &errMsg);
 
	if (rc != SQLITE_OK) {
 
		LOG4CXX_ERROR(logger, errMsg << " during statement " << query);
 
		// This error is OK, because we try to create buddies table every time
 
		// to detect if DB is created properly.
 
		if (errMsg != "table buddies already exists") {
 
			LOG4CXX_ERROR(logger, errMsg << " during statement " << query);
 
		}
 
		sqlite3_free(errMsg);
 
		return false;
 
	}
 
	return true;
 
}
 

	
 
void SQLite3Backend::setUser(const UserInfo &user) {
 
	sqlite3_reset(m_setUser);
 
	sqlite3_bind_text(m_setUser, 1, user.jid.c_str(), -1, SQLITE_STATIC);
 
	sqlite3_bind_text(m_setUser, 2, user.uin.c_str(), -1, SQLITE_STATIC);
 
	sqlite3_bind_text(m_setUser, 3, user.password.c_str(), -1, SQLITE_STATIC);
 
	sqlite3_bind_text(m_setUser, 4, user.language.c_str(), -1, SQLITE_STATIC);
 
	sqlite3_bind_text(m_setUser, 5, user.encoding.c_str(), -1, SQLITE_STATIC);
 
	sqlite3_bind_int (m_setUser, 6, user.vip);
 

	
 
	if(sqlite3_step(m_setUser) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "setUser query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
	}
 
}
 

	
 
bool SQLite3Backend::getUser(const std::string &barejid, UserInfo &user) {
 
// 	SELECT id, jid, uin, password, encoding, language, vip FROM " + m_prefix + "users WHERE jid=?
 
	sqlite3_reset(m_getUser);
 
	sqlite3_bind_text(m_getUser, 1, barejid.c_str(), -1, SQLITE_TRANSIENT);
 

	
 
	int ret;
 
	while((ret = sqlite3_step(m_getUser)) == SQLITE_ROW) {
 
		user.id = sqlite3_column_int(m_getUser, 0);
 
		user.jid = (const char *) sqlite3_column_text(m_getUser, 1);
 
		user.uin = (const char *) sqlite3_column_text(m_getUser, 2);
 
		user.password = (const char *) sqlite3_column_text(m_getUser, 3);
 
		user.encoding = (const char *) sqlite3_column_text(m_getUser, 4);
 
		user.language = (const char *) sqlite3_column_text(m_getUser, 5);
 
		user.vip = sqlite3_column_int(m_getUser, 6) != 0;
 
		while((ret = sqlite3_step(m_getUser)) == SQLITE_ROW) {
 
		}
 
		return true;
 
	}
 

	
 
	if (ret != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "getUser query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
	}
 

	
 
	return false;
 
}
 

	
 
void SQLite3Backend::setUserOnline(long id, bool online) {
 
	BEGIN(m_setUserOnline);
 
	BIND_INT(m_setUserOnline, (int)online);
 
	BIND_INT(m_setUserOnline, id);
 
	EXECUTE_STATEMENT(m_setUserOnline, "setUserOnline query");
 
}
 

	
 
bool SQLite3Backend::getOnlineUsers(std::vector<std::string> &users) {
 
	sqlite3_reset(m_getOnlineUsers);
 

	
 
	int ret;
 
	while((ret = sqlite3_step(m_getOnlineUsers)) == SQLITE_ROW) {
 
		std::string jid = (const char *) sqlite3_column_text(m_getOnlineUsers, 0);
 
		users.push_back(jid);
 
	}
 

	
 
	if (ret != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "getOnlineUsers query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
long SQLite3Backend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
 
// 	"INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES (?, ?, ?, ?, ?, ?)"
 
	BEGIN(m_addBuddy);
 
	BIND_INT(m_addBuddy, userId);
 
	BIND_STR(m_addBuddy, buddyInfo.legacyName);
 
	BIND_STR(m_addBuddy, buddyInfo.subscription);
 
	BIND_STR(m_addBuddy, Util::serializeGroups(buddyInfo.groups));
 
	BIND_STR(m_addBuddy, buddyInfo.alias);
 
	BIND_INT(m_addBuddy, buddyInfo.flags);
 

	
 
	if(sqlite3_step(m_addBuddy) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "addBuddy query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return -1;
 
	}
 

	
 
	long id = (long) sqlite3_last_insert_rowid(m_db);
 

	
 
// 	INSERT OR REPLACE INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES (?, ?, ?, ?, ?)
 
	BEGIN(m_updateBuddySetting);
 
	BIND_INT(m_updateBuddySetting, userId);
 
	BIND_INT(m_updateBuddySetting, id);
 
	BIND_STR(m_updateBuddySetting, buddyInfo.settings.find("icon_hash")->first);
 
	BIND_INT(m_updateBuddySetting, TYPE_STRING);
 
	BIND_STR(m_updateBuddySetting, buddyInfo.settings.find("icon_hash")->second.s);
 

	
 
	EXECUTE_STATEMENT(m_updateBuddySetting, "updateBuddySetting query");
 
	return id;
 
}
 

	
 
void SQLite3Backend::updateBuddy(long userId, const BuddyInfo &buddyInfo) {
 
// 	UPDATE " + m_prefix + "buddies SET groups=?, nickname=?, flags=?, subscription=? WHERE user_id=? AND uin=?
 
	BEGIN(m_updateBuddy);
 
	BIND_STR(m_updateBuddy, Util::serializeGroups(buddyInfo.groups));
 
	BIND_STR(m_updateBuddy, buddyInfo.alias);
 
	BIND_INT(m_updateBuddy, buddyInfo.flags);
 
	BIND_STR(m_updateBuddy, buddyInfo.subscription);
 
	BIND_INT(m_updateBuddy, userId);
 
	BIND_STR(m_updateBuddy, buddyInfo.legacyName);
 

	
 
	EXECUTE_STATEMENT(m_updateBuddy, "updateBuddy query");
 

	
 
// 	INSERT OR REPLACE INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES (?, ?, ?, ?, ?)
 
	BEGIN(m_updateBuddySetting);
 
	BIND_INT(m_updateBuddySetting, userId);
 
	BIND_INT(m_updateBuddySetting, buddyInfo.id);
 
	BIND_STR(m_updateBuddySetting, buddyInfo.settings.find("icon_hash")->first);
 
	BIND_INT(m_updateBuddySetting, TYPE_STRING);
 
	BIND_STR(m_updateBuddySetting, buddyInfo.settings.find("icon_hash")->second.s);
 

	
 
	EXECUTE_STATEMENT(m_updateBuddySetting, "updateBuddySetting query");
 
}
 

	
 
bool SQLite3Backend::getBuddies(long id, std::list<BuddyInfo> &roster) {
 
//	SELECT id, uin, subscription, nickname, groups, flags FROM " + m_prefix + "buddies WHERE user_id=? ORDER BY id ASC
 
	BEGIN(m_getBuddies);
 
	BIND_INT(m_getBuddies, id);
 

	
 
// 	"SELECT buddy_id, type, var, value FROM " + m_prefix + "buddies_settings WHERE user_id=? ORDER BY buddy_id ASC"
 
	BEGIN(m_getBuddiesSettings);
 
	BIND_INT(m_getBuddiesSettings, id);
 

	
 
	SettingVariableInfo var;
 
	long buddy_id = -1;
 
	std::string key;
 

	
 
	int ret;
 
	while((ret = sqlite3_step(m_getBuddies)) == SQLITE_ROW) {
 
		BuddyInfo b;
 
		RESET_GET_COUNTER(m_getBuddies);
 
		b.id = GET_INT(m_getBuddies);
 
		b.legacyName = GET_STR(m_getBuddies);
 
		b.subscription = GET_STR(m_getBuddies);
 
		b.alias = GET_STR(m_getBuddies);
 
		std::string groups = GET_STR(m_getBuddies);
 
		b.groups = Util::deserializeGroups(groups);
 
		b.flags = GET_INT(m_getBuddies);
 

	
 
		if (buddy_id == b.id) {
 
			std::cout << "Adding buddy info " << key << "\n";
 
			b.settings[key] = var;
 
			buddy_id = -1;
 
		}
 

	
 
		while(buddy_id == -1 && (ret = sqlite3_step(m_getBuddiesSettings)) == SQLITE_ROW) {
 
			RESET_GET_COUNTER(m_getBuddiesSettings);
 
			buddy_id = GET_INT(m_getBuddiesSettings);
 
			
 
			var.type = GET_INT(m_getBuddiesSettings);
 
			key = GET_STR(m_getBuddiesSettings);
 
			std::string val = GET_STR(m_getBuddiesSettings);
 

	
 
			switch (var.type) {
 
				case TYPE_BOOLEAN:
 
					var.b = atoi(val.c_str());
 
					break;
 
				case TYPE_STRING:
 
					var.s = val;
 
					break;
 
				default:
 
					if (buddy_id == b.id) {
 
						buddy_id = -1;
 
					}
 
					continue;
 
					break;
 
			}
 
			if (buddy_id == b.id) {
 
				std::cout << "Adding buddy info " << key << "=" << val << "\n";
 
				b.settings[key] = var;
 
				buddy_id = -1;
 
			}
 
		}
 

	
 
// 		if (ret != SQLITE_DONE) {
 
// 			LOG4CXX_ERROR(logger, "getBuddiesSettings query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
// 			return false;
 
// 		}
 

	
 
		roster.push_back(b);
 
	}
 

	
 
	while((ret = sqlite3_step(m_getBuddiesSettings)) == SQLITE_ROW) {
 
	}
 

	
 
	if (ret != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "getBuddies query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return false;
 
	}
 
	
 
	return true;
 
}
 

	
 
void SQLite3Backend::removeBuddy(long id) {
 
	sqlite3_reset(m_removeBuddy);
 
	sqlite3_bind_int(m_removeBuddy, 1, id);
 
	if(sqlite3_step(m_removeBuddy) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "removeBuddy query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return;
 
	}
 

	
 
	sqlite3_reset(m_removeBuddySettings);
 
	sqlite3_bind_int(m_removeBuddySettings, 1, id);
 
	if(sqlite3_step(m_removeBuddySettings) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "removeBuddySettings query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return;
 
	}
 
}
 

	
 
bool SQLite3Backend::removeUser(long id) {
 
	sqlite3_reset(m_removeUser);
 
	sqlite3_bind_int(m_removeUser, 1, id);
 
	if(sqlite3_step(m_removeUser) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "removeUser query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return false;
 
	}
 

	
 
	sqlite3_reset(m_removeUserSettings);
 
	sqlite3_bind_int(m_removeUserSettings, 1, id);
 
	if(sqlite3_step(m_removeUserSettings) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "removeUserSettings query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return false;
 
	}
 

	
 
	sqlite3_reset(m_removeUserBuddies);
 
	sqlite3_bind_int(m_removeUserBuddies, 1, id);
 
	if(sqlite3_step(m_removeUserBuddies) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "removeUserBuddies query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return false;
 
	}
 

	
 
	sqlite3_reset(m_removeUserBuddiesSettings);
 
	sqlite3_bind_int(m_removeUserBuddiesSettings, 1, id);
 
	if(sqlite3_step(m_removeUserBuddiesSettings) != SQLITE_DONE) {
 
		LOG4CXX_ERROR(logger, "removeUserBuddiesSettings query"<< (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db)));
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
void SQLite3Backend::getUserSetting(long id, const std::string &variable, int &type, std::string &value) {
 
	BEGIN(m_getUserSetting);
 
	BIND_INT(m_getUserSetting, id);
 
	BIND_STR(m_getUserSetting, variable);
 
	if(sqlite3_step(m_getUserSetting) != SQLITE_ROW) {
 
		BEGIN(m_setUserSetting);
 
		BIND_INT(m_setUserSetting, id);
 
		BIND_STR(m_setUserSetting, variable);
 
		BIND_INT(m_setUserSetting, type);
 
		BIND_STR(m_setUserSetting, value);
 
		EXECUTE_STATEMENT(m_setUserSetting, "m_setUserSetting");
 
	}
 
	else {
 
		type = GET_INT(m_getUserSetting);
 
		value = GET_STR(m_getUserSetting);
 
	}
 

	
 
	int ret;
 
	while((ret = sqlite3_step(m_getUserSetting)) == SQLITE_ROW) {
 
	}
 
}
 

	
 
void SQLite3Backend::updateUserSetting(long id, const std::string &variable, const std::string &value) {
 
	BEGIN(m_updateUserSetting);
 
	BIND_STR(m_updateUserSetting, value);
 
	BIND_INT(m_updateUserSetting, id);
 
	BIND_STR(m_updateUserSetting, variable);
 
	EXECUTE_STATEMENT(m_updateUserSetting, "m_updateUserSetting");
 
}
 

	
 
void SQLite3Backend::getBuddySetting(long userId, long buddyId, const std::string &variable, int &type, std::string &value) {
 
	BEGIN(m_getBuddySetting);
 
	BIND_INT(m_getBuddySetting, userId);
 
	BIND_INT(m_getBuddySetting, buddyId);
 
	BIND_STR(m_getBuddySetting, variable);
 
	if(sqlite3_step(m_getBuddySetting) == SQLITE_ROW) {
 
		type = GET_INT(m_getBuddySetting);
 
		value = GET_STR(m_getBuddySetting);
 
	}
 

	
 
	int ret;
 
	while((ret = sqlite3_step(m_getBuddySetting)) == SQLITE_ROW) {
 
	}
 
}
 

	
 
void SQLite3Backend::updateBuddySetting(long userId, long buddyId, const std::string &variable, int type, const std::string &value) {
 
	BEGIN(m_updateBuddySetting);
 
	BIND_INT(m_updateBuddySetting, userId);
 
	BIND_INT(m_updateBuddySetting, buddyId);
 
	BIND_STR(m_updateBuddySetting, variable);
 
	BIND_INT(m_updateBuddySetting, type);
 
	BIND_STR(m_updateBuddySetting, value);
 
	EXECUTE_STATEMENT(m_updateBuddySetting, "m_updateBuddySetting");
 
}
 

	
 
void SQLite3Backend::beginTransaction() {
 
	exec("BEGIN TRANSACTION;");
 
}
 

	
 
void SQLite3Backend::commitTransaction() {
 
	exec("COMMIT TRANSACTION;");
 
}
 

	
 
}
 

	
 
#endif
src/transport.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/transport.h"
 
#include <boost/bind.hpp>
 
#include <boost/smart_ptr/make_shared.hpp>
 
#include <boost/algorithm/string/predicate.hpp>
 
#include "transport/storagebackend.h"
 
#include "transport/factory.h"
 
#include "transport/userregistry.h"
 
#include "transport/logging.h"
 
#include "discoinforesponder.h"
 
#include "discoitemsresponder.h"
 
#include "storageparser.h"
 
#ifdef _MSC_VER
 
#include <Swiften/TLS/CAPICertificate.h>
 
#include "Swiften/TLS/Schannel/SchannelServerContext.h"
 
#include "Swiften/TLS/Schannel/SchannelServerContextFactory.h"
 
#else
 
#include "Swiften/TLS/PKCS12Certificate.h"
 
#include "Swiften/TLS/OpenSSL/OpenSSLServerContext.h"
 
#include "Swiften/TLS/OpenSSL/OpenSSLServerContextFactory.h"
 
#endif
 
#include "Swiften/Parser/PayloadParsers/AttentionParser.h"
 
#include "Swiften/Serializer/PayloadSerializers/AttentionSerializer.h"
 
#include "Swiften/Parser/PayloadParsers/XHTMLIMParser.h"
 
#include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h"
 
#include "Swiften/Parser/PayloadParsers/StatsParser.h"
 
#include "Swiften/Serializer/PayloadSerializers/StatsSerializer.h"
 
#include "Swiften/Parser/PayloadParsers/GatewayPayloadParser.h"
 
#include "Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.h"
 
#include "Swiften/Serializer/PayloadSerializers/SpectrumErrorSerializer.h"
 
#include "Swiften/Parser/PayloadParsers/MUCPayloadParser.h"
 
#include "transport/BlockParser.h"
 
#include "transport/BlockSerializer.h"
 
#include "Swiften/Parser/PayloadParsers/InvisibleParser.h"
 
#include "Swiften/Serializer/PayloadSerializers/InvisibleSerializer.h"
 
#include "Swiften/Swiften.h"
 

	
 
using namespace Swift;
 
using namespace boost;
 

	
 
namespace Transport {
 
	
 
DEFINE_LOGGER(logger, "Component");
 
DEFINE_LOGGER(logger_xml, "Component.XML");
 

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

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

	
 
	m_factories = factories;
 

	
 
	m_reconnectTimer = m_factories->getTimerFactory()->createTimer(3000);
 
	m_reconnectTimer->onTick.connect(bind(&Component::start, this)); 
 

	
 
	if (CONFIG_BOOL(m_config, "service.server_mode")) {
 
		LOG4CXX_INFO(logger, "Creating component in server mode on port " << CONFIG_INT(m_config, "service.port"));
 
		m_server = new Swift::Server(loop, m_factories, m_userRegistry, m_jid, CONFIG_INT(m_config, "service.port"));
 
		if (!CONFIG_STRING(m_config, "service.cert").empty()) {
 
			LOG4CXX_INFO(logger, "Using PKCS#12 certificate " << CONFIG_STRING(m_config, "service.cert"));
 
			LOG4CXX_INFO(logger, "SSLv23_server_method used.");
 
#ifdef _MSC_VER
 
			TLSServerContextFactory *f = new SchannelServerContextFactory();
 
			m_server->addTLSEncryption(f, boost::make_shared<CAPICertificate>(CONFIG_STRING(m_config, "service.cert")));
 
#else
 
			TLSServerContextFactory *f = new OpenSSLServerContextFactory();
 
			m_server->addTLSEncryption(f, boost::make_shared<PKCS12Certificate>(CONFIG_STRING(m_config, "service.cert"), createSafeByteArray(CONFIG_STRING(m_config, "service.cert_password"))));
 
#endif
 
			
 
		}
 
		else {
 
			LOG4CXX_WARN(logger, "No PKCS#12 certificate used. TLS is disabled.");
 
		}
 
// 		m_server->start();
 
		m_stanzaChannel = m_server->getStanzaChannel();
 
		m_iqRouter = m_server->getIQRouter();
 

	
 
		m_server->addPayloadParserFactory(new GenericPayloadParserFactory<StorageParser>("private", "jabber:iq:private"));
 
		m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
 
		m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
 
		m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
 
		m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
 
		m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::StatsParser>("query", "http://jabber.org/protocol/stats"));
 
		m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::GatewayPayloadParser>("query", "jabber:iq:gateway"));
 
		m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::MUCPayloadParser>("x", "http://jabber.org/protocol/muc"));
 

	
 
		m_server->addPayloadSerializer(new Swift::AttentionSerializer());
 
		m_server->addPayloadSerializer(new Swift::XHTMLIMSerializer());
 
		m_server->addPayloadSerializer(new Transport::BlockSerializer());
 
		m_server->addPayloadSerializer(new Swift::InvisibleSerializer());
 
		m_server->addPayloadSerializer(new Swift::StatsSerializer());
 
		m_server->addPayloadSerializer(new Swift::SpectrumErrorSerializer());
 
		m_server->addPayloadSerializer(new Swift::GatewayPayloadSerializer());
 

	
 
		m_server->onDataRead.connect(boost::bind(&Component::handleDataRead, this, _1));
 
		m_server->onDataWritten.connect(boost::bind(&Component::handleDataWritten, this, _1));
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, "Creating component in gateway mode");
 
		m_component = new Swift::Component(loop, m_factories, m_jid, CONFIG_STRING(m_config, "service.password"));
 
		m_component->setSoftwareVersion("", "");
 
		m_component->onConnected.connect(bind(&Component::handleConnected, this));
 
		m_component->onError.connect(boost::bind(&Component::handleConnectionError, this, _1));
 
		m_component->onDataRead.connect(boost::bind(&Component::handleDataRead, this, _1));
 
		m_component->onDataWritten.connect(boost::bind(&Component::handleDataWritten, this, _1));
 

	
 
		m_component->addPayloadParserFactory(new GenericPayloadParserFactory<StorageParser>("private", "jabber:iq:private"));
 
		m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
 
		m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
 
		m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
 
		m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
 
		m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::StatsParser>("query", "http://jabber.org/protocol/stats"));
 
		m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::GatewayPayloadParser>("query", "jabber:iq:gateway"));
 
		m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::MUCPayloadParser>("x", "http://jabber.org/protocol/muc"));
 

	
 
		m_component->addPayloadSerializer(new Swift::AttentionSerializer());
 
		m_component->addPayloadSerializer(new Swift::XHTMLIMSerializer());
 
		m_component->addPayloadSerializer(new Transport::BlockSerializer());
 
		m_component->addPayloadSerializer(new Swift::InvisibleSerializer());
 
		m_component->addPayloadSerializer(new Swift::StatsSerializer());
 
		m_component->addPayloadSerializer(new Swift::SpectrumErrorSerializer());
 
		m_component->addPayloadSerializer(new Swift::GatewayPayloadSerializer());
 

	
 
		m_stanzaChannel = m_component->getStanzaChannel();
 
		m_iqRouter = m_component->getIQRouter();
 
	}
 

	
 
	m_capsMemoryStorage = new CapsMemoryStorage();
 
	m_capsManager = new CapsManager(m_capsMemoryStorage, m_stanzaChannel, m_iqRouter);
 
	m_entityCapsManager = new EntityCapsManager(m_capsManager, m_stanzaChannel);
 
 	m_entityCapsManager->onCapsChanged.connect(boost::bind(&Component::handleCapsChanged, this, _1));
 
	
 
	m_presenceOracle = new Transport::PresenceOracle(m_stanzaChannel);
 
	m_presenceOracle->onPresenceChange.connect(bind(&Component::handlePresence, this, _1));
 

	
 
	m_discoInfoResponder = new DiscoInfoResponder(m_iqRouter, m_config);
 
	m_discoInfoResponder->start();
 

	
 
	m_discoItemsResponder = new DiscoItemsResponder(m_iqRouter);
 
	m_discoItemsResponder->start();
 

	
 
// 
 
// 	m_registerHandler = new SpectrumRegisterHandler(m_component);
 
// 	m_registerHandler->start();
 
}
 

	
 
Component::~Component() {
 
	delete m_presenceOracle;
 
	delete m_entityCapsManager;
 
	delete m_capsManager;
 
	delete m_capsMemoryStorage;
 
	delete m_discoInfoResponder;
 
	delete m_discoItemsResponder;
 
	if (m_component)
 
		delete m_component;
 
	if (m_server) {
 
		m_server->stop();
 
		delete m_server;
 
	}
 
}
 

	
 
Swift::StanzaChannel *Component::getStanzaChannel() {
 
	return m_stanzaChannel;
 
}
 

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

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

	
 
Swift::CapsInfo &Component::getBuddyCapsInfo() {
 
		return m_discoInfoResponder->getBuddyCapsInfo();
 
}
 

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

	
 
void Component::start() {
 
	if (m_component && !m_component->isAvailable()) {
 
		LOG4CXX_INFO(logger, "Connecting XMPP server " << CONFIG_STRING(m_config, "service.server") << " port " << CONFIG_INT(m_config, "service.port"));
 
		if (CONFIG_INT(m_config, "service.port") == 5222) {
 
			LOG4CXX_WARN(logger, "Port 5222 is usually used for client connections, not for component connections! Are you sure you are using right port?");
 
		}
 
		m_reconnectCount++;
 
		m_component->connect(CONFIG_STRING(m_config, "service.server"), CONFIG_INT(m_config, "service.port"));
 
		m_reconnectTimer->stop();
 
	}
 
	else if (m_server) {
 
		LOG4CXX_INFO(logger, "Starting component in server mode on port " << CONFIG_INT(m_config, "service.port"));
 
		m_server->start();
 

	
 
		//Type casting to BoostConnectionServer since onStopped signal is not defined in ConnectionServer
 
		//Ideally, onStopped must be defined in ConnectionServer
 
		boost::dynamic_pointer_cast<Swift::BoostConnectionServer>(m_server->getConnectionServer())->onStopped.connect(boost::bind(&Component::handleServerStopped, this, _1));
 
		
 
		// We're connected right here, because we're in server mode...
 
		handleConnected();
 
	}
 
}
 

	
 
void Component::stop() {
 
	if (m_component) {
 
		m_reconnectCount = 0;
 
		// TODO: Call this once swiften will fix assert(!session_);
 
// 		m_component->disconnect();
 
		m_reconnectTimer->stop();
 
	}
 
	else if (m_server) {
 
		LOG4CXX_INFO(logger, "Stopping component in server mode on port " << CONFIG_INT(m_config, "service.port"));
 
		m_server->stop();
 
	}
 
}
 

	
 
void Component::handleConnected() {
 
	onConnected();
 
	m_reconnectCount = 0;
 
	m_reconnectTimer->stop();
 
}
 

	
 
void Component::handleServerStopped(boost::optional<Swift::BoostConnectionServer::Error> e) {
 
	if(e != NULL ) {
 
		if(*e == Swift::BoostConnectionServer::Conflict)
 
			LOG4CXX_INFO(logger, "Port "<< CONFIG_INT(m_config, "service.port") << " already in use! Stopping server..");
 
		if(*e == Swift::BoostConnectionServer::UnknownError)
 
			LOG4CXX_INFO(logger, "Unknown error occured! Stopping server..");
 
		exit(1);
 
	}
 
}
 

	
 

	
 
void Component::handleConnectionError(const ComponentError &error) {
 
	onConnectionError(error);
 
// 	if (m_reconnectCount == 2)
 
// 		Component::instance()->userManager()->removeAllUsers();
 
	std::string str = "Unknown error";
 
	switch (error.getType()) {
 
		case ComponentError::UnknownError: str = "Unknown error"; break;
 
		case ComponentError::ConnectionError: str = "Connection error"; break;
 
		case ComponentError::ConnectionReadError: str = "Connection read error"; break;
 
		case ComponentError::ConnectionWriteError: str = "Connection write error"; break;
 
		case ComponentError::XMLError: str = "XML Error"; break;
 
		case ComponentError::AuthenticationFailedError: str = "Authentication failed error"; break;
 
		case ComponentError::UnexpectedElementError: str = "Unexpected element error"; break;
 
	}
 
	LOG4CXX_INFO(logger, "Disconnected from XMPP server. Error: " << str);
 

	
 
	m_reconnectTimer->start();
 
}
 

	
 
void Component::handleDataRead(const Swift::SafeByteArray &data) {
 
	std::string d = safeByteArrayToString(data);
 
	if (!boost::starts_with(d, "<auth")) {
 
		LOG4CXX_INFO(logger_xml, "XML IN " << d);
 
	}
 
}
 

	
 
void Component::handleDataWritten(const Swift::SafeByteArray &data) {
 
	LOG4CXX_INFO(logger_xml, "XML OUT " << safeByteArrayToString(data));
 
}
 

	
 
void Component::handlePresence(Swift::Presence::ref presence) {
 
	bool isMUC = presence->getPayload<MUCPayload>() != NULL || *presence->getTo().getNode().c_str() == '#';
 
	// filter out login/logout presence spam
 
	if (!presence->getTo().getNode().empty() && isMUC == false)
 
		return;
 

	
 
	// filter out bad presences
 
	if (!presence->getFrom().isValid()) {
 
		return;
 
	}
 

	
 
	// check if we have this client's capabilities and ask for them
 
	if (presence->getType() != Swift::Presence::Unavailable) {
 
		boost::shared_ptr<CapsInfo> capsInfo = presence->getPayload<CapsInfo>();
 
		if (capsInfo && capsInfo->getHash() == "sha-1") {
 
			/*haveFeatures = */m_entityCapsManager->getCaps(presence->getFrom()) != DiscoInfo::ref();
 
		}
 
#ifdef SUPPORT_LEGACY_CAPS
 
		else {
 
			GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(presence->getFrom(), m_iqRouter);
 
			discoInfoRequest->onResponse.connect(boost::bind(&Component::handleDiscoInfoResponse, this, _1, _2, presence->getFrom()));
 
			discoInfoRequest->send();
 
		}
 
#endif
 
	}
 

	
 
	onUserPresenceReceived(presence);
 
}
 

	
 
void Component::handleDiscoInfoResponse(boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error, const Swift::JID& jid) {
 
#ifdef SUPPORT_LEGACY_CAPS
 
	onUserDiscoInfoReceived(jid, info);
 
#endif
 
}
 

	
 
void Component::handleCapsChanged(const Swift::JID& jid) {
 
	onUserDiscoInfoReceived(jid, m_entityCapsManager->getCaps(jid));
 
}
 

	
 
}
src/usermanager.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/usermanager.h"
 
#include "transport/user.h"
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 
#include "transport/conversationmanager.h"
 
#include "transport/rostermanager.h"
 
#include "transport/userregistry.h"
 
#include "transport/logging.h"
 
#include "storageresponder.h"
 

	
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Elements/StreamError.h"
 
#include "malloc.h"
 
// #include "valgrind/memcheck.h"
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "UserManager");
 

	
 
UserManager::UserManager(Component *component, UserRegistry *userRegistry, StorageBackend *storageBackend) {
 
	m_cachedUser = NULL;
 
	m_onlineBuddies = 0;
 
	m_sentToXMPP = 0;
 
	m_sentToBackend = 0;
 
	m_component = component;
 
	m_storageBackend = storageBackend;
 
	m_storageResponder = NULL;
 
	m_userRegistry = userRegistry;
 

	
 
	if (m_storageBackend) {
 
		m_storageResponder = new StorageResponder(component->getIQRouter(), m_storageBackend, this);
 
		m_storageResponder->start();
 
	}
 

	
 
	component->onUserPresenceReceived.connect(bind(&UserManager::handlePresence, this, _1));
 
	component->onUserDiscoInfoReceived.connect(bind(&UserManager::handleDiscoInfo, this, _1, _2));
 
	m_component->getStanzaChannel()->onMessageReceived.connect(bind(&UserManager::handleMessageReceived, this, _1));
 
	m_component->getStanzaChannel()->onPresenceReceived.connect(bind(&UserManager::handleGeneralPresenceReceived, this, _1));
 

	
 
	m_userRegistry->onConnectUser.connect(bind(&UserManager::connectUser, this, _1));
 
	m_userRegistry->onDisconnectUser.connect(bind(&UserManager::disconnectUser, this, _1));
 

	
 
	m_removeTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(1);
 
}
 

	
 
UserManager::~UserManager(){
 
	if (m_storageResponder) {
 
		m_storageResponder->stop();
 
		delete m_storageResponder;
 
	}
 
}
 

	
 
void UserManager::addUser(User *user) {
 
	m_users[user->getJID().toBare().toString()] = user;
 
	if (m_storageBackend) {
 
		m_storageBackend->setUserOnline(user->getUserInfo().id, true);
 
	}
 
	onUserCreated(user);
 
}
 

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

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

	
 
Swift::DiscoInfo::ref UserManager::getCaps(const Swift::JID &jid) const {
 
	std::map<std::string, User *>::const_iterator it = m_users.find(jid.toBare().toString());
 
	if (it == m_users.end()) {
 
		return Swift::DiscoInfo::ref();
 
	}
 

	
 
	User *user = it->second;
 
	return user->getCaps(jid);
 
}
 

	
 
void UserManager::removeUser(User *user, bool onUserBehalf) {
 
	m_users.erase(user->getJID().toBare().toString());
 
	if (m_cachedUser == user)
 
		m_cachedUser = NULL;
 

	
 
	if (m_component->inServerMode()) {
 
		disconnectUser(user->getJID());
 
	}
 

	
 
	if (m_storageBackend && onUserBehalf) {
 
		m_storageBackend->setUserOnline(user->getUserInfo().id, false);
 
	}
 

	
 
	onUserDestroyed(user);
 
	delete user;
 
#ifndef WIN32
 
	malloc_trim(0);
 
#endif
 
// 	VALGRIND_DO_LEAK_CHECK;
 
}
 

	
 
void UserManager::removeAllUsers(bool onUserBehalf) {
 
	while(m_users.begin() != m_users.end()) {
 
		removeUser((*m_users.begin()).second, onUserBehalf);
 
	}
 
}
 

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

	
 
void UserManager::handleDiscoInfo(const Swift::JID& jid, boost::shared_ptr<Swift::DiscoInfo> info) {
 
	User *user = getUser(jid.toBare().toString());
 
	if (!user) {
 
		return;
 
	}
 

	
 
	user->handleDiscoInfo(jid, info);
 
}
 

	
 
void UserManager::handlePresence(Swift::Presence::ref presence) {
 
	std::string barejid = presence->getTo().toBare().toString();
 
	std::string userkey = presence->getFrom().toBare().toString();
 

	
 
	User *user = getUser(userkey);
 
	// Create user class if it's not there
 
	if (!user) {
 
		// Admin user is not legacy network user, so do not create User class instance for him
 
		if (m_component->inServerMode()) {
 
		    std::vector<std::string> const &x = CONFIG_VECTOR(m_component->getConfig(),"service.admin_jid");
 
		    if (std::find(x.begin(), x.end(), presence->getFrom().toBare().toString()) != x.end()) {
 
		
 
			// Send admin contact to the user.
 
			Swift::RosterPayload::ref payload = Swift::RosterPayload::ref(new Swift::RosterPayload());
 
			Swift::RosterItemPayload item;
 
			item.setJID(m_component->getJID());
 
			item.setName("Admin");
 
			item.setSubscription(Swift::RosterItemPayload::Both);
 
			payload->addItem(item);
 

	
 
			Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(payload, presence->getFrom(), m_component->getIQRouter());
 
			request->send();
 

	
 
			Swift::Presence::ref response = Swift::Presence::create();
 
			response->setTo(presence->getFrom());
 
			response->setFrom(m_component->getJID());
 
			m_component->getStanzaChannel()->sendPresence(response);
 
			return;
 
		    }
 
		}
 

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

	
 
			// Set user offline in database
 
			if (m_storageBackend) {
 
				UserInfo res;
 
				bool registered = m_storageBackend->getUser(userkey, res);
 
				if (registered) {
 
					m_storageBackend->setUserOnline(res.id, false);
 
				}
 
			}
 
			return;
 
		}
 

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

	
 
		// In server mode, there's no registration, but we store users into database
 
		// (if storagebackend is available) because of caching. Passwords are not stored
 
		// in server mode.
 
		if (m_component->inServerMode()) {
 
			if (!registered) {
 
				res.password = "";
 
				res.uin = presence->getFrom().getNode();
 
				res.jid = userkey;
 
				if (res.uin.find_last_of("%") != std::string::npos) { // OK
 
					res.uin.replace(res.uin.find_last_of("%"), 1, "@"); // OK
 
				}
 
				if (m_storageBackend) {
 
					// store user and getUser again to get user ID.
 
					m_storageBackend->setUser(res);
 
					registered = m_storageBackend->getUser(userkey, res);
 
				}
 
				else {
 
					registered = true;
 
				}
 
			}
 
			res.password = m_userRegistry->getUserPassword(userkey);
 
		}
 

	
 
		// We allow auto_register feature in gateway-mode. This allows IRC user to register
 
		// the transport just by joining the room.
 
		if (!m_component->inServerMode()) {
 
			if (!registered && CONFIG_BOOL(m_component->getConfig(), "registration.auto_register")) {
 
				res.password = "";
 
				res.jid = userkey;
 

	
 
				bool isMUC = presence->getPayload<Swift::MUCPayload>() != NULL || *presence->getTo().getNode().c_str() == '#';
 
				if (isMUC) {
 
					res.uin = presence->getTo().getResource();
 
				}
 
				else {
 
					res.uin = presence->getFrom().toString();
 
				}
 
				LOG4CXX_INFO(logger, "Auto-registering user " << userkey << " with uin=" << res.uin);
 

	
 
				if (m_storageBackend) {
 
					// store user and getUser again to get user ID.
 
					m_storageBackend->setUser(res);
 
					registered = m_storageBackend->getUser(userkey, res);
 
				}
 
				else {
 
					registered = false;
 
				}
 
			}
 
		}
 

	
 

	
 
		// Unregistered users are not able to login
 
		if (!registered) {
 
			LOG4CXX_WARN(logger, "Unregistered user " << userkey << " tried to login");
 
			return;
 
		}
 

	
 
		// Create new user class and set storagebackend
 
		user = new User(presence->getFrom(), res, m_component, this);
 
		user->getRosterManager()->setStorageBackend(m_storageBackend);
 
		addUser(user);
 
	}
 

	
 
	// User can be handleDisconnected in addUser callback, so refresh the pointer
 
	user = getUser(userkey);
 
	if (!user) {
 
		m_userRegistry->onPasswordInvalid(presence->getFrom());
 
		return;
 
	}
 

	
 
	// Handle this presence
 
	user->handlePresence(presence);
 

	
 
	// Unavailable MUC presence should not trigger whole account disconnection, so block it here.
 
	bool isMUC = presence->getPayload<Swift::MUCPayload>() != NULL || *presence->getTo().getNode().c_str() == '#';
 
	if (isMUC)
 
		return;
 

	
 
	// Unavailable presence could remove this user, because he could be unavailable
 
	if (presence->getType() == Swift::Presence::Unavailable) {
 
		if (user) {
 
			Swift::Presence::ref highest = m_component->getPresenceOracle()->getHighestPriorityPresence(presence->getFrom().toBare());
 
			// There's no presence for this user, so disconnect
 
			if (!highest || (highest && highest->getType() == Swift::Presence::Unavailable)) {
 
				m_removeTimer->onTick.connect(boost::bind(&UserManager::handleRemoveTimeout, this, user->getJID().toBare().toString(), user, false));
 
				m_removeTimer->start();
 
			}
 
		}
 
	}
 
}
 

	
 
void UserManager::handleRemoveTimeout(const std::string jid, User *u, bool reconnect) {
 
	m_removeTimer->onTick.disconnect(boost::bind(&UserManager::handleRemoveTimeout, this, jid, u, reconnect));
 

	
 
	// Maybe this User instance has been deleted in mean time and we would remove new one,
 
	// so better check for it and ignore deletion if "u" does not exist anymore.
 
	User *user = getUser(jid);
 
	if (user != u) {
 
		return;
 
	}
 

	
 
	// Remove user
 
	if (user) {
 
		removeUser(user);
 
	}
 

	
 
	// Connect the user again when we're reconnecting.
 
	if (reconnect) {
 
		connectUser(jid);
 
	}
 
}
 

	
 
void UserManager::handleMessageReceived(Swift::Message::ref message) {
 
	if (message->getType() == Swift::Message::Error) {
 
		return;
 
	}
 

	
 
	if (message->getBody().empty()) {
 
		return;
 
	}
 

	
 
	User *user = getUser(message->getFrom().toBare().toString());
 
	if (!user ){
 
	if (!user){
 
		return;
 
	}
 

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

	
 
	// Do not count chatstate notification...
 
	boost::shared_ptr<Swift::ChatState> statePayload = message->getPayload<Swift::ChatState>();
 
	if (!statePayload) {
 
		messageToBackendSent();
 
	}
 
}
 

	
 
void UserManager::handleGeneralPresenceReceived(Swift::Presence::ref presence) {
 
	switch(presence->getType()) {
 
		case Swift::Presence::Subscribe:
 
		case Swift::Presence::Subscribed:
 
		case Swift::Presence::Unsubscribe:
 
		case Swift::Presence::Unsubscribed:
 
			handleSubscription(presence);
 
			break;
 
		case Swift::Presence::Available:
 
		case Swift::Presence::Unavailable:
 
			break;
 
		case Swift::Presence::Probe:
 
			handleProbePresence(presence);
 
			break;
 
		default:
 
			break;
 
	};
 
}
 

	
 
void UserManager::handleProbePresence(Swift::Presence::ref presence) {
 
	User *user = getUser(presence->getFrom().toBare().toString());
 

	
 
 	if (user) {
 
 		user->getRosterManager()->sendCurrentPresence(presence->getTo(), presence->getFrom());
 
 	}
 
 	else {
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setFrom(presence->getTo());
 
		response->setTo(presence->getFrom());
 
		response->setType(Swift::Presence::Unavailable);
 
		m_component->getStanzaChannel()->sendPresence(response);
 
	}
 
}
 

	
 
void UserManager::handleSubscription(Swift::Presence::ref presence) {
 
	// answer to subscibe for transport itself
 
	if (presence->getType() == Swift::Presence::Subscribe && presence->getTo().getNode().empty()) {
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setFrom(presence->getTo().toBare());
 
		response->setTo(presence->getFrom().toBare());
 
		response->setType(Swift::Presence::Subscribed);
 
		m_component->getStanzaChannel()->sendPresence(response);
 

	
 
// 		response = Swift::Presence::create();
 
// 		response->setFrom(presence->getTo());
 
// 		response->setTo(presence->getFrom());
 
// 		response->setType(Swift::Presence::Subscribe);
 
// 		m_component->getStanzaChannel()->sendPresence(response);
 
		return;
 
	}
 

	
 
	// Don't let RosterManager to handle presences for us
 
	if (presence->getTo().getNode().empty()) {
 
		return;
 
	}
 

	
 
	User *user = getUser(presence->getFrom().toBare().toString());
 

	
 
 	if (user) {
 
 		user->handleSubscription(presence);
 
 	}
 
 	else if (presence->getType() == Swift::Presence::Unsubscribe) {
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setFrom(presence->getTo());
 
		response->setTo(presence->getFrom());
 
		response->setType(Swift::Presence::Unsubscribed);
 
		m_component->getStanzaChannel()->sendPresence(response);
 
	}
 
// 	else {
 
// // 		Log(presence->getFrom().toString().getUTF8String(), "Subscribe presence received, but this user is not logged in");
 
// 	}
 
}
 

	
 
void UserManager::connectUser(const Swift::JID &user) {
 
	// Called by UserRegistry in server mode when user connects the server and wants
 
	// to connect legacy network
 
	if (m_users.find(user.toBare().toString()) != m_users.end()) {
 
		if (!m_component->inServerMode()) {
 
			return;
 
		}
 

	
 
		User *u = m_users[user.toBare().toString()];
 
		if (u->isConnected()) {
 
			// User is already logged in, so his password is OK, but this new user has different password => bad password.
 
			// We can't call m_userRegistry->onPasswordInvalid() here, because this fuction is called from Swift::Parser
 
			// and onPasswordInvalid destroys whole session together with parser itself, which leads to crash.
 
			if (m_userRegistry->getUserPassword(user.toBare().toString()) != u->getUserInfo().password) {
 
				m_userRegistry->removeLater(user);
 
				return;
 
			}
 
			if (CONFIG_BOOL(m_component->getConfig(), "service.more_resources")) {
 
				m_userRegistry->onPasswordValid(user);
 
			}
 
			else {
 
				// Send message to currently logged in session
 
				boost::shared_ptr<Swift::Message> msg(new Swift::Message());
 
				msg->setBody("You have signed on from another location.");
 
				msg->setTo(user);
 
				msg->setFrom(m_component->getJID());
 
				m_component->getStanzaChannel()->sendMessage(msg);
 

	
 
				// Switch the session = accept new one, disconnect old one.
 
				// Unavailable presence from old session has to be ignored, otherwise it would disconnect the user from legacy network.
 
				m_userRegistry->onPasswordValid(user);
 
				m_component->onUserPresenceReceived.disconnect(bind(&UserManager::handlePresence, this, _1));
 
				dynamic_cast<Swift::ServerStanzaChannel *>(m_component->getStanzaChannel())->finishSession(user, boost::shared_ptr<Swift::Element>(new Swift::StreamError()), true);
 
				m_component->onUserPresenceReceived.connect(bind(&UserManager::handlePresence, this, _1));
 
			}
 
		}
 
		else {
 
			// User is created, but not connected => he's loggin in or he just logged out, but hasn't been deleted yet.
 
			// Stop deletion process if there's any
 
			m_removeTimer->onTick.disconnect(boost::bind(&UserManager::handleRemoveTimeout, this, user.toBare().toString(), m_users[user.toBare().toString()], false));
 

	
 
			// Delete old User instance but create new one immediatelly
 
			m_removeTimer->onTick.connect(boost::bind(&UserManager::handleRemoveTimeout, this, user.toBare().toString(), m_users[user.toBare().toString()], true));
 
			m_removeTimer->start();
 
		}
 
	}
 
	else {
 
		// simulate initial available presence to start connecting this user.
 
		Swift::Presence::ref response = Swift::Presence::create();
 
		response->setTo(m_component->getJID());
 
		response->setFrom(user);
 
		response->setType(Swift::Presence::Available);
 
		dynamic_cast<Swift::ServerStanzaChannel *>(m_component->getStanzaChannel())->onPresenceReceived(response);
 
	}
 
}
 

	
 

	
 
void UserManager::disconnectUser(const Swift::JID &user) {
 
	Swift::Presence::ref response = Swift::Presence::create();
 
	response->setTo(m_component->getJID());
 
	response->setFrom(user);
 
	response->setType(Swift::Presence::Unavailable);
 
	dynamic_cast<Swift::ServerStanzaChannel *>(m_component->getStanzaChannel())->onPresenceReceived(response);
 
}
 

	
 
}
0 comments (0 inline, 0 general)