umonitor.c 36.4 KB
Newer Older
1
2
3
/* Synchronet for *nix node activity monitor */

/* $Id$ */
rswindell's avatar
rswindell committed
4
// vi: tabstop=4
5
6
7
8
9

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
rswindell's avatar
rswindell committed
10
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
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
 *																			*
 * 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.	*
 ****************************************************************************/

37
#include <signal.h>
deuce's avatar
deuce committed
38
#include <sys/types.h>
39
#include <time.h>
deuce's avatar
deuce committed
40
41
42
#ifdef __QNX__
#include <string.h>
#endif
43
#include <stdio.h>
44
45
46
47

#include "ciolib.h"
#define __COLORS		/* Disable the colour macros in sbbsdefs.h ToDo */
#include "sbbs.h"
48
#include "genwrap.h"
49
50
51
#include "uifc.h"
#include "sbbsdefs.h"
#include "genwrap.h"	/* stricmp */
52
#include "dirwrap.h"	/* lock/unlock/sopen */
53
#include "filewrap.h"	/* lock/unlock/sopen */
54
55
56
#include "sbbs_ini.h"	/* INI parsing */
#include "scfglib.h"	/* SCFG files */
#include "ars_defs.h"	/* needed for SCFG files */
57
#include "userdat.h"	/* getnodedat() */
58
#include "spyon.h"
59
#include "chat.h"
60

61
#define CTRL(x) (x&037)
62
63
64
65
66

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

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);
}
83

84
85
void bail(int code)
{
deuce's avatar
deuce committed
86
87
88
	if(code) {
		puts("\nHit a key...");
		getch();
89
	}
deuce's avatar
deuce committed
90
	uifc.bail();
91

deuce's avatar
deuce committed
92
	exit(code);
93
94
95
96
}

void allocfail(uint size)
{
deuce's avatar
deuce committed
97
98
	printf("\7Error allocating %u bytes of memory.\n",size);
	bail(1);
99
100
}

101
void node_toggles(scfg_t *cfg,int nodenum)  {
102
	int nodefile = -1;
103
104
105
106
107
	char**	opt;
	int		i,j;
	node_t	node;
	int		save=0;

deuce's avatar
deuce committed
108
109
110
	if((opt=(char **)alloca(sizeof(char *)*(4+1)))==NULL)
		allocfail(sizeof(char *)*(4+1));
	for(i=0;i<(4+1);i++)
111
		if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
112
113
114
			allocfail(MAX_OPLN);

	i=0;
115
116
	uifc.helpbuf=	"`Node Toggles\n"
	                "`------------`\n\n"
deuce's avatar
deuce committed
117
118
119
120
121
122
123
124
125
126
127
	                "`The following are `Yes/No `options.  Hitting Enter toggles between.\n\n"
	                "`Locked for SysOps only : `Locks the node so that only SysOps may \n"
	                "                         logon to them.\n"
	                "`Interrupt (Hangup)     : `The current user will be kicked as soon as it \n"
	                "                         is safe to do so.  A brief message is given\n"
	                "                         to user.\n"
	                "`Re-run on logoff       : `Toggles the system to reload the configuration\n"
	                "                         files when the current user logs off.\n"
	                "`Down node after logoff : `Takes the node offline after current user logs\n"
	                "                         off.\n\n"
	                "`[Note] `These toggles take effect immediately.";
128
	while(save==0) {
129
		if(getnodedat(cfg,nodenum,&node,FALSE,&nodefile)) {
130
131
132
			uifc.msg("Error reading node data!");
			break;
		}
133
134
135
		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
136
137
138
		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);
139
140
		opt[j][0]=0;

141
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"Node Toggles",opt)) {
142
143
144
145
146
147
148
149
			case 0:	/* Locked */
				node.misc ^= NODE_LOCK;
				break;

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

rswindell's avatar
rswindell committed
150
			case 2:	/* Re-run */
151
152
153
				node.misc ^= NODE_RRUN;
				break;

rswindell's avatar
rswindell committed
154
			case 3:	/* Down */
155
				if(node.status != NODE_WFC && node.status != NODE_OFFLINE)
156
					node.misc ^= NODE_DOWN;
157
				else {
158
159
160
161
162
					if(node.status!=NODE_OFFLINE)
						node.status=NODE_OFFLINE;
					else
						node.status=NODE_WFC;
				}
163
164
165
166
167
				break;

			case -1:
				save=1;
				break;
168

169
170
171
172
			default:
				uifc.msg("Option not implemented");
				continue;
		}
173
		putnodedat(cfg,nodenum,&node,FALSE,nodefile);
deuce's avatar
deuce committed
174
	}
