Start on a ncurses GUI
[tpg/opendispense2.git] / src / client / main.c
index 2bb4f63..5f5f1c8 100644 (file)
 #include <ctype.h>     // isspace
 #include <stdarg.h>
 #include <regex.h>
+#include <ncurses.h>
 
 #include <unistd.h>    // close
 #include <netdb.h>     // gethostbyname
+#include <pwd.h>       // getpwuids
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -29,8 +31,11 @@ typedef struct sItem {
 }      tItem;
 
 // === PROTOTYPES ===
+ int   ShowNCursesUI(void);
+
  int   sendf(int Socket, const char *Format, ...);
  int   OpenConnection(const char *Host, int Port);
+void   Authenticate(int Socket);
 char   *trim(char *string);
  int   RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage);
 void   CompileRegex(regex_t *regex, const char *pattern, int flags);
@@ -120,8 +125,7 @@ int main(int argc, char *argv[])
                }
        }
        
-       
-       // Display the list for the user
+       // Get item information
        for( i = 0; i < giNumItems; i ++ )
        {
                regmatch_t      matches[6];
@@ -151,8 +155,17 @@ int main(int argc, char *argv[])
                printf("%3i %s\n", gaItems[i].Price, gaItems[i].Desc);
        }
        
+       Authenticate(sock);
        
        // and choose what to dispense
+       // TODO: ncurses interface (with separation between item classes)
+       // - Hmm... that would require standardising the item ID to be <class>:<index>
+       // Oh, why not :)
+       
+       #if 1
+       i = ShowNCursesUI();
+       #else
+       
        for(;;)
        {
                char    *buf;
@@ -175,36 +188,40 @@ int main(int argc, char *argv[])
                                printf("Bad item (should be between 0 and %i)\n", giNumItems);
                                continue;
                        }
-                       
-                       sendf(sock, "DISPENSE %s\n", gaItems[i].Ident);
-                       
-                       len = recv(sock, buffer, BUFSIZ-1, 0);
-                       buffer[len] = '\0';
-                       trim(buffer);
-                       
-                       responseCode = atoi(buffer);
-                       switch( responseCode )
-                       {
-                       case 200:
-                               printf("Dispense OK\n");
-                               break;
-                       case 401:
-                               printf("Not authenticated\n");
-                               break;
-                       case 402:
-                               printf("Insufficient balance\n");
-                               break;
-                       case 406:
-                               printf("Bad item name, bug report\n");
-                               break;
-                       case 500:
-                               printf("Item failed to dispense, is the slot empty?\n");
-                               break;
-                       default:
-                               printf("Unknown response code %i\n", responseCode);
-                               break;
-                       }
-                       
+                       break;
+               }
+       }
+       #endif
+       
+       if( i >= 0 )
+       {       
+               // Dispense!
+               sendf(sock, "DISPENSE %s\n", gaItems[i].Ident);
+               
+               len = recv(sock, buffer, BUFSIZ-1, 0);
+               buffer[len] = '\0';
+               trim(buffer);
+               
+               responseCode = atoi(buffer);
+               switch( responseCode )
+               {
+               case 200:
+                       printf("Dispense OK\n");
+                       break;
+               case 401:
+                       printf("Not authenticated\n");
+                       break;
+               case 402:
+                       printf("Insufficient balance\n");
+                       break;
+               case 406:
+                       printf("Bad item name, bug report\n");
+                       break;
+               case 500:
+                       printf("Item failed to dispense, is the slot empty?\n");
+                       break;
+               default:
+                       printf("Unknown response code %i\n", responseCode);
                        break;
                }
        }
@@ -214,6 +231,160 @@ int main(int argc, char *argv[])
        return 0;
 }
 
