+
+ gsTextArgs[giTextArgc++] = argv[i];
+
+ }
+
+ //
+ // `dispense acct`
+ // -
+ if( strcmp(gsTextArgs[0], "acct") == 0 )
+ {
+ // Connect to server
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if( sock < 0 ) return RV_SOCKET_ERROR;
+ // List accounts?
+ if( giTextArgc == 1 ) {
+ ret = Dispense_EnumUsers(sock);
+ close(sock);
+ return ret;
+ }
+
+ // gsTextArgs[1]: Username
+
+ // Alter account?
+ if( giTextArgc != 2 )
+ {
+ if( giTextArgc != 4 ) {
+ fprintf(stderr, "`dispense acct` requires a reason\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+
+ // Authentication required
+ ret = Authenticate(sock);
+ if(ret) return ret;
+
+ // gsTextArgs[1]: Username
+ // gsTextArgs[2]: Ammount
+ // gsTextArgs[3]: Reason
+
+ if( gsTextArgs[2][0] == '=' ) {
+ // Set balance
+ if( gsTextArgs[2][1] != '0' && atoi(gsTextArgs[2]+1) == 0 ) {
+ fprintf(stderr, "Error: Invalid balance to be set\n");
+ exit(1);
+ }
+
+ ret = Dispense_SetBalance(sock, gsTextArgs[1], atoi(gsTextArgs[2]+1), gsTextArgs[3]);
+ }
+ else {
+ // Alter balance
+ ret = Dispense_AlterBalance(sock, gsTextArgs[1], atoi(gsTextArgs[2]), gsTextArgs[3]);
+ }
+ }
+ // On error, quit
+ if( ret ) {
+ close(sock);
+ return ret;
+ }
+
+ // Show user information
+ ret = Dispense_ShowUser(sock, gsTextArgs[1]);
+
+ close(sock);
+ return ret;
+ }
+ //
+ // `dispense give`
+ // - "Here, have some money."
+ else if( strcmp(gsTextArgs[0], "give") == 0 )
+ {
+ if( giTextArgc != 4 ) {
+ fprintf(stderr, "`dispense give` takes three arguments\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+
+ // gsTextArgs[1]: Destination
+ // gsTextArgs[2]: Ammount
+ // gsTextArgs[3]: Reason
+
+ // Connect to server
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if( sock < 0 ) return RV_SOCKET_ERROR;
+
+ // Authenticate
+ ret = Authenticate(sock);
+ if(ret) return ret;
+
+ ret = Dispense_Give(sock, gsTextArgs[1], atoi(gsTextArgs[2]), gsTextArgs[3]);
+
+ close(sock);
+
+ return ret;
+ }
+ //
+ // `dispense user`
+ // - User administration (Admin Only)
+ if( strcmp(gsTextArgs[0], "user") == 0 )
+ {
+ // Check argument count
+ if( giTextArgc == 1 ) {
+ fprintf(stderr, "Error: `dispense user` requires arguments\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+
+ // Connect to server
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if( sock < 0 ) return RV_SOCKET_ERROR;
+
+ // Attempt authentication
+ ret = Authenticate(sock);
+ if(ret) return ret;
+
+ // Add new user?
+ if( strcmp(gsTextArgs[1], "add") == 0 )
+ {
+ if( giTextArgc != 3 ) {
+ fprintf(stderr, "Error: `dispense user add` requires an argument\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+
+ ret = Dispense_AddUser(sock, gsTextArgs[2]);
+ }
+ // Update a user
+ else if( strcmp(gsTextArgs[1], "type") == 0 || strcmp(gsTextArgs[1], "flags") == 0 )
+ {
+ if( giTextArgc < 4 || giTextArgc > 5 ) {
+ fprintf(stderr, "Error: `dispense user type` requires two arguments\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+
+ if( giTextArgc == 4 )
+ ret = Dispense_SetUserType(sock, gsTextArgs[2], gsTextArgs[3], "");
+ else
+ ret = Dispense_SetUserType(sock, gsTextArgs[2], gsTextArgs[3], gsTextArgs[4]);
+ }
+ else
+ {
+ fprintf(stderr, "Error: Unknown sub-command for `dispense user`\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+ close(sock);
+ return ret;
+ }
+ // Donation!
+ else if( strcmp(gsTextArgs[0], "donate") == 0 )
+ {
+ // Check argument count
+ if( giTextArgc != 3 ) {
+ fprintf(stderr, "Error: `dispense donate` requires two arguments\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+
+ // Connect to server
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if( sock < 0 ) return RV_SOCKET_ERROR;
+
+ // Attempt authentication
+ ret = Authenticate(sock);
+ if(ret) return ret;
+
+ // Do donation
+ ret = Dispense_Donate(sock, atoi(gsTextArgs[1]), gsTextArgs[2]);
+
+ close(sock);
+
+ return ret;
+ }
+ // Refund an item
+ else if( strcmp(gsTextArgs[0], "refund") == 0 )
+ {
+ int price = 0;
+ // Check argument count
+ if( giTextArgc != 3 && giTextArgc != 4 ) {
+ fprintf(stderr, "Error: `dispense refund` takes 2 or 3 arguments\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+
+ // Connect to server
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if(sock < 0) return RV_SOCKET_ERROR;
+
+ // Attempt authentication
+ ret = Authenticate(sock);
+ if(ret) return ret;
+
+ if( giTextArgc == 4 ) {
+ price = atoi(gsTextArgs[3]);
+ if( price <= 0 ) {
+ fprintf(stderr, "Error: Override price is invalid (should be > 0)\n");
+ return RV_ARGUMENTS;
+ }
+ }
+
+ // Username, Item, cost
+ ret = Dispense_Refund(sock, gsTextArgs[1], gsTextArgs[2], price);
+
+ // TODO: More
+ close(sock);
+ return ret;
+ }
+ // Query an item price
+ else if( strcmp(gsTextArgs[0], "iteminfo") == 0 )
+ {
+ regmatch_t matches[3];
+ char *type;
+ int id;
+ // Check argument count
+ if( giTextArgc != 2 ) {
+ fprintf(stderr, "Error: `dispense iteminfo` requires an argument\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+ // Parse item ID
+ if( RunRegex(&gUserItemIdentRegex, gsTextArgs[1], 3, matches, NULL) != 0 ) {
+ fprintf(stderr, "Error: Invalid item ID passed (<type>:<id> expected)\n");
+ return RV_ARGUMENTS;
+ }
+ type = gsTextArgs[1] + matches[1].rm_so;
+ gsTextArgs[1][ matches[1].rm_eo ] = '\0';
+ id = atoi( gsTextArgs[1] + matches[2].rm_so );
+
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if( sock < 0 ) return RV_SOCKET_ERROR;
+
+ ret = Dispense_ItemInfo(sock, type, id);
+ close(sock);
+ return ret;
+ }
+ // Set slot
+ else if( strcmp(gsTextArgs[0], "slot") == 0 )
+ {
+ regmatch_t matches[3];
+ char *item_type, *newname;
+ int item_id, price;
+
+ // Check arguments
+ if( giTextArgc != 4 ) {
+ fprintf(stderr, "Error: `dispense slot` takes three arguments\n");
+ ShowUsage();
+ return RV_ARGUMENTS;
+ }
+
+ // Parse arguments
+ if( RunRegex(&gUserItemIdentRegex, gsTextArgs[1], 3, matches, NULL) != 0 ) {
+ fprintf(stderr, "Error: Invalid item ID passed (<type>:<id> expected)\n");
+ return RV_ARGUMENTS;
+ }
+ item_type = gsTextArgs[1] + matches[1].rm_so;
+ gsTextArgs[1][ matches[1].rm_eo ] = '\0';
+ item_id = atoi( gsTextArgs[1] + matches[2].rm_so );
+
+ // - Price
+ price = atoi( gsTextArgs[2] );
+ if( price <= 0 && gsTextArgs[2][0] != '0' ) {
+ fprintf(stderr, "Error: Invalid price passed (must be >= 0)\n");
+ return RV_ARGUMENTS;
+ }
+
+ // - New name
+ newname = gsTextArgs[3];
+ // -- Sanity
+ {
+ char *pos;
+ for( pos = newname; *pos; pos ++ )
+ {
+ if( !isalnum(*pos) && *pos != ' ' ) {
+ fprintf(stderr, "Error: You should only have letters, numbers and spaces in an item name\n");
+ return RV_ARGUMENTS;
+ }
+ }