updates.
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
1 /*
2   silc-server.c : irssi
3
4   Copyright (C) 2000 - 2001 Timo Sirainen
5                             Pekka Riikonen <priikone@poseidon.pspt.fi>
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 "silc-servers.h"
38 #include "silc-channels.h"
39 #include "silc-queries.h"
40 #include "silc-nicklist.h"
41 #include "window-item-def.h"
42
43 #include "fe-common/core/printtext.h"
44 #include "fe-common/silc/module-formats.h"
45
46 void silc_servers_reconnect_init(void);
47 void silc_servers_reconnect_deinit(void);
48
49 static void silc_send_channel(SILC_SERVER_REC *server,
50                               char *channel, char *msg)
51 {
52   SILC_CHANNEL_REC *rec;
53   
54   rec = silc_channel_find(server, channel);
55   if (rec == NULL || rec->entry == NULL)
56     return;
57   
58   silc_client_send_channel_message(silc_client, server->conn, rec->entry, 
59                                    NULL, 0, msg, strlen(msg), TRUE);
60 }
61
62 typedef struct {
63   char *nick;
64   char *msg;
65   SILC_SERVER_REC *server;
66 } PRIVMSG_REC;
67
68 /* Callback function that sends the private message if the client was
69    resolved from the server. */
70
71 static void silc_send_msg_clients(SilcClient client,
72                                   SilcClientConnection conn,
73                                   SilcClientEntry *clients,
74                                   uint32 clients_count,
75                                   void *context)
76 {
77   PRIVMSG_REC *rec = context;
78   SILC_SERVER_REC *server = rec->server;
79   SilcClientEntry target;
80   char *nickname = NULL;
81
82   if (!clients_count) {
83     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
84               "%s: There is no such client", rec->nick);
85   } else {
86     if (clients_count > 1) {
87       silc_parse_userfqdn(rec->nick, &nickname, NULL);
88
89       /* Find the correct one. The rec->nick might be a formatted nick
90          so this will find the correct one. */
91       clients = silc_client_get_clients_local(silc_client, server->conn, 
92                                               nickname, rec->nick, 
93                                               &clients_count);
94       if (!clients) {
95         printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
96                   "%s: There is no such client", rec->nick);
97         silc_free(nickname);
98         goto out;
99       }
100       silc_free(nickname);
101     }
102
103     target = clients[0];
104
105     /* Still check for exact math for nickname, this compares the
106        real (formatted) nickname and the nick (maybe formatted) that
107        use gave. This is to assure that `nick' does not match 
108        `nick@host'. */
109     if (strcasecmp(rec->nick, clients[0]->nickname)) {
110       printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
111                 "%s: There is no such client", rec->nick);
112       goto out;
113     }
114
115     /* Send the private message */
116     silc_client_send_private_message(client, conn, target, 0,
117                                      rec->msg, strlen(rec->msg),
118                                      TRUE);
119   }
120   
121  out:
122   g_free(rec->nick);
123   g_free(rec->msg);
124   g_free(rec);
125 }
126
127 static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
128 {
129   PRIVMSG_REC *rec;
130   SilcClientEntry *clients;
131   uint32 clients_count;
132   char *nickname = NULL;
133
134   if (!silc_parse_userfqdn(nick, &nickname, NULL)) {
135     printformat_module("fe-common/silc", server, NULL,
136                        MSGLEVEL_CRAP, SILCTXT_BAD_NICK, nick);
137     return;
138   }
139
140   /* Find client entry */
141   clients = silc_client_get_clients_local(silc_client, server->conn, 
142                                           nickname, nick, &clients_count);
143   if (!clients) {
144     rec = g_new0(PRIVMSG_REC, 1);
145     rec->nick = g_strdup(nick);
146     rec->msg = g_strdup(msg);
147     rec->server = server;
148
149     /* Could not find client with that nick, resolve it from server. */
150     silc_client_get_clients(silc_client, server->conn,
151                             nickname, NULL, silc_send_msg_clients, rec);
152     silc_free(nickname);
153     return;
154   }
155
156   /* Send the private message directly */
157   silc_free(nickname);
158   silc_client_send_private_message(silc_client, server->conn, 
159                                    clients[0], 0, msg, strlen(msg), TRUE);
160 }
161
162 static int isnickflag_func(char flag)
163 {
164   return flag == '@' || flag == '+';
165 }
166
167 static int ischannel_func(const char *data)
168 {
169   return *data == '#';
170 }
171
172 const char *get_nick_flags(void)
173 {
174   return "@\0\0";
175 }
176
177 static void send_message(SILC_SERVER_REC *server, char *target, char *msg)
178 {
179   g_return_if_fail(server != NULL);
180   g_return_if_fail(target != NULL);
181   g_return_if_fail(msg != NULL);
182
183   if (*target == '#')
184     silc_send_channel(server, target, msg);
185   else
186     silc_send_msg(server, target, msg);
187 }
188
189 static void sig_connected(SILC_SERVER_REC *server)
190 {
191   SilcClientConnection conn;
192   int fd;
193
194   if (!IS_SILC_SERVER(server))
195     return;
196
197   conn = silc_client_add_connection(silc_client,
198                                     server->connrec->address,
199                                     server->connrec->port,
200                                     server);
201   server->conn = conn;
202         
203   fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
204   if (!silc_client_start_key_exchange(silc_client, conn, fd)) {
205     /* some internal error occured */
206     server_disconnect(SERVER(server));
207     signal_stop();
208     return;
209   }
210
211   server->isnickflag = isnickflag_func;
212   server->ischannel = ischannel_func;
213   server->get_nick_flags = get_nick_flags;
214   server->send_message = (void *) send_message;
215 }
216
217 static void sig_disconnected(SILC_SERVER_REC *server)
218 {
219   if (!IS_SILC_SERVER(server))
220     return;
221   
222   if (server->conn && server->conn->sock != NULL) {
223     silc_client_close_connection(silc_client, NULL, server->conn);
224     
225     /* SILC closes the handle */
226     g_io_channel_unref(net_sendbuffer_handle(server->handle));
227     net_sendbuffer_destroy(server->handle, FALSE);
228     server->handle = NULL;
229   }
230 }
231
232 SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn)
233 {
234   SILC_SERVER_REC *server;
235
236   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
237   if (conn->address == NULL || *conn->address == '\0') 
238     return NULL;
239   if (conn->nick == NULL || *conn->nick == '\0') {
240     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
241               "Cannot connect: nickname is not set");
242     return NULL;
243   }
244
245   server = g_new0(SILC_SERVER_REC, 1);
246   server->chat_type = SILC_PROTOCOL;
247   server->connrec = conn;
248   if (server->connrec->port <= 0) 
249     server->connrec->port = 706;
250
251   if (!server_start_connect((SERVER_REC *) server)) {
252     server_connect_free(SERVER_CONNECT(conn));
253     g_free(server);
254     return NULL;
255   }
256
257   server->ftp_sessions = silc_dlist_init();
258
259   return server;
260 }
261
262 /* Return a string of all channels in server in server->channels_join() 
263    format */
264
265 char *silc_server_get_channels(SILC_SERVER_REC *server)
266 {
267   GSList *tmp;
268   GString *chans;
269   char *ret;
270
271   g_return_val_if_fail(server != NULL, FALSE);
272
273   chans = g_string_new(NULL);
274   for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
275     CHANNEL_REC *channel = tmp->data;
276     
277     g_string_sprintfa(chans, "%s,", channel->name);
278   }
279
280   if (chans->len > 0)
281     g_string_truncate(chans, chans->len-1);
282
283   ret = chans->str;
284   g_string_free(chans, FALSE);
285   
286   return ret;
287 }
288
289 /* Syntaxes of all SILC commands for HELP files (the help file generation
290    will snoop these from here). */
291
292 /* SYNTAX: BAN <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
293 /* SYNTAX: CMODE <channel> +|-<modes> [{ <arguments>}] */
294 /* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] [-pubkey|<passwd>] */
295 /* SYNTAX: GETKEY <nickname or server name> */
296 /* SYNTAX: INVITE <channel> [<nickname>[@hostname>] */
297 /* SYNTAX: INVITE <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
298 /* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
299 /* SYNTAX: KEY CHANNEL <channel> set|unset|list|agreement|negotiate [<arguments>] */
300 /* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
301 /* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] */
302 /* SYNTAX: OPER <username> [-pubkey] */
303 /* SYNTAX: SILCOPER <username> [-pubkey] */
304 /* SYNTAX: TOPIC <channel> [<topic>] */
305 /* SYNTAX: UMODE +|-<modes> */
306 /* SYNTAX: WHOIS <nickname>[@<hostname>] [<count>] */
307 /* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
308 /* SYNTAX: CLOSE <server> [<port>] */
309 /* SYNTAX: SHUTDOWN */
310 /* SYNTAX: MOTD [<server>] */
311 /* SYNTAX: LIST [<channel>] */
312 /* SYNTAX: ME <message> */
313 /* SYNTAX: ACTION <channel> <message> */
314 /* SYNTAX: AWAY [<message>] */
315 /* SYNTAX: INFO [<server>] */
316 /* SYNTAX: NICK <nickname> */
317 /* SYNTAX: NOTICE <message> */
318 /* SYNTAX: PART [<channel>] */
319 /* SYNTAX: PING */
320 /* SYNTAX: SCONNECT <server> [<port>] */
321 /* SYNTAX: USERS <channel> */
322 /* SYNTAX: FILE SEND <filepath> <nickname> [<local IP> [<local port>]] */
323 /* SYNTAX: FILE RECEIVE [<nickname>] */
324 /* SYNTAX: FILE CLOSE [<nickname>] */
325 /* SYNTAX: FILE */
326 /* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder <-pubkey|passwd>] */
327
328 void silc_command_exec(SILC_SERVER_REC *server,
329                        const char *command, const char *args)
330 {
331   uint32 argc = 0;
332   unsigned char **argv;
333   uint32 *argv_lens, *argv_types;
334   char *data, *tmpcmd;
335   SilcClientCommand cmd;
336   SilcClientCommandContext ctx;
337
338   g_return_if_fail(server != NULL);
339
340   tmpcmd = g_strdup(command); 
341   g_strup(tmpcmd);
342   cmd = silc_client_command_find(silc_client, tmpcmd);
343   g_free(tmpcmd);
344   if (cmd == NULL)
345     return;
346   
347   /* Now parse all arguments */
348   data = g_strconcat(command, " ", args, NULL);
349   silc_parse_command_line(data, &argv, &argv_lens,
350                           &argv_types, &argc, cmd->max_args);
351   g_free(data);
352
353   /* Allocate command context. This and its internals must be free'd
354      by the command routine receiving it. */
355   ctx = silc_client_command_alloc();
356   ctx->client = silc_client;
357   ctx->conn = server->conn;
358   ctx->command = cmd;
359   ctx->argc = argc;
360   ctx->argv = argv;
361   ctx->argv_lens = argv_lens;
362   ctx->argv_types = argv_types;
363   
364   /* Execute command */
365   silc_client_command_call(cmd, ctx);
366 }
367
368 /* Generic command function to call any SILC command directly. */
369
370 static void command_self(const char *data, SILC_SERVER_REC *server,
371                          WI_ITEM_REC *item)
372 {
373   if (!IS_SILC_SERVER(server) || !server->connected) {
374     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
375     return;
376   }
377
378   if (IS_SILC_CHANNEL(item)) {
379     SILC_CHANNEL_REC *chanrec;
380     chanrec = silc_channel_find(server, item->name);
381     if (chanrec)
382       server->conn->current_channel = chanrec->entry;
383   }
384
385   silc_command_exec(server, current_command, data);
386   signal_stop();
387 }
388
389 /* SCONNECT command.  Calls actually SILC's CONNECT command since Irssi
390    has CONNECT command for other purposes. */
391
392 static void command_sconnect(const char *data, SILC_SERVER_REC *server)
393 {
394   if (!IS_SILC_SERVER(server) || !server->connected) {
395     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
396     return;
397   }
398
399   silc_command_exec(server, "CONNECT", data);
400   signal_stop();
401 }
402
403 static void event_text(const char *line, SILC_SERVER_REC *server,
404                        WI_ITEM_REC *item)
405 {
406   char *str;
407
408   g_return_if_fail(line != NULL);
409
410   if (!IS_SILC_ITEM(item))
411     return;
412
413   str = g_strdup_printf("%s %s", item->name, line);
414   signal_emit("command msg", 3, str, server, item);
415   g_free(str);
416
417  signal_stop();
418 }
419
420 /* FILE command */
421
422 SILC_TASK_CALLBACK(silc_client_file_close_later)
423 {
424   FtpSession ftp = (FtpSession)context;
425
426   SILC_LOG_DEBUG(("Start"));
427
428   silc_client_file_close(silc_client, ftp->conn, ftp->session_id);
429   silc_free(ftp->filepath);
430   silc_free(ftp);
431 }
432
433 static void silc_client_file_monitor(SilcClient client,
434                                      SilcClientConnection conn,
435                                      SilcClientMonitorStatus status,
436                                      SilcClientFileError error,
437                                      uint64 offset,
438                                      uint64 filesize,
439                                      SilcClientEntry client_entry,
440                                      uint32 session_id,
441                                      const char *filepath,
442                                      void *context)
443 {
444   SILC_SERVER_REC *server = (SILC_SERVER_REC *)context;
445   FtpSession ftp;
446   char fsize[32];
447
448   snprintf(fsize, sizeof(fsize) - 1, "%llu", ((filesize + 1023) / 1024));
449
450   silc_dlist_start(server->ftp_sessions);
451   while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
452     if (ftp->session_id == session_id) {
453       if (!ftp->filepath && filepath)
454         ftp->filepath = strdup(filepath);
455       break;
456     }
457   }
458
459   if (ftp == SILC_LIST_END)
460     return;
461
462   if (status == SILC_CLIENT_FILE_MONITOR_ERROR) {
463     if (error == SILC_CLIENT_FILE_NO_SUCH_FILE)
464       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
465                          SILCTXT_FILE_ERROR_NO_SUCH_FILE, 
466                          client_entry->nickname, 
467                          filepath ? filepath : "[N/A]");
468     else if (error == SILC_CLIENT_FILE_PERMISSION_DENIED)
469       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
470                          SILCTXT_FILE_ERROR_PERMISSION_DENIED, 
471                          client_entry->nickname);
472     else
473       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
474                          SILCTXT_FILE_ERROR, client_entry->nickname);
475     silc_schedule_task_add(silc_client->schedule, 0,
476                            silc_client_file_close_later, ftp,
477                            1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
478     if (ftp == server->current_session)
479       server->current_session = NULL;
480     silc_dlist_del(server->ftp_sessions, ftp);
481   }
482
483   if (status == SILC_CLIENT_FILE_MONITOR_KEY_AGREEMENT) {
484     printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
485                        SILCTXT_FILE_KEY_EXCHANGE, client_entry->nickname);
486   }
487
488   /* Save some transmission data */
489   if (offset && filesize) {
490     unsigned long delta = time(NULL) - ftp->starttime;
491
492     ftp->percent = ((double)offset / (double)filesize) * (double)100.0;
493     if (delta)
494       ftp->kps = (double)((offset / (double)delta) + 1023) / (double)1024;
495     else
496       ftp->kps = (double)(offset + 1023) / (double)1024;
497     ftp->offset = offset;
498     ftp->filesize = filesize;
499   }
500
501   if (status == SILC_CLIENT_FILE_MONITOR_SEND) {
502     if (offset == 0) {
503       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
504                          SILCTXT_FILE_TRANSMIT, filepath, fsize,
505                          client_entry->nickname);
506       ftp->starttime = time(NULL);
507     }
508     if (offset == filesize) {
509       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
510                          SILCTXT_FILE_TRANSMITTED, filepath, fsize,
511                          client_entry->nickname, ftp->kps);
512       silc_schedule_task_add(silc_client->schedule, 0,
513                              silc_client_file_close_later, ftp,
514                              1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
515       if (ftp == server->current_session)
516         server->current_session = NULL;
517       silc_dlist_del(server->ftp_sessions, ftp);
518     }
519   }
520
521   if (status == SILC_CLIENT_FILE_MONITOR_RECEIVE) {
522     if (offset == 0) {
523       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
524                          SILCTXT_FILE_RECEIVE, filepath, fsize,
525                          client_entry->nickname);
526       ftp->starttime = time(NULL);
527     }
528
529     if (offset == filesize) {
530       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
531                          SILCTXT_FILE_RECEIVED, filepath, fsize,
532                          client_entry->nickname, ftp->kps);
533       silc_schedule_task_add(silc_client->schedule, 0,
534                              silc_client_file_close_later, ftp,
535                              1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
536       if (ftp == server->current_session)
537         server->current_session = NULL;
538       silc_dlist_del(server->ftp_sessions, ftp);
539     }
540   }
541 }
542
543 typedef struct {
544   SILC_SERVER_REC *server;
545   char *data;
546   char *nick;
547   WI_ITEM_REC *item;
548 } *FileGetClients;
549
550 static void silc_client_command_file_get_clients(SilcClient client,
551                                                  SilcClientConnection conn,
552                                                  SilcClientEntry *clients,
553                                                  uint32 clients_count,
554                                                  void *context)
555 {
556   FileGetClients internal = (FileGetClients)context;
557
558   if (!clients) {
559     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", 
560               internal->nick);
561     silc_free(internal->data);
562     silc_free(internal->nick);
563     silc_free(internal);
564     return;
565   }
566
567   signal_emit("command file", 3, internal->data, internal->server,
568               internal->item);
569
570   silc_free(internal->data);
571   silc_free(internal->nick);
572   silc_free(internal);
573 }
574
575 static void command_file(const char *data, SILC_SERVER_REC *server,
576                          WI_ITEM_REC *item)
577 {
578   SilcClientConnection conn;
579   SilcClientEntry *entrys, client_entry;
580   SilcClientFileError ret;
581   uint32 entry_count;
582   char *nickname = NULL, *tmp;
583   unsigned char **argv;
584   uint32 argc;
585   uint32 *argv_lens, *argv_types;
586   int type = 0;
587   FtpSession ftp;
588   char *local_ip = NULL;
589   uint32 local_port = 0;
590
591   if (!server || !IS_SILC_SERVER(server) || !server->connected)
592     cmd_return_error(CMDERR_NOT_CONNECTED);
593
594   conn = server->conn;
595
596   /* Now parse all arguments */
597   tmp = g_strconcat("FILE", " ", data, NULL);
598   silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
599   g_free(tmp);
600
601   if (argc == 1)
602     type = 4;
603
604   if (argc >= 2) {
605     if (!strcasecmp(argv[1], "send"))
606       type = 1;
607     if (!strcasecmp(argv[1], "receive"))
608       type = 2;
609     if (!strcasecmp(argv[1], "close"))
610       type = 3;
611   }
612   
613   if (type == 0)
614     cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
615
616   switch (type) {
617   case 1:
618     if (argc < 4)
619       cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
620
621     /* Parse the typed nickname. */
622     if (!silc_parse_userfqdn(argv[3], &nickname, NULL)) {
623       printformat_module("fe-common/silc", server, NULL,
624                          MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[3]);
625       goto out;
626     }
627     
628     /* Find client entry */
629     entrys = silc_client_get_clients_local(silc_client, conn, nickname,
630                                            argv[3], &entry_count);
631     if (!entrys) {
632       FileGetClients inter = silc_calloc(1, sizeof(*inter));
633       inter->server = server;
634       inter->data = strdup(data);
635       inter->nick = strdup(nickname);
636       inter->item = item;
637       silc_client_get_clients(silc_client, conn, nickname, argv[3],
638                               silc_client_command_file_get_clients, inter);
639       goto out;
640     }
641     client_entry = entrys[0];
642     silc_free(entrys);
643
644     if (argc >= 5)
645       local_ip = argv[4];
646     if (argc >= 6)
647       local_port = atoi(argv[5]);
648
649     ftp = silc_calloc(1, sizeof(*ftp));
650     ftp->session_id = 
651       silc_client_file_send(silc_client, conn, silc_client_file_monitor, 
652                             server, local_ip, local_port, 
653                             client_entry, argv[2]);
654
655     printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
656                        SILCTXT_FILE_SEND, client_entry->nickname,
657                        argv[2]);
658
659     ftp->client_entry = client_entry;
660     ftp->filepath = strdup(argv[2]);
661     ftp->conn = conn;
662     ftp->send = TRUE;
663     silc_dlist_add(server->ftp_sessions, ftp);
664     server->current_session = ftp;
665
666     break;
667
668   case 2:
669     /* Parse the typed nickname. */
670     if (argc >= 3) {
671       if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
672         printformat_module("fe-common/silc", server, NULL,
673                            MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
674         goto out;
675       }
676     
677       /* Find client entry */
678       entrys = silc_client_get_clients_local(silc_client, conn, nickname,
679                                              argv[2], &entry_count);
680       if (!entrys) {
681         FileGetClients inter = silc_calloc(1, sizeof(*inter));
682         inter->server = server;
683         inter->data = strdup(data);
684         inter->nick = strdup(nickname);
685         inter->item = item;
686         silc_client_get_clients(silc_client, conn, nickname, argv[2],
687                                 silc_client_command_file_get_clients, inter);
688         goto out;
689       }
690       client_entry = entrys[0];
691       silc_free(entrys);
692     } else {
693       if (!server->current_session) {
694         printformat_module("fe-common/silc", server, NULL,
695                            MSGLEVEL_CRAP, SILCTXT_FILE_NA);
696         goto out;
697       }
698
699       ret = silc_client_file_receive(silc_client, conn, 
700                                      silc_client_file_monitor, server,
701                                      server->current_session->session_id);
702       if (ret != SILC_CLIENT_FILE_OK) {
703         if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
704           printformat_module("fe-common/silc", server, NULL,
705                              MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
706                              server->current_session->client_entry->nickname);
707         else
708           printformat_module("fe-common/silc", server, NULL,
709                              MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
710                              server->current_session->client_entry->nickname);
711       }
712
713       goto out;
714     }
715
716     silc_dlist_start(server->ftp_sessions);
717     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
718       if (ftp->client_entry == client_entry && !ftp->filepath) {
719         ret = silc_client_file_receive(silc_client, conn, 
720                                        silc_client_file_monitor, server,
721                                        ftp->session_id);
722         if (ret != SILC_CLIENT_FILE_OK) {
723           if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
724             printformat_module("fe-common/silc", server, NULL,
725                                MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
726                                client_entry->nickname);
727           else
728             printformat_module("fe-common/silc", server, NULL,
729                                MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
730                                client_entry->nickname);
731         }
732         break;
733       }
734     }
735
736     if (ftp == SILC_LIST_END) {
737       printformat_module("fe-common/silc", server, NULL,
738                          MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
739                          client_entry->nickname);
740       goto out;
741     }
742     break;
743
744   case 3:
745     /* Parse the typed nickname. */
746     if (argc >= 3) {
747       if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
748         printformat_module("fe-common/silc", server, NULL,
749                            MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
750         goto out;
751       }
752     
753       /* Find client entry */
754       entrys = silc_client_get_clients_local(silc_client, conn, nickname,
755                                              argv[2], &entry_count);
756       if (!entrys) {
757         FileGetClients inter = silc_calloc(1, sizeof(*inter));
758         inter->server = server;
759         inter->data = strdup(data);
760         inter->nick = strdup(nickname);
761         inter->item = item;
762         silc_client_get_clients(silc_client, conn, nickname, argv[2],
763                                 silc_client_command_file_get_clients, inter);
764         goto out;
765       }
766       client_entry = entrys[0];
767       silc_free(entrys);
768     } else {
769       if (!server->current_session) {
770         printformat_module("fe-common/silc", server, NULL,
771                            MSGLEVEL_CRAP, SILCTXT_FILE_NA);
772         goto out;
773       }
774  
775       silc_client_file_close(silc_client, conn, 
776                              server->current_session->session_id);
777       printformat_module("fe-common/silc", server, NULL,
778                          MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
779                          server->current_session->client_entry->nickname,
780                          server->current_session->filepath ?
781                          server->current_session->filepath : "[N/A]");
782       silc_dlist_del(server->ftp_sessions, server->current_session);
783       silc_free(server->current_session->filepath);
784       silc_free(server->current_session);
785       server->current_session = NULL;
786       goto out;
787     }
788
789     silc_dlist_start(server->ftp_sessions);
790     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
791       if (ftp->client_entry == client_entry) {
792         silc_client_file_close(silc_client, conn, ftp->session_id);
793         printformat_module("fe-common/silc", server, NULL,
794                            MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
795                            client_entry->nickname,
796                            ftp->filepath ? ftp->filepath : "[N/A]");
797         if (ftp == server->current_session)
798           server->current_session = NULL;
799         silc_dlist_del(server->ftp_sessions, ftp);
800         silc_free(ftp->filepath);
801         silc_free(ftp);
802         break;
803       }
804     }
805
806     if (ftp == SILC_LIST_END) {
807       printformat_module("fe-common/silc", server, NULL,
808                          MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
809                          client_entry->nickname);
810       goto out;
811     }
812     break;
813
814   case 4:
815
816     if (!silc_dlist_count(server->ftp_sessions)) {
817       printformat_module("fe-common/silc", server, NULL,
818                          MSGLEVEL_CRAP, SILCTXT_FILE_NA);
819       goto out;
820     }
821
822     printformat_module("fe-common/silc", server, NULL,
823                        MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_HEADER);
824
825     silc_dlist_start(server->ftp_sessions);
826     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
827       printformat_module("fe-common/silc", server, NULL,
828                          MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_LINE,
829                          ftp->client_entry->nickname, 
830                          ftp->send ? "send" : "receive",
831                          (uint32)(ftp->offset + 1023) / 1024,
832                          (uint32)(ftp->filesize + 1023) / 1024,
833                          ftp->percent, ftp->kps,
834                          ftp->filepath ? ftp->filepath : "[N/A]");
835     }
836
837     break;
838
839   default:
840     break;
841   }
842
843  out:
844   silc_free(nickname);
845 }
846
847 void silc_server_init(void)
848 {
849   silc_servers_reconnect_init();
850
851   signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
852   signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
853   signal_add("send text", (SIGNAL_FUNC) event_text);
854   command_bind("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
855   command_bind("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
856   command_bind("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
857   command_bind("topic", MODULE_NAME, (SIGNAL_FUNC) command_self);
858   command_bind("cmode", MODULE_NAME, (SIGNAL_FUNC) command_self);
859   command_bind("cumode", MODULE_NAME, (SIGNAL_FUNC) command_self);
860   command_bind("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
861   command_bind("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
862   command_bind("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
863   command_bind("oper", MODULE_NAME, (SIGNAL_FUNC) command_self);
864   command_bind("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
865   command_bind("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
866   command_bind("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
867   command_bind("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
868   command_bind("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
869   command_bind("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
870   command_bind("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
871   command_bind("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
872   command_bind("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
873   command_bind("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
874   command_bind("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
875   command_bind("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
876   command_bind("file", MODULE_NAME, (SIGNAL_FUNC) command_file);
877
878   command_set_options("connect", "+silcnet");
879 }
880
881 void silc_server_deinit(void)
882 {
883   silc_servers_reconnect_deinit();
884
885   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
886   signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
887   signal_remove("send text", (SIGNAL_FUNC) event_text);
888   command_unbind("whois", (SIGNAL_FUNC) command_self);
889   command_unbind("whowas", (SIGNAL_FUNC) command_self);
890   command_unbind("nick", (SIGNAL_FUNC) command_self);
891   command_unbind("topic", (SIGNAL_FUNC) command_self);
892   command_unbind("cmode", (SIGNAL_FUNC) command_self);
893   command_unbind("cumode", (SIGNAL_FUNC) command_self);
894   command_unbind("users", (SIGNAL_FUNC) command_self);
895   command_unbind("list", (SIGNAL_FUNC) command_self);
896   command_unbind("oper", (SIGNAL_FUNC) command_self);
897   command_unbind("silcoper", (SIGNAL_FUNC) command_self);
898   command_unbind("umode", (SIGNAL_FUNC) command_self);
899   command_unbind("invite", (SIGNAL_FUNC) command_self);
900   command_unbind("kill", (SIGNAL_FUNC) command_self);
901   command_unbind("kick", (SIGNAL_FUNC) command_self);
902   command_unbind("info", (SIGNAL_FUNC) command_self);
903   command_unbind("ping", (SIGNAL_FUNC) command_self);
904   command_unbind("motd", (SIGNAL_FUNC) command_self);
905   command_unbind("ban", (SIGNAL_FUNC) command_self);
906   command_unbind("close", (SIGNAL_FUNC) command_self);
907   command_unbind("shutdown", (SIGNAL_FUNC) command_self);
908   command_unbind("getkey", (SIGNAL_FUNC) command_self);
909   command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
910   command_unbind("file", (SIGNAL_FUNC) command_file);
911 }
912
913 void silc_server_free_ftp(SILC_SERVER_REC *server,
914                           SilcClientEntry client_entry)
915 {
916   FtpSession ftp;
917
918   silc_dlist_start(server->ftp_sessions);
919   while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
920     if (ftp->client_entry == client_entry) {
921       silc_dlist_del(server->ftp_sessions, ftp);
922       silc_free(ftp->filepath);
923       silc_free(ftp);
924     }
925   }
926 }