]> git.cworth.org Git - ttt/commitdiff
2005-12-05 Richard D. Worth <richard@theworths.org>
authorRichard Worth <richard@theworths.org>
Tue, 6 Dec 2005 02:50:42 +0000 (02:50 +0000)
committerRichard Worth <richard@theworths.org>
Tue, 6 Dec 2005 02:50:42 +0000 (02:50 +0000)
        * PROTOCOL: Fill some missing possible error occurrences.

        * TODO: Check off INVITE, ACCEPT, ERROR COMMAND, ERROR NO_USER

        * src/ttt-server.h:
        * src/ttt-server.c: (ttt_server_verify_username):
        * src/ttt-client.c: (_ttt_client_execute_help),
        (_ttt_client_execute_invite), (_ttt_client_execute_accept):
        Implement INVITE, ACCEPT

ChangeLog
PROTOCOL
TODO
src/ttt-client.c
src/ttt-server.c
src/ttt-server.h

index 498579a21a3043e9d20b08c3a425e60643258b26..fdbb515f31f50357f55b7ede766d39d579acfc5d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2005-12-05  Richard D. Worth  <richard@theworths.org>
+
+       * PROTOCOL: Fill some missing possible error occurrences.
+
+       * TODO: Check off INVITE, ACCEPT, ERROR COMMAND, ERROR NO_USER
+       
+       * src/ttt-server.h:
+       * src/ttt-server.c: (ttt_server_verify_username):
+       * src/ttt-client.c: (_ttt_client_execute_help),
+       (_ttt_client_execute_invite), (_ttt_client_execute_accept):
+       Implement INVITE, ACCEPT
+
 2005-12-05  Richard D. Worth  <richard@theworths.org>
 
        * src/ttt-curses-client.c: (mvprintstr), (mvwprintstr), (wprint),
index 4fcec635a468d92e7d491d81c35467ecaa0b8663..ad6369c25857f591422abe21bbdefe83cc5b5108 100644 (file)
--- a/PROTOCOL
+++ b/PROTOCOL
@@ -139,7 +139,7 @@ Document Conventions
 
        INVITE
 
-       Possible errors: NO_USER, BUSY, NO_NAME_SET
+       Possible errors: NO_NAME_SET, NO_USER, BUSY
 
     1.3.2. Accepting an invitation
 
@@ -149,7 +149,7 @@ Document Conventions
 
        ACCEPT
 
-       Possible errors: NO_USER, NO_NAME_SET
+       Possible errors: NO_NAME_SET, NO_USER
 
     1.3.3. Retracting an invitiation
 
@@ -159,7 +159,7 @@ Document Conventions
 
        RETRACT
 
-       Possible errors: NO_USER, NO_NAME_SET
+       Possible errors: NO_NAME_SET, NO_USER
 
     1.3.3. Declining an invitation
 
@@ -169,7 +169,7 @@ Document Conventions
 
        DECLINE
 
-       Possible errors: NO_USER, NO_NAME_SET
+       Possible errors: NO_NAME_SET, NO_USER
 
 1.4. In-game commands
 
@@ -231,7 +231,7 @@ Document Conventions
        6|7|8
     
        Possible errors: NO_NAME_SET, NO_GAME, NOT_IN_GAME,
-       NOT_YOUR_MOVE, NOT_GRID
+       NOT_PLAYING, NOT_YOUR_MOVE, NOT_GRID
     
 2. Asynchronous notification.  
 
@@ -332,13 +332,15 @@ Document Conventions
        'helo' must be sent before any command other than 'help',
        'version', 'quit'.
 
-       Possibly returned by: WHO MESSAGE INVITE ACCEPT SHOW PART MOVE
+       Possibly returned by: WHO, STATISTICS, MESSAGE, INVITE,
+       ACCEPT, RETRACT, DECLINE, SHOW, PART, MOVE
     
     3.1.2. Invalid name
     
        ERROR INVALID_NAME
     
-       All names must be of non-zero length and must be unique.
+       All names must be of non-zero length and must be unique,
+       though case-insensitive.
 
        Possibly returned by: HELO
 
@@ -350,29 +352,35 @@ Document Conventions
     
        ERROR COMMAND
     
-       An invalid command was specified
-    
+       An invalid command was specified.
+
     3.2.2. Syntax
     
        ERROR SYNTAX
     
-       A syntax error was detected
+       A syntax error was detected.
     
     3.2.3. Not number
     
        ERROR NOT_NUMBER
     
-       A non-numeric value was supplied where a number was required
+       A non-numeric value was supplied where a number was required.
        
+       Possibly returned by: MOVE
+
     3.2.4. Not a grid number
     
        ERROR NOT_GRID
     
-       The number specified in the command was not a valid grid number
+       The number specified in the command was not a valid grid
+       number.
+
+       Possibly returned by: MOVE
     
 3.3. Global command errors.
 
-    There are no errors from any of the global commands
+    There are no global command errors from any of the global
+    commands.
 
 3.4. Game management errors.
 
@@ -382,7 +390,9 @@ Document Conventions
        
        ERROR NO_GAME
        
-       A game name was provided that does not exist.
+       A game id was provided that does not exist.
+
+       Possibly returned by: SHOW, PART, MOVE
        
 3.5. User information errors
 
@@ -392,25 +402,21 @@ Document Conventions
 
        A user name was provided that does not exist.
 
