umonitor.c 19.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>
deuce's avatar
deuce committed
41
42
43
44
45
#include <sys/types.h>
#include <sys/time.h>
#ifdef __QNX__
#include <string.h>
#endif
46
#include <stdio.h>
47
#include <unistd.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

60
#define CTRL(x) (x&037)
deuce's avatar
deuce committed
61
62
63
64
65
66
#define PCHAT_LEN 1000		/* Size of Private chat file */

typedef struct {
	chtype ls, rs, ts, bs, tl, tr, bl, br;
} box_t;

67
68
69
70
71
72
73

/********************/
/* Global Variables */
/********************/
uifcapi_t uifc; /* User Interface (UIFC) Library API */
char tmp[256];
int nodefile;
74
75
const char *YesStr="Yes";
const char *NoStr="No";
76
scfg_t	cfg;
deuce's avatar
deuce committed
77
box_t boxch;
78
79
80
81
82
83
84
85
86
87
88
89
90
91

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

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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);
108
109
}

110
111
112
113
114
115
116
117
118
119
120
121
122
123
void node_toggles(int nodenum)  {
	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"
124
					"\nToDo: Add help (Mention that changes take effect immediately)";
125
	while(save==0) {
126
127
128
129
		if(getnodedat(&cfg,nodenum,&node,&nodefile)) {
			uifc.msg("Error reading node data!");
			break;
		}
130
131
132
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);
		sprintf(opt[j++],"%-30s%3s","Page disabled",node.misc&NODE_POFF ? YesStr : NoStr);
		sprintf(opt[j++],"%-30s%3s","Activity alert disabled",node.misc&NODE_AOFF ? YesStr : NoStr);
		sprintf(opt[j++],"%-30s%3s","Re-run on logoff",node.misc&NODE_RRUN ? YesStr : NoStr);
136
		sprintf(opt[j++],"%-30s%3s","Down node after logoff",(node.misc&NODE_DOWN || (node.status==NODE_OFFLINE)) ? YesStr : NoStr);
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
		sprintf(opt[j++],"%-30s%3s","Reset private chat",node.misc&NODE_RPCHT ? YesStr : NoStr);
		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;

			case 2:	/* Page disabled */
				node.misc ^= NODE_POFF;
				break;

			case 3:	/* Activity alert */
				node.misc ^= NODE_AOFF;
				break;

			case 4:	/* Re-run */
				node.misc ^= NODE_RRUN;
				break;

			case 5:	/* Down */
162
				if(node.status != NODE_WFC && node.status != NODE_OFFLINE)
163
					node.misc ^= NODE_DOWN;
164
				else {
165
166
167
168
169
					if(node.status!=NODE_OFFLINE)
						node.status=NODE_OFFLINE;
					else
						node.status=NODE_WFC;
				}
170
171
172
173
174
175
176
177
178
179
180
181
182
183
				break;

			case 6:	/* Reset chat */
				node.misc ^= NODE_RPCHT;
				break;
				
			case -1:
				save=1;
				break;
				
			default:
				uifc.msg("Option not implemented");
				continue;
		}
184
		putnodedat(&cfg,nodenum,&node,nodefile);
185
186
187
	}
}

deuce's avatar
deuce committed
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
bool togglechat(int node_num, node_t *node, bool on)
{
    static int  org_act;

	if(getnodedat(&cfg,node_num,node,&nodefile)) {
		uifc.msg("Error reading node data!");
		return(FALSE);
	}
    if(on) {
        org_act=node->action;
        if(org_act==NODE_PCHT)
            org_act=NODE_MAIN;
        node->misc|=NODE_LCHAT;
    } else {
        node->action=org_act;
        node->misc&=~NODE_LCHAT;
    }
	putnodedat(&cfg,node_num,node,nodefile);
    return(TRUE);
}

