silc-client: handle prompt abort better
[silc.git] / apps / irssi / src / silc / core / client_ops.c
index cfefe3275300b7e98db354c1ec378c3128b751b9..b3b6cdbc59b2112d4eab5898dae76dd594cd28d0 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2001 - 2008 Pekka Riikonen
+  Copyright (C) 2001 - 2014 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
@@ -212,14 +212,26 @@ void silc_say(SilcClient client, SilcClientConnection conn,
              SilcClientMessageType type, char *msg, ...)
 {
   SILC_SERVER_REC *server;
+  char *target = NULL;
   va_list va;
   char *str;
 
   server = conn == NULL ? NULL : conn->context;
 
+  switch (conn->context_type) {
+    case SILC_ID_CLIENT:
+      target = (conn->client_entry->nickname[0] ?
+               conn->client_entry->nickname : NULL);
+    break;
+
+    case SILC_ID_CHANNEL:
+      target = conn->channel_entry->channel_name;
+    break;
+  }
+
   va_start(va, msg);
   str = g_strdup_vprintf(msg, va);
-  printtext(server, NULL, MSGLEVEL_CRAP, "%s", str);
+  printtext(server, target, MSGLEVEL_CRAP, "%s", str);
   g_free(str);
   va_end(va);
 }
@@ -262,10 +274,11 @@ int verify_message_signature(SilcClientEntry sender,
                                      sizeof(sender->fingerprint));
       if (strcmp(fingerprint, fingerprint2)) {
         /* since the public key differs from the senders public key, the
-           verification _failed_ */
+           verification won't be done */
         silc_pkcs_public_key_free(pk);
         silc_free(fingerprint);
-        ret = SILC_MSG_SIGNED_UNKNOWN;
+        silc_free(fingerprint2);
+       return SILC_MSG_SIGNED_UNKNOWN;
       }
       silc_free(fingerprint2);
     }
@@ -945,7 +958,7 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
        tmp = cp;
       }
 
-      chanrec->topic = *tmp == '\0' ? NULL : g_strdup(tmp);
+      chanrec->topic = (tmp && *tmp == '\0' ? NULL : g_strdup(tmp));
       signal_emit("channel topic changed", 1, chanrec);
 
       silc_free(dm);
@@ -1406,7 +1419,7 @@ typedef struct {
   SilcIdType id_type;
 } *GetkeyContext;
 
-void silc_getkey_cb(bool success, void *context)
+void silc_getkey_cb(SilcBool success, void *context)
 {
   GetkeyContext getkey = (GetkeyContext)context;
   char *entity = (getkey->id_type == SILC_ID_CLIENT ? "user" : "server");
@@ -2401,13 +2414,13 @@ static void verify_public_key_completion(const char *line, void *context,
   SilcBool success = (reason == KeyboardCompletionSuccess);
 
   if (success && (line[0] == 'Y' || line[0] == 'y')) {
-    /* Call the completion */
-    if (verify->completion)
-      verify->completion(TRUE, verify->context);
-
     /* Save the key for future checking */
     silc_pkcs_save_public_key(verify->filename, verify->public_key,
                              SILC_PKCS_FILE_BASE64);
+
+    /* Call the completion */
+    if (verify->completion)
+      verify->completion(TRUE, verify->context);
   } else {
     /* Call the completion */
     if (verify->completion)
@@ -2463,7 +2476,7 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
   SilcPublicKey local_pubkey;
   SilcSILCPublicKey silc_pubkey;
   SilcUInt16 port;
-  SILC_SERVER_REC *server;
+  SILC_SERVER_REC *server = NULL;
   const char *hostname, *ip;
   unsigned char *pk;
   SilcUInt32 pk_len;
@@ -2475,11 +2488,19 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
   int i;
 
   server = (SILC_SERVER_REC*)conn->context;
-  SILC_VERIFY(server);
-  if (!server) {
-    if (completion)
-      completion(FALSE, context);
-    return;
+  if (conn_type != SILC_CONN_CLIENT) {
+    SILC_VERIFY(server);
+    if (!server) {
+      if (completion)
+       completion(FALSE, context);
+      return;
+    }
+  }
+
+  /* If we have pending public key prompt already up */
+  if (server && server->prompt_op) {
+    silc_async_abort(server->prompt_op, NULL, NULL);
+    server->prompt_op = NULL;
   }
 
   if (silc_pkcs_get_type(public_key) != SILC_PKCS_SILC) {
@@ -2584,6 +2605,14 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
                         silc_pubkey->identifier.realname,
                         silc_pubkey->identifier.email ?
                         silc_pubkey->identifier.email : "");
+    else if (conn_type == SILC_CONN_CLIENT &&
+            (silc_pubkey->identifier.realname ||
+             silc_pubkey->identifier.email))
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
+                        SILCTXT_PUBKEY_RECEIVED_CLIENT, "",
+                        silc_pubkey->identifier.realname,
+                        silc_pubkey->identifier.email ?
+                        silc_pubkey->identifier.email : "");
     printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                       SILCTXT_PUBKEY_FINGERPRINT, entity, fingerprint);
     printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
@@ -2591,7 +2620,8 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
     format = format_get_text("fe-common/silc", NULL, NULL, NULL,
                             SILCTXT_PUBKEY_ACCEPT);
     silc_keyboard_entry_redirect(verify_public_key_completion,
-                           format, 0, verify, &server->prompt_op);
+                                format, 0, verify,
+                                server ? &server->prompt_op : NULL);
     g_free(format);
     silc_free(fingerprint);
     silc_free(babbleprint);
@@ -2615,6 +2645,14 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
                           silc_pubkey->identifier.realname,
                           silc_pubkey->identifier.email ?
                           silc_pubkey->identifier.email : "");
