Wed Mar 7 18:38:18 CET 2007 Jochen Eisinger <coffee@silcnet.org>
[silc.git] / apps / irssi / src / silc / core / silc-channels.c
index a0e1dcd6146732d04cd0bfadec187fabf4d369ee..4311cca5a8963f3b0496005479764076f19c293d 100644 (file)
@@ -1,8 +1,8 @@
 /*
   silc-channels.c : irssi
 
-  Copyright (C) 2000 - 2001, 2004, 2006 Timo Sirainen
-                Pekka Riikonen <priikone@silcnet.org>
+  Copyright (C) 2000 - 2001, 2004 Timo Sirainen
+                            Pekka Riikonen <priikone@poseidon.pspt.fi>
 
   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
 #include "silc-commands.h"
 
 void sig_mime(SILC_SERVER_REC *server, SILC_CHANNEL_REC *channel,
-             const char *blob, const char *nick, int verified)
+               const char *blob, const char *nick, int verified)
 {
-  unsigned char *message;
-  SilcUInt32 message_len;
-  SilcMime mime;
+  char type[128], enc[128];
+  unsigned char *data, *message;
+  SilcUInt32 data_len, message_len;
 
   if (!(IS_SILC_SERVER(server)))
     return;
 
   message = silc_unescape_data(blob, &message_len);
 
-  mime = silc_mime_decode(NULL, message, message_len);
-  if (!mime) {
+  memset(type, 0, sizeof(type));
+  memset(enc, 0, sizeof(enc));
+
+  if (!silc_mime_parse(message, message_len, NULL, 0, type, sizeof(type) - 1,
+                enc, sizeof(enc) - 1, &data, &data_len)) {
     silc_free(message);
     return;
   }
 
   printformat_module("fe-common/silc", server,
-                    channel == NULL ? NULL : channel->name,
-                    MSGLEVEL_CRAP, SILCTXT_MESSAGE_DATA,
-                    nick == NULL ? "[<unknown>]" : nick,
-                    silc_mime_get_field(mime, "Content-Type"));
+                      channel == NULL ? NULL : channel->name,
+                      MSGLEVEL_CRAP, SILCTXT_MESSAGE_DATA,
+                      nick == NULL ? "[<unknown>]" : nick, type);
 
   silc_free(message);
-  silc_mime_free(mime);
+
 }
 
 SILC_CHANNEL_REC *silc_channel_create(SILC_SERVER_REC *server,
@@ -111,7 +113,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 +124,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);
@@ -136,7 +159,7 @@ static void sig_connected(SILC_SERVER_REC *server)
 
 static void sig_server_quit(SILC_SERVER_REC *server, const char *msg)
 {
-  if (IS_SILC_SERVER(server) && server->conn)
+  if (IS_SILC_SERVER(server) && server->conn && server->conn->sock)
     silc_command_exec(server, "QUIT", msg);
 }
 
@@ -442,9 +465,9 @@ static void command_away(const char *data, SILC_SERVER_REC *server,
 }
 
 typedef struct {
-  SILC_SERVER_REC *server;
   int type;                    /* 1 = msg, 2 = channel */
-  SilcBool responder;
+  bool responder;
+  SILC_SERVER_REC *server;
 } *KeyInternal;
 
 /* Key agreement callback that is called after the key agreement protocol
@@ -459,7 +482,7 @@ static void keyagr_completion(SilcClient client,
                              SilcClientConnection conn,
                              SilcClientEntry client_entry,
                              SilcKeyAgreementStatus status,
-                             SilcSKEKeyMaterial key,
+                             SilcSKEKeyMaterial *key,
                              void *context)
 {
   KeyInternal i = (KeyInternal)context;
@@ -473,7 +496,7 @@ static void keyagr_completion(SilcClient client,
       /* Set the private key for this client */
       silc_client_del_private_message_key(client, conn, client_entry);
       silc_client_add_private_message_key_ske(client, conn, client_entry,
-                                             NULL, NULL, key);
+                                             NULL, NULL, key, i->responder);
       printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KEY_AGREEMENT_PRIVMSG,
                         client_entry->nickname);
