3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * cokebank.c - Coke-Bank management
7 * This file is licenced under the 3-clause BSD Licence. See the file COPYING
16 #define HACK_TPG_NOAUTH 1
19 extern int Bank_GetMinAllowedBalance(int ID);
20 extern int Bank_GetUserBalance(int ID);
21 extern int Bank_AlterUserBalance(int ID, int Delta);
22 extern int Bank_GetUserByUnixID(int UnixID);
23 extern int Bank_GetUserUnixID(int ID);
24 extern int Bank_AddUser(int UnixID);
25 extern FILE *gBank_File;
26 extern tUser *gaBank_Users;
27 extern int giBank_NumUsers;
30 void Init_Cokebank(const char *Argument);
31 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
32 int GetBalance(int User);
33 char *GetUserName(int User);
34 int GetUserID(const char *Username);
35 int GetUserAuth(const char *Username, const char *Password);
39 * \brief Load the cokebank database
41 void Init_Cokebank(const char *Argument)
43 gBank_File = fopen(Argument, "rb+");
45 gBank_File = fopen(Argument, "wb+");
48 perror("Opening coke bank");
51 fseek(gBank_File, 0, SEEK_END);
52 giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
53 fseek(gBank_File, 0, SEEK_SET);
54 gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
55 fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
59 * \brief Transfers money from one user to another
60 * \param SourceUser Source user
61 * \param DestUser Destination user
62 * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
63 * \param Reason Reason for the transfer (essentially a comment)
64 * \return Boolean failure
66 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
68 if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
70 if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) )
72 Bank_AlterUserBalance(DestUser, Ammount);
73 Bank_AlterUserBalance(SourceUser, -Ammount);
78 * \brief Get the balance of the passed user
80 int GetBalance(int User)
86 * \brief Return the name the passed user
88 char *GetUserName(int User)
91 int unixid = Bank_GetUserUnixID(User);
94 return strdup(">sales");
97 return strdup(">liability");
99 pwd = getpwuid(unixid);
100 if( !pwd ) return NULL;
102 return strdup(pwd->pw_name);
106 * \brief Get the User ID of the named user
108 int GetUserID(const char *Username)
112 if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
115 else if( strcmp(Username, ">liability") == 0 ) { // Pseudo acount that money is added from
121 pwd = getpwnam(Username);
122 if( !pwd ) return -1;
126 // Get internal ID (or create new user)
127 ret = Bank_GetUserByUnixID(uid);
129 ret = Bank_AddUser(uid);
136 * \brief Authenticate a user
137 * \return User ID, or -1 if authentication failed
139 int GetUserAuth(const char *Username, const char *Password)
142 if( strcmp(Username, "tpg") == 0 )
143 return GetUserID("tpg");