reply.c 11 KB

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