After a rage with TRS, allowed `dispense acct` to take -ve, disabled new background...
[tpg/opendispense2.git] / src / server / dispense.c
1 /**
2  */
3 #include "common.h"
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <string.h>
7
8  int    _GetMinBalance(int Account);
9  int    _CanTransfer(int Source, int Destination, int Ammount);
10  int    _Transfer(int Source, int Destination, int Ammount, const char *Reason);
11
12 // === CODE ===
13 /**
14  * \brief Dispense an item for a user
15  * 
16  * The core of the dispense system, I kinda like it :)
17  */
18 int DispenseItem(int ActualUser, int User, tItem *Item)
19 {
20          int    ret, salesAcct;
21         tHandler        *handler;
22         char    *username, *actualUsername;
23         
24         salesAcct = Bank_GetAcctByName(COKEBANK_SALES_ACCT);
25
26         // Check if the user can afford it
27         if( Item->Price && !_CanTransfer(User, salesAcct, Item->Price) )
28         {
29                 return 2;       // 2: No balance
30         }
31         
32         handler = Item->Handler;
33         
34         // Check if the dispense is possible
35         if( handler->CanDispense ) {
36                 ret = handler->CanDispense( User, Item->ID );
37                 if(ret) return 1;       // 1: Unable to dispense
38         }
39         
40         // Get username for debugging
41         username = Bank_GetAcctName(User);
42         
43         // Actually do the dispense
44         if( handler->DoDispense ) {
45                 ret = handler->DoDispense( User, Item->ID );
46                 if(ret) {
47                         Log_Error("Dispense failed (%s dispensing '%s' - %ic)",
48                                 username, Item->Name, Item->Price);
49                         free( username );
50                         return -1;      // 1: Unknown Error again
51                 }
52         }
53         
54         // Take away money
55         if( Item->Price )
56         {
57                 char    *reason;
58                 reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
59                 _Transfer( User, salesAcct, Item->Price, reason );
60                 free(reason);
61         }
62         
63         actualUsername = Bank_GetAcctName(ActualUser);
64         
65         // And log that it happened
66         Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i]",
67                 Item->Name, handler->Name, Item->ID,
68                 username, actualUsername, Item->Price, Bank_GetBalance(User)
69                 );
70         
71         free( username );
72         free( actualUsername );
73         return 0;       // 0: EOK
74 }
75
76 /**
77  * \brief Give money from one user to another
78  */
79 int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
80 {
81          int    ret;
82         char    *actualUsername;
83         char    *srcName, *dstName;
84         
85         if( Ammount < 0 )       return 1;       // Um... negative give? Not on my watch!
86         
87         ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
88         if(ret) return 2;       // No Balance
89         
90         
91         actualUsername = Bank_GetAcctName(ActualUser);
92         srcName = Bank_GetAcctName(SrcUser);
93         dstName = Bank_GetAcctName(DestUser);
94         
95         Log_Info("give %i to %s from %s by %s [balances %i, %i] - %s",
96                 Ammount, dstName, srcName, actualUsername,
97                 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
98                 ReasonGiven
99                 );
100         
101         free(srcName);
102         free(dstName);
103         free(actualUsername);
104         
105         return 0;
106 }
107
108 /**
109  * \brief Move money from one user to another (Admin Only)
110  */
111 int DispenseTransfer(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven)
112 {
113          int    ret;
114         char    *actualUsername;
115         char    *srcName, *dstName;
116
117         // Make sure the user is an admin
118         if( !(Bank_GetFlags(ActualUser) & USER_FLAG_ADMIN) )
119                 return 1;
120         
121         ret = _Transfer( SrcUser, DestUser, Ammount, ReasonGiven );
122         if(ret) return 2;       // No Balance
123         
124         
125         actualUsername = Bank_GetAcctName(ActualUser);
126         srcName = Bank_GetAcctName(SrcUser);
127         dstName = Bank_GetAcctName(DestUser);
128         
129         Log_Info("move %i to %s from %s by %s [balances %i, %i] - %s",
130                 Ammount, dstName, srcName, actualUsername,
131                 Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser),
132                 ReasonGiven
133                 );
134         
135         free(srcName);
136         free(dstName);
137         free(actualUsername);
138         
139         return 0;
140 }
141 /**
142  * \brief Add money to an account
143  */
144 int DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven)
145 {
146          int    ret;
147         char    *dstName, *byName;
148         
149 #if DISPENSE_ADD_BELOW_MIN
150 //      ret = _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
151 #else
152         ret = Bank_Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven );
153 #endif
154         if(ret) return 2;
155         
156         byName = Bank_GetAcctName(ActualUser);
157         dstName = Bank_GetAcctName(User);
158         
159         Log_Info("add %i to %s by %s [balance %i] - %s",
160                 Ammount, dstName, byName, Bank_GetBalance(User), ReasonGiven
161                 );
162         
163         free(byName);
164         free(dstName);
165         
166         return 0;
167 }
168
169 int DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven)
170 {
171          int    curBal = Bank_GetBalance(User);
172         char    *byName, *dstName;
173         
174         _Transfer( Bank_GetAcctByName(COKEBANK_DEBT_ACCT), User, Balance-curBal, ReasonGiven );
175         
176         byName = Bank_GetAcctName(ActualUser);
177         dstName = Bank_GetAcctName(User);
178         
179         Log_Info("set balance of %s to %i by %s [balance %i] - %s",
180                 dstName, Balance, byName, Bank_GetBalance(User), ReasonGiven
181                 );
182         
183         free(byName);
184         free(dstName);
185         
186         return 0;
187 }
188
189 /**
190  * \brief Donate money to the club
191  */
192 int DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGiven)
193 {
194          int    ret;
195         char    *srcName, *byName;
196         
197         if( Ammount < 0 )       return 2;
198         
199         ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_DEBT_ACCT), Ammount, ReasonGiven );
200         if(ret) return 2;
201         
202         byName = Bank_GetAcctName(ActualUser);
203         srcName = Bank_GetAcctName(User);
204         
205         Log_Info("donate %i from %s by %s [balance %i] - %s",
206                 Ammount, srcName, byName, Bank_GetBalance(User), ReasonGiven
207                 );
208         
209         free(byName);
210         free(srcName);
211         
212         return 0;
213 }
214
215 // --- Internal Functions ---
216 int _GetMinBalance(int Account)
217 {
218          int    flags = Bank_GetFlags(Account);
219         
220         // Evil little piece of HACK:
221         // root's balance cannot be changed by any of the above functions
222         // - Stops dispenses as root by returning insufficent balance.
223         {
224                 char    *username = Bank_GetAcctName(Account);
225                 if( strcmp(username, "root") == 0 )
226                 {
227                         free(username);
228                         return INT_MAX;
229                 }
230                 free(username);
231         }
232         
233         // - Internal accounts have no lower bound
234         if( flags & USER_FLAG_INTERNAL )        return INT_MIN;
235         
236         // Admin to -$50
237         if( flags & USER_FLAG_ADMIN )   return -5000;
238         
239         // Coke to -$20
240         if( flags & USER_FLAG_COKE )    return -2000;
241         
242         // Anyone else, non-negative
243         return 0;
244 }
245
246 /**
247  * \brief Check if a transfer is possible
248  */
249 int _CanTransfer(int Source, int Destination, int Ammount)
250 {
251         if( Ammount > 0 )
252         {
253                 if( Bank_GetBalance(Source) - Ammount < _GetMinBalance(Source) )
254                         return 0;
255         }
256         else
257         {
258                 if( Bank_GetBalance(Destination) + Ammount < _GetMinBalance(Destination) )
259                         return 0;
260         }
261         return 1;
262 }
263
264 int _Transfer(int Source, int Destination, int Ammount, const char *Reason)
265 {
266         if( !_CanTransfer(Source, Destination, Ammount) )
267                 return 1;
268         return Bank_Transfer(Source, Destination, Ammount, Reason);
269 }

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