umonitor.c 24.7 KB
Newer Older
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
/* umonitor.c */

/* Synchronet for *nix node activity monitor */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

38
#include "sbbs.h"
39
40
#include "conwrap.h"	/* this has to go BEFORE curses.h so getkey() can be macroed around */
#include <curses.h>
41
#include <signal.h>
deuce's avatar
deuce committed
42
#include <sys/types.h>
43
#include <time.h>
deuce's avatar
deuce committed
44
45
46
47
#include <sys/time.h>
#ifdef __QNX__
#include <string.h>
#endif
48
#include <stdio.h>
49
#include <unistd.h>
50
#include "genwrap.h"
51
52
53
#include "uifc.h"
#include "sbbsdefs.h"
#include "genwrap.h"	/* stricmp */
54
#include "dirwrap.h"	/* lock/unlock/sopen */
55
#include "filewrap.h"	/* lock/unlock/sopen */
56
57
58
#include "sbbs_ini.h"	/* INI parsing */
#include "scfglib.h"	/* SCFG files */
#include "ars_defs.h"	/* needed for SCFG files */
59
#include "userdat.h"	/* getnodedat() */
60
#include "spyon.h"
61
#include "chat.h"
62

63
#define CTRL(x) (x&037)
64
65
66
67
68

/********************/
/* Global Variables */
/********************/
uifcapi_t uifc; /* User Interface (UIFC) Library API */
69
70
const char *YesStr="Yes";
const char *NoStr="No";
71
72
73
74
75
76
77
78
79
80
81
82
83
84

int lprintf(char *fmt, ...)
{
	va_list argptr;
	char sbuf[1024];
	int	len;

	va_start(argptr,fmt);
	len=vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;
	va_end(argptr);
	uifc.msg(sbuf);
	return(len);
}
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
void bail(int code)
{
    if(code) {
        puts("\nHit a key...");
        getch(); 
	}
    uifc.bail();

    exit(code);
}

void allocfail(uint size)
{
    printf("\7Error allocating %u bytes of memory.\n",size);
    bail(1);
101
102
}

103
104
void node_toggles(scfg_t *cfg,int nodenum)  {
	int nodefile;
105
106
107
108
109
110
111
112
113
114
115
116
117
	char**	opt;
	int		i,j;
	node_t	node;
	int		save=0;

	if((opt=(char **)MALLOC(sizeof(char *)*(MAX_OPTS+1)))==NULL)
		allocfail(sizeof(char *)*(MAX_OPTS+1));
	for(i=0;i<(MAX_OPTS+1);i++)
		if((opt[i]=(char *)MALLOC(MAX_OPLN))==NULL)
			allocfail(MAX_OPLN);

	i=0;
	uifc.helpbuf=	"`Node Toggles:`\n"
118
					"\nToDo: Add help (Mention that changes take effect immediately)";
119
	while(save==0) {
120
		if(getnodedat(cfg,nodenum,&node,&nodefile)) {
121
122
123
			uifc.msg("Error reading node data!");
			break;
		}
124
125
126
		j=0;
		sprintf(opt[j++],"%-30s%3s","Locked for SysOps only",node.misc&NODE_LOCK ? YesStr : NoStr);
		sprintf(opt[j++],"%-30s%3s","Interrupt (Hangup)",node.misc&NODE_INTR ? YesStr : NoStr);
rswindell's avatar
rswindell committed
127
128
129
		sprintf(opt[j++],"%-30s%3s","Re-run on logoff",node.misc&NODE_RRUN ? YesStr : NoStr);
		sprintf(opt[j++],"%-30s%3s","Down node after logoff"
			,(node.misc&NODE_DOWN || (node.status==NODE_OFFLINE)) ? YesStr : NoStr);
130
131
132
133
134
135
136
137
138
139
140
		opt[j][0]=0;

		switch(uifc.list(WIN_MID,0,0,0,&i,0,"Node Toggles",opt)) {
			case 0:	/* Locked */
				node.misc ^= NODE_LOCK;
				break;

			case 1:	/* Interrupt */
				node.misc ^= NODE_INTR;
				break;

rswindell's avatar
rswindell committed
141
			case 2:	/* Re-run */
142
143
144
				node.misc ^= NODE_RRUN;
				break;

rswindell's avatar
rswindell committed
145
			case 3:	/* Down */
146
				if(node.status != NODE_WFC && node.status != NODE_OFFLINE)
147
					node.misc ^= NODE_DOWN;
148
				else {
149
150
151
152
153
					if(node.status!=NODE_OFFLINE)
						node.status=NODE_OFFLINE;
					else
						node.status=NODE_WFC;
				}
154
155
156
157
158
159
160
161
162
163
				break;

			case -1:
				save=1;
				break;
				
			default:
				uifc.msg("Option not implemented");
				continue;
		}
164
		putnodedat(cfg,nodenum,&node,nodefile);
deuce's avatar
deuce committed
165
	}
deuce's avatar
deuce committed
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
}