+/**
+ */
+int ShowNCursesUI(void)
+{
+        int    ch;
+        int    i, times;
+        int    xBase, yBase;
+       const int       displayMinWidth = 34;
+       const int       displayMinItems = 8;
+       char    *titleString = "Dispense";
+        int    titleStringLen = strlen(titleString);
+        int    itemCount = displayMinItems;
+        int    itemBase = 0;
+        
+        int    height = itemCount + 3;
+        int    width = displayMinWidth;
+        
+       // Enter curses mode
+       initscr();
+       raw(); noecho();
+       
+       xBase = COLS/2 - width/2;
+       yBase = LINES/2 - height/2;
+       
+       for( ;; )
+       {
+               // Header
+               move( yBase, xBase );
+               addch('/');
+               times = width/2 - titleStringLen/2 - 2;
+               while(times --) addch('-');
+               addch(' ');
+               addstr(titleString);
+               addch(' ');
+               times = width/2 - titleStringLen/2 - 2;
+               while(times --) addch('-');
+               addch('\\');
+               
+               // Items
+               for( i = 0; i < itemCount; i ++ )
+               {
+                        int    _x, _y;
+                       move( yBase + 1 + i, xBase );
+                       addch('|');
+                       addch(' ');
+                       
+                       // Check for ... row
+                       if( i == 0 && itemBase > 0 ) {
+                               printw("   ...");
+                               times = width - 1 - 8;
+                               while(times--)  addch(' ');
+                       }
+                       else if( i == itemCount - 1 && itemBase < giNumItems - itemCount ) {
+                               printw("   ...");
+                               times = width - 1 - 8;
+                               while(times--)  addch(' ');
+                       }
+                       // Show an item
+                       else {
+                               if( itemBase + i < 0 || itemBase + i >= giNumItems ) {
+                                       printw("%02i %i OOR", itemBase + i, i);
+                                       continue ;
+                               }
+                               printw("%02i %s", itemBase + i, gaItems[itemBase + i].Desc);
+                               
+                               getyx(stdscr, _y, _x);
+                               times = width - 6 - (_x - xBase);       // TODO: Better handling for large prices
+                               while(times--)  addch(' ');
+                               printw("%4i ", gaItems[itemBase + i].Price);
+                       }
+                       
+                       // Scrollbar (if needed)
+                       if( giNumItems > itemCount ) {
+                               if( i == 0 ) {
+                                       addch('A');
+                               }
+                               else if( i == itemCount - 1 ) {
+                                       addch('V');
+                               }
+                               else {
+                                        int    percentage = itemBase * 100 / (giNumItems-itemCount);
+                                       if( i-1 == percentage*(itemCount-3)/100 ) {
+                                               addch('#');
+                                       }
+                                       else {
+                                               addch('|');
+                                       }
+                               }
+                       }
+                       else {
+                               addch('|');
+                       }
+               }
+               
+               // Footer
+               move( yBase + 1 + itemCount, xBase );
+               addch('\\');
+               times = width/2 - titleStringLen/2 - 2;
+               while(times --) addch('-');
+               addch(' ');
+               addstr(titleString);
+               addch(' ');
+               times = width/2 - titleStringLen/2 - 2;
+               while(times --) addch('-');
+               addch('/');
+               
+               move( yBase + 1 + itemCount + 1, xBase );
+               {
+                        int    count = itemCount-2;
+                        int    ofs = itemBase;
+                       if( itemBase == 0 )     count ++;
+                       else    ofs ++;
+                       if( itemBase == giNumItems-itemCount) {
+                               count ++;
+                               ofs ++;
+                       }
+                       printw("%i - %i / %i items", itemBase, itemBase+count, giNumItems);
+               }
+               
+               ch = getch();
+               
+               if( ch == '\x1B' ) {
+                       ch = getch();
+                       if( ch == '[' ) {
+                               ch = getch();
+                               
+                               switch(ch)
+                               {
+                               case 'B':
+                                       if( itemBase < giNumItems - (itemCount) )
+                                               itemBase ++;
+                                       break;
+                               case 'A':
+                                       if( itemBase > 0 )
+                                               itemBase --;
+                                       break;
+                               }
+                       }
+                       else {
+                               
+                       }
+               }
+               else {
+                       break;
+               }
+               
+       }
+       
+       
+       // Leave
+       endwin();
+       return -1;
+}
+
 // === HELPERS ===
 int sendf(int Socket, const char *Format, ...)
 {
@@ -259,6 +430,17 @@ int OpenConnection(const char *Host, int Port)
                return -1;
        }
        
+       #if USE_AUTOAUTH
+       {
+               struct sockaddr_in      localAddr;
+               memset(&localAddr, 0, sizeof(localAddr));
+               localAddr.sin_family = AF_INET; // IPv4
+               localAddr.sin_port = 1023;      // IPv4
+               // Attempt to bind to low port for autoauth
+               bind(sock, &localAddr, sizeof(localAddr));
+       }
+       #endif
+       
        if( connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0 ) {
                fprintf(stderr, "Failed to connect to server\n");
                return -1;
@@ -267,6 +449,41 @@ int OpenConnection(const char *Host, int Port)
        return sock;
 }
 
+void Authenticate(int Socket)
+{
+       struct passwd   *pwd;
+       char    buf[512];
+        int    responseCode;
+       
+       // Get user name
+       pwd = getpwuid( getuid() );
+       
+       // Attempt automatic authentication
+       sendf(Socket, "AUTOAUTH %s\n", pwd->pw_name);
+       
+       // Check if it worked
+       recv(Socket, buf, 511, 0);
+       trim(buf);
+       
+       responseCode = atoi(buf);
+       switch( responseCode )
+       {
+       case 200:       // Authenticated, return :)
+               return ;
+       case 401:       // Untrusted, attempt password authentication
+               break;
+       case 404:       // Bad Username
+               fprintf(stderr, "Bad Username '%s'\n", pwd->pw_name);
+               exit(-1);
+       default:
+               fprintf(stderr, "Unkown response code %i from server\n", responseCode);
+               printf("%s\n", buf);
+               exit(-1);
+       }
+       
+       printf("%s\n", buf);
+}
+
 char *trim(char *string)
 {
         int    i;

UCC git Repository :: git.ucc.asn.au