reply.c 10 KB

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