shithub: riscv

Download patch

ref: 87ab441ab4392eb07cb691668010a5210c917482
parent: 539f74db812b9d0a62db24070d89a8f26a542f19
author: cinap_lenrek <[email protected]>
date: Tue Nov 27 16:10:55 EST 2012

upas: handle Bcc: header

when -8 flag is given, upas/marshal override To:, Cc: and Bcc: headers.
add -B option for Bcc. it is also now valid to pass recipient list
on upas/marshal arguments and have -8 flag set. make nedmail and
/rc/bin/mail call upas/marshal with -8 now to make it effective.

--- a/rc/bin/mail
+++ b/rc/bin/mail
@@ -8,5 +8,5 @@
 case -f* -r* -c* -m*
 	exec upas/nedmail $*
 case *
-	exec upas/marshal $*
+	exec upas/marshal -8 $*
 }
--- a/sys/man/1/marshal
+++ b/sys/man/1/marshal
@@ -10,6 +10,9 @@
 .B -C
 .I copyaddr
 ] [
+.B -B
+.I copyaddr
+] [
 .B -Fr#xn
 ] [
 .B -p[es]
@@ -86,8 +89,12 @@
 (if it can) when the mail message is read.
 .TP
 .BI -C copyaddr
+or
+.BI -B copyaddr
 adds a
 .B Cc:
+or
+.B Bcc:
 header with
 .I copyaddr
 and also adds
--- a/sys/src/cmd/upas/marshal/marshal.c
+++ b/sys/src/cmd/upas/marshal/marshal.c
@@ -122,10 +122,10 @@
 int	printsubject(Biobuf*, char*);
 int	printto(Biobuf*, Addr*);
 Alias*	readaliases(void);
-int	readheaders(Biobuf*, int*, String**, Addr**, int);
+int	readheaders(Biobuf*, int*, String**, Addr**, Addr**, Addr**, int);
 void	readmimetypes(void);
 int	rfc2047fmt(Fmt*);
-int	sendmail(Addr*, Addr*, int*, char*);
+int	sendmail(Addr*, Addr*, Addr*, int*, char*);
 char*	waitforsubprocs(void);
 
 int rflag, lbflag, xflag, holding, nflag, Fflag, eightflag, dflag;
@@ -188,10 +188,10 @@
 void
 main(int argc, char **argv)
 {
-	int ccargc, flags, fd, noinput, headersrv;
+	int ccargc, bccargc, flags, fd, noinput, headersrv;
 	char *subject, *type, *boundary;
-	char *ccargv[32];
-	Addr *cc, *to;
+	char *ccargv[32], *bccargv[32];
+	Addr *to, *cc, *bcc;
 	Attach *first, **l, *a;
 	Biobuf in, out, *b;
 	String *file, *hdrstring;
@@ -202,7 +202,7 @@
 	l = &first;
 	type = nil;
 	hdrstring = nil;
-	ccargc = 0;
+	ccargc = bccargc = 0;
 
 	quotefmtinstall();
 	fmtinstall('Z', doublequote);
@@ -227,6 +227,11 @@
 			sysfatal("too many cc's");
 		ccargv[ccargc++] = EARGF(usage());
 		break;
+	case 'B':
+		if(bccargc >= nelem(bccargv)-1)
+			sysfatal("too many bcc's");
+		bccargv[bccargc++] = EARGF(usage());
+		break;
 	case 'd':
 		dflag = 1;		/* for sendmail */
 		break;
@@ -278,21 +283,21 @@
 
 	if(nflag && eightflag)
 		sysfatal("can't use both -n and -8");
-	if(eightflag && argc >= 1)
+	if(!eightflag && argc < 1)
 		usage();
-	else if(!eightflag && argc < 1)
-		usage();
 
 	aliases = readaliases();
-	if(!eightflag){
+	to = cc = bcc = nil;
+	if(argc > 0)
 		to = expand(argc, argv);
+	if(ccargc > 0)
 		cc = expand(ccargc, ccargv);
-	} else
-		to = cc = nil;
+	if(bccargc > 0)
+		bcc = expand(bccargc, bccargv);
 
 	flags = 0;
 	headersrv = Nomessage;
-	if(!nflag && !xflag && !lbflag &&!dflag) {
+	if(!nflag && !xflag && !lbflag && !dflag) {
 		/*
 		 * pass through headers, keeping track of which we've seen,
 		 * perhaps building to list.
@@ -299,7 +304,7 @@
 		 */
 		holding = holdon();
 		headersrv = readheaders(&in, &flags, &hdrstring,
-			eightflag? &to: nil, 1);
+			eightflag? &to: nil, eightflag? &cc: nil, eightflag? &bcc: nil, 1);
 		if(rfc822syntaxerror){
 			Bdrain(&in);
 			fatal("rfc822 syntax error, message not sent");
@@ -321,7 +326,7 @@
 		}
 	}
 
-	fd = sendmail(to, cc, &pid, Fflag ? argv[0] : nil);
+	fd = sendmail(to, cc, bcc, &pid, Fflag ? argv[0] : nil);
 	if(fd < 0)
 		sysfatal("execing sendmail: %r\n:");
 	if(xflag || lbflag || dflag){
@@ -340,7 +345,7 @@
 	mboxpath("headers", user, file, 0);
 	b = Bopen(s_to_c(file), OREAD);
 	if(b != nil){
-		if (readheaders(b, &flags, &hdrstring, nil, 0) == Error)
+		if (readheaders(b, &flags, &hdrstring, nil, nil, nil, 0) == Error)
 			fatal("reading");
 		Bterm(b);
 		bwritesfree(&out, &hdrstring);
@@ -440,16 +445,16 @@
  * remove Bcc: line.
  */
 int
-readheaders(Biobuf *in, int *fp, String **sp, Addr **top, int strict)
+readheaders(Biobuf *in, int *fp, String **sp, Addr **top, Addr **ccp, Addr **bccp, int strict)
 {
 	int i, seen, hdrtype;
 	char *p;
-	Addr *to;
+	Addr *to, *cc, *bcc;
 	String *s, *sline;
 
 	s = s_new();
+	to = cc = bcc = nil;
 	sline = nil;
-	to = nil;
 	hdrtype = -1;
 	seen = 0;
 	for(;;) {
@@ -468,22 +473,28 @@
 
 		/* process the current header, it's all been read */
 		if(sline) {
-			assert(hdrtype != -1);
-			if(top){
-				switch(hdrtype){
-				case Hto:
-				case Hcc:
-				case Hbcc:
+			switch(hdrtype){
+			default:
+			Addhdr:
+				s_append(s, s_to_c(sline));
+				s_append(s, "\n");
+				break;
+			case Hto:
+				if(top)
 					to = expandline(&sline, to);
-					break;
-				}
-			}
-			if(hdrtype == Hsubject){
+				goto Addhdr;
+			case Hcc:
+				if(ccp)
+					cc = expandline(&sline, cc);
+				goto Addhdr;
+			case Hbcc:
+				if(bccp)
+					bcc = expandline(&sline, bcc);
+				break;
+			case Hsubject:
 				s_append(s, mksubject(s_to_c(sline)));
 				s_append(s, "\n");
-			}else if(top==nil || hdrtype!=Hbcc){
-				s_append(s, s_to_c(sline));
-				s_append(s, "\n");
+				break;
 			}
 			s_free(sline);
 			sline = nil;
@@ -530,9 +541,25 @@
 	}
 
 	*sp = s;
-	if(top)
+
+	if(to){
+		freeaddrs(*top);
 		*top = to;
+	}else
+		freeaddrs(to);
 
+	if(cc){
+		freeaddrs(*ccp);
+		*ccp = cc;
+	}else
+		freeaddrs(cc);
+
+	if(bcc){
+		freeaddrs(*bccp);
+		*bccp = bcc;
+	}else
+		freeaddrs(bcc);
+
 	if(seen == 0){
 		if(Blinelen(in) == 0)
 			return Nomessage;
@@ -760,11 +787,13 @@
 }
 
 int
-printto(Biobuf *b, Addr *a)
+printaddr(Biobuf *b, char *s, Addr *a)
 {
 	int i;
 
-	if(Bprint(b, "To: %s", a->v) < 0)
+	if(a == nil)
+		return 0;
+	if(Bprint(b, "%s %s", s, a->v) < 0)
 		return -1;
 	i = 0;
 	for(a = a->next; a != nil; a = a->next)
@@ -776,21 +805,15 @@
 }
 
 int
-printcc(Biobuf *b, Addr *a)
+printto(Biobuf *b, Addr *a)
 {
-	int i;
+	return printaddr(b, "To:", a);
+}
 
-	if(a == nil)
-		return 0;
-	if(Bprint(b, "CC: %s", a->v) < 0)
-		return -1;
-	i = 0;
-	for(a = a->next; a != nil; a = a->next)
-		if(Bprint(b, "%s%s", ((i++ & 7) == 7)?",\n\t":", ", a->v) < 0)
-			return -1;
-	if(Bprint(b, "\n") < 0)
-		return -1;
-	return 0;
+int
+printcc(Biobuf *b, Addr *a)
+{
+	return printaddr(b, "Cc:", a);
 }
 
 int
@@ -1032,7 +1055,7 @@
 
 /* start up sendmail and return an fd to talk to it with */
 int
-sendmail(Addr *to, Addr *cc, int *pid, char *rcvr)
+sendmail(Addr *to, Addr *cc, Addr *bcc, int *pid, char *rcvr)
 {
 	int ac, fd;
 	int pfd[2];
@@ -1049,6 +1072,8 @@
 		ac++;
 	for(a = cc; a != nil; a = a->next)
 		ac++;
+	for(a = bcc; a != nil; a = a->next)
+		ac++;
 	v = av = emalloc(sizeof(char*)*(ac+20));
 	ac = 0;
 	v[ac++] = "sendmail";
@@ -1063,6 +1088,8 @@
 	for(a = to; a != nil; a = a->next)
 		v[ac++] = a->v;
 	for(a = cc; a != nil; a = a->next)
+		v[ac++] = a->v;
+	for(a = bcc; a != nil; a = a->next)
 		v[ac++] = a->v;
 	v[ac] = 0;
 
--- a/sys/src/cmd/upas/ned/nedmail.c
+++ b/sys/src/cmd/upas/ned/nedmail.c
@@ -1819,6 +1819,7 @@
 		return nil;
 	}
 
+	av[ai++] = "-8";
 	for(nm = m; nm != &top; nm = nm->parent){
 		if(*nm->subject){
 			av[ai++] = "-s";
@@ -1862,8 +1863,8 @@
 Message*
 mcmd(Cmd *c, Message *m)
 {
-	char **av;
-	int i, ai;
+	char *av[128];
+	int i, ai = 1;
 	String *path;
 
 	if(m == &top){
@@ -1876,8 +1877,7 @@
 		return nil;
 	}
 
-	ai = 1;
-	av = malloc(sizeof(char*)*(c->an + 8));
+	av[ai++] = "-8";
 
 	av[ai++] = "-t";
 	if(m->parent == &top)
@@ -1892,7 +1892,7 @@
 	if(strchr(c->av[0], 'M') == nil)
 		av[ai++] = "-n";
 
-	for(i = 1; i < c->an; i++)
+	for(i = 1; i < c->an && ai < nelem(av)-1; i++)
 		av[ai++] = c->av[i];
 	av[ai] = 0;
 
@@ -1900,7 +1900,6 @@
 		m = nil;
 	if(path != nil)
 		s_free(path);
-	free(av);
 	return m;
 }
 
@@ -1916,6 +1915,8 @@
 		Bprint(&out, "!address\n");
 		return nil;
 	}
+
+	av[ai++] = "-8";
 
 	if(*m->subject){
 		av[ai++] = "-s";