Merge commit 'origin/silc.1.1.branch'
[silc.git] / apps / irssi / src / silc / core / silc-channels.c
index a0e1dcd6146732d04cd0bfadec187fabf4d369ee..4bd4e49ef556fb0d8a464fb930f8f014028eafa7 100644 (file)
@@ -1,7 +1,7 @@
 /*
   silc-channels.c : irssi
 
-  Copyright (C) 2000 - 2001, 2004, 2006 Timo Sirainen
+  Copyright (C) 2000 - 2001, 2004, 2006, 2007 Timo Sirainen
                 Pekka Riikonen <priikone@silcnet.org>
 
   This program is free software; you can redistribute it and/or modify
@@ -111,7 +111,10 @@ static void silc_channels_join(SILC_SERVER_REC *server,
                               const char *channels, int automatic)
 {
   char **list, **tmp;
+  char *channel, *key;
   SILC_CHANNEL_REC *chanrec;
+  CHANNEL_SETUP_REC *schannel;
+  GString *tmpstr;
 
   list = g_strsplit(channels, ",", -1);
   for (tmp = list; *tmp != NULL; tmp++) {
@@ -119,7 +122,25 @@ static void silc_channels_join(SILC_SERVER_REC *server,
     if (chanrec)
       continue;
 
-    silc_command_exec(server, "JOIN", *tmp);
+    channel = *tmp;
+    key = strchr(channel, ' ');
+    if (key != NULL) {
+      *key = '\0';
+      key++;
+    }
+    tmpstr = g_string_new(NULL);
+
+    schannel = channel_setup_find(channel, server->connrec->chatnet);
+    if (key && *key != '\0')
+      g_string_sprintfa(tmpstr, "%s %s", channel, key);
+    else if (schannel && schannel->password && schannel->password[0] != '\0')
+      g_string_sprintfa(tmpstr, "%s %s", channel, schannel->password);
+    else
+      g_string_sprintfa(tmpstr, "%s", channel);
+
+
+    silc_command_exec(server, "JOIN", tmpstr->str);
+    g_string_free(tmpstr, FALSE);
   }
 
   g_strfreev(list);
@@ -140,11 +161,6 @@ static void sig_server_quit(SILC_SERVER_REC *server, const char *msg)
     silc_command_exec(server, "QUIT", msg);
 }
 
-static void sig_gui_quit(SILC_SERVER_REC *server, const char *msg)
-{
-  silc_client_stop(silc_client);
-}
-
 /* Find Irssi channel entry by SILC channel entry */
 
 SILC_CHANNEL_REC *silc_channel_find_entry(SILC_SERVER_REC *server,
@@ -214,6 +230,7 @@ static void command_action(const char *data, SILC_SERVER_REC *server,
   char *message = NULL;
   int target_type;
   void *free_arg;
+  SilcBool sign = FALSE;
 
   CMD_SILC_SERVER(server);
   if (!IS_SILC_SERVER(server) || !server->connected)
@@ -256,21 +273,23 @@ static void command_action(const char *data, SILC_SERVER_REC *server,
 
   if (target != NULL) {
     if (target_type == SEND_TARGET_CHANNEL) {
+      sign = (g_hash_table_lookup(optlist, "sign") ? TRUE :
+             settings_get_bool("sign_channel_messages") ? TRUE : FALSE);
       if (silc_send_channel(server, target, (message != NULL ? message : msg),
                            SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 |
-                           (g_hash_table_lookup(optlist, "sign") != NULL ?
-                            SILC_MESSAGE_FLAG_SIGNED : 0))) {
+                           (sign ? SILC_MESSAGE_FLAG_SIGNED : 0))) {
        if (g_hash_table_lookup(optlist, "sign"))
           signal_emit("message silc signed_own_action", 3, server, msg, target);
        else
           signal_emit("message silc own_action", 3, server, msg, target);
       }
     } else {
+      sign = (g_hash_table_lookup(optlist, "sign") ? TRUE :
+             settings_get_bool("sign_private_messages") ? TRUE : FALSE);
       if (silc_send_msg(server, target, (message != NULL ? message : msg),
                        (message != NULL ? strlen(message) : strlen(msg)),
                        SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 |
-                       (g_hash_table_lookup(optlist, "sign") != NULL ?
-                        SILC_MESSAGE_FLAG_SIGNED : 0))) {
+                       (sign ? SILC_MESSAGE_FLAG_SIGNED : 0))) {
        if (g_hash_table_lookup(optlist, "sign"))
          signal_emit("message silc signed_own_private_action", 3,
                          server, msg, target);
@@ -318,6 +337,7 @@ static void command_notice(const char *data, SILC_SERVER_REC *server,
   char *message = NULL;
   int target_type;
   void *free_arg;
+  SilcBool sign;
 
   CMD_SILC_SERVER(server);
   if (!IS_SILC_SERVER(server) || !server->connected)
@@ -360,21 +380,23 @@ static void command_notice(const char *data, SILC_SERVER_REC *server,
 
   if (target != NULL) {
     if (target_type == SEND_TARGET_CHANNEL) {
+      sign = (g_hash_table_lookup(optlist, "sign") ? TRUE :
+             settings_get_bool("sign_channel_messages") ? TRUE : FALSE);
       if (silc_send_channel(server, target, (message != NULL ? message : msg),
                            SILC_MESSAGE_FLAG_NOTICE | SILC_MESSAGE_FLAG_UTF8 |
-                           (g_hash_table_lookup(optlist, "sign") != NULL ?
-                            SILC_MESSAGE_FLAG_SIGNED : 0))) {
+                           (sign ? SILC_MESSAGE_FLAG_SIGNED : 0))) {
        if (g_hash_table_lookup(optlist, "sign"))
           signal_emit("message silc signed_own_notice", 3, server, msg, target);
        else
           signal_emit("message silc own_notice", 3, server, msg, target);
       }
     } else {
+      sign = (g_hash_table_lookup(optlist, "sign") ? TRUE :
+             settings_get_bool("sign_private_messages") ? TRUE : FALSE);
       if (silc_send_msg(server, target, (message != NULL ? message : msg),
                        (message != NULL ? strlen(message) : strlen(msg)),
                        SILC_MESSAGE_FLAG_NOTICE | SILC_MESSAGE_FLAG_UTF8 |
-                       (g_hash_table_lookup(optlist, "sign") != NULL ?
-                        SILC_MESSAGE_FLAG_SIGNED : 0))) {
+                       (sign ? SILC_MESSAGE_FLAG_SIGNED : 0))) {
        if (g_hash_table_lookup(optlist, "sign"))
          signal_emit("message silc signed_own_private_notice", 3,
                          server, msg, target);
@@ -573,7 +595,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
   SilcDList clients;
   SILC_CHANNEL_REC *chanrec = NULL;
   SilcChannelEntry channel_entry = NULL;
-  char nickname[128 + 1], *tmp;
+  char *nickname = NULL, *tmp;
   int command = 0, port = 0, type = 0;
   char *hostname = NULL;
   KeyInternal internal = NULL;
@@ -581,7 +603,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
   unsigned char **argv;
   SilcUInt32 *argv_lens, *argv_types;
   char *bindhost = NULL;
-  SilcChannelEntry ch = NULL;
+  SilcChannelPrivateKey ch = NULL;
   SilcDList ckeys;
   SilcBool udp = FALSE;
   int i;
@@ -612,25 +634,23 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 
   if (type == 1) {
     if (argv[2][0] == '*') {
-      strcpy(nickname, "*");
+      nickname = strdup("*");
     } else {
       /* Parse the typed nickname. */
-      if (!silc_parse_userfqdn(argv[2], nickname, sizeof(nickname), NULL, 0)) {
-       printformat_module("fe-common/silc", server, NULL,
-                          MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
-       return;
-      }
+      silc_client_nickname_parse(silc_client, conn, argv[2], &nickname);
+      if (!nickname)
+       nickname = strdup(argv[2]);
 
       /* Find client entry */
-      clients = silc_client_get_clients_local(silc_client, conn, nickname,
-                                             argv[2]);
+      clients = silc_client_get_clients_local(silc_client, conn, argv[2],
+                                             FALSE);
       if (!clients) {
        KeyGetClients inter = silc_calloc(1, sizeof(*inter));
        inter->server = server;
        inter->data = strdup(data);
        inter->nick = strdup(nickname);
        inter->item = item;
-       silc_client_get_clients(silc_client, conn, nickname, argv[2],
+       silc_client_get_clients(silc_client, conn, nickname, NULL,
                                silc_client_command_key_get_clients, inter);
        goto out;
       }
@@ -645,20 +665,16 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
     char *name;
 
     if (argv[2][0] == '*') {
-      if (!conn->current_channel) {
-       silc_free(nickname);
+      if (!conn->current_channel)
        cmd_return_error(CMDERR_NOT_JOINED);
-      }
       name = conn->current_channel->channel_name;
     } else {
       name = argv[2];
     }
 
     chanrec = silc_channel_find(server, name);
-    if (chanrec == NULL) {
-      silc_free(nickname);
+    if (chanrec == NULL)
       cmd_return_error(CMDERR_CHAN_NOT_FOUND);
-    }
     channel_entry = chanrec->entry;
   }
 
@@ -731,6 +747,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
        if (!ckeys)
          goto out;
 
+       silc_dlist_start(ckeys);
        if (!number || number > silc_dlist_count(ckeys)) {
          silc_dlist_uninit(ckeys);
          goto out;
@@ -750,7 +767,6 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
     }
   }
 
-#if 0
   /* List command */
   if (!strcasecmp(argv[3], "list")) {
     command = 3;
@@ -830,7 +846,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
       silc_client_free_private_message_keys(keys, keys_count);
 
     } else if (type == 2) {
-      int k, len;
+      int len;
       char buf[1024];
 
       ckeys = silc_client_list_channel_private_keys(silc_client, conn,
@@ -843,20 +859,21 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
       if (!ckeys)
        goto out;
 
-      for (k = 0; k < keys_count; k++) {
+      silc_dlist_start(ckeys);
+      while ((ch = silc_dlist_get(ckeys))) {
        memset(buf, 0, sizeof(buf));
        strncat(buf, "  ", 2);
 
-       len = strlen(silc_cipher_get_name(keys[k]->cipher));
-       strncat(buf, silc_cipher_get_name(keys[k]->cipher),
+       len = strlen(silc_cipher_get_name(ch->send_key));
+       strncat(buf, silc_cipher_get_name(ch->send_key),
                len > 16 ? 16 : len);
        if (len < 16)
          for (i = 0; i < 16 - len; i++)
            strcat(buf, " ");
        strcat(buf, " ");
 
-       len = strlen(silc_hmac_get_name(keys[k]->hmac));
-       strncat(buf, silc_hmac_get_name(keys[k]->hmac), len > 16 ? 16 : len);
+       len = strlen(silc_hmac_get_name(ch->hmac));
+       strncat(buf, silc_hmac_get_name(ch->hmac), len > 16 ? 16 : len);
        if (len < 16)
          for (i = 0; i < 16 - len; i++)
            strcat(buf, " ");
@@ -867,12 +884,11 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
        silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", buf);
       }
 
-      silc_client_free_channel_private_keys(keys, keys_count);
+      silc_dlist_uninit(ckeys);
     }
 
     goto out;
   }
-#endif /* 0 */
 
   /* Send command is used to send key agreement */
   if (!strcasecmp(argv[3], "agreement")) {
@@ -947,6 +963,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
       if (!ckeys)
        goto out;
 
+      silc_dlist_start(ckeys);
       if (argc == 4) {
        chanrec->cur_key++;
        if (chanrec->cur_key >= silc_dlist_count(ckeys))
@@ -1042,41 +1059,46 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
   }
 
  out:
+  silc_free(nickname);
   return;
 }
 
-#if 0
 void silc_list_key(const char *pub_filename, int verbose)
 {
   SilcPublicKey public_key;
   SilcPublicKeyIdentifier ident;
+  SilcSILCPublicKey silc_pubkey;
   char *fingerprint, *babbleprint;
   unsigned char *pk;
   SilcUInt32 pk_len;
-  SilcPKCS pkcs;
   SilcUInt32 key_len = 0;
   int is_server_key = (strstr(pub_filename, "serverkeys") != NULL);
 
-  if (silc_pkcs_load_public_key((char *)pub_filename, &public_key,
-                                SILC_PKCS_FILE_PEM) == FALSE)
-    if (silc_pkcs_load_public_key((char *)pub_filename, &public_key,
-                                  SILC_PKCS_FILE_BIN) == FALSE) {
-      printformat_module("fe-common/silc", NULL, NULL,
-                         MSGLEVEL_CRAP, SILCTXT_LISTKEY_LOADPUB,
-                         pub_filename);
-      return;
-    }
+  if (!silc_pkcs_load_public_key((char *)pub_filename, SILC_PKCS_ANY,
+                                &public_key)) {
+    printformat_module("fe-common/silc", NULL, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_LISTKEY_LOADPUB,
+                      pub_filename);
+    return;
+  }
+
+  /* Print only SILC public keys */
+  if (silc_pkcs_get_type(public_key) != SILC_PKCS_SILC) {
+    printformat_module("fe-common/silc", NULL, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_LISTKEY_LOADPUB,
+                      pub_filename);
+    return;
+  }
 
-  ident = silc_pkcs_decode_identifier(public_key->identifier);
+  silc_pubkey = silc_pkcs_public_key_get_pkcs(SILC_PKCS_SILC, public_key);
+  ident = &silc_pubkey->identifier;
 
-  pk = silc_pkcs_public_key_encode(public_key, &pk_len);
+  pk = silc_pkcs_public_key_encode(NULL, public_key, &pk_len);
+  if (!pk)
+    return;
   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);
-    silc_pkcs_free(pkcs);
-  }
+  key_len = silc_pkcs_public_key_get_len(public_key);
 
   printformat_module("fe-common/silc", NULL, NULL,
                      MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_FILE,
@@ -1085,11 +1107,15 @@ void silc_list_key(const char *pub_filename, int verbose)
   if (verbose)
     printformat_module("fe-common/silc", NULL, NULL,
                        MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_ALG,
-                       public_key->name);
+                      silc_pkcs_get_name(public_key));
   if (key_len && verbose)
     printformat_module("fe-common/silc", NULL, NULL,
                         MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_BITS,
                         (unsigned int)key_len);
+  if (ident->version && verbose)
+    printformat_module("fe-common/silc", NULL, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_VER,
+                        ident->version);
   if (ident->realname && (!is_server_key || verbose))
     printformat_module("fe-common/silc", NULL, NULL,
                        MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_RN,
@@ -1128,8 +1154,6 @@ void silc_list_key(const char *pub_filename, int verbose)
   silc_free(babbleprint);
   silc_free(pk);
   silc_pkcs_public_key_free(public_key);
-  silc_pkcs_free_identifier(ident);
-
 }
 
 void silc_list_keys_in_dir(const char *dirname, const char *where)
@@ -1140,7 +1164,7 @@ void silc_list_keys_in_dir(const char *dirname, const char *where)
   dir = opendir(dirname);
 
   if (dir == NULL)
-         cmd_return_error(CMDERR_ERRNO);
+    cmd_return_error(CMDERR_ERRNO);
 
   printformat_module("fe-common/silc", NULL, NULL,
                      MSGLEVEL_CRAP, SILCTXT_LISTKEY_LIST,
@@ -1190,9 +1214,7 @@ void silc_list_file(const char *filename)
 list_key:
 
   silc_list_key(path, TRUE);
-
 }
-#endif /* 0 */
 
 /* Lists locally saved client and server public keys. */
 static void command_listkeys(const char *data, SILC_SERVER_REC *server,
@@ -1239,7 +1261,6 @@ void silc_channels_init(void)
   signal_add("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed);
   signal_add("server connected", (SIGNAL_FUNC) sig_connected);
   signal_add("server quit", (SIGNAL_FUNC) sig_server_quit);
-  signal_add("gui exit", (SIGNAL_FUNC) sig_gui_quit);
   signal_add("mime", (SIGNAL_FUNC) sig_mime);
 
   command_bind_silc("part", MODULE_NAME, (SIGNAL_FUNC) command_part);
@@ -1248,9 +1269,9 @@ void silc_channels_init(void)
   command_bind_silc("notice", MODULE_NAME, (SIGNAL_FUNC) command_notice);
   command_bind_silc("away", MODULE_NAME, (SIGNAL_FUNC) command_away);
   command_bind_silc("key", MODULE_NAME, (SIGNAL_FUNC) command_key);
-  //  command_bind("listkeys", MODULE_NAME, (SIGNAL_FUNC) command_listkeys);
+  command_bind("listkeys", MODULE_NAME, (SIGNAL_FUNC) command_listkeys);
 
-  //command_set_options("listkeys", "clients servers");
+  command_set_options("listkeys", "clients servers");
   command_set_options("action", "sign channel");
   command_set_options("notice", "sign channel");
 
@@ -1262,7 +1283,6 @@ void silc_channels_deinit(void)
   signal_remove("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed);
   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
   signal_remove("server quit", (SIGNAL_FUNC) sig_server_quit);
-  signal_remove("gui exit", (SIGNAL_FUNC) sig_gui_quit);
   signal_remove("mime", (SIGNAL_FUNC) sig_mime);
 
   command_unbind("part", (SIGNAL_FUNC) command_part);
@@ -1271,7 +1291,7 @@ void silc_channels_deinit(void)
   command_unbind("notice", (SIGNAL_FUNC) command_notice);
   command_unbind("away", (SIGNAL_FUNC) command_away);
   command_unbind("key", (SIGNAL_FUNC) command_key);
-  //  command_unbind("listkeys", (SIGNAL_FUNC) command_listkeys);
+  command_unbind("listkeys", (SIGNAL_FUNC) command_listkeys);
 
   silc_nicklist_deinit();
 }