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

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