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:
1  2 
apps/silc/Makefile.am
apps/silc/client_ops.c
apps/silc/client_ops.h
apps/silc/clientconfig.c
apps/silc/clientutil.c
apps/silc/clientutil.h
apps/silc/local_command.c
apps/silc/local_command.h
apps/silc/screen.h
apps/silc/silc.c
apps/silc/silc.h
lib/silcclient/Makefile.am
lib/silcclient/README
lib/silcclient/client.c
lib/silcclient/client.h
lib/silcclient/command.c
lib/silcclient/command.h
lib/silcclient/command_reply.c
lib/silcclient/command_reply.h
lib/silcclient/idlist.c
lib/silcclient/idlist.h
lib/silcclient/ops.h
lib/silcclient/protocol.c
lib/silcclient/protocol.h

index 264c67f1fe3b9658114c4d698e9cc9586db0a288,1b358e63308cf34102a79e80b4b32e86b6367b90..91cab5844579e0120f1abb837e6b664553508304
@@@ -22,20 -22,19 +22,19 @@@ bin_PROGRAMS = sil
  
  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../lib/silcmath/gmp-3.0.1
++      -I../includes -I../lib/silcclient -I../lib/silcutil \
 +      -I../lib/silcmath/gmp
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..3a08a798aed1ad0e2753030f97270ef5bca03abd
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,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,
++};
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..70b6f0f5ea9daa2f7d799e4d544c6f9287c6b023
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,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,e55157b30fdee38ec48eb3c322a87f1b2314e70a..b6f5b150a832b58c814b063752ea0ff355e1660a
    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.
 - *    Importet from internal CVS/Added Log headers.
-- *
-- *
-- */
++/* $Id$ */
  
  #include "clientincludes.h"
  #include "clientconfig.h"
@@@ -543,7 -539,7 +529,8 @@@ int silc_client_config_parse_lines(Silc
  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"));
  
        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);
  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"));
  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"));
  
        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,21043795c0249108bc680645c5583a99488b9461..210f50b02c5d2307f6a8bb4bf12a211bb97e6c38
    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.
 - *    Importet 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;
  
    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];
 -  char message[1024];
--  
--  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. */
  
  void silc_print(SilcClient client, char *msg, ...)
  {
    va_list vp;
 -  char message[1024];
 +  char message[2048];
++  SilcClientInternal app = client->application;
    
    memset(message, 0, sizeof(message));
    strncat(message, "\n ", 2);
    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,84 -209,48 +185,39 @@@ 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". */
  
 -char *silc_client_ask_passphrase(SilcClient client)
 +int silc_client_ask_yes_no(SilcClient client, char *prompt)
  {
 -  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--;
++  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;
 +  } else if (!strncasecmp(answer, "no", strlen(answer)) ||
 +           !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();
  
 -  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);
++  silc_screen_input_reset(app->screen);
  
    return ret;
  }
@@@ -590,428 -441,4 +493,239 @@@ New pair of keys will be created.  Plea
    silc_math_primegen_uninit();
    silc_rng_free(rng);
    silc_pkcs_free(pkcs);
 +
 +  return TRUE;
 +}
 +
 +/* This checks stats for various SILC files and directories. First it 
 +   checks if ~/.silc directory exist and is owned by the correct user. If 
 +   it doesn't exist, it will create the directory. After that it checks if
 +   user's Public and Private key files exists and that they aren't expired.
 +   If they doesn't exist or they are expired, they will be (re)created
 +   after return. */
 +
 +int silc_client_check_silc_dir()
 +{
 +  char filename[256], file_public_key[256], file_private_key[256];
 +  char servfilename[256];
 +  char *identifier;
 +  struct stat st;
 +  struct passwd *pw;
 +  int firstime = FALSE;
 +  time_t curtime, modtime;
 +
 +  SILC_LOG_DEBUG(("Checking ~./silc directory"));
 +
 +  memset(filename, 0, sizeof(filename));
 +  memset(file_public_key, 0, sizeof(file_public_key));
 +  memset(file_private_key, 0, sizeof(file_private_key));
 +
 +  pw = getpwuid(getuid());
 +  if (!pw) {
 +    fprintf(stderr, "silc: %s\n", strerror(errno));
 +    return FALSE;
 +  }
 +
 +  identifier = silc_client_create_identifier();
 +
 +  /* We'll take home path from /etc/passwd file to be sure. */
 +  snprintf(filename, sizeof(filename) - 1, "%s/.silc/", pw->pw_dir);
 +  snprintf(servfilename, sizeof(servfilename) - 1, "%s/.silc/serverkeys", 
 +         pw->pw_dir);
 +
 +  /*
 +   * Check ~/.silc directory
 +   */
 +  if ((stat(filename, &st)) == -1) {
 +    /* If dir doesn't exist */
 +    if (errno == ENOENT) {
 +      if (pw->pw_uid == geteuid()) {
 +      if ((mkdir(filename, 0755)) == -1) {
 +        fprintf(stderr, "Couldn't create `%s' directory\n", filename);
 +        return FALSE;
 +      }
 +
 +      /* Directory was created. First time running SILC */
 +      firstime = TRUE;
 +      } else {
 +      fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n",
 +              filename);
 +      return FALSE;
 +      }
 +    } else {
 +      fprintf(stderr, "%s\n", strerror(errno));
 +      return FALSE;
 +    }
 +  } else {
 +    
 +    /* Check the owner of the dir */
 +    if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { 
 +      fprintf(stderr, "You don't seem to own `%s' directory\n",
 +            filename);
 +      return FALSE;
 +    }
 +    
 +    /* Check the permissions of the dir */
 +    if ((st.st_mode & 0777) != 0755) {
 +      if ((chmod(filename, 0755)) == -1) {
 +      fprintf(stderr, "Permissions for `%s' directory must be 0755\n", 
 +              filename);
 +      return FALSE;
 +      }
 +    }
 +  }
 +
 +  /*
 +   * Check ~./silc/serverkeys directory
 +   */
 +  if ((stat(servfilename, &st)) == -1) {
 +    /* If dir doesn't exist */
 +    if (errno == ENOENT) {
 +      if (pw->pw_uid == geteuid()) {
 +      if ((mkdir(servfilename, 0755)) == -1) {
 +        fprintf(stderr, "Couldn't create `%s' directory\n", servfilename);
 +        return FALSE;
 +      }
 +      } else {
 +      fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n",
 +              servfilename);
 +      return FALSE;
 +      }
 +    } else {
 +      fprintf(stderr, "%s\n", strerror(errno));
 +      return FALSE;
 +    }
 +  }
 +  
 +  /*
 +   * Check Public and Private keys
 +   */
 +  snprintf(file_public_key, sizeof(file_public_key) - 1, "%s%s", 
 +         filename, SILC_CLIENT_PUBLIC_KEY_NAME);
 +  snprintf(file_private_key, sizeof(file_private_key) - 1, "%s%s", 
 +         filename, SILC_CLIENT_PRIVATE_KEY_NAME);
 +  
 +  /* If running SILC first time */
 +  if (firstime) {
 +    fprintf(stdout, "Running SILC for the first time\n");
 +    silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, 
 +                              SILC_CLIENT_DEF_PKCS_LEN,
 +                              file_public_key, file_private_key, 
 +                              identifier, NULL, NULL);
 +    return TRUE;
 +  }
 +  
 +  if ((stat(file_public_key, &st)) == -1) {
 +    /* If file doesn't exist */
 +    if (errno == ENOENT) {
 +      fprintf(stdout, "Your public key doesn't exist\n");
 +      silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, 
 +                                SILC_CLIENT_DEF_PKCS_LEN,
 +                                file_public_key, 
 +                                file_private_key, identifier, NULL, NULL);
 +    } else {
 +      fprintf(stderr, "%s\n", strerror(errno));
 +      return FALSE;
 +    }
 +  }
 +
 +  if ((stat(file_private_key, &st)) == -1) {
 +    /* If file doesn't exist */
 +    if (errno == ENOENT) {
 +      fprintf(stdout, "Your private key doesn't exist\n");
 +      silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, 
 +                                SILC_CLIENT_DEF_PKCS_LEN,
 +                                file_public_key, 
 +                                file_private_key, identifier, NULL, NULL);
 +    } else {
 +      fprintf(stderr, "%s\n", strerror(errno));
 +      return FALSE;
 +    }
 +  }
 +    
 +  /* Check the owner of the public key */
 +  if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { 
 +    fprintf(stderr, "You don't seem to own your public key!?\n");
 +    return FALSE;
 +  }
 +  
 +  /* Check the owner of the private key */
 +  if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { 
 +    fprintf(stderr, "You don't seem to own your private key!?\n");
 +    return FALSE;
 +  }
 +    
 +  /* Check the permissions for the private key */
 +  if ((st.st_mode & 0777) != 0600) {
 +    fprintf(stderr, "Wrong permissions in your private key file `%s'!\n"
 +          "Trying to change them ... ", file_private_key);
 +    if ((chmod(file_private_key, 0600)) == -1) {
 +      fprintf(stderr,
 +            "Failed to change permissions for private key file!\n" 
 +            "Permissions for your private key file must be 0600.\n");
 +      return FALSE;
 +    }
 +    fprintf(stderr, "Done.\n\n");
 +  }
 +
 +  /* See if the key has expired. */
 +  modtime = st.st_mtime;      /* last modified */
 +  curtime = time(0) - modtime;
 +    
 +  /* 86400 is seconds in a day. */
 +  if (curtime >= (86400 * SILC_CLIENT_KEY_EXPIRES)) {
 +    fprintf(stdout, 
 +          "--------------------------------------------------\n"
 +          "Your private key has expired and needs to be\n" 
 +          "recreated.  This will be done automatically now.\n"
 +          "Your new key will expire in %d days from today.\n"
 +          "--------------------------------------------------\n",
 +          SILC_CLIENT_KEY_EXPIRES);
 +
 +    silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, 
 +                              SILC_CLIENT_DEF_PKCS_LEN,
 +                              file_public_key, 
 +                              file_private_key, identifier, NULL, NULL);
 +  }
 +  
 +  if (identifier)
 +    silc_free(identifier);
 +
 +  return TRUE;
 +}
 +
 +/* Loads public and private key from files. */
 +
 +int silc_client_load_keys(SilcClient client)
 +{
 +  char filename[256];
 +  struct passwd *pw;
 +
 +  SILC_LOG_DEBUG(("Loading public and private keys"));
 +
 +  pw = getpwuid(getuid());
 +  if (!pw)
 +    return FALSE;
 +
 +  memset(filename, 0, sizeof(filename));
 +  snprintf(filename, sizeof(filename) - 1, "%s/.silc/%s", 
 +         pw->pw_dir, SILC_CLIENT_PRIVATE_KEY_NAME);
 +
 +  if (silc_pkcs_load_private_key(filename, &client->private_key,
 +                               SILC_PKCS_FILE_BIN) == FALSE)
 +    if (silc_pkcs_load_private_key(filename, &client->private_key,
 +                                 SILC_PKCS_FILE_PEM) == FALSE)
 +      return FALSE;
 +
 +  memset(filename, 0, sizeof(filename));
 +  snprintf(filename, sizeof(filename) - 1, "%s/.silc/%s", 
 +         pw->pw_dir, SILC_CLIENT_PUBLIC_KEY_NAME);
 +
 +  if (silc_pkcs_load_public_key(filename, &client->public_key,
 +                              SILC_PKCS_FILE_PEM) == FALSE)
 +    if (silc_pkcs_load_public_key(filename, &client->public_key,
 +                                SILC_PKCS_FILE_BIN) == FALSE)
 +      return FALSE;
 +
 +  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,e18cf2d71eef0a5c8f576d3057137e1a8403773c..38f0d9e857b7cfc88e28c0f6cd3c85f093c242cf
  #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);
  void silc_client_list_ciphers();
  void silc_client_list_hash_funcs();
  void silc_client_list_pkcs();
 -void silc_client_create_key_pair(char *pkcs_name, int bits);
 +char *silc_client_create_identifier();
 +int silc_client_create_key_pair(char *pkcs_name, int bits,
 +                              char *public_key, char *private_key,
 +                              char *identifier, 
 +                              SilcPublicKey *ret_pub_key,
 +                              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
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..3d969f78165ac1aeb560a88fe7d3dc06b0fa69f8
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,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);
++}
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..56730578e734a927aad878caecf2842ce740e843
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,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,25d083b06278c54c619f6351dd2b294a413faf60..a1fe8ce32522b326703790a9ef2b092412f8cb89
@@@ -54,8 -53,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,e510ad2dbcd2a85256324ff9fe27e9d5ea193fff..ce9f7ee028dd58d84490abbb52132efcb99cef88
    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.
 - *    Importet 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 -42,6 +48,7 @@@
    { "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 -71,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 -88,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 -106,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) 
            case 'q':
              opt_no_silcrc = TRUE;
              break;
++          case 'd':
++            silc_debug = TRUE;
++            break;
            case 'h':
              usage();
              exit(0);
@@@ -224,45 -214,36 +230,97 @@@ SILC Secure Internet Live Conferencing
    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, 
 +                              NULL, NULL, NULL, NULL, NULL);
 +    exit(0);
 +  }
 +
    /* Default configuration file */
    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 */
 -  if (opt_create_keypair == TRUE) {
 -    /* Create new key pair and exit */
 -    silc_client_create_key_pair(opt_pkcs, opt_bits);
 -    exit(0);
 -  }
