978c1848739d33b3003115aa1fa95cbb3e74e6b6
[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 DOOR_UNLOCKED_DELAY     10      // 10 seconds before it re-locks
18
19 // === IMPORTS ===
20
21 // === PROTOTYPES ===
22  int    Door_InitHandler();
23  int    Door_CanDispense(int User, int Item);
24  int    Door_DoDispense(int User, int Item);
25
26 // === GLOBALS ===
27 tHandler        gDoor_Handler = {
28         "door",
29         Door_InitHandler,
30         Door_CanDispense,
31         Door_DoDispense
32 };
33 char    *gsDoor_Password;
34 char    *gsDoor_Command;
35 // int  giDoor_SerialFD;
36
37 // == CODE ===
38 int Door_InitHandler(void)
39 {               
40         gsDoor_Command = malloc(sizeof("llogin door -w ")+strlen(gsDoor_Password));
41         sprintf(gsDoor_Command, "llogin door -w %s", gsDoor_Password);
42         
43         return 0;
44 }
45
46 /**
47  */
48 int Door_CanDispense(int User, int Item)
49 {
50         // Sanity please
51         if( Item == 0 ) return -1;
52         
53         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
54                 return 1;
55         
56         return 0;
57 }
58
59 /**
60  * \brief Actually do a dispense from the coke machine
61  */
62 int Door_DoDispense(int User, int Item)
63 {
64         FILE    *pipe;
65         // Sanity please
66         if( Item != 0 ) return -1;
67         
68         // Check if user is in door
69         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
70                 return 1;
71         
72         // llogin or other
73         //pipe = popen(gsDoor_Command, "w");
74         pipe = popen("llogin door -w -", "w");
75         if( !pipe || pipe == (void*)-1 )
76                 return -1;
77         
78         fputs(gsDoor_Password, pipe);   fputs("\n", pipe);
79         fputs("ATH1\n", pipe);
80         sleep(DOOR_UNLOCKED_DELAY);
81         fputs("ATH0\n", pipe);
82         
83         pclose(pipe);
84
85         return 0;
86 }
87
88

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