deuce's avatar
deuce committed
175
176
177
178
179
180
181
}

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

	if(bbs_startup->temp_dir[0])
deuce's avatar
deuce committed
182
		snprintf(str,sizeof(str),"%slocalspy%d.sock", bbs_startup->temp_dir, nodenum);
deuce's avatar
deuce committed
183
184
185
186
187
188
189
	else
		snprintf(str,sizeof(str),"%slocalspy%d.sock", bbs_startup->ctrl_dir, nodenum);
	i=spyon(str);
	switch(i) {
		case SPY_NOSOCKET:
			uifc.msg("Could not create socket");
			return(-1);
190

deuce's avatar
deuce committed
191
192
193
194
195
196
197
198
199
200
201
202
		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);
203

deuce's avatar
deuce committed
204
205
206
		case SPY_STDINLOST:
			uifc.msg("STDIN has gone away... you probably can't close this window.  :-)");
			return(-1);
207

deuce's avatar
deuce committed
208
209
		case SPY_CLOSED:
			break;
210

deuce's avatar
deuce committed
211
212
213
214
215
216
217
218
		default:
			sprintf(str,"Unknown return code %d",i);
			uifc.msg(str);
			return(-1);
	}
	return(0);
}

219
int sendmessage(scfg_t *cfg, int nodenum,node_t *node)  {
220
221
	char str[80],str2[80];

222
	uifc.input(WIN_MID|WIN_SAV,0,0,"Telegram",str2,58,K_WRAP|K_MSG);
223
	sprintf(str,"\1n\1y\1hMessage From Sysop:\1w %s\r\n",str2);
224
	if(getnodedat(cfg,nodenum,node,FALSE,NULL))
225
226
227
		return(-1);
	if(node->useron==0)
		return(-1);
228
	putsmsg(cfg, node->useron, str);
229
230
231
	return(0);
}

232
int clearerrors(scfg_t *cfg, int nodenum, node_t *node) {
233
234
	int nodefile = -1;
	if(getnodedat(cfg,nodenum,node,TRUE,&nodefile)) {
235
236
237
238
		uifc.msg("getnodedat() failed! (Nothing done)");
		return(-1);
	}
	node->errors=0;
239
	putnodedat(cfg,nodenum,node,TRUE,nodefile);
240
	uifc.msg("Error count cleared for this node.");
241
242
243
	return(0);
}

244
245
246
247
248
/* 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);
249
			return(outstr);
250
251
252
		}
		if(size<10000) {	/* Bytes with comma */
			snprintf(outstr,12,"%ld,%03ld bytes",(size/1000),(size%1000));
253
			return(outstr);
254
255
256
257
258
		}
		size = size/1024;
	}
	if(size<1000) {	/* KB */
		snprintf(outstr,12,"%ld KB",size);
259
		return(outstr);
260
261
262
	}
	if(size<999999) { /* KB With comma */
		snprintf(outstr,12,"%ld,%03ld KB",(size/1000),(size%1000));
263
		return(outstr);
264
265
266
267
	}
	size = size/1024;
	if(size<1000) {	/* MB */
		snprintf(outstr,12,"%ld MB",size);
268
		return(outstr);
269
270
271
	}
	if(size<999999) { /* MB With comma */
		snprintf(outstr,12,"%ld,%03ld MB",(size/1000),(size%1000));
272
		return(outstr);
273
274
275
276
	}
	size = size/1024;
	if(size<1000) {	/* GB */
		snprintf(outstr,12,"%ld GB",size);
277
		return(outstr);
278
279
280
	}
	if(size<999999) { /* GB With comma */
		snprintf(outstr,12,"%ld,%03ld GB",(size/1000),(size%1000));
281
		return(outstr);
282
283
284
285
	}
	size = size/1024;
	if(size<1000) {	/* TB (Yeah, right) */
		snprintf(outstr,12,"%ld TB",size);
286
		return(outstr);
287
288
	}
	sprintf(outstr,"Plenty");
289
	return(outstr);
290
291
292
293
294
295
}

/* Assumes a 12 char outstr */
char *getnumstr(char *outstr, ulong size) {
	if(size < 1000) {
		snprintf(outstr,12,"%ld",size);
296
		return(outstr);
297
298
299
	}
	if(size<1000000) {
		snprintf(outstr,12,"%ld,%03ld",(size/1000),(size%1000));
300
		return(outstr);
301
302
303
	}
	if(size<1000000000) {
		snprintf(outstr,12,"%ld,%03ld,%03ld",(size/1000000),((size/1000)%1000),(size%1000));
304
		return(outstr);
305
306
307
308
	}
	size=size/1000000;
	if(size<1000000) {
		snprintf(outstr,12,"%ld,%03ld M",(size/1000),(size%1000));
309
		return(outstr);
310
311
312
	}
	if(size<10000000) {
		snprintf(outstr,12,"%ld,%03ld,%03ld M",(size/1000000),((size/1000)%1000),(size%1000));
313
		return(outstr);
314
315
	}
	sprintf(outstr,"Plenty");
316
	return(outstr);
317
318
}