++  /* XXX Read local configuration file */
  
 -  /* 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
    exit(0);
  
   fail:
 -  if (config)
 -    silc_client_config_free(config);
 +  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,baa83b349de6e9db3a35b664c7e7735b6aed74fe..828e2c03db10071020eeab1ddfa1dfe740756280
     home directory. This may override global configuration settings. */
  #define SILC_CLIENT_HOME_CONFIG_FILE ".silcrc"
  
 +/* Default public and private key file names */
 +#define SILC_CLIENT_PUBLIC_KEY_NAME "public_key.pub"
 +#define SILC_CLIENT_PRIVATE_KEY_NAME "private_key.prv"
 +
 +/* Default key expiration time, one year. */
 +#define SILC_CLIENT_KEY_EXPIRES 365
 +
 +/* Default settings for creating key pair */
 +#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
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..8f914cf7f80019108ca6b8b9b1f7fca1bac638ef
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,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
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..26eb38588397ca08265c53f6c0f334f3b2e19b14
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,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).
index b3a6d391bc0c38ac1e3f220e51bea94256cbacf4,0000000000000000000000000000000000000000..d2b4aa660420eafa56ffaf918292651712fdcd9f
mode 100644,000000..100644
--- /dev/null
@@@ -1,1993 -1,0 +1,1499 @@@
- /*
-  * $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);
 +/*
 +
 +  client.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.
 +
 +*/
- SILC_TASK_CALLBACK(silc_client_update_clock);
- SILC_TASK_CALLBACK(silc_client_run_commands);
- SILC_TASK_CALLBACK(silc_client_process_key_press);
++/* $Id$ */
++
++#include "clientlibincludes.h"
 +
 +/* Static task callback prototypes */
- 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;
 +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);
 +
