From 747a73feb09523f99f5fb65dcb048ba38cc6eeb6 Mon Sep 17 00:00:00 2001 From: Richard Worth Date: Tue, 6 Dec 2005 02:50:42 +0000 Subject: [PATCH 1/1] 2005-12-05 Richard D. Worth * 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 | 12 +++++++ PROTOCOL | 58 ++++++++++++++++-------------- TODO | 15 ++++---- src/ttt-client.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++-- src/ttt-server.c | 24 +++++++++++++ src/ttt-server.h | 14 ++++++++ 6 files changed, 179 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 498579a..fdbb515 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2005-12-05 Richard D. Worth + + * 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 * src/ttt-curses-client.c: (mvprintstr), (mvwprintstr), (wprint), diff --git a/PROTOCOL b/PROTOCOL index 4fcec63..ad6369c 100644 --- 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 0aaf881..e4f3b5d 100644 --- 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 ✓ 2.1.2. NOTICE QUIT 2.1.3. Game Invitation - 2.1.3.1. NOTICE INVITE - 2.1.3.2. NOTICE ACCEPT +✓ 2.1.3.1. NOTICE INVITE +✓ 2.1.3.2. NOTICE ACCEPT 2.1.3.3. NOTICE RETRACT 2.1.3.4. NOTICE DECLINE 2.1.4. NOTICE NEWGAME @@ -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 diff --git a/src/ttt-client.c b/src/ttt-client.c index c92c4eb..9d88380 100644 --- a/src/ttt-client.c +++ b/src/ttt-client.c @@ -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 ", "Display help for a command."}, + {"INVITE", 1, 1, _ttt_client_execute_invite, + "INVITE ", "Invite a player to play a game."}, + + {"ACCEPT", 1, 1, _ttt_client_execute_accept, + "ACCEPT ", "Accept a game invitation."}, + {"MESSAGE", 1, 1, _ttt_client_execute_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 (¬ice, "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 (¬ice, "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); diff --git a/src/ttt-server.c b/src/ttt-server.c index 8130420..c94d3d2 100644 --- a/src/ttt-server.c +++ b/src/ttt-server.c @@ -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) diff --git a/src/ttt-server.h b/src/ttt-server.h index 8f2d34b..8d0f9c4 100644 --- a/src/ttt-server.h +++ b/src/ttt-server.h @@ -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. * */ -- 2.43.0