Merged silc_1_0_branch to trunk.
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
index f6e0403af4169d9ff6b26f5a3784b4ae30939e28..476205d450e0cfcf3c00b81e9f4da307873bf1b2 100644 (file)
@@ -1,14 +1,14 @@
 /*
   silc-server.c : irssi
 
-  Copyright (C) 2000 - 2001 Timo Sirainen
-                            Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Copyright (C) 2000 - 2005 Timo Sirainen
+                            Pekka Riikonen <priikone@silcnet.org>
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
-  
+
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 #include "servers-setup.h"
 
+#include "client_ops.h"
 #include "silc-servers.h"
 #include "silc-channels.h"
 #include "silc-queries.h"
 #include "silc-nicklist.h"
+#include "silc-cmdqueue.h"
 #include "window-item-def.h"
 
 #include "fe-common/core/printtext.h"
 void silc_servers_reconnect_init(void);
 void silc_servers_reconnect_deinit(void);
 
-static void silc_send_channel(SILC_SERVER_REC *server,
-                             char *channel, char *msg)
+int silc_send_channel(SILC_SERVER_REC *server,
+                     char *channel, char *msg,
+                     SilcMessageFlags flags)
 {
   SILC_CHANNEL_REC *rec;
-  
+
   rec = silc_channel_find(server, channel);
   if (rec == NULL || rec->entry == NULL) {
-    cmd_return_error(CMDERR_NOT_JOINED);
-    return;
+    cmd_return_error_value(CMDERR_NOT_JOINED, FALSE);
   }
 
-  silc_client_send_channel_message(silc_client, server->conn, rec->entry, 
-                                  NULL, SILC_MESSAGE_FLAG_UTF8,
-                                  msg, strlen(msg), TRUE);
+  silc_client_send_channel_message(silc_client, server->conn, rec->entry,
+                                  NULL, flags, msg, strlen(msg), TRUE);
+  return TRUE;
 }
 
 typedef struct {
@@ -89,7 +91,7 @@ static void silc_send_msg_clients(SilcClient client,
   char *nickname = NULL;
 
   if (!clients_count) {
-    printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
+    printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
              "%s: There is no such client", rec->nick);
   } else {
     if (clients_count > 1) {
@@ -97,11 +99,11 @@ static void silc_send_msg_clients(SilcClient client,
 
       /* Find the correct one. The rec->nick might be a formatted nick
         so this will find the correct one. */