-    the client. */
++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
- int silc_client_alloc(SilcClient *new_client)
++   the client. The `application' is application specific user data pointer
++   and caller must free it. */
 +
-   *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;
++SilcClient silc_client_alloc(SilcClientOperations *ops, void *application)
 +{
++  SilcClient new_client;
 +
-   return TRUE;
++  new_client = silc_calloc(1, sizeof(*new_client));
++  new_client->application = application;
++  new_client->ops = ops;
 +
++  return new_client;
 +}
 +
 +/* Free's client object */
 +
 +void silc_client_free(SilcClient client)
 +{
 +  if (client) {
 +    silc_free(client);
 +  }
 +}
 +
 +/* Initializes the client. This makes all the necessary steps to make
 +   the client ready to be run. One must call silc_client_run to run the
 +   client. */
 +
 +int silc_client_init(SilcClient 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);
 +  SILC_LOG_DEBUG(("Initializing client"));
-   /* 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;
-   }
 +
 +  /* Initialize hash functions for client to use */
 +  silc_hash_alloc("md5", &client->md5hash);
 +  silc_hash_alloc("sha1", &client->sha1hash);
 +
 +  /* Initialize none cipher */
 +  silc_cipher_alloc("none", &client->none_cipher);
 +
 +  /* Initialize random number generator */
 +  client->rng = silc_rng_alloc();
 +  silc_rng_init(client->rng);
 +  silc_math_primegen_init(); /* XXX */
 +
-   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);
 +  /* Register protocols */
 +  silc_client_protocols_register();
 +
 +  /* Initialize the scheduler */
-  err0:
-   silc_task_queue_free(client->timeout_queue);
-  err1:
-   silc_task_queue_free(client->io_queue);
-  err2:
-   return FALSE;
++  silc_schedule_init(&client->io_queue, &client->timeout_queue, 
++                   &client->generic_queue, 5000);
 +
 +  return TRUE;
-   SILC_LOG_DEBUG(("Client client"));
 +}
 +
 +/* Stops the client. This is called to stop the client and thus to stop
 +   the program. */
 +
 +void silc_client_stop(SilcClient client)
 +{
 +  SILC_LOG_DEBUG(("Stopping client"));
 +
 +  /* Stop the scheduler, although it might be already stopped. This
 +     doesn't hurt anyone. This removes all the tasks and task queues,
 +     as well. */
 +  silc_schedule_stop();
 +  silc_schedule_uninit();
 +
 +  silc_client_protocols_unregister();
 +
- /* 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. */
++  SILC_LOG_DEBUG(("Client stopped"));
 +}
 +
 +/* Runs the client. */
 +
 +void silc_client_run(SilcClient client)
 +{
 +  SILC_LOG_DEBUG(("Running client"));
 +
 +  /* Start the scheduler, the heart of the SILC client. When this returns
 +     the program will be terminated. */
 +  silc_schedule();
 +}
 +
- SilcClientWindow silc_client_create_main_window(SilcClient client)
++/* 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 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 silc_client_add_connection(SilcClient client,
++                                              void *context)
 +{
-   /* Add the pointers */
-   win->screen = silc_screen_add_output_window(client->screen);
-   win->sock = NULL;
++  SilcClientConnection conn;
++  int i;
 +
-   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 = silc_calloc(1, sizeof(*conn));
 +
 +  /* Initialize ID caches */
-     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);
++  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;
 +    }
 +
-   } 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);
++  client->conns = silc_realloc(client->conns, sizeof(*client->conns)
++                             * (client->conns_count + 1));
++  client->conns[client->conns_count] = conn;
++  client->conns_count++;
 +
- /* Returns the command fetched from user typed command line */
++  return conn;
 +}
 +
- static char *silc_client_parse_command(unsigned char *buffer)
++/* Removes connection from client. */
 +
-   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);
++void silc_client_del_connection(SilcClient client, SilcClientConnection conn)
 +{
-     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;
++  int i;
 +
-     
-     /* 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;
-   }
++  for (i = 0; i < client->conns_count; i++)
++    if (client->conns[i] == conn) {
++      silc_free(conn);
++      client->conns[i] = NULL;
 +    }
- /* Connects to remote server */
 +}
 +
 +/* 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;
 +  int port;
 +  int tries;
 +} SilcClientInternalConnectContext;
 +
 +static int 
 +silc_client_connect_to_server_internal(SilcClientInternalConnectContext *ctx)
 +{
 +  int sock;
 +
 +  /* XXX In the future we should give up this non-blocking connect all
 +     together and use threads instead. */
 +  /* Create connection to server asynchronously */
 +  sock = silc_net_create_connection_async(ctx->port, ctx->host);
 +  if (sock < 0)
 +    return -1;
 +
 +  /* Register task that will receive the async connect and will
 +     read the result. */
 +  ctx->task = silc_task_register(ctx->client->io_queue, sock, 
 +                               silc_client_connect_to_server_start,
 +                               (void *)ctx, 0, 0, 
 +                               SILC_TASK_FD,
 +                               SILC_TASK_PRI_NORMAL);
 +  silc_task_reset_iotype(ctx->task, SILC_TASK_WRITE);
 +  silc_schedule_set_listen_fd(sock, ctx->task->iomask);
 +
 +  ctx->sock = sock;
 +
 +  return sock;
 +}
 +
-                                 char *host)
++/* 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,
-   silc_say(client, "Connecting to port %d of server %s", port, host);
++                                char *host, void *context)
 +{
 +  SilcClientInternalConnectContext *ctx;
++  SilcClientConnection conn;
 +
 +  SILC_LOG_DEBUG(("Connecting to port %d of server %s",
 +                port, host));
 +
-   client->current_win->remote_host = strdup(host);
-   client->current_win->remote_port = port;
++  conn = silc_client_add_connection(client, context);
++  conn->remote_host = strdup(host);
++  conn->remote_port = port;
 +
-       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, 
++                 "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;
 +
 +  /* Do the actual connecting process */
 +  return silc_client_connect_to_server_internal(ctx);
 +}
 +
 +/* Start of the connection to the remote server. This is called after
 +   succesful TCP/IP connection has been established to the remote host. */
 +
 +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);
 +
 +  SILC_LOG_DEBUG(("Start"));
 +
 +  /* Check the socket status as it might be in error */
 +  getsockopt(fd, SOL_SOCKET, SO_ERROR, &opt, &opt_len);
 +  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));
++      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);
 +      silc_net_close_connection(fd);
 +      silc_task_unregister(client->io_queue, ctx->task);
 +
 +      /* Try again */
 +      silc_client_connect_to_server_internal(ctx);
 +      ctx->tries++;
 +    } else {
 +      /* Connection failed and we won't try anymore */
-   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");
++      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;
 +  }
 +
 +  silc_schedule_unset_listen_fd(fd);
 +  silc_task_unregister(client->io_queue, ctx->task);
 +  silc_free(ctx);
 +
 +  /* Allocate new socket connection object */
-   client->current_win->sock->hostname = client->current_win->remote_host;
-   client->current_win->sock->port = client->current_win->remote_port;
++  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;
 +  }
-   proto_ctx->sock = client->current_win->sock;
++
++  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;
-     silc_say(client, "Error: Could not start authentication protocol");
++  proto_ctx->sock = conn->sock;
 +  proto_ctx->rng = client->rng;
 +  proto_ctx->responder = FALSE;
 +
 +  /* Perform key exchange protocol. silc_client_connect_to_server_final
 +     will be called after the protocol is finished. */
 +  silc_protocol_alloc(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE, 
 +                    &protocol, (void *)proto_ctx,
 +                    silc_client_connect_to_server_second);
 +  if (!protocol) {
-   client->current_win->sock->protocol = protocol;
++    client->ops->say(client, conn, 
++                   "Error: Could not start authentication protocol");
++    client->ops->connect(client, conn, FALSE);
 +    return;
 +  }
-   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 */
++  conn->sock->protocol = protocol;
 +
 +  /* Register the connection for network input and output. This sets
 +     that scheduler will listen for incoming packets for this connection 
 +     and sets that outgoing packets may be sent to this connection as well.
 +     However, this doesn't set the scheduler for outgoing traffic, it will 
 +     be set separately by calling SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT,
 +     later when outgoing data is available. */
 +  context = (void *)client;
 +  SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd);
 +
 +  /* Execute the protocol */
 +  protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
 +}
 +
 +/* Second part of the connecting to the server. This executed 
 +   authentication protocol. */
 +
 +SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
 +{
 +  SilcProtocol protocol = (SilcProtocol)context;
 +  SilcClientKEInternalContext *ctx = 
 +    (SilcClientKEInternalContext *)protocol->context;
 +  SilcClient client = (SilcClient)ctx->client;
 +  SilcSocketConnection sock = NULL;
 +  SilcClientConnAuthInternalContext *proto_ctx;
 +
 +  SILC_LOG_DEBUG(("Start"));
 +
 +  if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
 +    /* Error occured during protocol */
 +    SILC_LOG_DEBUG(("Error during KE protocol"));
 +    silc_protocol_free(protocol);
 +    if (ctx->ske)
 +      silc_ske_free(ctx->ske);
 +    if (ctx->dest_id)
 +      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;
 +  }
 +
 +  /* Allocate internal context for the authentication protocol. This
 +     is sent as context for the protocol. */
 +  proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
 +  proto_ctx->client = (void *)client;
 +  proto_ctx->sock = sock = ctx->sock;
 +  proto_ctx->ske = ctx->ske;  /* Save SKE object from previous protocol */
 +  proto_ctx->dest_id_type = ctx->dest_id_type;
 +  proto_ctx->dest_id = ctx->dest_id;
 +
 +  /* Resolve the authentication method to be used in this connection */
-   } else {
-     /* XXX Resolve by sending AUTH_REQUEST to server */
-     proto_ctx->auth_meth = SILC_PROTOCOL_CONN_AUTH_NONE;
-   }
++  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;
 +    }
-   SilcClientWindow win = (SilcClientWindow)ctx->sock->user_data;
 +
 +  /* Free old protocol as it is finished now */
 +  silc_protocol_free(protocol);
 +  if (ctx->packet)
 +    silc_buffer_free(ctx->packet);
 +  silc_free(ctx);
 +  /* silc_free(ctx->keymat....); */
 +  sock->protocol = NULL;
 +
 +  /* Allocate the authentication protocol. This is allocated here
 +     but we won't start it yet. We will be receiving party of this
 +     protocol thus we will wait that connecting party will make
 +     their first move. */
 +  silc_protocol_alloc(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH, 
 +                    &sock->protocol, (void *)proto_ctx, 
 +                    silc_client_connect_to_server_final);
 +
 +  /* Execute the protocol */
 +  sock->protocol->execute(client->timeout_queue, 0, sock->protocol, fd, 0, 0);
 +}
 +
 +/* Finalizes the connection to the remote SILC server. This is called
 +   after authentication protocol has been completed. This send our
 +   user information to the server to receive our client ID from
 +   server. */
 +
 +SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
 +{
 +  SilcProtocol protocol = (SilcProtocol)context;
 +  SilcClientConnAuthInternalContext *ctx = 
 +    (SilcClientConnAuthInternalContext *)protocol->context;
 +  SilcClient client = (SilcClient)ctx->client;
-     win->sock->protocol = NULL;
++  SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
 +  SilcBuffer packet;
 +
 +  SILC_LOG_DEBUG(("Start"));
 +
 +  if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
 +    /* Error occured during protocol */
 +    SILC_LOG_DEBUG(("Error during authentication protocol"));
 +    silc_protocol_free(protocol);
 +    if (ctx->auth_data)
 +      silc_free(ctx->auth_data);
 +    if (ctx->ske)
 +      silc_ske_free(ctx->ske);
 +    if (ctx->dest_id)
 +      silc_free(ctx->dest_id);
 +    silc_free(ctx);
-   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->sock->protocol = NULL;
++
++    /* Notify application of failure */
++    client->ops->connect(client, ctx->sock->user_data, FALSE);
 +    return;
 +  }
 +
 +  /* Send NEW_CLIENT packet to the server. We will become registered
 +     to the SILC network after sending this packet and we will receive
 +     client ID from the server. */
 +  packet = silc_buffer_alloc(2 + 2 + strlen(client->username) + 
 +                           strlen(client->realname));
 +  silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
 +  silc_buffer_format(packet,
 +                   SILC_STR_UI_SHORT(strlen(client->username)),
 +                   SILC_STR_UI_XNSTRING(client->username,
 +                                        strlen(client->username)),
 +                   SILC_STR_UI_SHORT(strlen(client->realname)),
 +                   SILC_STR_UI_XNSTRING(client->realname,
 +                                        strlen(client->realname)),
 +                   SILC_STR_END);
 +
 +  /* Send the packet */
 +  silc_client_packet_send(client, ctx->sock, SILC_PACKET_NEW_CLIENT,
 +                        NULL, 0, NULL, NULL, 
 +                        packet->data, packet->len, TRUE);
 +  silc_buffer_free(packet);
 +
 +  /* Save remote ID. */
-   silc_say(client, "Connected to port %d of host %s",
-          win->remote_port, win->remote_host);
++  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;
 +
-   client->screen->bottom_line->connection = win->remote_host;
-   silc_screen_print_bottom_line(client->screen, 0);
++  client->ops->say(client, conn, "Connected to port %d of host %s",
++                 conn->remote_port, conn->remote_host);
 +
-   win->sock->protocol = NULL;
++  /* Notify application of successful connection */
++  client->ops->connect(client, conn, TRUE);
 +
 +  silc_protocol_free(protocol);
 +  if (ctx->auth_data)
 +    silc_free(ctx->auth_data);
 +  if (ctx->ske)
 +    silc_ske_free(ctx->ske);
 +  if (ctx->dest_id)
 +    silc_free(ctx->dest_id);
 +  silc_free(ctx);
-   SilcClientWindow win;
++  conn->sock->protocol = NULL;
 +}
 +
 +/* Internal routine that sends packet or marks packet to be sent. This
 +   is used directly only in special cases. Normal cases should use
 +   silc_server_packet_send. Returns < 0 on error. */
 +
 +static int silc_client_packet_send_real(SilcClient client,
 +                                      SilcSocketConnection sock,
 +                                      int force_send)
 +{
 +  int ret;
 +
 +  /* Send the packet */
 +  ret = silc_packet_send(sock, force_send);
 +  if (ret != -2)
 +    return ret;
 +
 +  /* Mark that there is some outgoing data available for this connection. 
 +     This call sets the connection both for input and output (the input
 +     is set always and this call keeps the input setting, actually). 
 +     Actual data sending is performed by silc_client_packet_process. */
 +  SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(sock->sock);
 +
 +  /* Mark to socket that data is pending in outgoing buffer. This flag
 +     is needed if new data is added to the buffer before the earlier
 +     put data is sent to the network. */
 +  SILC_SET_OUTBUF_PENDING(sock);
 +
 +  return 0;
 +}
 +
 +/* Packet processing callback. This is used to send and receive packets
 +   from network. This is generic task. */
 +
 +SILC_TASK_CALLBACK(silc_client_packet_process)
 +{
 +  SilcClient client = (SilcClient)context;
 +  SilcSocketConnection sock = NULL;
-   win = (SilcClientWindow)sock->user_data;
++  SilcClientConnection conn;
 +  int ret;
 +
 +  SILC_LOG_DEBUG(("Processing packet"));
 +
 +  SILC_CLIENT_GET_SOCK(client, fd, sock);
 +  if (sock == NULL)
 +    return;
 +
-       silc_say(client, "Connection closed: premature EOF");
++  conn = (SilcClientConnection)sock->user_data;
 +
 +  /* Packet sending */
 +  if (type == SILC_TASK_WRITE) {
 +    SILC_LOG_DEBUG(("Writing data to connection"));
 +
 +    if (sock->outbuf->data - sock->outbuf->head)
 +      silc_buffer_push(sock->outbuf, 
 +                     sock->outbuf->data - sock->outbuf->head);
 +
 +    ret = silc_client_packet_send_real(client, sock, TRUE);
 +
 +    /* If returned -2 could not write to connection now, will do
 +       it later. */
 +    if (ret == -2)
 +      return;
 +    
 +    /* The packet has been sent and now it is time to set the connection
 +       back to only for input. When there is again some outgoing data 
 +       available for this connection it will be set for output as well. 
 +       This call clears the output setting and sets it only for input. */
 +    SILC_CLIENT_SET_CONNECTION_FOR_INPUT(fd);
 +    SILC_UNSET_OUTBUF_PENDING(sock);
 +
 +    silc_buffer_clear(sock->outbuf);
 +    return;
 +  }
 +
 +  /* Packet receiving */
 +  if (type == SILC_TASK_READ) {
 +    SILC_LOG_DEBUG(("Reading data from connection"));
 +
 +    /* Read data from network */
 +    ret = silc_packet_receive(sock);
 +    if (ret < 0)
 +      return;
 +    
 +    /* EOF */
 +    if (ret == 0) {
 +      SILC_LOG_DEBUG(("Read EOF"));
 +
 +      /* If connection is disconnecting already we will finally
 +       close the connection */
 +      if (SILC_IS_DISCONNECTING(sock)) {
 +      silc_client_close_connection(client, sock);
++      client->ops->disconnect(client, conn);
 +      return;
 +      }
 +      
++      client->ops->say(client, conn, "Connection closed: premature EOF");
 +      SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
-     if (!silc_packet_receive_process(sock, win->receive_key, win->hmac,
 +      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. */
-   SilcClientWindow win = (SilcClientWindow)sock->user_data;
++    if (!silc_packet_receive_process(sock, conn->receive_key, conn->hmac,
 +                                   silc_client_packet_parse, client)) {
 +      silc_buffer_clear(sock->inbuf);
 +      return;
 +    }
 +  }
 +}
 +
 +/* Parses whole packet, received earlier. */
 +
 +SILC_TASK_CALLBACK(silc_client_packet_parse_real)
 +{
 +  SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
 +  SilcClient client = (SilcClient)parse_ctx->context;
 +  SilcPacketContext *packet = parse_ctx->packet;
 +  SilcBuffer buffer = packet->buffer;
 +  SilcSocketConnection sock = parse_ctx->sock;
-   ret = silc_packet_decrypt(win->receive_key, win->hmac, buffer, packet);
++  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
 +  int ret;
 +
 +  SILC_LOG_DEBUG(("Start"));
 +
 +  /* Decrypt the received packet */
-   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);
-   }
++  ret = silc_packet_decrypt(conn->receive_key, conn->hmac, buffer, packet);
 +  if (ret < 0)
 +    goto out;
 +
 +  if (ret == 0) {
 +    /* Parse the packet. Packet type is returned. */
 +    ret = silc_packet_parse(packet);
 +  } else {
 +    /* Parse the packet header in special way as this is "special"
 +       packet type. */
 +    ret = silc_packet_parse_special(packet);
 +  }
 +
 +  if (ret == SILC_PACKET_NONE)
 +    goto out;
 +
 +  /* Parse the incoming packet type */
 +  silc_client_packet_parse_type(client, sock, packet);
 +
 + out:
 +  silc_buffer_clear(buffer);
 +  silc_free(packet);
 +  silc_free(parse_ctx);
 +}
 +
 +/* Parser callback called by silc_packet_receive_process. Thie merely
 +   registers timeout that will handle the actual parsing when appropriate. */
 +
 +void silc_client_packet_parse(SilcPacketParserContext *parser_context)
 +{
 +  SilcClient client = (SilcClient)parser_context->context;
-     {
-       SilcClientCommandReplyContext ctx;
-       ctx = silc_calloc(1, sizeof(*ctx));
-       ctx->client = client;
-       ctx->sock = sock;
-       ctx->packet = packet;
-       silc_client_command_reply_msg((void *)ctx);
-     }
++
++  /* 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
 +   requires. This is done for all incoming packets. */
 +
 +void silc_client_packet_parse_type(SilcClient client, 
 +                                 SilcSocketConnection sock,
 +                                 SilcPacketContext *packet)
 +{
 +  SilcBuffer buffer = packet->buffer;
 +  SilcPacketType type = packet->type;
 +
 +  SILC_LOG_DEBUG(("Parsing packet type %d", type));
 +
 +  /* Parse the packet type */
 +  switch(type) {
 +  case SILC_PACKET_DISCONNECT:
 +    silc_client_disconnected_by_server(client, sock, buffer);
 +    break;
 +  case SILC_PACKET_SUCCESS:
 +    /*
 +     * Success received for something. For now we can have only
 +     * one protocol for connection executing at once hence this
 +     * success message is for whatever protocol is executing currently.
 +     */
 +    if (sock->protocol) {
 +      sock->protocol->execute(client->timeout_queue, 0,
 +                            sock->protocol, sock->sock, 0, 0);
 +    }
 +    break;
 +  case SILC_PACKET_FAILURE:
 +    /*
 +     * Failure received for some protocol. Set the protocol state to 
 +     * error and call the protocol callback. This fill cause error on
 +     * protocol and it will call the final callback.
 +     */
 +    if (sock->protocol) {
 +      sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
 +      sock->protocol->execute(client->timeout_queue, 0,
 +                            sock->protocol, sock->sock, 0, 0);
 +    }
 +    break;
 +  case SILC_PACKET_REJECT:
 +    break;
 +
 +  case SILC_PACKET_NOTIFY:
 +    /*
 +     * Received notify message 
 +     */
 +    silc_client_notify_by_server(client, sock, buffer);
 +    break;
 +
 +  case SILC_PACKET_ERROR:
 +    /*
 +     * Received error message
 +     */
 +    silc_client_error_by_server(client, sock, buffer);
 +    break;
 +
 +  case SILC_PACKET_CHANNEL_MESSAGE:
 +    /*
 +     * Received message to (from, actually) a channel
 +     */
 +    silc_client_channel_message(client, sock, packet);
 +    break;
 +  case SILC_PACKET_CHANNEL_KEY:
 +    /*
 +     * Received key for a channel. By receiving this key the client will be
 +     * able to talk to the channel it has just joined. This can also be
 +     * a new key for existing channel as keys expire peridiocally.
 +     */
 +    silc_client_receive_channel_key(client, sock, buffer);
 +    break;
 +
 +  case SILC_PACKET_PRIVATE_MESSAGE:
 +    /*
 +     * Received private message
 +     */
-     if (!cipher && ((SilcClientWindow)sock->user_data)->send_key)
-       cipher = ((SilcClientWindow)sock->user_data)->send_key;
++    silc_client_private_message(client, sock, packet);
 +    break;
 +  case SILC_PACKET_PRIVATE_MESSAGE_KEY:
 +    /*
 +     * Received private message key
 +     */
 +    break;
 +
 +  case SILC_PACKET_COMMAND_REPLY:
 +    /*
 +     * Recived reply for a command
 +     */
 +    silc_client_command_reply_process(client, sock, packet);
 +    break;
 +
 +  case SILC_PACKET_KEY_EXCHANGE:
 +    if (sock->protocol) {
 +      SilcClientKEInternalContext *proto_ctx = 
 +      (SilcClientKEInternalContext *)sock->protocol->context;
 +
 +      proto_ctx->packet = buffer;
 +      proto_ctx->dest_id_type = packet->src_id_type;
 +      proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
 +
 +      /* Let the protocol handle the packet */
 +      sock->protocol->execute(client->timeout_queue, 0,
 +                            sock->protocol, sock->sock, 0, 0);
 +    } else {
 +      SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
 +                    "protocol active, packet dropped."));
 +
 +      /* XXX Trigger KE protocol?? Rekey actually! */
 +    }
 +    break;
 +
 +  case SILC_PACKET_KEY_EXCHANGE_1:
 +    if (sock->protocol) {
 +
 +    } else {
 +      SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
 +                    "protocol active, packet dropped."));
 +    }
 +    break;
 +  case SILC_PACKET_KEY_EXCHANGE_2:
 +    if (sock->protocol) {
 +      SilcClientKEInternalContext *proto_ctx = 
 +      (SilcClientKEInternalContext *)sock->protocol->context;
 +
 +      if (proto_ctx->packet)
 +      silc_buffer_free(proto_ctx->packet);
 +
 +      proto_ctx->packet = buffer;
 +      proto_ctx->dest_id_type = packet->src_id_type;
 +      proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
 +
 +      /* Let the protocol handle the packet */
 +      sock->protocol->execute(client->timeout_queue, 0,
 +                            sock->protocol, sock->sock, 0, 0);
 +    } else {
 +      SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
 +                    "protocol active, packet dropped."));
 +    }
 +    break;
 +
 +  case SILC_PACKET_NEW_ID:
 +    {
 +      /*
 +       * Received new ID from server. This packet is received at
 +       * the connection to the server.  New ID is also received when 
 +       * user changes nickname but in that case the new ID is received
 +       * as command reply and not as this packet type.
 +       */
 +      unsigned char *id_string;
 +      unsigned short id_type;
 +      
 +      silc_buffer_unformat(buffer,
 +                         SILC_STR_UI_SHORT(&id_type),
 +                         SILC_STR_UI16_STRING_ALLOC(&id_string),
 +                         SILC_STR_END);
 +      
 +      if ((SilcIdType)id_type != SILC_ID_CLIENT)
 +      break;
 +
 +      silc_client_receive_new_id(client, sock, id_string);
 +      silc_free(id_string);
 +      break;
 +    }
 +
 +  default:
 +    SILC_LOG_DEBUG(("Incorrect packet type %d, packet dropped", type));
 +    break;
 +  }
 +}
 +
 +/* Sends packet. This doesn't actually send the packet instead it assembles
 +   it and marks it to be sent. However, if force_send is TRUE the packet
 +   is sent immediately. if dst_id, cipher and hmac are NULL those parameters
 +   will be derived from sock argument. Otherwise the valid arguments sent
 +   are used. */
 +
 +void silc_client_packet_send(SilcClient client, 
 +                           SilcSocketConnection sock,
 +                           SilcPacketType type, 
 +                           void *dst_id,
 +                           SilcIdType dst_id_type,
 +                           SilcCipher cipher,
 +                           SilcHmac hmac,
 +                           unsigned char *data, 
 +                           unsigned int data_len, 
 +                           int force_send)
 +{
 +  SilcPacketContext packetdata;
 +
 +  SILC_LOG_DEBUG(("Sending packet, type %d", type));
 +
 +  /* Get data used in the packet sending, keys and stuff */
 +  if ((!cipher || !hmac || !dst_id) && sock->user_data) {
-     if (!hmac && ((SilcClientWindow)sock->user_data)->hmac)
-       hmac = ((SilcClientWindow)sock->user_data)->hmac;
++    if (!cipher && ((SilcClientConnection)sock->user_data)->send_key)
++      cipher = ((SilcClientConnection)sock->user_data)->send_key;
 +
-     if (!dst_id && ((SilcClientWindow)sock->user_data)->remote_id) {
-       dst_id = ((SilcClientWindow)sock->user_data)->remote_id;
++    if (!hmac && ((SilcClientConnection)sock->user_data)->hmac)
++      hmac = ((SilcClientConnection)sock->user_data)->hmac;
 +
-   if (((SilcClientWindow)sock->user_data)->local_id_data)
-     packetdata.src_id = ((SilcClientWindow)sock->user_data)->local_id_data;
++    if (!dst_id && ((SilcClientConnection)sock->user_data)->remote_id) {
++      dst_id = ((SilcClientConnection)sock->user_data)->remote_id;
 +      dst_id_type = SILC_ID_SERVER;
 +    }
 +  }
 +
 +  /* Set the packet context pointers */
 +  packetdata.flags = 0;
 +  packetdata.type = type;
-   SilcClientWindow win = (SilcClientWindow)sock->user_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;
 +  packetdata.src_id_type = SILC_ID_CLIENT;
 +  if (dst_id) {
 +    packetdata.dst_id = silc_id_id2str(dst_id, dst_id_type);
 +    packetdata.dst_id_len = silc_id_get_len(dst_id_type);
 +    packetdata.dst_id_type = dst_id_type;
 +  } else {
 +    packetdata.dst_id = NULL;
 +    packetdata.dst_id_len = 0;
 +    packetdata.dst_id_type = SILC_ID_NONE;
 +  }
 +  packetdata.rng = client->rng;
 +  packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
 +    packetdata.src_id_len + packetdata.dst_id_len;
 +  packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
 +
 +  /* Prepare outgoing data buffer for packet sending */
 +  silc_packet_send_prepare(sock, 
 +                         SILC_PACKET_HEADER_LEN +
 +                         packetdata.src_id_len + 
 +                         packetdata.dst_id_len,
 +                         packetdata.padlen,
 +                         data_len);
 +
 +  SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
 +
 +  packetdata.buffer = sock->outbuf;
 +
 +  /* Put the data to the buffer */
 +  if (data && data_len)
 +    silc_buffer_put(sock->outbuf, data, data_len);
 +
 +  /* Create the outgoing packet */
 +  silc_packet_assemble(&packetdata);
 +
 +  /* Encrypt the packet */
 +  if (cipher)
 +    silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
 +
 +  SILC_LOG_HEXDUMP(("Packet, len %d", sock->outbuf->len),
 +                 sock->outbuf->data, sock->outbuf->len);
 +
 +  /* Now actually send the packet */
 +  silc_client_packet_send_real(client, sock, force_send);
 +}
 +
 +/* Sends packet to a channel. Packet to channel is always encrypted
 +   differently from "normal" packets. SILC header of the packet is 
 +   encrypted with the next receiver's key and the rest of the packet is
 +   encrypted with the channel specific key. Padding and HMAC is computed
 +   with the next receiver's key. */
 +
 +void silc_client_packet_send_to_channel(SilcClient client, 
 +                                      SilcSocketConnection sock,
 +                                      SilcChannelEntry channel,
 +                                      unsigned char *data, 
 +                                      unsigned int data_len, 
 +                                      int force_send)
 +{
 +  int i;
-     silc_say(client, "Cannot talk to channel: key does not exist");
++  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
 +  SilcBuffer payload;
 +  SilcPacketContext packetdata;
 +  SilcCipher cipher;
 +  SilcHmac hmac;
 +  unsigned char *id_string;
 +
 +  SILC_LOG_DEBUG(("Sending packet to channel"));
 +
 +  if (!channel || !channel->key) {
-   payload = silc_channel_encode_payload(strlen(win->nickname), win->nickname,
++    client->ops->say(client, conn, 
++                   "Cannot talk to channel: key does not exist");
 +    return;
 +  }
 +
 +  /* Generate IV */
 +  if (!channel->iv)
 +    for (i = 0; i < 16; i++)
 +      channel->iv[i] = silc_rng_get_byte(client->rng);
 +  else
 +    silc_hash_make(client->md5hash, channel->iv, 16, channel->iv);
 +
 +  /* Encode the channel payload */
-     silc_say(client, 
-            "Error: Could not create packet to be sent to the channel");
++  payload = silc_channel_encode_payload(strlen(conn->nickname), conn->nickname,
 +                                      data_len, data, 16, channel->iv, 
 +                                      client->rng);
 +  if (!payload) {
-   cipher = win->send_key;
-   hmac = win->hmac;
++    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. */
-   packetdata.src_id = win->local_id_data;
++  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
 +     the Channel ID of the channel. Server and router will handle the
 +     distribution of the packet. */
 +  packetdata.flags = 0;
 +  packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
-   SilcClientWindow win = (SilcClientWindow)sock->user_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;
 +  packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
 +  packetdata.dst_id_type = SILC_ID_CHANNEL;
 +  packetdata.rng = client->rng;
 +  packetdata.truelen = payload->len + SILC_PACKET_HEADER_LEN + 
 +    packetdata.src_id_len + packetdata.dst_id_len;
 +  packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
 +                                        packetdata.src_id_len +
 +                                        packetdata.dst_id_len));
 +
 +  /* Prepare outgoing data buffer for packet sending */
 +  silc_packet_send_prepare(sock, 
 +                         SILC_PACKET_HEADER_LEN +
 +                         packetdata.src_id_len + 
 +                         packetdata.dst_id_len,
 +                         packetdata.padlen,
 +                         payload->len);
 +
 +  packetdata.buffer = sock->outbuf;
 +
 +  /* Encrypt payload of the packet. This is encrypted with the channel key. */
 +  channel->channel_key->cipher->encrypt(channel->channel_key->context,
 +                                      payload->data, payload->data,
 +                                      payload->len - 16, /* -IV_LEN */
 +                                      channel->iv);
 +
 +  /* Put the actual encrypted payload data into the buffer. */
 +  silc_buffer_put(sock->outbuf, payload->data, payload->len);
 +
 +  /* Create the outgoing packet */
 +  silc_packet_assemble(&packetdata);
 +
 +  /* Encrypt the header and padding of the packet. This is encrypted 
 +     with normal session key shared with our server. */
 +  silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN + 
 +                    packetdata.src_id_len + packetdata.dst_id_len +
 +                    packetdata.padlen);
 +
 +  SILC_LOG_HEXDUMP(("Packet to channel, len %d", sock->outbuf->len),
 +                 sock->outbuf->data, sock->outbuf->len);
 +
 +  /* Now actually send the packet */
 +  silc_client_packet_send_real(client, sock, force_send);
 +  silc_buffer_free(payload);
 +  silc_free(id_string);
 +}
 +
 +/* Sends private message to remote client. If private message key has
 +   not been set with this client then the message will be encrypted using
 +   normal session keys. Private messages are special packets in SILC
 +   network hence we need this own function for them. This is similiar
 +   to silc_client_packet_send_to_channel except that we send private
 +   message. */
 +
 +void silc_client_packet_send_private_message(SilcClient client,
 +                                           SilcSocketConnection sock,
 +                                           SilcClientEntry client_entry,
 +                                           unsigned char *data, 
 +                                           unsigned int data_len, 
 +                                           int force_send)
 +{
-   nick_len = strlen(client->current_win->nickname);
++  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
 +  SilcBuffer buffer;
 +  SilcPacketContext packetdata;
 +  unsigned int nick_len;
 +  SilcCipher cipher;
 +  SilcHmac hmac;
 +
 +  SILC_LOG_DEBUG(("Sending private message"));
 +
 +  /* Create private message payload */
-                    SILC_STR_UI_XNSTRING(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),
-   hmac = win->hmac;
++                   SILC_STR_UI_XNSTRING(conn->nickname,
 +                                        nick_len),
 +                   SILC_STR_UI_XNSTRING(data, data_len),
 +                   SILC_STR_END);
 +
 +  /* If we don't have private message specific key then private messages
 +     are just as any normal packet thus call normal packet sending.  If
 +     the key exist then the encryption process is a bit different and
 +     will be done in the rest of this function. */
 +  if (!client_entry->send_key) {
 +    silc_client_packet_send(client, sock, SILC_PACKET_PRIVATE_MESSAGE,
 +                          client_entry->id, SILC_ID_CLIENT, NULL, NULL,
 +                          buffer->data, buffer->len, force_send);
 +    goto out;
 +  }
 +
 +  /* We have private message specific key */
 +
 +  /* Get data used in the encryption */
 +  cipher = client_entry->send_key;
-   packetdata.src_id = win->local_id_data;
++  hmac = conn->hmac;
 +
 +  /* Set the packet context pointers. */
 +  packetdata.flags = 0;
 +  packetdata.type = SILC_PACKET_PRIVATE_MESSAGE;
-     packetdata.dst_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
-   SilcClientWindow win;
++    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;
 +  packetdata.truelen = buffer->len + SILC_PACKET_HEADER_LEN + 
 +    packetdata.src_id_len + packetdata.dst_id_len;
 +  packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
 +                                        packetdata.src_id_len +
 +                                        packetdata.dst_id_len));
 +
 +  /* Prepare outgoing data buffer for packet sending */
 +  silc_packet_send_prepare(sock, 
 +                         SILC_PACKET_HEADER_LEN +
 +                         packetdata.src_id_len + 
 +                         packetdata.dst_id_len,
 +                         packetdata.padlen,
 +                         buffer->len);
 +  
 +  packetdata.buffer = sock->outbuf;
 +
 +  /* Encrypt payload of the packet. Encrypt with private message specific
 +     key if it exist, otherwise with session key. */
 +  cipher->cipher->encrypt(cipher->context, buffer->data, buffer->data,
 +                        buffer->len, cipher->iv);
 +      
 +  /* Put the actual encrypted payload data into the buffer. */
 +  silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
 +
 +  /* Create the outgoing packet */
 +  silc_packet_assemble(&packetdata);
 +
 +  /* Encrypt the header and padding of the packet. */
 +  silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN + 
 +                    packetdata.src_id_len + packetdata.dst_id_len +
 +                    packetdata.padlen);
 +
 +  SILC_LOG_HEXDUMP(("Private message packet, len %d", sock->outbuf->len),
 +                 sock->outbuf->data, sock->outbuf->len);
 +
 +  /* Now actually send the packet */
 +  silc_client_packet_send_real(client, sock, force_send);
 +  silc_free(packetdata.dst_id);
 +
 + out:
 +  silc_free(buffer);
 +}     
 +
 +/* Closes connection to remote end. Free's all allocated data except
 +   for some information such as nickname etc. that are valid at all time. */
 +
 +void silc_client_close_connection(SilcClient client,
 +                                SilcSocketConnection sock)
 +{
-   silc_say(client, "Closed connection to host %s", sock->hostname ?
-          sock->hostname : sock->ip);
++  SilcClientConnection conn;
 +
 +  /* We won't listen for this connection anymore */
 +  silc_schedule_unset_listen_fd(sock->sock);
 +
 +  /* Unregister all tasks */
 +  silc_task_unregister_by_fd(client->io_queue, sock->sock);
 +  silc_task_unregister_by_fd(client->timeout_queue, sock->sock);
 +
 +  /* Close the actual connection */
 +  silc_net_close_connection(sock->sock);
 +
-     win = (SilcClientWindow)sock->user_data;
++  client->ops->say(client, sock->user_data,
++                 "Closed connection to host %s", sock->hostname ?
++                 sock->hostname : sock->ip);
 +
 +  /* Free everything */
 +  if (sock->user_data) {
-     silc_idcache_del_all(win->client_cache);
-     silc_idcache_del_all(win->channel_cache);
++    conn = (SilcClientConnection)sock->user_data;
 +
 +    /* XXX Free all client entries and channel entries. */
 +
 +    /* Clear ID caches */
-     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);
++    silc_idcache_del_all(conn->client_cache);
++    silc_idcache_del_all(conn->channel_cache);
 +
 +    /* Free data */
-     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;
++    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);
 +    }
 +
-   silc_say(client, msg);
++    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) {
 +    silc_protocol_free(sock->protocol);
 +    sock->protocol = NULL;
 +  }
 +  silc_socket_free(sock);
 +}
 +
 +/* Called when we receive disconnection packet from server. This 
 +   closes our end properly and displays the reason of the disconnection
 +   on the screen. */
 +
 +void silc_client_disconnected_by_server(SilcClient client,
 +                                      SilcSocketConnection sock,
 +                                      SilcBuffer message)
 +{
 +  char *msg;
 +
 +  SILC_LOG_DEBUG(("Server disconnected us, sock %d", sock->sock));
 +
 +  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);
 +  silc_client_close_connection(client, sock);
 +}
 +
 +/* Received error message from server. Display it on the screen. 
 +   We don't take any action what so ever of the error message. */
 +
 +void silc_client_error_by_server(SilcClient client,
 +                               SilcSocketConnection sock,
 +                               SilcBuffer message)
 +{
 +  char *msg;
 +
 +  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);
 +}
 +
 +/* Received notify message from server */
 +
 +void silc_client_notify_by_server(SilcClient client,
 +                                SilcSocketConnection sock,
 +                                SilcBuffer message)
 +{
 +  char *msg;
 +
 +  msg = silc_calloc(message->len + 1, sizeof(char));
 +  memcpy(msg, message->data, message->len);
-   SilcClientWindow win = (SilcClientWindow)sock->user_data;
++  client->ops->say(client, sock->user_data, msg);
 +  silc_free(msg);
 +}
 +
 +/* Processes the received new Client ID from server. Old Client ID is
 +   deleted from cache and new one is added. */
 +
 +void silc_client_receive_new_id(SilcClient client,
 +                              SilcSocketConnection sock,
 +                              unsigned char *id_string)
 +{
-   silc_idcache_del_by_id(win->client_cache, SILC_ID_CLIENT, win->local_id);
++  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
 +
 +  /* Delete old ID from ID cache */
-   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 = 
++  silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT, conn->local_id);
 +  
 +  /* Save the new ID */
-   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;
++  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));
-   silc_idcache_add(win->client_cache, win->nickname, SILC_ID_CLIENT,
-                  win->local_id, (void *)win->local_entry, TRUE);
++  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 */
-   SilcClientWindow win = (SilcClientWindow)sock->user_data;
++  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
 +   joins to channel and server replies with channel ID. The ID is cached. */
 +
 +void silc_client_new_channel_id(SilcClient client,
 +                              SilcSocketConnection sock,
 +                              char *channel_name,
 +                              unsigned int mode,
 +                              unsigned char *id_string)
 +{
-   win->current_channel = channel;
++  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
 +  SilcChannelID *id;
 +  SilcChannelEntry channel;
 +
 +  SILC_LOG_DEBUG(("New channel ID"));
 +
 +  id = silc_id_str2id(id_string, SILC_ID_CHANNEL);
 +  channel = silc_calloc(1, sizeof(*channel));
 +  channel->channel_name = channel_name;
 +  channel->id = id;
 +  channel->mode = mode;
-   silc_idcache_add(win->channel_cache, channel_name, SILC_ID_CHANNEL,
++  conn->current_channel = channel;
 +  
 +  /* Put it to the ID cache */
-   SilcClientWindow win = (SilcClientWindow)sock->user_data;
++  silc_idcache_add(conn->channel_cache, channel_name, SILC_ID_CHANNEL,
 +                 (void *)id, (void *)channel, TRUE);
 +}
 +
 +/* Processes received key for channel. The received key will be used
 +   to protect the traffic on the channel for now on. Client must receive
 +   the key to the channel before talking on the channel is possible. 
 +   This is the key that server has generated, this is not the channel
 +   private key, it is entirely local setting. */
 +
 +void silc_client_receive_channel_key(SilcClient client,
 +                                   SilcSocketConnection sock,
 +                                   SilcBuffer packet)
 +{
 +  unsigned char *id_string, *key, *cipher;
 +  unsigned int key_len;
-   if (!silc_idcache_find_by_id_one(win->channel_cache, (void *)id,
++  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
 +  SilcChannelID *id;
 +  SilcIDCacheEntry id_cache = NULL;
 +  SilcChannelEntry channel;
 +  SilcChannelKeyPayload payload;
 +
 +  SILC_LOG_DEBUG(("Received key for channel"));
 +  
 +  payload = silc_channel_key_parse_payload(packet);
 +  if (!payload)
 +    return;
 +
 +  id_string = silc_channel_key_get_id(payload, NULL);
 +  if (!id_string) {
 +    silc_channel_key_free_payload(payload);
 +    return;
 +  }
 +  id = silc_id_str2id(id_string, SILC_ID_CHANNEL);
 +
 +  /* Find channel. */
-     silc_say(client, "Cannot talk to channel: unsupported cipher %s", cipher);
++  if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
 +                                 SILC_ID_CHANNEL, &id_cache))
 +    goto out;
 +  
 +  /* Save the key */
 +  key = silc_channel_key_get_key(payload, &key_len);
 +  cipher = silc_channel_key_get_cipher(payload, NULL);
 +
 +  channel = (SilcChannelEntry)id_cache->context;
 +  channel->key_len = key_len;
 +  channel->key = silc_calloc(key_len, sizeof(*channel->key));
 +  memcpy(channel->key, key, key_len);
 +
 +  silc_cipher_alloc(cipher, &channel->channel_key);
 +  if (!channel->channel_key) {
-   SilcClientWindow win = (SilcClientWindow)sock->user_data;
++    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, 
 +                                      key, key_len);
 +
 +  /* Client is now joined to the channel */
 +  channel->on_channel = TRUE;
 +
 + out:
 +  silc_free(id);
 +  silc_channel_key_free_payload(payload);
 +}
 +
 +/* Process received message to a channel (or from a channel, really). This
 +   decrypts the channel message with channel specific key and parses the
 +   channel payload. Finally it displays the message on the screen. */
 +
 +void silc_client_channel_message(SilcClient client, 
 +                               SilcSocketConnection sock, 
 +                               SilcPacketContext *packet)
 +{
-   if (!silc_idcache_find_by_id_one(win->channel_cache, (void *)id,
++  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
 +  SilcBuffer buffer = packet->buffer;
 +  SilcChannelPayload payload = NULL;
 +  SilcChannelID *id = NULL;
 +  SilcChannelEntry channel;
 +  SilcIDCacheEntry id_cache = NULL;
 +
 +  /* Sanity checks */
 +  if (packet->dst_id_type != SILC_ID_CHANNEL)
 +    goto out;
 +
 +  id = silc_id_str2id(packet->dst_id, SILC_ID_CHANNEL);
 +
 +  /* Find the channel entry from channels on this window */
-   /* Display the message on screen */
++  if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
 +                                 SILC_ID_CHANNEL, &id_cache))
 +    goto out;
 +
 +  channel = (SilcChannelEntry)id_cache->context;
 +
 +  /* Decrypt the channel message payload. Push the IV out of the way,
 +     since it is not encrypted (after pushing buffer->tail has the IV). */
 +  silc_buffer_push_tail(buffer, 16);
 +  channel->channel_key->cipher->decrypt(channel->channel_key->context,
 +                                      buffer->data, buffer->data,
 +                                      buffer->len, buffer->tail);
 +  silc_buffer_pull_tail(buffer, 16);
 +
 +  /* Parse the channel message payload */
 +  payload = silc_channel_parse_payload(buffer);
 +  if (!payload)
 +    goto out;
 +
-     /* 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));
++  /* Pass the message to application */
 +  if (packet->src_id_type == SILC_ID_CLIENT) {
-     silc_say(client, "%s", 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 */
++    /* XXX maybe this should be passed to app... */
++    client->ops->say(client, conn, "%s", silc_channel_get_data(payload, NULL));
 +  }
 +
 + out:
 +  if (id)
 +    silc_free(id);
 +  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);
++}
index b4a5a0e213e09a3f8ae099a72886fe60e5be8f44,661bd83a481604aad891dc11bc2d0263043af5ca..8caffe812c574e61d749268d4635165b25b3ef25
  #ifndef CLIENT_H
  #define CLIENT_H
  
 -/* 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. */
 -typedef struct {
 +/* 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
 +   as been received. */
 +typedef struct SilcClientPingStruct {
 +  time_t start_time;
 +  void *dest_id;
 +  char *dest_name;
 +} SilcClientPing;
 +
 +/* Structure to hold away messages set by user. This is mainly created
 +   for future extensions where away messages could be set according filters
 +   such as nickname and hostname. For now only one away message can 
 +   be set in one connection. */
 +typedef struct SilcClientAwayStruct {
 +  char *away;
 +  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 
     */
    char *remote_host;
    int remote_port;
    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;
    /* Client ID and Channel ID cache. Messages transmitted in SILC network
       are done using different unique ID's. These are the cache for
       thoses ID's used in the communication. */
 -  SilcIDCache *client_id_cache[96];
 -  unsigned int client_id_cache_count[96];
 -  SilcIDCache *channel_id_cache[96];
 -  unsigned int channel_id_cache_count[96];
 -  SilcIDCache *server_id_cache;
 -  unsigned int server_id_cache_count;
 -
 -  /* Current channel on window. All channel's are saved (allocated) into
 +  SilcIDCache client_cache;
 +  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;
  
       referencing (sock->user_data). */
    SilcSocketConnection sock;
  
 -  /* The actual physical screen. This data is handled by the
 -     screen handling routines. */
 -  void *screen;
 -} *SilcClientWindow;
 +  /* Requested pings. */
 +  SilcClientPing *ping;
 +  unsigned int ping_count;
 +
 +  /* 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.
++   */
 -typedef struct {
++  /* 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;
  
 -  /* 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;
 -
--  /* 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
 -} SilcClientObject;
 -
 -typedef SilcClientObject *SilcClient;
 +};
  
  /* 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 -174,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 -246,4 +273,7 @@@ void silc_client_receive_channel_key(Si
  void silc_client_channel_message(SilcClient client, 
                                 SilcSocketConnection sock, 
                                 SilcPacketContext *packet);
++void silc_client_private_message(SilcClient client, 
++                               SilcSocketConnection sock, 
++                               SilcPacketContext *packet);
  #endif
index c64f44f932ed4719a1fc0be59b7130d1f76bccab,0000000000000000000000000000000000000000..1784d4e8779d376f311e57b50c5dd8bc94150e89
mode 100644,000000..100644
--- /dev/null
@@@ -1,937 -1,0 +1,757 @@@
- /*
-  * $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"
 +/*
 +
 +  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.
 +
 +*/
-   /*
-    * 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},
++/* $Id$ */
++
++#include "clientlibincludes.h"
 +
 +/* Client command list. */
 +SilcClientCommand silc_command_list[] =
 +{
 +  SILC_CLIENT_CMD(whois, WHOIS, "WHOIS", SILC_CF_LAG | SILC_CF_REG, 3),
 +  SILC_CLIENT_CMD(whowas, WHOWAS, "WHOWAS", SILC_CF_LAG | SILC_CF_REG, 3),
 +  SILC_CLIENT_CMD(identify, IDENTIFY, "IDENTIFY", 
 +                SILC_CF_LAG | SILC_CF_REG, 3),
 +  SILC_CLIENT_CMD(nick, NICK, "NICK", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(list, LIST, "LIST", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(topic, TOPIC, "TOPIC", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(invite, INVITE, "INVITE", SILC_CF_LAG | SILC_CF_REG, 3),
 +  SILC_CLIENT_CMD(quit, QUIT, "QUIT", SILC_CF_LAG | SILC_CF_REG, 1),
 +  SILC_CLIENT_CMD(kill, KILL, "KILL", 
 +                SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
 +  SILC_CLIENT_CMD(info, INFO, "INFO", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(connect, CONNECT, "CONNECT",
 +                SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
 +  SILC_CLIENT_CMD(ping, PING, "PING", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(oper, OPER, "OPER",
 +                SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
 +  SILC_CLIENT_CMD(join, JOIN, "JOIN", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(motd, MOTD, "MOTD", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(umode, UMODE, "UMODE", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(cmode, CMODE, "CMODE", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(kick, KICK, "KICK", SILC_CF_LAG | SILC_CF_REG, 2),
 +  SILC_CLIENT_CMD(restart, RESTART, "RESTART",
 +                SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
 +  SILC_CLIENT_CMD(close, CLOSE, "CLOSE",
 +                SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
 +  SILC_CLIENT_CMD(die, DIE, "DIE",
 +                SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
 +  SILC_CLIENT_CMD(silcoper, SILCOPER, "SILOPER",
 +                SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER, 2),
 +  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),
 +
- #define SILC_NOT_CONNECTED(x) \
-   silc_say((x), "You are not connected to a server, use /SERVER to connect");
++  { NULL, 0, NULL, 0, 0 },
 +};
 +
- static void silc_client_command_free(SilcClientCommandContext cmd)
++#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.
 +
 +   The argument `reply_cmd' is the command reply from where the callback
 +   function is to be called, thus, it IS NOT the command to be executed.
 +
 +   XXX: If needed in the future this support may be extended for
 +   commands as well, when any command could be executed after executing
 +   some specific command. */
 +
 +void silc_client_command_pending(SilcCommand reply_cmd,
 +                               SilcClientCommandCallback callback,
 +                               void *context)
 +{
 +  SilcClientCommandPending *reply, *r;
 +
 +  reply = silc_calloc(1, sizeof(*reply));
 +  reply->reply_cmd = reply_cmd;
 +  reply->context = context;
 +  reply->callback = callback;
 +
 +  if (silc_command_pending == NULL) {
 +    silc_command_pending = reply;
 +    return;
 +  }
 +
 +  for (r = silc_command_pending; r; r = r->next) {
 +    if (r->next == NULL) {
 +      r->next = reply;
 +      break;
 +    }
 +  }
 +}
 +
 +/* Deletes pending command by reply command type. */
 +
 +void silc_client_command_pending_del(SilcCommand reply_cmd)
 +{
 +  SilcClientCommandPending *r, *tmp;
 +  
 +  if (silc_command_pending) {
 +    if (silc_command_pending->reply_cmd == reply_cmd) {
 +      silc_free(silc_command_pending);
 +      silc_command_pending = NULL;
 +      return;
 +    }
 +
 +    for (r = silc_command_pending; r; r = r->next) {
 +      if (r->next && r->next->reply_cmd == reply_cmd) {
 +      tmp = r->next;
 +      r->next = r->next->next;
 +      silc_free(tmp);
 +      break;
 +      }
 +    }
 +  }
 +}
 +
 +/* Free command context and its internals */
 +
