shithub: Nail

Download patch

ref: 93bcc5ba3883ba0737702558885112ef47b2edf9
parent: 396fe6a9f2f224c384935fec4826542a1c7aa816
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Dec 28 14:09:08 EST 2020

replies: use 'replyto' over 'from' addr, dedup and comma separate

When replying to a message, we were dropping the replyto address
when we should really have been using it. This change parses the
address fields instead of copying them verbating, merges, and
dedups them.

It also comma separates the addresses so marshal won't complain
about invalid formats.

--- a/comp.c
+++ b/comp.c
@@ -182,7 +182,64 @@
 	return f;
 }
 
+int
+strpcmp(void *a, void *b)
+{
+	return strcmp(*(char**)a, *(char**)b);
+}
+
 void
+show(Biobuf *fd, char *type, char **addrs, int naddrs)
+{
+	char *sep;
+	int i, w;
+
+	w = 0;
+	sep = "";
+	if(naddrs == 0)
+		return;
+	qsort(addrs, naddrs, sizeof(char*), strpcmp);
+	Bprint(fd, "%s: ", type);
+	for(i = 0; i < naddrs; i++){
+		if(i > 0 && strcmp(addrs[i-1], addrs[i]) == 0)
+			continue;
+		w += Bprint(fd, "%s%s", sep, addrs[i]);
+		sep = ", ";
+		if(w > 50){
+			w = 0;
+			sep = "";
+			Bprint(fd, "\n%s: ", type);
+		}
+	}
+	Bprint(fd, "\n");
+}
+
+void
+respondto(Biobuf *fd, char *to, Mesg *r, int all)
+{
+	char *rpto, **addrs;
+	int n;
+
+	rpto = to;
+	if(r != nil)
+		rpto = (strlen(r->replyto) > 0) ? r->replyto : r->from;
+	if(r == nil || !all){
+		Bprint(fd, "To: %s\n", rpto);
+		return;
+	}
+
+	n = 0;
+	addrs = emalloc(64*sizeof(char*));
+	n += tokenize(to, addrs+n, 64-n);
+	n += tokenize(rpto, addrs+n, 64-n);
+	n += tokenize(r->to, addrs+n, 64-n);
+	show(fd, "To", addrs, n);
+	n = tokenize(r->cc, addrs+n, 64-n);
+	show(fd, "CC", addrs, n);
+	free(addrs);
+}
+
+void
 compose(char *to, Mesg *r, int quote, int all)
 {
 	static int ncompose;
@@ -199,16 +256,7 @@
 
 	wintagwrite(c, "Post ");
 	wfd = bwinopen(c, "body", OWRITE);
-	if(r == nil || !all)
-		Bprint(wfd, "To: %s\n", to);
-	else{
-		if(strlen(r->from) > 0)
-			Bprint(wfd, "To: %s\n", r->from);
-		if(strlen(r->to) > 0)
-			Bprint(wfd, "To: %s\n", r->to);
-		if(strlen(r->cc) > 0)
-			Bprint(wfd, "CC: %s\n", r->cc);
-	}
+	respondto(wfd, to, r, all);
 	if(r == nil)
 		Bprint(wfd, "Subject: ");
 	else{