Changeset - d0ead2e24cff
[Not reviewed]
Merge
0 5 0
Darchigh - 12 years ago 2013-01-22 10:42:29
Darchigh@gmail.com
Merge branch 'master' of http://github.com/hanzz/libtransport
4 files changed with 47 insertions and 41 deletions:
0 comments (0 inline, 0 general)
spectrum/src/sample.cfg
Show inline comments
 
@@ -4,33 +4,33 @@ password = secret
 
server = 127.0.0.1
 
port = 5222
 
server_mode = 1
 
backend_host=localhost
 
pidfile=./test.pid
 
# < this option doesn't work yet
 
#backend_port=10001
 
#admin_jid=admin@localhost
 
admin_password=test
 
#cert=server.pfx #patch to PKCS#12 certificate
 
#cert_password=test #password to that certificate if any
 
users_per_backend=10
 
backend=../..//backends/swiften/spectrum2_swiften_backend
 
#backend=../..//backends/swiften/spectrum2_swiften_backend
 
#backend=../../backends/twitter/spectrum2_twitter_backend
 
#backend=/home/hanzz/code/libtransport/backends/libcommuni/spectrum2_libcommuni_backend
 
backend=/home/hanzz/code/libtransport/backends/libcommuni/spectrum2_libcommuni_backend
 
protocol=prpl-jabber
 
#protocol=prpl-msn
 
#protocol=any
 
#protocol=prpl-icq
 
working_dir=./
 
portfile=$jid.port
 
portfile=./$jid.port
 
irc_server=irc.freenode.org
 

	
 
[backend]
 
#default_avatar=catmelonhead.jpg
 
#no_vcard_fetch=true
 

	
 
[logging]
 
#config=logging.cfg # log4cxx/log4j logging configuration file
 
#backend_config=/home/hanzz/code/libtransport/spectrum/src/backend-logging.cfg # log4cxx/log4j logging configuration file for backends
 

	
 
[database]
 
type=sqlite3 # or "none" without database backend
spectrum_manager/src/main.cpp
Show inline comments
 