-   if (cmd->argc < 2 || cmd->argc > 3) {
-     silc_say(cmd->client, "Usage: /WHOIS <nickname>[@<server>] [<count>]");
++void silc_client_command_free(SilcClientCommandContext cmd)
 +{
 +  int i;
 +
 +  if (cmd) {
 +    for (i = 0; i < cmd->argc; i++)
 +      silc_free(cmd->argv[i]);
 +    silc_free(cmd);
 +  }
 +}
 +
 +/* Command WHOIS. This command is used to query information about 
 +   specific user. */
 +
 +SILC_CLIENT_CMD_FUNC(whois)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
++  SilcClientConnection conn = cmd->conn;
 +  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;
 +  }
 +
-   silc_client_packet_send(cmd->client, cmd->client->current_win->sock,
++  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);
-   if (cmd->argc < 2 || cmd->argc > 3) {
-     silc_say(cmd->client, "Usage: /IDENTIFY <nickname>[@<server>] [<count>]");
++  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--;
 +
++  /* Notify application */
++  COMMAND;
++
 + out:
 +  silc_client_command_free(cmd);
 +}
 +
 +SILC_CLIENT_CMD_FUNC(whowas)
 +{
 +}
 +
 +/* Command IDENTIFY. This command is used to query information about 
 +   specific user, especially ID's. */
 +
 +SILC_CLIENT_CMD_FUNC(identify)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