319
int drawstats(scfg_t *cfg, int nodenum, node_t *node, int *curp, int *barp) {
320
321
322
323
324
325
326
327
	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;
328
	int		shownode=1;
329

330
	if(getnodedat(cfg,nodenum,node,FALSE,NULL)) {
331
332
333
334
		shownode=0;
	}
	else {
		getstats(cfg, nodenum, &nstats);
335
336
337
338
339
340
341
342
343
344
	}
	username(cfg,node->useron,usrname);

	getstats(cfg, 0, &sstats);
	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);
345
	}
346
347
	else
		getsizestr(str[0][1],free,FALSE);
348
349
350
351
352
	if(shownode) {
		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);
rswindell's avatar
rswindell committed
353
		snprintf(str[2][0],12,"%s/%s",getnumstr(str[3][2],sstats.etoday),getnumstr(str[3][3],getmail(cfg,0,0,0)));
354
355
356
357
358
359
		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));
rswindell's avatar
rswindell committed
360
		snprintf(str[2][2],12,"%s/%s",getnumstr(str[3][2],sstats.ftoday),getnumstr(str[3][3],getmail(cfg,1,0,0)));
361
362
363
364
365
366
367
368
369
370
371
		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);
	}
	else {
		snprintf(str[1][0],12,"%s",getnumstr(str[3][3],sstats.ltoday));
		getnumstr(str[1][1],sstats.logons);
		snprintf(str[1][2],12,"%s",getnumstr(str[3][3],sstats.ttoday));
		getnumstr(str[1][3],sstats.timeon);
rswindell's avatar
rswindell committed
372
		snprintf(str[2][0],12,"%s/%s",getnumstr(str[3][2],sstats.etoday),getnumstr(str[3][3],getmail(cfg,0,0,0)));
373
374
375
376
377
378
		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));
rswindell's avatar
rswindell committed
379
		snprintf(str[2][2],12,"%s/%s",getnumstr(str[3][2],sstats.ftoday),getnumstr(str[3][3],getmail(cfg,1,0,0)));
380
381
382
383
384
385
		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
386
387
388
389
	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",
390
391
392
393
394
			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
395
	uifc.showbuf(WIN_HLP|WIN_L2R|WIN_DYN|WIN_PACK,1,1,80,6,"Statistics",statbuf,curp,barp);
396
/* Node 5 :	Mar 11  Space: 162,024k
397
   Logons: 23/103      Total: 62,610      Timeon: 322/2430    Total: 5,321,900
398
399
400
401
402
   Emails: 4/265       Posts: 4/12811     Fbacks: 2/17	       Users: 1/592
   Uloads: 324k        Files: 1/2195      Dloads: 9,308k      Files: 52 */
	return(0);
}

403
404
405
406
407
408
409
410
411
412
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);
413
			if(j >= 0 && (buf=(char *)malloc(j+1))!=NULL) {
414
415
416
417
				read(buffile,buf,j);
				close(buffile);
				*(buf+j)=0;
				uifc.showbuf(WIN_MID,0,0,76,uifc.scrn_len-2,title,buf,NULL,NULL);
418
				free(buf);
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
				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);
deuce's avatar
deuce committed
447
448
449
	if((opt=(char **)alloca(sizeof(char *)*(9+1)))==NULL)
		allocfail(sizeof(char *)*(9+1));
	for(i=0;i<(9+1);i++)
450
		if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
451
452
453
			allocfail(MAX_OPLN);

	i=0;
454
455
	strcpy(opt[i++],"Today's callers");
	strcpy(opt[i++],"Yesterday's callers");
456
	strcpy(opt[i++],"Error log");
457
458
	strcpy(opt[i++],"Today's log");
	strcpy(opt[i++],"Yesterday's log");
459
	strcpy(opt[i++],"Spam log");
460
	strcpy(opt[i++],"SBBSecho log");
461
462
463
464
	strcpy(opt[i++],"Guru log");
	strcpy(opt[i++],"Hack log");
	opt[i][0]=0;
	i=0;
465
	uifc.helpbuf=	"`View Logs\n"
deuce's avatar
deuce committed
466
467
468
469
470
471
472
	                "`---------\n\n"
	                "`Today's callers     : `View a list of Today's callers.\n"
	                "`Yesterday's callers : `View a list of Yesterday's callers.\n"
	                "`Error log           : `View the Error log.\n"
	                "`Today's log         : `View Today's system activity.\n"
	                "`Yesterday's log     : `View Yesterday's system activity.\n"
	                "`Spam log            : `View the log of Spam E-Mail sent to the system.\n"
