aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/upas/filterkit/deliver.c
blob: 3b129317f0743d44b1df140c53ef68ab55f11084 (plain)
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
#include "dat.h"
#include <thread.h>
#include "common.h"

void
usage(void)
{
	fprint(2, "usage: %s recipient fromaddr-file mbox\n", argv0);
	threadexitsall("usage");
}

void
main(int argc, char **argv)
{
	int fd;
	char now[30];
	Addr *a;
	char *deliveredto;
	Mlock *l;
	int bytes;

	ARGBEGIN{
	}ARGEND;

	if(argc != 3)
		usage();

	deliveredto = strrchr(argv[0], '!');
	if(deliveredto == nil)
		deliveredto = argv[0];
	else
		deliveredto++;
	a = readaddrs(argv[1], nil);
	if(a == nil)
		sysfatal("missing from address");

	l = syslock(argv[2]);

	/* append to mbox */
	fd = open(argv[2], OWRITE);
	if(fd < 0)
		sysfatal("opening mailbox: %r");
	seek(fd, 0, 2);
	strncpy(now, ctime(time(0)), sizeof(now));
	now[28] = 0;
	if(fprint(fd, "From %s %s\n", a->val, now) < 0)
		sysfatal("writing mailbox: %r");

	/* copy message handles escapes and any needed new lines */
	bytes = appendfiletombox(0, fd);
	if(bytes < 0)
		sysfatal("writing mailbox: %r");

	close(fd);
	sysunlock(l);

	/* log it */
	syslog(0, "mail", "delivered %s From %s %s (%s) %d", deliveredto,
		a->val, now, argv[0], bytes);
	exits(0);
}