smbactiv.c 4.69 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
/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "load_cfg.h"
#include "smblib.h"
#include "str_util.h"
#include "nopen.h"
#include <stdarg.h>
deuce's avatar
deuce committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#define SMBACTIV_VER "1.01"

typedef struct {
	ulong read;
	ulong firstmsg;
} sub_status_t;

smb_t smb;

ulong first_msg()
{
	smbmsg_t msg;

39
	msg.idx_offset=0;
rswindell's avatar
rswindell committed
40
41
42
43
	msg.hdr.number=0;
	if(smb_getmsgidx(&smb,&msg))			/* Get first message index */
		return(0);
	return(msg.idx.number);
deuce's avatar
deuce committed
44
45
}

deuce's avatar
deuce committed
46
long lputs(char *str)
deuce's avatar
deuce committed
47
48
49
50
{
    char tmp[256];
    int i,j,k;

rswindell's avatar
rswindell committed
51
52
53
54
55
56
57
58
	j=strlen(str);
	for(i=k=0;i<j;i++)      /* remove CRs */
		if(str[i]==CR && str[i+1]==LF)
			continue;
		else
			tmp[k++]=str[i];
	tmp[k]=0;
	return(fputs(tmp,stderr));
deuce's avatar
deuce committed
59
60
61
62
63
64
}

/****************************************************************************/
/* Performs printf() through local assembly routines                        */
/* Called from everywhere                                                   */
/****************************************************************************/
65
int lprintf(const char *fmat, ...)
deuce's avatar
deuce committed
66
67
68
69
70
{
	va_list argptr;
	char sbuf[256];
	int chcount;

rswindell's avatar
rswindell committed
71
72
73
74
75
	va_start(argptr,fmat);
	chcount=vsprintf(sbuf,fmat,argptr);
	va_end(argptr);
	lputs(sbuf);
	return(chcount);
deuce's avatar
deuce committed
76
77
78
79
}

void bail(int code)
{
rswindell's avatar
rswindell committed
80
	exit(code);
deuce's avatar
deuce committed
81
82
83
84
}

int main(int argc, char **argv)
{
85
86
	char str[256];
	const char *p;
deuce's avatar
deuce committed
87
	int i,j,file;
88
89
	off_t length;
	ulong max_users=0xffffffff;
deuce's avatar
64-bit    
deuce committed
90
	uint32_t l;
deuce's avatar
deuce committed
91
92
93
	sub_status_t *sub_status;
	scfg_t	cfg;
	glob_t	gl;
deuce's avatar
deuce committed
94
	size_t	glp;
deuce's avatar
deuce committed
95

rswindell's avatar
rswindell committed
96
97
	fprintf(stderr,"\nSMBACTIV Version %s (%s) - Synchronet Message Base Activity "
		"Monitor\n", SMBACTIV_VER, PLATFORM_DESC);
deuce's avatar
deuce committed
98
99
100
101
102
103
104
105
106
107

	if(argc>1)
		max_users=atol(argv[1]);

	if(!max_users) {
		lprintf("\nusage: SMBACTIV [max_users]\n\n");
		lprintf("max_users = limit output to subs read by this many users "
			"or less\n");
		return(0); }

108
	p = get_ctrl_dir(/* warn: */TRUE);
deuce's avatar
deuce committed
109
110
111
112
113

	memset(&cfg,0,sizeof(cfg));
	cfg.size=sizeof(cfg);
	SAFECOPY(cfg.ctrl_dir,p);

114
	if(!load_cfg(&cfg, /* text: */NULL, /* prep: */TRUE, /* node: */FALSE, str, sizeof(str))) {
deuce's avatar
deuce committed
115
116
117
		fprintf(stderr,"!ERROR loading configuration files: %s\n",str);
		return(1);
	}
118
	(void)chdir(cfg.ctrl_dir);
deuce's avatar
deuce committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

	if((sub_status=(sub_status_t *)MALLOC
		(cfg.total_subs*sizeof(sub_status_t)))==NULL) {
		printf("ERROR Allocating memory for sub_status\r\n");
		return(1); }

	lprintf("\nReading sub-board ");
	for(i=0;i<cfg.total_subs;i++) {
		lprintf("%5d of %-5d\b\b\b\b\b\b\b\b\b\b\b\b\b\b",i+1,cfg.total_subs);
		sprintf(smb.file,"%s%s",cfg.sub[i]->data_dir,cfg.sub[i]->code);
		if((j=smb_open(&smb))!=0) {
			lprintf("Error %d opening %s\r\n",j,smb.file);
			sub_status[i].read=0;
			sub_status[i].firstmsg=0L;
            continue; }
		sub_status[i].read=0;
		sub_status[i].firstmsg=first_msg();
		smb_close(&smb); }

	sprintf(str,"%suser/ptrs/*.ixb",cfg.data_dir);
	if(glob(str, GLOB_MARK, NULL, &gl)) {
		lprintf("Unable to find any user pointer files.\n");
		globfree(&gl);
deuce's avatar
deuce committed
142
		free(sub_status);
deuce's avatar
deuce committed
143
144
145
		return(1); }

	lprintf("\nComparing user pointers ");
deuce's avatar
deuce committed
146
	for(glp=0; glp<gl.gl_pathc; glp++) {
147
		lprintf("%-5ld\b\b\b\b\b",glp);
deuce's avatar
deuce committed
148
		SAFECOPY(str,gl.gl_pathv[glp]);
deuce's avatar
deuce committed
149
150
151
152
153
154
		if((file=nopen(str,O_RDONLY|O_BINARY))==-1) {
			continue; }
		length=filelength(file);
		for(i=0;i<cfg.total_subs;i++) {
			if(sub_status[i].read>max_users)
				continue;
deuce's avatar
deuce committed
155
			if(length<(cfg.sub[i]->ptridx+1)*10UL)
deuce's avatar
deuce committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
				continue;
			else {
				lseek(file,((long)cfg.sub[i]->ptridx*10L)+4L,SEEK_SET);
				read(file,&l,4); }
			if(l>sub_status[i].firstmsg)
				sub_status[i].read++; }
		close(file); }
	globfree(&gl);

	printf("NumUsers    Sub-board\n");
	printf("--------    -------------------------------------------------"
		"-----------\n");
	for(i=0;i<cfg.total_subs;i++) {
		if(sub_status[i].read>max_users)
			continue;
		printf("%8lu    %-*s %-*s\n"
			,sub_status[i].read
			,LEN_GSNAME,cfg.grp[cfg.sub[i]->grp]->sname
			,LEN_SLNAME,cfg.sub[i]->lname); }

	return(0);
}