473
	                "`SBBSecho log        : `View the FidoNet EchoMail program log.\n"
deuce's avatar
deuce committed
474
475
	                "`Guru log            : `View the transcriptions of chats with the Guru.\n"
	                "`Hack log            : `View the Hack attempt log.";
476

477
	while(1) {
478
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"View Logs",opt))  {
479
480
481
			case -1:
				return(0);
			case 0:
deuce's avatar
deuce committed
482
				sprintf(str,"%slogs/%2.2d%2.2d%2.2d.lol",cfg->logs_dir,tm.tm_mon+1,tm.tm_mday
483
484
485
486
					,TM_YEAR(tm.tm_year));
				view_log(str,"Todays Callers");
				break;
			case 1:
deuce's avatar
deuce committed
487
				sprintf(str,"%slogs/%2.2d%2.2d%2.2d.lol",cfg->logs_dir,tm_yest.tm_mon+1
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
					,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);
511
				view_log(str,"SBBSecho Log");
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
				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;
528
529
530
531
	struct text_info ti;
	char *p;

	gettextinfo(&ti);
532
	p=alloca(ti.screenheight*ti.screenwidth*2);
533
	gettext(1,1,ti.screenwidth,ti.screenheight,p);
534
	i=system(cmd);
535
	puttext(1,1,ti.screenwidth,ti.screenheight,p);
536
537
538
	return(i);
}

539
int qwk_callouts(scfg_t *cfg)
540
{
541
542
543
544
545
546
547
548
549
	char**	opt;
	int		i,j;
	char	str[1024];

	if(cfg->total_qhubs<1) {
		uifc.msg("No QWK hubs configured!");
		return(1);
	}

deuce's avatar
deuce committed
550
551
552
	if((opt=(char **)alloca(sizeof(char *)*(cfg->total_qhubs+1)))==NULL)
		allocfail(sizeof(char *)*(cfg->total_qhubs+1));
	for(i=0;i<(cfg->total_qhubs+1);i++)
553
		if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
554
555
556
			allocfail(MAX_OPLN);


557
558
559
560
	uifc.helpbuf=	"`QWK Callouts\n"
					"`------------\n\n"
					"Select the Hub and press enter to initiate a call out to\n"
					"the highlighted system.";
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

	j=0;
	while(1) {
		for(i=0;i<cfg->total_qhubs;i++) {
			strcpy(opt[i],cfg->qhub[i]->id);
			sprintf(str,"%sqnet/%s.now",cfg->data_dir,cfg->qhub[i]->id);
			if(fexist(str))
				strcat(opt[i]," (pending)");
		}
		opt[i][0]=0;
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&j,0,"QWK Callouts",opt))  {
			case -1:
				return(0);
				break;
			default:
				sprintf(str,"%sqnet/%s.now",cfg->data_dir,cfg->qhub[j]->id);
				ftouch(str);
		}
	}
580
581
582
	return(0);
}

583
int run_events(scfg_t *cfg)
584
{
585
586
587
588
	char**	opt;
	int		i,j;
	char	str[1024];

deuce's avatar
deuce committed
589
590
591
	if((opt=(char **)alloca(sizeof(char *)*(cfg->total_events+1)))==NULL)
		allocfail(sizeof(char *)*(cfg->total_events+1));
	for(i=0;i<(cfg->total_events+1);i++)
592
		if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
593
594
595
596
597
598
599
600
			allocfail(MAX_OPLN);

	if(cfg->total_events<1) {
		uifc.msg("No events configured!");
		return(1);
	}
	j=0;

601
602
603
	uifc.helpbuf=	"`Run Events\n"
					"`----------\n\n"
					"To run and event, highlight it and press Enter";
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621

	while(1) {
		for(i=0;i<cfg->total_events;i++) {
			strcpy(opt[i],cfg->event[i]->code);
			sprintf(str,"%s%s.now",cfg->data_dir,cfg->event[i]->code);
			if(fexist(str))
				strcat(opt[i]," (pending)");
		}
		opt[i][0]=0;
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&j,0,"Run Events",opt))  {
			case -1:
				return(0);
				break;
			default:
				sprintf(str,"%s%s.now",cfg->data_dir,cfg->event[j]->code);
				ftouch(str);
		}
	}
622
623
624
	return(0);
}

