~case/dbns

ref: 85d3c74215baa48cb2fd97d3c85ff6060e7c42b1 dbns/src/pfiles.c -rw-r--r-- 10.1 KiB
85d3c742Zerhyn SBC oopsie 3 years ago
                                                                                
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
/****************************************************************************
 * [S]imulated [M]edieval [A]dventure multi[U]ser [G]ame      |   \\._.//   *
 * -----------------------------------------------------------|   (0...0)   *
 * SMAUG 1.4 (C) 1994, 1995, 1996, 1998  by Derek Snider      |   ) .:.(   *
 * -----------------------------------------------------------|    {o o}    *
 * SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus,      |   / ' ' \   *
 * Scryn, Rennard, Swordbearer, Gorog, Grishnakh, Nivek,      |~'~.VxvxV.~'~*
 * Tricops and Fireblade                                      |             *
 * ------------------------------------------------------------------------ *
 * Merc 2.1 Diku Mud improvments copyright (C) 1992, 1993 by Michael        *
 * Chastain, Michael Quan, and Mitchell Tse.                                *
 * Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,          *
 * Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.     *
 * ------------------------------------------------------------------------ *
 *			   Pfile autocleanup code			    *
 * ------------------------------------------------------------------------ *
 *			  capsule/corp (C)2016, 2017		    	    *
 *                            case@capsulecorp.org			    *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include "mud.h"

extern char *GfpName;
extern bool StopFP;

/* Globals */
time_t 	pfile_time;
HOUR_MIN_SEC set_pfile_time_struct;
HOUR_MIN_SEC *set_pfile_time;
struct tm *new_pfile_time;
struct tm new_pfile_struct;
time_t 	new_pfile_time_t;
sh_int 	num_pfiles;			/* Count up number of pfiles */

#ifndef SOLANCODE
void
save_timedata(void)
{
	FILE   *fp;
	char 	filename[MAX_INPUT_LENGTH];

	if ((fp = fopen(TIME_FILE, "w")) == NULL) {
		bug("save_timedata: fopen");
		perror(filename);
	} else {
		fprintf(fp, "#TIME\n");
		fprintf(fp, "Mhour       %d\n", time_info.hour);
		fprintf(fp, "Mday        %d\n", time_info.day);
		fprintf(fp, "Mmonth      %d\n", time_info.month);
		fprintf(fp, "Myear       %d\n", time_info.year);
		fprintf(fp, "Purgetime   %ld\n", new_pfile_time_t);
		fprintf(fp, "LastRMonth  %d\n", time_info.lastRealMonth);
		fprintf(fp, "End\n\n");
		fprintf(fp, "#END\n");
	}

	FCLOSE(fp);
}

#ifdef KEY
#undef KEY
#endif
#define KEY(literal, field, value) 		\
     if (!str_cmp( word, literal) ) 		\
     {						\
     field  = value;				\
     fMatch = true;				\
     break;					\
     }

/* Reads the actual time file from disk - Samson 1-21-99 */
void
fread_timedata(FILE *fp)
{
	char   *word = NULL;
	bool 	fMatch = false;

	for (;;) {
		word = feof(fp) ? "End" : fread_word(fp);
		fMatch = false;

		switch (UPPER(word[0])) {
		case '*':
			fMatch = true;
			fread_to_eol(fp);
			break;

		case 'E':
			if (!str_cmp(word, "End"))
				return;
			break;

		case 'L':
			KEY("LastRMonth", time_info.hour, fread_number(fp));
		case 'M':
			KEY("Mhour", time_info.hour, fread_number(fp));
			KEY("Mday", time_info.day, fread_number(fp));
			KEY("Mmonth", time_info.month, fread_number(fp));
			KEY("Myear", time_info.year, fread_number(fp));
			break;
		case 'P':
			KEY("Purgetime", new_pfile_time_t, fread_number(fp));
			break;
		}

		if (!fMatch) {
			bug("Fread_timedata: no match: %s", word);
			fread_to_eol(fp);
		}
	}
}

