updates.
[silc.git] / apps / silc / client_ops.c
index a127140ce0b0fc7112542f2683b24431fcdb9edd..8d9566fe14a84ecf91d455799e7b26e2bb42ab47 100644 (file)
@@ -46,22 +46,26 @@ void silc_say(SilcClient client, SilcClientConnection conn,
    received in the packet. The `channel_name' is the name of the channel. */
 
 void silc_channel_message(SilcClient client, SilcClientConnection conn,
-                         char *sender, char *channel_name, char *msg)
+                         SilcClientEntry sender, SilcChannelEntry channel
+                         , char *msg)
 {
   /* Message from client */
-  if (!strcmp(conn->current_channel->channel_name, channel_name))
-    silc_print(client, "<%s> %s", sender, msg);
+  if (conn && !strcmp(conn->current_channel->channel_name, 
+                     channel->channel_name))
+    silc_print(client, "<%s> %s", sender ? sender->nickname : "[<unknown>]", 
+              msg);
   else
-    silc_print(client, "<%s:%s> %s", sender, channel_name, msg);
+    silc_print(client, "<%s:%s> %s", sender ? sender->nickname : "[<unknown>]",
+              channel->channel_name, msg);
 }
 
 /* Private message to the client. The `sender' is the nickname of the
    sender received in the packet. */
 
 void silc_private_message(SilcClient client, SilcClientConnection conn,
-                         char *sender, char *msg)
+                         SilcClientEntry sender, char *msg)
 {
-  silc_print(client, "*%s* %s", sender, msg);
+  silc_print(client, "*%s* %s", sender->nickname, msg);
 }
 
 
@@ -81,7 +85,7 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
   char message[4096];
   SilcClientEntry client_entry, client_entry2;
   SilcChannelEntry channel_entry;
-  char *tmp;
+  char *tmp = NULL;
   unsigned int tmp_int;
 
   va_start(vp, type);
@@ -126,12 +130,15 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
 
   case SILC_NOTIFY_TYPE_SIGNOFF:
     client_entry = va_arg(vp, SilcClientEntry);
+    tmp = va_arg(vp, char *);
     if (client_entry->server)
-      snprintf(message, sizeof(message), "Signoff: %s@%s", 
-              client_entry->nickname, client_entry->server);
+      snprintf(message, sizeof(message), "Signoff: %s@%s %s%s%s", 
+              client_entry->nickname, client_entry->server,
+              tmp ? "(" : "", tmp ? tmp : "", tmp ? ")" : "");
     else
-      snprintf(message, sizeof(message), "Signoff: %s", 
-              client_entry->nickname);
+      snprintf(message, sizeof(message), "Signoff: %s %s%s%s", 
+              client_entry->nickname,
+              tmp ? "(" : "", tmp ? tmp : "", tmp ? ")" : "");
     break;
 
   case SILC_NOTIFY_TYPE_TOPIC_SET:
@@ -182,10 +189,10 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     client_entry2 = va_arg(vp, SilcClientEntry);
     channel_entry = va_arg(vp, SilcChannelEntry);
     if (tmp)
