Created SILC Client Libary by moving stuff from silc/ directory.
authorPekka Riikonen <priikone@silcnet.org>
Wed, 13 Sep 2000 17:47:53 +0000 (17:47 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Wed, 13 Sep 2000 17:47:53 +0000 (17:47 +0000)
SILC client library is SILC client without UI. Old UI still exists
in silc/ directory and uses the new client.

Bug fixes and several new functions, structures and functions
naming changes during the change was made.

24 files changed:
apps/silc/Makefile.am
apps/silc/client_ops.c [new file with mode: 0644]
apps/silc/client_ops.h [new file with mode: 0644]
apps/silc/clientconfig.c
apps/silc/clientutil.c
apps/silc/clientutil.h
apps/silc/local_command.c [new file with mode: 0644]
apps/silc/local_command.h [new file with mode: 0644]
apps/silc/screen.h
apps/silc/silc.c
apps/silc/silc.h
lib/silcclient/Makefile.am [new file with mode: 0644]
lib/silcclient/README [new file with mode: 0644]
lib/silcclient/client.c [moved from apps/silc/client.c with 58% similarity]
lib/silcclient/client.h [moved from apps/silc/client.h with 74% similarity]
lib/silcclient/command.c [moved from apps/silc/command.c with 51% similarity]
lib/silcclient/command.h [moved from apps/silc/command.h with 95% similarity]
lib/silcclient/command_reply.c [moved from apps/silc/command_reply.c with 73% similarity]
lib/silcclient/command_reply.h [moved from apps/silc/command_reply.h with 97% similarity]
lib/silcclient/idlist.c [moved from apps/silc/idlist.c with 82% similarity]
lib/silcclient/idlist.h [moved from apps/silc/idlist.h with 96% similarity]
lib/silcclient/ops.h [new file with mode: 0644]
lib/silcclient/protocol.c [moved from apps/silc/protocol.c with 84% similarity]
lib/silcclient/protocol.h [moved from apps/silc/protocol.h with 88% similarity]

index 264c67f1fe3b9658114c4d698e9cc9586db0a288..91cab5844579e0120f1abb837e6b664553508304 100644 (file)
@@ -22,20 +22,19 @@ bin_PROGRAMS = silc
 
 silc_SOURCES = \
        silc.c \
-       client.c \
-       command.c \
-       command_reply.c \
        clientconfig.c \
        clientutil.c \
-       idlist.c \
-       protocol.c \
-       screen.c
+       local_command.c \
+       screen.c \
+       client_ops.c
 
-LDADD = -L. -L.. -L../lib -lsilc -lcurses
+silc_DEPENDENCIES = ../lib/libsilcclient.a ../lib/libsilc.a
+
+LDADD = -L. -L.. -L../lib -lsilcclient -lsilc -lcurses
 
 EXTRA_DIST = *.h
 
 INCLUDES = -I. -I.. -I../lib/silccore -I../lib/silccrypt \
        -I../lib/silcmath -I../lib/silcske -I../lib/silcsim \
-       -I../includes \
+       -I../includes -I../lib/silcclient -I../lib/silcutil \
        -I../lib/silcmath/gmp
diff --git a/apps/silc/client_ops.c b/apps/silc/client_ops.c
new file mode 100644 (file)
index 0000000..3a08a79
--- /dev/null
@@ -0,0 +1,411 @@
+/*
+
+  client_ops.c
+
+  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+
+  Copyright (C) 2000 Pekka Riikonen
+
+  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
+  GNU General Public License for more details.
+
+*/
+
+#include "clientincludes.h"
+
+/* Prints a message with three star (*) sign before the actual message
+   on the current output window. This is used to print command outputs
+   and error messages. */
+
+void silc_say(SilcClient client, SilcClientConnection conn, 
+             char *msg, ...)
+{
+  va_list vp;
+  char message[2048];
+  SilcClientInternal app = (SilcClientInternal)client->application;
+
+  memset(message, 0, sizeof(message));
+  strncat(message, "\n***  ", 5);
+
+  va_start(vp, msg);
+  vsprintf(message + 5, msg, vp);
+  va_end(vp);
+  
+  /* Print the message */
+  silc_print_to_window(app->screen->output_win[0], message);
+}
+
+/* Message for a channel. The `sender' is the nickname of the sender 
+   received in the packet. The `channel_name' is the name of the channel. */
+
+void silc_channel_message(SilcClient client, SilcClientConnection conn,
+                         char *sender, char *channel_name, char *msg)
+{
+  /* Message from client */
+  if (!strcmp(conn->current_channel->channel_name, channel_name))
+    silc_print(client, "<%s> %s", sender, msg);
+  else
+    silc_print(client, "<%s:%s> %s", sender, channel_name, msg);
+}
+
+/* Private message to the client. The `sender' is the nickname of the
+   sender received in the packet. */
+
+void silc_private_message(SilcClient client, SilcClientConnection conn,
+                         char *sender, char *msg)
+{
+  silc_print(client, "*%s* %s", sender, msg);
+}
+
+/* Command handler. This function is called always in the command function.
+   If error occurs it will be called as well. `conn' is the associated
+   client connection. `cmd_context' is the command context that was
+   originally sent to the command. `success' is FALSE if error occured
+   during command. `command' is the command being processed. It must be
+   noted that this is not reply from server. This is merely called just
+   after application has called the command. Just to tell application
+   that the command really was processed. */
+
+void silc_command(SilcClient client, SilcClientConnection conn, 
+                 SilcClientCommandContext cmd_context, int success,
+                 SilcCommand command)
+{
+  SilcClientInternal app = (SilcClientInternal)client->application;
+
+  if (!success)
+    return;
+
+  switch(command)
+    {
+    case SILC_COMMAND_QUIT:
+      app->screen->bottom_line->channel = NULL;
+      silc_screen_print_bottom_line(app->screen, 0);
+      break;
+
+    case SILC_COMMAND_LEAVE:
+#if 0
+      if (!strncmp(conn->current_channel->channel_name, name, strlen(name))) {
+       app->screen->bottom_line->channel = NULL;
+       silc_screen_print_bottom_line(app->screen, 0);
+      }
+#endif
+      break;
+
+    }
+}
+
+/* Command reply handler. This function is called always in the command reply
+   function. If error occurs it will be called as well. Normal scenario
+   is that it will be called after the received command data has been parsed
+   and processed. The function is used to pass the received command data to
+   the application. 
+
+   `conn' is the associated client connection. `cmd_payload' is the command
+   payload data received from server and it can be ignored. It is provided
+   if the application would like to re-parse the received command data,
+   however, it must be noted that the data is parsed already by the library
+   thus the payload can be ignored. `success' is FALSE if error occured.
+   In this case arguments are not sent to the application. `command' is the
+   command reply being processed. The function has variable argument list
+   and each command defines the number and type of arguments it passes to the
+   application (on error they are not sent). */
+
+void silc_command_reply(SilcClient client, SilcClientConnection conn,
+                       SilcCommandPayload cmd_payload, int success,
+                       SilcCommand command, ...)
+{
+  SilcClientInternal app = (SilcClientInternal)client->application;
+  va_list vp;
+
+  if (!success)
+    return;
+
+  va_start(vp, command);
+
+  switch(command)
+    {
+
+    case SILC_COMMAND_JOIN:
+      app->screen->bottom_line->channel = va_arg(vp, char *);
+      silc_screen_print_bottom_line(app->screen, 0);
+      break;
+
+    case SILC_COMMAND_NICK:
+      app->screen->bottom_line->nickname = va_arg(vp, char *);
+      silc_screen_print_bottom_line(app->screen, 0);
+      break;
+
+    }
+}
+
+/* Called to indicate that connection was either successfully established
+   or connecting failed.  This is also the first time application receives
+   the SilcClientConnection objecet which it should save somewhere. */
+
+void silc_connect(SilcClient client, SilcClientConnection conn, int success)
+{
+  SilcClientInternal app = (SilcClientInternal)client->application;
+
+  if (success) {
+    app->screen->bottom_line->connection = conn->remote_host;
+    silc_screen_print_bottom_line(app->screen, 0);
+    app->conn = conn;
+  }
+}
+
+/* Called to indicate that connection was disconnected to the server. */
+
+void silc_disconnect(SilcClient client, SilcClientConnection conn)
+{
+  SilcClientInternal app = (SilcClientInternal)client->application;
+
+  app->screen->bottom_line->connection = NULL;
+  silc_screen_print_bottom_line(app->screen, 0);
+}
+
+/* Asks passphrase from user on the input line. */
+
+unsigned char *silc_ask_passphrase(SilcClient client, 
+                                  SilcClientConnection conn)
+{
+  SilcClientInternal app = (SilcClientInternal)conn->client->application;
+  char pass1[256], pass2[256];
+  char *ret;
+  int try = 3;
+
+  while(try) {
+
+    /* Print prompt */
+    wattroff(app->screen->input_win, A_INVIS);
+    silc_screen_input_print_prompt(app->screen, "Passphrase: ");
+    wattron(app->screen->input_win, A_INVIS);
+    
+    /* Get string */
+    memset(pass1, 0, sizeof(pass1));
+    wgetnstr(app->screen->input_win, pass1, sizeof(pass1));
+    
+    /* Print retype prompt */
+    wattroff(app->screen->input_win, A_INVIS);
+    silc_screen_input_print_prompt(app->screen, "Retype passphrase: ");
+    wattron(app->screen->input_win, A_INVIS);
+    
+    /* Get string */
+    memset(pass2, 0, sizeof(pass2));
+    wgetnstr(app->screen->input_win, pass2, sizeof(pass2));
+
+    if (!strncmp(pass1, pass2, strlen(pass2)))
+      break;
+
+    try--;
+  }
+
+  ret = silc_calloc(strlen(pass1), sizeof(char));
+  memcpy(ret, pass1, strlen(pass1));
+
+  memset(pass1, 0, sizeof(pass1));
+  memset(pass2, 0, sizeof(pass2));
+
+  wattroff(app->screen->input_win, A_INVIS);
+  silc_screen_input_reset(app->screen);
+
+  return ret;
+}
+
+/* Verifies received public key. If user decides to trust the key it is
+   saved as trusted server key for later use. If user does not trust the
+   key this returns FALSE. */
+
+int silc_verify_server_key(SilcClient client,
+                          SilcClientConnection conn, 
+                          unsigned char *pk, unsigned int pk_len,
+                          SilcSKEPKType pk_type)
+{
+  SilcSocketConnection sock = conn->sock;
+  char filename[256];
+  char file[256];
+  char *hostname, *fingerprint;
+  struct passwd *pw;
+  struct stat st;
+
+  hostname = sock->hostname ? sock->hostname : sock->ip;
+
+  if (pk_type != SILC_SKE_PK_TYPE_SILC) {
+    silc_say(client, conn, "We don't support server %s key type", hostname);
+    return FALSE;
+  }
+
+  pw = getpwuid(getuid());
+  if (!pw)
+    return FALSE;
+
+  memset(filename, 0, sizeof(filename));
+  memset(file, 0, sizeof(file));
+  snprintf(file, sizeof(file) - 1, "serverkey_%s_%d.pub", hostname,
+          sock->port);
+  snprintf(filename, sizeof(filename) - 1, "%s/.silc/serverkeys/%s", 
+          pw->pw_dir, file);
+
+  /* Check wheter this key already exists */
+  if (stat(filename, &st) < 0) {
+
+    fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
+    silc_say(client, conn, "Received server %s public key", hostname);
+    silc_say(client, conn, "Fingerprint for the server %s key is", hostname);
+    silc_say(client, conn, "%s", fingerprint);
+    silc_free(fingerprint);
+
+    /* Ask user to verify the key and save it */
+    if (silc_client_ask_yes_no(client, 
+       "Would you like to accept the key (y/n)? "))
+      {
+       /* Save the key for future checking */
+       silc_pkcs_save_public_key_data(filename, pk, pk_len, 
+                                      SILC_PKCS_FILE_PEM);
+       return TRUE;
+      }
+  } else {
+    /* The key already exists, verify it. */
+    SilcPublicKey public_key;
+    unsigned char *encpk;
+    unsigned int encpk_len;
+
+    /* Load the key file */
+    if (!silc_pkcs_load_public_key(filename, &public_key, 
+                                  SILC_PKCS_FILE_PEM))
+      if (!silc_pkcs_load_public_key(filename, &public_key, 
+                                    SILC_PKCS_FILE_BIN)) {
+       fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
+       silc_say(client, conn, "Received server %s public key", hostname);
+       silc_say(client, conn, "Fingerprint for the server %s key is", hostname);
+       silc_say(client, conn, "%s", fingerprint);
+       silc_free(fingerprint);
+       silc_say(client, conn, "Could not load your local copy of the server %s key",
+                hostname);
+       if (silc_client_ask_yes_no(client, 
+          "Would you like to accept the key anyway (y/n)? "))
+         {
+           /* Save the key for future checking */
+           unlink(filename);
+           silc_pkcs_save_public_key_data(filename, pk, pk_len,
+                                          SILC_PKCS_FILE_PEM);
+           return TRUE;
+         }
+       
+       return FALSE;
+      }
+  
+    /* Encode the key data */
+    encpk = silc_pkcs_public_key_encode(public_key, &encpk_len);
+    if (!encpk) {
+      fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
+      silc_say(client, conn, "Received server %s public key", hostname);
+      silc_say(client, conn, "Fingerprint for the server %s key is", hostname);
+      silc_say(client, conn, "%s", fingerprint);
+      silc_free(fingerprint);
+      silc_say(client, conn, "Your local copy of the server %s key is malformed",
+              hostname);
+      if (silc_client_ask_yes_no(client, 
+         "Would you like to accept the key anyway (y/n)? "))
+       {
+         /* Save the key for future checking */
+         unlink(filename);
+         silc_pkcs_save_public_key_data(filename, pk, pk_len,
+                                        SILC_PKCS_FILE_PEM);
+         return TRUE;
+       }
+
+      return FALSE;
+    }
+
+    if (memcmp(encpk, pk, encpk_len)) {
+      fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
+      silc_say(client, conn, "Received server %s public key", hostname);
+      silc_say(client, conn, "Fingerprint for the server %s key is", hostname);
+      silc_say(client, conn, "%s", fingerprint);
+      silc_free(fingerprint);
+      silc_say(client, conn, "Server %s key does not match with your local copy",
+              hostname);
+      silc_say(client, conn, "It is possible that the key has expired or changed");
+      silc_say(client, conn, "It is also possible that some one is performing "
+                      "man-in-the-middle attack");
+      
+      /* Ask user to verify the key and save it */
+      if (silc_client_ask_yes_no(client, 
+         "Would you like to accept the key anyway (y/n)? "))
+       {
+         /* Save the key for future checking */
+         unlink(filename);
+         silc_pkcs_save_public_key_data(filename, pk, pk_len,
+                                        SILC_PKCS_FILE_PEM);
+         return TRUE;
+       }
+
+      silc_say(client, conn, "Will not accept server %s key", hostname);
+      return FALSE;
+    }
+
+    /* Local copy matched */
+    return TRUE;
+  }
+
+  silc_say(client, conn, "Will not accept server %s key", hostname);
+  return FALSE;
+}
+
+/* Find authentication method and authentication data by hostname and
+   port. The hostname may be IP address as well. The found authentication
+   method and authentication data is returned to `auth_meth', `auth_data'
+   and `auth_data_len'. The function returns TRUE if authentication method
+   is found and FALSE if not. `conn' may be NULL. */
+
+int silc_get_auth_method(SilcClient client, SilcClientConnection conn,
+                        char *hostname, unsigned short port,
+                        SilcProtocolAuthMeth *auth_meth,
+                        unsigned char **auth_data,
+                        unsigned int *auth_data_len)
+{
+  SilcClientInternal app = (SilcClientInternal)client->application;
+
+  if (app->config->conns) {
+    SilcClientConfigSectionConnection *conn = NULL;
+
+    /* Check if we find a match from user configured connections */
+    conn = silc_client_config_find_connection(app->config,
+                                             hostname,
+                                             port);
+    if (conn) {
+      /* Match found. Use the configured authentication method */
+      *auth_meth = conn->auth_meth;
+
+      if (conn->auth_data) {
+       *auth_data = strdup(conn->auth_data);
+       *auth_data_len = strlen(conn->auth_data);
+      }
+
+      return TRUE;
+    }
+  }
+
+  return FALSE;
+}
+
+/* SILC client operations */
+SilcClientOperations ops = {
+  say:                  silc_say,
+  channel_message:      silc_channel_message,
+  private_message:      silc_private_message,
+  command:              silc_command,
+  command_reply:        silc_command_reply,
+  connect:              silc_connect,
+  disconnect:           silc_disconnect,
+  get_auth_method:      silc_get_auth_method,
+  verify_server_key:    silc_verify_server_key,
+  ask_passphrase:       silc_ask_passphrase,
+};
diff --git a/apps/silc/client_ops.h b/apps/silc/client_ops.h
new file mode 100644 (file)
index 0000000..70b6f0f
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+
+  client_ops.h
+
+  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+
+  Copyright (C) 2000 Pekka Riikonen
+
+  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
+  GNU General Public License for more details.
+
+*/
+
+#ifndef CLIENT_OPS_H
+#define CLIENT_OPS_H
+
+void silc_say(SilcClient client, SilcClientConnection conn, char *msg, ...);
+void silc_channel_message(SilcClient client, SilcClientConnection conn,
+                         char *sender, char *channel_name, char *msg);
+void silc_private_message(SilcClient client, SilcClientConnection conn,
+                         char *sender, char *msg);
+void silc_command(SilcClient client, SilcClientConnection conn, 
+                 SilcClientCommandContext cmd_context, int success,
+                 SilcCommand command);
+void silc_command_reply(SilcClient client, SilcClientConnection conn,
+                       SilcCommandPayload cmd_payload, int success,
+                       SilcCommand command, ...);
+void silc_connect(SilcClient client, SilcClientConnection conn, int success);
+void silc_disconnect(SilcClient client, SilcClientConnection conn);
+unsigned char *silc_ask_passphrase(SilcClient client, 
+                                  SilcClientConnection conn);
+int silc_verify_server_key(SilcClient client, SilcClientConnection conn, 
+                          unsigned char *pk, unsigned int pk_len,
+                          SilcSKEPKType pk_type);
+int silc_get_auth_method(SilcClient client, SilcClientConnection conn,
+                        char *hostname, unsigned short port,
+                        SilcProtocolAuthMeth *auth_meth,
+                        unsigned char **auth_data,
+                        unsigned int *auth_data_len);
+
+#endif
index af12288ffb6d0b03629800fdf0d3bf9aeeaaa9cb..b6f5b150a832b58c814b063752ea0ff355e1660a 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.3  2000/07/05 06:12:05  priikone
- *     Global cosmetic changes.
- *
- * Revision 1.2  2000/07/03 05:49:11  priikone
- *     Remove ':' from the end of line when in commands section.  Command
- *     execution should work now from config file ok.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "clientincludes.h"
 #include "clientconfig.h"
@@ -543,7 +529,8 @@ int silc_client_config_parse_lines(SilcClientConfig config,
 void silc_client_config_register_ciphers(SilcClientConfig config)
 {
   SilcClientConfigSectionAlg *alg;
-  SilcClient client = (SilcClient)config->client;
+  SilcClientInternal app = (SilcClientInternal)config->client;
+  SilcClient client = app->client;
 
   SILC_LOG_DEBUG(("Registering configured ciphers"));
 
@@ -600,11 +587,11 @@ void silc_client_config_register_ciphers(SilcClientConfig config)
        SILC_LOG_DEBUG(("context_len=%p", cipher.context_len));
 
        /* Put the SIM to the table of all SIM's in client */
-       client->sim = silc_realloc(client->sim,
-                                  sizeof(*client->sim) * 
-                                  (client->sim_count + 1));
-       client->sim[client->sim_count] = sim;
-       client->sim_count++;
+       app->sim = silc_realloc(app->sim,
+                                  sizeof(*app->sim) * 
+                                  (app->sim_count + 1));
+       app->sim[app->sim_count] = sim;
+       app->sim_count++;
       } else {
        SILC_LOG_ERROR(("Error configuring ciphers"));
        silc_client_stop(client);
@@ -629,7 +616,8 @@ void silc_client_config_register_ciphers(SilcClientConfig config)
 void silc_client_config_register_pkcs(SilcClientConfig config)
 {
   SilcClientConfigSectionAlg *alg = config->pkcs;
-  SilcClient client = (SilcClient)config->client;
+  SilcClientInternal app = (SilcClientInternal)config->client;
+  SilcClient client = app->client;
   SilcPKCS tmp = NULL;
 
   SILC_LOG_DEBUG(("Registering configured PKCS"));
@@ -653,7 +641,8 @@ void silc_client_config_register_pkcs(SilcClientConfig config)
 void silc_client_config_register_hashfuncs(SilcClientConfig config)
 {
   SilcClientConfigSectionAlg *alg;
-  SilcClient client = (SilcClient)config->client;
+  SilcClientInternal app = (SilcClientInternal)config->client;
+  SilcClient client = app->client;
 
   SILC_LOG_DEBUG(("Registering configured hash functions"));
 
@@ -706,11 +695,11 @@ void silc_client_config_register_hashfuncs(SilcClientConfig config)
        SILC_LOG_DEBUG(("context_len=%p", hash.context_len));
 
        /* Put the SIM to the table of all SIM's in client */
-       client->sim = silc_realloc(client->sim,
-                                  sizeof(*client->sim) * 
-                                  (client->sim_count + 1));
-       client->sim[client->sim_count] = sim;
-       client->sim_count++;
+       app->sim = silc_realloc(app->sim,
+                                  sizeof(*app->sim) * 
+                                  (app->sim_count + 1));
+       app->sim[app->sim_count] = sim;
+       app->sim_count++;
       } else {
        SILC_LOG_ERROR(("Error configuring hash functions"));
        silc_client_stop(client);
index a68a37e37a5157c7c744aa19f7608a5049f9d38f..210f50b02c5d2307f6a8bb4bf12a211bb97e6c38 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.8  2000/07/20 05:49:32  priikone
- *     Added default mail directory path.
- *
- * Revision 1.7  2000/07/19 07:07:16  priikone
- *     Search mail by From:
- *
- * Revision 1.6  2000/07/14 06:11:32  priikone
- *     Fixed key-pair generation.
- *
- * Revision 1.5  2000/07/12 05:55:50  priikone
- *     Added client_parse_nickname.
- *
- * Revision 1.4  2000/07/10 05:40:05  priikone
- *     Added support for verifying incoming public keys from user.
- *     Shows fingerprint of the public key now plus other changes.
- *
- * Revision 1.3  2000/07/07 06:53:45  priikone
- *     Added support for server public key verification.
- *
- * Revision 1.2  2000/07/05 06:11:00  priikone
- *     Added ~./silc directory checking, autoloading of keys and
- *     tweaked the key pair generation function.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "clientincludes.h"
 
-/* Internal routine used to print lines to window. This can split the
+/* Routine used to print lines to window. This can split the
    line neatly if a word would overlap the line. */
 
-static void silc_print_to_window(WINDOW *win, char *message)
+void silc_print_to_window(WINDOW *win, char *message)
 {
   int str_len, len;
 
@@ -85,29 +55,6 @@ static void silc_print_to_window(WINDOW *win, char *message)
   wrefresh(win);
 }
 
-/* Prints a message with three star (*) sign before the actual message
-   on the current output window. This is used to print command outputs
-   and error messages. */
-/* XXX Change to accept SilcClientWindow and use output window 
-   from there (the pointer to the output window must be added to the
-   SilcClientWindow object. */
-
-void silc_say(SilcClient client, char *msg, ...)
-{
-  va_list vp;
-  char message[2048];
-  
-  memset(message, 0, sizeof(message));
-  strncat(message, "\n***  ", 5);
-
-  va_start(vp, msg);
-  vsprintf(message + 5, msg, vp);
-  va_end(vp);
-  
-  /* Print the message */
-  silc_print_to_window(client->screen->output_win[0], message);
-}
-
 /* Prints message to the screen. This is used to print the messages
    user is typed and message that came on channels. */
 
@@ -115,6 +62,7 @@ void silc_print(SilcClient client, char *msg, ...)
 {
   va_list vp;
   char message[2048];
+  SilcClientInternal app = client->application;
   
   memset(message, 0, sizeof(message));
   strncat(message, "\n ", 2);
@@ -124,7 +72,7 @@ void silc_print(SilcClient client, char *msg, ...)
   va_end(vp);
   
   /* Print the message */
-  silc_print_to_window(client->screen->output_win[0], message);
+  silc_print_to_window(app->screen->output_win[0], message);
 }
 
 /* Returns user's mail path */
@@ -237,71 +185,26 @@ int silc_client_time_til_next_min()
   return 60 - min->tm_sec;
 }
 
-/* Asks passphrase from user on the input line. */
-
-char *silc_client_ask_passphrase(SilcClient client)
-{
-  char pass1[256], pass2[256];
-  char *ret;
-  int try = 3;
-
-  while(try) {
-
-    /* Print prompt */
-    wattroff(client->screen->input_win, A_INVIS);
-    silc_screen_input_print_prompt(client->screen, "Passphrase: ");
-    wattron(client->screen->input_win, A_INVIS);
-    
-    /* Get string */
-    memset(pass1, 0, sizeof(pass1));
-    wgetnstr(client->screen->input_win, pass1, sizeof(pass1));
-    
-    /* Print retype prompt */
-    wattroff(client->screen->input_win, A_INVIS);
-    silc_screen_input_print_prompt(client->screen, "Retype passphrase: ");
-    wattron(client->screen->input_win, A_INVIS);
-    
-    /* Get string */
-    memset(pass2, 0, sizeof(pass2));
-    wgetnstr(client->screen->input_win, pass2, sizeof(pass2));
-
-    if (!strncmp(pass1, pass2, strlen(pass2)))
-      break;
-
-    try--;
-  }
-
-  ret = silc_calloc(strlen(pass1), sizeof(char));
-  memcpy(ret, pass1, strlen(pass1));
-
-  memset(pass1, 0, sizeof(pass1));
-  memset(pass2, 0, sizeof(pass2));
-
-  wattroff(client->screen->input_win, A_INVIS);
-  silc_screen_input_reset(client->screen);
-
-  return ret;
-}
-
 /* Asks yes/no from user on the input line. Returns TRUE on "yes" and
    FALSE on "no". */
 
 int silc_client_ask_yes_no(SilcClient client, char *prompt)
 {
+  SilcClientInternal app = (SilcClientInternal)client->application;
   char answer[4];
   int ret;
 
  again:
-  silc_screen_input_reset(client->screen);
+  silc_screen_input_reset(app->screen);
 
   /* Print prompt */
-  wattroff(client->screen->input_win, A_INVIS);
-  silc_screen_input_print_prompt(client->screen, prompt);
+  wattroff(app->screen->input_win, A_INVIS);
+  silc_screen_input_print_prompt(app->screen, prompt);
 
   /* Get string */
   memset(answer, 0, sizeof(answer));
   echo();
-  wgetnstr(client->screen->input_win, answer, sizeof(answer));
+  wgetnstr(app->screen->input_win, answer, sizeof(answer));
   if (!strncasecmp(answer, "yes", strlen(answer)) ||
       !strncasecmp(answer, "y", strlen(answer))) {
     ret = TRUE;
@@ -309,12 +212,12 @@ int silc_client_ask_yes_no(SilcClient client, char *prompt)
             !strncasecmp(answer, "n", strlen(answer))) {
     ret = FALSE;
   } else {
-    silc_say(client, "Type yes or no");
+    silc_say(client, app->conn, "Type yes or no");
     goto again;
   }
   noecho();
 
-  silc_screen_input_reset(client->screen);
+  silc_screen_input_reset(app->screen);
 
   return ret;
 }
@@ -826,192 +729,3 @@ int silc_client_load_keys(SilcClient client)
 
   return TRUE;
 }
-
-/* Verifies received public key. If user decides to trust the key it is
-   saved as trusted server key for later use. If user does not trust the
-   key this returns FALSE. */
-
-int silc_client_verify_server_key(SilcClient client, 
-                                 SilcSocketConnection sock,
-                                 unsigned char *pk, unsigned int pk_len,
-                                 SilcSKEPKType pk_type)
-{
-  char filename[256];
-  char file[256];
-  char *hostname, *fingerprint;
-  struct passwd *pw;
-  struct stat st;
-
-  hostname = sock->hostname ? sock->hostname : sock->ip;
-
-  if (pk_type != SILC_SKE_PK_TYPE_SILC) {
-    silc_say(client, "We don't support server %s key type", hostname);
-    return FALSE;
-  }
-
-  pw = getpwuid(getuid());
-  if (!pw)
-    return FALSE;
-
-  memset(filename, 0, sizeof(filename));
-  memset(file, 0, sizeof(file));
-  snprintf(file, sizeof(file) - 1, "serverkey_%s_%d.pub", hostname,
-          sock->port);
-  snprintf(filename, sizeof(filename) - 1, "%s/.silc/serverkeys/%s", 
-          pw->pw_dir, file);
-
-  /* Check wheter this key already exists */
-  if (stat(filename, &st) < 0) {
-
-    fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
-    silc_say(client, "Received server %s public key", hostname);
-    silc_say(client, "Fingerprint for the server %s key is", hostname);
-    silc_say(client, "%s", fingerprint);
-    silc_free(fingerprint);
-
-    /* Ask user to verify the key and save it */
-    if (silc_client_ask_yes_no(client, 
-       "Would you like to accept the key (y/n)? "))
-      {
-       /* Save the key for future checking */
-       silc_pkcs_save_public_key_data(filename, pk, pk_len, 
-                                      SILC_PKCS_FILE_PEM);
-       return TRUE;
-      }
-  } else {
-    /* The key already exists, verify it. */
-    SilcPublicKey public_key;
-    unsigned char *encpk;
-    unsigned int encpk_len;
-
-    /* Load the key file */
-    if (!silc_pkcs_load_public_key(filename, &public_key, 
-                                  SILC_PKCS_FILE_PEM))
-      if (!silc_pkcs_load_public_key(filename, &public_key, 
-                                    SILC_PKCS_FILE_BIN)) {
-       fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
-       silc_say(client, "Received server %s public key", hostname);
-       silc_say(client, "Fingerprint for the server %s key is", hostname);
-       silc_say(client, "%s", fingerprint);
-       silc_free(fingerprint);
-       silc_say(client, "Could not load your local copy of the server %s key",
-                hostname);
-       if (silc_client_ask_yes_no(client, 
-          "Would you like to accept the key anyway (y/n)? "))
-         {
-           /* Save the key for future checking */
-           unlink(filename);
-           silc_pkcs_save_public_key_data(filename, pk, pk_len,
-                                          SILC_PKCS_FILE_PEM);
-           return TRUE;
-         }
-       
-       return FALSE;
-      }
-  
-    /* Encode the key data */
-    encpk = silc_pkcs_public_key_encode(public_key, &encpk_len);
-    if (!encpk) {
-      fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
-      silc_say(client, "Received server %s public key", hostname);
-      silc_say(client, "Fingerprint for the server %s key is", hostname);
-      silc_say(client, "%s", fingerprint);
-      silc_free(fingerprint);
-      silc_say(client, "Your local copy of the server %s key is malformed",
-              hostname);
-      if (silc_client_ask_yes_no(client, 
-         "Would you like to accept the key anyway (y/n)? "))
-       {
-         /* Save the key for future checking */
-         unlink(filename);
-         silc_pkcs_save_public_key_data(filename, pk, pk_len,
-                                        SILC_PKCS_FILE_PEM);
-         return TRUE;
-       }
-
-      return FALSE;
-    }
-
-    if (memcmp(encpk, pk, encpk_len)) {
-      fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
-      silc_say(client, "Received server %s public key", hostname);
-      silc_say(client, "Fingerprint for the server %s key is", hostname);
-      silc_say(client, "%s", fingerprint);
-      silc_free(fingerprint);
-      silc_say(client, "Server %s key does not match with your local copy",
-              hostname);
-      silc_say(client, "It is possible that the key has expired or changed");
-      silc_say(client, "It is also possible that some one is performing "
-                      "man-in-the-middle attack");
-      
-      /* Ask user to verify the key and save it */
-      if (silc_client_ask_yes_no(client, 
-         "Would you like to accept the key anyway (y/n)? "))
-       {
-         /* Save the key for future checking */
-         unlink(filename);
-         silc_pkcs_save_public_key_data(filename, pk, pk_len,
-                                        SILC_PKCS_FILE_PEM);
-         return TRUE;
-       }
-
-      silc_say(client, "Will not accept server %s key", hostname);
-      return FALSE;
-    }
-
-    /* Local copy matched */
-    return TRUE;
-  }
-
-  silc_say(client, "Will not accept server %s key", hostname);
-  return FALSE;
-}
-
-
-/* Parse nickname string. The format may be <num>!<nickname>@<server> to
-   support multiple same nicknames. The <num> is the final unifier if same
-   nickname is on same server. Note, this is only local format and server
-   does not know anything about these. */
-
-int silc_client_parse_nickname(char *string, char **nickname, char **server,
-                              unsigned int *num)
-{
-  unsigned int tlen;
-  char tmp[256];
-
-  if (!string)
-    return FALSE;
-
-  if (strchr(string, '!')) {
-    tlen = strcspn(string, "!");
-    memset(tmp, 0, sizeof(tmp));
-    memcpy(tmp, string, tlen);
-
-    if (num)
-      *num = atoi(tmp);
-
-    if (tlen >= strlen(string))
-      return FALSE;
-
-    string += tlen + 1;
-  }
-
-  if (strchr(string, '@')) {
-    tlen = strcspn(string, "@");
-    
-    if (nickname) {
-      *nickname = silc_calloc(tlen + 1, sizeof(char));
-      memcpy(*nickname, string, tlen);
-    }
-    
-    if (server) {
-      *server = silc_calloc(strlen(string) - tlen, sizeof(char));
-      memcpy(*server, string + tlen + 1, strlen(string) - tlen - 1);
-    }
-  } else {
-    if (nickname)
-      *nickname = strdup(string);
-  }
-
-  return TRUE;
-}
index 195a5eeb454f9ffce0cfd9cf9523c5ee9d30ee30..38f0d9e857b7cfc88e28c0f6cd3c85f093c242cf 100644 (file)
 #define CLIENTUTIL_H
 
 /* Prototypes */
-void silc_say(SilcClient client, char *msg, ...);
+void silc_print_to_window(WINDOW *win, char *message);
 void silc_print(SilcClient client, char *msg, ...);
 char *silc_get_mail_path();
 int silc_get_number_of_emails();
 char *silc_get_username();
 char *silc_get_real_name();
 int silc_client_time_til_next_min();
-char *silc_client_ask_passphrase(SilcClient client);
 int silc_client_ask_yes_no(SilcClient client, char *prompt);
 char *silc_client_get_input(const char *prompt);
 char *silc_client_get_passphrase(const char *prompt);
@@ -44,11 +43,5 @@ int silc_client_create_key_pair(char *pkcs_name, int bits,
                                SilcPrivateKey *ret_prv_key);
 int silc_client_check_silc_dir();
 int silc_client_load_keys(SilcClient client);
-int silc_client_verify_server_key(SilcClient client, 
-                                 SilcSocketConnection sock,
-                                 unsigned char *pk, unsigned int pk_len,
-                                 SilcSKEPKType pk_type);
-int silc_client_parse_nickname(char *string, char **nickname, char **server,
-                              unsigned int *num);
 
 #endif
diff --git a/apps/silc/local_command.c b/apps/silc/local_command.c
new file mode 100644 (file)
index 0000000..3d969f7
--- /dev/null
@@ -0,0 +1,254 @@
+/*
+
+  local_command.c
+
+  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+
+  Copyright (C) 1997 - 2000 Pekka Riikonen
+
+  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
+  GNU General Public License for more details.
+
+*/
+/*
+ * $Id$
+ * $Log$
+ * Revision 1.1  2000/09/13 17:47:54  priikone
+ *     Created SILC Client Libary by moving stuff from silc/ directory.
+ *     SILC client library is SILC client without UI. Old UI still exists
+ *     in silc/ directory and uses the new client.
+ *
+ *     Bug fixes and several new functions, structures and functions
+ *     naming changes during the change was made.
+ *
+ */
+
+#include "clientincludes.h"
+
+/* Local commands. */
+SilcClientCommand silc_local_command_list[] =
+{
+  SILC_CLIENT_LCMD(help, HELP, "HELP", 0, 2),
+  SILC_CLIENT_LCMD(clear, CLEAR, "CLEAR", 0, 1),
+  SILC_CLIENT_LCMD(version, VERSION, "VERSION", 0, 1),
+  SILC_CLIENT_LCMD(server, SERVER, "SERVER", 0, 2),
+  SILC_CLIENT_LCMD(msg, MSG, "MSG", 0, 3),
+  SILC_CLIENT_LCMD(away, AWAY, "AWAY", 0, 2),
+
+  { NULL, 0, NULL, 0, 0 },
+};
+
+/* Finds and returns a pointer to the command list. Return NULL if the
+   command is not found. */
+
+SilcClientCommand *silc_client_local_command_find(const char *name)
+{
+  SilcClientCommand *cmd;
+
+  for (cmd = silc_local_command_list; cmd->name; cmd++) {
+    if (!strcmp(cmd->name, name))
+      return cmd;
+  }
+
+  return NULL;
+}
+
+/* HELP command. This is local command and shows help on SILC */
+
+SILC_CLIENT_LCMD_FUNC(help)
+{
+
+}
+
+/* CLEAR command. This is local command and clears current output window */
+
+SILC_CLIENT_LCMD_FUNC(clear)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClient client = cmd->client;
+  SilcClientInternal app = (SilcClientInternal)client->application;
+
+#if 0
+  wclear((WINDOW *)app->screen);
+  wrefresh((WINDOW *)app->screen);
+#endif
+
+  silc_client_command_free(cmd);
+}
+
+/* VERSION command. This is local command and shows version of the client */
+
+SILC_CLIENT_LCMD_FUNC(version)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClient client = cmd->client;
+  extern char *silc_version;
+  extern char *silc_name;
+  extern char *silc_fullname;
+
+  silc_say(client, cmd->conn,
+          "%s (%s) version %s", silc_name, silc_fullname,
+          silc_version);
+
+  silc_client_command_free(cmd);
+}
+
+/* Command MSG. Sends private message to user or list of users. Note that
+   private messages are not really commands, they are message packets,
+   however, on user interface it is convenient to show them as commands
+   as that is the common way of sending private messages (like in IRC). */
+/* XXX supports only one destination */
+
+SILC_CLIENT_LCMD_FUNC(msg)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientConnection conn = cmd->conn;
+  SilcClient client = cmd->client;
+  SilcClientEntry client_entry = NULL;
+  unsigned int num = 0;
+  char *nickname = NULL, *server = NULL;
+
+  if (!cmd->conn) {
+    silc_say(client, conn,
+            "You are not connected to a server, use /SERVER to connect");
+    goto out;
+  }
+
+  if (cmd->argc < 3) {
+    silc_say(client, conn, "Usage: /MSG <nickname> <message>");
+    goto out;
+  }
+
+  /* Parse the typed nickname. */
+  if (!silc_parse_nickname(cmd->argv[1], &nickname, &server, &num)) {
+    silc_say(client, conn, "Bad nickname");
+    goto out;
+  }
+
+  /* Find client entry */
+  client_entry = silc_idlist_get_client(client, conn, nickname, server, num);
+  if (!client_entry) {
+    /* Client entry not found, it was requested thus mark this to be
+       pending command. */
+    silc_client_command_pending(SILC_COMMAND_IDENTIFY, 
+                               silc_client_local_command_msg, context);
+    return;
+  }
+
+  /* Display the message for our eyes. */
+  silc_print(client, "-> *%s* %s", cmd->argv[1], cmd->argv[2]);
+
+  /* Send the private message */
+  silc_client_packet_send_private_message(client, conn->sock, client_entry,
+                                         cmd->argv[2], cmd->argv_lens[2],
+                                         TRUE);
+
+ out:
+  silc_client_command_free(cmd);
+}
+
+
+/* Command SERVER. Connects to remote SILC server. This is local command. */
+
+SILC_CLIENT_LCMD_FUNC(server)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClient client = cmd->client;
+  SilcClientConnection conn = cmd->conn;
+  int i = 0, len, port;
+  char *hostname;
+
+  if (cmd->argc < 2) {
+    /* Show current servers */
+
+    if (!cmd->conn) {
+      silc_say(client, conn, "You are not connected to any server");
+      silc_say(client, conn, "Usage: /SERVER [<server>[:<port>]]");
+      goto out;
+    }
+
+    silc_say(client, conn, "Current server: %s on %d %s", 
+            conn->remote_host, conn->remote_port,
+            conn->remote_info ? conn->remote_info : "");
+    
+    silc_say(client, conn, "Server list:");
+    for (i = 0; i < client->conns_count; i++) {
+      silc_say(client, conn, " [%d] %s on %d %s", i + 1,
+              client->conns[i]->remote_host, 
+              client->conns[i]->remote_port,
+              client->conns[i]->remote_info ? 
+              client->conns[i]->remote_info : "");
+    }
+
+    goto out;
+  }
+
+  /* See if port is included and then extract it */
+  if (strchr(cmd->argv[1], ':')) {
+    len = strcspn(cmd->argv[1], ":");
+    hostname = silc_calloc(len + 1, sizeof(char));
+    memcpy(hostname, cmd->argv[1], len);
+    port = atoi(cmd->argv[1] + 1 + len);
+  } else {
+    hostname = cmd->argv[1];
+    port = 706;
+  }
+
+  /* Connect asynchronously to not to block user interface */
+  silc_client_connect_to_server(cmd->client, port, hostname, NULL);
+
+ out:
+  silc_client_command_free(cmd);
+}
+
+/* Local command AWAY. Client replies with away message to whomever sends
+   private message to the client if the away message is set. If this is
+   given without arguments the away message is removed. */
+
+SILC_CLIENT_LCMD_FUNC(away)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientConnection conn = cmd->conn;
+  SilcClient client = cmd->client;
+  SilcClientInternal app = (SilcClientInternal)client->application;
+
+  if (!cmd->conn) {
+    silc_say(client, conn,
+            "You are not connected to a server, use /SERVER to connect");
+    goto out;
+  }
+
+  if (cmd->argc == 1) {
+    if (conn->away) {
+      silc_free(conn->away->away);
+      silc_free(conn->away);
+      conn->away = NULL;
+      app->screen->bottom_line->away = FALSE;
+
+      silc_say(client, conn, "Away message removed");
+      silc_screen_print_bottom_line(app->screen, 0);
+    }
+  } else {
+
+    if (conn->away)
+      silc_free(conn->away->away);
+    else
+      conn->away = silc_calloc(1, sizeof(*conn->away));
+    
+    app->screen->bottom_line->away = TRUE;
+    conn->away->away = strdup(cmd->argv[1]);
+
+    silc_say(client, conn, "Away message set: %s", conn->away->away);
+    silc_screen_print_bottom_line(app->screen, 0);
+  }
+
+ out:
+  silc_client_command_free(cmd);
+}
diff --git a/apps/silc/local_command.h b/apps/silc/local_command.h
new file mode 100644 (file)
index 0000000..5673057
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+
+  local_command.h
+
+  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+
+  Copyright (C) 1997 - 2000 Pekka Riikonen
+
+  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
+  GNU General Public License for more details.
+
+*/
+
+#ifndef LOCAL_COMMAND_H
+#define LOCAL_COMMAND_H
+
+/* All local commands */
+extern SilcClientCommand silc_local_command_list[];
+
+/* Local commands */
+#define SILC_LOCAL_COMMAND_HELP                1
+#define SILC_LOCAL_COMMAND_CLEAR       2
+#define SILC_LOCAL_COMMAND_VERSION     3
+#define SILC_LOCAL_COMMAND_SERVER       4
+#define SILC_LOCAL_COMMAND_MSG                 5
+#define SILC_LOCAL_COMMAND_AWAY                6
+
+/* Macros */
+
+/* Macro used for command declaration in command list structure */
+#define SILC_CLIENT_LCMD(func, cmd, name, flags, args) \
+{ silc_client_local_command_##func, SILC_LOCAL_COMMAND_##cmd, \
+  name, flags, args }
+
+/* Macro used to declare command functions */
+#define SILC_CLIENT_LCMD_FUNC(func) \
+void silc_client_local_command_##func(void *context)
+
+/* Prototypes */
+SilcClientCommand *silc_client_local_command_find(const char *name);
+SILC_CLIENT_LCMD_FUNC(help);
+SILC_CLIENT_LCMD_FUNC(clear);
+SILC_CLIENT_LCMD_FUNC(version);
+SILC_CLIENT_LCMD_FUNC(msg);
+SILC_CLIENT_LCMD_FUNC(server);
+SILC_CLIENT_LCMD_FUNC(away);
+
+#endif
index 6ea20b6482827d52547e5def22db5af8fe4d801d..a1fe8ce32522b326703790a9ef2b092412f8cb89 100644 (file)
@@ -54,8 +54,8 @@ typedef struct {
 
   /* XXX */
   struct upper_status_line {
-    char *program_name;
-    char *program_version;
+    const char *program_name;
+    const char *program_version;
   } u_stat_line;
 
 } SilcScreenObject;
