xref: /plan9/acme/mail/src/reply.c (revision 1517f4bcaafeec8f0dfc0d2ad935e62cecf80d8e)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <thread.h>
5 #include <ctype.h>
6 #include <plumb.h>
7 #include "dat.h"
8 
9 static int	replyid;
10 
11 int
12 quote(Message *m, Biobuf *b, char *dir, char *quotetext)
13 {
14 	char *body, *type;
15 	int i, n, nlines;
16 	char **lines;
17 
18 	if(quotetext){
19 		body = quotetext;
20 		n = strlen(body);
21 		type = nil;
22 	}else{
23 		/* look for first textual component to quote */
24 		type = readfile(dir, "type", &n);
25 		if(type == nil){
26 			print("no type in %s\n", dir);
27 			return 0;
28 		}
29 		if(strncmp(type, "multipart/", 10)==0 || strncmp(type, "message/", 8)==0){
30 			dir = estrstrdup(dir, "1/");
31 			if(quote(m, b, dir, nil)){
32 				free(type);
33 				free(dir);
34 				return 1;
35 			}
36 			free(dir);
37 		}
38 		if(strncmp(type, "text", 4) != 0){
39 			free(type);
40 			return 0;
41 		}
42 		body = readbody(m->type, dir, &n);
43 		if(body == nil)
44 			return 0;
45 	}
46 	nlines = 0;
47 	for(i=0; i<n; i++)
48 		if(body[i] == '\n')
49 			nlines++;
50 	nlines++;
51 	lines = emalloc(nlines*sizeof(char*));
52 	nlines = getfields(body, lines, nlines, 0, "\n");
53 	/* delete leading and trailing blank lines */
54 	i = 0;
55 	while(i<nlines && lines[i][0]=='\0')
56 		i++;
57 	while(i<nlines && lines[nlines-1][0]=='\0')
58 		nlines--;
59 	while(i < nlines){
60 		Bprint(b, ">%s%s\n", lines[i][0]=='>'? "" : " ", lines[i]);
61 		i++;
62 	}
63 	free(lines);
64 	free(body);	/* will free quotetext if non-nil */
65 	free(type);
66 	return 1;
67 }
68 
69 void
70 mkreply(Message *m, char *label, char *to, Plumbattr *attr, char *quotetext)
71 {
72 	Message *r;
73 	char *dir, *t;
74 	int quotereply;
75 	Plumbattr *a;
76 
77 	quotereply = (label[0] == 'Q');
78 	r = emalloc(sizeof(Message));
79 	r->isreply = 1;
80 	if(m != nil)
81 		r->replyname = estrdup(m->name);
82 	r->next = replies.head;
83 	r->prev = nil;
84 	if(replies.head != nil)
85 		replies.head->prev = r;
86 	replies.head = r;
87 	if(replies.tail == nil)
88 		replies.tail = r;
89 	r->name = emalloc(strlen(mbox.name)+strlen(label)+10);
90 	sprint(r->name, "%s%s%d", mbox.name, label, ++replyid);
91 	r->w = newwindow();
92 	winname(r->w, r->name);
93 	ctlprint(r->w->ctl, "cleartag");
94 	wintagwrite(r->w, "fmt Look Post Undo", 4+5+5+4);
95 	r->tagposted = 1;
96 	threadcreate(mesgctl, r, STACK);
97 	winopenbody(r->w, OWRITE);
98 	if(to!=nil && to[0]!='\0')
99 		Bprint(r->w->body, "%s\n", to);
100 	for(a=attr; a; a=a->next)
101 		Bprint(r->w->body, "%s: %s\n", a->name, a->value);
102 	dir = nil;
103 	if(m != nil){
104 		dir = estrstrdup(mbox.name, m->name);
105 		if(to == nil && attr == nil){
106 			/* Reply goes to replyto; Reply all goes to From and To and CC */
107 			if(strstr(label, "all") == nil)
108 				Bprint(r->w->body, "To: %s\n", m->replyto);
109 			else{	/* Replyall */
110 				if(strlen(m->from) > 0)
111 					Bprint(r->w->body, "To: %s\n", m->from);
112 				if(strlen(m->to) > 0)
113 					Bprint(r->w->body, "To: %s\n", m->to);
114 				if(strlen(m->cc) > 0)
115 					Bprint(r->w->body, "CC: %s\n", m->cc);
116 			}
117 		}
118 		if(strlen(m->subject) > 0){
119 			t = "Subject: Re: ";
120 			if(strlen(m->subject) >= 3)
121 				if(tolower(m->subject[0])=='r' && tolower(m->subject[1])=='e' && m->subject[2]==':')
122 					t = "Subject: ";
123 			Bprint(r->w->body, "%s%s\n", t, m->subject);
124 		}
125 		if(!quotereply){
126 			Bprint(r->w->body, "Include: %sraw\n", dir);
127 			free(dir);
128 		}
129 	}
130 	Bprint(r->w->body, "\n");
131 	if(m == nil)
132 		Bprint(r->w->body, "\n");
133 	else if(quotereply){
134 		quote(m, r->w->body, dir, quotetext);
135 		free(dir);
136 	}
137 	winclosebody(r->w);
138 	if(m==nil && (to==nil || to[0]=='\0'))
139 		winselect(r->w, "0", 0);
140 	else
141 		winselect(r->w, "$", 0);
142 	winclean(r->w);
143 	windormant(r->w);
144 }
145 
146 void
147 delreply(Message *m)
148 {
149 	if(m->next == nil)
150 		replies.tail = m->prev;
151 	else
152 		m->next->prev = m->prev;
153 	if(m->prev == nil)
154 		replies.head = m->next;
155 	else
156 		m->prev->next = m->next;
157 	mesgfreeparts(m);
158 	free(m);
159 }
160 
161 /* copy argv to stack and free the incoming strings, so we don't leak argument vectors */
162 void
163 buildargv(char **inargv, char *argv[NARGS+1], char args[NARGCHAR])
164 {
165 	int i, n;
166 	char *s, *a;
167 
168 	s = args;
169 	for(i=0; i<NARGS; i++){
170 		a = inargv[i];
171 		if(a == nil)
172 			break;
173 		n = strlen(a)+1;
174 		if((s-args)+n >= NARGCHAR)	/* too many characters */
175 			break;
176 		argv[i] = s;
177 		memmove(s, a, n);
178 		s += n;
179 		free(a);
180 	}
181 	argv[i] = nil;
182 }
183 
184 void
185 execproc(void *v)
186 {
187 	struct Exec *e;
188 	int p[2], q[2];
189 	char *prog;
190 	char *argv[NARGS+1], args[NARGCHAR];
191 
192 	e = v;
193 	p[0] = e->p[0];
194 	p[1] = e->p[1];
195 	q[0] = e->q[0];
196 	q[1] = e->q[1];
197 	prog = e->prog;	/* known not to be malloc'ed */
198 	rfork(RFFDG);
199 	sendul(e->sync, 1);
200 	buildargv(e->argv, argv, args);
201 	free(e->argv);
202 	chanfree(e->sync);
203 	free(e);
204 	dup(p[0], 0);
205 	close(p[0]);
206 	close(p[1]);
207 	if(q[0]){
208 		dup(q[1], 1);
209 		close(q[0]);
210 		close(q[1]);
211 	}
212 	procexec(nil, prog, argv);
213 //fprint(2, "exec: %s", e->prog);
214 //{int i;
215 //for(i=0; argv[i]; i++) print(" '%s'", argv[i]);
216 //print("\n");
217 //}
218 //argv[0] = "cat";
219 //argv[1] = nil;
220 //procexec(nil, "/bin/cat", argv);
221 	fprint(2, "Mail: can't exec %s: %r\n", prog);
222 	threadexits("can't exec");
223 }
224 
225 enum{
226 	ATTACH,
227 	BCC,
228 	CC,
229 	FROM,
230 	INCLUDE,
231 	TO,
232 };
233 
234 char *headers[] = {
235 	"attach:",
236 	"bcc:",
237 	"cc:",
238 	"from:",
239 	"include:",
240 	"to:",
241 	nil,
242 };
243 
244 int
245 whichheader(char *h)
246 {
247 	int i;
248 
249 	for(i=0; headers[i]!=nil; i++)
250 		if(cistrcmp(h, headers[i]) == 0)
251 			return i;
252 	return -1;
253 }
254 
255 char *tolist[200];
256 char	*cclist[200];
257 char	*bcclist[200];
258 int ncc, nbcc, nto;
259 char	*attlist[200];
260 char	included[200];
261 
262 int
263 addressed(char *name)
264 {
265 	int i;
266 
267 	for(i=0; i<nto; i++)
268 		if(strcmp(name, tolist[i]) == 0)
269 			return 1;
270 	for(i=0; i<ncc; i++)
271 		if(strcmp(name, cclist[i]) == 0)
272 			return 1;
273 	for(i=0; i<nbcc; i++)
274 		if(strcmp(name, bcclist[i]) == 0)
275 			return 1;
276 	return 0;
277 }
278 
279 char*
280 skipbl(char *s, char *e)
281 {
282 	while(s < e){
283 		if(*s!=' ' && *s!='\t' && *s!=',')
284 			break;
285 		s++;
286 	}
287 	return s;
288 }
289 
290 char*
291 findbl(char *s, char *e)
292 {
293 	while(s < e){
294 		if(*s==' ' || *s=='\t' || *s==',')
295 			break;
296 		s++;
297 	}
298 	return s;
299 }
300 
301 /*
302  * comma-separate possibly blank-separated strings in line; e points before newline
303  */
304 void
305 commas(char *s, char *e)
306 {
307 	char *t;
308 
309 	/* may have initial blanks */
310 	s = skipbl(s, e);
311 	while(s < e){
312 		s = findbl(s, e);
313 		if(s == e)
314 			break;
315 		t = skipbl(s, e);
316 		if(t == e)	/* no more words */
317 			break;
318 		/* patch comma */
319 		*s++ = ',';
320 		while(s < t)
321 			*s++ = ' ';
322 	}
323 }
324 
325 int
326 print2(int fd, int ofd, char *fmt, ...)
327 {
328 	int m, n;
329 	char *s;
330 	va_list arg;
331 
332 	va_start(arg, fmt);
333 	s = vsmprint(fmt, arg);
334 	va_end(arg);
335 	if(s == nil)
336 		return -1;
337 	m = strlen(s);
338 	n = write(fd, s, m);
339 	if(ofd > 0)
340 		write(ofd, s, m);
341 	return n;
342 }
343 
344 void
345 write2(int fd, int ofd, char *buf, int n, int nofrom)
346 {
347 	char *from, *p;
348 	int m;
349 
350 	write(fd, buf, n);
351 
352 	if(ofd <= 0)
353 		return;
354 
355 	if(nofrom == 0){
356 		write(ofd, buf, n);
357 		return;
358 	}
359 
360 	/* need to escape leading From lines to avoid corrupting 'outgoing' mailbox */
361 	for(p=buf; *p; p+=m){
362 		from = cistrstr(p, "from");
363 		if(from == nil)
364 			m = n;
365 		else
366 			m = from - p;
367 		if(m > 0)
368 			write(ofd, p, m);
369 		if(from){
370 			if(p==buf || from[-1]=='\n')
371 				write(ofd, " ", 1);	/* escape with space if From is at start of line */
372 			write(ofd, from, 4);
373 			m += 4;
374 		}
375 		n -= m;
376 	}
377 }
378 
379 void
380 mesgsend(Message *m)
381 {
382 	char *s, *body, *to;
383 	int i, j, h, n, natt, p[2];
384 	struct Exec *e;
385 	Channel *sync;
386 	int first, nfld, delit, ofd;
387 	char *copy, *fld[100], *now;
388 
389 	body = winreadbody(m->w, &n);
390 	/* assemble to: list from first line, to: line, and cc: line */
391 	nto = 0;
392 	natt = 0;
393 	ncc = 0;
394 	nbcc = 0;
395 	first = 1;
396 	to = body;
397 	for(;;){
398 		for(s=to; *s!='\n'; s++)
399 			if(*s == '\0'){
400 				free(body);
401 				return;
402 			}
403 		if(s++ == to)	/* blank line */
404 			break;
405 		/* make copy of line to tokenize */
406 		copy = emalloc(s-to);
407 		memmove(copy, to, s-to);
408 		copy[s-to-1] = '\0';
409 		nfld = tokenizec(copy, fld, nelem(fld), ", \t");
410 		if(nfld == 0){
411 			free(copy);
412 			break;
413 		}
414 		n -= s-to;
415 		switch(h = whichheader(fld[0])){
416 		case TO:
417 		case FROM:
418 			delit = 1;
419 			commas(to+strlen(fld[0]), s-1);
420 			for(i=1; i<nfld && nto<nelem(tolist); i++)
421 				if(!addressed(fld[i]))
422 					tolist[nto++] = estrdup(fld[i]);
423 			break;
424 		case BCC:
425 			delit = 1;
426 			commas(to+strlen(fld[0]), s-1);
427 			for(i=1; i<nfld && nbcc<nelem(bcclist); i++)
428 				if(!addressed(fld[i]))
429 					bcclist[nbcc++] = estrdup(fld[i]);
430 			break;
431 		case CC:
432 			delit = 1;
433 			commas(to+strlen(fld[0]), s-1);
434 			for(i=1; i<nfld && ncc<nelem(cclist); i++)
435 				if(!addressed(fld[i]))
436 					cclist[ncc++] = estrdup(fld[i]);
437 			break;
438 		case ATTACH:
439 		case INCLUDE:
440 			delit = 1;
441 			for(i=1; i<nfld && natt<nelem(attlist); i++){
442 				attlist[natt] = estrdup(fld[i]);
443 				included[natt++] = (h == INCLUDE);
444 			}
445 			break;
446 		default:
447 			if(first){
448 				delit = 1;
449 				for(i=0; i<nfld && nto<nelem(tolist); i++)
450 					tolist[nto++] = estrdup(fld[i]);
451 			}else	/* ignore it */
452 				delit = 0;
453 			break;
454 		}
455 		if(delit){
456 			/* delete line from body */
457 			memmove(to, s, n+1);
458 		}else
459 			to = s;
460 		free(copy);
461 		first = 0;
462 	}
463 
464 	ofd = open(outgoing, OWRITE|OCEXEC);	/* no error check necessary */
465 	if(ofd > 0){
466 		/* From dhog Fri Aug 24 22:13:00 EDT 2001 */
467 		now = ctime(time(0));
468 		fprint(ofd, "From %s %s", user, now);
469 		fprint(ofd, "From: %s\n", user);
470 		fprint(ofd, "Date: %s", now);
471 		for(i=0; i<natt; i++)
472 			if(included[i])
473 				fprint(ofd, "Include: %s\n", attlist[i]);
474 			else
475 				fprint(ofd, "Attach: %s\n", attlist[i]);
476 		/* needed because mail is by default Latin-1 */
477 		fprint(ofd, "Content-Type: text/plain; charset=\"UTF-8\"\n");
478 		fprint(ofd, "Content-Transfer-Encoding: 8bit\n");
479 	}
480 
481 	e = emalloc(sizeof(struct Exec));
482 	if(pipe(p) < 0)
483 		error("can't create pipe: %r");
484 	e->p[0] = p[0];
485 	e->p[1] = p[1];
486 	e->prog = "/bin/upas/marshal";
487 	e->argv = emalloc((1+1+2+4*natt+1)*sizeof(char*));
488 	e->argv[0] = estrdup("marshal");
489 	e->argv[1] = estrdup("-8");
490 	j = 2;
491 	if(m->replyname){
492 		e->argv[j++] = estrdup("-R");
493 		e->argv[j++] = estrstrdup(mbox.name, m->replyname);
494 	}
495 	for(i=0; i<natt; i++){
496 		if(included[i])
497 			e->argv[j++] = estrdup("-A");
498 		else
499 			e->argv[j++] = estrdup("-a");
500 		e->argv[j++] = estrdup(attlist[i]);
501 	}
502 	sync = chancreate(sizeof(int), 0);
503 	e->sync = sync;
504 	proccreate(execproc, e, EXECSTACK);
505 	recvul(sync);
506 	close(p[0]);
507 
508 	/* using marshal -8, so generate rfc822 headers */
509 	if(nto > 0){
510 		print2(p[1], ofd, "To: ");
511 		for(i=0; i<nto-1; i++)
512 			print2(p[1], ofd, "%s, ", tolist[i]);
513 		print2(p[1], ofd, "%s\n", tolist[i]);
514 	}
515 	if(ncc > 0){
516 		print2(p[1], ofd, "CC: ");
517 		for(i=0; i<ncc-1; i++)
518 			print2(p[1], ofd, "%s, ", cclist[i]);
519 		print2(p[1], ofd, "%s\n", cclist[i]);
520 	}
521 	if(nbcc > 0){
522 		print2(p[1], ofd, "BCC: ");
523 		for(i=0; i<nbcc-1; i++)
524 			print2(p[1], ofd, "%s, ", bcclist[i]);
525 		print2(p[1], ofd, "%s\n", bcclist[i]);
526 	}
527 
528 	i = strlen(body);
529 	if(i > 0)
530 		write2(p[1], ofd, body, i, 1);
531 
532 	/* guarantee a blank line, to ensure attachments are separated from body */
533 	if(i==0 || body[i-1]!='\n')
534 		write2(p[1], ofd, "\n\n", 2, 0);
535 	else if(i>1 && body[i-2]!='\n')
536 		write2(p[1], ofd, "\n", 1, 0);
537 
538 	/* these look like pseudo-attachments in the "outgoing" box */
539 	if(ofd>0 && natt>0){
540 		for(i=0; i<natt; i++)
541 			if(included[i])
542 				fprint(ofd, "=====> Include: %s\n", attlist[i]);
543 			else
544 				fprint(ofd, "=====> Attach: %s\n", attlist[i]);
545 	}
546 	if(ofd > 0)
547 		write(ofd, "\n", 1);
548 
549 	for(i=0; i<natt; i++)
550 		free(attlist[i]);
551 	close(ofd);
552 	close(p[1]);
553 	free(body);
554 
555 	if(m->replyname != nil)
556 		mesgmenumark(mbox.w, m->replyname, "\t[replied]");
557 	if(m->name[0] == '/')
558 		s = estrdup(m->name);
559 	else
560 		s = estrstrdup(mbox.name, m->name);
561 	s = egrow(s, "-R", nil);
562 	winname(m->w, s);
563 	free(s);
564 	winclean(m->w);
565 	/* mark message unopened because it's no longer the original message */
566 	m->opened = 0;
567 }
568