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);
// 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
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`
- printf("TODO: Implement `dispense give`\n");
+
+ // 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
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;
void Server_Cmd_USERFLAGS(tClient *Client, char *Args);
// --- Helpers ---
int sendf(int Socket, const char *Format, ...);
-void HexBin(uint8_t *Dest, char *Src, int BufSize);
// === CONSTANTS ===
// - Commands
return send(Socket, buf, len, 0);
}
}
-
-/**
- * \brief Decode a Base64 value
- */
-int UnBase64(uint8_t *Dest, char *Src, int BufSize)
-{
- uint32_t val;
- int i, j;
- char *start_src = Src;
-
- for( i = 0; i+2 < BufSize; i += 3 )
- {
- val = 0;
- for( j = 0; j < 4; j++, Src ++ ) {
- if('A' <= *Src && *Src <= 'Z')
- val |= (*Src - 'A') << ((3-j)*6);
- else if('a' <= *Src && *Src <= 'z')
- val |= (*Src - 'a' + 26) << ((3-j)*6);
- else if('0' <= *Src && *Src <= '9')
- val |= (*Src - '0' + 52) << ((3-j)*6);
- else if(*Src == '+')
- val |= 62 << ((3-j)*6);
- else if(*Src == '/')
- val |= 63 << ((3-j)*6);
- else if(!*Src)
- break;
- else if(*Src != '=')
- j --; // Ignore invalid characters
- }
- Dest[i ] = (val >> 16) & 0xFF;
- Dest[i+1] = (val >> 8) & 0xFF;
- Dest[i+2] = val & 0xFF;
- if(j != 4) break;
- }
-
- // Finish things off
- if(i < BufSize)
- Dest[i] = (val >> 16) & 0xFF;
- if(i+1 < BufSize)
- Dest[i+1] = (val >> 8) & 0xFF;
-
- return Src - start_src;
-}