1 /* ttt-client.c - client handling code for tic-tac-toe game server
3 * Copyright © 2005 Carl Worth
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Author: Carl Worth <cworth@cworth.org>
22 #include "ttt-client.h"
23 #include "ttt-error.h"
24 #include "ttt-server.h"
25 #include "ttt-socket.h"
26 #include "ttt-token.h"
28 #define YY_DECL int yylex (yyscan_t yyscanner, ttt_token_t *token)
33 pthread_mutex_t mutex;
40 char **request_strings;
41 int num_request_strings;
44 ttt_bool_t registered;
48 typedef ttt_error_t (*ttt_command_func_t) (ttt_client_t *client,
53 _ttt_client_execute_helo (ttt_client_t *client,
58 _ttt_client_execute_who (ttt_client_t *client,
63 _ttt_client_execute_statistics (ttt_client_t *client,
68 _ttt_client_execute_message (ttt_client_t *client,
73 _ttt_client_execute_help (ttt_client_t *client,
78 _ttt_client_execute_version (ttt_client_t *client,
83 _ttt_client_execute_quit (ttt_client_t *client,
88 _ttt_client_execute_invite (ttt_client_t *client,
93 _ttt_client_execute_accept (ttt_client_t *client,
98 _ttt_client_execute_retract (ttt_client_t *client,
103 _ttt_client_execute_decline (ttt_client_t *client,
107 typedef struct _ttt_command_description {
111 ttt_command_func_t execute;
113 const char *description;
114 } ttt_command_description_t;
116 ttt_command_description_t command_descriptions[] = {
117 {"HELO", 1, 1, _ttt_client_execute_helo,
118 "HELO <username> ", "Register."},
120 {"HELP", 0, 1, _ttt_client_execute_help,
121 "HELP <command> ", "Display help for a command."},
123 {"INVITE", 1, 1, _ttt_client_execute_invite,
124 "INVITE <username> ", "Invite a player to play a game."},
126 {"ACCEPT", 1, 1, _ttt_client_execute_accept,
127 "ACCEPT <username> ", "Accept a game invitation."},
129 {"RETRACT", 1, 1, _ttt_client_execute_retract,
130 "RETRACT <username> ", "Retract a game invitation."},
132 {"DECLINE", 1, 1, _ttt_client_execute_decline,
133 "DECLINE <username> ", "Decline a game invitation."},
135 {"MESSAGE", 1, 1, _ttt_client_execute_message,
136 "MESSAGE <message> ", "Send a message to everyone."},
138 {"STATISTICS", 1, 1, _ttt_client_execute_statistics,
139 "STATISTICS <username> ", "Lists the statistics for the specified user."},
141 {"QUIT", 0, 0, _ttt_client_execute_quit,
142 "QUIT ", "Quit session."},
144 {"VERSION", 1, 1, _ttt_client_execute_version,
145 "VERSION <client-version>", "Negotiate protocol version."},
147 {"WHO", 0, 0, _ttt_client_execute_who,
148 "WHO ", "List registered users."}
151 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
154 _ttt_client_execute_helo (ttt_client_t *client,
162 assert (num_args == 1);
164 if (!(client->registered))
166 ttt_client_set_username (client, args[0]);
168 error = ttt_server_register_client (client->server, client);
171 client->registered = TRUE;
173 xasprintf (¬ice, "NOTICE USER %s\r\n",
174 ttt_client_get_username (client));
175 ttt_server_broadcast (client->server, notice);
180 xasprintf (&response, "HELO %s %s %s\r\n",
181 ttt_client_get_username (client),
182 ttt_server_get_host (client->server),
183 ttt_server_get_port (client->server));
184 ttt_client_send (client, response);
188 return TTT_ERROR_NONE;
192 _ttt_client_execute_who (ttt_client_t *client,
198 assert (num_args == 0);
200 if (!client->registered)
201 return TTT_ERROR_NO_NAME_SET;
203 response = xstrdup (ttt_server_who (client->server));
204 ttt_client_send (client, response);
208 return TTT_ERROR_NONE;
212 _ttt_client_execute_statistics (ttt_client_t *client,
216 const char *argusername;
217 ttt_client_t *argclient;
221 assert (num_args == 1);
223 argusername = args[0];
225 if (!client->registered)
226 return TTT_ERROR_NO_NAME_SET;
228 error = ttt_server_get_client_from_username (client->server,
234 xasprintf (&response, "STATISTICS %s \"\r\n"
235 "TICTACTOE WINS %d\r\n"
237 ttt_client_get_username(argclient),
238 ttt_client_get_num_wins(argclient));
240 ttt_client_send (client, response);
244 return TTT_ERROR_NONE;
248 _ttt_client_execute_message (ttt_client_t *client,
252 const char *argmessage;
256 assert (num_args == 1);
258 argmessage = args[0];
260 if (!client->registered)
261 return TTT_ERROR_NO_NAME_SET;
263 xasprintf (&response, "MESSAGE\r\n");
264 ttt_client_send (client, response);
266 xasprintf (¬ice, "NOTICE MESSAGE %s \"%s\"\r\n",
267 ttt_client_get_username(client),
269 ttt_server_broadcast (client->server, notice);
274 return TTT_ERROR_NONE;
278 _ttt_client_execute_help (ttt_client_t *client,
284 ttt_command_description_t *desc;
285 ttt_bool_t is_command = FALSE;
289 xasprintf (&response, "HELP \"\r\n"
290 "Available Commands:\r\n");
291 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
292 desc = &command_descriptions[i];
293 xasprintf (&response, "%s\r\n %s - %s\r\n",
298 xasprintf (&response, "%s\"\r\n", response);
300 argcommand = args[0];
301 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
302 desc = &command_descriptions[i];
303 if (strcasecmp (desc->command, argcommand) == 0) {
305 xasprintf (&response, "HELP %s \"\r\n"
314 /* XXX: Add detailed help. */
319 if ((num_args == 1) && (!is_command))
320 return TTT_ERROR_SYNTAX;
322 ttt_client_send (client, response);
325 return TTT_ERROR_NONE;
329 _ttt_client_execute_version (ttt_client_t *client,
338 assert (num_args == 1);
340 argversion = args[0];
342 /* Verify that provided version arg is a positive integer */
343 for (i = 0; i < strlen(argversion); i++)
344 if (!isdigit (argversion[i]))
345 return TTT_ERROR_SYNTAX;
347 version = atoi (argversion);
350 return TTT_ERROR_SYNTAX;
352 if (version > TTT_SERVER_PROTOCOL_VERSION)
353 version = TTT_SERVER_PROTOCOL_VERSION;
355 xasprintf (&response, "VERSION %d\r\n",
357 ttt_client_send (client, response);
360 return TTT_ERROR_NONE;
364 _ttt_client_execute_quit (ttt_client_t *client,
370 assert (num_args == 0);
372 if (!client->registered)
373 return TTT_ERROR_QUIT_REQUESTED;
375 xasprintf (¬ice, "NOTICE QUIT %s\r\n",
376 ttt_client_get_username(client));
377 ttt_server_broadcast (client->server, notice);
381 return TTT_ERROR_QUIT_REQUESTED;
385 _ttt_client_execute_invite (ttt_client_t *client,
389 const char *invitee_username;
390 ttt_client_t *invitee;
395 assert (num_args == 1);
397 invitee_username = args[0];
399 if (!client->registered)
400 return TTT_ERROR_NO_NAME_SET;
402 error = ttt_server_get_client_from_username (client->server,
408 ttt_server_add_invite (client->server,
412 xasprintf (&response, "INVITE\r\n");
413 ttt_client_send (client, response);
415 xasprintf (¬ice, "NOTICE INVITE %s %s\r\n",
416 ttt_client_get_username(client),
417 ttt_client_get_username(invitee));
418 ttt_server_broadcast (client->server, notice);
423 return TTT_ERROR_NONE;
427 _ttt_client_execute_accept (ttt_client_t *client,
431 const char *actor_username;
437 assert (num_args == 1);
439 actor_username = args[0];
441 if (!client->registered)
442 return TTT_ERROR_NO_NAME_SET;
444 error = ttt_server_get_client_from_username (client->server,
450 error = ttt_server_remove_invite (client->server,
456 xasprintf (&response, "ACCEPT\r\n");
457 ttt_client_send (client, response);
459 xasprintf (¬ice, "NOTICE ACCEPT %s %s\r\n",
460 ttt_client_get_username(client),
461 ttt_client_get_username(actor));
462 ttt_server_broadcast (client->server, notice);
464 /* XXX: Start a new game */
469 return TTT_ERROR_NONE;
473 _ttt_client_execute_retract (ttt_client_t *client,
477 const char *invitee_username;
478 ttt_client_t *invitee;
483 assert (num_args == 1);
485 invitee_username = args[0];
487 if (!client->registered)
488 return TTT_ERROR_NO_NAME_SET;
490 error = ttt_server_get_client_from_username (client->server,
496 error = ttt_server_remove_invite (client->server,
502 xasprintf (&response, "RETRACT\r\n");
503 ttt_client_send (client, response);
505 xasprintf (¬ice, "NOTICE RETRACT %s %s\r\n",
506 ttt_client_get_username(client),
507 ttt_client_get_username(invitee));
508 ttt_server_broadcast (client->server, notice);
513 return TTT_ERROR_NONE;
517 _ttt_client_execute_decline (ttt_client_t *client,
521 const char *actor_username;
527 assert (num_args == 1);
529 actor_username = args[0];
531 if (!client->registered)
532 return TTT_ERROR_NO_NAME_SET;
534 error = ttt_server_get_client_from_username (client->server,
540 error = ttt_server_remove_invite (client->server,
546 xasprintf (&response, "DECLINE\r\n");
547 ttt_client_send (client, response);
549 xasprintf (¬ice, "NOTICE DECLINE %s %s\r\n",
550 ttt_client_get_username(client),
551 ttt_client_get_username(actor));
552 ttt_server_broadcast (client->server, notice);
557 return TTT_ERROR_NONE;
561 _free_request (ttt_client_t *client);
564 _ttt_client_init (ttt_client_t *client,
565 ttt_server_t *server,
570 pthread_mutex_init (&client->mutex, NULL);
572 client->server = server;
573 client->socket = socket;
575 file = xfdopen (socket, "r");
576 yylex_init (&client->scanner);
577 yyset_in (file, client->scanner);
579 client->request_strings = NULL;
580 client->num_request_strings = 0;
582 client->username = NULL;
583 client->registered = FALSE;
584 client->num_wins = 0;
588 _ttt_client_fini (ttt_client_t *client)
590 pthread_mutex_lock (&client->mutex);
592 if (client->registered)
593 ttt_server_unregister_client (client->server, client);
595 free (client->username);
596 client->username = NULL;
598 yylex_destroy (client->scanner);
599 shutdown (client->socket, SHUT_RDWR);
601 _free_request (client);
603 pthread_mutex_unlock (&client->mutex);
605 pthread_mutex_destroy (&client->mutex);
608 /* XXX: The memory management for the request strings is pretty cheesy. */
610 _append_to_request (ttt_client_t *client,
613 client->num_request_strings++;
614 client->request_strings =
615 xrealloc (client->request_strings,
616 client->num_request_strings * sizeof (char *));
618 client->request_strings[client->num_request_strings - 1] = xstrdup (string);
622 _free_request (ttt_client_t *client)
626 for (i = 0; i < client->num_request_strings; i++)
627 free (client->request_strings[i]);
629 free (client->request_strings);
631 client->request_strings = NULL;
632 client->num_request_strings = 0;
636 _read_request (ttt_client_t *client)
639 ttt_token_type_t token_type;
641 _free_request (client);
644 token_type = yylex (client->scanner, &token);
645 /* Yes, EOF in two different enums is pretty ugly. */
646 if (token_type == TTT_TOKEN_TYPE_EOF)
647 return TTT_STATUS_EOF;
649 if (token_type == TTT_TOKEN_TYPE_NEWLINE) {
650 if (client->num_request_strings)
651 return TTT_STATUS_SUCCESS;
656 assert (token_type == TTT_TOKEN_TYPE_STRING);
658 _append_to_request (client, token.u.string);
660 free (token.u.string);
665 _execute_request (ttt_client_t *client)
667 char *command = client->request_strings[0];
668 int num_args = client->num_request_strings - 1;
669 ttt_command_description_t *desc;
672 for (i = 0; i < strlen (command); i++)
673 command[i] = toupper (command[i]);
675 for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
676 desc = &command_descriptions[i];
677 if (strcmp (command, desc->command) == 0) {
678 if ((num_args < desc->args_min) || (num_args > desc->args_max))
679 return TTT_ERROR_SYNTAX;
680 return (desc->execute) (client,
681 &client->request_strings[1],
686 return TTT_ERROR_COMMAND;
690 _handle_requests_thread (void *closure)
693 ttt_client_t *client = closure;
697 status = _read_request (client);
698 if (status == TTT_STATUS_EOF)
703 error = _execute_request (client);
704 if (error == TTT_ERROR_QUIT_REQUESTED)
707 ttt_client_send (client, ttt_error_string (error));
710 _ttt_client_fini (client);
716 /* Exported: See ttt-client.h for documentation. */
718 ttt_client_new (void *closure, int client_socket)
720 ttt_server_t *server = closure;
721 ttt_client_t *client;
724 client = xmalloc (sizeof (ttt_client_t));
726 _ttt_client_init (client, server, client_socket);
728 err = pthread_create (&client->thread, NULL,
729 _handle_requests_thread, client);
731 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\r\n",
737 /* Exported: See ttt-client.h for documentation. */
739 ttt_client_send (ttt_client_t *client, const char *message)
741 pthread_mutex_lock (&client->mutex);
743 ttt_socket_write (client->socket, message, strlen (message));
745 pthread_mutex_unlock (&client->mutex);
748 /* Exported: See ttt-client.h for documentation. */
750 ttt_client_get_username (ttt_client_t *client)
752 return client->username;
755 /* Exported: See ttt-client.h for documentation. */
757 ttt_client_set_username (ttt_client_t *client, const char *username)
759 free (client->username);
760 client->username = xstrdup (username);
763 /* Exported: See ttt-client.h for documentation. */
765 ttt_client_get_num_wins (ttt_client_t *client)
767 return client->num_wins;
770 /* This is just to keep the compiler quiet about a function declared
771 * static but never defined. Just an annoying bug in flex's output. */
772 static int yy_init_globals (yyscan_t yyscanner) {return 0;}
773 void use_yy_init_globals (void);
774 void use_yy_init_globals (void) {yyscan_t scan; yy_init_globals(scan);}