Handle disconnection better. And more porting to new API.
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
1 /*
2   silc-server.c : irssi
3
4   Copyright (C) 2000 - 2006 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     /* If we resumed old session check whether we need to update
339        our nickname */
340     if (strcmp(server->nick, conn->local_entry->nickname)) {
341       char *old;
342       old = g_strdup(server->nick);
343       server_change_nick(SERVER(server), conn->local_entry->nickname);
344       nicklist_rename_unique(SERVER(server),
345                              conn->local_entry, server->nick,
346                              conn->local_entry, conn->local_entry->nickname);
347       signal_emit("message own_nick", 4, server, server->nick, old, "");
348       g_free(old);
349     }
350
351     /* Remove the detach data now */
352     file = silc_get_session_filename(server);
353     unlink(file);
354     silc_free(file);
355     break;
356
357   case SILC_CLIENT_CONN_DISCONNECTED:
358     /* Server disconnected */
359     if (server->conn && server->conn->local_entry) {
360       nicklist_rename_unique(SERVER(server),
361                              server->conn->local_entry, server->nick,
362                              server->conn->local_entry,
363                              silc_client->username);
364       silc_change_nick(server, silc_client->username);
365     }
366
367     if (message)
368       silc_say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
369                "Server closed connection: %s (%d) %s",
370                silc_get_status_message(error), error,
371                message ? message : "");
372
373     if (server->conn)
374       server->conn->context = NULL;
375     server->conn = NULL;
376     server->connection_lost = TRUE;
377     if (!server->disconnected)
378       server_disconnect(SERVER(server));
379     server_unref(SERVER(server));
380     break;
381
382   default:
383     file = silc_get_session_filename(server);
384     if (silc_file_size(file) > 0)
385       printformat_module("fe-common/silc", server, NULL,
386                          MSGLEVEL_CRAP, SILCTXT_REATTACH_FAILED, file);
387
388     silc_free(file);
389
390     server->connection_lost = TRUE;
391     if (server->conn)
392       server->conn->context = NULL;
393     if (!server->disconnected)
394       server_disconnect(SERVER(server));
395     server_unref(SERVER(server));
396     break;
397   }
398 }
399
400 static void sig_connected_stream_created(SilcSocketStreamStatus status,
401                                          SilcStream stream, void *context)
402 {
403   SILC_SERVER_REC *server = context;
404   SilcClientConnectionParams params;
405   char *file;
406
407   if (!stream) {
408     server->connection_lost = TRUE;
409     server_disconnect(SERVER(server));
410     return;
411   }
412
413   if (server->disconnected) {
414     silc_stream_destroy(stream);
415     return;
416   }
417
418   /* Set connection parameters */
419   memset(&params, 0, sizeof(params));
420   params.nickname = (opt_nickname ? (char *)opt_nickname :
421                      (char *)settings_get_str("nick"));
422   params.timeout_secs = settings_get_int("key_exchange_timeout_secs");
423   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
424   params.pfs = settings_get_bool("key_exchange_rekey_pfs");
425
426   /* Try to read detached session data and use it if found. */
427   file = silc_get_session_filename(server);
428   params.detach_data = silc_file_readfile(file, &params.detach_data_len);
429   if (params.detach_data)
430     params.detach_data[params.detach_data_len] = 0;
431   if (params.detach_data)
432     printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
433                         SILCTXT_REATTACH, server->tag);
434
435   /* Start key exchange */
436   server->op = silc_client_key_exchange(silc_client, &params,
437                                         irssi_pubkey, irssi_privkey,
438                                         stream, SILC_CONN_SERVER,
439                                         silc_connect_cb, server);
440   if (!server->op) {
441     server->connection_lost = TRUE;
442     server_disconnect(SERVER(server));
443     silc_stream_destroy(stream);
444     return;
445   }
446
447   server_ref(SERVER(server));
448   server->ftp_sessions = silc_dlist_init();
449   server->isnickflag = isnickflag_func;
450   server->ischannel = ischannel_func;
451   server->get_nick_flags = get_nick_flags;
452   server->send_message = (void *) send_message;
453 }
454
455 static void sig_connected(SILC_SERVER_REC *server)
456 {
457   int fd;
458
459   if (!IS_SILC_SERVER(server))
460     return;
461
462   /* Wrap the socket to TCP stream */
463   fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
464   silc_socket_tcp_stream_create(fd, TRUE, FALSE, silc_client->schedule,
465                                 sig_connected_stream_created, server);
466 }
467
468 static void sig_disconnected(SILC_SERVER_REC *server)
469 {
470   if (!IS_SILC_SERVER(server))
471     return;
472
473   silc_dlist_uninit(server->ftp_sessions);
474
475   if (server->conn) {
476     silc_client_close_connection(silc_client, server->conn);
477
478     /* SILC closes the handle */
479     g_io_channel_unref(net_sendbuffer_handle(server->handle));
480     net_sendbuffer_destroy(server->handle, FALSE);
481     server->handle = NULL;
482   } else if (server->op) {
483     silc_async_abort(server->op, NULL, NULL);
484     server->op = NULL;
485   }
486 }
487
488 SERVER_REC *silc_server_init_connect(SERVER_CONNECT_REC *conn)
489 {
490   SILC_SERVER_REC *server;
491
492   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
493   if (conn->address == NULL || *conn->address == '\0')
494     return NULL;
495   if (conn->nick == NULL || *conn->nick == '\0') {
496     silc_say_error("Cannot connect: nickname is not set");
497     return NULL;
498   }
499
500   server = g_new0(SILC_SERVER_REC, 1);
501   server->chat_type = SILC_PROTOCOL;
502   server->connrec = (SILC_SERVER_CONNECT_REC *)conn;
503   server_connect_ref(conn);
504
505   if (server->connrec->port <= 0)
506     server->connrec->port = 706;
507
508   server_connect_init((SERVER_REC *)server);
509   return (SERVER_REC *)server;
510 }
511
512 void silc_server_connect(SERVER_REC *server)
513 {
514   if (!server_start_connect(server)) {
515     server_connect_unref(server->connrec);
516     g_free(server);
517     return;
518   }
519 }
520
521 /* Return a string of all channels in server in server->channels_join()
522    format */
523
524 char *silc_server_get_channels(SILC_SERVER_REC *server)
525 {
526   GSList *tmp;
527   GString *chans;
528   char *ret;
529
530   g_return_val_if_fail(server != NULL, FALSE);
531
532   chans = g_string_new(NULL);
533   for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
534     CHANNEL_REC *channel = tmp->data;
535
536     g_string_sprintfa(chans, "%s,", channel->name);
537   }
538
539   if (chans->len > 0)
540     g_string_truncate(chans, chans->len-1);
541
542   ret = chans->str;
543   g_string_free(chans, FALSE);
544
545   return ret;
546 }
547
548 /* Syntaxes of all SILC commands for HELP files (the help file generation
549    will snoop these from here). */
550
551 /* SYNTAX: BAN <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
552 /* SYNTAX: CMODE <channel> +|-<modes> [{ <arguments>}] */
553 /* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] */
554 /* SYNTAX: GETKEY <nickname or server name> */
555 /* SYNTAX: INVITE <channel> [<nickname>[@hostname>] */
556 /* SYNTAX: INVITE <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
557 /* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
558 /* SYNTAX: KEY CHANNEL <channel> set|unset|list|change [<arguments>] */
559 /* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
560 /* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] [-pubkey] */
561 /* SYNTAX: OPER <username> [-pubkey] */
562 /* SYNTAX: SILCOPER <username> [-pubkey] */
563 /* SYNTAX: TOPIC <channel> [<topic>] */
564 /* SYNTAX: UMODE +|-<modes> */
565 /* SYNTAX: WHOIS [<nickname>[@<hostname>]] [-details] [-pubkey <pubkeyfile>] [<count>] */
566 /* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
567 /* SYNTAX: CLOSE <server> [<port>] */
568 /* SYNTAX: SHUTDOWN */
569 /* SYNTAX: MOTD [<server>] */
570 /* SYNTAX: LIST [<channel>] */
571 /* SYNTAX: ME <message> */
572 /* SYNTAX: ACTION [-sign] [-channel] <target> <message> */
573 /* SYNTAX: AWAY [<message>] */
574 /* SYNTAX: INFO [<server>] */
575 /* SYNTAX: NICK <nickname> */
576 /* SYNTAX: NOTICE [-sign] [-channel] <target> <message> */
577 /* SYNTAX: PART [<channel>] */
578 /* SYNTAX: PING */
579 /* SYNTAX: SCONNECT <server> [<port>] */
580 /* SYNTAX: USERS <channel> */
581 /* SYNTAX: FILE SEND <filepath> <nickname> [<local IP> [<local port>]] [-no-listener]*/
582 /* SYNTAX: FILE ACCEPT [<nickname>] */
583 /* SYNTAX: FILE CLOSE [<nickname>] */
584 /* SYNTAX: FILE */
585 /* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder] [-auth [<pubkeyfile> <privkeyfile> [<privkey passphrase>]]]*/
586 /* SYNTAX: DETACH */
587 /* SYNTAX: WATCH [<-add | -del> <nickname>] [-pubkey +|-<pubkeyfile>] */
588 /* SYNTAX: STATS */
589 /* SYNTAX: ATTR [<-del> <option> [{ <value>}]] */
590 /* SYNTAX: SMSG [<-channel>] <target> <message> */
591 /* SYNTAX: LISTKEYS [-servers] [-clients] [<public key file>] */
592
593 void silc_command_exec(SILC_SERVER_REC *server,
594                        const char *command, const char *args)
595 {
596   char *data;
597   g_return_if_fail(server != NULL);
598
599   /* Call the command */
600   data = g_strconcat(command, " ", args, NULL);
601   silc_queue_command_call(silc_client, server->conn, data);
602   g_free(data);
603 }
604
605 /* Generic command function to call any SILC command directly. */
606
607 static void command_self(const char *data, SILC_SERVER_REC *server,
608                          WI_ITEM_REC *item)
609 {
610   CMD_SILC_SERVER(server);
611
612   if (!IS_SILC_SERVER(server) || !server->connected) {
613     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
614     return;
615   }
616
617   if (IS_SILC_CHANNEL(item)) {
618     SILC_CHANNEL_REC *chanrec;
619     chanrec = silc_channel_find(server, item->visible_name);
620     if (chanrec)
621       server->conn->current_channel = chanrec->entry;
622   }
623
624   silc_command_exec(server, current_command, data);
625   signal_stop();
626 }
627
628 /* SCONNECT command.  Calls actually SILC's CONNECT command since Irssi
629    has CONNECT command for other purposes. */
630
631 static void command_sconnect(const char *data, SILC_SERVER_REC *server)
632 {
633   CMD_SILC_SERVER(server);
634   if (!IS_SILC_SERVER(server) || !server->connected) {
635     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
636     return;
637   }
638
639   silc_command_exec(server, "CONNECT", data);
640   signal_stop();
641 }
642
643 /* SMSG command, to send digitally signed messages */
644
645 static void command_smsg(const char *data, SILC_SERVER_REC *server,
646                          WI_ITEM_REC *item)
647 {
648   GHashTable *optlist;
649   char *target, *origtarget, *msg;
650   void *free_arg;
651   int free_ret, target_type;
652
653   g_return_if_fail(data != NULL);
654   if (server == NULL || !server->connected)
655     cmd_param_error(CMDERR_NOT_CONNECTED);
656
657   if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
658                       PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
659                       "msg", &optlist, &target, &msg))
660     return;
661   if (*target == '\0' || *msg == '\0')
662     cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
663
664   origtarget = target;
665   free_ret = FALSE;
666
667   if (strcmp(target, "*") == 0) {
668     if (item == NULL)
669       cmd_param_error(CMDERR_NOT_JOINED);
670
671     target_type = IS_CHANNEL(item) ?
672       SEND_TARGET_CHANNEL : SEND_TARGET_NICK;
673     target = (char *) window_item_get_target(item);
674   } else if (g_hash_table_lookup(optlist, "channel") != NULL) {
675     target_type = SEND_TARGET_CHANNEL;
676   } else {
677     target_type = server_ischannel(SERVER(server), target) ?
678       SEND_TARGET_CHANNEL : SEND_TARGET_NICK;
679   }
680
681   if (target != NULL) {
682     char *message = NULL, *t = NULL;
683     int len, result;
684
685     if (!silc_term_utf8()) {
686       len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LOCALE);
687       message = silc_calloc(len + 1, sizeof(*message));
688       g_return_if_fail(message != NULL);
689       silc_utf8_encode(msg, strlen(msg), SILC_STRING_LOCALE, message, len);
690     }
691
692     if (target_type == SEND_TARGET_CHANNEL)
693       result = silc_send_channel(server, target, message ? message : msg,
694                                  SILC_MESSAGE_FLAG_UTF8 |
695                                  SILC_MESSAGE_FLAG_SIGNED);
696     else {
697       if (!silc_term_utf8()) {
698         len = silc_utf8_encoded_len(target, strlen(target),
699                                     SILC_STRING_LOCALE);
700         t = silc_calloc(len + 1, sizeof(*t));
701         g_return_if_fail(t != NULL);
702         silc_utf8_encode(target, strlen(target), SILC_STRING_LOCALE, t, len);
703       }
704       result = silc_send_msg(server, t ? t : target, message ? message : msg,
705                              message ? strlen(message) : strlen(msg),
706                              SILC_MESSAGE_FLAG_UTF8 |
707                              SILC_MESSAGE_FLAG_SIGNED);
708     }
709     silc_free(message);
710     silc_free(t);
711     if (!result)
712       goto out;
713   }
714
715   signal_emit(target != NULL && target_type == SEND_TARGET_CHANNEL ?
716               "message signed_own_public" : "message signed_own_private", 4,
717               server, msg, target, origtarget);
718 out:
719   if (free_ret && target != NULL) g_free(target);
720   cmd_params_free(free_arg);
721 }
722
723 #if 0
724 /* FILE command */
725
726 SILC_TASK_CALLBACK(silc_client_file_close_later)
727 {
728   FtpSession ftp = (FtpSession)context;
729
730   SILC_LOG_DEBUG(("Start"));
731
732   silc_client_file_close(silc_client, ftp->conn, ftp->session_id);
733   silc_free(ftp->filepath);
734   silc_free(ftp);
735 }
736
737 static void silc_client_file_monitor(SilcClient client,
738                                      SilcClientConnection conn,
739                                      SilcClientMonitorStatus status,
740                                      SilcClientFileError error,
741                                      SilcUInt64 offset,
742                                      SilcUInt64 filesize,
743                                      SilcClientEntry client_entry,
744                                      SilcUInt32 session_id,
745                                      const char *filepath,
746                                      void *context)
747 {
748   SILC_SERVER_REC *server = (SILC_SERVER_REC *)context;
749   FtpSession ftp;
750   char fsize[32];
751
752   if (status == SILC_CLIENT_FILE_MONITOR_CLOSED)
753     return;
754
755   snprintf(fsize, sizeof(fsize) - 1, "%llu", ((filesize + 1023) / 1024));
756
757   silc_dlist_start(server->ftp_sessions);
758   while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
759     if (ftp->session_id == session_id) {
760       if (!ftp->filepath && filepath)
761         ftp->filepath = strdup(filepath);
762       break;
763     }
764   }
765
766   if (ftp == SILC_LIST_END)
767     return;
768
769   if (status == SILC_CLIENT_FILE_MONITOR_ERROR) {
770     if (error == SILC_CLIENT_FILE_NO_SUCH_FILE)
771       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
772                          SILCTXT_FILE_ERROR_NO_SUCH_FILE,
773                          client_entry->nickname,
774                          filepath ? filepath : "[N/A]");
775     else if (error == SILC_CLIENT_FILE_PERMISSION_DENIED)
776       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
777                          SILCTXT_FILE_ERROR_PERMISSION_DENIED,
778                          client_entry->nickname);
779     else
780       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
781                          SILCTXT_FILE_ERROR, client_entry->nickname);
782     silc_schedule_task_add(silc_client->schedule, 0,
783                            silc_client_file_close_later, ftp,
784                            1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
785     silc_dlist_del(server->ftp_sessions, ftp);
786     if (ftp == server->current_session) {
787       server->current_session = NULL;
788       silc_dlist_start(server->ftp_sessions);
789       server->current_session = silc_dlist_get(server->ftp_sessions);
790     }
791   }
792
793   if (status == SILC_CLIENT_FILE_MONITOR_KEY_AGREEMENT) {
794     printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
795                        SILCTXT_FILE_KEY_EXCHANGE, client_entry->nickname);
796   }
797
798   /* Save some transmission data */
799   if (offset && filesize) {
800     unsigned long delta = time(NULL) - ftp->starttime;
801
802     ftp->percent = ((double)offset / (double)filesize) * (double)100.0;
803     if (delta)
804       ftp->kps = (double)((offset / (double)delta) + 1023) / (double)1024;
805     else
806       ftp->kps = (double)(offset + 1023) / (double)1024;
807     ftp->offset = offset;
808     ftp->filesize = filesize;
809   }
810
811   if (status == SILC_CLIENT_FILE_MONITOR_SEND) {
812     if (offset == 0) {
813       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
814                          SILCTXT_FILE_TRANSMIT, filepath, fsize,
815                          client_entry->nickname);
816       ftp->starttime = time(NULL);
817     }
818     if (offset == filesize) {
819       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
820                          SILCTXT_FILE_TRANSMITTED, filepath, fsize,
821                          client_entry->nickname, ftp->kps);
822       silc_schedule_task_add(silc_client->schedule, 0,
823                              silc_client_file_close_later, ftp,
824                              1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
825       silc_dlist_del(server->ftp_sessions, ftp);
826       if (ftp == server->current_session) {
827         server->current_session = NULL;
828         silc_dlist_start(server->ftp_sessions);
829         server->current_session = silc_dlist_get(server->ftp_sessions);
830       }
831
832     }
833   }
834
835   if (status == SILC_CLIENT_FILE_MONITOR_RECEIVE) {
836     if (offset == 0) {
837       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
838                          SILCTXT_FILE_RECEIVE, filepath, fsize,
839                          client_entry->nickname);
840       ftp->starttime = time(NULL);
841     }
842
843     if (offset == filesize) {
844       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
845                          SILCTXT_FILE_RECEIVED, filepath, fsize,
846                          client_entry->nickname, ftp->kps);
847       silc_schedule_task_add(silc_client->schedule, 0,
848                              silc_client_file_close_later, ftp,
849                              1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
850       silc_dlist_del(server->ftp_sessions, ftp);
851       if (ftp == server->current_session) {
852         server->current_session = NULL;
853         silc_dlist_start(server->ftp_sessions);
854         server->current_session = silc_dlist_get(server->ftp_sessions);
855       }
856
857     }
858   }
859 }
860
861 typedef struct {
862   SILC_SERVER_REC *server;
863   char *data;
864   char *nick;
865   WI_ITEM_REC *item;
866 } *FileGetClients;
867
868 static void silc_client_command_file_get_clients(SilcClient client,
869                                                  SilcClientConnection conn,
870                                                  SilcClientEntry *clients,
871                                                  SilcUInt32 clients_count,
872                                                  void *context)
873 {
874   FileGetClients internal = (FileGetClients)context;
875
876   if (!clients) {
877     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s",
878               internal->nick);
879     silc_free(internal->data);
880     silc_free(internal->nick);
881     silc_free(internal);
882     return;
883   }
884
885   signal_emit("command file", 3, internal->data, internal->server,
886               internal->item);
887
888   silc_free(internal->data);
889   silc_free(internal->nick);
890   silc_free(internal);
891 }
892
893 static void command_file(const char *data, SILC_SERVER_REC *server,
894                          WI_ITEM_REC *item)
895 {
896   SilcClientConnection conn;
897   SilcClientEntry *entrys, client_entry;
898   SilcClientFileError ret;
899   SilcUInt32 entry_count;
900   char *nickname = NULL, *tmp;
901   unsigned char **argv;
902   SilcUInt32 argc;
903   SilcUInt32 *argv_lens, *argv_types;
904   int type = 0;
905   FtpSession ftp;
906   char *local_ip = NULL;
907   SilcUInt32 local_port = 0;
908   SilcUInt32 session_id;
909   bool do_not_bind = FALSE;
910
911   CMD_SILC_SERVER(server);
912   if (!server || !IS_SILC_SERVER(server) || !server->connected)
913     cmd_return_error(CMDERR_NOT_CONNECTED);
914
915   conn = server->conn;
916
917   /* Now parse all arguments */
918   tmp = g_strconcat("FILE", " ", data, NULL);
919   silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 7);
920   g_free(tmp);
921
922   if (argc == 1)
923     type = 4;
924
925   if (argc >= 2) {
926     if (!strcasecmp(argv[1], "send"))
927       type = 1;
928     if (!strcasecmp(argv[1], "accept"))
929       type = 2;
930     if (!strcasecmp(argv[1], "close"))
931       type = 3;
932   }
933
934   if (type == 0)
935     cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
936
937   switch (type) {
938   case 1:
939     if (argc < 4)
940       cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
941
942     /* Parse the typed nickname. */
943     if (!silc_parse_userfqdn(argv[3], &nickname, NULL)) {
944       printformat_module("fe-common/silc", server, NULL,
945                          MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[3]);
946       goto out;
947     }
948
949     /* Find client entry */
950     entrys = silc_client_get_clients_local(silc_client, conn, nickname,
951                                            argv[3], &entry_count);
952     if (!entrys) {
953       FileGetClients inter = silc_calloc(1, sizeof(*inter));
954       inter->server = server;
955       inter->data = strdup(data);
956       inter->nick = strdup(nickname);
957       inter->item = item;
958       silc_client_get_clients(silc_client, conn, nickname, argv[3],
959                               silc_client_command_file_get_clients, inter);
960       goto out;
961     }
962     client_entry = entrys[0];
963     silc_free(entrys);
964
965     if (argc >= 5) {
966       if (!strcasecmp(argv[4], "-no-listener"))
967         do_not_bind = TRUE;
968       else
969         local_ip = argv[4];
970     }
971     if (argc >= 6) {
972       if (!strcasecmp(argv[5], "-no-listener"))
973         do_not_bind = TRUE;
974       else
975         local_port = atoi(argv[5]);
976     }
977     if (argc >= 7) {
978       if (!strcasecmp(argv[6], "-no-listener"))
979         do_not_bind = TRUE;
980     }
981
982     ret =
983       silc_client_file_send(silc_client, conn, silc_client_file_monitor,
984                             server, local_ip, local_port, do_not_bind,
985                             client_entry, argv[2], &session_id);
986     if (ret == SILC_CLIENT_FILE_OK) {
987       ftp = silc_calloc(1, sizeof(*ftp));
988       ftp->session_id = session_id;
989
990       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
991                          SILCTXT_FILE_SEND, client_entry->nickname,
992                          argv[2]);
993
994       ftp->client_entry = client_entry;
995       ftp->filepath = strdup(argv[2]);
996       ftp->conn = conn;
997       ftp->send = TRUE;
998       silc_dlist_add(server->ftp_sessions, ftp);
999       server->current_session = ftp;
1000     } else {
1001       if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
1002         printformat_module("fe-common/silc", server, NULL,
1003                            MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
1004                            client_entry->nickname);
1005       if (ret == SILC_CLIENT_FILE_NO_SUCH_FILE)
1006         printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
1007                            SILCTXT_FILE_ERROR_NO_SUCH_FILE,
1008                            client_entry->nickname, argv[2]);
1009     }
1010
1011     break;
1012
1013   case 2:
1014     /* Parse the typed nickname. */
1015     if (argc >= 3) {
1016       if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
1017         printformat_module("fe-common/silc", server, NULL,
1018                            MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
1019         goto out;
1020       }
1021
1022       /* Find client entry */
1023       entrys = silc_client_get_clients_local(silc_client, conn, nickname,
1024                                              argv[2], &entry_count);
1025       if (!entrys) {
1026         FileGetClients inter = silc_calloc(1, sizeof(*inter));
1027         inter->server = server;
1028         inter->data = strdup(data);
1029         inter->nick = strdup(nickname);
1030         inter->item = item;
1031         silc_client_get_clients(silc_client, conn, nickname, argv[2],
1032                                 silc_client_command_file_get_clients, inter);
1033         goto out;
1034       }
1035       client_entry = entrys[0];
1036       silc_free(entrys);
1037     } else {
1038       if (!server->current_session) {
1039         printformat_module("fe-common/silc", server, NULL,
1040                            MSGLEVEL_CRAP, SILCTXT_FILE_NA);
1041         goto out;
1042       }
1043
1044       ret = silc_client_file_receive(silc_client, conn,
1045                                      silc_client_file_monitor, server, NULL,
1046                                      server->current_session->session_id,
1047                                      NULL, NULL);
1048       if (ret != SILC_CLIENT_FILE_OK) {
1049         if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
1050           printformat_module("fe-common/silc", server, NULL,
1051                              MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
1052                              server->current_session->client_entry->nickname);
1053         else {
1054           printformat_module("fe-common/silc", server, NULL,
1055                              MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
1056                              server->current_session->client_entry->nickname);
1057
1058           silc_client_file_close(silc_client, conn,
1059                                  server->current_session->session_id);
1060           silc_dlist_del(server->ftp_sessions, server->current_session);
1061           silc_free(server->current_session->filepath);
1062           silc_free(server->current_session);
1063           server->current_session = NULL;
1064
1065           silc_dlist_start(server->ftp_sessions);
1066           server->current_session = silc_dlist_get(server->ftp_sessions);
1067         }
1068       }
1069
1070       goto out;
1071     }
1072
1073     silc_dlist_start(server->ftp_sessions);
1074     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1075       if (ftp->client_entry == client_entry && !ftp->filepath) {
1076         ret = silc_client_file_receive(silc_client, conn,
1077                                        silc_client_file_monitor, server,
1078                                        NULL, ftp->session_id, NULL, NULL);
1079         if (ret != SILC_CLIENT_FILE_OK) {
1080           if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
1081             printformat_module("fe-common/silc", server, NULL,
1082                                MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
1083                                client_entry->nickname);
1084           else {
1085             printformat_module("fe-common/silc", server, NULL,
1086                                MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
1087                                client_entry->nickname);
1088             silc_client_file_close(silc_client, conn, ftp->session_id);
1089             silc_dlist_del(server->ftp_sessions, ftp);
1090             if (ftp == server->current_session) {
1091               server->current_session = NULL;
1092               silc_dlist_start(server->ftp_sessions);
1093               server->current_session = silc_dlist_get(server->ftp_sessions);
1094             }
1095             silc_free(ftp->filepath);
1096             silc_free(ftp);
1097           }
1098         }
1099         break;
1100       }
1101     }
1102
1103     if (ftp == SILC_LIST_END) {
1104       printformat_module("fe-common/silc", server, NULL,
1105                          MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
1106                          client_entry->nickname);
1107       goto out;
1108     }
1109     break;
1110
1111   case 3:
1112     /* Parse the typed nickname. */
1113     if (argc >= 3) {
1114       if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
1115         printformat_module("fe-common/silc", server, NULL,
1116                            MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
1117         goto out;
1118       }
1119
1120       /* Find client entry */
1121       entrys = silc_client_get_clients_local(silc_client, conn, nickname,
1122                                              argv[2], &entry_count);
1123       if (!entrys) {
1124         FileGetClients inter = silc_calloc(1, sizeof(*inter));
1125         inter->server = server;
1126         inter->data = strdup(data);
1127         inter->nick = strdup(nickname);
1128         inter->item = item;
1129         silc_client_get_clients(silc_client, conn, nickname, argv[2],
1130                                 silc_client_command_file_get_clients, inter);
1131         goto out;
1132       }
1133       client_entry = entrys[0];
1134       silc_free(entrys);
1135     } else {
1136       if (!server->current_session) {
1137         printformat_module("fe-common/silc", server, NULL,
1138                            MSGLEVEL_CRAP, SILCTXT_FILE_NA);
1139         goto out;
1140       }
1141
1142       silc_client_file_close(silc_client, conn,
1143                              server->current_session->session_id);
1144       printformat_module("fe-common/silc", server, NULL,
1145                          MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
1146                          server->current_session->client_entry->nickname,
1147                          server->current_session->filepath ?
1148                          server->current_session->filepath : "[N/A]");
1149       silc_dlist_del(server->ftp_sessions, server->current_session);
1150       silc_free(server->current_session->filepath);
1151       silc_free(server->current_session);
1152       server->current_session = NULL;
1153
1154       silc_dlist_start(server->ftp_sessions);
1155       server->current_session = silc_dlist_get(server->ftp_sessions);
1156       goto out;
1157     }
1158
1159     silc_dlist_start(server->ftp_sessions);
1160     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1161       if (ftp->client_entry == client_entry) {
1162         silc_client_file_close(silc_client, conn, ftp->session_id);
1163         printformat_module("fe-common/silc", server, NULL,
1164                            MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
1165                            client_entry->nickname,
1166                            ftp->filepath ? ftp->filepath : "[N/A]");
1167         silc_dlist_del(server->ftp_sessions, ftp);
1168         if (ftp == server->current_session) {
1169           server->current_session = NULL;
1170           silc_dlist_start(server->ftp_sessions);
1171           server->current_session = silc_dlist_get(server->ftp_sessions);
1172         }
1173         silc_free(ftp->filepath);
1174         silc_free(ftp);
1175         break;
1176       }
1177     }
1178
1179     if (ftp == SILC_LIST_END) {
1180       printformat_module("fe-common/silc", server, NULL,
1181                          MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
1182                          client_entry->nickname);
1183       goto out;
1184     }
1185     break;
1186
1187   case 4:
1188
1189     if (!silc_dlist_count(server->ftp_sessions)) {
1190       printformat_module("fe-common/silc", server, NULL,
1191                          MSGLEVEL_CRAP, SILCTXT_FILE_NA);
1192       goto out;
1193     }
1194
1195     printformat_module("fe-common/silc", server, NULL,
1196                        MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_HEADER);
1197
1198     silc_dlist_start(server->ftp_sessions);
1199     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1200       printformat_module("fe-common/silc", server, NULL,
1201                          MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_LINE,
1202                          ftp->client_entry->nickname,
1203                          ftp->session_id,
1204                          ftp->send ? "send" : "receive",
1205                          (SilcUInt32)(ftp->offset + 1023) / 1024,
1206                          (SilcUInt32)(ftp->filesize + 1023) / 1024,
1207                          ftp->percent, ftp->kps,
1208                          ftp->filepath ? ftp->filepath : "[N/A]");
1209     }
1210
1211     break;
1212
1213   default:
1214     break;
1215   }
1216
1217  out:
1218   silc_free(nickname);
1219 }
1220 #endif /* 0 */
1221
1222 void silc_server_init(void)
1223 {
1224   silc_servers_reconnect_init();
1225
1226   signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
1227   signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
1228   signal_add("mime-send", (SIGNAL_FUNC)silc_send_mime);
1229   command_bind_silc("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
1230   command_bind_silc("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
1231   command_bind_silc("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
1232   command_bind_silc("topic", MODULE_NAME, (SIGNAL_FUNC) command_self);
1233   command_bind_silc("cmode", MODULE_NAME, (SIGNAL_FUNC) command_self);
1234   command_bind_silc("cumode", MODULE_NAME, (SIGNAL_FUNC) command_self);
1235   command_bind_silc("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
1236   command_bind_silc("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
1237   command_bind_silc("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
1238   command_bind_silc("oper", MODULE_NAME, (SIGNAL_FUNC) command_self);
1239   command_bind_silc("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
1240   command_bind_silc("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
1241   command_bind_silc("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
1242   command_bind_silc("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
1243   command_bind_silc("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
1244   command_bind_silc("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
1245   command_bind_silc("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
1246   command_bind_silc("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
1247   command_bind_silc("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
1248   command_bind_silc("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
1249   command_bind_silc("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
1250   command_bind_silc("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
1251 //  command_bind_silc("file", MODULE_NAME, (SIGNAL_FUNC) command_file);
1252   command_bind_silc("detach", MODULE_NAME, (SIGNAL_FUNC) command_self);
1253   command_bind_silc("watch", MODULE_NAME, (SIGNAL_FUNC) command_self);
1254   command_bind_silc("stats", MODULE_NAME, (SIGNAL_FUNC) command_self);
1255   command_bind_silc("attr", MODULE_NAME, (SIGNAL_FUNC) command_attr);
1256   command_bind_silc("smsg", MODULE_NAME, (SIGNAL_FUNC) command_smsg);
1257
1258   command_set_options("connect", "+silcnet");
1259 }
1260
1261 void silc_server_deinit(void)
1262 {
1263   silc_servers_reconnect_deinit();
1264
1265   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
1266   signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
1267   signal_remove("mime-send", (SIGNAL_FUNC)silc_send_mime);
1268   command_unbind("whois", (SIGNAL_FUNC) command_self);
1269   command_unbind("whowas", (SIGNAL_FUNC) command_self);
1270   command_unbind("nick", (SIGNAL_FUNC) command_self);
1271   command_unbind("topic", (SIGNAL_FUNC) command_self);
1272   command_unbind("cmode", (SIGNAL_FUNC) command_self);
1273   command_unbind("cumode", (SIGNAL_FUNC) command_self);
1274   command_unbind("users", (SIGNAL_FUNC) command_self);
1275   command_unbind("list", (SIGNAL_FUNC) command_self);
1276   command_unbind("oper", (SIGNAL_FUNC) command_self);
1277   command_unbind("silcoper", (SIGNAL_FUNC) command_self);
1278   command_unbind("umode", (SIGNAL_FUNC) command_self);
1279   command_unbind("invite", (SIGNAL_FUNC) command_self);
1280   command_unbind("kill", (SIGNAL_FUNC) command_self);
1281   command_unbind("kick", (SIGNAL_FUNC) command_self);
1282   command_unbind("info", (SIGNAL_FUNC) command_self);
1283   command_unbind("ping", (SIGNAL_FUNC) command_self);
1284   command_unbind("motd", (SIGNAL_FUNC) command_self);
1285   command_unbind("ban", (SIGNAL_FUNC) command_self);
1286   command_unbind("close", (SIGNAL_FUNC) command_self);
1287   command_unbind("shutdown", (SIGNAL_FUNC) command_self);
1288   command_unbind("getkey", (SIGNAL_FUNC) command_self);
1289   command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
1290 //  command_unbind("file", (SIGNAL_FUNC) command_file);
1291   command_unbind("detach", (SIGNAL_FUNC) command_self);
1292   command_unbind("watch", (SIGNAL_FUNC) command_self);
1293   command_unbind("stats", (SIGNAL_FUNC) command_self);
1294   command_unbind("attr", (SIGNAL_FUNC) command_attr);
1295   command_unbind("smsg", (SIGNAL_FUNC) command_smsg);
1296 }
1297
1298 #if 0
1299 void silc_server_free_ftp(SILC_SERVER_REC *server,
1300                           SilcClientEntry client_entry)
1301 {
1302   FtpSession ftp;
1303
1304   silc_dlist_start(server->ftp_sessions);
1305   while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1306     if (ftp->client_entry == client_entry) {
1307       silc_dlist_del(server->ftp_sessions, ftp);
1308       silc_free(ftp->filepath);
1309       silc_free(ftp);
1310     }
1311   }
1312 }
1313 #endif /* 0 */
1314
1315 bool silc_term_utf8(void)
1316 {
1317   const char *str;
1318   str = settings_get_str("term_type");
1319   if (str)
1320     if (g_strcasecmp(str, "utf-8") == 0)
1321       return TRUE;
1322   return FALSE;
1323 }