12 quote(Message *m, Biobuf *b, char *dir, char *quotetext)
23 /* look for first textual component to quote */
24 type = readfile(dir, "type", &n);
26 print("no type in %s\n", dir);
29 if(strncmp(type, "multipart/", 10)==0 || strncmp(type, "message/", 8)==0){
30 dir = estrstrdup(dir, "1/");
31 if(quote(m, b, dir, nil)){
38 if(strncmp(type, "text", 4) != 0){
42 body = readbody(m->type, dir, &n);
51 lines = emalloc(nlines*sizeof(char*));
52 nlines = getfields(body, lines, nlines, 0, "\n");
53 /* delete leading and trailing blank lines */
55 while(i<nlines && lines[i][0]=='\0')
57 while(i<nlines && lines[nlines-1][0]=='\0')
60 Bprint(b, ">%s%s\n", lines[i][0]=='>'? "" : " ", lines[i]);
64 free(body); /* will free quotetext if non-nil */
70 mkreply(Message *m, char *label, char *to, Plumbattr *attr, char *quotetext)
77 quotereply = (label[0] == 'Q');
78 r = emalloc(sizeof(Message));
81 r->replyname = estrdup(m->name);
82 r->next = replies.head;
84 if(replies.head != nil)
85 replies.head->prev = r;
87 if(replies.tail == nil)
89 r->name = emalloc(strlen(mbox.name)+strlen(label)+10);
90 sprint(r->name, "%s%s%d", mbox.name, label, ++replyid);
92 winname(r->w, r->name);
93 ctlprint(r->w->ctl, "cleartag");
94 wintagwrite(r->w, "fmt Look Post Undo", 4+5+5+4);
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);
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);
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);
118 if(strlen(m->subject) > 0){
120 if(strlen(m->subject) >= 3)
121 if(tolower(m->subject[0])=='r' && tolower(m->subject[1])=='e' && m->subject[2]==':')
123 Bprint(r->w->body, "%s%s\n", t, m->subject);
126 Bprint(r->w->body, "Include: %sraw\n", dir);
130 Bprint(r->w->body, "\n");
132 Bprint(r->w->body, "\n");
134 quote(m, r->w->body, dir, quotetext);
138 if(m==nil && (to==nil || to[0]=='\0'))
139 winselect(r->w, "0", 0);
141 winselect(r->w, "$", 0);
150 replies.tail = m->prev;
152 m->next->prev = m->prev;
154 replies.head = m->next;
156 m->prev->next = m->next;
161 /* copy argv to stack and free the incoming strings, so we don't leak argument vectors */
163 buildargv(char **inargv, char *argv[NARGS+1], char args[NARGCHAR])
169 for(i=0; i<NARGS; i++){
174 if((s-args)+n >= NARGCHAR) /* too many characters */
190 char *argv[NARGS+1], args[NARGCHAR];
197 prog = e->prog; /* known not to be malloc'ed */
200 buildargv(e->argv, argv, args);
212 procexec(nil, prog, argv);
213 //fprint(2, "exec: %s", e->prog);
215 //for(i=0; argv[i]; i++) print(" '%s'", argv[i]);
220 //procexec(nil, "/bin/cat", argv);
221 fprint(2, "Mail: can't exec %s: %r\n", prog);
222 threadexits("can't exec");
249 for(i=0; headers[i]!=nil; i++)
250 if(cistrcmp(h, headers[i]) == 0)
263 addressed(char *name)
268 if(strcmp(name, tolist[i]) == 0)
271 if(strcmp(name, cclist[i]) == 0)
273 for(i=0; i<nbcc; i++)
274 if(strcmp(name, bcclist[i]) == 0)
280 skipbl(char *s, char *e)
283 if(*s!=' ' && *s!='\t' && *s!=',')
291 findbl(char *s, char *e)
294 if(*s==' ' || *s=='\t' || *s==',')
302 * comma-separate possibly blank-separated strings in line; e points before newline
305 commas(char *s, char *e)
309 /* may have initial blanks */
316 if(t == e) /* no more words */
326 print2(int fd, int ofd, char *fmt, ...)
333 s = vsmprint(fmt, arg);
345 write2(int fd, int ofd, char *buf, int n, int nofrom)
351 m = write(fd, buf, n);
357 return write(ofd, buf, n);
359 /* need to escape leading From lines to avoid corrupting 'outgoing' mailbox */
360 for(p=buf; *p; p+=m){
361 from = cistrstr(p, "from");
369 /* escape with space if From is at start of line */
370 if(p==buf || from[-1]=='\n')
384 int i, j, h, n, natt, p[2];
387 int first, nfld, delit, ofd;
388 char *copy, *fld[100], *now;
390 body = winreadbody(m->w, &n);
391 /* assemble to: list from first line, to: line, and cc: line */
399 for(s=to; *s!='\n'; s++)
404 if(s++ == to) /* blank line */
406 /* make copy of line to tokenize */
407 copy = emalloc(s-to);
408 memmove(copy, to, s-to);
410 nfld = tokenizec(copy, fld, nelem(fld), ", \t");
416 switch(h = whichheader(fld[0])){
420 commas(to+strlen(fld[0]), s-1);
421 for(i=1; i<nfld && nto<nelem(tolist); i++)
422 if(!addressed(fld[i]))
423 tolist[nto++] = estrdup(fld[i]);
427 commas(to+strlen(fld[0]), s-1);
428 for(i=1; i<nfld && nbcc<nelem(bcclist); i++)
429 if(!addressed(fld[i]))
430 bcclist[nbcc++] = estrdup(fld[i]);
434 commas(to+strlen(fld[0]), s-1);
435 for(i=1; i<nfld && ncc<nelem(cclist); i++)
436 if(!addressed(fld[i]))
437 cclist[ncc++] = estrdup(fld[i]);
442 for(i=1; i<nfld && natt<nelem(attlist); i++){
443 attlist[natt] = estrdup(fld[i]);
444 included[natt++] = (h == INCLUDE);
450 for(i=0; i<nfld && nto<nelem(tolist); i++)
451 tolist[nto++] = estrdup(fld[i]);
452 }else /* ignore it */
457 /* delete line from body */
465 ofd = open(outgoing, OWRITE|OCEXEC); /* no error check necessary */
467 /* From dhog Fri Aug 24 22:13:00 EDT 2001 */
468 now = ctime(time(0));
469 fprint(ofd, "From %s %s", user, now);
470 fprint(ofd, "From: %s\n", user);
471 fprint(ofd, "Date: %s", now);
472 for(i=0; i<natt; i++)
474 fprint(ofd, "Include: %s\n", attlist[i]);
476 fprint(ofd, "Attach: %s\n", attlist[i]);
477 /* needed because mail is by default Latin-1 */
478 fprint(ofd, "Content-Type: text/plain; charset=\"UTF-8\"\n");
479 fprint(ofd, "Content-Transfer-Encoding: 8bit\n");
482 e = emalloc(sizeof(struct Exec));
484 error("can't create pipe: %r");
487 e->prog = "/bin/upas/marshal";
488 e->argv = emalloc((1+1+2+4*natt+1)*sizeof(char*));
489 e->argv[0] = estrdup("marshal");
490 e->argv[1] = estrdup("-8");
493 e->argv[j++] = estrdup("-R");
494 e->argv[j++] = estrstrdup(mbox.name, m->replyname);
496 for(i=0; i<natt; i++){
498 e->argv[j++] = estrdup("-A");
500 e->argv[j++] = estrdup("-a");
501 e->argv[j++] = estrdup(attlist[i]);
503 sync = chancreate(sizeof(int), 0);
505 proccreate(execproc, e, EXECSTACK);
509 /* using marshal -8, so generate rfc822 headers */
511 print2(p[1], ofd, "To: ");
512 for(i=0; i<nto-1; i++)
513 print2(p[1], ofd, "%s, ", tolist[i]);
514 print2(p[1], ofd, "%s\n", tolist[i]);
517 print2(p[1], ofd, "CC: ");
518 for(i=0; i<ncc-1; i++)
519 print2(p[1], ofd, "%s, ", cclist[i]);
520 print2(p[1], ofd, "%s\n", cclist[i]);
523 print2(p[1], ofd, "BCC: ");
524 for(i=0; i<nbcc-1; i++)
525 print2(p[1], ofd, "%s, ", bcclist[i]);
526 print2(p[1], ofd, "%s\n", bcclist[i]);
531 write2(p[1], ofd, body, i, 1);
533 /* guarantee a blank line, to ensure attachments are separated from body */
534 if(i==0 || body[i-1]!='\n')
535 write2(p[1], ofd, "\n\n", 2, 0);
536 else if(i>1 && body[i-2]!='\n')
537 write2(p[1], ofd, "\n", 1, 0);
539 /* these look like pseudo-attachments in the "outgoing" box */
541 for(i=0; i<natt; i++)
543 fprint(ofd, "=====> Include: %s\n", attlist[i]);
545 fprint(ofd, "=====> Attach: %s\n", attlist[i]);
550 for(i=0; i<natt; i++)
556 if(m->replyname != nil)
557 mesgmenumark(mbox.w, m->replyname, "\t[replied]");
558 if(m->name[0] == '/')
559 s = estrdup(m->name);
561 s = estrstrdup(mbox.name, m->name);
562 s = egrow(s, "-R", nil);
566 /* mark message unopened because it's no longer the original message */