X-Git-Url: https://git.cworth.org/git?p=loudgame;a=blobdiff_plain;f=loudgame.c;h=7c9114e4fffd7783763e9c1af8f3aee0d54832e3;hp=aaeea5f7cec620f8531683e56cc25e0f3c0b4421;hb=HEAD;hpb=197c1ca4317efc2c685708a248fa42da3aac60f5 diff --git a/loudgame.c b/loudgame.c index aaeea5f..7c9114e 100644 --- a/loudgame.c +++ b/loudgame.c @@ -71,7 +71,7 @@ static void connection_open_cb (LmConnection *connection, gboolean result, loudgame_t *lg) { lm_connection_authenticate (connection, - lg->name, lg->passwd, "TestLM", + lg->name, lg->passwd, "loudgame", authentication_cb, lg, FALSE, NULL); } @@ -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; }