index 68b4a90f952887e4a26ac325f1ca10d828bf2752..ce9f7ee028dd58d84490abbb52132efcb99cef88 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.4  2000/07/14 06:11:32  priikone
- *     Fixed key-pair generation.
- *
- * Revision 1.3  2000/07/05 06:11:00  priikone
- *     Added ~./silc directory checking, autoloading of keys and
- *     tweaked the key pair generation function.
- *
- * Revision 1.2  2000/06/30 10:49:48  priikone
- *     Added SOCKS4 and SOCKS5 support for SILC client.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "clientincludes.h"
 #include "version.h"
 
+/* Static function prototypes */
+static int silc_client_bad_keys(unsigned char key);
+static void silc_client_clear_input(SilcClientInternal app);
+static void silc_client_process_message(SilcClientInternal app);
+static char *silc_client_parse_command(unsigned char *buffer);
+
+void silc_client_create_main_window(SilcClientInternal app);
+
+/* Static task callback prototypes */
+SILC_TASK_CALLBACK(silc_client_update_clock);
+SILC_TASK_CALLBACK(silc_client_run_commands);
+SILC_TASK_CALLBACK(silc_client_process_key_press);
+
 /* Long command line options */
 static struct option long_opts[] = 
 {
@@ -52,6 +48,7 @@ static struct option long_opts[] =
   { "private-key", 1, NULL, 'k' },
   { "config-file", 1, NULL, 'f' },
   { "no-silcrc", 0, NULL, 'q' },
+  { "debug", 0, NULL, 'd' },
   { "help", 0, NULL, 'h' },
   { "version", 0, NULL, 'V' },
   { "list-ciphers", 0, NULL, 1 },
@@ -81,6 +78,9 @@ static int opt_create_keypair = FALSE;
 static char *opt_pkcs = NULL;
 static int opt_bits = 0;
 
+/* SILC Client operations */
+extern SilcClientOperations ops;
+
 /* Prints out the usage of silc client */
 
 void usage()
@@ -98,6 +98,7 @@ Usage: silc [options]\n\
   -k, --private-key=FILE       Private key used in SILC\n\
   -f, --config-file=FILE       Alternate configuration file\n\
   -q, --no-silcrc              Don't load ~/.silcrc on startup\n\
+  -d, --debug                  Enable debugging\n\
   -h, --help                   Display this help message\n\
   -V, --version                Display version\n\
       --list-ciphers           List supported ciphers\n\
@@ -116,13 +117,15 @@ int main(int argc, char **argv)
   int opt, option_index = 1;
   int ret;
   SilcClient silc = NULL;
-  SilcClientConfig config = NULL;
-  
+  SilcClientInternal app = NULL;
+
+  silc_debug = FALSE;
+
   if (argc > 1) 
     {
       while ((opt = 
              getopt_long(argc, argv,
-                         "s:p:n:c:b:k:f:qhVC",
+                         "s:p:n:c:b:k:f:qdhVC",
                          long_opts, &option_index)) != EOF)
        {
          switch(opt) 
@@ -165,6 +168,9 @@ int main(int argc, char **argv)
            case 'q':
              opt_no_silcrc = TRUE;
              break;
+           case 'd':
+             silc_debug = TRUE;
+             break;
            case 'h':
              usage();
              exit(0);
@@ -224,6 +230,11 @@ SILC Secure Internet Live Conferencing, version %s\n",
   signal(SIGFPE, SIG_DFL);
   //  signal(SIGINT, SIG_IGN);
   
+#ifdef SOCKS
+  /* Init SOCKS */
+  SOCKSinit(argv[0]);
+#endif
+
   if (opt_create_keypair == TRUE) {
     /* Create new key pair and exit */
     silc_client_create_key_pair(opt_pkcs, opt_bits, 
@@ -235,34 +246,81 @@ SILC Secure Internet Live Conferencing, version %s\n",
   if (!opt_config_file)
     opt_config_file = strdup(SILC_CLIENT_CONFIG_FILE);
 
+  /* Allocate internal application context */
+  app = silc_calloc(1, sizeof(*app));
+
+  /* Allocate new client */
+  app->client = silc = silc_client_alloc(&ops, app);
+  if (!silc)
+    goto fail;
+
   /* Read global configuration file. */
-  config = silc_client_config_alloc(opt_config_file);
-  if (config == NULL)
+  app->config = silc_client_config_alloc(opt_config_file);
+  if (app->config == NULL)
     goto fail;
 
-  /* Read local configuration file */
+  /* XXX Read local configuration file */
 
   /* Check ~/.silc directory and public and private keys */
   if (silc_client_check_silc_dir() == FALSE)
     goto fail;
 
-#ifdef SOCKS
-  /* Init SOCKS */
-  SOCKSinit(argv[0]);
-#endif
+  /* Get user information */
+  silc->username = silc_get_username();
+  silc->realname = silc_get_real_name();
 
-  /* Allocate new client */
-  ret = silc_client_alloc(&silc);
-  if (ret == FALSE)
+  /* Register all configured ciphers, PKCS and hash functions. */
+  app->config->client = (void *)app;
+  silc_client_config_register_ciphers(app->config);
+  silc_client_config_register_pkcs(app->config);
+  silc_client_config_register_hashfuncs(app->config);
+
+  /* Load public and private key */
+  if (silc_client_load_keys(silc) == FALSE)
     goto fail;
 
-  /* Initialize the client */
-  silc->config = config;
+  /* Initialize the client. This initializes the client library and
+     sets everything ready for silc_client_run. */
   ret = silc_client_init(silc);
   if (ret == FALSE)
     goto fail;
 
-  /* Run the client */
+  /* Register the main task that is used in client. This receives
+     the key pressings. */
+  silc_task_register(silc->io_queue, fileno(stdin), 
+                    silc_client_process_key_press,
+                    (void *)silc, 0, 0, 
+                    SILC_TASK_FD,
+                    SILC_TASK_PRI_NORMAL);
+
+  /* Register timeout task that updates clock every minute. */
+  silc_task_register(silc->timeout_queue, 0,
+                    silc_client_update_clock,
+                    (void *)silc, 
+                    silc_client_time_til_next_min(), 0,
+                    SILC_TASK_TIMEOUT,
+                    SILC_TASK_PRI_LOW);
+
+  if (app->config->commands) {
+    /* Run user configured commands with timeout */
+    silc_task_register(silc->timeout_queue, 0,
+                      silc_client_run_commands,
+                      (void *)silc, 0, 1,
+                      SILC_TASK_TIMEOUT,
+                      SILC_TASK_PRI_LOW);
+  }
+
+  /* Allocate the input buffer used to save typed characters */
+  app->input_buffer = silc_buffer_alloc(SILC_SCREEN_INPUT_WIN_SIZE);
+  silc_buffer_pull_tail(app->input_buffer, 
+                       SILC_BUFFER_END(app->input_buffer));
+
+  /* Initialize the screen */
+  silc_client_create_main_window(app);
+  silc_screen_print_coordinates(app->screen, 0);
+
+  /* Run the client. When this returns the application will be
+     terminated. */
   silc_client_run(silc);
 
   /* Stop the client. This probably has been done already but it
@@ -275,9 +333,324 @@ SILC Secure Internet Live Conferencing, version %s\n",
  fail:
   if (opt_config_file)
     silc_free(opt_config_file);
-  if (config)
-    silc_client_config_free(config);
+  if (app->config)
+    silc_client_config_free(app->config);
   if (silc)
     silc_client_free(silc);
   exit(1);
 }
+
+/* Creates the main window used in SILC client. This is called always
+   at the initialization of the client. If user wants to create more
+   than one windows a new windows are always created by calling 
+   silc_client_add_window. */
+
+void silc_client_create_main_window(SilcClientInternal app)
+{
+  void *screen;
+
+  SILC_LOG_DEBUG(("Creating main window"));
+
+  app->screen = silc_screen_init();
+  app->screen->input_buffer = app->input_buffer->data;
+  app->screen->u_stat_line.program_name = silc_name;
+  app->screen->u_stat_line.program_version = silc_version;
+
+  /* Create the actual screen */
+  screen = (void *)silc_screen_create_output_window(app->screen);
+  silc_screen_create_input_window(app->screen);
+  silc_screen_init_upper_status_line(app->screen);
+  silc_screen_init_output_status_line(app->screen);
+
+  app->screen->bottom_line->nickname = silc_get_username();
+  silc_screen_print_bottom_line(app->screen, 0);
+}
+
+/* The main task on SILC client. This processes the key pressings user
+   has made. */
+
+SILC_TASK_CALLBACK(silc_client_process_key_press)
+{
+  SilcClient client = (SilcClient)context;
+  SilcClientInternal app = (SilcClientInternal)client->application;
+  int c;
+
+  /* There is data pending in stdin, this gets it directly */
+  c = wgetch(app->screen->input_win);
+  if (silc_client_bad_keys(c))
+    return;
+
+  SILC_LOG_DEBUG(("Pressed key: %d", c));
+
+  switch(c) {
+    /* 
+     * Special character handling
+     */
+  case KEY_UP: 
+  case KEY_DOWN:
+    break;
+  case KEY_RIGHT:
+    /* Right arrow */
+    SILC_LOG_DEBUG(("RIGHT"));
+    silc_screen_input_cursor_right(app->screen);
+    break;
+  case KEY_LEFT:
+    /* Left arrow */
+    SILC_LOG_DEBUG(("LEFT"));
+    silc_screen_input_cursor_left(app->screen);
+    break;
+  case KEY_BACKSPACE:
+  case KEY_DC:
+  case '\177':
+  case '\b':
+    /* Backspace */
+    silc_screen_input_backspace(app->screen);
+    break;
+  case '\011':
+    /* Tabulator */
+    break;
+  case KEY_IC:
+    /* Insert switch. Turns on/off insert on input window */
+    silc_screen_input_insert(app->screen);
+    break;
+  case CTRL('j'):
+  case '\r':
+    /* Enter, Return. User pressed enter we are ready to
+       process the message. */
+    silc_client_process_message(app);
+    break;
+  case CTRL('l'):
+    /* Refresh screen, Ctrl^l */
+    silc_screen_refresh_all(app->screen);
+    break;
+  case CTRL('a'):
+  case KEY_HOME:
+  case KEY_BEG:
+    /* Beginning, Home */
+    silc_screen_input_cursor_home(app->screen);
+    break;
+  case CTRL('e'):
+  case KEY_END:
+    /* End */
+    silc_screen_input_cursor_end(app->screen);
+    break;
+  case KEY_LL:
+    /* End */
+    break;
+  case CTRL('g'):
+    /* Bell, Ctrl^g */
+    beep();
+    break;
+  case KEY_DL:
+  case CTRL('u'):
+    /* Delete line */
+    silc_client_clear_input(app);
+    break;
+  default:
+    /* 
+     * Other characters 
+     */
+    if (c < 32) {
+      /* Control codes are printed as reversed */
+      c = (c & 127) | 64;
+      wattron(app->screen->input_win, A_REVERSE);
+      silc_screen_input_print(app->screen, c);
+      wattroff(app->screen->input_win, A_REVERSE);
+    } else  {
+      /* Normal character */
+      silc_screen_input_print(app->screen, c);
+    }
+  }
+
+  silc_screen_print_coordinates(app->screen, 0);
+  silc_screen_refresh_win(app->screen->input_win);
+}
+
+static int silc_client_bad_keys(unsigned char key)
+{
+  /* these are explained in curses.h */
+  switch(key) {
+  case KEY_SF:
+  case KEY_SR:
+  case KEY_NPAGE:
+  case KEY_PPAGE:
+  case KEY_PRINT:
+  case KEY_A1:
+  case KEY_A3:
+  case KEY_B2:
+  case KEY_C1:
+  case KEY_C3:
+  case KEY_UNDO:
+  case KEY_EXIT:
+  case '\v':           /* VT */
+  case '\E':           /* we ignore ESC */
+    return TRUE;
+  default: 
+    return FALSE; 
+  }
+}
+
+/* Clears input buffer */
+
+static void silc_client_clear_input(SilcClientInternal app)
+{
+  silc_buffer_clear(app->input_buffer);
+  silc_buffer_pull_tail(app->input_buffer,
+                       SILC_BUFFER_END(app->input_buffer));
+  silc_screen_input_reset(app->screen);
+}
+
+/* Processes messages user has typed on the screen. This either sends
+   a packet out to network or if command were written executes it. */
+
+static void silc_client_process_message(SilcClientInternal app)
+{
+  unsigned char *data;
+  unsigned int len;
+
+  SILC_LOG_DEBUG(("Start"));
+
+  data = app->input_buffer->data;
+  len = strlen(data);
+
+  if (data[0] == '/' && data[1] != ' ') {
+    /* Command */
+    unsigned int argc = 0;
+    unsigned char **argv, *tmpcmd;
+    unsigned int *argv_lens, *argv_types;
+    SilcClientCommand *cmd;
+    SilcClientCommandContext ctx;
+
+    /* Get the command */
+    tmpcmd = silc_client_parse_command(data);
+    cmd = silc_client_local_command_find(tmpcmd);
+    if (!cmd && (cmd = silc_client_command_find(tmpcmd)) == NULL) {
+      silc_say(app->client, app->current_win, "Invalid command: %s", tmpcmd);
+      silc_free(tmpcmd);
+      goto out;
+    }
+
+    /* Now parse all arguments */
+    silc_parse_command_line(data + 1, &argv, &argv_lens, 
+                           &argv_types, &argc, cmd->max_args);
+    silc_free(tmpcmd);
+
+    SILC_LOG_DEBUG(("Executing command: %s", cmd->name));
+
+    /* Allocate command context. This and its internals must be free'd 
+       by the command routine receiving it. */
+    ctx = silc_calloc(1, sizeof(*ctx));
+    ctx->client = app->client;
+    ctx->conn = app->conn;
+    ctx->command = cmd;
+    ctx->argc = argc;
+    ctx->argv = argv;
+    ctx->argv_lens = argv_lens;
+    ctx->argv_types = argv_types;
+
+    /* Execute command */
+    (*cmd->cb)(ctx);
+
+  } else {
+    /* Normal message to a channel */
+    if (len && app->conn->current_channel &&
+       app->conn->current_channel->on_channel == TRUE) {
+      silc_print(app->client, "> %s", data);
+      silc_client_packet_send_to_channel(app->client, 
+                                        app->conn->sock,
+                                        app->conn->current_channel,
+                                        data, strlen(data), TRUE);
+    }
+  }
+
+ out:
+  /* Clear the input buffer */
+  silc_client_clear_input(app);
+}
+
+/* Returns the command fetched from user typed command line */
+
+static char *silc_client_parse_command(unsigned char *buffer)
+{
+  char *ret;
+  const char *cp = buffer;
+  int len;
+
+  len = strcspn(cp, " ");
+  ret = silc_to_upper((char *)++cp);
+  ret[len - 1] = 0;
+
+  return ret;
+}
+
+/* Updates clock on the screen every minute. */
+
+SILC_TASK_CALLBACK(silc_client_update_clock)
+{
+  SilcClient client = (SilcClient)context;
+  SilcClientInternal app = (SilcClientInternal)client->application;
+
+  /* Update the clock on the screen */
+  silc_screen_print_clock(app->screen);
+
+  /* Re-register this same task */
+  silc_task_register(qptr, 0, silc_client_update_clock, context, 
+                    silc_client_time_til_next_min(), 0,
+                    SILC_TASK_TIMEOUT,
+                    SILC_TASK_PRI_LOW);
+
+  silc_screen_refresh_win(app->screen->input_win);
+}
+
+/* Runs commands user configured in configuration file. This is
+   called when initializing client. */
+
+SILC_TASK_CALLBACK(silc_client_run_commands)
+{
+  SilcClient client = (SilcClient)context;
+  SilcClientInternal app = (SilcClientInternal)client->application;
+  SilcClientConfigSectionCommand *cs;
+
+  SILC_LOG_DEBUG(("Start"));
+
+  cs = app->config->commands;
+  while(cs) {
+    unsigned int argc = 0;
+    unsigned char **argv, *tmpcmd;
+    unsigned int *argv_lens, *argv_types;
+    SilcClientCommand *cmd;
+    SilcClientCommandContext ctx;
+
+    /* Get the command */
+    tmpcmd = silc_client_parse_command(cs->command);
+    cmd = silc_client_local_command_find(tmpcmd);
+    if (!cmd && (cmd = silc_client_command_find(tmpcmd)) == NULL) {
+      silc_say(client, app->conn, "Invalid command: %s", tmpcmd);
+      silc_free(tmpcmd);
+      continue;
+    }
+    
+    /* Now parse all arguments */
+    silc_parse_command_line(cs->command + 1, &argv, &argv_lens, 
+                           &argv_types, &argc, cmd->max_args);
+    silc_free(tmpcmd);
+
+    SILC_LOG_DEBUG(("Executing command: %s", cmd->name));
+
+    /* Allocate command context. This and its internals must be free'd 
+       by the command routine receiving it. */
+    ctx = silc_calloc(1, sizeof(*ctx));
+    ctx->client = client;
+    ctx->conn = app->conn;
+    ctx->command = cmd;
+    ctx->argc = argc;
+    ctx->argv = argv;
+    ctx->argv_lens = argv_lens;
+    ctx->argv_types = argv_types;
+
+    /* Execute command */
+    (*cmd->cb)(ctx);
+
+    cs = cs->next;
+  }
+}
index 91fe64b9064b8c16e61c260d6b368fb8cfc35763..828e2c03db10071020eeab1ddfa1dfe740756280 100644 (file)
 #define SILC_CLIENT_DEF_PKCS "rsa"
 #define SILC_CLIENT_DEF_PKCS_LEN 1024
 
+/* XXX This is entirely temporary structure until UI is written again. */
+typedef struct {
+  /* Input buffer that holds the characters user types. This is
+     used only to store the typed chars for a while. */
+  SilcBuffer input_buffer;
+
+  /* The SILC client screen object */
+  SilcScreen screen;
+
+  /* Current physical window */
+  void *current_win;
+
+  SilcClientConnection conn;
+
+  /* Configuration object */
+  SilcClientConfig config;
+
+#ifdef SILC_SIM
+  /* SIM (SILC Module) table */
+  SilcSimContext **sim;
+  unsigned int sim_count;
+#endif
+
+  /* The allocated client */
+  SilcClient client;
+} *SilcClientInternal;
+
+/* Macros */
+
+#ifndef CTRL
+#define CTRL(x) ((x) & 0x1f)   /* Ctrl+x */
+#endif
+
 #endif
diff --git a/lib/silcclient/Makefile.am b/lib/silcclient/Makefile.am
new file mode 100644 (file)
index 0000000..8f914cf
--- /dev/null
@@ -0,0 +1,35 @@
+#
+#  Makefile.am
+#
+#  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+#
+#  Copyright (C) 2000 Pekka Riikonen
+#
+#  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
+#  GNU General Public License for more details.
+#
+
+AUTOMAKE_OPTIONS = 1.0 no-dependencies foreign
+
+noinst_LIBRARIES = libsilcclient.a
+
+libsilcclient_a_SOURCES = \
+       client.c \
+       command.c \
+       command_reply.c \
+       idlist.c \
+       protocol.c
+
+EXTRA_DIST = *.h
+
+INCLUDES = -I. -I.. -I../silccore -I../silccrypt -I../silcutil \
+       -I../silcmath -I../silcske -I../silcsim \
+       -I../../includes \
+       -I../silcmath/gmp
diff --git a/lib/silcclient/README b/lib/silcclient/README
new file mode 100644 (file)
index 0000000..26eb385
--- /dev/null
@@ -0,0 +1,114 @@
+SILC Client Library
+===================
+
+This directory includes the SILC Client implementation.  The library uses
+common and core components of SILC protocol from lib/silccore library and
+normal utility routines from lib/silcutil library.  The library has been
+designed to be complete SILC Client implementation without actual user
+interface.  The library provides API for the application which can use
+it to implement generally what ever user interface it wants.
+
+The `ops.h' file defines the function prototypes that application must
+implement in order to be able to create the user interface with the
+library.  The idea is that the application can implement what ever user
+interface routines in the functions and display the data what ever way
+it wants.  The library is entirely transparent to the user interface and
+it does not include any user interface specific issues such as window
+handling or item handling on the screen etc.  These does not interest
+the library.
+
+
+
+Creating Client
+===============
+
+The client is context or entity based (which ever) thus several client
+entitites can be created in the application if needed.  However, it should
+be noted that they are completely independent from each other and can
+be seen as different applications.  Usually only one client entity is
+needed per application.
+
+The client object is SilcClient which is usually allocated in following
+manner:
+
+       SilcClient client = silc_client_alloc(&ops, context);
+
+`ops' is the static structure of client operations that library will call.
+`context' can be some application specific context that will be saved into
+the SilcClient object.  It is up to the caller to free this context.
+SilcClient is always passed to the application thus the application specific
+context can be retrieved from the SilcClient object.  See `client.h' file
+for detailed definition of SilcClient object.
+
+`ops' can be defined for example as follows:
+
+SilcClientOperations ops = {
+  say:                  silc_say,
+  channel_message:      silc_channel_message,
+  private_message:      silc_private_message,
+  command:              silc_command,
+  command_reply:        silc_command_reply,
+  connect:              silc_connect,
+  disconnect:           silc_disconnect,
+  get_auth_method:      silc_get_auth_method,
+  verify_server_key:    silc_verify_server_key,
+  ask_passphrase:       silc_ask_passphrase,
+};
+
+
+Initializing the Client
+=======================
+
+The client must be initialized before running.  However, there are also
+some other tasks that must be done before initializing the client.  Following
+pointers must be set before calling the initializing function:
+
+       client->username
+       client->realname
+       client->pkcs
+       client->public_key
+       client->private_key
+
+After setting the pointers one must call:
+
+       silc_client_init(client);
+
+which then initializes the client library for the `client'.  If the pointers
+mentioned above are not initialized the silc_client_init will fail.
+
+
+Running the Client
+==================
+
+The client is run by calling silc_client_run.  The function will call
+the scheduler from utility library that will be run until the program is
+ended.  When silc_client_run returns the application is ended.  Thus,
+to run the client, call:
+
+       silc_client_run(client);
+
+Usually application may do some other initializations before calling
+this function.  For example before calling this function application should
+initialize the user interface.
+
+
+Creating Connection to Server
+=============================
+
+Connection to remote SILC server is done by calling:
+
+       silc_client_connect_to_server(client, port, hostname, context);
+
+The function will create the connection asynchronously to the server, ie.
+the function will return before the actual connection is created.  After
+the connection is created the client->ops->connect operation is called.
+
+Generally speaking the connections are associated with windows' on the
+screen.  IRC is usually implemented this way, however it is not the necessary
+way to associate the client's connections.  SilcClientConnection object
+is provided by the library (and is always passed to the application) that
+can be used in the application to associate the connection from the library.
+Application specific context can be saved to the SilcClientConnection object
+which then can be retrieved in the application, thus perhaps associate
+the connection with what ever object in the application (window or something
+else).
similarity index 58%
rename from apps/silc/client.c
rename to lib/silcclient/client.c
index b3a6d391bc0c38ac1e3f220e51bea94256cbacf4..d2b4aa660420eafa56ffaf918292651712fdcd9f 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.13  2000/07/20 10:17:25  priikone
- *     Added dynamic protocol registering/unregistering support.  The
- *     patch was provided by cras.
- *
- * Revision 1.12  2000/07/19 07:07:34  priikone
- *     Save packet on private message's command context.
- *
- * Revision 1.11  2000/07/18 12:20:39  priikone
- *     Added ^U functionality, clears input line (patch form cras).
- *
- * Revision 1.10  2000/07/18 06:53:15  priikone
- *     Allow partial command strings in comparison.
- *
- * Revision 1.9  2000/07/14 06:13:19  priikone
- *     Moved all the generic packet sending, encryption, reception,
- *     decryption and processing functions to library as they were
- *     duplicated code with the server. Now client uses the generic
- *     routines which is a lot cleaner.
- *
- * Revision 1.8  2000/07/12 05:56:32  priikone
- *     Major rewrite of ID Cache system. Support added for the new
- *     ID cache system.
- *
- * Revision 1.7  2000/07/10 05:40:33  priikone
- *     Minor bug fixes.
- *
- * Revision 1.6  2000/07/07 06:54:16  priikone
- *     Print channel name when receiving channel message to non-current
- *     channel.
- *
- * Revision 1.5  2000/07/06 07:14:36  priikone
- *     Fixes to NAMES command handling.
- *     Fixes when leaving from channel.
- *
- * Revision 1.4  2000/07/05 06:12:05  priikone
- *     Global cosmetic changes.
- *
- * Revision 1.3  2000/07/04 08:29:12  priikone
- *     Added support for PING command. The ping times are calculated
- *     and showed to the user.
- *
- * Revision 1.2  2000/07/03 05:49:48  priikone
- *     Implemented LEAVE command.  Minor bug fixes.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
-
-#include "clientincludes.h"
-
-/* Static function prototypes */
-static int silc_client_bad_keys(unsigned char key);
-static void silc_client_clear_input(SilcClient client);
-static void silc_client_process_message(SilcClient client);
-static char *silc_client_parse_command(unsigned char *buffer);
+/* $Id$ */
+
+#include "clientlibincludes.h"
 
 /* Static task callback prototypes */
-SILC_TASK_CALLBACK(silc_client_update_clock);
-SILC_TASK_CALLBACK(silc_client_run_commands);
-SILC_TASK_CALLBACK(silc_client_process_key_press);
 SILC_TASK_CALLBACK(silc_client_connect_to_server_start);
 SILC_TASK_CALLBACK(silc_client_connect_to_server_second);
 SILC_TASK_CALLBACK(silc_client_connect_to_server_final);
 SILC_TASK_CALLBACK(silc_client_packet_process);
 SILC_TASK_CALLBACK(silc_client_packet_parse_real);
 
-SilcClientWindow silc_client_create_main_window(SilcClient client);
-SilcClientWindow silc_client_add_window(SilcClient client,
-                                       int is_current);
-void silc_client_packet_parse(SilcPacketParserContext *parser_context);
-void silc_client_packet_parse_type(SilcClient client, 
-                                  SilcSocketConnection sock,
-                                  SilcPacketContext *packet);
-void silc_client_private_message_process(SilcClient client,
-                                        SilcSocketConnection sock,
-                                        SilcPacketContext *packet);
-
-/* Definitions from version.h */
-extern char *silc_version;
-extern char *silc_name;
-extern char *silc_fullname;
+static void silc_client_packet_parse(SilcPacketParserContext *parser_context);
+static void silc_client_packet_parse_type(SilcClient client, 
+                                         SilcSocketConnection sock,
+                                         SilcPacketContext *packet);
 
 /* Allocates new client object. This has to be done before client may
    work. After calling this one must call silc_client_init to initialize
-   the client. */
+   the client. The `application' is application specific user data pointer
+   and caller must free it. */
 
-int silc_client_alloc(SilcClient *new_client)
+SilcClient silc_client_alloc(SilcClientOperations *ops, void *application)
 {
+  SilcClient new_client;
 
-  *new_client = silc_calloc(1, sizeof(**new_client));
-  (*new_client)->input_buffer = NULL;
-  (*new_client)->screen = NULL;
-  (*new_client)->windows = NULL;
-  (*new_client)->windows_count = 0;
-  (*new_client)->current_win = NULL;
+  new_client = silc_calloc(1, sizeof(*new_client));
+  new_client->application = application;
+  new_client->ops = ops;
 
-  return TRUE;
+  return new_client;
 }
 
 /* Free's client object */
@@ -136,18 +64,7 @@ void silc_client_free(SilcClient client)
 
 int silc_client_init(SilcClient client)
 {
-
   SILC_LOG_DEBUG(("Initializing client"));
-  assert(client);
-
-  client->username = silc_get_username();
-  client->realname = silc_get_real_name();
-
-  /* Register all configured ciphers, PKCS and hash functions. */
-  client->config->client = (void *)client;
-  silc_client_config_register_ciphers(client->config);
-  silc_client_config_register_pkcs(client->config);
-  silc_client_config_register_hashfuncs(client->config);
 
   /* Initialize hash functions for client to use */
   silc_hash_alloc("md5", &client->md5hash);
@@ -161,84 +78,14 @@ int silc_client_init(SilcClient client)
   silc_rng_init(client->rng);
   silc_math_primegen_init(); /* XXX */
 
-  /* Load public and private key */
-  if (silc_client_load_keys(client) == FALSE)
-    goto err0;
-
-  /* Register the task queues. In SILC we have by default three task queues. 
-     One task queue for non-timeout tasks which perform different kind of 
-     I/O on file descriptors, timeout task queue for timeout tasks, and,
-     generic non-timeout task queue whose tasks apply to all connections. */
-  silc_task_queue_alloc(&client->io_queue, TRUE);
-  if (!client->io_queue) {
-    goto err0;
-  }
-  silc_task_queue_alloc(&client->timeout_queue, TRUE);
-  if (!client->timeout_queue) {
-    goto err1;
-  }
-  silc_task_queue_alloc(&client->generic_queue, TRUE);
-  if (!client->generic_queue) {
-    goto err1;
-  }
-
   /* Register protocols */
   silc_client_protocols_register();
 
   /* Initialize the scheduler */
-  silc_schedule_init(client->io_queue, client->timeout_queue, 
-                    client->generic_queue, 5000);
-
-  /* Register the main task that is used in client. This received
-     the key pressings. */
-  if (silc_task_register(client->io_queue, fileno(stdin), 
-                        silc_client_process_key_press,
-                        (void *)client, 0, 0, 
-                        SILC_TASK_FD,
-                        SILC_TASK_PRI_NORMAL) == NULL) {
-    goto err2;
-  }
-
-  /* Register timeout task that updates clock every minute. */
-  if (silc_task_register(client->timeout_queue, 0,
-                        silc_client_update_clock,
-                        (void *)client, 
-                        silc_client_time_til_next_min(), 0,
-                        SILC_TASK_TIMEOUT,
-                        SILC_TASK_PRI_LOW) == NULL) {
-    goto err2;
-  }
-
-  if (client->config->commands) {
-    /* Run user configured commands with timeout */
-    if (silc_task_register(client->timeout_queue, 0,
-                          silc_client_run_commands,
-                          (void *)client, 0, 1,
-                          SILC_TASK_TIMEOUT,
-                          SILC_TASK_PRI_LOW) == NULL) {
-      goto err2;
-    }
-  }
-
-  /* Allocate the input buffer used to save typed characters */
-  client->input_buffer = silc_buffer_alloc(SILC_SCREEN_INPUT_WIN_SIZE);
-  silc_buffer_pull_tail(client->input_buffer, 
-                       SILC_BUFFER_END(client->input_buffer));
-
-  /* Initialize the screen */
-  client->screen = silc_screen_init();
-  silc_client_create_main_window(client);
-  client->screen->input_buffer = client->input_buffer->data;
-  silc_screen_print_coordinates(client->screen, 0);
+  silc_schedule_init(&client->io_queue, &client->timeout_queue, 
+                    &client->generic_queue, 5000);
 
   return TRUE;
-
- err0:
-  silc_task_queue_free(client->timeout_queue);
- err1:
-  silc_task_queue_free(client->io_queue);
- err2:
-  return FALSE;
 }
 
 /* Stops the client. This is called to stop the client and thus to stop
@@ -256,7 +103,7 @@ void silc_client_stop(SilcClient client)
 
   silc_client_protocols_unregister();
 
-  SILC_LOG_DEBUG(("Client client"));
+  SILC_LOG_DEBUG(("Client stopped"));
 }
 
 /* Runs the client. */
@@ -270,457 +117,60 @@ void silc_client_run(SilcClient client)
   silc_schedule();
 }
 
-/* Creates the main window used in SILC client. This is called always
-   at the initialization of the client. If user wants to create more
-   than one windows a new windows are always created by calling 
-   silc_client_add_window. */
+/* Allocates and adds new connection to the client. This adds the allocated
+   connection to the connection table and returns a pointer to it. A client
+   can have multiple connections to multiple servers. Every connection must
+   be added to the client using this function. User data `context' may
+   be sent as argument. */
 
-SilcClientWindow silc_client_create_main_window(SilcClient client)
+SilcClientConnection silc_client_add_connection(SilcClient client,
+                                               void *context)
 {
-  SilcClientWindow win;
-  void *screen;
-
-  SILC_LOG_DEBUG(("Creating main window"));
-
-  assert(client->screen != NULL);
-
-  client->screen->u_stat_line.program_name = silc_name;
-  client->screen->u_stat_line.program_version = silc_version;
-
-  /* Create windows */
-  win = silc_calloc(1, sizeof(*win));
-  win->nickname = silc_get_username();
-  win->local_id = NULL;
-  win->local_id_data = NULL;
-  win->local_id_data_len = 0;
-  win->remote_host = NULL;
-  win->remote_port = -1;
-  win->sock = NULL;
-
-  /* Initialize ID caches */
-  win->client_cache = silc_idcache_alloc(0);
-  win->channel_cache = silc_idcache_alloc(0);
-  win->server_cache = silc_idcache_alloc(0);
-
-  /* Create the actual screen */
-  screen = (void *)silc_screen_create_output_window(client->screen);
-  silc_screen_create_input_window(client->screen);
-  silc_screen_init_upper_status_line(client->screen);
-  silc_screen_init_output_status_line(client->screen);
-  win->screen = screen;
-
-  client->screen->bottom_line->nickname = win->nickname;
-  silc_screen_print_bottom_line(client->screen, 0);
-
-  /* Add the window to windows table */
-  client->windows = silc_calloc(1, sizeof(*client->windows));
-  client->windows[client->windows_count] = win;
-  client->windows_count = 1;
-
-  /* Automatically becomes the current active window */
-  client->current_win = win;
-
-  return win;
-}
-
-/* Allocates and adds new window to the client. This allocates new
-   physical window and internal window for connection specific data. 
-   All the connection specific data is always saved into a window
-   since connection is always associated to a active window. */
-
-SilcClientWindow silc_client_add_window(SilcClient client,
-                                       int is_current)
-{
-  SilcClientWindow win;
-
-  assert(client->screen != NULL);
-
-  win = silc_calloc(1, sizeof(*win));
+  SilcClientConnection conn;
+  int i;
 
-  /* Add the pointers */
-  win->screen = silc_screen_add_output_window(client->screen);
-  win->sock = NULL;
+  conn = silc_calloc(1, sizeof(*conn));
 
   /* Initialize ID caches */
-  win->client_cache = silc_idcache_alloc(0);
-  win->channel_cache = silc_idcache_alloc(0);
-  win->server_cache = silc_idcache_alloc(0);
-
-  /* Add the window to windows table */
-  client->windows = silc_realloc(client->windows, sizeof(*client->windows)
-                                * (client->windows_count + 1));
-  client->windows[client->windows_count] = win;
-  client->windows_count++;
-
-  if (is_current == TRUE)
-    client->current_win = win;
-
-  return win;
-}
-
-/* The main task on SILC client. This processes the key pressings user
-   has made. */
-
-SILC_TASK_CALLBACK(silc_client_process_key_press)
-{
-  SilcClient client = (SilcClient)context;
-  int c;
-
-  /* There is data pending in stdin, this gets it directly */
-  c = wgetch(client->screen->input_win);
-  if (silc_client_bad_keys(c))
-    return;
-
-  SILC_LOG_DEBUG(("Pressed key: %d", c));
-
-  switch(c) {
-    /* 
-     * Special character handling
-     */
-  case KEY_UP: 
-  case KEY_DOWN:
-    break;
-  case KEY_RIGHT:
-    /* Right arrow */
-    SILC_LOG_DEBUG(("RIGHT"));
-    silc_screen_input_cursor_right(client->screen);
-    break;
-  case KEY_LEFT:
-    /* Left arrow */
-    SILC_LOG_DEBUG(("LEFT"));
-    silc_screen_input_cursor_left(client->screen);
-    break;
-  case KEY_BACKSPACE:
-  case KEY_DC:
-  case '\177':
-  case '\b':
-    /* Backspace */
-    silc_screen_input_backspace(client->screen);
-    break;
-  case '\011':
-    /* Tabulator */
-    break;
-  case KEY_IC:
-    /* Insert switch. Turns on/off insert on input window */
-    silc_screen_input_insert(client->screen);
-    break;
-  case CTRL('j'):
-  case '\r':
-    /* Enter, Return. User pressed enter we are ready to
-       process the message. */
-    silc_client_process_message(client);
-    break;
-  case CTRL('l'):
-    /* Refresh screen, Ctrl^l */
-    silc_screen_refresh_all(client->screen);
-    break;
-  case CTRL('a'):
-  case KEY_HOME:
-  case KEY_BEG:
-    /* Beginning, Home */
-    silc_screen_input_cursor_home(client->screen);
-    break;
-  case CTRL('e'):
-  case KEY_END:
-    /* End */
-    silc_screen_input_cursor_end(client->screen);
-    break;
-  case KEY_LL:
-    /* End */
-    break;
-  case CTRL('g'):
-    /* Bell, Ctrl^g */
-    beep();
-    break;
-  case KEY_DL:
-  case CTRL('u'):
-    /* Delete line */
-    silc_client_clear_input(client);
-    break;
-  default:
-    /* 
-     * Other characters 
-     */
-    if (c < 32) {
-      /* Control codes are printed as reversed */
-      c = (c & 127) | 64;
-      wattron(client->screen->input_win, A_REVERSE);
-      silc_screen_input_print(client->screen, c);
-      wattroff(client->screen->input_win, A_REVERSE);
-    } else  {
-      /* Normal character */
-      silc_screen_input_print(client->screen, c);
-    }
-  }
-
-  silc_screen_print_coordinates(client->screen, 0);
-  silc_screen_refresh_win(client->screen->input_win);
-}
-
-static int silc_client_bad_keys(unsigned char key)
-{
-  /* these are explained in curses.h */
-  switch(key) {
-  case KEY_SF:
-  case KEY_SR:
-  case KEY_NPAGE:
-  case KEY_PPAGE:
-  case KEY_PRINT:
-  case KEY_A1:
-  case KEY_A3:
-  case KEY_B2:
-  case KEY_C1:
-  case KEY_C3:
-  case KEY_UNDO:
-  case KEY_EXIT:
-  case '\v':           /* VT */
-  case '\E':           /* we ignore ESC */
-    return TRUE;
-  default: 
-    return FALSE; 
-  }
-}
-
-/* Clears input buffer */
-
-static void silc_client_clear_input(SilcClient client)
-{
-  silc_buffer_clear(client->input_buffer);
-  silc_buffer_pull_tail(client->input_buffer,
-                       SILC_BUFFER_END(client->input_buffer));
-  silc_screen_input_reset(client->screen);
-}
-
-/* Processes messages user has typed on the screen. This either sends
-   a packet out to network or if command were written executes it. */
-
-static void silc_client_process_message(SilcClient client)
-{
-  unsigned char *data;
-  unsigned int len;
-
-  SILC_LOG_DEBUG(("Start"));
-
-  data = client->input_buffer->data;
-  len = strlen(data);
-
-  if (data[0] == '/' && data[1] != ' ') {
-    /* Command */
-    unsigned int argc = 0;
-    unsigned char **argv, *tmpcmd;
-    unsigned int *argv_lens, *argv_types;
-    SilcClientCommand *cmd;
-    SilcClientCommandContext ctx;
-
-    /* Get the command */
-    tmpcmd = silc_client_parse_command(data);
-
-    /* Find command match */
-    for (cmd = silc_command_list; cmd->name; cmd++) {
-      if (!strncmp(cmd->name, tmpcmd, strlen(tmpcmd)))
-       break;
+  conn->client_cache = silc_idcache_alloc(0);
+  conn->channel_cache = silc_idcache_alloc(0);
+  conn->server_cache = silc_idcache_alloc(0);
+  conn->client = client;
+  conn->context = context;
+
+  /* Add the connection to connections table */
+  for (i = 0; i < client->conns_count; i++)
+    if (client->conns && !client->conns[i]) {
+      client->conns[i] = conn;
+      return conn;
     }
 
-    if (cmd->name == NULL) {
-      silc_say(client, "Invalid command: %s", tmpcmd);
-      silc_free(tmpcmd);
-      goto out;
-    }
-
-    /* Now parse all arguments */
-    silc_client_parse_command_line(data, &argv, &argv_lens, 
-                                  &argv_types, &argc, cmd->max_args);
-    silc_free(tmpcmd);
-
-    SILC_LOG_DEBUG(("Exeuting command: %s", cmd->name));
-
-    /* Allocate command context. This and its internals must be free'd 
-       by the command routine receiving it. */
-    ctx = silc_calloc(1, sizeof(*ctx));
-    ctx->client = client;
-    ctx->sock = client->current_win->sock;
-    ctx->argc = argc;
-    ctx->argv = argv;
-    ctx->argv_lens = argv_lens;
-    ctx->argv_types = argv_types;
-
-    /* Execute command */
-    (*cmd->cb)(ctx);
+  client->conns = silc_realloc(client->conns, sizeof(*client->conns)
+                              * (client->conns_count + 1));
+  client->conns[client->conns_count] = conn;
+  client->conns_count++;
 
-  } else {
-    /* Normal message to a channel */
-    if (len && client->current_win->current_channel &&
-       client->current_win->current_channel->on_channel == TRUE) {
-      silc_print(client, "> %s", data);
-      silc_client_packet_send_to_channel(client, 
-                                        client->current_win->sock,
-                                        client->current_win->current_channel,
-                                        data, strlen(data), TRUE);
-    }
-  }
-
- out:
-  /* Clear the input buffer */
-  silc_client_clear_input(client);
+  return conn;
 }
 
-/* Returns the command fetched from user typed command line */
+/* Removes connection from client. */
 
-static char *silc_client_parse_command(unsigned char *buffer)
+void silc_client_del_connection(SilcClient client, SilcClientConnection conn)
 {
-  char *ret;
-  const char *cp = buffer;
-  int len;
-
-  len = strcspn(cp, " ");
-  ret = silc_to_upper((char *)++cp);
-  ret[len - 1] = 0;
-
-  return ret;
-}
-
-/* Parses user typed command line. At most `max_args' is taken. Rest
-   of the line will be allocated as the last argument if there are more
-   than `max_args' arguments in the line. Note that the command name
-   is counted as one argument and is saved. */
-
-void silc_client_parse_command_line(unsigned char *buffer, 
-                                   unsigned char ***parsed,
-                                   unsigned int **parsed_lens,
-                                   unsigned int **parsed_types,
-                                   unsigned int *parsed_num,
-                                   unsigned int max_args)
-{
-  int i, len = 0;
-  int argc = 0;
-  const char *cp = buffer;
-
-  /* Take the '/' away */
-  cp++;
-
-  *parsed = silc_calloc(1, sizeof(**parsed));
-  *parsed_lens = silc_calloc(1, sizeof(**parsed_lens));
-
-  /* Get the command first */
-  len = strcspn(cp, " ");
-  (*parsed)[0] = silc_to_upper((char *)cp);
-  (*parsed_lens)[0] = len;
-  cp += len + 1;
-  argc++;
-
-  /* Parse arguments */
-  if (strchr(cp, ' ') || strlen(cp) != 0) {
-    for (i = 1; i < max_args; i++) {
-
-      if (i != max_args - 1)
-       len = strcspn(cp, " ");
-      else
-       len = strlen(cp);
-      
-      *parsed = silc_realloc(*parsed, sizeof(**parsed) * (argc + 1));
-      *parsed_lens = silc_realloc(*parsed_lens, 
-                                 sizeof(**parsed_lens) * (argc + 1));
-      (*parsed)[argc] = silc_calloc(len + 1, sizeof(char));
-      memcpy((*parsed)[argc], cp, len);
-      (*parsed_lens)[argc] = len;
-      argc++;
-
-      cp += len;
-      if (strlen(cp) == 0)
-       break;
-      else
-       cp++;
-    }
-  }
-
-  /* Save argument types. Protocol defines all argument types but
-     this implementation makes sure that they are always in correct
-     order hence this simple code. */
-  *parsed_types = silc_calloc(argc, sizeof(**parsed_types));
-  for (i = 0; i < argc; i++)
-    (*parsed_types)[i] = i;
-
-  *parsed_num = argc;
-}
-
-/* Updates clock on the screen every minute. */
-
-SILC_TASK_CALLBACK(silc_client_update_clock)
-{
-  SilcClient client = (SilcClient)context;
-
-  /* Update the clock on the screen */
-  silc_screen_print_clock(client->screen);
-
-  /* Re-register this same task */
-  silc_task_register(qptr, 0, silc_client_update_clock, context, 
-                    silc_client_time_til_next_min(), 0,
-                    SILC_TASK_TIMEOUT,
-                    SILC_TASK_PRI_LOW);
-
-  silc_screen_refresh_win(client->screen->input_win);
-}
-
-/* Runs commands user configured in configuration file. This is
-   called when initializing client. */
-
-SILC_TASK_CALLBACK(silc_client_run_commands)
-{
-  SilcClient client = (SilcClient)context;
-  SilcClientConfigSectionCommand *cs;
-
-  SILC_LOG_DEBUG(("Start"));
-
-  cs = client->config->commands;
-  while(cs) {
-    unsigned int argc = 0;
-    unsigned char **argv, *tmpcmd;
-    unsigned int *argv_lens, *argv_types;
-    SilcClientCommand *cmd;
-    SilcClientCommandContext ctx;
-
-    /* Get the command */
-    tmpcmd = silc_client_parse_command(cs->command);
+  int i;
 
-    for (cmd = silc_command_list; cmd->name; cmd++) {
-      if (!strcmp(cmd->name, tmpcmd))
-       break;
-    }
-    
-    if (cmd->name == NULL) {
-      silc_say(client, "Invalid command: %s", tmpcmd);
-      silc_free(tmpcmd);
-      continue;
+  for (i = 0; i < client->conns_count; i++)
+    if (client->conns[i] == conn) {
+      silc_free(conn);
+      client->conns[i] = NULL;
     }
-    
-    /* Now parse all arguments */
-    silc_client_parse_command_line(cs->command, &argv, &argv_lens, 
-                                  &argv_types, &argc, cmd->max_args);
-    silc_free(tmpcmd);
-
-    SILC_LOG_DEBUG(("Exeuting command: %s", cmd->name));
-
-    /* Allocate command context. This and its internals must be free'd 
-       by the command routine receiving it. */
-    ctx = silc_calloc(1, sizeof(*ctx));
-    ctx->client = client;
-    ctx->sock = client->current_win->sock;
-    ctx->argc = argc;
-    ctx->argv = argv;
-    ctx->argv_lens = argv_lens;
-    ctx->argv_types = argv_types;
-
-    /* Execute command */
-    (*cmd->cb)(ctx);
-
-    cs = cs->next;
-  }
 }
 
 /* Internal context for connection process. This is needed as we
    doing asynchronous connecting. */
 typedef struct {
   SilcClient client;
+  SilcClientConnection conn;
   SilcTask task;
   int sock;
   char *host;
@@ -755,25 +205,32 @@ silc_client_connect_to_server_internal(SilcClientInternalConnectContext *ctx)
   return sock;
 }
 
-/* Connects to remote server */
+/* Connects to remote server. This is the main routine used to connect
+   to SILC server. Returns -1 on error and the created socket otherwise. 
+   The `context' is user context that is saved into the SilcClientConnection
+   that is created after the connection is created. */
 
 int silc_client_connect_to_server(SilcClient client, int port,
-                                 char *host)
+                                 char *host, void *context)
 {
   SilcClientInternalConnectContext *ctx;
+  SilcClientConnection conn;
 
   SILC_LOG_DEBUG(("Connecting to port %d of server %s",
                  port, host));
 
-  silc_say(client, "Connecting to port %d of server %s", port, host);
+  conn = silc_client_add_connection(client, context);
+  conn->remote_host = strdup(host);
+  conn->remote_port = port;
 
-  client->current_win->remote_host = strdup(host);
-  client->current_win->remote_port = port;
+  client->ops->say(client, conn, 
+                  "Connecting to port %d of server %s", port, host);
 
   /* Allocate internal context for connection process. This is
      needed as we are doing async connecting. */
   ctx = silc_calloc(1, sizeof(*ctx));
   ctx->client = client;
+  ctx->conn = conn;
   ctx->host = strdup(host);
   ctx->port = port;
   ctx->tries = 0;
@@ -790,6 +247,7 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
   SilcClientInternalConnectContext *ctx =
     (SilcClientInternalConnectContext *)context;
   SilcClient client = ctx->client;
+  SilcClientConnection conn = ctx->conn;
   SilcProtocol protocol;
   SilcClientKEInternalContext *proto_ctx;
   int opt, opt_len = sizeof(opt);
@@ -801,10 +259,11 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
   if (opt != 0) {
     if (ctx->tries < 2) {
       /* Connection failed but lets try again */
-      silc_say(ctx->client, "Could not connect to server %s: %s",
-              ctx->host, strerror(opt));
-      silc_say(client, "Connecting to port %d of server %s resumed", 
-              ctx->port, ctx->host);
+      client->ops->say(client, conn, "Could not connect to server %s: %s",
+                      ctx->host, strerror(opt));
+      client->ops->say(client, conn, 
+                      "Connecting to port %d of server %s resumed", 
+                      ctx->port, ctx->host);
 
       /* Unregister old connection try */
       silc_schedule_unset_listen_fd(fd);
@@ -816,12 +275,15 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
       ctx->tries++;
     } else {
       /* Connection failed and we won't try anymore */
-      silc_say(ctx->client, "Could not connect to server %s: %s",
-              ctx->host, strerror(opt));
+      client->ops->say(client, conn, "Could not connect to server %s: %s",
+                      ctx->host, strerror(opt));
       silc_schedule_unset_listen_fd(fd);
       silc_net_close_connection(fd);
       silc_task_unregister(client->io_queue, ctx->task);
       silc_free(ctx);
+
+      /* Notify application of failure */
+      client->ops->connect(client, conn, FALSE);
     }
     return;
   }
@@ -831,22 +293,24 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
   silc_free(ctx);
 
   /* Allocate new socket connection object */
-  silc_socket_alloc(fd, SILC_SOCKET_TYPE_SERVER, 
-                   (void *)client->current_win, 
-                   &client->current_win->sock);
-  if (client->current_win->sock == NULL) {
-    silc_say(client, "Error: Could not allocate connection socket");
+  silc_socket_alloc(fd, SILC_SOCKET_TYPE_SERVER, (void *)conn, &conn->sock);
+  if (conn->sock == NULL) {
+    client->ops->say(client, conn, 
+                    "Error: Could not allocate connection socket");
     silc_net_close_connection(fd);
+    client->ops->connect(client, conn, FALSE);
     return;
   }
-  client->current_win->sock->hostname = client->current_win->remote_host;
-  client->current_win->sock->port = client->current_win->remote_port;
+
+  conn->nickname = strdup(client->username);
+  conn->sock->hostname = conn->remote_host;
+  conn->sock->port = conn->remote_port;
 
   /* Allocate internal Key Exchange context. This is sent to the
      protocol as context. */
   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
   proto_ctx->client = (void *)client;
-  proto_ctx->sock = client->current_win->sock;
+  proto_ctx->sock = conn->sock;
   proto_ctx->rng = client->rng;
   proto_ctx->responder = FALSE;
 
@@ -856,10 +320,12 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
                      &protocol, (void *)proto_ctx,
                      silc_client_connect_to_server_second);
   if (!protocol) {
-    silc_say(client, "Error: Could not start authentication protocol");
+    client->ops->say(client, conn, 
+                    "Error: Could not start authentication protocol");
+    client->ops->connect(client, conn, FALSE);
     return;
   }
-  client->current_win->sock->protocol = protocol;
+  conn->sock->protocol = protocol;
 
   /* Register the connection for network input and output. This sets
      that scheduler will listen for incoming packets for this connection 
@@ -898,6 +364,9 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
       silc_free(ctx->dest_id);
     ctx->sock->protocol = NULL;
     silc_free(ctx);
+
+    /* Notify application of failure */
+    client->ops->connect(client, ctx->sock->user_data, FALSE);
     return;
   }
 
@@ -911,29 +380,14 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
   proto_ctx->dest_id = ctx->dest_id;
 
   /* Resolve the authentication method to be used in this connection */
-  proto_ctx->auth_meth = SILC_PROTOCOL_CONN_AUTH_NONE;
-  if (client->config->conns) {
-    SilcClientConfigSectionConnection *conn = NULL;
-
-    /* Check if we find a match from user configured connections */
-    conn = silc_client_config_find_connection(client->config,
-                                             sock->hostname,
-                                             sock->port);
-    if (conn) {
-      /* Match found. Use the configured authentication method */
-      proto_ctx->auth_meth = conn->auth_meth;
-      if (conn->auth_data) {
-       proto_ctx->auth_data = strdup(conn->auth_data);
-       proto_ctx->auth_data_len = strlen(conn->auth_data);
-      }
-    } else {
-      /* No match found. Resolve by sending AUTH_REQUEST to server */
+  if (!client->ops->get_auth_method(client, sock->user_data, sock->hostname,
+                                   sock->port, &proto_ctx->auth_meth,
+                                   &proto_ctx->auth_data, 
+                                   &proto_ctx->auth_data_len))
+    {
+      /* XXX do AUTH_REQUEST resolcing with server */
       proto_ctx->auth_meth = SILC_PROTOCOL_CONN_AUTH_NONE;
     }
-  } else {
-    /* XXX Resolve by sending AUTH_REQUEST to server */
-    proto_ctx->auth_meth = SILC_PROTOCOL_CONN_AUTH_NONE;
-  }
 
   /* Free old protocol as it is finished now */
   silc_protocol_free(protocol);
@@ -966,7 +420,7 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
   SilcClientConnAuthInternalContext *ctx = 
     (SilcClientConnAuthInternalContext *)protocol->context;
   SilcClient client = (SilcClient)ctx->client;
-  SilcClientWindow win = (SilcClientWindow)ctx->sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
   SilcBuffer packet;
 
   SILC_LOG_DEBUG(("Start"));
@@ -982,7 +436,10 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
     if (ctx->dest_id)
       silc_free(ctx->dest_id);
     silc_free(ctx);
-    win->sock->protocol = NULL;
+    conn->sock->protocol = NULL;
+
+    /* Notify application of failure */
+    client->ops->connect(client, ctx->sock->user_data, FALSE);
     return;
   }
 
@@ -1008,15 +465,15 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
   silc_buffer_free(packet);
 
   /* Save remote ID. */
-  win->remote_id = ctx->dest_id;
-  win->remote_id_data = silc_id_id2str(ctx->dest_id, SILC_ID_CHANNEL);
-  win->remote_id_data_len = SILC_ID_CHANNEL_LEN;
+  conn->remote_id = ctx->dest_id;
+  conn->remote_id_data = silc_id_id2str(ctx->dest_id, SILC_ID_SERVER);
+  conn->remote_id_data_len = SILC_ID_SERVER_LEN;
 
-  silc_say(client, "Connected to port %d of host %s",
-          win->remote_port, win->remote_host);
+  client->ops->say(client, conn, "Connected to port %d of host %s",
+                  conn->remote_port, conn->remote_host);
 
-  client->screen->bottom_line->connection = win->remote_host;
-  silc_screen_print_bottom_line(client->screen, 0);
+  /* Notify application of successful connection */
+  client->ops->connect(client, conn, TRUE);
 
   silc_protocol_free(protocol);
   if (ctx->auth_data)
@@ -1026,7 +483,7 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
   if (ctx->dest_id)
     silc_free(ctx->dest_id);
   silc_free(ctx);
-  win->sock->protocol = NULL;
+  conn->sock->protocol = NULL;
 }
 
 /* Internal routine that sends packet or marks packet to be sent. This
@@ -1065,7 +522,7 @@ SILC_TASK_CALLBACK(silc_client_packet_process)
 {
   SilcClient client = (SilcClient)context;
   SilcSocketConnection sock = NULL;
-  SilcClientWindow win;
+  SilcClientConnection conn;
   int ret;
 
   SILC_LOG_DEBUG(("Processing packet"));
@@ -1074,7 +531,7 @@ SILC_TASK_CALLBACK(silc_client_packet_process)
   if (sock == NULL)
     return;
 
-  win = (SilcClientWindow)sock->user_data;
+  conn = (SilcClientConnection)sock->user_data;
 
   /* Packet sending */
   if (type == SILC_TASK_WRITE) {
@@ -1119,19 +576,20 @@ SILC_TASK_CALLBACK(silc_client_packet_process)
         close the connection */
       if (SILC_IS_DISCONNECTING(sock)) {
        silc_client_close_connection(client, sock);
+       client->ops->disconnect(client, conn);
        return;
       }
       
-      silc_say(client, "Connection closed: premature EOF");
+      client->ops->say(client, conn, "Connection closed: premature EOF");
       SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
-
       silc_client_close_connection(client, sock);
+      client->ops->disconnect(client, conn);
       return;
     }
 
     /* Process the packet. This will call the parser that will then
        decrypt and parse the packet. */
-    if (!silc_packet_receive_process(sock, win->receive_key, win->hmac,
+    if (!silc_packet_receive_process(sock, conn->receive_key, conn->hmac,
                                     silc_client_packet_parse, client)) {
       silc_buffer_clear(sock->inbuf);
       return;
@@ -1148,13 +606,13 @@ SILC_TASK_CALLBACK(silc_client_packet_parse_real)
   SilcPacketContext *packet = parse_ctx->packet;
   SilcBuffer buffer = packet->buffer;
   SilcSocketConnection sock = parse_ctx->sock;
-  SilcClientWindow win = (SilcClientWindow)sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   int ret;
 
   SILC_LOG_DEBUG(("Start"));
 
   /* Decrypt the received packet */
-  ret = silc_packet_decrypt(win->receive_key, win->hmac, buffer, packet);
+  ret = silc_packet_decrypt(conn->receive_key, conn->hmac, buffer, packet);
   if (ret < 0)
     goto out;
 
@@ -1185,27 +643,13 @@ SILC_TASK_CALLBACK(silc_client_packet_parse_real)
 void silc_client_packet_parse(SilcPacketParserContext *parser_context)
 {
   SilcClient client = (SilcClient)parser_context->context;
-  SilcClientWindow win = (SilcClientWindow)parser_context->sock->user_data;
-
-  /* If this packet is for the current active connection we will
-     parse the packet right away to get it quickly on the screen.
-     Otherwise, it will be parsed with a timeout as the data is
-     for inactive window (which might not be visible at all). */
-  if (SILC_CLIENT_IS_CURRENT_WIN(client, win)) {
-    /* Parse it real soon */
-    silc_task_register(client->timeout_queue, parser_context->sock->sock, 
-                      silc_client_packet_parse_real,
-                      (void *)parser_context, 0, 1, 
-                      SILC_TASK_TIMEOUT,
-                      SILC_TASK_PRI_NORMAL);
-  } else {
-    /* Parse the packet with timeout */
-    silc_task_register(client->timeout_queue, parser_context->sock->sock, 
-                      silc_client_packet_parse_real,
-                      (void *)parser_context, 0, 200000, 
-                      SILC_TASK_TIMEOUT,
-                      SILC_TASK_PRI_NORMAL);
-  }
+
+  /* Parse the packet */
+  silc_task_register(client->timeout_queue, parser_context->sock->sock, 
+                    silc_client_packet_parse_real,
+                    (void *)parser_context, 0, 1, 
+                    SILC_TASK_TIMEOUT,
+                    SILC_TASK_PRI_NORMAL);
 }
   
 /* Parses the packet type and calls what ever routines the packet type
@@ -1284,14 +728,7 @@ void silc_client_packet_parse_type(SilcClient client,
     /*
      * Received private message
      */
-    {
-      SilcClientCommandReplyContext ctx;
-      ctx = silc_calloc(1, sizeof(*ctx));
-      ctx->client = client;
-      ctx->sock = sock;
-      ctx->packet = packet;
-      silc_client_command_reply_msg((void *)ctx);
-    }
+    silc_client_private_message(client, sock, packet);
     break;
   case SILC_PACKET_PRIVATE_MESSAGE_KEY:
     /*
@@ -1408,14 +845,14 @@ void silc_client_packet_send(SilcClient client,
 
   /* Get data used in the packet sending, keys and stuff */
   if ((!cipher || !hmac || !dst_id) && sock->user_data) {
-    if (!cipher && ((SilcClientWindow)sock->user_data)->send_key)
-      cipher = ((SilcClientWindow)sock->user_data)->send_key;
+    if (!cipher && ((SilcClientConnection)sock->user_data)->send_key)
+      cipher = ((SilcClientConnection)sock->user_data)->send_key;
 
-    if (!hmac && ((SilcClientWindow)sock->user_data)->hmac)
-      hmac = ((SilcClientWindow)sock->user_data)->hmac;
+    if (!hmac && ((SilcClientConnection)sock->user_data)->hmac)
+      hmac = ((SilcClientConnection)sock->user_data)->hmac;
 
-    if (!dst_id && ((SilcClientWindow)sock->user_data)->remote_id) {
-      dst_id = ((SilcClientWindow)sock->user_data)->remote_id;
+    if (!dst_id && ((SilcClientConnection)sock->user_data)->remote_id) {
+      dst_id = ((SilcClientConnection)sock->user_data)->remote_id;
       dst_id_type = SILC_ID_SERVER;
     }
   }
@@ -1423,8 +860,8 @@ void silc_client_packet_send(SilcClient client,
   /* Set the packet context pointers */
   packetdata.flags = 0;
   packetdata.type = type;
-  if (((SilcClientWindow)sock->user_data)->local_id_data)
-    packetdata.src_id = ((SilcClientWindow)sock->user_data)->local_id_data;
+  if (((SilcClientConnection)sock->user_data)->local_id_data)
+    packetdata.src_id = ((SilcClientConnection)sock->user_data)->local_id_data;
   else 
     packetdata.src_id = silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char));
   packetdata.src_id_len = SILC_ID_CLIENT_LEN;
@@ -1487,7 +924,7 @@ void silc_client_packet_send_to_channel(SilcClient client,
                                        int force_send)
 {
   int i;
-  SilcClientWindow win = (SilcClientWindow)sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   SilcBuffer payload;
   SilcPacketContext packetdata;
   SilcCipher cipher;
@@ -1497,7 +934,8 @@ void silc_client_packet_send_to_channel(SilcClient client,
   SILC_LOG_DEBUG(("Sending packet to channel"));
 
   if (!channel || !channel->key) {
-    silc_say(client, "Cannot talk to channel: key does not exist");
+    client->ops->say(client, conn, 
+                    "Cannot talk to channel: key does not exist");
     return;
   }
 
@@ -1509,20 +947,20 @@ void silc_client_packet_send_to_channel(SilcClient client,
     silc_hash_make(client->md5hash, channel->iv, 16, channel->iv);
 
   /* Encode the channel payload */
-  payload = silc_channel_encode_payload(strlen(win->nickname), win->nickname,
+  payload = silc_channel_encode_payload(strlen(conn->nickname), conn->nickname,
                                        data_len, data, 16, channel->iv, 
                                        client->rng);
   if (!payload) {
-    silc_say(client
-            "Error: Could not create packet to be sent to the channel");
+    client->ops->say(client, conn
+                    "Error: Could not create packet to be sent to channel");
     return;
   }
 
   /* Get data used in packet header encryption, keys and stuff. Rest
      of the packet (the payload) is, however, encrypted with the 
      specified channel key. */
-  cipher = win->send_key;
-  hmac = win->hmac;
+  cipher = conn->send_key;
+  hmac = conn->hmac;
   id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
 
   /* Set the packet context pointers. The destination ID is always
@@ -1530,7 +968,7 @@ void silc_client_packet_send_to_channel(SilcClient client,
      distribution of the packet. */
   packetdata.flags = 0;
   packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
-  packetdata.src_id = win->local_id_data;
+  packetdata.src_id = conn->local_id_data;
   packetdata.src_id_len = SILC_ID_CLIENT_LEN;
   packetdata.src_id_type = SILC_ID_CLIENT;
   packetdata.dst_id = id_string;
@@ -1594,7 +1032,7 @@ void silc_client_packet_send_private_message(SilcClient client,
                                             unsigned int data_len, 
                                             int force_send)
 {
-  SilcClientWindow win = (SilcClientWindow)sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   SilcBuffer buffer;
   SilcPacketContext packetdata;
   unsigned int nick_len;
@@ -1604,12 +1042,12 @@ void silc_client_packet_send_private_message(SilcClient client,
   SILC_LOG_DEBUG(("Sending private message"));
 
   /* Create private message payload */
-  nick_len = strlen(client->current_win->nickname);
+  nick_len = strlen(conn->nickname);
   buffer = silc_buffer_alloc(2 + nick_len + data_len);
   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
   silc_buffer_format(buffer,
                     SILC_STR_UI_SHORT(nick_len),
-                    SILC_STR_UI_XNSTRING(client->current_win->nickname,
+                    SILC_STR_UI_XNSTRING(conn->nickname,
                                          nick_len),
                     SILC_STR_UI_XNSTRING(data, data_len),
                     SILC_STR_END);
@@ -1629,18 +1067,18 @@ void silc_client_packet_send_private_message(SilcClient client,
 
   /* Get data used in the encryption */
   cipher = client_entry->send_key;
-  hmac = win->hmac;
+  hmac = conn->hmac;
 
   /* Set the packet context pointers. */
   packetdata.flags = 0;
   packetdata.type = SILC_PACKET_PRIVATE_MESSAGE;
-  packetdata.src_id = win->local_id_data;
+  packetdata.src_id = conn->local_id_data;
   packetdata.src_id_len = SILC_ID_CLIENT_LEN;
   packetdata.src_id_type = SILC_ID_CLIENT;
   if (client_entry)
     packetdata.dst_id = silc_id_id2str(client_entry->id, SILC_ID_CLIENT);
   else
-    packetdata.dst_id = win->local_id_data;
+    packetdata.dst_id = conn->local_id_data;
   packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
   packetdata.dst_id_type = SILC_ID_CLIENT;
   packetdata.rng = client->rng;
@@ -1693,7 +1131,7 @@ void silc_client_packet_send_private_message(SilcClient client,
 void silc_client_close_connection(SilcClient client,
                                  SilcSocketConnection sock)
 {
-  SilcClientWindow win;
+  SilcClientConnection conn;
 
   /* We won't listen for this connection anymore */
   silc_schedule_unset_listen_fd(sock->sock);
@@ -1705,49 +1143,50 @@ void silc_client_close_connection(SilcClient client,
   /* Close the actual connection */
   silc_net_close_connection(sock->sock);
 
-  silc_say(client, "Closed connection to host %s", sock->hostname ?
-          sock->hostname : sock->ip);
+  client->ops->say(client, sock->user_data,
+                  "Closed connection to host %s", sock->hostname ?
+                  sock->hostname : sock->ip);
 
   /* Free everything */
   if (sock->user_data) {
-    win = (SilcClientWindow)sock->user_data;
+    conn = (SilcClientConnection)sock->user_data;
 
     /* XXX Free all client entries and channel entries. */
 
     /* Clear ID caches */
-    silc_idcache_del_all(win->client_cache);
-    silc_idcache_del_all(win->channel_cache);
+    silc_idcache_del_all(conn->client_cache);
+    silc_idcache_del_all(conn->channel_cache);
 
     /* Free data */
-    if (win->remote_host)
-      silc_free(win->remote_host);
-    if (win->local_id)
-      silc_free(win->local_id);
-    if (win->local_id_data)
-      silc_free(win->local_id_data);
-    if (win->send_key)
-      silc_cipher_free(win->send_key);
-    if (win->receive_key)
-      silc_cipher_free(win->receive_key);
-    if (win->hmac)
-      silc_hmac_free(win->hmac);
-    if (win->hmac_key) {
-      memset(win->hmac_key, 0, win->hmac_key_len);
-      silc_free(win->hmac_key);
+    if (conn->remote_host)
+      silc_free(conn->remote_host);
+    if (conn->local_id)
+      silc_free(conn->local_id);
+    if (conn->local_id_data)
+      silc_free(conn->local_id_data);
+    if (conn->send_key)
+      silc_cipher_free(conn->send_key);
+    if (conn->receive_key)
+      silc_cipher_free(conn->receive_key);
+    if (conn->hmac)
+      silc_hmac_free(conn->hmac);
+    if (conn->hmac_key) {
+      memset(conn->hmac_key, 0, conn->hmac_key_len);
+      silc_free(conn->hmac_key);
     }
 
-    win->sock = NULL;
-    win->remote_port = 0;
-    win->remote_type = 0;
-    win->send_key = NULL;
-    win->receive_key = NULL;
-    win->hmac = NULL;
-    win->hmac_key = NULL;
-    win->hmac_key_len = 0;
-    win->local_id = NULL;
-    win->local_id_data = NULL;
-    win->remote_host = NULL;
-    win->current_channel = NULL;
+    conn->sock = NULL;
+    conn->remote_port = 0;
+    conn->remote_type = 0;
+    conn->send_key = NULL;
+    conn->receive_key = NULL;
+    conn->hmac = NULL;
+    conn->hmac_key = NULL;
+    conn->hmac_key_len = 0;
+    conn->local_id = NULL;
+    conn->local_id_data = NULL;
+    conn->remote_host = NULL;
+    conn->current_channel = NULL;
   }
 
   if (sock->protocol) {
@@ -1771,7 +1210,7 @@ void silc_client_disconnected_by_server(SilcClient client,
 
   msg = silc_calloc(message->len + 1, sizeof(char));
   memcpy(msg, message->data, message->len);
-  silc_say(client, msg);
+  client->ops->say(client, sock->user_data, msg);
   silc_free(msg);
 
   SILC_SET_DISCONNECTED(sock);
@@ -1789,7 +1228,7 @@ void silc_client_error_by_server(SilcClient client,
 
   msg = silc_calloc(message->len + 1, sizeof(char));
   memcpy(msg, message->data, message->len);
-  silc_say(client, msg);
+  client->ops->say(client, sock->user_data, msg);
   silc_free(msg);
 }
 
@@ -1803,7 +1242,7 @@ void silc_client_notify_by_server(SilcClient client,
 
   msg = silc_calloc(message->len + 1, sizeof(char));
   memcpy(msg, message->data, message->len);
-  silc_say(client, msg);
+  client->ops->say(client, sock->user_data, msg);
   silc_free(msg);
 }
 
@@ -1814,29 +1253,29 @@ void silc_client_receive_new_id(SilcClient client,
                                SilcSocketConnection sock,
                                unsigned char *id_string)
 {
-  SilcClientWindow win = (SilcClientWindow)sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
 
   /* Delete old ID from ID cache */
-  silc_idcache_del_by_id(win->client_cache, SILC_ID_CLIENT, win->local_id);
+  silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT, conn->local_id);
   
   /* Save the new ID */
-  if (win->local_id)
-    silc_free(win->local_id);
-  win->local_id = silc_id_str2id(id_string, SILC_ID_CLIENT);
-  if (win->local_id_data)
-    silc_free(win->local_id_data);
-  win->local_id_data = 
+  if (conn->local_id)
+    silc_free(conn->local_id);
+  conn->local_id = silc_id_str2id(id_string, SILC_ID_CLIENT);
+  if (conn->local_id_data)
+    silc_free(conn->local_id_data);
+  conn->local_id_data = 
     silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char));
-  memcpy(win->local_id_data, id_string, SILC_ID_CLIENT_LEN);
-  win->local_id_data_len = SILC_ID_CLIENT_LEN;
-  if (!win->local_entry)
-    win->local_entry = silc_calloc(1, sizeof(*win->local_entry));
-  win->local_entry->nickname = win->nickname;
-  win->local_entry->id = win->local_id;
+  memcpy(conn->local_id_data, id_string, SILC_ID_CLIENT_LEN);
+  conn->local_id_data_len = SILC_ID_CLIENT_LEN;
+  if (!conn->local_entry)
+    conn->local_entry = silc_calloc(1, sizeof(*conn->local_entry));
+  conn->local_entry->nickname = conn->nickname;
+  conn->local_entry->id = conn->local_id;
   
   /* Put it to the ID cache */
-  silc_idcache_add(win->client_cache, win->nickname, SILC_ID_CLIENT,
-                  win->local_id, (void *)win->local_entry, TRUE);
+  silc_idcache_add(conn->client_cache, conn->nickname, SILC_ID_CLIENT,
+                  conn->local_id, (void *)conn->local_entry, TRUE);
 }
 
 /* Processed received Channel ID for a channel. This is called when client
@@ -1848,7 +1287,7 @@ void silc_client_new_channel_id(SilcClient client,
                                unsigned int mode,
                                unsigned char *id_string)
 {
-  SilcClientWindow win = (SilcClientWindow)sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   SilcChannelID *id;
   SilcChannelEntry channel;
 
@@ -1859,10 +1298,10 @@ void silc_client_new_channel_id(SilcClient client,
   channel->channel_name = channel_name;
   channel->id = id;
   channel->mode = mode;
-  win->current_channel = channel;
+  conn->current_channel = channel;
   
   /* Put it to the ID cache */
-  silc_idcache_add(win->channel_cache, channel_name, SILC_ID_CHANNEL,
+  silc_idcache_add(conn->channel_cache, channel_name, SILC_ID_CHANNEL,
                   (void *)id, (void *)channel, TRUE);
 }
 
@@ -1878,7 +1317,7 @@ void silc_client_receive_channel_key(SilcClient client,
 {
   unsigned char *id_string, *key, *cipher;
   unsigned int key_len;
-  SilcClientWindow win = (SilcClientWindow)sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   SilcChannelID *id;
   SilcIDCacheEntry id_cache = NULL;
   SilcChannelEntry channel;
@@ -1898,7 +1337,7 @@ void silc_client_receive_channel_key(SilcClient client,
   id = silc_id_str2id(id_string, SILC_ID_CHANNEL);
 
   /* Find channel. */
-  if (!silc_idcache_find_by_id_one(win->channel_cache, (void *)id,
+  if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
                                   SILC_ID_CHANNEL, &id_cache))
     goto out;
   
@@ -1913,7 +1352,8 @@ void silc_client_receive_channel_key(SilcClient client,
 
   silc_cipher_alloc(cipher, &channel->channel_key);
   if (!channel->channel_key) {
-    silc_say(client, "Cannot talk to channel: unsupported cipher %s", cipher);
+    client->ops->say(client, conn,
+                    "Cannot talk to channel: unsupported cipher %s", cipher);
     goto out;
   }
   channel->channel_key->cipher->set_key(channel->channel_key->context, 
@@ -1935,7 +1375,7 @@ void silc_client_channel_message(SilcClient client,
                                 SilcSocketConnection sock, 
                                 SilcPacketContext *packet)
 {
-  SilcClientWindow win = (SilcClientWindow)sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   SilcBuffer buffer = packet->buffer;
   SilcChannelPayload payload = NULL;
   SilcChannelID *id = NULL;
@@ -1949,7 +1389,7 @@ void silc_client_channel_message(SilcClient client,
   id = silc_id_str2id(packet->dst_id, SILC_ID_CHANNEL);
 
   /* Find the channel entry from channels on this window */
-  if (!silc_idcache_find_by_id_one(win->channel_cache, (void *)id,
+  if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
                                   SILC_ID_CHANNEL, &id_cache))
     goto out;
 
@@ -1968,21 +1408,16 @@ void silc_client_channel_message(SilcClient client,
   if (!payload)
     goto out;
 
-  /* Display the message on screen */
+  /* Pass the message to application */
   if (packet->src_id_type == SILC_ID_CLIENT) {
-    /* Message from client */
-    if (channel == win->current_channel)
-      silc_print(client, "<%s> %s", 
-                silc_channel_get_nickname(payload, NULL),
-                silc_channel_get_data(payload, NULL));
-    else
-      silc_print(client, "<%s:%s> %s", 
-                silc_channel_get_nickname(payload, NULL),
-                channel->channel_name,
-                silc_channel_get_data(payload, NULL));
+    client->ops->channel_message(client, conn, 
+                                silc_channel_get_nickname(payload, NULL),
+                                channel->channel_name,
+                                silc_channel_get_data(payload, NULL));
   } else {
     /* Message from server */
-    silc_say(client, "%s", silc_channel_get_data(payload, NULL));
+    /* XXX maybe this should be passed to app... */
+    client->ops->say(client, conn, "%s", silc_channel_get_data(payload, NULL));
   }
 
  out:
@@ -1991,3 +1426,74 @@ void silc_client_channel_message(SilcClient client,
   if (payload)
     silc_channel_free_payload(payload);
 }
+
+/* Private message received. This processes the private message and
+   finally displays it on the screen. */
+
+void silc_client_private_message(SilcClient client, 
+                                SilcSocketConnection sock, 
+                                SilcPacketContext *packet)
+{
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
+  SilcBuffer buffer = packet->buffer;
+  unsigned short nick_len;
+  unsigned char *nickname, *message;
+
+  /* Get nickname */
+  silc_buffer_unformat(buffer, 
+                      SILC_STR_UI16_NSTRING_ALLOC(&nickname, &nick_len),
+                      SILC_STR_END);
+  silc_buffer_pull(buffer, 2 + nick_len);
+     
+  message = silc_calloc(buffer->len + 1, sizeof(char));
+  memcpy(message, buffer->data, buffer->len);
+
+  /* Pass the private message to application */
+  client->ops->private_message(client, conn, nickname, message);
+
+  /* See if we are away (gone). If we are away we will reply to the
+     sender with the set away message. */
+  if (conn->away && conn->away->away) {
+    SilcClientID *remote_id;
+    SilcClientEntry remote_client;
+    SilcIDCacheEntry id_cache;
+
+    if (packet->src_id_type != SILC_ID_CLIENT)
+      goto out;
+
+    remote_id = silc_id_str2id(packet->src_id, SILC_ID_CLIENT);
+    if (!remote_id)
+      goto out;
+
+    if (!SILC_ID_CLIENT_COMPARE(remote_id, conn->local_id))
+      goto out;
+
+    /* Check whether we know this client already */
+    if (!silc_idcache_find_by_id_one(conn->client_cache, remote_id,
+                                    SILC_ID_CLIENT, &id_cache))
+      {
+       /* Allocate client entry */
+       remote_client = silc_calloc(1, sizeof(*remote_client));
+       remote_client->id = remote_id;
+       remote_client->nickname = strdup(nickname);
+
+       /* Save the client to cache */
+       silc_idcache_add(conn->client_cache, remote_client->nickname,
+                        SILC_ID_CLIENT, remote_client->id, remote_client, 
+                        TRUE);
+      } else {
+       silc_free(remote_id);
+       remote_client = (SilcClientEntry)id_cache->context;
+      }
+
+    /* Send the away message */
+    silc_client_packet_send_private_message(client, sock, remote_client,
+                                           conn->away->away,
+                                           strlen(conn->away->away), TRUE);
+  }
+
+ out:
+  memset(message, 0, buffer->len);
+  silc_free(message);
+  silc_free(nickname);
+}
similarity index 74%
rename from apps/silc/client.h
rename to lib/silcclient/client.h
index b4a5a0e213e09a3f8ae099a72886fe60e5be8f44..8caffe812c574e61d749268d4635165b25b3ef25 100644 (file)
 /* Forward declaration for client */
 typedef struct SilcClientObject *SilcClient;
 
-/* Forward declaration for client window */
-typedef struct SilcClientWindowObject *SilcClientWindow;
+/* Forward declaration for client connection */
+typedef struct SilcClientConnectionObject *SilcClientConnection;
 
 #include "idlist.h"
+#include "command.h"
+#include "ops.h"
 
 /* Structure to hold ping time information. Every PING command will 
    add entry of this structure and is removed after reply to the ping
@@ -47,11 +49,9 @@ typedef struct SilcClientAwayStruct {
   struct SilcClientAwayStruct *next;
 } SilcClientAway;
 
-/* Window structure used in client to associate all the important
-   connection (window) specific data to this structure. How the window
-   actually appears on the screen in handeled by the silc_screen*
-   routines in screen.c. */
-struct SilcClientWindowObject {
+/* Connection structure used in client to associate all the important
+   connection specific data to this structure. */
+struct SilcClientConnectionObject {
   /*
    * Local data 
    */
@@ -76,10 +76,10 @@ struct SilcClientWindowObject {
   int remote_type;
   char *remote_info;
 
-  /* Remote client ID for this connection */
-  SilcClientID *remote_id;
+  /* Remote server ID for this connection */
+  SilcServerID *remote_id;
 
-  /* Remote local ID so that the above defined ID would not have
+  /* Decoded remote ID so that the above defined ID would not have
      to be decoded for every packet. */
   unsigned char *remote_id_data;
   unsigned int remote_id_data_len;
@@ -101,7 +101,7 @@ struct SilcClientWindowObject {
   SilcIDCache channel_cache;
   SilcIDCache server_cache;
 
-  /* Current channel on window. All channel's are saved (allocated) into
+  /* Current channel on window. All channels are saved (allocated) into
      the cache entries. */
   SilcChannelEntry current_channel;
 
@@ -117,67 +117,66 @@ struct SilcClientWindowObject {
   /* Set away message */
   SilcClientAway *away;
 
-  /* The actual physical screen. This data is handled by the
-     screen handling routines. */
-  void *screen;
+  /* Pointer back to the SilcClient. This object is passed to the application
+     and the actual client object is accesible thourh this pointer. */
+  SilcClient client;
+
+  /* User data context. Library does not touch this. */
+  void *context;
 };
 
+/* Main client structure. */
 struct SilcClientObject {
+  /*
+   * Public data. All the following pointers must be set by the allocator
+   * of this structure.
+   */
+
+  /* Users's username and realname. */
   char *username;
   char *realname;
 
-  /* Private and public key */
+  /* Private and public key of the user. */
   SilcPKCS pkcs;
   SilcPublicKey public_key;
   SilcPrivateKey private_key;
 
+  /* Application specific user data pointer. Client library does not
+     touch this. */
+  void *application;
+
+  /*
+   * Private data. Following pointers are used internally by the client
+   * library and should be considered read-only fields.
+   */
+
+  /* All client operations that are implemented in the application. */
+  SilcClientOperations *ops;
+
   /* SILC client task queues */
   SilcTaskQueue io_queue;
   SilcTaskQueue timeout_queue;
   SilcTaskQueue generic_queue;
 
-  /* Input buffer that holds the characters user types. This is
-     used only to store the typed chars for a while. */
-  SilcBuffer input_buffer;
-
-  /* Table of windows in client. All the data, including connection
-     specific data, is saved in here. */
-  SilcClientWindow *windows;
-  unsigned int windows_count;
-
-  /* Currently active window. This is pointer to the window table 
-     defined above. This must never be free'd directly. */
-  SilcClientWindow current_win;
+  /* Table of connections in client. All the connection data is saved here. */
+  SilcClientConnection *conns;
+  unsigned int conns_count;
 
-  /* The SILC client screen object */
-  SilcScreen screen;
-
-  /* Generic cipher and hash objects */
+  /* Generic cipher and hash objects. These can be used and referenced
+     by the application as well. */
   SilcCipher none_cipher;
   SilcHash md5hash;
   SilcHash sha1hash;
   SilcHmac md5hmac;
   SilcHmac sha1hmac;
 
-  /* Configuration object */
-  SilcClientConfig config;
-
-  /* Random Number Generator */
+  /* Random Number Generator. Application should use this as its primary
+     random number generator. */
   SilcRng rng;
-
-#ifdef SILC_SIM
-  /* SIM (SILC Module) table */
-  SilcSimContext **sim;
-  unsigned int sim_count;
-#endif
 };
 
 /* Macros */
 
-#ifndef CTRL
-#define CTRL(x) ((x) & 0x1f)   /* Ctrl+x */
-#endif
-
 /* Registers generic task for file descriptor for reading from network and
    writing to network. As being generic task the actual task is allocated 
    only once and after that the same task applies to all registered fd's. */
@@ -207,32 +206,26 @@ do {                                                              \
 do {                                                   \
   int __i;                                             \
                                                        \
-  for (__i = 0; __i < (__x)->windows_count; __i++)     \
-    if ((__x)->windows[__i]->sock->sock == (__fd))     \
+  for (__i = 0; __i < (__x)->conns_count; __i++)       \
+    if ((__x)->conns[__i]->sock->sock == (__fd))       \
       break;                                           \
                                                        \
-  if (__i >= (__x)->windows_count)                     \
+  if (__i >= (__x)->conns_count)                       \
     (__sock) = NULL;                                   \
- (__sock) = (__x)->windows[__i]->sock;                 \
+ (__sock) = (__x)->conns[__i]->sock;                   \
 } while(0)
 
-/* Returns TRUE if windows is currently active window */
-#define SILC_CLIENT_IS_CURRENT_WIN(__x, __win) ((__x)->current_win == (__win))
-
 /* Prototypes */
-int silc_client_alloc(SilcClient *new_client);
+
+SilcClient silc_client_alloc(SilcClientOperations *ops, void *application);
 void silc_client_free(SilcClient client);
 int silc_client_init(SilcClient client);
 void silc_client_stop(SilcClient client);
 void silc_client_run(SilcClient client);
-void silc_client_parse_command_line(unsigned char *buffer, 
-                                   unsigned char ***parsed,
-                                   unsigned int **parsed_lens,
-                                   unsigned int **parsed_types,
-                                   unsigned int *parsed_num,
-                                   unsigned int max_args);
+SilcClientConnection silc_client_add_connection(SilcClient client,
+                                               void *context);
 int silc_client_connect_to_server(SilcClient client, int port,
-                                 char *host);
+                                 char *host, void *context);
 void silc_client_packet_send(SilcClient client, 
                             SilcSocketConnection sock,
                             SilcPacketType type, 
@@ -280,4 +273,7 @@ void silc_client_receive_channel_key(SilcClient client,
 void silc_client_channel_message(SilcClient client, 
                                 SilcSocketConnection sock, 
                                 SilcPacketContext *packet);
+void silc_client_private_message(SilcClient client, 
+                                SilcSocketConnection sock, 
+                                SilcPacketContext *packet);
 #endif
similarity index 51%
rename from apps/silc/command.c
rename to lib/silcclient/command.c
index c64f44f932ed4719a1fc0be59b7130d1f76bccab..1784d4e8779d376f311e57b50c5dd8bc94150e89 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.11  2000/07/19 09:19:05  priikone
- *     Enhancements to AWAY command.
- *
- * Revision 1.10  2000/07/19 07:06:33  priikone
- *     Added AWAY command.
- *
- * Revision 1.9  2000/07/12 05:56:32  priikone
- *     Major rewrite of ID Cache system. Support added for the new
- *     ID cache system.
- *
- * Revision 1.8  2000/07/10 05:39:11  priikone
- *     Added INFO and VERSION commands. Minor changes to SERVER command
- *     to show current servers when giving without arguments.
- *
- * Revision 1.7  2000/07/07 06:54:44  priikone
- *     Fixed channel joining bug, do not allow joining twice on the
- *     same channel.
- *
- * Revision 1.6  2000/07/06 07:14:36  priikone
- *     Fixes to NAMES command handling.
- *     Fixes when leaving from channel.
- *
- * Revision 1.5  2000/07/05 06:12:05  priikone
- *     Global cosmetic changes.
- *
- * Revision 1.4  2000/07/04 08:28:03  priikone
- *     Added INVITE, PING and NAMES command.
- *
- * Revision 1.3  2000/07/03 05:49:49  priikone
- *     Implemented LEAVE command.  Minor bug fixes.
- *
- * Revision 1.2  2000/06/27 19:38:40  priikone
- *     Added missing goto flag.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
-
-#include "clientincludes.h"
+/* $Id$ */
+
+#include "clientlibincludes.h"
 
 /* Client command list. */
 SilcClientCommand silc_command_list[] =
@@ -98,25 +57,40 @@ SilcClientCommand silc_command_list[] =
   SILC_CLIENT_CMD(leave, LEAVE, "LEAVE", SILC_CF_LAG | SILC_CF_REG, 2),
   SILC_CLIENT_CMD(names, NAMES, "NAMES", SILC_CF_LAG | SILC_CF_REG, 2),
 
-  /*
-   * Local. Client specific commands
-   */
-  SILC_CLIENT_CMD(help, HELP, "HELP", SILC_CF_NONE, 2),
-  SILC_CLIENT_CMD(clear, CLEAR, "CLEAR", SILC_CF_NONE, 1),
-  SILC_CLIENT_CMD(version, VERSION, "VERSION", SILC_CF_NONE, 1),
-  SILC_CLIENT_CMD(server, SERVER, "SERVER", SILC_CF_NONE, 2),
-  SILC_CLIENT_CMD(msg, MSG, "MSG", SILC_CF_NONE, 3),
-  SILC_CLIENT_CMD(away, AWAY, "AWAY", SILC_CF_NONE, 2),
-
-  { NULL, 0, NULL, 0},
+  { NULL, 0, NULL, 0, 0 },
 };
 
-#define SILC_NOT_CONNECTED(x) \
-  silc_say((x), "You are not connected to a server, use /SERVER to connect");
+#define SILC_NOT_CONNECTED(x, c) \
+  x->ops->say((x), (c), \
+          "You are not connected to a server, use /SERVER to connect");
+
+/* Command operation that is called at the end of all commands. 
+   Usage: COMMAND; */
+#define COMMAND cmd->client->ops->command(cmd->client, cmd->conn, \
+  cmd, TRUE, cmd->command->cmd)
+
+/* Error to application. Usage: COMMAND_ERROR; */
+#define COMMAND_ERROR cmd->client->ops->command(cmd->client, cmd->conn, \
+  cmd, FALSE, cmd->command->cmd)
 
 /* List of pending commands. */
 SilcClientCommandPending *silc_command_pending = NULL;
 
+/* Finds and returns a pointer to the command list. Return NULL if the
+   command is not found. */
+
+SilcClientCommand *silc_client_command_find(const char *name)
+{
+  SilcClientCommand *cmd;
+
+  for (cmd = silc_command_list; cmd->name; cmd++) {
+    if (!strcmp(cmd->name, name))
+      return cmd;
+  }
+
+  return NULL;
+}
+
 /* Add new pending command to the list of pending commands. Currently
    pending commands are executed from command replies, thus we can
    execute any command after receiving some specific command reply.
@@ -178,7 +152,7 @@ void silc_client_command_pending_del(SilcCommand reply_cmd)
 
 /* Free command context and its internals */
 
-static void silc_client_command_free(SilcClientCommandContext cmd)
+void silc_client_command_free(SilcClientCommandContext cmd)
 {
   int i;
 
@@ -195,22 +169,26 @@ static void silc_client_command_free(SilcClientCommandContext cmd)
 SILC_CLIENT_CMD_FUNC(whois)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientConnection conn = cmd->conn;
   SilcBuffer buffer;
 
-  if (cmd->argc < 2 || cmd->argc > 3) {
-    silc_say(cmd->client, "Usage: /WHOIS <nickname>[@<server>] [<count>]");
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (cmd->argc < 2 || cmd->argc > 3) {
+    cmd->client->ops->say(cmd->client, conn, 
+            "Usage: /WHOIS <nickname>[@<server>] [<count>]");
+    COMMAND_ERROR;
     goto out;
   }
 
   buffer = silc_command_encode_payload(SILC_COMMAND_WHOIS,
                                       cmd->argc - 1, ++cmd->argv,
                                       ++cmd->argv_lens, ++cmd->argv_types);
-  silc_client_packet_send(cmd->client, cmd->client->current_win->sock,
+  silc_client_packet_send(cmd->client, cmd->conn->sock,
                          SILC_PACKET_COMMAND, NULL, 0, NULL, NULL,
                          buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
@@ -218,6 +196,9 @@ SILC_CLIENT_CMD_FUNC(whois)
   cmd->argv_lens--;
   cmd->argv_types--;
 
+  /* Notify application */
+  COMMAND;
+
  out:
   silc_client_command_free(cmd);
 }
@@ -232,22 +213,26 @@ SILC_CLIENT_CMD_FUNC(whowas)
 SILC_CLIENT_CMD_FUNC(identify)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientConnection conn = cmd->conn;
   SilcBuffer buffer;
 
-  if (cmd->argc < 2 || cmd->argc > 3) {
-    silc_say(cmd->client, "Usage: /IDENTIFY <nickname>[@<server>] [<count>]");
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (cmd->argc < 2 || cmd->argc > 3) {
+    cmd->client->ops->say(cmd->client, conn,
+            "Usage: /IDENTIFY <nickname>[@<server>] [<count>]");
+    COMMAND_ERROR;
     goto out;
   }
 
   buffer = silc_command_encode_payload(SILC_COMMAND_IDENTIFY,
                                       cmd->argc - 1, ++cmd->argv,
                                       ++cmd->argv_lens, ++cmd->argv_types);
-  silc_client_packet_send(cmd->client, cmd->client->current_win->sock,
+  silc_client_packet_send(cmd->client, cmd->conn->sock,
                          SILC_PACKET_COMMAND, NULL, 0, NULL, NULL,
                          buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
@@ -255,6 +240,9 @@ SILC_CLIENT_CMD_FUNC(identify)
   cmd->argv_lens--;
   cmd->argv_types--;
 
+  /* Notify application */
+  COMMAND;
+
  out:
   silc_client_command_free(cmd);
 }
@@ -265,95 +253,47 @@ SILC_CLIENT_CMD_FUNC(identify)
 SILC_CLIENT_CMD_FUNC(nick)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientWindow win = NULL;
+  SilcClientConnection conn = cmd->conn;
   SilcBuffer buffer;
 
-  if (!cmd->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
   /* Show current nickname */
   if (cmd->argc < 2) {
-    if (cmd->sock) {
-      silc_say(cmd->client, "Your nickname is %s on server %s", 
-              win->nickname, win->remote_host);
+    if (cmd->conn) {
+      cmd->client->ops->say(cmd->client, conn, 
+                           "Your nickname is %s on server %s", 
+                           conn->nickname, conn->remote_host);
     } else {
-      silc_say(cmd->client, "Your nickname is %s", win->nickname);
+      cmd->client->ops->say(cmd->client, conn, 
+                           "Your nickname is %s", conn->nickname);
     }
+    /* XXX Notify application */
+    COMMAND;
     goto out;
   }
 
-  win = (SilcClientWindow)cmd->sock->user_data;
-
   /* Set new nickname */
   buffer = silc_command_encode_payload(SILC_COMMAND_NICK,
                                       cmd->argc - 1, ++cmd->argv,
                                       ++cmd->argv_lens, ++cmd->argv_types);
-  silc_client_packet_send(cmd->client, cmd->sock,
+  silc_client_packet_send(cmd->client, cmd->conn->sock,
                          SILC_PACKET_COMMAND, NULL, 0, NULL, NULL,
                          buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
   cmd->argv--;
   cmd->argv_lens--;
   cmd->argv_types--;
-  if (win->nickname)
-    silc_free(win->nickname);
-  win->nickname = strdup(cmd->argv[1]);
-
- out:
-  silc_client_command_free(cmd);
-}
-
-/* Command SERVER. Connects to remote SILC server. This is local command. */
-
-SILC_CLIENT_CMD_FUNC(server)
-{
-  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClient client = cmd->client;
-  int i = 0, len, port;
-  char *hostname;
-
-  if (cmd->argc < 2) {
-    /* Show current servers */
-
-    if (!cmd->client->current_win->sock) {
-      silc_say(cmd->client, "You are not connected to any server");
-      silc_say(cmd->client, "Usage: /SERVER [<server>[:<port>]]");
-      goto out;
-    }
-
-    silc_say(client, "Current server: %s on %d %s", 
-            client->current_win->remote_host,
-            client->current_win->remote_port,
-            client->windows[i]->remote_info ?
-            client->windows[i]->remote_info : "");
-    
-    silc_say(client, "Server list:");
-    for (i = 0; i < client->windows_count; i++) {
-      silc_say(client, " [%d] %s on %d %s", i + 1,
-              client->windows[i]->remote_host,
-              client->windows[i]->remote_port,
-              client->windows[i]->remote_info ?
-              client->windows[i]->remote_info : "");
-    }
-
-    goto out;
-  }
+  if (conn->nickname)
+    silc_free(conn->nickname);
+  conn->nickname = strdup(cmd->argv[1]);
 
-  /* See if port is included and then extract it */
-  if (strchr(cmd->argv[1], ':')) {
-    len = strcspn(cmd->argv[1], ":");
-    hostname = silc_calloc(len + 1, sizeof(char));
-    memcpy(hostname, cmd->argv[1], len);
-    port = atoi(cmd->argv[1] + 1 + len);
-  } else {
-    hostname = cmd->argv[1];
-    port = 706;
-  }
-
-  /* Connect asynchronously to not to block user interface */
-  silc_client_connect_to_server(cmd->client, port, hostname);
+  /* Notify application */
+  COMMAND;
 
  out:
   silc_client_command_free(cmd);
@@ -373,7 +313,7 @@ SILC_CLIENT_CMD_FUNC(invite)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClient client = cmd->client;
-  SilcClientWindow win = NULL;
+  SilcClientConnection conn = cmd->conn;
   SilcClientEntry client_entry;
   SilcChannelEntry channel_entry;
   SilcBuffer buffer;
@@ -381,26 +321,28 @@ SILC_CLIENT_CMD_FUNC(invite)
   char *nickname = NULL, *server = NULL;
   unsigned char *client_id, *channel_id;
 
-  if (cmd->argc != 3) {
-    silc_say(cmd->client, "Usage: /INVITE <nickname>[@<server>] <channel>");
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (cmd->argc != 3) {
+    cmd->client->ops->say(cmd->client, conn,
+                         "Usage: /INVITE <nickname>[@<server>] <channel>");
+    COMMAND_ERROR;
     goto out;
   }
 
-  win = (SilcClientWindow)cmd->sock->user_data;
-
   /* Parse the typed nickname. */
-  if (!silc_client_parse_nickname(cmd->argv[1], &nickname, &server, &num)) {
-    silc_say(cmd->client, "Bad nickname");
+  if (!silc_parse_nickname(cmd->argv[1], &nickname, &server, &num)) {
+    cmd->client->ops->say(cmd->client, conn, "Bad nickname");
+    COMMAND_ERROR;
     goto out;
   }
 
   /* Find client entry */
-  client_entry = silc_idlist_get_client(client, win, nickname, server, num);
+  client_entry = silc_idlist_get_client(client, conn, nickname, server, num);
   if (!client_entry) {
     /* Client entry not found, it was requested thus mark this to be
        pending command. */
@@ -412,10 +354,11 @@ SILC_CLIENT_CMD_FUNC(invite)
   client_id = silc_id_id2str(client_entry->id, SILC_ID_CLIENT);
 
   /* Find channel entry */
-  channel_entry = silc_idlist_get_channel(client, win, cmd->argv[2]);
+  channel_entry = silc_idlist_get_channel(client, conn, cmd->argv[2]);
   if (!channel_entry) {
-    silc_say(cmd->client, "You are not on that channel");
+    cmd->client->ops->say(cmd->client, conn, "You are not on that channel");
     silc_free(client_id);
+    COMMAND_ERROR;
     goto out;
   }
 
@@ -424,12 +367,16 @@ SILC_CLIENT_CMD_FUNC(invite)
   buffer = silc_command_encode_payload_va(SILC_COMMAND_INVITE, 2,
                                          1, client_id, SILC_ID_CLIENT_LEN,
                                          2, channel_id, SILC_ID_CHANNEL_LEN);
-  silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
 
-  silc_say(cmd->client, "Inviting %s to channel %s", cmd->argv[1], 
-          cmd->argv[2]);
+  cmd->client->ops->say(cmd->client, conn, 
+                       "Inviting %s to channel %s", cmd->argv[1], 
+                       cmd->argv[2]);
+
+  /* Notify application */
+  COMMAND;
 
  out:
   silc_client_command_free(cmd);
@@ -442,25 +389,29 @@ SILC_CLIENT_CMD_FUNC(quit)
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcBuffer buffer;
 
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
   buffer = silc_command_encode_payload(SILC_COMMAND_QUIT, cmd->argc - 1, 
                                       ++cmd->argv, ++cmd->argv_lens,
                                       ++cmd->argv_types);
-  silc_client_packet_send(cmd->client, cmd->sock, SILC_PACKET_COMMAND, NULL, 
-                         0, NULL, NULL, buffer->data, buffer->len, TRUE);
+  silc_client_packet_send(cmd->client, cmd->conn->sock, SILC_PACKET_COMMAND, 
+                         NULL, 0, NULL, NULL, 
+                         buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
   cmd->argv--;
   cmd->argv_lens--;
   cmd->argv_types--;
 
   /* Close connection */
-  silc_client_close_connection(cmd->client, cmd->sock);
-  cmd->client->screen->bottom_line->connection = NULL;
-  silc_screen_print_bottom_line(cmd->client->screen, 0);
+  silc_client_close_connection(cmd->client, cmd->conn->sock);
+  cmd->client->ops->disconnect(cmd->client, cmd->conn);
+
+  /* Notify application */
+  COMMAND;
 
  out:
   silc_client_command_free(cmd);
@@ -476,29 +427,31 @@ SILC_CLIENT_CMD_FUNC(kill)
 SILC_CLIENT_CMD_FUNC(info)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientWindow win = NULL;
+  SilcClientConnection conn = cmd->conn;
   SilcBuffer buffer;
   char *name;
 
-  if (!cmd->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
-  win = (SilcClientWindow)cmd->sock->user_data;
-
   if (cmd->argc < 2)
-    name = strdup(win->remote_host);
+    name = strdup(conn->remote_host);
   else
     name = strdup(cmd->argv[1]);
 
   /* Send the command */
   buffer = silc_command_encode_payload_va(SILC_COMMAND_INFO, 1, 
                                          1, name, strlen(name));
-  silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
 
+  /* Notify application */
+  COMMAND;
+
  out:
   silc_client_command_free(cmd);
 }
@@ -513,51 +466,53 @@ SILC_CLIENT_CMD_FUNC(connect)
 SILC_CLIENT_CMD_FUNC(ping)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientWindow win = NULL;
+  SilcClientConnection conn = cmd->conn;
   SilcBuffer buffer;
   void *id;
   int i;
   char *name = NULL;
 
-  if (!cmd->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
-  win = (SilcClientWindow)cmd->sock->user_data;
-
-  if (cmd->argc == 1 || !strcmp(cmd->argv[1], win->remote_host))
-    name = strdup(win->remote_host);
+  if (cmd->argc == 1 || !strcmp(cmd->argv[1], conn->remote_host))
+    name = strdup(conn->remote_host);
 
-  id = silc_id_str2id(win->remote_id_data, SILC_ID_SERVER);
+  id = silc_id_str2id(conn->remote_id_data, SILC_ID_SERVER);
 
   /* Send the command */
   buffer = silc_command_encode_payload_va(SILC_COMMAND_PING, 1, 
-                                         1, win->remote_id_data, 
+                                         1, conn->remote_id_data, 
                                          SILC_ID_SERVER_LEN);
-  silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
 
   /* Start counting time */
-  for (i = 0; i < win->ping_count; i++) {
-    if (win->ping[i].dest_id == NULL) {
-      win->ping[i].start_time = time(NULL);
-      win->ping[i].dest_id = id;
-      win->ping[i].dest_name = name;
-      win->ping_count++;
+  for (i = 0; i < conn->ping_count; i++) {
+    if (conn->ping[i].dest_id == NULL) {
+      conn->ping[i].start_time = time(NULL);
+      conn->ping[i].dest_id = id;
+      conn->ping[i].dest_name = name;
+      conn->ping_count++;
       break;
     }
   }
-  if (i >= win->ping_count) {
-    i = win->ping_count;
-    win->ping = silc_realloc(win->ping, sizeof(*win->ping) * (i + 1));
-    win->ping[i].start_time = time(NULL);
-    win->ping[i].dest_id = id;
-    win->ping[i].dest_name = name;
-    win->ping_count++;
+  if (i >= conn->ping_count) {
+    i = conn->ping_count;
+    conn->ping = silc_realloc(conn->ping, sizeof(*conn->ping) * (i + 1));
+    conn->ping[i].start_time = time(NULL);
+    conn->ping[i].dest_id = id;
+    conn->ping[i].dest_name = name;
+    conn->ping_count++;
   }
   
+  /* Notify application */
+  COMMAND;
+
  out:
   silc_client_command_free(cmd);
 }
@@ -579,36 +534,32 @@ SILC_CLIENT_CMD_FUNC(notice)
 SILC_CLIENT_CMD_FUNC(join)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientWindow win = NULL;
+  SilcClientConnection conn = cmd->conn;
   SilcIDCacheEntry id_cache = NULL;
   SilcBuffer buffer;
 
-  if (cmd->argc < 2) {
-    /* Show channels currently joined to */
-    if (!cmd->client->current_win->sock) {
-      silc_say(cmd->client, "No current channel for this window");
-      SILC_NOT_CONNECTED(cmd->client);
-      goto out;
-
-    }
-
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (cmd->argc < 2) {
+    /* Show channels currently joined to */
+
     goto out;
   }
 
-  win = (SilcClientWindow)cmd->sock->user_data;
-
   /* See if we have joined to the requested channel already */
-  if (silc_idcache_find_by_data_one(win->channel_cache, cmd->argv[1],
+  if (silc_idcache_find_by_data_one(conn->channel_cache, cmd->argv[1],
                                    &id_cache)) {
-    silc_say(cmd->client, "You are talking to channel %s", cmd->argv[1]);
-    win->current_channel = (SilcChannelEntry)id_cache->context;
+    cmd->client->ops->say(cmd->client, conn, 
+                         "You are talking to channel %s", cmd->argv[1]);
+    conn->current_channel = (SilcChannelEntry)id_cache->context;
+#if 0
     cmd->client->screen->bottom_line->channel = cmd->argv[1];
     silc_screen_print_bottom_line(cmd->client->screen, 0);
+#endif
     goto out;
   }
 
@@ -616,13 +567,16 @@ SILC_CLIENT_CMD_FUNC(join)
   buffer = silc_command_encode_payload(SILC_COMMAND_JOIN,
                                       cmd->argc - 1, ++cmd->argv,
                                       ++cmd->argv_lens, ++cmd->argv_types);
-  silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
   cmd->argv--;
   cmd->argv_lens--;
   cmd->argv_types--;
 
+  /* Notify application */
+  COMMAND;
+
  out:
   silc_client_command_free(cmd);
 }
@@ -664,43 +618,46 @@ SILC_CLIENT_CMD_FUNC(silcoper)
 SILC_CLIENT_CMD_FUNC(leave)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientWindow win = NULL;
+  SilcClientConnection conn = cmd->conn;
   SilcIDCacheEntry id_cache = NULL;
   SilcChannelEntry channel;
   SilcBuffer buffer;
   unsigned char *id_string;
   char *name;
 
-  if (cmd->argc != 2) {
-    silc_say(cmd->client, "Usage: /LEAVE <channel>");
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (cmd->argc != 2) {
+    cmd->client->ops->say(cmd->client, conn, "Usage: /LEAVE <channel>");
+    COMMAND_ERROR;
     goto out;
   }
 
-  win = (SilcClientWindow)cmd->sock->user_data;
-
   if (cmd->argv[1][0] == '*') {
-    if (!win->current_channel) {
-      silc_say(cmd->client, "You are not on any chanenl");
+    if (!conn->current_channel) {
+      cmd->client->ops->say(cmd->client, conn, "You are not on any chanenl");
+      COMMAND_ERROR;
       goto out;
     }
-    name = win->current_channel->channel_name;
+    name = conn->current_channel->channel_name;
   } else {
     name = cmd->argv[1];
   }
 
-  if (!win->current_channel) {
-    silc_say(cmd->client, "You are not on that channel");
+  if (!conn->current_channel) {
+    cmd->client->ops->say(cmd->client, conn, "You are not on that channel");
+    COMMAND_ERROR;
     goto out;
   }
 
   /* Get the Channel ID of the channel */
-  if (!silc_idcache_find_by_data_one(win->channel_cache, name, &id_cache)) {
-    silc_say(cmd->client, "You are not on that channel");
+  if (!silc_idcache_find_by_data_one(conn->channel_cache, name, &id_cache)) {
+    cmd->client->ops->say(cmd->client, conn, "You are not on that channel");
+    COMMAND_ERROR;
     goto out;
   }
 
@@ -710,20 +667,16 @@ SILC_CLIENT_CMD_FUNC(leave)
   id_string = silc_id_id2str(id_cache->id, SILC_ID_CHANNEL);
   buffer = silc_command_encode_payload_va(SILC_COMMAND_LEAVE, 1, 
                                          1, id_string, SILC_ID_CHANNEL_LEN);
-  silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
 
   /* We won't talk anymore on this channel */
-  silc_say(cmd->client, "You have left channel %s", name);
+  cmd->client->ops->say(cmd->client, conn, "You have left channel %s", name);
 
-  if (!strncmp(win->current_channel->channel_name, name, strlen(name))) {
-    cmd->client->screen->bottom_line->channel = NULL;
-    silc_screen_print_bottom_line(cmd->client->screen, 0);
-    win->current_channel = NULL;
-  }
+  conn->current_channel = NULL;
 
-  silc_idcache_del_by_id(win->channel_cache, SILC_ID_CHANNEL, channel->id);
+  silc_idcache_del_by_id(conn->channel_cache, SILC_ID_CHANNEL, channel->id);
   silc_free(channel->channel_name);
   silc_free(channel->id);
   silc_free(channel->key);
@@ -731,6 +684,9 @@ SILC_CLIENT_CMD_FUNC(leave)
   silc_free(channel);
   silc_free(id_string);
 
+  /* Notify application */
+  COMMAND;
+
  out:
   silc_client_command_free(cmd);
 }
@@ -741,33 +697,35 @@ SILC_CLIENT_CMD_FUNC(leave)
 SILC_CLIENT_CMD_FUNC(names)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientWindow win = NULL;
+  SilcClientConnection conn = cmd->conn;
   SilcIDCacheEntry id_cache = NULL;
   SilcBuffer buffer;
   char *name;
   unsigned char *id_string;
 
-  if (cmd->argc != 2) {
-    silc_say(cmd->client, "Usage: /NAMES <channel>");
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
     goto out;
   }
 
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
+  if (cmd->argc != 2) {
+    cmd->client->ops->say(cmd->client, conn, "Usage: /NAMES <channel>");
+    COMMAND_ERROR;
     goto out;
   }
 
-  win = (SilcClientWindow)cmd->sock->user_data;
-
   if (cmd->argv[1][0] == '*')
-    name = win->current_channel->channel_name;
+    name = conn->current_channel->channel_name;
   else
     name = cmd->argv[1];
 
   /* Get the Channel ID of the channel */
-  if (!silc_idcache_find_by_data_one(win->channel_cache, name, &id_cache)) {
+  if (!silc_idcache_find_by_data_one(conn->channel_cache, name, &id_cache)) {
     /* XXX should resolve the channel ID; LIST command */
-    silc_say(cmd->client, "You are not on that channel", name);
+    cmd->client->ops->say(cmd->client, conn, 
+                         "You are not on that channel", name);
+    COMMAND_ERROR;
     goto out;
   }
 
@@ -775,7 +733,7 @@ SILC_CLIENT_CMD_FUNC(names)
   id_string = silc_id_id2str(id_cache->id, SILC_ID_CHANNEL);
   buffer = silc_command_encode_payload_va(SILC_COMMAND_NAMES, 1, 
                                          1, id_string, SILC_ID_CHANNEL_LEN);
-  silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
   silc_free(id_string);
@@ -791,146 +749,8 @@ SILC_CLIENT_CMD_FUNC(names)
   silc_client_command_pending(SILC_COMMAND_NAMES, 
                              silc_client_command_names, NULL);
 
- out:
-  silc_client_command_free(cmd);
-}
-
-/*
- * Local commands
- */
-
-/* HELP command. This is local command and shows help on SILC */
-
-SILC_CLIENT_CMD_FUNC(help)
-{
-
-}
-
-/* CLEAR command. This is local command and clears current output window */
-
-SILC_CLIENT_CMD_FUNC(clear)
-{
-  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClient client = cmd->client;
-
-  assert(client->current_win != NULL);
-  wclear((WINDOW *)client->current_win->screen);
-  wrefresh((WINDOW *)client->current_win->screen);
-
-  silc_client_command_free(cmd);
-}
-
-/* VERSION command. This is local command and shows version of the client */
-
-SILC_CLIENT_CMD_FUNC(version)
-{
-  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClient client = cmd->client;
-  extern char *silc_version;
-  extern char *silc_name;
-  extern char *silc_fullname;
-
-  silc_say(client, "%s (%s) version %s", silc_name, silc_fullname,
-          silc_version);
-
-  silc_client_command_free(cmd);
-}
-
-/* Command MSG. Sends private message to user or list of users. Note that
-   private messages are not really commands, they are message packets,
-   however, on user interface it is convenient to show them as commands
-   as that is the common way of sending private messages (like in IRC). */
-/* XXX supports only one destination */
-
-SILC_CLIENT_CMD_FUNC(msg)
-{
-  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientWindow win = NULL;
-  SilcClient client = cmd->client;
-  SilcClientEntry client_entry = NULL;
-  unsigned int num = 0;
-  char *nickname = NULL, *server = NULL;
-
-  if (cmd->argc < 3) {
-    silc_say(cmd->client, "Usage: /MSG <nickname> <message>");
-    goto out;
-  }
-
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
-    goto out;
-  }
-
-  win = (SilcClientWindow)cmd->sock->user_data;
-
-  /* Parse the typed nickname. */
-  if (!silc_client_parse_nickname(cmd->argv[1], &nickname, &server, &num)) {
-    silc_say(cmd->client, "Bad nickname");
-    goto out;
-  }
-
-  /* Find client entry */
-  client_entry = silc_idlist_get_client(client, win, nickname, server, num);
-  if (!client_entry) {
-    /* Client entry not found, it was requested thus mark this to be
-       pending command. */
-    silc_client_command_pending(SILC_COMMAND_IDENTIFY, 
-                               silc_client_command_msg, context);
-    return;
-  }
-
-  /* Display the message for our eyes. */
-  silc_print(client, "-> *%s* %s", cmd->argv[1], cmd->argv[2]);
-
-  /* Send the private message */
-  silc_client_packet_send_private_message(client, cmd->sock, client_entry,
-                                         cmd->argv[2], cmd->argv_lens[2],
-                                         TRUE);
-
- out:
-  silc_client_command_free(cmd);
-}
-
-/* Local command AWAY. Client replies with away message to whomever sends
-   private message to the client if the away message is set. If this is
-   given without arguments the away message is removed. */
-
-SILC_CLIENT_CMD_FUNC(away)
-{
-  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientWindow win = NULL;
-  SilcClient client = cmd->client;
-
-  if (!cmd->client->current_win->sock) {
-    SILC_NOT_CONNECTED(cmd->client);
-    goto out;
-  }
-
-  win = (SilcClientWindow)cmd->sock->user_data;
-
-  if (cmd->argc == 1) {
-    if (win->away) {
-      silc_free(win->away->away);
-      silc_free(win->away);
-      win->away = NULL;
-      client->screen->bottom_line->away = FALSE;
-
-      silc_say(client, "Away message removed");
-      silc_screen_print_bottom_line(cmd->client->screen, 0);
-    }
-  } else {
-
-    if (win->away)
-      silc_free(win->away->away);
-    else
-      win->away = silc_calloc(1, sizeof(*win->away));
-    
-    client->screen->bottom_line->away = TRUE;
-    win->away->away = strdup(cmd->argv[1]);
-
-    silc_say(client, "Away message set: %s", win->away->away);
-    silc_screen_print_bottom_line(cmd->client->screen, 0);
-  }
+  /* Notify application */
+  COMMAND;
 
  out:
   silc_client_command_free(cmd);
similarity index 95%
rename from apps/silc/command.h
rename to lib/silcclient/command.h
index 19db809b936484e47199e6a98002ca3dbce81cfb..edb714505f12fec5e244134fa14487939455a3c7 100644 (file)
@@ -66,7 +66,8 @@ typedef void (*SilcClientCommandCallback)(void *context);
 /* Context sent as argument to all commands */
 typedef struct {
   SilcClient client;
-  SilcSocketConnection sock;
+  SilcClientConnection conn;
+  SilcClientCommand *command;
   unsigned int argc;
   unsigned char **argv;
   unsigned int *argv_lens;
@@ -126,6 +127,8 @@ do {                                                        \
 } while(0)
 
 /* Prototypes */
+void silc_client_command_free(SilcClientCommandContext cmd);
+SilcClientCommand *silc_client_command_find(const char *name);
 void silc_client_command_pending(SilcCommand reply_cmd,
                                 SilcClientCommandCallback callback,
                                 void *context);
@@ -134,7 +137,6 @@ SILC_CLIENT_CMD_FUNC(whois);
 SILC_CLIENT_CMD_FUNC(whowas);
 SILC_CLIENT_CMD_FUNC(identify);
 SILC_CLIENT_CMD_FUNC(nick);
-SILC_CLIENT_CMD_FUNC(server);
 SILC_CLIENT_CMD_FUNC(list);
 SILC_CLIENT_CMD_FUNC(topic);
 SILC_CLIENT_CMD_FUNC(invite);
@@ -155,10 +157,5 @@ SILC_CLIENT_CMD_FUNC(die);
 SILC_CLIENT_CMD_FUNC(silcoper);
 SILC_CLIENT_CMD_FUNC(leave);
 SILC_CLIENT_CMD_FUNC(names);
-SILC_CLIENT_CMD_FUNC(help);
-SILC_CLIENT_CMD_FUNC(clear);
-SILC_CLIENT_CMD_FUNC(version);
-SILC_CLIENT_CMD_FUNC(msg);
-SILC_CLIENT_CMD_FUNC(away);
 
 #endif
similarity index 73%
rename from apps/silc/command_reply.c
rename to lib/silcclient/command_reply.c
index 8d78b329df65c3a48931b1b88298ed24a892d372..604389376afaa09e6ac221de992ab4d4e297ddff 100644 (file)
  * Command reply functions are "the otherside" of the command functions.
  * Reply to a command sent by server is handled by these functions.
  */
-/*
- * $Id$
- * $Log$
- * Revision 1.9  2000/07/19 09:19:05  priikone
- *     Enhancements to AWAY command.
- *
- * Revision 1.8  2000/07/19 07:06:51  priikone
- *     Added AWAY command.
- *
- * Revision 1.7  2000/07/12 05:56:32  priikone
- *     Major rewrite of ID Cache system. Support added for the new
- *     ID cache system.
- *
- * Revision 1.6  2000/07/10 05:38:32  priikone
- *     Added INFO command.
- *
- * Revision 1.5  2000/07/06 07:14:36  priikone
- *     Fixes to NAMES command handling.
- *     Fixes when leaving from channel.
- *
- * Revision 1.4  2000/07/05 06:12:34  priikone
- *     Tweaked NAMES command reply for better. Should now show users
- *     joined to a channel.
- *
- * Revision 1.3  2000/07/04 08:27:14  priikone
- *     Changes to LEAVE command -- more consistent now and does error
- *     handling better. Added INVITE, PING and part of NAMES commands.
- *     SilcPacketContext is included into command structure.
- *
- * Revision 1.2  2000/07/03 05:49:49  priikone
- *     Implemented LEAVE command.  Minor bug fixes.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
-#include "clientincludes.h"
+#include "clientlibincludes.h"
 
 /* Client command reply list. */
 SilcClientCommandReply silc_command_reply_list[] =
@@ -74,7 +38,6 @@ SilcClientCommandReply silc_command_reply_list[] =
   SILC_CLIENT_CMD_REPLY(quit, QUIT),
   SILC_CLIENT_CMD_REPLY(kill, KILL),
   SILC_CLIENT_CMD_REPLY(info, INFO),
-  SILC_CLIENT_CMD_REPLY(away, AWAY),
   SILC_CLIENT_CMD_REPLY(connect, CONNECT),
   SILC_CLIENT_CMD_REPLY(ping, PING),
   SILC_CLIENT_CMD_REPLY(oper, OPER),
@@ -141,6 +104,16 @@ const SilcCommandStatusMessage silc_command_status_messages[] = {
   { 0, NULL }
 };
 
+/* Command reply operation that is called at the end of all command replys. 
+   Usage: COMMAND_REPLY((ARGS, argument1, argument2, etc...)), */
+#define COMMAND_REPLY(args) cmd->client->ops->command_reply args
+#define ARGS cmd->client, cmd->sock->user_data, \
+             cmd->payload, TRUE, silc_command_get(cmd->payload)
+
+/* Error reply to application. Usage: COMMAND_REPLY_ERROR; */
+#define COMMAND_REPLY_ERROR cmd->client->ops->command_reply(cmd->client, \
+  cmd->sock->user_data, cmd->payload, FALSE, silc_command_get(cmd->payload))
+
 /* Process received command reply. */
 
 void silc_client_command_reply_process(SilcClient client,
@@ -204,10 +177,12 @@ void silc_client_command_reply_free(SilcClientCommandReplyContext cmd)
 
 /* Received reply for WHOIS command. This maybe called several times
    for one WHOIS command as server may reply with list of results. */
+/* Sends to application: (no arguments) */
 
 SILC_CLIENT_CMD_REPLY_FUNC(whois)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcCommandStatus status;
   unsigned char *tmp;
 
@@ -220,14 +195,17 @@ SILC_CLIENT_CMD_REPLY_FUNC(whois)
       /* Take nickname which may be provided */
       tmp = silc_command_get_arg_type(cmd->payload, 3, NULL);
       if (tmp)
-       silc_say(cmd->client, "%s: %s", tmp,
+       cmd->client->ops->say(cmd->client, conn, "%s: %s", tmp,
                 silc_client_command_status_message(status));
       else
-       silc_say(cmd->client, "%s",
+       cmd->client->ops->say(cmd->client, conn, "%s",
                 silc_client_command_status_message(status));
+      COMMAND_REPLY_ERROR;
       goto out;
     } else {
-      silc_say(cmd->client, "%s", silc_client_command_status_message(status));
+      cmd->client->ops->say(cmd->client, conn,
+              "%s", silc_client_command_status_message(status));
+      COMMAND_REPLY_ERROR;
       goto out;
     }
   }
@@ -267,16 +245,19 @@ SILC_CLIENT_CMD_REPLY_FUNC(whois)
     /* Save received Client ID to ID cache */
     /* XXX Maybe should not be saved as /MSG will get confused */
     id = silc_id_str2id(id_data, SILC_ID_CLIENT);
-    client->current_win->client_id_cache_count[(int)nickname[0] - 32] =
-    silc_idcache_add(&client->current_win->
+    client->current_conn->client_id_cache_count[(int)nickname[0] - 32] =
+    silc_idcache_add(&client->current_conn->
                     client_id_cache[(int)nickname[0] - 32],
-                    client->current_win->
+                    client->current_conn->
                     client_id_cache_count[(int)nickname[0] - 32],
                     strdup(nickname), SILC_ID_CLIENT, id, NULL);
 #endif
 
-    silc_say(cmd->client, "%s", buf);
-   }
+    cmd->client->ops->say(cmd->client, conn, "%s", buf);
+
+    /* Notify application */
+    COMMAND_REPLY((ARGS));
+  }
 
   if (status == SILC_STATUS_LIST_START) {
 
@@ -303,7 +284,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(whowas)
 SILC_CLIENT_CMD_REPLY_FUNC(identify)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
-  SilcClientWindow win = (SilcClientWindow)cmd->sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcClientEntry client_entry;
   SilcCommandStatus status;
   unsigned char *tmp;
@@ -317,14 +298,17 @@ SILC_CLIENT_CMD_REPLY_FUNC(identify)
       /* Take nickname which may be provided */
       tmp = silc_command_get_arg_type(cmd->payload, 3, NULL);
       if (tmp)
-       silc_say(cmd->client, "%s: %s", tmp,
+       cmd->client->ops->say(cmd->client, conn, "%s: %s", tmp,
                 silc_client_command_status_message(status));
       else
-       silc_say(cmd->client, "%s",
+       cmd->client->ops->say(cmd->client, conn, "%s",
                 silc_client_command_status_message(status));
+      COMMAND_REPLY_ERROR;
       goto out;
     } else {
-      silc_say(cmd->client, "%s", silc_client_command_status_message(status));
+      cmd->client->ops->say(cmd->client, conn,
+              "%s", silc_client_command_status_message(status));
+      COMMAND_REPLY_ERROR;
       goto out;
     }
   }
@@ -343,7 +327,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(identify)
     client_entry->nickname = strdup(nickname);
 
     /* Save received Client ID to ID cache */
-    silc_idcache_add(win->client_cache, client_entry->nickname,
+    silc_idcache_add(conn->client_cache, client_entry->nickname,
                     SILC_ID_CLIENT, client_entry->id, client_entry, TRUE);
   }
 
@@ -363,11 +347,12 @@ SILC_CLIENT_CMD_REPLY_FUNC(identify)
 
 /* Received reply for command NICK. If everything went without errors
    we just received our new Client ID. */
+/* Sends to application: char * (nickname). */
 
 SILC_CLIENT_CMD_REPLY_FUNC(nick)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
-  SilcClientWindow win = (SilcClientWindow)cmd->sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcCommandStatus status;
   unsigned char *tmp, *id_string;
   int argc;
@@ -377,14 +362,17 @@ SILC_CLIENT_CMD_REPLY_FUNC(nick)
   tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
   SILC_GET16_MSB(status, tmp);
   if (status != SILC_STATUS_OK) {
-    silc_say(cmd->client, "Cannot set nickname: %s", 
+    cmd->client->ops->say(cmd->client, conn, "Cannot set nickname: %s", 
             silc_client_command_status_message(status));
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
   argc = silc_command_get_arg_num(cmd->payload);
   if (argc < 2 || argc > 2) {
-    silc_say(cmd->client, "Cannot set nickname: bad reply to command");
+    cmd->client->ops->say(cmd->client, conn, 
+                         "Cannot set nickname: bad reply to command");
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
@@ -392,9 +380,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(nick)
   id_string = silc_command_get_arg_type(cmd->payload, 2, NULL);
   silc_client_receive_new_id(cmd->client, cmd->sock, id_string);
 
-  /* Update nickname on screen */
-  cmd->client->screen->bottom_line->nickname = win->nickname;
-  silc_screen_print_bottom_line(cmd->client->screen, 0);
+  /* Notify application */
+  COMMAND_REPLY((ARGS, conn->nickname));
 
  out:
   silc_client_command_reply_free(cmd);
@@ -409,21 +396,28 @@ SILC_CLIENT_CMD_REPLY_FUNC(topic)
 }
 
 /* Received reply to invite command. */
+/* Sends to application: (no arguments) */
 
 SILC_CLIENT_CMD_REPLY_FUNC(invite)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcCommandStatus status;
   unsigned char *tmp;
 
   tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
   SILC_GET16_MSB(status, tmp);
   if (status != SILC_STATUS_OK) {
-    silc_say(cmd->client, "%s", silc_client_command_status_message(status));
+    cmd->client->ops->say(cmd->client, conn,
+            "%s", silc_client_command_status_message(status));
     silc_client_command_reply_free(cmd);
+    COMMAND_REPLY_ERROR;
     return;
   }
 
+  /* Notify application */
+  COMMAND_REPLY((ARGS));
+
   silc_client_command_reply_free(cmd);
 }
  
@@ -437,10 +431,12 @@ SILC_CLIENT_CMD_REPLY_FUNC(kill)
 
 /* Received reply to INFO command. We receive the server ID and some
    information about the server user requested. */
+/* Sends to application: char * (server information) */
 
 SILC_CLIENT_CMD_REPLY_FUNC(info)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcClient client = cmd->client;
   SilcCommandStatus status;
   unsigned char *tmp;
@@ -448,8 +444,10 @@ SILC_CLIENT_CMD_REPLY_FUNC(info)
   tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
   SILC_GET16_MSB(status, tmp);
   if (status != SILC_STATUS_OK) {
-    silc_say(cmd->client, "%s", silc_client_command_status_message(status));
+    cmd->client->ops->say(cmd->client, conn,
+            "%s", silc_client_command_status_message(status));
     silc_client_command_reply_free(cmd);
+    COMMAND_REPLY_ERROR;
     return;
   }
 
@@ -465,26 +463,26 @@ SILC_CLIENT_CMD_REPLY_FUNC(info)
   if (!tmp)
     goto out;
 
-  silc_say(client, "Info: %s", tmp);
+  client->ops->say(cmd->client, conn, "Info: %s", tmp);
+
+  /* Notify application */
+  COMMAND_REPLY((ARGS, (char *)tmp));
 
  out:
   silc_client_command_reply_free(cmd);
 }
 
-SILC_CLIENT_CMD_REPLY_FUNC(away)
-{
-}
-
 SILC_CLIENT_CMD_REPLY_FUNC(connect)
 {
 }
 
 /* Received reply to PING command. The reply time is shown to user. */
+/* Sends to application: (no arguments) */
 
 SILC_CLIENT_CMD_REPLY_FUNC(ping)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
-  SilcClientWindow win = (SilcClientWindow)cmd->sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcCommandStatus status;
   void *id;
   char *tmp;
@@ -494,24 +492,31 @@ SILC_CLIENT_CMD_REPLY_FUNC(ping)
   tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
   SILC_GET16_MSB(status, tmp);
   if (status != SILC_STATUS_OK) {
-    silc_say(cmd->client, "%s", silc_client_command_status_message(status));
+    cmd->client->ops->say(cmd->client, conn,
+            "%s", silc_client_command_status_message(status));
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
   curtime = time(NULL);
   id = silc_id_str2id(cmd->packet->src_id, cmd->packet->src_id_type);
 
-  for (i = 0; i < win->ping_count; i++) {
-    if (!SILC_ID_SERVER_COMPARE(win->ping[i].dest_id, id)) {
-      diff = curtime - win->ping[i].start_time;
-      silc_say(cmd->client, "Ping reply from %s: %d second%s", 
-              win->ping[i].dest_name, diff, diff == 1 ? "" : "s");
-
-      win->ping[i].start_time = 0;
-      silc_free(win->ping[i].dest_id);
-      win->ping[i].dest_id = NULL;
-      silc_free(win->ping[i].dest_name);
-      win->ping[i].dest_name = NULL;
+  for (i = 0; i < conn->ping_count; i++) {
+    if (!SILC_ID_SERVER_COMPARE(conn->ping[i].dest_id, id)) {
+      diff = curtime - conn->ping[i].start_time;
+      cmd->client->ops->say(cmd->client, conn, 
+                           "Ping reply from %s: %d second%s", 
+                           conn->ping[i].dest_name, diff, 
+                           diff == 1 ? "" : "s");
+
+      conn->ping[i].start_time = 0;
+      silc_free(conn->ping[i].dest_id);
+      conn->ping[i].dest_id = NULL;
+      silc_free(conn->ping[i].dest_name);
+      conn->ping[i].dest_name = NULL;
+
+      /* Notify application */
+      COMMAND_REPLY((ARGS));
       goto out;
     }
   }
@@ -529,6 +534,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(oper)
 SILC_CLIENT_CMD_REPLY_FUNC(join)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcClient client = cmd->client;
   SilcCommandStatus status;
   unsigned int argc, mode;
@@ -540,20 +546,26 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
   SILC_GET16_MSB(status, tmp);
   if (status != SILC_STATUS_OK) {
-    silc_say(cmd->client, "%s", silc_client_command_status_message(status));
+    cmd->client->ops->say(cmd->client, conn,
+            "%s", silc_client_command_status_message(status));
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
   argc = silc_command_get_arg_num(cmd->payload);
   if (argc < 3 || argc > 4) {
-    silc_say(cmd->client, "Cannot join channel: Bad reply packet");
+    cmd->client->ops->say(cmd->client, conn,
+            "Cannot join channel: Bad reply packet");
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
   /* Get channel name */
   tmp = silc_command_get_arg_type(cmd->payload, 2, NULL);
   if (!tmp) {
-    silc_say(cmd->client, "Cannot join channel: Bad reply packet");
+    cmd->client->ops->say(cmd->client, conn, 
+                         "Cannot join channel: Bad reply packet");
+    COMMAND_REPLY_ERROR;
     goto out;
   }
   channel_name = strdup(tmp);
@@ -561,7 +573,9 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   /* Get Channel ID */
   id_string = silc_command_get_arg_type(cmd->payload, 3, NULL);
   if (!id_string) {
-    silc_say(cmd->client, "Cannot join channel: Bad reply packet");
+    cmd->client->ops->say(cmd->client, conn, 
+                         "Cannot join channel: Bad reply packet");
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
@@ -579,12 +593,12 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   silc_client_new_channel_id(cmd->client, cmd->sock, channel_name, 
                             mode, id_string);
 
-  /* Print channel name on screen */
-  client->screen->bottom_line->channel = channel_name;
-  silc_screen_print_bottom_line(client->screen, 0);
-
   if (topic)
-    silc_say(client, "Topic for %s: %s", channel_name, topic);
+    client->ops->say(cmd->client, conn, 
+                    "Topic for %s: %s", channel_name, topic);
+
+  /* Notify application */
+  COMMAND_REPLY((ARGS, channel_name, topic));
 
  out:
   silc_client_command_reply_free(cmd);
@@ -623,17 +637,26 @@ SILC_CLIENT_CMD_REPLY_FUNC(silcoper)
 }
 
 /* Reply to LEAVE command. */
+/* Sends to application: (no arguments) */
 
 SILC_CLIENT_CMD_REPLY_FUNC(leave)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcCommandStatus status;
   unsigned char *tmp;
 
   tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
   SILC_GET16_MSB(status, tmp);
-  if (status != SILC_STATUS_OK)
-    silc_say(cmd->client, "%s", silc_client_command_status_message(status));
+  if (status != SILC_STATUS_OK) {
+    cmd->client->ops->say(cmd->client, conn,
+            "%s", silc_client_command_status_message(status));
+    COMMAND_REPLY_ERROR;
+    return;
+  }
+
+  /* Notify application */
+  COMMAND_REPLY((ARGS));
 
   silc_client_command_reply_free(cmd);
 }
@@ -644,7 +667,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(leave)
 SILC_CLIENT_CMD_REPLY_FUNC(names)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
-  SilcClientWindow win = (SilcClientWindow)cmd->sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcCommandStatus status;
   SilcIDCacheEntry id_cache = NULL;
   SilcChannelEntry channel;
@@ -659,14 +682,18 @@ SILC_CLIENT_CMD_REPLY_FUNC(names)
   tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
   SILC_GET16_MSB(status, tmp);
   if (status != SILC_STATUS_OK) {
-    silc_say(cmd->client, "%s", silc_client_command_status_message(status));
+    cmd->client->ops->say(cmd->client, conn,
+            "%s", silc_client_command_status_message(status));
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
   /* Get channel ID */
   tmp = silc_command_get_arg_type(cmd->payload, 2, NULL);
   if (!tmp) {
-    silc_say(cmd->client, "Cannot get user list: Bad reply packet");
+    cmd->client->ops->say(cmd->client, conn, 
+                         "Cannot get user list: Bad reply packet");
+    COMMAND_REPLY_ERROR;
     goto out;
   }
   channel_id = silc_id_str2id(tmp, SILC_ID_CHANNEL);
@@ -674,14 +701,18 @@ SILC_CLIENT_CMD_REPLY_FUNC(names)
   /* Get the name list of the channel */
   name_list = silc_command_get_arg_type(cmd->payload, 3, &len1);
   if (!name_list) {
-    silc_say(cmd->client, "Cannot get user list: Bad reply packet");
+    cmd->client->ops->say(cmd->client, conn, 
+                         "Cannot get user list: Bad reply packet");
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
   /* Get Client ID list */
   tmp = silc_command_get_arg_type(cmd->payload, 4, &len2);
   if (!tmp) {
-    silc_say(cmd->client, "Cannot get user list: Bad reply packet");
+    cmd->client->ops->say(cmd->client, conn, 
+                         "Cannot get user list: Bad reply packet");
+    COMMAND_REPLY_ERROR;
     goto out;
   }
 
@@ -690,9 +721,11 @@ SILC_CLIENT_CMD_REPLY_FUNC(names)
   silc_buffer_put(client_id_list, tmp, len2);
 
   /* Get the channel name */
-  if (!silc_idcache_find_by_id_one(win->channel_cache, (void *)channel_id,
-                                  SILC_ID_CHANNEL, &id_cache))
+  if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
+                                  SILC_ID_CHANNEL, &id_cache)) {
+    COMMAND_REPLY_ERROR;
     goto out;
+  }
   
   channel = (SilcChannelEntry)id_cache->context;
 
@@ -719,7 +752,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(names)
        list_count++;
       }
 
-    silc_say(cmd->client, "Users on %s: %s", channel->channel_name, name_list);
+    cmd->client->ops->say(cmd->client, conn,
+                         "Users on %s: %s", channel->channel_name, name_list);
   }
 
   /* Cache the received name list and client ID's. This cache expires
@@ -732,14 +766,14 @@ SILC_CLIENT_CMD_REPLY_FUNC(names)
     SilcClientEntry client;
 
     memcpy(nickname, name_list, nick_len);
-    client_id = silc_id_str2id(client_id_list->data, SILC_ID_CLIENT_LEN);
+    client_id = silc_id_str2id(client_id_list->data, SILC_ID_CLIENT);
     silc_buffer_pull(client_id_list, SILC_ID_CLIENT_LEN);
 
     client = silc_calloc(1, sizeof(*client));
     client->id = client_id;
     client->nickname = nickname;
 
-    silc_idcache_add(win->client_cache, nickname, SILC_ID_CLIENT,
+    silc_idcache_add(conn->client_cache, nickname, SILC_ID_CLIENT,
                     client_id, (void *)client, TRUE);
     name_list = name_list + nick_len + 1;
   }
@@ -751,74 +785,3 @@ SILC_CLIENT_CMD_REPLY_FUNC(names)
     silc_free(channel_id);
   silc_client_command_reply_free(cmd);
 }
-
-/* Private message received. This processes the private message and
-   finally displays it on the screen. Note that private messages are not
-   really commands except on user interface which is reason why this
-   handling resides here. */
-
-SILC_CLIENT_CMD_REPLY_FUNC(msg)
-{
-  SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
-  SilcClient client = cmd->client;
-  SilcClientWindow win = (SilcClientWindow)cmd->sock->user_data;
-  SilcBuffer buffer = (SilcBuffer)cmd->packet->buffer;
-  unsigned short nick_len;
-  unsigned char *nickname, *message;
-
-  /* Get nickname */
-  silc_buffer_unformat(buffer, 
-                      SILC_STR_UI16_NSTRING_ALLOC(&nickname, &nick_len),
-                      SILC_STR_END);
-  silc_buffer_pull(buffer, 2 + nick_len);
-     
-  message = silc_calloc(buffer->len + 1, sizeof(char));
-  memcpy(message, buffer->data, buffer->len);
-  silc_print(client, "*%s* %s", nickname, message);
-
-  /* See if we are away (gone). If we are away we will reply to the
-     sender with the set away message. */
-  if (win->away && win->away->away) {
-    SilcClientID *remote_id;
-    SilcClientEntry remote_client;
-    SilcIDCacheEntry id_cache;
-
-    if (cmd->packet->src_id_type != SILC_ID_CLIENT)
-      goto out;
-
-    remote_id = silc_id_str2id(cmd->packet->src_id, SILC_ID_CLIENT);
-    if (!remote_id)
-      goto out;
-
-    if (!SILC_ID_CLIENT_COMPARE(remote_id, win->local_id))
-      goto out;
-
-    /* Check whether we know this client already */
-    if (!silc_idcache_find_by_id_one(win->client_cache, remote_id,
-                                    SILC_ID_CLIENT, &id_cache))
-      {
-       /* Allocate client entry */
-       remote_client = silc_calloc(1, sizeof(*remote_client));
-       remote_client->id = remote_id;
-       remote_client->nickname = strdup(nickname);
-
-       /* Save the client to cache */
-       silc_idcache_add(win->client_cache, remote_client->nickname,
-                        SILC_ID_CLIENT, remote_client->id, remote_client, 
-                        TRUE);
-      } else {
-       silc_free(remote_id);
-       remote_client = (SilcClientEntry)id_cache->context;
-      }
-
-    /* Send the away message */
-    silc_client_packet_send_private_message(client, cmd->sock, remote_client,
-                                           win->away->away,
-                                           strlen(win->away->away), TRUE);
-  }
-
- out:
-  memset(message, 0, buffer->len);
-  silc_free(message);
-  silc_free(nickname);
-}
similarity index 97%
rename from apps/silc/command_reply.h
rename to lib/silcclient/command_reply.h
index 0af5dddf0643aa0f6e1ce8853652d976841a0948..1a7e057867405232e565b21640e793ee77230a52 100644 (file)
@@ -86,7 +86,6 @@ SILC_CLIENT_CMD_REPLY_FUNC(info);
 SILC_CLIENT_CMD_REPLY_FUNC(links);
 SILC_CLIENT_CMD_REPLY_FUNC(stats);
 SILC_CLIENT_CMD_REPLY_FUNC(users);
-SILC_CLIENT_CMD_REPLY_FUNC(away);
 SILC_CLIENT_CMD_REPLY_FUNC(connect);
 SILC_CLIENT_CMD_REPLY_FUNC(ping);
 SILC_CLIENT_CMD_REPLY_FUNC(pong);
@@ -102,6 +101,5 @@ SILC_CLIENT_CMD_REPLY_FUNC(die);
 SILC_CLIENT_CMD_REPLY_FUNC(silcoper);
 SILC_CLIENT_CMD_REPLY_FUNC(leave);
 SILC_CLIENT_CMD_REPLY_FUNC(names);
-SILC_CLIENT_CMD_REPLY_FUNC(msg);
 
 #endif
similarity index 82%
rename from apps/silc/idlist.c
rename to lib/silcclient/idlist.c
index ed42fca4a5002bf158f5402af11a8dbe8540171c..7f632e965ed30ab389f56df3622bebdd5ce66cc3 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.1  2000/07/12 05:55:05  priikone
- *     Created idlist.c
- *
- */
+/* $Id$ */
 
-#include "clientincludes.h"
+#include "clientlibincludes.h"
 
 /* Finds client entry from cache by nickname. If the entry is not found
    from the cache this function queries it from the server. If `server'
@@ -34,7 +28,7 @@
    handling. This also ignores case-sensitivity. */
 
 SilcClientEntry silc_idlist_get_client(SilcClient client,
-                                      SilcClientWindow win,
+                                      SilcClientConnection conn,
                                       char *nickname,
                                       char *server,
                                       unsigned int num)
@@ -44,7 +38,7 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
   SilcClientEntry entry = NULL;
 
   /* Find ID from cache */
-  if (!silc_idcache_find_by_data_loose(win->client_cache, nickname, &list)) {
+  if (!silc_idcache_find_by_data_loose(conn->client_cache, nickname, &list)) {
     SilcClientCommandContext ctx;
     char ident[512];
     
@@ -56,12 +50,13 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
        sending simple IDENTIFY command to the server. */
     ctx = silc_calloc(1, sizeof(*ctx));
     ctx->client = client;
-    ctx->sock = win->sock;
+    ctx->conn = conn;
+    ctx->command = silc_client_command_find("IDENTIFY");
     memset(ident, 0, sizeof(ident));
-    snprintf(ident, sizeof(ident), "/IDENTIFY %s", nickname);
-    silc_client_parse_command_line(ident, &ctx->argv, &ctx->argv_lens, 
-                                  &ctx->argv_types, &ctx->argc, 2);
-    silc_client_command_identify(ctx);
+    snprintf(ident, sizeof(ident), "IDENTIFY %s", nickname);
+    silc_parse_command_line(ident, &ctx->argv, &ctx->argv_lens, 
+                           &ctx->argv_types, &ctx->argc, 2);
+    ctx->command->cb(ctx);
 
     if (list)
       silc_idcache_list_free(list);
@@ -108,13 +103,13 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
 /* Finds channel entry from ID cache by channel name. */
 
 SilcChannelEntry silc_idlist_get_channel(SilcClient client,
-                                        SilcClientWindow win,
+                                        SilcClientConnection conn,
                                         char *channel)
 {
   SilcIDCacheEntry id_cache;
   SilcChannelEntry entry;
 
-  if (!silc_idcache_find_by_data_one(win->channel_cache, channel, &id_cache))
+  if (!silc_idcache_find_by_data_one(conn->channel_cache, channel, &id_cache))
     return NULL;
 
   entry = (SilcChannelEntry)id_cache->context;
similarity index 96%
rename from apps/silc/idlist.h
rename to lib/silcclient/idlist.h
index 2191211bb397173436049e89f470d8a9cbba435a..8a7b82815fc8528284bd0beda6e9bd8e582f92bc 100644 (file)
@@ -61,12 +61,12 @@ typedef SilcChannelEntryObject *SilcChannelEntry;
 /* Prototypes */
 
 SilcClientEntry silc_idlist_get_client(SilcClient client,
-                                      SilcClientWindow win,
+                                      SilcClientConnection conn,
                                       char *nickname,
                                       char *server,
                                       unsigned int num);
 SilcChannelEntry silc_idlist_get_channel(SilcClient client,
-                                        SilcClientWindow win,
+                                        SilcClientConnection conn,
                                         char *channel);
 
 #endif
diff --git a/lib/silcclient/ops.h b/lib/silcclient/ops.h
new file mode 100644 (file)
index 0000000..49d3b8c
--- /dev/null
@@ -0,0 +1,161 @@
+/*
+
+  ops.h
+
+  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+
+  Copyright (C) 2000 Pekka Riikonen
+
+  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
+  GNU General Public License for more details.
+
+*/
+
+#ifndef OPS_H
+#define OPS_H
+
+/*
+ * SILC Client Operations
+ *
+ * These functions must be implemented by the application calling the
+ * SILC client library. The client library can call these functions at
+ * any time.
+ *
+ * To use this structure: define a static SilcClientOperations variable,
+ * fill it and pass its pointer to silc_client_alloc function.
+ */
+typedef struct {
+  void (*say)(SilcClient client, SilcClientConnection conn, char *msg, ...);
+  void (*channel_message)(SilcClient client, SilcClientConnection conn, 
+                         char *sender, char *channel_name, char *msg);
+  void (*private_message)(SilcClient client, SilcClientConnection conn,
+                         char *sender, char *msg);
+  void (*command)(SilcClient client, SilcClientConnection conn, 
+                 SilcClientCommandContext cmd_context, int success,
+                 SilcCommand command);
+  void (*command_reply)(SilcClient client, SilcClientConnection conn,
+                       SilcCommandPayload cmd_payload, int success,
+                       SilcCommand command, ...);
+  void (*connect)(SilcClient client, SilcClientConnection conn, int success);
+  void (*disconnect)(SilcClient client, SilcClientConnection conn);
+  int (*get_auth_method)(SilcClient client, SilcClientConnection conn,
+                        char *hostname, unsigned short port,
+                        SilcProtocolAuthMeth *auth_meth,
+                        unsigned char **auth_data,
+                        unsigned int *auth_data_len);
+  int (*verify_server_key)(SilcClient client, SilcClientConnection conn,
+                          unsigned char *pk, unsigned int pk_len,
+                          SilcSKEPKType pk_type);
+  unsigned char *(*ask_passphrase)(SilcClient client, 
+                                  SilcClientConnection conn);
+} SilcClientOperations;
+
+/* 
+   Descriptions of above operation functions:
+
+   void (*say)(SilcClient client, SilcClientConnection conn, char *msg, ...);
+
+   Message sent to the application by library. `conn' associates the
+   message to a specific connection.  `conn', however, may be NULL.
+
+
+   void (*channel_message)(client, SilcClientConnection conn, 
+                          char *sender, char *channel_name, char *msg);
+
+   Message for a channel. The `sender' is the nickname of the sender 
+   received in the packet. The `channel_name' is the name of the channel. 
+
+
+   void (*private_message)(client, SilcClientConnection conn,
+                          char *sender, char *msg);
+
+   Private message to the client. The `sender' is the nickname of the
+   sender received in the packet.
+
+
+   void (*command)(SilcClient client, SilcClientConnection conn, 
+                  SilcClientCommandContext cmd_context, int success,
+                  SilcCommand command);
+
+   Command handler. This function is called always in the command function.
+   If error occurs it will be called as well. `conn' is the associated
+   client connection. `cmd_context' is the command context that was
+   originally sent to the command. `success' is FALSE if error occured
+   during command. `command' is the command being processed. It must be
+   noted that this is not reply from server. This is merely called just
+   after application has called the command. Just to tell application
+   that the command really was processed.
+
+
+   void (*command_reply)(SilcClient client, SilcClientConnection conn,
+                        SilcCommandPayload cmd_payload, int success,
+                        SilcCommand command, ...);
+
+   Command reply handler. This function is called always in the command reply
+   function. If error occurs it will be called as well. Normal scenario
+   is that it will be called after the received command data has been parsed
+   and processed. The function is used to pass the received command data to
+   the application. 
+
+   `conn' is the associated client connection. `cmd_payload' is the command
+   payload data received from server and it can be ignored. It is provided
+   if the application would like to re-parse the received command data,
+   however, it must be noted that the data is parsed already by the library
+   thus the payload can be ignored. `success' is FALSE if error occured.
+   In this case arguments are not sent to the application. `command' is the
+   command reply being processed. The function has variable argument list
+   and each command defines the number and type of arguments it passes to the
+   application (on error they are not sent).
+
+
+   void (*connect)(SilcClient client, SilcClientConnection conn, int success);
+
+   Called to indicate that connection was either successfully established
+   or connecting failed.  This is also the first time application receives
+   the SilcClientConnection objecet which it should save somewhere.
+
+
+   void (*disconnect)(SilcClient client, SilcClientConnection conn);
+
+   Called to indicate that connection was disconnected to the server.
+
+
+   int (*get_auth_method)(SilcClient client, SilcClientConnection conn,
+                         char *hostname, unsigned short port,
+                         SilcProtocolAuthMeth *auth_meth,
+                         unsigned char **auth_data,
+                         unsigned int *auth_data_len);
+
+   Find authentication method and authentication data by hostname and
+   port. The hostname may be IP address as well. The found authentication
+   method and authentication data is returned to `auth_meth', `auth_data'
+   and `auth_data_len'. The function returns TRUE if authentication method
+   is found and FALSE if not. `conn' may be NULL.
+
+
+   int (*verify_server_key)(SilcClient client, SilcClientConnection conn,
+                           unsigned char *pk, unsigned int pk_len,
+                           SilcSKEPKType pk_type);
+
+   Verifies received public key. The public key has been received from
+   a server. If user decides to trust the key may be saved as trusted
+   server key for later use. If user does not trust the key this returns
+   FALSE. If everything is Ok this returns TRUE. 
+
+
+   unsigned char *(*ask_passphrase)(SilcClient client, 
+                                   SilcClientConnection conn);
+
+   Ask (interact, that is) a passphrase from user. Returns the passphrase
+   or NULL on error. 
+
+*/
+
+#endif
similarity index 84%
rename from apps/silc/protocol.c
rename to lib/silcclient/protocol.c
index 342597069f2f48a56d954167eb83a4bb95993414..d0b8064c1093adc736e96f4cd8dbd5f3bd179f79 100644 (file)
 /*
  * Client side of the protocols.
  */
-/*
- * $Id$
- * $Log$
- * Revision 1.8  2000/07/20 10:17:25  priikone
- *     Added dynamic protocol registering/unregistering support.  The
- *     patch was provided by cras.
- *
- * Revision 1.7  2000/07/19 07:07:47  priikone
- *     Added version detection support to SKE.
- *
- * Revision 1.6  2000/07/14 06:12:29  priikone
- *     Put the HMAC keys into the HMAC object instead on having them
- *     saved elsewhere; we can use now silc_hmac_make instead of
- *     silc_hmac_make_with_key.
- *
- * Revision 1.5  2000/07/07 11:36:09  priikone
- *     Inform user when received unsupported public key from server.
- *
- * Revision 1.4  2000/07/07 06:53:45  priikone
- *     Added support for server public key verification.
- *
- * Revision 1.3  2000/07/06 07:14:16  priikone
- *     Deprecated old `channel_auth' protocol.
- *
- * Revision 1.2  2000/07/05 06:12:05  priikone
- *     Global cosmetic changes.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
-#include "clientincludes.h"
+#include "clientlibincludes.h"
 
 SILC_TASK_CALLBACK(silc_client_protocol_connection_auth);
 SILC_TASK_CALLBACK(silc_client_protocol_key_exchange);
@@ -99,8 +68,9 @@ silc_client_protocol_ke_verify_key(SilcSKE ske,
 
   SILC_LOG_DEBUG(("Start"));
 
-  if (!silc_client_verify_server_key(client, ctx->sock, 
-                                    pk_data, pk_len, pk_type))
+  /* Verify server key from user. */
+  if (!client->ops->verify_server_key(client, ctx->sock->user_data, 
+                                     pk_data, pk_len, pk_type))
     return SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY;
 
   return SILC_SKE_STATUS_OK;
@@ -115,38 +85,38 @@ static void silc_client_protocol_ke_set_keys(SilcSKE ske,
                                             SilcPKCS pkcs,
                                             SilcHash hash)
 {
-  SilcClientWindow win = (SilcClientWindow)sock->user_data;
+  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   SilcHash nhash;
 
   SILC_LOG_DEBUG(("Setting new keys into use"));
 
   /* Allocate cipher to be used in the communication */
-  silc_cipher_alloc(cipher->cipher->name, &win->send_key);
-  silc_cipher_alloc(cipher->cipher->name, &win->receive_key);
+  silc_cipher_alloc(cipher->cipher->name, &conn->send_key);
+  silc_cipher_alloc(cipher->cipher->name, &conn->receive_key);
 
-  win->send_key->cipher->set_key(win->send_key->context, 
+  conn->send_key->cipher->set_key(conn->send_key->context, 
                                 keymat->send_enc_key, 
                                 keymat->enc_key_len);
-  win->send_key->set_iv(win->send_key, keymat->send_iv);
-  win->receive_key->cipher->set_key(win->receive_key->context, 
+  conn->send_key->set_iv(conn->send_key, keymat->send_iv);
+  conn->receive_key->cipher->set_key(conn->receive_key->context, 
                                    keymat->receive_enc_key, 
                                    keymat->enc_key_len);
-  win->receive_key->set_iv(win->receive_key, keymat->receive_iv);
+  conn->receive_key->set_iv(conn->receive_key, keymat->receive_iv);
 
   /* Allocate PKCS to be used */
 #if 0
   /* XXX Do we ever need to allocate PKCS for the connection??
      If yes, we need to change KE protocol to get the initiators
      public key. */
-  silc_pkcs_alloc(pkcs->pkcs->name, &win->public_Key);
-  silc_pkcs_set_public_key(win->public_key, ske->ke2_payload->pk_data, 
+  silc_pkcs_alloc(pkcs->pkcs->name, &conn->public_Key);
+  silc_pkcs_set_public_key(conn->public_key, ske->ke2_payload->pk_data, 
                           ske->ke2_payload->pk_len);
 #endif
 
   /* Save HMAC key to be used in the communication. */
   silc_hash_alloc(hash->hash->name, &nhash);
-  silc_hmac_alloc(nhash, &win->hmac);
-  silc_hmac_set_key(win->hmac, keymat->hmac_key, keymat->hmac_key_len);
+  silc_hmac_alloc(nhash, &conn->hmac);
+  silc_hmac_set_key(conn->hmac, keymat->hmac_key, keymat->hmac_key_len);
 }
 
 /* Performs key exchange protocol. This is used for both initiator
@@ -158,6 +128,7 @@ SILC_TASK_CALLBACK(silc_client_protocol_key_exchange)
   SilcClientKEInternalContext *ctx = 
     (SilcClientKEInternalContext *)protocol->context;
   SilcClient client = (SilcClient)ctx->client;
+  SilcClientConnection conn = ctx->sock->user_data;
   SilcSKEStatus status;
 
   SILC_LOG_DEBUG(("Start"));
@@ -305,11 +276,13 @@ SILC_TASK_CALLBACK(silc_client_protocol_key_exchange)
       if (status != SILC_SKE_STATUS_OK) {
 
         if (status == SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY) {
-          silc_say(client, "Received unsupported server %s public key",
-                   ctx->sock->hostname);
+          client->ops->say(client, conn, 
+                          "Received unsupported server %s public key",
+                          ctx->sock->hostname);
         } else {
-          silc_say(client, "Error during key exchange protocol with server %s",
-                   ctx->sock->hostname);
+          client->ops->say(client, conn,
+                          "Error during key exchange protocol with server %s",
+                          ctx->sock->hostname);
         }
        protocol->state = SILC_PROTOCOL_STATE_ERROR;
        protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
@@ -371,6 +344,7 @@ SILC_TASK_CALLBACK(silc_client_protocol_connection_auth)
   SilcClientConnAuthInternalContext *ctx = 
     (SilcClientConnAuthInternalContext *)protocol->context;
   SilcClient client = (SilcClient)ctx->client;
+  SilcClientConnection conn = ctx->sock->user_data;
 
   SILC_LOG_DEBUG(("Start"));
 
@@ -402,9 +376,10 @@ SILC_TASK_CALLBACK(silc_client_protocol_connection_auth)
          break;
        }
 
-       silc_say(client, "Password authentication required by server %s",
-                ctx->sock->hostname);
-       auth_data = silc_client_ask_passphrase(client);
+       client->ops->say(client, conn, 
+                        "Password authentication required by server %s",
+                        ctx->sock->hostname);
+       auth_data = client->ops->ask_passphrase(client, conn);
        auth_data_len = strlen(auth_data);
        break;
 
similarity index 88%
rename from apps/silc/protocol.h
rename to lib/silcclient/protocol.h
index 684995dd2b431df6c03c4f454ede1256bf49510d..87c2573ad06230c76a31d335085248a3309a3ce2 100644 (file)
 #define PROTOCOL_H
 
 /* SILC client protocol types */
-#define SILC_PROTOCOL_CLIENT_NONE 0
-#define SILC_PROTOCOL_CLIENT_CONNECTION_AUTH 1
-#define SILC_PROTOCOL_CLIENT_KEY_EXCHANGE 2
-/* #define SILC_PROTOCOL_CLIENT_MAX 255 */
+#define SILC_PROTOCOL_CLIENT_NONE               0
+#define SILC_PROTOCOL_CLIENT_CONNECTION_AUTH    1
+#define SILC_PROTOCOL_CLIENT_KEY_EXCHANGE       2
+/* #define SILC_PROTOCOL_CLIENT_MAX             255 */
 
 /* Internal context for key exchange protocol */
 typedef struct {
@@ -53,7 +53,7 @@ typedef struct {
 
   /* Auth method that must be used. This is resolved before this
      connection authentication protocol is started. */
-  unsigned int auth_meth;
+  SilcProtocolAuthMeth auth_meth;
 
   /* Destinations ID from KE protocol context */
   void *dest_id;