]> git.cworth.org Git - ttt/blobdiff - src/ttt-server.c
Deleted debugging code and changed return errors to fit protocol
[ttt] / src / ttt-server.c
index 450582b7667dae57fa374abc2d126dbfb76e4bf1..4b29cf2f774d277dc14dd5bc675f4b5a9a25df6d 100644 (file)
 #include "ttt-error.h"
 #include "ttt-socket.h"
 
+struct _ttt_invite {
+    ttt_client_t    *actor;
+    ttt_client_t    *invitee;
+};
+
 struct _ttt_server {
     pthread_mutex_t mutex;
 
-    const char *host;
-    const char *port;
+    const char      *host;
+    const char      *port;
 
-    ttt_client_t **clients;
-    int clients_size;
-    int num_clients;
+    ttt_client_t    **clients;
+    int             clients_size;
+    int             num_clients;
+
+    ttt_invite_t    **invites;
+    int             invites_size;
+    int             num_invites;
 };
 
 static void
@@ -48,6 +57,10 @@ ttt_server_init (ttt_server_t *server, const char *host, const char *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. */
@@ -56,25 +69,25 @@ ttt_server_register_client (ttt_server_t *server, ttt_client_t *client)
 {
     int i;
     ttt_error_t error = TTT_ERROR_NONE;
-    const char *name;
+    const char *username;
 
     pthread_mutex_lock (&server->mutex);
 
-    name = ttt_client_get_name (client);
+    username = ttt_client_get_username (client);
 
-    assert (name != NULL);
+    assert (username != NULL);
 
-    if (name[0] == '\0')
+    if (username[0] == '\0')
        return TTT_ERROR_INVALID_NAME;
 
     for (i = 0; i < server->num_clients; i++) {
-       if (strcmp (ttt_client_get_name (server->clients[i]), name) == 0) {
+       if (strcasecmp (ttt_client_get_username (server->clients[i]), username) == 0) {
            error = TTT_ERROR_INVALID_NAME;
            goto CLEANUP_LOCK;
        }
     }
 
-    printf ("Client %s has joined.\n", name);
+    fprintf (stderr, "Client %s has joined.\r\n", username);
 
     server->num_clients++;
 
@@ -88,7 +101,7 @@ ttt_server_register_client (ttt_server_t *server, ttt_client_t *client)
                                    server->clients_size * sizeof (ttt_client_t *));
     }
 
-    server->clients [server->num_clients - 1] = client;
+    server->clients[server->num_clients - 1] = client;
 
  CLEANUP_LOCK:
     pthread_mutex_unlock (&server->mutex);
@@ -100,17 +113,46 @@ ttt_server_register_client (ttt_server_t *server, ttt_client_t *client)
 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 (&notice, "%s" "NOTICE RETRACT %s %s\r\n",
+                          notice,
+                          ttt_client_get_username(invite->actor),
+                          ttt_client_get_username(invite->invitee));
+           else
+               xasprintf (&notice, "%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);
 
-    printf ("Client %s has left.\n", ttt_client_get_name (client));
+    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 *));
@@ -118,8 +160,15 @@ ttt_server_unregister_client (ttt_server_t *server, ttt_client_t *client)
     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)
 {
@@ -133,6 +182,143 @@ ttt_server_broadcast (ttt_server_t *server, const char *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)
+{
+    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;
+}
+
 /* Exported: See ttt-server.h for documentation. */
 const char*
 ttt_server_get_host (ttt_server_t *server)
@@ -148,26 +334,18 @@ ttt_server_get_port (ttt_server_t *server)
 }
 
 static const char *WELCOME_MESSAGE = 
