-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientCommands.cpp
300 lines (247 loc) · 7.06 KB
/
clientCommands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <iostream>
#include <fstream>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "crypto.h"
#define write cwrite
#define read cread
void closeSocket(int atmSocket) {
printf("Closing \n");
fflush(stdout);
close(atmSocket);
exit(1);
}
void error(const char *msg){
perror(msg);
exit(0);
}
// Validate that pin is 6 characters long
bool validPin(std::string pin){
if(pin.length() == 6) return true;
return false;
}
bool checkGood(std::string response){
if(response.compare("error! bad command!") == 0){
std::cout << "Didn't Work" << std::endl;
return true;
}
if(response.compare("-1") == 0){
std::cout << "Error" << std::endl;
return true;
}
else return false;
}
bool isNumber(std::string input){
// std::string input(word);
for(int i=0; i < input.length(); i++){
if(!(input[i] >= '0' && input[i] <= '9')){
return false;
}
}
return true;
}
// Read input until we read a space, then for each character add it to the command string
std::string advanceCommand(const std::string& line, int &index) {
std::string command = "";
for(; line[index] != '\n' && line[index] != '\0' && line[index] != ' ' && index <= line.length(); index++) {
command += line[index];
}
return command;
}
// Skip any number of spaces
void advanceSpaces(const std::string &line, int &index) {
for(; line[index] == ' ' && index <= line.length(); index++);
}
int parseRecieve(char * recieve, int messageNumber, int socketNo){
int messageIndex = 0;
std::string number = advanceCommand(recieve, messageIndex);
advanceSpaces(recieve, messageIndex);
int num = atoi(number.c_str());
if(num == -1){
// Error dont read response
bzero(recieve,256);
closeSocket(socketNo);
exit(-1);
// return messageNumber;
}
else if(num!= (++messageNumber)){
// Error dont read response
bzero(recieve,256);
closeSocket(socketNo);
exit(-1);
// return messageNumber;
}
std::string converted(recieve);
std::string subConverted = converted.substr(messageIndex, converted.length());
bzero(recieve,256);
strcpy (recieve,subConverted.c_str());
messageNumber++;
return messageNumber;
}
int sendRecieve(int socketNo, char* sendMessage, char* recieve, int messageNumber){
std::stringstream ss;
ss << messageNumber;
std::string num = ss.str();
char numMessage[256];
strcpy(numMessage, num.c_str());
strcat(numMessage, " ");
strcat(numMessage, sendMessage);
int n = write(socketNo, numMessage, strlen(numMessage)+1);
if (n < 0) error("ERROR writing to socket");
bzero(recieve,256);
n = read(socketNo,recieve,255);
if (n < 0) error("ERROR reading from socket");
messageNumber = parseRecieve(recieve, messageNumber, socketNo);
return messageNumber;
}
int login(const std::string username, int socketNo, int messageNumber, char *ans){
std::string cardInfo;
char cardName[80];
strcpy (cardName,username.c_str());
strcat(cardName, ".card");
std::ifstream inFile(cardName);
std::string str;
if(! inFile){
std::cout << "Broken... Couldn't open file" << std::endl;
strcpy (ans, "broken");
return messageNumber;
}
if(!std::getline(inFile, str)){
std::cout << "Broken..." << std::endl;
strcpy (ans, "broken");
return messageNumber;
}
std::string password;
std::cout << "Password : ";
std::cin >> password;
if(!validPin(password)){
std::cout << "Error Incorrect Password" << std::endl;
strcpy (ans, "broken");
return messageNumber;
}
char message[256];
strcpy(message, "login ");
strcat(message, username.c_str());
strcat(message, " ");
strcat(message, password.c_str());
char buffer[256];
messageNumber = sendRecieve(socketNo, message, buffer, messageNumber);
if(checkGood(std::string(buffer))){
strcpy (ans, "broken");
return messageNumber;
}
std::string response(buffer);
int index = 0;
std::string works = advanceCommand(response, index);
if(works.compare("y") == 0){
advanceSpaces(response, index);
std::string code = advanceCommand(response, index);
std:: cout << "Logged in" << std::endl;
strcpy (ans, code.c_str());
}
else{
std::cout << "Didn't work .... sorry" << std::endl;
strcpy (ans, "broken");
}
return messageNumber;
}
int balance(const std::string sessionKey, int socketNo, int messageNumber){
char message[256];
strcpy (message, sessionKey.c_str());
strcat(message, " balance");
char buffer[256];
messageNumber = sendRecieve(socketNo, message, buffer, messageNumber);
if(checkGood(std::string(buffer))){
return messageNumber;
}
std::cout << "Balance is: " << buffer << std::endl;
return messageNumber;
}
int withdraw(const std::string sessionKey, std::string amount, int socketNo, int messageNumber){
// Check balance for sufficient funds
char message[256];
strcpy (message, sessionKey.c_str());
strcat(message, " balance");
char buffer[256];
messageNumber = sendRecieve(socketNo, message, buffer, messageNumber);
if(checkGood(std::string(buffer))){
return messageNumber;
}
if(!isNumber(amount)){
return messageNumber;
}
float balance = atof(buffer);
float amountf = atof(amount.c_str());
// If sufficient funds, allow withdraw
if(amountf <= balance){
bzero(message,256);
strcpy (message, sessionKey.c_str());
strcat (message," withdraw ");
strcat(message, amount.c_str());
bzero(buffer,256);
messageNumber = sendRecieve(socketNo, message, buffer, messageNumber);
if(checkGood(std::string(buffer))){
return messageNumber;
}
std:: cout << buffer << std::endl;
}
else{
std::cout << "Insufficient funds to transfer" << buffer << std::endl;
}
return messageNumber;
}
int transfer(const std::string sessionKey, std::string amount, std::string username, int socketNo, int messageNumber){
// Check balance for sufficient funds
char message[256];
strcpy (message, sessionKey.c_str());
strcat(message, " balance");
char buffer[256];
bzero(buffer,256);
messageNumber = sendRecieve(socketNo, message, buffer, messageNumber);
if(checkGood(std::string(buffer))){
return messageNumber;
}
if(!isNumber(amount)){
return messageNumber;
}
float balance = atof(buffer);
float amountf = atof(amount.c_str());
// If sufficient funds, allow transfer
if(amountf <= balance){
// Check if other user exitss
bzero(message,256);
strcpy (message, sessionKey.c_str());
strcat (message," check ");
strcat(message, username.c_str());
bzero(buffer,256);
messageNumber = sendRecieve(socketNo, message, buffer, messageNumber);
if(checkGood(std::string(buffer))){
return messageNumber;
}
if((std::string(buffer)).compare("y") == 0){
bzero(message,256);
strcpy (message, sessionKey.c_str());
strcat (message," transfer ");
strcat(message, amount.c_str());
strcat(message, " ");
strcat(message, username.c_str());
bzero(buffer,256);
messageNumber = sendRecieve(socketNo, message, buffer, messageNumber);
if(checkGood(std::string(buffer))){
return messageNumber;
}
std:: cout << buffer << std::endl;
}
else{
std::cout << "The other user does not exist" << std::endl;
}
}
else{
std::cout << "Insufficient funds to transfer" << buffer << std::endl;
}
return messageNumber;
}