1 /*
2 newsstar - advanced news fetcher
3 Copyright (C) 2001-2002 Tony Houghton <h@realh.co.uk>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /* Score/killfile */
21
22
23 #include "common.h"
24
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include "parserc.h"
29 #include "score.h"
30
31 /* Opens score file for reading, returning fd (-1 if doesn't exist) */
score_open_file(const char * server_name)32 static int score_open_file(const char *server_name)
33 {
34 int fd;
35 char *filename = XSTRDUPCAT(CONF_DIR "/score.", server_name);
36
37 deblog_info(_("Opening scorefile %s\n"), filename);
38 fd = open(filename, O_RDONLY);
39 if (fd == -1)
40 {
41 deblog_info(_("Scorefile %s doesn't exist\n"), filename);
42 }
43 free(filename);
44 return fd;
45 }
46
47 #ifdef HAVE_REGEX_H
48
49 #include <regex.h>
50
51 struct score_t {
52 regex_t regex;
53 long int score; /* 0 marks end of array */
54 };
55
56 /* If score_master_present is TRUE but score_master_array is NULL, this means
57 * we haven't checked yet */
58 static score_t *score_master_array = NULL;
59 static BOOL score_master_present = TRUE;
60
score_need_to_load_master(void)61 inline static BOOL score_need_to_load_master(void)
62 {
63 return score_master_present && !score_master_array;
64 }
65
66 /* Scores array has to hold one more entry than loaded with a score of 0 to
67 * represent end of array */
68 typedef struct {
69 score_t *scores;
70 size_t scores_size;
71 size_t nscores;
72 } score_parserc_handle;
73
74 /* keyword is actually score represented as a string; value is regex; handle is
75 * score_parserc_handle */
score_parserc_handler(const char * keyword,const char * value,void * handle)76 static const char *score_parserc_handler(const char *keyword, const char
77 *value, void *handle)
78 {
79 score_parserc_handle *shandle = (score_parserc_handle * ) handle;
80 score_t *score;
81 regex_t reg;
82 int errcode;
83 long int scoreval = atol(keyword);
84
85 if (!scoreval)
86 return _("0 score disallowed");
87 if ((errcode = regcomp(®, value, REG_EXTENDED | REG_NEWLINE | REG_NOSUB
88 | (maincf_get_regex_ic() ? REG_ICASE : 0))) != 0)
89 {
90 static char score_regexerr[256];
91
92 regerror(errcode, ®, score_regexerr, 256);
93 return score_regexerr;
94 }
95 if (shandle->nscores + 2 > shandle->scores_size)
96 {
97 shandle->scores = XREALLOC(shandle->scores, (shandle->scores_size *= 2)
98 * sizeof(score_t));
99 }
100 deblog_debug(_(" Rule %d scores %ld for %s\n"), shandle->nscores,
101 scoreval, value);
102 score = shandle->scores + shandle->nscores;
103 score->score = scoreval;
104 score->regex = reg;
105 score = shandle->scores + ++shandle->nscores;
106 score->score = 0;
107 deblog_debug("Added\n");
108 return NULL;
109 }
110
111 /* NULL server_name means load master.score if not already loaded */
score_load(const char * server_name)112 score_t *score_load(const char *server_name)
113 {
114 score_parserc_handle handle;
115 int fd = -1;
116
117 if (!server_name)
118 {
119 if (score_need_to_load_master())
120 {
121 deblog_info(_("Opening scorefile " CONF_DIR "/master.score\n"));
122 fd = open(CONF_DIR "/master.score", O_RDONLY);
123 if (fd == -1)
124 {
125 score_master_present = FALSE;
126 deblog_info(_("master.score doesn't exist\n"));
127 }
128 }
129 else
130 {
131 return NULL;
132 }
133 }
134 else
135 {
136 if (score_need_to_load_master())
137 {
138 score_load(NULL);
139 }
140 fd = score_open_file(server_name);
141 }
142
143 if (fd == -1)
144 return NULL;
145 handle.scores = XMALLOC(sizeof(score_t));
146 handle.scores_size = 1;
147 handle.nscores = 0;
148 handle.scores[0].score = 0;
149 parserc_register_master_handler(score_parserc_handler, &handle);
150 if (server_name)
151 {
152 deblog_info(_("Reading scorefile for %s\n"), server_name);
153 }
154 else
155 {
156 deblog_info(_("Reading master.score\n"));
157 }
158 parserc_read(fd);
159 if (!handle.nscores)
160 {
161 free(handle.scores);
162 handle.scores = NULL;
163 }
164 if (!server_name)
165 {
166 score_master_array = handle.scores;
167 score_master_present = score_master_array != NULL;
168 }
169 return handle.scores;
170 }
171
172 /* NULL scores arg means use master */
score_calculate(const score_t * scores,const char * headers)173 long int score_calculate(const score_t *scores, const char *headers)
174 {
175 size_t n;
176 long int score = 0;
177
178 if (scores)
179 {
180 if (score_master_present)
181 {
182 deblog_debug(_("Checking headers against master.score\n"));
183 score = score_calculate(NULL, headers);
184 }
185 }
186 else
187 {
188 scores = score_master_array;
189 }
190
191 for (n = 0; scores[n].score; ++n)
192 {
193 if (!regexec(&scores[n].regex, headers, 0, NULL, 0))
194 {
195 score += scores[n].score;
196 deblog_debug(_(" Expression %d matches, new score %ld\n"), n,
197 score);
198 }
199 }
200 return score;
201 }
202
score_have_master(void)203 BOOL score_have_master(void)
204 {
205 if (score_need_to_load_master())
206 score_load(NULL);
207 return score_master_present;
208 }
209
210 #else /* HAVE_REGEX_H */
211
212 struct score_t {
213 long int score;
214 };
215
score_load(const char * server_name)216 score_t *score_load(const char *server_name)
217 {
218 int fd = score_open_file(server_name);
219
220 if (fd != -1)
221 {
222 close(fd);
223 deblog_warning(_("%s has a scorefile, but scoring isn't supported\n"),
224 server_name);
225 }
226 return NULL;
227 }
228
229 #endif /* HAVE_REGEX_H */
230
231