+int GetUserBalance(int Socket)
+{
+ regmatch_t matches[6];
+ struct passwd *pwd;
+ char *buf;
+ int responseCode;
+
+ if( !gsUserName )
+ {
+ if( gsEffectiveUser ) {
+ gsUserName = gsEffectiveUser;
+ }
+ else {
+ pwd = getpwuid( getuid() );
+ gsUserName = strdup(pwd->pw_name);
+ }
+ }
+
+ sendf(Socket, "USER_INFO %s\n", gsUserName);
+ buf = ReadLine(Socket);
+ responseCode = atoi(buf);
+ switch(responseCode)
+ {
+ case 202: break; // Ok
+
+ case 404:
+ printf("Invalid user? (USER_INFO failed)\n");
+ free(buf);
+ return RV_INVALID_USER;
+
+ default:
+ fprintf(stderr, "Unkown response code %i from server\n", responseCode);
+ printf("%s\n", buf);
+ free(buf);
+ return RV_UNKNOWN_ERROR;
+ }
+
+ RunRegex(&gUserInfoRegex, buf, 6, matches, "Malformed server response");
+
+ giUserBalance = atoi( buf + matches[4].rm_so );
+ gsUserFlags = strdup( buf + matches[5].rm_so );
+
+ free(buf);
+
+ return 0;
+}
+
+/**
+ * \brief Read an item info response from the server
+ * \param Dest Destination for the read item (strings will be on the heap)
+ */
+int ReadItemInfo(int Socket, tItem *Dest)
+{
+ char *buf;
+ int responseCode;
+
+ regmatch_t matches[8];
+ char *statusStr;
+
+ // Get item info
+ buf = ReadLine(Socket);
+ responseCode = atoi(buf);
+
+ switch(responseCode)
+ {
+ case 202: break;
+
+ case 406:
+ printf("Bad item name\n");
+ free(buf);
+ return RV_BAD_ITEM;
+
+ default:
+ fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n%s", responseCode, buf);
+ free(buf);
+ return RV_UNKNOWN_ERROR;
+ }
+
+ RunRegex(&gItemRegex, buf, 8, matches, "Malformed server response");
+
+ buf[ matches[3].rm_eo ] = '\0';
+ buf[ matches[5].rm_eo ] = '\0';
+ buf[ matches[7].rm_eo ] = '\0';
+
+ statusStr = &buf[ matches[5].rm_so ];
+
+ Dest->ID = atoi( buf + matches[4].rm_so );
+
+ if( strcmp(statusStr, "avail") == 0 )
+ Dest->Status = 0;
+ else if( strcmp(statusStr, "sold") == 0 )
+ Dest->Status = 1;
+ else if( strcmp(statusStr, "error") == 0 )
+ Dest->Status = -1;
+ else {
+ fprintf(stderr, "Unknown response from dispense server (status '%s')\n",
+ statusStr);
+ return RV_UNKNOWN_ERROR;
+ }
+ Dest->Price = atoi( buf + matches[6].rm_so );
+
+ // Hack a little to reduce heap fragmentation
+ {
+ char tmpType[strlen(buf + matches[3].rm_so) + 1];
+ char tmpDesc[strlen(buf + matches[7].rm_so) + 1];
+ strcpy(tmpType, buf + matches[3].rm_so);
+ strcpy(tmpDesc, buf + matches[7].rm_so);
+ free(buf);
+ Dest->Type = strdup( tmpType );
+ Dest->Desc = strdup( tmpDesc );
+ }
+
+ return 0;
+}