+
+ assert (i < server->num_clients);
+
+ printf ("Client %s has left.\r\n", ttt_client_get_username (client));
+
+ memmove (&server->clients[i], &server->clients[i+1],
+ (server->num_clients - i - 1) * sizeof (ttt_client_t *));
+
+ server->num_clients--;
+
+ pthread_mutex_unlock (&server->mutex);
+}
+
+/* Exported: See ttt-server.h for documentation. */
+void
+ttt_server_broadcast (ttt_server_t *server, const char *message)
+{
+ int i;
+
+ pthread_mutex_lock (&server->mutex);
+
+ for (i = 0; i < server->num_clients; i++)
+ ttt_client_send (server->clients[i], message);
+
+ pthread_mutex_unlock (&server->mutex);
+}
+
+/* Exported: See ttt-server.h for documentation. */
+const char*
+ttt_server_who (ttt_server_t *server)
+{
+ int i;
+ char *response;
+
+ pthread_mutex_lock (&server->mutex);
+
+ xasprintf (&response, "WHO");
+
+ for (i = 0; i < server->num_clients; i++)
+ xasprintf (&response, "%s %s",
+ response,
+ ttt_client_get_username (server->clients[i]));
+
+ xasprintf (&response, "%s\r\n", response);
+
+ pthread_mutex_unlock (&server->mutex);
+
+ return response;
+}
+
+/* Exported: See ttt-server.h for documentation. */
+ttt_error_t
+ttt_server_statistics (ttt_server_t *server, const char *username, char **response)
+{
+ ttt_bool_t usernamefound = FALSE;
+ char *client_username;
+ int client_num_wins;
+ 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;
+ client_num_wins = ttt_client_get_num_wins (server->clients[i]);
+ xasprintf (response, "STATISTICS %s \"\r\n"
+ "TICTACTOE WINS %d\r\n\"\r\n",
+ client_username,
+ client_num_wins);
+ }
+ }
+
+ pthread_mutex_unlock (&server->mutex);
+
+ if (!usernamefound)
+ return TTT_ERROR_NO_USER;
+
+ 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;