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