[PATCH 2/5] PROXY/SIMPLE: added simple proxy method

Enrico Scholz <[email protected]> Wed, 27 Feb 2008 11:38:20 +0100
Newsgroups gmane.network.irc.irssi.devel
Message-ID <1820286a9f5d52d29bad99161a9da39cabbd7527.1204046798.git.enrico.scholz@informatik.tu-chemnitz.de>
This method implements the string + string_after mechanism implemented by
previous irssi versions.

To use, set
* proxy_type to 'simple' or keep it empty
* string + string_after in the known ways

Signed-off-by: Enrico Scholz <[email protected]>
---
 src/core/network-proxy-simple.c |  121 +++++++++++++++++++++++++++++++++++++++
 src/core/network-proxy-simple.h |   32 ++++++++++
 2 files changed, 153 insertions(+), 0 deletions(-)
 create mode 100644 src/core/network-proxy-simple.c
 create mode 100644 src/core/network-proxy-simple.h

diff --git a/src/core/network-proxy-simple.c b/src/core/network-proxy-simple.c
new file mode 100644
index 0000000..a7ac1fa
--- /dev/null
+++ b/src/core/network-proxy-simple.c
@@ -0,0 +1,121 @@
+/*	--*- c -*--
+ * Copyright (C) 2008 Enrico Scholz <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 and/or 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "module.h"
+#include "network-proxy-simple.h"
+
+#include "network-proxy-priv.h"
+#include "network.h"
+
+static void
+network_proxy_simple_destroy(struct network_proxy *proxy)
+{
+	struct _network_proxy_simple	*self = container_of(proxy, struct _network_proxy_simple, proxy);
+
+	g_free((void *)self->password);
+	g_free((void *)self->string_after);
+	g_free((void *)self->string);
+
+	_network_proxy_destroy(proxy);
+
+	g_free(self);
+}
+
+static struct network_proxy *
+network_proxy_simple_clone(struct network_proxy const *proxy)
+{
+	struct _network_proxy_simple	*self = container_of(proxy, struct _network_proxy_simple, proxy);
+	struct _network_proxy_simple	*res;
+
+	res = g_malloc0(sizeof *res);
+
+	_network_proxy_clone(&res->proxy, &self->proxy);
+
+	res->string       = g_strdup(self->string);
+	res->string_after = g_strdup(self->string_after);
+	res->password     = g_strdup(self->password);
+	return &res->proxy;
+}
+
+static GIOChannel *
+network_proxy_simple_connect(struct network_proxy const *proxy, IPADDR const *hint_ip,
+			     char const *address, int port)
+{
+	struct _network_proxy_simple	*self = container_of(proxy, struct _network_proxy_simple, proxy);
+
+	(void)address;
+	(void)port;
+	if (hint_ip)
+		return net_connect_ip(hint_ip, self->proxy.port, NULL);
+	else
+		return net_connect(self->proxy.host, self->proxy.port, NULL);
+}
+
+static void
+network_proxy_simple_send_string(struct network_proxy const *proxy,
+				 struct network_proxy_send_string_info const *info)
+{
+	struct _network_proxy_simple	*self = container_of(proxy, struct _network_proxy_simple, proxy);
+	char				*cmd;
+
+	if (self->password && self->password[0]) {
+		cmd = g_strdup_printf("PASS %s", self->password);
+		info->func(info->obj, cmd);
+		g_free(cmd);
+	}
+
+	if (self->string && self->string[0]) {
+		cmd = g_strdup_printf(self->string, info->host, info->port);
+		info->func(info->obj, cmd);
+		g_free(cmd);
+	}
+}
+
+static void
+network_proxy_simple_send_string_after(struct network_proxy const *proxy,
+				 struct network_proxy_send_string_info const *info)
+{
+	struct _network_proxy_simple	*self = container_of(proxy, struct _network_proxy_simple, proxy);
+	char				*cmd;
+
+	if (self->string_after && self->string_after[0]) {
+		cmd = g_strdup_printf(self->string_after, info->host, info->port);
+		info->func(info->obj, cmd);
+		g_free(cmd);
+	}
+}
+
+struct network_proxy *
+_network_proxy_simple_create(void)
+{
+	struct _network_proxy_simple	*res;
+
+	res = g_malloc0(sizeof *res);
+
+	_network_proxy_create(&res->proxy);
+	res->string        = g_strdup(settings_get_str("proxy_string"));
+	res->string_after  = g_strdup(settings_get_str("proxy_string_after"));
+	res->password      = g_strdup(settings_get_str("proxy_password"));
+
+	res->proxy.destroy = network_proxy_simple_destroy;
+	res->proxy.connect = network_proxy_simple_connect;
+	res->proxy.clone   = network_proxy_simple_clone;
+
+	res->proxy.send_string       = network_proxy_simple_send_string;
+	res->proxy.send_string_after = network_proxy_simple_send_string_after;
+
+	return &res->proxy;
+}
diff --git a/src/core/network-proxy-simple.h b/src/core/network-proxy-simple.h
new file mode 100644
index 0000000..b2132cf
--- /dev/null
+++ b/src/core/network-proxy-simple.h
@@ -0,0 +1,32 @@
+/*	--*- c -*--
+ * Copyright (C) 2008 Enrico Scholz <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 and/or 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef H_IRSSI_SRC_CORE_PROXY_SIMPLE_H
+#define H_IRSSI_SRC_CORE_PROXY_SIMPLE_H
+
+#include "network-proxy.h"
+
+struct _network_proxy_simple {
+	struct network_proxy	proxy;
+
+	char const		*string_after;
+	char const		*string;
+	char const		*password;
+};
+
+struct network_proxy *		_network_proxy_simple_create(void);
+
+#endif	/* H_IRSSI_SRC_CORE_PROXY_SIMPLE_H */
-- 
1.5.4.1