🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #32868

Re: De-spamming a diablo newsfeed?

#32868
From: Richard Hernandez
Date: Wed, 11 Nov 1998 00:00
854 lines
19023 bytes
Jeremy (jeremy@exit109.com) wrote:
: The ideal thing would be for servers like Cyclone and Diablo to have
: an embedded Perl.  The next best thing would be a little C program that
: runs on the external filter interface, embeds Perl, and implements the
: superior INN-style filter interface.  Presumably such a program could
: suck in articles and parse them faster than regular Perl code.
: -- 
: Jeremy  |  jeremy@exit109.com
: "Everything you think you know is wrong."  --Jack Chalker

You asked for it. No instructions, no time. Works for news and mail.
Works on NCR's MPRAS, Solaris Intel, Solaris Sparc, SunOS.
Edited to fit usenet.
***************************************************************************
COPYRIGHT 1998 <Richard "Rick" Hernandez, NCR Corporation>
All rights reserved,

You have a right to use, modify, or distribute this software for any
use provided that you keep this copyright notice with the software.

Use of the software for profit is specifically prohibited with out
the authors consent.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR, OR NCR CORPORATION, BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

rick@pobox.com
***************************************************************************
Makefile
***************************************************************************
# if DNS support were included

# NCR 3.0 or greater
#CFLAGS = -g -DSVR4 -DSYSEXITSH -DUSE_DNS
#LIBS   = -lsocket -lnsl -lresolv
#OBJS   = gparser.o getcannon.o
# SVR4/SOLARIS
#CFLAGS = -g -DSVR4 -DUSE_DNS
#LIBS   = -lsocket -lnsl -lresolv
#OBJS   = gparser.o getcannon.o
# SunOS
#CFLAGS = -g -DSunOS -DUSE_DNS
#LIBS   = -lnsl -lresolv
#OBJS   = gparser.o getcannon.o compat.o

# else

OBJS    = gparser.o
# SVR4 && NCR 3.0 or greater
CFLAGS  = -g SVR4
# SunOS
CFLAGS  = -g -DSunOS

# endif

gparser:	$(OBJS)
	$(CC) -o $@ $(OBJS)

***************************************************************************
gparser.c
***************************************************************************
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>

#undef isspace

#define FROM       1
#define FROM_      2
#define SENDER     3
#define REPLYTO    4
#define XREPLYTO   5
#define RETURNPATH 6
#define MAXDNAME   256
#define MAXHOSTS   20


#ifdef SVR4
#  ifdef USE_DNS
extern char *findhostbyip(char *);
#  endif
void listadd(char *);
#else
#  ifdef SunOS
#    ifdef USE_DNS
extern char *findhostbyip();
#    endif
void listadd();
#  endif
#endif

static char mime[8];
static char lastbyhost[80];
FILE *tfp, *tfp2;

int ckDNS = 0;
char list[MAXHOSTS][MAXDNAME];

void
trim(string)
char *string;
{
int i;
char *p, *b;

if (isspace(*string)) {
	p=string;
	b=string;
	while (isspace(*p)) p++;
	for (;*p;p++,b++) *b = *p;
	*b = '\0';
}

for (i=strlen(string),i--;i >= 0 && isspace(*(string+i));i--)
	*(string+i)='\0';

return;
}

void
lowercase(string)
char *string;
{
for (;*string;string++)
	if (isalpha(*string) && isupper(*string)) *string = tolower(*string);
}

