Changeset - 37cc935c1800
[Not reviewed]
Merge
0 8 0
Vitaly Takmazov - 14 years ago 2011-10-20 14:23:11
vitalyster@gmail.com
merge next
8 files changed with 239 insertions and 58 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -162,25 +162,27 @@ include_directories(include)
 
include_directories(${EVENT_INCLUDE_DIRS})
 
include_directories(${SWIFTEN_INCLUDE_DIR})
 
include_directories(${Boost_INCLUDE_DIRS})
 
include_directories(${OPENSSL_INCLUDE_DIR})
 

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

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

	
 
if(DOXYGEN_FOUND)
 
	message("Docs              : yes")
 
	ADD_SUBDIRECTORY(docs)
 
else(DOXYGEN_FOUND)
backends/libpurple/main.cpp
Show inline comments
 
@@ -1688,24 +1688,51 @@ static void spectrum_sigchld_handler(int sig)
 

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

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

	
 
static int create_socket(char *host, int portno) {
 
	struct sockaddr_in serv_addr;
 
	
 
	int m_sock = socket(AF_INET, SOCK_STREAM, 0);
 
	memset((char *) &serv_addr, 0, sizeof(serv_addr));
 
	serv_addr.sin_family = AF_INET;
 
	serv_addr.sin_port = htons(portno);
 

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

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

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

	
 
static void transportDataReceived(gpointer data, gint source, PurpleInputCondition cond) {
 
	if (cond & PURPLE_INPUT_READ) {
 
		char buffer[65535];
 
		char *ptr = buffer;
 
		ssize_t n = read(source, ptr, sizeof(buffer));
 
		if (n <= 0) {
 
			LOG4CXX_INFO(logger, "Diconnecting from spectrum2 server");
 
			exit(errno);
 
		}
 
		std::string d = std::string(buffer, n);
 
		np->handleDataRead(d);
 
	}
 
@@ -1789,48 +1816,25 @@ int main(int argc, char **argv) {
 
		else {
 
			log4cxx::helpers::Properties p;
 
			log4cxx::helpers::FileInputStream *istream = new log4cxx::helpers::FileInputStream(KEYFILE_STRING("logging", "backend_config"));
 

	
 
			p.load(istream);
 
			p.setProperty("pid", stringOf(getpid()));
 
			p.setProperty("jid", KEYFILE_STRING("service", "jid"));
 
			log4cxx::PropertyConfigurator::configure(p);
 
		}
 

	
 
		initPurple();
 

	
 
		int portno = port;
 
		struct sockaddr_in serv_addr;
 

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

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

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

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

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

	
 
		np = new SpectrumNetworkPlugin(host, port);
 
		bool libev = KEYFILE_STRING("service", "eventloop") == "libev";
 

	
 
		GMainLoop *m_loop;
 
#ifdef WITH_LIBEVENT
 
		if (!libev) {
 
			m_loop = g_main_loop_new(NULL, FALSE);
 
		}
cmake_modules/SwiftenConfig.cmake
Show inline comments
 
FIND_LIBRARY(SWIFTEN_LIBRARY NAMES Swiften)
 
FIND_PATH(SWIFTEN_INCLUDE_DIR NAMES "Swiften.h" PATH_SUFFIXES libSwiften Swiften )
 

	
 
if( SWIFTEN_LIBRARY AND SWIFTEN_INCLUDE_DIR )
 
	find_program(SWIFTEN_CONFIG_EXECUTABLE NAMES swiften-config DOC "swiften-config executable")
 
	set( SWIFTEN_CFLAGS "" )
 
	if (SWIFTEN_CONFIG_EXECUTABLE)
 
		execute_process(
 
			COMMAND ${SWIFTEN_CONFIG_EXECUTABLE} --libs
 
			OUTPUT_VARIABLE SWIFTEN_LIBRARY)
 
		string(REGEX REPLACE "[\r\n]"                  " " SWIFTEN_LIBRARY "${SWIFTEN_LIBRARY}")
 
		string(REGEX REPLACE " +$"                     ""  SWIFTEN_LIBRARY "${SWIFTEN_LIBRARY}")
 
		string(REGEX REPLACE "[\r\n]"                  " " SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY})
 
		string(REGEX REPLACE " +$"                     ""  SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY})
 
		string(REGEX REPLACE " " ";" SWIFTEN_LIBRARY ${SWIFTEN_LIBRARY})
 
	else()
 
		message( FATAL_ERROR "Could NOT find swiften-config" )
 
	endif()
 

	
 
	set( SWIFTEN_INCLUDE_DIR ${SWIFTEN_INCLUDE_DIR}/.. )
 
	message( STATUS "Found libSwiften: ${SWIFTEN_LIBRARY}, ${SWIFTEN_INCLUDE_DIR}")
 
	set( SWIFTEN_FOUND 1 )
 
else( SWIFTEN_LIBRARY AND SWIFTEN_INCLUDE_DIR )
 
    message( FATAL_ERROR "Could NOT find libSwiften" )
 
endif( SWIFTEN_LIBRARY AND SWIFTEN_INCLUDE_DIR )
plugin/src/CMakeLists.txt
Show inline comments
 
@@ -7,20 +7,26 @@ ADD_DEPENDENCIES(transport-plugin pb)
 
SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/../../include/transport/protocol.pb.cc PROPERTIES GENERATED 1)
 
 
if (CMAKE_COMPILER_IS_GNUCXX)
 
	ADD_DEFINITIONS(-fPIC)
 