int dospy(int nodenum, bbs_startup_t *bbs_startup)  {
	char str[80],str2[80];
	int i;

	if(bbs_startup->temp_dir[0])
    	snprintf(str,sizeof(str),"%slocalspy%d.sock", bbs_startup->temp_dir, nodenum);
	else
		snprintf(str,sizeof(str),"%slocalspy%d.sock", bbs_startup->ctrl_dir, nodenum);
	endwin();
	i=spyon(str);
	refresh();
	switch(i) {
		case SPY_NOSOCKET:
			uifc.msg("Could not create socket");
			return(-1);
					
		case SPY_NOCONNECT:
			sprintf(str2,"Failed to connect to %s",str);
			uifc.msg(str2);
			return(-1);

		case SPY_SELECTFAILED:
			uifc.msg("select() failed, connection terminated.");
			return(-1);

		case SPY_SOCKETLOST:
			uifc.msg("Spy socket lost");
			return(-1);
							
		case SPY_STDINLOST:
			uifc.msg("STDIN has gone away... you probably can't close this window.  :-)");
			return(-1);
							
		case SPY_CLOSED:
			break;
			
		default:
			sprintf(str,"Unknown return code %d",i);
			uifc.msg(str);
			return(-1);
	}
	return(0);
}

212
int sendmessage(scfg_t *cfg, int nodenum,node_t *node)  {
213
214
215
	char str[80],str2[80];

	uifc.input(WIN_MID,0,0,"Telegram",str2,58,K_WRAP|K_MSG);
216
	sprintf(str,"\1n\1y\1hMessage From Sysop:\1w %s\r\n",str2);
217
	if(getnodedat(cfg,nodenum,node,NULL))
218
219
220
		return(-1);
	if(node->useron==0)
		return(-1);
221
	putsmsg(cfg, node->useron, str);
222
223
224
	return(0);
}

225
226
227
int clearerrors(scfg_t *cfg, int nodenum, node_t *node) {
	int nodefile;
	if(getnodedat(cfg,nodenum,node,&nodefile)) {
228
229
230
231
		uifc.msg("getnodedat() failed! (Nothing done)");
		return(-1);
	}
	node->errors=0;
232
	putnodedat(cfg,nodenum,node,nodefile);
233
234
235
	return(0);
}

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
/* Assumes a 12 char outstr */
char *getsizestr(char *outstr, long size, BOOL bytes) {
	if(bytes) {
		if(size < 1000) {	/* Bytes */
			snprintf(outstr,12,"%ld bytes",size);
			return(outstr);			
		}
		if(size<10000) {	/* Bytes with comma */
			snprintf(outstr,12,"%ld,%03ld bytes",(size/1000),(size%1000));
			return(outstr);			
		}
		size = size/1024;
	}
	if(size<1000) {	/* KB */
		snprintf(outstr,12,"%ld KB",size);
		return(outstr);			
	}
	if(size<999999) { /* KB With comma */
		snprintf(outstr,12,"%ld,%03ld KB",(size/1000),(size%1000));
		return(outstr);			
	}
	size = size/1024;
	if(size<1000) {	/* MB */
		snprintf(outstr,12,"%ld MB",size);
		return(outstr);			
	}
	if(size<999999) { /* MB With comma */
		snprintf(outstr,12,"%ld,%03ld MB",(size/1000),(size%1000));
		return(outstr);			
	}
	size = size/1024;
	if(size<1000) {	/* GB */
		snprintf(outstr,12,"%ld GB",size);
		return(outstr);			
	}
	if(size<999999) { /* GB With comma */
		snprintf(outstr,12,"%ld,%03ld GB",(size/1000),(size%1000));
		return(outstr);			
	}
	size = size/1024;
	if(size<1000) {	/* TB (Yeah, right) */
		snprintf(outstr,12,"%ld TB",size);
		return(outstr);			
	}
	sprintf(outstr,"Plenty");
	return(outstr);			
}