int
badIP(host)
char *host;
{
int a,b,c,d;
char *p, *q;
char tmphost[18];

	if (strlen(host) > 17) return(1);
	if (!strcmp(host, "[127.0.0.1]")) return(1);
	if (!strcmp(host, "[0.0.0.0]")) return(1);

	/* Drop []'s */
	(void) strcpy(tmphost, host+1);
	if (!(p = strchr(tmphost, ']'))) return(1);
	*p = '\0';

	/* quad one */
	if (!(q = strchr(tmphost, '.'))) return(1);
	*q = '\0';
	q++;
	a = atoi(tmphost);

	/* quad two */
	if (!(p = strchr(q, '.'))) return(1);
	*p = '\0';
	p++;
	b = atoi(q);
	q = p;

	/* quad three */
	if (!(p = strchr(q, '.'))) return(1);
	*p = '\0';
	p++;
	c = atoi(q);
	q = p;

	/* quad four */
	d = atoi(q);

	/* IANA reserved experimental, private intranets,
		broadcast address, etc. */
	if (a == 10) return(1);
	if (a == 172 && b >= 16 && b <= 31) return(1);
	if (a == 192 && b == 168) return(1);
	if (a >= 224) return(1); /* Not a possible email address */

return(0);
}

void
test4IP(host)
char *host;
{
int i, cnt;
char newname[18];

	if (host[0] != '[') {
		newname[0] = '[';
		for (i = 1; *(host + i - 1) && i < 17; i++) {
			newname[i] = *(host + i - 1);
			switch(*(host + i - 1)) {
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				cnt++;
				break;
			case '.':
				if (cnt > 0 && cnt < 4) {
					cnt = 0;
				} else { /* not an IP address */
					return;
				}
				break;
			default:
				return; /* not an IP address */
			}
		}
		newname[i++] = ']';	
		newname[i] = '\0';	
		(void) strcpy(host, newname);
	} else if (!strchr(host, ']')) return;
}

char *
ckHost(host)
char *host;
{
int rc, stat, len;
char *name, *p;
static char hostname[BUFSIZ];
	
	if (strchr(host, '@')) return(NULL);
	if (!strchr(host, '.')) return(NULL);
	if (strlen(host) < 4) return(NULL);

		/* Need to have space for larger name */
	(void) strcpy(hostname, host);
	host = hostname;

	test4IP(hostname);
	if (hostname[0] == '[') {
		if (rc = badIP(hostname)) {
			return(NULL);
		}
	}
	if (!ckDNS) {
		lowercase(hostname);
		return(host);
	}

#ifdef USE_DNS

	if (host[0] == '[') {
		if ((p = strchr(host, ']')) == NULL) {
			(void) fprintf(stderr, "%s: unbalanced '['\n", host);
			return(NULL);
		}
		*p = '\0';
		host++;

		if ((name = findhostbyip(host)) == NULL) return(NULL);

		if (!strchr(name, '.')) {
			(void) strcpy(hostname, name); /* need space */
			rc=dns_getcanonname(hostname,
				sizeof(hostname),1,&stat);
			if (rc) {
				lowercase(hostname);
				len = strlen(hostname);
				if (hostname[len-1] == '.')
					hostname[len - 1] = '\0';
				return(hostname);
			} else {
				return(NULL);
			}
		}
		lowercase(name);
		len = strlen(name);
		if (name[len-1] == '.') name[len - 1] = '\0';
		return(name);

	} else {
		rc = dns_getcanonname(host, sizeof(hostname), 0, &stat);
		if (rc) {
			lowercase(host);
			len = strlen(host);
			if (host[len-1] == '.') host[len - 1] = '\0';
			return(host);
		} else {
			return(NULL);
		}
	}

#endif

}

int
parse_path(line)
char *line;
{
char *a, *p;

	trim(line);
	while ((p = strchr(line, '!')) != NULL) {
		*p = '\0';
		(void) fprintf(tfp2, "path:\t%s\n", line);
		p++;
		line = p;
	}
	(void) fprintf(tfp2, "path:\t%s\n", line);

return(0);
}

