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