++  SilcClientConnection conn = cmd->conn;
 +  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;
 +  }
 +
-   silc_client_packet_send(cmd->client, cmd->client->current_win->sock,
++  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);
-   SilcClientWindow win = NULL;
++  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--;
 +
++  /* Notify application */
++  COMMAND;
++
 + out:
 +  silc_client_command_free(cmd);
 +}
 +
 +/* Command NICK. Shows current nickname/sets new nickname on current
 +   window. */
 +
 +SILC_CLIENT_CMD_FUNC(nick)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-   if (!cmd->sock) {
-     SILC_NOT_CONNECTED(cmd->client);
++  SilcClientConnection conn = cmd->conn;
 +  SilcBuffer buffer;
 +
-     if (cmd->sock) {
-       silc_say(cmd->client, "Your nickname is %s on server %s", 
-              win->nickname, win->remote_host);
++  if (!cmd->conn) {
++    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
++    COMMAND_ERROR;
 +    goto out;
 +  }
 +
 +  /* Show current nickname */
 +  if (cmd->argc < 2) {
-       silc_say(cmd->client, "Your nickname is %s", win->nickname);
++    if (cmd->conn) {
++      cmd->client->ops->say(cmd->client, conn, 
++                          "Your nickname is %s on server %s", 
++                          conn->nickname, conn->remote_host);
 +    } else {
-   win = (SilcClientWindow)cmd->sock->user_data;
++      cmd->client->ops->say(cmd->client, conn, 
++                          "Your nickname is %s", conn->nickname);
 +    }
++    /* XXX Notify application */
++    COMMAND;
 +    goto out;
 +  }
 +
-   silc_client_packet_send(cmd->client, cmd->sock,
 +  /* Set new nickname */
 +  buffer = silc_command_encode_payload(SILC_COMMAND_NICK,
 +                                     cmd->argc - 1, ++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;
-   }
++  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--;
-   /* 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);
++  if (conn->nickname)
++    silc_free(conn->nickname);
++  conn->nickname = strdup(cmd->argv[1]);
 +
-   SilcClientWindow win = NULL;
++  /* Notify application */
++  COMMAND;
 +
 + out:
 +  silc_client_command_free(cmd);
 +}
 +
 +SILC_CLIENT_CMD_FUNC(list)
 +{
 +}
 +
 +SILC_CLIENT_CMD_FUNC(topic)
 +{
 +}
 +
 +/* Command INVITE. Invites specific client to join a channel. */
 +
 +SILC_CLIENT_CMD_FUNC(invite)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
 +  SilcClient client = cmd->client;
-   if (cmd->argc != 3) {
-     silc_say(cmd->client, "Usage: /INVITE <nickname>[@<server>] <channel>");
++  SilcClientConnection conn = cmd->conn;
 +  SilcClientEntry client_entry;
 +  SilcChannelEntry channel_entry;
 +  SilcBuffer buffer;
 +  unsigned int num = 0;
 +  char *nickname = NULL, *server = NULL;
 +  unsigned char *client_id, *channel_id;
 +
-   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;
 +  }
 +
-   win = (SilcClientWindow)cmd->sock->user_data;
++  if (cmd->argc != 3) {
++    cmd->client->ops->say(cmd->client, conn,
++                        "Usage: /INVITE <nickname>[@<server>] <channel>");
++    COMMAND_ERROR;
 +    goto out;
 +  }
 +
-   if (!silc_client_parse_nickname(cmd->argv[1], &nickname, &server, &num)) {
-     silc_say(cmd->client, "Bad nickname");
 +  /* Parse the typed nickname. */
-   client_entry = silc_idlist_get_client(client, win, nickname, server, num);
++  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 */
-   channel_entry = silc_idlist_get_channel(client, win, cmd->argv[2]);
++  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_command_invite, context);
 +    return;
 +  }
 +  
 +  client_id = silc_id_id2str(client_entry->id, SILC_ID_CLIENT);
 +
 +  /* Find channel entry */
-     silc_say(cmd->client, "You are not on that channel");
++  channel_entry = silc_idlist_get_channel(client, conn, cmd->argv[2]);
 +  if (!channel_entry) {
-   silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
++    cmd->client->ops->say(cmd->client, conn, "You are not on that channel");
 +    silc_free(client_id);
++    COMMAND_ERROR;
 +    goto out;
 +  }
 +
 +  channel_id = silc_id_id2str(channel_entry->id, SILC_ID_CHANNEL);
 +
 +  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_say(cmd->client, "Inviting %s to channel %s", cmd->argv[1], 
-          cmd->argv[2]);
++  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
 +                        0, NULL, NULL, buffer->data, buffer->len, TRUE);
 +  silc_buffer_free(buffer);
 +
