From: John Hodge Date: Fri, 25 Mar 2011 06:35:32 +0000 (+0800) Subject: Multiple fixes X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=32b161c885d90286f7d4a72b9d05e947c96069e5;p=tpg%2Fopendispense2.git Multiple fixes - Client fixed to pass error messages correctly - Fiddling with client ReadLine (was infinite looping, should be fixed now) - Fixed server to start the timer thread in the main process (not the parent) - Added a hack to detect refund `dispense acct` messages and reject them --- diff --git a/src/client/Makefile b/src/client/Makefile index 66c21fa..fa56d77 100644 --- a/src/client/Makefile +++ b/src/client/Makefile @@ -16,8 +16,8 @@ clean: $(BIN): $(OBJ) $(CC) -o $(BIN) $(OBJ) $(LDFLAGS) - chown root $(BIN) - chmod u+s $(BIN) +# chown root $(BIN) +# chmod u+s $(BIN) %.o: %.c $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) diff --git a/src/client/main.c b/src/client/main.c index fc7c19f..5601c08 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -1684,7 +1684,7 @@ int DispenseItem(int Socket, const char *Type, int ID) int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const char *Reason) { char *buf; - int responseCode; + int responseCode, rv = -1; // Check for a dry run if( gbDryRun ) { @@ -1702,26 +1702,32 @@ int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const c buf = ReadLine(Socket); responseCode = atoi(buf); - free(buf); switch(responseCode) { - case 200: return 0; // OK + case 200: + rv = 0; // OK + break; case 402: fprintf(stderr, "Insufficient balance\n"); - return RV_BAD_ITEM; + rv = RV_BAD_ITEM; + break; case 403: // Not in coke fprintf(stderr, "You are not in coke (sucker)\n"); - return RV_PERMISSIONS; + rv = RV_PERMISSIONS; + break; case 404: // Unknown user fprintf(stderr, "Unknown user '%s'\n", Username); - return RV_INVALID_USER; + rv = RV_INVALID_USER; + break; default: - fprintf(stderr, "Unknown response code %i\n", responseCode); - return RV_UNKNOWN_RESPONSE; + fprintf(stderr, "Unknown response code %i\n'%s'\n", responseCode, buf); + rv = RV_UNKNOWN_RESPONSE; + break; } + free(buf); - return -1; + return rv; } /** @@ -2210,8 +2216,8 @@ char *ReadLine(int Socket) free(ret); return strdup("499 Client Connection Error\n"); } - buf[bufPos+len] = '\0'; } + buf[bufPos+len] = '\0'; newline = strchr( buf+bufPos, '\n' ); if( newline ) { diff --git a/src/server/common.h b/src/server/common.h index a216dd5..19337e5 100644 --- a/src/server/common.h +++ b/src/server/common.h @@ -33,6 +33,7 @@ struct sItem char *Name; //!< Display Name int Price; //!< Price int bHidden; //!< Hidden item? + int bDisabledi; //!< Disabled Item tHandler *Handler; //!< Handler for the item short ID; //!< Item ID @@ -75,6 +76,7 @@ extern int giDebugLevel; extern void Items_UpdateFile(void); // --- Helpers -- +extern void StartPeriodicThread(void); extern void AddPeriodicFunction(void (*Fcn)(void)); extern void CompileRegex(regex_t *Regex, const char *Pattern, int Flags); extern int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage); diff --git a/src/server/itemdb.c b/src/server/itemdb.c index 18a1487..49fb743 100644 --- a/src/server/itemdb.c +++ b/src/server/itemdb.c @@ -81,7 +81,7 @@ void ItemList_Changed(int signum) #endif /** - * \brief Read the initiali item list + * \brief Read the initial item list */ void Load_Itemlist(void) { diff --git a/src/server/main.c b/src/server/main.c index d6fefbc..a8d8d32 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -43,6 +43,7 @@ char *gsCokebankPath = "cokebank.db"; struct sPeriodicCall { void (*Function)(void); } gaPeriodicCalls[ciMaxPeriodics]; +pthread_t gTimerThread; // === CODE === void sigint_handler() @@ -68,7 +69,6 @@ void PrintUsage(const char *progname) int main(int argc, char *argv[]) { int i; - pthread_t timer_thread; // Parse Arguments for( i = 1; i < argc; i++ ) @@ -130,7 +130,7 @@ int main(int argc, char *argv[]) gbServer_RunInBackground = 1; } else if( strcmp(arg, "--dont-daemonise") == 0 ) { - gbServer_RunInBackground = 1; + gbServer_RunInBackground = 0; } else { // Usage error? @@ -157,11 +157,9 @@ int main(int argc, char *argv[]) Load_Itemlist(); - pthread_create( &timer_thread, NULL, Periodic_Thread, NULL ); - Server_Start(); - pthread_kill(timer_thread, SIGKILL); + pthread_kill(gTimerThread, SIGKILL); return 0; } @@ -184,6 +182,11 @@ void *Periodic_Thread(void *Unused) return NULL; } +void StartPeriodicThread(void) +{ + pthread_create( &gTimerThread, NULL, Periodic_Thread, NULL ); +} + void AddPeriodicFunction(void (*Fcn)(void)) { int i; diff --git a/src/server/server.c b/src/server/server.c index b34d93d..f663ae5 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -21,6 +21,7 @@ #include #define DEBUG_TRACE_CLIENT 0 +#define HACK_NO_REFUNDS 1 // Statistics #define MAX_CONNECTION_QUEUE 5 @@ -171,6 +172,9 @@ void Server_Start(void) dup2(newout, 1); dup2(newerr, 2); } + + // Start the helper thread + StartPeriodicThread(); // Listen if( listen(giServer_Socket, MAX_CONNECTION_QUEUE) < 0 ) { @@ -910,6 +914,14 @@ void Server_Cmd_ADD(tClient *Client, char *Args) return ; } + #if HACK_NO_REFUNDS + if( strstr(reason, "refund") != NULL || strstr(reason, "misdispense") != NULL ) + { + sendf(Client->Socket, "499 Don't use `dispense acct` for refunds, use `dispense refund` (and `dispense -G` to get item IDs)\n"); + return ; + } + #endif + // Get recipient uid = Bank_GetAcctByName(user, 0); if( uid == -1 ) {