@@ -483,7 +506,6 @@ static void keyagr_completion(SilcClient client,
     break;
 
   case SILC_KEY_AGREEMENT_ERROR:
-  case SILC_KEY_AGREEMENT_NO_MEMORY:
     printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP,
                       SILCTXT_KEY_AGREEMENT_ERROR, client_entry->nickname);
     break;
@@ -542,8 +564,8 @@ typedef struct {
 
 static void silc_client_command_key_get_clients(SilcClient client,
                                                SilcClientConnection conn,
-                                               SilcStatus status,
-                                               SilcDList clients,
+                                               SilcClientEntry *clients,
+                                               SilcUInt32 clients_count,
                                                void *context)
 {
   KeyGetClients internal = (KeyGetClients)context;
@@ -569,11 +591,11 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
                        WI_ITEM_REC *item)
 {
   SilcClientConnection conn;
-  SilcClientEntry client_entry = NULL;
-  SilcDList clients;
+  SilcClientEntry *entrys, client_entry = NULL;
+  SilcUInt32 entry_count;
   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,10 +603,6 @@ 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;
-  SilcDList ckeys;
-  SilcBool udp = FALSE;
-  int i;
 
   CMD_SILC_SERVER(server);
 
@@ -612,19 +630,19 @@ 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)) {
+      if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
        printformat_module("fe-common/silc", server, NULL,
                           MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
        return;
       }
 
       /* Find client entry */
-      clients = silc_client_get_clients_local(silc_client, conn, nickname,
-                                             argv[2]);
-      if (!clients) {
+      entrys = silc_client_get_clients_local(silc_client, conn, nickname,
+                                            argv[2], &entry_count);
+      if (!entrys) {
        KeyGetClients inter = silc_calloc(1, sizeof(*inter));
        inter->server = server;
        inter->data = strdup(data);
@@ -634,9 +652,8 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
                                silc_client_command_key_get_clients, inter);
        goto out;
       }
-
-      client_entry = silc_dlist_get(clients);
-      silc_client_list_free(silc_client, conn, clients);
+      client_entry = entrys[0];
+      silc_free(entrys);
     }
   }
 