/* Assumes a 12 char outstr */
char *getnumstr(char *outstr, ulong size) {
	if(size < 1000) {
		snprintf(outstr,12,"%ld",size);
		return(outstr);			
	}
	if(size<1000000) {
		snprintf(outstr,12,"%ld,%03ld",(size/1000),(size%1000));
		return(outstr);			
	}
	if(size<1000000000) {
		snprintf(outstr,12,"%ld,%03ld,%03ld",(size/1000000),((size/1000)%1000),(size%1000));
		return(outstr);			
	}
	size=size/1000000;
	if(size<1000000) {
		snprintf(outstr,12,"%ld,%03ld M",(size/1000),(size%1000));
		return(outstr);			
	}
	if(size<10000000) {
		snprintf(outstr,12,"%ld,%03ld,%03ld M",(size/1000000),((size/1000)%1000),(size%1000));
		return(outstr);			
	}
	sprintf(outstr,"Plenty");
	return(outstr);			
}

311
int drawstats(scfg_t *cfg, int nodenum, node_t *node, int *curp, int *barp) {
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	stats_t	sstats;
	stats_t	nstats;
	char	statbuf[6*78];		/* Buffer to hold the stats for passing to uifc.showbuf() */
	char	str[4][4][12];
	char	usrname[128];
	ulong	free;
	uint	i,l,m;
	time_t	t;

	if(getnodedat(cfg,nodenum,node,NULL)) {
		return(-1);
	}
	username(cfg,node->useron,usrname);

	getstats(cfg, 0, &sstats);
	getstats(cfg, nodenum, &nstats);
	t=time(NULL);
	strftime(str[0][0],12,"%b %e",localtime(&t));
	free=getfreediskspace(cfg->temp_dir,1024);
	if(free<1000) {
		free=getfreediskspace(cfg->temp_dir,0);
		getsizestr(str[0][1],free,TRUE);
334
	}
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
	else
		getsizestr(str[0][1],free,FALSE);
	snprintf(str[1][0],12,"%s/%s",getnumstr(str[3][2],nstats.ltoday),getnumstr(str[3][3],sstats.ltoday));
	getnumstr(str[1][1],sstats.logons);
	snprintf(str[1][2],12,"%s/%s",getnumstr(str[3][2],nstats.ttoday),getnumstr(str[3][3],sstats.ttoday));
	getnumstr(str[1][3],sstats.timeon);
	snprintf(str[2][0],12,"%s/%s",getnumstr(str[3][2],sstats.etoday),getnumstr(str[3][3],getmail(cfg,0,0)));
	l=m=0;
	for(i=0;i<cfg->total_subs;i++)
		l+=getposts(cfg,i); 			/* l=total posts */
	for(i=0;i<cfg->total_dirs;i++)
		m+=getfiles(cfg,i); 			/* m=total files */
	snprintf(str[2][1],12,"%s/%s",getnumstr(str[3][2],sstats.ptoday),getnumstr(str[3][3],l));
	snprintf(str[2][2],12,"%s/%s",getnumstr(str[3][2],sstats.ftoday),getnumstr(str[3][3],getmail(cfg,1,0)));
	snprintf(str[2][3],12,"%s/%s",getnumstr(str[3][2],sstats.nusers),getnumstr(str[3][3],total_users(cfg)));
	getsizestr(str[3][0],sstats.ulb,TRUE);
	snprintf(str[3][1],12,"%s/%s",getnumstr(str[3][2],sstats.uls),getnumstr(str[3][3],m));
	getsizestr(str[3][2],sstats.dlb,TRUE);
	getnumstr(str[3][3],sstats.dls);
deuce's avatar
deuce committed
354
355
356
357
	snprintf(statbuf,sizeof(statbuf),"`Node #`: %-3d %6s  `Space`: %s"
			"\n`Logons`: %-11s `Total`: %-11s `Timeon`: %-11s `Total`: %-11s"
			"\n`Emails`: %-11s `Posts`: %-11s `Fbacks`: %-11s `Users`: %-11s"
			"\n`Uloads`: %-11s `Files`: %-11s `Dloads`: %-11s `Files`: %-11s",
358
359
360
361
362
			nodenum,str[0][0],str[0][1],
			str[1][0],str[1][1],str[1][2],str[1][3],
			str[2][0],str[2][1],str[2][2],str[2][3],
			str[3][0],str[3][1],str[3][2],str[3][3]);

deuce's avatar
deuce committed
363
	uifc.showbuf(WIN_HLP|WIN_L2R|WIN_DYN|WIN_PACK,1,1,80,6,"Statistics",statbuf,curp,barp);
364
365
366
367
368
369
370
/* Node 5 :	Mar 11  Space: 162,024k
   Logons: 23/103      Total: 62,610      Timeon: 322/2430    Total: 5,321,900   
   Emails: 4/265       Posts: 4/12811     Fbacks: 2/17	       Users: 1/592
   Uloads: 324k        Files: 1/2195      Dloads: 9,308k      Files: 52 */
	return(0);
}

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
int view_log(char *filename, char *title)
{
	char str[1024];
	int buffile;
	int j;
	char *buf;

	if(fexist(filename)) {
		if((buffile=sopen(filename,O_RDONLY,SH_DENYWR))>=0) {
			j=filelength(buffile);
			if((buf=(char *)MALLOC(j+1))!=NULL) {
				read(buffile,buf,j);
				close(buffile);
				*(buf+j)=0;
				uifc.showbuf(WIN_MID,0,0,76,uifc.scrn_len-2,title,buf,NULL,NULL);
				free(buf);
				return(0);
			}
			close(buffile);
			uifc.msg("Error allocating memory for the error log");
			return(3);
		}
		sprintf(str,"Error opening %s",title);
		uifc.msg(str);
		return(1);
	}
	sprintf(str,"%s does not exists",title);
	uifc.msg(str);
	return(2);
}

