Completed removing SET, implemented GIVE
authorJohn Hodge <[email protected]>
Fri, 7 Jan 2011 08:27:45 +0000 (16:27 +0800)
committerJohn Hodge <[email protected]>
Fri, 7 Jan 2011 08:27:45 +0000 (16:27 +0800)
proto.txt
src/client/main.c
src/server/dispense.c
src/server/server.c

index 18a5416..c27ff06 100644 (file)
--- 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 <user> <ammount> <reason>\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 <user> <ammount> <reason>\n
-s      200 Set OK\n or 403 Not Coke\n or 404 Bad User\n
 
 --- Get Item list ---
 c      ENUM_ITEMS\n
index 17971dc..787eb97 100644 (file)
@@ -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;
index 7b8f9d4..3c18b58 100644 (file)
@@ -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);
index 330ac66..9469b60 100644 (file)
@@ -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;
-}

UCC git Repository :: git.ucc.asn.au