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