umonitor.c 20.5 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
38
39
/* 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.	*
 ****************************************************************************/

#include "conwrap.h"	/* this has to go BEFORE curses.h so getkey() can be macroed around */
#include <curses.h>
40
41
#include <poll.h>
#include <stdio.h>
42
#include <termios.h>
43
#include <unistd.h>
44
45
46
#include "sockwrap.h"	/* Must go before <sys/un.h> */
#include <sys/un.h>
#include "genwrap.h"
47
48
49
#include "uifc.h"
#include "sbbsdefs.h"
#include "genwrap.h"	/* stricmp */
50
#include "dirwrap.h"	/* lock/unlock/sopen */
51
#include "filewrap.h"	/* lock/unlock/sopen */
52
#include "sockwrap.h"
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
enum {
	 MODE_LIST
	,MODE_ANON
	,MODE_LOCK
	,MODE_INTR
	,MODE_RRUN
	,MODE_DOWN
	,MODE_EVENT
	,MODE_NOPAGE
	,MODE_NOALERTS
	,MODE_STATUS
	,MODE_USERON
	,MODE_ACTION
	,MODE_ERRORS
	,MODE_MISC
	,MODE_CONN
	,MODE_AUX
	,MODE_EXTAUX
	};

74
75
76
77
78
79
80
81
enum {
	 SPY_NOSOCKET
	,SPY_NOCONNECT
	,SPY_SOCKETLOST
	,SPY_STDINLOST
	,SPY_CLOSED
	};

82
83
84
85
86
87
/********************/
/* Global Variables */
/********************/
uifcapi_t uifc; /* User Interface (UIFC) Library API */
char tmp[256];
int nodefile;
88
89
90
const char *YesStr="Yes";
const char *NoStr="No";

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

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

109
int spyon(char *sockname)  {
110
111
112
113
114
115
116
117
118
	SOCKET		spy_sock=INVALID_SOCKET;
	struct sockaddr_un spy_name;
	socklen_t	spy_len;
	char		key;
	char		buf[1];
	int		i;
	struct	termios	tio;
	struct	termios	old_tio;
	struct pollfd pset[2];
119
	BOOL	b;
120
	int		retval=0;
121
122

	/* ToDo Test for it actually being a socket! */
123
124
	/* Well, it will fail to connect won't it?   */

125
126
127

	if((spy_sock=socket(PF_LOCAL,SOCK_STREAM,0))==INVALID_SOCKET)  {
		printf("ERROR %d creating local spy socket", errno);
128
		return(SPY_NOSOCKET);
129
130
131
132
133
134
	}
	
	spy_name.sun_family=AF_LOCAL;
	SAFECOPY(spy_name.sun_path,sockname);
	spy_len=SUN_LEN(&spy_name);
	if(connect(spy_sock,(struct sockaddr *)&spy_name,spy_len))  {
135
		return(SPY_NOCONNECT);
136
137
138
139
140
	}
	i=1;

	tcgetattr(STDIN_FILENO,&old_tio);
	tcgetattr(STDIN_FILENO,&tio);
deuce's avatar
deuce committed
141
	cfmakeraw(&tio);
142
	tcsetattr(STDIN_FILENO,TCSANOW,&tio);
143
	printf("\r\n\r\nLocal spy mode... press CTRL-C to return to monitor\r\n\r\n");
144
145
146
147
148
	pset[0].fd=STDIN_FILENO;
	pset[0].events=POLLIN;
	pset[1].fd=spy_sock;
	pset[1].events=POLLIN;
	while(spy_sock!=INVALID_SOCKET)  {
149
		if((poll(pset,2,-1))<0)  {
150
151
			close(spy_sock);
			spy_sock=INVALID_SOCKET;
152
153
154
155
156
			break;
		}
		if(!socket_check(spy_sock,NULL,&b,0)) {
			close(spy_sock);
			spy_sock=INVALID_SOCKET;
157
			retval=SPY_SOCKETLOST;
158
			break;
159
160
161
162
163
		}
		if(pset[0].revents)  {
			if(pset[0].revents&(POLLNVAL|POLLHUP|POLLERR))  {
				close(spy_sock);
				spy_sock=INVALID_SOCKET;
164
				retval=SPY_STDINLOST;
165
				break;
166
167
			}
			else  {
deuce's avatar
deuce committed
168
169
170
171
172
173
				if((i=read(STDIN_FILENO,&key,1))==1)  {
					/* Check for control keys */
					switch(key)  {
						case CTRL('c'):
							close(spy_sock);
							spy_sock=INVALID_SOCKET;
174
							retval=SPY_CLOSED;
175
							break;
deuce's avatar
deuce committed
176
177
178
						default:
							write(spy_sock,&key,1);
					}
179
				}
deuce's avatar
deuce committed
180
				else if(i<0) {
181
182
					close(spy_sock);
					spy_sock=INVALID_SOCKET;
183
					retval=SPY_STDINLOST;
184
					break;
185
186
187
188
189
190
191
				}
			}
		}
		if(pset[1].revents)  {
			if(pset[1].revents&(POLLNVAL|POLLHUP|POLLERR))  {
				close(spy_sock);
				spy_sock=INVALID_SOCKET;
192
				retval=SPY_SOCKETLOST;
193
				break;
194
195
			}
			else  {
deuce's avatar
deuce committed
196
				if((i=read(spy_sock,&buf,1))==1)  {
197
					write(STDOUT_FILENO,buf,1);
deuce's avatar
deuce committed
198
199
				}
				else if(i<0) {
200
201
					close(spy_sock);
					spy_sock=INVALID_SOCKET;
202
					retval=SPY_SOCKETLOST;
203
					break;
204
205
206
207
208
				}
			}
		}
	}
	tcsetattr(STDIN_FILENO,TCSANOW,&old_tio);
209
	return(retval);
210
211
}

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
char* itoa(int val, char* str, int radix)
{
	switch(radix) {
		case 8:
			sprintf(str,"%o",val);
			break;
		case 10:
			sprintf(str,"%u",val);
			break;
		case 16:
			sprintf(str,"%x",val);
			break;
		default:
			sprintf(str,"bad radix: %d",radix);
			break;
	}
	return(str);
}