void wsetcolor(WINDOW *win, int fg, int bg)  {
	int   attrs=A_NORMAL;
	short	colour;
	int		lastfg=0;
	int		lastbg=0;

	if (lastfg==fg && lastbg==bg)
		return;

	lastfg=fg;
	lastbg=bg;

	if (fg & 8)  {
		attrs |= A_BOLD;
	}
	wattrset(win, attrs);
	colour = COLOR_PAIR( ((fg&7)|(bg<<3))+1 );
	#ifdef NCURSES_VERSION_MAJOR
	wcolor_set(win, colour,NULL);
	#endif
	wbkgdset(win, colour);
}

void drawchatwin(WINDOW **uwin, WINDOW **swin) {
	int maxy,maxx;

	endwin();
	getmaxyx(stdscr,maxy,maxx);
	*uwin=newwin(maxy/2,maxx,0,0);
	*swin=newwin(0,maxx,maxy/2,0);
	wsetcolor(*uwin,YELLOW,BLUE);
	wclear(*uwin);
	wborder(*uwin,  boxch.ls, boxch.rs, boxch.ts, boxch.bs, boxch.tl, boxch.tr, boxch.bl, boxch.br);
	wmove(*uwin,0,5);
	waddstr(*uwin,"Remote User");
	wmove(*uwin,1,2);
	scrollok(*uwin,TRUE);
	wrefresh(*uwin);
	wsetcolor(*swin,YELLOW,BLUE);
	wclear(*swin);
	wborder(*swin,  boxch.ls, boxch.rs, boxch.ts, boxch.bs, boxch.tl, boxch.tr, boxch.bl, boxch.br);
	wmove(*swin,0,5);
	waddstr(*swin,"Sysop");
	wmove(*swin,1,2);
	scrollok(*swin,TRUE);
	wrefresh(*swin);
}

int chatchar(WINDOW *win, int ch) {
	int	maxy,maxx;
	int	cury,curx;
	getmaxyx(win,maxy,maxx);
	getyx(win,cury,curx);
	switch(ch) {
		case 8:
			curx-=1;
			if(curx<2)
				curx+=1;
			mvwaddch(win,cury,curx,' ');
			wmove(win,cury,curx);
			wrefresh(win);
			break;
		
		case '\r':
		case '\n':
			curx=2;
			cury+=1;
			wmove(win,cury,curx);
			if(cury>=maxy-1) {
				wscrl(win,1);
				cury-=1;
				wmove(win,cury,curx-1);
				whline(win,' ',maxx-2);
				wborder(win, boxch.ls, boxch.rs, boxch.ts, boxch.bs, boxch.tl, boxch.tr, boxch.bl, boxch.br);
				wmove(win,0,5);
				waddstr(win,"Sysop");
				wmove(win,cury,curx);
			}
			wrefresh(win);
			break;
			
		default:
			waddch(win,ch);
			getyx(win,cury,curx);
			if(curx>=maxx-2) {
				curx=2;
				cury+=1;
				if(cury>=maxy-1) {
					wscrl(win,1);
					cury-=1;
					wmove(win,cury,curx-1);
					whline(win,' ',maxx-2);
					wborder(win, boxch.ls, boxch.rs, boxch.ts, boxch.bs, boxch.tl, boxch.tr, boxch.bl, boxch.br);
					wmove(win,0,5);
					waddstr(win,"Sysop");
				}
				wmove(win,cury,curx);
			}
			wrefresh(win);
	}
	return(0);
}

