]> git.cworth.org Git - loudgame/commitdiff
Add say and whisper commands for chatting.
authorCarl Worth <cworth@cworth.org>
Tue, 19 Feb 2008 19:38:56 +0000 (11:38 -0800)
committerCarl Worth <cworth@cworth.org>
Tue, 19 Feb 2008 19:38:56 +0000 (11:38 -0800)
loudgame.c

index aaeea5f7cec620f8531683e56cc25e0f3c0b4421..a540fa7fc848b1a452cf4320a584be9e71f2645e 100644 (file)
@@ -187,21 +187,45 @@ loudgame_broadcastf (loudgame_t   *lg,
 }
 
 static void
-handle_command (LmConnection   *connection,
+handle_command (loudgame_t     *lg,
                const char      *peer,
-               const char      *command,
-               loudgame_t      *lg)
+               const char      *command)
 {
-    char *error;
-
     if (strcmp (command, "quit") == 0) {
        loudgame_quit (lg, 0);
        return;
     }
 
-    error = g_strdup_printf ("Unknown command: '%s'", command);
-    loudgame_send (lg, peer, error);
-    free (error);
+    loudgame_sendf (lg, "Unknown command: '%s'", command);
+}
+
+static void
+handle_say (loudgame_t *lg,
+           const char  *peer,
+           const char  *message)
+{
+    loudgame_broadcastf (lg, "%s: %s", peer, message);
+}
+
+static void
+handle_whisper (loudgame_t  *lg,
+               const char  *peer,
+               const char  *body)
+{
+    const char *recipient, *space, *message;
+
+    space = strchr (body, ' ');
+    if (space == NULL) {
+       loudgame_sendf (lg, peer,
+                       "Error: whisper should be of the form 'user message'");
+       return;
+    }
+
+    recipient = g_strndup (body, space - body);
+
+    message = space + 1;
+
+    loudgame_sendf (lg, recipient, "*%s*: %s", peer, message);
 }
  
 static LmHandlerResult
@@ -231,10 +255,16 @@ handle_messages (LmMessageHandler *handler,
 
     body_str = lm_message_node_get_value (body);
 
-    if (body_str && body_str[0] == '%')
-       handle_command (connection, peer, body_str + 1, lg);
-    else if (lg->handle_message)
+    if (body_str) {
+       if (body_str[0] == '%')
+           handle_command (lg, peer, body_str + 1);
+       else if (strncmp (body_str, "say ", 4) == 0)
+           handle_say (lg, peer, body_str + 4);
+       else if (strncmp (body_str, "whisper ", 8) == 0)
+           handle_whisper (lg, peer, body_str + 8);
+       else if (lg->handle_message)
        (lg->handle_message) (lg, peer, body_str);
+    }
        
     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 }