endif()
 
 
if (NOT WIN32)
 
	TARGET_LINK_LIBRARIES(transport-plugin ${PROTOBUF_LIBRARIES} ${LOG4CXX_LIBRARIES})
 
else()
 
	TARGET_LINK_LIBRARIES(transport-plugin ${PROTOBUF_LIBRARIES} ${LOG4CXX_LIBRARIES} ws2_32.lib)
 
endif() 
 
 
if (NOT WIN32)
 
TARGET_LINK_LIBRARIES(transport-plugin ${PROTOBUF_LIBRARIES} ${LOG4CXX_LIBRARIES})
 
else()
 
TARGET_LINK_LIBRARIES(transport-plugin ${PROTOBUF_LIBRARIES} ${LOG4CXX_LIBRARIES} ws2_32.lib)
 
endif()
 
 
SET_TARGET_PROPERTIES(transport-plugin PROPERTIES
 
      VERSION ${TRANSPORT_VERSION} SOVERSION ${TRANSPORT_VERSION}
 
)
 
 
INSTALL(TARGETS transport-plugin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib COMPONENT libraries)
 
 
#CONFIGURE_FILE(transport.pc.in "${CMAKE_CURRENT_BINARY_DIR}/transport.pc")
 
#INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/transport.pc" DESTINATION lib/pkgconfig)
spectrum/src/main.cpp
Show inline comments
 
