Blob


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 <9pclient.h>
8 #include "dat.h"
10 static int replyid;
12 int
13 quote(Message *m, CFid *fid, char *dir, char *quotetext)
14 {
15 char *body, *type;
16 int i, n, nlines;
17 char **lines;
19 if(quotetext){
20 body = quotetext;
21 n = strlen(body);
22 type = nil;
23 }else{
24 /* look for first textual component to quote */
25 type = readfile(dir, "type", &n);
26 if(type == nil){
27 print("no type in %s\n", dir);
28 return 0;
29 }
30 if(strncmp(type, "multipart/", 10)==0 || strncmp(type, "message/", 8)==0){
31 dir = estrstrdup(dir, "1/");
32 if(quote(m, fid, dir, nil)){
33 free(type);
34 free(dir);
35 return 1;
36 }
37 free(dir);
38 }
39 if(strncmp(type, "text", 4) != 0){
40 free(type);
41 return 0;
42 }
43 body = readbody(m->type, dir, &n);
44 if(body == nil)
45 return 0;
46 }
47 nlines = 0;
48 for(i=0; i<n; i++)
49 if(body[i] == '\n')
50 nlines++;
51 nlines++;
52 lines = emalloc(nlines*sizeof(char*));
53 nlines = getfields(body, lines, nlines, 0, "\n");
54 /* delete leading and trailing blank lines */
55 i = 0;
56 while(i<nlines && lines[i][0]=='\0')
57 i++;
58 while(i<nlines && lines[nlines-1][0]=='\0')
59 nlines--;
60 while(i < nlines){
61 fsprint(fid, ">%s%s\n", lines[i][0]=='>'? "" : " ", lines[i]);
62 i++;
63 }
64 free(lines);
65 free(body); /* will free quotetext if non-nil */
66 free(type);
67 return 1;
68 }
70 #if 0 /* jpc */
71 int
72 quote(Message *m, Biobuf *b, char *dir, char *quotetext)
73 {
74 char *body, *type;
75 int i, n, nlines;
76 char **lines;
78 if(quotetext){
79 body = quotetext;
80 n = strlen(body);
81 type = nil;
82 }else{
83 /* look for first textual component to quote */
84 type = readfile(dir, "type", &n);
85 if(type == nil){
86 print("no type in %s\n", dir);
87 return 0;
88 }
89 if(strncmp(type, "multipart/", 10)==0 || strncmp(type, "message/", 8)==0){
90 dir = estrstrdup(dir, "1/");
91 if(quote(m, b, dir, nil)){
92 free(type);
93 free(dir);
94 return 1;
95 }
96 free(dir);
97 }
98 if(strncmp(type, "text", 4) != 0){
99 free(type);
100 return 0;
102 body = readbody(m->type, dir, &n);
103 if(body == nil)
104 return 0;
106 nlines = 0;
107 for(i=0; i<n; i++)
108 if(body[i] == '\n')
109 nlines++;
110 nlines++;
111 lines = emalloc(nlines*sizeof(char*));
112 nlines = getfields(body, lines, nlines, 0, "\n");
113 /* delete leading and trailing blank lines */
114 i = 0;
115 while(i<nlines && lines[i][0]=='\0')
116 i++;
117 while(i<nlines && lines[nlines-1][0]=='\0')
118 nlines--;
119 while(i < nlines){
120 Bprint(b, ">%s%s\n", lines[i][0]=='>'? "" : " ", lines[i]);
121 i++;
123 free(lines);
124 free(body); /* will free quotetext if non-nil */
125 free(type);
126 return 1;
128 #endif
130 void
131 mkreply(Message *m, char *label, char *to, Plumbattr *attr, char *quotetext)
133 Message *r;
134 char *dir, *t;
135 int quotereply;
136 Plumbattr *a;
138 quotereply = (label[0] == 'Q');
139 r = emalloc(sizeof(Message));
140 r->isreply = 1;
141 if(m != nil)
142 r->replyname = estrdup(m->name);
143 r->next = replies.head;
144 r->prev = nil;
145 if(replies.head != nil)
146 replies.head->prev = r;
147 replies.head = r;
148 if(replies.tail == nil)
149 replies.tail = r;
150 r->name = emalloc(strlen(mbox.name)+strlen(label)+10);
151 sprint(r->name, "%s%s%d", mbox.name, label, ++replyid);
152 r->w = newwindow();
153 winname(r->w, r->name);
154 ctlprint(r->w->ctl, "cleartag");
155 wintagwrite(r->w, "fmt Look Post Undo", 4+5+5+4);
156 r->tagposted = 1;
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);
167 dir = nil;
168 if(m != nil){
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);
176 else{ /* Replyall */
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){
192 t = "Subject: Re: ";
193 if(strlen(m->subject) >= 3)
194 if(tolower(m->subject[0])=='r' && tolower(m->subject[1])=='e' && m->subject[2]==':')
195 t = "Subject: ";
196 // Bprint(r->w->body, "%s%s\n", t, m->subject);
197 fsprint(r->w->body, "%s%s\n", t, m->subject);
199 if(!quotereply){
200 // Bprint(r->w->body, "Include: %sraw\n", dir);
201 fsprint(r->w->body, "Include: %sraw\n", dir);
202 free(dir);
205 // Bprint(r->w->body, "\n");
206 fsprint(r->w->body, "\n");
207 if(m == nil) {
208 // Bprint(r->w->body, "\n");
209 fsprint(r->w->body, "\n");
211 else if(quotereply){
212 quote(m, r->w->body, dir, quotetext);
213 free(dir);
215 winclosebody(r->w);
216 if(m==nil && (to==nil || to[0]=='\0'))
217 winselect(r->w, "0", 0);
218 else
219 winselect(r->w, "$", 0);
220 winclean(r->w);
221 windormant(r->w);
224 void
225 delreply(Message *m)
227 if(m->next == nil)
228 replies.tail = m->prev;
229 else
230 m->next->prev = m->prev;
231 if(m->prev == nil)
232 replies.head = m->next;
233 else
234 m->prev->next = m->next;
235 mesgfreeparts(m);
236 free(m);
239 /* copy argv to stack and free the incoming strings, so we don't leak argument vectors */
240 void
241 buildargv(char **inargv, char *argv[NARGS+1], char args[NARGCHAR])
243 int i, n;
244 char *s, *a;
246 s = args;
247 for(i=0; i<NARGS; i++){
248 a = inargv[i];
249 if(a == nil)
250 break;
251 n = strlen(a)+1;
252 if((s-args)+n >= NARGCHAR) /* too many characters */
253 break;
254 argv[i] = s;
255 memmove(s, a, n);
256 s += n;
257 free(a);
259 argv[i] = nil;
262 void
263 execproc(void *v)
265 struct Exec *e;
266 int p[2], q[2];
267 char *prog;
268 char *argv[NARGS+1], args[NARGCHAR];
269 int fd[3];
271 e = v;
272 p[0] = e->p[0];
273 p[1] = e->p[1];
274 q[0] = e->q[0];
275 q[1] = e->q[1];
276 prog = e->prog; /* known not to be malloc'ed */
277 rfork(RFFDG);
278 sendul(e->sync, 1);
279 buildargv(e->argv, argv, args);
280 free(e->argv);
281 chanfree(e->sync);
282 free(e);
283 dup(p[0], 0);
284 close(p[0]);
285 close(p[1]);
286 if(q[0]){
287 dup(q[1], 1);
288 close(q[0]);
289 close(q[1]);
292 // jpc - start
293 fd[0] = dup(0, -1);
294 fd[1] = dup(1, -1);
295 threadexec(nil, fd, prog, argv);
296 close(fd[0]);
297 close(fd[1]);
298 /* jpc - procexec(nil, prog, argv); */
299 // jpc end
300 //fprint(2, "exec: %s", e->prog);
301 //{int i;
302 //for(i=0; argv[i]; i++) print(" '%s'", argv[i]);
303 //print("\n");
304 //}
305 //argv[0] = "cat";
306 //argv[1] = nil;
307 //procexec(nil, "/bin/cat", argv);
308 fprint(2, "Mail: can't exec %s: %r\n", prog);
309 threadexits("can't exec");
312 enum{
313 ATTACH,
314 BCC,
315 CC,
316 FROM,
317 INCLUDE,
318 TO,
319 };
321 char *headers[] = {
322 "attach:",
323 "bcc:",
324 "cc:",
325 "from:",
326 "include:",
327 "to:",
328 nil,
329 };
331 int
332 whichheader(char *h)
334 int i;
336 for(i=0; headers[i]!=nil; i++)
337 if(cistrcmp(h, headers[i]) == 0)
338 return i;
339 return -1;
342 char *tolist[200];
343 char *cclist[200];
344 char *bcclist[200];
345 int ncc, nbcc, nto;
346 char *attlist[200];
347 char included[200];
349 int
350 addressed(char *name)
352 int i;
354 for(i=0; i<nto; i++)
355 if(strcmp(name, tolist[i]) == 0)
356 return 1;
357 for(i=0; i<ncc; i++)
358 if(strcmp(name, cclist[i]) == 0)
359 return 1;
360 for(i=0; i<nbcc; i++)
361 if(strcmp(name, bcclist[i]) == 0)
362 return 1;
363 return 0;
366 char*
367 skipbl(char *s, char *e)
369 while(s < e){
370 if(*s!=' ' && *s!='\t' && *s!=',')
371 break;
372 s++;
374 return s;
377 char*
378 findbl(char *s, char *e)
380 while(s < e){
381 if(*s==' ' || *s=='\t' || *s==',')
382 break;
383 s++;
385 return s;
388 /*
389 * comma-separate possibly blank-separated strings in line; e points before newline
390 */
391 void
392 commas(char *s, char *e)
394 char *t;
396 /* may have initial blanks */
397 s = skipbl(s, e);
398 while(s < e){
399 s = findbl(s, e);
400 if(s == e)
401 break;
402 t = skipbl(s, e);
403 if(t == e) /* no more words */
404 break;
405 /* patch comma */
406 *s++ = ',';
407 while(s < t)
408 *s++ = ' ';
412 int
413 print2(int fd, int ofd, char *fmt, ...)
415 int m, n;
416 char *s;
417 va_list arg;
419 va_start(arg, fmt);
420 s = vsmprint(fmt, arg);
421 va_end(arg);
422 if(s == nil)
423 return -1;
424 m = strlen(s);
425 n = write(fd, s, m);
426 if(ofd > 0)
427 write(ofd, s, m);
428 return n;
431 void
432 write2(int fd, int ofd, char *buf, int n, int nofrom)
434 char *from, *p;
435 int m;
437 write(fd, buf, n);
439 if(ofd <= 0)
440 return;
442 if(nofrom == 0){
443 write(ofd, buf, n);
444 return;
447 /* need to escape leading From lines to avoid corrupting 'outgoing' mailbox */
448 for(p=buf; *p; p+=m){
449 from = cistrstr(p, "from");
450 if(from == nil)
451 m = n;
452 else
453 m = from - p;
454 if(m > 0)
455 write(ofd, p, m);
456 if(from){
457 if(p==buf || from[-1]=='\n')
458 write(ofd, " ", 1); /* escape with space if From is at start of line */
459 write(ofd, from, 4);
460 m += 4;
462 n -= m;
466 void
467 mesgsend(Message *m)
469 char *s, *body, *to;
470 int i, j, h, n, natt, p[2];
471 struct Exec *e;
472 Channel *sync;
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 */
478 nto = 0;
479 natt = 0;
480 ncc = 0;
481 nbcc = 0;
482 first = 1;
483 to = body;
484 for(;;){
485 for(s=to; *s!='\n'; s++)
486 if(*s == '\0'){
487 free(body);
488 return;
490 if(s++ == to) /* blank line */
491 break;
492 /* make copy of line to tokenize */
493 copy = emalloc(s-to);
494 memmove(copy, to, s-to);
495 copy[s-to-1] = '\0';
496 nfld = tokenizec(copy, fld, nelem(fld), ", \t");
497 if(nfld == 0){
498 free(copy);
499 break;
501 n -= s-to;
502 switch(h = whichheader(fld[0])){
503 case TO:
504 case FROM:
505 delit = 1;
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]);
510 break;
511 case BCC:
512 delit = 1;
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]);
517 break;
518 case CC:
519 delit = 1;
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]);
524 break;
525 case ATTACH:
526 case INCLUDE:
527 delit = 1;
528 for(i=1; i<nfld && natt<nelem(attlist); i++){
529 attlist[natt] = estrdup(fld[i]);
530 included[natt++] = (h == INCLUDE);
532 break;
533 default:
534 if(first){
535 delit = 1;
536 for(i=0; i<nfld && nto<nelem(tolist); i++)
537 tolist[nto++] = estrdup(fld[i]);
538 }else /* ignore it */
539 delit = 0;
540 break;
542 if(delit){
543 /* delete line from body */
544 memmove(to, s, n+1);
545 }else
546 to = s;
547 free(copy);
548 first = 0;
551 ofd = open(outgoing, OWRITE|OCEXEC); /* no error check necessary */
552 if(ofd > 0){
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++)
559 if(included[i])
560 fprint(ofd, "Include: %s\n", attlist[i]);
561 else
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));
569 if(pipe(p) < 0)
570 error("can't create pipe: %r");
571 e->p[0] = p[0];
572 e->p[1] = p[1];
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");
577 j = 2;
578 if(m->replyname){
579 e->argv[j++] = estrdup("-R");
580 e->argv[j++] = estrstrdup(mbox.name, m->replyname);
582 for(i=0; i<natt; i++){
583 if(included[i])
584 e->argv[j++] = estrdup("-A");
585 else
586 e->argv[j++] = estrdup("-a");
587 e->argv[j++] = estrdup(attlist[i]);
589 sync = chancreate(sizeof(int), 0);
590 e->sync = sync;
591 proccreate(execproc, e, EXECSTACK);
592 recvul(sync);
593 // close(p[0]);
595 /* using marshal -8, so generate rfc822 headers */
596 if(nto > 0){
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]);
602 if(ncc > 0){
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]);
608 if(nbcc > 0){
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]);
615 i = strlen(body);
616 if(i > 0)
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 */
626 if(ofd>0 && natt>0){
627 for(i=0; i<natt; i++)
628 if(included[i])
629 fprint(ofd, "=====> Include: %s\n", attlist[i]);
630 else
631 fprint(ofd, "=====> Attach: %s\n", attlist[i]);
633 if(ofd > 0)
634 write(ofd, "\n", 1);
636 for(i=0; i<natt; i++)
637 free(attlist[i]);
638 close(ofd);
639 close(p[1]);
640 free(body);
642 if(m->replyname != nil)
643 mesgmenumark(mbox.w, m->replyname, "\t[replied]");
644 if(m->name[0] == '/')
645 s = estrdup(m->name);
646 else
647 s = estrstrdup(mbox.name, m->name);
648 s = egrow(s, "-R", nil);
649 winname(m->w, s);
650 free(s);
651 winclean(m->w);
652 /* mark message unopened because it's no longer the original message */
653 m->opened = 0;