-   if (!cmd->client->current_win->sock) {
-     SILC_NOT_CONNECTED(cmd->client);
++  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);
 +}
 +
 +/* Command QUIT. Closes connection with current server. */
 + 
 +SILC_CLIENT_CMD_FUNC(quit)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
 +  SilcBuffer buffer;
 +
-   silc_client_packet_send(cmd->client, cmd->sock, SILC_PACKET_COMMAND, NULL, 
-                         0, NULL, NULL, buffer->data, buffer->len, TRUE);
++  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_close_connection(cmd->client, cmd->sock);
-   cmd->client->screen->bottom_line->connection = NULL;
-   silc_screen_print_bottom_line(cmd->client->screen, 0);
++  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 */
-   SilcClientWindow win = NULL;
++  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);
 +}
 +
 +SILC_CLIENT_CMD_FUNC(kill)
 +{
 +}
 +
 +/* Command INFO. Request information about specific server. If specific
 +   server is not provided the current server is used. */
 +
 +SILC_CLIENT_CMD_FUNC(info)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-   if (!cmd->sock) {
-     SILC_NOT_CONNECTED(cmd->client);
++  SilcClientConnection conn = cmd->conn;
 +  SilcBuffer buffer;
 +  char *name;
 +
-   win = (SilcClientWindow)cmd->sock->user_data;
++  if (!cmd->conn) {
++    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
++    COMMAND_ERROR;
 +    goto out;
 +  }
 +
-     name = strdup(win->remote_host);
 +  if (cmd->argc < 2)
-   silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
++    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));
-   SilcClientWindow win = 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);
 +}
 +
 +SILC_CLIENT_CMD_FUNC(connect)
 +{
 +}
 +
 +/* Command PING. Sends ping to server. This is used to test the 
 +   communication channel. */
 +
 +SILC_CLIENT_CMD_FUNC(ping)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-   if (!cmd->sock) {
-     SILC_NOT_CONNECTED(cmd->client);
++  SilcClientConnection conn = cmd->conn;
 +  SilcBuffer buffer;
 +  void *id;
 +  int i;
 +  char *name = NULL;
 +
-   win = (SilcClientWindow)cmd->sock->user_data;
-   if (cmd->argc == 1 || !strcmp(cmd->argv[1], win->remote_host))
-     name = strdup(win->remote_host);
++  if (!cmd->conn) {
++    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
++    COMMAND_ERROR;
 +    goto out;
 +  }
 +
-   id = silc_id_str2id(win->remote_id_data, SILC_ID_SERVER);
++  if (cmd->argc == 1 || !strcmp(cmd->argv[1], conn->remote_host))
++    name = strdup(conn->remote_host);
 +
-                                         1, win->remote_id_data, 
++  id = silc_id_str2id(conn->remote_id_data, SILC_ID_SERVER);
 +
 +  /* Send the command */
 +  buffer = silc_command_encode_payload_va(SILC_COMMAND_PING, 1, 
-   silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
++                                        1, conn->remote_id_data, 
 +                                        SILC_ID_SERVER_LEN);
-   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++;
++  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 */
-   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++;
++  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;
 +    }
 +  }
