aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorRichard M. Stallman1990-11-13 05:11:43 +0000
committerRichard M. Stallman1990-11-13 05:11:43 +0000
commitb1ce62a86f40b1df8884256ccc7dda8c9db8ae84 (patch)
treed83193963fc791b081b743fb14616cd384e42dc0 /lib-src
parentecb759429709c58d1ec5e1865f74fd5979d83e2e (diff)
downloademacs-b1ce62a86f40b1df8884256ccc7dda8c9db8ae84.tar.gz
emacs-b1ce62a86f40b1df8884256ccc7dda8c9db8ae84.zip
*** empty log message ***
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/movemail.c478
1 files changed, 256 insertions, 222 deletions
diff --git a/lib-src/movemail.c b/lib-src/movemail.c
index 269b3b6b64f..028e5616f5a 100644
--- a/lib-src/movemail.c
+++ b/lib-src/movemail.c
@@ -104,9 +104,7 @@ main (argc, argv)
104 mmdf_init (argv[0]); 104 mmdf_init (argv[0]);
105#endif 105#endif
106 106
107 /* Check access to input and output file. */ 107 /* Check access to output file. */
108 if (access (inname, R_OK | W_OK) != 0)
109 pfatal_with_name (inname);
110 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0) 108 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
111 pfatal_with_name (outname); 109 pfatal_with_name (outname);
112 110
@@ -138,6 +136,10 @@ main (argc, argv)
138 setuid (getuid()); 136 setuid (getuid());
139#endif /* MAIL_USE_POP */ 137#endif /* MAIL_USE_POP */
140 138
139 /* Check access to input file. */
140 if (access (inname, R_OK | W_OK) != 0)
141 pfatal_with_name (inname);
142
141#ifndef MAIL_USE_MMDF 143#ifndef MAIL_USE_MMDF
142#ifndef MAIL_USE_FLOCK 144#ifndef MAIL_USE_FLOCK
143 /* Use a lock file named /usr/spool/mail/$USER.lock: 145 /* Use a lock file named /usr/spool/mail/$USER.lock:
@@ -276,11 +278,11 @@ fatal (s1, s2)
276 278
277/* Print error message. `s1' is printf control string, `s2' is arg for it. */ 279/* Print error message. `s1' is printf control string, `s2' is arg for it. */
278 280
279error (s1, s2) 281error (s1, s2, s3)
280 char *s1, *s2; 282 char *s1, *s2, *s3;
281{ 283{
282 printf ("movemail: "); 284 printf ("movemail: ");
283 printf (s1, s2); 285 printf (s1, s2, s3);
284 printf ("\n"); 286 printf ("\n");
285} 287}
286 288
@@ -356,313 +358,345 @@ char Errmsg[80];
356 358
357static int debug = 0; 359static int debug = 0;
358 360
359popmail(user, outfile) 361char *get_errmsg ();
360char *user; 362char *getenv ();
361char *outfile; 363int mbx_write ();
364
365popmail (user, outfile)
366 char *user;
367 char *outfile;
362{ 368{
363 char *host; 369 char *host;
364 int nmsgs, nbytes; 370 int nmsgs, nbytes;
365 char response[128]; 371 char response[128];
366 register int i; 372 register int i;
367 int mbfi; 373 int mbfi;
368 FILE *mbf; 374 FILE *mbf;
369 char *getenv(); 375
370 int mbx_write(); 376 host = getenv ("MAILHOST");
371 char *get_errmsg(); 377 if (host == NULL)
372 378 {
373 host = getenv("MAILHOST"); 379 fatal ("no MAILHOST defined");
374 if (host == NULL) {
375 fatal("no MAILHOST defined");
376 } 380 }
377 381
378 if (pop_init(host) == NOTOK) { 382 if (pop_init (host) == NOTOK)
379 error(Errmsg); 383 {
380 return(1); 384 error (Errmsg);
385 return 1;
381 } 386 }
382 387
383 if (getline(response, sizeof response, sfi) != OK) { 388 if (getline (response, sizeof response, sfi) != OK)
384 error(response); 389 {
385 return(1); 390 error (response);
391 return 1;
386 } 392 }
387 393
388 if (pop_command("USER %s", user) == NOTOK || 394 if (pop_command ("USER %s", user) == NOTOK
389 pop_command("RPOP %s", user) == NOTOK) { 395 || pop_command ("RPOP %s", user) == NOTOK)
390 error(Errmsg); 396 {
391 pop_command("QUIT"); 397 error (Errmsg);
392 return(1); 398 pop_command ("QUIT");
399 return 1;
393 } 400 }
394 401
395 if (pop_stat(&nmsgs, &nbytes) == NOTOK) { 402 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
396 error(Errmsg); 403 {
397 pop_command("QUIT"); 404 error (Errmsg);
398 return(1); 405 pop_command ("QUIT");
406 return 1;
399 } 407 }
400 408
401 if (!nmsgs) 409 if (!nmsgs)
402 { 410 {
403 pop_command("QUIT"); 411 pop_command ("QUIT");
404 return(0); 412 return 0;
405 } 413 }
406 414
407 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666); 415 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
408 if (mbfi < 0) 416 if (mbfi < 0)
409 { 417 {
410 pop_command("QUIT"); 418 pop_command ("QUIT");
411 error("Error in open: %s, %s", get_errmsg(), outfile); 419 error ("Error in open: %s, %s", get_errmsg (), outfile);
412 return(1); 420 return 1;
413 } 421 }
414 fchown(mbfi, getuid(), -1); 422 fchown (mbfi, getuid (), -1);
415 423
416 if ((mbf = fdopen(mbfi, "w")) == NULL) 424 if ((mbf = fdopen (mbfi, "w")) == NULL)
417 { 425 {
418 pop_command("QUIT"); 426 pop_command ("QUIT");
419 error("Error in fdopen: %s", get_errmsg()); 427 error ("Error in fdopen: %s", get_errmsg ());
420 close(mbfi); 428 close (mbfi);
421 unlink(outfile); 429 unlink (outfile);
422 return(1); 430 return 1;
423 } 431 }
424 432
425 for (i = 1; i <= nmsgs; i++) { 433 for (i = 1; i <= nmsgs; i++)
426 mbx_delimit_begin(mbf); 434 {
427 if (pop_retr(i, mbx_write, mbf) != OK) { 435 mbx_delimit_begin (mbf);
428 error(Errmsg); 436 if (pop_retr (i, mbx_write, mbf) != OK)
429 pop_command("QUIT"); 437 {
430 close(mbfi); 438 error (Errmsg);
431 return(1); 439 pop_command ("QUIT");
440 close (mbfi);
441 return 1;
432 } 442 }
433 mbx_delimit_end(mbf); 443 mbx_delimit_end (mbf);
434 fflush(mbf); 444 fflush (mbf);
435 } 445 }
436 446
437 for (i = 1; i <= nmsgs; i++) { 447 for (i = 1; i <= nmsgs; i++)
438 if (pop_command("DELE %d", i) == NOTOK) { 448 {
439 error(Errmsg); 449 if (pop_command ("DELE %d", i) == NOTOK)
440 pop_command("QUIT"); 450 {
441 close(mbfi); 451 error (Errmsg);
442 return(1); 452 pop_command ("QUIT");
453 close (mbfi);
454 return 1;
443 } 455 }
444 } 456 }
445 457
446 pop_command("QUIT"); 458 pop_command ("QUIT");
447 close(mbfi); 459 close (mbfi);
448 return(0); 460 return 0;
449} 461}
450 462
451pop_init(host) 463pop_init (host)
452char *host; 464 char *host;
453{ 465{
454 register struct hostent *hp; 466 register struct hostent *hp;
455 register struct servent *sp; 467 register struct servent *sp;
456 int lport = IPPORT_RESERVED - 1; 468 int lport = IPPORT_RESERVED - 1;
457 struct sockaddr_in sin; 469 struct sockaddr_in sin;
458 register int s; 470 register int s;
459 char *get_errmsg(); 471
460 472 hp = gethostbyname (host);
461 hp = gethostbyname(host); 473 if (hp == NULL)
462 if (hp == NULL) { 474 {
463 sprintf(Errmsg, "MAILHOST unknown: %s", host); 475 sprintf (Errmsg, "MAILHOST unknown: %s", host);
464 return(NOTOK); 476 return NOTOK;
465 } 477 }
466 478
467 sp = getservbyname("pop", "tcp"); 479 sp = getservbyname ("pop", "tcp");
468 if (sp == 0) { 480 if (sp == 0)
469 strcpy(Errmsg, "tcp/pop: unknown service"); 481 {
470 return(NOTOK); 482 strcpy (Errmsg, "tcp/pop: unknown service");
483 return NOTOK;
471 } 484 }
472 485
473 sin.sin_family = hp->h_addrtype; 486 sin.sin_family = hp->h_addrtype;
474 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 487 bcopy (hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
475 sin.sin_port = sp->s_port; 488 sin.sin_port = sp->s_port;
476 s = rresvport(&lport); 489 s = rresvport (&lport);
477 if (s < 0) { 490 if (s < 0)
478 sprintf(Errmsg, "error creating socket: %s", get_errmsg()); 491 {
479 return(NOTOK); 492 sprintf (Errmsg, "error creating socket: %s", get_errmsg ());
493 return NOTOK;
480 } 494 }
481 495
482 if (connect(s, (char *)&sin, sizeof sin) < 0) { 496 if (connect (s, (char *)&sin, sizeof sin) < 0)
483 sprintf(Errmsg, "error during connect: %s", get_errmsg()); 497 {
484 close(s); 498 sprintf (Errmsg, "error during connect: %s", get_errmsg ());
485 return(NOTOK); 499 close (s);
500 return NOTOK;
486 } 501 }
487 502
488 sfi = fdopen(s, "r"); 503 sfi = fdopen (s, "r");
489 sfo = fdopen(s, "w"); 504 sfo = fdopen (s, "w");
490 if (sfi == NULL || sfo == NULL) { 505 if (sfi == NULL || sfo == NULL)
491 sprintf(Errmsg, "error in fdopen: %s", get_errmsg()); 506 {
492 close(s); 507 sprintf (Errmsg, "error in fdopen: %s", get_errmsg ());
493 return(NOTOK); 508 close (s);
509 return NOTOK;
494 } 510 }
495 511
496 return(OK); 512 return OK;
497} 513}
498 514
499pop_command(fmt, a, b, c, d) 515pop_command (fmt, a, b, c, d)
500char *fmt; 516 char *fmt;
501{ 517{
502 char buf[128]; 518 char buf[128];
503 char errmsg[64]; 519 char errmsg[64];
504 520
505 sprintf(buf, fmt, a, b, c, d); 521 sprintf (buf, fmt, a, b, c, d);
506 522
507 if (debug) fprintf(stderr, "---> %s\n", buf); 523 if (debug) fprintf (stderr, "---> %s\n", buf);
508 if (putline(buf, Errmsg, sfo) == NOTOK) return(NOTOK); 524 if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
509 525
510 if (getline(buf, sizeof buf, sfi) != OK) { 526 if (getline (buf, sizeof buf, sfi) != OK)
511 strcpy(Errmsg, buf); 527 {
512 return(NOTOK); 528 strcpy (Errmsg, buf);
529 return NOTOK;
513 } 530 }
514 531
515 if (debug) fprintf(stderr, "<--- %s\n", buf); 532 if (debug) fprintf (stderr, "<--- %s\n", buf);
516 if (*buf != '+') { 533 if (*buf != '+')
517 strcpy(Errmsg, buf); 534 {
518 return(NOTOK); 535 strcpy (Errmsg, buf);
519 } else { 536 return NOTOK;
520 return(OK); 537 }
538 else
539 {
540 return OK;
521 } 541 }
522} 542}
523 543
524 544
525pop_stat(nmsgs, nbytes) 545pop_stat (nmsgs, nbytes)
526int *nmsgs, *nbytes; 546 int *nmsgs, *nbytes;
527{ 547{
528 char buf[128]; 548 char buf[128];
529 549
530 if (debug) fprintf(stderr, "---> STAT\n"); 550 if (debug) fprintf (stderr, "---> STAT\n");
531 if (putline("STAT", Errmsg, sfo) == NOTOK) return(NOTOK); 551 if (putline ("STAT", Errmsg, sfo) == NOTOK) return NOTOK;
532 552
533 if (getline(buf, sizeof buf, sfi) != OK) { 553 if (getline (buf, sizeof buf, sfi) != OK)
534 strcpy(Errmsg, buf); 554 {
535 return(NOTOK); 555 strcpy (Errmsg, buf);
556 return NOTOK;
536 } 557 }
537 558
538 if (debug) fprintf(stderr, "<--- %s\n", buf); 559 if (debug) fprintf (stderr, "<--- %s\n", buf);
539 if (*buf != '+') { 560 if (*buf != '+')
540 strcpy(Errmsg, buf); 561 {
541 return(NOTOK); 562 strcpy (Errmsg, buf);
542 } else { 563 return NOTOK;
543 sscanf(buf, "+OK %d %d", nmsgs, nbytes); 564 }
544 return(OK); 565 else
566 {
567 sscanf (buf, "+OK %d %d", nmsgs, nbytes);
568 return OK;
545 } 569 }
546} 570}
547 571
548pop_retr(msgno, action, arg) 572pop_retr (msgno, action, arg)
549int (*action)(); 573 int (*action)();
550{ 574{
551 char buf[128]; 575 char buf[128];
552 576
553 sprintf(buf, "RETR %d", msgno); 577 sprintf (buf, "RETR %d", msgno);
554 if (debug) fprintf(stderr, "%s\n", buf); 578 if (debug) fprintf (stderr, "%s\n", buf);
555 if (putline(buf, Errmsg, sfo) == NOTOK) return(NOTOK); 579 if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
556 580
557 if (getline(buf, sizeof buf, sfi) != OK) { 581 if (getline (buf, sizeof buf, sfi) != OK)
558 strcpy(Errmsg, buf); 582 {
559 return(NOTOK); 583 strcpy (Errmsg, buf);
584 return NOTOK;
560 } 585 }
561 586
562 while (1) { 587 while (1)
563 switch (multiline(buf, sizeof buf, sfi)) { 588 {
589 switch (multiline (buf, sizeof buf, sfi))
590 {
564 case OK: 591 case OK:
565 (*action)(buf, arg); 592 (*action)(buf, arg);
566 break; 593 break;
567 case DONE: 594 case DONE:
568 return (OK); 595 return OK;
569 case NOTOK: 596 case NOTOK:
570 strcpy(Errmsg, buf); 597 strcpy (Errmsg, buf);
571 return (NOTOK); 598 return NOTOK;
572 } 599 }
573 } 600 }
574} 601}
575 602
576getline(buf, n, f) 603getline (buf, n, f)
577char *buf; 604 char *buf;
578register int n; 605 register int n;
579FILE *f; 606 FILE *f;
580{ 607{
581 register char *p; 608 register char *p;
582 int c; 609 int c;
583 610
584 p = buf; 611 p = buf;
585 while (--n > 0 && (c = fgetc(f)) != EOF) 612 while (--n > 0 && (c = fgetc (f)) != EOF)
586 if ((*p++ = c) == '\n') break; 613 if ((*p++ = c) == '\n') break;
587 614
588 if (ferror(f)) { 615 if (ferror (f))
589 strcpy(buf, "error on connection"); 616 {
590 return (NOTOK); 617 strcpy (buf, "error on connection");
618 return NOTOK;
591 } 619 }
592 620
593 if (c == EOF && p == buf) { 621 if (c == EOF && p == buf)
594 strcpy(buf, "connection closed by foreign host"); 622 {
595 return (DONE); 623 strcpy (buf, "connection closed by foreign host");
624 return DONE;
596 } 625 }
597 626
598 *p = NULL; 627 *p = NULL;
599 if (*--p == '\n') *p = NULL; 628 if (*--p == '\n') *p = NULL;
600 if (*--p == '\r') *p = NULL; 629 if (*--p == '\r') *p = NULL;
601 return(OK); 630 return OK;
602} 631}
603 632
604multiline(buf, n, f) 633multiline (buf, n, f)
605char *buf; 634 char *buf;
606register int n; 635 register int n;
607FILE *f; 636 FILE *f;
608{ 637{
609 if (getline(buf, n, f) != OK) return (NOTOK); 638 if (getline (buf, n, f) != OK) return NOTOK;
610 if (*buf == '.') { 639 if (*buf == '.')
611 if (*(buf+1) == NULL) { 640 {
612 return (DONE); 641 if (*(buf+1) == NULL)
613 } else { 642 {
614 strcpy(buf, buf+1); 643 return DONE;
644 }
645 else
646 {
647 strcpy (buf, buf+1);
615 } 648 }
616 } 649 }
617 return(OK); 650 return OK;
618} 651}
619 652
620char * 653char *
621get_errmsg() 654get_errmsg ()
622{ 655{
623 extern int errno, sys_nerr; 656 extern int errno, sys_nerr;
624 extern char *sys_errlist[]; 657 extern char *sys_errlist[];
625 char *s; 658 char *s;
626 659
627 if (errno < sys_nerr) 660 if (errno < sys_nerr)
628 s = sys_errlist[errno]; 661 s = sys_errlist[errno];
629 else 662 else
630 s = "unknown error"; 663 s = "unknown error";
631 return(s); 664 return (s);
632} 665}
633 666
634putline(buf, err, f) 667putline (buf, err, f)
635char *buf; 668 char *buf;
636char *err; 669 char *err;
637FILE *f; 670 FILE *f;
638{ 671{
639 fprintf(f, "%s\r\n", buf); 672 fprintf (f, "%s\r\n", buf);
640 fflush(f); 673 fflush (f);
641 if (ferror(f)) { 674 if (ferror (f))
642 strcpy(err, "lost connection"); 675 {
643 return(NOTOK); 676 strcpy (err, "lost connection");
677 return NOTOK;
644 } 678 }
645 return(OK); 679 return OK;
646} 680}
647 681
648mbx_write(line, mbf) 682mbx_write (line, mbf)
649char *line; 683 char *line;
650FILE *mbf; 684 FILE *mbf;
651{ 685{
652 fputs(line, mbf); 686 fputs (line, mbf);
653 fputc(0x0a, mbf); 687 fputc (0x0a, mbf);
654} 688}
655 689
656mbx_delimit_begin(mbf) 690mbx_delimit_begin (mbf)
657FILE *mbf; 691 FILE *mbf;
658{ 692{
659 fputs("\f\n0,unseen,,\n", mbf); 693 fputs ("\f\n0,unseen,,\n", mbf);
660} 694}
661 695
662mbx_delimit_end(mbf) 696mbx_delimit_end (mbf)
663FILE *mbf; 697 FILE *mbf;
664{ 698{
665 putc('\037', mbf); 699 putc ('\037', mbf);
666} 700}
667 701
668#endif /* MAIL_USE_POP */ 702#endif /* MAIL_USE_POP */