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