changeset 113:6a99814c2317

Irccd: more general cleanup
author David Demelier <markand@malikania.fr>
date Thu, 28 Apr 2016 13:30:30 +0200
parents a45a0f817a7b
children 8cbbce7b4327
files lib/irccd/cmd-server-connect.cpp lib/irccd/config.cpp lib/irccd/js-server.cpp lib/irccd/logger.cpp lib/irccd/logger.hpp lib/irccd/plugin.cpp lib/irccd/server-state-connected.cpp lib/irccd/server-state-connecting.cpp lib/irccd/server-state-disconnected.cpp lib/irccd/server.cpp lib/irccd/server.hpp lib/irccd/transport-server.cpp
diffstat 12 files changed, 234 insertions(+), 319 deletions(-) [+]
line wrap: on
line diff
--- a/lib/irccd/cmd-server-connect.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/cmd-server-connect.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -113,8 +113,8 @@
 	ServerSettings settings;
 
 	settings.command = object.valueOr("commandChar", json::Type::String, settings.command).toString();
-	settings.reconnect_tries = object.valueOr("reconnectTries", json::Type::Int, settings.reconnect_tries).toInt();
-	settings.reconnect_timeout = object.valueOr("reconnectTimeout", json::Type::Int, settings.reconnect_timeout).toInt();
+	settings.reconnectTries = object.valueOr("reconnectTries", json::Type::Int, settings.reconnectTries).toInt();
+	settings.reconnectDelay = object.valueOr("reconnectTimeout", json::Type::Int, settings.reconnectDelay).toInt();
 
 	return settings;
 }
--- a/lib/irccd/config.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/config.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -324,13 +324,13 @@
 	/* Reconnect */
 	try {
 		if ((it = sc.find("reconnect-tries")) != sc.end()) {
-			settings.reconnect_tries = std::stoi(it->value());
+			settings.reconnectTries = std::stoi(it->value());
 		}
 		if ((it = sc.find("reconnect-timeout")) != sc.end()) {
-			settings.reconnect_timeout = std::stoi(it->value());
+			settings.reconnectDelay = std::stoi(it->value());
 		}
 		if ((it = sc.find("ping-timeout")) != sc.end()) {
-			settings.ping_timeout = std::stoi(it->value());
+			settings.pingTimeout = std::stoi(it->value());
 		}
 	} catch (const std::exception &) {
 		throw std::invalid_argument("server " + info.name + ": invalid number for " + it->key() + ": " + it->value());
--- a/lib/irccd/js-server.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/js-server.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -391,8 +391,8 @@
 		settings.channels.push_back(Server::splitChannel(chan));
 	}
 
-	settings.reconnect_tries = duk::optionalProperty<int>(ctx, 0, "recoTries", (int)settings.reconnect_tries);
-	settings.reconnect_timeout = duk::optionalProperty<int>(ctx, 0, "recoTimeout", (int)settings.reconnect_timeout);
+	settings.reconnectTries = duk::optionalProperty<int>(ctx, 0, "recoTries", (int)settings.reconnectTries);
+	settings.reconnectDelay = duk::optionalProperty<int>(ctx, 0, "recoTimeout", (int)settings.reconnectDelay);
 
 	if (duk::optionalProperty<bool>(ctx, 0, "joinInvite", false)) {
 		settings.flags |= ServerSettings::JoinInvite;
--- a/lib/irccd/logger.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/logger.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -133,18 +133,18 @@
  */
 
 /* Information buffer */
-Buffer buffer_info{Buffer::Info};
+Buffer bufferInfo{Buffer::Info};
 
 /* Warning buffer */
-Buffer buffer_warning{Buffer::Warning};
+Buffer bufferWarning{Buffer::Warning};
 
 /* Debug buffer */
-Buffer buffer_debug{Buffer::Debug};
+Buffer bufferDebug{Buffer::Debug};
 
 /* Stream outputs. */
-std::ostream stream_info(&buffer_info);
-std::ostream stream_warning(&buffer_warning);
-std::ostream stream_debug(&buffer_debug);
+std::ostream streamInfo(&bufferInfo);
+std::ostream streamWarning(&bufferWarning);
+std::ostream streamDebug(&bufferDebug);
 
 } // !namespace
 
@@ -174,24 +174,24 @@
  */
 
 File::File(std::string normal, std::string errors)
