+/* Exported: See ttt-server.h for documentation. */
+void
+ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client)
+{
+ ttt_invite_t *invite;
+ char *notice;
+ ttt_bool_t send_notice = FALSE;
+ int i;
+
+ pthread_mutex_lock (&server->mutex);
+
+ /* Auto-retract and decline pending notices */
+ /* Notices are sent after mutex unlock */
+ for (i = 0; i < server->num_invites; i++)
+ {
+ invite = server->invites[i];
+ if ((invite->actor == client) || (invite->invitee == client))
+ {
+ send_notice = TRUE;
+ if (invite->actor == client)
+ xasprintf (¬ice, "%s" "NOTICE RETRACT %s %s\r\n",
+ notice,
+ ttt_client_get_username(invite->actor),
+ ttt_client_get_username(invite->invitee));
+ else
+ xasprintf (¬ice, "%s" "NOTICE DECLINE %s %s\r\n",
+ notice,
+ ttt_client_get_username(invite->invitee),
+ ttt_client_get_username(invite->actor));
+
+ memmove (&server->invites[i], &server->invites[i+1],
+ (server->num_invites - i - 1) * sizeof (ttt_invite_t *));
+ server->num_invites--;
+ i--;
+ }
+ }
+
+ for (i = 0; i < server->num_clients; i++)
+ if (server->clients[i] == client)
+ break;
+
+ assert (i < server->num_clients);
+
+ fprintf (stderr, "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);
+
+ if (send_notice)
+ {
+ ttt_server_broadcast(server, notice);
+ free (notice);
+ }
+}
+
+/* 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_verify_username (ttt_server_t *server,
+ const char *username)