Restuctured cokebank
[tpg/opendispense2.git] / src / cokebank_basic / bank.c
1 /*
2  * OpenDispense 2
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  * - Cokebank (Basic Version)
5  *
6  * bank.c - Actual bank database
7  *
8  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
9  * for full details.
10  */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include <pwd.h>
16 #include "common.h"
17
18 enum {
19         FLAG_TYPEMASK    = 0x03,
20         USER_TYPE_NORMAL = 0x00,
21         USER_TYPE_COKE   = 0x01,
22         USER_TYPE_WHEEL  = 0x02,
23         USER_TYPE_GOD    = 0x03
24 };
25
26 // === PROTOTYPES ===
27 static int      GetUnixID(const char *Name);
28
29 // === GLOBALS ===
30 tUser   *gaBank_Users;
31  int    giBank_NumUsers;
32 FILE    *gBank_File;
33
34 // === CODE ===
35 int Bank_GetUserByUnixID(int UnixID)
36 {
37          int    i;
38         // Expensive search :(
39         for( i = 0; i < giBank_NumUsers; i ++ )
40         {
41                 if( gaBank_Users[i].UnixID == UnixID )
42                         return i;
43         }
44
45         return -1;
46 }
47
48 int Bank_GetUserBalance(int ID)
49 {
50         if( ID < 0 || ID >= giBank_NumUsers )
51                 return INT_MIN;
52
53         return gaBank_Users[ID].Balance;
54 }
55
56 int Bank_AlterUserBalance(int ID, int Delta)
57 {
58         // Sanity
59         if( ID < 0 || ID >= giBank_NumUsers )
60                 return -1;
61
62         // Update
63         gaBank_Users[ID].Balance += Delta;
64
65         // Commit
66         fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
67         fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
68         
69         return 0;
70 }
71
72 int Bank_SetUserBalance(int ID, int Value)
73 {
74         // Sanity
75         if( ID < 0 || ID >= giBank_NumUsers )
76                 return -1;
77
78         // Update
79         gaBank_Users[ID].Balance = Value;
80         
81         // Commit
82         fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
83         fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
84         
85         return 0;
86 }
87
88 int Bank_GetMinAllowedBalance(int ID)
89 {
90         if( ID < 0 || ID >= giBank_NumUsers )
91                 return -1;
92
93         switch( gaBank_Users[ID].Flags & FLAG_TYPEMASK )
94         {
95         case USER_TYPE_NORMAL:  return     0;
96         case USER_TYPE_COKE:    return  -2000;
97         case USER_TYPE_WHEEL:   return -10000;
98         case USER_TYPE_GOD:     return INT_MIN;
99         default:        return 0;
100         }
101 }
102
103 /**
104  * \brief Create a new user in our database
105  */
106 int Bank_AddUser(const char *Username)
107 {
108         void    *tmp;
109          int    uid = GetUnixID(Username);
110
111         // Can has moar space plz?
112         tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0]));
113         if( !tmp )      return -1;
114         gaBank_Users = tmp;
115
116         // Crete new user
117         gaBank_Users[giBank_NumUsers].UnixID = uid;
118         gaBank_Users[giBank_NumUsers].Balance = 0;
119         gaBank_Users[giBank_NumUsers].Flags = 0;
120         
121         // Commit to file
122         fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET);
123         fwrite(&gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File);
124
125         // Increment count
126         giBank_NumUsers ++;
127
128         return 0;
129 }
130
131 // ---
132 // Unix user dependent code
133 // TODO: Modify to keep its own list of usernames
134 // ---
135 char *Bank_GetUserName(int ID)
136 {
137         struct passwd   *pwd;
138         
139         if( ID < 0 || ID >= giBank_NumUsers )
140                 return NULL;
141         
142         if( gaBank_Users[ID].UnixID == -1 )
143                 return strdup(">sales");
144
145         if( gaBank_Users[ID].UnixID == -2 )
146                 return strdup(">liability");
147
148         pwd = getpwuid(gaBank_Users[ID].UnixID);
149         if( !pwd )      return NULL;
150
151         return strdup(pwd->pw_name);
152 }
153
154 static int GetUnixID(const char *Username)
155 {
156          int    uid;
157
158         if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
159                 uid = -1;
160         }
161         else if( strcmp(Username, ">liability") == 0 ) {        // Pseudo acount that money is added from
162                 uid = -2;
163         }
164         else {
165                 struct passwd   *pwd;
166                 // Get user ID
167                 pwd = getpwnam(Username);
168                 if( !pwd )      return -1;
169                 uid = pwd->pw_uid;
170         }
171         return uid;
172 }

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