summaryrefslogtreecommitdiff
path: root/news/slurp/files/siteexclude.c
blob: 67e4aaa0561611527783931e5a28b479237ed7b3 (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
162
163
164
165
166
167
168
/* siteexclude.c - check article id for excluded sites
 *
 * Copyright (C) 1995 Peter Fox <fox@roestock.demon.co.uk>
 *
 * This code may be freely copied in it's entirety, but the
 * above notice may not be removed or altered without
 * reference to the author.
 * Individuals may make changes for their own use, however
 * any changed copies may not be distributed without
 * reference to the author.
 * There isn't any guarantee of usefulness or efficacy for
 * any particular purpose. Take it as it is.
 */
#include <stdio.h>
#include <string.h>
#include "syslog.h"
#include "conf.h"

#ifdef TEST
#define SITEEXCLUDING
#endif /*TEST*/

#ifdef SITEEXCLUDING
typedef struct slist
{
	struct slist *next;
	char *str;
} SLIST, *SLISTPTR;

#define HASHSIZE 256
#define HASHFN(x) (((x[1] & 15) << 4)|(x[2] & 15)) /* x is (char *), skip '@' */

#ifdef TEST
#define SITEFILE "./sites"
#define DROPFILE "./dropped"
#define log_msg printf
#define log_sys printf
#define debug_flag 1
#else /*!TEST*/
extern int debug_flag;
#ifndef SITEFILE
#define SITEFILE "/var/lib/news/excludesites"
#endif /*SITEFILE*/
#ifndef DROPFILE
#define DROPFILE "/var/lib/news/droppedarticles"
#endif /*DROPFILE*/
#endif /*TEST*/

SLIST hash[HASHSIZE];

static FILE *dropfp = (FILE*)0;

site_init()
{
	int i;
	FILE *fp;
	char linbuf[BUFSIZ];
	SLISTPTR ptr;
	int entries, collisions;
	entries = 0;
	collisions = 0;
	for(i = 0; i < HASHSIZE; i++)
	{
		hash[i].next = (SLISTPTR)0;
		hash[i].str = (char *)0;
	}
	if(!(fp = fopen(SITEFILE, "r")))
	{
		log_msg("site_init: warning - no site exclude file %s", SITEFILE);
		return;
	}
	if(!(dropfp = fopen(DROPFILE, "a")))
	{
		log_msg("site_init: warning - cannot open file %s for append, dropped articles logged instead", DROPFILE);
	}
	else
		fprintf(dropfp, "-----\n");
	/* File contains lines with complete site names to exclude */
	/* We add the leading @ and trailing > */
	linbuf[0] = '@';
	while(1)
	{
        /* Read in a line */
        (void) fgets(linbuf+1, sizeof (linbuf)-2, fp);
        if(feof(fp))
			break;
        /* If a read error then report it and abort */
        if (ferror(fp))
		{
            log_sys("site_init: error reading %s", SITEFILE);
            break;
		}
		if(linbuf[1])
		{
			/* Kill the \n */
			linbuf[strlen(linbuf)-1] = '>';
			entries++;
			ptr = &hash[HASHFN(linbuf)];
			if(debug_flag)
				fprintf(stderr, "'%s: %08lx'\n", linbuf, ptr);
			if(!ptr->str)
				ptr->str = strdup(linbuf);
			else
			{
				/* Run along to the last in line */
				while(ptr->next)
					ptr = ptr->next;
				ptr->next = (SLISTPTR)malloc(sizeof(SLIST));
				ptr = ptr->next;
				ptr->next = (SLISTPTR)0;
				ptr->str = strdup(linbuf);
				collisions++;
			}
		}
	}
	fclose(fp);
	if(debug_flag)
		log_msg("site_init: site exclude file %s read: %d entries, %d collisions",
			SITEFILE, entries, collisions);
}

site_check(char *site)
{
	SLISTPTR ptr;
	ptr = &hash[HASHFN(site)];
	if(debug_flag)
		fprintf(stderr, " '%s: %08lx'", site, ptr);
	if(!ptr->str)
		return(0);
	while(ptr)
	{
		if(strcmp(site, ptr->str) == 0)
			return(1);
		ptr = ptr->next;
	}
	return(0);
}

site_droparticle(char *id)
{
	if(dropfp)
		fprintf(dropfp, "%s\n", id);
	else
		log_msg("Excluded article: %s", id);
}

site_close()
{
	if(dropfp)
		fclose(dropfp);
}

#ifdef TEST
main(int argc, char **argv)
{
	site_init();
	argv++;
	argc--;
	while(argc--)
	{
		if(site_check(*argv))
			printf("Found %s\n", *argv);
		argv++;
	}
	return(0);
}
#endif /*TEST*/
#endif /* SITEEXCLUDING */