-   SilcClientWindow win = NULL;
++  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);
 +}
 +
 +SILC_CLIENT_CMD_FUNC(oper)
 +{
 +}
 +
 +SILC_CLIENT_CMD_FUNC(trace)
 +{
 +}
 +
 +SILC_CLIENT_CMD_FUNC(notice)
 +{
 +}
 +
 +/* Command JOIN. Joins to a channel. */
 +
 +SILC_CLIENT_CMD_FUNC(join)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-   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;
-     }
++  SilcClientConnection conn = cmd->conn;
 +  SilcIDCacheEntry id_cache = NULL;
 +  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;
 +  }
 +
-   win = (SilcClientWindow)cmd->sock->user_data;
++  if (cmd->argc < 2) {
++    /* Show channels currently joined to */
++
 +    goto out;
 +  }
 +
-   if (silc_idcache_find_by_data_one(win->channel_cache, cmd->argv[1],
 +  /* See if we have joined to the requested channel already */
-     silc_say(cmd->client, "You are talking to channel %s", cmd->argv[1]);
-     win->current_channel = (SilcChannelEntry)id_cache->context;
++  if (silc_idcache_find_by_data_one(conn->channel_cache, cmd->argv[1],
 +                                  &id_cache)) {
-   silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
++    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;
 +  }
 +
 +  /* Send JOIN command to the server */
 +  buffer = silc_command_encode_payload(SILC_COMMAND_JOIN,
 +                                     cmd->argc - 1, ++cmd->argv,
 +                                     ++cmd->argv_lens, ++cmd->argv_types);
-   SilcClientWindow win = 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);
 +}
 +
 +SILC_CLIENT_CMD_FUNC(motd)
 +{
 +}
 +
 +SILC_CLIENT_CMD_FUNC(umode)
 +{
 +}
 +
 +SILC_CLIENT_CMD_FUNC(cmode)
 +{
 +}
 +
 +SILC_CLIENT_CMD_FUNC(kick)
 +{
 +}
 +
 +SILC_CLIENT_CMD_FUNC(restart)
 +{
 +}
 + 
 +SILC_CLIENT_CMD_FUNC(close)
 +{
 +}
 + 
 +SILC_CLIENT_CMD_FUNC(die)
 +{
 +}
 + 
 +SILC_CLIENT_CMD_FUNC(silcoper)
 +{
 +}
 +
 +/* LEAVE command. Leaves a channel. Client removes itself from a channel. */
 +
 +SILC_CLIENT_CMD_FUNC(leave)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-   if (cmd->argc != 2) {
-     silc_say(cmd->client, "Usage: /LEAVE <channel>");
++  SilcClientConnection conn = cmd->conn;
 +  SilcIDCacheEntry id_cache = NULL;
 +  SilcChannelEntry channel;
 +  SilcBuffer buffer;
 +  unsigned char *id_string;
 +  char *name;
 +
-   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;
 +  }
 +
-   win = (SilcClientWindow)cmd->sock->user_data;
++  if (cmd->argc != 2) {
++    cmd->client->ops->say(cmd->client, conn, "Usage: /LEAVE <channel>");
++    COMMAND_ERROR;
 +    goto out;
 +  }
 +
-     if (!win->current_channel) {
-       silc_say(cmd->client, "You are not on any chanenl");
 +  if (cmd->argv[1][0] == '*') {
-     name = win->current_channel->channel_name;
++    if (!conn->current_channel) {
++      cmd->client->ops->say(cmd->client, conn, "You are not on any chanenl");
++      COMMAND_ERROR;
 +      goto out;
 +    }
-   if (!win->current_channel) {
-     silc_say(cmd->client, "You are not on that channel");
++    name = conn->current_channel->channel_name;
 +  } else {
 +    name = cmd->argv[1];
 +  }
 +
-   if (!silc_idcache_find_by_data_one(win->channel_cache, name, &id_cache)) {
-     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 */
-   silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
++  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;
 +  }
 +
 +  channel = (SilcChannelEntry)id_cache->context;
 +
 +  /* Send LEAVE command to the server */
 +  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_say(cmd->client, "You have left channel %s", name);
++  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 */
-   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;
-   }
++  cmd->client->ops->say(cmd->client, conn, "You have left channel %s", name);
 +
-   silc_idcache_del_by_id(win->channel_cache, SILC_ID_CHANNEL, channel->id);
++  conn->current_channel = NULL;
 +
-   SilcClientWindow win = NULL;
++  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);
 +  silc_cipher_free(channel->channel_key);
 +  silc_free(channel);
 +  silc_free(id_string);
 +
++  /* Notify application */
++  COMMAND;
++
 + out:
 +  silc_client_command_free(cmd);
 +}
 +
 +/* Command NAMES. Requests the names of the clients joined on requested
 +   channel. */
 +
 +SILC_CLIENT_CMD_FUNC(names)
 +{
 +  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-   if (cmd->argc != 2) {
-     silc_say(cmd->client, "Usage: /NAMES <channel>");
++  SilcClientConnection conn = cmd->conn;
 +  SilcIDCacheEntry id_cache = NULL;
 +  SilcBuffer buffer;
 +  char *name;
 +  unsigned char *id_string;
 +
-   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;
 +  }
 +
-   win = (SilcClientWindow)cmd->sock->user_data;
++  if (cmd->argc != 2) {
++    cmd->client->ops->say(cmd->client, conn, "Usage: /NAMES <channel>");
++    COMMAND_ERROR;
 +    goto out;
 +  }
 +
-     name = win->current_channel->channel_name;
 +  if (cmd->argv[1][0] == '*')
-   if (!silc_idcache_find_by_data_one(win->channel_cache, name, &id_cache)) {
++    name = conn->current_channel->channel_name;
 +  else
 +    name = cmd->argv[1];
 +
 +  /* Get the Channel ID of the channel */
-     silc_say(cmd->client, "You are not on that channel", name);
++  if (!silc_idcache_find_by_data_one(conn->channel_cache, name, &id_cache)) {
 +    /* XXX should resolve the channel ID; LIST command */
-   silc_client_packet_send(cmd->client, win->sock, SILC_PACKET_COMMAND, NULL, 
++    cmd->client->ops->say(cmd->client, conn, 
++                        "You are not on that channel", name);
++    COMMAND_ERROR;
 +    goto out;
 +  }
 +
 +  /* Send NAMES command to the server */
 +  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);
-  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);
-   }
++  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);
 +
 +  /* Register dummy pending command that will tell the reply command
 +     that user called this command. Server may send reply to this command
 +     even if user did not send this command thus we want to handle things
 +     differently when user sent the command. This is dummy and won't be
 +     execute. */
 +  /* XXX this is kludge and should be removed after pending command reply 
 +     support is added. Currently only commands may be pending not command
 +     replies. */
 +  silc_client_command_pending(SILC_COMMAND_NAMES, 
 +                            silc_client_command_names, NULL);
 +
++  /* Notify application */
++  COMMAND;
 +
 + out:
 +  silc_client_command_free(cmd);
 +}
index 19db809b936484e47199e6a98002ca3dbce81cfb,19db809b936484e47199e6a98002ca3dbce81cfb..edb714505f12fec5e244134fa14487939455a3c7
@@@ -66,7 -66,7 +66,8 @@@ typedef void (*SilcClientCommandCallbac
  /* 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 -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 -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 -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
index 8d78b329df65c3a48931b1b88298ed24a892d372,491808ec8e8bfee1fb062ab945e5cd3d5302a46d..604389376afaa09e6ac221de992ab4d4e297ddff
   * 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.
 - *    Importet from internal CVS/Added Log headers.
-- *
-- *
-- */
++/* $Id$ */
  
--#include "clientincludes.h"
++#include "clientlibincludes.h"
  
  /* Client command reply list. */
  SilcClientCommandReply silc_command_reply_list[] =
@@@ -74,7 -45,7 +38,6 @@@
    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 -111,6 +104,16 @@@ const SilcCommandStatusMessage silc_com
    { 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 -172,11 +177,12 @@@ void silc_client_command_reply_free(Sil
  
  /* 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;
 -  SilcClient client = cmd->client;
++  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
    SilcCommandStatus status;
    unsigned char *tmp;
  
    SILC_GET16_MSB(status, tmp);
    if (status != SILC_STATUS_OK) {
      if (status == SILC_STATUS_ERR_NO_SUCH_NICK) {
 -      tmp += 2;
 -      silc_say(cmd->client, "%s: %s", tmp,
 -             silc_client_command_status_message(status));
 +      /* 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;
      }
    }
      /* 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 -268,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;
    SILC_GET16_MSB(status, tmp);
    if (status != SILC_STATUS_OK) {
      if (status == SILC_STATUS_ERR_NO_SUCH_NICK) {
 -      tmp += 2;
 -      silc_say(cmd->client, "%s: %s", tmp,
 -             silc_client_command_status_message(status));
 +      /* 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;
      }
    }
      client_entry->nickname = strdup(nickname);
  
      /* Save received Client ID to ID cache */
-     silc_idcache_add(win->client_cache, client_entry->nickname,
 -    CIDCC(nickname[0]) =
 -      silc_idcache_add(&CIDC(nickname[0]), CIDCC(nickname[0]),
 -                     client_entry->nickname, SILC_ID_CLIENT, 
 -                     client_entry->id, client_entry);
++    silc_idcache_add(conn->client_cache, client_entry->nickname,
 +                   SILC_ID_CLIENT, client_entry->id, client_entry, TRUE);
    }
  
    if (status == SILC_STATUS_LIST_START) {
  
  /* 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;
    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;
    }
  
    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);
@@@ -408,23 -375,8 +395,30 @@@ SILC_CLIENT_CMD_REPLY_FUNC(topic
  {
  }
  
 +/* Received reply to invite command. */
++/* Sends to application: (no arguments) */
 +
  SILC_CLIENT_CMD_REPLY_FUNC(invite)
  {
-     silc_say(cmd->client, "%s", silc_client_command_status_message(status));
 +  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) {
++    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);
  }
   
  SILC_CLIENT_CMD_REPLY_FUNC(quit)
@@@ -435,89 -387,20 +429,100 @@@ 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;
  
 -SILC_CLIENT_CMD_REPLY_FUNC(away)
 -{
 +  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;
 +  }
 +
 +  /* Get server ID */
 +  tmp = silc_command_get_arg_type(cmd->payload, 2, NULL);
 +  if (!tmp)
 +    goto out;
 +
 +  /* XXX save server id */
 +
 +  /* Get server info */
 +  tmp = silc_command_get_arg_type(cmd->payload, 3, NULL);
 +  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)
  {
-   SilcClientWindow win = (SilcClientWindow)cmd->sock->user_data;
 +  SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
-     silc_say(cmd->client, "%s", silc_client_command_status_message(status));
++  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
 +  SilcCommandStatus status;
 +  void *id;
 +  char *tmp;
 +  int i;
 +  time_t diff, curtime;
 +
 +  tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
 +  SILC_GET16_MSB(status, tmp);
 +  if (status != SILC_STATUS_OK) {
-   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;
++    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 < 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;
 +    }
 +  }
 +
 + out:
 +  silc_client_command_reply_free(cmd);
  }
  
  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;
 +  unsigned int argc, mode;
    unsigned char *id_string;
    char *topic, *tmp, *channel_name;
  
    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);
-     silc_say(cmd->client, "Cannot join channel: Bad reply packet");
 +  if (!tmp) {
++    cmd->client->ops->say(cmd->client, conn, 
++                        "Cannot join channel: Bad reply packet");
++    COMMAND_REPLY_ERROR;
 +    goto out;
 +  }
    channel_name = strdup(tmp);
  
 -  /* Get channel ID */
 +  /* Get Channel ID */
    id_string = silc_command_get_arg_type(cmd->payload, 3, NULL);
-     silc_say(cmd->client, "Cannot join channel: Bad reply packet");
 +  if (!id_string) {
++    cmd->client->ops->say(cmd->client, conn, 
++                        "Cannot join channel: Bad reply packet");
++    COMMAND_REPLY_ERROR;
 +    goto out;
 +  }
 +
 +  /* Get channel mode */
 +  tmp = silc_command_get_arg_type(cmd->payload, 4, NULL);
 +  if (tmp)
 +    SILC_GET32_MSB(mode, tmp);
 +  else
 +    mode = 0;
  
    /* Get topic */
 -  topic = silc_command_get_arg_type(cmd->payload, 4, NULL);
 +  topic = silc_command_get_arg_type(cmd->payload, 5, NULL);
  
    /* Save received Channel ID */
 -  silc_client_new_channel_id(cmd->client, cmd->sock, channel_name, id_string);
 -
 -  /* Print channel name on screen */
 -  client->screen->bottom_line->channel = channel_name;
 -  silc_screen_print_bottom_line(client->screen, 0);
 +  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);