bool
load_timedata(void)
{
	char 	filename[MAX_INPUT_LENGTH];
	FILE   *fp;
	bool 	found;

	found = false;
	sprintf(filename, "%stime.dat", SYSTEM_DIR);

	if ((fp = fopen(filename, "r")) != NULL) {

		found = true;
		for (;;) {
			char 	letter = '\0';
			char   *word = NULL;

			letter = fread_letter(fp);
			if (letter == '*') {
				fread_to_eol(fp);
				continue;
			}
			if (letter != '#') {
				bug("Load_timedata: # not found.");
				break;
			}
			word = fread_word(fp);
			if (!str_cmp(word, "TIME")) {
				fread_timedata(fp);
				break;
			} else if (!str_cmp(word, "END"))
				break;
			else {
				bug("Load_timedata: bad section - %s.", word);
				break;
			}
		}
		FCLOSE(fp);
	}
	return found;
}

#endif

void
init_pfile_scan_time(void)
{
	/*
         * Init pfile scan time.
         */
	set_pfile_time = &set_pfile_time_struct;

	new_pfile_time = update_time(localtime(&current_time));
	/*
	 * Copies *new_pfile_time to new_pfile_struct, and then points
	 * new_pfile_time to new_pfile_struct again. -- Alty
	 */
	new_pfile_struct = *new_pfile_time;
	new_pfile_time = &new_pfile_struct;
	if (new_pfile_time->tm_hour > 5)
		new_pfile_time->tm_mday += 1;
	new_pfile_time->tm_sec = 0;
	new_pfile_time->tm_min = 0;
	new_pfile_time->tm_hour = 5;

	/* Update new_pfile_time (due to day increment) */
	new_pfile_time = update_time(new_pfile_time);
	new_pfile_struct = *new_pfile_time;
	new_pfile_time = &new_pfile_struct;
	/* Bug fix submitted by Gabe Yoder */
	new_pfile_time_t = mktime(new_pfile_time);

#ifndef SOLANCODE
	if (!load_timedata()) {
		strcpy(log_buf, "Pfile scan time reset to default time of 5am.");
		log_string(log_buf);
	}
#endif
}

time_t 	now_time;
sh_int 	deleted = 0;
sh_int 	days = 0;

#if defined(KEY)
#undef KEY
#endif

#define KEY(literal, field, value) 		\
     if (!strcmp( word, literal) ) 		\
     {						\
     field  = value;				\
     fMatch = true;				\
     break;					\
     }

void
fread_pfile(FILE *fp, time_t tdiff, char *fname)
{
	char   *word;
	char   *name = NULL;
	sh_int 	level = 0;
	sh_int 	file_ver = 0;
	bool 	fMatch;
	long double exp = 0;
	char   *description;

	for (;;) {
		word = feof(fp) ? "End" : fread_word(fp);
		fMatch = false;

		if (StopFP) {
			bug("Bad Pfile detected.  Stoping proccessing of bad Pfile.");
			StopFP = false;
			return;
		}
		switch (UPPER(word[0])) {
		case '*':
			fMatch = true;
			fread_to_eol(fp);
			break;

		case 'B':
			if (!strcmp(word, "Bio"))
				goto timecheck;
			break;
		case 'D':
			KEY("Description", description, fread_string(fp));
			break;

		case 'E':
			KEY("Exp", exp, fread_number_ld(fp));
			if (!strcmp(word, "End"))
				goto timecheck;
			break;

		case 'L':
			KEY("Level", level, fread_number(fp));
			break;

		case 'N':
			KEY("Name", name, fread_string(fp));
			break;

		case 'V':
			KEY("Version", file_ver, fread_number(fp));
			break;
		}

		if (!fMatch)
			fread_to_eol(fp);
	}

timecheck:
	if ((exp < 5000 && tdiff > sysdata.newbie_purge && level < LEVEL_IMMORTAL)
	    || (level < LEVEL_IMMORTAL && tdiff > sysdata.regular_purge)) {
		if (level < LEVEL_IMMORTAL) {
			if (unlink(fname) == -1)
				perror("Unlink");
			else {
				if (exp < 5000)
					days = sysdata.newbie_purge;
				else
					days = sysdata.regular_purge;
				sprintf(log_buf, "Player %s was deleted. Exceeded time limit of %d days.", name, days);
				log_string(log_buf);
				++deleted;
				return;
			}
		}
	}
}