/****************************************************************************/
/* Unpacks the password 'pass' from the 5bit ASCII inside node_t. 32bits in */
/* node.extaux, and the other 8bits in the upper byte of node.aux			*/
/****************************************************************************/
char *unpackchatpass(char *pass, node_t node)
{
	char 	bits;
	int 	i;

	pass[0]=(node.aux&0x1f00)>>8;
	pass[1]=(char)(((node.aux&0xe000)>>13)|((node.extaux&0x3)<<3));
	bits=2;
	for(i=2;i<8;i++) {
		pass[i]=(char)((node.extaux>>bits)&0x1f);
		bits+=5; }
	pass[8]=0;
	for(i=0;i<8;i++)
		if(pass[i])
			pass[i]+=64;
	return(pass);
}

/******************************************************************************************/
/* Returns the information for node number 'number' contained in 'node' into string 'str' */
/******************************************************************************************/
char *nodedat(char *str, int number, node_t node)
{
    char hour,mer[3];
	char buf[1024];

	sprintf(str,"Node %2d: ",number);
	switch(node.status) {
		case NODE_WFC:
			strcat(str,"Waiting for call");
			break;
		case NODE_OFFLINE:
			strcat(str,"Offline");
			break;
		case NODE_NETTING:
			strcat(str,"Networking");
			break;
		case NODE_LOGON:
			strcat(str,"At logon prompt");
			break;
		case NODE_EVENT_WAITING:
			strcat(str,"Waiting for all nodes to become inactive");
			break;
		case NODE_EVENT_LIMBO:
			sprintf(buf,"Waiting for node %d to finish external event",node.aux);
			strcat(str,buf);
			break;
		case NODE_EVENT_RUNNING:
			strcat(str,"Running external event");
			break;
		case NODE_NEWUSER:
			strcat(str,"New user");
			strcat(str," applying for access ");
			if(!node.connection)
				strcat(str,"locally");
			else if(node.connection==0xffff)
				strcat(str,"via telnet");
			else {
				sprintf(buf,"at %ubps",node.connection);
				strcat(str,buf); }
			break;
		case NODE_QUIET:
		case NODE_INUSE:
			sprintf(buf,"User #%d",node.useron);
			strcat(str,buf);
			strcat(str," ");
			switch(node.action) {
				case NODE_MAIN:
					strcat(str,"at main menu");
					break;
				case NODE_RMSG:
					strcat(str,"reading messages");
					break;
				case NODE_RMAL:
					strcat(str,"reading mail");
					break;
				case NODE_RSML:
					strcat(str,"reading sent mail");
					break;
				case NODE_RTXT:
					strcat(str,"reading text files");
					break;
				case NODE_PMSG:
					strcat(str,"posting message");
					break;
				case NODE_SMAL:
					strcat(str,"sending mail");
					break;
				case NODE_AMSG:
					strcat(str,"posting auto-message");
					break;
				case NODE_XTRN:
					if(!node.aux)
						strcat(str,"at external program menu");
					else
						sprintf(buf,"running external program #%d",node.aux);
						strcat(str,buf);
					break;
				case NODE_DFLT:
					strcat(str,"changing defaults");
					break;
				case NODE_XFER:
					strcat(str,"at transfer menu");
					break;
				case NODE_RFSD:
					sprintf(buf,"retrieving from device #%d",node.aux);
					strcat(str,buf);
					break;
				case NODE_DLNG:
					strcat(str,"downloading");
					break;
				case NODE_ULNG:
					strcat(str,"uploading");
					break;
				case NODE_BXFR:
					strcat(str,"transferring bidirectional");
					break;
				case NODE_LFIL:
					strcat(str,"listing files");
					break;
				case NODE_LOGN:
					strcat(str,"logging on");
					break;
				case NODE_LCHT:
					strcat(str,"in local chat with sysop");
					break;
				case NODE_MCHT:
					if(node.aux) {
						sprintf(buf,"in multinode chat channel %d",node.aux&0xff);
						strcat(str,buf);
						if(node.aux&0x1f00) { /* password */
							strcat(str,"*");
							sprintf(buf," %s",unpackchatpass(tmp,node));
							strcat(str,buf); } }
					else
						strcat(str,"in multinode global chat channel");
					break;
				case NODE_PAGE:
					sprintf(buf,"paging node %u for private chat",node.aux);
					strcat(str,buf);
					break;
				case NODE_PCHT:
					sprintf(buf,"in private chat with node %u",node.aux);
					strcat(str,buf);
					break;
				case NODE_GCHT:
					strcat(str,"chatting with The Guru");
					break;
				case NODE_CHAT:
					strcat(str,"in chat section");
					break;
				case NODE_TQWK:
					strcat(str,"transferring QWK packet");
					break;
				case NODE_SYSP:
					strcat(str,"performing sysop activities");
					break;
				default:
					sprintf(buf,itoa(node.action,tmp,10));
					strcat(str,buf);
					break;  }
			if(!node.connection)
				strcat(str," locally");
			else if(node.connection==0xffff)
				strcat(str," via telnet");
			else {
				sprintf(buf," at %ubps",node.connection);
				strcat(str,buf); }
			if(node.action==NODE_DLNG) {
				if((node.aux/60)>=12) {
					if(node.aux/60==12)
						hour=12;
					else
						hour=(node.aux/60)-12;
					strcpy(mer,"pm"); }
				else {
					if((node.aux/60)==0)    /* 12 midnite */
						hour=12;
					else hour=node.aux/60;
					strcpy(mer,"am"); }
				sprintf(buf," ETA %02d:%02d %s"
					,hour,node.aux-((node.aux/60)*60),mer); 
				strcat(str,buf); }
			break; }
	if(node.misc&(NODE_LOCK|NODE_POFF|NODE_AOFF|NODE_MSGW|NODE_NMSG)) {
		strcat(str," (");
		if(node.misc&NODE_AOFF)
			strcat(str,"A");
		if(node.misc&NODE_LOCK)
			strcat(str,"L");
		if(node.misc&(NODE_MSGW|NODE_NMSG))
			strcat(str,"M");
		if(node.misc&NODE_POFF)
			strcat(str,"P");
		strcat(str,")"); }
	if(((node.misc
		&(NODE_ANON|NODE_UDAT|NODE_INTR|NODE_RRUN|NODE_EVENT|NODE_DOWN))
		|| node.status==NODE_QUIET)) {
		strcat(str," [");
		if(node.misc&NODE_ANON)
			strcat(str,"A");
		if(node.misc&NODE_INTR)
			strcat(str,"I");
		if(node.misc&NODE_RRUN)
			strcat(str,"R");
		if(node.misc&NODE_UDAT)
			strcat(str,"U");
		if(node.status==NODE_QUIET)
			strcat(str,"Q");
		if(node.misc&NODE_EVENT)
			strcat(str,"E");
		if(node.misc&NODE_DOWN)
			strcat(str,"D");
		if(node.misc&NODE_LCHAT)
			strcat(str,"C");
		strcat(str,"]"); }
	if(node.errors) {
		sprintf(buf," %d error%c",node.errors, node.errors>1 ? 's' : '\0' );
		strcat(str,buf); }
	return(str);
}