int
parse_rcvd(rl)
char *rl;
{
int l, r;
char *p, *b, *e;
char *valid_host;
char byhost[256], fromhost[256], parenhost[256];
char leftparenhost[256], rightparenhost[256];

	byhost[0] = '\0';
	fromhost[0] = '\0';
	parenhost[0] = '\0';
	leftparenhost[0] = '\0';
	rightparenhost[0] = '\0';

	/* get last received by host bogus or not */
	if ((b = strstr(rl, "by ")) == NULL) return(1);
	b+=3;
	if ((e = strchr(b, ' ')) != NULL) {
		if (e-b > sizeof(lastbyhost)) {
			(void) strncpy(lastbyhost, b, sizeof(lastbyhost));
			lastbyhost[sizeof(lastbyhost)-1] = '\0';
		} else {
			(void) strncpy(lastbyhost, b, e-b);
			lastbyhost[e-b] = '\0';
		}
		lowercase(lastbyhost);
	} else return(1);

	/* Get received by host */
	if ((b=strstr(rl, " by ")) == NULL) return(1);
	*b = '\0'; b+=4;
	while (isspace(*b)) b++;
	e = strchr(b, ' ');
	*e = '\0';
	if (valid_host = ckHost(b)) {
		(void) strcpy(byhost, valid_host);
	} else return(1);

	/*
	** if we don't want to bother with forged addresses
	** throw in a return or something.
	*/

	p = rl;
	if (!strncmp(p, "from ", 5)) { /* from_ is the first word */
		b = strchr(p, ' ');
		while (isspace(*b)) b++;
		if ((p = strchr(b, ' ')) != NULL) {
			*p = '\0'; p++;
			while (isspace(*p)) p++;
		}
		if (valid_host = ckHost(b))
			(void) strcpy(fromhost, valid_host);
	}

		/* handles ([9.9.9.9]) case */
	if (p != NULL && *p == '(') {
		p++;
			/* unbalanced parens */
		if ((e = strchr(p, ')')) == NULL) return(1);
		*e = '\0';
		if (e = strchr(p, ' ')) {
				/* if more than 2 words in the ()'s */
			*e = '\0'; e++;
			if (valid_host = ckHost(p)) {
				(void) strcpy(leftparenhost, valid_host);
			}
			if (b = strrchr(e, ' ')) {
				b++;
				if (valid_host = ckHost(b)) {
					(void)strcpy(rightparenhost,
						valid_host);
				}
			} else if (valid_host = ckHost(e)) {
				(void) strcpy(rightparenhost, valid_host);
			}
		} else if (valid_host = ckHost(p)) {
			/* only one word in the ()'s */
			(void) strcpy(parenhost, valid_host);
		}
	}
		/* output */
	(void) fprintf(tfp2, "received:\t%s", byhost);
	if (ckDNS) listadd(byhost);
	if (strlen(parenhost)) {
		(void) fprintf(tfp2, " (%s)", parenhost);
		if (ckDNS) listadd(parenhost);
	} else if ((l=strlen(leftparenhost)) || (r=strlen(rightparenhost))) {
		if (strcmp(leftparenhost, rightparenhost)) {
			if (l && r) {
				(void) fprintf(tfp2,
					" (%s %s)",
					leftparenhost, rightparenhost);
				if (ckDNS) listadd(leftparenhost);
				if (ckDNS) listadd(rightparenhost);
			} else {
				if (l) {
					(void) fprintf(tfp2,
						" (%s)", leftparenhost);
					if (ckDNS) listadd(rightparenhost);
				} else {
					(void) fprintf(tfp2,
						" (%s)", rightparenhost);
					if (ckDNS) listadd(rightparenhost);
				}
			}
		} else {
			(void) fprintf(tfp2, " (%s)", leftparenhost);
			if (ckDNS) listadd(leftparenhost);
		}
	}
	if (strlen(fromhost)) {
		(void) fprintf(tfp2, " %s", fromhost);
		if (ckDNS) listadd(fromhost);
	}
	(void) fprintf(tfp2, "\n");

return(0);
}

