1 /*
2  *	binkleyforce -- unix FTN mailer project
3  *
4  *	Copyright (c) 1998-2000 Alexander Belkin, 2:5020/1398.11
5  *
6  *	This program is free software; you can redistribute it and/or modify
7  *	it under the terms of the GNU General Public License as published by
8  *	the Free Software Foundation; either version 2 of the License, or
9  *	(at your option) any later version.
10  *
11  *	$Id: bfindex.c,v 1.1.1.1 2004/09/09 09:52:40 kstepanenkov Exp $
12  */
13 
14 #include "includes.h"
15 #include "confread.h"
16 #include "version.h"
17 #include "logger.h"
18 #include "util.h"
19 #include "nodelist.h"
20 
21 /*
22  *  Our fake expressions checker. Allways return FALSE (?)
23  */
eventexpr(s_expr * expr)24 bool eventexpr(s_expr *expr)
25 {
26 	return FALSE;
27 }
28 
usage(void)29 static void usage(void)
30 {
31 	printf_usage("nodelist compiler",
32 		"usage: bfindex [-fh]\n"
33 		"\n"
34 		"options:\n"
35 		"  -f                force nodelist compiling\n"
36 		"  -h                show this help message\n"
37 		"\n"
38 	);
39 }
40 
nodelist_makeindex(s_nodelist * nlp,s_faddr addr)41 static int nodelist_makeindex(s_nodelist *nlp, s_faddr addr)
42 {
43 	s_bni bni;
44 	char buf[1024];
45 	long countnodes = 0L;
46 	long countlines = 0L;
47 	enum nodelist_keyword keyword;
48 
49 	memset(&bni, 0, sizeof(s_bni));
50 	bni.zone   = addr.zone;
51 	bni.net    = addr.net;
52 	bni.node   = addr.node;
53 	bni.point  = addr.point;
54 
55 	if( nodelist_createheader(nlp) == -1 )
56 	{
57 		bf_log("cannot create nodelist index header");
58 		return -1;
59 	}
60 
61 	while(1)
62 	{
63 		char *p, *q;
64 
65 		if( (bni.offset = ftell(nlp->fp_nodelist)) == -1 )
66 		{
67 			logerr("ftell() return -1 for nodelist");
68 			return -1;
69 		}
70 
71 		if( fgets(buf, sizeof(buf), nlp->fp_nodelist) == NULL )
72 		{
73 			return countnodes;
74 		}
75 
76 		string_chomp(buf);
77 		++countlines;
78 
79 		if( buf[0] == ';' || buf[0] == '\0' ) continue;
80 
81 		if( (p = strchr(buf, ',')) )
82 		{
83 			*p++ = '\0';
84 			if( (q = strchr(p, ',')) ) *q = '\0';
85 		}
86 
87 		if( p == NULL || *p == '\0' )
88 		{
89 			bf_log("incorrect nodelist line %ld: Short line", countlines);
90 			continue;
91 		}
92 
93 		if( (keyword = nodelist_keywordval(buf)) == -1 )
94 		{
95 			bf_log("incorrect nodelist line %d: Bad keyword \"%s\"",
96 				countlines, buf);
97 			continue;
98 		}
99 
100 		if( keyword == KEYWORD_BOSS )
101 		{
102 			s_faddr tmpaddr;
103 
104 			if( ftn_addrparse(&tmpaddr, p, FALSE) )
105 			{
106 				bf_log("incorrect nodelist line %ld: Bad boss address \"%s\"",
107 					countlines, p);
108 			}
109 			else
110 			{
111 				bni.zone  = tmpaddr.zone;
112 				bni.net   = tmpaddr.net;
113 				bni.node  = tmpaddr.node;
114 				bni.point = 0;
115 				bni.hub   = 0;
116 			}
117 		}
118 		else if( ISDEC(p) )
119 		{
120 			int value = atoi(p);
121 
122 			if( value > 0x7fff )
123 				value = 0x7fff;
124 			else if( value < 0 )
125 				value = 0;
126 
127 			switch(keyword) {
128 			case KEYWORD_ZONE:
129 				bni.zone  = value;
130 				bni.net   = 0;
131 				bni.node  = 0;
132 				bni.point = 0;
133 				bni.hub   = 0;
134 				break;
135 			case KEYWORD_REGION:
136 				bni.net   = value;
137 				bni.node  = 0;
138 				bni.point = 0;
139 				bni.hub   = 0;
140 				break;
141 			case KEYWORD_HOST:
142 				bni.net   = value;
143 				bni.node  = 0;
144 				bni.point = 0;
145 				bni.hub   = 0;
146 				break;
147 			case KEYWORD_HUB:
148 				bni.node  = value;
149 				bni.point = 0;
150 				bni.hub   = value;
151 				break;
152 			case KEYWORD_EMPTY:
153 			case KEYWORD_PVT:
154 			case KEYWORD_HOLD:
155 			case KEYWORD_DOWN:
156 				bni.node  = value;
157 				bni.point = 0;
158 				break;
159 			case KEYWORD_POINT:
160 				bni.point = value;
161 				break;
162 			default:
163 				ASSERT_MSG();
164 			}
165 
166 			if( nodelist_putindex(nlp, &bni) == -1 )
167 				return -1;
168 
169 			++countnodes;
170 		}
171 		else
172 		{
173 			bf_log("incorrect nodelist line %ld: Bad number \"%s\"",
174 				countlines, p);
175 		}
176 	}
177 
178 	return -1;
179 }
180 
main(int argc,char * argv[])181 int main(int argc, char *argv[])
182 {
183 	s_cval_entry *cfptr;
184 	char *nodelistdir = NULL;
185 	time_t starttime  = 0L;
186 	bool forcecompile = FALSE;
187 	long countnodes   = 0L;
188 	char c;
189 
190 	/* Initialise random number generation */
191 	(void)srand((unsigned)time(0));
192 	/* Initialise current locale */
193 	(void)setlocale(LC_ALL, "");
194 	/* Set our name (for logging only) */
195 
196 	while( (c = getopt(argc, argv, "hf")) != EOF )
197 	{
198 		switch( c ) {
199 		case 'f':
200 			forcecompile = TRUE;
201 			break;
202 		case 'h':
203 			usage();
204 			exit(0);
205 		default:
206 			usage();
207 			exit(1);
208 		}
209 	}
210 
211 	if( conf_readconf(conf_getconfname(), 0) )
212 	{
213 		exit(1);
214 	}
215 
216 	if( log_open(log_getfilename(LOG_FILE_SESSION), NULL, NULL) )
217 	{
218 		bf_log("can't continue without logging");
219 		exit(1);
220 	}
221 
222 	/* Ignore some terminating signals */
223 	signal(SIGHUP, SIG_IGN);
224 	signal(SIGINT, SIG_IGN);
225 	signal(SIGTERM, SIG_IGN);
226 
227 	time(&starttime);
228 
229 	nodelistdir = conf_string(cf_nodelist_directory);
230 
231 	for( cfptr = conf_first(cf_nodelist); cfptr; cfptr = conf_next(cfptr) )
232 	{
233 		s_nodelist *nlp = NULL;
234 		bool willcompile = FALSE;
235 
236 		if( forcecompile )
237 		{
238 			willcompile = TRUE;
239 		}
240 		else if( (nlp = nodelist_open(nodelistdir,
241 		                              cfptr->d.falist.what,
242 		                              NODELIST_READ)) == NULL )
243 		{
244 			willcompile = TRUE;
245 		}
246 		else
247 		{
248 			nodelist_close(nlp); nlp = NULL;
249 			willcompile = FALSE;
250 		}
251 
252 		if( willcompile )
253 		{
254 			bf_log("rebuilding index for nodelist \"%s\"", cfptr->d.falist.what);
255 			if( (nlp = nodelist_open(nodelistdir, cfptr->d.falist.what,
256 			                         NODELIST_WRITE)) )
257 			{
258 				long rc = nodelist_makeindex(nlp, cfptr->d.falist.addr);
259 				nodelist_close(nlp); nlp = NULL;
260 				if( rc > 0 ) countnodes += rc;
261 			}
262 		}
263 		else
264 			bf_log("nodelist \"%s\" is up to date", cfptr->d.falist.what);
265 	}
266 
267 	if( countnodes > 0 )
268 	{
269 		long ptime = (long)time(NULL) - (long)starttime;
270 		long nps = (ptime > 0) ? countnodes / ptime : countnodes;
271 		bf_log("processed %ld nodes in %ld second(s) (%ld node/sec)",
272 			countnodes, ptime, nps);
273 	}
274 
275 	deinit_conf();
276 
277 	/*
278 	 * Shutdown logging services
279 	 */
280 	if( log_isopened() ) log_close();
281 #ifdef DEBUG
282 	if( debug_isopened() ) debug_close();
283 #endif
284 
285 	exit(0);
286 }
287