int view_logs(scfg_t *cfg)
{
	char**	opt;
	int		i;
	char	str[1024];
	struct tm tm;
	struct tm tm_yest;
	time_t	now;

	now=time(NULL);
	localtime_r(&now,&tm);
	now -= 60*60*24;
	localtime_r(&now,&tm_yest);
	if((opt=(char **)MALLOC(sizeof(char *)*(MAX_OPTS+1)))==NULL)
		allocfail(sizeof(char *)*(MAX_OPTS+1));
	for(i=0;i<(MAX_OPTS+1);i++)
		if((opt[i]=(char *)MALLOC(MAX_OPLN))==NULL)
			allocfail(MAX_OPLN);

	i=0;
	strcpy(opt[i++],"Todays callers");
	strcpy(opt[i++],"Yesterdays callers");
	strcpy(opt[i++],"Error log");
	strcpy(opt[i++],"Todays log");
	strcpy(opt[i++],"Yesterdays log");
	strcpy(opt[i++],"Spam log");
	strcpy(opt[i++],"SBBSEcho log");
	strcpy(opt[i++],"Guru log");
	strcpy(opt[i++],"Hack log");
	opt[i][0]=0;
	i=0;
	while(1) {
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"System Options",opt))  {
			case -1:
				return(0);
			case 0:
				sprintf(str,"%slogs/%2.2d%2.2d%2.2d.LOL",cfg->logs_dir,tm.tm_mon+1,tm.tm_mday
					,TM_YEAR(tm.tm_year));
				view_log(str,"Todays Callers");
				break;
			case 1:
				sprintf(str,"%slogs/%2.2d%2.2d%2.2d.LOL",cfg->logs_dir,tm_yest.tm_mon+1
					,tm_yest.tm_mday,TM_YEAR(tm_yest.tm_year));
				view_log(str,"Yesterdays Callers");
				break;
			case 2:
				sprintf(str,"%s/error.log",cfg->logs_dir);
				view_log(str,"Error Log");
				break;
			case 3:
				sprintf(str,"%slogs/%2.2d%2.2d%2.2d.log",cfg->logs_dir,tm.tm_mon+1,tm.tm_mday
					,TM_YEAR(tm.tm_year));
				view_log(str,"Todays Log");
				break;
			case 4:
				sprintf(str,"%slogs/%2.2d%2.2d%2.2d.log",cfg->logs_dir,tm_yest.tm_mon+1
					,tm_yest.tm_mday,TM_YEAR(tm_yest.tm_year));
				view_log(str,"Yesterdays Log");
				break;
			case 5:
				sprintf(str,"%sspam.log",cfg->logs_dir);
				view_log(str,"SPAM Log");
				break;
			case 6:
				sprintf(str,"%ssbbsecho.log",cfg->logs_dir);
				view_log(str,"SBBSEcho Log");
				break;
			case 7:
				sprintf(str,"%sguru.log",cfg->logs_dir);
				view_log(str,"Guru Log");
				break;
			case 8:
				sprintf(str,"%shack.log",cfg->logs_dir);
				view_log(str,"Hack Log");
				break;
		}
	}
}

int do_cmd(char *cmd)
{
	int i;

	endwin();
	i=system(cmd);
	refresh();
	return(i);
}