void
rfcjoin()
{
int first = 1;
char linebuf[BUFSIZ], *line;

	linebuf[0] = '\0';
	line = linebuf;
	while (gets(line)) {
		if (!strlen(line)) return;
		if (isspace(line[0])) {
			while(isspace(*line)) line++;
			(void) fprintf(tfp, " ");
			(void) fprintf(tfp, line);
		} else {
			if (first) {
				first=0;
			} else {
				(void) fprintf(tfp, "\n");
			}
			(void) fprintf(tfp, line);
		}
	}
	(void) fprintf(tfp, "\n");

return;
}

char *
findaddr(line)
char *line;
{
int i,j;
int openparen=0;
char *p, *b;
char *buf;

		/* formats:
		**    "Last, First" <user@host.domain>,
		**    <user@host.domain> (Last, First),
		**    user@host.domain (Last, First),
		**    user,
		**    user@host,
		**    user@host.do.main
		*/

	buf = line;
	if ((p=strchr(buf, '<')) != NULL) {
		p++;
		b=p;
		if ((p=strchr(b, '>')) != NULL) {
			*p = '\0';
			if (strchr(b, ' ') || strchr(b, '\t')) return(NULL);
			lowercase(b);
			return(b);
		}
	}
	if ((p=strstr(buf, "[SMTP:")) != NULL) {
		p+=6;
		b=p;
		if ((p=strchr(b, ']')) != NULL) {
			*p = '\0';
			if (strchr(b, ' ') || strchr(b, '\t')) return(NULL);
			lowercase(b);
			return(b);
		}
	}
	if ((p=strchr(buf, '(')) != NULL) {
		for (i=0,j=0; i<strlen(buf); i++) {
			if (buf[i] == '(') {
				openparen=1;
				continue;
			}
			if (openparen) {
				if (buf[i] == ')') openparen=0;
				continue;
			}
			buf[j++] = buf[i];
		}
		buf[j] = '\0';
	}
	if (strchr(buf, '(') || strchr(buf, ')')) {
		(void) fprintf(stderr,
			"Unbalanced or too many '()': %s\n", line);
		return(NULL);
	}
	if ((p=strchr(buf, '"')) != NULL) {
		for (i=0,j=0; i<strlen(buf); i++) {
			if (buf[i] != '"') buf[j++] = buf[i];
		}
		buf[j] = '\0';
	}
	trim(buf);
	if (strchr(buf, ' ') || strchr(buf, '\t')) return(NULL);

lowercase(buf);
return(buf);
}

int
parse_addr(key, line)
int key;
char *line;
{
int n;
char *p, *from_;

	switch(key) {
	case FROM_:
		from_ = strtok(line, " ");
		for (n = 0; p = strtok(NULL, " "); n++);
		if (n != 5) {
			(void) fprintf(stderr,
				"Bad 'From ' line\n%s\n", line);
			return(1);
		}
		lowercase(from_);
		(void) fprintf(tfp2, "from\t%s\n", from_);
		break;
	case FROM:
		if ((p=findaddr(line)) == NULL) {
			return(1);
		}
		(void) fprintf(tfp2, "from:\t%s\n", p);
		break;
	case XREPLYTO:
		if ((p=findaddr(line)) == NULL) {
			return(1);
		}
		(void) fprintf(tfp2, "x-reply-to:\t%s\n", p);
		break;
	case REPLYTO:
		if ((p=findaddr(line)) == NULL) {
			return(1);
		}
		(void) fprintf(tfp2, "reply-to:\t%s\n", p);
		break;
	case SENDER:
		if ((p=findaddr(line)) == NULL) {
			return(1);
		}
		(void) fprintf(tfp2, "sender:\t%s\n", p);
		break;
	case RETURNPATH:
		if ((p=findaddr(line)) == NULL) {
			return(1);
		}
		(void) fprintf(tfp2, "return-path:\t%s\n", p);
		break;
	default:
		(void) fprintf(tfp2,
			"Huh? what happened, how come I got here?\n");
		break;
	}

return(0);
}

