Added user and balance view to the ncurses gui
authorJohn Hodge <[email protected]>
Sun, 20 Feb 2011 03:22:57 +0000 (11:22 +0800)
committerJohn Hodge <[email protected]>
Sun, 20 Feb 2011 03:22:57 +0000 (11:22 +0800)
src/client/main.c

index f30ce43..c5f335b 100644 (file)
@@ -47,6 +47,7 @@ void  PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1, const
 // --- Coke Server Communication ---
  int   OpenConnection(const char *Host, int Port);
  int   Authenticate(int Socket);
+ int   GetUserBalance(int Socket);
 void   PopulateItemList(int Socket);
  int   Dispense_ItemInfo(int Socket, const char *Type, int ID);
  int   DispenseItem(int Socket, const char *Type, int ID);
@@ -81,6 +82,9 @@ char  *gsEffectiveUser;       //!< '-u' Dispense as another user
  int   gbDryRun = 0;   //!< '-n' Read-only
  int   giMinimumBalance = INT_MIN;     //!< '-m' Minumum balance for `dispense acct`
  int   giMaximumBalance = INT_MAX;     //!< '-M' Maximum balance for `dispense acct`
+char   *gsUserName;    //!< User that dispense will happen as
+char   *gsUserFlags;   //!< User's flag set
+ int   giUserBalance=-1;       //!< User balance (set by Authenticate)
 
 // === CODE ===
 int main(int argc, char *argv[])
@@ -330,6 +334,9 @@ int main(int argc, char *argv[])
        sock = OpenConnection(gsDispenseServer, giDispensePort);
        if( sock < 0 )  return -1;
 
+       // Get the user's balance
+       GetUserBalance(sock);
+
        // Get items
        PopulateItemList(sock);
        
@@ -410,9 +417,13 @@ int main(int argc, char *argv[])
                                }
                                else {
                                        // TODO: Allow ambiguous matches?
+                                       // or just print a wanrning
+                                       printf("Warning - Ambiguous pattern, stopping\n");
+                                       return 1;
                                }
                        }
                        
+                       // Was a match found?
                        if( best == -1 )
                        {
                                fprintf(stderr, "No item matches the passed string\n");
@@ -539,9 +550,23 @@ int ShowNCursesUI(void)
         int    itemBase = 0;
         int    currentItem = 0;
         int    ret = -2;       // -2: Used for marking "no return yet"
+       
+       char    balance_str[5+1+2+1];   // If $9999.99 is too little, something's wrong
+       char    *username;
+       struct passwd *pwd;
         
         int    height, width;
         
+       // Get Username
+       if( gsEffectiveUser )
+               username = gsEffectiveUser;
+       else {
+               pwd = getpwuid( getuid() );
+               username = pwd->pw_name;
+       }
+       // Get balance
+       snprintf(balance_str, sizeof balance_str, "$%i.%02i", giUserBalance/100, giUserBalance%100);
+       
        // Enter curses mode
        initscr();
        raw(); noecho();
@@ -621,6 +646,12 @@ int ShowNCursesUI(void)
                // Footer
                PrintAlign(yBase+height-2, xBase, width, "\\", '-', "", '-', "/");
                
+               // User line
+               // - Username, balance, flags
+               PrintAlign(yBase+height-1, xBase+1, width-2,
+                       username, ' ', balance_str, ' ', gsUserFlags);
+               
+               
                // Get input
                ch = getch();
                
@@ -748,7 +779,7 @@ void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1,
                addstr(tmp);
        }
        // - Left padding
-       times = Width/2 - mLen/2 - lLen;
+       times = (Width - mLen)/2 - lLen;
        while(times--)  addch(Pad1);
        // - Middle
        {
@@ -757,7 +788,8 @@ void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1,
                addstr(tmp);
        }
        // - Right Padding
-       times = Width/2 - mLen/2 - rLen;
+       times = (Width - mLen)/2 - rLen;
+       if( (Width - mLen) % 2 )        times ++;
        while(times--)  addch(Pad2);
        // - Right
        {
@@ -972,6 +1004,53 @@ int Authenticate(int Socket)
        return 0;
 }
 
+int GetUserBalance(int Socket)
+{
+       regmatch_t      matches[6];
+       struct passwd   *pwd;
+       char    *buf;
+        int    responseCode;
+       
+       if( !gsUserName )
+       {
+               if( gsEffectiveUser ) {
+                       gsUserName = gsEffectiveUser;
+               }
+               else {
+                       pwd = getpwuid( getuid() );
+                       gsUserName = strdup(pwd->pw_name);
+               }
+       }
+       
+       sendf(Socket, "USER_INFO %s\n", gsUserName);
+       buf = ReadLine(Socket);
+       responseCode = atoi(buf);
+       switch(responseCode)
+       {
+       case 202:       break;  // Ok
+       
+       case 404:
+               printf("Invalid user? (USER_INFO failed)\n");
+               free(buf);
+               return -1;
+       
+       default:
+               fprintf(stderr, "Unkown response code %i from server\n", responseCode);
+               printf("%s\n", buf);
+               free(buf);
+               exit(-1);
+       }
+
+       RunRegex(&gUserInfoRegex, buf, 6, matches, "Malformed server response");
+       
+       giUserBalance = atoi( buf + matches[4].rm_so );
+       gsUserFlags = strdup( buf + matches[5].rm_so );
+       
+       free(buf);
+       
+       return 0;
+}
+
 
 /**
  * \brief Fill the item information structure

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