int qwk_callouts(void)
{
	return(0);
}

int run_events(void)
{
	return(0);
}

int recycle_servers(void)
{
	return(0);
}

int edit_cfg(scfg_t *cfg)
{
	char**	opt;
	int		i;
	char	cmd[1024];

	if(getenv("EDITOR")==NULL) {
		uifc.msg("EDITOR environment variable not found");
		return(1);
	}
	if((opt=(char **)MALLOC(sizeof(char *)*(MAX_OPTS+1)))==NULL)
		allocfail(sizeof(char *)*(MAX_OPTS+1));
	for(i=0;i<(MAX_OPTS+1);i++)
		if((opt[i]=(char *)MALLOC(MAX_OPLN))==NULL)
			allocfail(MAX_OPLN);

	i=0;
	strcpy(opt[i++],"alias.cfg");
	strcpy(opt[i++],"attr.cfg");
	strcpy(opt[i++],"dns_blacklist.cfg");
	strcpy(opt[i++],"dnsbl_exempt.cfg");
	strcpy(opt[i++],"domains.cfg");
	strcpy(opt[i++],"mailproc.cfg");
	strcpy(opt[i++],"mime_types.cfg");
	strcpy(opt[i++],"relay.cfg");
	strcpy(opt[i++],"sbbsecho.cfg");
	strcpy(opt[i++],"services.cfg");
	strcpy(opt[i++],"ftpalias.cfg");
	strcpy(opt[i++],"sockopts.cfg");
	strcpy(opt[i++],"spambait.cfg");
	strcpy(opt[i++],"spamblock.cfg");
	strcpy(opt[i++],"twitlist.cfg");
	opt[i][0]=0;
	i=0;
	while(1) {
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"System Options",opt))  {
			case -1:
				return(0);
			default:
				sprintf(cmd,"%s %s%s",getenv("EDITOR"),cfg->ctrl_dir,opt[i]);
				do_cmd(cmd);
				break;
		}
	}
	return(0);
}
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
int edit_can(scfg_t *cfg)
{
	char**	opt;
	int		i;
	char	cmd[1024];

	if(getenv("EDITOR")==NULL) {
		uifc.msg("EDITOR environment variable not found");
		return(1);
	}
	if((opt=(char **)MALLOC(sizeof(char *)*(MAX_OPTS+1)))==NULL)
		allocfail(sizeof(char *)*(MAX_OPTS+1));
	for(i=0;i<(MAX_OPTS+1);i++)
		if((opt[i]=(char *)MALLOC(MAX_OPLN))==NULL)
			allocfail(MAX_OPLN);

	i=0;
	strcpy(opt[i++],"email.can");
	strcpy(opt[i++],"file.can");
	strcpy(opt[i++],"host.can");
	strcpy(opt[i++],"ip.can");
	strcpy(opt[i++],"ip-silent.can");
	strcpy(opt[i++],"name.can");
	strcpy(opt[i++],"phone.can");
	strcpy(opt[i++],"rlogin.can");
	strcpy(opt[i++],"subject.can");
	opt[i][0]=0;
	i=0;
	while(1) {
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"System Options",opt))  {
			case -1:
				return(0);
			default:
				sprintf(cmd,"%s %s%s",getenv("EDITOR"),cfg->ctrl_dir,opt[i]);
				do_cmd(cmd);
				break;
		}
	}
	return(0);
}
593

