collect.c revision 88227
11897Swollman/* 299979Salfred * Copyright (c) 1980, 1993 399979Salfred * The Regents of the University of California. All rights reserved. 499979Salfred * 51897Swollman * Redistribution and use in source and binary forms, with or without 61897Swollman * modification, are permitted provided that the following conditions 71897Swollman * are met: 81897Swollman * 1. Redistributions of source code must retain the above copyright 91897Swollman * notice, this list of conditions and the following disclaimer. 101897Swollman * 2. Redistributions in binary form must reproduce the above copyright 11100441Scharnier * notice, this list of conditions and the following disclaimer in the 121897Swollman * documentation and/or other materials provided with the distribution. 131897Swollman * 3. All advertising materials mentioning features or use of this software 141897Swollman * must display the following acknowledgement: 15100441Scharnier * This product includes software developed by the University of 161897Swollman * California, Berkeley and its contributors. 171897Swollman * 4. Neither the name of the University nor the names of its contributors 181897Swollman * may be used to endorse or promote products derived from this software 19100441Scharnier * without specific prior written permission. 201897Swollman * 211897Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221897Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23100441Scharnier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241897Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251897Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261897Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27100441Scharnier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281897Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291897Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301897Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311897Swollman * SUCH DAMAGE. 3299979Salfred */ 331897Swollman 3412798Swpaul#ifndef lint 3512798Swpaul#if 0 3612798Swpaulstatic char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94"; 3712798Swpaul#endif 3812798Swpaulstatic const char rcsid[] = 3912798Swpaul "$FreeBSD: head/usr.bin/mail/collect.c 88227 2001-12-19 21:50:22Z ache $"; 4012798Swpaul#endif /* not lint */ 4112798Swpaul 4212798Swpaul/* 4312798Swpaul * Mail -- a mail program 4412798Swpaul * 4512798Swpaul * Collect input from standard input, handling 4612798Swpaul * ~ escapes. 4712798Swpaul */ 4812798Swpaul 4912798Swpaul#include "rcv.h" 5012798Swpaul#include "extern.h" 5112798Swpaul 5212798Swpaul/* 53100441Scharnier * Read a message from standard output and return a read file to it 5412798Swpaul * or NULL on error. 55100441Scharnier */ 5612798Swpaul 5712798Swpaul/* 5812798Swpaul * The following hokiness with global variables is so that on 5912798Swpaul * receipt of an interrupt signal, the partial message can be salted 6012798Swpaul * away on dead.letter. 61100441Scharnier */ 6212798Swpaul 6312798Swpaulstatic sig_t saveint; /* Previous SIGINT value */ 6412798Swpaulstatic sig_t savehup; /* Previous SIGHUP value */ 651897Swollmanstatic sig_t savetstp; /* Previous SIGTSTP value */ 66100441Scharnierstatic sig_t savettou; /* Previous SIGTTOU value */ 671897Swollmanstatic sig_t savettin; /* Previous SIGTTIN value */ 6812798Swpaulstatic FILE *collf; /* File for saving away */ 6912798Swpaulstatic int hadintr; /* Have seen one SIGINT so far */ 701897Swollman 71100441Scharnierstatic jmp_buf colljmp; /* To get back to work */ 721897Swollmanstatic int colljmp_p; /* whether to long jump */ 7312798Swpaulstatic jmp_buf collabort; /* To end collection with error */ 7412798Swpaul 751897SwollmanFILE * 761897Swollmancollect(hp, printheaders) 7712798Swpaul struct header *hp; 781897Swollman int printheaders; 791897Swollman{ 801897Swollman FILE *fbuf; 811897Swollman int lc, cc, escape, eofcount, fd, c, t; 8212798Swpaul char linebuf[LINESIZE], tempname[PATHSIZE], *cp, getsub; 83152398Sdwmalone sigset_t nset; 8412798Swpaul int longline, lastlong, rc; /* So we don't make 2 or more lines 8512798Swpaul out of a long input line. */ 8612798Swpaul 8712798Swpaul collf = NULL; 8812798Swpaul /* 8912798Swpaul * Start catching signals from here, but we're still die on interrupts 9012798Swpaul * until we're in the main loop. 9112798Swpaul */ 9212798Swpaul (void)sigemptyset(&nset); 9312798Swpaul (void)sigaddset(&nset, SIGINT); 9412798Swpaul (void)sigaddset(&nset, SIGHUP); 9512798Swpaul (void)sigprocmask(SIG_BLOCK, &nset, NULL); 9612798Swpaul if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN) 9712798Swpaul (void)signal(SIGINT, collint); 9812798Swpaul if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN) 9912798Swpaul (void)signal(SIGHUP, collhup); 100152398Sdwmalone savetstp = signal(SIGTSTP, collstop); 101152398Sdwmalone savettou = signal(SIGTTOU, collstop); 10212798Swpaul savettin = signal(SIGTTIN, collstop); 10312798Swpaul if (setjmp(collabort) || setjmp(colljmp)) { 10412798Swpaul (void)rm(tempname); 10512798Swpaul goto err; 10612798Swpaul } 1071897Swollman (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 108100441Scharnier 1091897Swollman noreset++; 11012798Swpaul (void)snprintf(tempname, sizeof(tempname), 1111897Swollman "%s/mail.RsXXXXXXXXXX", tmpdir); 1121897Swollman if ((fd = mkstemp(tempname)) == -1 || 1131897Swollman (collf = Fdopen(fd, "w+")) == NULL) { 114152398Sdwmalone warn("%s", tempname); 1151897Swollman goto err; 116152398Sdwmalone } 1171897Swollman (void)rm(tempname); 1181897Swollman 1191897Swollman /* 1201897Swollman * If we are going to prompt for a subject, 1211897Swollman * refrain from printing a newline after 12212798Swpaul * the headers (since some people mind). 12312798Swpaul */ 12412798Swpaul t = GTO|GSUBJECT|GCC|GNL; 12512798Swpaul getsub = 0; 12612798Swpaul if (hp->h_subject == NULL && value("interactive") != NULL && 1271897Swollman (value("ask") != NULL || value("asksub") != NULL)) 12812798Swpaul t &= ~GNL, getsub++; 1291897Swollman if (printheaders) { 13012798Swpaul puthead(hp, stdout, t); 131100441Scharnier (void)fflush(stdout); 13212798Swpaul } 13312798Swpaul if ((cp = value("escape")) != NULL) 13412798Swpaul escape = *cp; 13512798Swpaul else 13612798Swpaul escape = ESCAPE; 137149680Sstefanf eofcount = 0; 13812798Swpaul hadintr = 0; 13912798Swpaul lastlong = 0; 14012798Swpaul longline = 0; 14112798Swpaul 14212798Swpaul if (!setjmp(colljmp)) { 14312798Swpaul if (getsub) 14412798Swpaul grabh(hp, GSUBJECT); 14512798Swpaul } else { 14612798Swpaul /* 14712798Swpaul * Come here for printing the after-signal message. 14812798Swpaul * Duplicate messages won't be printed because 14912798Swpaul * the write is aborted if we get a SIGTTOU. 15012798Swpaul */ 15112798Swpaulcont: 152100441Scharnier if (hadintr) { 15312798Swpaul (void)fflush(stdout); 154149682Sstefanf fprintf(stderr, 155149682Sstefanf "\n(Interrupt -- one more to kill letter)\n"); 156152398Sdwmalone } else { 157149682Sstefanf printf("(continue)\n"); 158100441Scharnier (void)fflush(stdout); 159100441Scharnier } 160152398Sdwmalone } 161152398Sdwmalone for (;;) { 1621897Swollman colljmp_p = 1; 16312798Swpaul c = readline(stdin, linebuf, LINESIZE); 16412798Swpaul colljmp_p = 0; 1651897Swollman if (c < 0) { 166152398Sdwmalone if (value("interactive") != NULL && 1671897Swollman value("ignoreeof") != NULL && ++eofcount < 25) { 16812798Swpaul printf("Use \".\" to terminate letter\n"); 16912798Swpaul continue; 1701897Swollman } 171152398Sdwmalone break; 172152398Sdwmalone } 173152398Sdwmalone lastlong = longline; 174152398Sdwmalone longline = c == LINESIZE - 1; 175152398Sdwmalone eofcount = 0; 176152398Sdwmalone hadintr = 0; 177152398Sdwmalone if (linebuf[0] == '.' && linebuf[1] == '\0' && 178152398Sdwmalone value("interactive") != NULL && !lastlong && 179152398Sdwmalone (value("dot") != NULL || value("ignoreeof") != NULL)) 180149682Sstefanf break; 181149682Sstefanf if (linebuf[0] != escape || value("interactive") == NULL || 182149682Sstefanf lastlong) { 183149682Sstefanf if (putline(collf, linebuf, !longline) < 0) 184152398Sdwmalone goto err; 185152398Sdwmalone continue; 186149682Sstefanf } 1871897Swollman c = linebuf[1]; 188100441Scharnier switch (c) { 1891897Swollman default: 190149682Sstefanf /* 1911897Swollman * On double escape, just send the single one. 1921897Swollman * Otherwise, it's an error. 193100441Scharnier */ 1941897Swollman if (c == escape) { 195152398Sdwmalone if (putline(collf, &linebuf[1], !longline) < 0) 196149695Sstefanf goto err; 197149695Sstefanf else 198152398Sdwmalone break; 1991897Swollman } 2001897Swollman printf("Unknown tilde escape.\n"); 201100441Scharnier break; 2021897Swollman case 'C': 203152398Sdwmalone /* 204149682Sstefanf * Dump core. 205152398Sdwmalone */ 206149682Sstefanf core(); 207152398Sdwmalone break; 208152398Sdwmalone case '!': 209152398Sdwmalone /* 210152398Sdwmalone * Shell escape, send the balance of the 211149682Sstefanf * line to sh -c. 2121897Swollman */ 2131897Swollman shell(&linebuf[2]); 2141897Swollman break; 215149682Sstefanf case ':': 216152398Sdwmalone /* 217152398Sdwmalone * Escape to command mode, but be nice! 21812798Swpaul */ 21912798Swpaul execute(&linebuf[2], 1); 22012798Swpaul goto cont; 22112798Swpaul case '.': 222149682Sstefanf /* 223152398Sdwmalone * Simulate end of file on input. 224152398Sdwmalone */ 225152398Sdwmalone goto out; 226152398Sdwmalone case 'q': 227152398Sdwmalone /* 228152398Sdwmalone * Force a quit of sending mail. 229152398Sdwmalone * Act like an interrupt happened. 230152398Sdwmalone */ 231 hadintr++; 232 collint(SIGINT); 233 exit(1); 234 case 'h': 235 /* 236 * Grab a bunch of headers. 237 */ 238 grabh(hp, GTO|GSUBJECT|GCC|GBCC); 239 goto cont; 240 case 't': 241 /* 242 * Add to the To list. 243 */ 244 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO)); 245 break; 246 case 's': 247 /* 248 * Set the Subject line. 249 */ 250 cp = &linebuf[2]; 251 while (isspace((unsigned char)*cp)) 252 cp++; 253 hp->h_subject = savestr(cp); 254 break; 255 case 'R': 256 /* 257 * Set the Reply-To line. 258 */ 259 cp = &linebuf[2]; 260 while (isspace((unsigned char)*cp)) 261 cp++; 262 hp->h_replyto = savestr(cp); 263 break; 264 case 'c': 265 /* 266 * Add to the CC list. 267 */ 268 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC)); 269 break; 270 case 'b': 271 /* 272 * Add stuff to blind carbon copies list. 273 */ 274 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC)); 275 break; 276 case 'd': 277 if (strlcpy(linebuf + 2, getdeadletter(), sizeof(linebuf) - 2) 278 >= sizeof(linebuf) - 2) { 279 printf("Line buffer overflow\n"); 280 break; 281 } 282 /* fall into . . . */ 283 case 'r': 284 /* 285 * Invoke a file: 286 * Search for the file name, 287 * then open it and copy the contents to collf. 288 */ 289 cp = &linebuf[2]; 290 while (isspace((unsigned char)*cp)) 291 cp++; 292 if (*cp == '\0') { 293 printf("Interpolate what file?\n"); 294 break; 295 } 296 cp = expand(cp); 297 if (cp == NULL) 298 break; 299 if (isdir(cp)) { 300 printf("%s: Directory\n", cp); 301 break; 302 } 303 if ((fbuf = Fopen(cp, "r")) == NULL) { 304 warn("%s", cp); 305 break; 306 } 307 printf("\"%s\" ", cp); 308 (void)fflush(stdout); 309 lc = 0; 310 cc = 0; 311 while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) { 312 if (rc != LINESIZE - 1) 313 lc++; 314 if ((t = putline(collf, linebuf, 315 rc != LINESIZE - 1)) < 0) { 316 (void)Fclose(fbuf); 317 goto err; 318 } 319 cc += t; 320 } 321 (void)Fclose(fbuf); 322 printf("%d/%d\n", lc, cc); 323 break; 324 case 'w': 325 /* 326 * Write the message on a file. 327 */ 328 cp = &linebuf[2]; 329 while (*cp == ' ' || *cp == '\t') 330 cp++; 331 if (*cp == '\0') { 332 fprintf(stderr, "Write what file!?\n"); 333 break; 334 } 335 if ((cp = expand(cp)) == NULL) 336 break; 337 rewind(collf); 338 exwrite(cp, collf, 1); 339 break; 340 case 'm': 341 case 'M': 342 case 'f': 343 case 'F': 344 /* 345 * Interpolate the named messages, if we 346 * are in receiving mail mode. Does the 347 * standard list processing garbage. 348 * If ~f is given, we don't shift over. 349 */ 350 if (forward(linebuf + 2, collf, tempname, c) < 0) 351 goto err; 352 goto cont; 353 case '?': 354 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) { 355 warn("%s", _PATH_TILDE); 356 break; 357 } 358 while ((t = getc(fbuf)) != EOF) 359 (void)putchar(t); 360 (void)Fclose(fbuf); 361 break; 362 case 'p': 363 /* 364 * Print out the current state of the 365 * message without altering anything. 366 */ 367 rewind(collf); 368 printf("-------\nMessage contains:\n"); 369 puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL); 370 while ((t = getc(collf)) != EOF) 371 (void)putchar(t); 372 goto cont; 373 case '|': 374 /* 375 * Pipe message through command. 376 * Collect output as new message. 377 */ 378 rewind(collf); 379 mespipe(collf, &linebuf[2]); 380 goto cont; 381 case 'v': 382 case 'e': 383 /* 384 * Edit the current message. 385 * 'e' means to use EDITOR 386 * 'v' means to use VISUAL 387 */ 388 rewind(collf); 389 mesedit(collf, c); 390 goto cont; 391 } 392 } 393 goto out; 394err: 395 if (collf != NULL) { 396 (void)Fclose(collf); 397 collf = NULL; 398 } 399out: 400 if (collf != NULL) 401 rewind(collf); 402 noreset--; 403 (void)sigprocmask(SIG_BLOCK, &nset, NULL); 404 (void)signal(SIGINT, saveint); 405 (void)signal(SIGHUP, savehup); 406 (void)signal(SIGTSTP, savetstp); 407 (void)signal(SIGTTOU, savettou); 408 (void)signal(SIGTTIN, savettin); 409 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 410 return (collf); 411} 412 413/* 414 * Write a file, ex-like if f set. 415 */ 416int 417exwrite(name, fp, f) 418 char name[]; 419 FILE *fp; 420 int f; 421{ 422 FILE *of; 423 int c, lc; 424 long cc; 425 struct stat junk; 426 427 if (f) { 428 printf("\"%s\" ", name); 429 (void)fflush(stdout); 430 } 431 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) { 432 if (!f) 433 fprintf(stderr, "%s: ", name); 434 fprintf(stderr, "File exists\n"); 435 return (-1); 436 } 437 if ((of = Fopen(name, "w")) == NULL) { 438 warn((char *)NULL); 439 return (-1); 440 } 441 lc = 0; 442 cc = 0; 443 while ((c = getc(fp)) != EOF) { 444 cc++; 445 if (c == '\n') 446 lc++; 447 (void)putc(c, of); 448 if (ferror(of)) { 449 warnx("%s", name); 450 (void)Fclose(of); 451 return (-1); 452 } 453 } 454 (void)Fclose(of); 455 printf("%d/%ld\n", lc, cc); 456 (void)fflush(stdout); 457 return (0); 458} 459 460/* 461 * Edit the message being collected on fp. 462 * On return, make the edit file the new temp file. 463 */ 464void 465mesedit(fp, c) 466 FILE *fp; 467 int c; 468{ 469 sig_t sigint = signal(SIGINT, SIG_IGN); 470 FILE *nf = run_editor(fp, (off_t)-1, c, 0); 471 472 if (nf != NULL) { 473 (void)fseeko(nf, (off_t)0, SEEK_END); 474 collf = nf; 475 (void)Fclose(fp); 476 } 477 (void)signal(SIGINT, sigint); 478} 479 480/* 481 * Pipe the message through the command. 482 * Old message is on stdin of command; 483 * New message collected from stdout. 484 * Sh -c must return 0 to accept the new message. 485 */ 486void 487mespipe(fp, cmd) 488 FILE *fp; 489 char cmd[]; 490{ 491 FILE *nf; 492 int fd; 493 sig_t sigint = signal(SIGINT, SIG_IGN); 494 char *sh, tempname[PATHSIZE]; 495 496 (void)snprintf(tempname, sizeof(tempname), 497 "%s/mail.ReXXXXXXXXXX", tmpdir); 498 if ((fd = mkstemp(tempname)) == -1 || 499 (nf = Fdopen(fd, "w+")) == NULL) { 500 warn("%s", tempname); 501 goto out; 502 } 503 (void)rm(tempname); 504 /* 505 * stdin = current message. 506 * stdout = new message. 507 */ 508 if ((sh = value("SHELL")) == NULL) 509 sh = _PATH_CSHELL; 510 if (run_command(sh, 511 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) { 512 (void)Fclose(nf); 513 goto out; 514 } 515 if (fsize(nf) == 0) { 516 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd); 517 (void)Fclose(nf); 518 goto out; 519 } 520 /* 521 * Take new files. 522 */ 523 (void)fseeko(nf, (off_t)0, SEEK_END); 524 collf = nf; 525 (void)Fclose(fp); 526out: 527 (void)signal(SIGINT, sigint); 528} 529 530/* 531 * Interpolate the named messages into the current 532 * message, preceding each line with a tab. 533 * Return a count of the number of characters now in 534 * the message, or -1 if an error is encountered writing 535 * the message temporary. The flag argument is 'm' if we 536 * should shift over and 'f' if not. 537 */ 538int 539forward(ms, fp, fn, f) 540 char ms[]; 541 FILE *fp; 542 char *fn; 543 int f; 544{ 545 int *msgvec; 546 struct ignoretab *ig; 547 char *tabst; 548 549 msgvec = (int *)salloc((msgCount+1) * sizeof(*msgvec)); 550 if (msgvec == NULL) 551 return (0); 552 if (getmsglist(ms, msgvec, 0) < 0) 553 return (0); 554 if (*msgvec == 0) { 555 *msgvec = first(0, MMNORM); 556 if (*msgvec == 0) { 557 printf("No appropriate messages\n"); 558 return (0); 559 } 560 msgvec[1] = 0; 561 } 562 if (f == 'f' || f == 'F') 563 tabst = NULL; 564 else if ((tabst = value("indentprefix")) == NULL) 565 tabst = "\t"; 566 ig = isupper((unsigned char)f) ? NULL : ignore; 567 printf("Interpolating:"); 568 for (; *msgvec != 0; msgvec++) { 569 struct message *mp = message + *msgvec - 1; 570 571 touch(mp); 572 printf(" %d", *msgvec); 573 if (sendmessage(mp, fp, ig, tabst) < 0) { 574 warnx("%s", fn); 575 return (-1); 576 } 577 } 578 printf("\n"); 579 return (0); 580} 581 582/* 583 * Print (continue) when continued after ^Z. 584 */ 585/*ARGSUSED*/ 586void 587collstop(s) 588 int s; 589{ 590 sig_t old_action = signal(s, SIG_DFL); 591 sigset_t nset; 592 593 (void)sigemptyset(&nset); 594 (void)sigaddset(&nset, s); 595 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 596 (void)kill(0, s); 597 (void)sigprocmask(SIG_BLOCK, &nset, NULL); 598 (void)signal(s, old_action); 599 if (colljmp_p) { 600 colljmp_p = 0; 601 hadintr = 0; 602 longjmp(colljmp, 1); 603 } 604} 605 606/* 607 * On interrupt, come here to save the partial message in ~/dead.letter. 608 * Then jump out of the collection loop. 609 */ 610/*ARGSUSED*/ 611void 612collint(s) 613 int s; 614{ 615 /* 616 * the control flow is subtle, because we can be called from ~q. 617 */ 618 if (!hadintr) { 619 if (value("ignore") != NULL) { 620 printf("@"); 621 (void)fflush(stdout); 622 clearerr(stdin); 623 return; 624 } 625 hadintr = 1; 626 longjmp(colljmp, 1); 627 } 628 rewind(collf); 629 if (value("nosave") == NULL) 630 savedeadletter(collf); 631 longjmp(collabort, 1); 632} 633 634/*ARGSUSED*/ 635void 636collhup(s) 637 int s; 638{ 639 rewind(collf); 640 savedeadletter(collf); 641 /* 642 * Let's pretend nobody else wants to clean up, 643 * a true statement at this time. 644 */ 645 exit(1); 646} 647 648void 649savedeadletter(fp) 650 FILE *fp; 651{ 652 FILE *dbuf; 653 int c; 654 char *cp; 655 656 if (fsize(fp) == 0) 657 return; 658 cp = getdeadletter(); 659 c = umask(077); 660 dbuf = Fopen(cp, "a"); 661 (void)umask(c); 662 if (dbuf == NULL) 663 return; 664 while ((c = getc(fp)) != EOF) 665 (void)putc(c, dbuf); 666 (void)Fclose(dbuf); 667 rewind(fp); 668} 669