changeset 79:d0deb5835aed

Irccd: use bridge for libircclient to hide dependency, #449
author David Demelier <markand@malikania.fr>
date Wed, 30 Mar 2016 21:30:53 +0200
parents d58e45e3515f
children 98bd4d6355fe
files lib/irccd/CMakeSources.cmake lib/irccd/js-server.h lib/irccd/server-private.h lib/irccd/server-state.cpp lib/irccd/server-state.h lib/irccd/server.cpp lib/irccd/server.h
diffstat 7 files changed, 231 insertions(+), 156 deletions(-) [+]
line wrap: on
line diff
--- a/lib/irccd/CMakeSources.cmake	Wed Mar 30 20:56:22 2016 +0200
+++ b/lib/irccd/CMakeSources.cmake	Wed Mar 30 21:30:53 2016 +0200
@@ -57,6 +57,7 @@
 	${CMAKE_CURRENT_LIST_DIR}/plugin.h
 	${CMAKE_CURRENT_LIST_DIR}/rule.h
 	${CMAKE_CURRENT_LIST_DIR}/server.h
+	${CMAKE_CURRENT_LIST_DIR}/server-private.h
 	${CMAKE_CURRENT_LIST_DIR}/server-state.h
 	${CMAKE_CURRENT_LIST_DIR}/sockets.h
 	${CMAKE_CURRENT_LIST_DIR}/system.h
--- a/lib/irccd/js-server.h	Wed Mar 30 20:56:22 2016 +0200
+++ b/lib/irccd/js-server.h	Wed Mar 30 21:30:53 2016 +0200
@@ -35,7 +35,7 @@
 	static inline void prototype(ContextPtr ctx)
 	{
 		getGlobal<void>(ctx, "Irccd");
-		getGlobal<void>(ctx, "Server");
+		getProperty<void>(ctx, -1, "Server");
 		getProperty<void>(ctx, -1, "prototype");
 		remove(ctx, -2);
 		remove(ctx, -2);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/irccd/server-private.h	Wed Mar 30 21:30:53 2016 +0200
@@ -0,0 +1,64 @@
+/*
+ * server-private.h -- libircclient bridge
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef IRCCD_SERVER_PRIVATE_H
+#define IRCCD_SERVER_PRIVATE_H
+
+#include <memory>
+
+#include <libircclient.h>
+
+#include "server.h"
+
+namespace irccd {
+
+/**
+ * @brief Bridge for libircclient
+ */
+class Server::Session {
+public:
+	using Handle = std::unique_ptr<irc_session_t, void (*)(irc_session_t *)>;
+
+private:
+	Handle m_handle;
+
+public:
+	inline Session()
+		: m_handle(nullptr, nullptr)
+	{
+	}
+
+	inline operator const irc_session_t *() const noexcept
+	{
+		return m_handle.get();
+	}
+
+	inline operator irc_session_t *() noexcept
+	{
+		return m_handle.get();
+	}
+
+	inline Handle &handle() noexcept
+	{
+		return m_handle;
+	}
+};
+
+} // !irccd
+
+#endif // !IRCCD_SERVER_PRIVATE_H
--- a/lib/irccd/server-state.cpp	Wed Mar 30 20:56:22 2016 +0200
+++ b/lib/irccd/server-state.cpp	Wed Mar 30 21:30:53 2016 +0200
@@ -28,7 +28,7 @@
 #endif
 
 #include "server-state.h"
-#include "server.h"
+#include "server-private.h"
 
 namespace irccd {
 
--- a/lib/irccd/server-state.h	Wed Mar 30 20:56:22 2016 +0200
+++ b/lib/irccd/server-state.h	Wed Mar 30 21:30:53 2016 +0200
@@ -16,8 +16,8 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifndef _IRCCD_SERVER_STATE_H_
-#define _IRCCD_SERVER_STATE_H_
+#ifndef IRCCD_SERVER_STATE_H
+#define IRCCD_SERVER_STATE_H
 
 #include <irccd-config.h>
 
@@ -73,5 +73,4 @@
 
 } // !irccd
 