594
int main(int argc, char** argv)  {
595
596
597
598
	char**	opt;
	char**	mopt;
	int		main_dflt=0;
	int		main_bar=0;
deuce's avatar
deuce committed
599
600
601
602
603
	char	revision[16];
	char	str[256],ctrl_dir[41],*p;
	char	title[256];
	int		i,j;
	node_t	node;
604
605
606
	int		nodefile;
	box_t	boxch;
	scfg_t	cfg;
607
	int		done;
deuce's avatar
deuce committed
608
609
610
	/******************/
	/* Ini file stuff */
	/******************/
611
612
613
	char	ini_file[MAX_PATH+1];
	FILE*				fp;
	bbs_startup_t		bbs_startup;
614
615

	sscanf("$Revision$", "%*s %s", revision);
616

617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
    printf("\nSynchronet UNIX Monitor %s-%s  Copyright 2003 "
        "Rob Swindell\n",revision,PLATFORM_DESC);

	p=getenv("SBBSCTRL");
	if(p==NULL) {
		printf("\7\nSBBSCTRL environment variable is not set.\n");
		printf("This environment variable must be set to your CTRL directory.");
		printf("\nExample: SET SBBSCTRL=/sbbs/ctrl\n");
		exit(1); }

	sprintf(ctrl_dir,"%.40s",p);
	if(ctrl_dir[strlen(ctrl_dir)-1]!='\\'
		&& ctrl_dir[strlen(ctrl_dir)-1]!='/')
		strcat(ctrl_dir,"/");

deuce's avatar
deuce committed
632
	gethostname(str,sizeof(str)-1);
633
634

	sbbs_get_ini_fname(ini_file, ctrl_dir, str);
635
636
637
638
639
640
641
642
643
644
645
646

	/* Initialize BBS startup structure */
    memset(&bbs_startup,0,sizeof(bbs_startup));
    bbs_startup.size=sizeof(bbs_startup);
    strcpy(bbs_startup.ctrl_dir,ctrl_dir);

	/* Read .ini file here */
	if(ini_file[0]!=0 && (fp=fopen(ini_file,"r"))!=NULL) {
		printf("Reading %s\n",ini_file);
	}
	/* We call this function to set defaults, even if there's no .ini file */
	sbbs_read_ini(fp, 
647
		NULL, &bbs_startup, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
648
649
650
651
652
653
654
655

	/* close .ini file here */
	if(fp!=NULL)
		fclose(fp);

	chdir(bbs_startup.ctrl_dir);
	
	/* Read .cfg files here */
656
    memset(&cfg,0,sizeof(cfg));
657
	cfg.size=sizeof(cfg);
658
659
	SAFECOPY(cfg.ctrl_dir,bbs_startup.ctrl_dir);
	if(!load_cfg(&cfg, NULL, TRUE, str)) {
660
661
662
		printf("ERROR! %s\n",str);
		exit(1);
	}
663
	prep_dir(cfg.data_dir, cfg.temp_dir, sizeof(cfg.temp_dir));
664

665
666
667
668
    memset(&uifc,0,sizeof(uifc));

	uifc.esc_delay=500;

deuce's avatar
deuce committed
669
670
671
672
673
674
675
676
	boxch.ls=ACS_VLINE; 
	boxch.rs=ACS_VLINE;
	boxch.ts=ACS_HLINE;
	boxch.bs=ACS_HLINE;
	boxch.tl=ACS_ULCORNER;
	boxch.tr=ACS_URCORNER;
	boxch.bl=ACS_LLCORNER;
	boxch.br=ACS_LRCORNER;
677
678
679
680
681
682
683
684
685
686
687
688
689
690
	for(i=1;i<argc;i++) {
        if(argv[i][0]=='-'
            )
            switch(toupper(argv[i][1])) {
                case 'C':
        			uifc.mode|=UIFC_COLOR;
                    break;
                case 'L':
                    uifc.scrn_len=atoi(argv[i]+2);
                    break;
                case 'E':
                    uifc.esc_delay=atoi(argv[i]+2);
                    break;
				case 'I':
691
					/* Set up ex-ascii codes */
deuce's avatar
deuce committed
692
693
694
695
696
697
698
699
					boxch.ls=186; 
					boxch.rs=186;
					boxch.ts=205;
					boxch.bs=205;
					boxch.tl=201;
					boxch.tr=187;
					boxch.bl=200;
					boxch.br=188;
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
					uifc.mode|=UIFC_IBM;
					break;
                default:
                    printf("\nusage: %s [ctrl_dir] [options]"
                        "\n\noptions:\n\n"
                        "-c  =  force color mode\n"
                        "-e# =  set escape delay to #msec\n"
						"-i  =  force IBM charset\n"
                        "-l# =  set screen lines to #\n"
						,argv[0]
                        );
        			exit(0);
           }
    }

715
716
	signal(SIGPIPE, SIG_IGN);   

717
	uifc.size=sizeof(uifc);
deuce's avatar
deuce committed
718
#ifdef USE_CURSES
719
	i=uifcinic(&uifc);  /* curses */
deuce's avatar
deuce committed
720
721
722
#else
	i=uifcini32(&uifc);  /* curses */
#endif
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
	if(i!=0) {
		printf("uifc library init returned error %d\n",i);
		exit(1);
	}

	if((opt=(char **)MALLOC(sizeof(char *)*(MAX_OPTS+1)))==NULL)
		allocfail(sizeof(char *)*(MAX_OPTS+1));
	for(i=0;i<(MAX_OPTS+1);i++)
		if((opt[i]=(char *)MALLOC(MAX_OPLN))==NULL)
			allocfail(MAX_OPLN);

	if((mopt=(char **)MALLOC(sizeof(char *)*MAX_OPTS))==NULL)
		allocfail(sizeof(char *)*MAX_OPTS);
	for(i=0;i<MAX_OPTS;i++)
		if((mopt[i]=(char *)MALLOC(MAX_OPLN))==NULL)
			allocfail(MAX_OPLN);

740
741
	sprintf(title,"Synchronet UNIX Monitor %s-%s",revision,PLATFORM_DESC);
	if(uifc.scrn(title)) {
742
743
744
745
746
		printf(" USCRN (len=%d) failed!\n",uifc.scrn_len+1);
		bail(1);
	}

	while(1) {
747
		strcpy(mopt[0],"System Options");
748
749
		for(i=1;i<=cfg.sys_nodes;i++) {
			if((j=getnodedat(&cfg,i,&node,NULL)))
750
				sprintf(mopt[i],"Error reading node data (%d)!",j);
751
			else
752
				sprintf(mopt[i],"%3d: %s",i,nodestatus(&cfg,&node,str,71));
753
		}
deuce's avatar
deuce committed
754
		mopt[i-1][0]=0;
755
756

		uifc.helpbuf=	"`Synchronet Monitor:`\n"
deuce's avatar
deuce committed
757
						"\nCTRL-E displays the error log"
758
759
						"\nF12 spys on the currently selected node"
						"\nF11 send message to the currently selected node"
760
						"\nF10 Chats with the user on the currently selected node"
761
762
763
764
765
766
						"\nDEL Clear errors on currently selected node"
						"\nCTRL-L Lock node toggle"
						"\nCTRL-R Rerun node"
						"\nCTRL-D Down node toggle"
						"\nCTRL-I Interrupt node"
						"\nToDo: Add more help. (Explain what you're looking at)";
deuce's avatar
deuce committed
767

768
		drawstats(&cfg, main_dflt, &node, &main_dflt, &main_bar);
deuce's avatar
deuce committed
769

770
		j=uifc.list(WIN_L2R|WIN_ESC|WIN_ACT|WIN_DYN,0,5,70,&main_dflt,&main_bar
771
772
			,title,mopt);

deuce's avatar
deuce committed
773
774
775
		if(j == -2)
			continue;

776
		if(j==-7) {	/* CTRL-E */
777
			sprintf(str,"%s/error.log",cfg.data_dir);
778
			view_log(str,"Error Log");
779
780
			continue;
		}
deuce's avatar
deuce committed
781
		
782
		if(j==-2-KEY_DC) {	/* Clear errors */
783
			clearerrors(&cfg, main_dflt+1,&node);
784
785
786
787
			continue;
		}

		if(j==-2-KEY_F(10)) {	/* Chat */
788
789
790
791
			if(getnodedat(&cfg,main_dflt+1,&node,NULL)) {
				uifc.msg("Error reading node data!");
				continue;
			}
792
			if((node.status==NODE_INUSE) && node.useron)
793
				chat(&cfg,main_dflt+1,&node,&boxch,uifc.timedisplay);
794
795
796
797
			continue;
		}

		if(j==-2-KEY_F(11)) {	/* Send message */
798
			sendmessage(&cfg, main_dflt+1,&node);
799
800
801
802
			continue;
		}
		
		if(j==-2-KEY_F(12)) {	/* Spy */
deuce's avatar
deuce committed
803
			dospy(main_dflt+1,&bbs_startup);
804
805
806
807
808
809
810
811
812
813
814
			continue;
		}
		
		if(j==-2-CTRL('l')) {	/* Lock node */
			if(getnodedat(&cfg,main_dflt+1,&node,&nodefile)) {
				uifc.msg("Error reading node data!");
				continue;
			}
			node.misc^=NODE_LOCK;
			putnodedat(&cfg,main_dflt+1,&node,nodefile);
			continue;
deuce's avatar
deuce committed
815
816
		}
		
817
818
819
820
821
822
823
824
825
		if(j==-2-CTRL('r')) {	/* Rerun node */
			if(getnodedat(&cfg,main_dflt+1,&node,&nodefile)) {
				uifc.msg("Error reading node data!");
				continue;
			}
			node.misc^=NODE_RRUN;
			putnodedat(&cfg,main_dflt+1,&node,nodefile);
			continue;
		}
826

827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
		if(j==-2-CTRL('d')) {	/* Down node */
			if(getnodedat(&cfg,main_dflt+1,&node,&nodefile)) {
				uifc.msg("Error reading node data!");
				continue;
			}
			if(node.status != NODE_WFC && node.status != NODE_OFFLINE)
				node.misc ^= NODE_DOWN;
			else {
				if(node.status!=NODE_OFFLINE)
					node.status=NODE_OFFLINE;
				else
					node.status=NODE_WFC;
			}
			putnodedat(&cfg,main_dflt+1,&node,nodefile);
			continue;
		}

		if(j==-2-CTRL('i')) {	/* Interrupt node */
			if(getnodedat(&cfg,main_dflt+1,&node,&nodefile)) {
				uifc.msg("Error reading node data!");
				continue;
			}
			node.misc^=NODE_INTR;
			putnodedat(&cfg,main_dflt+1,&node,nodefile);
			continue;
		}
		
854
		if(j <= -2)
855
856
			continue;

deuce's avatar
deuce committed
857
		if(j==-1) {
858
			i=0;
859
860
			strcpy(opt[0],YesStr);
			strcpy(opt[1],NoStr);
861
862
863
864
865
			opt[2][0]=0;
			uifc.helpbuf=	"`Exit Synchronet UNIX Monitor:`\n"
							"\n"
							"\nIf you want to exit the Synchronet UNIX monitor utility,"
							"\nselect `Yes`. Otherwise, select `No` or hit ~ ESC ~.";
deuce's avatar
deuce committed
866
			i=uifc.list(WIN_MID,0,0,0,&i,0,"Exit Synchronet Monitor",opt);
867
868
869
870
			if(!i)
				bail(0);
			continue;
		}
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919

		if(j==0) {
			/* System Options */
			i=0;
			strcpy(opt[i++],"Run SCFG");
			strcpy(opt[i++],"View logs");
			strcpy(opt[i++],"Force QWK Net callout");
			strcpy(opt[i++],"Run event");
			strcpy(opt[i++],"Recycle servers");
			strcpy(opt[i++],"Edit CFG files");
			strcpy(opt[i++],"Edit trashcan files");
			opt[i][0]=0;
			uifc.helpbuf=	"`System Options:`\n"
							"\nToDo: Add help";

			done=0;
			i=0;
			while(!done) {
				switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"System Options",opt))  {
					case -1:
						done=1;
						break;
					case 0:
						sprintf(str,"%sscfg",cfg.exec_dir);
						do_cmd(str);
						break;
					case 1:
						view_logs(&cfg);
						break;
					case 2:
						qwk_callouts();
						break;
					case 3:
						run_events();
						break;
					case 4:
						recycle_servers();
						break;
					case 5:
						edit_cfg(&cfg);
						break;
					case 6:
						edit_can(&cfg);
						break;
				}
			}
		}

		if(j<cfg.sys_nodes && j>0) {
920
921
922
923
			i=0;
			strcpy(opt[i++],"Spy on node");
			strcpy(opt[i++],"Node toggles");
			strcpy(opt[i++],"Clear Errors");
924
925
926
			strcpy(opt[i++],"View node log");
			strcpy(opt[i++],"View crash log");
			if(!getnodedat(&cfg,j,&node,NULL)) {
927
928
929
930
				if((node.status==NODE_INUSE) && node.useron) {
					strcpy(opt[i++],"Send message to user");
					strcpy(opt[i++],"Chat with user");
				}
deuce's avatar
deuce committed
931
			}
932
933
934
935
			opt[i][0]=0;
			i=0;
			uifc.helpbuf=	"`Node Options:`\n"
							"\nToDo: Add help";
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
			done=0;
			while(!done) {
				switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"Node Options",opt))  {
					case 0:	/* Spy */
						dospy(j,&bbs_startup);
						break;

					case 1: /* Node Toggles */
						node_toggles(&cfg, j);
						break;
	
					case 2:
						clearerrors(&cfg, j,&node);
						break;
	
					case 3: /* Node log */
						sprintf(str,"%snode.log",cfg.node_path[j-1]);
						view_log(str,"Node Log");
						break;

					case 4: /* Crash log */
						sprintf(str,"%scrash.log",cfg.node_path[j-1]);
						view_log(str,"Crash Log");
						break;
960
					
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
					case 5:	/* Send message */
						sendmessage(&cfg, j,&node);
						break;
	
					case 6:
						chat(&cfg,main_dflt+1,&node,&boxch,uifc.timedisplay);
						break;
					
					case -1:
						done=1;
						break;
					
					default:
						uifc.msg("Option not implemented");
						break;
				}
977
			}
978
979
		}
	}
980
}