From: John Hodge Date: Fri, 7 Jan 2011 08:27:45 +0000 (+0800) Subject: Completed removing SET, implemented GIVE X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=3b0d5c732ceb17236bd93ebc2fe718ec1b2b3080;p=tpg%2Fopendispense2.git Completed removing SET, implemented GIVE --- diff --git a/proto.txt b/proto.txt index 18a5416..c27ff06 100644 --- a/proto.txt +++ b/proto.txt @@ -49,9 +49,6 @@ s 200 Give OK\n or 402 Poor You\n or 404 Bad User\n --- Alter balance --- c ADD \n s 200 Add OK\n or 402 No balance\n or 403 Not Coke\n or 404 Bad User\n ---- Set balance --- -c SET \n -s 200 Set OK\n or 403 Not Coke\n or 404 Bad User\n --- Get Item list --- c ENUM_ITEMS\n diff --git a/src/client/main.c b/src/client/main.c index 17971dc..787eb97 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -48,7 +48,7 @@ void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1, const void PopulateItemList(int Socket); int DispenseItem(int Socket, int ItemID); int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const char *Reason); - int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const char *Reason); + int Dispense_Give(int Socket, const char *Username, int Ammount, const char *Reason); int Dispense_EnumUsers(int Socket); int Dispense_ShowUser(int Socket, const char *Username); void _PrintUserLine(const char *Line); @@ -156,14 +156,8 @@ int main(int argc, char *argv[]) // argv[i+2]: Ammount // argv[i+3]: Reason - if( argv[i+2][0] == '=' ) { - // Set balance - Dispense_SetBalance(sock, argv[i+1], atoi(argv[i+2] + 1), argv[i+3]); - } - else { - // Alter balance - Dispense_AlterBalance(sock, argv[i+1], atoi(argv[i+2]), argv[i+3]); - } + // Alter balance + Dispense_AlterBalance(sock, argv[i+1], atoi(argv[i+2]), argv[i+3]); } // Show user information @@ -172,12 +166,36 @@ int main(int argc, char *argv[]) close(sock); return 0; } + // + // `dispense give` + // - "Here, have some money." else if( strcmp(arg, "give") == 0 ) { + if( i + 3 >= argc ) { + fprintf(stderr, "`dispense give` takes three arguments\n"); + ShowUsage(); + return -1; + } // TODO: `dispense give` - printf("TODO: Implement `dispense give`\n"); + + // argv[i+1]: Destination + // argv[i+2]: Ammount + // argv[i+3]: Reason + + // Connect to server + sock = OpenConnection(gsDispenseServer, giDispensePort); + if( sock < 0 ) return -1; + + // Authenticate + if( Authenticate(sock) ) + return -1; + + Dispense_Give(sock, argv[i+1], atoi(argv[i+2]), argv[i+3]); return 0; } + // + // `dispense user` + // - User administration (Wheel Only) else if( strcmp(arg, "user") == 0 ) { // Check argument count @@ -932,6 +950,9 @@ int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const c switch(responseCode) { case 200: return 0; // OK + case 402: + fprintf(stderr, "Insufficient balance\n"); + return 1; case 403: // Not in coke fprintf(stderr, "You are not in coke (sucker)\n"); return 1; @@ -949,12 +970,23 @@ int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const c /** * \brief Alter a user's balance */ -int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const char *Reason) +int Dispense_Give(int Socket, const char *Username, int Ammount, const char *Reason) { char *buf; int responseCode; - sendf(Socket, "SET %s %i %s\n", Username, Ammount, Reason); + 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; + } + + sendf(Socket, "GIVE %s %i %s\n", Username, Ammount, Reason); buf = ReadLine(Socket); responseCode = atoi(buf); @@ -963,12 +995,15 @@ int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const cha switch(responseCode) { case 200: return 0; // OK - case 403: // Not in coke - fprintf(stderr, "You are not in coke (sucker)\n"); + + case 402: + fprintf(stderr, "Insufficient balance\n"); return 1; + case 404: // Unknown user fprintf(stderr, "Unknown user '%s'\n", Username); return 2; + default: fprintf(stderr, "Unknown response code %i\n", responseCode); return -1; diff --git a/src/server/dispense.c b/src/server/dispense.c index 7b8f9d4..3c18b58 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -79,7 +79,7 @@ int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const c actualUsername = GetUserName(ActualUser); Log_Info("give %i to %s from %s by %s (%s)", - Ammount, srcName, dstName, actualUsername, ReasonGiven + Ammount, dstName, srcName, actualUsername, ReasonGiven ); free(srcName); diff --git a/src/server/server.c b/src/server/server.c index 330ac66..9469b60 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -67,7 +67,6 @@ void Server_Cmd_USERADD(tClient *Client, char *Args); void Server_Cmd_USERFLAGS(tClient *Client, char *Args); // --- Helpers --- int sendf(int Socket, const char *Format, ...); -void HexBin(uint8_t *Dest, char *Src, int BufSize); // === CONSTANTS === // - Commands @@ -928,46 +927,3 @@ int sendf(int Socket, const char *Format, ...) return send(Socket, buf, len, 0); } } - -/** - * \brief Decode a Base64 value - */ -int UnBase64(uint8_t *Dest, char *Src, int BufSize) -{ - uint32_t val; - int i, j; - char *start_src = Src; - - for( i = 0; i+2 < BufSize; i += 3 ) - { - val = 0; - for( j = 0; j < 4; j++, Src ++ ) { - if('A' <= *Src && *Src <= 'Z') - val |= (*Src - 'A') << ((3-j)*6); - else if('a' <= *Src && *Src <= 'z') - val |= (*Src - 'a' + 26) << ((3-j)*6); - else if('0' <= *Src && *Src <= '9') - val |= (*Src - '0' + 52) << ((3-j)*6); - else if(*Src == '+') - val |= 62 << ((3-j)*6); - else if(*Src == '/') - val |= 63 << ((3-j)*6); - else if(!*Src) - break; - else if(*Src != '=') - j --; // Ignore invalid characters - } - Dest[i ] = (val >> 16) & 0xFF; - Dest[i+1] = (val >> 8) & 0xFF; - Dest[i+2] = val & 0xFF; - if(j != 4) break; - } - - // Finish things off - if(i < BufSize) - Dest[i] = (val >> 16) & 0xFF; - if(i+1 < BufSize) - Dest[i+1] = (val >> 8) & 0xFF; - - return Src - start_src; -}