d661187088046b4efe3566e2d0695dec40d69bb5
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
1 /*
2   silc-server.c : irssi
3
4   Copyright (C) 2000 - 2007 Timo Sirainen
5                             Pekka Riikonen <priikone@silcnet.org>
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21
22 #include "module.h"
23
24 #include "net-nonblock.h"
25 #include "net-sendbuffer.h"
26 #include "signals.h"
27 #include "servers.h"
28 #include "commands.h"
29 #include "levels.h"
30 #include "modules.h"
31 #include "rawlog.h"
32 #include "misc.h"
33 #include "settings.h"
34
35 #include "servers-setup.h"
36
37 #include "client_ops.h"
38 #include "silc-servers.h"
39 #include "silc-channels.h"
40 #include "silc-queries.h"
41 #include "silc-nicklist.h"
42 #include "silc-cmdqueue.h"
43 #include "window-item-def.h"
44
45 #include "fe-common/core/printtext.h"
46 #include "fe-common/core/fe-channels.h"
47 #include "fe-common/core/keyboard.h"
48 #include "fe-common/silc/module-formats.h"
49
50 #include "silc-commands.h"
51
52 void silc_servers_reconnect_init(void);
53 void silc_servers_reconnect_deinit(void);
54
55 int silc_send_channel(SILC_SERVER_REC *server,
56                       char *channel, char *msg,
57                       SilcMessageFlags flags)
58 {
59   SILC_CHANNEL_REC *rec;
60
61   rec = silc_channel_find(server, channel);
62   if (rec == NULL || rec->entry == NULL) {
63     cmd_return_error_value(CMDERR_NOT_JOINED, FALSE);
64   }
65
66   return silc_client_send_channel_message(silc_client, server->conn,
67                                           rec->entry, NULL, flags, sha1hash,
68                                           msg, strlen(msg));
69 }
70
71 typedef struct {
72   char *nick;
73   char *msg;
74   int len;
75   SilcMessageFlags flags;
76   SILC_SERVER_REC *server;
77 } PRIVMSG_REC;
78
79 /* Callback function that sends the private message if the client was
80    resolved from the server. */
81
82 static void silc_send_msg_clients(SilcClient client,
83                                   SilcClientConnection conn,
84                                   SilcStatus status,
85                                   SilcDList clients,
86                                   void *context)
87 {
88   PRIVMSG_REC *rec = context;
89   SILC_SERVER_REC *server = rec->server;
90   SilcClientEntry target;
91   char nickname[128 + 1];
92   SilcDList lclients = NULL;
93
94   if (!clients) {
95     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
96               "%s: There is no such client", rec->nick);
97   } else {
98     if (silc_dlist_count(clients) > 1) {
99       silc_parse_userfqdn(rec->nick, nickname, sizeof(nickname), NULL, 0);
100
101       /* Find the correct one. The rec->nick might be a formatted nick
102          so this will find the correct one. */
103       clients = lclients =
104         silc_client_get_clients_local(silc_client, server->conn,
105                                       nickname, rec->nick);
106       if (!clients) {
107         printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
108                   "%s: There is no such client", rec->nick);
109         goto out;
110       }
111     }
112
113     target = silc_dlist_get(clients);
114
115     /* Still check for exact math for nickname, this compares the
116        real (formatted) nickname and the nick (maybe formatted) that
117        user gave. This is to assure that `nick' does not match
118        `nick@host'. */
119     if (!silc_utf8_strcasecmp(rec->nick, target->nickname)) {
120       printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
121                 "%s: There is no such client", rec->nick);
122       goto out;
123     }
124
125     /* Send the private message */
126     silc_client_send_private_message(client, conn, target,
127                                      rec->flags, sha1hash,
128                                      rec->msg, rec->len);
129   }
130
131  out:
132   silc_client_list_free(silc_client, server->conn, lclients);
133   g_free(rec->nick);
134   g_free(rec->msg);
135   g_free(rec);
136 }
137
138 int silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg,
139                   int msg_len, SilcMessageFlags flags)
140 {
141   PRIVMSG_REC *rec;
142   char nickname[128 + 1];
143   SilcDList clients;
144   SilcClientEntry target;
145   int ret;
146
147   if (!silc_parse_userfqdn(nick, nickname, sizeof(nickname), NULL, 0)) {
148     printformat_module("fe-common/silc", server, NULL,
149                        MSGLEVEL_CRAP, SILCTXT_BAD_NICK, nick);
150     return FALSE;
151   }
152
153   /* Find client entry */
154   clients = silc_client_get_clients_local(silc_client, server->conn,
155                                           nickname, nick);
156   if (!clients) {
157     rec = g_new0(PRIVMSG_REC, 1);
158     rec->nick = g_strdup(nick);
159     rec->msg = g_strdup(msg);
160     rec->server = server;
161     rec->flags = flags;
162     rec->len = msg_len;
163
164     /* Could not find client with that nick, resolve it from server. */
165     silc_client_get_clients_whois(silc_client, server->conn,
166                                   nickname, NULL, NULL,
167                                   silc_send_msg_clients, rec);
168     return TRUE;
169   }
170
171   /* Send the private message directly */
172   target = silc_dlist_get(clients);
173   ret = silc_client_send_private_message(silc_client, server->conn,
174                                          target, flags, sha1hash,
175                                          msg, msg_len);
176
177   silc_client_list_free(silc_client, server->conn, clients);
178
179   return ret;
180 }
181
182 void silc_send_mime(SILC_SERVER_REC *server, int channel, const char *to,
183                     const char *data, int sign)
184 {
185   char *unescaped_data;
186   SilcUInt32 unescaped_data_len;
187   int target_type;
188
189   if (!(IS_SILC_SERVER(server)) || (data == NULL) || (to == NULL))
190     return;
191
192   if (channel) {
193     target_type = SEND_TARGET_CHANNEL;
194   } else {
195     target_type = server_ischannel(SERVER(server), to) ?
196       SEND_TARGET_CHANNEL : SEND_TARGET_NICK;
197   }
198
199   unescaped_data = silc_unescape_data(data, &unescaped_data_len);
200
201   if (target_type == SEND_TARGET_CHANNEL) {
202     SILC_CHANNEL_REC *rec;
203
204     rec = silc_channel_find(server, to);
205     if (rec == NULL || rec->entry == NULL) {
206       cmd_return_error(CMDERR_NOT_JOINED);
207     }
208
209     silc_client_send_channel_message(silc_client, server->conn, rec->entry,
210                                      NULL, SILC_MESSAGE_FLAG_DATA |
211                                      (sign ? SILC_MESSAGE_FLAG_SIGNED : 0),
212                                      sha1hash, unescaped_data,
213                                      unescaped_data_len);
214   } else {
215     silc_send_msg(server, (char *)to, unescaped_data, unescaped_data_len,
216                   SILC_MESSAGE_FLAG_DATA |
217                   (sign ? SILC_MESSAGE_FLAG_SIGNED : 0));
218
219   }
220
221   signal_stop();
222
223   silc_free(unescaped_data);
224 }
225
226 static int isnickflag_func(char flag)
227 {
228   return flag == '@' || flag == '+';
229 }
230
231 static int ischannel_func(SERVER_REC *server, const char *data)
232 {
233   return FALSE;
234 }
235
236 const char *get_nick_flags(void)
237 {
238   return "@\0\0";
239 }
240
241 static void send_message(SILC_SERVER_REC *server, char *target,
242                          char *msg, int target_type)
243 {
244   char *message = NULL, *t = NULL;
245   int len;
246   SilcBool sign;
247
248   g_return_if_fail(server != NULL);
249   g_return_if_fail(target != NULL);
250   g_return_if_fail(msg != NULL);
251
252   if (!silc_term_utf8()) {
253     len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LOCALE);
254     message = silc_calloc(len + 1, sizeof(*message));
255     g_return_if_fail(message != NULL);
256     silc_utf8_encode(msg, strlen(msg), SILC_STRING_LOCALE, message, len);
257   }
258
259   if (target_type == SEND_TARGET_CHANNEL) {
260     sign = settings_get_bool("sign_channel_messages");
261     silc_send_channel(server, target, message ? message : msg,
262                       SILC_MESSAGE_FLAG_UTF8 |
263                       (sign ? SILC_MESSAGE_FLAG_SIGNED : 0));
264   } else {
265     sign = settings_get_bool("sign_private_messages");
266     if (!silc_term_utf8()) {
267       len = silc_utf8_encoded_len(target, strlen(target), SILC_STRING_LOCALE);
268       t = silc_calloc(len + 1, sizeof(*t));
269       g_return_if_fail(t != NULL);
270       silc_utf8_encode(target, strlen(target), SILC_STRING_LOCALE, t, len);
271     }
272
273     silc_send_msg(server, t ? t : target, message ? message : msg,
274                   message ? strlen(message) : strlen(msg),
275                   SILC_MESSAGE_FLAG_UTF8 |
276                   (sign ? SILC_MESSAGE_FLAG_SIGNED : 0));
277   }
278
279   silc_free(message);
280   silc_free(t);
281 }
282
283 /* Connection callback */
284
285 static void silc_connect_cb(SilcClient client,
286                             SilcClientConnection conn,
287                             SilcClientConnectionStatus status,
288                             SilcStatus error,
289                             const char *message,
290                             void *context)
291 {
292   SILC_SERVER_REC *server = context;
293   char *file;
294
295   server->op = NULL;
296
297   switch (status) {
298   case SILC_CLIENT_CONN_SUCCESS:
299     if (server->disconnected) {
300       silc_client_close_connection(client, conn);
301       return;
302     }
303
304     /* We have successfully connected to server */
305
306     /* Enable queueing until we have our requested nick */
307     if (((opt_nickname &&
308           strcmp(opt_nickname, conn->local_entry->nickname)) ||
309          (settings_get_str("nick") &&
310           strcmp(settings_get_str("nick"), conn->local_entry->nickname))) &&
311         !strcmp(conn->local_entry->nickname, conn->local_entry->username))
312       silc_queue_enable(conn);
313
314     /* Put default attributes */
315     silc_query_attributes_default(silc_client, conn);
316
317     server->connected = TRUE;
318     server->conn = conn;
319     server->conn->context = server;
320     signal_emit("event connected", 1, server);
321     break;
322
323   case SILC_CLIENT_CONN_SUCCESS_RESUME:
324     if (server->disconnected) {
325       silc_client_close_connection(client, conn);
326       return;
327     }
328
329     /* We have successfully resumed old detached session */
330     server->connected = TRUE;
331     server->conn = conn;
332     server->conn->context = server;
333     signal_emit("event connected", 1, server);
334
335     /* Put default attributes */
336     silc_query_attributes_default(silc_client, conn);
337
338     /* Remove the detach data now */
339     file = silc_get_session_filename(server);
340     unlink(file);
341     silc_free(file);
342     break;
343
344   case SILC_CLIENT_CONN_DISCONNECTED:
345     /* Server disconnected */
346     if (server->conn && server->conn->local_entry) {
347       nicklist_rename_unique(SERVER(server),
348                              server->conn->local_entry, server->nick,
349                              server->conn->local_entry,
350                              silc_client->username);
351       silc_change_nick(server, silc_client->username);
352     }
353
354     if (message)
355       silc_say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
356                "Server closed connection: %s (%d) %s",
357                silc_get_status_message(error), error,
358                message ? message : "");
359
360     if (server->conn)
361       server->conn->context = NULL;
362     server->conn = NULL;
363     server->connection_lost = TRUE;
364     if (!server->disconnected)
365       server_disconnect(SERVER(server));
366     server_unref(SERVER(server));
367     break;
368
369   default:
370     file = silc_get_session_filename(server);
371     if (silc_file_size(file) > 0)
372       printformat_module("fe-common/silc", server, NULL,
373                          MSGLEVEL_CRAP, SILCTXT_REATTACH_FAILED, file);
374     silc_free(file);
375
376     server->connection_lost = TRUE;
377     if (server->conn)
378       server->conn->context = NULL;
379     if (!server->disconnected)
380       server_disconnect(SERVER(server));
381     server_unref(SERVER(server));
382     break;
383   }
384 }
385
386 /* Called after TCP stream has been created */
387
388 static void sig_connected_stream_created(SilcSocketStreamStatus status,
389                                          SilcStream stream, void *context)
390 {
391   SILC_SERVER_REC *server = context;
392   SilcClientConnectionParams params;
393   char *file;
394
395   if (!stream) {
396     server->connection_lost = TRUE;
397     server_disconnect(SERVER(server));
398     return;
399   }
400
401   if (server->disconnected) {
402     silc_stream_destroy(stream);
403     return;
404   }
405
406   /* Set connection parameters */
407   memset(&params, 0, sizeof(params));
408   params.nickname = (opt_nickname ? (char *)opt_nickname :
409                      (char *)settings_get_str("nick"));
410   params.timeout_secs = settings_get_int("key_exchange_timeout_secs");
411   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
412   params.pfs = settings_get_bool("key_exchange_rekey_pfs");
413
414   /* Try to read detached session data and use it if found. */
415   file = silc_get_session_filename(server);
416   params.detach_data = silc_file_readfile(file, &params.detach_data_len);
417   if (params.detach_data)
418     params.detach_data[params.detach_data_len] = 0;
419   if (params.detach_data)
420     printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
421                         SILCTXT_REATTACH, server->tag);
422
423   /* Start key exchange */
424   server->op = silc_client_key_exchange(silc_client, &params,
425                                         irssi_pubkey, irssi_privkey,
426                                         stream, SILC_CONN_SERVER,
427                                         silc_connect_cb, server);
428   if (!server->op) {
429     server->connection_lost = TRUE;
430     server_disconnect(SERVER(server));
431     silc_stream_destroy(stream);
432     return;
433   }
434
435   server_ref(SERVER(server));
436   server->ftp_sessions = silc_dlist_init();
437   server->isnickflag = isnickflag_func;
438   server->ischannel = ischannel_func;
439   server->get_nick_flags = get_nick_flags;
440   server->send_message = (void *) send_message;
441 }
442
443 static void sig_connected(SILC_SERVER_REC *server)
444 {
445   int fd;
446
447   if (!IS_SILC_SERVER(server))
448     return;
449
450   /* Wrap the socket to TCP stream */
451   fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
452   silc_socket_tcp_stream_create(fd, TRUE, FALSE, silc_client->schedule,
453                                 sig_connected_stream_created, server);
454 }
455
456 static void sig_disconnected(SILC_SERVER_REC *server)
457 {
458   if (!IS_SILC_SERVER(server))
459     return;
460
461   silc_dlist_uninit(server->ftp_sessions);
462
463   if (server->conn) {
464     /* Close connection */
465     silc_client_close_connection(silc_client, server->conn);
466   } else if (server->op) {
467     /* Abort on going connecting (key exchange) */
468     silc_async_abort(server->op, NULL, NULL);
469     server->op = NULL;
470   }
471
472   /* SILC closes the handle */
473   g_io_channel_unref(net_sendbuffer_handle(server->handle));
474   net_sendbuffer_destroy(server->handle, FALSE);
475   server->handle = NULL;
476 }
477
478 SERVER_REC *silc_server_init_connect(SERVER_CONNECT_REC *conn)
479 {
480   SILC_SERVER_REC *server;
481
482   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
483   if (conn->address == NULL || *conn->address == '\0')
484     return NULL;
485   if (conn->nick == NULL || *conn->nick == '\0') {
486     silc_say_error("Cannot connect: nickname is not set");
487     return NULL;
488   }
489
490   server = g_new0(SILC_SERVER_REC, 1);
491   server->chat_type = SILC_PROTOCOL;
492   server->connrec = (SILC_SERVER_CONNECT_REC *)conn;
493   server_connect_ref(conn);
494
495   if (server->connrec->port <= 0)
496     server->connrec->port = 706;
497
498   server_connect_init((SERVER_REC *)server);
499   return (SERVER_REC *)server;
500 }
501
502 void silc_server_connect(SERVER_REC *server)
503 {
504   if (!server_start_connect(server)) {
505     server_connect_unref(server->connrec);
506     g_free(server);
507     return;
508   }
509 }
510
511 /* Return a string of all channels in server in server->channels_join()
512    format */
513
514 char *silc_server_get_channels(SILC_SERVER_REC *server)
515 {
516   GSList *tmp;
517   GString *chans;
518   char *ret;
519
520   g_return_val_if_fail(server != NULL, FALSE);
521
522   chans = g_string_new(NULL);
523   for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
524     CHANNEL_REC *channel = tmp->data;
525
526     g_string_sprintfa(chans, "%s,", channel->name);
527   }
528
529   if (chans->len > 0)
530     g_string_truncate(chans, chans->len-1);
531
532   ret = chans->str;
533   g_string_free(chans, FALSE);
534
535   return ret;
536 }
537
538 /* Syntaxes of all SILC commands for HELP files (the help file generation
539    will snoop these from here). */
540
541 /* SYNTAX: BAN <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
542 /* SYNTAX: CMODE <channel> +|-<modes> [{ <arguments>}] */
543 /* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] */
544 /* SYNTAX: GETKEY <nickname or server name> */
545 /* SYNTAX: INVITE <channel> [<nickname>[@hostname>] */
546 /* SYNTAX: INVITE <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
547 /* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
548 /* SYNTAX: KEY CHANNEL <channel> set|unset|list|change [<arguments>] */
549 /* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
550 /* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] [-pubkey] */
551 /* SYNTAX: OPER <username> [-pubkey] */
552 /* SYNTAX: SILCOPER <username> [-pubkey] */
553 /* SYNTAX: TOPIC <channel> [<topic>] */
554 /* SYNTAX: UMODE +|-<modes> */
555 /* SYNTAX: WHOIS [<nickname>[@<hostname>]] [-details] [-pubkey <pubkeyfile>] [<count>] */
556 /* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
557 /* SYNTAX: CLOSE <server> [<port>] */
558 /* SYNTAX: SHUTDOWN */
559 /* SYNTAX: MOTD [<server>] */
560 /* SYNTAX: LIST [<channel>] */
561 /* SYNTAX: ME <message> */
562 /* SYNTAX: ACTION [-sign] [-channel] <target> <message> */
563 /* SYNTAX: AWAY [<message>] */
564 /* SYNTAX: INFO [<server>] */
565 /* SYNTAX: NICK <nickname> */
566 /* SYNTAX: NOTICE [-sign] [-channel] <target> <message> */
567 /* SYNTAX: PART [<channel>] */
568 /* SYNTAX: PING */
569 /* SYNTAX: SCONNECT <server> [<port>] */
570 /* SYNTAX: USERS <channel> */
571 /* SYNTAX: FILE SEND <filepath> <nickname> [<local IP> [<local port>]] [-no-listener]*/
572 /* SYNTAX: FILE ACCEPT [<nickname>] */
573 /* SYNTAX: FILE CLOSE [<nickname>] */
574 /* SYNTAX: FILE */
575 /* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder] [-auth [<pubkeyfile> <privkeyfile> [<privkey passphrase>]]]*/
576 /* SYNTAX: DETACH */
577 /* SYNTAX: WATCH [<-add | -del> <nickname>] [-pubkey +|-<pubkeyfile>] */
578 /* SYNTAX: STATS */
579 /* SYNTAX: ATTR [<-del> <option> [{ <value>}]] */
580 /* SYNTAX: SMSG [<-channel>] <target> <message> */
581 /* SYNTAX: LISTKEYS [-servers] [-clients] [<public key file>] */
582
583 void silc_command_exec(SILC_SERVER_REC *server,
584                        const char *command, const char *args)
585 {
586   char *data;
587   g_return_if_fail(server != NULL);
588
589   /* Call the command */
590   data = g_strconcat(command, " ", args, NULL);
591   silc_queue_command_call(silc_client, server->conn, data);
592   g_free(data);
593 }
594
595 /* Generic command function to call any SILC command directly. */
596
597 static void command_self(const char *data, SILC_SERVER_REC *server,
598                          WI_ITEM_REC *item)
599 {
600   CMD_SILC_SERVER(server);
601
602   if (!IS_SILC_SERVER(server) || !server->connected) {
603     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
604     return;
605   }
606
607   if (IS_SILC_CHANNEL(item)) {
608     SILC_CHANNEL_REC *chanrec;
609     chanrec = silc_channel_find(server, item->visible_name);
610     if (chanrec)
611       server->conn->current_channel = chanrec->entry;
612   }
613
614   silc_command_exec(server, current_command, data);
615   signal_stop();
616 }
617
618 /* SCONNECT command.  Calls actually SILC's CONNECT command since Irssi
619    has CONNECT command for other purposes. */
620
621 static void command_sconnect(const char *data, SILC_SERVER_REC *server)
622 {
623   CMD_SILC_SERVER(server);
624   if (!IS_SILC_SERVER(server) || !server->connected) {
625     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
626     return;
627   }
628
629   silc_command_exec(server, "CONNECT", data);
630   signal_stop();
631 }
632
633 /* SMSG command, to send digitally signed messages */
634
635 static void command_smsg(const char *data, SILC_SERVER_REC *server,
636                          WI_ITEM_REC *item)
637 {
638   GHashTable *optlist;
639   char *target, *origtarget, *msg;
640   void *free_arg;
641   int free_ret, target_type;
642
643   g_return_if_fail(data != NULL);
644   if (server == NULL || !server->connected)
645     cmd_param_error(CMDERR_NOT_CONNECTED);
646
647   if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
648                       PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
649                       "msg", &optlist, &target, &msg))
650     return;
651   if (*target == '\0' || *msg == '\0')
652     cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
653
654   origtarget = target;
655   free_ret = FALSE;
656
657   if (strcmp(target, "*") == 0) {
658     if (item == NULL)
659       cmd_param_error(CMDERR_NOT_JOINED);
660
661     target_type = IS_CHANNEL(item) ?
662       SEND_TARGET_CHANNEL : SEND_TARGET_NICK;
663     target = (char *) window_item_get_target(item);
664   } else if (g_hash_table_lookup(optlist, "channel") != NULL) {
665     target_type = SEND_TARGET_CHANNEL;
666   } else {
667     target_type = server_ischannel(SERVER(server), target) ?
668       SEND_TARGET_CHANNEL : SEND_TARGET_NICK;
669   }
670
671   if (target != NULL) {
672     char *message = NULL, *t = NULL;
673     int len, result;
674
675     if (!silc_term_utf8()) {
676       len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LOCALE);
677       message = silc_calloc(len + 1, sizeof(*message));
678       g_return_if_fail(message != NULL);
679       silc_utf8_encode(msg, strlen(msg), SILC_STRING_LOCALE, message, len);
680     }
681
682     if (target_type == SEND_TARGET_CHANNEL)
683       result = silc_send_channel(server, target, message ? message : msg,
684                                  SILC_MESSAGE_FLAG_UTF8 |
685                                  SILC_MESSAGE_FLAG_SIGNED);
686     else {
687       if (!silc_term_utf8()) {
688         len = silc_utf8_encoded_len(target, strlen(target),
689                                     SILC_STRING_LOCALE);
690         t = silc_calloc(len + 1, sizeof(*t));
691         g_return_if_fail(t != NULL);
692         silc_utf8_encode(target, strlen(target), SILC_STRING_LOCALE, t, len);
693       }
694       result = silc_send_msg(server, t ? t : target, message ? message : msg,
695                              message ? strlen(message) : strlen(msg),
696                              SILC_MESSAGE_FLAG_UTF8 |
697                              SILC_MESSAGE_FLAG_SIGNED);
698     }
699     silc_free(message);
700     silc_free(t);
701     if (!result)
702       goto out;
703   }
704
705   signal_emit(target != NULL && target_type == SEND_TARGET_CHANNEL ?
706               "message signed_own_public" : "message signed_own_private", 4,
707               server, msg, target, origtarget);
708 out:
709   if (free_ret && target != NULL) g_free(target);
710   cmd_params_free(free_arg);
711 }
712
713 #if 0
714 /* FILE command */
715
716 SILC_TASK_CALLBACK(silc_client_file_close_later)
717 {
718   FtpSession ftp = (FtpSession)context;
719
720   SILC_LOG_DEBUG(("Start"));
721
722   silc_client_file_close(silc_client, ftp->conn, ftp->session_id);
723   silc_free(ftp->filepath);
724   silc_free(ftp);
725 }
726
727 static void silc_client_file_monitor(SilcClient client,
728                                      SilcClientConnection conn,
729                                      SilcClientMonitorStatus status,
730                                      SilcClientFileError error,
731                                      SilcUInt64 offset,
732                                      SilcUInt64 filesize,
733                                      SilcClientEntry client_entry,
734                                      SilcUInt32 session_id,
735                                      const char *filepath,
736                                      void *context)
737 {
738   SILC_SERVER_REC *server = (SILC_SERVER_REC *)context;
739   FtpSession ftp;
740   char fsize[32];
741
742   if (status == SILC_CLIENT_FILE_MONITOR_CLOSED)
743     return;
744
745   snprintf(fsize, sizeof(fsize) - 1, "%llu", ((filesize + 1023) / 1024));
746
747   silc_dlist_start(server->ftp_sessions);
748   while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
749     if (ftp->session_id == session_id) {
750       if (!ftp->filepath && filepath)
751         ftp->filepath = strdup(filepath);
752       break;
753     }
754   }
755
756   if (ftp == SILC_LIST_END)
757     return;
758
759   if (status == SILC_CLIENT_FILE_MONITOR_ERROR) {
760     if (error == SILC_CLIENT_FILE_NO_SUCH_FILE)
761       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
762                          SILCTXT_FILE_ERROR_NO_SUCH_FILE,
763                          client_entry->nickname,
764                          filepath ? filepath : "[N/A]");
765     else if (error == SILC_CLIENT_FILE_PERMISSION_DENIED)
766       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
767                          SILCTXT_FILE_ERROR_PERMISSION_DENIED,
768                          client_entry->nickname);
769     else
770       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
771                          SILCTXT_FILE_ERROR, client_entry->nickname);
772     silc_schedule_task_add(silc_client->schedule, 0,
773                            silc_client_file_close_later, ftp,
774                            1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
775     silc_dlist_del(server->ftp_sessions, ftp);
776     if (ftp == server->current_session) {
777       server->current_session = NULL;
778       silc_dlist_start(server->ftp_sessions);
779       server->current_session = silc_dlist_get(server->ftp_sessions);
780     }
781   }
782
783   if (status == SILC_CLIENT_FILE_MONITOR_KEY_AGREEMENT) {
784     printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
785                        SILCTXT_FILE_KEY_EXCHANGE, client_entry->nickname);
786   }
787
788   /* Save some transmission data */
789   if (offset && filesize) {
790     unsigned long delta = time(NULL) - ftp->starttime;
791
792     ftp->percent = ((double)offset / (double)filesize) * (double)100.0;
793     if (delta)
794       ftp->kps = (double)((offset / (double)delta) + 1023) / (double)1024;
795     else
796       ftp->kps = (double)(offset + 1023) / (double)1024;
797     ftp->offset = offset;
798     ftp->filesize = filesize;
799   }
800
801   if (status == SILC_CLIENT_FILE_MONITOR_SEND) {
802     if (offset == 0) {
803       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
804                          SILCTXT_FILE_TRANSMIT, filepath, fsize,
805                          client_entry->nickname);
806       ftp->starttime = time(NULL);
807     }
808     if (offset == filesize) {
809       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
810                          SILCTXT_FILE_TRANSMITTED, filepath, fsize,
811                          client_entry->nickname, ftp->kps);
812       silc_schedule_task_add(silc_client->schedule, 0,
813                              silc_client_file_close_later, ftp,
814                              1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
815       silc_dlist_del(server->ftp_sessions, ftp);
816       if (ftp == server->current_session) {
817         server->current_session = NULL;
818         silc_dlist_start(server->ftp_sessions);
819         server->current_session = silc_dlist_get(server->ftp_sessions);
820       }
821
822     }
823   }
824
825   if (status == SILC_CLIENT_FILE_MONITOR_RECEIVE) {
826     if (offset == 0) {
827       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
828                          SILCTXT_FILE_RECEIVE, filepath, fsize,
829                          client_entry->nickname);
830       ftp->starttime = time(NULL);
831     }
832
833     if (offset == filesize) {
834       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
835                          SILCTXT_FILE_RECEIVED, filepath, fsize,
836                          client_entry->nickname, ftp->kps);
837       silc_schedule_task_add(silc_client->schedule, 0,
838                              silc_client_file_close_later, ftp,
839                              1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
840       silc_dlist_del(server->ftp_sessions, ftp);
841       if (ftp == server->current_session) {
842         server->current_session = NULL;
843         silc_dlist_start(server->ftp_sessions);
844         server->current_session = silc_dlist_get(server->ftp_sessions);
845       }
846
847     }
848   }
849 }
850
851 typedef struct {
852   SILC_SERVER_REC *server;
853   char *data;
854   char *nick;
855   WI_ITEM_REC *item;
856 } *FileGetClients;
857
858 static void silc_client_command_file_get_clients(SilcClient client,
859                                                  SilcClientConnection conn,
860                                                  SilcClientEntry *clients,
861                                                  SilcUInt32 clients_count,
862                                                  void *context)
863 {
864   FileGetClients internal = (FileGetClients)context;
865
866   if (!clients) {
867     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s",
868               internal->nick);
869     silc_free(internal->data);
870     silc_free(internal->nick);
871     silc_free(internal);
872     return;
873   }
874
875   signal_emit("command file", 3, internal->data, internal->server,
876               internal->item);
877
878   silc_free(internal->data);
879   silc_free(internal->nick);
880   silc_free(internal);
881 }
882
883 static void command_file(const char *data, SILC_SERVER_REC *server,
884                          WI_ITEM_REC *item)
885 {
886   SilcClientConnection conn;
887   SilcClientEntry *entrys, client_entry;
888   SilcClientFileError ret;
889   SilcUInt32 entry_count;
890   char *nickname = NULL, *tmp;
891   unsigned char **argv;
892   SilcUInt32 argc;
893   SilcUInt32 *argv_lens, *argv_types;
894   int type = 0;
895   FtpSession ftp;
896   char *local_ip = NULL;
897   SilcUInt32 local_port = 0;
898   SilcUInt32 session_id;
899   bool do_not_bind = FALSE;
900
901   CMD_SILC_SERVER(server);
902   if (!server || !IS_SILC_SERVER(server) || !server->connected)
903     cmd_return_error(CMDERR_NOT_CONNECTED);
904
905   conn = server->conn;
906
907   /* Now parse all arguments */
908   tmp = g_strconcat("FILE", " ", data, NULL);
909   silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 7);
910   g_free(tmp);
911
912   if (argc == 1)
913     type = 4;
914
915   if (argc >= 2) {
916     if (!strcasecmp(argv[1], "send"))
917       type = 1;
918     if (!strcasecmp(argv[1], "accept"))
919       type = 2;
920     if (!strcasecmp(argv[1], "close"))
921       type = 3;
922   }
923
924   if (type == 0)
925     cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
926
927   switch (type) {
928   case 1:
929     if (argc < 4)
930       cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
931
932     /* Parse the typed nickname. */
933     if (!silc_parse_userfqdn(argv[3], &nickname, NULL)) {
934       printformat_module("fe-common/silc", server, NULL,
935                          MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[3]);
936       goto out;
937     }
938
939     /* Find client entry */
940     entrys = silc_client_get_clients_local(silc_client, conn, nickname,
941                                            argv[3], &entry_count);
942     if (!entrys) {
943       FileGetClients inter = silc_calloc(1, sizeof(*inter));
944       inter->server = server;
945       inter->data = strdup(data);
946       inter->nick = strdup(nickname);
947       inter->item = item;
948       silc_client_get_clients(silc_client, conn, nickname, argv[3],
949                               silc_client_command_file_get_clients, inter);
950       goto out;
951     }
952     client_entry = entrys[0];
953     silc_free(entrys);
954
955     if (argc >= 5) {
956       if (!strcasecmp(argv[4], "-no-listener"))
957         do_not_bind = TRUE;
958       else
959         local_ip = argv[4];
960     }
961     if (argc >= 6) {
962       if (!strcasecmp(argv[5], "-no-listener"))
963         do_not_bind = TRUE;
964       else
965         local_port = atoi(argv[5]);
966     }
967     if (argc >= 7) {
968       if (!strcasecmp(argv[6], "-no-listener"))
969         do_not_bind = TRUE;
970     }
971
972     ret =
973       silc_client_file_send(silc_client, conn, silc_client_file_monitor,
974                             server, local_ip, local_port, do_not_bind,
975                             client_entry, argv[2], &session_id);
976     if (ret == SILC_CLIENT_FILE_OK) {
977       ftp = silc_calloc(1, sizeof(*ftp));
978       ftp->session_id = session_id;
979
980       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
981                          SILCTXT_FILE_SEND, client_entry->nickname,
982                          argv[2]);
983
984       ftp->client_entry = client_entry;
985       ftp->filepath = strdup(argv[2]);
986       ftp->conn = conn;
987       ftp->send = TRUE;
988       silc_dlist_add(server->ftp_sessions, ftp);
989       server->current_session = ftp;
990     } else {
991       if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
992         printformat_module("fe-common/silc", server, NULL,
993                            MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
994                            client_entry->nickname);
995       if (ret == SILC_CLIENT_FILE_NO_SUCH_FILE)
996         printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
997                            SILCTXT_FILE_ERROR_NO_SUCH_FILE,
998                            client_entry->nickname, argv[2]);
999     }
1000
1001     break;
1002
1003   case 2:
1004     /* Parse the typed nickname. */
1005     if (argc >= 3) {
1006       if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
1007         printformat_module("fe-common/silc", server, NULL,
1008                            MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
1009         goto out;
1010       }
1011
1012       /* Find client entry */
1013       entrys = silc_client_get_clients_local(silc_client, conn, nickname,
1014                                              argv[2], &entry_count);
1015       if (!entrys) {
1016         FileGetClients inter = silc_calloc(1, sizeof(*inter));
1017         inter->server = server;
1018         inter->data = strdup(data);
1019         inter->nick = strdup(nickname);
1020         inter->item = item;
1021         silc_client_get_clients(silc_client, conn, nickname, argv[2],
1022                                 silc_client_command_file_get_clients, inter);
1023         goto out;
1024       }
1025       client_entry = entrys[0];
1026       silc_free(entrys);
1027     } else {
1028       if (!server->current_session) {
1029         printformat_module("fe-common/silc", server, NULL,
1030                            MSGLEVEL_CRAP, SILCTXT_FILE_NA);
1031         goto out;
1032       }
1033
1034       ret = silc_client_file_receive(silc_client, conn,
1035                                      silc_client_file_monitor, server, NULL,
1036                                      server->current_session->session_id,
1037                                      NULL, NULL);
1038       if (ret != SILC_CLIENT_FILE_OK) {
1039         if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
1040           printformat_module("fe-common/silc", server, NULL,
1041                              MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
1042                              server->current_session->client_entry->nickname);
1043         else {
1044           printformat_module("fe-common/silc", server, NULL,
1045                              MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
1046                              server->current_session->client_entry->nickname);
1047
1048           silc_client_file_close(silc_client, conn,
1049                                  server->current_session->session_id);
1050           silc_dlist_del(server->ftp_sessions, server->current_session);
1051           silc_free(server->current_session->filepath);
1052           silc_free(server->current_session);
1053           server->current_session = NULL;
1054
1055           silc_dlist_start(server->ftp_sessions);
1056           server->current_session = silc_dlist_get(server->ftp_sessions);
1057         }
1058       }
1059
1060       goto out;
1061     }
1062
1063     silc_dlist_start(server->ftp_sessions);
1064     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1065       if (ftp->client_entry == client_entry && !ftp->filepath) {
1066         ret = silc_client_file_receive(silc_client, conn,
1067                                        silc_client_file_monitor, server,
1068                                        NULL, ftp->session_id, NULL, NULL);
1069         if (ret != SILC_CLIENT_FILE_OK) {
1070           if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
1071             printformat_module("fe-common/silc", server, NULL,
1072                                MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
1073                                client_entry->nickname);
1074           else {
1075             printformat_module("fe-common/silc", server, NULL,
1076                                MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
1077                                client_entry->nickname);
1078             silc_client_file_close(silc_client, conn, ftp->session_id);
1079             silc_dlist_del(server->ftp_sessions, ftp);
1080             if (ftp == server->current_session) {
1081               server->current_session = NULL;
1082               silc_dlist_start(server->ftp_sessions);
1083               server->current_session = silc_dlist_get(server->ftp_sessions);
1084             }
1085             silc_free(ftp->filepath);
1086             silc_free(ftp);
1087           }
1088         }
1089         break;
1090       }
1091     }
1092
1093     if (ftp == SILC_LIST_END) {
1094       printformat_module("fe-common/silc", server, NULL,
1095                          MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
1096                          client_entry->nickname);
1097       goto out;
1098     }
1099     break;
1100
1101   case 3:
1102     /* Parse the typed nickname. */
1103     if (argc >= 3) {
1104       if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
1105         printformat_module("fe-common/silc", server, NULL,
1106                            MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
1107         goto out;
1108       }
1109
1110       /* Find client entry */
1111       entrys = silc_client_get_clients_local(silc_client, conn, nickname,
1112                                              argv[2], &entry_count);
1113       if (!entrys) {
1114         FileGetClients inter = silc_calloc(1, sizeof(*inter));
1115         inter->server = server;
1116         inter->data = strdup(data);
1117         inter->nick = strdup(nickname);
1118         inter->item = item;
1119         silc_client_get_clients(silc_client, conn, nickname, argv[2],
1120                                 silc_client_command_file_get_clients, inter);
1121         goto out;
1122       }
1123       client_entry = entrys[0];
1124       silc_free(entrys);
1125     } else {
1126       if (!server->current_session) {
1127         printformat_module("fe-common/silc", server, NULL,
1128                            MSGLEVEL_CRAP, SILCTXT_FILE_NA);
1129         goto out;
1130       }
1131
1132       silc_client_file_close(silc_client, conn,
1133                              server->current_session->session_id);
1134       printformat_module("fe-common/silc", server, NULL,
1135                          MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
1136                          server->current_session->client_entry->nickname,
1137                          server->current_session->filepath ?
1138                          server->current_session->filepath : "[N/A]");
1139       silc_dlist_del(server->ftp_sessions, server->current_session);
1140       silc_free(server->current_session->filepath);
1141       silc_free(server->current_session);
1142       server->current_session = NULL;
1143
1144       silc_dlist_start(server->ftp_sessions);
1145       server->current_session = silc_dlist_get(server->ftp_sessions);
1146       goto out;
1147     }
1148
1149     silc_dlist_start(server->ftp_sessions);
1150     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1151       if (ftp->client_entry == client_entry) {
1152         silc_client_file_close(silc_client, conn, ftp->session_id);
1153         printformat_module("fe-common/silc", server, NULL,
1154                            MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
1155                            client_entry->nickname,
1156                            ftp->filepath ? ftp->filepath : "[N/A]");
1157         silc_dlist_del(server->ftp_sessions, ftp);
1158         if (ftp == server->current_session) {
1159           server->current_session = NULL;
1160           silc_dlist_start(server->ftp_sessions);
1161           server->current_session = silc_dlist_get(server->ftp_sessions);
1162         }
1163         silc_free(ftp->filepath);
1164         silc_free(ftp);
1165         break;
1166       }
1167     }
1168
1169     if (ftp == SILC_LIST_END) {
1170       printformat_module("fe-common/silc", server, NULL,
1171                          MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
1172                          client_entry->nickname);
1173       goto out;
1174     }
1175     break;
1176
1177   case 4:
1178
1179     if (!silc_dlist_count(server->ftp_sessions)) {
1180       printformat_module("fe-common/silc", server, NULL,
1181                          MSGLEVEL_CRAP, SILCTXT_FILE_NA);
1182       goto out;
1183     }
1184
1185     printformat_module("fe-common/silc", server, NULL,
1186                        MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_HEADER);
1187
1188     silc_dlist_start(server->ftp_sessions);
1189     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1190       printformat_module("fe-common/silc", server, NULL,
1191                          MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_LINE,
1192                          ftp->client_entry->nickname,
1193                          ftp->session_id,
1194                          ftp->send ? "send" : "receive",
1195                          (SilcUInt32)(ftp->offset + 1023) / 1024,
1196                          (SilcUInt32)(ftp->filesize + 1023) / 1024,
1197                          ftp->percent, ftp->kps,
1198                          ftp->filepath ? ftp->filepath : "[N/A]");
1199     }
1200
1201     break;
1202
1203   default:
1204     break;
1205   }
1206
1207  out:
1208   silc_free(nickname);
1209 }
1210 #endif /* 0 */
1211
1212 void silc_server_init(void)
1213 {
1214   silc_servers_reconnect_init();
1215
1216   signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
1217   signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
1218   signal_add("mime-send", (SIGNAL_FUNC)silc_send_mime);
1219   command_bind_silc("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
1220   command_bind_silc("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
1221   command_bind_silc("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
1222   command_bind_silc("topic", MODULE_NAME, (SIGNAL_FUNC) command_self);
1223   command_bind_silc("cmode", MODULE_NAME, (SIGNAL_FUNC) command_self);
1224   command_bind_silc("cumode", MODULE_NAME, (SIGNAL_FUNC) command_self);
1225   command_bind_silc("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
1226   command_bind_silc("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
1227   command_bind_silc("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
1228   command_bind_silc("oper", MODULE_NAME, (SIGNAL_FUNC) command_self);
1229   command_bind_silc("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
1230   command_bind_silc("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
1231   command_bind_silc("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
1232   command_bind_silc("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
1233   command_bind_silc("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
1234   command_bind_silc("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
1235   command_bind_silc("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
1236   command_bind_silc("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
1237   command_bind_silc("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
1238   command_bind_silc("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
1239   command_bind_silc("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
1240   command_bind_silc("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
1241 //  command_bind_silc("file", MODULE_NAME, (SIGNAL_FUNC) command_file);
1242   command_bind_silc("detach", MODULE_NAME, (SIGNAL_FUNC) command_self);
1243   command_bind_silc("watch", MODULE_NAME, (SIGNAL_FUNC) command_self);
1244   command_bind_silc("stats", MODULE_NAME, (SIGNAL_FUNC) command_self);
1245   command_bind_silc("attr", MODULE_NAME, (SIGNAL_FUNC) command_attr);
1246   command_bind_silc("smsg", MODULE_NAME, (SIGNAL_FUNC) command_smsg);
1247
1248   command_set_options("connect", "+silcnet");
1249 }
1250
1251 void silc_server_deinit(void)
1252 {
1253   silc_servers_reconnect_deinit();
1254
1255   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
1256   signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
1257   signal_remove("mime-send", (SIGNAL_FUNC)silc_send_mime);
1258   command_unbind("whois", (SIGNAL_FUNC) command_self);
1259   command_unbind("whowas", (SIGNAL_FUNC) command_self);
1260   command_unbind("nick", (SIGNAL_FUNC) command_self);
1261   command_unbind("topic", (SIGNAL_FUNC) command_self);
1262   command_unbind("cmode", (SIGNAL_FUNC) command_self);
1263   command_unbind("cumode", (SIGNAL_FUNC) command_self);
1264   command_unbind("users", (SIGNAL_FUNC) command_self);
1265   command_unbind("list", (SIGNAL_FUNC) command_self);
1266   command_unbind("oper", (SIGNAL_FUNC) command_self);
1267   command_unbind("silcoper", (SIGNAL_FUNC) command_self);
1268   command_unbind("umode", (SIGNAL_FUNC) command_self);
1269   command_unbind("invite", (SIGNAL_FUNC) command_self);
1270   command_unbind("kill", (SIGNAL_FUNC) command_self);
1271   command_unbind("kick", (SIGNAL_FUNC) command_self);
1272   command_unbind("info", (SIGNAL_FUNC) command_self);
1273   command_unbind("ping", (SIGNAL_FUNC) command_self);
1274   command_unbind("motd", (SIGNAL_FUNC) command_self);
1275   command_unbind("ban", (SIGNAL_FUNC) command_self);
1276   command_unbind("close", (SIGNAL_FUNC) command_self);
1277   command_unbind("shutdown", (SIGNAL_FUNC) command_self);
1278   command_unbind("getkey", (SIGNAL_FUNC) command_self);
1279   command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
1280 //  command_unbind("file", (SIGNAL_FUNC) command_file);
1281   command_unbind("detach", (SIGNAL_FUNC) command_self);
1282   command_unbind("watch", (SIGNAL_FUNC) command_self);
1283   command_unbind("stats", (SIGNAL_FUNC) command_self);
1284   command_unbind("attr", (SIGNAL_FUNC) command_attr);
1285   command_unbind("smsg", (SIGNAL_FUNC) command_smsg);
1286 }
1287
1288 #if 0
1289 void silc_server_free_ftp(SILC_SERVER_REC *server,
1290                           SilcClientEntry client_entry)
1291 {
1292   FtpSession ftp;
1293
1294   silc_dlist_start(server->ftp_sessions);
1295   while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1296     if (ftp->client_entry == client_entry) {
1297       silc_dlist_del(server->ftp_sessions, ftp);
1298       silc_free(ftp->filepath);
1299       silc_free(ftp);
1300     }
1301   }
1302 }
1303 #endif /* 0 */
1304
1305 bool silc_term_utf8(void)
1306 {
1307   const char *str;
1308   str = settings_get_str("term_charset");
1309   if (str)
1310     if (g_strcasecmp(str, "utf-8") == 0)
1311       return TRUE;
1312   return FALSE;
1313 }