int
parse_key(line)
char *line;
{
char *p;

	if (p = strchr(line, '\n')) *p = '\0';
	p=line;
	if ((p = strchr(line, ':')) == NULL) return(1);
	p++;
	if (*p != ' ') return(1);
	*p = '\0';
	p++;

	lowercase(line);
	(void) fprintf(tfp2, "%s\t%s\n", line, p);

return(0);
}

int
parse_to(line)
char *line;
{
char *a, *p;

	while ((p = strchr(line, ',')) != NULL) {
		*p = '\0';
		p++;
		while (isspace(*p)) p++;
		if ((a=findaddr(line)) != NULL)
			(void) fprintf(tfp2, "to:\t%s\n", a);
		line = p;
	}
	trim(line);
	if ((a=findaddr(line)) != NULL)
		(void) fprintf(tfp2, "to:\t%s\n", a);

return(0);
}

int parse_type(line)
char *line;
{
char *p, *a, *b, *e;

	if (e = strchr(line, '\n')) *e = '\0';
	p = line;
	while (b = strchr(p, ';')) {
		*b = '\0'; b++;
		if (*b != '\0') while (isspace(*b)) b++;
		if (a = strchr(p, '=')) {
			*a = '\0'; a++;
			if (*a == '"') {  /* remove parens */
				a++;
				if (e = strchr(a, '"')) *e = '\0';
			}
			lowercase(p);
			(void) fprintf(tfp2, "content-type-%s:\t%s\n", p, a);
		} else {
			(void) fprintf(tfp2, "content-type:\t%s\n", p);
		}
		p = b;
	}
	if (*p != '\0' && strlen(p)) {
		if (a = strchr(p, '=')) {
			*a = '\0'; a++;
			if (*a == '"') {  /* remove parens */
				a++;
				if (e = strchr(a, '"')) *e = '\0';
			}
			lowercase(p);
			(void) fprintf(tfp2, "content-type-%s:\t%s\n", p, a);
		} else {
			(void) fprintf(tfp2, "content-type:\t%s\n", p);
		}
	}

return(0);
}

void
keywords()
{
char line[BUFSIZ];

	rewind(tfp);

	while (fgets(line, BUFSIZ, tfp) != NULL) {
		if (!strncmp("From ", line, 5)) {
			parse_addr(FROM_, line+5);
			continue;
		}
		if (!strncasecmp("From: ", line, 6)) {
			parse_addr(FROM, line+6);
			continue;
		}
		if (!strncasecmp("Sender: ", line, 8)) {
			parse_addr(SENDER, line+8);
			continue;
		}
		if (!strncasecmp("Reply-To: ", line, 10)) {
			parse_addr(REPLYTO, line+10);
			continue;
		}
		if (!strncasecmp("X-Reply-To: ", line, 12)) {
			parse_addr(XREPLYTO, line+12);
			continue;
		}
		if (!strncasecmp("Return-Path: ", line, 13)) {
			parse_addr(RETURNPATH, line+13);
			continue;
		}
		if (!strncasecmp("To: ", line, 4)) {
			parse_to(line+4);
			continue;
		}
		if (!strncasecmp("Cc: ", line, 4)) {
			parse_to(line+4);
			continue;
		}
		if (!strncasecmp("Received: ", line, 10)) {
			parse_rcvd(line+10);
			continue;
		}
		if (!strncasecmp("MIME-Version: ", line, 14)) {
			if (strlen(line+14) < 8) (void) strcpy(mime, line+14);
			else strcpy(mime, "1.0");
			continue;
		}
		if (!strncasecmp("Content-Type: ", line, 14)) {
			parse_type(line+14);
			continue;
		}
		if (!strncasecmp("Path: ", line, 6)) {
			parse_path(line+6);
			continue;
		}
		parse_key(line);
	}
}

int
addok(host)
char *host;
{
int i;

	for (i = 0; i < MAXHOSTS; i++) {
		if (list[i][0] == '\0') { return(i); }
		if (!strcmp(list[i], host)) return(-1);
	}
	/* silent rejection of more than MAXHOSTNAMES */

return(-1);
}

