forked from hyperyoda/netrek-client-brmh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getname.c
380 lines (350 loc) · 8.28 KB
/
getname.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
/*
* getname.c
*
* Kevin P. Smith 09/28/88
*
*/
#include "copyright2.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#ifdef hpux
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <errno.h>
#include <pwd.h>
#include <string.h>
#include <ctype.h>
#include "netrek.h"
static char tempname[16];
static char password1[16];
static char password2[16];
static int state;
#define ST_GETNAME 0
#define ST_GETPASS 1
#define ST_MAKEPASS1 2
#define ST_MAKEPASS2 3
#define ST_DONE 4
#ifdef AUTOLOGIN /* COW-lite */
void
noautologin ()
{
static char *alf = "Automatic login failed";
autologin = 0;
*defpasswd = *password1 = *password2 = '\0';
W_WriteText (w, 100, 130, textColor, alf, strlen (alf),
W_BoldFont);
}
#endif /* AUTOLOGIN */
void
getname(defname)
char *defname;
/* Let person identify themselves from w */
{
int secondsLeft = 99;
W_Event event;
unsigned char ch;
char tempstr[40];
long lasttime;
struct timeval tv;
fd_set mask;
MZERO(mystats, sizeof(struct stats));
mystats->st_tticks = 1;
mystats->st_flags = ST_MAPMODE + ST_NAMEMODE + ST_SHOWSHIELDS +
ST_KEEPPEACE + ST_SHOWLOCAL * 2 + ST_SHOWGLOBAL * 2;
lasttime = time(NULL);
tempname[0] = '\0';
password1[0] = '\0';
password2[0] = '\0';
state = ST_GETNAME;
#ifdef AUTOLOGIN
if (autologin)
try_autologin(defname);
if (state == ST_DONE)
return;
#endif /*AUTOLOGIN*/
displayStartup(defname);
for (;;) {
/* TSH: added select here so this doesn't busy wait (only done once
but it messes up performance metrics) */
if (state == ST_DONE) {
W_ClearWindow(w);
return;
}
while(!W_EventsPending()){
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&mask);
FD_SET(W_Socket(), &mask);
FD_SET(sock, &mask);
/* udp sock will not be active here */
select(32, &mask, 0, 0, &tv);
if (lasttime != time(NULL)) {
lasttime++;
secondsLeft--;
sprintf(tempstr, "Seconds to go: %d ", secondsLeft);
W_WriteText(w, 150, 400, textColor, tempstr, strlen(tempstr),
W_BoldFont);
if (secondsLeft == 0) {
me->p_status = PFREE;
printf("Auto-Quit\n");
exit(0);
}
}
if(FD_ISSET(sock, &mask)){
readFromServer(&mask);
if (isServerDead()) {
printf("Ack! We've been ghostbusted!\n");
exit(0);
}
}
if (!FD_ISSET(W_Socket(), &mask))
continue;
}
W_NextEvent(&event);
if (event.Window != w)
continue;
switch ((int) event.type) {
case W_EV_EXPOSE:
displayStartup(defname);
sprintf(tempstr, "Seconds to go: %d ", secondsLeft);
W_WriteText(w, 150, 400, textColor, tempstr, strlen(tempstr),
W_BoldFont);
break;
case W_EV_KEY:
ch = event.key;
if (ch == 10)
ch = 13;
switch(ch){
case 4:
case 'd' + 96:
case 'D' + 96:
if(state == ST_GETNAME && *tempname == 0)
exit(0);
break;
default:
break;
}
if (ch < 32 && ch != 21 && ch != 13 && ch != 8)
break;
switch (state) {
case ST_GETNAME:
if (ch == 13) {
if (*tempname == '\0') {
strcpy(tempname, defname);
}
loaddude();
displayStartup(defname);
} else {
adjustString(ch, tempname, defname);
}
break;
case ST_GETPASS:
if (ch == 13) {
checkpassword();
displayStartup(defname);
} else {
adjustString(ch, password1, defname);
}
break;
case ST_MAKEPASS1:
if (ch == 13) {
state = ST_MAKEPASS2;
displayStartup(defname);
} else {
adjustString(ch, password1, defname);
}
break;
case ST_MAKEPASS2:
if (ch == 13) {
makeNewGuy();
displayStartup(defname);
} else {
adjustString(ch, password2, defname);
}
break;
}
}
}
}
#ifdef AUTOLOGIN
void
try_autologin(defname)
char *defname;
{
strcpy(tempname, defname);
loaddude();
if (state != ST_DONE) {
strcpy(password1, defpasswd);
checkpassword();
}
if (state == ST_GETNAME)
noautologin();
}
#endif
void
loaddude()
/*
* Query dude.
*/
{
if (strcmp(tempname, "Guest") == 0 || strcmp(tempname, "guest") == 0) {
loginAccept = -1;
sendLoginReq(tempname, "", (char *) login, 0);
state = ST_DONE;
me->p_stats.st_tticks = 1;/* prevent overflow */
strcpy(me->p_name, tempname);
while (loginAccept == -1) {
socketPause();
readFromServer(NULL);
if (isServerDead()) {
printf("Server is dead!\n");
exit(0);
}
}
if (loginAccept == 0) {
printf("Hmmm... The SOB server won't let me log in as guest!\n");
exit(0);
}
return;
}
/* Ask about the user */
loginAccept = -1;
sendLoginReq(tempname, "", login, 1);
while (loginAccept == -1) {
socketPause();
readFromServer(NULL);
if (isServerDead()) {
printf("Server is dead!\n");
exit(0);
}
}
*password1 = *password2 = 0;
if (loginAccept == 0) {
state = ST_MAKEPASS1;
} else {
state = ST_GETPASS;
}
}
void
checkpassword()
/*
* Check dude's password. If he is ok, move to state ST_DONE.
*/
{
char *s;
sendLoginReq(tempname, password1, login, 0);
loginAccept = -1;
while (loginAccept == -1) {
socketPause();
readFromServer(NULL);
if (isServerDead()) {
printf("Server is dead!\n");
exit(0);
}
}
if (loginAccept == 0) {
s = "Bad password!";
W_WriteText(w, 100, 100, textColor, s, strlen(s), W_BoldFont);
(void) W_EventsPending();
sleep(1);
W_ClearWindow(w);
*tempname = 0;
state = ST_GETNAME;
return;
}
strcpy(me->p_name, tempname);
state = ST_DONE;
}
void
makeNewGuy()
/*
* Make the dude with name tempname and password password1. Move to state
* ST_DONE.
*/
{
char *s;
if (strcmp(password1, password2) != 0) {
s = "Passwords do not match";
W_WriteText(w, 100, 120, textColor, s, strlen(s), W_BoldFont);
(void) W_EventsPending();
sleep(3);
W_ClearWindow(w);
*tempname = 0;
state = ST_GETNAME;
return;
}
/* same routine! */
checkpassword();
}
void
adjustString(ch, str, defname)
unsigned char ch;
char *str;
char *defname;
{
switch((unsigned char) ch){
case 21:
case 'u' + 96:
case 'U' + 96:
*str = '\0';
if (state == ST_GETNAME)
displayStartup(defname);
break;
case 8:
case '\177':
case 'h' + 96:
case 'H' + 96:
if (strlen(str) > 0) {
str[strlen(str) - 1] = '\0';
if (state == ST_GETNAME)
displayStartup(defname);
}
break;
default:
if (strlen(str) == 15)
return;
str[strlen(str) + 1] = '\0';
str[strlen(str)] = ch;
if (state == ST_GETNAME)
displayStartup(defname);
break;
}
}
void
displayStartup(defname)
char *defname;
/* Draws entry screen based upon state. */
{
char s[100];
char *t;
if (state == ST_DONE)
return;
t = "Enter your name. Use the name 'guest' to remain anonymous.";
W_WriteText(w, 100, 30, textColor, t, strlen(t), W_BoldFont);
t = "Type ^D (Ctrl - D) to quit.";
W_WriteText(w, 100, 40, textColor, t, strlen(t), W_BoldFont);
sprintf(s, "Your name (default = %s): %s ", defname, tempname);
W_WriteText(w, 100, 50, textColor, s, strlen(s), W_BoldFont);
if (state == ST_GETPASS) {
t = "Enter password: ";
W_WriteText(w, 100, 60, textColor, t, strlen(t), W_BoldFont);
}
if (state > ST_GETPASS) {
t = "You need to make a password.";
W_WriteText(w, 100, 70, textColor, t, strlen(t), W_BoldFont);
t = "So think of a password you can remember, and enter it.";
W_WriteText(w, 100, 80, textColor, t, strlen(t), W_BoldFont);
t = "What is your password? :";
W_WriteText(w, 100, 90, textColor, t, strlen(t), W_BoldFont);
}
if (state == ST_MAKEPASS2) {
t = "Enter it again to make sure you typed it right.";
W_WriteText(w, 100, 100, textColor, t, strlen(t), W_BoldFont);
t = "Your password? :";
W_WriteText(w, 100, 110, textColor, t, strlen(t), W_BoldFont);
}
}