// --- 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);
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[])
sock = OpenConnection(gsDispenseServer, giDispensePort);
if( sock < 0 ) return -1;
+ // Get the user's balance
+ GetUserBalance(sock);
+
// Get items
PopulateItemList(sock);
}
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");
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();
// 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();
addstr(tmp);
}
// - Left padding
- times = Width/2 - mLen/2 - lLen;
+ times = (Width - mLen)/2 - lLen;
while(times--) addch(Pad1);
// - Middle
{
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
{
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