]> git.cworth.org Git - ttt/blobdiff - src/ttt-client.c
2005-12-05 Richard D. Worth <richard@theworths.org>
[ttt] / src / ttt-client.c
index c92c4ebe1cd8e174e772d29c1aa40ed23a52eabf..9d88380640bd096bc950c0965a2f78f467753086 100644 (file)
@@ -84,6 +84,16 @@ _ttt_client_execute_quit (ttt_client_t *client,
                          char         **args,
                          int          num_args);
 
+static ttt_error_t
+_ttt_client_execute_invite (ttt_client_t *client,
+                           char         **args,
+                           int          num_args);
+
+static ttt_error_t
+_ttt_client_execute_accept (ttt_client_t *client,
+                           char         **args,
+                           int          num_args);
+
 typedef struct _ttt_command_description {
     const char         *command;
     int                args_min;
@@ -100,6 +110,12 @@ ttt_command_description_t command_descriptions[] = {
     {"HELP",    0, 1, _ttt_client_execute_help,
      "HELP <command>          ", "Display help for a command."},
 
+    {"INVITE",  1, 1, _ttt_client_execute_invite,
+     "INVITE <username>       ", "Invite a player to play a game."},
+
+    {"ACCEPT",  1, 1, _ttt_client_execute_accept,
+     "ACCEPT <username>       ", "Accept a game invitation."},
+
     {"MESSAGE", 1, 1, _ttt_client_execute_message,
      "MESSAGE <message>       ", "Send a message to everyone."},
 
@@ -228,8 +244,8 @@ _ttt_client_execute_message (ttt_client_t  *client,
 
 static ttt_error_t
 _ttt_client_execute_help (ttt_client_t  *client,
-                         char     **args,
-                         int      num_args)
+                         char          **args,
+                         int           num_args)
 {
     char *response;
     char *command;
@@ -334,6 +350,78 @@ _ttt_client_execute_quit (ttt_client_t *client,
     return TTT_ERROR_QUIT_REQUESTED;
 }
 
+static ttt_error_t
+_ttt_client_execute_invite (ttt_client_t *client,
+                           char         **args,
+                           int          num_args)
+{
+    const char *username;
+    char *response;
+    char *notice;
+    ttt_error_t error;
+
+    assert (num_args == 1);
+
+    username = args[0];
+
+    if (!client->registered)
+        return TTT_ERROR_NO_NAME_SET;
+
+    error = ttt_server_verify_username (client->server,        username);
+    if (error)
+       return error;
+
+    xasprintf (&response, "INVITE\r\n");
+    ttt_client_send (client, response);
+
+    xasprintf (&notice, "NOTICE INVITE %s %s\r\n",
+              client->username,
+              username);
+    ttt_server_broadcast (client->server, notice);
+
+    free (notice);
+    free (response);
+
+    return TTT_ERROR_NONE;
+}
+
+static ttt_error_t
+_ttt_client_execute_accept (ttt_client_t *client,
+                           char         **args,
+                           int          num_args)
+{
+    const char *username;
+    char *response;
+    char *notice;
+    ttt_error_t error;
+
+    assert (num_args == 1);
+
+    username = args[0];
+
+    if (!client->registered)
+        return TTT_ERROR_NO_NAME_SET;
+
+    error = ttt_server_verify_username (client->server,        username);
+    if (error)
+       return error;
+
+    xasprintf (&response, "ACCEPT\r\n");
+    ttt_client_send (client, response);
+
+    xasprintf (&notice, "NOTICE ACCEPT %s %s\r\n",
+              client->username,
+              username);
+    ttt_server_broadcast (client->server, notice);
+
+    /* XXX: Start a new game */
+
+    free (notice);
+    free (response);
+
+    return TTT_ERROR_NONE;
+}
+
 static void
 _free_request (ttt_client_t *client);