+/* 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)
+{
+ ttt_client_t *client;
+
+ return ttt_server_get_client_from_username (server,
+ username,
+ &client);
+}
+
+/* Exported: See ttt-server.h for documentation. */
+ttt_error_t
+ttt_server_get_client_from_username (ttt_server_t *server,
+ const char *username,
+ ttt_client_t **client)
+{
+ ttt_bool_t usernamefound = FALSE;
+ const 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;
+ *client = server->clients[i];
+ }
+ }
+
+ 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_add_invite (ttt_server_t *server,
+ ttt_client_t *actor,
+ ttt_client_t *invitee)
+{
+ ttt_invite_t *invite;
+
+ pthread_mutex_lock (&server->mutex);
+
+ invite = xmalloc (sizeof (ttt_invite_t));
+
+ invite->actor = actor;
+ invite->invitee = invitee;
+
+ server->num_invites++;
+
+ if (server->num_invites > server->invites_size) {
+ if (server->invites_size == 0)
+ server->invites_size = 1;
+ else
+ server->invites_size *= 2;
+
+ server->invites = xrealloc (server->invites,
+ server->invites_size * sizeof (ttt_invite_t *));
+ }
+
+ server->invites[server->num_invites - 1] = invite;
+
+ pthread_mutex_unlock (&server->mutex);
+
+ return TTT_ERROR_NONE;
+}
+
+/* Exported: See ttt-server.h for documentation. */
+ttt_error_t
+ttt_server_remove_invite (ttt_server_t *server,
+ ttt_client_t *actor,
+ ttt_client_t *invitee)
+{
+ ttt_invite_t *invite;
+ ttt_error_t error;
+ int i;
+
+ pthread_mutex_lock (&server->mutex);
+
+ error = TTT_ERROR_NO_INVITE;
+ for (i = 0; i < server->num_invites; i++)
+ {
+ invite = server->invites[i];
+ if ((invite->actor == actor) && (invite->invitee == invitee))
+ {
+ error = TTT_ERROR_NONE;
+ break;
+ }
+ }
+
+ if (error)
+ goto CLEANUP_LOCK;
+
+ assert (i < server->num_invites);
+
+ memmove (&server->invites[i], &server->invites[i+1],
+ (server->num_invites - i - 1) * sizeof (ttt_invite_t *));
+
+ server->num_invites--;
+
+ CLEANUP_LOCK:
+ pthread_mutex_unlock (&server->mutex);
+
+ return error;
+}
+