void
listadd(host)
char *host;
{
int rcode, row;
char *b, *p;
char *mxhosts[MAXHOSTS + 1];

	if ((row = addok(host)) >= 0) {
		(void) strcpy(list[row], host);

#ifdef USE_DNS
		if (b = strrchr(host, '.')) { /* find and skip last '.' */
			*b = '\0';
			if (p = strrchr(host, '.')) {
				/*try do.main name search*/
				*b = '.';
				*p = '\0';
	 			if (getmxrr(p+1, mxhosts, &rcode))
					if ((row = addok(p+1)) >= 0)
						(void) strcpy(list[row], p+1);
				if (b = strrchr(host, '.')) {
					/* try do.ma.in name search */
					*p = '.';
	 				if (getmxrr(b+1, mxhosts, &rcode))
						if ((row = addok(b+1)) >= 0)
						(void)strcpy(list[row],b+1);
				}
			}
		}
#endif

	}
}

void
dump()
{
char buf[BUFSIZ];

	/* dump current file contents */
	rewind(tfp2);
	while(fgets(buf, sizeof(buf), tfp2)) {
		fputs(buf, stdout);
		fflush(stdout);
	}
}

void
init()
{
char tmpfile[BUFSIZ];
char tmpfile2[BUFSIZ];
	
	(void) sprintf(tmpfile, "/tmp/Aparser.%d", getpid());
	(void) sprintf(tmpfile2, "/tmp/Bparser.%d", getpid());

	if ((tfp=fopen(tmpfile, "w+")) == NULL) {
		(void) fprintf(stderr, "failed open of %s\n", tmpfile);
		exit(1);
	}
	(void) unlink(tmpfile);

	if ((tfp2=fopen(tmpfile2, "w+")) == NULL) {
		(void) fprintf(stderr, "failed open of %s\n", tmpfile);
		exit(1);
	}
	(void) unlink(tmpfile2);

return;
}
	
int
main(argc, argv)
int argc;
char **argv;
{
int i;

	if (argc == 2) {
#ifdef USE_DNS
		if (!strcasecmp(argv[1], "nodns")) ckDNS = 0;
		else ckDNS = 1;
#else
		ckDNS = 0;
#endif
	}
	init();
	rfcjoin();
	keywords();
	if (strlen(lastbyhost))
		(void) fprintf(tfp2, "lastbyhost:\t%s\n", lastbyhost);
	for (i = 0; i < MAXHOSTS; i++) {
		if (list[i][0] == '\0') break;
		(void) fprintf(tfp2, "MAILPATH: %s\n", list[i]);
	}
	dump();

return(0);
}
***************************************************************************
--
"I'm paid to have opinions, This isn't one of them." Rick Hernandez
email:  rick@pobox.com  web:  http://www.pobox.com/~rick

Technical Details
Message-ID: <72cn2s$91s@rap.SanDiegoCA.NCR.COM>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
References: <brad-1011981913570001@brad.techos.skynet.be> <72aas7$egt@newsops.execpc.com> <87g1bqliwb.fsf@erlenstar.demon.co.uk> <72cjf6$foc$1@east44.supernews.com>

Show Full Headers
Complete Article Headers
Message-ID: <72cn2s$91s@rap.SanDiegoCA.NCR.COM>
Subject: Re: De-spamming a diablo newsfeed?
Date: Wed, 11 Nov 1998 00:00:00 +0000
From: rick@sparc.sandiegoca.ncr.com (Richard Hernandez)
References: <brad-1011981913570001@brad.techos.skynet.be> <72aas7$egt@newsops.execpc.com> <87g1bqliwb.fsf@erlenstar.demon.co.uk> <72cjf6$foc$1@east44.supernews.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
X-Deja-AN: 410814011
Organization: NCR Corporation, San Diego, CA
Reply-To: rick@pobox.com
Newsgroups: news.software.nntp