forked from MagerValp/CGTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.c
491 lines (438 loc) · 10.4 KB
/
chat.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include <string.h>
#include <stdlib.h>
#include <SDL.h>
#include "kernal.h"
#include "gfx.h"
#include "config.h"
#include "timer.h"
#include "status.h"
#include "net.h"
#define BUFLEN 256
static unsigned char output_buffer[BUFLEN * 3 + 100];
static unsigned char input_buffer[BUFLEN];
static unsigned char color_buffer[BUFLEN];
static int input_maxlen = BUFLEN - 1;
static int input_len;
static int input_width;
static int input_offset;
static int input_pos;
static int input_color;
static int input_rvs;
static int input_startcolor;
static int outputline;
/*
HELO:protocol:message\r connect greeting
TALK:username:message\r public message
MESG:username:message\r private message
STAT:message\r status message
INFO:message\r info message from server
LOCL:message\r local client message
*/
void print_msg(unsigned char *msg) {
unsigned char *p;
unsigned char *data = msg + 5;
if (strlen(msg) < 5) {
print("\x96" "sERVER SENT GARBAGE!\x0d");
return;
}
if (strncmp("helo:", msg, 5) == 0) {
if (strncmp("64chat ", data, 7) != 0) {
print("mALFORMED SERVER MESSAGE\x0d");
return;
}
ffd2(5);
print(data);
ffd2(13);
} else if (strncmp("talk:", msg, 5) == 0) {
if ((p = strchr(data, ':')) == NULL) {
print("\x96" "mALFORMED SERVER MESSAGE\x0d");
return;
}
*p++ = 0;
ffd2(158);
print(data);
ffd2(153);
ffd2(' ');
print(p);
ffd2(13);
} else if (strncmp("mesg:", msg, 5) == 0) {
if ((p = strchr(data, ':')) == NULL) {
print("\x96" "mALFORMED SERVER MESSAGE\x0d");
return;
}
*p++ = 0;
ffd2(156);
print(data);
ffd2(154);
ffd2(' ');
print(p);
ffd2(13);
} else if (strncmp("stat:", msg, 5) == 0) {
ffd2(150);
print(data);
ffd2(13);
} else if (strncmp("locl:", msg, 5) == 0) {
ffd2(150);
print(data);
ffd2(13);
} else if (strncmp("info:", msg, 5) == 0) {
ffd2(155);
print(data);
ffd2(13);
} else if (strncmp("noop:", msg, 5) == 0) {
// do nothing
} else {
print("\x96" "uNKNOWN SERVER MESSAGE\x0d");
print(msg);
ffd2(13);
return;
}
}
void chat_print_msg(unsigned char *msg) {
status_clear();
gfx_setcursxy(0, outputline);
print_msg(msg);
outputline = gfx_cursy;
gfx_setcursxy(input_pos, 24);
gfx_fgcolor(input_color);
status_draw();
}
void chat_print_net_status(int code, char *message) {
char msg[80];
int i = 0;
char c;
if (code == 2) {
strcpy(msg, "locl:");
while ((c = *message++) && i < 74) {
if (c >= 'A' && c <= 'Z') {
c += 32;
} else if (c >= 'a' && c <= 'z') {
c -= 32;
}
msg[i + 5] = c;
++i;
}
msg[i + 5] = 0;
print_msg(msg);
}
}
void parse_cmd(unsigned char *chars, unsigned char *colors, int length, unsigned char *outdata) {
unsigned char *data = NULL, *p;
char cmd[BUFLEN * 3];
int color, rev;
outdata[0] = 0;
rev = 0;
if (*chars != '/') {
strcpy(outdata, "talk:");
color = 256;
gfx_conv_screen_to_pet(chars, colors, outdata + 5, &color, &rev, 0, input_len);
if (strlen(outdata + 5) == 0) {
outdata[0] = 0;
}
return;
}
color = *colors;
gfx_conv_screen_to_pet(chars, colors, cmd, &color, &rev, 0, input_len);
if ((data = strchr(cmd, ' '))) {
*data++ = 0;
}
// /register username password
if (strcasecmp("/REG", cmd) == 0) {
if (data && (p = strchr(data, ' '))) {
strcpy(outdata, "regi:");
*p = ':';
strcat(outdata, data);
} else {
print_msg("locl:uSAGE: /REG <USERNAME> <PASSWORD>");
return;
}
// /login username password
} else if (strcasecmp("/LOGIN", cmd) == 0) {
if (data && (p = strchr(data, ' '))) {
strcpy(outdata, "auth:");
*p = ':';
strcat(outdata, data);
} else {
print_msg("locl:uSAGE: /LOGIN <USERNAME> <PASSWORD>");
return;
}
// /nick nickname
} else if (strcasecmp("/NICK", cmd) == 0) {
if (data) {
strcpy(outdata, "nick:");
strcat(outdata, data);
} else {
print_msg("locl:uSAGE: /NICK <NICKNAME>");
return;
}
// /quit message
} else if (strcasecmp("/QUIT", cmd) == 0) {
strcpy(outdata, "quit:");
if (data) {
strcat(outdata, data);
} else {
strcat(outdata, "nO REASON");
}
net_send_string(outdata);
net_send(13);
timer_delay(100);
net_disconnect();
exit(0);
// /disconnect message
} else if (strcasecmp("/DISCONNECT", cmd) == 0) {
strcpy(outdata, "quit:");
if (data) {
strcat(outdata, data);
} else {
strcat(outdata, "nO REASON");
}
cfg_nextreconnect = 0;
// /msg username message
} else if (strcasecmp("/MSG", cmd) == 0) {
if (data && (p = strchr(data, ' '))) {
strcpy(outdata, "mesg:");
*p = ':';
strcat(outdata, data);
} else {
print_msg("locl:uSAGE: /MSG <USERNAME> <MESSAGE>");
return;
}
// /msg username message
} else if (strcasecmp("/PASS", cmd) == 0) {
if (data && (p = strchr(data, ' '))) {
strcpy(outdata, "pass:");
*p = ':';
strcat(outdata, data);
} else {
print_msg("locl:uSAGE: /PASS <OLDPASS> <NEWPASS>");
return;
}
// /who
} else if (strcasecmp("/WHO", cmd) == 0) {
strcpy(outdata, "whol");
// /whois username
} else if (strcasecmp("/WHOIS", cmd) == 0) {
if (data) {
strcpy(outdata, "whoi:");
strcat(outdata, data);
} else {
print_msg("locl:uSAGE: /WHOIS <USERNAME>");
return;
}
// /cls
} else if (strcasecmp("/CLS", cmd) == 0) {
gfx_cls();
outputline = 0;
status_draw();
// /connect server port
} else if (strcasecmp("/CONNECT", cmd) == 0) {
if (net_connected()) {
print_msg("locl:aLREADY CONNECTED");
return;
}
if (data) {
if ((p = strchr(data, ' '))) {
*p++ = 0;
cfg_port = strtol(p, (char **)NULL, 10);
if (cfg_port <= 0 || cfg_port > 65535) {
print_msg("locl:iLLEGAL PORT NUMBER");
return;
}
} else {
cfg_port = 23;
}
cfg_sethost(data);
net_connect(cfg_host, cfg_port, &chat_print_net_status);
} else {
print_msg("locl:uSAGE: /CONNECT <server> [port]");
return;
}
// unknown command
} else {
print_msg("locl:uNKNOWN COMMAND");
return;
}
}
void input_draw(void) {
unsigned char convbuf[80];
memset(convbuf, ' ', 80);
memcpy(convbuf, input_buffer + input_offset, input_len - input_offset);
gfx_copy_line(convbuf, color_buffer + input_offset, 24);
gfx_setcursxy(input_pos, 24);
gfx_fgcolor(input_color);
}
void chat_inputkey(unsigned char key) {
int i;
switch (key) {
default:
if ((key >= 32 && key <= 127) || (key >= 160)) {
if (key == '/' && input_pos + input_len == 0) {
input_color = COLOR_YELLOW;
}
if (input_len < input_maxlen) {
++input_len;
i = strlen(input_buffer + input_pos + input_offset) + 1;
memmove(input_buffer + input_pos + input_offset + 1,
input_buffer + input_pos + input_offset,
i);
memmove(color_buffer + input_pos + input_offset + 1,
color_buffer + input_pos + input_offset,
i);
input_buffer[input_pos + input_offset] = screencode[key] + (input_rvs<<7);
input_buffer[input_len] = 0;
color_buffer[input_pos + input_offset] = input_color;
color_buffer[input_len] = 0;
if (input_pos + input_offset < input_len) {
if (input_pos < input_width - 1) {
++input_pos;
} else if (input_len > input_offset + input_pos) {
++input_offset;
}
}
}
}
break;
case 13:
if (input_len) {
if (input_buffer[0] != '/') {
input_startcolor = input_color;
}
parse_cmd(input_buffer, color_buffer, input_len, output_buffer);
if (output_buffer[0]) {
net_send_string(output_buffer);
net_send(13);
}
input_len = input_offset = input_pos = input_rvs = 0;
input_color = input_startcolor;
input_buffer[0] = 0;
}
break;
case 29: // right
if (input_pos + input_offset < input_len) {
if (input_pos < input_width - 1) {
++input_pos;
} else if (input_len > input_offset + input_pos) {
++input_offset;
}
}
break;
case 157: // left
if (input_pos > 0) {
--input_pos;
} else if (input_offset > 0) {
--input_offset;
}
break;
case 19: // home
if (input_pos || input_offset) {
input_pos = input_offset = 0;
}
break;
case 147: // clear
input_pos = input_offset = input_len = 0;
input_color = input_startcolor;
input_buffer[0] = 0;
break;
case 20: // delete
if (input_pos || input_offset) {
if (input_pos) {
--input_pos;
} else {
--input_offset;
}
i = input_offset + input_pos;
while (input_buffer[i]) {
input_buffer[i] = input_buffer[i+1];
color_buffer[i] = color_buffer[i+1];
++i;
}
--input_len;
if (input_len == 0) {
input_color = input_startcolor;
}
}
break;
case 148: // insert
if (input_pos + input_offset < input_len) {
i = input_offset + input_pos;
while (input_buffer[i]) {
input_buffer[i] = input_buffer[i+1];
color_buffer[i] = color_buffer[i+1];
++i;
}
--input_len;
if (input_len == 0) {
input_color = input_startcolor;
}
}
break;
case 18: // rvs on
input_rvs = 1;
break;
case 146: // rvs off
input_rvs = 0;
break;
// all the colors
case 5:
input_color = COLOR_WHITE;
break;
case 28:
input_color = COLOR_RED;
break;
case 30:
input_color = COLOR_GREEN;
break;
case 31:
input_color = COLOR_BLUE;
break;
case 129:
input_color = COLOR_ORANGE;
break;
/* except black
case 144:
input_color = COLOR_BLACK;
break;
*/
case 149:
input_color = COLOR_BROWN;
break;
case 150:
input_color = COLOR_LTRED;
break;
case 151:
input_color = COLOR_DKGRAY;
break;
case 152:
input_color = COLOR_GRAY;
break;
case 153:
input_color = COLOR_LTGREEN;
break;
case 154:
input_color = COLOR_LTBLUE;
break;
case 155:
input_color = COLOR_LTGRAY;
break;
case 156:
input_color = COLOR_PURPLE;
break;
case 158:
input_color = COLOR_YELLOW;
break;
case 159:
input_color = COLOR_CYAN;
break;
}
input_draw();
}
void chat_init(void) {
outputline = gfx_cursy;
input_width = cfg_columns;
input_len = input_offset = input_pos = input_rvs = 0;
input_color = input_startcolor = COLOR_LTGREEN;
input_buffer[0] = 0;
status_init();
status_draw();
input_draw();
}