2057a5d41d9fd9e8804ade0a67e6ca6f2a6f3b41
[tpg/opendispense2.git] / src / cokebank.h
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * cokebank.h - Coke-Bank common definitions
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
8  * for full details.
9  */
10 /**
11  * \file cokebank.h
12  * \brief Coke Bank API Documentation
13  */
14 #ifndef _COKEBANK_H_
15 #define _COKEBANK_H_
16
17 #include <stdlib.h>
18
19 #define COKEBANK_SALES_ACCT     ">sales"        //!< Sales made into
20 #define COKEBANK_SALES_PREFIX   ">sales:"       //!< Sales made into
21 #define COKEBANK_DEBT_ACCT      ">liability"    //!< Credit taken out of
22 #define COKEBANK_FREE_ACCT      ">freeitems"    //!< ODay drink costs taken out of
23
24 /**
25  * \brief Account iterator opaque structure
26  *
27  * Opaque structure for account iterators returned by Bank_Iterator
28  * and used by Bank_IteratorNext and Bank_DelIterator
29  */
30 typedef struct sAcctIterator    tAcctIterator;
31
32 #if 0
33 /**
34  * \brief Iterator for a collection of items
35  */
36 typedef struct sItemIterator    tItemIterator;
37
38 /**
39  * \brief Item structure
40  */
41 typedef struct
42 {
43         char    *Handler;
44          int    ID;
45          int    Price;
46         char    Name[];
47 }       tItem;
48 #endif
49
50 /**
51  * \brief Flag values for the \a Flags parameter to Bank_Iterator
52  */
53 enum eBank_ItFlags
54 {
55         BANK_ITFLAG_MINBALANCE  = 0x01, //!< Balance value is Minium Balance
56         BANK_ITFLAG_MAXBALANCE  = 0x02, //!< Balance value is Maximum Balance (higher priority)
57         BANK_ITFLAG_SEENAFTER   = 0x04, //!< Last seen value is lower bound
58         BANK_ITFLAG_SEENBEFORE  = 0x08, //!< Last seen value is upper bound (higher priority)
59         
60         BANK_ITFLAG_SORT_NONE   = 0x000,        //!< No sorting (up to the implementation)
61         BANK_ITFLAG_SORT_NAME   = 0x100,        //!< Sort alphabetically ascending by name
62         BANK_ITFLAG_SORT_BAL    = 0x200,        //!< Sort by balance, ascending
63         BANK_ITFLAG_SORT_UNIXID = 0x300,        //!< Sort by UnixUID (TODO: Needed?)
64         BANK_ITFLAG_SORT_LASTSEEN = 0x400,      //!< Sort by last seen time (ascending)
65         BANK_ITFLAG_SORTMASK    = 0x700,        //!< Sort type mask
66         BANK_ITFLAG_REVSORT     = 0x800 //!< Sort descending instead
67 };
68 /**
69  * \brief Flag values for the \a Flags parameter to Items_Iterator
70  */
71 enum eItems_ItFlags
72 {
73         ITEMS_ITFLAG_SHOWDISABLED = 0x001,      //!< Show disabled items
74         ITEMS_ITFLAG_SORT_NONE  = 0x000,        //!< No sorting (up to the implementation)
75         ITEMS_ITFLAG_SORT_NAME  = 0x100,        //!< Sort alphabetically ascending by name
76         ITEMS_ITFLAG_SORT_PRICE = 0x200,        //!< Sort by price, ascending
77         ITEMS_ITFLAG_SORT_IDENT = 0x300,        //!< Sort by Identifier (handler:id)
78         ITEMS_ITFLAG_SORTMASK   = 0x700,        //!< Sort type mask
79         ITEMS_ITFLAG_REVSORT    = 0x800 //!< Sort descending instead
80 };
81
82 /**
83  * \brief User flag values
84  *
85  * User flag values used by Bank_GetFlags and Bank_SetFlags
86  */
87 enum eCokebank_Flags {  
88         USER_FLAG_COKE          = 0x01, //!< User is a coke member (can do coke accounting)
89         USER_FLAG_ADMIN         = 0x02, //!< User is a administrator (can create, delete and lock accounts)
90         USER_FLAG_DOORGROUP     = 0x04, //!< User is in the door group (can open the clubroom door)
91         USER_FLAG_INTERNAL      = 0x40, //!< Account is internal (cannot be authenticated, no lower balance limit)
92         USER_FLAG_DISABLED      = 0x80  //!< Account is disabled (no transactions allowed)
93 };
94
95 // --- Cokebank Functions ---
96 /**
97  * \brief Initialise the cokebank
98  * \param Argument      Cokebank argument specified in the dispense config file (typically the database path)
99  * \return Boolean Failure
100  */
101 extern int      Bank_Initialise(const char *Argument);
102
103 /**
104  * \brief Transfer money from one account to another
105  * \param SourceAcct    UID (from \a Bank_GetUserID) to take the money from
106  * \param DestAcct      UID (from \a Bank_GetUserID) give money to
107  * \param Ammount       Amount of money (in cents) to transfer
108  * \param Reason        Reason for the transfer
109  */
110 extern int      Bank_Transfer(int SourceAcct, int DestAcct, int Ammount, const char *Reason);
111 /**
112  * \brief Get flags on an account
113  * \param AcctID        UID to get flags from
114  * \return Flag set as defined in eCokebank_Flags
115  */
116 extern int      Bank_GetFlags(int AcctID);
117 /**
118  * \brief Set an account's flags
119  * \param AcctID        UID to set flags on
120  * \param Mask  Mask of flags changed
121  * \param Value Final value of changed flags
122  */
123 extern int      Bank_SetFlags(int AcctID, int Mask, int Value);
124 /**
125  * \brief Get an account's balance
126  * \param AcctID        Account to query
127  */
128 extern int      Bank_GetBalance(int AcctID);
129 /**
130  * \brief Get the name associated with an account
131  * \return Heap string
132  */
133 extern char     *Bank_GetAcctName(int AcctID);
134 /**
135  * \brief Get an account ID from a passed name
136  * \param Name  Name to search for
137  * \return ID of the account, or -1 if not found
138  */
139 extern int      Bank_GetAcctByName(const char *Name, int bCreate);
140 /**
141  * \brief Create a new account
142  * \param Name  Name for the new account (if NULL, an anoymous account is created)
143  * \return ID of the new account
144  */
145 extern int      Bank_CreateAcct(const char *Name);
146
147 /**
148  * \brief Create an account iterator
149  * \param FlagMask      Mask of account flags to check
150  * \param FlagValues    Wanted values for checked flags
151  * \param Flags Specifies the operation of \a MinMaxBalance and \a Timestamp  (\see eBank_ItFlags)
152  * \param MinMaxBalance Mininum/Maximum balance
153  * \param LastSeen      Latest/Earliest last seen time
154  * \return Pointer to an iterator across the selected data set
155  */
156 extern tAcctIterator    *Bank_Iterator(int FlagMask, int FlagValues,
157         int Flags, int MinMaxBalance, time_t LastSeen);
158
159 /**
160  * \brief Get the current entry in the iterator and move to the next
161  * \param It    Iterator returned by Bank_Iterator
162  * \return Accoun ID, or -1 for end of list
163  */
164 extern int      Bank_IteratorNext(tAcctIterator *It);
165
166 /**
167  * \brief Free an allocated iterator
168  * \param It    Iterator returned by Bank_Iterator
169  */
170 extern void     Bank_DelIterator(tAcctIterator *It);
171
172 /**
173  * \brief Validates a user's authentication
174  * \param Salt  Salt given to the client for hashing the password
175  * \param Username      Username used
176  * \param Password      Password sent by the client
177  * \return User ID
178  */
179 extern int      Bank_GetUserAuth(const char *Salt, const char *Username, const char *Password);
180
181 /**
182  * \brief Get an account ID from a MIFARE card ID
183  * \param CardID        MIFARE card ID
184  * \return Account ID
185  */
186 extern int      Bank_GetAcctByCard(const char *CardID);
187
188 /**
189  * \brief Add a card to an account
190  * \param AcctID        Account ID
191  * \param CardID        MIFARE card ID
192  * \return Boolean failure
193  * \retval 0    Success
194  * \retval 1    Bad account ID
195  * \retval 2    Card in use
196  */
197 extern int      Bank_AddAcctCard(int AcctID, const char *CardID);
198
199 // === Item Manipulation ===
200 #if 0
201 extern tItem    *Items_GetItem(char *Handler, int ID);
202 /**
203  * \brief Create an item iterator
204  * \return Pointer to an iterator across the selected data set
205  */
206 extern tItemIterator    *Items_Iterator(int Flags, char *Handler, int MaxPrice);
207
208 /**
209  * \brief Get the current entry in the iterator and move to the next
210  * \param It    Iterator returned by Items_Iterator
211  * \return Item ID, or -1 for end of list
212  */
213 extern tItem    *Items_IteratorNext(tItemIterator *It);
214
215 /**
216  * \brief Free an allocated iterator
217  * \param It    Iterator returned by Items_Iterator
218  */
219 extern void     Items_DelIterator(tItemIterator *It);
220 #endif
221
222 // ---
223 // Server provided helper functions
224 // ---
225 /**
226  * \brief Create a formatted string on the heap
227  * \param Format        Format string
228  * \return Heap string
229  */
230 extern char     *mkstr(const char *Format, ...);
231
232 /**
233  * \brief Dispense log access
234  * \note Try not to over-use, only stuff that matters should go in here
235  */
236 extern void     Log_Info(const char *Format, ...);
237
238 #endif

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