-      snprintf(message, sizeof(message), "%s changed %s mode to +%s", 
+      snprintf(message, sizeof(message), "%s changed %s's mode to +%s", 
               client_entry->nickname, client_entry2->nickname, tmp);
     else
-      snprintf(message, sizeof(message), "%s removed %s modes", 
+      snprintf(message, sizeof(message), "%s removed %s's modes", 
               client_entry->nickname, client_entry2->nickname);
     if (client_entry2 == conn->local_entry) {
       if (app->screen->bottom_line->mode)
@@ -219,6 +226,30 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     }
     return;
 
+  case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
+    break;
+
+  case SILC_NOTIFY_TYPE_KICKED:
+    client_entry = va_arg(vp, SilcClientEntry);
+    tmp = va_arg(vp, char *);
+    channel_entry = va_arg(vp, SilcChannelEntry);
+
+    if (client_entry == conn->local_entry) {
+      snprintf(message, sizeof(message), 
+              "You have been kicked off channel %s %s%s%s", 
+              conn->current_channel->channel_name,
+              tmp ? "(" : "", tmp ? tmp : "", tmp ? ")" : "");
+    } else {
+      snprintf(message, sizeof(message), 
+              "%s%s%s has been kicked off channel %s %s%s%s", 
+              client_entry->nickname, 
+              client_entry->server ? "@" : "",
+              client_entry->server ? client_entry->server : "",
+              conn->current_channel->channel_name,
+              tmp ? "(" : "", tmp ? tmp : "", tmp ? ")" : "");
+    }
+    break;
+
   default:
     break;
   }
@@ -246,6 +277,7 @@ void silc_command(SilcClient client, SilcClientConnection conn,
 
   switch(command)
     {
+       
     case SILC_COMMAND_QUIT:
       app->screen->bottom_line->channel = NULL;
       silc_screen_print_bottom_line(app->screen, 0);
@@ -263,6 +295,56 @@ void silc_command(SilcClient client, SilcClientConnection conn,
     }
 }
 
+/* We've resolved all clients we don't know about, now just print the
+   users from the channel on the screen. */
+
+void silc_client_show_users(SilcClient client,
+                           SilcClientConnection conn,
+                           SilcClientEntry *clients,
+                           unsigned int clients_count,
+                           void *context)
+{
+  SilcChannelEntry channel = (SilcChannelEntry)context;
+  SilcChannelUser chu;
+  int k = 0, len1 = 0, len2 = 0;
+  char *name_list = NULL;
+
+  if (!clients)
+    return;
+
+  silc_list_start(channel->clients);
+  while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
+    char *m, *n = chu->client->nickname;
+    if (!n)
+      continue;
+
+    len2 = strlen(n);
+    len1 += len2;
+    
+    name_list = silc_realloc(name_list, sizeof(*name_list) * (len1 + 3));
+    
+    m = silc_client_chumode_char(chu->mode);
+    if (m) {
+      memcpy(name_list + (len1 - len2), m, strlen(m));
+      len1 += strlen(m);
+      silc_free(m);
+    }
+    
+    memcpy(name_list + (len1 - len2), n, len2);
+    name_list[len1] = 0;
+    
+    if (k == silc_list_count(channel->clients) - 1)
+      break;
+    memcpy(name_list + len1, " ", 1);
+    len1++;
+    k++;
+  }
+
+  client->ops->say(client, conn, "Users on %s: %s", channel->channel_name, 
+                  name_list);
+  silc_free(name_list);
+}
+
 /* Command reply handler. This function is called always in the command reply
    function. If error occurs it will be called as well. Normal scenario
    is that it will be called after the received command data has been parsed
@@ -284,8 +366,8 @@ void silc_command_reply(SilcClient client, SilcClientConnection conn,
                        SilcCommand command, SilcCommandStatus status, ...)
 {
   SilcClientInternal app = (SilcClientInternal)client->application;
+  SilcChannelUser chu;
   va_list vp;
-  int i;
 
   if (!success)
     return;
@@ -294,16 +376,77 @@ void silc_command_reply(SilcClient client, SilcClientConnection conn,
 
   switch(command)
     {
+    case SILC_COMMAND_WHOIS:
+      {
+       char buf[1024], *nickname, *username, *realname;
+       int len;
+       unsigned int idle;
+
+       (void)va_arg(vp, SilcClientEntry);
+       nickname = va_arg(vp, char *);
+       username = va_arg(vp, char *);
+       realname = va_arg(vp, char *);
+       (void)va_arg(vp, void *);
+       idle = va_arg(vp, unsigned int);
+
+       memset(buf, 0, sizeof(buf));
+
+       if (nickname) {
+         len = strlen(nickname);
+         strncat(buf, nickname, len);
+         strncat(buf, " is ", 4);
+       }
+       
+       if (username) {
+         strncat(buf, username, strlen(nickname));
+       }
+       
+       if (realname) {
+         strncat(buf, " (", 2);
+         strncat(buf, realname, strlen(realname));
+         strncat(buf, ")", 1);
+       }
+
+       client->ops->say(client, conn, "%s", buf);
+       if (idle && nickname)
+         client->ops->say(client, conn, "%s has been idle %d %s",
+                          nickname,
+                          idle > 60 ? (idle / 60) : idle,
+                          idle > 60 ? "minutes" : "seconds");
+      }
+      break;
 
     case SILC_COMMAND_JOIN:
       {
        unsigned int mode;
+       char *topic;
+       SilcBuffer client_id_list;
+       unsigned int list_count;
+       SilcChannelEntry channel;
 
        app->screen->bottom_line->channel = va_arg(vp, char *);
-       (void)va_arg(vp, void *);
+       channel = va_arg(vp, SilcChannelEntry);
        mode = va_arg(vp, unsigned int);
+       (void)va_arg(vp, unsigned int);
+       (void)va_arg(vp, unsigned char *);
+       (void)va_arg(vp, unsigned char *);
+       (void)va_arg(vp, unsigned char *);
+       topic = va_arg(vp, char *);
+       (void)va_arg(vp, unsigned char *);
+       list_count = va_arg(vp, unsigned int);
+       client_id_list = va_arg(vp, SilcBuffer);
+
+       if (topic)
+         client->ops->say(client, conn, "Topic for %s: %s", 
+                          app->screen->bottom_line->channel, topic);
+       
        app->screen->bottom_line->channel_mode = silc_client_chmode(mode);
        silc_screen_print_bottom_line(app->screen, 0);
+
+       /* Resolve the client information */
+       silc_client_get_clients_by_list(client, conn, list_count,
+                                       client_id_list,
+                                       silc_client_show_users, channel);
       }
       break;
 