625
int recycle_servers(scfg_t *cfg)
626
{
627
628
629
630
	char str[1024];
	char **opt;
	int i=0;

deuce's avatar
deuce committed
631
632
633
	if((opt=(char **)alloca(sizeof(char *)*(5+1)))==NULL)
		allocfail(sizeof(char *)*(5+1));
	for(i=0;i<(5+1);i++)
634
		if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
635
636
637
638
639
640
641
642
643
644
			allocfail(MAX_OPLN);

	i=0;
	strcpy(opt[i++],"FTP server");
	strcpy(opt[i++],"Mail server");
	strcpy(opt[i++],"Services");
	strcpy(opt[i++],"Telnet server");
	strcpy(opt[i++],"Web server");
	opt[i][0]=0;

645
646
647
648
649
	uifc.helpbuf=	"`Recycle Servers\n"
					"`---------------\n\n"
					"To rerun a server, highlight it and press Enter.\n"
					"This will reload the configuration files for selected\n"
					"server.";
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

	i=0;
	while(1) {
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"Recycle Servers",opt))  {
			case -1:
				return(0);
				break;
			case 0:
				sprintf(str,"%sftpsrvr.rec",cfg->ctrl_dir);
				ftouch(str);
				break;
			case 1:
				sprintf(str,"%smailsrvr.rec",cfg->ctrl_dir);
				ftouch(str);
				break;
			case 2:
				sprintf(str,"%sservices.rec",cfg->ctrl_dir);
				ftouch(str);
				break;
			case 3:
				sprintf(str,"%stelnet.rec",cfg->ctrl_dir);
				ftouch(str);
				break;
			case 4:
				sprintf(str,"%swebsrvr.rec",cfg->ctrl_dir);
				ftouch(str);
				break;
		}
	}
679
680
681
	return(0);
}

682
683
684
685
686
687
688
689
690
691
692
693
694
695
char *geteditor(char *edit)
{
	if(getenv("EDITOR")==NULL && (getenv("VISUAL")==NULL || getenv("DISPLAY")==NULL))
		strcpy(edit,"vi");
	else {
		if(getenv("DISPLAY")!=NULL && getenv("VISUAL")!=NULL)
			strcpy(edit,getenv("VISUAL"));
		else
			strcpy(edit,getenv("EDITOR"));
	}
	return(edit);
}


696
697
698
699
700
int edit_cfg(scfg_t *cfg)
{
	char**	opt;
	int		i;
	char	cmd[1024];
701
	char	editcmd[1024];
702

deuce's avatar
deuce committed
703
704
705
	if((opt=(char **)alloca(sizeof(char *)*(17+1)))==NULL)
		allocfail(sizeof(char *)*(17+1));
	for(i=0;i<(17+1);i++)
706
		if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
707
708
709
			allocfail(MAX_OPLN);

	i=0;
710
	strcpy(opt[i++],"sbbs.ini");
711
712
713
714
715
	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");
716
717
	strcpy(opt[i++],"mailproc.ini");
	strcpy(opt[i++],"mime_types.ini");
718
	strcpy(opt[i++],"relay.cfg");
719
	strcpy(opt[i++],"sbbsecho.ini");
720
	strcpy(opt[i++],"services.ini");
721
	strcpy(opt[i++],"ftpalias.cfg");
722
	strcpy(opt[i++],"sockopts.ini");
723
724
725
726
	strcpy(opt[i++],"spambait.cfg");
	strcpy(opt[i++],"spamblock.cfg");
	strcpy(opt[i++],"twitlist.cfg");
	opt[i][0]=0;
727
	uifc.helpbuf= "Highlight desired file and hit Enter to edit it.";
728
729
730
731
732
	i=0;
	while(1) {
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"System Options",opt))  {
			case -1:
				return(0);
733
				break;
734
			default:
735
				sprintf(cmd,"%s %s%s",geteditor(editcmd),cfg->ctrl_dir,opt[i]);
736
737
738
739
740
741
				do_cmd(cmd);
				break;
		}
	}
	return(0);
}
742

743
744
745
746
747
int edit_can(scfg_t *cfg)
{
	char**	opt;
	int		i;
	char	cmd[1024];
748
	char	editcmd[1024];
749

deuce's avatar
deuce committed
750
751
752
	if((opt=(char **)alloca(sizeof(char *)*(9+1)))==NULL)
		allocfail(sizeof(char *)*(9+1));
	for(i=0;i<(9+1);i++)
753
		if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
754
755
756
757
758
759
760
761
762
763
764
765
766
			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;
767
	uifc.helpbuf="Highlight desired file and hit Enter to edit it.";
768
769
770
771
772
	i=0;
	while(1) {
		switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"System Options",opt))  {
			case -1:
				return(0);
773
				break;
774
			default:
775
				sprintf(cmd,"%s %s%s",geteditor(editcmd),cfg->text_dir,opt[i]);
776
777
778
779
780
781
				do_cmd(cmd);
				break;
		}
	}
	return(0);
}
782

