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 */
72 quote(Message *m, Biobuf *b, char *dir, char *quotetext)
83 /* look for first textual component to quote */
84 type = readfile(dir, "type", &n);
86 print("no type in %s\n", dir);
89 if(strncmp(type, "multipart/", 10)==0 || strncmp(type, "message/", 8)==0){
90 dir = estrstrdup(dir, "1/");
91 if(quote(m, b, dir, nil)){
98 if(strncmp(type, "text", 4) != 0){
102 body = readbody(m->type, dir, &n);
111 lines = emalloc(nlines*sizeof(char*));
112 nlines = getfields(body, lines, nlines, 0, "\n");
113 /* delete leading and trailing blank lines */
115 while(i<nlines && lines[i][0]=='\0')
117 while(i<nlines && lines[nlines-1][0]=='\0')
120 Bprint(b, ">%s%s\n", lines[i][0]=='>'? "" : " ", lines[i]);
124 free(body); /* will free quotetext if non-nil */
131 mkreply(Message *m, char *label, char *to, Plumbattr *attr, char *quotetext)
138 quotereply = (label[0] == 'Q');
139 r = emalloc(sizeof(Message));
142 r->replyname = estrdup(m->name);
143 r->next = replies.head;
145 if(replies.head != nil)
146 replies.head->prev = r;
148 if(replies.tail == nil)
150 r->name = emalloc(strlen(mbox.name)+strlen(label)+10);
151 sprint(r->name, "%s%s%d", mbox.name, label, ++replyid);
153 winname(r->w, r->name);
154 ctlprint(r->w->ctl, "cleartag");
155 wintagwrite(r->w, "fmt Look Post Undo", 4+5+5+4);
157 threadcreate(mesgctl, r, STACK);
158 winopenbody(r->w, OWRITE);
159 if(to!=nil && to[0]!='\0') {
160 // Bprint(r->w->body, "%s\n", to);
161 fsprint(r->w->body, "%s\n", to);
163 for(a=attr; a; a=a->next) {
164 // Bprint(r->w->body, "%s: %s\n", a->name, a->value);
165 fsprint(r->w->body, "%s: %s\n", a->name, a->value);
169 dir = estrstrdup(mbox.name, m->name);
170 if(to == nil && attr == nil){
171 /* Reply goes to replyto; Reply all goes to From and To and CC */
172 if(strstr(label, "all") == nil) {
173 // jpc Bprint(r->w->body, "To: %s\n", m->replyto);
174 fsprint(r->w->body, "To: %s\n", m->replyto);
177 if(strlen(m->from) > 0) {
178 // Bprint(r->w->body, "To: %s\n", m->from);
179 fsprint(r->w->body, "To: %s\n", m->from);
181 if(strlen(m->to) > 0) {
182 // Bprint(r->w->body, "To: %s\n", m->to);
183 fsprint(r->w->body, "To: %s\n", m->to);
185 if(strlen(m->cc) > 0) {
186 // Bprint(r->w->body, "CC: %s\n", m->cc);
187 fsprint(r->w->body, "CC: %s\n", m->cc);
191 if(strlen(m->subject) > 0){
193 if(strlen(m->subject) >= 3)
194 if(tolower(m->subject[0])=='r' && tolower(m->subject[1])=='e' && m->subject[2]==':')
196 // Bprint(r->w->body, "%s%s\n", t, m->subject);
197 fsprint(r->w->body, "%s%s\n", t, m->subject);
200 // Bprint(r->w->body, "Include: %sraw\n", dir);
201 fsprint(r->w->body, "Include: %sraw\n", dir);
205 // Bprint(r->w->body, "\n");
206 fsprint(r->w->body, "\n");
208 // Bprint(r->w->body, "\n");
209 fsprint(r->w->body, "\n");
212 quote(m, r->w->body, dir, quotetext);
216 if(m==nil && (to==nil || to[0]=='\0'))
217 winselect(r->w, "0", 0);
219 winselect(r->w, "$", 0);
228 replies.tail = m->prev;
230 m->next->prev = m->prev;
232 replies.head = m->next;
234 m->prev->next = m->next;
239 /* copy argv to stack and free the incoming strings, so we don't leak argument vectors */
241 buildargv(char **inargv, char *argv[NARGS+1], char args[NARGCHAR])
247 for(i=0; i<NARGS; i++){
252 if((s-args)+n >= NARGCHAR) /* too many characters */
268 char *argv[NARGS+1], args[NARGCHAR];
276 prog = e->prog; /* known not to be malloc'ed */
279 buildargv(e->argv, argv, args);
295 threadexec(nil, fd, prog, argv);
298 /* jpc - procexec(nil, prog, argv); */
300 //fprint(2, "exec: %s", e->prog);
302 //for(i=0; argv[i]; i++) print(" '%s'", argv[i]);
307 //procexec(nil, "/bin/cat", argv);
308 fprint(2, "Mail: can't exec %s: %r\n", prog);
309 threadexits("can't exec");
336 for(i=0; headers[i]!=nil; i++)
337 if(cistrcmp(h, headers[i]) == 0)
350 addressed(char *name)
355 if(strcmp(name, tolist[i]) == 0)
358 if(strcmp(name, cclist[i]) == 0)
360 for(i=0; i<nbcc; i++)
361 if(strcmp(name, bcclist[i]) == 0)
367 skipbl(char *s, char *e)
370 if(*s!=' ' && *s!='\t' && *s!=',')
378 findbl(char *s, char *e)
381 if(*s==' ' || *s=='\t' || *s==',')
389 * comma-separate possibly blank-separated strings in line; e points before newline
392 commas(char *s, char *e)
396 /* may have initial blanks */
403 if(t == e) /* no more words */
413 print2(int fd, int ofd, char *fmt, ...)
420 s = vsmprint(fmt, arg);
432 write2(int fd, int ofd, char *buf, int n, int nofrom)
447 /* need to escape leading From lines to avoid corrupting 'outgoing' mailbox */
448 for(p=buf; *p; p+=m){
449 from = cistrstr(p, "from");
457 if(p==buf || from[-1]=='\n')
458 write(ofd, " ", 1); /* escape with space if From is at start of line */
470 int i, j, h, n, natt, p[2];
473 int first, nfld, delit, ofd;
474 char *copy, *fld[100], *now;
476 body = winreadbody(m->w, &n);
477 /* assemble to: list from first line, to: line, and cc: line */
485 for(s=to; *s!='\n'; s++)
490 if(s++ == to) /* blank line */
492 /* make copy of line to tokenize */
493 copy = emalloc(s-to);
494 memmove(copy, to, s-to);
496 nfld = tokenizec(copy, fld, nelem(fld), ", \t");
502 switch(h = whichheader(fld[0])){
506 commas(to+strlen(fld[0]), s-1);
507 for(i=1; i<nfld && nto<nelem(tolist); i++)
508 if(!addressed(fld[i]))
509 tolist[nto++] = estrdup(fld[i]);
513 commas(to+strlen(fld[0]), s-1);
514 for(i=1; i<nfld && nbcc<nelem(bcclist); i++)
515 if(!addressed(fld[i]))
516 bcclist[nbcc++] = estrdup(fld[i]);
520 commas(to+strlen(fld[0]), s-1);
521 for(i=1; i<nfld && ncc<nelem(cclist); i++)
522 if(!addressed(fld[i]))
523 cclist[ncc++] = estrdup(fld[i]);
528 for(i=1; i<nfld && natt<nelem(attlist); i++){
529 attlist[natt] = estrdup(fld[i]);
530 included[natt++] = (h == INCLUDE);
536 for(i=0; i<nfld && nto<nelem(tolist); i++)
537 tolist[nto++] = estrdup(fld[i]);
538 }else /* ignore it */
543 /* delete line from body */
551 ofd = open(outgoing, OWRITE|OCEXEC); /* no error check necessary */
553 /* From dhog Fri Aug 24 22:13:00 EDT 2001 */
554 now = ctime(time(0));
555 fprint(ofd, "From %s %s", user, now);
556 fprint(ofd, "From: %s\n", user);
557 fprint(ofd, "Date: %s", now);
558 for(i=0; i<natt; i++)
560 fprint(ofd, "Include: %s\n", attlist[i]);
562 fprint(ofd, "Attach: %s\n", attlist[i]);
563 /* needed because mail is by default Latin-1 */
564 fprint(ofd, "Content-Type: text/plain; charset=\"UTF-8\"\n");
565 fprint(ofd, "Content-Transfer-Encoding: 8bit\n");
568 e = emalloc(sizeof(struct Exec));
570 error("can't create pipe: %r");
573 e->prog = unsharp("#9/bin/upas/marshal");
574 e->argv = emalloc((1+1+2+4*natt+1)*sizeof(char*));
575 e->argv[0] = estrdup("marshal");
576 e->argv[1] = estrdup("-8");
579 e->argv[j++] = estrdup("-R");
580 e->argv[j++] = estrstrdup(mbox.name, m->replyname);
582 for(i=0; i<natt; i++){
584 e->argv[j++] = estrdup("-A");
586 e->argv[j++] = estrdup("-a");
587 e->argv[j++] = estrdup(attlist[i]);
589 sync = chancreate(sizeof(int), 0);
591 proccreate(execproc, e, EXECSTACK);
595 /* using marshal -8, so generate rfc822 headers */
597 print2(p[1], ofd, "To: ");
598 for(i=0; i<nto-1; i++)
599 print2(p[1], ofd, "%s, ", tolist[i]);
600 print2(p[1], ofd, "%s\n", tolist[i]);
603 print2(p[1], ofd, "CC: ");
604 for(i=0; i<ncc-1; i++)
605 print2(p[1], ofd, "%s, ", cclist[i]);
606 print2(p[1], ofd, "%s\n", cclist[i]);
609 print2(p[1], ofd, "BCC: ");
610 for(i=0; i<nbcc-1; i++)
611 print2(p[1], ofd, "%s, ", bcclist[i]);
612 print2(p[1], ofd, "%s\n", bcclist[i]);
617 write2(p[1], ofd, body, i, 1);
619 /* guarantee a blank line, to ensure attachments are separated from body */
620 if(i==0 || body[i-1]!='\n')
621 write2(p[1], ofd, "\n\n", 2, 0);
622 else if(i>1 && body[i-2]!='\n')
623 write2(p[1], ofd, "\n", 1, 0);
625 /* these look like pseudo-attachments in the "outgoing" box */
627 for(i=0; i<natt; i++)
629 fprint(ofd, "=====> Include: %s\n", attlist[i]);
631 fprint(ofd, "=====> Attach: %s\n", attlist[i]);
636 for(i=0; i<natt; i++)
642 if(m->replyname != nil)
643 mesgmenumark(mbox.w, m->replyname, "\t[replied]");
644 if(m->name[0] == '/')
645 s = estrdup(m->name);
647 s = estrstrdup(mbox.name, m->name);
648 s = egrow(s, "-R", nil);
652 /* mark message unopened because it's no longer the original message */