+       Possibly returned by: STATISTICS, INVITE, ACCEPT, RETRACT,
+       DECLINE
+
 3.6. In-game errors
 
     3.6.1. Global game errors
     
-       3.6.1.1. Not in game
-    
-           ERROR NOT_IN_GAME
-    
-           A game playing command was made, but the user is not a
-           particpant of any game.
-    
-           Possibly returned by: MOVE
-
-       3.6.1.2. Not playing
+       3.6.1.1. Not playing
 
            ERROR NOT_PLAYING
 
            A command was executed by a watching user that is
-           permitted only to players
+           permitted only to players.
+
+           Possibly returned by: MOVE
     
     3.6.2. Moving errors 
     
@@ -418,6 +424,6 @@ Document Conventions
 
            ERROR NOT_YOUR_TURN
     
-           A move was submitted during the other player's turn
+           A move was submitted during the other player's turn.
 
            Possibly returned by: MOVE
diff --git a/TODO b/TODO
index 0aaf8818a8d67e1c00d8152356716aee0b5527c5..e4f3b5d4b2a3d3920d318b0cdc50b50281696888 100644 (file)
--- a/TODO
+++ b/TODO
@@ -14,8 +14,8 @@ S C
 ✓   1.2.5. QUIT
 ✓   1.2.6. VERSION
     1.3. Game management commands
-    1.3.1. INVITE
-    1.3.2. ACCEPT
+   1.3.1. INVITE
+   1.3.2. ACCEPT
     1.3.3. RETRACT
     1.3.4. DECLINE
     1.4. In-game commands
@@ -27,8 +27,8 @@ S C
 ✓   2.1.1. NOTICE USER <username>
 ✓   2.1.2. NOTICE QUIT <username>
     2.1.3. Game Invitation
-    2.1.3.1. NOTICE INVITE <username1> <username2>
-    2.1.3.2. NOTICE ACCEPT <usernmae2> <username1>
+   2.1.3.1. NOTICE INVITE <username1> <username2>
+✓   2.1.3.2. NOTICE ACCEPT <username2> <username1>
     2.1.3.3. NOTICE RETRACT <username1> <username2>
     2.1.3.4. NOTICE DECLINE <username2> <username1>
     2.1.4. NOTICE NEWGAME <game> <username> <username>
@@ -44,7 +44,7 @@ S C
 ✓   3.1.1. ERROR NO_NAME_SET
 ✓   3.1.2. ERROR INVALID_NAME
     3.2. Command format errors
-    3.2.1. ERROR COMMAND
+   3.2.1. ERROR COMMAND
 ✓   3.2.2. ERROR SYNTAX
     3.2.3. ERROR NOT_NUMBER
     3.2.4. ERROR NOT_GRID
@@ -52,11 +52,10 @@ S C
     3.4. Game management errors.
     3.4.1. ERROR NO_GAME
     3.5. User information errors
-    3.5.1. ERROR NO_USER
+   3.5.1. ERROR NO_USER
     3.6. In-game errors
     3.6.1. Global game errors
-    3.6.1.1. ERROR NOT_IN_GAME
-    3.6.1.2. ERROR NOT_PLAYING
+    3.6.1.1. ERROR NOT_PLAYING
     3.6.2. Moving errors 
     3.6.2.1. ERROR NOT_YOUR_TURN
 
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);
 
index 813042044873fa20b03d9ff571dfb3ab0a7f3b9f..c94d3d23a44c5fc3d624b6d87bd30ddd7c9fa0b4 100644 (file)
@@ -188,6 +188,30 @@ ttt_server_statistics (ttt_server_t *server, const char *username, char **respon
     return TTT_ERROR_NONE;
 }
 
+/* Exported: See ttt-server.h for documentation. */
+ttt_error_t
+ttt_server_verify_username (ttt_server_t *server, const char *username)
+{
+    ttt_bool_t usernamefound = FALSE;
+    char *client_username;
+    int i;
+
+    pthread_mutex_lock (&server->mutex);
+
+    for (i = 0; i < server->num_clients; i++) {
+       client_username = ttt_client_get_username (server->clients[i]);
+       if (strcasecmp (username, client_username) == 0)
+           usernamefound = TRUE;
+    }
+
+    pthread_mutex_unlock (&server->mutex);
+
+    if (!usernamefound)
+       return TTT_ERROR_NO_USER;
+
+    return TTT_ERROR_NONE;
+}
+
 /* Exported: See ttt-server.h for documentation. */
 const char*
 ttt_server_get_host (ttt_server_t *server)
index 8f2d34b7c1bd5b2b6c08cef9aa0f8d3867b9b2ec..8d0f9c410ac6a9ef69e83ba41aa0e84693ae36c7 100644 (file)
@@ -89,6 +89,20 @@ ttt_server_statistics (ttt_server_t *server,
                       const char *username,
                       char **response);
 
+/* Checks to see if the username is registered. If the username exists
+ * will return TTT_ERROR_NONE, else TTT_ERROR_NO_USER.
+ *
+ * Locking: The server mutex will be acquired and held throughout the
+ * execution of this function. Each client mutex may also be acquired
+ * and held by functions called during the execution of this function.
+ *
+ * Errors: If an error such as an IO error occurs, this function will
+ * not return.
+ */
+ttt_error_t
+ttt_server_verify_username (ttt_server_t *server,
+                           const char *username);
+
 /* Gets the server hostname.
  *
  */