int chat(int nodenum, node_t *node, bbs_startup_t *bbs_startup) {
	WINDOW	*uwin;
	WINDOW	*swin;
	int		in,out;
	char	inpath[MAX_PATH];
	char	outpath[MAX_PATH];
	char	*p;
	char	ch;
	int		inpos=0;
	int		outpos=0;

	drawchatwin(&uwin,&swin);

	if(getnodedat(&cfg,nodenum,node,NULL)) {
		uifc.msg("Error reading node data!");
		return(-1);
	}

	sprintf(outpath,"%slchat.dab",cfg.node_path[nodenum-1]);
	if((out=sopen(outpath,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
		,S_IREAD|S_IWRITE))==-1) {
		uifc.msg("!Error opening lchat.dab");
		return(-1);
	}

	sprintf(inpath,"%schat.dab",cfg.node_path[nodenum-1]);
	if((in=sopen(inpath,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
		,S_IREAD|S_IWRITE))==-1) {
		close(out);
		uifc.msg("!Error opening chat.dab");
		return(-1);
    }

	if((p=(char *)MALLOC(PCHAT_LEN))==NULL) {
		close(in);
		close(out);
		uifc.msg("!Error allocating memory");
		return(-1);
    }
	memset(p,0,PCHAT_LEN);
	write(in,p,PCHAT_LEN);
	write(out,p,PCHAT_LEN);
	FREE(p);
	lseek(in,0,SEEK_SET);
	lseek(out,0,SEEK_SET);
	halfdelay(1);

	togglechat(nodenum,node,TRUE);

	while(in != -1) {
deuce's avatar
deuce committed
362
363
364
		utime(outpath,NULL);
		utime(inpath,NULL);
		
deuce's avatar
deuce committed
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
		if(getnodedat(&cfg,nodenum,node,NULL)) {
			uifc.msg("Loop error reading node data!");
			break;
		}
	    if(node->misc&NODE_LCHAT) {
			YIELD();
			continue;
		}
		
		if(!node->status || node->status>NODE_QUIET || node->action!=NODE_PCHT) {
	        uifc.msg("CHAT: User Disconnected\r\n");
			close(in);
			in=-1;
		}
		switch (read(in,&ch,1)) {
			case -1:
				close(in);
				in=-1;
				break;
				
			case 0:
				lseek(in,0,SEEK_SET);	/* Wrapped */
				continue;
				
			case 1:
				lseek(in,-1L,SEEK_CUR);
				if(ch) {
					chatchar(uwin,ch);
					ch=0;
					write(in,&ch,1);
				}
		}
deuce's avatar
deuce committed
397
		if((ch=wgetch(swin))) {
deuce's avatar
deuce committed
398
399
			switch(ch)  {
				case 0:
deuce's avatar
deuce committed
400
401
402
403
				case ERR:
					ch=0;
					write(out,&ch,1);
					lseek(out,-1,SEEK_CUR);
deuce's avatar
deuce committed
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
					break;
					
				case ESC:
				case 3:
					close(in);
					in=-1;
					continue;
					
				default:
					if(lseek(out,0,SEEK_CUR)>=PCHAT_LEN)
						lseek(out,0,SEEK_SET);
					chatchar(swin,ch);
					switch(write(out,&ch,1)) {
						case -1:
							close(in);
							in=-1;
							break;
					}
			}
		}
	}
	if(in != -1)
		close(in);
	if(out != -1)
		close(out);
	nocbreak();
	cbreak();
	togglechat(nodenum,node,FALSE);
	delwin(uwin);
	delwin(swin);
	touchwin(stdscr);
	refresh();
	return(0);
deuce's avatar
deuce committed
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
}

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

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
int sendmessage(int nodenum,node_t *node)  {
	char str[80],str2[80];

	uifc.input(WIN_MID,0,0,"Telegram",str2,58,K_WRAP|K_MSG);
	sprintf(str,"\1n\1y\1hMessage From Sysop:\1w %s",str2);
	if(getnodedat(&cfg,nodenum,node,NULL))
		return(-1);
	if(node->useron==0)
		return(-1);
	putsmsg(&cfg, node->useron, str);
	return(0);
}

int clearerrors(int nodenum, node_t *node) {
	if(getnodedat(&cfg,nodenum,node,&nodefile)) {
		uifc.msg("getnodedat() failed! (Nothing done)");
		return(-1);
	}
	node->errors=0;
	putnodedat(&cfg,nodenum,node,nodefile);
	return(0);
}

