Changeset - 20d8dd690e21
CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
project(libtransport)
 
 
message(STATUS "Variables to override default places where to find libraries:")
 
message(STATUS "|- cppunit : -DCPPUNIT_INCLUDE_DIR,  -DCPPUNIT_LIBRARY")
 
message(STATUS "|- swiften : -DSWIFTEN_INCLUDE_DIR,  -DSWIFTEN_LIBRARY")
 
message(STATUS "  |- zlib  :                         -DZLIB_LIBRARY")
 
message(STATUS "  |- expat :                         -DEXPAT_LIBRARY")
 
message(STATUS "  |-libidn :                         -DLIBIDN_LIBRARY")
 
message(STATUS "  |-libxml :                         -DLIBXML_LIBRARY")
 
message(STATUS "|- boost   : -DBOOST_INCLUDEDIR,     -DBOOST_LIBRARYDIR")
 
message(STATUS "|- protobuf: -DPROTOBUF_INCLUDE_DIR, -DPROTOBUF_LIBRARY")
 
message(STATUS "           : -DPROTOBUF_PROTOC_EXECUTABLE")
 
message(STATUS "|- log4cxx : -DLOG4CXX_INCLUDE_DIR,  -DLOG4CXX_LIBRARY")
 
message(STATUS "|- purple  : -DPURPLE_INCLUDE_DIR,   -DPURPLE_LIBRARY")
 
message(STATUS "           : -DPURPLE_NOT_RUNTIME - enables compilation with libpurple.lib")
 
 
option(ENABLE_SQLITE3 "Build with SQLite3 support" ON)
 
option(ENABLE_MYSQL "Build with MySQL support" ON)
 
option(ENABLE_PQXX "Build with Postgres supoort" ON)
 
 
option(ENABLE_FROTZ "Build Frotz plugin" ON)
 
option(ENABLE_IRC "Build IRC plugin" ON)
 
option(ENABLE_PURPLE "Build Libpurple plugin" ON)
 
option(ENABLE_SMSTOOLS3 "Build SMSTools3 plugin" ON)
 
option(ENABLE_SKYPE "Build Skype plugin" ON)
 
option(ENABLE_SWIFTEN "Build Swiften plugin" ON)
 
option(ENABLE_TWITTER "Build Twitter plugin" ON)
 
option(ENABLE_YAHOO2 "Build Libyahoo2 plugin" ON)
 
 
option(ENABLE_DOCS "Build Docs" ON)
 
# option(ENABLE_LOG "Build with logging using Log4cxx" ON)
 
option(ENABLE_TESTS "Build Tests using CppUnit" OFF)
 
 
MACRO(LIST_CONTAINS var value)
 
	SET(${var})
 
	FOREACH (value2 ${ARGN})
 
		IF (${value} STREQUAL ${value2})
 
		SET(${var} TRUE)
 
		ENDIF (${value} STREQUAL ${value2})
 
	ENDFOREACH (value2)
 
ENDMACRO(LIST_CONTAINS)
 
 
if(NOT LIB_INSTALL_DIR)
 
	set(LIB_INSTALL_DIR "lib")
 
endif()
 
 
set(CMAKE_MODULE_PATH "cmake_modules")
 
 
###### Prerequisites ######
 
 
 
# FIND SWIFTEN
 
 
set(Swiften_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(Swiften)
 
 
if(NOT SWIFTEN_FOUND)
 
	if (ZLIB_LIBRARY)
 
		set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} ${ZLIB_LIBRARY})
 
	endif()
 
	if (EXPAT_LIBRARY)
 
		set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} ${EXPAT_LIBRARY})
 
	endif()
 
	if (LIBIDN_LIBRARY)
 
		set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} ${LIBIDN_LIBRARY})
 
	endif()
 
	if (LIBXML_LIBRARY)
 
		set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} ${LIBXML_LIBRARY})
 
	endif()
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Dnsapi")
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Crypt32")
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Secur32")
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Iphlpapi")
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Winscard")
 
	message(STATUS "Using swiften: ${SWIFTEN_INCLUDE_DIR} ${SWIFTEN_LIBRARY}")
 
endif()
 
 
# FIND BOOST
 
if (WIN32)
 
	set(Boost_USE_STATIC_LIBS      ON)
 
	set(Boost_USE_MULTITHREADED      ON)
 
	set(Boost_USE_STATIC_RUNTIME    OFF)
 
	find_package(Boost COMPONENTS program_options date_time system filesystem regex thread signals REQUIRED)
 
else(WIN32)
 
	LIST_CONTAINS(contains -lboost_program_options ${SWIFTEN_LIBRARY})
 
	if(contains)
 
		message(STATUS "Using non-multithreaded boost")
 
		set(Boost_USE_MULTITHREADED 0)
 
	endif(contains)
 
	set(Boost_FIND_QUIETLY ON)
 
	find_package(Boost COMPONENTS program_options date_time system filesystem regex thread-mt signals)
 
	if (NOT Boost_FOUND)
 
		set(Boost_FIND_QUIETLY OFF)
 
		find_package(Boost COMPONENTS program_options date_time system filesystem regex thread signals REQUIRED)
 
	endif()
 
endif(WIN32)
 
 
message( STATUS "Found Boost: ${Boost_VERSION}, ${Boost_LIBRARIES}, ${Boost_INCLUDE_DIR}")
 
 
if (${Boost_VERSION} GREATER 104999)
 
	message( STATUS "Using BOOST_FILESYSTEM_VERSION=3")
 
	add_definitions(-DBOOST_FILESYSTEM_VERSION=3)
 
endif()
 
 
# FIND POPT
 
