13 quote(Message *m, CFid *fid, char *dir, char *quotetext)
24 /* look for first textual component to quote */
25 type = readfile(dir, "type", &n);
27 print("no type in %s\n", dir);
30 if(strncmp(type, "multipart/", 10)==0 || strncmp(type, "message/", 8)==0){
31 dir = estrstrdup(dir, "1/");
32 if(quote(m, fid, dir, nil)){
39 if(strncmp(type, "text", 4) != 0){
43 body = readbody(m->type, dir, &n);
52 lines = emalloc(nlines*sizeof(char*));
53 nlines = getfields(body, lines, nlines, 0, "\n");
54 /* delete leading and trailing blank lines */
56 while(i<nlines && lines[i][0]=='\0')
58 while(i<nlines && lines[nlines-1][0]=='\0')
61 fsprint(fid, ">%s%s\n", lines[i][0]=='>'? "" : " ", lines[i]);
65 free(body); /* will free quotetext if non-nil */
71 mkreply(Message *m, char *label, char *to, Plumbattr *attr, char *quotetext)
80 quotereply = (label[0] == 'Q');
82 if(quotereply && m && m->replywinid > 0){
83 snprint(buf, sizeof buf, "%d/body", m->replywinid);
84 if((fd = fsopen(acmefs, buf, OWRITE)) != nil){
85 dir = estrstrdup(mbox.name, m->name);
86 quote(m, fd, dir, quotetext);
92 r = emalloc(sizeof(Message));
95 r->replyname = estrdup(m->name);
96 r->next = replies.head;
98 if(replies.head != nil)
99 replies.head->prev = r;
101 if(replies.tail == nil)
103 r->name = emalloc(strlen(mbox.name)+strlen(label)+10);
104 sprint(r->name, "%s%s%d", mbox.name, label, ++replyid);
107 m->replywinid = r->w->id;
108 winname(r->w, r->name);
109 ctlprint(r->w->ctl, "cleartag");
110 wintagwrite(r->w, "fmt Look Post Undo", 4+5+5+4);
112 threadcreate(mesgctl, r, STACK);
113 winopenbody(r->w, OWRITE);
114 if(to!=nil && to[0]!='\0')
115 fsprint(r->w->body, "%s\n", to);
116 for(a=attr; a; a=a->next)
117 fsprint(r->w->body, "%s: %s\n", a->name, a->value);
120 dir = estrstrdup(mbox.name, m->name);
121 if(to == nil && attr == nil){
122 /* Reply goes to replyto; Reply all goes to From and To and CC */
123 if(strstr(label, "all") == nil)
124 fsprint(r->w->body, "To: %s\n", m->replyto);
126 if(strlen(m->from) > 0)
127 fsprint(r->w->body, "To: %s\n", m->from);
128 if(strlen(m->to) > 0)
129 fsprint(r->w->body, "To: %s\n", m->to);
130 if(strlen(m->cc) > 0)
131 fsprint(r->w->body, "CC: %s\n", m->cc);
134 if(strlen(m->subject) > 0){
136 if(strlen(m->subject) >= 3)
137 if(tolower(m->subject[0])=='r' && tolower(m->subject[1])=='e' && m->subject[2]==':')
139 fsprint(r->w->body, "%s%s\n", t, m->subject);
142 fsprint(r->w->body, "Include: %sraw\n", dir);
146 fsprint(r->w->body, "\n");
148 fsprint(r->w->body, "\n");
150 quote(m, r->w->body, dir, quotetext);
154 if(m==nil && (to==nil || to[0]=='\0'))
155 winselect(r->w, "0", 0);
157 winselect(r->w, "$", 0);
166 replies.tail = m->prev;
168 m->next->prev = m->prev;
170 replies.head = m->next;
172 m->prev->next = m->next;
177 /* copy argv to stack and free the incoming strings, so we don't leak argument vectors */
179 buildargv(char **inargv, char *argv[NARGS+1], char args[NARGCHAR])
185 for(i=0; i<NARGS; i++){
190 if((s-args)+n >= NARGCHAR) /* too many characters */
206 char *argv[NARGS+1], args[NARGCHAR];
214 prog = e->prog; /* known not to be malloc'ed */
216 fd[0] = dup(p[0], -1);
218 fd[1] = dup(q[1], -1);
223 buildargv(e->argv, argv, args);
228 threadexec(nil, fd, prog, argv);
233 fprint(2, "Mail: can't exec %s: %r\n", prog);
234 threadexits("can't exec");
261 for(i=0; headers[i]!=nil; i++)
262 if(cistrcmp(h, headers[i]) == 0)
275 addressed(char *name)
280 if(strcmp(name, tolist[i]) == 0)
283 if(strcmp(name, cclist[i]) == 0)
285 for(i=0; i<nbcc; i++)
286 if(strcmp(name, bcclist[i]) == 0)
292 skipbl(char *s, char *e)
295 if(*s!=' ' && *s!='\t' && *s!=',')
303 findbl(char *s, char *e)
306 if(*s==' ' || *s=='\t' || *s==',')
314 * comma-separate possibly blank-separated strings in line; e points before newline
317 commas(char *s, char *e)
321 /* may have initial blanks */
328 if(t == e) /* no more words */
338 print2(int fd, int ofd, char *fmt, ...)
345 s = vsmprint(fmt, arg);
357 write2(int fd, int ofd, char *buf, int n, int nofrom)
372 /* need to escape leading From lines to avoid corrupting 'outgoing' mailbox */
373 for(p=buf; *p; p+=m){
374 from = cistrstr(p, "from");
382 if(p==buf || from[-1]=='\n')
383 write(ofd, " ", 1); /* escape with space if From is at start of line */
395 int i, j, h, n, natt, p[2];
398 int first, nfld, delit, ofd;
399 char *copy, *fld[100], *now;
401 body = winreadbody(m->w, &n);
402 /* assemble to: list from first line, to: line, and cc: line */
410 for(s=to; *s!='\n'; s++)
415 if(s++ == to) /* blank line */
417 /* make copy of line to tokenize */
418 copy = emalloc(s-to);
419 memmove(copy, to, s-to);
421 nfld = tokenizec(copy, fld, nelem(fld), ", \t");
427 switch(h = whichheader(fld[0])){
431 commas(to+strlen(fld[0]), s-1);
432 for(i=1; i<nfld && nto<nelem(tolist); i++)
433 if(!addressed(fld[i]))
434 tolist[nto++] = estrdup(fld[i]);
438 commas(to+strlen(fld[0]), s-1);
439 for(i=1; i<nfld && nbcc<nelem(bcclist); i++)
440 if(!addressed(fld[i]))
441 bcclist[nbcc++] = estrdup(fld[i]);
445 commas(to+strlen(fld[0]), s-1);
446 for(i=1; i<nfld && ncc<nelem(cclist); i++)
447 if(!addressed(fld[i]))
448 cclist[ncc++] = estrdup(fld[i]);
453 for(i=1; i<nfld && natt<nelem(attlist); i++){
454 attlist[natt] = estrdup(fld[i]);
455 included[natt++] = (h == INCLUDE);
461 for(i=0; i<nfld && nto<nelem(tolist); i++)
462 tolist[nto++] = estrdup(fld[i]);
463 }else /* ignore it */
468 /* delete line from body */
476 ofd = open(outgoing, OWRITE|OCEXEC); /* no error check necessary */
478 /* From dhog Fri Aug 24 22:13:00 EDT 2001 */
479 now = ctime(time(0));
481 fprint(ofd, "From %s %s", user, now);
482 fprint(ofd, "From: %s\n", user);
483 fprint(ofd, "Date: %s", now);
484 for(i=0; i<natt; i++)
486 fprint(ofd, "Include: %s\n", attlist[i]);
488 fprint(ofd, "Attach: %s\n", attlist[i]);
489 /* needed because mail is by default Latin-1 */
490 fprint(ofd, "Content-Type: text/plain; charset=\"UTF-8\"\n");
491 fprint(ofd, "Content-Transfer-Encoding: 8bit\n");
494 e = emalloc(sizeof(struct Exec));
496 error("can't create pipe: %r");
499 e->prog = unsharp("#9/bin/upas/marshal");
500 e->argv = emalloc((1+1+2+4*natt+1)*sizeof(char*));
501 e->argv[0] = estrdup("marshal");
502 e->argv[1] = estrdup("-8");
505 e->argv[j++] = estrdup("-R");
506 e->argv[j++] = estrstrdup(mbox.name, m->replyname);
508 for(i=0; i<natt; i++){
510 e->argv[j++] = estrdup("-A");
512 e->argv[j++] = estrdup("-a");
513 e->argv[j++] = estrdup(attlist[i]);
515 sync = chancreate(sizeof(int), 0);
517 proccreate(execproc, e, EXECSTACK);
521 /* using marshal -8, so generate rfc822 headers */
523 print2(p[1], ofd, "To: ");
524 for(i=0; i<nto-1; i++)
525 print2(p[1], ofd, "%s, ", tolist[i]);
526 print2(p[1], ofd, "%s\n", tolist[i]);
529 print2(p[1], ofd, "CC: ");
530 for(i=0; i<ncc-1; i++)
531 print2(p[1], ofd, "%s, ", cclist[i]);
532 print2(p[1], ofd, "%s\n", cclist[i]);
535 print2(p[1], ofd, "BCC: ");
536 for(i=0; i<nbcc-1; i++)
537 print2(p[1], ofd, "%s, ", bcclist[i]);
538 print2(p[1], ofd, "%s\n", bcclist[i]);
543 write2(p[1], ofd, body, i, 1);
545 /* guarantee a blank line, to ensure attachments are separated from body */
546 if(i==0 || body[i-1]!='\n')
547 write2(p[1], ofd, "\n\n", 2, 0);
548 else if(i>1 && body[i-2]!='\n')
549 write2(p[1], ofd, "\n", 1, 0);
551 /* these look like pseudo-attachments in the "outgoing" box */
553 for(i=0; i<natt; i++)
555 fprint(ofd, "=====> Include: %s\n", attlist[i]);
557 fprint(ofd, "=====> Attach: %s\n", attlist[i]);
562 for(i=0; i<natt; i++)
568 if(m->replyname != nil)
569 mesgmenumark(mbox.w, m->replyname, "\t[replied]");
570 if(m->name[0] == '/')
571 s = estrdup(m->name);
573 s = estrstrdup(mbox.name, m->name);
574 s = egrow(s, "-R", nil);
578 /* mark message unopened because it's no longer the original message */