From: Carl Worth Date: Tue, 19 Feb 2008 19:38:56 +0000 (-0800) Subject: Add say and whisper commands for chatting. X-Git-Url: https://git.cworth.org/git?p=loudgame;a=commitdiff_plain;h=2aa374ba86da3e1a1e9f317675c5652949b3fdac Add say and whisper commands for chatting. --- diff --git a/loudgame.c b/loudgame.c index aaeea5f..a540fa7 100644 --- a/loudgame.c +++ b/loudgame.c @@ -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; }