-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.hpp
More file actions
313 lines (254 loc) · 6.97 KB
/
Server.hpp
File metadata and controls
313 lines (254 loc) · 6.97 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
#pragma once
#ifndef TERMINAL_SERVER_HPP
#define TERMINAL_SERVER_HPP
/* Terminal: Server
#include "Server.hpp"
The server tracks its internal state, and sends and recieves packets.
Mostly used for text-only websites and also databases.
PACKET TYPES:
PIN - Ping. Ping target.
CON - Connect. Request connection.
ACK - Acknowledge. Acknowledge connection.
RPG - Request page. Request current page data from server.
REJ - Reject. Reject connection.
DIS - Disconnect. Disconnect connection.
PGD - Page data. Data to render requested page. Entire page is passed at once for simplicity.
RDR - Redraw. Client should request new page data.
*/
#include <string>
class Server
{
public:
std::string number;
Server()
{
}
// Server will return the main page upon successful connection.
virtual std::string servePage()
{
std::string mainPkt = "[PGD][data]";
return "";
}
virtual std::string recievePacket(std::string _packet)
{
return "";
}
virtual void sendPacket(std::string _packet)
{
}
};
Server testServer;
/*
RedThread is public BBS. Browse threads with up/down arrow.
*/
class Server_RedThread: public Server
{
class Thread
{
public:
std::string title;
std::string mainText;
Vector <std::string> vReply;
Thread(std::string _title="")
{
title=_title;
mainText="";
}
void addReply(std::string _reply)
{
vReply.push(_reply);
}
};
public:
//std::string number;
std::string userHandle;
Vector <Thread*> vThread;
int currentThread; // size() means new thread.
enum ActiveMenu
{
MAIN,
BBS,
READ_THREAD,
REPLY_THREAD
};
ActiveMenu activeMenu;
Server_RedThread()
{
number = "0011111111";
userHandle="";
activeMenu=MAIN;
currentThread = 0;
initThreads();
}
void initThreads()
{
char r = 1;
std::string redacted = std::string() + r + "" + r + "/" + r + r + "/" +r +r +r +r;
Thread * thread1 = new Thread("TEST");
thread1->mainText = "AYY THIS IS A TEST OF THE BBS THREAD SYSTEM.";
vThread.push(thread1);
Thread * thread2 = new Thread("HOW DO I POST BBS");
thread2->mainText = "AYO THIS IS ALSO A TEST OF THE BBS THREAD SYSTEM.";
thread2->addReply("Hey boi I heard you were testing the BBS thread system.");
thread2->addReply("Damn straight son.");
vThread.push(thread2);
Thread * thread3 = new Thread("HELP I AM STUCK IN COMPUTER WORLD");
thread3->mainText = "FUCK THIS BULLSHIT ISEKAI I DON'T EVEN HAVE A HAREM.";
thread3->addReply("lol sux 2b u.");
vThread.push(thread3);
}
virtual std::string servePage()
{
std::string strMain = "";
if (activeMenu==MAIN)
{
strMain+="REDTHREAD\n\033[1;31m________________________________________________________________\033[0m";
strMain+= "\n\nPlease enter a handle:\033[10;10Htest\033[11;10Htest\033[12;12Hayy\033[5;0H";
}
else if (activeMenu==BBS)
{
strMain+="TOPIC LIST\n\033[1;31m________________________________________________________________\033[0m";
//int currentY=2;
for (int i=0;i<vThread.size();++i)
{
strMain+=DataTools::toString(i);
strMain+=". ";
strMain+=vThread(i)->title;
strMain+="\n";
}
strMain+="\n#, new, or exit.\n";
}
else if (activeMenu==READ_THREAD)
{
if ( vThread.isSafe(currentThread) )
{
Thread* t = vThread(currentThread);
strMain+="READING THREAD: "+t->title+"\n\033[1;31m________________________________________________________________\033[0m";
strMain+=t->mainText+"\n\033[1;31m________________________________________________________________\033[0m";
for (int i2=0;i2<t->vReply.size();++i2)
{
strMain+=t->vReply(i2)+"\n\033[1;31m________________________________________________________________\033[0m";
}
//put cursor bottom.
strMain+="\n\nreply or exit\n";
}
}
else if (activeMenu==REPLY_THREAD)
{
strMain="Write reply (enter to send):\n";
}
return strMain;
}
// To scroll through BBS topics or posts.
void scrollDown()
{
}
void scrollUp()
{
}
std::string recievePacket(std::string _command)
{
if ( activeMenu==MAIN )
{
activeMenu=BBS;
// send redraw command
return "[RDR]";
}
else if (activeMenu==BBS)
{
if (_command=="EXIT")
{
activeMenu=MAIN;
}
else if (DataTools::isNumber(_command))
{
std::cout<<"READ TOPIC: "<<_command<<".\n";
int topicNumber = DataTools::toInt(_command);
if ( topicNumber < vThread.size() )
{
activeMenu=READ_THREAD;
currentThread=topicNumber;
}
}
//activeMenu=MAIN;
// send redraw command
return "[RDR]";
}
else if (activeMenu==READ_THREAD)
{
if (_command=="EXIT")
{
activeMenu=BBS;
return "[RDR]";
}
else if (_command=="REPLY")
{
activeMenu=REPLY_THREAD;
return "[RDR]";
}
}
else if (activeMenu==REPLY_THREAD)
{
if (vThread.isSafe(currentThread))
{
vThread(currentThread)->addReply(_command);
activeMenu=READ_THREAD;
return "[RDR]";
}
}
return "";
}
};
Server_RedThread serverRedThread;
// Mail can be stored using real encryption, which will require password to read.
class Server_SpicyMail: public Server
{
public:
enum ActiveMenu
{
LOGIN,
MAIL_USER,
MAIL_ADMIN
};
ActiveMenu activeMenu;
std::string username;
std::string password;
Server_SpicyMail()
{
number = "0012222222";
activeMenu=LOGIN;
username="";
password="";
}
virtual std::string servePage()
{
std::string strMain = "";
if (activeMenu==LOGIN)
{
strMain+="WELCOME TO SPICYMAIL";
strMain+= "\nUsername:\nPassword:\033[1,1H"; /* put cursor at username */
}
else if (activeMenu==MAIL_USER)
{
strMain+="MAIL\nYou have x unread messages.";
}
return strMain;
}
};
Server_SpicyMail serverMail;
class Server_LumaBank: public Server
{
public:
enum ActiveMenu
{
LOGIN,
CASHMONEY
};
ActiveMenu activeMenu;
Server_LumaBank()
{
number="0013333333";
}
};
Server_LumaBank serverLumaBank;
#endif