-#endif // !_IRCCD_SERVER_STATE_H_
-
+#endif // !IRCCD_SERVER_STATE_H
--- a/lib/irccd/server.cpp	Wed Mar 30 20:56:22 2016 +0200
+++ b/lib/irccd/server.cpp	Wed Mar 30 21:30:53 2016 +0200
@@ -24,7 +24,7 @@
 #include <libirc_rfcnumeric.h>
 
 #include "logger.h"
-#include "server.h"
+#include "server-private.h"
 #include "util.h"
 
 namespace irccd {
@@ -310,7 +310,7 @@
 	: m_info(std::move(info))
 	, m_settings(std::move(settings))
 	, m_identity(std::move(identity))
-	, m_session(nullptr, nullptr)
+	, m_session(std::make_unique<Session>())
 	, m_state(ServerState::Connecting)
 	, m_next(ServerState::Undefined)
 {
@@ -374,16 +374,16 @@
 		static_cast<Server *>(irc_get_ctx(session))->handleMode(orig, params);
 	};
 
-	m_session = Session{irc_create_session(&callbacks), irc_destroy_session};
+	m_session->handle() = Session::Handle{irc_create_session(&callbacks), irc_destroy_session};
 
 	/* Save this to the session */
-	irc_set_ctx(m_session.get(), this);
-	irc_set_ctcp_version(m_session.get(), m_identity.ctcpversion.c_str());
+	irc_set_ctx(*m_session, this);
+	irc_set_ctcp_version(*m_session, m_identity.ctcpversion.c_str());
 }
 
 Server::~Server()
 {
-	irc_disconnect(m_session.get());
+	irc_disconnect(*m_session);
 }
 
 void Server::update() noexcept
@@ -410,6 +410,20 @@
 	}
 }
 
