Merged silc_1_1_branch to trunk.
[silc.git] / apps / irssi / src / silc / core / clientutil.c
index c3bcd3287d8367a64ecb7f067ada1bb27a3c1e6b..836347338a3457deaa8af281f5d37ef4d6775dc1 100644 (file)
 
 #include "fe-common/core/printtext.h"
 #include "fe-common/core/keyboard.h"
+#include "fe-common/silc/module-formats.h"
 
 #include "core.h"
 
+#ifdef SILC_PLUGIN
+void silc_client_print_list(char *list)
+{
+  char **items;
+  int i=0;
+
+  items = g_strsplit(list, ",", -1);
+
+  while (items[i] != NULL)
+    printformat_module("fe-common/silc", NULL, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_CONFIG_LIST,
+                      items[i++]);
+
+  g_strfreev(items);
+}
+#endif
+
 /* Lists supported ciphers */
 
 void silc_client_list_ciphers()
 {
   char *ciphers = silc_cipher_get_supported();
+#ifdef SILC_PLUGIN
+  printformat_module("fe-common/silc", NULL, NULL,
+                    MSGLEVEL_CRAP, SILCTXT_CONFIG_ALGOS,
+                    "cipher");
+  silc_client_print_list(ciphers);
+#else
   fprintf(stdout, "%s\n", ciphers);
+#endif
   silc_free(ciphers);
 }
 
@@ -58,7 +83,14 @@ void silc_client_list_ciphers()
 void silc_client_list_hash_funcs()
 {
   char *hash = silc_hash_get_supported();
+#ifdef SILC_PLUGIN
+  printformat_module("fe-common/silc", NULL, NULL,
+                    MSGLEVEL_CRAP, SILCTXT_CONFIG_ALGOS,
+                    "hash");
+  silc_client_print_list(hash);
+#else
   fprintf(stdout, "%s\n", hash);
+#endif
   silc_free(hash);
 }
 
@@ -67,7 +99,14 @@ void silc_client_list_hash_funcs()
 void silc_client_list_hmacs()
 {
   char *hash = silc_hmac_get_supported();
+#ifdef SILC_PLUGIN
+  printformat_module("fe-common/silc", NULL, NULL,
+                    MSGLEVEL_CRAP, SILCTXT_CONFIG_ALGOS,
+                    "hmac");
+  silc_client_print_list(hash);
+#else
   fprintf(stdout, "%s\n", hash);
+#endif
   silc_free(hash);
 }
 
@@ -76,7 +115,14 @@ void silc_client_list_hmacs()
 void silc_client_list_pkcs()
 {
   char *pkcs = silc_pkcs_get_supported();
+#ifdef SILC_PLUGIN
+  printformat_module("fe-common/silc", NULL, NULL,
+                    MSGLEVEL_CRAP, SILCTXT_CONFIG_ALGOS,
+                    "pkcs");
+  silc_client_print_list(pkcs);
+#else
   fprintf(stdout, "%s\n", pkcs);
+#endif
   silc_free(pkcs);
 }
 
@@ -323,3 +369,103 @@ int silc_client_load_keys(SilcClient client)
 
   return ret;
 }
+
+#ifdef SILC_PLUGIN
+void create_key_passphrase(const char *answer, CREATE_KEY_REC *rec)
+{
+  char priv_key_file[128], pub_key_file[128];
+
+  signal_stop();
+
+  if ((rec->passphrase == NULL) && (answer) && (*answer != '\0')) {
+    rec->passphrase = g_strdup(answer);
+    keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
+                           format_get_text("fe-common/silc", NULL, NULL,
+                                           NULL, SILCTXT_CONFIG_PASS_ASK2),
+                           ENTRY_REDIRECT_FLAG_HIDDEN, rec);
+    return;
+  }
+
+  if ((answer) && (*answer != '\0') && (rec->passphrase != NULL)) {
+    if (strcmp(answer, rec->passphrase)) {
+      printformat_module("fe-common/silc", NULL, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_CONFIG_PASSMISMATCH);
+      g_free(rec->pkcs);
+      g_free(rec->passphrase);
+      g_free(rec);
+      return;
+    }
+  }
+
+  memset(priv_key_file, 0, sizeof(priv_key_file));
+  memset(pub_key_file, 0, sizeof(pub_key_file));
+  snprintf(priv_key_file, sizeof(priv_key_file) - 1, "%s/%s",
+          get_irssi_dir(), SILC_CLIENT_PRIVATE_KEY_NAME);
+  snprintf(pub_key_file, sizeof(pub_key_file) - 1, "%s/%s",
+          get_irssi_dir(), SILC_CLIENT_PUBLIC_KEY_NAME);
+
+  if (silc_create_key_pair(rec->pkcs, rec->bits, pub_key_file, priv_key_file,
+                      NULL, (rec->passphrase == NULL ? "" : rec->passphrase),
+                      NULL, NULL, FALSE) == TRUE)
+    printformat_module("fe-common/silc", NULL, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_CONFIG_CREATE);
+  else
+    printformat_module("fe-common/silc", NULL, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_CONFIG_CREATE_FAIL);
+
+  g_free(rec->passphrase);
+  g_free(rec->pkcs);
+  g_free(rec);
+}
+
+void change_private_key_passphrase(const char *answer, CREATE_KEY_REC *rec)
+{
+  signal_stop();
+
+  if (rec->old == NULL) {
+    rec->old = g_strdup((answer == NULL ? "" : answer));
+    keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
+                           format_get_text("fe-common/silc", NULL, NULL,
+                                           NULL, SILCTXT_CONFIG_PASS_ASK2),
+                           ENTRY_REDIRECT_FLAG_HIDDEN, rec);
+    return;
+  }
+
+  if ((rec->passphrase == NULL) && (answer) && (*answer != '\0')) {
+    rec->passphrase = g_strdup(answer);
+    keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
+                           format_get_text("fe-common/silc", NULL, NULL,
+                                           NULL, SILCTXT_CONFIG_PASS_ASK3),
+                           ENTRY_REDIRECT_FLAG_HIDDEN, rec);
+    return;
+  }
+
+  if ((answer) && (*answer != '\0') && (rec->passphrase != NULL)) {
+    if (strcmp(answer, rec->passphrase)) {
+      printformat_module("fe-common/silc", NULL, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_CONFIG_PASSMISMATCH);
+      g_free(rec->old);
+      g_free(rec->file);
+      g_free(rec->pkcs);
+      g_free(rec->passphrase);
+      g_free(rec);
+      return;
+    }
+  }
+
+  if (silc_change_private_key_passphrase(rec->file, rec->old,
+                                    (rec->passphrase == NULL ?
+                                     "" : rec->passphrase)) == TRUE)
+    printformat_module("fe-common/silc", NULL, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_CONFIG_PASSCHANGE);
+  else
+    printformat_module("fe-common/silc", NULL, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_CONFIG_PASSCHANGE_FAIL);
+  g_free(rec->old);
+  g_free(rec->file);
+  g_free(rec->passphrase);
+  g_free(rec->pkcs);
+  g_free(rec);
+
+}
+#endif