- port_short = port_long;
- addr->sin_port = htons (port_short);
- } else {
- /* Named port */
- servent = getservbyname (port, "tcp");
- if (port == NULL) {
- fprintf (stderr, "Error: Lookup failed for port %s: %s\n",
- port, hstrerror (h_errno));
- return TTT_STATUS_FAILURE;
+ }
+
+ 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];