+void Server::disconnect() noexcept
+{
+	using namespace std::placeholders;
+
+	irc_disconnect(*m_session);
+	onDie();
+}
+
+void Server::reconnect() noexcept
+{
+	irc_disconnect(*m_session);
+	next(ServerState::Type::Connecting);
+}
+
 void Server::sync(fd_set &setinput, fd_set &setoutput) noexcept
 {
 	/*
@@ -428,7 +442,117 @@
 	}
 
 	/* 2. Read data */
-	irc_process_select_descriptors(m_session.get(), &setinput, &setoutput);
+	irc_process_select_descriptors(*m_session, &setinput, &setoutput);
+}
+
+void Server::cmode(std::string channel, std::string mode)
+{
+	m_queue.push([=] () {
+		return irc_cmd_channel_mode(*m_session, channel.c_str(), mode.c_str()) == 0;
+	});
+}
+
+void Server::cnotice(std::string channel, std::string message) noexcept
+{
+	m_queue.push([=] () {
+		return irc_cmd_notice(*m_session, channel.c_str(), message.c_str()) == 0;
+	});
+}
+
+void Server::invite(std::string target, std::string channel) noexcept
+{
+	m_queue.push([=] () {
+		return irc_cmd_invite(*m_session, target.c_str(), channel.c_str()) == 0;
+	});
+}
+
+void Server::join(std::string channel, std::string password) noexcept
+{
+	m_queue.push([=] () {
+		const char *ptr = password.empty() ? nullptr : password.c_str();
+
+		return irc_cmd_join(*m_session, channel.c_str(), ptr) == 0;
+	});
+}
+
+void Server::kick(std::string target, std::string channel, std::string reason) noexcept
+{
+	m_queue.push([=] () {
+		return irc_cmd_kick(*m_session, target.c_str(), channel.c_str(), reason.c_str()) == 0;
+	});
+}
+
+void Server::me(std::string target, std::string message)
+{
+	m_queue.push([=] () {
+		return irc_cmd_me(*m_session, target.c_str(), message.c_str()) == 0;
+	});
+}
+
+void Server::message(std::string target, std::string message)
+{
+	m_queue.push([=] () {
+		return irc_cmd_msg(*m_session, target.c_str(), message.c_str()) == 0;
+	});
+}
+
+void Server::mode(std::string mode)
+{
+	m_queue.push([=] () {
+		return irc_cmd_user_mode(*m_session, mode.c_str()) == 0;
+	});
+}
+
+void Server::names(std::string channel)
+{
+	m_queue.push([=] () {
+		return irc_cmd_names(*m_session, channel.c_str()) == 0;
+	});
+}
+
+void Server::nick(std::string newnick)
+{
+	m_queue.push([=] () {
+		return irc_cmd_nick(*m_session, newnick.c_str()) == 0;
+	});
+}
+
+void Server::notice(std::string target, std::string message)
+{
+	m_queue.push([=] () {
+		return irc_cmd_notice(*m_session, target.c_str(), message.c_str()) == 0;
+	});
+}
+
+void Server::part(std::string channel, std::string reason)
+{
+	m_queue.push([=] () -> bool {
+		if (reason.empty())
+			return irc_cmd_part(*m_session, channel.c_str()) == 0;
+
+		return irc_send_raw(*m_session, "PART %s :%s", channel.c_str(), reason.c_str());
+	});
+}
+
+void Server::send(std::string raw)
+{
+	m_queue.push([=] () {
+		return irc_send_raw(*m_session, raw.c_str()) == 0;
+	});
+}
+
+void Server::topic(std::string channel, std::string topic)
+{
+	m_queue.push([=] () {
+		return irc_cmd_topic(*m_session, channel.c_str(), topic.c_str()) == 0;
+	});
+}
+
+void Server::whois(std::string target)
+{
+	m_queue.push([=] () {
+		return irc_cmd_whois(*m_session, target.c_str()) == 0;
+	});
 }
 
 } // !irccd
--- a/lib/irccd/server.h	Wed Mar 30 20:56:22 2016 +0200
+++ b/lib/irccd/server.h	Wed Mar 30 21:30:53 2016 +0200
@@ -16,8 +16,8 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifndef _IRCCD_SERVER_H_
-#define _IRCCD_SERVER_H_
+#ifndef IRCCD_SERVER_H
+#define IRCCD_SERVER_H
 
 #include <cstdint>
 #include <functional>
@@ -30,8 +30,6 @@
 #include <utility>
 #include <vector>
 
-#include <libircclient.h>
-
 #include <irccd-config.h>
 
 #include "logger.h"
