reply.c 11 KB

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