@@@ -622,203 -489,63 +636,152 @@@ 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;
  
 -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));
++  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);
  }
  
 -/* Private message received. This processes the private message and
 -   finally displays it on the screen. */
 +/* Reply to NAMES command. Received list of client names on the channel 
 +   we requested. */
  
 -SILC_CLIENT_CMD_REPLY_FUNC(msg)
 +SILC_CLIENT_CMD_REPLY_FUNC(names)
  {
    SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
-   SilcClientWindow win = (SilcClientWindow)cmd->sock->user_data;
 -  SilcClient client = cmd->client;
 -  SilcBuffer buffer = (SilcBuffer)cmd->context;
 -  unsigned short nick_len;
 -  unsigned char *nickname, *message;
 -  SilcIDCache *id_cache;
 -  unsigned char *id_string;
 -  void *id;
++  SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
 +  SilcCommandStatus status;
 +  SilcIDCacheEntry id_cache = NULL;
 +  SilcChannelEntry channel;
 +  SilcChannelID *channel_id = NULL;
 +  SilcBuffer client_id_list;
 +  unsigned char *tmp;
 +  char *name_list;
 +  int i, len1, len2, list_count = 0;
  
 -  /* Get nickname */
 -  silc_buffer_unformat(buffer, 
 -                     SILC_STR_UI16_NSTRING_ALLOC(&nickname, &nick_len),
 -                     SILC_STR_END);
 -  silc_buffer_pull(buffer, 2 + nick_len);
 +  SILC_LOG_DEBUG(("Start"));
  
 -#if 0
 -  /* Get ID of the sender */
 -  id_string = silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char *));
 -  silc_buffer_push(buffer, SILC_ID_CLIENT_LEN + SILC_ID_CLIENT_LEN);
 -  memcpy(id_string, buffer->data, SILC_ID_CLIENT_LEN);
 -  silc_buffer_pull(buffer, SILC_ID_CLIENT_LEN + SILC_ID_CLIENT_LEN);
 -  id = silc_id_str2id(id_string, SILC_ID_CLIENT);
 -  silc_free(id_string);
 +  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;
 +  }
  
 -  /* Nickname should be verified if we don't have it in the cache */
 -  if (silc_idcache_find_by_data(client->current_win->
 -                              client_id_cache[nickname[0] - 32],
 -                              client->current_win->
 -                              client_id_cache_count[nickname[0] - 32],
 -                              nickname, &id_cache) == FALSE) {
 +  /* 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);
 +
 +  /* 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;
 +  }
  
 -    SilcClientCommandContext ctx;
 -    char whois[255];
 +  /* 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;
 +  }
  
 -    /* Private message from unknown source, try to resolve it. */
 +  client_id_list = silc_buffer_alloc(len2);
 +  silc_buffer_pull_tail(client_id_list, len2);
 +  silc_buffer_put(client_id_list, tmp, len2);
  
-   if (!silc_idcache_find_by_id_one(win->channel_cache, (void *)channel_id,
-                                  SILC_ID_CHANNEL, &id_cache))
 +  /* Get the channel name */
++  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;
  
 -    return;
 +  /* If there is pending command we know that user has called this command
 +     and we will handle the name list differently. */
 +  if (cmd->callback) {
 +    /* We will resolve all the necessary information about the people
 +       on the channel. Only after that will we display the user list. */
 +    for (i = 0; i < len1; i++) {
 +      /* XXX */
 +
 +    }
 +    silc_client_command_pending_del(SILC_COMMAND_NAMES);
 +  } else {
 +    /* there is no pending callback it means that this command reply
 +       has been received without calling the command, ie. server has sent
 +       the reply without getting the command from us first. This happens
 +       with SILC servers that sends NAMES reply after joining to a channel. */
 +
 +    /* Remove commas from list */
 +    for (i = 0; i < len1; i++)
 +      if (name_list[i] == ',') {
 +      name_list[i] = ' ';
 +      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);
    }
 -#endif
 -     
 -  message = silc_calloc(buffer->len + 1, sizeof(char));
 -  memcpy(message, buffer->data, buffer->len);
 -  silc_print(client, "*%s* %s", nickname, message);
 -  memset(message, 0, buffer->len);
 -  silc_free(message);
 +
 +  /* Cache the received name list and client ID's. This cache expires
 +     whenever server sends notify message to channel. It means two things;
 +     some user has joined or leaved the channel. */
 +  for (i = 0; i < list_count; i++) {
 +    int nick_len = strcspn(name_list, " ");
 +    char *nickname = silc_calloc(nick_len, sizeof(*nickname));
 +    SilcClientID *client_id;
 +    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;
 +  }
 +
 +  silc_buffer_free(client_id_list);
 +
 + out:
 +  if (channel_id)
 +    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);
- }
index 0af5dddf0643aa0f6e1ce8853652d976841a0948,36b312860733035d2df58f02f963c30bc38c93ad..1a7e057867405232e565b21640e793ee77230a52
@@@ -86,7 -85,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,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
index ed42fca4a5002bf158f5402af11a8dbe8540171c,0000000000000000000000000000000000000000..7f632e965ed30ab389f56df3622bebdd5ce66cc3
mode 100644,000000..100644
--- /dev/null
@@@ -1,123 -1,0 +1,118 @@@
- /*
-  * $Id$
-  * $Log$
-  * Revision 1.1  2000/07/12 05:55:05  priikone
-  *    Created idlist.c
-  *
-  */
 +/*
 +
 +  idlist.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"
++/* $Id$ */
 +
-                                      SilcClientWindow win,
++#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'
 +   and `num' are defined as well thisk checks the match from multiple
 +   cache entries thus providing support for multiple same nickname
 +   handling. This also ignores case-sensitivity. */
 +
 +SilcClientEntry silc_idlist_get_client(SilcClient client,
-   if (!silc_idcache_find_by_data_loose(win->client_cache, nickname, &list)) {
++                                     SilcClientConnection conn,
 +                                     char *nickname,
 +                                     char *server,
 +                                     unsigned int num)
 +{
 +  SilcIDCacheEntry id_cache;
 +  SilcIDCacheList list = NULL;
 +  SilcClientEntry entry = NULL;
 +
 +  /* Find ID from cache */
-     ctx->sock = win->sock;
++  if (!silc_idcache_find_by_data_loose(conn->client_cache, nickname, &list)) {
 +    SilcClientCommandContext ctx;
 +    char ident[512];
 +    
 +  identify:
 +
 +    SILC_LOG_DEBUG(("Requesting Client ID from server"));
 +
 +    /* No ID found. Do query from the server. The query is done by 
 +       sending simple IDENTIFY command to the server. */
 +    ctx = silc_calloc(1, sizeof(*ctx));
 +    ctx->client = client;
-     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);
++    ctx->conn = conn;
++    ctx->command = silc_client_command_find("IDENTIFY");
 +    memset(ident, 0, sizeof(ident));
-                                        SilcClientWindow win,
++    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);
 +
 +    return NULL;
 +  }
 +
 +  if (!server && !num) {
 +    /* Take first found cache entry */
 +    if (!silc_idcache_list_first(list, &id_cache))
 +      goto identify;
 +
 +    entry = (SilcClientEntry)id_cache->context;
 +  } else {
 +    /* Check multiple cache entries for match */
 +    while (silc_idcache_list_next(list, &id_cache)) {
 +      entry = (SilcClientEntry)id_cache->context;
 +
 +      if (server && entry->server && 
 +        strncasecmp(server, entry->server, strlen(server))) {
 +      entry = NULL;
 +      continue;
 +      }
 +      
 +      if (num && entry->num != num) {
 +      entry = NULL;
 +      continue;
 +      }
 +
 +      break;
 +    }
 +
 +    /* If match weren't found, request it */
 +    if (!entry)
 +      goto identify;
 +  }
 +
 +  if (list)
 +    silc_idcache_list_free(list);
 +
 +  return entry;
 +}
 +
 +/* Finds channel entry from ID cache by channel name. */
 +
 +SilcChannelEntry silc_idlist_get_channel(SilcClient client,
-   if (!silc_idcache_find_by_data_one(win->channel_cache, channel, &id_cache))
++                                       SilcClientConnection conn,
 +                                       char *channel)
 +{
 +  SilcIDCacheEntry id_cache;
 +  SilcChannelEntry entry;
 +
++  if (!silc_idcache_find_by_data_one(conn->channel_cache, channel, &id_cache))
 +    return NULL;
 +
 +  entry = (SilcChannelEntry)id_cache->context;
 +
 +  return entry;
 +}
index 2191211bb397173436049e89f470d8a9cbba435a,9c769e9839578215407f626e2222903e590dcf30..8a7b82815fc8528284bd0beda6e9bd8e582f92bc
@@@ -58,15 -55,4 +58,15 @@@ typedef struct SilcChannelEntryStruct 
  
  typedef SilcChannelEntryObject *SilcChannelEntry;
  
-                                      SilcClientWindow win,
 +/* 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,
++                                       SilcClientConnection conn,
 +                                       char *channel);
 +
  #endif
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..49d3b8c48a7e20fe914dd2cc01d046feff51c721
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,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
index 342597069f2f48a56d954167eb83a4bb95993414,ba89d9dcf80a4fb47ddd4d9b24a98aeee608d0f3..d0b8064c1093adc736e96f4cd8dbd5f3bd179f79
  /*
   * 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.
 - *    Importet 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_channel_auth);
  SILC_TASK_CALLBACK(silc_client_protocol_key_exchange);
  
 -/* SILC client protocol list */
 -const SilcProtocolObject silc_protocol_list[] =
 -{
 -  { SILC_PROTOCOL_CLIENT_CONNECTION_AUTH, 
 -    silc_client_protocol_connection_auth },
 -  { SILC_PROTOCOL_CLIENT_CHANNEL_AUTH, 
 -    silc_client_protocol_channel_auth },
 -  { SILC_PROTOCOL_CLIENT_KEY_EXCHANGE, 
 -    silc_client_protocol_key_exchange },
 -
 -  { SILC_PROTOCOL_CLIENT_NONE, NULL },
 -};
 +extern char *silc_version_string;
  
  /*
   * Key Exhange protocol functions
@@@ -99,11 -90,6 +68,12 @@@ silc_client_protocol_ke_verify_key(Silc
  
    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;
  }
  
  /* Sets the negotiated key material into use for particular connection. */
@@@ -115,38 -101,42 +85,38 @@@ static void silc_client_protocol_ke_set
                                             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);
 -  win->hmac_key_len = keymat->hmac_key_len;
 -  win->hmac_key = silc_calloc(win->hmac_key_len,
 -                                  sizeof(unsigned char));
 -  memcpy(win->hmac_key, 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 -148,6 +128,7 @@@ SILC_TASK_CALLBACK(silc_client_protocol
    SilcClientKEInternalContext *ctx = 
      (SilcClientKEInternalContext *)protocol->context;
    SilcClient client = (SilcClient)ctx->client;
++  SilcClientConnection conn = ctx->sock->user_data;
    SilcSKEStatus status;
  
    SILC_LOG_DEBUG(("Start"));
        } else {
        /* Finish the protocol. This verifies the Key Exchange 2 payload
           sent by responder. */
 -      status = 
 -        silc_ske_initiator_finish(ctx->ske,
 -                                  ctx->packet,
 -                                  silc_client_protocol_ke_finish_cb,
 -                                  context);
 +      status = silc_ske_initiator_finish(ctx->ske, ctx->packet,
 +                                         silc_client_protocol_ke_verify_key,
 +                                         context, NULL, NULL);
        }
  
 -      switch(status) {
 -      default:
 -      break;
 +      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);
 +      return;
        }
        
        /* Send Ok to the other end. We will end the protocol as server
@@@ -371,6 -352,6 +344,7 @@@ SILC_TASK_CALLBACK(silc_client_protocol
    SilcClientConnAuthInternalContext *ctx = 
      (SilcClientConnAuthInternalContext *)protocol->context;
    SilcClient client = (SilcClient)ctx->client;
++  SilcClientConnection conn = ctx->sock->user_data;
  
    SILC_LOG_DEBUG(("Start"));
  
          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;
  
index 684995dd2b431df6c03c4f454ede1256bf49510d,1c40ef382ab8e6c0f94ab6ff81cab71a9d9414cc..87c2573ad06230c76a31d335085248a3309a3ce2
  #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_CHANNEL_AUTH 2
 -#define SILC_PROTOCOL_CLIENT_KEY_EXCHANGE 3
--/* #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,11 -48,7 +53,11 @@@ 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;
 +  SilcIdType dest_id_type;
  
    /* Authentication data if we alreay know it. This is filled before
       starting the protocol if we know the authentication data. Otherwise