@@ -129,24 +129,27 @@ int main(int argc, char **argv)
 
	if (command[0] == "start") {
 
		return start_instances(&config);
 
	}
 
	else if (command[0] == "stop") {
 
		stop_instances(&config);
 
	}
 
	else if (command[0] == "status") {
 
		return show_status(&config);
 
	}
 
	else if (command[0] == "list") {
 
		std::vector<std::string> list = show_list(&config);
 
	}
 
	else if (command[0] == "restart") {
 
		return restart_instances(&config);
 
	}
 
	else if (command[0] == "server") {
 
		Server server(&config);
 
		if (server.start() == false) {
 
			std::cerr << "Can't set up server handler.\n";
 
			return 1;
 
		}
 
		while (1) { sleep(10); }
 
	}
 
	else {
 
		if (command.size() < 2) {
 
			std::cout << desc << "\n";
 
			return 11;
src/logging.cpp
Show inline comments
 
@@ -82,26 +82,29 @@ protected:
 
	{
 
		return 0;
 
	}
 

	
 
private:
 
    log4cxx::LoggerPtr _logger;
 
};
 

	
 
static intercept_stream* intercepter_cout;
 
static intercept_stream* intercepter_cerr;
 

	
 

	
 
static void initLogging(Config *config, std::string key) {
 
static void initLogging(Config *config, std::string key, bool only_create_dir = false) {
 
	if (CONFIG_STRING(config, key).empty()) {
 
		if (only_create_dir) {
 
			return;
 
		}
 
		root = log4cxx::Logger::getRootLogger();
 
#ifdef _MSC_VER
 
		root->addAppender(new ConsoleAppender(new PatternLayout(L"%d %-5p %c: %m%n")));
 
#else
 
		root->addAppender(new ConsoleAppender(new PatternLayout("%d %-5p %c: %m%n")));
 
#endif
 
	}
 
	else {
 
		log4cxx::helpers::Properties p;
 

	
 
		log4cxx::helpers::FileInputStream *istream = NULL;
 
		try {
 
@@ -124,81 +127,90 @@ static void initLogging(Config *config, std::string key) {
 
		log4cxx::helpers::Transcoder::decode(CONFIG_STRING(config, "service.jid"), jid);
 
		log4cxx::helpers::Transcoder::decode(CONFIG_STRING_DEFAULTED(config, "service.backend_id", ""), id);
 
#ifdef _MSC_VER
 
		p.setProperty(L"pid", pid);
 
		p.setProperty(L"jid", jid);
 
		p.setProperty(L"id", id);
 
#else
 
		p.setProperty("pid", pid);
 
		p.setProperty("jid", jid);
 
		p.setProperty("id", id);
 
#endif
 

	
 
		std::string dir;
 
		std::vector<std::string> dirs;
 
		BOOST_FOREACH(const log4cxx::LogString &prop, p.propertyNames()) {
 
// 			if (boost::ends_with(prop, ".File")) {
 
			if (boost::ends_with(prop, ".File")) {
 
				std::string dir;
 
				log4cxx::helpers::Transcoder::encode(p.get(prop), dir);
 
				boost::replace_all(dir, "${jid}", jid);
 
				boost::replace_all(dir, "${pid}", pid);
 
				boost::replace_all(dir, "${id}", id);
 
				break;
 
// 			}
 
				dirs.push_back(dir);
 
			}
 
		}
 
		mode_t old_cmask;
 
		if (!dir.empty()) {
 
			// create directories
 
		// create directories
 
#ifndef WIN32
 
			old_cmask = umask(0007);
 
		old_cmask = umask(0007);
 
#endif
 
			try {
 
				Transport::Util::createDirectories(config, boost::filesystem::path(dir).parent_path());
 
			}
 
			catch (const boost::filesystem::filesystem_error &e) {
 
				std::cerr << "Can't create logging directory directory " << boost::filesystem::path(dir).parent_path().string() << ": " << e.what() << ".\n";
 

	
 
		BOOST_FOREACH(std::string &dir, dirs) {
 
			if (!dir.empty()) {
 
				try {
 
					Transport::Util::createDirectories(config, boost::filesystem::path(dir).parent_path());
 
				}
 
				catch (const boost::filesystem::filesystem_error &e) {
 
					std::cerr << "Can't create logging directory directory " << boost::filesystem::path(dir).parent_path().string() << ": " << e.what() << ".\n";
 
				}
 
			}
 
		}
 

	
 
#ifndef WIN32
 
		umask(old_cmask);
 
#endif
 

	
 
		if (only_create_dir) {
 
			return;
 
		}
 

	
 
		log4cxx::PropertyConfigurator::configure(p);
 

	
 
		// Change owner of main log file
 
#ifndef WIN32
 
	if (!CONFIG_STRING(config, "service.group").empty() && !CONFIG_STRING(config, "service.user").empty()) {
 
		struct group *gr;
 
		if ((gr = getgrnam(CONFIG_STRING(config, "service.group").c_str())) == NULL) {
 
			std::cerr << "Invalid service.group name " << CONFIG_STRING(config, "service.group") << "\n";
 
		}
 
		struct passwd *pw;
 
		if ((pw = getpwnam(CONFIG_STRING(config, "service.user").c_str())) == NULL) {
 
			std::cerr << "Invalid service.user name " << CONFIG_STRING(config, "service.user") << "\n";
 
	BOOST_FOREACH(std::string &dir, dirs) {
 
		if (!CONFIG_STRING(config, "service.group").empty() && !CONFIG_STRING(config, "service.user").empty()) {
 
			struct group *gr;
 
			if ((gr = getgrnam(CONFIG_STRING(config, "service.group").c_str())) == NULL) {
 
				std::cerr << "Invalid service.group name " << CONFIG_STRING(config, "service.group") << "\n";
 
			}
 
			struct passwd *pw;
 
			if ((pw = getpwnam(CONFIG_STRING(config, "service.user").c_str())) == NULL) {
 
				std::cerr << "Invalid service.user name " << CONFIG_STRING(config, "service.user") << "\n";
 
			}
 
			chown(dir.c_str(), pw->pw_uid, gr->gr_gid);
 
		}
 
		chown(dir.c_str(), pw->pw_uid, gr->gr_gid);
 
	}
 
#endif
 

	
 
#ifndef WIN32
 
		if (!dir.empty()) {
 
			umask(old_cmask);
 
		}
 
#endif
 
	}
 
}
 

	
 
void initBackendLogging(Config *config) {
 
	initLogging(config, "logging.backend_config");
 

	
 
	redirect_stderr();
 
}
 

	
 
void initMainLogging(Config *config) {
 
	initLogging(config, "logging.config");
 
	initLogging(config, "logging.backend_config", true);
 
}
 

	
 
void redirect_stderr() {
 
	 intercepter_cerr = new intercept_stream(std::cerr, "cerr");
 
	 intercepter_cout = new intercept_stream(std::cout, "cout");
 
}
 

	
 
void shutdownLogging() {
 
	delete intercepter_cerr;
 
	delete intercepter_cout;
 
	log4cxx::LogManager::shutdown();
 
}
src/userregistry.cpp
Show inline comments
 
@@ -70,56 +70,47 @@ void UserRegistry::isValidUserPassword(const Swift::JID& user, Swift::ServerFrom
 

	
 
void UserRegistry::stopLogin(const Swift::JID& user, Swift::ServerFromClientSession *session) {
 
	std::string key = user.toBare().toString();
 
	if (users.find(key) != users.end()) {
 
		if (users[key].session == session) {
 
			LOG4CXX_INFO(logger, key << ": Stopping login process (user probably disconnected while logging in)");
 
			users.erase(key);
 
		}
 
		else {
 
			LOG4CXX_WARN(logger, key << ": Stopping login process (user probably disconnected while logging in), but this is not active session");
 
		}
 
	}
 
	else {
 
		LOG4CXX_WARN(logger, key << ": Stopping login process (user probably disconnected while logging in) for invalid user");
 
	}
 

	
 
	// ::removeLater can be called only by libtransport, not by Swift and libtransport
 
	// takes care about user disconnecting itself, so don't call our signal.
 
	if (!m_inRemoveLater)
 
		onDisconnectUser(user);
 
}
 

	
 
void UserRegistry::onPasswordValid(const Swift::JID &user) {
 
	std::string key = user.toBare().toString();
 
	if (users.find(key) != users.end()) {
 
		LOG4CXX_INFO(logger, key << ": Password is valid");
 
		users[key].session->handlePasswordValid();
 
		users.erase(key);
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, key << ": onPasswordValid called for invalid user");
 
	}
 
}
 

	
 
void UserRegistry::onPasswordInvalid(const Swift::JID &user, const std::string &error) {
 
	std::string key = user.toBare().toString();
 
	if (users.find(key) != users.end()) {
 
		LOG4CXX_INFO(logger, key << ": Password is invalid");
 
		LOG4CXX_INFO(logger, key << ": Password is invalid or there was an error when connecting the legacy network");
 
		users[key].session->handlePasswordInvalid(error);
 
		users.erase(key);
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, key << ": onPasswordInvalid called for invalid user");
 
	}
 
}
 

	
 
void UserRegistry::handleRemoveTimeout(const Swift::JID &user) {
 
	m_inRemoveLater = true;
 
	onPasswordInvalid(user);
 
	m_inRemoveLater = false;
 
}
 

	
 
void UserRegistry::removeLater(const Swift::JID &user) {
 
	m_removeTimer->onTick.connect(boost::bind(&UserRegistry::handleRemoveTimeout, this, user));
 
	m_removeTimer->start();
 
}
0 comments (0 inline, 0 general)