reply.c 12 KB

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