// === TYPES ===
typedef struct sItem {
- char *Ident;
+ char *Type;
+ int ID;
char *Desc;
int Price;
} tItem;
void PopulateItemList(int Socket);
int DispenseItem(int Socket, int ItemID);
int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const char *Reason);
- int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const char *Reason);
+ int Dispense_Give(int Socket, const char *Username, int Ammount, const char *Reason);
int Dispense_EnumUsers(int Socket);
int Dispense_ShowUser(int Socket, const char *Username);
void _PrintUserLine(const char *Line);
// > Code Type Count ...
CompileRegex(&gArrayRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([0-9]+)", REG_EXTENDED); //
// > Code Type Ident Price Desc
- CompileRegex(&gItemRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([A-Za-z0-9:]+?)\\s+([0-9]+)\\s+(.+)$", REG_EXTENDED);
+ CompileRegex(&gItemRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([A-Za-z]+):([0-9]+)\\s+([0-9]+)\\s+(.+)$", REG_EXTENDED);
// > Code 'SALT' salt
CompileRegex(&gSaltRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+(.+)$", REG_EXTENDED);
// > Code 'User' Username Balance Flags
// argv[i+2]: Ammount
// argv[i+3]: Reason
- if( argv[i+2][0] == '=' ) {
- // Set balance
- Dispense_SetBalance(sock, argv[i+1], atoi(argv[i+2] + 1), argv[i+3]);
- }
- else {
- // Alter balance
- Dispense_AlterBalance(sock, argv[i+1], atoi(argv[i+2]), argv[i+3]);
- }
+ // Alter balance
+ Dispense_AlterBalance(sock, argv[i+1], atoi(argv[i+2]), argv[i+3]);
}
+ // Show user information
Dispense_ShowUser(sock, argv[i+1]);
close(sock);
return 0;
}
+ //
+ // `dispense give`
+ // - "Here, have some money."
+ else if( strcmp(arg, "give") == 0 )
+ {
+ if( i + 3 >= argc ) {
+ fprintf(stderr, "`dispense give` takes three arguments\n");
+ ShowUsage();
+ return -1;
+ }
+ // TODO: `dispense give`
+
+ // argv[i+1]: Destination
+ // argv[i+2]: Ammount
+ // argv[i+3]: Reason
+
+ // Connect to server
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if( sock < 0 ) return -1;
+
+ // Authenticate
+ if( Authenticate(sock) )
+ return -1;
+
+ Dispense_Give(sock, argv[i+1], atoi(argv[i+2]), argv[i+3]);
+ return 0;
+ }
+ //
+ // `dispense user`
+ // - User administration (Wheel Only)
else if( strcmp(arg, "user") == 0 )
{
// Check argument count
if( gsItemPattern )
{
-
+ // TODO: Implement `dispense <name>`
+ printf("TODO: Implement `dispense <name>`\n");
+ i = -1;
}
else if( gbUseNCurses )
{
else
{
for( i = 0; i < giNumItems; i ++ ) {
- printf("%2i %s\t%3i %s\n", i, gaItems[i].Ident, gaItems[i].Price, gaItems[i].Desc);
+ printf("%2i %s:%i\t%3i %s\n", i, gaItems[i].Type, gaItems[i].ID,
+ gaItems[i].Price, gaItems[i].Desc);
}
printf(" q Quit\n");
for(;;)
" Give some of your money away\n"
" dispense acct [<user>]\n"
" Show user balances\n"
- " dispense acct <user> [+-=]<ammount> \"<reason>\"\n"
+ " dispense acct <user> [+-]<ammount> \"<reason>\"\n"
" Alter a account value (Coke members only)\n"
" dispense user add <user>\n"
" Create new coke account (Wheel members only)\n"
responseCode = atoi(buf);
switch( responseCode )
{
-
- case 200: // Authenticated, return :)
+ case 200: // Autoauth succeeded, return
free(buf);
break;
// Fetch item information
for( i = 0; i < giNumItems; i ++ )
{
- regmatch_t matches[6];
+ regmatch_t matches[7];
// Get item info
buf = ReadLine(Socket);
exit(-1);
}
- RunRegex(&gItemRegex, buf, 6, matches, "Malformed server response");
+ RunRegex(&gItemRegex, buf, 7, matches, "Malformed server response");
buf[ matches[3].rm_eo ] = '\0';
- gaItems[i].Ident = strdup( buf + matches[3].rm_so );
- gaItems[i].Price = atoi( buf + matches[4].rm_so );
- gaItems[i].Desc = strdup( buf + matches[5].rm_so );
+ gaItems[i].Type = strdup( buf + matches[3].rm_so );
+ gaItems[i].ID = atoi( buf + matches[4].rm_so );
+ gaItems[i].Price = atoi( buf + matches[5].rm_so );
+ gaItems[i].Desc = strdup( buf + matches[6].rm_so );
free(buf);
}
if( ItemID < 0 || ItemID > giNumItems ) return -1;
// Dispense!
- sendf(Socket, "DISPENSE %s\n", gaItems[ItemID].Ident);
+ sendf(Socket, "DISPENSE %s:%i\n", gaItems[ItemID].Type, gaItems[ItemID].ID);
buf = ReadLine(Socket);
responseCode = atoi(buf);
switch(responseCode)
{
case 200: return 0; // OK
+ case 402:
+ fprintf(stderr, "Insufficient balance\n");
+ return 1;
case 403: // Not in coke
fprintf(stderr, "You are not in coke (sucker)\n");
return 1;
/**
* \brief Alter a user's balance
*/
-int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const char *Reason)
+int Dispense_Give(int Socket, const char *Username, int Ammount, const char *Reason)
{
char *buf;
int responseCode;
- sendf(Socket, "SET %s %i %s\n", Username, Ammount, Reason);
+ if( Ammount < 0 ) {
+ printf("Sorry, you can only give, you can't take.\n");
+ return -1;
+ }
+
+ // Fast return on zero
+ if( Ammount == 0 ) {
+ printf("Are you actually going to give any?\n");
+ return 0;
+ }
+
+ sendf(Socket, "GIVE %s %i %s\n", Username, Ammount, Reason);
buf = ReadLine(Socket);
responseCode = atoi(buf);
switch(responseCode)
{
case 200: return 0; // OK
- case 403: // Not in coke
- fprintf(stderr, "You are not in coke (sucker)\n");
+
+ case 402:
+ fprintf(stderr, "Insufficient balance\n");
return 1;
+
case 404: // Unknown user
fprintf(stderr, "Unknown user '%s'\n", Username);
return 2;
+
default:
fprintf(stderr, "Unknown response code %i\n", responseCode);
return -1;
flags[flagsLen] = '\0';
bal = atoi(Line + matches[4].rm_so);
- printf("%-15s: $%4i.%02i (%s)\n", username, bal/100, bal%100, flags);
+ printf("%-15s: $%4i.%02i (%s)\n", username, bal/100, abs(bal)%100, flags);
}
}