783
int main(int argc, char** argv)  {
784

785
786
787
788
	char**	opt;
	char**	mopt;
	int		main_dflt=0;
	int		main_bar=0;
deuce's avatar
deuce committed
789
	char	revision[16];
790
	char	str[256],ctrl_dir[MAX_PATH + 1];
deuce's avatar
deuce committed
791
792
793
	char	title[256];
	int		i,j;
	node_t	node;
794
	int		nodefile = -1;
795
796
	box_t	boxch;
	scfg_t	cfg;
797
	int		done;
deuce's avatar
deuce committed
798
	int		ciolib_mode=CIOLIB_MODE_AUTO;
799
	time_t	last_semfile_check = time(NULL);
800
	int		idle_sleep=100;
801

deuce's avatar
deuce committed
802
803
804
	/******************/
	/* Ini file stuff */
	/******************/
805
	char	ini_file[MAX_PATH+1];
806
	FILE*				fp=NULL;
807
	bbs_startup_t		bbs_startup;
808
809

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

deuce's avatar
deuce committed
811
812
	printf("\nSynchronet UNIX Monitor %s-%s  Copyright %s "
		"Rob Swindell\n",revision,PLATFORM_DESC,__DATE__+7);
813

814
	SAFECOPY(ctrl_dir, get_ctrl_dir());
815
	backslash(ctrl_dir);
816

deuce's avatar
deuce committed
817
	gethostname(str,sizeof(str)-1);
818
819

	sbbs_get_ini_fname(ini_file, ctrl_dir, str);
820
821

	/* Initialize BBS startup structure */
deuce's avatar
deuce committed
822
823
	memset(&bbs_startup,0,sizeof(bbs_startup));
	bbs_startup.size=sizeof(bbs_startup);
824
	SAFECOPY(bbs_startup.ctrl_dir,ctrl_dir);
825
826
827
828
829
830

	/* 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 */
rswindell's avatar
rswindell committed
831
	sbbs_read_ini(fp, ini_file,
832
		NULL,		/* global_startup */
833
		NULL, &bbs_startup,
834
		NULL, NULL, /* ftp_startup */
835
		NULL, NULL, /* web_startup */
836
837
838
		NULL, NULL, /* mail_startup */
		NULL, NULL  /* services_startup */
		);
839
840
841
842
843
844

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

	chdir(bbs_startup.ctrl_dir);
845

846
	/* Read .cfg files here */
deuce's avatar
deuce committed
847
	memset(&cfg,0,sizeof(cfg));
848
	cfg.size=sizeof(cfg);
849
850
	SAFECOPY(cfg.ctrl_dir,bbs_startup.ctrl_dir);
	if(!load_cfg(&cfg, NULL, TRUE, str)) {
851
852
853
		printf("ERROR! %s\n",str);
		exit(1);
	}
854
	prep_dir(cfg.data_dir, cfg.temp_dir, sizeof(cfg.temp_dir));
855

deuce's avatar
deuce committed
856
	memset(&uifc,0,sizeof(uifc));
857
	uifc.mode|=UIFC_NOCTRL;
858
859
860

	uifc.esc_delay=500;

deuce's avatar
deuce committed
861
862
863
864
865
866
867
868
	boxch.ls=(char)186;
	boxch.rs=(char)186;
	boxch.ts=(char)205;
	boxch.bs=(char)205;
	boxch.tl=(char)201;
	boxch.tr=(char)187;
	boxch.bl=(char)200;
	boxch.br=(char)188;
869
	for(i=1;i<argc;i++) {
deuce's avatar
deuce committed
870
871
872
873
874
875
876
877
878
879
880
		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;
881
882
883
				case 'S':
					idle_sleep=atoi(argv[i]+2);
					break;
884
				case 'I':
deuce's avatar
deuce committed
885
886
887
888
889
890
891
892
					switch(toupper(argv[i][2])) {
						case 'A':
							ciolib_mode=CIOLIB_MODE_ANSI;
							break;
						case 'C':
							ciolib_mode=CIOLIB_MODE_CURSES;
							break;
						case 0:
893
							printf("NOTICE: The -i option is deprecated, use -if instead\r\n");
deuce's avatar
deuce committed
894
895
896
897
898
899
900
901
902
903
904
905
906
							SLEEP(2000);
						case 'F':
							ciolib_mode=CIOLIB_MODE_CURSES_IBM;
							break;
						case 'X':
							ciolib_mode=CIOLIB_MODE_X;
							break;
						case 'W':
							ciolib_mode=CIOLIB_MODE_CONIO;
							break;
						default:
							goto USAGE;
					}
907
					break;
deuce's avatar
deuce committed
908
909
910
911
912
913
914
				default:
USAGE:
					printf("\nusage: %s [ctrl_dir] [options]\r\n"
					         "options:\n\n"
					         "-c  =  force color mode\n"
					         "-e# =  set escape delay to #msec\n"
					         "-iX =  set interface mode to X (default=auto) where X is one of:\r\n"
deuce's avatar
deuce committed
915
#ifdef __unix__
deuce's avatar
deuce committed
916
917
918
					         "       X = X11 mode\r\n"
					         "       C = Curses mode\r\n"
					         "       F = Curses mode with forced IBM charset\r\n"
deuce's avatar
deuce committed
919
#else
deuce's avatar
deuce committed
920
					         "       W = Win32 native mode\r\n"
deuce's avatar
deuce committed
921
#endif
deuce's avatar
deuce committed
922
923
924
925
926
927
928
929
930
					         "       A = ANSI mode\r\n"
					         "-l# =  set screen lines to #\n"
					         "-s# =  set idle slsep to # milliseconds (defualt: %d)\n"
					    ,argv[0]
					    ,idle_sleep
					);
					exit(0);
			}
	}