-      clients = silc_client_get_clients_local(silc_client, server->conn, 
-                                             nickname, rec->nick, 
+      clients = silc_client_get_clients_local(silc_client, server->conn,
+                                             nickname, rec->nick,
                                              &clients_count);
       if (!clients) {
-       printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
+       printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
                  "%s: There is no such client", rec->nick);
        silc_free(nickname);
        goto out;
@@ -113,29 +115,29 @@ static void silc_send_msg_clients(SilcClient client,
 
     /* Still check for exact math for nickname, this compares the
        real (formatted) nickname and the nick (maybe formatted) that
-       use gave. This is to assure that `nick' does not match 
+       use gave. This is to assure that `nick' does not match
        `nick@host'. */
-    if (strcasecmp(rec->nick, clients[0]->nickname)) {
-      printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
+    if (!silc_utf8_strcasecmp(rec->nick, clients[0]->nickname)) {
+      printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
                "%s: There is no such client", rec->nick);
       goto out;
     }
 
     /* Send the private message */
-    silc_client_send_private_message(client, conn, target, 
+    silc_client_send_private_message(client, conn, target,
                                     rec->flags,
                                     rec->msg, rec->len,
                                     TRUE);
   }
-  
+
  out:
   g_free(rec->nick);
   g_free(rec->msg);
   g_free(rec);
 }
 
-static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg,
-                       int msg_len, SilcMessageFlags flags)
+int silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg,
+                 int msg_len, SilcMessageFlags flags)
 {
   PRIVMSG_REC *rec;
   SilcClientEntry *clients;
@@ -145,11 +147,11 @@ static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg,
   if (!silc_parse_userfqdn(nick, &nickname, NULL)) {
     printformat_module("fe-common/silc", server, NULL,
                       MSGLEVEL_CRAP, SILCTXT_BAD_NICK, nick);
-    return;
+    return FALSE;
   }
 
   /* Find client entry */
-  clients = silc_client_get_clients_local(silc_client, server->conn, 
+  clients = silc_client_get_clients_local(silc_client, server->conn,
                                          nickname, nick, &clients_count);
   if (!clients) {
     rec = g_new0(PRIVMSG_REC, 1);
@@ -163,57 +165,58 @@ static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg,
     silc_client_get_clients(silc_client, server->conn,
                            nickname, NULL, silc_send_msg_clients, rec);
     silc_free(nickname);
-    return;
+    return TRUE;
   }
 
   /* Send the private message directly */
   silc_free(nickname);
-  silc_client_send_private_message(silc_client, server->conn, 
+  silc_client_send_private_message(silc_client, server->conn,
                                   clients[0], flags,
                                   msg, msg_len, TRUE);
+  return TRUE;
 }
 
-void silc_send_mime(SILC_SERVER_REC *server, WI_ITEM_REC *to,
-                   const char *data, int data_len,
-                   const char *enc, const char *type)
+void silc_send_mime(SILC_SERVER_REC *server, int channel, const char *to,
+                   const char *data, int sign)
 {
-  SILC_CHANNEL_REC *channel;
-  QUERY_REC *query;
-  char *mime_data;
-  int mime_data_len;
-  
-  if (!(IS_SILC_SERVER(server)) || (data == NULL) || (to == NULL) || 
-      (enc == NULL) || (type == NULL))
-    return;
-           
-#define SILC_MIME_HEADER "MIME-Version: 1.0\r\nContent-Type: %s\r\nContent-Transfer-Encoding: %s\r\n\r\n"
+  char *unescaped_data;
+  SilcUInt32 unescaped_data_len;
+  int target_type;
 
-  mime_data_len = data_len + strlen(SILC_MIME_HEADER) - 4
-    + strlen(enc) + strlen(type);
-  if (mime_data_len >= SILC_PACKET_MAX_LEN)
+  if (!(IS_SILC_SERVER(server)) || (data == NULL) || (to == NULL))
     return;
 
-  /* we risk to large packets here... */
-  mime_data = silc_calloc(mime_data_len, sizeof(*mime_data));
-  snprintf(mime_data, mime_data_len, SILC_MIME_HEADER, type, enc);
-  memmove(mime_data + strlen(SILC_MIME_HEADER) - 4 + strlen(enc) + strlen(type),
-         data, data_len);
-
-#undef SILC_MIME_HEADER
-
-  if (IS_SILC_CHANNEL(to)) {
-    channel = SILC_CHANNEL(to);
-    silc_client_send_channel_message(silc_client, server->conn, channel->entry, 
-                                    NULL, SILC_MESSAGE_FLAG_DATA,
-                                    mime_data, mime_data_len, TRUE);
-  } else if (IS_SILC_QUERY(to)) {
-    query = SILC_QUERY(to);
-    silc_send_msg(server, query->name, mime_data, mime_data_len,
-                 SILC_MESSAGE_FLAG_DATA);
-    
+  if (channel) {
+    target_type = SEND_TARGET_CHANNEL;
+  } else {
+    target_type = server_ischannel(SERVER(server), to) ?
+      SEND_TARGET_CHANNEL : SEND_TARGET_NICK;
+  }
+
+  unescaped_data = silc_unescape_data(data, &unescaped_data_len);
+
+  if (target_type == SEND_TARGET_CHANNEL) {
+    SILC_CHANNEL_REC *rec;
+
+    rec = silc_channel_find(server, to);
+    if (rec == NULL || rec->entry == NULL) {
+      cmd_return_error(CMDERR_NOT_JOINED);
+    }
+
+    silc_client_send_channel_message(silc_client, server->conn, rec->entry,
+                                    NULL, SILC_MESSAGE_FLAG_DATA |
+                                    (sign ? SILC_MESSAGE_FLAG_SIGNED : 0),
+                                    unescaped_data, unescaped_data_len, TRUE);
+  } else {
+    silc_send_msg(server, (char *)to, unescaped_data, unescaped_data_len,
+                 SILC_MESSAGE_FLAG_DATA |
+                 (sign ? SILC_MESSAGE_FLAG_SIGNED : 0));
+
   }
 
-  silc_free(mime_data);
+  signal_stop();
+
+  silc_free(unescaped_data);
 }
 
 static int isnickflag_func(char flag)
@@ -234,7 +237,7 @@ const char *get_nick_flags(void)
 static void send_message(SILC_SERVER_REC *server, char *target,
                         char *msg, int target_type)
 {
-  char *message = NULL;
+  char *message = NULL, *t = NULL;
   int len;
 
   g_return_if_fail(server != NULL);
@@ -242,23 +245,33 @@ static void send_message(SILC_SERVER_REC *server, char *target,
   g_return_if_fail(msg != NULL);
 
   if (!silc_term_utf8()) {
-    len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LANGUAGE);
+    len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LOCALE);
     message = silc_calloc(len + 1, sizeof(*message));
     g_return_if_fail(message != NULL);
-    silc_utf8_encode(msg, strlen(msg), SILC_STRING_LANGUAGE, message, len);
+    silc_utf8_encode(msg, strlen(msg), SILC_STRING_LOCALE, message, len);
   }
 
   if (target_type == SEND_TARGET_CHANNEL)
-    silc_send_channel(server, target, message ? message : msg);
-  else
-    silc_send_msg(server, target, message ? message : msg,
+    silc_send_channel(server, target, message ? message : msg,
+                     SILC_MESSAGE_FLAG_UTF8);
+  else {
+    if (!silc_term_utf8()) {
+      len = silc_utf8_encoded_len(target, strlen(target), SILC_STRING_LOCALE);
+      t = silc_calloc(len + 1, sizeof(*t));
+      g_return_if_fail(t != NULL);
+      silc_utf8_encode(target, strlen(target), SILC_STRING_LOCALE, t, len);
+    }
+
+    silc_send_msg(server, t ? t : target, message ? message : msg,
                  message ? strlen(message) : strlen(msg),
                  SILC_MESSAGE_FLAG_UTF8);
+  }
 
   silc_free(message);
+  silc_free(t);
 }
 
-void silc_send_heartbeat(SilcSocketConnection sock, 
+void silc_send_heartbeat(SilcSocketConnection sock,
                         void *hb_context)
 {
   SILC_SERVER_REC *server = SILC_SERVER(hb_context);
@@ -266,15 +279,15 @@ void silc_send_heartbeat(SilcSocketConnection sock,
   if (server == NULL)
     return;
 
-  silc_client_packet_send(silc_client, sock, SILC_PACKET_HEARTBEAT,
-                         NULL, 0, NULL, NULL, NULL, 0, FALSE);
+  silc_client_send_packet(silc_client, server->conn, SILC_PACKET_HEARTBEAT,
+                         NULL, 0);
 }
 
 static void sig_connected(SILC_SERVER_REC *server)
 {
   SilcClientConnection conn;
   SilcClientConnectionParams params;
-  char file[256];
+  char *file;
   int fd;
 
   if (!IS_SILC_SERVER(server))
@@ -282,8 +295,7 @@ static void sig_connected(SILC_SERVER_REC *server)
 
   /* Try to read detached session data and use it if found. */
   memset(&params, 0, sizeof(params));
-  memset(file, 0, sizeof(file));
-  snprintf(file, sizeof(file) - 1, "%s/session", get_irssi_dir());
+  file = silc_get_session_filename(server);
   params.detach_data = silc_file_readfile(file, &params.detach_data_len);
   if (params.detach_data)
     params.detach_data[params.detach_data_len] = 0;
@@ -296,12 +308,10 @@ static void sig_connected(SILC_SERVER_REC *server)
   server->conn = conn;
 
   if (params.detach_data)
-    keyboard_entry_redirect(NULL,
-                           "-- Resuming old session, may take a while ...",
-                           ENTRY_REDIRECT_FLAG_HIDDEN, server);
+    printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+                       SILCTXT_REATTACH, server->tag);
 
   silc_free(params.detach_data);
-  unlink(file);
 
   fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
 
@@ -347,7 +357,7 @@ SERVER_REC *silc_server_init_connect(SERVER_CONNECT_REC *conn)
   SILC_SERVER_REC *server;
 
   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
-  if (conn->address == NULL || *conn->address == '\0') 
+  if (conn->address == NULL || *conn->address == '\0')
     return NULL;
   if (conn->nick == NULL || *conn->nick == '\0') {
     silc_say_error("Cannot connect: nickname is not set");
@@ -359,7 +369,7 @@ SERVER_REC *silc_server_init_connect(SERVER_CONNECT_REC *conn)
   server->connrec = (SILC_SERVER_CONNECT_REC *)conn;
   server_connect_ref(conn);
 
-  if (server->connrec->port <= 0) 
+  if (server->connrec->port <= 0)
     server->connrec->port = 706;
 
   server_connect_init((SERVER_REC *)server);
@@ -375,7 +385,7 @@ void silc_server_connect(SERVER_REC *server)
   }
 }
 
-/* Return a string of all channels in server in server->channels_join() 
+/* Return a string of all channels in server in server->channels_join()
    format */
 
 char *silc_server_get_channels(SILC_SERVER_REC *server)
@@ -389,7 +399,7 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
   chans = g_string_new(NULL);
   for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
     CHANNEL_REC *channel = tmp->data;
-    
+
     g_string_sprintfa(chans, "%s,", channel->name);
   }
 
@@ -398,7 +408,7 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
 
   ret = chans->str;
   g_string_free(chans, FALSE);
-  
+
   return ret;
 }
 
@@ -414,36 +424,38 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
 /* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
 /* SYNTAX: KEY CHANNEL <channel> set|unset|list|change [<arguments>] */
 /* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
-/* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] */
+/* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] [-pubkey] */
 /* SYNTAX: OPER <username> [-pubkey] */
 /* SYNTAX: SILCOPER <username> [-pubkey] */
 /* SYNTAX: TOPIC <channel> [<topic>] */
 /* SYNTAX: UMODE +|-<modes> */
-/* SYNTAX: WHOIS <nickname>[@<hostname>] [-details] [<count>] */
+/* SYNTAX: WHOIS [<nickname>[@<hostname>]] [-details] [-pubkey <pubkeyfile>] [<count>] */
 /* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
 /* SYNTAX: CLOSE <server> [<port>] */
 /* SYNTAX: SHUTDOWN */
 /* SYNTAX: MOTD [<server>] */
 /* SYNTAX: LIST [<channel>] */
 /* SYNTAX: ME <message> */
-/* SYNTAX: ACTION <channel> <message> */
+/* SYNTAX: ACTION [-sign] [-channel] <target> <message> */
 /* SYNTAX: AWAY [<message>] */
 /* SYNTAX: INFO [<server>] */
 /* SYNTAX: NICK <nickname> */
-/* SYNTAX: NOTICE <message> */
+/* SYNTAX: NOTICE [-sign] [-channel] <target> <message> */
 /* SYNTAX: PART [<channel>] */
 /* SYNTAX: PING */
 /* SYNTAX: SCONNECT <server> [<port>] */
 /* SYNTAX: USERS <channel> */
-/* SYNTAX: FILE SEND <filepath> <nickname> [<local IP> [<local port>]] */
-/* SYNTAX: FILE RECEIVE [<nickname>] */
+/* SYNTAX: FILE SEND <filepath> <nickname> [<local IP> [<local port>]] [-no-listener]*/
+/* SYNTAX: FILE ACCEPT [<nickname>] */
 /* SYNTAX: FILE CLOSE [<nickname>] */
 /* SYNTAX: FILE */
-/* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder] */
+/* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder] [-auth [<pubkeyfile> <privkeyfile> [<privkey passphrase>]]]*/
 /* SYNTAX: DETACH */
-/* SYNTAX: WATCH [<-add | -del> <nickname>] */
+/* SYNTAX: WATCH [<-add | -del> <nickname>] [-pubkey +|-<pubkeyfile>] */
 /* SYNTAX: STATS */
 /* SYNTAX: ATTR [<-del> <option> [{ <value>}]] */
+/* SYNTAX: SMSG [<-channel>] <target> <message> */
+/* SYNTAX: LISTKEYS [-servers] [-clients] [<public key file>] */
 
 void silc_command_exec(SILC_SERVER_REC *server,
                       const char *command, const char *args)
@@ -453,7 +465,7 @@ void silc_command_exec(SILC_SERVER_REC *server,
 
   /* Call the command */
   data = g_strconcat(command, " ", args, NULL);
-  silc_client_command_call(silc_client, server->conn, data);
+  silc_queue_command_call(silc_client, server->conn, data);
   g_free(data);
 }
 
@@ -495,6 +507,86 @@ static void command_sconnect(const char *data, SILC_SERVER_REC *server)
   signal_stop();
 }
 
+/* SMSG command, to send digitally signed messages */
+
+static void command_smsg(const char *data, SILC_SERVER_REC *server,
+                        WI_ITEM_REC *item)
+{
+  GHashTable *optlist;
+  char *target, *origtarget, *msg;
+  void *free_arg;
+  int free_ret, target_type;
+
+  g_return_if_fail(data != NULL);
+  if (server == NULL || !server->connected)
+    cmd_param_error(CMDERR_NOT_CONNECTED);
+
+  if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
+                     PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
+                     "msg", &optlist, &target, &msg))
+    return;
+  if (*target == '\0' || *msg == '\0')
+    cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
+
+  origtarget = target;
+  free_ret = FALSE;
+
+  if (strcmp(target, "*") == 0) {
+    if (item == NULL)
+      cmd_param_error(CMDERR_NOT_JOINED);
+
+    target_type = IS_CHANNEL(item) ?
+      SEND_TARGET_CHANNEL : SEND_TARGET_NICK;
+    target = (char *) window_item_get_target(item);
+  } else if (g_hash_table_lookup(optlist, "channel") != NULL) {
+    target_type = SEND_TARGET_CHANNEL;
+  } else {
+    target_type = server_ischannel(SERVER(server), target) ?
+      SEND_TARGET_CHANNEL : SEND_TARGET_NICK;
+  }
+
+  if (target != NULL) {
+    char *message = NULL, *t = NULL;
+    int len, result;
+
+    if (!silc_term_utf8()) {
+      len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LOCALE);
+      message = silc_calloc(len + 1, sizeof(*message));
+      g_return_if_fail(message != NULL);
+      silc_utf8_encode(msg, strlen(msg), SILC_STRING_LOCALE, message, len);
+    }
+
+    if (target_type == SEND_TARGET_CHANNEL)
+      result = silc_send_channel(server, target, message ? message : msg,
+                                SILC_MESSAGE_FLAG_UTF8 |
+                                SILC_MESSAGE_FLAG_SIGNED);
+    else {
+      if (!silc_term_utf8()) {
+       len = silc_utf8_encoded_len(target, strlen(target),
+                                   SILC_STRING_LOCALE);
+       t = silc_calloc(len + 1, sizeof(*t));
+       g_return_if_fail(t != NULL);
+       silc_utf8_encode(target, strlen(target), SILC_STRING_LOCALE, t, len);
+      }
+      result = silc_send_msg(server, t ? t : target, message ? message : msg,
+                            message ? strlen(message) : strlen(msg),
+                            SILC_MESSAGE_FLAG_UTF8 |
+                            SILC_MESSAGE_FLAG_SIGNED);
+    }
+    silc_free(message);
+    silc_free(t);
+    if (!result)
+      goto out;
+  }
+
+  signal_emit(target != NULL && target_type == SEND_TARGET_CHANNEL ?
+             "message signed_own_public" : "message signed_own_private", 4,
+             server, msg, target, origtarget);
+out:
+  if (free_ret && target != NULL) g_free(target);
+  cmd_params_free(free_arg);
+}
+
 /* FILE command */
 
 SILC_TASK_CALLBACK(silc_client_file_close_later)