-"Welcome to ttt-server. So far, this program is still a demonstration\n"
-"TCP/IP server, acting something like a rather braindead chat server.\n"
-"The server is currently listening on:\n"
-"\n    %s:%s\n"
-"\nTo test this, simply connect one or more clients to that host and port.\n"
-"For example:\n"
-"\n    telnet %s %s\n"
-"\nOnce you have connected a client, the server will send each line of text\n"
-"it receives to all connected clients. The server reports client joins and\n"
-"departures on stdout.\n"
-"\nNote that to terminate the telnet client you type Control-], then\n"
-"<Enter>, then \"close\" (and <Enter>) at the \"telnet> \" prompt.\n"
-"\nHave fun!\n"
-"-Carl\n"
-"\nPS. At this point we're ready to leave the demonstration phase and to\n"
-"begin implementing TTTP (tic-tac-toe protocol) as well as fixing the\n"
-"protocol specifcation. We don't need a custom client to move forward on\n"
-"the server (that is one of the ideas behind using a telnet-compatible\n"
-"protocol), but a custom client would still be a fine project for a\n"
-"motivated beginning programmer.\n\n";
+"Welcome to ttt-server. The server is currently listening on:\r\n"
+"\r\n"
+"      %s:%s\r\n"
+"\r\n"
+"To test this, simply connect one or more clients to that host and port.\r\n"
+"For example:\r\n"
+"\r\n"
+"      telnet %s %s\r\n"
+"\r\n"
+"The TTTP (tic-tac-toe protocol) has been partially implemented.\r\n"
+"The following commands should work: HELO, HELP, INVITE, ACCEPT, RETRACT, DECLINE, MESSAGE, STATISTICS, QUIT, VERSION, WHO.\r\n"
+"\r\n";
 
 static void
 _ttt_server_accept (void *closure, int client_socket)
@@ -177,6 +355,35 @@ _ttt_server_accept (void *closure, int client_socket)
     ttt_client_new (server, client_socket);
 }
 
+static void
+_detach_and_write_child_pid_to (const char *filename)
+{
+    pid_t pid;
+
+    /* Use the Unix double-fork trick to detach completely. See
+     * setsid(2) for some details as to why two forks are
+     * needed. */
+    pid = xfork ();
+    if (pid) {
+       /* First parent just exits */
+       exit (0);
+    }
+    
+    chdir ("/");
+    setsid ();
+    
+    pid = xfork ();
+    if (pid) {
+       /* Second parent exits after writing pid */
+       FILE *file = xfopen (filename, "w");
+       fprintf (file, "%d\n", pid);
+       fclose (file);
+       exit (0);
+    }
+    
+    /* Final, detached child returns. */
+}
+
 int 
 main (int argc, char **argv)
 {
@@ -186,12 +393,57 @@ main (int argc, char **argv)
 
     ttt_args_parse (&args, argc, argv);
 
-    if (args.log_file)
-       xfreopen (args.log_file, "a", stderr);
+    if (args.log_file || args.detach) {
+       FILE *log_file;
+       /* In the detach case, we force redirection to a log file. */
+       if (args.log_file == NULL)
+           args.log_file = "/var/log/ttt-server.log";
+       log_file = fopen (args.log_file, "a");
+       if (log_file == NULL) {
+           printf ("Warning: Failed to open log file %s: %s.\n",
+                   args.log_file, strerror (errno));
+           printf ("Logging will be disabled.\n");
+           xdup2 (1, 2);
+       } else {
+           xdup2 (fileno (log_file), 2);
+       }
+    }
+
+    if (args.detach)
+       _detach_and_write_child_pid_to (args.pid_file);
+
+    /* Now that we've setup logging and the pid file, drop any special
+     * permissions we might have if we were asked to do that. */
+    if (args.user) {
+       int ret;
+       struct passwd *pwd;
+       errno = 0;
+       pwd = getpwnam (args.user);
+       if (pwd == NULL) {
+           fprintf (stderr, "Error: Failed to lookup uid for %s: %s. Aborting.\n",
+                    args.user,
+                    errno == 0 ? "User not found" : strerror (errno));
+           exit (1);
+       }
+       ret = setuid (pwd->pw_uid);
+       if (ret == -1) {
+           fprintf (stderr, "Error: Failed to setuid to %d (%s): %s. Aborting.\n",
+                    pwd->pw_uid, args.user, strerror (errno));
+           exit (1);
+       }
+    }
 
     socket = ttt_socket_create_server (args.host, args.port);
 
-    printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
+    if (args.detach) {
+       printf ("Server started listening on %s:%s\n", args.host, args.port);
+       fprintf (stderr, "Server started listening on %s:%s\n", args.host, args.port);
+    } else {
+       printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
+    }
+
+    fclose (stdout);
+    fclose (stdin);
 
     ttt_server_init (&server, args.host, args.port);