/****************************************************************************/
/* Reads the data for node number 'number' into the structure 'node'        */
/* from NODE.DAB															*/
/* if lockit is non-zero, locks this node's record. putnodedat() unlocks it */
/****************************************************************************/
void getnodedat(int number, node_t *node, int lockit)
{
	int count;
	char	msg[80];

	number--;	/* make zero based */
	for(count=0;count<LOOP_NODEDAB;count++) {
		if(count)
			SLEEP(100);
		lseek(nodefile,(long)number*sizeof(node_t),SEEK_SET);
		if(lockit
			&& lock(nodefile,(long)number*sizeof(node_t),sizeof(node_t))==-1) 
			continue; 
		if(read(nodefile,node,sizeof(node_t))==sizeof(node_t))
			break;
	}
	if(count>=(LOOP_NODEDAB/2)) {
		sprintf(msg,"NODE.DAB (node %d) COLLISION (READ) - Count: %d\n"
			,number+1, count); 
		uifc.msg(msg); }
	else if(count==LOOP_NODEDAB) {
		sprintf(msg,"!Error reading nodefile for node %d\n",number+1);
		uifc.msg(msg); }
}

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
/****************************************************************************/
/* Write the data from the structure 'node' into NODE.DAB  					*/
/* getnodedat(num,&node,1); must have been called before calling this func  */
/*          NOTE: ------^   the indicates the node record has been locked   */
/****************************************************************************/
void putnodedat(int number, node_t node)
{
	number--;	/* make zero based */
	lseek(nodefile,(long)number*sizeof(node_t),SEEK_SET);
	if(write(nodefile,&node,sizeof(node_t))!=sizeof(node_t)) {
		unlock(nodefile,(long)number*sizeof(node_t),sizeof(node_t));
		printf("Error writing to nodefile for node %d\n",number+1);
		return; }
	unlock(nodefile,(long)number*sizeof(node_t),sizeof(node_t));
}

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

	getnodedat(nodenum,&node,0);
	i=0;
	uifc.helpbuf=	"`Node Toggles:`\n"
