Plan 9 from Bell Labs’s /usr/web/sources/contrib/quanstro/root/sys/src/cmd/upas/send/filter.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "common.h"
#include "send.h"
#include <regexp.h>

Biobuf	bin;
int	flagn;
int	flagx;
int	rmail;
int	tflg;
char	*subjectarg;

char*
findbody(char *p)
{
	if(*p == '\n')
		return p;

	while(*p){
		if(*p == '\n' && *(p+1) == '\n')
			return p+1;
		p++;
	}
	return p;
}

int
refuse(dest*, message *, char *cp, int, int)
{
	fprint(2, "%s", cp);
	exits("error");
	return 0;
}

void
usage(void)
{
	fprint(2, "usage: upas/filter [-bh] rcvr mailbox [regexp file] ...\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	char *cp, file[Pathlen];
	int i, header, body;
	message *mp;
	dest *dp;
	Reprog *p;
	Resub match[10];

	header = body = 0;
	ARGBEGIN {
	case 'h':
		header = 1;
		break;
	case 'b':
		header = 1;
		body = 1;
		break;
	default:
		usage();
	} ARGEND

	Binit(&bin, 0, OREAD);
	if(argc < 2)
		usage();
	mp = m_read(&bin, 1, 0);

	/* get rid of local system name */
	cp = strchr(s_to_c(mp->sender), '!');
	if(cp){
		cp++;
		mp->sender = s_copy(cp);
	}

	dp = d_new(s_copy(argv[0]));
	dp->repl1 = s_copy(argv[1]);

	strecpy(file, file+sizeof file, argv[1]);
	cp = findbody(s_to_c(mp->body));
	for(i = 2; i < argc; i += 2){
		p = regcomp(argv[i]);
		if(p == 0)
			continue;
		if(regexec(p, s_to_c(mp->sender), match, 10)){
			regsub(argv[i+1], file, sizeof(file), match, 10);
			break;
		}
		if(header == 0 && body == 0)
			continue;
		if(regexec(p, s_to_c(mp->body), match, 10)){
			if(body == 0 && match[0].sp >= cp)
				continue;
			regsub(argv[i+1], file, sizeof(file), match, 10);
			break;
		}
	}
	if(cat_mail(dp, mp) != 0)
		exits("fail");
	exits("");
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.