]> git.cworth.org Git - ttt/blobdiff - src/ttt-curses-client.c
Deleted debugging code and changed return errors to fit protocol
[ttt] / src / ttt-curses-client.c
index 47e8370678ea6edd66d5d9bdfd8b8ddc0925d98f..25d724acf428a8680b9ad0af55629474ced6a076 100644 (file)
@@ -1,4 +1,4 @@
-/* ttt-curses-client.c - curses based tic-tac-toe game client
+/*ttt-curses-client.c - curses based tic-tac-toe game client
  *
  * Copyright © 2005 Bryan Worth
  *
 
 /* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code
  *      to handle window resizing
- *      
- *      add code to read username from config file or prompt user if config
- *      file does not exist. (username is currently hardcoded and needs
- *      changed before compiling)
  *
  */     
 
@@ -102,54 +98,66 @@ void wprint (WINDOW *window,
 
     while (xx < l) {
        outc = string[xx];
-       if (string[xx] != '\r')
+       if (string[xx] != '\r') 
            waddch (window, outc);
        xx++;
     }
 }
 
-char *message2;
-int _socket;
-size_t maxread;
+FILE *sockin, *sockout;
 
 int
 main (int argc, char **argv)
 {
-    char *message;
-    char buffer[1000];
-    char *host;
-    char *port;
+    char *host="localhost";
+    char *port="5334";
     chtype c;
-    ssize_t numread;
+    char *username;
+    char *envuser;
+    char *confpath;
+    char *conffile;
+    char *command_string;
+    int _socket;
+    char buffer[BUFSIZ];
+    char inplin[1024];
+    ttt_status_t status;
 
     static WINDOW *mainwnd;
     static WINDOW *dispwin;
+    static WINDOW *statwin;
     static WINDOW *inpwin;
+    FILE *conf_file;
 
     mainwnd = initscr();
     int dlines = LINES - 2, cols = COLS;
 
     (void) nonl ();
+    (void) nl ();
     noecho ();
     cbreak ();
     nodelay (mainwnd, TRUE);
     curs_set (0);
     refresh ();
-    dispwin = newwin (dlines - 1, cols - 2, 0, 0);
-    inpwin = newwin (1, cols - 2, dlines, 0);
+    dispwin = newwin (dlines - 5, cols - 2, 0, 0);
+    statwin = newwin (4, cols - 2, dlines -3, 0);
+    inpwin = newwin (2, cols - 2, dlines, 0);
     keypad (mainwnd, TRUE);    // enable keyboard mapping 
     keypad (inpwin, TRUE);     // enable keyboard mapping 
     nodelay (inpwin, TRUE);
     wrefresh (mainwnd);
     wrefresh (dispwin);
+    wrefresh (statwin);
     wrefresh (inpwin);
     (void) scrollok (mainwnd, TRUE);
     (void) scrollok (dispwin, TRUE);
+    (void) scrollok (statwin, TRUE);
     (void) scrollok (inpwin, TRUE);
     (void) idlok (mainwnd, TRUE);
     (void) idlok (dispwin, TRUE);
+    (void) idlok (statwin, TRUE);
     (void) idlok (inpwin, TRUE);
     wsetscrreg (dispwin, 0, dlines - 2);
+    wsetscrreg (statwin, 0, 4);
     
     (void) signal (SIGINT, finish);    /* arrange interrupts to terminate */
     if (has_colors ()) {
@@ -164,58 +172,133 @@ main (int argc, char **argv)
        init_pair (COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
        init_pair (COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
     }
-    wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
-    wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
+    //wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
+    //wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
     wrefresh (dispwin);
     wrefresh (inpwin);
 
-    xasprintf (&host, "localhost");
-    xasprintf (&port, "5334");
-    xasprintf (&message, "HELO bryan\r\n");
+    xasprintf(&confpath,"%s/.ttt/",getenv("HOME"));
+    xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME"));
+    username="user";
+    if (access (conffile, F_OK) != 0 ) {
+       envuser=getenv("USER");
+       if (envuser != NULL) username=strdup(envuser);
+       if (access (confpath, F_OK) != 0 ) {
+           xasprintf(&command_string,"mkdir %s",confpath);
+           system (command_string);
+       }
+       if ((conf_file = fopen(conffile,"w")) != NULL) {
+               fprintf(conf_file,"username=%s",username);
+               fclose(conf_file);
+       }
+    }
+    if ((conf_file = fopen(conffile,"r")) != NULL) {
+        while (fgets(buffer,BUFSIZ,conf_file)) {
+           if (buffer[strlen(buffer)-1] == 10) buffer[strlen(buffer)-1] = '\0';
+           if (strncmp(buffer,"username=",9) == 0) {
+               xasprintf (&username, "%s", &buffer[9]);
+           }
+           if (strncmp(buffer,"port=",5) == 0) {
+               xasprintf (&port, "%s",&buffer[5]);
+           }
+       }
+    fclose(conf_file); 
+    }
+    status=ttt_socket_create_client (host, port, &_socket);
+    if (status) {
+       endwin();
+       printf("Unable to connect to server! Connection status: %d\n",status);
+       exit(1);
+    }
+    sockin=fdopen(_socket,"r");
+    sockout=fdopen(_socket,"w");
+
 
-    ttt_socket_create_client (host, port, &_socket);
-    ttt_socket_write (_socket, message, strlen (message));
 
-    maxread = 100;
-    numread = read (_socket, buffer, maxread);
-    if (numread > 0) {
-       buffer[numread] = '\0';
-       xasprintf (&message, "%s", buffer);
-       wprint (dispwin, message);
+    fprintf(sockout, "HELO %s\r\n",username);
+    fflush(sockout);
+    
+    if (fgets(buffer,BUFSIZ,sockin)) {
+       if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
+               wprint (dispwin, buffer);
+       }
+       else {
+           if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
+       }
        wrefresh (dispwin);
+       wrefresh (statwin);
     }
-    xasprintf (&message2, "MESSAGE \"");
+    
+    fprintf(sockout, "WHO \r\n");
+    fflush(sockout);
+    
+    inplin[0]='\0';
     while (1) {
        curs_set (1);
        c = wgetch (inpwin);
-       if (c != ERR) {
-           xasprintf (&message2, "%s%c", message2, (int) c);
-           waddch (inpwin, c);
-           wrefresh (inpwin);
+       if ((c != ERR) && (strlen(inplin)< 1000)) {
+           switch ((int) c) {
+               case 13: 
+                   break;
+               case 10:
+                   break;
+               case 8:
+                   inplin[strlen(inplin)-1]='\0';
+                   wmove (inpwin,0,strlen(inplin));
+                   wclrtoeol( inpwin);
+                   wrefresh (inpwin);
+                   break;
+               case 127:
+                   inplin[strlen(inplin)-1]='\0';
+                   wmove (inpwin,0,strlen(inplin));
+                   wclrtoeol (inpwin);
+                   wrefresh (inpwin);
+                   break;
+               case 263:
+                   inplin[strlen(inplin)-1]='\0';
+                   wmove (inpwin,0,strlen(inplin));
+                   wclrtoeol (inpwin);
+                   wrefresh (inpwin);
+                   break;
+               default: 
+                   sprintf (inplin,"%s%c",inplin,(int) c);
+                   waddch (inpwin, c);
+                   wrefresh (inpwin);
+           }
        }
 
-       numread = 0;
-       maxread = 100;
-       numread = read (_socket, buffer, maxread);
-       if (numread > 0) {
+       if (fgets(buffer,BUFSIZ,sockin)) {
            curs_set (0);
-           buffer[numread] = '\0';
-           wprint (dispwin, buffer);
+           if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
+               wprint (dispwin, &buffer[15]);
+           }
+           else {
+               if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
+           }
            wrefresh (dispwin);
+           wrefresh (statwin);
            wrefresh (inpwin);
        }
-       if ((int) c == 13) {
+       if (((int) c == 13) || ((int) c == 10)) {
            curs_set (0);
-           xasprintf (&message2, "%s\"\r\n", message2);
-           ttt_socket_write (_socket, message2, strlen (message2));
-           xasprintf (&message2, "MESSAGE \"");
-           maxread = 100;
-           numread = read (_socket, buffer, maxread);
-           if (numread > 0) {
-               buffer[numread] = '\0';
-               wprint (dispwin, buffer);
+           if (inplin[0] == '/') {
+               fprintf(sockout,"%s\r\n",&inplin[1]);
+           }
+           else {
+               fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin);
+           }
+           fflush(sockout);
+           inplin[0] = '\0';
+           if (fgets(buffer,BUFSIZ,sockin)) {
+               if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
+                   wprint (dispwin, buffer);
+               }
+               else {
+                   if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
+               }
            }
            wrefresh (dispwin);
+           wrefresh (statwin);
            werase (inpwin);
            wrefresh (inpwin);
        }
@@ -225,18 +308,14 @@ main (int argc, char **argv)
 static void
 finish (int sig)
 {
-    char buffer[1024];
-    ssize_t numread;
+    char buffer[BUFSIZ];
+    
     endwin();
 
     /* do your non-curses wrapup here */
-    xasprintf (&message2, "QUIT\r\n");
-    ttt_socket_write (_socket, message2, strlen (message2));
-
-    maxread = 100;
-    numread = read (_socket, buffer, maxread);
-    buffer[numread] = '\0';
-    xasprintf (&message2, "%s", buffer);
+    fprintf(sockout,"QUIT\r\n");
+    fflush(sockout);
 
+    while (fgets(buffer,BUFSIZ,sockin));
     exit (0);
 }