Door config settings (and some fixes to doorgroup reporting)
[tpg/opendispense2.git] / src / server / handler_door.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * handler_doror.c - Door Relay code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  */
10 #include "common.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16
17 #define DEBUG   1
18
19 #define DOOR_UNLOCKED_DELAY     10      // 10 seconds before it re-locks
20
21 // === IMPORTS ===
22
23 // === PROTOTYPES ===
24  int    Door_InitHandler();
25  int    Door_CanDispense(int User, int Item);
26  int    Door_DoDispense(int User, int Item);
27
28 // === GLOBALS ===
29 tHandler        gDoor_Handler = {
30         "door",
31         Door_InitHandler,
32         Door_CanDispense,
33         Door_DoDispense
34 };
35 char    *gsDoor_Password = "";
36 // int  giDoor_SerialFD;
37
38 // == CODE ===
39 int Door_InitHandler(void)
40 {       
41         return 0;
42 }
43
44 /**
45  */
46 int Door_CanDispense(int User, int Item)
47 {
48         #if DEBUG
49         printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
50         #endif
51         // Sanity please
52         if( Item != 0 ) return -1;
53         
54         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
55         {
56                 #if DEBUG
57                 printf("Door_CanDispense: User %i not in door\n", User);
58                 #endif
59                 return 1;
60         }
61         
62         #if DEBUG
63         printf("Door_CanDispense: User %i can open the door\n", User);
64         #endif
65         
66         return 0;
67 }
68
69 /**
70  * \brief Actually do a dispense from the coke machine
71  */
72 int Door_DoDispense(int User, int Item)
73 {
74         FILE    *pipe;
75         char    buf[512];       // Buffer flush location - the sewer :)
76         
77         #if DEBUG
78         printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
79         #endif
80         
81         // Sanity please
82         if( Item != 0 ) return -1;
83         
84         // Check if user is in door
85         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
86         {
87                 #if DEBUG
88                 printf("Door_CanDispense: User %i not in door\n", User);
89                 #endif
90                 return 1;
91         }
92         
93         // llogin or other
94         pipe = popen("llogin door -w -", "w");
95         if( !pipe || pipe == (void*)-1 ) {
96                 #if DEBUG
97                 printf("Door_DoDispense: llogin failure\n");
98                 #endif
99                 return -1;
100         }
101         if( fread(buf, 512, 1, pipe) == 0 )     return -1;      // Flush!
102         
103         // Send password
104         fputs(gsDoor_Password, pipe);
105         fputs("\n", pipe);
106         if( fread(buf, 512, 1, pipe) == 0 )     return -1;      // Flush!
107         
108         // ATH1 - Unlock door
109         fputs("ATH1\n", pipe);
110         if( fread(buf, 512, 1, pipe) == 0 )     return -1;      // Flush!
111         
112         // Wait before re-locking
113         sleep(DOOR_UNLOCKED_DELAY);
114
115         // Re-lock the door
116         fputs("ATH0\n", pipe);
117         if( fread(buf, 512, 1, pipe) == 0 )     return -1;      // Flush!
118         
119         pclose(pipe);
120         
121         #if DEBUG
122         printf("Door_DoDispense: User %i opened door\n", User);
123         #endif
124
125         return 0;
126 }
127
128

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