518
					"\nToDo: Add help (Mention that changes take effect immediately)";
519
520
521
522
523
524
525
	while(save==0) {
		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);
526
		sprintf(opt[j++],"%-30s%3s","Down node after logoff",(node.misc&NODE_DOWN || (node.status==NODE_OFFLINE)) ? YesStr : NoStr);
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
		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 */
552
				if(node.status == NODE_INUSE || node.status==NODE_LOGON)
553
					node.misc ^= NODE_DOWN;
554
				else {
555
556
557
558
559
					if(node.status!=NODE_OFFLINE)
						node.status=NODE_OFFLINE;
					else
						node.status=NODE_WFC;
				}
560
561
562
563
564
565
566
567
568
569
570
571
572
573
				break;

			case 6:	/* Reset chat */
				node.misc ^= NODE_RPCHT;
				break;
				
			case -1:
				save=1;
				break;
				
			default:
				uifc.msg("Option not implemented");
				continue;
		}
574
575
		lock(nodefile,(long)(nodenum-1)*sizeof(node_t),sizeof(node_t));
		putnodedat(nodenum,node);
576
577
578
	}
}

579
int main(int argc, char** argv)  {
580
581
582
583
584
	char**	opt;
	char**	mopt;
	int		main_dflt=0;
	int		main_bar=0;
	char revision[16];
585
	char str[256],str2[256],ctrl_dir[41],*p;
586
	char title[256];
587
588
	int sys_nodes;
	int i,j;
589
	node_t node;
590
591
	char	*buf;
	int		buffile;
592
593

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

595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
    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,"/");

	sprintf(str,"%snode.dab",ctrl_dir);
	if((nodefile=sopen(str,O_RDWR|O_BINARY,SH_DENYNO))==-1) {
		printf("\7\nError %d opening %s.\n",errno,str);
		exit(1); }

	sys_nodes=filelength(nodefile)/sizeof(node_t);
	if(!sys_nodes) {
		printf("%s reflects 0 nodes!\n",str);
		exit(1); }

    memset(&uifc,0,sizeof(uifc));

	uifc.esc_delay=500;

	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':
					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);