@@ -40,12 +38,6 @@
 
 namespace irccd {
 
-namespace js {
-
-class Context;
-
-} // !js
-
 /**
  * @class ServerIdentity
  * @brief Identity to use when connecting
@@ -172,6 +164,11 @@
 class Server {
 public:
 	/**
+	 * Bridge for libircclient.
+	 */
+	class Session;
+
+	/**
 	 * Signal: onChannelMode
 	 * ------------------------------------------------
 	 *
@@ -380,7 +377,7 @@
 	Signal<ServerWhois> onWhois;
 
 private:
-	using Session = std::unique_ptr<irc_session_t, void (*)(irc_session_t *)>;
+	using SessionPtr = std::unique_ptr<Session>;
 	using Queue = std::queue<ServerCommand>;
 
 	/**
@@ -397,7 +394,7 @@
 	ServerInfo m_info;
 	ServerSettings m_settings;
 	ServerIdentity m_identity;
-	Session m_session;
+	SessionPtr m_session;
 	ServerState m_state;
 	ServerState m_next;
 	Queue m_queue;
@@ -489,13 +486,7 @@
 	 * @see Irccd::serverDisconnect
 	 * @note Thread-safe
 	 */
-	inline void disconnect() noexcept
-	{
-		using namespace std::placeholders;
-
-		irc_disconnect(m_session.get());
-		onDie();
-	}
+	void disconnect() noexcept;
 
 	/**
 	 * Asks for a reconnection. This function does not notify the
@@ -504,11 +495,7 @@
 	 * @see Irccd::serverReconnect
 	 * @note Thread-safe
 	 */
-	inline void reconnect() noexcept
-	{
-		irc_disconnect(m_session.get());
-		next(ServerState::Type::Connecting);
-	}
+	void reconnect() noexcept;
 
 	/**
 	 * Flush the pending commands if possible. This function will send
@@ -621,14 +608,13 @@
 	}
 
 	/**
-	 * Get the libircclient session.
+	 * Get the private session.
 	 *
-	 * @warning Do not use this function, it is only required for ServerState's
 	 * @return the session
 	 */
-	inline irc_session_t *session() noexcept
+	inline Session &session() noexcept
 	{
-		return m_session.get();
+		return *m_session;
 	}
 
 	/**
@@ -638,12 +624,7 @@
 	 * @param mode the new mode
 	 * @note Thread-safe
 	 */
-	inline void cmode(std::string channel, std::string mode)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_channel_mode(m_session.get(), channel.c_str(), mode.c_str()) == 0;
-		});
-	}
+	void cmode(std::string channel, std::string mode);
 
 	/**
 	 * Send a channel notice.
@@ -652,12 +633,7 @@
 	 * @param message message notice
 	 * @note Thread-safe
 	 */
-	inline void cnotice(std::string channel, std::string message) noexcept
-	{
-		m_queue.push([=] () {
-			return irc_cmd_notice(this->m_session.get(), channel.c_str(), message.c_str()) == 0;
-		});
-	}
+	void cnotice(std::string channel, std::string message) noexcept;
 
 	/**
 	 * Invite a user to a channel.
@@ -666,12 +642,7 @@
 	 * @param channel the channel
 	 * @note Thread-safe
 	 */
-	inline void invite(std::string target, std::string channel) noexcept
-	{
-		m_queue.push([=] () {
-			return irc_cmd_invite(this->m_session.get(), target.c_str(), channel.c_str()) == 0;
-		});
-	}
+	void invite(std::string target, std::string channel) noexcept;
 
 	/**
 	 * Join a channel, the password is optional and can be kept empty.
@@ -680,14 +651,7 @@
 	 * @param password the optional password
 	 * @note Thread-safe
 	 */
-	inline void join(std::string channel, std::string password = "") noexcept
-	{
-		m_queue.push([=] () {
-			const char *ptr = password.empty() ? nullptr : password.c_str();
-
-			return irc_cmd_join(this->m_session.get(), channel.c_str(), ptr) == 0;
-		});
-	}
+	void join(std::string channel, std::string password = "") noexcept;
 
 	/**
 	 * Kick someone from the channel. Please be sure to have the rights
@@ -698,12 +662,7 @@
 	 * @param reason the optional reason
 	 * @note Thread-safe
 	 */
-	inline void kick(std::string target, std::string channel, std::string reason = "") noexcept
-	{
-		m_queue.push([=] () {
-			return irc_cmd_kick(this->m_session.get(), target.c_str(), channel.c_str(), reason.c_str()) == 0;
-		});
-	}
+	void kick(std::string target, std::string channel, std::string reason = "") noexcept;
 
 	/**
 	 * Send a CTCP Action as known as /me. The target may be either a
@@ -713,12 +672,7 @@
 	 * @param message the message
 	 * @note Thread-safe
 	 */
-	inline void me(std::string target, std::string message)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_me(m_session.get(), target.c_str(), message.c_str()) == 0;
-		});
-	}
+	void me(std::string target, std::string message);
 
 	/**
 	 * Send a message to the specified target or channel.
@@ -727,12 +681,7 @@
 	 * @param message the message
 	 * @note Thread-safe
 	 */