931

deuce's avatar
deuce committed
932
#ifdef SIGPIPE
933
	signal(SIGPIPE, SIG_IGN);
deuce's avatar
deuce committed
934
#endif
935

936
	uifc.size=sizeof(uifc);
deuce's avatar
deuce committed
937
938
	i=initciolib(ciolib_mode);
	if(i!=0) {
deuce's avatar
deuce committed
939
940
		printf("ciolib library init returned error %d\n",i);
		exit(1);
deuce's avatar
deuce committed
941
	}
deuce's avatar
deuce committed
942
	i=uifcini32(&uifc);  /* curses */
943
944
945
946
947
	if(i!=0) {
		printf("uifc library init returned error %d\n",i);
		exit(1);
	}

deuce's avatar
deuce committed
948
949
950
	if((opt=(char **)alloca(sizeof(char *)*(10+1)))==NULL)
		allocfail(sizeof(char *)*(10+1));
	for(i=0;i<(10+1);i++)
951
		if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
952
953
			allocfail(MAX_OPLN);

deuce's avatar
deuce committed
954
955
956
	if((mopt=(char **)alloca(sizeof(char *)*cfg.sys_nodes+2))==NULL)
		allocfail(sizeof(char *)*cfg.sys_nodes+2);
	for(i=0;i<cfg.sys_nodes+2;i++)
957
		if((mopt[i]=(char *)alloca(MAX_OPLN))==NULL)
958
959
			allocfail(MAX_OPLN);

960
961
	sprintf(title,"Synchronet UNIX Monitor %s-%s",revision,PLATFORM_DESC);
	if(uifc.scrn(title)) {
962
963
964
965
		printf(" USCRN (len=%d) failed!\n",uifc.scrn_len+1);
		bail(1);
	}

966
	int paging_node = 0;