+      else if (conn_type == SILC_CONN_CLIENT &&
+              (silc_pubkey->identifier.realname ||
+               silc_pubkey->identifier.email))
+       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
+                          SILCTXT_PUBKEY_RECEIVED_CLIENT, "",
+                          silc_pubkey->identifier.realname,
+                          silc_pubkey->identifier.email ?
+                          silc_pubkey->identifier.email : "");
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_FINGERPRINT, entity, fingerprint);
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
@@ -2624,7 +2662,9 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
       format = format_get_text("fe-common/silc", NULL, NULL, NULL,
                               SILCTXT_PUBKEY_ACCEPT_ANYWAY);
       silc_keyboard_entry_redirect(verify_public_key_completion,
-                             format, 0, verify, &server->prompt_op);
+                                  format, 0, verify,
+                                  server ? &server->prompt_op : NULL);
+
       g_free(format);
       silc_free(fingerprint);
       silc_free(babbleprint);
@@ -2645,6 +2685,14 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
                           silc_pubkey->identifier.realname,
                           silc_pubkey->identifier.email ?
                           silc_pubkey->identifier.email : "");
+      else if (conn_type == SILC_CONN_CLIENT &&
+              (silc_pubkey->identifier.realname ||
+               silc_pubkey->identifier.email))
+       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
+                          SILCTXT_PUBKEY_RECEIVED_CLIENT, "",
+                          silc_pubkey->identifier.realname,
+                          silc_pubkey->identifier.email ?
+                          silc_pubkey->identifier.email : "");
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_FINGERPRINT, entity, fingerprint);
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
@@ -2654,7 +2702,8 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
       format = format_get_text("fe-common/silc", NULL, NULL, NULL,
                               SILCTXT_PUBKEY_ACCEPT_ANYWAY);
       silc_keyboard_entry_redirect(verify_public_key_completion,
-                             format, 0, verify, &server->prompt_op);
+                                  format, 0, verify,
+                                  server ? &server->prompt_op : NULL);
       g_free(format);
       silc_free(fingerprint);
       silc_free(babbleprint);
@@ -2675,6 +2724,14 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
                           silc_pubkey->identifier.realname,
                           silc_pubkey->identifier.email ?
                           silc_pubkey->identifier.email : "");
+      else if (conn_type == SILC_CONN_CLIENT &&
+              (silc_pubkey->identifier.realname ||
+               silc_pubkey->identifier.email))
+       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
+                          SILCTXT_PUBKEY_RECEIVED_CLIENT, "",
+                          silc_pubkey->identifier.realname,
+                          silc_pubkey->identifier.email ?
+                          silc_pubkey->identifier.email : "");
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_FINGERPRINT, entity, fingerprint);
       printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
@@ -2690,7 +2747,8 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
       format = format_get_text("fe-common/silc", NULL, NULL, NULL,
                               SILCTXT_PUBKEY_ACCEPT_ANYWAY);
       silc_keyboard_entry_redirect(verify_public_key_completion,
-                             format, 0, verify, &server->prompt_op);
+                                  format, 0, verify,
+                                  server ? &server->prompt_op : NULL);
       g_free(format);
       silc_free(fingerprint);
       silc_free(babbleprint);