-	: m_output_normal(std::move(normal))
-	, m_output_error(std::move(errors))
+	: m_outputNormal(std::move(normal))
+	, m_outputError(std::move(errors))
 {
 }
 
 void File::info(const std::string &line)
 {
-	std::ofstream(m_output_normal, std::ofstream::out | std::ofstream::app) << line << std::endl;
+	std::ofstream(m_outputNormal, std::ofstream::out | std::ofstream::app) << line << std::endl;
 }
 
 void File::warning(const std::string &line)
 {
-	std::ofstream(m_output_error, std::ofstream::out | std::ofstream::app) << line << std::endl;
+	std::ofstream(m_outputError, std::ofstream::out | std::ofstream::app) << line << std::endl;
 }
 
 void File::debug(const std::string &line)
 {
-	std::ofstream(m_output_normal, std::ofstream::out | std::ofstream::app) << line << std::endl;
+	std::ofstream(m_outputNormal, std::ofstream::out | std::ofstream::app) << line << std::endl;
 }
 
 /*
@@ -260,28 +260,28 @@
 std::ostream &info(const std::string &message)
 {
 	if (!message.empty()) {
-		stream_info << message << std::endl;
+		streamInfo << message << std::endl;
 	}
 
-	return stream_info;
+	return streamInfo;
 }
 
 std::ostream &warning(const std::string &message)
 {
 	if (!message.empty()) {
-		stream_warning << message << std::endl;
+		streamWarning << message << std::endl;
 	}
 
-	return stream_warning;
+	return streamWarning;
 }
 
 std::ostream &debug(const std::string &message)
 {
 	if (!message.empty()) {
-		stream_debug << message << std::endl;
+		streamDebug << message << std::endl;
 	}
 
-	return stream_debug;
+	return streamDebug;
 }
 
 bool isVerbose() noexcept
--- a/lib/irccd/logger.hpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/logger.hpp	Thu Apr 28 13:30:30 2016 +0200
@@ -128,8 +128,8 @@
  */
 class File : public Interface {
 private:
-	std::string m_output_normal;
-	std::string m_output_error;
+	std::string m_outputNormal;
+	std::string m_outputError;
 
 public:
 	/**
--- a/lib/irccd/plugin.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/plugin.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -38,6 +38,7 @@
 #include "js-timer.hpp"
 #include "js-unicode.hpp"
 #include "js-util.hpp"
+#include "logger.hpp"
 #include "path.hpp"
 #include "plugin.hpp"
 #include "server.hpp"
--- a/lib/irccd/server-state-connected.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/server-state-connected.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -16,11 +16,15 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <format.h>
+
 #include "logger.hpp"
 #include "server-state-connected.hpp"
 #include "server-state-disconnected.hpp"
 #include "server-private.hpp"
 
+using namespace fmt::literals;
+
 namespace irccd {
 
 namespace state {
@@ -33,15 +37,14 @@
 	if (!irc_is_connected(server.session())) {
 		log::warning() << "server " << info.name << ": disconnected" << std::endl;
 
-		if (settings.reconnect_timeout > 0) {
-			log::warning() << "server " << info.name << ": retrying in "
-				       << settings.reconnect_timeout << " seconds" << std::endl;
+		if (settings.reconnectDelay > 0) {
+			log::warning("server {}: retrying in {} seconds"_format(info.name, settings.reconnectDelay));
 		}
 
 		server.next(std::make_unique<state::Disconnected>());
-	} else if (server.cache().ping_timer.elapsed() >= settings.ping_timeout * 1000) {
+	} else if (server.cache().pingTimer.elapsed() >= settings.pingTimeout * 1000) {
 		log::warning() << "server " << info.name << ": ping timeout after "
-			       << (server.cache().ping_timer.elapsed() / 1000) << " seconds" << std::endl;
+			       << (server.cache().pingTimer.elapsed() / 1000) << " seconds" << std::endl;
 		server.next(std::make_unique<state::Disconnected>());
 	} else {
 		irc_add_select_descriptors(server.session(), &setinput, &setoutput, reinterpret_cast<int *>(&maxfd));
--- a/lib/irccd/server-state-connecting.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/server-state-connecting.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -16,6 +16,9 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <format.h>
+
+#include "logger.hpp"
 #include "server-state-connecting.hpp"
 #include "server-state-connected.hpp"
 #include "server-state-disconnected.hpp"
@@ -29,6 +32,8 @@
 #  include <resolv.h>
 #endif
 
+using namespace fmt::literals;
+
 namespace irccd {
 
 namespace state {
@@ -73,11 +78,10 @@
 void Connecting::prepare(Server &server, fd_set &setinput, fd_set &setoutput, net::Handle &maxfd)
 {
 	/*
-	 * The connect function will either fail if the hostname wasn't resolved
-	 * or if any of the internal functions fail.
+	 * The connect function will either fail if the hostname wasn't resolved or if any of the internal functions
+	 * fail.
 	 *
-	 * It returns success if the connection was successful but it does not
-	 * mean that connection is established.
+	 * It returns success if the connection was successful but it does not mean that connection is established.
 	 *
 	 * Because this function will be called repeatidly, the connection was started and we're still not
 	 * connected in the specified timeout time, we mark the server as disconnected.
@@ -89,16 +93,15 @@
 	if (m_started) {
 		const ServerSettings &settings = server.settings();
 
-		if (m_timer.elapsed() > static_cast<unsigned>(settings.reconnect_timeout * 1000)) {
+		if (m_timer.elapsed() > static_cast<unsigned>(settings.reconnectDelay * 1000)) {
 			log::warning() << "server " << info.name << ": timeout while connecting" << std::endl;
 			server.next(std::make_unique<state::Disconnected>());
 		} else if (!irc_is_connected(server.session())) {
 			log::warning() << "server " << info.name << ": error while connecting: ";
 			log::warning() << irc_strerror(irc_errno(server.session())) << std::endl;
 
-			if (settings.reconnect_tries != 0) {
-				log::warning() << "server " << info.name << ": retrying in "
-					       << settings.reconnect_timeout << " seconds" << std::endl;
+			if (settings.reconnectTries != 0) {
+				log::warning("server {}: retrying in {} seconds"_format(info.name, settings.reconnectDelay));
 			}
 
 			server.next(std::make_unique<state::Disconnected>());
--- a/lib/irccd/server-state-disconnected.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/server-state-disconnected.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -31,17 +31,17 @@
 	ServerSettings &settings = server.settings();
 	ServerCache &cache = server.cache();
 
-	if (settings.reconnect_tries == 0) {
+	if (settings.reconnectTries == 0) {
 		log::warning() << "server " << info.name << ": reconnection disabled, skipping" << std::endl;
 		server.onDie();
-	} else if (settings.reconnect_tries > 0 && cache.reconnect_current > settings.reconnect_tries) {
+	} else if (settings.reconnectTries > 0 && cache.reconnectCurrent > settings.reconnectTries) {
 		log::warning() << "server " << info.name << ": giving up" << std::endl;
 		server.onDie();
 	} else {
-		if (m_timer.elapsed() > static_cast<unsigned>(settings.reconnect_timeout * 1000)) {
+		if (m_timer.elapsed() > static_cast<unsigned>(settings.reconnectDelay * 1000)) {
 			irc_disconnect(server.session());
 
-			server.cache().reconnect_current ++;
+			server.cache().reconnectCurrent ++;
 			server.next(std::make_unique<state::Connecting>());
 		}
 	}
--- a/lib/irccd/server.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/server.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -21,6 +21,8 @@
 #include <cstring>
 #include <stdexcept>
 
+#include <format.h>
+
 #include <libirc_rfcnumeric.h>
 
 #include "logger.hpp"
@@ -29,6 +31,8 @@
 #include "server-state-connecting.hpp"
 #include "util.hpp"
 
+using namespace fmt::literals;
+
 namespace irccd {
 
 namespace {
@@ -81,18 +85,18 @@
 	}
 
 	int j = 0;
-	bool read_modes = true;
+	bool readModes = true;
 	for (size_t i = 0; i < buf.size(); ++i) {
 		if (buf[i] == '(') {
 			continue;
 		}
 		if (buf[i] == ')') {
 			j = 0;
-			read_modes = false;
+			readModes = false;
 			continue;
 		}
 
-		if (read_modes) {
+		if (readModes) {
 			table[j++].first = buf[i];
 		} else {
 			table[j++].second = buf[i];
@@ -115,10 +119,10 @@
 void Server::handleConnect(const char *, const char **) noexcept
 {
 	/* Reset the number of tried reconnection. */
-	m_cache.reconnect_current = 0;
+	m_cache.reconnectCurrent = 0;
 
 	/* Reset the timer. */
-	m_cache.ping_timer.reset();
+	m_cache.pingTimer.reset();
 
 	/* Don't forget to change state and notify. */
 	next(std::make_unique<state::Connected>());
@@ -225,7 +229,7 @@
 
 		/* The listing may add some prefixes, remove them if needed */
 		for (std::string u : users) {
-			m_cache.names_map[params[2]].insert(cleanPrefix(m_info, u));
+			m_cache.namesMap[params[2]].insert(cleanPrefix(m_info, u));
 		}
 	} else if (event == LIBIRC_RFC_RPL_ENDOFNAMES) {
 		/*
@@ -240,12 +244,12 @@
 			return;
 		}
 
-		auto it = m_cache.names_map.find(params[1]);
-		if (it != m_cache.names_map.end()) {
+		auto it = m_cache.namesMap.find(params[1]);
+		if (it != m_cache.namesMap.end()) {
 			onNames(params[1], it->second);
 
 			/* Don't forget to remove the list */
-			m_cache.names_map.erase(it);
+			m_cache.namesMap.erase(it);
 		}
 	} else if (event == LIBIRC_RFC_RPL_WHOISUSER) {
 		/*
@@ -269,7 +273,7 @@
 		info.host = strify(params[3]);
 		info.realname = strify(params[5]);
 
-		m_cache.whois_map.emplace(info.nick, info);
+		m_cache.whoisMap.emplace(info.nick, info);
 	} else if (event == LIBIRC_RFC_RPL_WHOISCHANNELS) {
 		/*
 		 * Called when we have received channels for one user.
@@ -282,8 +286,8 @@
 			return;
 		}
 
-		auto it = m_cache.whois_map.find(params[1]);
-		if (it != m_cache.whois_map.end()) {
+		auto it = m_cache.whoisMap.find(params[1]);
+		if (it != m_cache.whoisMap.end()) {
 			std::vector<std::string> channels = util::split(params[2], " \t");
 
 			/* Clean their prefixes */
@@ -303,12 +307,12 @@
 		 * params[2] == End of WHOIS list
 		 */
 
-		auto it = m_cache.whois_map.find(params[1]);
-		if (it != m_cache.whois_map.end()) {
+		auto it = m_cache.whoisMap.find(params[1]);
+		if (it != m_cache.whoisMap.end()) {
 			onWhois(it->second);
 
 			/* Don't forget to remove */
-			m_cache.whois_map.erase(it);
+			m_cache.whoisMap.erase(it);
 		}
 	} else if (event == /* RPL_BOUNCE */ 5) {
 		/*
@@ -331,10 +335,10 @@
 void Server::handlePing(const char *, const char **params) noexcept
 {
 	/* Reset the timer to detect disconnection. */
-	m_cache.ping_timer.reset();
+	m_cache.pingTimer.reset();
 
 	/* Don't forget to respond */
-	send(params[0]);
+	send("PONG {}"_format(params[0]));
 }
 
 void Server::handleQuery(const char *orig, const char **params) noexcept
@@ -442,12 +446,11 @@
 
 void Server::update() noexcept
 {
-	if (m_state_next) {
-		log::debug() << "server " << m_info.name << ": switching state "
-			     << m_state->ident() << " -> " << m_state_next->ident() << std::endl;
+	if (m_stateNext) {
+		log::debug("server {}: switch state {} -> {}"_format(m_info.name, m_state->ident(), m_stateNext->ident()));
 
-		m_state = std::move(m_state_next);
-		m_state_next = nullptr;
+		m_state = std::move(m_stateNext);
+		m_stateNext = nullptr;
 	}
 }
 
@@ -465,7 +468,7 @@
 	next(std::make_unique<state::Connecting>());
 }
 
-void Server::sync(fd_set &setinput, fd_set &setoutput) noexcept
+void Server::sync(fd_set &setinput, fd_set &setoutput)
 {
 	/*
 	 * 1. Send maximum of command possible if available for write
--- a/lib/irccd/server.hpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/server.hpp	Thu Apr 28 13:30:30 2016 +0200
@@ -36,16 +36,13 @@
 #include <vector>
 
 #include "elapsed-timer.hpp"
-#include "logger.hpp"
 #include "server-state.hpp"
 #include "signals.hpp"
-#include "sysconfig.hpp"
 
 namespace irccd {
 
 /**
- * \class ServerIdentity
- * \brief Identity to use when connecting
+ * \brief Identity to use when connecting.
  */
 class ServerIdentity {
 public:
@@ -57,8 +54,7 @@
 };
 
 /**
- * \class ServerChannel
- * \brief A channel to join with an optional password
+ * \brief A channel to join with an optional password.
  */
 class ServerChannel {
 public:
@@ -67,39 +63,29 @@
 };
 
 /**
- * List of channels.
- */
-using ServerChannels = std::vector<ServerChannel>;
-
-/**
- * \enum ServerChanMode
- * \brief Prefixes for nicknames
+ * \brief Prefixes for nicknames.
  */
 enum class ServerChanMode {
-	Creator		= 'O',			//!< Channel creator
-	HalfOperator	= 'h',			//!< Half operator
-	Operator	= 'o',			//!< Channel operator
-	Protection	= 'a',			//!< Unkillable
-	Voiced		= 'v'			//!< Voice power
+	Creator		= 'O',				//!< Channel creator
+	HalfOperator	= 'h',				//!< Half operator
+	Operator	= 'o',				//!< Channel operator
+	Protection	= 'a',				//!< Unkillable
+	Voiced		= 'v'				//!< Voice power
 };
 
 /**
- * \class ServerWhois
- * \brief Describe a whois information
- *
- * This is provided when whois command was requested.
+ * \brief Describe a whois information.
  */
 class ServerWhois {
 public:
-	std::string nick;			//!< user's nickname
-	std::string user;			//!< user's user
-	std::string host;			//!< hostname
-	std::string realname;			//!< realname
-	std::vector<std::string> channels;	//!< the channels where the user is
+	std::string nick;				//!< user's nickname
+	std::string user;				//!< user's user
+	std::string host;				//!< hostname
+	std::string realname;				//!< realname
+	std::vector<std::string> channels;		//!< the channels where the user is
 };
 
 /**
- * \class ServerInfo
  * \brief Server information
  *
  * This class contains everything needed to connect to a server.
@@ -107,39 +93,37 @@
 class ServerInfo {
 public:
 	enum {
-		Ipv6		= (1 << 0),	//!< Connect using IPv6
-		Ssl		= (1 << 1),	//!< Use SSL
-		SslVerify	= (1 << 2)	//!< Verify SSL
+		Ipv6		= (1 << 0),		//!< Connect using IPv6
+		Ssl		= (1 << 1),		//!< Use SSL
+		SslVerify	= (1 << 2)		//!< Verify SSL
 	};
 
-	std::string name;			//!< Server's name
-	std::string host;			//!< Hostname
-	std::string password;			//!< Optional server password
-	std::uint16_t port{6667};		//!< Server's port
-	std::uint8_t flags{0};			//!< Optional flags
-	std::map<ServerChanMode, char> modes;	//!< IRC modes (e.g. @~)
+	std::string name;				//!< Server's name
+	std::string host;				//!< Hostname
+	std::string password;				//!< Optional server password
+	std::uint16_t port{6667};			//!< Server's port
+	std::uint8_t flags{0};				//!< Optional flags
+	std::map<ServerChanMode, char> modes;		//!< IRC modes (e.g. @~)
 };
 
 /**
- * \class ServerSettings
  * \brief Contains settings to tweak the server
  *
- * This class contains additional settings that tweaks the
- * server operations.
+ * This class contains additional settings that tweaks the server operations.
  */
 class ServerSettings {
 public:
 	enum {
-		AutoRejoin	= (1 << 0),	//!< Auto rejoin a channel after being kicked
-		JoinInvite	= (1 << 1)	//!< Join a channel on invitation
+		AutoRejoin	= (1 << 0),		//!< Auto rejoin a channel after being kicked
+		JoinInvite	= (1 << 1)		//!< Join a channel on invitation
 	};
 
-	ServerChannels channels;		//!< List of channel to join
-	std::string command{"!"};		//!< The command character to trigger plugin command
-	std::int8_t reconnect_tries{-1};		//!< Number of tries to reconnect before giving up
-	std::uint16_t reconnect_timeout{30};		//!< Number of seconds to wait before trying to connect
-	std::uint8_t flags{0};			//!< Optional flags
-	std::uint16_t ping_timeout{300};	//!< Time in seconds before ping timeout is announced
+	std::vector<ServerChannel> channels;		//!< List of channel to join
+	std::string command{"!"};			//!< The command character to trigger plugin command
+	std::int8_t reconnectTries{-1};			//!< Number of tries to reconnect before giving up
+	std::uint16_t reconnectDelay{30};		//!< Number of seconds to wait before trying to connect
+	std::uint8_t flags{0};				//!< Optional flags
+	std::uint16_t pingTimeout{300};			//!< Time in seconds before ping timeout is announced
 };
 
 /**
@@ -147,37 +131,21 @@
  */
 class ServerCache {
 public:
-	/**
-	 * Track elapsed time for ping timeout.
-	 */
-	ElapsedTimer ping_timer;
-
-	/**
-	 * Number of reconnection already tested.
-	 */
-	std::int8_t reconnect_current{1};
+	ElapsedTimer pingTimer;				//!< Track elapsed time for ping timeout.
+	std::int8_t reconnectCurrent{1};		//!< Number of reconnection already tested.
 
 	/**
 	 * Map of names being build by channels.
 	 */
-	std::map<std::string, std::set<std::string>> names_map;
+	std::map<std::string, std::set<std::string>> namesMap;
 
 	/**
 	 * Map of whois being build by nicknames.
 	 */
-	std::map<std::string, ServerWhois> whois_map;
+	std::map<std::string, ServerWhois> whoisMap;
 };
 
 /**
- * Deferred command to send to the server.
- *
- * If the command returns true, it has been correctly buffered for outgoing
- * and removed from the queue.
- */
-using ServerCommand = std::function<bool ()>;
-
-/**
- * \class Server
  * \brief The class that connect to a IRC server
  *
  * The server is a class that stores callbacks which will be called on IRC events. It is the lowest part of the
@@ -199,34 +167,34 @@
 
 	/**
 	 * Signal: onChannelMode
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when someone changed the channel mode.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the channel
-	 * - the mode
-	 * - the optional mode argument
+	 * - the origin,
+	 * - the channel,
+	 * - the mode,
+	 * - the optional mode argument.
 	 */
 	Signal<std::string, std::string, std::string, std::string> onChannelMode;
 
 	/**
 	 * Signal: onChannelNotice
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when a notice has been sent on a channel.
 	 *
 	 * Arguments:
-	 * - the origin (the nickname who has sent the notice)
-	 * - the channel name
-	 * - the notice message
+	 *   - the origin (the nickname who has sent the notice),
+	 *   - the channel name,
+	 *   - the notice message.
 	 */
 	Signal<std::string, std::string, std::string> onChannelNotice;
 
 	/**
 	 * Signal: onConnect
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when the server is successfully connected.
 	 */
@@ -242,185 +210,185 @@
 
 	/**
 	 * Signal: onInvite
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when an invite has been sent to you (the bot).
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the channel
-	 * - your nickname
+	 *   - the origin,
+	 *   - the channel,
+	 *   - your nickname.
 	 */
 	Signal<std::string, std::string, std::string> onInvite;
 
 	/**
 	 * Signal: onJoin
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when a user has joined the channel, it also includes you.
 	 *
 	 * Arguments:
-	 * - the origin (may be you)
-	 * - the channel
+	 *   - the origin (may be you),
+	 *   - the channel.
 	 */
 	Signal<std::string, std::string> onJoin;
 
 	/**
 	 * Signal: onKick
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when someone has been kicked from a channel.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the channel
-	 * - the target who has been kicked
-	 * - the optional reason
+	 *   - the origin,
+	 *   - the channel,
+	 *   - the target who has been kicked,
+	 *   - the optional reason.
 	 */
 	Signal<std::string, std::string, std::string, std::string> onKick;
 
 	/**
 	 * ServerEvent: onMessage
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when a message on a channel has been sent.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the channel
-	 * - the message
+	 *   - the origin,
+	 *   - the channel,
+	 *   - the message.
 	 */
 	Signal<std::string, std::string, std::string> onMessage;
 
 	/**
 	 * Signal: onMe
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered on a CTCP Action.
 	 *
-	 * This is both used in a channel and in a private message so the target
-	 * may be a channel or your nickname.
+	 * This is both used in a channel and in a private message so the target may be a channel or your nickname.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the target
-	 * - the message
+	 *   - the origin,
+	 *   - the target,
+	 *   - the message.
 	 */
 	Signal<std::string, std::string, std::string> onMe;
 
 	/**
 	 * Signal: onMode
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when the server changed your user mode.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the mode (e.g +i)
+	 *   - the origin,
+	 *   - the mode (e.g +i).
 	 */
 	Signal<std::string, std::string> onMode;
 
 	/**
 	 * Signal: onNames
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when names listing has finished on a channel.
 	 *
 	 * Arguments:
-	 * - the channel
-	 * - the ordered list of names
+	 *   - the channel,
+	 *   - the ordered list of names.
 	 */
 	Signal<std::string, std::set<std::string>> onNames;
 
 	/**
 	 * Signal: onNick
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when someone changed its nickname, it also includes you.
 	 *
 	 * Arguments:
-	 * - the old nickname (may be you)
-	 * - the new nickname
+	 *   - the old nickname (may be you),
+	 *   - the new nickname.
 	 */
 	Signal<std::string, std::string> onNick;
 
 	/**
 	 * Signal: onNotice
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when someone has sent a notice to you.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the notice message
+	 *   - the origin,
+	 *   - the notice message.
 	 */
 	Signal<std::string, std::string> onNotice;
 
 	/**
 	 * Signal: onPart
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when someone has left the channel.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the channel that the nickname has left
-	 * - the optional reason
+	 *   - the origin,
+	 *   - the channel that the nickname has left,
+	 *   - the optional reason.
 	 */
 	Signal<std::string, std::string, std::string> onPart;
 
 	/**
 	 * Signal: onQuery
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when someone has sent you a private message.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the message
+	 *   - the origin,
+	 *   - the message.
 	 */
 	Signal<std::string, std::string> onQuery;
 
 	/**
 	 * Signal: onTopic
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when someone changed the channel topic.
 	 *
 	 * Arguments:
-	 * - the origin
-	 * - the channel
-	 * - the new topic
+	 *   - the origin,
+	 *   - the channel,
+	 *   - the new topic.
 	 */
 	Signal<std::string, std::string, std::string> onTopic;
 
 	/**
 	 * Signal: onWhois
-	 * ------------------------------------------------
+	 * ----------------------------------------------------------
 	 *
 	 * Triggered when whois information has been received.
 	 *
 	 * Arguments:
-	 * - the whois object
+	 *   - the whois object.
 	 */
 	Signal<ServerWhois> onWhois;
 
 private:
-	using SessionPtr = std::unique_ptr<Session>;
-	using Queue = std::queue<ServerCommand>;
-
-private:
 	ServerInfo m_info;
 	ServerSettings m_settings;
 	ServerIdentity m_identity;
 	ServerCache m_cache;
-	SessionPtr m_session;
-	Queue m_queue;
+
+	/* queue of requests to send */
+	std::queue<std::function<bool ()>> m_queue;
+
+	/* libircclient session (bridge) */
+	std::unique_ptr<Session> m_session;
 
 	/* States */
 	std::unique_ptr<ServerState> m_state;
-	std::unique_ptr<ServerState> m_state_next;
+	std::unique_ptr<ServerState> m_stateNext;
 
+	/* Handle libircclient callbacks */
 	void handleChannel(const char *, const char **) noexcept;
 	void handleChannelMode(const char *, const char **) noexcept;
 	void handleChannelNotice(const char *, const char **) noexcept;
@@ -462,88 +430,6 @@
 	virtual ~Server();
 
 	/**
-	 * Set the next state, it is not changed immediately but on next iteration.
-	 *
-	 * \param state the new state
-	 */
-	inline void next(std::unique_ptr<ServerState> state) noexcept
-	{
-		m_state_next = std::move(state);
-	}
-
-	/**
-	 * Switch to next state if it has.
-	 *
-	 * If the server is installed into irccd, it is called automatically.
-	 *
-	 * \warning Not thread-safe
-	 */
-	void update() noexcept;
-
-	/**
-	 * Request to disconnect. This function does not notify the
-	 * ServerService.
-	 *
-	 * \see Irccd::serverDisconnect
-	 * \note Thread-safe
-	 */
-	void disconnect() noexcept;
-
-	/**
-	 * Asks for a reconnection. This function does not notify the
-	 * ServerService.
-	 *
-	 * \see Irccd::serverReconnect
-	 * \note Thread-safe
-	 */
-	void reconnect() noexcept;
-
-	/**
-	 * Flush the pending commands if possible. This function will send
-	 * as much as possible commands.
-	 *
-	 * If the server is installed into the ServerManager, it is called
-	 * automatically.
-	 *
-	 * \note Thread-safe
-	 */
-	void flush() noexcept;
-
-	/**
-	 * Prepare the IRC session.
-	 *
-	 * \warning Not thread-safe
-	 */
-	inline void prepare(fd_set &setinput, fd_set &setoutput, net::Handle &maxfd) noexcept
-	{
-		m_state->prepare(*this, setinput, setoutput, maxfd);
-	}
-
-	/**
-	 * Process incoming/outgoing data after selection.
-	 *
-	 * If the server is installed into the ServerManager, it is called
-	 * automatically.
-	 *
-	 * \param setinput
-	 * \param setoutput
-	 * \throw any exception that have been throw from user functions
-	 */
-	void sync(fd_set &setinput, fd_set &setoutput) noexcept;
-
-	/**
-	 * Get the server information.
-	 *
-	 * \warning This overload should not be used by the user, it is required to
-	 *          update the nickname.
-	 * \return the server information
-	 */
-	inline ServerInfo &info() noexcept
-	{
-		return m_info;
-	}
-
-	/**
 	 * Get the server information.
 	 *
 	 * \return the server information
@@ -554,21 +440,9 @@
 	}
 
 	/**
-	 * Access the cache.
-	 *
-	 * \return the cache
-	 * \warning use with care
-	 */
-	inline ServerCache &cache() noexcept
-	{
-		return m_cache;
-	}
-
-	/**
 	 * Get the server settings.
 	 *
-	 * \warning This overload should not be used by the user, it is required to
-	 *          update the reconnection information.
+	 * \note some settings will be used only after the next reconnection
 	 * \return the settings
 	 */
 	inline ServerSettings &settings() noexcept
@@ -587,17 +461,7 @@
 	}
 
 	/**
-	 * Get the identity.
-	 *
-	 * \return the identity
-	 */
-	inline ServerIdentity &identity() noexcept
-	{
-		return m_identity;
-	}
-
-	/**
-	 * Overloaded function
+	 * Access the identity.
 	 *
 	 * \return the identity
 	 */
@@ -607,6 +471,17 @@
 	}
 
 	/**
+	 * Access the cache.
+	 *
+	 * \return the cache
+	 * \warning use with care
+	 */
+	inline ServerCache &cache() noexcept
+	{
+		return m_cache;
+	}
+
+	/**
 	 * Get the private session.
 	 *
 	 * \return the session
@@ -617,6 +492,50 @@
 	}
 
 	/**
+	 * Set the next state, it is not changed immediately but on next iteration.
+	 *
+	 * \param state the new state
+	 */
+	inline void next(std::unique_ptr<ServerState> state) noexcept
+	{
+		m_stateNext = std::move(state);
+	}
+
+	/**
+	 * Switch to next state if it has.
+	 */
+	void update() noexcept;
+
+	/**
+	 * Force disconnection.
+	 */
+	void disconnect() noexcept;
+
+	/**
+	 * Asks for a reconnection.
+	 */
+	void reconnect() noexcept;
+
+	/**
+	 * Prepare the IRC session.
+	 *
+	 * \warning Not thread-safe
+	 */
+	inline void prepare(fd_set &setinput, fd_set &setoutput, net::Handle &maxfd) noexcept
+	{
+		m_state->prepare(*this, setinput, setoutput, maxfd);
+	}
+
+	/**
+	 * Process incoming/outgoing data after selection.
+	 *
+	 * \param setinput
+	 * \param setoutput
+	 * \throw any exception that have been throw from user functions
+	 */
+	void sync(fd_set &setinput, fd_set &setoutput);
+
+	/**
 	 * Determine if the nickname is the bot itself.
 	 *
 	 * \param nick the nickname to check
@@ -629,7 +548,6 @@
 	 *
 	 * \param channel the channel
 	 * \param mode the new mode
-	 * \note Thread-safe
 	 */
 	void cmode(std::string channel, std::string mode);
 
