Integer type name change.
[silc.git] / apps / irssi / src / silc / core / clientutil.c
index 1bcc9c8bc3fde0d889130218400c74f69c6c3ed3..93b575fe9a5fdc14a38da05166f101a169c7ce1d 100644 (file)
@@ -2,14 +2,13 @@
 
   client.c
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2000 Pekka Riikonen
+  Copyright (C) 1997 - 2002 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.
+  the Free Software Foundation; version 2 of the License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 */
 /* $Id$ */
 
-#include "clientincludes.h"
+#include "module.h"
 
-/* Routine used to print lines to window. This can split the
-   line neatly if a word would overlap the line. */
+#include "net-nonblock.h"
+#include "net-sendbuffer.h"
+#include "signals.h"
+#include "servers.h"
+#include "commands.h"
+#include "levels.h"
+#include "modules.h"
+#include "rawlog.h"
+#include "misc.h"
+#include "settings.h"
 
-void silc_print_to_window(WINDOW *win, char *message)
-{
-  int str_len, len;
-
-  str_len = strlen(message);
-  if (str_len > COLS - 1) {
-    /* Split overlapping words to next line */
-    /* XXX In principal this is wrong as this modifies the original
-       string as it replaces the last ' ' with '\n'. This could be done
-       with little more work so that it would not replace anything. */
-    len = COLS - 1;
-    while (1) {
-
-      while (len && message[len] != ' ')
-       len--;
-
-      if (!len)
-       break;
-
-      message[len] = '\n';
-      len += COLS - 1;
-      if (len > str_len)
-       break;
-    }
-  }
-
-  wprintw(win, "%s", message);
-  wrefresh(win);
-}
-
-/* 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[2048];
-  SilcClientInternal app = client->application;
-  
-  memset(message, 0, sizeof(message));
-  strncat(message, "\n ", 2);
-
-  va_start(vp, msg);
-  vsprintf(message + 1, msg, vp);
-  va_end(vp);
-  
-  /* Print the message */
-  silc_print_to_window(app->screen->output_win[0], message);
-}
-
-/* Returns user's mail path */
-
-char *silc_get_mail_path()
-{
-  char pathbuf[MAXPATHLEN];
-  char *path;
-
-#ifndef _PATH_MAILDIR
-#define _PATH_MAILDIR "/var/mail"
-#endif
-  
-  path = getenv("MAIL");
-  if (path) {
-    strncpy(pathbuf, path, strlen(path));
-  } else {
-    strcpy(pathbuf, _PATH_MAILDIR);
-    strcat(pathbuf, "/");
-    strcat(pathbuf, silc_get_username());
-  }
-
-  return strdup(pathbuf);
-}
-
-/* gets the number of the user's mails, if possible */
-
-int silc_get_number_of_emails()
-{
-  FILE *tl;
-  int num = 0;
-  char *filename;
-  char data[1024];
-  
-  filename = silc_get_mail_path();
-  
-  tl = fopen(filename, "r");
-  if (!tl) {
-    fprintf(stderr, "Couldn't open mail file (%s).\n", filename);
-  } else {
-    while((fscanf(tl, "%s", data)) != EOF) { 
-      if(!strcmp(data, "From:"))
-       num++;
-    }
-    
-    fclose(tl);
-  }
-  
-  return num;
-}
-
-/* Returns time til next minute changes. Used to update the clock when
-   needed. */
-
-int silc_client_time_til_next_min()
-{
-  time_t curtime;
-  struct tm *min;
-  
-  curtime = time(0);
-  min = localtime(&curtime);
-  
-  return 60 - min->tm_sec;
-}
-
-/* Asks yes/no from user on the input line. Returns TRUE on "yes" and
-   FALSE on "no". */
-
-int silc_client_ask_yes_no(SilcClient client, char *prompt)
-{
-  SilcClientInternal app = (SilcClientInternal)client->application;
-  char answer[4];
-  int ret;
-
- again:
-  silc_screen_input_reset(app->screen);
-
-  /* Print 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(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, app->conn, "Type yes or no");
-    goto again;
-  }
-  noecho();
+#include "channels-setup.h"
 
-  silc_screen_input_reset(app->screen);
+#include "silc-servers.h"
+#include "silc-channels.h"
+#include "silc-queries.h"
+#include "silc-nicklist.h"
+#include "window-item-def.h"
 
-  return ret;
-}
+#include "fe-common/core/printtext.h"
+#include "fe-common/core/keyboard.h"
 
-/* Lists supported (builtin) ciphers */
+/* Lists supported ciphers */
 
 void silc_client_list_ciphers()
 {
@@ -184,7 +51,7 @@ void silc_client_list_ciphers()
   silc_free(ciphers);
 }
 
-/* Lists supported (builtin) hash functions */
+/* Lists supported hash functions */
 
 void silc_client_list_hash_funcs()
 {
@@ -193,6 +60,15 @@ void silc_client_list_hash_funcs()
   silc_free(hash);
 }
 
+/* Lists supported hash functions */
+
+void silc_client_list_hmacs()
+{
+  char *hash = silc_hmac_get_supported();
+  fprintf(stdout, "%s\n", hash);
+  silc_free(hash);
+}
+
 /* Lists supported PKCS algorithms */
 
 void silc_client_list_pkcs()
@@ -234,78 +110,21 @@ char *silc_client_get_input(const char *prompt)
   return strdup(input);
 }
 