506
int main(int argc, char** argv)  {
507
508
509
510
511
	char**	opt;
	char**	mopt;
	int		main_dflt=0;
	int		main_bar=0;
	char revision[16];
512
	char str[256],ctrl_dir[41],*p;
513
	char title[256];
514
	int i,j;
515
	node_t node;
516
517
	char	*buf;
	int		buffile;
518
519
520
521
522
523
524
/******************/
/* Ini file stuff */
/******************/
	char	host_name[128]="";
	char	ini_file[MAX_PATH+1];
	FILE*				fp;
	bbs_startup_t		bbs_startup;
525
526

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

528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
    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,"/");

543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
	gethostname(host_name,sizeof(host_name)-1);
	sprintf(ini_file,"%s%c%s.ini",ctrl_dir,BACKSLASH,host_name);
#if defined(PREFIX)
	if(!fexistcase(ini_file))
		sprintf(ini_file,"%s/etc/sbbs.ini",PREFIX);
#endif
	if(!fexistcase(ini_file))
		sprintf(ini_file,"%s%csbbs.ini",ctrl_dir,BACKSLASH);

	/* 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, 
563
		NULL, &bbs_startup, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
564
565
566
567
568
569
570
571

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

	chdir(bbs_startup.ctrl_dir);
	
	/* Read .cfg files here */
572
	cfg.size=sizeof(cfg);
573
574
575
576
577
	if(!read_main_cfg(&cfg, str)) {
		printf("ERROR! %s\n",str);
		exit(1);
	}

578
579
580
581
582
	if(!read_xtrn_cfg(&cfg, str)) {
		printf("ERROR! %s\n",str);
		exit(1);
	}

deuce's avatar
deuce committed
583
584
585
586
587
/*	if(!read_node_cfg(&cfg, str)) {
		printf("ERROR! %s\n",str);
		exit(1);
	} */

588
589
590
591
    memset(&uifc,0,sizeof(uifc));

	uifc.esc_delay=500;

deuce's avatar
deuce committed
592
593
594
595
596
597
598
599
	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;
600
601
602
603
604
605
606
607
608
609
610
611
612
613
	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':
deuce's avatar
deuce committed
614
615
616
617
618
619
620
621
					boxch.ls=186; 
					boxch.rs=186;
					boxch.ts=205;
					boxch.bs=205;
					boxch.tl=201;
					boxch.tr=187;
					boxch.bl=200;
					boxch.br=188;
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
					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);
           }
    }

	uifc.size=sizeof(uifc);
	i=uifcinic(&uifc);  /* curses */
	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);

