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' ? DEFAULT_SERVER_ADD_PORT : 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, "ssl"))
150 if (g_hash_table_lookup(optlist, "auto")) rec->autoconnect = TRUE;
151 if (g_hash_table_lookup(optlist, "noauto")) rec->autoconnect = FALSE;
152 if (g_hash_table_lookup(optlist, "proxy")) rec->no_proxy = FALSE;
153 if (g_hash_table_lookup(optlist, "noproxy")) rec->no_proxy = TRUE;
155 if (*password != '\0' && strcmp(password, "-") != 0) rec->password = g_strdup(password);
156 value = g_hash_table_lookup(optlist, "host");
157 if (value != NULL && *value != '\0') {
158 rec->own_host = g_strdup(value);
159 rec->own_ip4 = rec->own_ip6 = NULL;
162 signal_emit("server add fill", 2, rec, optlist);
164 server_setup_add(rec);
165 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
166 TXT_SETUPSERVER_ADDED, addr, port);
168 cmd_params_free(free_arg);
171 /* SYNTAX: SERVER REMOVE <address> [<port>] */
172 static void cmd_server_remove(const char *data)
174 SERVER_SETUP_REC *rec;
178 if (!cmd_get_params(data, &free_arg, 2, &addr, &port))
180 if (*addr == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
183 rec = server_setup_find(addr, -1, NULL);
185 rec = server_setup_find_port(addr, atoi(port));
188 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_SETUPSERVER_NOT_FOUND, addr, port);
190 server_setup_remove(rec);
191 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_SETUPSERVER_REMOVED, addr, port);
194 cmd_params_free(free_arg);
197 static void cmd_server(const char *data)
202 if (servers == NULL && lookup_servers == NULL &&
203 reconnects == NULL) {
204 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
205 TXT_NO_CONNECTED_SERVERS);
208 print_lookup_servers();
215 static void cmd_server_connect(const char *data)
221 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
222 "connect", &optlist, &addr))
225 if (*addr == '\0' || strcmp(addr, "+") == 0)
226 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
227 if (*addr == '+') window_create(NULL, FALSE);
229 cmd_params_free(free_arg);
232 static void server_command(const char *data, SERVER_REC *server,
235 if (server == NULL) {
236 /* this command accepts non-connected server too */
237 server = active_win->connect_server;
240 signal_continue(3, data, server, item);
243 static void sig_server_looking(SERVER_REC *server)
245 g_return_if_fail(server != NULL);
247 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE, TXT_LOOKING_UP, server->connrec->address);
250 static void sig_server_connecting(SERVER_REC *server, IPADDR *ip)
252 char ipaddr[MAX_IP_LEN];
254 g_return_if_fail(server != NULL);
259 net_ip2host(ip, ipaddr);
261 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE, TXT_CONNECTING,
262 server->connrec->address, ipaddr, server->connrec->port);
265 static void sig_server_connected(SERVER_REC *server)
267 g_return_if_fail(server != NULL);
269 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
270 TXT_CONNECTION_ESTABLISHED, server->connrec->address);
273 static void sig_connect_failed(SERVER_REC *server, gchar *msg)
275 g_return_if_fail(server != NULL);
278 /* no message so this wasn't unexpected fail - send
279 connection_lost message instead */
280 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
281 TXT_CONNECTION_LOST, server->connrec->address);
283 printformat(server, NULL, MSGLEVEL_CLIENTERROR,
284 TXT_CANT_CONNECT, server->connrec->address, server->connrec->port, msg);
288 static void sig_server_disconnected(SERVER_REC *server)
290 g_return_if_fail(server != NULL);
292 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
293 TXT_CONNECTION_LOST, server->connrec->address);
296 static void sig_server_quit(SERVER_REC *server, const char *msg)
298 g_return_if_fail(server != NULL);
300 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
301 TXT_SERVER_QUIT, server->connrec->address, msg);
304 static void sig_server_lag_disconnected(SERVER_REC *server)
306 g_return_if_fail(server != NULL);
308 printformat(server, NULL, MSGLEVEL_CLIENTNOTICE,
309 TXT_LAG_DISCONNECTED, server->connrec->address,
310 time(NULL)-server->lag_sent.tv_sec);
313 static void sig_server_reconnect_removed(RECONNECT_REC *reconnect)
315 g_return_if_fail(reconnect != NULL);
317 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
318 TXT_RECONNECT_REMOVED, reconnect->conn->address, reconnect->conn->port,
319 reconnect->conn->chatnet == NULL ? "" : reconnect->conn->chatnet);
322 static void sig_server_reconnect_not_found(const char *tag)
324 g_return_if_fail(tag != NULL);
326 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
327 TXT_RECONNECT_NOT_FOUND, tag);
330 static void sig_chat_protocol_unknown(const char *protocol)
332 g_return_if_fail(protocol != NULL);
334 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
335 TXT_UNKNOWN_CHAT_PROTOCOL, protocol);
338 void fe_server_init(void)
340 command_bind("server", NULL, (SIGNAL_FUNC) cmd_server);
341 command_bind("server connect", NULL, (SIGNAL_FUNC) cmd_server_connect);
342 command_bind("server add", NULL, (SIGNAL_FUNC) cmd_server_add);
343 command_bind("server remove", NULL, (SIGNAL_FUNC) cmd_server_remove);
344 command_bind_first("server", NULL, (SIGNAL_FUNC) server_command);
345 command_bind_first("disconnect", NULL, (SIGNAL_FUNC) server_command);
346 command_set_options("server add", "4 6 ssl auto noauto proxy noproxy -host -port");
348 signal_add("server looking", (SIGNAL_FUNC) sig_server_looking);
349 signal_add("server connecting", (SIGNAL_FUNC) sig_server_connecting);
350 signal_add("server connected", (SIGNAL_FUNC) sig_server_connected);
351 signal_add("server connect failed", (SIGNAL_FUNC) sig_connect_failed);
352 signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
353 signal_add("server quit", (SIGNAL_FUNC) sig_server_quit);
355 signal_add("server lag disconnect", (SIGNAL_FUNC) sig_server_lag_disconnected);
356 signal_add("server reconnect remove", (SIGNAL_FUNC) sig_server_reconnect_removed);
357 signal_add("server reconnect not found", (SIGNAL_FUNC) sig_server_reconnect_not_found);
359 signal_add("chat protocol unknown", (SIGNAL_FUNC) sig_chat_protocol_unknown);
362 void fe_server_deinit(void)
364 command_unbind("server", (SIGNAL_FUNC) cmd_server);
365 command_unbind("server connect", (SIGNAL_FUNC) cmd_server_connect);
366 command_unbind("server add", (SIGNAL_FUNC) cmd_server_add);
367 command_unbind("server remove", (SIGNAL_FUNC) cmd_server_remove);
368 command_unbind("server", (SIGNAL_FUNC) server_command);
369 command_unbind("disconnect", (SIGNAL_FUNC) server_command);
371 signal_remove("server looking", (SIGNAL_FUNC) sig_server_looking);
372 signal_remove("server connecting", (SIGNAL_FUNC) sig_server_connecting);
373 signal_remove("server connected", (SIGNAL_FUNC) sig_server_connected);
374 signal_remove("server connect failed", (SIGNAL_FUNC) sig_connect_failed);
375 signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
376 signal_remove("server quit", (SIGNAL_FUNC) sig_server_quit);
378 signal_remove("server lag disconnect", (SIGNAL_FUNC) sig_server_lag_disconnected);
379 signal_remove("server reconnect remove", (SIGNAL_FUNC) sig_server_reconnect_removed);
380 signal_remove("server reconnect not found", (SIGNAL_FUNC) sig_server_reconnect_not_found);
382 signal_remove("chat protocol unknown", (SIGNAL_FUNC) sig_chat_protocol_unknown);