+ttt_server_init (ttt_server_t *server, const char *host, const char *port)
+{
+ pthread_mutex_init (&server->mutex, NULL);
+
+ server->host = host;
+ server->port = port;
+
+ server->clients = NULL;
+ server->clients_size = 0;
+ server->num_clients = 0;
+
+ server->invites = NULL;
+ server->invites_size = 0;
+ server->num_invites = 0;
+}
+
+/* Exported: See ttt-server.h for documentation. */
+ttt_error_t
+ttt_server_register_client (ttt_server_t *server, ttt_client_t *client)
+{
+ int i;
+ ttt_error_t error = TTT_ERROR_NONE;
+ const char *username;
+
+ pthread_mutex_lock (&server->mutex);
+
+ username = ttt_client_get_username (client);
+
+ assert (username != NULL);
+
+ if (username[0] == '\0')
+ return TTT_ERROR_INVALID_NAME;
+
+ for (i = 0; i < server->num_clients; i++) {
+ if (strcasecmp (ttt_client_get_username (server->clients[i]), username) == 0) {
+ error = TTT_ERROR_INVALID_NAME;
+ goto CLEANUP_LOCK;
+ }
+ }
+
+ fprintf (stderr, "Client %s has joined.\r\n", username);
+
+ server->num_clients++;
+
+ if (server->num_clients > server->clients_size) {
+ if (server->clients_size == 0)
+ server->clients_size = 1;
+ else
+ server->clients_size *= 2;
+
+ server->clients = xrealloc (server->clients,
+ server->clients_size * sizeof (ttt_client_t *));
+ }
+
+ server->clients[server->num_clients - 1] = client;
+
+ CLEANUP_LOCK:
+ pthread_mutex_unlock (&server->mutex);
+
+ return error;
+}
+
+/* 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)
+{
+ 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)