Added AWAY command.
authorPekka Riikonen <priikone@silcnet.org>
Wed, 19 Jul 2000 07:06:33 +0000 (07:06 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Wed, 19 Jul 2000 07:06:33 +0000 (07:06 +0000)
apps/silc/client.h
apps/silc/command.c
apps/silc/command_reply.c

index 491464ee7d1ec23cd371de520a13bca34cefdeb6..b4a5a0e213e09a3f8ae099a72886fe60e5be8f44 100644 (file)
@@ -38,6 +38,15 @@ typedef struct SilcClientPingStruct {
   char *dest_name;
 } SilcClientPing;
 
+/* Structure to hold away messages set by user. This is mainly created
+   for future extensions where away messages could be set according filters
+   such as nickname and hostname. For now only one away message can 
+   be set in one connection. */
+typedef struct SilcClientAwayStruct {
+  char *away;
+  struct SilcClientAwayStruct *next;
+} SilcClientAway;
+
 /* Window structure used in client to associate all the important
    connection (window) specific data to this structure. How the window
    actually appears on the screen in handeled by the silc_screen*
@@ -105,6 +114,9 @@ struct SilcClientWindowObject {
   SilcClientPing *ping;
   unsigned int ping_count;
 
+  /* Set away message */
+  SilcClientAway *away;
+
   /* The actual physical screen. This data is handled by the
      screen handling routines. */
   void *screen;
index 98f9c54183a4beb320cf35990fed38ba7584c857..f0d3e18139555f1c2b267d83fde6f4149255de7f 100644 (file)
@@ -20,6 +20,9 @@
 /*
  * $Id$
  * $Log$
+ * Revision 1.10  2000/07/19 07:06:33  priikone
+ *     Added AWAY command.
+ *
  * Revision 1.9  2000/07/12 05:56:32  priikone
  *     Major rewrite of ID Cache system. Support added for the new
  *     ID cache system.
@@ -830,7 +833,10 @@ SILC_CLIENT_CMD_FUNC(version)
   silc_client_command_free(cmd);
 }
 
-/* Command MSG. Sends private message to user or list of users. */
+/* Command MSG. Sends private message to user or list of users. Note that
+   private messages are not really commands, they are message packets,
+   however, on user interface it is convenient to show them as commands
+   as that is the common way of sending private messages (like in IRC). */
 /* XXX supports only one destination */
 
 SILC_CLIENT_CMD_FUNC(msg)
@@ -882,6 +888,43 @@ SILC_CLIENT_CMD_FUNC(msg)
   silc_client_command_free(cmd);
 }
 
+/* Local command AWAY. Client replies with away message to whomever sends
+   private message to the client if the away message is set. If this is
+   given without arguments the away message is removed. */
+
 SILC_CLIENT_CMD_FUNC(away)
 {
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientWindow win = NULL;
+  SilcClient client = cmd->client;
+
+  if (!cmd->client->current_win->sock) {
+    SILC_NOT_CONNECTED(cmd->client);
+    goto out;
+  }
+
+  win = (SilcClientWindow)cmd->sock->user_data;
+
+  if (cmd->argc == 1) {
+    if (win->away) {
+      silc_free(win->away->away);
+      silc_free(win->away);
+      win->away = NULL;
+
+      silc_say(client, "Away message removed");
+    }
+  } else {
+
+    if (win->away)
+      silc_free(win->away->away);
+    else
+      win->away = silc_calloc(1, sizeof(*win->away));
+    
+    win->away->away = strdup(cmd->argv[1]);
+
+    silc_say(client, "Away message set: %s", win->away->away);
+  }
+
+ out:
+  silc_client_command_free(cmd);
 }
index 1362f79cee1202d3f5a161325a7d1a472dfc8532..df4d9a4309fb2b7bfa9abf71720b1ab7c0b5ad77 100644 (file)
@@ -24,6 +24,9 @@
 /*
  * $Id$
  * $Log$
+ * Revision 1.8  2000/07/19 07:06:51  priikone
+ *     Added AWAY command.
+ *
  * Revision 1.7  2000/07/12 05:56:32  priikone
  *     Major rewrite of ID Cache system. Support added for the new
  *     ID cache system.
@@ -747,13 +750,16 @@ SILC_CLIENT_CMD_REPLY_FUNC(names)
 }
 
 /* Private message received. This processes the private message and
-   finally displays it on the screen. */
+   finally displays it on the screen. Note that private messages are not
+   really commands except on user interface which is reason why this
+   handling resides here. */
 
 SILC_CLIENT_CMD_REPLY_FUNC(msg)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
   SilcClient client = cmd->client;
-  SilcBuffer buffer = (SilcBuffer)cmd->context;
+  SilcClientWindow win = (SilcClientWindow)cmd->sock->user_data;
+  SilcBuffer buffer = (SilcBuffer)cmd->packet->buffer;
   unsigned short nick_len;
   unsigned char *nickname, *message;
 
@@ -766,6 +772,46 @@ SILC_CLIENT_CMD_REPLY_FUNC(msg)
   message = silc_calloc(buffer->len + 1, sizeof(char));
   memcpy(message, buffer->data, buffer->len);
   silc_print(client, "*%s* %s", nickname, message);
+
+  /* See if we are away (gone). If we are away we will reply to the
+     sender with the set away message. */
+  if (win->away && win->away->away) {
+    SilcClientID *remote_id;
+    SilcClientEntry remote_client;
+    SilcIDCacheEntry id_cache;
+
+    if (cmd->packet->src_id_type != SILC_ID_CLIENT)
+      goto out;
+
+    remote_id = silc_id_str2id(cmd->packet->src_id, SILC_ID_CLIENT);
+    if (!remote_id)
+      goto out;
+
+    /* Check whether we know this client already */
+    if (!silc_idcache_find_by_id_one(win->client_cache, remote_id,
+                                    SILC_ID_CLIENT, &id_cache))
+      {
+       /* Allocate client entry */
+       remote_client = silc_calloc(1, sizeof(*remote_client));
+       remote_client->id = remote_id;
+       remote_client->nickname = strdup(nickname);
+
+       /* Save the client to cache */
+       silc_idcache_add(win->client_cache, remote_client->nickname,
+                        SILC_ID_CLIENT, remote_client->id, remote_client, 
+                        TRUE);
+      } else {
+       silc_free(remote_id);
+       remote_client = (SilcClientEntry)id_cache->context;
+      }
+
+    /* Send the away message */
+    silc_client_packet_send_private_message(client, cmd->sock, remote_client,
+                                           win->away->away,
+                                           strlen(win->away->away), TRUE);
+  }
+
+ out:
   memset(message, 0, buffer->len);
   silc_free(message);
 }