-/* Displays prompt on command line and takes passphrase with echo 
-   off from user. */
-
-char *silc_client_get_passphrase(const char *prompt)
-{
-#if 0
-  char input[2048];
-  char *ret;
-  int fd;
-  struct termios to;
-  struct termios to_old;
-
-  fd = open("/dev/tty", O_RDONLY);
-  if (fd < 0) {
-    fprintf(stderr, "silc: %s\n", strerror(errno));
-    return NULL;
-  }
-
-  signal(SIGINT, SIG_IGN);
-
-  /* Get terminal info */
-  tcgetattr(fd, &to);
-  to_old = to;
-
-  /* Echo OFF */
-  to.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
-  tcsetattr(fd, TCSANOW, &to);
-
-  memset(input, 0, sizeof(input));
-
-  printf("%s", prompt);
-  fflush(stdout);
-
-  if ((read(fd, input, sizeof(input))) < 0) {
-    fprintf(stderr, "silc: %s\n", strerror(errno));
-    return NULL;
-  }
-
-  if (strlen(input) <= 1) {
-    tcsetattr(fd, TCSANOW, &to_old);
-    return NULL;
-  }
-
-  if (strchr(input, '\n'))
-    *strchr(input, '\n') = '\0';
-
-  /* Restore old terminfo */
-  tcsetattr(fd, TCSANOW, &to_old);
-  signal(SIGINT, SIG_DFL);
-
-  ret = silc_calloc(strlen(input), sizeof(char));
-  memcpy(ret, input, strlen(input));
-  memset(input, 0, sizeof(input));
-  return ret;
-#else
-  return NULL;
-#endif
-}
-
 /* Returns identifier string for public key generation. */
 
 char *silc_client_create_identifier()
 {
   char *username = NULL, *realname = NULL;
-  char hostname[256], email[256];
+  char *hostname, email[256];
+  char *ident;
   
   /* Get realname */
   realname = silc_get_real_name();
 
   /* Get hostname */
-  memset(hostname, 0, sizeof(hostname));
-  gethostname(hostname, sizeof(hostname));
+  hostname = silc_net_localhost();
+  if (!hostname)
+    return NULL;
 
   /* Get username (mandatory) */
   username = silc_get_username();
@@ -315,8 +134,14 @@ char *silc_client_create_identifier()
   /* Create default email address, whether it is right or not */
   snprintf(email, sizeof(email), "%s@%s", username, hostname);
 
-  return silc_pkcs_encode_identifier(username, hostname, realname, email,
-                                    NULL, NULL);
+  ident = silc_pkcs_encode_identifier(username, hostname, realname, email,
+                                     NULL, NULL);
+  if (realname)
+    silc_free(realname);
+  silc_free(hostname);
+  silc_free(username);
+
+  return ident;
 }
 
 /* Creates new public key and private key pair. This is used only
@@ -333,7 +158,7 @@ int silc_client_create_key_pair(char *pkcs_name, int bits,
   SilcPrivateKey prv_key;
   SilcRng rng;
   unsigned char *key;
-  uint32 key_len;
+  SilcUInt32 key_len;
   char line[256];
   char *pkfile = NULL, *prvfile = NULL;
 
@@ -478,14 +303,16 @@ int silc_client_check_silc_dir()
   memset(file_public_key, 0, sizeof(file_public_key));
   memset(file_private_key, 0, sizeof(file_private_key));
 
+  identifier = silc_client_create_identifier();
+
   pw = getpwuid(getuid());
   if (!pw) {
     fprintf(stderr, "silc: %s\n", strerror(errno));
+    if (identifier)
+      silc_free(identifier);
     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", 
@@ -525,6 +352,7 @@ int silc_client_check_silc_dir()
       return FALSE;
     }
     
+#if 0
     /* Check the permissions of the dir */
     if ((st.st_mode & 0777) != 0755) {
       if ((chmod(filename, 0755)) == -1) {
@@ -533,6 +361,7 @@ int silc_client_check_silc_dir()
        return FALSE;
       }
     }
+#endif
   }
 
   /*
@@ -709,6 +538,10 @@ int silc_client_load_keys(SilcClient client)
                                  SILC_PKCS_FILE_BIN) == FALSE)
       return FALSE;
 
+  silc_pkcs_alloc(client->public_key->name, &client->pkcs);
+  silc_pkcs_public_key_set(client->pkcs, client->public_key);
+  silc_pkcs_private_key_set(client->pkcs, client->private_key);
+
   return TRUE;
 }
 
@@ -718,9 +551,9 @@ int silc_client_show_key(char *keyfile)
 {
   SilcPublicKey public_key;
   SilcPublicKeyIdentifier ident;
-  char *fingerprint;
+  char *fingerprint, *babbleprint;
   unsigned char *pk;
-  uint32 pk_len;
+  SilcUInt32 pk_len;
   SilcPKCS pkcs;
   int key_len = 0;
 
@@ -736,6 +569,7 @@ int silc_client_show_key(char *keyfile)
 
   pk = silc_pkcs_public_key_encode(public_key, &pk_len);
   fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
+  babbleprint = silc_hash_babbleprint(NULL, pk, pk_len);
 
   if (silc_pkcs_alloc(public_key->name, &pkcs)) {
     key_len = silc_pkcs_public_key_set(pkcs, public_key);
@@ -759,10 +593,12 @@ int silc_client_show_key(char *keyfile)
   if (ident->country)
     printf("Country            : %s\n", ident->country);
   printf("Fingerprint (SHA1) : %s\n", fingerprint); 
+  printf("Babbleprint (SHA1) : %s\n", babbleprint); 
 
   fflush(stdout);
 
   silc_free(fingerprint);
+  silc_free(babbleprint);
   silc_free(pk);
   silc_pkcs_public_key_free(public_key);
   silc_pkcs_free_identifier(ident);