@@ -669,17 +686,42 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
     if (argc >= 5) {
       char *cipher = NULL, *hmac = NULL;
 
-      if (argc >= 6)
-       cipher = argv[5];
-      if (argc >= 7)
-       hmac = argv[6];
-
       if (type == 1 && client_entry) {
        /* Set private message key */
+       bool responder = FALSE;
+
        silc_client_del_private_message_key(silc_client, conn, client_entry);
+
+       if (argc >= 6) {
+         if (!strcasecmp(argv[5], "-responder"))
+           responder = TRUE;
+         else
+           cipher = argv[5];
+       }
+       if (argc >= 7) {
+         if (!strcasecmp(argv[6], "-responder"))
+           responder = TRUE;
+         else
+           hmac = argv[6];
+       }
+       if (argc >= 8) {
+         if (!strcasecmp(argv[7], "-responder"))
+           responder = TRUE;
+       }
+
        silc_client_add_private_message_key(silc_client, conn, client_entry,
                                            cipher, hmac,
-                                           argv[4], argv_lens[4]);
+                                           argv[4], argv_lens[4],
+                                           (argv[4][0] == '*' ?
+                                            TRUE : FALSE), responder);
+
+       /* Send the key to the remote client so that it starts using it
+          too. */
+       /* XXX for now we don't do this.  This feature is pretty stupid
+          and should perhaps be removed altogether from SILC.
+       silc_client_send_private_message_key(silc_client, conn,
+                                            client_entry, TRUE);
+       */
       } else if (type == 2) {
        /* Set private channel key */
        if (!(channel_entry->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
@@ -689,6 +731,11 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
          goto out;
        }
 
+       if (argc >= 6)
+         cipher = argv[5];
+       if (argc >= 7)
+         hmac = argv[6];
+
        if (!silc_client_add_channel_private_key(silc_client, conn,
                                                 channel_entry, NULL,
                                                 cipher, hmac,
@@ -718,6 +765,8 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
       silc_client_del_private_message_key(silc_client, conn, client_entry);
     } else if (type == 2) {
       /* Unset channel key(s) */
+      SilcChannelPrivateKey *keys;
+      SilcUInt32 keys_count;
       int number;
 
       if (argc == 4)
@@ -726,31 +775,26 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 
       if (argc > 4) {
        number = atoi(argv[4]);
-       ckeys = silc_client_list_channel_private_keys(silc_client, conn,
-                                                     channel_entry);
-       if (!ckeys)
+       keys = silc_client_list_channel_private_keys(silc_client, conn,
+                                                    channel_entry,
+                                                    &keys_count);
+       if (!keys)
          goto out;
 
-       if (!number || number > silc_dlist_count(ckeys)) {
-         silc_dlist_uninit(ckeys);
+       if (!number || number > keys_count) {
+         silc_client_free_channel_private_keys(keys, keys_count);
          goto out;
        }
 
-       for (i = 0; i < number; i++)
-         ch = silc_dlist_get(ckeys);
-       if (!ch)
-         goto out;
-
        silc_client_del_channel_private_key(silc_client, conn, channel_entry,
-                                           ch);
-       silc_dlist_uninit(ckeys);
+                                           keys[number - 1]);
+       silc_client_free_channel_private_keys(keys, keys_count);
       }
 
       goto out;
     }
   }
 
-#if 0
   /* List command */
   if (!strcasecmp(argv[3], "list")) {
     command = 3;
@@ -830,17 +874,20 @@ 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;
+      SilcChannelPrivateKey *keys;
+      SilcUInt32 keys_count;
+      int k, i, len;
       char buf[1024];
 
-      ckeys = silc_client_list_channel_private_keys(silc_client, conn,
-                                                   channel_entry);
+      keys = silc_client_list_channel_private_keys(silc_client, conn,
+                                                  channel_entry,
+                                                  &keys_count);
 
       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
                         SILCTXT_CH_PRIVATE_KEY_LIST,
                         channel_entry->channel_name);
 
-      if (!ckeys)
+      if (!keys)
        goto out;
 
       for (k = 0; k < keys_count; k++) {
@@ -872,7 +919,6 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 
     goto out;
   }
-#endif /* 0 */
 
   /* Send command is used to send key agreement */
   if (!strcasecmp(argv[3], "agreement")) {
@@ -880,14 +926,8 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 
     if (argc >= 5)
       hostname = argv[4];
-    if (argc >= 6) {
-      if (!strcasecmp(argv[5], "UDP"))
-       udp = TRUE;
-      else
-       port = atoi(argv[5]);
-    }
-    if (argc >= 7)
-      udp = TRUE;
+    if (argc >= 6)
+      port = atoi(argv[5]);
 
     internal = silc_calloc(1, sizeof(*internal));
     internal->type = type;
@@ -895,6 +935,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 
     if (!hostname) {
       if (settings_get_bool("use_auto_addr")) {
+
         hostname = (char *)settings_get_str("auto_public_ip");
 
        /* If the hostname isn't set, treat this case as if auto_public_ip
@@ -921,14 +962,8 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 
     if (argc >= 5)
       hostname = argv[4];
-    if (argc >= 6) {
-      if (!strcasecmp(argv[5], "UDP"))
-       udp = TRUE;
-      else
-       port = atoi(argv[5]);
-    }
-    if (argc >= 7)
-      udp = TRUE;
+    if (argc >= 6)
+      port = atoi(argv[5]);
 
     internal = silc_calloc(1, sizeof(*internal));
     internal->type = type;
@@ -940,40 +975,39 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
     command = 6;
     if (type == 2) {
       /* Unset channel key(s) */
+      SilcChannelPrivateKey *keys;
+      SilcUInt32 keys_count;
       int number;
 
-      ckeys = silc_client_list_channel_private_keys(silc_client, conn,
-                                                   channel_entry);
-      if (!ckeys)
+      keys = silc_client_list_channel_private_keys(silc_client, conn,
+                                                  channel_entry,
+                                                  &keys_count);
+      if (!keys)
        goto out;
 
       if (argc == 4) {
        chanrec->cur_key++;
-       if (chanrec->cur_key >= silc_dlist_count(ckeys))
+       if (chanrec->cur_key >= keys_count)
          chanrec->cur_key = 0;
       }
 
       if (argc > 4) {
        number = atoi(argv[4]);
-       if (!number || number > silc_dlist_count(ckeys))
+       if (!number || number > keys_count)
          chanrec->cur_key = 0;
        else
          chanrec->cur_key = number - 1;
       }
 
-      for (i = 0; i < chanrec->cur_key; i++)
-       ch = silc_dlist_get(ckeys);
-      if (!ch)
-       goto out;
-
       /* Set the current channel private key */
       silc_client_current_channel_private_key(silc_client, conn,
-                                             channel_entry, ch);
+                                             channel_entry,
+                                             keys[chanrec->cur_key]);
       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
-                        SILCTXT_CH_PRIVATE_KEY_CHANGE, i + 1,
+                        SILCTXT_CH_PRIVATE_KEY_CHANGE, chanrec->cur_key + 1,
                         channel_entry->channel_name);
 
-      silc_dlist_uninit(ckeys);
+      silc_client_free_channel_private_keys(keys, keys_count);
       goto out;
     }
   }
@@ -986,22 +1020,13 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
   }
 
   if (command == 4 && client_entry) {
-    SilcClientConnectionParams params;
-
     printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
                       SILCTXT_KEY_AGREEMENT, argv[2]);
     internal->responder = TRUE;