if (NOT WIN32)
 
	set(popt_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(popt REQUIRED)
 
endif()
 
 
###### Database ######
 
 
# FIND SQLITE3
 
if (ENABLE_SQLITE3)
 
	if (MSVC)
 
		set(SQLITE3_FOUND 1)
 
		ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/msvc-deps)
 
	else()
 
		if (WIN32)
 
			ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/msvc-deps/sqlite3)
 
		else()
 
			set(sqlite3_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
			find_package(sqlite3)
 
		endif()
 
	endif()
 
endif()
 
 
# FIND MYSQL
 
if(ENABLE_MYSQL)
 
	set(mysql_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(mysql)
 
endif()
 
 
# FIND PQXX
 
if(ENABLE_PQXX)
 
	set(pqxx_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(pqxx)
 
endif()
 
 
###### Plugins ######
 
 
# FIND LIBPURPLE
 
if(ENABLE_PURPLE)
 
	set(purple_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(purple)
 
 
	if (WIN32)
 
		if (PURPLE_NOT_RUNTIME)
 
			add_definitions(-DPURPLE_RUNTIME=0)
 
		else(PURPLE_NOT_RUNTIME)
 
			add_definitions(-DPURPLE_RUNTIME=1)
 
		endif(PURPLE_NOT_RUNTIME)
 
	else()
 
		add_definitions(-DPURPLE_RUNTIME=0)
 
	endif()
 
 
	# FIND LIBEVENT
 
	set(event_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(event)
 
endif()
 
 
# FIND GLIB
 
if(ENABLE_SKYPE OR ENABLE_PURPLE)
 
#	if (GLIB2_INCLUDE_DIR AND GLIB2_LIBRARIES)
 
#		set(GLIB2_FOUND TRUE)
 
#	else()
 
		set(glib_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
		find_package(glib)
 
#	endif()
 
endif()
 
 
# FIND LIBXML2
 
# set(libxml2_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
# find_package(libxml2)
 
 
# FIND PROTOBUF
 
set(Protobuf_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(Protobuf REQUIRED)
 
 
if (NOT PROTOBUF_FOUND AND PROTOBUF_INCLUDE_DIR AND PROTOBUF_LIBRARY)
 
	set(PROTOBUF_FOUND 1)
 
	set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
 
	if (PROTOBUF_PROTOC_EXECUTABLE)
 
	else()
 
		set(PROTOBUF_PROTOC_EXECUTABLE protoc)
 
	endif()
 
	message(STATUS "Using protobuf: ${PROTOBUF_INCLUDE_DIRS} ${PROTOBUF_LIBRARY}")
 
endif()
 
 
if (WIN32)
 
	add_definitions(-DSWIFTEN_STATIC=1)
 
	ADD_DEFINITIONS(-D_UNICODE)
 
	ADD_DEFINITIONS(-DUNICODE)
 
endif()
 
 
 
if (CMAKE_COMPILER_IS_GNUCXX)
 
set(openssl_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(openssl)
 
endif()
 
 
if(ENABLE_IRC)
 
	set(Communi_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(Communi)
 
 
	INCLUDE(FindQt4)
 
	FIND_PACKAGE(Qt4 COMPONENTS QtCore QtNetwork)
 
	# ADD_DEFINITIONS(${SWIFTEN_CFLAGS})
 
	ADD_DEFINITIONS(-DSUPPORT_LEGACY_CAPS)
 
	# ADD_DEFINITIONS(-DBOOST_FILESYSTEM_VERSION=2)
 
endif()
 
 
set(event_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(event)
 
 
if (NOT WIN32 AND ENABLE_SKYPE)
 
set(dbus_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
find_package(dbus)
 
endif()
 
 
# if(ENABLE_YAHOO2)
 
# 	set(yahoo2_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
# 	find_package(yahoo2)
 
# endif()
 
 
####### Miscallanous ######
 
 
if(ENABLE_DOCS)
 
	find_package(Doxygen)
 
endif()
 
 
# if(ENABLE_LOG)
 
	if(LOG4CXX_INCLUDE_DIR AND LOG4CXX_LIBRARY)
 
		set(LOG4CXX_LIBRARIES ${LOG4CXX_LIBRARY})
 
		set(LOG4CXX_FOUND 1)
 
		message(STATUS "Using log4cxx: ${CPPUNIT_INCLUDE_DIR} ${LOG4CXX_INCLUDE_DIR}")
 
	else()
 
		set(log4cxx_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
		find_package(log4cxx)
 
	endif()
 
# endif()
 
 
# FIND CPPUNIT
 
if(ENABLE_TESTS)
 
	set(cppunit_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(cppunit)
 
 
	if(NOT CPPUNIT_FOUND AND CPPUNIT_INCLUDE_DIR AND CPPUNIT_LIBRARY)
 
		set(CCPUNIT_LIBRARIES ${CPPUNIT_LIBRARY})
 
		set(CPPUNIT_FOUND 1)
 
		message(STATUS "Using cppunit: ${CPPUNIT_INCLUDE_DIR} ${CPPUNIT_LIBRARIES}")
 
	endif()
 
endif()
 
 
if (APPLE)
 
        FIND_LIBRARY(IOKIT_FRAMEWORK IOKit)
 
        FIND_LIBRARY(SECURITY_FRAMEWORK Security)
 
        FIND_LIBRARY(APPKIT_FRAMEWORK AppKit)
 
        FIND_LIBRARY(SYSTEMCONFIGURATION_FRAMEWORK SystemConfiguration)
 
        FIND_LIBRARY(SECURITYINTERFACE_FRAMEWORK SecurityInterface)
 
        MARK_AS_ADVANCED(IOKIT_FRAMEWORK APPKIT_FRAMEWORK SYSTEMCONFIGURATION_FRAMEWORK SECURITY_FRAMEWORK SECURITYINTERFACE_FRAMEWORK)
 
        SET (APPLE_FRAMEWORKS ${IOKIT_FRAMEWORK} ${APPKIT_FRAMEWORK} ${SYSTEMCONFIGURATION_FRAMEWORK} ${SECURITY_FRAMEWORK} ${SECURITYINTERFACE_FRAMEWORK})
 
endif()
 
 
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("${CMAKE_SOURCE_DIR}/msvc-deps/sqlite3")
 
		message("SQLite3           : bundled")
 
	else()
 
		set(SQLITE3_LIBRARIES "")
 
		if(ENABLE_SQLITE3)
 
			message("SQLite3           : no (install sqlite3)")
 
		else(ENABLE_SQLITE3)
 
			message("SQLite3           : no (user disabled)")
 
		endif()
 
	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 "")
 
	if(ENABLE_MYSQL)
 
		message("MySQL             : no (install mysql-devel)")
 
	else(ENABLE_MYSQL)
 
		message("MySQL             : no (user disabled)")
 
	endif()
 
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 "")
 
	if(ENABLE_PQXX)
 
		message("PostgreSQL        : no (install libpqxx-devel)")
 
	else(ENABLE_PQXX)
 
		message("PostgreSQL        : no (user disabled)")
 
	endif()
 
endif (PQXX_FOUND)
 
 
if (PROTOBUF_FOUND)
 
	ADD_DEFINITIONS(-DWITH_PROTOBUF)
 
	include_directories(${PROTOBUF_INCLUDE_DIRS})
 
	message("Network plugins   : yes")
 
 
	if(PURPLE_FOUND)
 
		message("Libpurple plugin  : yes")
 
		include_directories(${PURPLE_INCLUDE_DIR})
 
		include_directories(${GLIB2_INCLUDE_DIR})
 
	else()
 
		if(ENABLE_PURPLE)
 
			message("Libpurple plugin  : no (install libpurple)")
 
		else(ENABLE_PURPLE)
 
			message("Libpurple plugin  : no (user disabled)")
 
		endif()
 
	endif()
 
 
	if (HAVE_EVENT)
 
		ADD_DEFINITIONS(-DWITH_LIBEVENT)
 
		include_directories(${EVENT_INCLUDE_DIRS})
 
		message("  libev eventloop : yes")
 
	else()
 
		if(ENABLE_PURPLE)
 
			message("  libev eventloop : no (install libev-devel)")
 
		endif()
 
	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()
 
		if(ENABLE_IRC)
 
			message("IRC plugin        : no (install libCommuni and libprotobuf-dev)")
 
		else(ENABLE_IRC)
 
			message("IRC plugin        : no (user disabled)")
 
		endif()
 
	endif()
 
	if(ENABLE_TWITTER)
 
		message("Twitter plugin    : yes")
 
	else(ENABLE_TWITTER)
 
		message("Twitter plugin    : no (user disabled)")
 
	endif()
 
	if (NOT WIN32)
 
		if(ENABLE_FROTZ)
 
			message("Frotz plugin      : yes")
 
		else()
 
			message("Frotz plugin      : no (user disabled)")
 
		endif()
 
		if(ENABLE_SMSTOOLS3)
 
			message("SMSTools3 plugin  : yes")
 
		else()
 
			message("SMSTools3 plugin  : no (user disabled)")
 
		endif()
 
		if(${LIBDBUSGLIB_FOUND})
 
			message("Skype plugin      : yes")
 
			include_directories(${LIBDBUSGLIB_INCLUDE_DIRS})
 
		else()
 
			if(ENABLE_SKYPE)
 
				message("Skype plugin      : no (install dbus-glib-devel)")
 
			else(ENABLE_SKYPE)
 
				message("Skype plugin      : no (user disabled)")
 
			endif()
 
		endif()
 
	else()
 
		message("Frotz plugin      : no (does not run on Win32)")
 
		message("SMSTools3 plugin  : no (does not run on Win32)")
 
		message("Skype plugin      : no (does not run on Win32)")
 
	endif()
 
 
#  	if(YAHOO2_FOUND)
 
# 		message("Libyahoo2 plugin  : yes")
 
#  		include_directories(${YAHOO2_INCLUDE_DIR})
 
# 	else()
 
		if(ENABLE_YAHOO2)
 
			set(YAHOO2_FOUND 1)
 
			message("Libyahoo2 plugin  : yes")
 
		else(ENABLE_YAHOO2)
 
			message("Libyahoo2 plugin  : no (user disabled)")
 
		endif()
 
#  	endif()
 
 
	if(ENABLE_SWIFTEN)
 
		message("Swiften plugin    : yes")
 
	else()
 
		message("Swiften plugin    : no (user disabled)")
 
	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)")
 
	message("Swiften plugin    : no (install libprotobuf-dev)")
 
    message("Twitter 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 "")
 
	if (WIN32)
 
		message("Log4cxx           : no (install log4cxx-devel)")
 
	else()
 
		message(FATAL_ERROR "Log4cxx           : no (install log4cxx-devel)")
 
	endif()
 
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(-O0)
 
		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()
 
	if(ENABLE_TESTS)
 
		message("tests             : no (install CPPUnit)")
 
	else(ENABLE_TESTS)
 
		message("tests             : no (user disabled)")
 
	endif()
 
endif()
 
 
if(DOXYGEN_FOUND)
 
	message("Docs              : yes")
 
	ADD_SUBDIRECTORY(docs)
 
else(DOXYGEN_FOUND)
 
	if(ENABLE_DOCS)
 
		message("Docs              : no (install doxygen)")
 
	else(ENABLE_DOCS)
 
		message("Docs              : no (user disabled)")
 
	endif()
 
endif(DOXYGEN_FOUND)
 
 
message("----------------------")
 
 
if(NOT SQLITE3_FOUND AND NOT MYSQL_FOUND AND NOT PQXX_FOUND)
 
	if(ENABLE_SQLITE3 OR ENABLE_MYSQL OR ENABLE_PQXX)
 
		message("Could not find any database - Please install at least one of sqlite3-devel, mysql-devel or pqxx-devel if you want to use transport mode.")
 
	else(ENABLE_SQLITE3 OR ENABLE_MYSQL OR ENABLE_PQXX)
 
		message("Please enable at least one of SQLITE3, MYSQL, PQXX databases to use transport mode.")
 
	endif()
 
endif()
 
 
cmake_minimum_required(VERSION 2.6)
 
project(libtransport)
 

	
 
message(STATUS "Variables to override default places where to find libraries:")
 
message(STATUS "|- cppunit : -DCPPUNIT_INCLUDE_DIR,  -DCPPUNIT_LIBRARY")
 
message(STATUS "|- swiften : -DSWIFTEN_INCLUDE_DIR,  -DSWIFTEN_LIBRARY")
 
message(STATUS "  |- zlib  :                         -DZLIB_LIBRARY")
 
message(STATUS "  |- expat :                         -DEXPAT_LIBRARY")
 
message(STATUS "  |-libidn :                         -DLIBIDN_LIBRARY")
 
message(STATUS "  |-libxml :                         -DLIBXML_LIBRARY")
 
message(STATUS "|- boost   : -DBOOST_INCLUDEDIR,     -DBOOST_LIBRARYDIR")
 
message(STATUS "|- protobuf: -DPROTOBUF_INCLUDE_DIR, -DPROTOBUF_LIBRARY")
 
message(STATUS "           : -DPROTOBUF_PROTOC_EXECUTABLE")
 
message(STATUS "|- log4cxx : -DLOG4CXX_INCLUDE_DIR,  -DLOG4CXX_LIBRARY")
 
message(STATUS "|- purple  : -DPURPLE_INCLUDE_DIR,   -DPURPLE_LIBRARY")
 
message(STATUS "           : -DPURPLE_NOT_RUNTIME - enables compilation with libpurple.lib")
 

	
 
option(ENABLE_SQLITE3 "Build with SQLite3 support" ON)
 
option(ENABLE_MYSQL "Build with MySQL support" ON)
 
option(ENABLE_PQXX "Build with Postgres supoort" ON)
 

	
 
option(ENABLE_FROTZ "Build Frotz plugin" ON)
 
option(ENABLE_IRC "Build IRC plugin" ON)
 
option(ENABLE_PURPLE "Build Libpurple plugin" ON)
 
option(ENABLE_SMSTOOLS3 "Build SMSTools3 plugin" ON)
 
option(ENABLE_SKYPE "Build Skype plugin" ON)
 
option(ENABLE_SWIFTEN "Build Swiften plugin" ON)
 
option(ENABLE_TWITTER "Build Twitter plugin" ON)
 
option(ENABLE_YAHOO2 "Build Libyahoo2 plugin" ON)
 

	
 
option(ENABLE_DOCS "Build Docs" ON)
 
# option(ENABLE_LOG "Build with logging using Log4cxx" ON)
 
option(ENABLE_TESTS "Build Tests using CppUnit" OFF)
 

	
 
MACRO(LIST_CONTAINS var value)
 
	SET(${var})
 
	FOREACH (value2 ${ARGN})
 
		IF (${value} STREQUAL ${value2})
 
		SET(${var} TRUE)
 
		ENDIF (${value} STREQUAL ${value2})
 
	ENDFOREACH (value2)
 
ENDMACRO(LIST_CONTAINS)
 

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

	
 
set(CMAKE_MODULE_PATH "cmake_modules")
 

	
 
###### Prerequisites ######
 

	
 

	
 
# FIND SWIFTEN
 

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

	
 
if(NOT SWIFTEN_FOUND)
 
	if (ZLIB_LIBRARY)
 
		set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} ${ZLIB_LIBRARY})
 
	endif()
 
	if (EXPAT_LIBRARY)
 
		set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} ${EXPAT_LIBRARY})
 
	endif()
 
	if (LIBIDN_LIBRARY)
 
		set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} ${LIBIDN_LIBRARY})
 
	endif()
 
	if (LIBXML_LIBRARY)
 
		set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} ${LIBXML_LIBRARY})
 
	endif()
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Dnsapi")
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Crypt32")
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Secur32")
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Iphlpapi")
 
	set(SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY} "Winscard")
 
	message(STATUS "Using swiften: ${SWIFTEN_INCLUDE_DIR} ${SWIFTEN_LIBRARY}")
 
endif()
 

	
 
# FIND BOOST
 
if (WIN32)
 
	set(Boost_USE_STATIC_LIBS      ON)
 
	set(Boost_USE_MULTITHREADED      ON)
 
	set(Boost_USE_STATIC_RUNTIME    OFF)
 
	find_package(Boost COMPONENTS program_options date_time system filesystem regex thread signals REQUIRED)
 
else(WIN32)
 
	LIST_CONTAINS(contains -lboost_program_options ${SWIFTEN_LIBRARY})
 
	if(contains)
 
		message(STATUS "Using non-multithreaded boost")
 
		set(Boost_USE_MULTITHREADED 0)
 
	endif(contains)
 
	set(Boost_FIND_QUIETLY ON)
 
	find_package(Boost COMPONENTS program_options date_time system filesystem regex thread-mt signals)
 
	if (NOT Boost_FOUND)
 
		set(Boost_FIND_QUIETLY OFF)
 
		find_package(Boost COMPONENTS program_options date_time system filesystem regex thread signals REQUIRED)
 
	endif()
 
endif(WIN32)
 

	
 
message( STATUS "Found Boost: ${Boost_VERSION}, ${Boost_LIBRARIES}, ${Boost_INCLUDE_DIR}")
 

	
 
if (${Boost_VERSION} GREATER 104999)
 
	message( STATUS "Using BOOST_FILESYSTEM_VERSION=3")
 
	add_definitions(-DBOOST_FILESYSTEM_VERSION=3)
 
endif()
 

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

	
 
###### Database ######
 

	
 
# FIND SQLITE3
 
if (ENABLE_SQLITE3)
 
	if (MSVC)
 
		set(SQLITE3_FOUND 1)
 
		ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/msvc-deps)
 
	else()
 
		if (WIN32)
 
			ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/msvc-deps/sqlite3)
 
		else()
 
			set(sqlite3_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
			find_package(sqlite3)
 
		endif()
 
	endif()
 
endif()
 

	
 
# FIND MYSQL
 
if(ENABLE_MYSQL)
 
	set(mysql_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(mysql)
 
endif()
 

	
 
# FIND PQXX
 
if(ENABLE_PQXX)
 
	set(pqxx_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(pqxx)
 
endif()
 

	
 
###### Plugins ######
 

	
 
# FIND LIBPURPLE
 
if(ENABLE_PURPLE)
 
	set(purple_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(purple)
 

	
 
	if (WIN32)
 
		if (PURPLE_NOT_RUNTIME)
 
			add_definitions(-DPURPLE_RUNTIME=0)
 
		else(PURPLE_NOT_RUNTIME)
 
			add_definitions(-DPURPLE_RUNTIME=1)
 
		endif(PURPLE_NOT_RUNTIME)
 
	else()
 
		add_definitions(-DPURPLE_RUNTIME=0)
 
	endif()
 

	
 
	# FIND LIBEVENT
 
	set(event_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(event)
 
endif()
 

	
 
# FIND GLIB
 
if(ENABLE_SKYPE OR ENABLE_PURPLE)
 
#	if (GLIB2_INCLUDE_DIR AND GLIB2_LIBRARIES)
 
#		set(GLIB2_FOUND TRUE)
 
#	else()
 
		set(glib_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
		find_package(glib)
 
#	endif()
 
endif()
 

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

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

	
 
if (NOT PROTOBUF_FOUND AND PROTOBUF_INCLUDE_DIR AND PROTOBUF_LIBRARY)
 
	set(PROTOBUF_FOUND 1)
 
	set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
 
	if (PROTOBUF_PROTOC_EXECUTABLE)
 
	else()
 
		set(PROTOBUF_PROTOC_EXECUTABLE protoc)
 
	endif()
 
	message(STATUS "Using protobuf: ${PROTOBUF_INCLUDE_DIRS} ${PROTOBUF_LIBRARY}")
 
endif()
 

	
 
if (WIN32)
 
	add_definitions(-DSWIFTEN_STATIC=1)
 
	ADD_DEFINITIONS(-D_UNICODE)
 
	ADD_DEFINITIONS(-DUNICODE)
 
endif()
 

	
 

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

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

	
 
	INCLUDE(FindQt4)
 
	FIND_PACKAGE(Qt4 COMPONENTS QtCore QtNetwork)
 
	# ADD_DEFINITIONS(${SWIFTEN_CFLAGS})
 
	ADD_DEFINITIONS(-DSUPPORT_LEGACY_CAPS)
 
	# ADD_DEFINITIONS(-DBOOST_FILESYSTEM_VERSION=2)
 
endif()
 

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

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

	
 
# if(ENABLE_YAHOO2)
 
# 	set(yahoo2_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
# 	find_package(yahoo2)
 
# endif()
 

	
 
####### Miscallanous ######
 

	
 
if(ENABLE_DOCS)
 
	find_package(Doxygen)
 
endif()
 

	
 
# if(ENABLE_LOG)
 
	if(LOG4CXX_INCLUDE_DIR AND LOG4CXX_LIBRARY)
 
		set(LOG4CXX_LIBRARIES ${LOG4CXX_LIBRARY})
 
		set(LOG4CXX_FOUND 1)
 
		message(STATUS "Using log4cxx: ${CPPUNIT_INCLUDE_DIR} ${LOG4CXX_INCLUDE_DIR}")
 
	else()
 
		set(log4cxx_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
		find_package(log4cxx)
 
	endif()
 
# endif()
 

	
 
# FIND CPPUNIT
 
if(ENABLE_TESTS)
 
	set(cppunit_DIR "${CMAKE_SOURCE_DIR}/cmake_modules")
 
	find_package(cppunit)
 

	
 
	if(NOT CPPUNIT_FOUND AND CPPUNIT_INCLUDE_DIR AND CPPUNIT_LIBRARY)
 
		set(CCPUNIT_LIBRARIES ${CPPUNIT_LIBRARY})
 
		set(CPPUNIT_FOUND 1)
 
		message(STATUS "Using cppunit: ${CPPUNIT_INCLUDE_DIR} ${CPPUNIT_LIBRARIES}")
 
	endif()
 
endif()
 

	
 
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("${CMAKE_SOURCE_DIR}/msvc-deps/sqlite3")
 
		message("SQLite3           : bundled")
 
	else()
 
		set(SQLITE3_LIBRARIES "")
 
		if(ENABLE_SQLITE3)
 
			message("SQLite3           : no (install sqlite3)")
 
		else(ENABLE_SQLITE3)
 
			message("SQLite3           : no (user disabled)")
 
		endif()
 
	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 "")
 
	if(ENABLE_MYSQL)
 
		message("MySQL             : no (install mysql-devel)")
 
	else(ENABLE_MYSQL)
 
		message("MySQL             : no (user disabled)")
 
	endif()
 
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 "")
 
	if(ENABLE_PQXX)
 
		message("PostgreSQL        : no (install libpqxx-devel)")
 
	else(ENABLE_PQXX)
 
		message("PostgreSQL        : no (user disabled)")
 
	endif()
 
endif (PQXX_FOUND)
 

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

	
 
	if(PURPLE_FOUND)
 
		message("Libpurple plugin  : yes")
 
		include_directories(${PURPLE_INCLUDE_DIR})
 
		include_directories(${GLIB2_INCLUDE_DIR})
 
	else()
 
		if(ENABLE_PURPLE)
 
			message("Libpurple plugin  : no (install libpurple)")
 
		else(ENABLE_PURPLE)
 
			message("Libpurple plugin  : no (user disabled)")
 
		endif()
 
	endif()
 

	
 
	if (HAVE_EVENT)
 
		ADD_DEFINITIONS(-DWITH_LIBEVENT)
 
		include_directories(${EVENT_INCLUDE_DIRS})
 
		message("  libev eventloop : yes")
 
	else()
 
		if(ENABLE_PURPLE)
 
			message("  libev eventloop : no (install libev-devel)")
 
		endif()
 
	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()
 
		if(ENABLE_IRC)
 
			message("IRC plugin        : no (install libCommuni and libprotobuf-dev)")
 
		else(ENABLE_IRC)
 
			message("IRC plugin        : no (user disabled)")
 
		endif()
 
	endif()
 
	if(ENABLE_TWITTER)
 
		message("Twitter plugin    : yes")
 
	else(ENABLE_TWITTER)
 
		message("Twitter plugin    : no (user disabled)")
 
	endif()
 
	if (NOT WIN32)
 
		if(ENABLE_FROTZ)
 
			message("Frotz plugin      : yes")
 
		else()
 
			message("Frotz plugin      : no (user disabled)")
 
		endif()
 
		if(ENABLE_SMSTOOLS3)
 
			message("SMSTools3 plugin  : yes")
 
		else()
 
			message("SMSTools3 plugin  : no (user disabled)")
 
		endif()
 
		if(${LIBDBUSGLIB_FOUND})
 
			message("Skype plugin      : yes")
 
			include_directories(${LIBDBUSGLIB_INCLUDE_DIRS})
 
		else()
 
			if(ENABLE_SKYPE)
 
				message("Skype plugin      : no (install dbus-glib-devel)")
 
			else(ENABLE_SKYPE)
 
				message("Skype plugin      : no (user disabled)")
 
			endif()
 
		endif()
 
	else()
 
		message("Frotz plugin      : no (does not run on Win32)")
 
		message("SMSTools3 plugin  : no (does not run on Win32)")
 
		message("Skype plugin      : no (does not run on Win32)")
 
	endif()
 

	
 
#  	if(YAHOO2_FOUND)
 
# 		message("Libyahoo2 plugin  : yes")
 
#  		include_directories(${YAHOO2_INCLUDE_DIR})
 
# 	else()
 
		if(ENABLE_YAHOO2)
 
			set(YAHOO2_FOUND 1)
 
			message("Libyahoo2 plugin  : yes")
 
		else(ENABLE_YAHOO2)
 
			message("Libyahoo2 plugin  : no (user disabled)")
 
		endif()
 
#  	endif()
 

	
 
	if(ENABLE_SWIFTEN)
 
		message("Swiften plugin    : yes")
 
	else()
 
		message("Swiften plugin    : no (user disabled)")
 
	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)")
 
	message("Swiften plugin    : no (install libprotobuf-dev)")
 
    message("Twitter 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 "")
 
	if (WIN32)
 
		message("Log4cxx           : no (install log4cxx-devel)")
 
	else()
 
		message(FATAL_ERROR "Log4cxx           : no (install log4cxx-devel)")
 
	endif()
 
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(-O0)
 
		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()
 
	if(ENABLE_TESTS)
 
		message("tests             : no (install CPPUnit)")
 
	else(ENABLE_TESTS)
 
		message("tests             : no (user disabled)")
 
	endif()
 
endif()
 

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

	
 
message("----------------------")
 

	
 
if(NOT SQLITE3_FOUND AND NOT MYSQL_FOUND AND NOT PQXX_FOUND)
 
	if(ENABLE_SQLITE3 OR ENABLE_MYSQL OR ENABLE_PQXX)
 
		message("Could not find any database - Please install at least one of sqlite3-devel, mysql-devel or pqxx-devel if you want to use transport mode.")
 
	else(ENABLE_SQLITE3 OR ENABLE_MYSQL OR ENABLE_PQXX)
 
		message("Please enable at least one of SQLITE3, MYSQL, PQXX databases to use transport mode.")
 
	endif()
 
endif()
 

	
README.win32
Show inline comments
 
Prerequisites
 
=============
 
 
1. Microsoft Visual C++ 2010 Express or higher edition (http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express)
 
2. Git for Windows (http://code.google.com/p/msysgit/downloads/list)
 
3. CMake 2.8 or newer (http://www.cmake.org/cmake/resources/software.html)
 
4. Python 2.x for Swiften build scripts (scons) (http://www.python.org)
 
 
Libraries
 
=========
 
3. Swiften library (http://swift.im/git/swift)
 
4. Boost 1.48 or newer (http://sourceforge.net/projects/boost/files/boost/1.49.0/)
 
5. Google ProtoBuf library (http://code.google.com/p/protobuf/downloads/list)
 
 
 
Environment
 
===========
 
 
To create spectrum build environment do:
 
 
0. Create directory where we'll install all dependencies, e.g. C:\env-msvc-x64.
 
Create C:\env-msvc-x64\bin and add it to %PATH%.
 
Assuming you have git, python and cmake in %PATH%, 
 
launch "Visual Studio 2010 command prompt" or
 
"Visual Studio 2010(x64) command prompt", depends on your target (Windows x86 or Windows x86_64).
 
1. unpack and build boost libraries:
 
 
	bootstrap.bat
 
	b2.exe --without-mpi --without-python
 
	b2.exe --without-mpi --without-python install --prefix=C:\env-msvc-x64 release
 
 
2. clone swift repository and build it. Don't forget to point it to our env directory:
 
 
	git clone git://swift.im/swift
 
	cd swift
 
	echo boost_includedir="c:/env-msvc-x64/include/boost-1_49" > config.py
 
	echo boost_libdir="c:/env-msvc-x64/lib" >> config.py 
 
	scons.bat debug=no SWIFTEN_INSTALLDIR=C:\env-msvc-x64 force_configure=1
 
	scons.bat debug=no SWIFTEN_INSTALLDIR=C:\env-msvc-x64 C:\env-msvc-x64
 
 
3. unpack and compile protobuf as described in its documentation. 
 
 
Run extract_includes.bat in vsprojects/ directory and move resulting vsprojects/include/google/ directory to our C:\env-msvc-x64\include
 
 
Move protoc.exe to C:\env-msvc-x64\bin\ and libprotobuf.lib to C:\env-msvc-x64\lib
 
 
4. Install gtkmm
 
 
Download installer from https://live.gnome.org/gtkmm/MSWindows and install gtkmm into C:\env-msvc-x64\
 
 
5. Install libpurple headers
 
 
Download http://www.pidgin.im/download/source/ , extract it and copy libpurple directory in C:\env-msvc-x64\include
 
 
6. You're ready! :) Clone libtransport into C:\env-msvc-x64\libtransport (You *must* clone it into this directory, because libtransport will try to find the dependencies in ../lib and ../include)
 
 
Compile it as:
 
 
	set CMAKE_INCLUDE_PATH=C:\env-msvc-x64\include
 
	cmake . -G "NMake Makefiles" -DBOOST_INCLUDEDIR=../include/boost-1_49 -DBOOST_LIBRARYDIR=../lib -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=C:\env-msvc-x64 -DGIT_EXECUTABLE="c:\Program Files (x86)\git\bin\git.exe"
 
	nmake
 
Prerequisites
 
=============
 

	
 
1. Microsoft Visual C++ 2010 Express or higher edition (http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express)
 
2. Git for Windows (http://code.google.com/p/msysgit/downloads/list)
 
3. CMake 2.8 or newer (http://www.cmake.org/cmake/resources/software.html)
 
4. Python 2.x for Swiften build scripts (scons) (http://www.python.org)
 

	
 
Libraries
 
=========
 
3. Swiften library (http://swift.im/git/swift)
 
4. Boost 1.48 or newer (http://sourceforge.net/projects/boost/files/boost/1.49.0/)
 
5. Google ProtoBuf library (http://code.google.com/p/protobuf/downloads/list)
 

	
 

	
 
Environment
 
===========
 

	
 
To create spectrum build environment do:
 

	
 
0. Create directory where we'll install all dependencies, e.g. C:\env-msvc-x64.
 
Create C:\env-msvc-x64\bin and add it to %PATH%.
 
Assuming you have git, python and cmake in %PATH%, 
 
launch "Visual Studio 2010 command prompt" or
 
"Visual Studio 2010(x64) command prompt", depends on your target (Windows x86 or Windows x86_64).
 
1. unpack and build boost libraries:
 

	
 
	bootstrap.bat
 
	b2.exe --without-mpi --without-python
 
	b2.exe --without-mpi --without-python install --prefix=C:\env-msvc-x64 release
 

	
 
2. clone swift repository and build it. Don't forget to point it to our env directory:
 

	
 
	git clone git://swift.im/swift
 
	cd swift
 
	echo boost_includedir="c:/env-msvc-x64/include/boost-1_49" > config.py
 
	echo boost_libdir="c:/env-msvc-x64/lib" >> config.py 
 
	scons.bat debug=no SWIFTEN_INSTALLDIR=C:\env-msvc-x64 force_configure=1
 
	scons.bat debug=no SWIFTEN_INSTALLDIR=C:\env-msvc-x64 C:\env-msvc-x64
 

	
 
3. unpack and compile protobuf as described in its documentation. 
 

	
 
Run extract_includes.bat in vsprojects/ directory and move resulting vsprojects/include/google/ directory to our C:\env-msvc-x64\include
 

	
 
Move protoc.exe to C:\env-msvc-x64\bin\ and libprotobuf.lib to C:\env-msvc-x64\lib
 

	
 
4. Install gtkmm
 

	
 
Download installer from https://live.gnome.org/gtkmm/MSWindows and install gtkmm into C:\env-msvc-x64\
 

	
 
5. Install libpurple headers
 

	
 
Download http://www.pidgin.im/download/source/ , extract it and copy libpurple directory in C:\env-msvc-x64\include
 

	
 
6. You're ready! :) Clone libtransport into C:\env-msvc-x64\libtransport (You *must* clone it into this directory, because libtransport will try to find the dependencies in ../lib and ../include)
 

	
 
Compile it as:
 

	
 
	set CMAKE_INCLUDE_PATH=C:\env-msvc-x64\include
 
	cmake . -G "NMake Makefiles" -DBOOST_INCLUDEDIR=../include/boost-1_49 -DBOOST_LIBRARYDIR=../lib -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=C:\env-msvc-x64 -DGIT_EXECUTABLE="c:\Program Files (x86)\git\bin\git.exe"
 
	nmake
backends/frotz/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
 
ADD_SUBDIRECTORY(dfrotz)
 
 
FILE(GLOB SRC *.c *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_frotz_backend ${SRC})
 
 
target_link_libraries(spectrum2_frotz_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
 
INSTALL(TARGETS spectrum2_frotz_backend RUNTIME DESTINATION bin)
 
 
cmake_minimum_required(VERSION 2.6)
 

	
 
ADD_SUBDIRECTORY(dfrotz)
 

	
 
FILE(GLOB SRC *.c *.cpp)
 

	
 
ADD_EXECUTABLE(spectrum2_frotz_backend ${SRC})
 

	
 
target_link_libraries(spectrum2_frotz_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 

	
 
INSTALL(TARGETS spectrum2_frotz_backend RUNTIME DESTINATION bin)
 

	
backends/frotz/dfrotz/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC common/*.c dumb/*.c)
 
 
ADD_EXECUTABLE(dfrotz ${SRC})
 
 
# target_link_libraries(dfrotz)
 
 
INSTALL(TARGETS dfrotz RUNTIME DESTINATION bin)
 
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC common/*.c dumb/*.c)
 

	
 
ADD_EXECUTABLE(dfrotz ${SRC})
 

	
 
# target_link_libraries(dfrotz)
 

	
 
INSTALL(TARGETS dfrotz RUNTIME DESTINATION bin)
 

	
backends/libcommuni/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 
FILE(GLOB HEADERS *.h)
 
QT4_WRAP_CPP(SRC ${HEADERS} OPTIONS -DBOOST_TT_HAS_OPERATOR_HPP_INCLUDED)
 
ADD_EXECUTABLE(spectrum2_libcommuni_backend ${SRC})
 
 
if (NOT WIN32)
 
	target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} ${QT_LIBRARIES} transport pthread)
 
else ()
 
	target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} ${QT_LIBRARIES} transport)
 
endif()
 
INSTALL(TARGETS spectrum2_libcommuni_backend RUNTIME DESTINATION bin)
 
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 
FILE(GLOB HEADERS *.h)
 
QT4_WRAP_CPP(SRC ${HEADERS} OPTIONS -DBOOST_TT_HAS_OPERATOR_HPP_INCLUDED)
 
ADD_EXECUTABLE(spectrum2_libcommuni_backend ${SRC})
 

	
 
if (NOT WIN32)
 
	target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} ${QT_LIBRARIES} transport pthread)
 
else ()
 
	target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} ${QT_LIBRARIES} transport)
 
endif()
 
INSTALL(TARGETS spectrum2_libcommuni_backend RUNTIME DESTINATION bin)
 

	
backends/libpurple/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_libpurple_backend ${SRC})
 
 
if(CMAKE_COMPILER_IS_GNUCXX)
 
	if (NOT WIN32)
 
	target_link_libraries(spectrum2_libpurple_backend ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin pthread)
 
	else()
 
	target_link_libraries(spectrum2_libpurple_backend ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin)
 
	endif()
 
else()
 
target_link_libraries(spectrum2_libpurple_backend sqlite3 ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${LIBXML2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin ${PROTOBUF_LIBRARY})
 
endif()
 
 
INSTALL(TARGETS spectrum2_libpurple_backend RUNTIME DESTINATION bin)
 
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 

	
 
ADD_EXECUTABLE(spectrum2_libpurple_backend ${SRC})
 

	
 
if(CMAKE_COMPILER_IS_GNUCXX)
 
	if (NOT WIN32)
 
	target_link_libraries(spectrum2_libpurple_backend ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin pthread)
 
	else()
 
	target_link_libraries(spectrum2_libpurple_backend ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin)
 
	endif()
 
else()
 
target_link_libraries(spectrum2_libpurple_backend sqlite3 ${PURPLE_LIBRARY} ${GLIB2_LIBRARIES} ${LIBXML2_LIBRARIES} ${EVENT_LIBRARIES} transport-plugin ${PROTOBUF_LIBRARY})
 
endif()
 

	
 
INSTALL(TARGETS spectrum2_libpurple_backend RUNTIME DESTINATION bin)
 

	
backends/libyahoo2/CMakeLists.txt
Show inline comments
 
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)
 
 
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/skype/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_skype_backend ${SRC})
 
 
target_link_libraries(spectrum2_skype_backend ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport pthread ${LIBDBUSGLIB_LIBRARIES})
 
 
INSTALL(TARGETS spectrum2_skype_backend RUNTIME DESTINATION bin)
 
 
cmake_minimum_required(VERSION 2.6)
 
FILE(GLOB SRC *.cpp)
 

	
 
ADD_EXECUTABLE(spectrum2_skype_backend ${SRC})
 

	
 
target_link_libraries(spectrum2_skype_backend ${GLIB2_LIBRARIES} ${EVENT_LIBRARIES} transport pthread ${LIBDBUSGLIB_LIBRARIES})
 

	
 
INSTALL(TARGETS spectrum2_skype_backend RUNTIME DESTINATION bin)
 

	
backends/smstools3/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
 
FILE(GLOB SRC *.c *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_smstools3_backend ${SRC})
 
 
target_link_libraries(spectrum2_smstools3_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
 
INSTALL(TARGETS spectrum2_smstools3_backend RUNTIME DESTINATION bin)
 
 
cmake_minimum_required(VERSION 2.6)
 

	
 
FILE(GLOB SRC *.c *.cpp)
 

	
 
ADD_EXECUTABLE(spectrum2_smstools3_backend ${SRC})
 

	
 
target_link_libraries(spectrum2_smstools3_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 

	
 
INSTALL(TARGETS spectrum2_smstools3_backend RUNTIME DESTINATION bin)
 

	
backends/swiften/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
 
FILE(GLOB SRC *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_swiften_backend ${SRC})
 
 
IF (NOT WIN32)
 
target_link_libraries(spectrum2_swiften_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
else()
 
target_link_libraries(spectrum2_swiften_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
endif()
 
 
INSTALL(TARGETS spectrum2_swiften_backend RUNTIME DESTINATION bin)
 
 
cmake_minimum_required(VERSION 2.6)
 

	
 
FILE(GLOB SRC *.cpp)
 

	
 
ADD_EXECUTABLE(spectrum2_swiften_backend ${SRC})
 

	
 
IF (NOT WIN32)
 
target_link_libraries(spectrum2_swiften_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
else()
 
target_link_libraries(spectrum2_swiften_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
endif()
 

	
 
INSTALL(TARGETS spectrum2_swiften_backend RUNTIME DESTINATION bin)
 

	
backends/swiften/main.cpp
Show inline comments
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 
 
#include "boost/date_time/posix_time/posix_time.hpp"
 
 
// Swiften
 
#include "Swiften/Swiften.h"
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  SWIFTEN_VERSION >= 0x030000
 
 
#ifndef WIN32
 
// for signal handler
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
#endif
 
 
#ifndef __FreeBSD__
 
#ifndef __MACH__
 
// malloc_trim
 
#include "malloc.h"
 
#endif
 
#endif
 
 
// Boost
 
#include <boost/algorithm/string.hpp>
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 
 
DEFINE_LOGGER(logger, "Swiften");
 
DEFINE_LOGGER(logger_xml, "backend.xml");
 
 
// eventloop
 
Swift::SimpleEventLoop *loop_;
 
 
// Plugins
 
class SwiftenPlugin;
 
NetworkPlugin *np = NULL;
 
Swift::XMPPSerializer *serializer;
 
 
class ForwardIQHandler : public Swift::IQHandler {
 
	public:
 
		std::map <std::string, std::string> m_id2resource;
 
 
		ForwardIQHandler(NetworkPlugin *np, const std::string &user) {
 
			m_np = np;
 
			m_user = user;
 
		}
 
 
		bool handleIQ(boost::shared_ptr<Swift::IQ> iq) {
 
			if (iq->getPayload<Swift::RosterPayload>() != NULL) {
 
				return false;
 
			}
 
			if (iq->getType() == Swift::IQ::Get) {
 
				m_id2resource[iq->getID()] = iq->getFrom().getResource();
 
			}
 
 
			iq->setTo(m_user);
 
			std::string xml = safeByteArrayToString(serializer->serializeElement(iq));
 
			m_np->sendRawXML(xml);
 
			return true;
 
		}
 
 
	private:
 
		NetworkPlugin *m_np;
 
		std::string m_user;
 
		
 
};
 
 
class SwiftenPlugin : public NetworkPlugin, Swift::XMPPParserClient {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
		bool m_firstPing;
 
		
 
		Swift::FullPayloadSerializerCollection collection;
 
		Swift::XMPPParser *m_xmppParser;
 
		Swift::FullPayloadParserFactoryCollection m_collection2;
 
 
		SwiftenPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
			m_firstPing = true;
 
			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));
 
#if HAVE_SWIFTEN_3
 
			serializer = new Swift::XMPPSerializer(&collection, Swift::ClientStreamType, false);
 
#else
 
			serializer = new Swift::XMPPSerializer(&collection, Swift::ClientStreamType);
 
#endif
 
			m_xmppParser = new Swift::XMPPParser(this, &m_collection2, m_factories->getXMLParserFactory());
 
			m_xmppParser->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
 
 
			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) {
 
			if (m_firstPing) {
 
				m_firstPing = false;
 
				NetworkPlugin::PluginConfig cfg;
 
				cfg.setRawXML(true);
 
				sendConfig(cfg);
 
			}
 
			std::string d(data->begin(), data->end());
 
			handleDataRead(d);
 
		}
 
 
		void handleStreamStart(const Swift::ProtocolHeader&) {}
 
#if HAVE_SWIFTEN_3
 
		void handleElement(boost::shared_ptr<Swift::ToplevelElement> element) {
 
#else
 
		void handleElement(boost::shared_ptr<Swift::Element> element) {
 
#endif
 
			boost::shared_ptr<Swift::Stanza> stanza = boost::dynamic_pointer_cast<Swift::Stanza>(element);
 
			if (!stanza) {
 
				return;
 
			}
 
 
			std::string user = stanza->getFrom().toBare();
 
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (!client)
 
				return;
 
 
			stanza->setFrom(client->getJID());
 
 
			boost::shared_ptr<Swift::Message> message = boost::dynamic_pointer_cast<Swift::Message>(stanza);
 
			if (message) {
 
				client->sendMessage(message);
 
				return;
 
			}
 
 
			boost::shared_ptr<Swift::Presence> presence = boost::dynamic_pointer_cast<Swift::Presence>(stanza);
 
			if (presence) {
 
				client->sendPresence(presence);
 
				return;
 
			}
 
 
			boost::shared_ptr<Swift::IQ> iq = boost::dynamic_pointer_cast<Swift::IQ>(stanza);
 
			if (iq) {
 
				if (m_handlers[user]->m_id2resource.find(stanza->getID()) != m_handlers[user]->m_id2resource.end()) {
 
					std::string resource = m_handlers[user]->m_id2resource[stanza->getID()];
 
					if (resource.empty()) {
 
						iq->setTo(Swift::JID(iq->getTo().getNode(), iq->getTo().getDomain()));					
 
					} else {
 
						iq->setTo(Swift::JID(iq->getTo().getNode(), iq->getTo().getDomain(), resource));					
 
					}
 
					
 
					m_handlers[user]->m_id2resource.erase(stanza->getID());
 
				}
 
				client->getIQRouter()->sendIQ(iq);
 
				return;
 
			}
 
		}
 
 
		void handleStreamEnd() {}
 
 
		void handleRawXML(const std::string &xml) {
 
			m_xmppParser->parse(xml);
 
		}
 
 
		void handleSwiftDisconnected(const std::string &user, const boost::optional<Swift::ClientError> &error) {
 
			std::string message = "";
 
			bool reconnect = false;
 
			if (error) {
 
				switch(error->getType()) {
 
					case Swift::ClientError::UnknownError: message = ("Unknown Error"); reconnect = true; 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"); reconnect = true; break;
 
					case Swift::ClientError::ConnectionWriteError: message = ("Error while sending data to the server"); reconnect = true; break;
 
					case Swift::ClientError::XMLError: message = ("Error parsing server data"); reconnect = true; 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, reconnect ? 0 : 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);
 
				m_handlers.erase(user);
 
			}
 
 
#ifndef WIN32
 
#ifndef __FreeBSD__
 
#ifndef __MACH__
 
			// force returning of memory chunks allocated by libxml2 to kernel
 
			malloc_trim(0);
 
#endif
 
#endif
 
#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) {
 
// 			boost::shared_ptr<Swift::Client> client = m_users[user];
 
// 			if (client->getMUCRegistry()->isMUC(presence->getFrom().toBare())) {
 
// 				return;
 
// 			}
 
// 
 
// 			if (presence->getPayload<Swift::MUCUserPayload>() != NULL || presence->getPayload<Swift::MUCPayload>() != NULL) {
 
// 				return;
 
// 			}
 
// 
 
// 			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);
 
// 			}
 
			presence->setTo(user);
 
			std::string xml = safeByteArrayToString(serializer->serializeElement(presence));
 
			sendRawXML(xml);
 
		}
 
 
		void handleSwiftMessageReceived(const std::string &user, Swift::Message::ref message) {
 
			message->setTo(user);
 
			std::string xml = safeByteArrayToString(serializer->serializeElement(message));
 
			sendRawXML(xml);
 
		}
 
 
		void handleSwiftenDataRead(const Swift::SafeByteArray &data) {
 
			std::string d = safeByteArrayToString(data);
 
			if (!boost::starts_with(d, "<auth")) {
 
				LOG4CXX_INFO(logger_xml, "XML IN " << d);
 
			}
 
		}
 
 
		void handleSwiftenDataWritten(const Swift::SafeByteArray &data) {
 
			LOG4CXX_INFO(logger_xml, "XML OUT " << safeByteArrayToString(data));
 
		}
 
 
		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 + "/Spectrum"), 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));
 
			client->onDataRead.connect(boost::bind(&SwiftenPlugin::handleSwiftenDataRead, this, _1));
 
			client->onDataWritten.connect(boost::bind(&SwiftenPlugin::handleSwiftenDataWritten, this, _1));
 
			client->getSubscriptionManager()->onPresenceSubscriptionRequest.connect(boost::bind(&SwiftenPlugin::handleSubscriptionRequest, this, user, _1, _2, _3));
 
			client->getSubscriptionManager()->onPresenceSubscriptionRevoked.connect(boost::bind(&SwiftenPlugin::handleSubscriptionRevoked, this, user, _1, _2));
 
			Swift::ClientOptions opt;
 
			opt.allowPLAINWithoutTLS = true;
 
			client->connect(opt);
 
 
			boost::shared_ptr<ForwardIQHandler> handler = boost::make_shared<ForwardIQHandler>(this, user);
 
			client->getIQRouter()->addHandler(handler);
 
			m_handlers[user] = handler;
 
		}
 
 
		void handleSubscriptionRequest(const std::string &user, const Swift::JID& jid, const std::string& message, Swift::Presence::ref presence) {
 
			handleSwiftPresenceChanged(user, presence);
 
		}
 
 
		void handleSubscriptionRevoked(const std::string &user, const Swift::JID& jid, const std::string& message) {
 
			Swift::Presence::ref presence = Swift::Presence::create();
 
			presence->setTo(user);
 
			presence->setFrom(jid);
 
			presence->setType(Swift::Presence::Unsubscribe);
 
			handleSwiftPresenceChanged(user, presence);
 
		}
 
 
		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 = "", const std::string &id = "") {
 
		}
 
 
		void handleVCardRequest(const std::string &user, const std::string &legacyName, unsigned int id) {
 
		}
 
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) {
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				LOG4CXX_INFO(logger, user << ": Added/Updated buddy " << buddyName << ".");
 
				if (!client->getRoster()->containsJID(buddyName) || client->getRoster()->getSubscriptionStateForJID(buddyName) != Swift::RosterItemPayload::Both) {
 
					Swift::RosterItemPayload item;
 
					item.setName(alias);
 
					item.setJID(buddyName);
 
					item.setGroups(groups);
 
					boost::shared_ptr<Swift::RosterPayload> roster(new Swift::RosterPayload());
 
					roster->addItem(item);
 
					Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(roster, client->getIQRouter());
 
// 					request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
 
					request->send();
 
					client->getSubscriptionManager()->requestSubscription(buddyName);
 
				}
 
				else {
 
					Swift::JID contact(buddyName);
 
					Swift::RosterItemPayload item(contact, alias, client->getRoster()->getSubscriptionStateForJID(contact));
 
					item.setGroups(groups);
 
					boost::shared_ptr<Swift::RosterPayload> roster(new Swift::RosterPayload());
 
					roster->addItem(item);
 
					Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(roster, client->getIQRouter());
 
// 					request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
 
					request->send();
 
				}
 
 
			}
 
		}
 
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) {
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				Swift::RosterItemPayload item(buddyName, "", Swift::RosterItemPayload::Remove);
 
				boost::shared_ptr<Swift::RosterPayload> roster(new Swift::RosterPayload());
 
				roster->addItem(item);
 
				Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(roster, client->getIQRouter());
 
// 				request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
 
				request->send();
 
			}
 
		}
 
 
		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) {
 
 
		}
 
 
	private:
 
		Config *config;
 
		std::map<std::string, boost::shared_ptr<Swift::Client> > m_users;
 
		std::map<std::string, boost::shared_ptr<ForwardIQHandler> > m_handlers;
 
};
 
 
#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
 
 
	std::string error;
 
	Config *cfg = Config::createFromArgs(argc, argv, error, host, port);
 
	if (cfg == NULL) {
 
		std::cerr << error;
 
		return 1;
 
	}
 
 
	Logging::initBackendLogging(cfg);
 
 
	Swift::SimpleEventLoop eventLoop;
 
	loop_ = &eventLoop;
 
	np = new SwiftenPlugin(cfg, &eventLoop, host, port);
 
	loop_->run();
 
 
	return 0;
 
}
 
// Transport includes
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 

	
 
#include "boost/date_time/posix_time/posix_time.hpp"
 

	
 
// Swiften
 
#include "Swiften/Swiften.h"
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  SWIFTEN_VERSION >= 0x030000
 

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

	
 
#ifndef __FreeBSD__
 
#ifndef __MACH__
 
// malloc_trim
 
#include "malloc.h"
 
#endif
 
#endif
 

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

	
 
DEFINE_LOGGER(logger, "Swiften");
 
DEFINE_LOGGER(logger_xml, "backend.xml");
 

	
 
// eventloop
 
Swift::SimpleEventLoop *loop_;
 

	
 
// Plugins
 
class SwiftenPlugin;
 
NetworkPlugin *np = NULL;
 
Swift::XMPPSerializer *serializer;
 

	
 
class ForwardIQHandler : public Swift::IQHandler {
 
	public:
 
		std::map <std::string, std::string> m_id2resource;
 

	
 
		ForwardIQHandler(NetworkPlugin *np, const std::string &user) {
 
			m_np = np;
 
			m_user = user;
 
		}
 

	
 
		bool handleIQ(boost::shared_ptr<Swift::IQ> iq) {
 
			if (iq->getPayload<Swift::RosterPayload>() != NULL) {
 
				return false;
 
			}
 
			if (iq->getType() == Swift::IQ::Get) {
 
				m_id2resource[iq->getID()] = iq->getFrom().getResource();
 
			}
 

	
 
			iq->setTo(m_user);
 
			std::string xml = safeByteArrayToString(serializer->serializeElement(iq));
 
			m_np->sendRawXML(xml);
 
			return true;
 
		}
 

	
 
	private:
 
		NetworkPlugin *m_np;
 
		std::string m_user;
 
		
 
};
 

	
 
class SwiftenPlugin : public NetworkPlugin, Swift::XMPPParserClient {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
		bool m_firstPing;
 
		
 
		Swift::FullPayloadSerializerCollection collection;
 
		Swift::XMPPParser *m_xmppParser;
 
		Swift::FullPayloadParserFactoryCollection m_collection2;
 

	
 
		SwiftenPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
 
			this->config = config;
 
			m_firstPing = true;
 
			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));
 
#if HAVE_SWIFTEN_3
 
			serializer = new Swift::XMPPSerializer(&collection, Swift::ClientStreamType, false);
 
#else
 
			serializer = new Swift::XMPPSerializer(&collection, Swift::ClientStreamType);
 
#endif
 
			m_xmppParser = new Swift::XMPPParser(this, &m_collection2, m_factories->getXMLParserFactory());
 
			m_xmppParser->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");
 

	
 
			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) {
 
			if (m_firstPing) {
 
				m_firstPing = false;
 
				NetworkPlugin::PluginConfig cfg;
 
				cfg.setRawXML(true);
 
				sendConfig(cfg);
 
			}
 
			std::string d(data->begin(), data->end());
 
			handleDataRead(d);
 
		}
 

	
 
		void handleStreamStart(const Swift::ProtocolHeader&) {}
 
#if HAVE_SWIFTEN_3
 
		void handleElement(boost::shared_ptr<Swift::ToplevelElement> element) {
 
#else
 
		void handleElement(boost::shared_ptr<Swift::Element> element) {
 
#endif
 
			boost::shared_ptr<Swift::Stanza> stanza = boost::dynamic_pointer_cast<Swift::Stanza>(element);
 
			if (!stanza) {
 
				return;
 
			}
 

	
 
			std::string user = stanza->getFrom().toBare();
 

	
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (!client)
 
				return;
 

	
 
			stanza->setFrom(client->getJID());
 

	
 
			boost::shared_ptr<Swift::Message> message = boost::dynamic_pointer_cast<Swift::Message>(stanza);
 
			if (message) {
 
				client->sendMessage(message);
 
				return;
 
			}
 

	
 
			boost::shared_ptr<Swift::Presence> presence = boost::dynamic_pointer_cast<Swift::Presence>(stanza);
 
			if (presence) {
 
				client->sendPresence(presence);
 
				return;
 
			}
 

	
 
			boost::shared_ptr<Swift::IQ> iq = boost::dynamic_pointer_cast<Swift::IQ>(stanza);
 
			if (iq) {
 
				if (m_handlers[user]->m_id2resource.find(stanza->getID()) != m_handlers[user]->m_id2resource.end()) {
 
					std::string resource = m_handlers[user]->m_id2resource[stanza->getID()];
 
					if (resource.empty()) {
 
						iq->setTo(Swift::JID(iq->getTo().getNode(), iq->getTo().getDomain()));					
 
					} else {
 
						iq->setTo(Swift::JID(iq->getTo().getNode(), iq->getTo().getDomain(), resource));					
 
					}
 
					
 
					m_handlers[user]->m_id2resource.erase(stanza->getID());
 
				}
 
				client->getIQRouter()->sendIQ(iq);
 
				return;
 
			}
 
		}
 

	
 
		void handleStreamEnd() {}
 

	
 
		void handleRawXML(const std::string &xml) {
 
			m_xmppParser->parse(xml);
 
		}
 

	
 
		void handleSwiftDisconnected(const std::string &user, const boost::optional<Swift::ClientError> &error) {
 
			std::string message = "";
 
			bool reconnect = false;
 
			if (error) {
 
				switch(error->getType()) {
 
					case Swift::ClientError::UnknownError: message = ("Unknown Error"); reconnect = true; 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"); reconnect = true; break;
 
					case Swift::ClientError::ConnectionWriteError: message = ("Error while sending data to the server"); reconnect = true; break;
 
					case Swift::ClientError::XMLError: message = ("Error parsing server data"); reconnect = true; 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, reconnect ? 0 : 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);
 
				m_handlers.erase(user);
 
			}
 

	
 
#ifndef WIN32
 
#ifndef __FreeBSD__
 
#ifndef __MACH__
 
			// force returning of memory chunks allocated by libxml2 to kernel
 
			malloc_trim(0);
 
#endif
 
#endif
 
#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) {
 
// 			boost::shared_ptr<Swift::Client> client = m_users[user];
 
// 			if (client->getMUCRegistry()->isMUC(presence->getFrom().toBare())) {
 
// 				return;
 
// 			}
 
// 
 
// 			if (presence->getPayload<Swift::MUCUserPayload>() != NULL || presence->getPayload<Swift::MUCPayload>() != NULL) {
 
// 				return;
 
// 			}
 
// 
 
// 			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);
 
// 			}
 
			presence->setTo(user);
 
			std::string xml = safeByteArrayToString(serializer->serializeElement(presence));
 
			sendRawXML(xml);
 
		}
 

	
 
		void handleSwiftMessageReceived(const std::string &user, Swift::Message::ref message) {
 
			message->setTo(user);
 
			std::string xml = safeByteArrayToString(serializer->serializeElement(message));
 
			sendRawXML(xml);
 
		}
 

	
 
		void handleSwiftenDataRead(const Swift::SafeByteArray &data) {
 
			std::string d = safeByteArrayToString(data);
 
			if (!boost::starts_with(d, "<auth")) {
 
				LOG4CXX_INFO(logger_xml, "XML IN " << d);
 
			}
 
		}
 

	
 
		void handleSwiftenDataWritten(const Swift::SafeByteArray &data) {
 
			LOG4CXX_INFO(logger_xml, "XML OUT " << safeByteArrayToString(data));
 
		}
 

	
 
		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 + "/Spectrum"), 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));
 
			client->onDataRead.connect(boost::bind(&SwiftenPlugin::handleSwiftenDataRead, this, _1));
 
			client->onDataWritten.connect(boost::bind(&SwiftenPlugin::handleSwiftenDataWritten, this, _1));
 
			client->getSubscriptionManager()->onPresenceSubscriptionRequest.connect(boost::bind(&SwiftenPlugin::handleSubscriptionRequest, this, user, _1, _2, _3));
 
			client->getSubscriptionManager()->onPresenceSubscriptionRevoked.connect(boost::bind(&SwiftenPlugin::handleSubscriptionRevoked, this, user, _1, _2));
 
			Swift::ClientOptions opt;
 
			opt.allowPLAINWithoutTLS = true;
 
			client->connect(opt);
 

	
 
			boost::shared_ptr<ForwardIQHandler> handler = boost::make_shared<ForwardIQHandler>(this, user);
 
			client->getIQRouter()->addHandler(handler);
 
			m_handlers[user] = handler;
 
		}
 

	
 
		void handleSubscriptionRequest(const std::string &user, const Swift::JID& jid, const std::string& message, Swift::Presence::ref presence) {
 
			handleSwiftPresenceChanged(user, presence);
 
		}
 

	
 
		void handleSubscriptionRevoked(const std::string &user, const Swift::JID& jid, const std::string& message) {
 
			Swift::Presence::ref presence = Swift::Presence::create();
 
			presence->setTo(user);
 
			presence->setFrom(jid);
 
			presence->setType(Swift::Presence::Unsubscribe);
 
			handleSwiftPresenceChanged(user, presence);
 
		}
 

	
 
		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 = "", const std::string &id = "") {
 
		}
 

	
 
		void handleVCardRequest(const std::string &user, const std::string &legacyName, unsigned int id) {
 
		}
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) {
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				LOG4CXX_INFO(logger, user << ": Added/Updated buddy " << buddyName << ".");
 
				if (!client->getRoster()->containsJID(buddyName) || client->getRoster()->getSubscriptionStateForJID(buddyName) != Swift::RosterItemPayload::Both) {
 
					Swift::RosterItemPayload item;
 
					item.setName(alias);
 
					item.setJID(buddyName);
 
					item.setGroups(groups);
 
					boost::shared_ptr<Swift::RosterPayload> roster(new Swift::RosterPayload());
 
					roster->addItem(item);
 
					Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(roster, client->getIQRouter());
 
// 					request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
 
					request->send();
 
					client->getSubscriptionManager()->requestSubscription(buddyName);
 
				}
 
				else {
 
					Swift::JID contact(buddyName);
 
					Swift::RosterItemPayload item(contact, alias, client->getRoster()->getSubscriptionStateForJID(contact));
 
					item.setGroups(groups);
 
					boost::shared_ptr<Swift::RosterPayload> roster(new Swift::RosterPayload());
 
					roster->addItem(item);
 
					Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(roster, client->getIQRouter());
 
// 					request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
 
					request->send();
 
				}
 

	
 
			}
 
		}
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) {
 
			boost::shared_ptr<Swift::Client> client = m_users[user];
 
			if (client) {
 
				Swift::RosterItemPayload item(buddyName, "", Swift::RosterItemPayload::Remove);
 
				boost::shared_ptr<Swift::RosterPayload> roster(new Swift::RosterPayload());
 
				roster->addItem(item);
 
				Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(roster, client->getIQRouter());
 
// 				request->onResponse.connect(boost::bind(&RosterController::handleRosterSetError, this, _1, roster));
 
				request->send();
 
			}
 
		}
 

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

	
 
		}
 

	
 
	private:
 
		Config *config;
 
		std::map<std::string, boost::shared_ptr<Swift::Client> > m_users;
 
		std::map<std::string, boost::shared_ptr<ForwardIQHandler> > m_handlers;
 
};
 

	
 
#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
 

	
 
	std::string error;
 
	Config *cfg = Config::createFromArgs(argc, argv, error, host, port);
 
	if (cfg == NULL) {
 
		std::cerr << error;
 
		return 1;
 
	}
 

	
 
	Logging::initBackendLogging(cfg);
 

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

	
 
	return 0;
 
}
backends/template/CMakeLists.txt
Show inline comments
 
cmake_minimum_required(VERSION 2.6)
 
 
FILE(GLOB SRC *.c *.cpp)
 
 
ADD_EXECUTABLE(spectrum2_template_backend ${SRC})
 
 
if (CMAKE_COMPILER_IS_GNUCXX)
 
if (NOT WIN32)
 
target_link_libraries(spectrum2_template_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
else()
 
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
endif()
 
else()
 
target_link_libraries(spectrum2_template_backend transport ${PROTOBUF_LIBRARY} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
endif()
 
 
#INSTALL(TARGETS spectrum2_template_backend RUNTIME DESTINATION bin)
 
 
cmake_minimum_required(VERSION 2.6)
 

	
 
FILE(GLOB SRC *.c *.cpp)
 

	
 
ADD_EXECUTABLE(spectrum2_template_backend ${SRC})
 

	
 
if (CMAKE_COMPILER_IS_GNUCXX)
 
if (NOT WIN32)
 
target_link_libraries(spectrum2_template_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
else()
 
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
endif()
 
else()
 
target_link_libraries(spectrum2_template_backend transport ${PROTOBUF_LIBRARY} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
endif()
 

	
 
#INSTALL(TARGETS spectrum2_template_backend RUNTIME DESTINATION bin)
 

	
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
#include "TwitterPlugin.h"
 
#include "Requests/StatusUpdateRequest.h"
 
#include "Requests/DirectMessageRequest.h"
 
#include "Requests/TimelineRequest.h"
 
#include "Requests/FetchFriends.h"
 
#include "Requests/HelpMessageRequest.h"
 
#include "Requests/PINExchangeProcess.h"
 
#include "Requests/OAuthFlow.h"
 
#include "Requests/CreateFriendRequest.h"
 
#include "Requests/DestroyFriendRequest.h"
 
#include "Requests/RetweetRequest.h"
 
#include "Requests/ProfileImageRequest.h"
 
#include "Swiften/StringCodecs/Hexify.h"
 
 
DEFINE_LOGGER(logger, "Twitter Backend");
 
 
TwitterPlugin *np = NULL;
 
Swift::SimpleEventLoop *loop_; // Event Loop
 
 
const std::string OLD_APP_KEY = "PCWAdQpyyR12ezp2fVwEhw";
 
const std::string OLD_APP_SECRET = "EveLmCXJIg2R7BTCpm6OWV8YyX49nI0pxnYXh7JMvDg";
 
 
#define abs(x) ((x)<0?-(x):(x))
 
#define SHA(x) (Swift::Hexify::hexify(Swift::SHA1::getHash(Swift::createByteArray((x)))))
 
 
//Compares two +ve intergers 'a' and 'b' represented as strings 
 
static int cmp(std::string a, std::string b)
 
{
 
	int diff = abs((int)a.size() - (int)b.size());
 
	if(a.size() < b.size()) a = std::string(diff,'0') + a;
 
	else b = std::string(diff,'0') + b;
 
	
 
	if(a == b) return 0;
 
	if(a < b) return -1;
 
	return 1;
 
}
 
 
 
TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() 
 
{
 
	this->config = config;
 
	this->storagebackend = storagebackend;
 
	this->m_firstPing = true;
 
 
	if (CONFIG_HAS_KEY(config, "twitter.consumer_key") == false) {
 
		consumerKey = "5mFePMiJi0KpeURONkelg";
 
	}
 
	else {
 
		consumerKey = CONFIG_STRING(config, "twitter.consumer_key");
 
	}
 
	if (CONFIG_HAS_KEY(config, "twitter.consumer_secret") == false) {
 
		consumerSecret = "YFZCDJwRhbkccXEnaYr1waCQejTJcOY8F7l5Wim3FA";
 
	}
 
	else {
 
		consumerSecret = CONFIG_STRING(config, "twitter.consumer_secret");
 
	}
 
 
	if (consumerSecret.empty() || consumerKey.empty()) {
 
		LOG4CXX_ERROR(logger, "Consumer key and Consumer secret can't be empty.");
 
		exit(1);
 
	}
 
 
	adminLegacyName = "twitter.com"; 
 
	adminChatRoom = "#twitter"; 
 
	adminNickName = "twitter"; 
 
	adminAlias = "twitter";
 
 
	OAUTH_KEY = "twitter_oauth_token";
 
	OAUTH_SECRET = "twitter_oauth_secret";
 
	MODE = "mode";
 
 
	m_factories = new Swift::BoostNetworkFactories(loop);
 
	m_conn = m_factories->getConnectionFactory()->createConnection();
 
	m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
 
	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 
 
	tp = new ThreadPool(loop_, 10);
 
		
 
	tweet_timer = m_factories->getTimerFactory()->createTimer(90000);
 
	message_timer = m_factories->getTimerFactory()->createTimer(90000);
 
 
	tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
 
	message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
 
 
	tweet_timer->start();
 
	message_timer->start();
 
	
 
	LOG4CXX_INFO(logger, "Starting the plugin.");
 
}
 
 
TwitterPlugin::~TwitterPlugin() 
 
{
 
	delete storagebackend;
 
	std::set<std::string>::iterator it;
 
	for(it = onlineUsers.begin() ; it != onlineUsers.end() ; it++) delete userdb[*it].sessions;
 
	delete tp;
 
}
 
 
// Send data to NetworkPlugin server
 
void TwitterPlugin::sendData(const std::string &string) 
 
{
 
	m_conn->write(Swift::createSafeByteArray(string));
 
}
 
 
// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
 
void TwitterPlugin::_handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) 
 
{
 
	if (m_firstPing) {
 
		m_firstPing = false;
 
		// Users can join the network without registering if we allow
 
		// one user to connect multiple IRC networks.
 
		NetworkPlugin::PluginConfig cfg;
 
		cfg.setNeedPassword(false);
 
		sendConfig(cfg);
 
	}
 
 
	std::string d(data->begin(), data->end());
 
	handleDataRead(d);
 
}
 
 
// User trying to login into his twitter account
 
void TwitterPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) 
 
{
 
	if(userdb.count(user) && (userdb[user].connectionState == NEW || 
 
										userdb[user].connectionState == CONNECTED || 
 
										userdb[user].connectionState == WAITING_FOR_PIN)) {
 
		LOG4CXX_INFO(logger, std::string("A session corresponding to ") + user + std::string(" is already active"))
 
		return;
 
	}
 
	
 
	LOG4CXX_INFO(logger, std::string("Received login request for ") + user)	
 
	initUserSession(user, legacyName, password);
 
	handleConnected(user);
 
	
 
	LOG4CXX_INFO(logger, "SPECTRUM 1 USER? - " << (userdb[user].spectrum1User? "true" : "false")) 
 
	
 
	LOG4CXX_INFO(logger, user << ": Adding Buddy " << adminLegacyName << " " << adminAlias)
 
	handleBuddyChanged(user, adminLegacyName, adminAlias, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	userdb[user].nickName = "";
 
	
 
	LOG4CXX_INFO(logger, "Querying database for usersettings of " << user)
 
	std::string key, secret;
 
	getUserOAuthKeyAndSecret(user, key, secret);
 
 
	if(key == "" || secret == "") {			
 
		LOG4CXX_INFO(logger, "Intiating OAuth Flow for user " << user)
 
		setTwitterMode(user, 0);
 
		tp->runAsThread(new OAuthFlow(np, userdb[user].sessions, user, userdb[user].sessions->getTwitterUsername()));
 
	} else {
 
		LOG4CXX_INFO(logger, user << " is already registerd. Using the stored oauth key and secret")
 
		LOG4CXX_INFO(logger, key << " " << secret)	
 
		pinExchangeComplete(user, key, secret);
 
	}
 
}
 
 
// User logging out
 
void TwitterPlugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) 
 
{
 
	if (userdb.count(user)) {
 
		delete userdb[user].sessions;
 
		userdb[user].sessions = NULL;
 
		userdb[user].connectionState = DISCONNECTED;
 
	}
 
 
	if(onlineUsers.count(user)) {
 
		onlineUsers.erase(user);
 
	}
 
}
 
 
// User joining a Chatroom
 
void TwitterPlugin::handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password)
 
{
 
	if(room == adminChatRoom) {	
 
		LOG4CXX_INFO(logger, "Received Join Twitter room request for " << user)
 
 
		setTwitterMode(user, 2);
 
		handleParticipantChanged(user, adminNickName, room, 0, pbnetwork::STATUS_ONLINE);
 
		userdb[user].nickName = nickname;
 
		handleMessage(user, adminChatRoom, "Connected to Twitter room! Populating your followers list", adminNickName);
 
		tp->runAsThread(new FetchFriends(userdb[user].sessions, user,
 
										 boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3, _4)));
 
	} else {
 
		setTwitterMode(user, 0);
 
		LOG4CXX_ERROR(logger, "Couldn't connect to chatroom - " << room <<"! Try twitter-chatroom as the chatroom to access Twitter account")
 
		handleMessage(user, adminLegacyName, "Couldn't connect to chatroom! Try twitter-chatroom as the chatroom to access Twitter account");
 
	}	
 
}
 
 
// User leaving a Chatroom
 
void TwitterPlugin::handleLeaveRoomRequest(const std::string &user, const std::string &room)
 
{
 
	if(room == adminChatRoom && onlineUsers.count(user)) {
 
		LOG4CXX_INFO(logger, "Leaving chatroom! Switching back to default mode 0")
 
		setTwitterMode(user, 0);
 
		handleBuddyChanged(user, adminLegacyName, adminAlias, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	}
 
}
 
 
// Messages to be sent to Twitter 
 
void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml, const std::string &/*id*/) 
 
{
 
 
	LOG4CXX_INFO(logger, "Received " << user << " --> " << legacyName << " - " << message)
 
	
 
	if(legacyName == adminLegacyName || legacyName == adminChatRoom)  {
 
		std::string cmd = "", data = "";
 
	 
 
		/** Parsing the message - Assuming message format to be <cmd>[ ]*<data>**/	
 
		int i;
 
		for(i=0 ; i<message.size() && message[i] != ' '; i++) cmd += message[i];
 
		while(i<message.size() && message[i] == ' ') i++;
 
		data = message.substr(i);
 
		/***********************************************************************/
 
		
 
		if(cmd == "#pin") 
 
			tp->runAsThread(new PINExchangeProcess(np, userdb[user].sessions, user, data));
 
		else if(cmd == "#help") 
 
			tp->runAsThread(new HelpMessageRequest(user, CONFIG_STRING(config, "service.jid"), boost::bind(&TwitterPlugin::helpMessageResponse, this, _1, _2)));
 
		else if(cmd[0] == '@') {
 
			std::string username = cmd.substr(1); 
 
			tp->runAsThread(new DirectMessageRequest(userdb[user].sessions, user, username, data,
 
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		}
 
		else if(cmd == "#status") 
 
			tp->runAsThread(new StatusUpdateRequest(userdb[user].sessions, user, data,
 
														boost::bind(&TwitterPlugin::statusUpdateResponse, this, _1, _2)));
 
		else if(cmd == "#timeline") 
 
			tp->runAsThread(new TimelineRequest(userdb[user].sessions, user, data, "",
 
														boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
 
		else if(cmd == "#friends") 
 
			tp->runAsThread(new FetchFriends(userdb[user].sessions, user,
 
													   boost::bind(&TwitterPlugin::displayFriendlist, this, _1, _2, _3, _4)));
 
		else if(cmd == "#follow") 
 
			tp->runAsThread(new CreateFriendRequest(userdb[user].sessions, user, data.substr(0,data.find('@')),
 
													   boost::bind(&TwitterPlugin::createFriendResponse, this, _1, _2, _3, _4)));
 
		else if(cmd == "#unfollow") 
 
			tp->runAsThread(new DestroyFriendRequest(userdb[user].sessions, user, data.substr(0,data.find('@')),
 
													   boost::bind(&TwitterPlugin::deleteFriendResponse, this, _1, _2, _3)));
 
		else if(cmd == "#retweet") 
 
			tp->runAsThread(new RetweetRequest(userdb[user].sessions, user, data,
 
													   boost::bind(&TwitterPlugin::RetweetResponse, this, _1, _2)));
 
		else if(cmd == "#mode") {
 
			int m = 0;
 
			m = atoi(data.c_str());
 
			mode prevm = userdb[user].twitterMode;
 
 
			if((mode)m == userdb[user].twitterMode) return; //If same as current mode return
 
			if(m < 0 || m > 2) { // Invalid modes
 
				handleMessage(user, adminLegacyName, std::string("Error! Unknown mode ") + data + ". Allowed values 0,1,2." );
 
				return;
 
			}
 
 
			setTwitterMode(user, m);
 
			if((userdb[user].twitterMode == SINGLECONTACT || userdb[user].twitterMode == CHATROOM) && prevm == MULTIPLECONTACT) clearRoster(user);
 
			else if(userdb[user].twitterMode == MULTIPLECONTACT) 
 
				tp->runAsThread(new FetchFriends(userdb[user].sessions, user, boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3, _4)));
 
 
			handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
								std::string("Changed mode to ") + data, userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
 
			LOG4CXX_INFO(logger, user << ": Changed mode to " << data  << " <" << (userdb[user].twitterMode == CHATROOM ? adminNickName : "") << ">" )
 
		}
 
 
		else if(userdb[user].twitterMode == CHATROOM) {
 
			std::string buddy = message.substr(0, message.find(":"));
 
			if(userdb[user].buddies.count(buddy) == 0) {
 
				tp->runAsThread(new StatusUpdateRequest(userdb[user].sessions, user, message,
 
														boost::bind(&TwitterPlugin::statusUpdateResponse, this, _1, _2)));
 
			} else {
 
				data = message.substr(message.find(":")+1); //Can parse better??:P
 
				tp->runAsThread(new DirectMessageRequest(userdb[user].sessions, user, buddy, data,
 
												 		 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
			}
 
		}
 
		else handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
				                 "Unknown command! Type #help for a list of available commands.", userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	} 
 
 
	else {	
 
		std::string buddy = legacyName;
 
		if(userdb[user].twitterMode == CHATROOM) buddy = legacyName.substr(legacyName.find("/") + 1);
 
		if(legacyName != "twitter") {
 
			tp->runAsThread(new DirectMessageRequest(userdb[user].sessions, user, buddy, message,
 
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		}
 
	}
 
}
 
 
void TwitterPlugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) 
 
{
 
	if(userdb[user].connectionState != CONNECTED) {
 
		LOG4CXX_ERROR(logger, user << " is not connected to twitter!")
 
		return;
 
	}
 
 
	LOG4CXX_INFO(logger, user << " - Adding Twitter contact " << buddyName)
 
	tp->runAsThread(new CreateFriendRequest(userdb[user].sessions, user, buddyName, 
 
											boost::bind(&TwitterPlugin::createFriendResponse, this, _1, _2, _3, _4)));
 
}
 
 
void TwitterPlugin::handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) 
 
{
 
	if(userdb[user].connectionState != CONNECTED) {
 
		LOG4CXX_ERROR(logger, user << " is not connected to twitter!")
 
		return;
 
	}
 
	
 
	LOG4CXX_INFO(logger, user << " - Removing Twitter contact " << buddyName)
 
	tp->runAsThread(new DestroyFriendRequest(userdb[user].sessions, user, buddyName, 
 
											 boost::bind(&TwitterPlugin::deleteFriendResponse, this, _1, _2, _3)));
 
}
 
 
void TwitterPlugin::handleVCardRequest(const std::string &user, const std::string &legacyName, unsigned int id)
 
{
 
	if(userdb[user].connectionState != CONNECTED) {
 
		LOG4CXX_ERROR(logger, user << " is not connected to twitter!")
 
		return;
 
	}
 
	
 
	LOG4CXX_INFO(logger, user << " - VCardRequest for " << legacyName << ", " << userdb[user].buddiesInfo[legacyName].getProfileImgURL())
 
 
	if(getTwitterMode(user) != SINGLECONTACT && userdb[user].buddies.count(legacyName) 
 
		&& userdb[user].buddiesInfo[legacyName].getProfileImgURL().length()) {
 
		if(userdb[user].buddiesImgs.count(legacyName) == 0) {
 
			tp->runAsThread(new ProfileImageRequest(config, user, legacyName, userdb[user].buddiesInfo[legacyName].getProfileImgURL(), id,
 
													boost::bind(&TwitterPlugin::profileImageResponse, this, _1, _2, _3, _4, _5)));
 
		}
 
		handleVCard(user, id, legacyName, legacyName, "", userdb[user].buddiesImgs[legacyName]);
 
	}
 
}
 
 
void TwitterPlugin::pollForTweets()
 
{
 
	boost::mutex::scoped_lock lock(userlock);
 
	std::set<std::string>::iterator it = onlineUsers.begin();
 
	while(it != onlineUsers.end()) {
 
		std::string user = *it;
 
		tp->runAsThread(new TimelineRequest(userdb[user].sessions, user, "", getMostRecentTweetIDUnsafe(user),
 
											boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
 
		it++;
 
	}
 
	tweet_timer->start();
 
}
 
 
void TwitterPlugin::pollForDirectMessages()
 
{
 
	boost::mutex::scoped_lock lock(userlock);
 
	std::set<std::string>::iterator it = onlineUsers.begin();
 
	while(it != onlineUsers.end()) {
 
		std::string user = *it;
 
		tp->runAsThread(new DirectMessageRequest(userdb[user].sessions, user, "", getMostRecentDMIDUnsafe(user),
 
											boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		it++;
 
	}
 
	message_timer->start();
 
}
 
 
 
bool TwitterPlugin::getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 
 
	key="", secret=""; int type;
 
	storagebackend->getUserSetting((long)info.id, OAUTH_KEY, type, key);
 
	storagebackend->getUserSetting((long)info.id, OAUTH_SECRET, type, secret);
 
	return true;
 
}
 
 
bool TwitterPlugin::checkSpectrum1User(const std::string user) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 
 
	std::string first_synchronization_done = "";
 
	int type;
 
	storagebackend->getUserSetting((long)info.id, "first_synchronization_done", type, first_synchronization_done);
 
 
	LOG4CXX_INFO(logger, "first_synchronization_done: " << first_synchronization_done)
 
 
	if(first_synchronization_done.length()) return true;
 
	return false;
 
}
 
 
int TwitterPlugin::getTwitterMode(const std::string user) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return -1;
 
	}
 
 
	int type; int m;
 
	std::string s_m;
 
	storagebackend->getUserSetting((long)info.id, MODE, type, s_m);
 
	if(s_m == "") {
 
		s_m  = "0";
 
		storagebackend->updateUserSetting((long)info.id, MODE, s_m);
 
	}
 
	m = atoi(s_m.c_str());
 
	return m;
 
}
 
 
bool TwitterPlugin::setTwitterMode(const std::string user, int m) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 
 
	if(m < 0 || m > 2) {
 
		LOG4CXX_ERROR(logger, "Unknown mode " << m <<". Using default mode 0")
 
		m = 0;
 
	}
 
 
	userdb[user].twitterMode = (mode)m;
 
 
	//int type;
 
	std::string s_m = std::string(1,m+'0');
 
	LOG4CXX_ERROR(logger, "Storing mode " << m <<" for user " << user)
 
	storagebackend->updateUserSetting((long)info.id, MODE, s_m);
 
	return true;
 
}
 
 
bool TwitterPlugin::storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret) 
 
{
 
 
	boost::mutex::scoped_lock lock(dblock);
 
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 
 
	storagebackend->updateUserSetting((long)info.id, OAUTH_KEY, OAuthKey);	
 
	storagebackend->updateUserSetting((long)info.id, OAUTH_SECRET, OAuthSecret);
 
	return true;
 
}
 
 
void TwitterPlugin::initUserSession(const std::string user, const std::string legacyName, const std::string password)
 
{
 
	boost::mutex::scoped_lock lock(userlock);
 
 
	std::string username = legacyName;
 
	std::string passwd = password;
 
	LOG4CXX_INFO(logger, username + "  " + passwd)
 
 
	userdb[user].sessions = new twitCurl();	
 
	if(CONFIG_HAS_KEY(config,"proxy.server")) {			
 
		std::string ip = CONFIG_STRING(config,"proxy.server");
 
 
		std::ostringstream out; 
 
		out << CONFIG_INT(config,"proxy.port");
 
		std::string port = out.str();
 
 
		std::string puser = CONFIG_STRING(config,"proxy.user");
 
		std::string ppasswd = CONFIG_STRING(config,"proxy.password");
 
 
		LOG4CXX_INFO(logger, ip << " " << port << " " << puser << " " << ppasswd)
 
		
 
		if(ip != "localhost" && port != "0") {
 
			userdb[user].sessions->setProxyServerIp(ip);
 
			userdb[user].sessions->setProxyServerPort(port);
 
			userdb[user].sessions->setProxyUserName(puser);
 
			userdb[user].sessions->setProxyPassword(ppasswd);
 
		}
 
	}
 
 
	//Check if the user is spectrum1 user
 
	userdb[user].spectrum1User = checkSpectrum1User(user);
 
 
	userdb[user].connectionState = NEW;
 
	userdb[user].legacyName = username;	
 
	userdb[user].sessions->setTwitterUsername(username);
 
	userdb[user].sessions->setTwitterPassword(passwd);
 
 
	if(!userdb[user].spectrum1User) {
 
		userdb[user].sessions->getOAuth().setConsumerKey(consumerKey);
 
		userdb[user].sessions->getOAuth().setConsumerSecret(consumerSecret);
 
	} else {
 
		userdb[user].sessions->getOAuth().setConsumerKey(OLD_APP_KEY);
 
		userdb[user].sessions->getOAuth().setConsumerSecret(OLD_APP_SECRET);
 
	}
 
}
 
 
void TwitterPlugin::OAuthFlowComplete(const std::string user, twitCurl *obj) 
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
 
	delete userdb[user].sessions;
 
	userdb[user].sessions = obj->clone();	
 
	userdb[user].connectionState = WAITING_FOR_PIN;
 
}	
 
 
void TwitterPlugin::pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret) 
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
		
 
	userdb[user].sessions->getOAuth().setOAuthTokenKey( OAuthAccessTokenKey );
 
	userdb[user].sessions->getOAuth().setOAuthTokenSecret( OAuthAccessTokenSecret );
 
	userdb[user].connectionState = CONNECTED;
 
	userdb[user].twitterMode = (mode)getTwitterMode(user);
 
	
 
	if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
		tp->runAsThread(new FetchFriends(userdb[user].sessions, user, boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3, _4)));
 
	}
 
 
	onlineUsers.insert(user);
 
	userdb[user].mostRecentTweetID = "";
 
	userdb[user].mostRecentDirectMessageID = "";
 
}	
 
 
void TwitterPlugin::updateLastTweetID(const std::string user, const std::string ID)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	userdb[user].mostRecentTweetID = ID;
 
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return;
 
	}
 
 
	storagebackend->updateUserSetting((long)info.id, "twitter_last_tweet", ID);
 
}
 
 
std::string TwitterPlugin::getMostRecentTweetIDUnsafe(const std::string user)
 
{	
 
	std::string ID = "";
 
	if(onlineUsers.count(user)) {
 
		ID = userdb[user].mostRecentTweetID;
 
		if (ID.empty()) {
 
			int type;
 
			UserInfo info;
 
			if(storagebackend->getUser(user, info) == false) {
 
				LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
			}
 
			else {
 
				storagebackend->getUserSetting(info.id, "twitter_last_tweet", type, ID);
 
			}
 
		}
 
	}
 
	return ID;
 
}
 
 
std::string TwitterPlugin::getMostRecentTweetID(const std::string user)
 
{	
 
	boost::mutex::scoped_lock lock(userlock);
 
	return getMostRecentTweetIDUnsafe(user);
 
}
 
 
void TwitterPlugin::updateLastDMID(const std::string user, const std::string ID)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	userdb[user].mostRecentDirectMessageID = ID;
 
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return;
 
	}
 
 
	storagebackend->updateUserSetting((long)info.id, "twitter_last_dm", ID);
 
}
 
 
std::string TwitterPlugin::getMostRecentDMIDUnsafe(const std::string user) {
 
	std::string ID = "";
 
	if(onlineUsers.count(user)) {
 
		ID = userdb[user].mostRecentDirectMessageID;
 
		if (ID.empty()) {
 
			int type;
 
			UserInfo info;
 
			if(storagebackend->getUser(user, info) == false) {
 
				LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
			}
 
			else {
 
				storagebackend->getUserSetting(info.id, "twitter_last_dm", type, ID);
 
			}
 
		}
 
	}
 
	return ID;
 
}
 
 
std::string TwitterPlugin::getMostRecentDMID(const std::string user)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	return getMostRecentDMIDUnsafe(user);
 
}
 
 
/************************************** Twitter response functions **********************************/
 
void TwitterPlugin::statusUpdateResponse(std::string &user, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	} else {
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							"Status Update successful", userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	}
 
}
 
 
void TwitterPlugin::helpMessageResponse(std::string &user, std::string &msg)
 
{
 
	handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						msg, userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
}
 
 
void TwitterPlugin::clearRoster(const std::string user)
 
{
 
	if(userdb[user].buddies.size() == 0) return;
 
	std::set<std::string>::iterator it = userdb[user].buddies.begin();
 
	while(it != userdb[user].buddies.end()) {
 
		handleBuddyRemoved(user, *it);
 
		it++;
 
	}
 
	userdb[user].buddies.clear();
 
}
 
 
void TwitterPlugin::populateRoster(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg) 
 
{
 
	if(errMsg.getMessage().length() == 0) 
 
	{
 
		for(int i=0 ; i<friends.size() ; i++) {
 
			userdb[user].buddies.insert(friends[i].getScreenName());
 
			userdb[user].buddiesInfo[friends[i].getScreenName()] = friends[i];
 
			userdb[user].buddiesImgs[friends[i].getScreenName()] = friendAvatars[i];
 
			
 
			if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
				std::string lastTweet = friends[i].getLastStatus().getTweet();
 
				//LOG4CXX_INFO(logger, user << " - " << SHA(friendAvatars[i]))
 
				handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector<std::string>(), 
 
#if HAVE_SWIFTEN_3
 
					pbnetwork::STATUS_ONLINE, lastTweet, Swift::byteArrayToString(cryptoProvider->getSHA1Hash(Swift::createByteArray(friendAvatars[i]))));
 
#else
 
								   pbnetwork::STATUS_ONLINE, lastTweet, SHA(friendAvatars[i]));
 
#endif
 
			}
 
			else if(userdb[user].twitterMode == CHATROOM)
 
				handleParticipantChanged(user, friends[i].getScreenName(), adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
 
			
 
			/*handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							   	friends[i].getScreenName() + " - " + friends[i].getLastStatus().getTweet(), 
 
								userdb[user].twitterMode == CHATROOM ? adminNickName : "");*/
 
		}
 
	} else {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							   std::string("Error populating roster - ") + errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	}
 
 
	if(userdb[user].twitterMode == CHATROOM) handleParticipantChanged(user, userdb[user].nickName, adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
 
}
 
 
void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length() == 0) 
 
	{
 
		std::string userlist = "\n***************USER LIST****************\n";
 
		for(int i=0 ; i < friends.size() ; i++) {
 
			userlist += " - " + friends[i].getUserName() + " (" + friends[i].getScreenName() + ")\n";
 
		}	
 
		userlist += "***************************************\n";
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							userlist, userdb[user].twitterMode == CHATROOM ? adminNickName : "");	
 
	} else {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName, 
 
							   errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");	
 
	}
 
 
 
}
 
 
void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , Error &errMsg)
 
{
 
	if(errMsg.getMessage().length() == 0) {
 
		std::map<std::string, int> lastTweet;
 
		std::map<std::string, int>::iterator it;
 
 
		for(int i = tweets.size() - 1 ; i >= 0 ; i--) {
 
			if(userdb[user].twitterMode != CHATROOM) {
 
				std::string m = " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + " (MsgId: " + (tweets[i].getRetweetID().empty() ? tweets[i].getID() : tweets[i].getRetweetID()) + ")\n";
 
				handleMessage(user, adminLegacyName, m, "", "", tweets[i].getCreationTime(), true);
 
 
				std::string scrname = tweets[i].getUserData().getScreenName();
 
				if(lastTweet.count(scrname) == 0 || cmp(tweets[lastTweet[scrname]].getID(), tweets[i].getID()) <= 0) lastTweet[scrname] = i;
 
 
			} else {
 
				handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
									tweets[i].getTweet() + " (MsgId: " + (tweets[i].getRetweetID().empty() ? tweets[i].getID() : tweets[i].getRetweetID()) + ")", tweets[i].getUserData().getScreenName(), "", tweets[i].getCreationTime(), true);
 
			}
 
		}
 
		
 
		if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
			//Set as status user's last tweet
 
			for(it=lastTweet.begin() ; it!=lastTweet.end() ; it++) {
 
				int t =  it->second;
 
				handleBuddyChanged(user, tweets[t].getUserData().getScreenName(), tweets[t].getUserData().getUserName(), 
 
								   std::vector<std::string>(), pbnetwork::STATUS_ONLINE, tweets[t].getTweet());
 
			}
 
		}
 
 
		if((userRequested == "" || userRequested == user) && tweets.size()) {
 
			std::string tweetID = getMostRecentTweetID(user);
 
			if(tweetID != tweets[0].getID()) updateLastTweetID(user, tweets[0].getID());
 
		}
 
 
	} else {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							   errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");	
 
	}
 
}
 
 
void TwitterPlugin::directMessageResponse(std::string &user, std::string &username, std::vector<DirectMessage> &messages, Error &errMsg)
 
{
 
	if(errMsg.getCode() == "93") //Permission Denied
 
		return;
 
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
 
		if(username != "")
 
			handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						  std::string("Error while sending direct message! - ") + errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
		else
 
			handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						  std::string("Error while fetching direct messages! - ") + errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
		return;
 
	}
 
 
	if(username != "") {
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						   "Message delivered!", userdb[user].twitterMode == CHATROOM ? adminNickName : "");	
 
		return;
 
	}
 
	
 
	if(!messages.size()) return;
 
	
 
	if(userdb[user].twitterMode == SINGLECONTACT) {
 
 
		std::string msglist = "";
 
		std::string msgID = getMostRecentDMID(user);
 
		std::string maxID = msgID;
 
		
 
		for(int i=0 ; i < messages.size() ; i++) {
 
			if(cmp(msgID, messages[i].getID()) == -1) {
 
				msglist += " - " + messages[i].getSenderData().getScreenName() + ": " + messages[i].getMessage() + "\n";
 
				if(cmp(maxID, messages[i].getID()) == -1) maxID = messages[i].getID();
 
			}
 
		}	
 
 
		if(msglist.length()) handleMessage(user, adminLegacyName, msglist, "");	
 
		updateLastDMID(user, maxID);
 
 
	} else {
 
		
 
		std::string msgID = getMostRecentDMID(user);
 
		std::string maxID = msgID;
 
 
		for(int i=0 ; i < messages.size() ; i++) {
 
			if(cmp(msgID, messages[i].getID()) == -1) {
 
				if(userdb[user].twitterMode == MULTIPLECONTACT)
 
					handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage(), "");
 
				else
 
					handleMessage(user, adminChatRoom, messages[i].getMessage() + " - <Direct Message>", messages[i].getSenderData().getScreenName());
 
				if(cmp(maxID, messages[i].getID()) == -1) maxID = messages[i].getID();
 
			}
 
		}	
 
		
 
		if(maxID == getMostRecentDMID(user)) LOG4CXX_INFO(logger, "No new direct messages for " << user)
 
		updateLastDMID(user, maxID);
 
	}
 
}
 
 
void TwitterPlugin::createFriendResponse(std::string &user, User &frnd, std::string &img, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
		return;
 
	}
 
 
	handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						std::string("You are now following ") + frnd.getScreenName(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	
 
	userdb[user].buddies.insert(frnd.getScreenName());
 
	userdb[user].buddiesInfo[frnd.getScreenName()] = frnd;
 
	userdb[user].buddiesImgs[frnd.getScreenName()] = img;
 
	
 
	LOG4CXX_INFO(logger, user << " - " << frnd.getScreenName() << ", " << frnd.getProfileImgURL())
 
	if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
#if HAVE_SWIFTEN_3
 
		handleBuddyChanged(user, frnd.getScreenName(), frnd.getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE, "", Swift::byteArrayToString(cryptoProvider->getSHA1Hash(Swift::createByteArray(img))));
 
#else
 
		handleBuddyChanged(user, frnd.getScreenName(), frnd.getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE, "", SHA(img));
 
#endif
 
	} else if(userdb[user].twitterMode == CHATROOM) {
 
		handleParticipantChanged(user, frnd.getScreenName(), adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
 
	}
 
}
 
 
void TwitterPlugin::deleteFriendResponse(std::string &user, User &frnd, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName, 
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
		return;
 
	} 
 
	
 
	LOG4CXX_INFO(logger, user << " - " << frnd.getScreenName() << ", " << frnd.getProfileImgURL())
 
	userdb[user].buddies.erase(frnd.getScreenName());
 
	
 
	handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						std::string("You are not following ") + frnd.getScreenName() + " anymore", userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	
 
	if (userdb[user].twitterMode == CHATROOM) {
 
		handleParticipantChanged(user, frnd.getScreenName(), adminLegacyName, 0, pbnetwork::STATUS_NONE);
 
	}
 
	
 
	if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
		handleBuddyRemoved(user, frnd.getScreenName());
 
	} 
 
}
 
 
 