@@ -523,6 +615,9 @@ static void silc_client_file_monitor(SilcClient client,
   FtpSession ftp;
   char fsize[32];
 
+  if (status == SILC_CLIENT_FILE_MONITOR_CLOSED)
+    return;
+
   snprintf(fsize, sizeof(fsize) - 1, "%llu", ((filesize + 1023) / 1024));
 
   silc_dlist_start(server->ftp_sessions);
@@ -540,12 +635,12 @@ static void silc_client_file_monitor(SilcClient client,
   if (status == SILC_CLIENT_FILE_MONITOR_ERROR) {
     if (error == SILC_CLIENT_FILE_NO_SUCH_FILE)
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
-                        SILCTXT_FILE_ERROR_NO_SUCH_FILE, 
-                        client_entry->nickname, 
+                        SILCTXT_FILE_ERROR_NO_SUCH_FILE,
+                        client_entry->nickname,
                         filepath ? filepath : "[N/A]");
     else if (error == SILC_CLIENT_FILE_PERMISSION_DENIED)
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
-                        SILCTXT_FILE_ERROR_PERMISSION_DENIED, 
+                        SILCTXT_FILE_ERROR_PERMISSION_DENIED,
                         client_entry->nickname);
     else
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
@@ -553,9 +648,12 @@ static void silc_client_file_monitor(SilcClient client,
     silc_schedule_task_add(silc_client->schedule, 0,
                           silc_client_file_close_later, ftp,
                           1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
-    if (ftp == server->current_session)
-      server->current_session = NULL;
     silc_dlist_del(server->ftp_sessions, ftp);
+    if (ftp == server->current_session) {
+      server->current_session = NULL;
+      silc_dlist_start(server->ftp_sessions);
+      server->current_session = silc_dlist_get(server->ftp_sessions);
+    }
   }
 
   if (status == SILC_CLIENT_FILE_MONITOR_KEY_AGREEMENT) {
@@ -590,9 +688,13 @@ static void silc_client_file_monitor(SilcClient client,
       silc_schedule_task_add(silc_client->schedule, 0,
                             silc_client_file_close_later, ftp,
                             1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
-      if (ftp == server->current_session)
-       server->current_session = NULL;
       silc_dlist_del(server->ftp_sessions, ftp);
+      if (ftp == server->current_session) {
+       server->current_session = NULL;
+       silc_dlist_start(server->ftp_sessions);
+       server->current_session = silc_dlist_get(server->ftp_sessions);
+      }
+
     }
   }
 
@@ -611,9 +713,13 @@ static void silc_client_file_monitor(SilcClient client,
       silc_schedule_task_add(silc_client->schedule, 0,
                             silc_client_file_close_later, ftp,
                             1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
-      if (ftp == server->current_session)
-       server->current_session = NULL;
       silc_dlist_del(server->ftp_sessions, ftp);
+      if (ftp == server->current_session) {
+       server->current_session = NULL;
+       silc_dlist_start(server->ftp_sessions);
+       server->current_session = silc_dlist_get(server->ftp_sessions);
+      }
+
     }
   }
 }
@@ -634,7 +740,7 @@ static void silc_client_command_file_get_clients(SilcClient client,
   FileGetClients internal = (FileGetClients)context;
 
   if (!clients) {
-    printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", 
+    printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s",
              internal->nick);
     silc_free(internal->data);
     silc_free(internal->nick);
@@ -666,6 +772,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
   char *local_ip = NULL;
   SilcUInt32 local_port = 0;
   SilcUInt32 session_id;
+  bool do_not_bind = FALSE;
 
   CMD_SILC_SERVER(server);
   if (!server || !IS_SILC_SERVER(server) || !server->connected)
@@ -675,7 +782,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
 
   /* Now parse all arguments */
   tmp = g_strconcat("FILE", " ", data, NULL);
-  silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
+  silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 7);
   g_free(tmp);
 
   if (argc == 1)
@@ -684,12 +791,12 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
   if (argc >= 2) {
     if (!strcasecmp(argv[1], "send"))
       type = 1;
-    if (!strcasecmp(argv[1], "receive"))
+    if (!strcasecmp(argv[1], "accept"))
       type = 2;
     if (!strcasecmp(argv[1], "close"))
       type = 3;
   }
-  
+
   if (type == 0)
     cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
 
@@ -704,7 +811,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
                         MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[3]);
       goto out;
     }
