+
+ case 404: // Unknown user
+ fprintf(stderr, "Unknown user '%s'\n", Username);
+ return 2;
+
+ default:
+ fprintf(stderr, "Unknown response code %i\n", responseCode);
+ return -1;
+ }
+
+ return -1;
+}
+
+
+/**
+ * \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 0;
+ }
+
+ // 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");
+ return 1;
+
+ default:
+ fprintf(stderr, "Unknown response code %i\n", responseCode);
+ return -1;
+ }
+
+ return -1;
+}
+
+/**
+ * \brief Enumerate users
+ */
+int Dispense_EnumUsers(int Socket)
+{
+ char *buf;
+ int responseCode;
+ int nUsers;
+ regmatch_t matches[4];
+
+ if( giMinimumBalance != INT_MIN ) {
+ if( giMaximumBalance != INT_MAX ) {
+ sendf(Socket, "ENUM_USERS min_balance:%i max_balance:%i\n", giMinimumBalance, giMaximumBalance);
+ }
+ else {
+ sendf(Socket, "ENUM_USERS min_balance:%i\n", giMinimumBalance);
+ }
+ }
+ else {
+ if( giMaximumBalance != INT_MAX ) {
+ sendf(Socket, "ENUM_USERS max_balance:%i\n", giMaximumBalance);
+ }
+ else {
+ sendf(Socket, "ENUM_USERS\n");
+ }
+ }
+ buf = ReadLine(Socket);
+ responseCode = atoi(buf);
+
+ switch(responseCode)
+ {
+ case 201: break; // Ok, length follows
+
+ default:
+ fprintf(stderr, "Unknown response code %i\n%s\n", responseCode, buf);
+ free(buf);
+ return -1;
+ }
+
+ // Get count (not actually used)
+ RunRegex(&gArrayRegex, buf, 4, matches, "Malformed server response");
+ nUsers = atoi( buf + matches[3].rm_so );
+ printf("%i users returned\n", nUsers);
+
+ // Free string
+ free(buf);
+
+ // Read returned users
+ do {
+ buf = ReadLine(Socket);
+ responseCode = atoi(buf);
+
+ if( responseCode != 202 ) break;
+
+ _PrintUserLine(buf);
+ free(buf);
+ } while(responseCode == 202);
+
+ // Check final response
+ if( responseCode != 200 ) {
+ fprintf(stderr, "Unknown response code %i\n%s\n", responseCode, buf);
+ free(buf);
+ return -1;
+ }
+
+ free(buf);
+
+ return 0;
+}
+
+int Dispense_ShowUser(int Socket, const char *Username)
+{
+ char *buf;
+ int responseCode, ret;
+
+ sendf(Socket, "USER_INFO %s\n", Username);
+ buf = ReadLine(Socket);
+
+ responseCode = atoi(buf);
+
+ switch(responseCode)
+ {
+ case 202:
+ _PrintUserLine(buf);
+ ret = 0;
+ break;
+
+ case 404:
+ printf("Unknown user '%s'\n", Username);
+ ret = 1;
+ break;
+
+ default:
+ fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf);
+ ret = -1;
+ break;
+ }
+
+ free(buf);
+
+ return ret;
+}
+
+void _PrintUserLine(const char *Line)
+{
+ regmatch_t matches[6];
+ int bal;
+
+ RunRegex(&gUserInfoRegex, Line, 6, matches, "Malformed server response");
+ // 3: Username
+ // 4: Balance
+ // 5: Flags
+ {
+ int usernameLen = matches[3].rm_eo - matches[3].rm_so;
+ char username[usernameLen + 1];
+ int flagsLen = matches[5].rm_eo - matches[5].rm_so;
+ char flags[flagsLen + 1];
+
+ memcpy(username, Line + matches[3].rm_so, usernameLen);
+ username[usernameLen] = '\0';
+ memcpy(flags, Line + matches[5].rm_so, flagsLen);
+ flags[flagsLen] = '\0';
+
+ bal = atoi(Line + matches[4].rm_so);
+ printf("%-15s: $%4i.%02i (%s)\n", username, bal/100, abs(bal)%100, flags);
+ }
+}
+
+int Dispense_AddUser(int Socket, const char *Username)
+{
+ char *buf;
+ int responseCode, ret;
+
+ // Check for a dry run
+ if( gbDryRun ) {
+ printf("Dry Run - No action\n");
+ return 0;
+ }
+
+ sendf(Socket, "USER_ADD %s\n", Username);
+
+ buf = ReadLine(Socket);
+ responseCode = atoi(buf);
+
+ switch(responseCode)
+ {
+ case 200:
+ printf("User '%s' added\n", Username);
+ ret = 0;
+ break;
+
+ case 403:
+ printf("Only wheel can add users\n");
+ ret = 1;
+ break;
+
+ case 404:
+ printf("User '%s' already exists\n", Username);
+ ret = 0;
+ break;
+
+ default:
+ fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf);
+ ret = -1;
+ break;
+ }
+
+ free(buf);
+
+ return ret;
+}
+
+int Dispense_SetUserType(int Socket, const char *Username, const char *TypeString)
+{
+ char *buf;
+ int responseCode, ret;
+
+ // Check for a dry run
+ if( gbDryRun ) {
+ printf("Dry Run - No action\n");
+ return 0;
+ }
+
+ // TODO: Pre-validate the string
+
+ sendf(Socket, "USER_FLAGS %s %s\n", Username, TypeString);
+
+ buf = ReadLine(Socket);
+ responseCode = atoi(buf);
+
+ switch(responseCode)
+ {
+ case 200:
+ printf("User '%s' updated\n", Username);
+ ret = 0;
+ break;
+
+ case 403:
+ printf("Only wheel can modify users\n");
+ ret = 1;
+ break;
+
+ case 404:
+ printf("User '%s' does not exist\n", Username);
+ ret = 0;
+ break;
+
+ case 407:
+ printf("Flag string is invalid\n");
+ ret = 0;
+ break;
+