void
read_pfile(char *dirname, char *filename)
{
	FILE   *fp;
	char 	fname[MAX_STRING_LENGTH];
	struct stat fst;
	time_t 	tdiff;

	now_time = time(0);

	sprintf(fname, "%s/%s", dirname, filename);
	GfpName = fname;
	if (stat(fname, &fst) != -1) {
		tdiff = (now_time - fst.st_mtime) / 86400;

		if ((fp = fopen(fname, "r")) != NULL) {
			for (;;) {
				char 	letter;
				char   *word;

				if (StopFP) {
					bug("Bad Pfile detected.  Stoping proccessing of bad Pfile.");
					StopFP = false;
					return;
				}
				letter = fread_letter(fp);

				if (letter != '#')
					continue;

				word = fread_word(fp);

				if (!str_cmp(word, "End"))
					break;

				if (!str_cmp(word, "PLAYER"))
					fread_pfile(fp, tdiff, fname);
				else if (!str_cmp(word, "END"))	/* Done		 */
					break;
			}
			FCLOSE(fp);
		}
	}
	return;
}

void
pfile_scan(bool count)
{
	DIR    *dp;
	struct dirent *dentry;
	char 	dir_name[100];

	sh_int 	alpha_loop;
	sh_int 	cou = 0;

	now_time = time(0);

	for (alpha_loop = 0; alpha_loop <= 25; alpha_loop++) {
		sprintf(dir_name, "%s%c", PLAYER_DIR, 'a' + alpha_loop);
		dp = opendir(dir_name);
		if (dp != NULL) {
			dentry = readdir(dp);
			while (dentry) {
				if (dentry->d_name[0] != '.') {
					if (!count)
						read_pfile(dir_name, dentry->d_name);
					cou++;
				}
				dentry = readdir(dp);
			}
			closedir(dp);
		} else {
			continue;
		}
	}

	if (!count)
		log_string("Pfile cleanup completed.");
	else
		log_string("Pfile count completed.");

	sprintf(log_buf, "Total pfiles scanned: %d", cou);
	log_string(log_buf);

	if (!count) {
		sprintf(log_buf, "Total pfiles deleted: %d", deleted);
		log_string(log_buf);

		sprintf(log_buf, "Total pfiles remaining: %d", cou - deleted);
		num_pfiles = cou - deleted;
		log_string(log_buf);
	} else {
		num_pfiles = cou;
	}
}

void 
do_pfiles(CHAR_DATA * ch, char *argument)
{
	if (IS_NPC(ch)) {
		send_to_char("Mobs cannot use this command!\n\r", ch);
		return;
	}
	if (argument[0] == '\0' || !argument) {
		sprintf(log_buf, "Manual pfile cleanup started by %s.", ch->name);
		log_string(log_buf);
		pfile_scan(false);
		return;
	}
	if (!str_cmp(argument, "settime")) {
		new_pfile_time_t = current_time + 86400;
		save_timedata();
		send_to_char("New cleanup time set for 24 hrs from now.\n\r", ch);
		return;
	}
	if (!str_cmp(argument, "count")) {
		sprintf(log_buf, "Pfile count started by %s.", ch->name);
		log_string(log_buf);
		pfile_scan(true);
		return;
	}
	if (!str_cmp(argument, "wipe")) {
		sprintf(log_buf, "Pfile wipe started by %s.", ch->name);
		log_string(log_buf);
		return;
	}
	send_to_char("Invalid argument.\n\r", ch);
}

void
check_pfiles(time_t reset)
{

	/*
	 * This only counts them up on reboot if the cleanup isn't needed -
	 * Samson 1-2-00
	 */
	if (reset == 255 && new_pfile_time_t > current_time) {
		reset = 0;		/* Call me paranoid, but it might be
					 * meaningful later on */
		log_string("Counting pfiles.....");
		pfile_scan(true);
		return;
	}
	if (new_pfile_time_t <= current_time) {
		if (sysdata.CLEANPFILES == true) {
			char 	buf[MAX_STRING_LENGTH];

			system(buf);

			new_pfile_time_t = current_time + 86400;
			save_timedata();
			log_string("Automated pfile cleanup beginning....");
			pfile_scan(false);
		} else {
			new_pfile_time_t = current_time + 86400;
			save_timedata();
			log_string("Counting pfiles.....");
			pfile_scan(true);
		}
	}
}