void TwitterPlugin::RetweetResponse(std::string &user, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	} else {
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							"Retweet successful", userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	}
 
}
 
 
void TwitterPlugin::profileImageResponse(std::string &user, std::string &buddy, std::string &img, unsigned int reqID, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	} else {
 
		LOG4CXX_INFO(logger, user << " - Sending VCard for " << buddy)
 
		handleVCard(user, reqID, buddy, buddy, "", img);
 
	}
 
}
 
#include "TwitterPlugin.h"
 
#include "Requests/StatusUpdateRequest.h"
 
#include "Requests/DirectMessageRequest.h"
 
#include "Requests/TimelineRequest.h"
 
#include "Requests/FetchFriends.h"
 
#include "Requests/HelpMessageRequest.h"
 
#include "Requests/PINExchangeProcess.h"
 
#include "Requests/OAuthFlow.h"
 
#include "Requests/CreateFriendRequest.h"
 
#include "Requests/DestroyFriendRequest.h"
 
#include "Requests/RetweetRequest.h"
 
#include "Requests/ProfileImageRequest.h"
 
#include "Swiften/StringCodecs/Hexify.h"
 

	
 
DEFINE_LOGGER(logger, "Twitter Backend");
 

	
 
TwitterPlugin *np = NULL;
 
Swift::SimpleEventLoop *loop_; // Event Loop
 

	
 
