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