@@ -638,7 +556,6 @@
 	 *
 	 * \param channel the channel
 	 * \param message message notice
-	 * \note Thread-safe
 	 */
 	void cnotice(std::string channel, std::string message);
 
@@ -647,7 +564,6 @@
 	 *
 	 * \param target the target nickname
 	 * \param channel the channel
-	 * \note Thread-safe
 	 */
 	void invite(std::string target, std::string channel);
 
@@ -656,7 +572,6 @@
 	 *
 	 * \param channel the channel to join
 	 * \param password the optional password
-	 * \note Thread-safe
 	 */
 	void join(std::string channel, std::string password = "");
 
@@ -667,7 +582,6 @@
 	 * \param target the target to kick
 	 * \param channel from which channel
 	 * \param reason the optional reason
-	 * \note Thread-safe
 	 */
 	void kick(std::string target, std::string channel, std::string reason = "");
 
@@ -677,7 +591,6 @@
 	 *
 	 * \param target the nickname or the channel
 	 * \param message the message
-	 * \note Thread-safe
 	 */
 	void me(std::string target, std::string message);
 
@@ -686,7 +599,6 @@
 	 *
 	 * \param target the target
 	 * \param message the message
-	 * \note Thread-safe
 	 */
 	void message(std::string target, std::string message);
 