-    
+
     /* Find client entry */
     entrys = silc_client_get_clients_local(silc_client, conn, nickname,
                                           argv[3], &entry_count);
@@ -721,14 +828,26 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
     client_entry = entrys[0];
     silc_free(entrys);
 
-    if (argc >= 5)
-      local_ip = argv[4];
-    if (argc >= 6)
-      local_port = atoi(argv[5]);
+    if (argc >= 5) {
+      if (!strcasecmp(argv[4], "-no-listener"))
+       do_not_bind = TRUE;
+      else
+       local_ip = argv[4];
+    }
+    if (argc >= 6) {
+      if (!strcasecmp(argv[5], "-no-listener"))
+       do_not_bind = TRUE;
+      else
+       local_port = atoi(argv[5]);
+    }
+    if (argc >= 7) {
+      if (!strcasecmp(argv[6], "-no-listener"))
+       do_not_bind = TRUE;
+    }
 
-    ret = 
-      silc_client_file_send(silc_client, conn, silc_client_file_monitor, 
-                           server, local_ip, local_port, 
+    ret =
+      silc_client_file_send(silc_client, conn, silc_client_file_monitor,
+                           server, local_ip, local_port, do_not_bind,
                            client_entry, argv[2], &session_id);
     if (ret == SILC_CLIENT_FILE_OK) {
       ftp = silc_calloc(1, sizeof(*ftp));
@@ -751,7 +870,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
                           client_entry->nickname);
       if (ret == SILC_CLIENT_FILE_NO_SUCH_FILE)
        printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
-                          SILCTXT_FILE_ERROR_NO_SUCH_FILE, 
+                          SILCTXT_FILE_ERROR_NO_SUCH_FILE,
                           client_entry->nickname, argv[2]);
     }
 
