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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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
|
/* filter.c -- external kill/score processing
*/
/* This software is copyrighted as detailed in the LICENSE file. */
#include "EXTERN.h"
#include "common.h"
#ifdef USE_FILTER
#include "ngdata.h"
#include "util2.h"
#include "hash.h"
#include "cache.h"
#include "list.h"
#include "env.h"
#include "trn.h"
#include "final.h"
#include "rt-ov.h"
#include "nntpclient.h"
#include "datasrc.h"
#include "INTERN.h"
#include "filter.h"
#include "filter.ih"
static FILE* filt_wr;
static FILE* filt_rd;
static int pipe_1[2], pipe_2[2];
static int filter_restarting;
static int need_to_filter = 0;
static int want_filter = 1; /* consider making an option later */
static pid_t filter_pid = 0;
#ifdef FILTER_DEBUG
extern FILE* filter_error_file;
#endif
void
filter_init()
{
filt_wr = filt_rd = NULL;
pipe_1[0] = pipe_1[1] = pipe_2[0] = pipe_2[1] = -1;
filter_restarting = 0;
}
static void
filter_restart()
{
char* filter; /* external filter file */
struct stat f_inode; /* stat buf */
static int recursing = 0;
/* prevent unbounded recursion (e.g. filter_restart -> filter_nginit ->
filter_send -> filter_restart) */
if (recursing)
return;
recursing = 1;
filter = filexp(getval("FILTER", FILTERPROG));
want_filter = (strNE(filter,"NOFILTER") && stat(filter, &f_inode) == 0);
if (!want_filter) {
recursing = 0;
return;
}
if (!(f_inode.st_mode & S_IXUSR)) {
perror("opening filter");
recursing = 0;
return;
}
if (verbose) {
printf("\nStarting filter `%s'", filter);
fflush(stdout); /* print it *now* */
}
if (pipe(pipe_1) < 0)
perror("creating output pipe");
if (pipe(pipe_2) < 0)
perror("creating input pipe");
if ((filter_pid = fork()) < 0)
perror("filter_restart");
else if (filter_pid > 0) {
if ((filt_wr = fdopen(pipe_1[1], "w")) == NULL) {
perror("configuring output pipe");
filter_cleanup();
return;
}
if ((filt_rd = fdopen(pipe_2[0], "r")) == NULL) {
perror("configuring input pipe");
filter_cleanup();
return;
}
setvbuf(filt_wr, NULL, _IOLBF, BUFSIZ); /* line buffering */
setvbuf(filt_rd, NULL, _IOLBF, BUFSIZ);
close(pipe_1[0]);
close(pipe_2[1]);
}
else {
close(STDIN_FILENO);
close(STDOUT_FILENO);
sigignore(SIGINT);
if (dup2(pipe_1[0], STDIN_FILENO) < 0) {
perror("reopening child input stream");
finalize(1);
}
if (dup2(pipe_2[1], STDOUT_FILENO) < 0) {
perror("reopening child output stream");
finalize(1);
}
close(pipe_1[1]);
close(pipe_2[0]);
if (execl(filter, filter, NULL) < 0) {
perror("switching to filter process");
finalize(1);
}
}
filter_nginit();
recursing = 0;
}
void
filter_nginit()
{
static int recursing = 0;
char buf[1024];
if (recursing)
return;
need_to_filter = 0;
if (!want_filter)
return;
recursing = 1;
sprintf(buf, "newsgroup %s\n", ngname);
if (filter_send(buf) == 0) {
if (filter_recv(buf) != NULL) {
Uchar* fieldflags = datasrc->fieldflags;
int i;
if (strncaseEQ(buf, "overview", 8)) {
for (i = 1; i < OV_MAX_FIELDS; i++) {
if (fieldflags[i] & (FF_HAS_FIELD | FF_CHECK4FIELD))
fieldflags[i] |= FF_FILTERSEND;
}
need_to_filter = 1;
}
else if (strncaseEQ(buf, "want", 4)) {
char* s = buf + strlen(buf) - 1;
if (*s == '\n')
*s = '\0';
s = buf + 4;
for (i = 1; i < OV_MAX_FIELDS; i++)
fieldflags[i] &= ~FF_FILTERSEND;
for (;;) {
while (*s == ' ') s++;
if (!*s)
break;
s = cpytill(buf,s,' ');
i = ov_num(buf,(char*)NULL);
if (i) {
fieldflags[i] |= FF_FILTERSEND;
need_to_filter = 1;
}
}
}
}
}
recursing = 0;
}
int
filter(a)
ART_NUM a;
{
char buf[256];
long score = 0, sc = 0;
ARTICLE* ap;
ART_NUM i;
char* s;
if (!need_to_filter)
return 0;
ap = article_find(a);
if (ap && ap->refs) {
Uchar* fieldflags = datasrc->fieldflags;
sprintf(buf, "art %ld", ap->num);
if (filter_send(buf) < 0)
return 0;
for (i = 1; i < OV_MAX_FIELDS; i++) {
if (filter_send("\t") < 0)
return 0;
if (fieldflags[i] & FF_FILTERSEND) {
if (fieldflags[i] & FF_HAS_HDR) {
if (filter_send(ov_fieldname(i)) < 0
|| filter_send(": ") < 0)
return 0;
}
if ((s = ov_field(ap, i)) != NULL) {
if (filter_send(s) < 0)
return 0;
}
}
}
if (filter_send("\n") < 0 || filter_send("scores\n") < 0)
return 0;
while (filter_recv(buf) != NULL && strncaseNE(buf, "done", 4)) {
sscanf(buf, "%ld %ld", &i, &sc);
if (i == a)
score = sc;
#ifdef FILTER_DEBUG
else {
fprintf(filter_error_file, "article %d: filter returned %s",
a, buf);
}
#endif
}
}
return score;
}
static int
filter_send(cmd)
char* cmd;
{
if (filt_wr == NULL || fputs(cmd, filt_wr) == EOF) {
filter_restart();
if (filt_wr == NULL) {
#ifdef FILTER_DEBUG
fprintf (stderr, "filt_wr is still NULL\n");
#endif
return -1;
}
else if (fputs (cmd, filt_wr) == EOF) {
fprintf(stderr, "Could not restart filter process.\n");
return -1;
}
}
return 0;
}
static char*
filter_recv(buf)
char* buf;
{
if (filt_rd == NULL)
return NULL;
return (fgets(buf, 256, filt_rd));
}
void
filter_cleanup()
{
if (filter_pid > 0) {
kill(filter_pid, SIGTERM);
filter_pid = 0;
}
if (filt_wr != NULL) {
fputs("bye\n", filt_wr);
fclose(filt_wr);
filt_wr = NULL;
}
if (filt_rd != NULL) {
fclose(filt_rd);
filt_rd = NULL;
}
pipe_1[0] = pipe_1[1] = 0;
pipe_2[0] = pipe_2[1] = 0;
}
#endif /* USE_FILTER */
|