672
673
	sprintf(title,"Synchronet UNIX Monitor %s-%s",revision,PLATFORM_DESC);
	if(uifc.scrn(title)) {
674
675
676
677
678
679
680
681
682
		printf(" USCRN (len=%d) failed!\n",uifc.scrn_len+1);
		bail(1);
	}

	while(1) {
		for(i=1;i<=sys_nodes;i++) {
			getnodedat(i,&node,0);
			nodedat(mopt[i-1],i,node);
		}
deuce's avatar
deuce committed
683
		mopt[i-1][0]=0;
684
685

		uifc.helpbuf=	"`Synchronet Monitor:`\n"
deuce's avatar
deuce committed
686
687
						"\nCTRL-E displays the error log"
						"\nToDo: Add more help.";
688
						
689
		j=uifc.list(WIN_ORG|WIN_MID|WIN_ESC|WIN_ACT|WIN_DYN,0,0,70,&main_dflt,&main_bar
690
691
692
693
694
695
			,title,mopt);

		if(j==-7) {	/* CTRL-E */
			/* ToDo must get the logs dir from the config */
			sprintf(str,"%s../data/error.log",ctrl_dir);
			if(fexist(str)) {
deuce's avatar
deuce committed
696
				if((buffile=sopen(str,O_RDONLY,SH_DENYWR))>=0) {
697
698
699
700
701
702
703
704
705
706
707
708
709
					j=filelength(buffile);
					if((buf=(char *)MALLOC(j+1))!=NULL) {
						read(buffile,buf,j);
						close(buffile);
						*(buf+j)=0;
						uifc.helpbuf=buf;
						uifc.helptitle="Error Log";
						uifc.showhelp();
						uifc.helptitle=NULL;
						free(buf);
						continue;
					}
					close(buffile);
deuce's avatar
deuce committed
710
					uifc.msg("Error allocating memory for the error log");
711
712
					continue;
				}
deuce's avatar
deuce committed
713
				uifc.msg("Error opening error log");
714
715
716
717
718
719
720
721
			}
			else {
				uifc.msg("Error log does not exist");
			}
			continue;
		}

		if(j <= -2)
722
723
			continue;

deuce's avatar
deuce committed
724
		if(j==-1) {
725
			i=0;
726
727
			strcpy(opt[0],YesStr);
			strcpy(opt[1],NoStr);
728
729
730
731
732
			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
733
			i=uifc.list(WIN_MID,0,0,0,&i,0,"Exit Synchronet Monitor",opt);
734
735
736
737
738
			if(!i)
				bail(0);
			continue;
		}
		if(j<sys_nodes && j>=0) {
739
740
741
742
743
744
745
746
			i=0;
			strcpy(opt[i++],"Spy on node");
			strcpy(opt[i++],"Node toggles");
			strcpy(opt[i++],"Clear Errors");
			opt[i][0]=0;
			i=0;
			uifc.helpbuf=	"`Node Options:`\n"
							"\nToDo: Add help";
747
			switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"Node Options",opt))  {
748
749
750
				case 0:	/* Spy */
					snprintf(str,sizeof(str),"%slocalspy%d.sock", ctrl_dir, j+1);
					endwin();
751
					i=spyon(str);
752
					refresh();
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
					switch(i) {
						case SPY_NOSOCKET:
							uifc.msg("Could not create socket");
							break;
							
						case SPY_NOCONNECT:
							sprintf(str2,"Failed to connect to %s",str);
							uifc.msg(str2);
							break;
							
						case SPY_SOCKETLOST:
							uifc.msg("Spy socket lost");
							break;
							
						case SPY_STDINLOST:
							uifc.msg("STDIN has gone away... you probobly can't close this window.  :-)");
							break;
							
						case SPY_CLOSED:
							break;
							
						default:
							sprintf(str,"Unknown return code %d",i);
							uifc.msg(str);
					}
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
					break;

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

				case 2:
					getnodedat(j+1,&node,1);
					node.errors=0;
					putnodedat(j+1,node);
					break;

				case -1:
					break;
					
				default:
					uifc.msg("Option not implemented");
					break;
			}
797
798
		}
	}
799
}