-	inline void message(std::string target, std::string message)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_msg(m_session.get(), target.c_str(), message.c_str()) == 0;
-		});
-	}
+	void message(std::string target, std::string message);
 
 	/**
 	 * Change your user mode.
@@ -740,12 +689,7 @@
 	 * @param mode the mode
 	 * @note Thread-safe
 	 */
-	inline void mode(std::string mode)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_user_mode(m_session.get(), mode.c_str()) == 0;
-		});
-	}
+	void mode(std::string mode);
 
 	/**
 	 * Request the list of names.
@@ -753,12 +697,7 @@
 	 * @param channel the channel
 	 * @note Thread-safe
 	 */
-	inline void names(std::string channel)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_names(m_session.get(), channel.c_str()) == 0;
-		});
-	}
+	void names(std::string channel);
 
 	/**
 	 * Change your nickname.
@@ -766,12 +705,7 @@
 	 * @param newnick the new nickname to use
 	 * @note Thread-safe
 	 */
-	inline void nick(std::string newnick)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_nick(m_session.get(), newnick.c_str()) == 0;
-		});
-	}
+	void nick(std::string newnick);
 
 	/**
 	 * Send a private notice.
@@ -780,12 +714,7 @@
 	 * @param message the notice message
 	 * @note Thread-safe
 	 */
-	inline void notice(std::string target, std::string message)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_notice(m_session.get(), target.c_str(), message.c_str()) == 0;
-		});
-	}
+	void notice(std::string target, std::string message);
 
 	/**
 	 * Part from a channel.
@@ -796,15 +725,7 @@
 	 * @param reason the optional reason
 	 * @note Thread-safe
 	 */
-	inline void part(std::string channel, std::string reason = "")
-	{
-		m_queue.push([=] () -> bool {
-			if (reason.empty())
-				return irc_cmd_part(m_session.get(), channel.c_str()) == 0;
-
-			return irc_send_raw(m_session.get(), "PART %s :%s", channel.c_str(), reason.c_str());
-		});
-	}
+	void part(std::string channel, std::string reason = "");
 
 	/**
 	 * Send a raw message to the IRC server. You don't need to add
@@ -814,12 +735,7 @@
 	 * @param raw the raw message (without \r\n\r\n)
 	 * @note Thread-safe
 	 */
-	inline void send(std::string raw)
-	{
-		m_queue.push([=] () {
-			return irc_send_raw(m_session.get(), raw.c_str()) == 0;
-		});
-	}
+	void send(std::string raw);
 
 	/**
 	 * Change the channel topic.
@@ -828,12 +744,7 @@
 	 * @param topic the desired topic
 	 * @note Thread-safe
 	 */
-	inline void topic(std::string channel, std::string topic)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_topic(m_session.get(), channel.c_str(), topic.c_str()) == 0;
-		});
-	}
+	void topic(std::string channel, std::string topic);
 
 	/**
 	 * Request for whois information.
@@ -841,33 +752,9 @@
 	 * @param target the target nickname
 	 * @note Thread-safe
 	 */
-	inline void whois(std::string target)
-	{
-		m_queue.push([=] () {
-			return irc_cmd_whois(this->m_session.get(), target.c_str()) == 0;
-		});
-	}
-
-#if defined(WITH_JS)
-	/**
-	 * Get the object signature.
-	 *
-	 * @return the signature
-	 */
-	static inline const char *name() noexcept
-	{
-		return "\xff""\xff""Server";
-	}
-
-	/**
-	 * Push the JavaScript prototype for a Server.
-	 *
-	 * @param ctx the context
-	 */
-	void prototype(js::Context &ctx);
-#endif
+	void whois(std::string target);
 };
 
 } // !irccd
 
-#endif // !_IRCCD_SERVER_H_
+#endif // !IRCCD_SERVER_H