Fixing basic cokebank to actually work
[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 "common.h"
15
16 // === IMPORTS ===
17 extern int      Bank_GetMinAllowedBalance(int ID);
18 extern int      Bank_GetUserBalance(int ID);
19 extern int      Bank_AlterUserBalance(int ID, int Delta);
20 extern int      Bank_GetUserByUnixID(int UnixID);
21 extern int      Bank_GetUserUnixID(int ID);
22 extern int      Bank_AddUser(int UnixID);
23 extern FILE     *gBank_File;
24 extern tUser    *gaBank_Users;
25 extern int      giBank_NumUsers;
26
27 // === PROTOTYPES ===
28 void    Init_Cokebank(const char *Argument);
29  int    Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
30  int    GetBalance(int User);
31 char    *GetUserName(int User);
32  int    GetUserID(const char *Username); 
33  int    GetUserAuth(const char *Username, const char *Password);
34
35 // === CODE ===
36 /**
37  * \brief Load the cokebank database
38  */
39 void Init_Cokebank(const char *Argument)
40 {
41         gBank_File = fopen(Argument, "rb+");
42         if( !gBank_File ) {
43                 gBank_File = fopen(Argument, "wb+");
44         }
45         if( !gBank_File ) {
46                 perror("Opening coke bank");
47         }
48
49         fseek(gBank_File, 0, SEEK_END);
50         giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
51         fseek(gBank_File, 0, SEEK_SET);
52         gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
53         fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
54 }
55
56 /**
57  * \brief Transfers money from one user to another
58  * \param SourceUser    Source user
59  * \param DestUser      Destination user
60  * \param Ammount       Ammount of cents to move from \a SourceUser to \a DestUser
61  * \param Reason        Reason for the transfer (essentially a comment)
62  * \return Boolean failure
63  */
64 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
65 {
66         if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
67                 return 1;
68         if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) )
69                 return 1;
70         Bank_AlterUserBalance(DestUser, Ammount);
71         Bank_AlterUserBalance(SourceUser, -Ammount);
72         return 0;
73 }
74
75 /**
76  * \brief Get the balance of the passed user
77  */
78 int GetBalance(int User)
79 {
80         return 0;
81 }
82
83 /**
84  * \brief Return the name the passed user
85  */
86 char *GetUserName(int User)
87 {
88         struct passwd   *pwd;
89          int    unixid = Bank_GetUserUnixID(User);
90         
91         if( unixid == -1 )
92                 return strdup(">sales");
93
94         if( unixid == -2 )
95                 return strdup(">liability");
96
97         pwd = getpwuid(unixid);
98         if( !pwd )      return NULL;
99
100         return strdup(pwd->pw_name);
101 }
102
103 /**
104  * \brief Get the User ID of the named user
105  */
106 int GetUserID(const char *Username)
107 {
108          int    ret, uid;
109
110         if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
111                 uid = -1;
112         }
113         else if( strcmp(Username, ">liability") == 0 ) {        // Pseudo acount that money is added from
114                 uid = -2;
115         }
116         else {
117                 struct passwd   *pwd;
118                 // Get user ID
119                 pwd = getpwnam(Username);
120                 if( !pwd )      return -1;
121                 uid = pwd->pw_uid;
122         }
123
124         // Get internal ID (or create new user)
125         ret = Bank_GetUserByUnixID(uid);
126         if( ret == -1 ) {
127                 ret = Bank_AddUser(uid);
128         }
129
130         return ret;
131 }
132
133 /**
134  * \brief Authenticate a user
135  * \return User ID, or -1 if authentication failed
136  */
137 int GetUserAuth(const char *Username, const char *Password)
138 {
139         #if HACK_TPG_NOAUTH
140         if( strcmp(Username, "tpg") == 0 )
141                 return GetUserID("tpg");
142         #endif
143         return -1;
144 }
145

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