From: John Hodge Date: Wed, 5 Jan 2011 08:49:39 +0000 (+0800) Subject: Implemented `dispense add` in server X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=41eba5abceaa60bac8b33025f765d798f26e2062;p=tpg%2Fopendispense2.git Implemented `dispense add` in server --- diff --git a/proto.txt b/proto.txt index e68ed76..8c1dbed 100644 --- a/proto.txt +++ b/proto.txt @@ -43,8 +43,11 @@ s 200 Dispense OK\n or 402 Poor You\n or 500 Dispense Error\n or 406 Bad Item\n --- Give to another user --- c GIVE \n s 200 Give OK\n or 402 Poor You\n or 404 Bad User\n ---- Update balance --- +--- 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 Add OK\n or 403 Not Coke\n or 404 Bad User\n --- Get Item list --- diff --git a/src/server/common.h b/src/server/common.h index 280cd4e..ded278b 100644 --- a/src/server/common.h +++ b/src/server/common.h @@ -77,6 +77,7 @@ extern char *mkstr(const char *Format, ...); // --- Dispense --- extern int DispenseItem(int User, tItem *Item); extern int DispenseGive(int SrcUser, int DestUser, int Ammount, const char *ReasonGiven); +extern int DispenseAdd(int User, int ByUser, int Ammount, const char *ReasonGiven); // --- Logging --- extern void Log_Error(const char *Format, ...); diff --git a/src/server/dispense.c b/src/server/dispense.c index d574a1e..ae93793 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -47,9 +47,9 @@ int DispenseItem(int User, tItem *Item) } // And log that it happened - Log_Info("%s dispensed %s (%s:%i) [cost %i, balance %i cents]", - username, Item->Name, handler->Name, Item->ID, - Item->Price, GetBalance(User) + Log_Info("dispense %s (%s:%i) by %s [cost %i, balance %i cents]", + Item->Name, handler->Name, Item->ID, + username, Item->Price, GetBalance(User) ); free( username ); @@ -62,13 +62,28 @@ int DispenseItem(int User, tItem *Item) int DispenseGive(int SrcUser, int DestUser, int Ammount, const char *ReasonGiven) { int ret; - if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch + if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch! ret = Transfer( SrcUser, DestUser, Ammount, ReasonGiven ); if(ret) return 2; // No Balance - Log_Info("%s gave %i to %s (%s)", - GetUserName(SrcUser), Ammount, GetUserName(DestUser), ReasonGiven + Log_Info("give %i to %s from %s (%s)", + Ammount, GetUserName(DestUser), GetUserName(SrcUser), ReasonGiven + ); + + return 0; +} + +int DispenseAdd(int User, int ByUser, int Ammount, const char *ReasonGiven) +{ + int ret; + + ret = Transfer( GetUserID(">liability"), User, Ammount, ReasonGiven ); + + if(ret) return 2; + + Log_Info("add %i to %s by %s (%s)", + Ammount, GetUserName(User), GetUserName(ByUser), ReasonGiven ); return 0; diff --git a/src/server/server.c b/src/server/server.c index 35f16eb..be15a40 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -55,6 +55,8 @@ char *Server_Cmd_AUTOAUTH(tClient *Client, char *Args); char *Server_Cmd_ENUMITEMS(tClient *Client, char *Args); char *Server_Cmd_ITEMINFO(tClient *Client, char *Args); char *Server_Cmd_DISPENSE(tClient *Client, char *Args); +char *Server_Cmd_GIVE(tClient *Client, char *Args); +char *Server_Cmd_ADD(tClient *Client, char *Args); // --- Helpers --- int GetUserAuth(const char *Salt, const char *Username, const uint8_t *Hash); void HexBin(uint8_t *Dest, char *Src, int BufSize); @@ -72,7 +74,9 @@ struct sClientCommand { {"AUTOAUTH", Server_Cmd_AUTOAUTH}, {"ENUM_ITEMS", Server_Cmd_ENUMITEMS}, {"ITEM_INFO", Server_Cmd_ITEMINFO}, - {"DISPENSE", Server_Cmd_DISPENSE} + {"DISPENSE", Server_Cmd_DISPENSE}, + {"GIVE", Server_Cmd_GIVE}, + {"ADD", Server_Cmd_ADD} }; #define NUM_COMMANDS (sizeof(gaServer_Commands)/sizeof(gaServer_Commands[0])) int giServer_Socket; @@ -523,6 +527,45 @@ char *Server_Cmd_GIVE(tClient *Client, char *Args) } } +char *Server_Cmd_ADD(tClient *Client, char *Args) +{ + char *user, *ammount, *reason; + int uid, iAmmount; + + if( !Client->bIsAuthed ) return strdup("401 Not Authenticated\n"); + + user = Args; + + ammount = strchr(Args, ' '); + if( !ammount ) return strdup("407 Invalid Argument, expected 3 parameters, 1 encountered\n"); + *ammount = '\0'; + ammount ++; + + reason = strchr(ammount, ' '); + if( !reason ) return strdup("407 Invalid Argument, expected 3 parameters, 2 encountered\n"); + *reason = '\0'; + reason ++; + + // Get recipient + uid = GetUserID(user); + if( uid == -1 ) return strdup("404 Invalid user"); + + // Parse ammount + iAmmount = atoi(ammount); + if( iAmmount == 0 && ammount[0] != '0' ) return strdup("407 Invalid Argument, ammount must be > zero\n"); + + // Do give + switch( DispenseAdd(uid, Client->UID, iAmmount, reason) ) + { + case 0: + return strdup("200 Add OK\n"); + case 2: + return strdup("402 Poor Guy\n"); + default: + return strdup("500 Unknown error\n"); + } +} + /** * \brief Authenticate a user * \return User ID, or -1 if authentication failed