+ /* XXX: Verify invitation, else return ERROR NO_INVITE */
+
+ 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 ttt_error_t
+_ttt_client_execute_retract (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;
+
+ /* XXX: Verify invitation, else return ERROR NO_INVITE */
+
+ xasprintf (&response, "RETRACT\r\n");
+ ttt_client_send (client, response);
+
+ xasprintf (¬ice, "NOTICE RETRACT %s %s\r\n",
+ client->username,
+ username);
+ ttt_server_broadcast (client->server, notice);
+
+ /* XXX: Remove invitiation from state */
+
+ free (notice);
+ free (response);
+
+ return TTT_ERROR_NONE;
+}
+
+static ttt_error_t
+_ttt_client_execute_decline (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;
+
+ /* XXX: Verify invitation, else return ERROR NO_INVITE */
+
+ xasprintf (&response, "DECLINE\r\n");
+ ttt_client_send (client, response);
+
+ xasprintf (¬ice, "NOTICE DECLINE %s %s\r\n",
+ client->username,
+ username);
+ ttt_server_broadcast (client->server, notice);
+
+ /* XXX: Remove invitation from state */
+
+ free (notice);
+ free (response);
+
+ return TTT_ERROR_NONE;
+}
+
+static void
+_free_request (ttt_client_t *client);
+
+static void
+_ttt_client_init (ttt_client_t *client,
+ ttt_server_t *server,
+ int socket)
+{
+ FILE *file;
+
+ pthread_mutex_init (&client->mutex, NULL);
+
+ client->server = server;
+ client->socket = socket;
+
+ file = xfdopen (socket, "r");
+ yylex_init (&client->scanner);
+ yyset_in (file, client->scanner);
+
+ client->request_strings = NULL;
+ client->num_request_strings = 0;
+
+ client->username = NULL;
+ client->registered = FALSE;
+ client->num_wins = 0;
+}
+
+static void
+_ttt_client_fini (ttt_client_t *client)
+{
+ pthread_mutex_lock (&client->mutex);
+
+ if (client->registered)
+ ttt_server_unregister_client (client->server, client);
+
+ free (client->username);
+ client->username = NULL;
+
+ yylex_destroy (client->scanner);
+ shutdown (client->socket, SHUT_RDWR);
+
+ _free_request (client);
+
+ pthread_mutex_unlock (&client->mutex);
+
+ pthread_mutex_destroy (&client->mutex);
+}
+
+/* XXX: The memory management for the request strings is pretty cheesy. */
+static void
+_append_to_request (ttt_client_t *client,
+ const char *string)
+{
+ client->num_request_strings++;
+ client->request_strings =
+ xrealloc (client->request_strings,
+ client->num_request_strings * sizeof (char *));
+
+ client->request_strings[client->num_request_strings - 1] = xstrdup (string);
+}
+
+static void
+_free_request (ttt_client_t *client)
+{
+ int i;
+
+ for (i = 0; i < client->num_request_strings; i++)
+ free (client->request_strings[i]);
+
+ free (client->request_strings);
+
+ client->request_strings = NULL;
+ client->num_request_strings = 0;
+}
+
+static ttt_status_t
+_read_request (ttt_client_t *client)
+{
+ ttt_token_t token;
+ ttt_token_type_t token_type;
+
+ _free_request (client);
+
+ while (1) {
+ token_type = yylex (client->scanner, &token);
+ /* Yes, EOF in two different enums is pretty ugly. */
+ if (token_type == TTT_TOKEN_TYPE_EOF)
+ return TTT_STATUS_EOF;
+
+ if (token_type == TTT_TOKEN_TYPE_NEWLINE) {
+ if (client->num_request_strings)
+ return TTT_STATUS_SUCCESS;
+ else
+ continue;
+ }
+
+ assert (token_type == TTT_TOKEN_TYPE_STRING);
+
+ _append_to_request (client, token.u.string);
+
+ free (token.u.string);
+ }
+}
+
+static ttt_error_t
+_execute_request (ttt_client_t *client)
+{
+ int i;
+
+ char *command = client->request_strings[0];
+ int num_args = client->num_request_strings-1;
+ ttt_command_description_t *desc;
+
+ for (i = 0; i < strlen (command); i++)
+ command[i] = toupper (command[i]);
+
+ for (i = 0; i < ARRAY_SIZE(command_descriptions); i++) {
+ desc = &command_descriptions[i];
+ if (strcmp (command, desc->command) == 0) {
+ if ((num_args < desc->args_min) || (num_args > desc->args_max))
+ return TTT_ERROR_SYNTAX;
+ return (desc->execute) (client,
+ &client->request_strings[1],
+ num_args);
+ }
+ }
+
+ return TTT_ERROR_COMMAND;
+}
+
+static void *
+_handle_requests_thread (void *closure)
+{
+ ttt_status_t status;
+ ttt_error_t error;
+ ttt_client_t *client = closure;
+
+ while (1) {
+
+ status = _read_request (client);
+ if (status == TTT_STATUS_EOF)
+ break;
+ if (status)
+ ASSERT_NOT_REACHED;
+
+ error = _execute_request (client);
+ if (error == TTT_ERROR_QUIT_REQUESTED)
+ break;
+ if (error)
+ ttt_client_send (client, ttt_error_string (error));
+ }
+
+ _ttt_client_fini (client);
+ free (client);
+
+ return (void *) 0;
+}
+
+/* Exported: See ttt-client.h for documentation. */
+void
+ttt_client_new (void *closure, int client_socket)
+{
+ ttt_server_t *server = closure;
+ ttt_client_t *client;
+ int err;
+
+ client = xmalloc (sizeof (ttt_client_t));
+
+ _ttt_client_init (client, server, client_socket);
+
+ err = pthread_create (&client->thread, NULL,
+ _handle_requests_thread, client);
+ if (err != 0) {
+ fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\r\n",
+ strerror (err));
+ exit (1);
+ }
+}
+
+/* Exported: See ttt-client.h for documentation. */
+void
+ttt_client_send (ttt_client_t *client, const char *message)
+{
+ pthread_mutex_lock (&client->mutex);
+
+ ttt_socket_write (client->socket, message, strlen (message));
+
+ pthread_mutex_unlock (&client->mutex);
+}
+
+/* Exported: See ttt-client.h for documentation. */
+const char*
+ttt_client_get_username (ttt_client_t *client)
+{
+ return client->username;
+}
+
+/* Exported: See ttt-client.h for documentation. */
+void
+ttt_client_set_username (ttt_client_t *client, const char *username)
+{
+ free (client->username);
+ client->username = xstrdup (username);
+}
+
+/* Exported: See ttt-client.h for documentation. */
+int
+ttt_client_get_num_wins (ttt_client_t *client)
+{
+ return client->num_wins;