656
657
	sprintf(title,"Synchronet UNIX Monitor %s-%s",revision,PLATFORM_DESC);
	if(uifc.scrn(title)) {
658
659
660
661
662
		printf(" USCRN (len=%d) failed!\n",uifc.scrn_len+1);
		bail(1);
	}

	while(1) {
663
664
665
666
		for(i=1;i<=cfg.sys_nodes;i++) {
			if((j=getnodedat(&cfg,i,&node,NULL)))
				sprintf(mopt[i-1],"Error reading node data (%d)!",j);
			else
667
				nodestatus(&cfg,&node,mopt[i-1],72);
668
		}
deuce's avatar
deuce committed
669
		mopt[i-1][0]=0;
670
671

		uifc.helpbuf=	"`Synchronet Monitor:`\n"
deuce's avatar
deuce committed
672
						"\nCTRL-E displays the error log"
673
674
675
676
677
678
679
680
						"\nF12 spys on the currently selected node"
						"\nF11 send message to the currently selected node"
						"\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)";
681
						
682
		j=uifc.list(WIN_ORG|WIN_MID|WIN_ESC|WIN_ACT|WIN_DYN,0,0,70,&main_dflt,&main_bar
683
684
685
			,title,mopt);

		if(j==-7) {	/* CTRL-E */
686
			sprintf(str,"%s/error.log",cfg.data_dir);
687
			if(fexist(str)) {
deuce's avatar
deuce committed
688
				if((buffile=sopen(str,O_RDONLY,SH_DENYWR))>=0) {
689
690
691
692
693
					j=filelength(buffile);
					if((buf=(char *)MALLOC(j+1))!=NULL) {
						read(buffile,buf,j);
						close(buffile);
						*(buf+j)=0;
694
						uifc.showbuf(buf,"Error Log",0);
695
696
697
698
						free(buf);
						continue;
					}
					close(buffile);
deuce's avatar
deuce committed
699
					uifc.msg("Error allocating memory for the error log");
700
701
					continue;
				}
deuce's avatar
deuce committed
702
				uifc.msg("Error opening error log");
703
704
705
706
707
708
			}
			else {
				uifc.msg("Error log does not exist");
			}
			continue;
		}
deuce's avatar
deuce committed
709
		
710
711
712
713
714
715
		if(j==-2-KEY_DC) {	/* Clear errors */
			clearerrors(main_dflt+1,&node);
			continue;
		}

		if(j==-2-KEY_F(10)) {	/* Chat */
deuce's avatar
deuce committed
716
			chat(main_dflt+1,&node,&bbs_startup);
717
718
719
720
721
722
723
724
725
			continue;
		}

		if(j==-2-KEY_F(11)) {	/* Send message */
			sendmessage(main_dflt+1,&node);
			continue;
		}
		
		if(j==-2-KEY_F(12)) {	/* Spy */
deuce's avatar
deuce committed
726
			dospy(main_dflt+1,&bbs_startup);
727
728
729
730
731
732
733
734
735
736
737
			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
738
739
		}
		
740
741
742
743
744
745
746
747
748
		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;
		}
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
		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;
		}
		
777
		if(j <= -2)
778
779
			continue;

deuce's avatar
deuce committed
780
		if(j==-1) {
781
			i=0;
782
783
			strcpy(opt[0],YesStr);
			strcpy(opt[1],NoStr);
784
785
786
787
788
			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
789
			i=uifc.list(WIN_MID,0,0,0,&i,0,"Exit Synchronet Monitor",opt);
790
791
792
793
			if(!i)
				bail(0);
			continue;
		}
794
		if(j<cfg.sys_nodes && j>=0) {
795
796
797
798
			i=0;
			strcpy(opt[i++],"Spy on node");
			strcpy(opt[i++],"Node toggles");
			strcpy(opt[i++],"Clear Errors");
deuce's avatar
deuce committed
799
800
			getnodedat(&cfg,j+1,&node,NULL);
			if((node.status&NODE_INUSE) && node.useron) {
deuce's avatar
deuce committed
801
				strcpy(opt[i++],"Send message to user");
deuce's avatar
deuce committed
802
				strcpy(opt[i++],"Chat with user");
deuce's avatar
deuce committed
803
			}
804
805
806
807
			opt[i][0]=0;
			i=0;
			uifc.helpbuf=	"`Node Options:`\n"
							"\nToDo: Add help";
808
			switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"Node Options",opt))  {
809
				case 0:	/* Spy */
deuce's avatar
deuce committed
810
					dospy(j+1,&bbs_startup);
811
812
813
814
815
816
817
					break;

				case 1: /* Node Toggles */
					node_toggles(j+1);
					break;

				case 2:
818
					clearerrors(j+1,&node);
819
820
					break;

deuce's avatar
deuce committed
821
				case 3:	/* Send message */
822
					sendmessage(j+1,&node);
deuce's avatar
deuce committed
823
824
					break;

deuce's avatar
deuce committed
825
826
827
828
				case 4:
					chat(main_dflt+1,&node,&bbs_startup);
					break;
				
829
830
831
832
833
834
835
				case -1:
					break;
					
				default:
					uifc.msg("Option not implemented");
					break;
			}
836
837
		}
	}
838
}