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