@@ -765,7 +884,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
                           MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
        goto out;
       }
-    
+
       /* Find client entry */
       entrys = silc_client_get_clients_local(silc_client, conn, nickname,
                                             argv[2], &entry_count);
@@ -788,18 +907,30 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
        goto out;
       }
 
-      ret = silc_client_file_receive(silc_client, conn, 
+      ret = silc_client_file_receive(silc_client, conn,
                                     silc_client_file_monitor, server, NULL,
-                                    server->current_session->session_id);
+                                    server->current_session->session_id,
+                                    NULL, NULL);
       if (ret != SILC_CLIENT_FILE_OK) {
        if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
          printformat_module("fe-common/silc", server, NULL,
                             MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
                             server->current_session->client_entry->nickname);
-       else
+       else {
          printformat_module("fe-common/silc", server, NULL,
                             MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
                             server->current_session->client_entry->nickname);
+
+         silc_client_file_close(silc_client, conn,
+                                server->current_session->session_id);
+         silc_dlist_del(server->ftp_sessions, server->current_session);
+         silc_free(server->current_session->filepath);
+         silc_free(server->current_session);
+         server->current_session = NULL;
+
+         silc_dlist_start(server->ftp_sessions);
+         server->current_session = silc_dlist_get(server->ftp_sessions);
+       }
       }
 
       goto out;