const std::string OLD_APP_KEY = "PCWAdQpyyR12ezp2fVwEhw";
 
const std::string OLD_APP_SECRET = "EveLmCXJIg2R7BTCpm6OWV8YyX49nI0pxnYXh7JMvDg";
 

	
 
#define abs(x) ((x)<0?-(x):(x))
 
#define SHA(x) (Swift::Hexify::hexify(Swift::SHA1::getHash(Swift::createByteArray((x)))))
 

	
 
//Compares two +ve intergers 'a' and 'b' represented as strings 
 
static int cmp(std::string a, std::string b)
 
{
 
	int diff = abs((int)a.size() - (int)b.size());
 
	if(a.size() < b.size()) a = std::string(diff,'0') + a;
 
	else b = std::string(diff,'0') + b;
 
	
 
	if(a == b) return 0;
 
	if(a < b) return -1;
 
	return 1;
 
}
 

	
 

	
 
TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() 
 
{
 
	this->config = config;
 
	this->storagebackend = storagebackend;
 
	this->m_firstPing = true;
 

	
 
	if (CONFIG_HAS_KEY(config, "twitter.consumer_key") == false) {
 
		consumerKey = "5mFePMiJi0KpeURONkelg";
 
	}
 
	else {
 
		consumerKey = CONFIG_STRING(config, "twitter.consumer_key");
 
	}
 
	if (CONFIG_HAS_KEY(config, "twitter.consumer_secret") == false) {
 
		consumerSecret = "YFZCDJwRhbkccXEnaYr1waCQejTJcOY8F7l5Wim3FA";
 
	}
 
	else {
 
		consumerSecret = CONFIG_STRING(config, "twitter.consumer_secret");
 
	}
 

	
 
	if (consumerSecret.empty() || consumerKey.empty()) {
 
		LOG4CXX_ERROR(logger, "Consumer key and Consumer secret can't be empty.");
 
		exit(1);
 
	}
 

	
 
	adminLegacyName = "twitter.com"; 
 
	adminChatRoom = "#twitter"; 
 
	adminNickName = "twitter"; 
 
	adminAlias = "twitter";
 

	
 
	OAUTH_KEY = "twitter_oauth_token";
 
	OAUTH_SECRET = "twitter_oauth_secret";
 
	MODE = "mode";
 

	
 
	m_factories = new Swift::BoostNetworkFactories(loop);
 
	m_conn = m_factories->getConnectionFactory()->createConnection();
 
	m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
 
	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
	tp = new ThreadPool(loop_, 10);
 
		
 
	tweet_timer = m_factories->getTimerFactory()->createTimer(90000);
 
	message_timer = m_factories->getTimerFactory()->createTimer(90000);
 

	
 
	tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
 
	message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
 

	
 
	tweet_timer->start();
 
	message_timer->start();
 
	
 
	LOG4CXX_INFO(logger, "Starting the plugin.");
 
}
 

	
 
TwitterPlugin::~TwitterPlugin() 
 
{
 
	delete storagebackend;
 
	std::set<std::string>::iterator it;
 
	for(it = onlineUsers.begin() ; it != onlineUsers.end() ; it++) delete userdb[*it].sessions;
 
	delete tp;
 
}
 

	
 
// Send data to NetworkPlugin server
 
void TwitterPlugin::sendData(const std::string &string) 
 
{
 
	m_conn->write(Swift::createSafeByteArray(string));
 
}
 

	
 
// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
 
void TwitterPlugin::_handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) 
 
{
 
	if (m_firstPing) {
 
		m_firstPing = false;
 
		// Users can join the network without registering if we allow
 
		// one user to connect multiple IRC networks.
 
		NetworkPlugin::PluginConfig cfg;
 
		cfg.setNeedPassword(false);
 
		sendConfig(cfg);
 
	}
 

	
 
	std::string d(data->begin(), data->end());
 
	handleDataRead(d);
 
}
 

	
 
// User trying to login into his twitter account
 
void TwitterPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) 
 
{
 
	if(userdb.count(user) && (userdb[user].connectionState == NEW || 
 
										userdb[user].connectionState == CONNECTED || 
 
										userdb[user].connectionState == WAITING_FOR_PIN)) {
 
		LOG4CXX_INFO(logger, std::string("A session corresponding to ") + user + std::string(" is already active"))
 
		return;
 
	}
 
	
 
	LOG4CXX_INFO(logger, std::string("Received login request for ") + user)	
 
	initUserSession(user, legacyName, password);
 
	handleConnected(user);
 
	
 
	LOG4CXX_INFO(logger, "SPECTRUM 1 USER? - " << (userdb[user].spectrum1User? "true" : "false")) 
 
	
 
	LOG4CXX_INFO(logger, user << ": Adding Buddy " << adminLegacyName << " " << adminAlias)
 
	handleBuddyChanged(user, adminLegacyName, adminAlias, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	userdb[user].nickName = "";
 
	
 
	LOG4CXX_INFO(logger, "Querying database for usersettings of " << user)
 
	std::string key, secret;
 
	getUserOAuthKeyAndSecret(user, key, secret);
 

	
 
	if(key == "" || secret == "") {			
 
		LOG4CXX_INFO(logger, "Intiating OAuth Flow for user " << user)
 
		setTwitterMode(user, 0);
 
		tp->runAsThread(new OAuthFlow(np, userdb[user].sessions, user, userdb[user].sessions->getTwitterUsername()));
 
	} else {
 
		LOG4CXX_INFO(logger, user << " is already registerd. Using the stored oauth key and secret")
 
		LOG4CXX_INFO(logger, key << " " << secret)	
 
		pinExchangeComplete(user, key, secret);
 
	}
 
}
 

	
 
// User logging out
 
void TwitterPlugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) 
 
{
 
	if (userdb.count(user)) {
 
		delete userdb[user].sessions;
 
		userdb[user].sessions = NULL;
 
		userdb[user].connectionState = DISCONNECTED;
 
	}
 

	
 
	if(onlineUsers.count(user)) {
 
		onlineUsers.erase(user);
 
	}
 
}
 

	
 
// User joining a Chatroom
 
void TwitterPlugin::handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password)
 
{
 
	if(room == adminChatRoom) {	
 
		LOG4CXX_INFO(logger, "Received Join Twitter room request for " << user)
 

	
 
		setTwitterMode(user, 2);
 
		handleParticipantChanged(user, adminNickName, room, 0, pbnetwork::STATUS_ONLINE);
 
		userdb[user].nickName = nickname;
 
		handleMessage(user, adminChatRoom, "Connected to Twitter room! Populating your followers list", adminNickName);
 
		tp->runAsThread(new FetchFriends(userdb[user].sessions, user,
 
										 boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3, _4)));
 
	} else {
 
		setTwitterMode(user, 0);
 
		LOG4CXX_ERROR(logger, "Couldn't connect to chatroom - " << room <<"! Try twitter-chatroom as the chatroom to access Twitter account")
 
		handleMessage(user, adminLegacyName, "Couldn't connect to chatroom! Try twitter-chatroom as the chatroom to access Twitter account");
 
	}	
 
}
 

	
 
// User leaving a Chatroom
 
void TwitterPlugin::handleLeaveRoomRequest(const std::string &user, const std::string &room)
 
{
 
	if(room == adminChatRoom && onlineUsers.count(user)) {
 
		LOG4CXX_INFO(logger, "Leaving chatroom! Switching back to default mode 0")
 
		setTwitterMode(user, 0);
 
		handleBuddyChanged(user, adminLegacyName, adminAlias, std::vector<std::string>(), pbnetwork::STATUS_ONLINE);
 
	}
 
}
 

	
 
// Messages to be sent to Twitter 
 
void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml, const std::string &/*id*/) 
 