967
	while(1) {
968
		strcpy(mopt[0],"System Options");
969
		for(i=1;i<=cfg.sys_nodes;i++) {
970
			if((j=getnodedat(&cfg,i,&node,FALSE,NULL)))
971
				sprintf(mopt[i],"Error reading node data (%d)!",j);
972
			else {
973
				nodestatus(&cfg, &node, str, 71, i);
974
975
976
977
978
979
				if(i == paging_node) {
					strupr(str);
					strcat(str,  " <PAGING>");
				}
				sprintf(mopt[i],"%3d: %s", i, str);
			}
980
		}
981
		mopt[i][0]=0;
982

deuce's avatar
deuce committed
983
984
		uifc.helpbuf=  "`Synchronet UNIX Monitor\n"
		               "`------------------\n"
985
		                "Welcome to the Synchronet UNIX Monitor.\n"
986
987
988
989
		                "Displayed on this screen are the statitics for the BBS\n"
		                "You can scroll through the list starting at \"System Options\" \n"
		                "Pressing Enter on each will give a menu of option to perform.\n"
		                "Additionally these keys are available from this screen:\n\n"
deuce's avatar
deuce committed
990
991
992
993
994
995
996
997
998
		               "`CTRL-E : `Displays the error log\n"
		               "`F12    : `Spys on the currently selected node\n"
		               "`F11    : `Send message to the currently selected node\n"
		               "`F10    : `Chats with the user on the currently selected node\n"
		               "`DEL    : `Clear errors on currently selected node\n"
		               "`CTRL-L : `Lock node toggle\n"
		               "`CTRL-R : `Rerun node\n"
		               "`CTRL-D : `Down node toggle\n"
		               "`CTRL-I : `Interrupt node\n";
deuce's avatar
deuce committed
999

1000
		drawstats(&cfg, main_dflt, &node, &main_dflt, &main_bar);
deuce's avatar
deuce committed
1001

1002
		if(time(NULL) - last_semfile_check > bbs_startup.sem_chk_freq * 2) {
1003
			paging_node = 0;
1004
			SAFEPRINTF(str, "%ssyspage.*", cfg.ctrl_dir);
1005
			if(fexistcase(str) && (paging_node = atoi(getfext(str) + 1)) > 0) {
1006
1007
				putch(BEL);
				char msg[128];
1008
				SAFEPRINTF(msg, "Node %u is paging you to chat", paging_node);
1009
1010
1011
1012
1013
1014
1015
				uifc.pop(msg);
				SLEEP(1500);
				uifc.pop(NULL);
			}
			last_semfile_check = time(NULL);
		}

1016
		j=uifc.list(WIN_L2R|WIN_ESC|WIN_ACT|WIN_DYN,0,5,70,&main_dflt,&main_bar
1017
1018
			,title,mopt);

1019
		if(j == -2) {
1020
			SLEEP(idle_sleep);
deuce's avatar
deuce committed
1021
			continue;
1022
		}
deuce's avatar
deuce committed
1023

1024
1025
		last_semfile_check = time(NULL);

1026
		if(j==-7) {	/* CTRL-E */
1027
			sprintf(str,"%s/error.log",cfg.data_dir);
1028
			view_log(str,"Error Log");
1029
1030
			continue;
		}
1031

1032
		if(j==0) {
1033
1034
1035
			BOOL sysop_avail = sysop_available(&cfg);
			int sysop_chat_opt;

1036
1037
1038
			/* System Options */
			i=0;
			strcpy(opt[i++],"Run SCFG");
1039
			strcpy(opt[i++],"Run User Editor");
1040
			strcpy(opt[i++],"Run SyncTERM");
1041
1042
1043
1044
			strcpy(opt[i++],"View logs");
			strcpy(opt[i++],"Force QWK Net callout");
			strcpy(opt[i++],"Run event");
			strcpy(opt[i++],"Recycle servers");
1045
			strcpy(opt[i++],"Edit CFG/INI files");
1046
			strcpy(opt[i++],"Edit trashcan files");
1047
			sysop_chat_opt = i++;
1048
			opt[i][0]=0;
1049
1050
			uifc.helpbuf=	"`System Options`\n"
			                "`------------`\n\n"
deuce's avatar
deuce committed
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
			                "`Run SCFG              : `Run the Synchronet Configuration Utility.\n"
			                "`Run User Editor       : `Call up the User Editor.\n"
			                "`Run SyncTERM          : `Run SyncTERM for RLogin.  SyncTERM must be\n"
			                 "                        in the exec directory.\n"
			                "`View logs             : `View the various system logs.\n"
			                "`Force QWK Net callout : `Force a callout to QWK Net Hub.  Select which\n"
			                 "                        Hub from a popup list of configured Hubs.\n"
			                "`Run Event             : `Call up a menu of system events that can be\n"
			                "                        manually.\n"
			                "`Recycle Servers       : `Have the Servers reload their configuration \n"
			                "                        files.\n"
			                "`Edit trashcan files   : `Edit the various .can files.  i.e.; ip.can";
1063
1064
1065
1066

			done=0;
			i=0;
			while(!done) {
1067
				sprintf(opt[sysop_chat_opt], "Turn Sysop Chat availability %s", sysop_avail ? "Off" : "On");
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
				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);
						for(j=1; j<argc; j++) {
							strcat(str,"'");
							strcat(str,argv[j]);
							strcat(str,"' ");
						}
						do_cmd(str);
						break;
					case 1:
1082
						sprintf(str,"%suedit ",cfg.exec_dir);
1083
1084
1085
1086
1087
1088
						for(j=1; j<argc; j++) {
							strcat(str,"'");
							strcat(str,argv[j]);
							strcat(str,"' ");
						}
						do_cmd(str);
1089
1090
						break;
					case 2:
1091
1092
1093
1094
1095
1096
1097
						sprintf(str,"%ssyncterm",cfg.exec_dir);
						for(j=1; j<argc; j++) {
							strcat(str,"'");
							strcat(str,argv[j]);
							strcat(str,"' ");
						}
						do_cmd(str);
1098
1099
						break;
					case 3:
1100
						view_logs(&cfg);
1101
1102
						break;
					case 4:
1103
						qwk_callouts(&cfg);
1104
1105
						break;
					case 5:
1106
						run_events(&cfg);
deuce's avatar