@@ -808,18 +939,28 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
     silc_dlist_start(server->ftp_sessions);
     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
       if (ftp->client_entry == client_entry && !ftp->filepath) {
-       ret = silc_client_file_receive(silc_client, conn, 
+       ret = silc_client_file_receive(silc_client, conn,
                                       silc_client_file_monitor, server,
-                                      NULL, ftp->session_id);
+                                      NULL, ftp->session_id, NULL, NULL);
        if (ret != SILC_CLIENT_FILE_OK) {
          if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
            printformat_module("fe-common/silc", server, NULL,
                               MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
                               client_entry->nickname);
-         else
+         else {
            printformat_module("fe-common/silc", server, NULL,
                               MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
                               client_entry->nickname);
+           silc_client_file_close(silc_client, conn, ftp->session_id);
+           silc_dlist_del(server->ftp_sessions, ftp);
+           if (ftp == server->current_session) {
+             server->current_session = NULL;
+             silc_dlist_start(server->ftp_sessions);
+             server->current_session = silc_dlist_get(server->ftp_sessions);
+           }
+           silc_free(ftp->filepath);
+           silc_free(ftp);
+         }
        }
        break;
       }
@@ -841,7 +982,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
                           MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
        goto out;
       }
-    
+
       /* Find client entry */
       entrys = silc_client_get_clients_local(silc_client, conn, nickname,
                                             argv[2], &entry_count);
