+ ret = RV_SERVER_ERROR;
+ break;
+ default:
+ printf("Unknown response code %i ('%s')\n", responseCode, buf);
+ ret = RV_UNKNOWN_ERROR;
+ break;
+ }
+
+ free(buf);
+ return ret;
+}
+
+/**
+ * \brief Alter a user's balance
+ */
+int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const char *Reason)
+{
+ char *buf;
+ int responseCode, rv = -1;
+
+ // Check for a dry run
+ if( gbDryRun ) {
+ printf("Dry Run - No action\n");
+ return 0;
+ }
+
+ // Sanity
+ if( Ammount == 0 ) {
+ printf("An amount would be nice\n");
+ return RV_ARGUMENTS;
+ }
+
+ sendf(Socket, "ADD %s %i %s\n", Username, Ammount, Reason);
+ buf = ReadLine(Socket);
+
+ responseCode = atoi(buf);
+
+ switch(responseCode)
+ {
+ case 200:
+ rv = 0; // OK
+ break;
+ case 402:
+ fprintf(stderr, "Insufficient balance\n");
+ rv = RV_BAD_ITEM;
+ break;
+ case 403: // Not in coke
+ fprintf(stderr, "You are not in coke (sucker)\n");
+ rv = RV_PERMISSIONS;
+ break;
+ case 404: // Unknown user
+ fprintf(stderr, "Unknown user '%s'\n", Username);
+ rv = RV_INVALID_USER;
+ break;
+ default:
+ fprintf(stderr, "Unknown response code %i\n'%s'\n", responseCode, buf);
+ rv = RV_UNKNOWN_RESPONSE;
+ break;
+ }
+ free(buf);
+
+ return rv;
+}
+
+/**
+ * \brief Set a user's balance
+ * \note Only avaliable to dispense admins
+ */
+int Dispense_SetBalance(int Socket, const char *Username, int Balance, const char *Reason)
+{
+ char *buf;
+ int responseCode;
+
+ // Check for a dry run
+ if( gbDryRun ) {
+ printf("Dry Run - No action\n");
+ return 0;
+ }
+
+ sendf(Socket, "SET %s %i %s\n", Username, Balance, Reason);
+ buf = ReadLine(Socket);
+
+ responseCode = atoi(buf);
+ free(buf);
+
+ switch(responseCode)
+ {
+ case 200: return 0; // OK
+ case 403: // Not in coke
+ fprintf(stderr, "You are not an admin\n");
+ return RV_PERMISSIONS;
+ case 404: // Unknown user
+ fprintf(stderr, "Unknown user '%s'\n", Username);
+ return RV_INVALID_USER;
+ default:
+ fprintf(stderr, "Unknown response code %i\n", responseCode);
+ return RV_UNKNOWN_RESPONSE;
+ }
+
+ return -1;
+}
+
+/**
+ * \brief Give money to another user
+ */
+int Dispense_Give(int Socket, const char *Username, int Ammount, const char *Reason)
+{
+ char *buf;
+ int responseCode;
+
+ if( Ammount < 0 ) {
+ printf("Sorry, you can only give, you can't take.\n");
+ return RV_ARGUMENTS;
+ }
+
+ // Fast return on zero
+ if( Ammount == 0 ) {
+ printf("Are you actually going to give any?\n");
+ return RV_ARGUMENTS;
+ }
+
+ // Check for a dry run
+ if( gbDryRun ) {
+ printf("Dry Run - No action\n");
+ return 0;
+ }
+
+ sendf(Socket, "GIVE %s %i %s\n", Username, Ammount, Reason);
+
+ buf = ReadLine(Socket);
+ responseCode = atoi(buf);
+ free(buf);
+ switch(responseCode)
+ {
+ case 200:
+ printf("Give succeeded\n");
+ return RV_SUCCESS; // OK
+
+ case 402:
+ fprintf(stderr, "Insufficient balance\n");
+ return RV_BALANCE;
+
+ case 404: // Unknown user
+ fprintf(stderr, "Unknown user '%s'\n", Username);
+ return RV_INVALID_USER;
+
+ default:
+ fprintf(stderr, "Unknown response code %i\n", responseCode);
+ return RV_UNKNOWN_RESPONSE;
+ }
+
+ return -1;
+}
+
+int Dispense_Refund(int Socket, const char *Username, const char *Item, int PriceOverride)
+{
+ char *buf;
+ int responseCode, ret = -1;
+
+ // Check item id
+ if( RunRegex(&gUserItemIdentRegex, Item, 0, NULL, NULL) != 0 )
+ {
+ fprintf(stderr, "Error: Invalid item ID passed (should be <type>:<num>)\n");
+ return RV_ARGUMENTS;
+ }
+
+ // Check username (quick)
+ if( strchr(Username, ' ') || strchr(Username, '\n') )
+ {
+ fprintf(stderr, "Error: Username is invalid (no spaces or newlines please)\n");
+ return RV_ARGUMENTS;
+ }
+
+ // Send the query
+ sendf(Socket, "REFUND %s %s %i\n", Username, Item, PriceOverride);
+
+ buf = ReadLine(Socket);
+ responseCode = atoi(buf);
+ switch(responseCode)
+ {
+ case 200:
+ Dispense_ShowUser(Socket, Username); // Show destination account
+ ret = 0;
+ break;
+ case 403:
+ fprintf(stderr, "Refund access is only avaliable to coke members\n");
+ ret = RV_PERMISSIONS;
+ break;
+ case 404:
+ fprintf(stderr, "Unknown user '%s' passed\n", Username);
+ ret = RV_INVALID_USER;
+ break;
+ case 406:
+ fprintf(stderr, "Invalid item '%s' passed\n", Item);
+ ret = RV_BAD_ITEM;
+ break;
+ default:
+ fprintf(stderr, "Unknown response from server %i\n%s\n", responseCode, buf);
+ ret = -1;
+ break;
+ }
+ free(buf);
+ return ret;
+}
+
+/**
+ * \brief Donate money to the club
+ */
+int Dispense_Donate(int Socket, int Ammount, const char *Reason)
+{
+ char *buf;
+ int responseCode;
+
+ 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 1;
+ }
+
+ // Check for a dry run
+ if( gbDryRun ) {
+ printf("Dry Run - No action\n");
+ return 0;
+ }
+
+ sendf(Socket, "DONATE %i %s\n", Ammount, Reason);
+ buf = ReadLine(Socket);
+
+ responseCode = atoi(buf);
+ free(buf);
+
+ switch(responseCode)
+ {
+ case 200: return 0; // OK
+
+ case 402:
+ fprintf(stderr, "Insufficient balance\n");