ae111d4d88ecd2e226f6c118705247fc5771ecde
[tpg/opendispense2.git] / src / cokebank_basic / main.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * cokebank.c - Coke-Bank management
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
8  * for full details.
9  */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <pwd.h>
13 #include <string.h>
14 #include <openssl/sha.h>
15 #include "common.h"
16
17 // === IMPORTS ===
18 extern int      Bank_GetMinAllowedBalance(int ID);
19 extern int      Bank_GetUserBalance(int ID);
20 extern int      Bank_AlterUserBalance(int ID, int Delta);
21 extern int      Bank_GetUserByUnixID(int UnixID);
22 extern int      Bank_GetUserUnixID(int ID);
23 extern int      Bank_AddUser(int UnixID);
24 extern FILE     *gBank_File;
25 extern tUser    *gaBank_Users;
26 extern int      giBank_NumUsers;
27
28 // === PROTOTYPES ===
29 void    Init_Cokebank(const char *Argument);
30  int    Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
31  int    GetBalance(int User);
32 char    *GetUserName(int User);
33  int    GetUserID(const char *Username); 
34  int    GetUserAuth(const char *Username, const char *Password);
35
36 // === CODE ===
37 /**
38  * \brief Load the cokebank database
39  */
40 void Init_Cokebank(const char *Argument)
41 {
42         gBank_File = fopen(Argument, "rb+");
43         if( !gBank_File ) {
44                 gBank_File = fopen(Argument, "wb+");
45         }
46         if( !gBank_File ) {
47                 perror("Opening coke bank");
48         }
49
50         fseek(gBank_File, 0, SEEK_END);
51         giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
52         fseek(gBank_File, 0, SEEK_SET);
53         gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
54         fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
55 }
56
57 /**
58  * \brief Transfers money from one user to another
59  * \param SourceUser    Source user
60  * \param DestUser      Destination user
61  * \param Ammount       Ammount of cents to move from \a SourceUser to \a DestUser
62  * \param Reason        Reason for the transfer (essentially a comment)
63  * \return Boolean failure
64  */
65 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
66 {
67         if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
68                 return 1;
69         if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) )
70                 return 1;
71         Bank_AlterUserBalance(DestUser, Ammount);
72         Bank_AlterUserBalance(SourceUser, -Ammount);
73         return 0;
74 }
75
76 /**
77  * \brief Get the balance of the passed user
78  */
79 int GetBalance(int User)
80 {
81         return Bank_GetUserBalance(User);;
82 }
83
84 /**
85  * \brief Return the name the passed user
86  */
87 char *GetUserName(int User)
88 {
89         struct passwd   *pwd;
90          int    unixid = Bank_GetUserUnixID(User);
91         
92         if( unixid == -1 )
93                 return strdup(">sales");
94
95         if( unixid == -2 )
96                 return strdup(">liability");
97
98         pwd = getpwuid(unixid);
99         if( !pwd )      return NULL;
100
101         return strdup(pwd->pw_name);
102 }
103
104 /**
105  * \brief Get the User ID of the named user
106  */
107 int GetUserID(const char *Username)
108 {
109          int    ret, uid;
110
111         if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
112                 uid = -1;
113         }
114         else if( strcmp(Username, ">liability") == 0 ) {        // Pseudo acount that money is added from
115                 uid = -2;
116         }
117         else {
118                 struct passwd   *pwd;
119                 // Get user ID
120                 pwd = getpwnam(Username);
121                 if( !pwd )      return -1;
122                 uid = pwd->pw_uid;
123         }
124
125         // Get internal ID (or create new user)
126         ret = Bank_GetUserByUnixID(uid);
127         if( ret == -1 ) {
128                 ret = Bank_AddUser(uid);
129         }
130
131         return ret;
132 }
133

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