@@ -694,7 +606,6 @@
 	 * Change your user mode.
 	 *
 	 * \param mode the mode
-	 * \note Thread-safe
 	 */
 	void mode(std::string mode);
 
@@ -702,7 +613,6 @@
 	 * Request the list of names.
 	 *
 	 * \param channel the channel
-	 * \note Thread-safe
 	 */
 	void names(std::string channel);
 
@@ -710,7 +620,6 @@
 	 * Change your nickname.
 	 *
 	 * \param newnick the new nickname to use
-	 * \note Thread-safe
 	 */
 	void nick(std::string newnick);
 
@@ -719,7 +628,6 @@
 	 *
 	 * \param target the target
 	 * \param message the notice message
-	 * \note Thread-safe
 	 */
 	void notice(std::string target, std::string message);
 
@@ -730,7 +638,6 @@
 	 *
 	 * \param channel the channel to leave
 	 * \param reason the optional reason
-	 * \note Thread-safe
 	 */
 	void part(std::string channel, std::string reason = "");
 
@@ -740,7 +647,6 @@
 	 *
 	 * \warning Use this function with care
 	 * \param raw the raw message (without `\r\n\r\n`)
-	 * \note Thread-safe
 	 */
 	void send(std::string raw);
 
@@ -749,7 +655,6 @@
 	 *
 	 * \param channel the channel
 	 * \param topic the desired topic
-	 * \note Thread-safe
 	 */
 	void topic(std::string channel, std::string topic);
 
@@ -757,7 +662,6 @@
 	 * Request for whois information.
 	 *
 	 * \param target the target nickname
-	 * \note Thread-safe
 	 */
 	void whois(std::string target);
 };
--- a/lib/irccd/transport-server.cpp	Wed Apr 27 21:52:00 2016 +0200
+++ b/lib/irccd/transport-server.cpp	Thu Apr 28 13:30:30 2016 +0200
@@ -24,6 +24,7 @@
 
 #include <sstream>
 
+#include "logger.hpp"
 #include "transport-server.hpp"
 
 namespace irccd {