{
 

	
 
	LOG4CXX_INFO(logger, "Received " << user << " --> " << legacyName << " - " << message)
 
	
 
	if(legacyName == adminLegacyName || legacyName == adminChatRoom)  {
 
		std::string cmd = "", data = "";
 
	 
 
		/** Parsing the message - Assuming message format to be <cmd>[ ]*<data>**/	
 
		int i;
 
		for(i=0 ; i<message.size() && message[i] != ' '; i++) cmd += message[i];
 
		while(i<message.size() && message[i] == ' ') i++;
 
		data = message.substr(i);
 
		/***********************************************************************/
 
		
 
		if(cmd == "#pin") 
 
			tp->runAsThread(new PINExchangeProcess(np, userdb[user].sessions, user, data));
 
		else if(cmd == "#help") 
 
			tp->runAsThread(new HelpMessageRequest(user, CONFIG_STRING(config, "service.jid"), boost::bind(&TwitterPlugin::helpMessageResponse, this, _1, _2)));
 
		else if(cmd[0] == '@') {
 
			std::string username = cmd.substr(1); 
 
			tp->runAsThread(new DirectMessageRequest(userdb[user].sessions, user, username, data,
 
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		}
 
		else if(cmd == "#status") 
 
			tp->runAsThread(new StatusUpdateRequest(userdb[user].sessions, user, data,
 
														boost::bind(&TwitterPlugin::statusUpdateResponse, this, _1, _2)));
 
		else if(cmd == "#timeline") 
 
			tp->runAsThread(new TimelineRequest(userdb[user].sessions, user, data, "",
 
														boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
 
		else if(cmd == "#friends") 
 
			tp->runAsThread(new FetchFriends(userdb[user].sessions, user,
 
													   boost::bind(&TwitterPlugin::displayFriendlist, this, _1, _2, _3, _4)));
 
		else if(cmd == "#follow") 
 
			tp->runAsThread(new CreateFriendRequest(userdb[user].sessions, user, data.substr(0,data.find('@')),
 
													   boost::bind(&TwitterPlugin::createFriendResponse, this, _1, _2, _3, _4)));
 
		else if(cmd == "#unfollow") 
 
			tp->runAsThread(new DestroyFriendRequest(userdb[user].sessions, user, data.substr(0,data.find('@')),
 
													   boost::bind(&TwitterPlugin::deleteFriendResponse, this, _1, _2, _3)));
 
		else if(cmd == "#retweet") 
 
			tp->runAsThread(new RetweetRequest(userdb[user].sessions, user, data,
 
													   boost::bind(&TwitterPlugin::RetweetResponse, this, _1, _2)));
 
		else if(cmd == "#mode") {
 
			int m = 0;
 
			m = atoi(data.c_str());
 
			mode prevm = userdb[user].twitterMode;
 

	
 
			if((mode)m == userdb[user].twitterMode) return; //If same as current mode return
 
			if(m < 0 || m > 2) { // Invalid modes
 
				handleMessage(user, adminLegacyName, std::string("Error! Unknown mode ") + data + ". Allowed values 0,1,2." );
 
				return;
 
			}
 

	
 
			setTwitterMode(user, m);
 
			if((userdb[user].twitterMode == SINGLECONTACT || userdb[user].twitterMode == CHATROOM) && prevm == MULTIPLECONTACT) clearRoster(user);
 
			else if(userdb[user].twitterMode == MULTIPLECONTACT) 
 
				tp->runAsThread(new FetchFriends(userdb[user].sessions, user, boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3, _4)));
 

	
 
			handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
								std::string("Changed mode to ") + data, userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 

	
 
			LOG4CXX_INFO(logger, user << ": Changed mode to " << data  << " <" << (userdb[user].twitterMode == CHATROOM ? adminNickName : "") << ">" )
 
		}
 

	
 
		else if(userdb[user].twitterMode == CHATROOM) {
 
			std::string buddy = message.substr(0, message.find(":"));
 
			if(userdb[user].buddies.count(buddy) == 0) {
 
				tp->runAsThread(new StatusUpdateRequest(userdb[user].sessions, user, message,
 
														boost::bind(&TwitterPlugin::statusUpdateResponse, this, _1, _2)));
 
			} else {
 
				data = message.substr(message.find(":")+1); //Can parse better??:P
 
				tp->runAsThread(new DirectMessageRequest(userdb[user].sessions, user, buddy, data,
 
												 		 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
			}
 
		}
 
		else handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
				                 "Unknown command! Type #help for a list of available commands.", userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	} 
 

	
 
	else {	
 
		std::string buddy = legacyName;
 
		if(userdb[user].twitterMode == CHATROOM) buddy = legacyName.substr(legacyName.find("/") + 1);
 
		if(legacyName != "twitter") {
 
			tp->runAsThread(new DirectMessageRequest(userdb[user].sessions, user, buddy, message,
 
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		}
 
	}
 
}
 

	
 
void TwitterPlugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) 
 
{
 
	if(userdb[user].connectionState != CONNECTED) {
 
		LOG4CXX_ERROR(logger, user << " is not connected to twitter!")
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, user << " - Adding Twitter contact " << buddyName)
 
	tp->runAsThread(new CreateFriendRequest(userdb[user].sessions, user, buddyName, 
 
											boost::bind(&TwitterPlugin::createFriendResponse, this, _1, _2, _3, _4)));
 
}
 

	
 
void TwitterPlugin::handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups) 
 
{
 
	if(userdb[user].connectionState != CONNECTED) {
 
		LOG4CXX_ERROR(logger, user << " is not connected to twitter!")
 
		return;
 
	}
 
	
 
	LOG4CXX_INFO(logger, user << " - Removing Twitter contact " << buddyName)
 
	tp->runAsThread(new DestroyFriendRequest(userdb[user].sessions, user, buddyName, 
 
											 boost::bind(&TwitterPlugin::deleteFriendResponse, this, _1, _2, _3)));
 
}
 

	
 
void TwitterPlugin::handleVCardRequest(const std::string &user, const std::string &legacyName, unsigned int id)
 
{
 
	if(userdb[user].connectionState != CONNECTED) {
 
		LOG4CXX_ERROR(logger, user << " is not connected to twitter!")
 
		return;
 
	}
 
	
 
	LOG4CXX_INFO(logger, user << " - VCardRequest for " << legacyName << ", " << userdb[user].buddiesInfo[legacyName].getProfileImgURL())
 

	
 
	if(getTwitterMode(user) != SINGLECONTACT && userdb[user].buddies.count(legacyName) 
 
		&& userdb[user].buddiesInfo[legacyName].getProfileImgURL().length()) {
 
		if(userdb[user].buddiesImgs.count(legacyName) == 0) {
 
			tp->runAsThread(new ProfileImageRequest(config, user, legacyName, userdb[user].buddiesInfo[legacyName].getProfileImgURL(), id,
 
													boost::bind(&TwitterPlugin::profileImageResponse, this, _1, _2, _3, _4, _5)));
 
		}
 
		handleVCard(user, id, legacyName, legacyName, "", userdb[user].buddiesImgs[legacyName]);
 
	}
 
}
 

	
 
void TwitterPlugin::pollForTweets()
 
{
 
	boost::mutex::scoped_lock lock(userlock);
 
	std::set<std::string>::iterator it = onlineUsers.begin();
 
	while(it != onlineUsers.end()) {
 
		std::string user = *it;
 
		tp->runAsThread(new TimelineRequest(userdb[user].sessions, user, "", getMostRecentTweetIDUnsafe(user),
 
											boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
 
		it++;
 
	}
 
	tweet_timer->start();
 
}
 

	
 
void TwitterPlugin::pollForDirectMessages()
 
{
 
	boost::mutex::scoped_lock lock(userlock);
 
	std::set<std::string>::iterator it = onlineUsers.begin();
 
	while(it != onlineUsers.end()) {
 
		std::string user = *it;
 
		tp->runAsThread(new DirectMessageRequest(userdb[user].sessions, user, "", getMostRecentDMIDUnsafe(user),
 
											boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3, _4)));
 
		it++;
 
	}
 
	message_timer->start();
 
}
 

	
 

	
 
bool TwitterPlugin::getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 

	
 
	key="", secret=""; int type;
 
	storagebackend->getUserSetting((long)info.id, OAUTH_KEY, type, key);
 
	storagebackend->getUserSetting((long)info.id, OAUTH_SECRET, type, secret);
 
	return true;
 
}
 

	
 
bool TwitterPlugin::checkSpectrum1User(const std::string user) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 

	
 
	std::string first_synchronization_done = "";
 
	int type;
 
	storagebackend->getUserSetting((long)info.id, "first_synchronization_done", type, first_synchronization_done);
 

	
 
	LOG4CXX_INFO(logger, "first_synchronization_done: " << first_synchronization_done)
 

	
 
	if(first_synchronization_done.length()) return true;
 
	return false;
 
}
 

	
 
int TwitterPlugin::getTwitterMode(const std::string user) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return -1;
 
	}
 

	
 
	int type; int m;
 
	std::string s_m;
 
	storagebackend->getUserSetting((long)info.id, MODE, type, s_m);
 
	if(s_m == "") {
 
		s_m  = "0";
 
		storagebackend->updateUserSetting((long)info.id, MODE, s_m);
 
	}
 
	m = atoi(s_m.c_str());
 
	return m;
 
}
 

	
 
bool TwitterPlugin::setTwitterMode(const std::string user, int m) 
 
{
 
	boost::mutex::scoped_lock lock(dblock);
 
	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 

	
 
	if(m < 0 || m > 2) {
 
		LOG4CXX_ERROR(logger, "Unknown mode " << m <<". Using default mode 0")
 
		m = 0;
 
	}
 

	
 
	userdb[user].twitterMode = (mode)m;
 

	
 
	//int type;
 
	std::string s_m = std::string(1,m+'0');
 
	LOG4CXX_ERROR(logger, "Storing mode " << m <<" for user " << user)
 
	storagebackend->updateUserSetting((long)info.id, MODE, s_m);
 
	return true;
 
}
 

	
 
bool TwitterPlugin::storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret) 
 
{
 

	
 
	boost::mutex::scoped_lock lock(dblock);
 

	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return false;
 
	}
 

	
 
	storagebackend->updateUserSetting((long)info.id, OAUTH_KEY, OAuthKey);	
 
	storagebackend->updateUserSetting((long)info.id, OAUTH_SECRET, OAuthSecret);
 
	return true;
 
}
 

	
 
void TwitterPlugin::initUserSession(const std::string user, const std::string legacyName, const std::string password)
 
{
 
	boost::mutex::scoped_lock lock(userlock);
 

	
 
	std::string username = legacyName;
 
	std::string passwd = password;
 
	LOG4CXX_INFO(logger, username + "  " + passwd)
 

	
 
	userdb[user].sessions = new twitCurl();	
 
	if(CONFIG_HAS_KEY(config,"proxy.server")) {			
 
		std::string ip = CONFIG_STRING(config,"proxy.server");
 

	
 
		std::ostringstream out; 
 
		out << CONFIG_INT(config,"proxy.port");
 
		std::string port = out.str();
 

	
 
		std::string puser = CONFIG_STRING(config,"proxy.user");
 
		std::string ppasswd = CONFIG_STRING(config,"proxy.password");
 

	
 
		LOG4CXX_INFO(logger, ip << " " << port << " " << puser << " " << ppasswd)
 
		
 
		if(ip != "localhost" && port != "0") {
 
			userdb[user].sessions->setProxyServerIp(ip);
 
			userdb[user].sessions->setProxyServerPort(port);
 
			userdb[user].sessions->setProxyUserName(puser);
 
			userdb[user].sessions->setProxyPassword(ppasswd);
 
		}
 
	}
 

	
 
	//Check if the user is spectrum1 user
 
	userdb[user].spectrum1User = checkSpectrum1User(user);
 

	
 
	userdb[user].connectionState = NEW;
 
	userdb[user].legacyName = username;	
 
	userdb[user].sessions->setTwitterUsername(username);
 
	userdb[user].sessions->setTwitterPassword(passwd);
 

	
 
	if(!userdb[user].spectrum1User) {
 
		userdb[user].sessions->getOAuth().setConsumerKey(consumerKey);
 
		userdb[user].sessions->getOAuth().setConsumerSecret(consumerSecret);
 
	} else {
 
		userdb[user].sessions->getOAuth().setConsumerKey(OLD_APP_KEY);
 
		userdb[user].sessions->getOAuth().setConsumerSecret(OLD_APP_SECRET);
 
	}
 
}
 

	
 
void TwitterPlugin::OAuthFlowComplete(const std::string user, twitCurl *obj) 
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 

	
 
	delete userdb[user].sessions;
 
	userdb[user].sessions = obj->clone();	
 
	userdb[user].connectionState = WAITING_FOR_PIN;
 
}	
 

	
 
void TwitterPlugin::pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret) 
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
		
 
	userdb[user].sessions->getOAuth().setOAuthTokenKey( OAuthAccessTokenKey );
 
	userdb[user].sessions->getOAuth().setOAuthTokenSecret( OAuthAccessTokenSecret );
 
	userdb[user].connectionState = CONNECTED;
 
	userdb[user].twitterMode = (mode)getTwitterMode(user);
 
	
 
	if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
		tp->runAsThread(new FetchFriends(userdb[user].sessions, user, boost::bind(&TwitterPlugin::populateRoster, this, _1, _2, _3, _4)));
 
	}
 

	
 
	onlineUsers.insert(user);
 
	userdb[user].mostRecentTweetID = "";
 
	userdb[user].mostRecentDirectMessageID = "";
 
}	
 

	
 
void TwitterPlugin::updateLastTweetID(const std::string user, const std::string ID)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	userdb[user].mostRecentTweetID = ID;
 

	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return;
 
	}
 

	
 
	storagebackend->updateUserSetting((long)info.id, "twitter_last_tweet", ID);
 
}
 

	
 
std::string TwitterPlugin::getMostRecentTweetIDUnsafe(const std::string user)
 
{	
 
	std::string ID = "";
 
	if(onlineUsers.count(user)) {
 
		ID = userdb[user].mostRecentTweetID;
 
		if (ID.empty()) {
 
			int type;
 
			UserInfo info;
 
			if(storagebackend->getUser(user, info) == false) {
 
				LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
			}
 
			else {
 
				storagebackend->getUserSetting(info.id, "twitter_last_tweet", type, ID);
 
			}
 
		}
 
	}
 
	return ID;
 
}
 

	
 
std::string TwitterPlugin::getMostRecentTweetID(const std::string user)
 
{	
 
	boost::mutex::scoped_lock lock(userlock);
 
	return getMostRecentTweetIDUnsafe(user);
 
}
 

	
 
void TwitterPlugin::updateLastDMID(const std::string user, const std::string ID)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	userdb[user].mostRecentDirectMessageID = ID;
 

	
 
	UserInfo info;
 
	if(storagebackend->getUser(user, info) == false) {
 
		LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
		return;
 
	}
 

	
 
	storagebackend->updateUserSetting((long)info.id, "twitter_last_dm", ID);
 
}
 

	
 
std::string TwitterPlugin::getMostRecentDMIDUnsafe(const std::string user) {
 
	std::string ID = "";
 
	if(onlineUsers.count(user)) {
 
		ID = userdb[user].mostRecentDirectMessageID;
 
		if (ID.empty()) {
 
			int type;
 
			UserInfo info;
 
			if(storagebackend->getUser(user, info) == false) {
 
				LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
			}
 
			else {
 
				storagebackend->getUserSetting(info.id, "twitter_last_dm", type, ID);
 
			}
 
		}
 
	}
 
	return ID;
 
}
 

	
 
std::string TwitterPlugin::getMostRecentDMID(const std::string user)
 
{
 
	boost::mutex::scoped_lock lock(userlock);	
 
	return getMostRecentDMIDUnsafe(user);
 
}
 

	
 
/************************************** Twitter response functions **********************************/
 
void TwitterPlugin::statusUpdateResponse(std::string &user, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	} else {
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							"Status Update successful", userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	}
 
}
 

	
 
void TwitterPlugin::helpMessageResponse(std::string &user, std::string &msg)
 
{
 
	handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						msg, userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
}
 

	
 
void TwitterPlugin::clearRoster(const std::string user)
 
{
 
	if(userdb[user].buddies.size() == 0) return;
 
	std::set<std::string>::iterator it = userdb[user].buddies.begin();
 
	while(it != userdb[user].buddies.end()) {
 
		handleBuddyRemoved(user, *it);
 
		it++;
 
	}
 
	userdb[user].buddies.clear();
 
}
 

	
 
void TwitterPlugin::populateRoster(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg) 
 
{
 
	if(errMsg.getMessage().length() == 0) 
 
	{
 
		for(int i=0 ; i<friends.size() ; i++) {
 
			userdb[user].buddies.insert(friends[i].getScreenName());
 
			userdb[user].buddiesInfo[friends[i].getScreenName()] = friends[i];
 
			userdb[user].buddiesImgs[friends[i].getScreenName()] = friendAvatars[i];
 
			
 
			if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
				std::string lastTweet = friends[i].getLastStatus().getTweet();
 
				//LOG4CXX_INFO(logger, user << " - " << SHA(friendAvatars[i]))
 
				handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector<std::string>(), 
 
#if HAVE_SWIFTEN_3
 
					pbnetwork::STATUS_ONLINE, lastTweet, Swift::byteArrayToString(cryptoProvider->getSHA1Hash(Swift::createByteArray(friendAvatars[i]))));
 
#else
 
								   pbnetwork::STATUS_ONLINE, lastTweet, SHA(friendAvatars[i]));
 
#endif
 
			}
 
			else if(userdb[user].twitterMode == CHATROOM)
 
				handleParticipantChanged(user, friends[i].getScreenName(), adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
 
			
 
			/*handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							   	friends[i].getScreenName() + " - " + friends[i].getLastStatus().getTweet(), 
 
								userdb[user].twitterMode == CHATROOM ? adminNickName : "");*/
 
		}
 
	} else {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							   std::string("Error populating roster - ") + errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	}
 

	
 
	if(userdb[user].twitterMode == CHATROOM) handleParticipantChanged(user, userdb[user].nickName, adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
 
}
 

	
 
void TwitterPlugin::displayFriendlist(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length() == 0) 
 
	{
 
		std::string userlist = "\n***************USER LIST****************\n";
 
		for(int i=0 ; i < friends.size() ; i++) {
 
			userlist += " - " + friends[i].getUserName() + " (" + friends[i].getScreenName() + ")\n";
 
		}	
 
		userlist += "***************************************\n";
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							userlist, userdb[user].twitterMode == CHATROOM ? adminNickName : "");	
 
	} else {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName, 
 
							   errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");	
 
	}
 
 
 
}
 

	
 
void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , Error &errMsg)
 
{
 
	if(errMsg.getMessage().length() == 0) {
 
		std::map<std::string, int> lastTweet;
 
		std::map<std::string, int>::iterator it;
 

	
 
		for(int i = tweets.size() - 1 ; i >= 0 ; i--) {
 
			if(userdb[user].twitterMode != CHATROOM) {
 
				std::string m = " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + " (MsgId: " + (tweets[i].getRetweetID().empty() ? tweets[i].getID() : tweets[i].getRetweetID()) + ")\n";
 
				handleMessage(user, adminLegacyName, m, "", "", tweets[i].getCreationTime(), true);
 

	
 
				std::string scrname = tweets[i].getUserData().getScreenName();
 
				if(lastTweet.count(scrname) == 0 || cmp(tweets[lastTweet[scrname]].getID(), tweets[i].getID()) <= 0) lastTweet[scrname] = i;
 

	
 
			} else {
 
				handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
									tweets[i].getTweet() + " (MsgId: " + (tweets[i].getRetweetID().empty() ? tweets[i].getID() : tweets[i].getRetweetID()) + ")", tweets[i].getUserData().getScreenName(), "", tweets[i].getCreationTime(), true);
 
			}
 
		}
 
		
 
		if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
			//Set as status user's last tweet
 
			for(it=lastTweet.begin() ; it!=lastTweet.end() ; it++) {
 
				int t =  it->second;
 
				handleBuddyChanged(user, tweets[t].getUserData().getScreenName(), tweets[t].getUserData().getUserName(), 
 
								   std::vector<std::string>(), pbnetwork::STATUS_ONLINE, tweets[t].getTweet());
 
			}
 
		}
 

	
 
		if((userRequested == "" || userRequested == user) && tweets.size()) {
 
			std::string tweetID = getMostRecentTweetID(user);
 
			if(tweetID != tweets[0].getID()) updateLastTweetID(user, tweets[0].getID());
 
		}
 

	
 
	} else {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							   errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");	
 
	}
 
}
 

	
 
void TwitterPlugin::directMessageResponse(std::string &user, std::string &username, std::vector<DirectMessage> &messages, Error &errMsg)
 
{
 
	if(errMsg.getCode() == "93") //Permission Denied
 
		return;
 

	
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 

	
 
		if(username != "")
 
			handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						  std::string("Error while sending direct message! - ") + errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
		else
 
			handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						  std::string("Error while fetching direct messages! - ") + errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
		return;
 
	}
 

	
 
	if(username != "") {
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						   "Message delivered!", userdb[user].twitterMode == CHATROOM ? adminNickName : "");	
 
		return;
 
	}
 
	
 
	if(!messages.size()) return;
 
	
 
	if(userdb[user].twitterMode == SINGLECONTACT) {
 

	
 
		std::string msglist = "";
 
		std::string msgID = getMostRecentDMID(user);
 
		std::string maxID = msgID;
 
		
 
		for(int i=0 ; i < messages.size() ; i++) {
 
			if(cmp(msgID, messages[i].getID()) == -1) {
 
				msglist += " - " + messages[i].getSenderData().getScreenName() + ": " + messages[i].getMessage() + "\n";
 
				if(cmp(maxID, messages[i].getID()) == -1) maxID = messages[i].getID();
 
			}
 
		}	
 

	
 
		if(msglist.length()) handleMessage(user, adminLegacyName, msglist, "");	
 
		updateLastDMID(user, maxID);
 

	
 
	} else {
 
		
 
		std::string msgID = getMostRecentDMID(user);
 
		std::string maxID = msgID;
 

	
 
		for(int i=0 ; i < messages.size() ; i++) {
 
			if(cmp(msgID, messages[i].getID()) == -1) {
 
				if(userdb[user].twitterMode == MULTIPLECONTACT)
 
					handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage(), "");
 
				else
 
					handleMessage(user, adminChatRoom, messages[i].getMessage() + " - <Direct Message>", messages[i].getSenderData().getScreenName());
 
				if(cmp(maxID, messages[i].getID()) == -1) maxID = messages[i].getID();
 
			}
 
		}	
 
		
 
		if(maxID == getMostRecentDMID(user)) LOG4CXX_INFO(logger, "No new direct messages for " << user)
 
		updateLastDMID(user, maxID);
 
	}
 
}
 

	
 
void TwitterPlugin::createFriendResponse(std::string &user, User &frnd, std::string &img, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
		return;
 
	}
 

	
 
	handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						std::string("You are now following ") + frnd.getScreenName(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	
 
	userdb[user].buddies.insert(frnd.getScreenName());
 
	userdb[user].buddiesInfo[frnd.getScreenName()] = frnd;
 
	userdb[user].buddiesImgs[frnd.getScreenName()] = img;
 
	
 
	LOG4CXX_INFO(logger, user << " - " << frnd.getScreenName() << ", " << frnd.getProfileImgURL())
 
	if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
#if HAVE_SWIFTEN_3
 
		handleBuddyChanged(user, frnd.getScreenName(), frnd.getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE, "", Swift::byteArrayToString(cryptoProvider->getSHA1Hash(Swift::createByteArray(img))));
 
#else
 
		handleBuddyChanged(user, frnd.getScreenName(), frnd.getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE, "", SHA(img));
 
#endif
 
	} else if(userdb[user].twitterMode == CHATROOM) {
 
		handleParticipantChanged(user, frnd.getScreenName(), adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
 
	}
 
}
 

	
 
void TwitterPlugin::deleteFriendResponse(std::string &user, User &frnd, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName, 
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
		return;
 
	} 
 
	
 
	LOG4CXX_INFO(logger, user << " - " << frnd.getScreenName() << ", " << frnd.getProfileImgURL())
 
	userdb[user].buddies.erase(frnd.getScreenName());
 
	
 
	handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
						std::string("You are not following ") + frnd.getScreenName() + " anymore", userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	
 
	if (userdb[user].twitterMode == CHATROOM) {
 
		handleParticipantChanged(user, frnd.getScreenName(), adminLegacyName, 0, pbnetwork::STATUS_NONE);
 
	}
 
	
 
	if(userdb[user].twitterMode == MULTIPLECONTACT) {
 
		handleBuddyRemoved(user, frnd.getScreenName());
 
	} 
 
}
 

	
 

	
 
void TwitterPlugin::RetweetResponse(std::string &user, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	} else {
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							"Retweet successful", userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	}
 
}
 

	
 
void TwitterPlugin::profileImageResponse(std::string &user, std::string &buddy, std::string &img, unsigned int reqID, Error &errMsg)
 
{
 
	if(errMsg.getMessage().length()) {
 
		if (errMsg.isCurlError()) {
 
			handleDisconnected(user, 3, errMsg.getMessage());
 
			return;
 
		}
 
		handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
 
							errMsg.getMessage(), userdb[user].twitterMode == CHATROOM ? adminNickName : "");
 
	} else {
 
		LOG4CXX_INFO(logger, user << " - Sending VCard for " << buddy)
 
		handleVCard(user, reqID, buddy, buddy, "", img);
 
	}
 
}
backends/twitter/TwitterPlugin.h
Show inline comments
 
#ifndef TWITTER_PLUGIN
 
#define TWITTER_PLUGIN
 
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/mysqlbackend.h"
 
#include "transport/pqxxbackend.h"
 
#include "transport/storagebackend.h"
 
#include "transport/threadpool.h"
 
 
#include "Swiften/Swiften.h"
 
#ifndef _WIN32
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
#endif
 
#include <boost/algorithm/string.hpp>
 
#include <boost/signal.hpp>
 
#include <boost/thread.hpp>
 
#include <boost/thread/mutex.hpp>
 
 
#include "twitcurl.h"
 
#include "TwitterResponseParser.h"
 
 
#include <iostream>
 
#include <sstream>
 
#include <map>
 
#include <vector>
 
#include <queue>
 
#include <set>
 
#include <cstdio>
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  SWIFTEN_VERSION >= 0x030000
 
#if HAVE_SWIFTEN_3
 
#include <Swiften/Crypto/CryptoProvider.h>
 
#include <Swiften/Crypto/PlatformCryptoProvider.h>
 
#else
 
#include "Swiften/StringCodecs/SHA1.h"
 
#endif
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 
 
#define STR(x) (std::string("(") + x.from + ", " + x.to + ", " + x.message + ")")
 
 
class TwitterPlugin;
 
extern TwitterPlugin *np;
 
extern Swift::SimpleEventLoop *loop_; // Event Loop
 
 
 
class TwitterPlugin : public NetworkPlugin {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
#if HAVE_SWIFTEN_3
 
		boost::shared_ptr<Swift::CryptoProvider> cryptoProvider;
 
#endif
 
		Swift::Timer::ref tweet_timer;
 
		Swift::Timer::ref message_timer;
 
		StorageBackend *storagebackend;
 
 
		TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port);
 
		~TwitterPlugin();
 
 
		// Send data to NetworkPlugin server
 
		void sendData(const std::string &string);
 
 
		// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
 
		void _handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data);
 
	
 
		// User trying to login into his twitter account
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password);
 
		
 
		// User logging out
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName);
 
		
 
		void handleJoinRoomRequest(const std::string &/*user*/, const std::string &/*room*/, const std::string &/*nickname*/, const std::string &/*pasword*/);
 
 
		void handleLeaveRoomRequest(const std::string &/*user*/, const std::string &/*room*/);
 
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "", const std::string &id = "");
 
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups);
 
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups);
 
		
 
		void handleVCardRequest(const std::string &/*user*/, const std::string &/*legacyName*/, unsigned int /*id*/);
 
		
 
		void pollForTweets();
 
 
		void pollForDirectMessages();
 
		
 
		bool getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret);
 
		
 
		bool checkSpectrum1User(const std::string user);
 
		
 
		bool storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret);
 
		
 
		void initUserSession(const std::string user, const std::string legacyName, const std::string password);
 
		
 
		void OAuthFlowComplete(const std::string user, twitCurl *obj);
 
		
 
		void pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret);
 
		
 
		void updateLastTweetID(const std::string user, const std::string ID);
 
 
		std::string getMostRecentTweetID(const std::string user);
 
 
		void updateLastDMID(const std::string user, const std::string ID);
 
		
 
		std::string getMostRecentDMID(const std::string user);
 
 
		void clearRoster(const std::string user);
 
 
		int getTwitterMode(const std::string user);
 
 
		bool setTwitterMode(const std::string user, int m);
 
 
		/****************** Twitter response handlers **************************************/
 
		void statusUpdateResponse(std::string &user, Error &errMsg);
 
		
 
		void helpMessageResponse(std::string &user, std::string &msg);
 
		
 
		void populateRoster(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg);
 
		
 
		void displayFriendlist(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg);
 
		
 
		void displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , Error &errMsg);
 
		
 
		void directMessageResponse(std::string &user, std::string &username, std::vector<DirectMessage> &messages, Error &errMsg);
 
		
 
		void createFriendResponse(std::string &user, User &frnd, std::string &img, Error &errMsg);
 
		
 
		void deleteFriendResponse(std::string &user, User &frnd, Error &errMsg);
 
		
 
		void RetweetResponse(std::string &user, Error &errMsg);
 
		
 
		void profileImageResponse(std::string &user, std::string &buddy, std::string &img, unsigned int reqID, Error &errMsg);
 
		/***********************************************************************************/
 
 
	private:
 
		std::string getMostRecentTweetIDUnsafe(const std::string user);
 
		std::string getMostRecentDMIDUnsafe(const std::string user);
 
 
		enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
 
		enum mode {SINGLECONTACT, MULTIPLECONTACT, CHATROOM};
 
 
		Config *config;
 
		std::string adminLegacyName;
 
		std::string adminChatRoom;
 
		std::string adminNickName;
 
		std::string adminAlias;
 
 
		std::string consumerKey;
 
		std::string consumerSecret;
 
		std::string OAUTH_KEY;
 
		std::string OAUTH_SECRET;
 
		std::string MODE;
 
 
		boost::mutex dblock, userlock;
 
 
		ThreadPool *tp;
 
		std::set<std::string> onlineUsers;
 
		struct UserData
 
		{
 
			std::string legacyName;
 
			bool spectrum1User; //Legacy support
 
			User userTwitterObj;
 
			std::string userImg;
 
			twitCurl* sessions;		
 
			status connectionState;
 
			std::string mostRecentTweetID;
 
			std::string mostRecentDirectMessageID;
 
			std::string nickName;
 
			std::set<std::string> buddies;
 
			std::map<std::string, User> buddiesInfo;
 
			std::map<std::string, std::string> buddiesImgs;
 
			mode twitterMode;
 
 
			UserData() { sessions = NULL; }
 
		};
 
		std::map<std::string, UserData> userdb;
 
		bool m_firstPing;
 
};
 
#endif
 
#ifndef TWITTER_PLUGIN
 
#define TWITTER_PLUGIN
 

	
 
#include "transport/config.h"
 
#include "transport/networkplugin.h"
 
#include "transport/logging.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/mysqlbackend.h"
 
#include "transport/pqxxbackend.h"
 
#include "transport/storagebackend.h"
 
#include "transport/threadpool.h"
 

	
 
#include "Swiften/Swiften.h"
 
#ifndef _WIN32
 
#include "unistd.h"
 
#include "signal.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
#endif
 
#include <boost/algorithm/string.hpp>
 
#include <boost/signal.hpp>
 
#include <boost/thread.hpp>
 
#include <boost/thread/mutex.hpp>
 

	
 
#include "twitcurl.h"
 
#include "TwitterResponseParser.h"
 

	
 
#include <iostream>
 
#include <sstream>
 
#include <map>
 
#include <vector>
 
#include <queue>
 
#include <set>
 
#include <cstdio>
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  SWIFTEN_VERSION >= 0x030000
 
#if HAVE_SWIFTEN_3
 
#include <Swiften/Crypto/CryptoProvider.h>
 
#include <Swiften/Crypto/PlatformCryptoProvider.h>
 
#else
 
#include "Swiften/StringCodecs/SHA1.h"
 
#endif
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
using namespace Transport;
 

	
 
#define STR(x) (std::string("(") + x.from + ", " + x.to + ", " + x.message + ")")
 

	
 
class TwitterPlugin;
 
extern TwitterPlugin *np;
 
extern Swift::SimpleEventLoop *loop_; // Event Loop
 

	
 

	
 
class TwitterPlugin : public NetworkPlugin {
 
	public:
 
		Swift::BoostNetworkFactories *m_factories;
 
		Swift::BoostIOServiceThread m_boostIOServiceThread;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
#if HAVE_SWIFTEN_3
 
		boost::shared_ptr<Swift::CryptoProvider> cryptoProvider;
 
#endif
 
		Swift::Timer::ref tweet_timer;
 
		Swift::Timer::ref message_timer;
 
		StorageBackend *storagebackend;
 

	
 
		TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port);
 
		~TwitterPlugin();
 

	
 
		// Send data to NetworkPlugin server
 
		void sendData(const std::string &string);
 

	
 
		// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
 
		void _handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data);
 
	
 
		// User trying to login into his twitter account
 
		void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password);
 
		
 
		// User logging out
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName);
 
		
 
		void handleJoinRoomRequest(const std::string &/*user*/, const std::string &/*room*/, const std::string &/*nickname*/, const std::string &/*pasword*/);
 

	
 
		void handleLeaveRoomRequest(const std::string &/*user*/, const std::string &/*room*/);
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "", const std::string &id = "");
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups);
 

	
 
		void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups);
 
		
 
		void handleVCardRequest(const std::string &/*user*/, const std::string &/*legacyName*/, unsigned int /*id*/);
 
		
 
		void pollForTweets();
 

	
 
		void pollForDirectMessages();
 
		
 
		bool getUserOAuthKeyAndSecret(const std::string user, std::string &key, std::string &secret);
 
		
 
		bool checkSpectrum1User(const std::string user);
 
		
 
		bool storeUserOAuthKeyAndSecret(const std::string user, const std::string OAuthKey, const std::string OAuthSecret);
 
		
 
		void initUserSession(const std::string user, const std::string legacyName, const std::string password);
 
		
 
		void OAuthFlowComplete(const std::string user, twitCurl *obj);
 
		
 
		void pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret);
 
		
 
		void updateLastTweetID(const std::string user, const std::string ID);
 

	
 
		std::string getMostRecentTweetID(const std::string user);
 

	
 
		void updateLastDMID(const std::string user, const std::string ID);
 
		
 
		std::string getMostRecentDMID(const std::string user);
 

	
 
		void clearRoster(const std::string user);
 

	
 
		int getTwitterMode(const std::string user);
 

	
 
		bool setTwitterMode(const std::string user, int m);
 

	
 
		/****************** Twitter response handlers **************************************/
 
		void statusUpdateResponse(std::string &user, Error &errMsg);
 
		
 
		void helpMessageResponse(std::string &user, std::string &msg);
 
		
 
		void populateRoster(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg);
 
		
 
		void displayFriendlist(std::string &user, std::vector<User> &friends, std::vector<std::string> &friendAvatars, Error &errMsg);
 
		
 
		void displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , Error &errMsg);
 
		
 
		void directMessageResponse(std::string &user, std::string &username, std::vector<DirectMessage> &messages, Error &errMsg);
 
		
 
		void createFriendResponse(std::string &user, User &frnd, std::string &img, Error &errMsg);
 
		
 
		void deleteFriendResponse(std::string &user, User &frnd, Error &errMsg);
 
		
 
		void RetweetResponse(std::string &user, Error &errMsg);
 
		
 
		void profileImageResponse(std::string &user, std::string &buddy, std::string &img, unsigned int reqID, Error &errMsg);
 
		/***********************************************************************************/
 

	
 
	private:
 
		std::string getMostRecentTweetIDUnsafe(const std::string user);
 
		std::string getMostRecentDMIDUnsafe(const std::string user);
 

	
 
		enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
 
		enum mode {SINGLECONTACT, MULTIPLECONTACT, CHATROOM};
 

	
 
		Config *config;
 
		std::string adminLegacyName;
 
		std::string adminChatRoom;
 
		std::string adminNickName;
 
		std::string adminAlias;
 

	
 
		std::string consumerKey;
 
		std::string consumerSecret;
 
		std::string OAUTH_KEY;
 
		std::string OAUTH_SECRET;
 
		std::string MODE;
 

	
 
		boost::mutex dblock, userlock;
 

	
 
		ThreadPool *tp;
 
		std::set<std::string> onlineUsers;
 
		struct UserData
 
		{
 
			std::string legacyName;
 
			bool spectrum1User; //Legacy support
 
			User userTwitterObj;
 
			std::string userImg;
 
			twitCurl* sessions;		
 
			status connectionState;
 
			std::string mostRecentTweetID;
 
			std::string mostRecentDirectMessageID;
 
			std::string nickName;
 
			std::set<std::string> buddies;
 
			std::map<std::string, User> buddiesInfo;
 
			std::map<std::string, std::string> buddiesImgs;
 
			mode twitterMode;
 

	
 
			UserData() { sessions = NULL; }
 
		};
 
		std::map<std::string, UserData> userdb;
 
		bool m_firstPing;
 
};
 
#endif
backends/twitter/libtwitcurl/HMAC_SHA1.cpp
Show inline comments
 
//******************************************************************************
 
//* HMAC_SHA1.cpp : Implementation of HMAC SHA1 algorithm
 
//*                 Comfort to RFC 2104
 
//*
 
//******************************************************************************
 
#include "HMAC_SHA1.h"
 
#include <iostream>
 
#include <memory>
 
 
 
void CHMAC_SHA1::HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *digest)
 
{
 
	memset(SHA1_Key, 0, SHA1_BLOCK_SIZE);
 
 
	/* repeated 64 times for values in ipad and opad */
 
	memset(m_ipad, 0x36, sizeof(m_ipad));
 
	memset(m_opad, 0x5c, sizeof(m_opad));
 
 
	/* STEP 1 */
 
	if (key_len > SHA1_BLOCK_SIZE)
 
	{
 
		CSHA1::Reset();
 
		CSHA1::Update((UINT_8 *)key, key_len);
 
		CSHA1::Final();
 
 
		CSHA1::GetHash((UINT_8 *)SHA1_Key);
 
	}
 
	else
 
		memcpy(SHA1_Key, key, key_len);
 
 
	/* STEP 2 */
 
	for (size_t i=0; i<sizeof(m_ipad); i++)
 
	{
 
		m_ipad[i] ^= SHA1_Key[i];		
 
	}
 
 
	/* STEP 3 */
 
	memcpy(AppendBuf1, m_ipad, sizeof(m_ipad));
 
	memcpy(AppendBuf1 + sizeof(m_ipad), text, text_len);
 
 
	/* STEP 4 */
 
	CSHA1::Reset();
 
	CSHA1::Update((UINT_8 *)AppendBuf1, sizeof(m_ipad) + text_len);
 
	CSHA1::Final();
 
 
	CSHA1::GetHash((UINT_8 *)szReport);
 
 
	/* STEP 5 */
 
	for (size_t j=0; j<sizeof(m_opad); j++)
 
	{
 
		m_opad[j] ^= SHA1_Key[j];
 
	}
 
 
	/* STEP 6 */
 
	memcpy(AppendBuf2, m_opad, sizeof(m_opad));
 
	memcpy(AppendBuf2 + sizeof(m_opad), szReport, SHA1_DIGEST_LENGTH);
 
 
	/*STEP 7 */
 
	CSHA1::Reset();
 
	CSHA1::Update((UINT_8 *)AppendBuf2, sizeof(m_opad) + SHA1_DIGEST_LENGTH);
 
	CSHA1::Final();
 
 
	CSHA1::GetHash((UINT_8 *)digest);
 
}
 
//******************************************************************************
 
//* HMAC_SHA1.cpp : Implementation of HMAC SHA1 algorithm
 
//*                 Comfort to RFC 2104
 
//*
 
//******************************************************************************
 
#include "HMAC_SHA1.h"
 
#include <iostream>
 
#include <memory>
 

	
 

	
 
void CHMAC_SHA1::HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *digest)
 
{
 
	memset(SHA1_Key, 0, SHA1_BLOCK_SIZE);
 

	
 
	/* repeated 64 times for values in ipad and opad */
 
	memset(m_ipad, 0x36, sizeof(m_ipad));
 
	memset(m_opad, 0x5c, sizeof(m_opad));
 

	
 
	/* STEP 1 */
 
	if (key_len > SHA1_BLOCK_SIZE)
 
	{
 
		CSHA1::Reset();
 
		CSHA1::Update((UINT_8 *)key, key_len);
 
		CSHA1::Final();
 

	
 
		CSHA1::GetHash((UINT_8 *)SHA1_Key);
 
	}
 
	else
 
		memcpy(SHA1_Key, key, key_len);
 

	
 
	/* STEP 2 */
 
	for (size_t i=0; i<sizeof(m_ipad); i++)
 
	{
 
		m_ipad[i] ^= SHA1_Key[i];		
 
	}
 

	
 
	/* STEP 3 */
 
	memcpy(AppendBuf1, m_ipad, sizeof(m_ipad));
 
	memcpy(AppendBuf1 + sizeof(m_ipad), text, text_len);
 

	
 
	/* STEP 4 */
 
	CSHA1::Reset();
 
	CSHA1::Update((UINT_8 *)AppendBuf1, sizeof(m_ipad) + text_len);
 
	CSHA1::Final();
 

	
 
	CSHA1::GetHash((UINT_8 *)szReport);
 

	
 
	/* STEP 5 */
 
	for (size_t j=0; j<sizeof(m_opad); j++)
 
	{
 
		m_opad[j] ^= SHA1_Key[j];
 
	}
 

	
 
	/* STEP 6 */
 
	memcpy(AppendBuf2, m_opad, sizeof(m_opad));
 
	memcpy(AppendBuf2 + sizeof(m_opad), szReport, SHA1_DIGEST_LENGTH);
 

	
 
	/*STEP 7 */
 
	CSHA1::Reset();
 
	CSHA1::Update((UINT_8 *)AppendBuf2, sizeof(m_opad) + SHA1_DIGEST_LENGTH);
 
	CSHA1::Final();
 

	
 
	CSHA1::GetHash((UINT_8 *)digest);
 
}
backends/twitter/libtwitcurl/HMAC_SHA1.h
Show inline comments
 
/*
 
	100% free public domain implementation of the HMAC-SHA1 algorithm
 
	by Chien-Chung, Chung (Jim Chung) <jimchung1221@gmail.com>
 
*/
 
 
 
#ifndef __HMAC_SHA1_H__
 
#define __HMAC_SHA1_H__
 
 
#include "SHA1.h"
 
 
typedef unsigned char BYTE ;
 
 
class CHMAC_SHA1 : public CSHA1
 
{
 
    private:
 
		BYTE m_ipad[64];
 
        BYTE m_opad[64];
 
 
		char * szReport ;
 
		char * SHA1_Key ;
 
		char * AppendBuf1 ;
 
		char * AppendBuf2 ;
 
 
 
	public:
 
		
 
		enum {
 
			SHA1_DIGEST_LENGTH	= 20,
 
			SHA1_BLOCK_SIZE		= 64,
 
			HMAC_BUF_LEN		= 4096
 
		} ;
 
 
		CHMAC_SHA1()
 
			:szReport(new char[HMAC_BUF_LEN]),
 
             SHA1_Key(new char[HMAC_BUF_LEN]),
 
             AppendBuf1(new char[HMAC_BUF_LEN]),
 
             AppendBuf2(new char[HMAC_BUF_LEN])
 
		{}
 
 
        ~CHMAC_SHA1()
 
        {
 
            delete[] szReport ;
 
            delete[] AppendBuf1 ;
 
            delete[] AppendBuf2 ;
 
            delete[] SHA1_Key ;
 
        }
 
 
        void HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *digest);
 
};
 
 
 
#endif /* __HMAC_SHA1_H__ */
 
/*
 
	100% free public domain implementation of the HMAC-SHA1 algorithm
 
	by Chien-Chung, Chung (Jim Chung) <jimchung1221@gmail.com>
 
*/
 

	
 

	
 
#ifndef __HMAC_SHA1_H__
 
#define __HMAC_SHA1_H__
 

	
 
#include "SHA1.h"
 

	
 
typedef unsigned char BYTE ;
 

	
 
class CHMAC_SHA1 : public CSHA1
 
{
 
    private:
 
		BYTE m_ipad[64];
 
        BYTE m_opad[64];
 

	
 
		char * szReport ;
 
		char * SHA1_Key ;
 
		char * AppendBuf1 ;
 
		char * AppendBuf2 ;
 

	
 

	
 
	public:
 
		
 
		enum {
 
			SHA1_DIGEST_LENGTH	= 20,
 
			SHA1_BLOCK_SIZE		= 64,
 
			HMAC_BUF_LEN		= 4096
 
		} ;
 

	
 
		CHMAC_SHA1()
 
			:szReport(new char[HMAC_BUF_LEN]),
 
             SHA1_Key(new char[HMAC_BUF_LEN]),
 
             AppendBuf1(new char[HMAC_BUF_LEN]),
 
             AppendBuf2(new char[HMAC_BUF_LEN])
 
		{}
 

	
 
        ~CHMAC_SHA1()
 
        {
 
            delete[] szReport ;
 
            delete[] AppendBuf1 ;
 
            delete[] AppendBuf2 ;
 
            delete[] SHA1_Key ;
 
        }
 

	
 
        void HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *digest);
 
};
 

	
 

	
 
#endif /* __HMAC_SHA1_H__ */
backends/twitter/libtwitcurl/SHA1.cpp
Show inline comments
 
/*
 
	100% free public domain implementation of the SHA-1 algorithm
 
	by Dominik Reichl <dominik.reichl@t-online.de>
 
	Web: http://www.dominik-reichl.de/
 
 
	Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
 
	- You can set the endianness in your files, no need to modify the
 
	  header file of the CSHA1 class any more
 
	- Aligned data support
 
	- Made support/compilation of the utility functions (ReportHash
 
	  and HashFile) optional (useful, if bytes count, for example in
 
	  embedded environments)
 
 
	Version 1.5 - 2005-01-01
 
	- 64-bit compiler compatibility added
 
	- Made variable wiping optional (define SHA1_WIPE_VARIABLES)
 
	- Removed unnecessary variable initializations
 
	- ROL32 improvement for the Microsoft compiler (using _rotl)
 
 
	======== Test Vectors (from FIPS PUB 180-1) ========
 
 
	SHA1("abc") =
 
		A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
 
 
	SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
 
		84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
 
 
	SHA1(A million repetitions of "a") =
 
		34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
 
*/
 
 
#include "SHA1.h"
 
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
#define SHA1_MAX_FILE_BUFFER 8000
 
#endif
 
 
// Rotate x bits to the left
 
#ifndef ROL32
 
#ifdef _MSC_VER
 
#define ROL32(_val32, _nBits) _rotl(_val32, _nBits)
 
#else
 
#define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))
 
#endif
 
#endif
 
 
#ifdef SHA1_LITTLE_ENDIAN
 
#define SHABLK0(i) (m_block->l[i] = \
 
	(ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
 
#else
 
#define SHABLK0(i) (m_block->l[i])
 
#endif
 
 
#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
 
	^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
 
 
// SHA-1 rounds
 
#define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
 
#define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
 
#define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
 
#define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
 
#define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }
 
 
CSHA1::CSHA1()
 
{
 
	m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace;
 
 
	Reset();
 
}
 
 
CSHA1::~CSHA1()
 
{
 
	Reset();
 
}
 
 
void CSHA1::Reset()
 
{
 
	// SHA1 initialization constants
 
	m_state[0] = 0x67452301;
 
	m_state[1] = 0xEFCDAB89;
 
	m_state[2] = 0x98BADCFE;
 
	m_state[3] = 0x10325476;
 
	m_state[4] = 0xC3D2E1F0;
 
 
	m_count[0] = 0;
 
	m_count[1] = 0;
 
}
 
 
void CSHA1::Transform(UINT_32 *state, UINT_8 *buffer)
 
{
 
	// Copy state[] to working vars
 
	UINT_32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
 
 
	memcpy(m_block, buffer, 64);
 
 
	// 4 rounds of 20 operations each. Loop unrolled.
 
	_R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);
 
	_R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
 
	_R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
 
	_R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
 
	_R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
 
	_R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
 
	_R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
 
	_R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
 
	_R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
 
	_R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
 
	_R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
 
	_R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
 
	_R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
 
	_R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
 
	_R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
 
	_R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
 
	_R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
 
	_R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
 
	_R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
 
	_R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);
 
 
	// Add the working vars back into state
 
	state[0] += a;
 
	state[1] += b;
 
	state[2] += c;
 
	state[3] += d;
 
	state[4] += e;
 
 
	// Wipe variables
 
#ifdef SHA1_WIPE_VARIABLES
 
	a = b = c = d = e = 0;
 
#endif
 
}
 
 
// Use this function to hash in binary data and strings
 
void CSHA1::Update(UINT_8 *data, UINT_32 len)
 
{
 
	UINT_32 i, j;
 
 
	j = (m_count[0] >> 3) & 63;
 
 
	if((m_count[0] += len << 3) < (len << 3)) m_count[1]++;
 
 
	m_count[1] += (len >> 29);
 
 
	if((j + len) > 63)
 
	{
 
		i = 64 - j;
 
		memcpy(&m_buffer[j], data, i);
 
		Transform(m_state, m_buffer);
 
 
		for(; i + 63 < len; i += 64) Transform(m_state, &data[i]);
 
 
		j = 0;
 
	}
 
	else i = 0;
 
 
	memcpy(&m_buffer[j], &data[i], len - i);
 
}
 
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
// Hash in file contents
 
bool CSHA1::HashFile(char *szFileName)
 
{
 
	unsigned long ulFileSize, ulRest, ulBlocks;
 
	unsigned long i;
 
	UINT_8 uData[SHA1_MAX_FILE_BUFFER];
 
	FILE *fIn;
 
 
	if(szFileName == NULL) return false;
 
 
	fIn = fopen(szFileName, "rb");
 
	if(fIn == NULL) return false;
 
 
	fseek(fIn, 0, SEEK_END);
 
	ulFileSize = (unsigned long)ftell(fIn);
 
	fseek(fIn, 0, SEEK_SET);
 
 
	if(ulFileSize != 0)
 
	{
 
		ulBlocks = ulFileSize / SHA1_MAX_FILE_BUFFER;
 
		ulRest = ulFileSize % SHA1_MAX_FILE_BUFFER;
 
	}
 
	else
 
	{
 
		ulBlocks = 0;
 
		ulRest = 0;
 
	}
 
 
	for(i = 0; i < ulBlocks; i++)
 
	{
 
		fread(uData, 1, SHA1_MAX_FILE_BUFFER, fIn);
 
		Update((UINT_8 *)uData, SHA1_MAX_FILE_BUFFER);
 
	}
 
 
	if(ulRest != 0)
 
	{
 
		fread(uData, 1, ulRest, fIn);
 
		Update((UINT_8 *)uData, ulRest);
 
	}
 
 
	fclose(fIn); fIn = NULL;
 
	return true;
 
}
 
#endif
 
 
void CSHA1::Final()
 