-
-    memset(&params, 0, sizeof(params));
-    params.local_ip = hostname;
-    params.bind_ip = bindhost;
-    params.local_port = port;
-    params.udp = udp;
-    params.timeout_secs = settings_get_int("key_exchange_timeout_secs");
-
     silc_client_send_key_agreement(
-                          silc_client, conn, client_entry, &params,
-                          irssi_pubkey, irssi_privkey,
+                          silc_client, conn, client_entry, hostname,
+                          bindhost, port,
+                          settings_get_int("key_exchange_timeout_secs"),
                           keyagr_completion, internal);
     if (!hostname)
       silc_free(internal);
@@ -1009,43 +1034,19 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
   }
 
   if (command == 5 && client_entry && hostname) {
-    SilcClientConnectionParams params;
-
     printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
                       SILCTXT_KEY_AGREEMENT_NEGOTIATE, argv[2]);
     internal->responder = FALSE;
-
-    memset(&params, 0, sizeof(params));
-    if (udp) {
-      if (settings_get_bool("use_auto_addr")) {
-       params.local_ip = (char *)settings_get_str("auto_public_ip");
-       if ((params.local_ip) && (*params.local_ip == '\0')) {
-         params.local_ip = silc_net_localip();
-       } else {
-         params.bind_ip = (char *)settings_get_str("auto_bind_ip");
-         if ((params.bind_ip) && (*params.bind_ip == '\0'))
-           params.bind_ip = NULL;
-         params.local_port = settings_get_int("auto_bind_port");
-       }
-      }
-      if (!params.local_ip)
-       params.local_ip = silc_net_localip();
-    }
-    params.udp = udp;
-    params.timeout_secs = settings_get_int("key_exchange_timeout_secs");
-
-    silc_client_perform_key_agreement(silc_client, conn, client_entry, &params,
-                                     irssi_pubkey, irssi_privkey,
+    silc_client_perform_key_agreement(silc_client, conn, client_entry,
                                      hostname, port, keyagr_completion,
                                      internal);
     goto out;
   }
 
  out:
-  return;
+  silc_free(nickname);
 }
 
-#if 0
 void silc_list_key(const char *pub_filename, int verbose)
 {
   SilcPublicKey public_key;
@@ -1132,6 +1133,7 @@ void silc_list_key(const char *pub_filename, int verbose)
 
 }
 
+
 void silc_list_keys_in_dir(const char *dirname, const char *where)
 {
   DIR *dir;
@@ -1192,7 +1194,6 @@ 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,
@@ -1248,9 +1249,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");
 
@@ -1271,7 +1272,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();
 }