4 Copyright (C) 1999-2001 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "chat-protocols.h"
31 #include "servers-setup.h"
32 #include "servers-reconnect.h"
34 #include "module-formats.h"
35 #include "printtext.h"
37 static void print_servers(void)
41 for (tmp = servers; tmp != NULL; tmp = tmp->next) {
42 SERVER_REC *rec = tmp->data;
44 printformat(NULL, NULL, MSGLEVEL_CRAP, TXT_SERVER_LIST,
45 rec->tag, rec->connrec->address, rec->connrec->port,
46 rec->connrec->chatnet == NULL ? "" : rec->connrec->chatnet, rec->connrec->nick);
50 static void print_lookup_servers(void)
53 for (tmp = lookup_servers; tmp != NULL; tmp = tmp->next) {
54 SERVER_REC *rec = tmp->data;
56 printformat(NULL, NULL, MSGLEVEL_CRAP, TXT_SERVER_LOOKUP_LIST,
57 rec->tag, rec->connrec->address, rec->connrec->port,
58 rec->connrec->chatnet == NULL ? "" : rec->connrec->chatnet, rec->connrec->nick);
62 static void print_reconnects(void)
65 char *tag, *next_connect;
68 for (tmp = reconnects; tmp != NULL; tmp = tmp->next) {
69 RECONNECT_REC *rec = tmp->data;
70 SERVER_CONNECT_REC *conn = rec->conn;
72 tag = g_strdup_printf("RECON-%d", rec->tag);
73 left = rec->next_connect-time(NULL);
74 next_connect = g_strdup_printf("%02d:%02d", left/60, left%60);
75 printformat(NULL, NULL, MSGLEVEL_CRAP, TXT_SERVER_RECONNECT_LIST,
76 tag, conn->address, conn->port,
77 conn->chatnet == NULL ? "" : conn->chatnet,
78 conn->nick, next_connect);
84 static SERVER_SETUP_REC *create_server_setup(GHashTable *optlist)
86 CHAT_PROTOCOL_REC *rec;
87 SERVER_SETUP_REC *server;
90 rec = chat_protocol_find_net(optlist);
92 rec = chat_protocol_get_default();
94 chatnet = g_hash_table_lookup(optlist, rec->chatnet);
95 if (chatnet_find(chatnet) == NULL) {
96 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
97 TXT_UNKNOWN_CHATNET, chatnet);
102 server = rec->create_server_setup();
103 server->chat_type = rec->id;
107 static void cmd_server_add(const char *data)
110 SERVER_SETUP_REC *rec;
111 char *addr, *portstr, *password, *value;
115 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS,
116 "server add", &optlist, &addr, &portstr, &password))
119 if (*addr == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
120 port = *portstr == '\0' ? 6667 : atoi(portstr);
122 rec = server_setup_find_port(addr, port);
124 rec = create_server_setup(optlist);
126 cmd_params_free(free_arg);
129 rec->address = g_strdup(addr);
132 value = g_hash_table_lookup(optlist, "port");
133 if (value != NULL && *value != '\0') rec->port = atoi(value);
135 if (*password != '\0') g_free_and_null(rec->password);
136 if (g_hash_table_lookup(optlist, "host")) {
137 g_free_and_null(rec->own_host);
138 rec->own_ip4 = rec->own_ip6 = NULL;
142 if (g_hash_table_lookup(optlist, "6"))
143 rec->family = AF_INET6;
144 else if (g_hash_table_lookup(optlist, "4"))
145 rec->family = AF_INET;
147 if (g_hash_table_lookup(optlist, "auto")) rec->autoconnect = TRUE;
148 if (g_hash_table_lookup(optlist, "noauto")) rec->autoconnect = FALSE;
149 if (g_hash_table_lookup(optlist, "proxy")) rec->no_proxy = FALSE;
150 if (g_hash_table_lookup(optlist, "noproxy")) rec->no_proxy = TRUE;
152 if (*password != '\0' && strcmp(password, "-") != 0) rec->password = g_strdup(password);
153 value = g_hash_table_lookup(optlist, "host");
154 if (value != NULL && *value != '\0') {
155 rec->own_host = g_strdup(value);
156 rec->own_ip4 = rec->own_ip6 = NULL;
159 signal_emit("server add fill", 2, rec, optlist);
161 server_setup_add(rec);
162 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
163 TXT_SETUPSERVER_ADDED, addr, port);
165 cmd_params_free(free_arg);
168 /* SYNTAX: SERVER REMOVE <address> [<port>] */
169 static void cmd_server_remove(const char *data)
171 SERVER_SETUP_REC *rec;
175 if (!cmd_get_params(data, &free_arg, 2, &addr, &port))
177 if (*addr == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
180 rec = server_setup_find(addr, -1, NULL);
182 rec = server_setup_find_port(addr, atoi(port));
185 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_SETUPSERVER_NOT_FOUND, addr, port);
187 server_setup_remove(rec);
188 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_SETUPSERVER_REMOVED, addr, port);
191 cmd_params_free(free_arg);
194 static void cmd_server(const char *data)
199 if (servers == NULL && lookup_servers == NULL &&
200 reconnects == NULL) {
201 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
202 TXT_NO_CONNECTED_SERVERS);
205 print_lookup_servers();
212 static void cmd_server_connect(const char *data)
218 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
219 "connect", &optlist, &addr))
222 if (*addr == '\0' || strcmp(addr, "+") == 0)
223 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
224 if (*addr == '+') window_create(NULL, FALSE);
226 cmd_params_free(free_arg);
229 static void sig_server_looking(SERVER_REC *server)
231 g_return_if_fail(server != NULL);
233 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOOKING_UP, server->connrec->address);
236 static void sig_server_connecting(SERVER_REC *server, IPADDR *ip)
238 char ipaddr[MAX_IP_LEN];
240 g_return_if_fail(server != NULL);
241 g_return_if_fail(ip != NULL);
243 net_ip2host(ip, ipaddr);
244 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CONNECTING,
245 server->connrec->address, ipaddr, server->connrec->port);
248 static void sig_server_connected(SERVER_REC *server)
250 g_return_if_fail(server != NULL);
252 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
253 TXT_CONNECTION_ESTABLISHED, server->connrec->address);
256 static void sig_connect_failed(SERVER_REC *server, gchar *msg)
258 g_return_if_fail(server != NULL);
261 /* no message so this wasn't unexpected fail - send
262 connection_lost message instead */
263 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
264 TXT_CONNECTION_LOST, server->connrec->address);
266 printformat(server, NULL, MSGLEVEL_CLIENTERROR,
267 TXT_CANT_CONNECT, server->connrec->address, server->connrec->port, msg);
271 static void sig_server_disconnected(SERVER_REC *server)
273 g_return_if_fail(server != NULL);
275 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
276 TXT_CONNECTION_LOST, server->connrec->address);
279 static void sig_server_quit(SERVER_REC *server, const char *msg)
281 g_return_if_fail(server != NULL);
283 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
284 TXT_SERVER_QUIT, server->connrec->address, msg);
287 static void sig_server_lag_disconnected(SERVER_REC *server)
289 g_return_if_fail(server != NULL);
291 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
292 TXT_LAG_DISCONNECTED, server->connrec->address,
293 time(NULL)-server->lag_sent.tv_sec);
296 static void sig_server_reconnect_removed(RECONNECT_REC *reconnect)
298 g_return_if_fail(reconnect != NULL);
300 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
301 TXT_RECONNECT_REMOVED, reconnect->conn->address, reconnect->conn->port,
302 reconnect->conn->chatnet == NULL ? "" : reconnect->conn->chatnet);
305 static void sig_server_reconnect_not_found(const char *tag)
307 g_return_if_fail(tag != NULL);
309 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
310 TXT_RECONNECT_NOT_FOUND, tag);
313 static void sig_chat_protocol_unknown(const char *protocol)
315 g_return_if_fail(protocol != NULL);
317 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
318 TXT_UNKNOWN_CHAT_PROTOCOL, protocol);
321 void fe_server_init(void)
323 command_bind("server", NULL, (SIGNAL_FUNC) cmd_server);
324 command_bind("server connect", NULL, (SIGNAL_FUNC) cmd_server_connect);
325 command_bind("server add", NULL, (SIGNAL_FUNC) cmd_server_add);
326 command_bind("server remove", NULL, (SIGNAL_FUNC) cmd_server_remove);
327 command_set_options("server add", "4 6 auto noauto proxy noproxy -host -port");
329 signal_add("server looking", (SIGNAL_FUNC) sig_server_looking);
330 signal_add("server connecting", (SIGNAL_FUNC) sig_server_connecting);
331 signal_add("server connected", (SIGNAL_FUNC) sig_server_connected);
332 signal_add("server connect failed", (SIGNAL_FUNC) sig_connect_failed);
333 signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
334 signal_add("server quit", (SIGNAL_FUNC) sig_server_quit);
336 signal_add("server lag disconnect", (SIGNAL_FUNC) sig_server_lag_disconnected);
337 signal_add("server reconnect remove", (SIGNAL_FUNC) sig_server_reconnect_removed);
338 signal_add("server reconnect not found", (SIGNAL_FUNC) sig_server_reconnect_not_found);
340 signal_add("chat protocol unknown", (SIGNAL_FUNC) sig_chat_protocol_unknown);
343 void fe_server_deinit(void)
345 command_unbind("server", (SIGNAL_FUNC) cmd_server);
346 command_unbind("server connect", (SIGNAL_FUNC) cmd_server_connect);
347 command_unbind("server add", (SIGNAL_FUNC) cmd_server_add);
348 command_unbind("server remove", (SIGNAL_FUNC) cmd_server_remove);
350 signal_remove("server looking", (SIGNAL_FUNC) sig_server_looking);
351 signal_remove("server connecting", (SIGNAL_FUNC) sig_server_connecting);
352 signal_remove("server connected", (SIGNAL_FUNC) sig_server_connected);
353 signal_remove("server connect failed", (SIGNAL_FUNC) sig_connect_failed);
354 signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
355 signal_remove("server quit", (SIGNAL_FUNC) sig_server_quit);
357 signal_remove("server lag disconnect", (SIGNAL_FUNC) sig_server_lag_disconnected);
358 signal_remove("server reconnect remove", (SIGNAL_FUNC) sig_server_reconnect_removed);
359 signal_remove("server reconnect not found", (SIGNAL_FUNC) sig_server_reconnect_not_found);
361 signal_remove("chat protocol unknown", (SIGNAL_FUNC) sig_chat_protocol_unknown);