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