forked from sneezymud/dikumud
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodify.c
829 lines (673 loc) · 16 KB
/
modify.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/* ************************************************************************
* file: modify.c Part of DIKUMUD *
* Usage: Run-time modification (by users) of game variables *
* Copyright (C) 1990, 1991 - see 'license.doc' for complete information. *
************************************************************************ */
#include <signal.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "structs.h"
#include "utils.h"
#include "interpreter.h"
#include "handler.h"
#include "db.h"
#include "comm.h"
#define REBOOT_AT 10 /* 0-23, time of optional reboot if -e lib/reboot */
#define TP_MOB 0
#define TP_OBJ 1
#define TP_ERROR 2
void show_string(struct descriptor_data *d, char *input);
char *string_fields[] =
{
"name",
"short",
"long",
"description",
"title",
"delete-description",
"\n"
};
/* maximum length for text field x+1 */
int length[] =
{
15,
60,
256,
240,
60
};
char *skill_fields[] =
{
"learned",
"recognize",
"\n"
};
/* ************************************************************************
* modification of malloc'ed strings *
************************************************************************ */
/* Add user input to the 'current' string (as defined by d->str) */
void string_add(struct descriptor_data *d, char *str)
{
char *scan;
int terminator = 0;
/* determine if this is the terminal string, and truncate if so */
for (scan = str; *scan; scan++)
if (terminator = (*scan == '@'))
{
*scan = '\0';
break;
}
if (!(*d->str))
{
if (strlen(str) > d->max_str)
{
send_to_char("String too long - Truncated.\n\r",
d->character);
*(str + d->max_str) = '\0';
terminator = 1;
}
CREATE(*d->str, char, strlen(str) + 3);
strcpy(*d->str, str);
}
else
{
if (strlen(str) + strlen(*d->str) > d->max_str)
{
send_to_char("String too long. Last line skipped.\n\r",
d->character);
terminator = 1;
}
else
{
if (!(*d->str = (char *) realloc(*d->str, strlen(*d->str) +
strlen(str) + 3)))
{
perror("string_add");
exit(1);
}
strcat(*d->str, str);
}
}
if (terminator)
{
d->str = 0;
if (d->connected == CON_EXDSCR)
{
SEND_TO_Q(MENU, d);
d->connected = CON_SLCT;
}
}
else
strcat(*d->str, "\n\r");
}
#undef MAX_STR
/* interpret an argument for do_string */
void quad_arg(char *arg, int *type, char *name, int *field, char *string)
{
char buf[MAX_STRING_LENGTH];
int i;
/* determine type */
arg = one_argument(arg, buf);
if (is_abbrev(buf, "char"))
*type = TP_MOB;
else if (is_abbrev(buf, "obj"))
*type = TP_OBJ;
else
{
*type = TP_ERROR;
return;
}
/* find name */
arg = one_argument(arg, name);
/* field name and number */
arg = one_argument(arg, buf);
if (!(*field = old_search_block(buf, 0, strlen(buf), string_fields, 0)))
return;
/* string */
for (; isspace(*arg); arg++);
for (; *string = *arg; arg++, string++);
return;
}
/* modification of malloc'ed strings in chars/objects */
void do_string(struct char_data *ch, char *arg, int cmd)
{
char name[MAX_STRING_LENGTH], string[MAX_STRING_LENGTH];
int field, type;
struct char_data *mob;
struct obj_data *obj;
struct extra_descr_data *ed, *tmp;
if (IS_NPC(ch))
return;
quad_arg(arg, &type, name, &field, string);
if (type == TP_ERROR)
{
send_to_char(
"Syntax:\n\rstring ('obj'|'char') <name> <field> [<string>].",
ch);
return;
}
if (!field)
{
send_to_char("No field by that name. Try 'help string'.\n\r",
ch);
return;
}
if (type == TP_MOB)
{
/* locate the beast */
if (!(mob = get_char_vis(ch, name)))
{
send_to_char("I don't know anyone by that name...\n\r",
ch);
return;
}
switch(field)
{
case 1:
if (!IS_NPC(mob) && GET_LEVEL(ch) < 24)
{
send_to_char("You can't change that field for players.", ch);
return;
}
ch->desc->str = &GET_NAME(mob);
if (!IS_NPC(mob))
send_to_char(
"WARNING: You have changed the name of a player.\n\r", ch);
break;
case 2:
if (!IS_NPC(mob))
{
send_to_char(
"That field is for monsters only.\n\r", ch);
return;
}
ch->desc->str = &mob->player.short_descr;
break;
case 3:
if (!IS_NPC(mob))
{
send_to_char(
"That field is for monsters only.\n\r", ch);
return;
}
ch->desc->str = &mob->player.long_descr;
break;
case 4:ch->desc->str = &mob->player.description; break;
case 5:
if (IS_NPC(mob))
{
send_to_char("Monsters have no titles.\n\r",
ch);
return;
}
ch->desc->str = &mob->player.title;
break;
default:
send_to_char(
"That field is undefined for monsters.\n\r", ch);
return;
break;
}
}
else /* type == TP_OBJ */
{
/* locate the object */
if (!(obj = get_obj_vis(ch, name)))
{
send_to_char("Can't find such a thing here..\n\r", ch);
return;
}
switch(field)
{
case 1: ch->desc->str = &obj->name; break;
case 2: ch->desc->str = &obj->short_description; break;
case 3: ch->desc->str = &obj->description; break;
case 4:
if (!*string)
{
send_to_char("You have to supply a keyword.\n\r", ch);
return;
}
/* try to locate extra description */
for (ed = obj->ex_description; ; ed = ed->next)
if (!ed) /* the field was not found. create a new one. */
{
CREATE(ed , struct extra_descr_data, 1);
ed->next = obj->ex_description;
obj->ex_description = ed;
CREATE(ed->keyword, char, strlen(string) + 1);
strcpy(ed->keyword, string);
ed->description = 0;
ch->desc->str = &ed->description;
send_to_char("New field.\n\r", ch);
break;
}
else if (!str_cmp(ed->keyword, string)) /* the field exists */
{
free(ed->description);
ed->description = 0;
ch->desc->str = &ed->description;
send_to_char(
"Modifying description.\n\r", ch);
break;
}
ch->desc->max_str = MAX_STRING_LENGTH;
return; /* the stndrd (see below) procedure does not apply here */
break;
case 6:
if (!*string)
{
send_to_char("You must supply a field name.\n\r", ch);
return;
}
/* try to locate field */
for (ed = obj->ex_description; ; ed = ed->next)
if (!ed)
{
send_to_char("No field with that keyword.\n\r", ch);
return;
}
else if (!str_cmp(ed->keyword, string))
{
free(ed->keyword);
if (ed->description)
free(ed->description);
/* delete the entry in the desr list */
if (ed == obj->ex_description)
obj->ex_description = ed->next;
else
{
for(tmp = obj->ex_description; tmp->next != ed;
tmp = tmp->next);
tmp->next = ed->next;
}
free(ed);
send_to_char("Field deleted.\n\r", ch);
return;
}
break;
default:
send_to_char(
"That field is undefined for objects.\n\r", ch);
return;
break;
}
}
if (*ch->desc->str)
{
free(*ch->desc->str);
}
if (*string) /* there was a string in the argument array */
{
if (strlen(string) > length[field - 1])
{
send_to_char("String too long - truncated.\n\r", ch);
*(string + length[field - 1]) = '\0';
}
CREATE(*ch->desc->str, char, strlen(string) + 1);
strcpy(*ch->desc->str, string);
ch->desc->str = 0;
send_to_char("Ok.\n\r", ch);
}
else /* there was no string. enter string mode */
{
send_to_char("Enter string. terminate with '@'.\n\r", ch);
*ch->desc->str = 0;
ch->desc->max_str = length[field - 1];
}
}
/* db stuff *********************************************** */
/* One_Word is like one_argument, execpt that words in quotes "" are */
/* regarded as ONE word */
char *one_word(char *argument, char *first_arg )
{
int found, begin, look_at;
found = begin = 0;
do
{
for ( ;isspace(*(argument + begin)); begin++);
if (*(argument+begin) == '\"') { /* is it a quote */
begin++;
for (look_at=0; (*(argument+begin+look_at) >= ' ') &&
(*(argument+begin+look_at) != '\"') ; look_at++)
*(first_arg + look_at) = LOWER(*(argument + begin + look_at));
if (*(argument+begin+look_at) == '\"')
begin++;
} else {
for (look_at=0; *(argument+begin+look_at) > ' ' ; look_at++)
*(first_arg + look_at) = LOWER(*(argument + begin + look_at));
}
*(first_arg + look_at) = '\0';
begin += look_at;
}
while (fill_word(first_arg));
return(argument+begin);
}
struct help_index_element *build_help_index(FILE *fl, int *num)
{
int nr = -1, issorted, i;
struct help_index_element *list = 0, mem;
char buf[81], tmp[81], *scan;
long pos;
for (;;)
{
pos = ftell(fl);
fgets(buf, 81, fl);
*(buf + strlen(buf) - 1) = '\0';
scan = buf;
for (;;)
{
/* extract the keywords */
scan = one_word(scan, tmp);
if (!*tmp)
break;
if (!list)
{
CREATE(list, struct help_index_element, 1);
nr = 0;
}
else
RECREATE(list, struct help_index_element, ++nr + 1);
list[nr].pos = pos;
CREATE(list[nr].keyword, char, strlen(tmp) + 1);
strcpy(list[nr].keyword, tmp);
}
/* skip the text */
do
fgets(buf, 81, fl);
while (*buf != '#');
if (*(buf + 1) == '~')
break;
}
/* we might as well sort the stuff */
do
{
issorted = 1;
for (i = 0; i < nr; i++)
if (str_cmp(list[i].keyword, list[i + 1].keyword) > 0)
{
mem = list[i];
list[i] = list[i + 1];
list[i + 1] = mem;
issorted = 0;
}
}
while (!issorted);
*num = nr;
return(list);
}
void page_string(struct descriptor_data *d, char *str, int keep_internal)
{
if (!d)
return;
if (keep_internal)
{
CREATE(d->showstr_head, char, strlen(str) + 1);
strcpy(d->showstr_head, str);
d->showstr_point = d->showstr_head;
}
else
d->showstr_point = str;
show_string(d, "");
}
void show_string(struct descriptor_data *d, char *input)
{
char buffer[MAX_STRING_LENGTH], buf[MAX_INPUT_LENGTH];
register char *scan, *chk;
int lines = 0, toggle = 1;
one_argument(input, buf);
if (*buf)
{
if (d->showstr_head)
{
free(d->showstr_head);
d->showstr_head = 0;
}
d->showstr_point = 0;
return;
}
/* show a chunk */
for (scan = buffer;; scan++, d->showstr_point++)
if((((*scan = *d->showstr_point) == '\n') || (*scan == '\r')) &&
((toggle = -toggle) < 0))
lines++;
else if (!*scan || (lines >= 22))
{
*scan = '\0';
SEND_TO_Q(buffer, d);
/* see if this is the end (or near the end) of the string */
for (chk = d->showstr_point; isspace(*chk); chk++);
if (!*chk)
{
if (d->showstr_head)
{
free(d->showstr_head);
d->showstr_head = 0;
}
d->showstr_point = 0;
}
return;
}
}
void night_watchman(void)
{
long tc;
struct tm *t_info;
extern int clean_shutdown;
void send_to_all(char *messg);
tc = time(0);
t_info = localtime(&tc);
if ((t_info->tm_hour == 8) && (t_info->tm_wday > 0) &&
(t_info->tm_wday < 6))
if (t_info->tm_min > 50)
{
log_message("Leaving the scene for the serious folks.");
send_to_all("Closing down. Thank you for flying DikuMUD.\n\r");
clean_shutdown = 1;
}
else if (t_info->tm_min > 40)
send_to_all("ATTENTION: DikuMUD will shut down in 10 minutes.\n\r");
else if (t_info->tm_min > 30)
send_to_all("Warning: The game will close in 20 minutes.\n\r");
}
void check_reboot(void)
{
long tc;
struct tm *t_info;
char dummy;
FILE *boot;
extern int clean_shutdown, reboot;
tc = time(0);
t_info = localtime(&tc);
if ((t_info->tm_hour + 1) == REBOOT_AT && t_info->tm_min > 30)
if (boot = fopen("./reboot", "r"))
{
if (t_info->tm_min > 50)
{
log_message("Reboot exists.");
fread(&dummy, sizeof(dummy), 1, boot);
if (!feof(boot)) /* the file is nonepty */
{
log_message("Reboot is nonempty.");
/* the script can't handle the signals */
sigsetmask(sigmask(SIGUSR1) | sigmask(SIGUSR2) |
sigmask(SIGINT) | sigmask(SIGPIPE) | sigmask(SIGALRM) |
sigmask(SIGTERM) | sigmask(SIGURG) | sigmask(SIGXCPU) |
sigmask(SIGHUP) | sigmask(SIGVTALRM));
if (system("./reboot"))
{
log_message("Reboot script terminated abnormally");
send_to_all("The reboot was cancelled.\n\r");
system("mv ./reboot reboot.FAILED");
fclose(boot);
sigsetmask(0);
return;
}
else
system("mv ./reboot reboot.SUCCEEDED");
sigsetmask(0);
}
send_to_all("Automatic reboot. Come back in a little while.\n\r");
clean_shutdown = reboot = 1;
}
else if (t_info->tm_min > 40)
send_to_all("ATTENTION: DikuMUD will reboot in 10 minutes.\n\r");
else if (t_info->tm_min > 30)
send_to_all(
"Warning: The game will close and reboot in 20 minutes.\n\r");
fclose(boot);
}
}
#define GR
#define NEW
#ifdef GR
int workhours()
{
long tc;
struct tm *t_info;
tc = time(0);
t_info = localtime(&tc);
return((t_info->tm_wday > 0) && (t_info->tm_wday < 6) && (t_info->tm_hour >= 9)
&& (t_info->tm_hour < 17));
}
/*
* This procedure is *heavily* system dependent. If your system is not set up
* properly for this particular way of reading the system load (It's weird all
* right - but I couldn't think of anything better), change it, or don't use -l.
* It shouldn't be necessary to use -l anyhow. It's oppressive and unchristian
* to harness man's desire to play. Who needs a friggin' degree, anyhow?
*/
int load(void)
{
struct syslinfo {
char sl_date[12]; /* "Tue Sep 16\0" */
char sl_time[8]; /* "11:10\0" */
char sl_load1[6]; /* "12.0\0" */
char sl_load2[10]; /* "+2.3 14u\0" */
} info;
FILE *fl;
int ld, i, sum;
static int previous[5];
static int p_point = -1;
extern int slow_death;
if (!(fl = fopen("/tmp/.sysline", "r")))
{
perror("sysline. (dying)");
slow_death = 1;
return(-1);
}
if (!fread(&info, sizeof(info), 1, fl))
{
perror("fread sysline (dying)");
slow_death = 1;
return(-1);
}
fclose(fl);
if (p_point < 0)
{
previous[0] = atoi(info.sl_load1);
for (i = 1; i< 5; i++)
previous[i] = previous[0];
p_point = 1;
return(previous[0]);
}
else
{
/* put new figure in table */
previous[p_point] = atoi(info.sl_load1);
if (++p_point > 4)
p_point = 0;
for (i = 0, sum = 0; i < 5; i++)
sum += previous[i];
return((int) sum / 5);
}
}
char *nogames(void)
{
static char text[200];
FILE *fl;
if (fl = fopen("lib/nogames", "r"))
{
log_message("/usr/games/nogames exists");
fgets(text, 200, fl);
return(text);
fclose(fl);
}
else
return(0);
}
void coma_0(void)
{
extern struct descriptor_data *descriptor_list;
extern int tics;
void close_socket(struct descriptor_data *d);
log_message("Entering comatose state");
while (descriptor_list)
close_socket(descriptor_list);
do
{
sleep(300);
tics = 1;
if (workhours())
{
log_message("Working hours collision during coma. Exit.");
exit(0);
}
}
while (load() >= 6);
log_message("Leaving coma");
}
/* emulate the game regulator */
void gr(int s)
{
char *txt = 0, buf[1024];
int ld = 0;
static char *warnings[3] =
{
"If things don't look better within 3 minutes, the game will pause.\n\r",
"The game will close temporarily 2 minutes from now.\n\r",
"WARNING: The game will close in 1 minute.\n\r"
};
static int wnr = 0;
extern int slow_death, clean_shutdown;
void send_to_all(char *messg);
void coma_1(int s);
if (((ld = load()) >= 6) || (txt = nogames()) || slow_death)
{
if (ld >= 6)
{
sprintf(buf, "The system load is greater than 6.0 (%d)\n\r", ld);
send_to_all(buf);
}
else if (slow_death)
send_to_all("The game is dying.\n\r");
else
{
strcpy(buf,
"Game playing is no longer permitted on this machine:\n\r");
strcat(buf, txt);
strcat(buf, "\n\r");
send_to_all(buf);
}
if (wnr < 3)
send_to_all(warnings[wnr++]);
else
if (ld >= 6)
{
coma_1(s);
wnr = 0;
}
else
clean_shutdown = 1;
}
else if (workhours())
clean_shutdown = 1; /* this shouldn't happen */
else if (wnr)
{
send_to_all("Things look brighter now - you can continue playing.\n\r");
wnr = 0;
}
}
#endif