@@ -863,8 +1004,8 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
                           MSGLEVEL_CRAP, SILCTXT_FILE_NA);
        goto out;
       }
-      silc_client_file_close(silc_client, conn, 
+
+      silc_client_file_close(silc_client, conn,
                             server->current_session->session_id);
       printformat_module("fe-common/silc", server, NULL,
                         MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
@@ -875,6 +1016,9 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
       silc_free(server->current_session->filepath);
       silc_free(server->current_session);
       server->current_session = NULL;
+
+      silc_dlist_start(server->ftp_sessions);
+      server->current_session = silc_dlist_get(server->ftp_sessions);
       goto out;
     }
 
@@ -886,9 +1030,12 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
                           MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
                           client_entry->nickname,
                           ftp->filepath ? ftp->filepath : "[N/A]");
-       if (ftp == server->current_session)
-         server->current_session = NULL;
        silc_dlist_del(server->ftp_sessions, ftp);
+       if (ftp == server->current_session) {
+         server->current_session = NULL;
+         silc_dlist_start(server->ftp_sessions);
+         server->current_session = silc_dlist_get(server->ftp_sessions);
+       }
        silc_free(ftp->filepath);
        silc_free(ftp);
        break;
@@ -918,7 +1065,8 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
     while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
       printformat_module("fe-common/silc", server, NULL,
                         MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_LINE,
-                        ftp->client_entry->nickname, 
+                        ftp->client_entry->nickname,
+                        ftp->session_id,
                         ftp->send ? "send" : "receive",
                         (SilcUInt32)(ftp->offset + 1023) / 1024,
                         (SilcUInt32)(ftp->filesize + 1023) / 1024,
@@ -970,6 +1118,7 @@ void silc_server_init(void)
   command_bind_silc("watch", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind_silc("stats", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind_silc("attr", MODULE_NAME, (SIGNAL_FUNC) command_attr);
+  command_bind_silc("smsg", MODULE_NAME, (SIGNAL_FUNC) command_smsg);
 
   command_set_options("connect", "+silcnet");
 }
@@ -1008,6 +1157,7 @@ void silc_server_deinit(void)
   command_unbind("watch", (SIGNAL_FUNC) command_self);
   command_unbind("stats", (SIGNAL_FUNC) command_self);
   command_unbind("attr", (SIGNAL_FUNC) command_attr);
+  command_unbind("smsg", (SIGNAL_FUNC) command_smsg);
 }
 
 void silc_server_free_ftp(SILC_SERVER_REC *server,