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