Add support for autosendcmd on channel entries to the
[silc.git] / apps / irssi / src / silc / core / silc-channels.c
index 7a8797460528ff2b00e4b3dbe48d886e85773d64..055c6d7247a4df4d17af5379c96848a295408356 100644 (file)
@@ -31,6 +31,7 @@
 #include "rawlog.h"
 #include "misc.h"
 #include "settings.h"
+#include "special-vars.h"
 
 #include "channels-setup.h"
 
@@ -111,7 +112,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 +123,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,6 +162,25 @@ static void sig_server_quit(SILC_SERVER_REC *server, const char *msg)
     silc_command_exec(server, "QUIT", msg);
 }
 
+static void sig_silc_channel_joined(SILC_CHANNEL_REC *channel)
+{
+  CHANNEL_SETUP_REC *rec;
+
+  if (!IS_SILC_CHANNEL(channel))
+    return;
+  if (channel->server && channel->server->disconnected)
+    return;
+  if (channel->session_rejoin)
+    return;
+  
+  rec = channel_setup_find(channel->name, channel->server->connrec->chatnet); 
+
+  if (rec == NULL || rec->autosendcmd == NULL || !*rec->autosendcmd)
+    return;
+
+  eval_special_string(rec->autosendcmd, "", (SERVER_REC*)channel->server, (CHANNEL_REC*)channel);
+}
+
 /* Find Irssi channel entry by SILC channel entry */
 
 SILC_CHANNEL_REC *silc_channel_find_entry(SILC_SERVER_REC *server,
@@ -574,7 +615,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;
@@ -613,25 +654,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;
       }
@@ -646,20 +685,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;
   }
 
@@ -683,7 +718,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
                                            argv[4], argv_lens[4]);
       } else if (type == 2) {
        /* Set private channel key */
-       if (!(channel_entry->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
+       if (!(channel_entry) || !(channel_entry->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
          printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
                             SILCTXT_CH_PRIVATE_KEY_NOMODE,
                             channel_entry->channel_name);
@@ -732,6 +767,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;
@@ -830,7 +866,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,12 +879,13 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
       if (!ckeys)
        goto out;
 
+      silc_dlist_start(ckeys);
       while ((ch = silc_dlist_get(ckeys))) {
        memset(buf, 0, sizeof(buf));
        strncat(buf, "  ", 2);
 
-       len = strlen(silc_cipher_get_name(ch->cipher));
-       strncat(buf, silc_cipher_get_name(ch->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++)
@@ -946,6 +983,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))
@@ -1041,6 +1079,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
   }
 
  out:
+  silc_free(nickname);
   return;
 }
 
@@ -1074,6 +1113,8 @@ void silc_list_key(const char *pub_filename, int verbose)
   ident = &silc_pubkey->identifier;
 
   pk = silc_pkcs_public_key_encode(public_key, &pk_len);
+  if (!pk)
+    return;
   fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
   babbleprint = silc_hash_babbleprint(NULL, pk, pk_len);
   key_len = silc_pkcs_public_key_get_len(public_key);
@@ -1090,6 +1131,10 @@ void silc_list_key(const char *pub_filename, int 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,
@@ -1236,6 +1281,7 @@ void silc_channels_init(void)
   signal_add("server connected", (SIGNAL_FUNC) sig_connected);
   signal_add("server quit", (SIGNAL_FUNC) sig_server_quit);
   signal_add("mime", (SIGNAL_FUNC) sig_mime);
+  signal_add("channel joined", (SIGNAL_FUNC) sig_silc_channel_joined);
 
   command_bind_silc("part", MODULE_NAME, (SIGNAL_FUNC) command_part);
   command_bind_silc("me", MODULE_NAME, (SIGNAL_FUNC) command_me);
@@ -1258,6 +1304,7 @@ void silc_channels_deinit(void)
   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
   signal_remove("server quit", (SIGNAL_FUNC) sig_server_quit);
   signal_remove("mime", (SIGNAL_FUNC) sig_mime);
+  signal_remove("channel joined", (SIGNAL_FUNC) sig_silc_channel_joined);
 
   command_unbind("part", (SIGNAL_FUNC) command_part);
   command_unbind("me", (SIGNAL_FUNC) command_me);