@@ -318,17 +461,20 @@ void silc_command_reply(SilcClient client, SilcClientConnection conn,
       }
       break;
 
-    case SILC_COMMAND_NAMES:
-      for (i = 0; i < conn->current_channel->clients_count; i++)
-       if (conn->current_channel->clients[i].client == conn->local_entry) {
+    case SILC_COMMAND_USERS:
+      silc_list_start(conn->current_channel->clients);
+      while ((chu = silc_list_get(conn->current_channel->clients)) 
+            != SILC_LIST_END) {
+       if (chu->client == conn->local_entry) {
          if (app->screen->bottom_line->mode)
            silc_free(app->screen->bottom_line->mode);
          app->screen->bottom_line->mode = 
-           silc_client_chumode_char(conn->current_channel->clients[i].mode);
+           silc_client_chumode_char(chu->mode);
          silc_screen_print_bottom_line(app->screen, 0);
          break;
        }
       break;
+      }
     }
 }
 
@@ -355,6 +501,7 @@ void silc_disconnect(SilcClient client, SilcClientConnection conn)
 
   app->screen->bottom_line->connection = NULL;
   silc_screen_print_bottom_line(app->screen, 0);
+  app->conn = NULL;
 }
 
 /* Asks passphrase from user on the input line. */
@@ -593,10 +740,26 @@ int silc_get_auth_method(SilcClient client, SilcClientConnection conn,
 void silc_failure(SilcClient client, SilcClientConnection conn, 
                  SilcProtocol protocol, void *failure)
 {
-  SilcClientInternal app = (SilcClientInternal)client->application;
 
 }
 
+/* Asks whether the user would like to perform the key agreement protocol.
+   This is called after we have received an key agreement packet or an
+   reply to our key agreement packet. This returns TRUE if the user wants
+   the library to perform the key agreement protocol and FALSE if it is not
+   desired (application may start it later by calling the function
+   silc_client_perform_key_agreement). */
+
+int silc_key_agreement(SilcClient client, SilcClientConnection conn,
+                      SilcClientEntry client_entry, char *hostname,
+                      int port,
+                      SilcKeyAgreementCallback *completion,
+                      void **context)
+{
+
+  return FALSE;
+}
+
 /* SILC client operations */
 SilcClientOperations ops = {
   say:                  silc_say,
@@ -611,4 +774,5 @@ SilcClientOperations ops = {
   verify_server_key:    silc_verify_server_key,
   ask_passphrase:       silc_ask_passphrase,
   failure:              silc_failure,
+  key_agreement:        silc_key_agreement,
 };