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