Removed debug from server, cleaning client
[tpg/opendispense2.git] / src / cokebank_basic / bank.c
index cc09fa5..07b8271 100644 (file)
@@ -10,7 +10,9 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <limits.h>
+#include <pwd.h>
 #include "common.h"
 
 enum {
@@ -21,19 +23,25 @@ enum {
        USER_TYPE_GOD    = 0x03
 };
 
+// === PROTOTYPES ===
+static int     GetUnixID(const char *Username);
+
 // === GLOBALS ===
 tUser  *gaBank_Users;
  int   giBank_NumUsers;
 FILE   *gBank_File;
 
 // === CODE ===
-int Bank_GetUserByUnixID(int UnixID)
+int Bank_GetUserByName(const char *Username)
 {
-        int    i;
+        int    i, uid;
+       
+       uid = GetUnixID(Username);
+       
        // Expensive search :(
        for( i = 0; i < giBank_NumUsers; i ++ )
        {
-               if( gaBank_Users[i].UnixID == UnixID )
+               if( gaBank_Users[i].UnixID == uid )
                        return i;
        }
 
@@ -83,7 +91,7 @@ int Bank_SetUserBalance(int ID, int Value)
 int Bank_GetMinAllowedBalance(int ID)
 {
        if( ID < 0 || ID >= giBank_NumUsers )
-               return -1;
+               return 0;
 
        switch( gaBank_Users[ID].Flags & FLAG_TYPEMASK )
        {
@@ -95,20 +103,13 @@ int Bank_GetMinAllowedBalance(int ID)
        }
 }
 
-int Bank_GetUserUnixID(int ID)
-{
-       if( ID < 0 || ID >= giBank_NumUsers )
-               return -1;
-
-       return gaBank_Users[ID].UnixID;
-}
-
 /**
  * \brief Create a new user in our database
  */
-int Bank_AddUser(int UnixID)
+int Bank_AddUser(const char *Username)
 {
        void    *tmp;
+        int    uid = GetUnixID(Username);
 
        // Can has moar space plz?
        tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0]));
@@ -116,10 +117,17 @@ int Bank_AddUser(int UnixID)
        gaBank_Users = tmp;
 
        // Crete new user
-       gaBank_Users[giBank_NumUsers].UnixID = UnixID;
+       gaBank_Users[giBank_NumUsers].UnixID = uid;
        gaBank_Users[giBank_NumUsers].Balance = 0;
        gaBank_Users[giBank_NumUsers].Flags = 0;
        
+       if( strcmp(Username, ">liability") == 0 ) {
+               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+       }
+       else if( strcmp(Username, "root") == 0 ) {
+               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+       }
+       
        // Commit to file
        fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET);
        fwrite(&gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File);
@@ -129,3 +137,46 @@ int Bank_AddUser(int UnixID)
 
        return 0;
 }
+
+// ---
+// Unix user dependent code
+// TODO: Modify to keep its own list of usernames
+// ---
+char *Bank_GetUserName(int ID)
+{
+       struct passwd   *pwd;
+       
+       if( ID < 0 || ID >= giBank_NumUsers )
+               return NULL;
+       
+       if( gaBank_Users[ID].UnixID == -1 )
+               return strdup(">sales");
+
+       if( gaBank_Users[ID].UnixID == -2 )
+               return strdup(">liability");
+
+       pwd = getpwuid(gaBank_Users[ID].UnixID);
+       if( !pwd )      return NULL;
+
+       return strdup(pwd->pw_name);
+}
+
+static int GetUnixID(const char *Username)
+{
+        int    uid;
+
+       if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
+               uid = -1;
+       }
+       else if( strcmp(Username, ">liability") == 0 ) {        // Pseudo acount that money is added from
+               uid = -2;
+       }
+       else {
+               struct passwd   *pwd;
+               // Get user ID
+               pwd = getpwnam(Username);
+               if( !pwd )      return -1;
+               uid = pwd->pw_uid;
+       }
+       return uid;
+}

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