{
 
	UINT_32 i;
 
	UINT_8 finalcount[8];
 
 
	for(i = 0; i < 8; i++)
 
		finalcount[i] = (UINT_8)((m_count[((i >= 4) ? 0 : 1)]
 
			>> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
 
 
	Update((UINT_8 *)"\200", 1);
 
 
	while ((m_count[0] & 504) != 448)
 
		Update((UINT_8 *)"\0", 1);
 
 
	Update(finalcount, 8); // Cause a SHA1Transform()
 
 
	for(i = 0; i < 20; i++)
 
	{
 
		m_digest[i] = (UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
 
	}
 
 
	// Wipe variables for security reasons
 
#ifdef SHA1_WIPE_VARIABLES
 
	i = 0;
 
	memset(m_buffer, 0, 64);
 
	memset(m_state, 0, 20);
 
	memset(m_count, 0, 8);
 
	memset(finalcount, 0, 8);
 
	Transform(m_state, m_buffer);
 
#endif
 
}
 
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
// Get the final hash as a pre-formatted string
 
void CSHA1::ReportHash(char *szReport, unsigned char uReportType)
 
{
 
	unsigned char i;
 
	char szTemp[16];
 
 
	if(szReport == NULL) return;
 
 
	if(uReportType == REPORT_HEX)
 
	{
 
		sprintf(szTemp, "%02X", m_digest[0]);
 
		strcat(szReport, szTemp);
 
 
		for(i = 1; i < 20; i++)
 
		{
 
			sprintf(szTemp, " %02X", m_digest[i]);
 
			strcat(szReport, szTemp);
 
		}
 
	}
 
	else if(uReportType == REPORT_DIGIT)
 
	{
 
		sprintf(szTemp, "%u", m_digest[0]);
 
		strcat(szReport, szTemp);
 
 
		for(i = 1; i < 20; i++)
 
		{
 
			sprintf(szTemp, " %u", m_digest[i]);
 
			strcat(szReport, szTemp);
 
		}
 
	}
 
	else strcpy(szReport, "Error: Unknown report type!");
 
}
 
#endif
 
 
// Get the raw message digest
 
void CSHA1::GetHash(UINT_8 *puDest)
 
{
 
	memcpy(puDest, m_digest, 20);
 
}
 
/*
 
	100% free public domain implementation of the SHA-1 algorithm
 
	by Dominik Reichl <dominik.reichl@t-online.de>
 
	Web: http://www.dominik-reichl.de/
 

	
 
	Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
 
	- You can set the endianness in your files, no need to modify the
 
	  header file of the CSHA1 class any more
 
	- Aligned data support
 
	- Made support/compilation of the utility functions (ReportHash
 
	  and HashFile) optional (useful, if bytes count, for example in
 
	  embedded environments)
 

	
 
	Version 1.5 - 2005-01-01
 
	- 64-bit compiler compatibility added
 
	- Made variable wiping optional (define SHA1_WIPE_VARIABLES)
 
	- Removed unnecessary variable initializations
 
	- ROL32 improvement for the Microsoft compiler (using _rotl)
 

	
 
	======== Test Vectors (from FIPS PUB 180-1) ========
 

	
 
	SHA1("abc") =
 
		A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
 

	
 
	SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
 
		84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
 

	
 
	SHA1(A million repetitions of "a") =
 
		34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
 
*/
 

	
 
#include "SHA1.h"
 

	
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
#define SHA1_MAX_FILE_BUFFER 8000
 
#endif
 

	
 
// Rotate x bits to the left
 
#ifndef ROL32
 
#ifdef _MSC_VER
 
#define ROL32(_val32, _nBits) _rotl(_val32, _nBits)
 
#else
 
#define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))
 
#endif
 
#endif
 

	
 
#ifdef SHA1_LITTLE_ENDIAN
 
#define SHABLK0(i) (m_block->l[i] = \
 
	(ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
 
#else
 
#define SHABLK0(i) (m_block->l[i])
 
#endif
 

	
 
#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
 
	^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
 

	
 
// SHA-1 rounds
 
#define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
 
#define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
 
#define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
 
#define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
 
#define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }
 

	
 
CSHA1::CSHA1()
 
{
 
	m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace;
 

	
 
	Reset();
 
}
 

	
 
CSHA1::~CSHA1()
 
{
 
	Reset();
 
}
 

	
 
void CSHA1::Reset()
 
{
 
	// SHA1 initialization constants
 
	m_state[0] = 0x67452301;
 
	m_state[1] = 0xEFCDAB89;
 
	m_state[2] = 0x98BADCFE;
 
	m_state[3] = 0x10325476;
 
	m_state[4] = 0xC3D2E1F0;
 

	
 
	m_count[0] = 0;
 
	m_count[1] = 0;
 
}
 

	
 
void CSHA1::Transform(UINT_32 *state, UINT_8 *buffer)
 
{
 
	// Copy state[] to working vars
 
	UINT_32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
 

	
 
	memcpy(m_block, buffer, 64);
 

	
 
	// 4 rounds of 20 operations each. Loop unrolled.
 
	_R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);
 
	_R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
 
	_R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
 
	_R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
 
	_R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
 
	_R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
 
	_R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
 
	_R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
 
	_R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
 
	_R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
 
	_R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
 
	_R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
 
	_R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
 
	_R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
 
	_R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
 
	_R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
 
	_R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
 
	_R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
 
	_R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
 
	_R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);
 

	
 
	// Add the working vars back into state
 
	state[0] += a;
 
	state[1] += b;
 
	state[2] += c;
 
	state[3] += d;
 
	state[4] += e;
 

	
 
	// Wipe variables
 
#ifdef SHA1_WIPE_VARIABLES
 
	a = b = c = d = e = 0;
 
#endif
 
}
 

	
 
// Use this function to hash in binary data and strings
 
void CSHA1::Update(UINT_8 *data, UINT_32 len)
 
{
 
	UINT_32 i, j;
 

	
 
	j = (m_count[0] >> 3) & 63;
 

	
 
	if((m_count[0] += len << 3) < (len << 3)) m_count[1]++;
 

	
 
	m_count[1] += (len >> 29);
 

	
 
	if((j + len) > 63)
 
	{
 
		i = 64 - j;
 
		memcpy(&m_buffer[j], data, i);
 
		Transform(m_state, m_buffer);
 

	
 
		for(; i + 63 < len; i += 64) Transform(m_state, &data[i]);
 

	
 
		j = 0;
 
	}
 
	else i = 0;
 

	
 
	memcpy(&m_buffer[j], &data[i], len - i);
 
}
 

	
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
// Hash in file contents
 
bool CSHA1::HashFile(char *szFileName)
 
{
 
	unsigned long ulFileSize, ulRest, ulBlocks;
 
	unsigned long i;
 
	UINT_8 uData[SHA1_MAX_FILE_BUFFER];
 
	FILE *fIn;
 

	
 
	if(szFileName == NULL) return false;
 

	
 
	fIn = fopen(szFileName, "rb");
 
	if(fIn == NULL) return false;
 

	
 
	fseek(fIn, 0, SEEK_END);
 
	ulFileSize = (unsigned long)ftell(fIn);
 
	fseek(fIn, 0, SEEK_SET);
 

	
 
	if(ulFileSize != 0)
 
	{
 
		ulBlocks = ulFileSize / SHA1_MAX_FILE_BUFFER;
 
		ulRest = ulFileSize % SHA1_MAX_FILE_BUFFER;
 
	}
 
	else
 
	{
 
		ulBlocks = 0;
 
		ulRest = 0;
 
	}
 

	
 
	for(i = 0; i < ulBlocks; i++)
 
	{
 
		fread(uData, 1, SHA1_MAX_FILE_BUFFER, fIn);
 
		Update((UINT_8 *)uData, SHA1_MAX_FILE_BUFFER);
 
	}
 

	
 
	if(ulRest != 0)
 
	{
 
		fread(uData, 1, ulRest, fIn);
 
		Update((UINT_8 *)uData, ulRest);
 
	}
 

	
 
	fclose(fIn); fIn = NULL;
 
	return true;
 
}
 
#endif
 

	
 
void CSHA1::Final()
 
{
 
	UINT_32 i;
 
	UINT_8 finalcount[8];
 

	
 
	for(i = 0; i < 8; i++)
 
		finalcount[i] = (UINT_8)((m_count[((i >= 4) ? 0 : 1)]
 
			>> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
 

	
 
	Update((UINT_8 *)"\200", 1);
 

	
 
	while ((m_count[0] & 504) != 448)
 
		Update((UINT_8 *)"\0", 1);
 

	
 
	Update(finalcount, 8); // Cause a SHA1Transform()
 

	
 
	for(i = 0; i < 20; i++)
 
	{
 
		m_digest[i] = (UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
 
	}
 

	
 
	// Wipe variables for security reasons
 
#ifdef SHA1_WIPE_VARIABLES
 
	i = 0;
 
	memset(m_buffer, 0, 64);
 
	memset(m_state, 0, 20);
 
	memset(m_count, 0, 8);
 
	memset(finalcount, 0, 8);
 
	Transform(m_state, m_buffer);
 
#endif
 
}
 

	
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
// Get the final hash as a pre-formatted string
 
void CSHA1::ReportHash(char *szReport, unsigned char uReportType)
 
{
 
	unsigned char i;
 
	char szTemp[16];
 

	
 
	if(szReport == NULL) return;
 

	
 
	if(uReportType == REPORT_HEX)
 
	{
 
		sprintf(szTemp, "%02X", m_digest[0]);
 
		strcat(szReport, szTemp);
 

	
 
		for(i = 1; i < 20; i++)
 
		{
 
			sprintf(szTemp, " %02X", m_digest[i]);
 
			strcat(szReport, szTemp);
 
		}
 
	}
 
	else if(uReportType == REPORT_DIGIT)
 
	{
 
		sprintf(szTemp, "%u", m_digest[0]);
 
		strcat(szReport, szTemp);
 

	
 
		for(i = 1; i < 20; i++)
 
		{
 
			sprintf(szTemp, " %u", m_digest[i]);
 
			strcat(szReport, szTemp);
 
		}
 
	}
 
	else strcpy(szReport, "Error: Unknown report type!");
 
}
 
#endif
 

	
 
// Get the raw message digest
 
void CSHA1::GetHash(UINT_8 *puDest)
 
{
 
	memcpy(puDest, m_digest, 20);
 
}
backends/twitter/libtwitcurl/SHA1.h
Show inline comments
 
/*
 
	100% free public domain implementation of the SHA-1 algorithm
 
	by Dominik Reichl <dominik.reichl@t-online.de>
 
	Web: http://www.dominik-reichl.de/
 
 
	Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
 
	- You can set the endianness in your files, no need to modify the
 
	  header file of the CSHA1 class any more
 
	- Aligned data support
 
	- Made support/compilation of the utility functions (ReportHash
 
	  and HashFile) optional (useful, if bytes count, for example in
 
	  embedded environments)
 
 
	Version 1.5 - 2005-01-01
 
	- 64-bit compiler compatibility added
 
	- Made variable wiping optional (define SHA1_WIPE_VARIABLES)
 
	- Removed unnecessary variable initializations
 
	- ROL32 improvement for the Microsoft compiler (using _rotl)
 
 
	======== Test Vectors (from FIPS PUB 180-1) ========
 
 
	SHA1("abc") =
 
		A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
 
 
	SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
 
		84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
 
 
	SHA1(A million repetitions of "a") =
 
		34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
 
*/
 
 
#ifndef ___SHA1_HDR___
 
#define ___SHA1_HDR___
 
 
#if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
 
#define SHA1_UTILITY_FUNCTIONS
 
#endif
 
 
#include <memory.h> // Needed for memset and memcpy
 
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
#include <stdio.h>  // Needed for file access and sprintf
 
#include <string.h> // Needed for strcat and strcpy
 
#endif
 
 
#ifdef _MSC_VER
 
#include <stdlib.h>
 
#endif
 
 
// You can define the endian mode in your files, without modifying the SHA1
 
// source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
 
// in your files, before including the SHA1.h header file. If you don't
 
// define anything, the class defaults to little endian.
 
 
#if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
 
#define SHA1_LITTLE_ENDIAN
 
#endif
 
 
// Same here. If you want variable wiping, #define SHA1_WIPE_VARIABLES, if
 
// not, #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
 
// defaults to wiping.
 
 
#if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
 
#define SHA1_WIPE_VARIABLES
 
#endif
 
 
/////////////////////////////////////////////////////////////////////////////
 
// Define 8- and 32-bit variables
 
 
#ifndef UINT_32
 
 
#ifdef _MSC_VER
 
 
#define UINT_8  unsigned __int8
 
#define UINT_32 unsigned __int32
 
 
#else
 
 
#define UINT_8 unsigned char
 
 
#if (ULONG_MAX == 0xFFFFFFFF)
 
#define UINT_32 unsigned long
 
#else
 
#define UINT_32 unsigned int
 
#endif
 
 
#endif
 
#endif
 
 
/////////////////////////////////////////////////////////////////////////////
 
// Declare SHA1 workspace
 
 
typedef union
 
{
 
	UINT_8  c[64];
 
	UINT_32 l[16];
 
} SHA1_WORKSPACE_BLOCK;
 
 
class CSHA1
 
{
 
public:
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
	// Two different formats for ReportHash(...)
 
	enum
 
	{
 
		REPORT_HEX = 0,
 
		REPORT_DIGIT = 1
 
	};
 
#endif
 
 
	// Constructor and Destructor
 
	CSHA1();
 
	~CSHA1();
 
 
	UINT_32 m_state[5];
 
	UINT_32 m_count[2];
 
	UINT_32 __reserved1[1];
 
	UINT_8  m_buffer[64];
 
	UINT_8  m_digest[20];
 
	UINT_32 __reserved2[3];
 
 
	void Reset();
 
 
	// Update the hash value
 
	void Update(UINT_8 *data, UINT_32 len);
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
	bool HashFile(char *szFileName);
 
#endif
 
 
	// Finalize hash and report
 
	void Final();
 
 
	// Report functions: as pre-formatted and raw data
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
	void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
 
#endif
 
	void GetHash(UINT_8 *puDest);
 
 
private:
 
	// Private SHA-1 transformation
 
	void Transform(UINT_32 *state, UINT_8 *buffer);
 
 
	// Member variables
 
	UINT_8 m_workspace[64];
 
	SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
 
};
 
 
#endif
 
/*
 
	100% free public domain implementation of the SHA-1 algorithm
 
	by Dominik Reichl <dominik.reichl@t-online.de>
 
	Web: http://www.dominik-reichl.de/
 

	
 
	Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
 
	- You can set the endianness in your files, no need to modify the
 
	  header file of the CSHA1 class any more
 
	- Aligned data support
 
	- Made support/compilation of the utility functions (ReportHash
 
	  and HashFile) optional (useful, if bytes count, for example in
 
	  embedded environments)
 

	
 
	Version 1.5 - 2005-01-01
 
	- 64-bit compiler compatibility added
 
	- Made variable wiping optional (define SHA1_WIPE_VARIABLES)
 
	- Removed unnecessary variable initializations
 
	- ROL32 improvement for the Microsoft compiler (using _rotl)
 

	
 
	======== Test Vectors (from FIPS PUB 180-1) ========
 

	
 
	SHA1("abc") =
 
		A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
 

	
 
	SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
 
		84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
 

	
 
	SHA1(A million repetitions of "a") =
 
		34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
 
*/
 

	
 
#ifndef ___SHA1_HDR___
 
#define ___SHA1_HDR___
 

	
 
#if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
 
#define SHA1_UTILITY_FUNCTIONS
 
#endif
 

	
 
#include <memory.h> // Needed for memset and memcpy
 

	
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
#include <stdio.h>  // Needed for file access and sprintf
 
#include <string.h> // Needed for strcat and strcpy
 
#endif
 

	
 
#ifdef _MSC_VER
 
#include <stdlib.h>
 
#endif
 

	
 
// You can define the endian mode in your files, without modifying the SHA1
 
// source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
 
// in your files, before including the SHA1.h header file. If you don't
 
// define anything, the class defaults to little endian.
 

	
 
#if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
 
#define SHA1_LITTLE_ENDIAN
 
#endif
 

	
 
// Same here. If you want variable wiping, #define SHA1_WIPE_VARIABLES, if
 
// not, #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
 
// defaults to wiping.
 

	
 
#if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
 
#define SHA1_WIPE_VARIABLES
 
#endif
 

	
 
/////////////////////////////////////////////////////////////////////////////
 
// Define 8- and 32-bit variables
 

	
 
#ifndef UINT_32
 

	
 
#ifdef _MSC_VER
 

	
 
#define UINT_8  unsigned __int8
 
#define UINT_32 unsigned __int32
 

	
 
#else
 

	
 
#define UINT_8 unsigned char
 

	
 
#if (ULONG_MAX == 0xFFFFFFFF)
 
#define UINT_32 unsigned long
 
#else
 
#define UINT_32 unsigned int
 
#endif
 

	
 
#endif
 
#endif
 

	
 
/////////////////////////////////////////////////////////////////////////////
 
// Declare SHA1 workspace
 

	
 
typedef union
 
{
 
	UINT_8  c[64];
 
	UINT_32 l[16];
 
} SHA1_WORKSPACE_BLOCK;
 

	
 
class CSHA1
 
{
 
public:
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
	// Two different formats for ReportHash(...)
 
	enum
 
	{
 
		REPORT_HEX = 0,
 
		REPORT_DIGIT = 1
 
	};
 
#endif
 

	
 
	// Constructor and Destructor
 
	CSHA1();
 
	~CSHA1();
 

	
 
	UINT_32 m_state[5];
 
	UINT_32 m_count[2];
 
	UINT_32 __reserved1[1];
 
	UINT_8  m_buffer[64];
 
	UINT_8  m_digest[20];
 
	UINT_32 __reserved2[3];
 

	
 
	void Reset();
 

	
 
	// Update the hash value
 
	void Update(UINT_8 *data, UINT_32 len);
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
	bool HashFile(char *szFileName);
 
#endif
 

	
 
	// Finalize hash and report
 
	void Final();
 

	
 
	// Report functions: as pre-formatted and raw data
 
#ifdef SHA1_UTILITY_FUNCTIONS
 
	void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
 
#endif
 
	void GetHash(UINT_8 *puDest);
 

	
 
private:
 
	// Private SHA-1 transformation
 
	void Transform(UINT_32 *state, UINT_8 *buffer);
 

	
 
	// Member variables
 
	UINT_8 m_workspace[64];
 
	SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
 
};
 

	
 
#endif
backends/twitter/libtwitcurl/base64.cpp
Show inline comments
 
/* 
 
   base64.cpp and base64.h
 
 
   Copyright (C) 2004-2008 René Nyffenegger
 
 
   This source code is provided 'as-is', without any express or implied
 
   warranty. In no event will the author 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 source code must not be misrepresented; you must not
 
      claim that you wrote the original source code. If you use this source code
 
      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 source code.
 
 
   3. This notice may not be removed or altered from any source distribution.
 
 
   René Nyffenegger rene.nyffenegger@adp-gmbh.ch
 
 
*/
 
 
#include "base64.h"
 
#include <iostream>
 
 
static const std::string base64_chars = 
 
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
             "abcdefghijklmnopqrstuvwxyz"
 
             "0123456789+/";
 
 
 
static inline bool is_base64(unsigned char c) {
 
  return (isalnum(c) || (c == '+') || (c == '/'));
 
}
 
 
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
 
  std::string ret;
 
  int i = 0;
 
  int j = 0;
 
  unsigned char char_array_3[3];
 
  unsigned char char_array_4[4];
 
 
  while (in_len--) {
 
    char_array_3[i++] = *(bytes_to_encode++);
 
    if (i == 3) {
 
      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
 
      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
 
      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
 
      char_array_4[3] = char_array_3[2] & 0x3f;
 
 
      for(i = 0; (i <4) ; i++)
 
        ret += base64_chars[char_array_4[i]];
 
      i = 0;
 
    }
 
  }
 
 
  if (i)
 
  {
 
    for(j = i; j < 3; j++)
 
      char_array_3[j] = '\0';
 
 
    char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
 
    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
 
    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
 
    char_array_4[3] = char_array_3[2] & 0x3f;
 
 
    for (j = 0; (j < i + 1); j++)
 
      ret += base64_chars[char_array_4[j]];
 
 
    while((i++ < 3))
 
      ret += '=';
 
 
  }
 
 
  return ret;
 
 
}
 
 
std::string base64_decode(std::string const& encoded_string) {
 
  int in_len = encoded_string.size();
 
  int i = 0;
 
  int j = 0;
 
  int in_ = 0;
 
  unsigned char char_array_4[4], char_array_3[3];
 
  std::string ret;
 
 
  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
 
    char_array_4[i++] = encoded_string[in_]; in_++;
 
    if (i ==4) {
 
      for (i = 0; i <4; i++)
 
        char_array_4[i] = base64_chars.find(char_array_4[i]);
 
 
      char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
 
      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
 
      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
 
 
      for (i = 0; (i < 3); i++)
 
        ret += char_array_3[i];
 
      i = 0;
 
    }
 
  }
 
 
  if (i) {
 
    for (j = i; j <4; j++)
 
      char_array_4[j] = 0;
 
 
    for (j = 0; j <4; j++)
 
      char_array_4[j] = base64_chars.find(char_array_4[j]);
 
 
    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
 
    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
 
    char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
 
 
    for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
 
  }
 
 
  return ret;
 
/* 
 
   base64.cpp and base64.h
 

	
 
   Copyright (C) 2004-2008 René Nyffenegger
 

	
 
   This source code is provided 'as-is', without any express or implied
 
   warranty. In no event will the author 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 source code must not be misrepresented; you must not
 
      claim that you wrote the original source code. If you use this source code
 
      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 source code.
 

	
 
   3. This notice may not be removed or altered from any source distribution.
 

	
 
   René Nyffenegger rene.nyffenegger@adp-gmbh.ch
 

	
 
*/
 

	
 
#include "base64.h"
 
#include <iostream>
 

	
 
static const std::string base64_chars = 
 
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
             "abcdefghijklmnopqrstuvwxyz"
 
             "0123456789+/";
 

	
 

	
 
static inline bool is_base64(unsigned char c) {
 
  return (isalnum(c) || (c == '+') || (c == '/'));
 
}
 

	
 
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
 
  std::string ret;
 
  int i = 0;
 
  int j = 0;
 
  unsigned char char_array_3[3];
 
  unsigned char char_array_4[4];
 

	
 
  while (in_len--) {
 
    char_array_3[i++] = *(bytes_to_encode++);
 
    if (i == 3) {
 
      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
 
      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
 
      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
 
      char_array_4[3] = char_array_3[2] & 0x3f;
 

	
 
      for(i = 0; (i <4) ; i++)
 
        ret += base64_chars[char_array_4[i]];
 
      i = 0;
 
    }
 
  }
 

	
 
  if (i)
 
  {
 
    for(j = i; j < 3; j++)
 
      char_array_3[j] = '\0';
 

	
 
    char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
 
    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
 
    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
 
    char_array_4[3] = char_array_3[2] & 0x3f;
 

	
 
    for (j = 0; (j < i + 1); j++)
 
      ret += base64_chars[char_array_4[j]];
 

	
 
    while((i++ < 3))
 
      ret += '=';
 

	
 
  }
 

	
 
  return ret;
 

	
 
}
 

	
 
std::string base64_decode(std::string const& encoded_string) {
 
  int in_len = encoded_string.size();
 
  int i = 0;
 
  int j = 0;
 
  int in_ = 0;
 
  unsigned char char_array_4[4], char_array_3[3];
 
  std::string ret;
 

	
 
  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
 
    char_array_4[i++] = encoded_string[in_]; in_++;
 
    if (i ==4) {
 
      for (i = 0; i <4; i++)
 
        char_array_4[i] = base64_chars.find(char_array_4[i]);
 

	
 
      char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
 
      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
 
      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
 

	
 
      for (i = 0; (i < 3); i++)
 
        ret += char_array_3[i];
 
      i = 0;
 
    }
 
  }
 

	
 
  if (i) {
 
    for (j = i; j <4; j++)
 
      char_array_4[j] = 0;
 

	
 
    for (j = 0; j <4; j++)
 
      char_array_4[j] = base64_chars.find(char_array_4[j]);
 

	
 
    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
 
    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
 
    char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
 

	
 
    for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
 
  }
 

	
 
  return ret;
 
}
 
\ No newline at end of file
backends/twitter/libtwitcurl/base64.h
Show inline comments
 
#include <string>
 
 
std::string base64_encode(unsigned char const* , unsigned int len);
 
#include <string>
 

	
 
std::string base64_encode(unsigned char const* , unsigned int len);
 
std::string base64_decode(std::string const& s);
 
\ No newline at end of file
backends/twitter/libtwitcurl/oauthlib.cpp
Show inline comments
 
#include "twitcurlurls.h"
 
#include "oauthlib.h"
 
#include "HMAC_SHA1.h"
 
#include "base64.h"
 
#include "urlencode.h"
 
 
/*++
 
* @method: oAuth::oAuth
 
*
 
* @description: constructor
 
*
 
* @input: none
 
*
 
* @output: none
 
*
 
*--*/
 
oAuth::oAuth()
 
{
 
}
 
 
/*++
 
* @method: oAuth::~oAuth
 
*
 
* @description: destructor
 
*
 
* @input: none
 
*
 
* @output: none
 
*
 
*--*/
 
oAuth::~oAuth()
 
{
 
}
 
 
/*++
 
* @method: oAuth::clone
 
*
 
* @description: creates a clone of oAuth object
 
*
 
* @input: none
 
*
 
* @output: cloned oAuth object
 
*
 
*--*/
 
oAuth oAuth::clone()
 
{
 
    oAuth cloneObj;
 
    cloneObj.m_consumerKey = m_consumerKey;
 
    cloneObj.m_consumerSecret = m_consumerSecret;
 
    cloneObj.m_oAuthTokenKey = m_oAuthTokenKey;
 
    cloneObj.m_oAuthTokenSecret = m_oAuthTokenSecret;
 
    cloneObj.m_oAuthPin = m_oAuthPin;
 
    cloneObj.m_nonce = m_nonce;
 
    cloneObj.m_timeStamp = m_timeStamp;
 
    cloneObj.m_oAuthScreenName =  m_oAuthScreenName;
 
    return cloneObj;
 
}
 
 
 
/*++
 
* @method: oAuth::getConsumerKey
 
*
 
* @description: this method gives consumer key that is being used currently
 
*
 
* @input: none
 
*
 
* @output: consumer key
 
*
 
*--*/
 
void oAuth::getConsumerKey( std::string& consumerKey )
 
{
 
    consumerKey = m_consumerKey;
 
}
 
 
/*++
 
* @method: oAuth::setConsumerKey
 
*
 
* @description: this method saves consumer key that should be used
 
*
 
* @input: consumer key
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setConsumerKey( const std::string& consumerKey )
 
{
 
    m_consumerKey.assign( consumerKey );
 
}
 
 
/*++
 
* @method: oAuth::getConsumerSecret
 
*
 
* @description: this method gives consumer secret that is being used currently
 
*
 
* @input: none
 
*
 
* @output: consumer secret
 
*
 
*--*/
 
void oAuth::getConsumerSecret( std::string& consumerSecret )
 
{
 
    consumerSecret = m_consumerSecret;
 
}
 
 
/*++
 
* @method: oAuth::setConsumerSecret
 
*
 
* @description: this method saves consumer secret that should be used
 
*
 
* @input: consumer secret
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setConsumerSecret( const std::string& consumerSecret )
 
{
 
    m_consumerSecret = consumerSecret;
 
}
 
 
/*++
 
* @method: oAuth::getOAuthTokenKey
 
*
 
* @description: this method gives OAuth token (also called access token) that is being used currently
 
*
 
* @input: none
 
*
 
* @output: OAuth token
 
*
 
*--*/
 
void oAuth::getOAuthTokenKey( std::string& oAuthTokenKey )
 
{
 
    oAuthTokenKey = m_oAuthTokenKey;
 
}
 
 
/*++
 
* @method: oAuth::setOAuthTokenKey
 
*
 
* @description: this method saves OAuth token that should be used
 
*
 
* @input: OAuth token
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setOAuthTokenKey( const std::string& oAuthTokenKey )
 
{
 
    m_oAuthTokenKey = oAuthTokenKey;
 
}
 
 
/*++
 
* @method: oAuth::getOAuthTokenSecret
 
*
 
* @description: this method gives OAuth token secret that is being used currently
 
*
 
* @input: none
 
*
 
* @output: OAuth token secret
 
*
 
*--*/
 
void oAuth::getOAuthTokenSecret( std::string& oAuthTokenSecret )
 
{
 
    oAuthTokenSecret = m_oAuthTokenSecret;
 
}
 
 
/*++
 
* @method: oAuth::setOAuthTokenSecret
 
*
 
* @description: this method saves OAuth token that should be used
 
*
 
* @input: OAuth token secret
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setOAuthTokenSecret( const std::string& oAuthTokenSecret )
 
{
 
    m_oAuthTokenSecret = oAuthTokenSecret;
 
}
 
 
/*++
 
* @method: oAuth::getOAuthScreenName
 
*
 
* @description: this method gives authorized user's screenname
 
*
 
* @input: none
 
*
 
* @output: screen name
 
*
 
*--*/
 
void oAuth::getOAuthScreenName( std::string& oAuthScreenName )
 
{
 
    oAuthScreenName = m_oAuthScreenName;
 
}
 
 
/*++
 
* @method: oAuth::setOAuthScreenName
 
*
 
* @description: this method sets authorized user's screenname
 
*
 
* @input: screen name
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setOAuthScreenName( const std::string& oAuthScreenName )
 
{
 
    m_oAuthScreenName = oAuthScreenName;
 
}
 
 
/*++
 
* @method: oAuth::getOAuthPin
 
*
 
* @description: this method gives OAuth verifier PIN
 
*
 
* @input: none
 
*
 
* @output: OAuth verifier PIN
 
*
 
*--*/
 
void oAuth::getOAuthPin( std::string& oAuthPin )
 
{
 
    oAuthPin = m_oAuthPin;
 
}
 
 
/*++
 
* @method: oAuth::setOAuthPin
 
*
 
* @description: this method sets OAuth verifier PIN
 
*
 
* @input: OAuth verifier PIN
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setOAuthPin( const std::string& oAuthPin )
 
{
 
    m_oAuthPin = oAuthPin;
 
}
 
 
/*++
 
* @method: oAuth::generateNonceTimeStamp
 
*
 
* @description: this method generates nonce and timestamp for OAuth header
 
*
 
* @input: none
 
*
 
* @output: none
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
void oAuth::generateNonceTimeStamp()
 
{
 
    char szTime[oAuthLibDefaults::OAUTHLIB_BUFFSIZE];
 
    char szRand[oAuthLibDefaults::OAUTHLIB_BUFFSIZE];
 
    memset( szTime, 0, oAuthLibDefaults::OAUTHLIB_BUFFSIZE );
 
    memset( szRand, 0, oAuthLibDefaults::OAUTHLIB_BUFFSIZE );
 
    srand( (unsigned int)time( NULL ) );
 
    sprintf( szRand, "%x", rand()%1000 );
 
    sprintf( szTime, "%ld", time( NULL ) );
 
 
    m_nonce.assign( szTime );
 
    m_nonce.append( szRand );
 
    m_timeStamp.assign( szTime );
 
}
 
 
/*++
 
* @method: oAuth::buildOAuthRawDataKeyValPairs
 
*
 
* @description: this method prepares key-value pairs from the data part of the URL
 
*               or from the URL post fields data, as required by OAuth header
 
*               and signature generation.
 
*
 
* @input: rawData - Raw data either from the URL itself or from post fields.
 
*                   Should already be url encoded.
 
*         urlencodeData - If true, string will be urlencoded before converting
 
*                         to key value pairs.
 
*
 
* @output: rawDataKeyValuePairs - Map in which key-value pairs are populated
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
void oAuth::buildOAuthRawDataKeyValPairs( const std::string& rawData,
 
                                          bool urlencodeData,
 
                                          oAuthKeyValuePairs& rawDataKeyValuePairs )
 
{
 
    /* Raw data if it's present. Data should already be urlencoded once */
 
    if( rawData.empty() )
 
    {
 
        return;
 
    }
 
 
    size_t nSep = std::string::npos;
 
    size_t nPos = std::string::npos;
 
    std::string dataKeyVal;
 
    std::string dataKey;
 
    std::string dataVal;
 
 
    /* This raw data part can contain many key value pairs: key1=value1&key2=value2&key3=value3 */
 
    std::string dataPart = rawData;
 
    while( std::string::npos != ( nSep = dataPart.find_first_of("&") ) )
 
    {
 
        /* Extract first key=value pair */
 
        dataKeyVal = dataPart.substr( 0, nSep );
 
 
        /* Split them */
 
        nPos = dataKeyVal.find_first_of( "=" );
 
        if( std::string::npos != nPos )
 
        {
 
            dataKey = dataKeyVal.substr( 0, nPos );
 
            dataVal = dataKeyVal.substr( nPos + 1 );
 
 
            /* Put this key=value pair in map */
 
            rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal;
 
        }
 
        dataPart = dataPart.substr( nSep + 1 );
 
    }
 
 
    /* For the last key=value */
 
    dataKeyVal = dataPart.substr( 0, nSep );
 
 
    /* Split them */
 
    nPos = dataKeyVal.find_first_of( "=" );
 
    if( std::string::npos != nPos )
 
    {
 
        dataKey = dataKeyVal.substr( 0, nPos );
 
        dataVal = dataKeyVal.substr( nPos + 1 );
 
 
        /* Put this key=value pair in map */
 
        rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal;
 
    }
 
}
 
 
/*++
 
* @method: oAuth::buildOAuthTokenKeyValuePairs
 
*
 
* @description: this method prepares key-value pairs required for OAuth header
 
*               and signature generation.
 
*
 
* @input: includeOAuthVerifierPin - flag to indicate whether oauth_verifer key-value
 
*                                   pair needs to be included. oauth_verifer is only
 
*                                   used during exchanging request token with access token.
 
*         oauthSignature - base64 and url encoded OAuth signature.
 
*         generateTimestamp - If true, then generate new timestamp for nonce.
 
*
 
* @output: keyValueMap - map in which key-value pairs are populated
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
bool oAuth::buildOAuthTokenKeyValuePairs( const bool includeOAuthVerifierPin,
 
                                          const std::string& oauthSignature,
 
                                          oAuthKeyValuePairs& keyValueMap,
 
                                          const bool generateTimestamp )
 
{
 
    /* Generate nonce and timestamp if required */
 
    if( generateTimestamp )
 
    {
 
        generateNonceTimeStamp();
 
    }
 
 
    /* Consumer key and its value */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_CONSUMERKEY_KEY] = m_consumerKey;
 
 
    /* Nonce key and its value */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_NONCE_KEY] = m_nonce;
 
 
    /* Signature if supplied */
 
    if( oauthSignature.length() )
 
    {
 
        keyValueMap[oAuthLibDefaults::OAUTHLIB_SIGNATURE_KEY] = oauthSignature;
 
    }
 
 
    /* Signature method, only HMAC-SHA1 as of now */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_SIGNATUREMETHOD_KEY] = std::string( "HMAC-SHA1" );
 
 
    /* Timestamp */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_TIMESTAMP_KEY] = m_timeStamp;
 
 
    /* Token */
 
    if( m_oAuthTokenKey.length() )
 
    {
 
        keyValueMap[oAuthLibDefaults::OAUTHLIB_TOKEN_KEY] = m_oAuthTokenKey;
 
    }
 
 
    /* Verifier */
 
    if( includeOAuthVerifierPin && m_oAuthPin.length() )
 
    {
 
        keyValueMap[oAuthLibDefaults::OAUTHLIB_VERIFIER_KEY] = m_oAuthPin;
 
    }
 
 
    /* Version */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_VERSION_KEY] = std::string( "1.0" );
 
 
    return !keyValueMap.empty();
 
}
 
 
/*++
 
* @method: oAuth::getSignature
 
*
 
* @description: this method calculates HMAC-SHA1 signature of OAuth header
 
*
 
* @input: eType - HTTP request type
 
*         rawUrl - raw url of the HTTP request
 
*         rawKeyValuePairs - key-value pairs containing OAuth headers and HTTP data
 
*
 
* @output: oAuthSignature - base64 and url encoded signature
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
bool oAuth::getSignature( const eOAuthHttpRequestType eType,
 
                          const std::string& rawUrl,
 
                          const oAuthKeyValuePairs& rawKeyValuePairs,
 
                          std::string& oAuthSignature )
 
{
 
    std::string rawParams;
 
    std::string paramsSeperator;
 
    std::string sigBase;
 
 
    /* Initially empty signature */
 
    oAuthSignature = "";
 
 
    /* Build a string using key-value pairs */
 
    paramsSeperator = "&";
 
    getStringFromOAuthKeyValuePairs( rawKeyValuePairs, rawParams, paramsSeperator );
 
 
    /* Start constructing base signature string. Refer http://dev.twitter.com/auth#intro */
 
    switch( eType )
 
    {
 
    case eOAuthHttpGet:
 
        {
 
            sigBase.assign( "GET&" );
 
        }
 
        break;
 
 
    case eOAuthHttpPost:
 
        {
 
            sigBase.assign( "POST&" );
 
        }
 
        break;
 
 
    case eOAuthHttpDelete:
 
        {
 
            sigBase.assign( "DELETE&" );
 
        }
 
        break;
 
 
    default:
 
        {
 
            return false;
 
        }
 
        break;
 
    }
 
    sigBase.append( urlencode( rawUrl ) );
 
    sigBase.append( "&" );
 
    sigBase.append( urlencode( rawParams ) );
 
 
    /* Now, hash the signature base string using HMAC_SHA1 class */
 
    CHMAC_SHA1 objHMACSHA1;
 
    std::string secretSigningKey;
 
    unsigned char strDigest[oAuthLibDefaults::OAUTHLIB_BUFFSIZE_LARGE];
 
 
    memset( strDigest, 0, oAuthLibDefaults::OAUTHLIB_BUFFSIZE_LARGE );
 
 
    /* Signing key is composed of consumer_secret&token_secret */
 
    secretSigningKey.assign( m_consumerSecret );
 
    secretSigningKey.append( "&" );
 
    if( m_oAuthTokenSecret.length() )
 
    {
 
        secretSigningKey.append( m_oAuthTokenSecret );
 
    }
 
  
 
    objHMACSHA1.HMAC_SHA1( (unsigned char*)sigBase.c_str(),
 
                           sigBase.length(),
 
                           (unsigned char*)secretSigningKey.c_str(),
 
                           secretSigningKey.length(),
 
                           strDigest ); 
 
 
    /* Do a base64 encode of signature */
 
    std::string base64Str = base64_encode( strDigest, 20 /* SHA 1 digest is 160 bits */ );
 
 
    /* Do an url encode */
 
    oAuthSignature = urlencode( base64Str );
 
 
    return !oAuthSignature.empty();
 
}
 
 
/*++
 
* @method: oAuth::getOAuthHeader
 
*
 
* @description: this method builds OAuth header that should be used in HTTP requests to twitter
 
*
 
* @input: eType - HTTP request type
 
*         rawUrl - raw url of the HTTP request
 
*         rawData - HTTP data (post fields)
 
*         includeOAuthVerifierPin - flag to indicate whether or not oauth_verifier needs to included
 
*                                   in OAuth header
 
*
 
* @output: oAuthHttpHeader - OAuth header
 
*
 
*--*/
 
