X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-client.c;fp=src%2Fttt-client.c;h=9d88380640bd096bc950c0965a2f78f467753086;hp=c92c4ebe1cd8e174e772d29c1aa40ed23a52eabf;hb=747a73feb09523f99f5fb65dcb048ba38cc6eeb6;hpb=d7272cb44f0da95f375baba9c9b55691c98af792 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);