diff --git a/src/common.h b/src/common.h index 0a7b72f0..aa7e97e9 100644 --- a/src/common.h +++ b/src/common.h @@ -103,6 +103,8 @@ typedef struct _RAWLOG_REC RAWLOG_REC; typedef struct _CHAT_PROTOCOL_REC CHAT_PROTOCOL_REC; typedef struct _CHATNET_REC CHATNET_REC; +typedef struct _PROXY_REC PROXY_REC; +typedef struct _PROXY_PROTOCOL_REC PROXY_PROTOCOL_REC; typedef struct _SERVER_REC SERVER_REC; typedef struct _WI_ITEM_REC WI_ITEM_REC; typedef struct _CHANNEL_REC CHANNEL_REC; diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 10bd035a..5376334e 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -34,6 +34,8 @@ libcore_a_SOURCES = \ nicklist.c \ nickmatch-cache.c \ pidwait.c \ + proxy.c \ + proxy-protocols.c \ queries.c \ rawlog.c \ recode.c \ @@ -53,6 +55,7 @@ structure_headers = \ channel-rec.h \ channel-setup-rec.h \ chatnet-rec.h \ + proxy-rec.h \ query-rec.h \ server-rec.h \ server-setup-rec.h \ @@ -86,6 +89,8 @@ pkginc_core_HEADERS = \ nicklist.h \ nickmatch-cache.h \ pidwait.h \ + proxy.h \ + proxy-protocols.h \ queries.h \ rawlog.h \ recode.h \ diff --git a/src/core/core.c b/src/core/core.c index bf7cdd6b..d5bbe91e 100644 --- a/src/core/core.c +++ b/src/core/core.c @@ -33,6 +33,8 @@ #include "chat-protocols.h" #include "servers.h" #include "chatnets.h" +#include "proxy.h" +#include "proxy-protocols.h" #include "commands.h" #include "expandos.h" #include "write-buffer.h" @@ -239,6 +241,8 @@ void core_init(void) chat_protocols_init(); chatnets_init(); expandos_init(); + proxy_init(); + proxy_protocols_init(); ignore_init(); servers_init(); write_buffer_init(); @@ -290,6 +294,8 @@ void core_deinit(void) ignore_deinit(); expandos_deinit(); chatnets_deinit(); + proxy_deinit(); + proxy_protocols_deinit(); chat_protocols_deinit(); session_deinit(); diff --git a/src/core/proxy-protocols.c b/src/core/proxy-protocols.c new file mode 100644 index 00000000..d5259133 --- /dev/null +++ b/src/core/proxy-protocols.c @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2015 Alexander Færøy + * + * 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; either version 2 of the License, or (at your option) + * any later version. + * + * 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, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA + */ + +#include "module.h" +#include "proxy-protocols.h" +#include "signals.h" + +GSList *proxy_protocols; + +static void proxy_protocol_destroy(PROXY_PROTOCOL_REC *rec) +{ + g_return_if_fail(rec != NULL); + + proxy_protocols = g_slist_remove(proxy_protocols, rec); + + signal_emit("proxy protocol destroyed", 1, rec); + + g_free(rec->name); + g_free(rec); +} + +/* Register new Proxy Protocol. */ +PROXY_PROTOCOL_REC *proxy_protocol_register(PROXY_PROTOCOL_REC *rec) +{ + PROXY_PROTOCOL_REC *newrec; + int created; + + g_return_val_if_fail(rec != NULL, NULL); + + newrec = proxy_protocol_find(rec->name); + created = newrec == NULL; + + if (created) { + newrec = g_new0(PROXY_PROTOCOL_REC, 1); + proxy_protocols = g_slist_append(proxy_protocols, newrec); + } else { + g_free(newrec->name); + } + + memcpy(newrec, rec, sizeof(PROXY_PROTOCOL_REC)); + + newrec->id = module_get_uniq_id_str("PROXY PROTOCOL", rec->name); + newrec->name = g_strdup(rec->name); + + if (created) + signal_emit("proxy protocol created", 1, newrec); + else + signal_emit("proxy protocol updated", 1, newrec); + + return newrec; +} + +/* Unregister Proxy Protocol. */ +void proxy_protocol_unregister(const char *name) +{ + PROXY_PROTOCOL_REC *rec; + + g_return_if_fail(name != NULL); + + rec = proxy_protocol_find(name); + + if (rec != NULL) + proxy_protocol_destroy(rec); +} + +/* Lookup Proxy Protocols. */ +int proxy_protocol_lookup(const char *name) +{ + PROXY_PROTOCOL_REC *rec; + + g_return_val_if_fail(name != NULL, -1); + + rec = proxy_protocol_find(name); + + return rec == NULL ? -1 : rec->id; +} + +PROXY_PROTOCOL_REC *proxy_protocol_find(const char *name) +{ + GSList *tmp; + + g_return_val_if_fail(name != NULL, NULL); + + for (tmp = proxy_protocols; tmp != NULL; tmp = tmp->next) { + PROXY_PROTOCOL_REC *rec = tmp->data; + + if (g_ascii_strcasecmp(rec->name, name) == 0) + return rec; + } + + return NULL; +} + +PROXY_PROTOCOL_REC *proxy_protocol_find_id(int id) +{ + GSList *tmp; + + g_return_val_if_fail(id > 0, NULL); + + for (tmp = proxy_protocols; tmp != NULL; tmp = tmp->next) { + PROXY_PROTOCOL_REC *rec = tmp->data; + + if (rec->id == id) + return rec; + } + + return NULL; +} + +void proxy_protocols_init(void) +{ + proxy_protocols = NULL; +} + +void proxy_protocols_deinit(void) +{ + while (proxy_protocols != NULL) + proxy_protocol_destroy(proxy_protocols->data); +} diff --git a/src/core/proxy-protocols.h b/src/core/proxy-protocols.h new file mode 100644 index 00000000..22e9793a --- /dev/null +++ b/src/core/proxy-protocols.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015 Alexander Færøy + * + * 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; either version 2 of the License, or (at your option) + * any later version. + * + * 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, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA + */ + +#ifndef __PROXY_PROTOCOLS_H +#define __PROXY_PROTOCOLS_H + +#include "modules.h" + +struct _PROXY_PROTOCOL_REC { + int id; + char *name; +}; + +extern GSList *proxy_protocols; + +/* Register new Proxy Protocol. */ +PROXY_PROTOCOL_REC *proxy_protocol_register(PROXY_PROTOCOL_REC *rec); + +/* Unregister Proxy Protocol. */ +void proxy_protocol_unregister(const char *name); + +/* Lookup Proxy Protocols. */ +int proxy_protocol_lookup(const char *name); +PROXY_PROTOCOL_REC *proxy_protocol_find(const char *name); +PROXY_PROTOCOL_REC *proxy_protocol_find_id(int id); + +void proxy_protocols_init(void); +void proxy_protocols_deinit(void); + +#endif diff --git a/src/core/proxy-rec.h b/src/core/proxy-rec.h new file mode 100644 index 00000000..986de8ff --- /dev/null +++ b/src/core/proxy-rec.h @@ -0,0 +1,7 @@ +int type; /* module_get_uniq_id("PROXY", 0); */ +int proxy_type; /* proxy_protocol_lookup(); */ + +char *name; + +char *address; +int port; diff --git a/src/core/proxy.c b/src/core/proxy.c new file mode 100644 index 00000000..18f51d66 --- /dev/null +++ b/src/core/proxy.c @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2015 Alexander Færøy + * + * 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; either version 2 of the License, or (at your option) + * any later version. + * + * 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, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA + */ + +#include "module.h" +#include "signals.h" +#include "lib-config/iconfig.h" +#include "settings.h" + +#include "proxy.h" + +GSList *proxies; + +static void proxy_config_save(PROXY_REC *proxy) +{ + CONFIG_NODE *node; + + node = iconfig_node_traverse("proxies", TRUE); + node = iconfig_node_section(node, proxy->name, NODE_TYPE_BLOCK); + iconfig_node_clear(node); + + iconfig_node_set_str(node, "address", proxy->address); + iconfig_node_set_int(node, "port", proxy->port); + + signal_emit("proxy saved", 2, proxy, node); +} + +static void proxy_config_remove(PROXY_REC *proxy) +{ + CONFIG_NODE *node; + + node = iconfig_node_traverse("proxies", FALSE); + + if (node != NULL) + iconfig_node_set_str(node, proxy->name, NULL); +} + +static void read_proxy(CONFIG_NODE *node) +{ + PROXY_REC *rec; + + if (node == NULL || node->key == NULL) + return; + + rec = g_new(PROXY_REC, 1); + rec->name = g_strdup(node->key); + rec->address = g_strdup(config_node_get_str(node, "address", NULL)); + rec->port = config_node_get_int(node, "port", 0); + + proxies = g_slist_append(proxies, rec); + signal_emit("proxy read", 2, rec, node); +} + +static void read_proxies(void) +{ + CONFIG_NODE *node; + GSList *tmp; + + while (proxies != NULL) + proxy_destroy(proxies->data); + + node = iconfig_node_traverse("proxies", FALSE); + + if (node != NULL) { + for (tmp = config_node_first(node->value); tmp != NULL; tmp = config_node_next(tmp)) + read_proxy(tmp->data); + } +} + +void proxy_create(PROXY_REC *proxy) +{ + g_return_if_fail(proxy != NULL); + + proxy->type = module_get_uniq_id("PROXY", 0); + + if (g_slist_find(proxies, proxy) == NULL) + proxies = g_slist_append(proxies, proxy); + + proxy_config_save(proxy); + signal_emit("proxy created", 1, proxy); +} + +void proxy_remove(PROXY_REC *proxy) +{ + g_return_if_fail(proxy != NULL); + + signal_emit("proxy removed", 1, proxy); + + proxy_config_remove(proxy); + proxy_destroy(proxy); +} + +void proxy_destroy(PROXY_REC *proxy) +{ + g_return_if_fail(IS_PROXY(proxy)); + + proxies = g_slist_remove(proxies, proxy); + signal_emit("proxy destroyed", 1, proxy); + + g_free(proxy->name); + g_free(proxy->address); + g_free(proxy); +} + +PROXY_REC *proxy_find(const char *name) +{ + GSList *tmp; + + g_return_val_if_fail(name != NULL, NULL); + + for (tmp = proxies; tmp != NULL; tmp = tmp->next) { + PROXY_REC *rec = tmp->data; + + if (g_ascii_strcasecmp(rec->name, name) == 0) + return rec; + } + + return NULL; +} + +void proxy_init(void) +{ + proxies = NULL; + + settings_add_str("proxy", "default_proxy", NULL); + + signal_add("setup reread", (SIGNAL_FUNC)read_proxies); + signal_add("irssi init read settings", (SIGNAL_FUNC)read_proxies); +} + +void proxy_deinit(void) +{ + module_uniq_destroy("PROXY"); + + signal_remove("setup reread", (SIGNAL_FUNC)read_proxies); + signal_remove("irssi init read settings", (SIGNAL_FUNC)read_proxies); +} diff --git a/src/core/proxy.h b/src/core/proxy.h new file mode 100644 index 00000000..46732806 --- /dev/null +++ b/src/core/proxy.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2015 Alexander Færøy + * + * 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; either version 2 of the License, or (at your option) + * any later version. + * + * 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, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA + */ + +#ifndef __PROXY_H +#define __PROXY_H + +#include "modules.h" + +#define PROXY(proxy) \ + MODULE_CHECK_CAST(proxy, PROXY_REC, type, "PROXY") + +#define IS_PROXY(proxy) \ + (PROXY(proxy) ? TRUE : FALSE) + +struct _PROXY_REC { +#include "proxy-rec.h" +}; + +extern GSList *proxies; + +/* Add a proxy to the proxy list. */ +void proxy_create(PROXY_REC *proxy); + +/* Remove a proxy from the proxy list. */ +void proxy_remove(PROXY_REC *proxy); + +/* Destroy the proxy structure without removing it from configs. */ +void proxy_destroy(PROXY_REC *proxy); + +/* Find a proxy by name. */ +PROXY_REC *proxy_find(const char *name); + +void proxy_init(void); +void proxy_deinit(void); + +#endif + diff --git a/src/fe-common/core/Makefile.am b/src/fe-common/core/Makefile.am index 6efff411..08d6edc7 100644 --- a/src/fe-common/core/Makefile.am +++ b/src/fe-common/core/Makefile.am @@ -22,6 +22,7 @@ libfe_common_core_a_SOURCES = \ fe-messages.c \ fe-modules.c \ fe-queries.c \ + fe-proxy.c \ fe-server.c \ fe-settings.c \ fe-tls.c \ diff --git a/src/fe-common/core/fe-common-core.c b/src/fe-common/core/fe-common-core.c index 512fc84c..937f4199 100644 --- a/src/fe-common/core/fe-common-core.c +++ b/src/fe-common/core/fe-common-core.c @@ -82,6 +82,9 @@ void fe_messages_deinit(void); void fe_modules_init(void); void fe_modules_deinit(void); +void fe_proxy_init(void); +void fe_proxy_deinit(void); + void fe_server_init(void); void fe_server_deinit(void); @@ -176,6 +179,7 @@ void fe_common_core_init(void) fe_ignore_init(); fe_log_init(); fe_modules_init(); + fe_proxy_init(); fe_server_init(); fe_settings_init(); fe_tls_init(); @@ -218,6 +222,7 @@ void fe_common_core_deinit(void) fe_ignore_deinit(); fe_log_deinit(); fe_modules_deinit(); + fe_proxy_deinit(); fe_server_deinit(); fe_settings_deinit(); fe_tls_deinit(); diff --git a/src/fe-common/core/fe-proxy.c b/src/fe-common/core/fe-proxy.c new file mode 100644 index 00000000..2ded96bf --- /dev/null +++ b/src/fe-common/core/fe-proxy.c @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2015 Alexander Færøy + * + * 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; either version 2 of the License, or (at your option) + * any later version. + * + * 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, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA + */ + +#include "module.h" +#include "signals.h" +#include "commands.h" +#include "network.h" +#include "levels.h" +#include "settings.h" +#include "proxy.h" + +#include "module-formats.h" +#include "printtext.h" + +/* SYNTAX: PROXY ADD
*/ +static void cmd_proxy_add(const char *data) +{ + char *name, *type, *address, *port; + GHashTable *optlist; + PROXY_REC *rec; + void *free_arg; + + if (!cmd_get_params(data, &free_arg, 4 | PARAM_FLAG_OPTIONS, + "proxy add", &optlist, &name, &type, &address, &port)) + return; + + if (*name == '\0' || *type == '\0' || *address == '\0' || *port == '\0') + cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); + + rec = proxy_find(name); + + if (rec == NULL) { + rec = g_new0(PROXY_REC, 1); + rec->name = g_strdup(name); + } else { + g_free_and_null(rec->address); + } + + rec->address = g_strdup(address); + rec->port = atoi(port); + + proxy_create(rec); + printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_PROXY_ADDED, name); + + cmd_params_free(free_arg); +} + +/* SYNTAX: PROXY REMOVE */ +static void cmd_proxy_remove(const char *data) +{ + PROXY_REC *rec; + + if (*data == '\0') + cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS); + + rec = proxy_find(data); + + if (rec == NULL) + printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_PROXY_NOT_FOUND, data); + else { + printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_PROXY_REMOVED, data); + proxy_remove(rec); + } +} + +/* SYNTAX: PROXY LIST */ +static void cmd_proxy_list(void) +{ + GString *str; + GSList *tmp; + + str = g_string_new(NULL); + printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_PROXY_HEADER); + + for (tmp = proxies; tmp != NULL; tmp = tmp->next) { + PROXY_REC *rec = tmp->data; + + printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_PROXY_LINE, rec->name, rec->address, rec->port, str->str); + } + + g_string_free(str, TRUE); + printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_PROXY_FOOTER); +} + +static void cmd_proxy(const char *data, SERVER_REC *server, WI_ITEM_REC *item) +{ + if (*data == '\0') + cmd_proxy_list(); + else + command_runsub("proxy", data, server, item); +} + +void fe_proxy_init(void) +{ + command_bind("proxy", NULL, (SIGNAL_FUNC)cmd_proxy); + command_bind("proxy list", NULL, (SIGNAL_FUNC)cmd_proxy_list); + command_bind("proxy add", NULL, (SIGNAL_FUNC)cmd_proxy_add); + command_bind("proxy remove", NULL, (SIGNAL_FUNC)cmd_proxy_remove); + + command_set_options("proxy add", ""); +} + +void fe_proxy_deinit(void) +{ + command_unbind("proxy", (SIGNAL_FUNC)cmd_proxy); + command_unbind("proxy list", (SIGNAL_FUNC)cmd_proxy_list); + command_unbind("proxy add", (SIGNAL_FUNC)cmd_proxy_add); + command_unbind("proxy remove", (SIGNAL_FUNC)cmd_proxy_remove); +} diff --git a/src/fe-common/core/module-formats.c b/src/fe-common/core/module-formats.c index da9705be..4678e407 100644 --- a/src/fe-common/core/module-formats.c +++ b/src/fe-common/core/module-formats.c @@ -304,5 +304,15 @@ FORMAT_REC fecommon_core_formats[] = { { "tls_cert_fingerprint", "Certificate Fingerprint: {hilight $0} ({hilight $1})", 2, { 0, 0 } }, { "tls_protocol_version", "Protocol: {hilight $0} ({hilight $1} bit, {hilight $2})", 3, { 0, 1, 0 } }, + /* ---- */ + { NULL, "Proxy", 0 }, + + { "proxy_added", "Proxy $0 added", 1, { 0 } }, + { "proxy_not_found", "Proxy $0 not found", 1, { 0 } }, + { "proxy_removed", "Proxy $0 removed", 1, { 0 } }, + { "proxy_header", "%#Proxy Server Port Settings", 0 }, + { "proxy_line", "%#%|$[!10]0 $[20]1 $[5]2 $3", 4, { 0, 0, 1, 0 } }, + { "proxy_footer", "", 0 }, + { NULL, NULL, 0 } }; diff --git a/src/fe-common/core/module-formats.h b/src/fe-common/core/module-formats.h index a9ed28c5..bec948e9 100644 --- a/src/fe-common/core/module-formats.h +++ b/src/fe-common/core/module-formats.h @@ -266,7 +266,16 @@ enum { TXT_TLS_CERT_ISSUER, TXT_TLS_PUBKEY_FINGERPRINT, TXT_TLS_CERT_FINGERPRINT, - TXT_TLS_PROTOCOL_VERSION + TXT_TLS_PROTOCOL_VERSION, + + TXT_FILL_16, + + TXT_PROXY_ADDED, + TXT_PROXY_NOT_FOUND, + TXT_PROXY_REMOVED, + TXT_PROXY_HEADER, + TXT_PROXY_LINE, + TXT_PROXY_FOOTER }; extern FORMAT_REC fecommon_core_formats[];