@@ -56,70 +56,66 @@ static void spectrum_sigterm_handler(int sig) {
 
static void removeOldIcons(std::string iconDir) {
 
	std::vector<std::string> dirs;
 
	dirs.push_back(iconDir);
 

	
 
	boost::thread thread(boost::bind(Util::removeEverythingOlderThan, dirs, time(NULL) - 3600*24*14));
 
}
 

	
 
#ifndef WIN32
 
static void daemonize(const char *cwd, const char *lock_file) {
 
	pid_t pid, sid;
 
	FILE* lock_file_f;
 
	char process_pid[20];
 

	
 
	/* already a daemon */
 
	if ( getppid() == 1 ) return;
 

	
 
	/* Fork off the parent process */
 
	pid = fork();
 
	if (pid < 0) {
 
		exit(1);
 
	}
 
	/* If we got a good PID, then we can exit the parent process. */
 
	if (pid > 0) {
 
		if (lock_file) {
 
			/* write our pid into it & close the file. */
 
			lock_file_f = fopen(lock_file, "w+");
 
			if (lock_file_f == NULL) {
 
				std::cerr << "Cannot create lock file " << lock_file << ". Exiting\n";
 
				exit(1);
 
			}
 
			sprintf(process_pid,"%d\n",pid);
 
			if (fwrite(process_pid,1,strlen(process_pid),lock_file_f) < strlen(process_pid)) {
 
				std::cerr << "Cannot write to lock file " << lock_file << ". Exiting\n";
 
				exit(1);
 
			}
 
			fclose(lock_file_f);
 
		}
 
		exit(0);
 
	}
 

	
 
	/* At this point we are executing as the child process */
 

	
 
	/* Change the file mode mask */
 
	umask(0);
 

	
 
	/* Create a new SID for the child process */
 
	sid = setsid();
 
	if (sid < 0) {
 
		exit(1);
 
	}
 

	
 
	/* Change the current working directory.  This prevents the current
 
		directory from being locked; hence not being able to remove it. */
 
	if ((chdir(cwd)) < 0) {
 
		exit(1);
 
	}
 
	
 
	if (lock_file) {
 
		/* write our pid into it & close the file. */
 
		lock_file_f = fopen(lock_file, "w+");
 
		if (lock_file_f == NULL) {
 
			std::cout << "EE cannot create lock file " << lock_file << ". Exiting\n";
 
			exit(1);
 
		}
 
		sprintf(process_pid,"%d\n",getpid());
 
		if (fwrite(process_pid,1,strlen(process_pid),lock_file_f) < strlen(process_pid)) {
 
			std::cout << "EE cannot write to lock file " << lock_file << ". Exiting\n";
 
			exit(1);
 
		}
 
		fclose(lock_file_f);
 
	}
 
	
 
	if (freopen( "/dev/null", "r", stdin) == NULL) {
 
		std::cout << "EE cannot open /dev/null. Exiting\n";
 
		exit(1);
 
	}
 
}
 

	
 
#endif
 

	
 
int main(int argc, char **argv)
 
{
 
	Config config;
 

	
spectrum_manager/src/main.cpp
Show inline comments
 
#include "managerconfig.h"
 
#include "transport/transport.h"
 
#include "transport/usermanager.h"
 
#include "transport/logger.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/userregistration.h"
 
#include "transport/networkpluginserver.h"
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 
 
#include <boost/foreach.hpp>
 
#include <iostream>
 
#include <fstream>
 
#include <iterator>
 
#include <algorithm>
 
#include <boost/filesystem.hpp>
 
#include "signal.h"
 
#include "sys/wait.h"
 
 
 
using namespace Transport;
 
 
using namespace boost::filesystem;
 
 
using namespace boost;
 
 
static int finished;
 
static std::string *m;
 
 
static void handleDisconnected(Swift::Client *client, const boost::optional<Swift::ClientError> &) {
 
	std::cout << "[ DISCONNECTED ] " << client->getJID().getDomain() << "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
 
}
 
 
static void handleConnected(Swift::Client *client) {
 
	boost::shared_ptr<Swift::Message> message(new Swift::Message());
 
@@ -28,75 +42,231 @@ static void handleConnected(Swift::Client *client) {
 
	client->sendMessage(message);
 
}
 
 
static void handleMessageReceived(Swift::Client *client, Swift::Message::ref message) {
 
	std::string body = message->getBody();
 
	boost::replace_all(body, "\n", "\n[      OK      ] " + client->getJID().getDomain() + ": ");
 
	std::cout << "[      OK      ] " << client->getJID().getDomain() << ": " << body <<  "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
 
}
 
 
static std::string searchForBinary(const std::string &binary) {
 
	std::vector<std::string> path_list;
 
	char * env_path = getenv("PATH");
 
 
	if (env_path != NULL) {
 
		std::string buffer = "";
 
		for (int s = 0; s < strlen(env_path); s++) {
 
			if (env_path[s] == ':') {
 
				path_list.insert(path_list.end(), std::string(buffer));
 
				buffer = "";
 
			}
 
			else {
 
				buffer += env_path[s];
 
			}
 
		}
 
 
		if (buffer != "") {
 
			path_list.insert(path_list.end(), std::string(buffer));
 
			buffer = "";
 
		}
 
 
		for (std::vector<std::string>::iterator dit = path_list.begin(); dit < path_list.end(); dit++) {
 
			std::string bpath = *dit;
 
			bpath += "/";
 
			bpath += binary;
 
			path p(bpath);
 
			if (exists(p) && !is_directory(p)) {
 
				return bpath;
 
			}
 
		}
 
	}
 
	return "";
 
}
 
 
// Executes new backend
 
static unsigned long exec_(std::string path, std::string config) {
 
	// fork and exec
 
	pid_t pid = fork();
 
	if ( pid == 0 ) {
 
		// child process
 
		exit(execl(path.c_str(), path.c_str(), config.c_str(), NULL));
 
	} else if ( pid < 0 ) {
 
		// fork failed
 
	}
 
	else {
 
		waitpid(pid, 0, 0);
 
	}
 
 
	return (unsigned long) pid;
 
}
 
 
static int isRunning(const std::string &pidfile) {
 
	path p(pidfile);
 
	if (!exists(p) || is_directory(p)) {
 
		return 0;
 
	}
 
 
	std::ifstream f(p.string().c_str(), std::ios_base::in);
 
	std::string pid;
 
	f >> pid;
 
 
	if (pid.empty())
 
		return 0;
 
 
	if (kill(boost::lexical_cast<int>(pid), 0) != 0)
 
		return 0;
 
 
	return boost::lexical_cast<int>(pid);
 
}
 
 
static void start_all_instances(ManagerConfig *config) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 
 
		std::string spectrum2_binary = searchForBinary("spectrum2");
 
		if (spectrum2_binary.empty()) {
 
			std::cerr << "spectrum2 binary not found in PATH\n";
 
			exit(8);
 
		}
 
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
				}
 
 
				if (!isRunning(CONFIG_STRING(&cfg, "service.pidfile"))) {
 
					exec_(spectrum2_binary, itr->path().string());
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 
 
static void stop_all_instances(ManagerConfig *config) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
				}
 
 
				int pid = isRunning(CONFIG_STRING(&cfg, "service.pidfile"));
 
				if (pid) {
 
					kill(pid, SIGTERM);
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 
 
int main(int argc, char **argv)
 
{
 
	ManagerConfig config;
 
	std::string config_file;
 
	std::string command;
 
	boost::program_options::variables_map vm;
 
 
	boost::program_options::options_description desc("Usage: spectrum_manager <config_file.cfg> <command>\nAllowed options");
 
	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <COMMAND>\nAllowed options");
 
	desc.add_options()
 
		("help,h", "help")
 
		("help,h", "Show help output")
 
		("config,c", boost::program_options::value<std::string>(&config_file)->default_value("/etc/spectrum2/spectrum-manager.cfg"), "Spectrum manager config file")
 
		("command", boost::program_options::value<std::string>(&command)->default_value(""), "Command")
 
		;
 
	try
 
	{
 
		boost::program_options::variables_map vm;
 
		boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
 
		boost::program_options::positional_options_description p;
 
		p.add("command", -1);
 
		boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
 
          options(desc).positional(p).run(), vm);
 
		boost::program_options::notify(vm);
 
 
		if(vm.count("help"))
 
		{
 
			std::cout << desc << "\n";
 
			return 1;
 
		}
 
	}
 
	catch (std::runtime_error& e)
 
	{
 
		std::cout << desc << "\n";
 
		return 1;
 
		return 2;
 
	}
 
	catch (...)
 
	{
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 
 
	if (argc != 3) {
 
		std::cout << desc << "\n";
 
		return 1;
 
		return 3;
 
	}
 
 
 
	if (!config.load(argv[1])) {
 
	if (!config.load(config_file)) {
 
		std::cerr << "Can't load configuration file.\n";
 
		return 1;
 
		return 4;
 
	}
 
 
	if (command == "start") {
 
		start_all_instances(&config);
 
	}
 
	else if (command == "stop") {
 
		stop_all_instances(&config);
 
	}
 
	else {
 
		Swift::SimpleEventLoop eventLoop;
 
		Swift::BoostNetworkFactories networkFactories(&eventLoop);
 
 
	std::string message = argv[2];
 
		std::string message = argv[1];
 
		m = &message;
 
 
		std::vector<std::string> servers = CONFIG_VECTOR(&config, "servers.server");
 
		for (std::vector<std::string>::const_iterator it = servers.begin(); it != servers.end(); it++) {
 
			finished++;
 
			Swift::Client *client = new Swift::Client(CONFIG_STRING(&config, "service.admin_username") + "@" + (*it), CONFIG_STRING(&config, "service.admin_password"), &networkFactories);
 
			client->setAlwaysTrustCertificates();
 
			client->onConnected.connect(boost::bind(&handleConnected, client));
 
			client->onDisconnected.connect(bind(&handleDisconnected, client, _1));
 
			client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1));
 
			Swift::ClientOptions opt;
 
			opt.allowPLAINWithoutTLS = true;
 
			client->connect(opt);
 
	// 		std::cout << *it << "\n";
 
		}
 
 
		eventLoop.run();
 
	}
 
}
spectrum_manager/src/managerconfig.cpp
Show inline comments
 
@@ -22,24 +22,25 @@
 
#include <fstream>
 

	
 
using namespace boost::program_options;
 

	
 
bool ManagerConfig::load(const std::string &configfile, boost::program_options::options_description &opts) {
 
	std::ifstream ifs(configfile.c_str());
 
	if (!ifs.is_open())
 
		return false;
 

	
 
	opts.add_options()
 
		("service.admin_username", value<std::string>()->default_value(""), "Administrator username.")
 
		("service.admin_password", value<std::string>()->default_value(""), "Administrator password.")
 
		("service.config_directory", value<std::string>()->default_value("/etc/spectrum2/transports/"), "Directory with spectrum2 configuration files. One .cfg file per one instance")
 
		("servers.server", value<std::vector<std::string> >()->multitoken(), "Server.")
 
	;
 

	
 
	store(parse_config_file(ifs, opts), m_variables);
 
	notify(m_variables);
 

	
 
	m_file = configfile;
 

	
 
	onManagerConfigReloaded();
 

	
 
	return true;
 
}
spectrum_manager/src/spectrum_manager.cfg
Show inline comments
 
[service]
 
admin_username=admin
 
admin_password=test
 
config_directory=/etc/spectrum2/transports/
 

	
 
[servers]
 
server=localhost
 
#server=icq.spectrum.im
 
#server=msn.spectrum.im
 
\ No newline at end of file
0 comments (0 inline, 0 general)