bool oAuth::getOAuthHeader( const eOAuthHttpRequestType eType,
 
                            const std::string& rawUrl,
 
                            const std::string& rawData,
 
                            std::string& oAuthHttpHeader,
 
                            const bool includeOAuthVerifierPin )
 
{
 
    oAuthKeyValuePairs rawKeyValuePairs;
 
    std::string rawParams;
 
    std::string oauthSignature;
 
    std::string paramsSeperator;
 
    std::string pureUrl( rawUrl );
 
 
    /* Clear header string initially */
 
    oAuthHttpHeader = "";
 
    rawKeyValuePairs.clear();
 
 
    /* If URL itself contains ?key=value, then extract and put them in map */
 
    size_t nPos = rawUrl.find_first_of( "?" );
 
    if( std::string::npos != nPos )
 
    {
 
        /* Get only URL */
 
        pureUrl = rawUrl.substr( 0, nPos );
 
 
        /* Get only key=value data part */
 
        std::string dataPart = rawUrl.substr( nPos + 1 );
 
 
        /* Split the data in URL as key=value pairs */
 
        buildOAuthRawDataKeyValPairs( dataPart, true, rawKeyValuePairs );
 
    }
 
 
    /* Split the raw data if it's present, as key=value pairs. Data should already be urlencoded once */
 
    buildOAuthRawDataKeyValPairs( rawData, false, rawKeyValuePairs );
 
 
    /* Build key-value pairs needed for OAuth request token, without signature */
 
    buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin, std::string( "" ), rawKeyValuePairs, true );
 
 
    /* Get url encoded base64 signature using request type, url and parameters */
 
    getSignature( eType, pureUrl, rawKeyValuePairs, oauthSignature );
 
 
    /* Clear map so that the parameters themselves are not sent along with the OAuth values */
 
    rawKeyValuePairs.clear();
 
 
    /* Now, again build key-value pairs with signature this time */
 
    buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin, oauthSignature, rawKeyValuePairs, false );
 
 
    /* Get OAuth header in string format */
 
    paramsSeperator = ",";
 
    getStringFromOAuthKeyValuePairs( rawKeyValuePairs, rawParams, paramsSeperator );
 
 
    /* Build authorization header */
 
    oAuthHttpHeader.assign( oAuthLibDefaults::OAUTHLIB_AUTHHEADER_STRING );
 
    oAuthHttpHeader.append( rawParams );
 
 
    return !oAuthHttpHeader.empty();
 
}
 
 
/*++
 
* @method: oAuth::getStringFromOAuthKeyValuePairs
 
*
 
* @description: this method builds a sorted string from key-value pairs
 
*
 
* @input: rawParamMap - key-value pairs map
 
*         paramsSeperator - sepearator, either & or ,
 
*
 
* @output: rawParams - sorted string of OAuth parameters
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
bool oAuth::getStringFromOAuthKeyValuePairs( const oAuthKeyValuePairs& rawParamMap,
 
                                             std::string& rawParams,
 
                                             const std::string& paramsSeperator )
 
{
 
    rawParams = "";
 
    if( rawParamMap.empty() )
 
    {
 
        return false;
 
    }
 
 
    oAuthKeyValueList keyValueList;
 
    std::string dummyStr;
 
 
    /* Push key-value pairs to a list of strings */
 
    keyValueList.clear();
 
    oAuthKeyValuePairs::const_iterator itMap = rawParamMap.begin();
 
    for( ; itMap != rawParamMap.end(); itMap++ )
 
    {
 
        dummyStr.assign( itMap->first );
 
        dummyStr.append( "=" );
 
        if( paramsSeperator == "," )
 
        {
 
            dummyStr.append( "\"" );
 
        }
 
        dummyStr.append( itMap->second );
 
        if( paramsSeperator == "," )
 
        {
 
            dummyStr.append( "\"" );
 
        }
 
        keyValueList.push_back( dummyStr );
 
    }
 
 
    /* Sort key-value pairs based on key name */
 
    keyValueList.sort();
 
 
    /* Now, form a string */
 
    dummyStr = "";
 
    oAuthKeyValueList::iterator itKeyValue = keyValueList.begin();
 
    for( ; itKeyValue != keyValueList.end(); itKeyValue++ )
 
    {
 
        if( dummyStr.length() )
 
        {
 
            dummyStr.append( paramsSeperator );
 
         }
 
         dummyStr.append( itKeyValue->c_str() );
 
    }
 
    rawParams = dummyStr;
 
    return !rawParams.empty();
 
}
 
 
/*++
 
* @method: oAuth::extractOAuthTokenKeySecret
 
*
 
* @description: this method extracts oauth token key and secret from
 
*               twitter's HTTP response
 
*
 
* @input: requestTokenResponse - response from twitter
 
*
 
* @output: none
 
*
 
*--*/
 
bool oAuth::extractOAuthTokenKeySecret( const std::string& requestTokenResponse )
 
{
 
    if( requestTokenResponse.empty() )
 
    {
 
        return false;
 
    }
 
 
    size_t nPos = std::string::npos;
 
    std::string strDummy;
 
 
    /* Get oauth_token key */
 
    nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_TOKEN_KEY );
 
    if( std::string::npos != nPos )
 
    {
 
        nPos = nPos + oAuthLibDefaults::OAUTHLIB_TOKEN_KEY.length() + strlen( "=" );
 
        strDummy = requestTokenResponse.substr( nPos );
 
        nPos = strDummy.find( "&" );
 
        if( std::string::npos != nPos )
 
        {
 
            m_oAuthTokenKey = strDummy.substr( 0, nPos );
 
        }
 
    }
 
 
    /* Get oauth_token_secret */
 
    nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_TOKENSECRET_KEY );
 
    if( std::string::npos != nPos )
 
    {
 
        nPos = nPos + oAuthLibDefaults::OAUTHLIB_TOKENSECRET_KEY.length() + strlen( "=" );
 
        strDummy = requestTokenResponse.substr( nPos );
 
        nPos = strDummy.find( "&" );
 
        if( std::string::npos != nPos )
 
        {
 
            m_oAuthTokenSecret = strDummy.substr( 0, nPos );
 
        }
 
    }
 
 
    /* Get screen_name */
 
    nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_SCREENNAME_KEY );
 
    if( std::string::npos != nPos )
 
    {
 
        nPos = nPos + oAuthLibDefaults::OAUTHLIB_SCREENNAME_KEY.length() + strlen( "=" );
 
        strDummy = requestTokenResponse.substr( nPos );
 
        m_oAuthScreenName = strDummy;
 
    }
 
 
    return true;
 
}
 
 
#include "twitcurlurls.h"
 
#include "oauthlib.h"
 
#include "HMAC_SHA1.h"
 
#include "base64.h"
 
#include "urlencode.h"
 

	
 
/*++
 
* @method: oAuth::oAuth
 
*
 
* @description: constructor
 
*
 
* @input: none
 
*
 
* @output: none
 
*
 
*--*/
 
oAuth::oAuth()
 
{
 
}
 

	
 
/*++
 
* @method: oAuth::~oAuth
 
*
 
* @description: destructor
 
*
 
* @input: none
 
*
 
* @output: none
 
*
 
*--*/
 
oAuth::~oAuth()
 
{
 
}
 

	
 
/*++
 
* @method: oAuth::clone
 
*
 
* @description: creates a clone of oAuth object
 
*
 
* @input: none
 
*
 
* @output: cloned oAuth object
 
*
 
*--*/
 
oAuth oAuth::clone()
 
{
 
    oAuth cloneObj;
 
    cloneObj.m_consumerKey = m_consumerKey;
 
    cloneObj.m_consumerSecret = m_consumerSecret;
 
    cloneObj.m_oAuthTokenKey = m_oAuthTokenKey;
 
    cloneObj.m_oAuthTokenSecret = m_oAuthTokenSecret;
 
    cloneObj.m_oAuthPin = m_oAuthPin;
 
    cloneObj.m_nonce = m_nonce;
 
    cloneObj.m_timeStamp = m_timeStamp;
 
    cloneObj.m_oAuthScreenName =  m_oAuthScreenName;
 
    return cloneObj;
 
}
 

	
 

	
 
/*++
 
* @method: oAuth::getConsumerKey
 
*
 
* @description: this method gives consumer key that is being used currently
 
*
 
* @input: none
 
*
 
* @output: consumer key
 
*
 
*--*/
 
void oAuth::getConsumerKey( std::string& consumerKey )
 
{
 
    consumerKey = m_consumerKey;
 
}
 

	
 
/*++
 
* @method: oAuth::setConsumerKey
 
*
 
* @description: this method saves consumer key that should be used
 
*
 
* @input: consumer key
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setConsumerKey( const std::string& consumerKey )
 
{
 
    m_consumerKey.assign( consumerKey );
 
}
 

	
 
/*++
 
* @method: oAuth::getConsumerSecret
 
*
 
* @description: this method gives consumer secret that is being used currently
 
*
 
* @input: none
 
*
 
* @output: consumer secret
 
*
 
*--*/
 
void oAuth::getConsumerSecret( std::string& consumerSecret )
 
{
 
    consumerSecret = m_consumerSecret;
 
}
 

	
 
/*++
 
* @method: oAuth::setConsumerSecret
 
*
 
* @description: this method saves consumer secret that should be used
 
*
 
* @input: consumer secret
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setConsumerSecret( const std::string& consumerSecret )
 
{
 
    m_consumerSecret = consumerSecret;
 
}
 

	
 
/*++
 
* @method: oAuth::getOAuthTokenKey
 
*
 
* @description: this method gives OAuth token (also called access token) that is being used currently
 
*
 
* @input: none
 
*
 
* @output: OAuth token
 
*
 
*--*/
 
void oAuth::getOAuthTokenKey( std::string& oAuthTokenKey )
 
{
 
    oAuthTokenKey = m_oAuthTokenKey;
 
}
 

	
 
/*++
 
* @method: oAuth::setOAuthTokenKey
 
*
 
* @description: this method saves OAuth token that should be used
 
*
 
* @input: OAuth token
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setOAuthTokenKey( const std::string& oAuthTokenKey )
 
{
 
    m_oAuthTokenKey = oAuthTokenKey;
 
}
 

	
 
/*++
 
* @method: oAuth::getOAuthTokenSecret
 
*
 
* @description: this method gives OAuth token secret that is being used currently
 
*
 
* @input: none
 
*
 
* @output: OAuth token secret
 
*
 
*--*/
 
void oAuth::getOAuthTokenSecret( std::string& oAuthTokenSecret )
 
{
 
    oAuthTokenSecret = m_oAuthTokenSecret;
 
}
 

	
 
/*++
 
* @method: oAuth::setOAuthTokenSecret
 
*
 
* @description: this method saves OAuth token that should be used
 
*
 
* @input: OAuth token secret
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setOAuthTokenSecret( const std::string& oAuthTokenSecret )
 
{
 
    m_oAuthTokenSecret = oAuthTokenSecret;
 
}
 

	
 
/*++
 
* @method: oAuth::getOAuthScreenName
 
*
 
* @description: this method gives authorized user's screenname
 
*
 
* @input: none
 
*
 
* @output: screen name
 
*
 
*--*/
 
void oAuth::getOAuthScreenName( std::string& oAuthScreenName )
 
{
 
    oAuthScreenName = m_oAuthScreenName;
 
}
 

	
 
/*++
 
* @method: oAuth::setOAuthScreenName
 
*
 
* @description: this method sets authorized user's screenname
 
*
 
* @input: screen name
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setOAuthScreenName( const std::string& oAuthScreenName )
 
{
 
    m_oAuthScreenName = oAuthScreenName;
 
}
 

	
 
/*++
 
* @method: oAuth::getOAuthPin
 
*
 
* @description: this method gives OAuth verifier PIN
 
*
 
* @input: none
 
*
 
* @output: OAuth verifier PIN
 
*
 
*--*/
 
void oAuth::getOAuthPin( std::string& oAuthPin )
 
{
 
    oAuthPin = m_oAuthPin;
 
}
 

	
 
/*++
 
* @method: oAuth::setOAuthPin
 
*
 
* @description: this method sets OAuth verifier PIN
 
*
 
* @input: OAuth verifier PIN
 
*
 
* @output: none
 
*
 
*--*/
 
void oAuth::setOAuthPin( const std::string& oAuthPin )
 
{
 
    m_oAuthPin = oAuthPin;
 
}
 

	
 
/*++
 
* @method: oAuth::generateNonceTimeStamp
 
*
 
* @description: this method generates nonce and timestamp for OAuth header
 
*
 
* @input: none
 
*
 
* @output: none
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
void oAuth::generateNonceTimeStamp()
 
{
 
    char szTime[oAuthLibDefaults::OAUTHLIB_BUFFSIZE];
 
    char szRand[oAuthLibDefaults::OAUTHLIB_BUFFSIZE];
 
    memset( szTime, 0, oAuthLibDefaults::OAUTHLIB_BUFFSIZE );
 
    memset( szRand, 0, oAuthLibDefaults::OAUTHLIB_BUFFSIZE );
 
    srand( (unsigned int)time( NULL ) );
 
    sprintf( szRand, "%x", rand()%1000 );
 
    sprintf( szTime, "%ld", time( NULL ) );
 

	
 
    m_nonce.assign( szTime );
 
    m_nonce.append( szRand );
 
    m_timeStamp.assign( szTime );
 
}
 

	
 
/*++
 
* @method: oAuth::buildOAuthRawDataKeyValPairs
 
*
 
* @description: this method prepares key-value pairs from the data part of the URL
 
*               or from the URL post fields data, as required by OAuth header
 
*               and signature generation.
 
*
 
* @input: rawData - Raw data either from the URL itself or from post fields.
 
*                   Should already be url encoded.
 
*         urlencodeData - If true, string will be urlencoded before converting
 
*                         to key value pairs.
 
*
 
* @output: rawDataKeyValuePairs - Map in which key-value pairs are populated
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
void oAuth::buildOAuthRawDataKeyValPairs( const std::string& rawData,
 
                                          bool urlencodeData,
 
                                          oAuthKeyValuePairs& rawDataKeyValuePairs )
 
{
 
    /* Raw data if it's present. Data should already be urlencoded once */
 
    if( rawData.empty() )
 
    {
 
        return;
 
    }
 

	
 
    size_t nSep = std::string::npos;
 
    size_t nPos = std::string::npos;
 
    std::string dataKeyVal;
 
    std::string dataKey;
 
    std::string dataVal;
 

	
 
    /* This raw data part can contain many key value pairs: key1=value1&key2=value2&key3=value3 */
 
    std::string dataPart = rawData;
 
    while( std::string::npos != ( nSep = dataPart.find_first_of("&") ) )
 
    {
 
        /* Extract first key=value pair */
 
        dataKeyVal = dataPart.substr( 0, nSep );
 

	
 
        /* Split them */
 
        nPos = dataKeyVal.find_first_of( "=" );
 
        if( std::string::npos != nPos )
 
        {
 
            dataKey = dataKeyVal.substr( 0, nPos );
 
            dataVal = dataKeyVal.substr( nPos + 1 );
 

	
 
            /* Put this key=value pair in map */
 
            rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal;
 
        }
 
        dataPart = dataPart.substr( nSep + 1 );
 
    }
 

	
 
    /* For the last key=value */
 
    dataKeyVal = dataPart.substr( 0, nSep );
 

	
 
    /* Split them */
 
    nPos = dataKeyVal.find_first_of( "=" );
 
    if( std::string::npos != nPos )
 
    {
 
        dataKey = dataKeyVal.substr( 0, nPos );
 
        dataVal = dataKeyVal.substr( nPos + 1 );
 

	
 
        /* Put this key=value pair in map */
 
        rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal;
 
    }
 
}
 

	
 
/*++
 
* @method: oAuth::buildOAuthTokenKeyValuePairs
 
*
 
* @description: this method prepares key-value pairs required for OAuth header
 
*               and signature generation.
 
*
 
* @input: includeOAuthVerifierPin - flag to indicate whether oauth_verifer key-value
 
*                                   pair needs to be included. oauth_verifer is only
 
*                                   used during exchanging request token with access token.
 
*         oauthSignature - base64 and url encoded OAuth signature.
 
*         generateTimestamp - If true, then generate new timestamp for nonce.
 
*
 
* @output: keyValueMap - map in which key-value pairs are populated
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
bool oAuth::buildOAuthTokenKeyValuePairs( const bool includeOAuthVerifierPin,
 
                                          const std::string& oauthSignature,
 
                                          oAuthKeyValuePairs& keyValueMap,
 
                                          const bool generateTimestamp )
 
{
 
    /* Generate nonce and timestamp if required */
 
    if( generateTimestamp )
 
    {
 
        generateNonceTimeStamp();
 
    }
 

	
 
    /* Consumer key and its value */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_CONSUMERKEY_KEY] = m_consumerKey;
 

	
 
    /* Nonce key and its value */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_NONCE_KEY] = m_nonce;
 

	
 
    /* Signature if supplied */
 
    if( oauthSignature.length() )
 
    {
 
        keyValueMap[oAuthLibDefaults::OAUTHLIB_SIGNATURE_KEY] = oauthSignature;
 
    }
 

	
 
    /* Signature method, only HMAC-SHA1 as of now */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_SIGNATUREMETHOD_KEY] = std::string( "HMAC-SHA1" );
 

	
 
    /* Timestamp */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_TIMESTAMP_KEY] = m_timeStamp;
 

	
 
    /* Token */
 
    if( m_oAuthTokenKey.length() )
 
    {
 
        keyValueMap[oAuthLibDefaults::OAUTHLIB_TOKEN_KEY] = m_oAuthTokenKey;
 
    }
 

	
 
    /* Verifier */
 
    if( includeOAuthVerifierPin && m_oAuthPin.length() )
 
    {
 
        keyValueMap[oAuthLibDefaults::OAUTHLIB_VERIFIER_KEY] = m_oAuthPin;
 
    }
 

	
 
    /* Version */
 
    keyValueMap[oAuthLibDefaults::OAUTHLIB_VERSION_KEY] = std::string( "1.0" );
 

	
 
    return !keyValueMap.empty();
 
}
 

	
 
/*++
 
* @method: oAuth::getSignature
 
*
 
* @description: this method calculates HMAC-SHA1 signature of OAuth header
 
*
 
* @input: eType - HTTP request type
 
*         rawUrl - raw url of the HTTP request
 
*         rawKeyValuePairs - key-value pairs containing OAuth headers and HTTP data
 
*
 
* @output: oAuthSignature - base64 and url encoded signature
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
bool oAuth::getSignature( const eOAuthHttpRequestType eType,
 
                          const std::string& rawUrl,
 
                          const oAuthKeyValuePairs& rawKeyValuePairs,
 
                          std::string& oAuthSignature )
 
{
 
    std::string rawParams;
 
    std::string paramsSeperator;
 
    std::string sigBase;
 

	
 
    /* Initially empty signature */
 
    oAuthSignature = "";
 

	
 
    /* Build a string using key-value pairs */
 
    paramsSeperator = "&";
 
    getStringFromOAuthKeyValuePairs( rawKeyValuePairs, rawParams, paramsSeperator );
 

	
 
    /* Start constructing base signature string. Refer http://dev.twitter.com/auth#intro */
 
    switch( eType )
 
    {
 
    case eOAuthHttpGet:
 
        {
 
            sigBase.assign( "GET&" );
 
        }
 
        break;
 

	
 
    case eOAuthHttpPost:
 
        {
 
            sigBase.assign( "POST&" );
 
        }
 
        break;
 

	
 
    case eOAuthHttpDelete:
 
        {
 
            sigBase.assign( "DELETE&" );
 
        }
 
        break;
 

	
 
    default:
 
        {
 
            return false;
 
        }
 
        break;
 
    }
 
    sigBase.append( urlencode( rawUrl ) );
 
    sigBase.append( "&" );
 
    sigBase.append( urlencode( rawParams ) );
 

	
 
    /* Now, hash the signature base string using HMAC_SHA1 class */
 
    CHMAC_SHA1 objHMACSHA1;
 
    std::string secretSigningKey;
 
    unsigned char strDigest[oAuthLibDefaults::OAUTHLIB_BUFFSIZE_LARGE];
 

	
 
    memset( strDigest, 0, oAuthLibDefaults::OAUTHLIB_BUFFSIZE_LARGE );
 

	
 
    /* Signing key is composed of consumer_secret&token_secret */
 
    secretSigningKey.assign( m_consumerSecret );
 
    secretSigningKey.append( "&" );
 
    if( m_oAuthTokenSecret.length() )
 
    {
 
        secretSigningKey.append( m_oAuthTokenSecret );
 
    }
 
  
 
    objHMACSHA1.HMAC_SHA1( (unsigned char*)sigBase.c_str(),
 
                           sigBase.length(),
 
                           (unsigned char*)secretSigningKey.c_str(),
 
                           secretSigningKey.length(),
 
                           strDigest ); 
 

	
 
    /* Do a base64 encode of signature */
 
    std::string base64Str = base64_encode( strDigest, 20 /* SHA 1 digest is 160 bits */ );
 

	
 
    /* Do an url encode */
 
    oAuthSignature = urlencode( base64Str );
 

	
 
    return !oAuthSignature.empty();
 
}
 

	
 
/*++
 
* @method: oAuth::getOAuthHeader
 
*
 
* @description: this method builds OAuth header that should be used in HTTP requests to twitter
 
*
 
* @input: eType - HTTP request type
 
*         rawUrl - raw url of the HTTP request
 
*         rawData - HTTP data (post fields)
 
*         includeOAuthVerifierPin - flag to indicate whether or not oauth_verifier needs to included
 
*                                   in OAuth header
 
*
 
* @output: oAuthHttpHeader - OAuth header
 
*
 
*--*/
 
bool oAuth::getOAuthHeader( const eOAuthHttpRequestType eType,
 
                            const std::string& rawUrl,
 
                            const std::string& rawData,
 
                            std::string& oAuthHttpHeader,
 
                            const bool includeOAuthVerifierPin )
 
{
 
    oAuthKeyValuePairs rawKeyValuePairs;
 
    std::string rawParams;
 
    std::string oauthSignature;
 
    std::string paramsSeperator;
 
    std::string pureUrl( rawUrl );
 

	
 
    /* Clear header string initially */
 
    oAuthHttpHeader = "";
 
    rawKeyValuePairs.clear();
 

	
 
    /* If URL itself contains ?key=value, then extract and put them in map */
 
    size_t nPos = rawUrl.find_first_of( "?" );
 
    if( std::string::npos != nPos )
 
    {
 
        /* Get only URL */
 
        pureUrl = rawUrl.substr( 0, nPos );
 

	
 
        /* Get only key=value data part */
 
        std::string dataPart = rawUrl.substr( nPos + 1 );
 

	
 
        /* Split the data in URL as key=value pairs */
 
        buildOAuthRawDataKeyValPairs( dataPart, true, rawKeyValuePairs );
 
    }
 

	
 
    /* Split the raw data if it's present, as key=value pairs. Data should already be urlencoded once */
 
    buildOAuthRawDataKeyValPairs( rawData, false, rawKeyValuePairs );
 

	
 
    /* Build key-value pairs needed for OAuth request token, without signature */
 
    buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin, std::string( "" ), rawKeyValuePairs, true );
 

	
 
    /* Get url encoded base64 signature using request type, url and parameters */
 
    getSignature( eType, pureUrl, rawKeyValuePairs, oauthSignature );
 

	
 
    /* Clear map so that the parameters themselves are not sent along with the OAuth values */
 
    rawKeyValuePairs.clear();
 

	
 
    /* Now, again build key-value pairs with signature this time */
 
    buildOAuthTokenKeyValuePairs( includeOAuthVerifierPin, oauthSignature, rawKeyValuePairs, false );
 

	
 
    /* Get OAuth header in string format */
 
    paramsSeperator = ",";
 
    getStringFromOAuthKeyValuePairs( rawKeyValuePairs, rawParams, paramsSeperator );
 

	
 
    /* Build authorization header */
 
    oAuthHttpHeader.assign( oAuthLibDefaults::OAUTHLIB_AUTHHEADER_STRING );
 
    oAuthHttpHeader.append( rawParams );
 

	
 
    return !oAuthHttpHeader.empty();
 
}
 

	
 
/*++
 
* @method: oAuth::getStringFromOAuthKeyValuePairs
 
*
 
* @description: this method builds a sorted string from key-value pairs
 
*
 
* @input: rawParamMap - key-value pairs map
 
*         paramsSeperator - sepearator, either & or ,
 
*
 
* @output: rawParams - sorted string of OAuth parameters
 
*
 
* @remarks: internal method
 
*
 
*--*/
 
bool oAuth::getStringFromOAuthKeyValuePairs( const oAuthKeyValuePairs& rawParamMap,
 
                                             std::string& rawParams,
 
                                             const std::string& paramsSeperator )
 
{
 
    rawParams = "";
 
    if( rawParamMap.empty() )
 
    {
 
        return false;
 
    }
 

	
 
    oAuthKeyValueList keyValueList;
 
    std::string dummyStr;
 

	
 
    /* Push key-value pairs to a list of strings */
 
    keyValueList.clear();
 
    oAuthKeyValuePairs::const_iterator itMap = rawParamMap.begin();
 
    for( ; itMap != rawParamMap.end(); itMap++ )
 
    {
 
        dummyStr.assign( itMap->first );
 
        dummyStr.append( "=" );
 
        if( paramsSeperator == "," )
 
        {
 
            dummyStr.append( "\"" );
 
        }
 
        dummyStr.append( itMap->second );
 
        if( paramsSeperator == "," )
 
        {
 
            dummyStr.append( "\"" );
 
        }
 
        keyValueList.push_back( dummyStr );
 
    }
 

	
 
    /* Sort key-value pairs based on key name */
 
    keyValueList.sort();
 

	
 
    /* Now, form a string */
 
    dummyStr = "";
 
    oAuthKeyValueList::iterator itKeyValue = keyValueList.begin();
 
    for( ; itKeyValue != keyValueList.end(); itKeyValue++ )
 
    {
 
        if( dummyStr.length() )
 
        {
 
            dummyStr.append( paramsSeperator );
 
         }
 
         dummyStr.append( itKeyValue->c_str() );
 
    }
 
    rawParams = dummyStr;
 
    return !rawParams.empty();
 
}
 

	
 
/*++
 
* @method: oAuth::extractOAuthTokenKeySecret
 
*
 
* @description: this method extracts oauth token key and secret from
 
*               twitter's HTTP response
 
*
 
* @input: requestTokenResponse - response from twitter
 
*
 
* @output: none
 
*
 
*--*/
 
bool oAuth::extractOAuthTokenKeySecret( const std::string& requestTokenResponse )
 
{
 
    if( requestTokenResponse.empty() )
 
    {
 
        return false;
 
    }
 

	
 
    size_t nPos = std::string::npos;
 
    std::string strDummy;
 

	
 
    /* Get oauth_token key */
 
    nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_TOKEN_KEY );
 
    if( std::string::npos != nPos )
 
    {
 
        nPos = nPos + oAuthLibDefaults::OAUTHLIB_TOKEN_KEY.length() + strlen( "=" );
 
        strDummy = requestTokenResponse.substr( nPos );
 
        nPos = strDummy.find( "&" );
 
        if( std::string::npos != nPos )
 
        {
 
            m_oAuthTokenKey = strDummy.substr( 0, nPos );
 
        }
 
    }
 

	
 
    /* Get oauth_token_secret */
 
    nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_TOKENSECRET_KEY );
 
    if( std::string::npos != nPos )
 
    {
 
        nPos = nPos + oAuthLibDefaults::OAUTHLIB_TOKENSECRET_KEY.length() + strlen( "=" );
 
        strDummy = requestTokenResponse.substr( nPos );
 
        nPos = strDummy.find( "&" );
 
        if( std::string::npos != nPos )
 
        {
 
            m_oAuthTokenSecret = strDummy.substr( 0, nPos );
 
        }
 
    }
 

	
 
    /* Get screen_name */
 
    nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_SCREENNAME_KEY );
 
    if( std::string::npos != nPos )
 
    {
 
        nPos = nPos + oAuthLibDefaults::OAUTHLIB_SCREENNAME_KEY.length() + strlen( "=" );
 
        strDummy = requestTokenResponse.substr( nPos );
 
        m_oAuthScreenName = strDummy;
 
    }
 

	
 
    return true;
 
}
 

	

Changeset was too big and was cut off... Show full diff anyway

0 comments (0 inline, 0 general)