1 /* $NetBSD: collect.c,v 1.26 2002/03/05 21:29:30 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94"; 40 #else 41 __RCSID("$NetBSD: collect.c,v 1.26 2002/03/05 21:29:30 wiz Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 /* 46 * Mail -- a mail program 47 * 48 * Collect input from standard input, handling 49 * ~ escapes. 50 */ 51 52 #include "rcv.h" 53 #include "extern.h" 54 55 extern char *tempMail; 56 extern char *tempEdit; 57 58 /* 59 * Read a message from standard output and return a read file to it 60 * or NULL on error. 61 */ 62 63 /* 64 * The following hokiness with global variables is so that on 65 * receipt of an interrupt signal, the partial message can be salted 66 * away on dead.letter. 67 */ 68 69 static sig_t saveint; /* Previous SIGINT value */ 70 static sig_t savehup; /* Previous SIGHUP value */ 71 static sig_t savetstp; /* Previous SIGTSTP value */ 72 static sig_t savettou; /* Previous SIGTTOU value */ 73 static sig_t savettin; /* Previous SIGTTIN value */ 74 static FILE *collf; /* File for saving away */ 75 static int hadintr; /* Have seen one SIGINT so far */ 76 77 static jmp_buf colljmp; /* To get back to work */ 78 static int colljmp_p; /* whether to long jump */ 79 static jmp_buf collabort; /* To end collection with error */ 80 81 FILE * 82 collect(struct header *hp, int printheaders) 83 { 84 FILE *fbuf; 85 int lc, cc, escape, eofcount; 86 int c, t; 87 char linebuf[LINESIZE], *cp; 88 char getsub; 89 sigset_t nset; 90 int longline, lastlong, rc; /* So we don't make 2 or more lines 91 out of a long input line. */ 92 #if __GNUC__ 93 /* Avoid longjmp clobbering */ 94 (void)&escape; 95 (void)&eofcount; 96 (void)&getsub; 97 (void)&longline; 98 #endif 99 100 collf = NULL; 101 /* 102 * Start catching signals from here, but we're still die on interrupts 103 * until we're in the main loop. 104 */ 105 sigemptyset(&nset); 106 sigaddset(&nset, SIGINT); 107 sigaddset(&nset, SIGHUP); 108 sigprocmask(SIG_BLOCK, &nset, NULL); 109 if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN) 110 signal(SIGINT, collint); 111 if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN) 112 signal(SIGHUP, collhup); 113 savetstp = signal(SIGTSTP, collstop); 114 savettou = signal(SIGTTOU, collstop); 115 savettin = signal(SIGTTIN, collstop); 116 if (setjmp(collabort) || setjmp(colljmp)) { 117 rm(tempMail); 118 goto err; 119 } 120 sigprocmask(SIG_UNBLOCK, &nset, NULL); 121 122 noreset++; 123 if ((collf = Fopen(tempMail, "w+")) == NULL) { 124 warn("%s", tempMail); 125 goto err; 126 } 127 unlink(tempMail); 128 129 /* 130 * If we are going to prompt for a subject, 131 * refrain from printing a newline after 132 * the headers (since some people mind). 133 */ 134 t = GTO|GSUBJECT|GCC|GNL; 135 getsub = 0; 136 if (hp->h_subject == NULL && value("interactive") != NULL && 137 (value("ask") != NULL || value("asksub") != NULL)) 138 t &= ~GNL, getsub++; 139 if (printheaders) { 140 puthead(hp, stdout, t); 141 fflush(stdout); 142 } 143 if ((cp = value("escape")) != NULL) 144 escape = *cp; 145 else 146 escape = ESCAPE; 147 eofcount = 0; 148 hadintr = 0; 149 lastlong = 0; 150 longline = 0; 151 152 if (!setjmp(colljmp)) { 153 if (getsub) 154 grabh(hp, GSUBJECT); 155 } else { 156 /* 157 * Come here for printing the after-signal message. 158 * Duplicate messages won't be printed because 159 * the write is aborted if we get a SIGTTOU. 160 */ 161 cont: 162 if (hadintr) { 163 fflush(stdout); 164 fprintf(stderr, 165 "\n(Interrupt -- one more to kill letter)\n"); 166 } else { 167 printf("(continue)\n"); 168 fflush(stdout); 169 } 170 } 171 for (;;) { 172 colljmp_p = 1; 173 c = readline(stdin, linebuf, LINESIZE); 174 colljmp_p = 0; 175 if (c < 0) { 176 if (value("interactive") != NULL && 177 value("ignoreeof") != NULL && ++eofcount < 25) { 178 printf("Use \".\" to terminate letter\n"); 179 continue; 180 } 181 break; 182 } 183 lastlong = longline; 184 longline = c == LINESIZE-1; 185 eofcount = 0; 186 hadintr = 0; 187 if (linebuf[0] == '.' && linebuf[1] == '\0' && 188 value("interactive") != NULL && !lastlong && 189 (value("dot") != NULL || value("ignoreeof") != NULL)) 190 break; 191 if (linebuf[0] != escape || value("interactive") == NULL || 192 lastlong) { 193 if (putline(collf, linebuf, !longline) < 0) 194 goto err; 195 continue; 196 } 197 c = linebuf[1]; 198 switch (c) { 199 default: 200 /* 201 * On double escape, just send the single one. 202 * Otherwise, it's an error. 203 */ 204 if (c == escape) { 205 if (putline(collf, &linebuf[1], !longline) < 0) 206 goto err; 207 else 208 break; 209 } 210 printf("Unknown tilde escape.\n"); 211 break; 212 case 'C': 213 /* 214 * Dump core. 215 */ 216 core(NULL); 217 break; 218 case '!': 219 /* 220 * Shell escape, send the balance of the 221 * line to sh -c. 222 */ 223 shell(&linebuf[2]); 224 break; 225 case ':': 226 case '_': 227 /* 228 * Escape to command mode, but be nice! 229 */ 230 execute(&linebuf[2], 1); 231 goto cont; 232 case '.': 233 /* 234 * Simulate end of file on input. 235 */ 236 goto out; 237 case 'q': 238 /* 239 * Force a quit of sending mail. 240 * Act like an interrupt happened. 241 */ 242 hadintr++; 243 collint(SIGINT); 244 exit(1); 245 246 case 'x': /* exit, do not save in dead.letter */ 247 goto err; 248 249 case 'h': 250 /* 251 * Grab a bunch of headers. 252 */ 253 grabh(hp, GTO|GSUBJECT|GCC|GBCC); 254 goto cont; 255 case 't': 256 /* 257 * Add to the To list. 258 */ 259 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO)); 260 break; 261 case 's': 262 /* 263 * Set the Subject list. 264 */ 265 cp = &linebuf[2]; 266 while (isspace((unsigned char)*cp)) 267 cp++; 268 hp->h_subject = savestr(cp); 269 break; 270 case 'c': 271 /* 272 * Add to the CC list. 273 */ 274 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC)); 275 break; 276 case 'b': 277 /* 278 * Add stuff to blind carbon copies list. 279 */ 280 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC)); 281 break; 282 case 'i': 283 case 'A': 284 case 'a': 285 /* 286 * Insert named variable in message 287 */ 288 289 switch(c) { 290 case 'i': 291 cp = &linebuf[2]; 292 while(isspace((unsigned char) *cp)) 293 cp++; 294 295 break; 296 case 'a': 297 cp = "sign"; 298 break; 299 case 'A': 300 cp = "Sign"; 301 break; 302 default: 303 goto err; 304 } 305 306 if(*cp && (cp = value(cp)) != NULL) { 307 printf("%s\n", cp); 308 if(putline(collf, cp, 1) < 0) 309 goto err; 310 } 311 312 break; 313 314 case 'd': 315 strcpy(linebuf + 2, getdeadletter()); 316 /* fall into . . . */ 317 case 'r': 318 case '<': 319 /* 320 * Invoke a file: 321 * Search for the file name, 322 * then open it and copy the contents to collf. 323 */ 324 cp = &linebuf[2]; 325 while (isspace((unsigned char)*cp)) 326 cp++; 327 if (*cp == '\0') { 328 printf("Interpolate what file?\n"); 329 break; 330 } 331 332 cp = expand(cp); 333 if (cp == NULL) 334 break; 335 336 if (*cp == '!') { /* insert stdout of command */ 337 char *shellcmd; 338 int nullfd; 339 int rc2; 340 341 if((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) { 342 warn("/dev/null"); 343 break; 344 } 345 346 if ((fbuf = Fopen(tempEdit, "w+")) == NULL) { 347 warn("%s", tempEdit); 348 break; 349 } 350 (void)unlink(tempEdit); 351 352 if ((shellcmd = value("SHELL")) == NULL) 353 shellcmd = _PATH_CSHELL; 354 355 rc2 = run_command(shellcmd, 0, nullfd, fileno(fbuf), "-c", cp+1, NULL); 356 357 close(nullfd); 358 359 if (rc2 < 0) { 360 (void)Fclose(fbuf); 361 break; 362 } 363 364 if (fsize(fbuf) == 0) { 365 fprintf(stderr, "No bytes from command \"%s\"\n", cp+1); 366 (void)Fclose(fbuf); 367 break; 368 } 369 370 rewind(fbuf); 371 } 372 else if (isdir(cp)) { 373 printf("%s: Directory\n", cp); 374 break; 375 } 376 else if ((fbuf = Fopen(cp, "r")) == NULL) { 377 warn("%s", cp); 378 break; 379 } 380 printf("\"%s\" ", cp); 381 fflush(stdout); 382 lc = 0; 383 cc = 0; 384 while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) { 385 if (rc != LINESIZE-1) lc++; 386 if ((t = putline(collf, linebuf, 387 rc != LINESIZE-1)) < 0) { 388 Fclose(fbuf); 389 goto err; 390 } 391 cc += t; 392 } 393 Fclose(fbuf); 394 printf("%d/%d\n", lc, cc); 395 break; 396 case 'w': 397 /* 398 * Write the message on a file. 399 */ 400 cp = &linebuf[2]; 401 while (*cp == ' ' || *cp == '\t') 402 cp++; 403 if (*cp == '\0') { 404 fprintf(stderr, "Write what file!?\n"); 405 break; 406 } 407 if ((cp = expand(cp)) == NULL) 408 break; 409 rewind(collf); 410 exwrite(cp, collf, 1); 411 break; 412 case 'm': 413 case 'M': 414 case 'f': 415 case 'F': 416 /* 417 * Interpolate the named messages, if we 418 * are in receiving mail mode. Does the 419 * standard list processing garbage. 420 * If ~f is given, we don't shift over. 421 */ 422 if (forward(linebuf + 2, collf, c) < 0) 423 goto err; 424 goto cont; 425 case '?': 426 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) { 427 warn(_PATH_TILDE); 428 break; 429 } 430 while ((t = getc(fbuf)) != EOF) 431 (void)putchar(t); 432 Fclose(fbuf); 433 break; 434 case 'p': 435 /* 436 * Print out the current state of the 437 * message without altering anything. 438 */ 439 rewind(collf); 440 printf("-------\nMessage contains:\n"); 441 puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL); 442 while ((t = getc(collf)) != EOF) 443 (void)putchar(t); 444 goto cont; 445 case '|': 446 /* 447 * Pipe message through command. 448 * Collect output as new message. 449 */ 450 rewind(collf); 451 mespipe(collf, &linebuf[2]); 452 goto cont; 453 case 'v': 454 case 'e': 455 /* 456 * Edit the current message. 457 * 'e' means to use EDITOR 458 * 'v' means to use VISUAL 459 */ 460 rewind(collf); 461 mesedit(collf, c); 462 goto cont; 463 } 464 } 465 goto out; 466 err: 467 if (collf != NULL) { 468 Fclose(collf); 469 collf = NULL; 470 } 471 out: 472 if (collf != NULL) 473 rewind(collf); 474 noreset--; 475 sigprocmask(SIG_BLOCK, &nset, NULL); 476 signal(SIGINT, saveint); 477 signal(SIGHUP, savehup); 478 signal(SIGTSTP, savetstp); 479 signal(SIGTTOU, savettou); 480 signal(SIGTTIN, savettin); 481 sigprocmask(SIG_UNBLOCK, &nset, NULL); 482 return collf; 483 } 484 485 /* 486 * Write a file, ex-like if f set. 487 */ 488 int 489 exwrite(char name[], FILE *fp, int f) 490 { 491 FILE *of; 492 int c; 493 long cc; 494 int lc; 495 struct stat junk; 496 497 if (f) { 498 printf("\"%s\" ", name); 499 fflush(stdout); 500 } 501 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) { 502 if (!f) 503 fprintf(stderr, "%s: ", name); 504 fprintf(stderr, "File exists\n"); 505 return(-1); 506 } 507 if ((of = Fopen(name, "w")) == NULL) { 508 warn("%s", name); 509 return(-1); 510 } 511 lc = 0; 512 cc = 0; 513 while ((c = getc(fp)) != EOF) { 514 cc++; 515 if (c == '\n') 516 lc++; 517 (void)putc(c, of); 518 if (ferror(of)) { 519 warn("%s", name); 520 Fclose(of); 521 return(-1); 522 } 523 } 524 Fclose(of); 525 printf("%d/%ld\n", lc, cc); 526 fflush(stdout); 527 return(0); 528 } 529 530 /* 531 * Edit the message being collected on fp. 532 * On return, make the edit file the new temp file. 533 */ 534 void 535 mesedit(FILE *fp, int c) 536 { 537 sig_t sigint = signal(SIGINT, SIG_IGN); 538 FILE *nf = run_editor(fp, (off_t)-1, c, 0); 539 540 if (nf != NULL) { 541 fseek(nf, 0L, 2); 542 collf = nf; 543 Fclose(fp); 544 } 545 (void)signal(SIGINT, sigint); 546 } 547 548 /* 549 * Pipe the message through the command. 550 * Old message is on stdin of command; 551 * New message collected from stdout. 552 * Sh -c must return 0 to accept the new message. 553 */ 554 void 555 mespipe(FILE *fp, char cmd[]) 556 { 557 FILE *nf; 558 sig_t sigint = signal(SIGINT, SIG_IGN); 559 char *shellcmd; 560 561 if ((nf = Fopen(tempEdit, "w+")) == NULL) { 562 warn("%s", tempEdit); 563 goto out; 564 } 565 (void)unlink(tempEdit); 566 /* 567 * stdin = current message. 568 * stdout = new message. 569 */ 570 if ((shellcmd = value("SHELL")) == NULL) 571 shellcmd = _PATH_CSHELL; 572 if (run_command(shellcmd, 573 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) { 574 (void)Fclose(nf); 575 goto out; 576 } 577 if (fsize(nf) == 0) { 578 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd); 579 (void)Fclose(nf); 580 goto out; 581 } 582 /* 583 * Take new files. 584 */ 585 (void)fseek(nf, 0L, 2); 586 collf = nf; 587 (void)Fclose(fp); 588 out: 589 (void)signal(SIGINT, sigint); 590 } 591 592 /* 593 * Interpolate the named messages into the current 594 * message, preceding each line with a tab. 595 * Return a count of the number of characters now in 596 * the message, or -1 if an error is encountered writing 597 * the message temporary. The flag argument is 'm' if we 598 * should shift over and 'f' if not. 599 */ 600 int 601 forward(char ms[], FILE *fp, int f) 602 { 603 int *msgvec; 604 struct ignoretab *ig; 605 char *tabst; 606 607 msgvec = (int *) salloc((msgCount+1) * sizeof *msgvec); 608 if (msgvec == (int *) NULL) 609 return(0); 610 if (getmsglist(ms, msgvec, 0) < 0) 611 return(0); 612 if (*msgvec == 0) { 613 *msgvec = first(0, MMNORM); 614 if (*msgvec == 0) { 615 printf("No appropriate messages\n"); 616 return(0); 617 } 618 msgvec[1] = 0; 619 } 620 if (f == 'f' || f == 'F') 621 tabst = NULL; 622 else if ((tabst = value("indentprefix")) == NULL) 623 tabst = "\t"; 624 ig = isupper(f) ? NULL : ignore; 625 printf("Interpolating:"); 626 for (; *msgvec != 0; msgvec++) { 627 struct message *mp = message + *msgvec - 1; 628 629 touch(mp); 630 printf(" %d", *msgvec); 631 if (sendmessage(mp, fp, ig, tabst) < 0) { 632 warn("%s", tempMail); 633 return(-1); 634 } 635 } 636 printf("\n"); 637 return(0); 638 } 639 640 /* 641 * Print (continue) when continued after ^Z. 642 */ 643 /*ARGSUSED*/ 644 void 645 collstop(int s) 646 { 647 sig_t old_action = signal(s, SIG_DFL); 648 sigset_t nset; 649 650 sigemptyset(&nset); 651 sigaddset(&nset, s); 652 sigprocmask(SIG_UNBLOCK, &nset, NULL); 653 kill(0, s); 654 sigprocmask(SIG_BLOCK, &nset, NULL); 655 signal(s, old_action); 656 if (colljmp_p) { 657 colljmp_p = 0; 658 hadintr = 0; 659 longjmp(colljmp, 1); 660 } 661 } 662 663 /* 664 * On interrupt, come here to save the partial message in ~/dead.letter. 665 * Then jump out of the collection loop. 666 */ 667 /*ARGSUSED*/ 668 void 669 collint(int s) 670 { 671 /* 672 * the control flow is subtle, because we can be called from ~q. 673 */ 674 if (!hadintr) { 675 if (value("ignore") != NULL) { 676 puts("@"); 677 fflush(stdout); 678 clearerr(stdin); 679 return; 680 } 681 hadintr = 1; 682 longjmp(colljmp, 1); 683 } 684 rewind(collf); 685 if (value("nosave") == NULL) 686 savedeadletter(collf); 687 longjmp(collabort, 1); 688 } 689 690 /*ARGSUSED*/ 691 void 692 collhup(int s) 693 { 694 rewind(collf); 695 savedeadletter(collf); 696 /* 697 * Let's pretend nobody else wants to clean up, 698 * a true statement at this time. 699 */ 700 exit(1); 701 } 702 703 void 704 savedeadletter(FILE *fp) 705 { 706 FILE *dbuf; 707 int c; 708 char *cp; 709 710 if (fsize(fp) == 0) 711 return; 712 cp = getdeadletter(); 713 c = umask(077); 714 dbuf = Fopen(cp, "a"); 715 (void)umask(c); 716 if (dbuf == NULL) 717 return; 718 while ((c = getc(fp)) != EOF) 719 (void)putc(c, dbuf); 720 Fclose(dbuf); 721 rewind(fp); 722 } 723