Mercurial > emacs
changeset 9157:29f4cce05fa5
Improve POP code, move most of it into a separate file.
(mbx_delimit_end, mbx_delimit_begin): Check for errors.
(mbx_write): Check for errors and for From line.
(pop_retr, popmail): Use subroutines in pop.c to do the real work.
(get_errmsg, multiline, getline, putline, pop_stat, pop_command)
(pop_init): Functions deleted.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 27 Sep 1994 19:45:00 +0000 |
parents | bc43d1b2bf9f |
children | 939488de5d3f |
files | lib-src/movemail.c |
diffstat | 1 files changed, 110 insertions(+), 254 deletions(-) [+] |
line wrap: on
line diff
--- a/lib-src/movemail.c Tue Sep 27 19:43:07 1994 +0000 +++ b/lib-src/movemail.c Tue Sep 27 19:45:00 1994 +0000 @@ -46,6 +46,11 @@ * New routines in movemail.c: * get_errmsg - return pointer to system error message * + * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies) + * + * Move all of the POP code into a separate file, "pop.c". + * Use strerror instead of get_errmsg. + * */ #define NO_SHORTNAMES /* Tell config not to load remap.h */ @@ -55,6 +60,9 @@ #include <sys/file.h> #include <errno.h> #include <../src/syswait.h> +#ifdef MAIL_USE_POP +#include "pop.h" +#endif #ifdef MSDOS #undef access @@ -445,346 +453,194 @@ char *progname; FILE *sfi; FILE *sfo; +char ibuffer[BUFSIZ]; +char obuffer[BUFSIZ]; char Errmsg[80]; -static int debug = 0; - -char *get_errmsg (); -char *getenv (); -int mbx_write (); - popmail (user, outfile) char *user; char *outfile; { - char *host; int nmsgs, nbytes; - char response[128]; register int i; int mbfi; FILE *mbf; - struct passwd *pw = (struct passwd *) getpwuid (getuid ()); - if (pw == NULL) - fatal ("cannot determine user name"); + char *getenv (); + int mbx_write (); + PopServer server; + extern char *strerror (); - host = getenv ("MAILHOST"); - if (host == NULL) + server = pop_open (0, user, 0, POP_NO_GETPASS); + if (! server) { - fatal ("no MAILHOST defined"); - } - - if (pop_init (host) == NOTOK) - { - fatal (Errmsg); + error (pop_error); + return (1); } - if (getline (response, sizeof response, sfi) != OK) - { - fatal (response); - } - - if (pop_command ("USER %s", user) == NOTOK - || pop_command ("RPOP %s", pw->pw_name) == NOTOK) + if (pop_stat (server, &nmsgs, &nbytes)) { - pop_command ("QUIT"); - fatal (Errmsg); - } - - if (pop_stat (&nmsgs, &nbytes) == NOTOK) - { - pop_command ("QUIT"); - fatal (Errmsg); + error (pop_error); + return (1); } if (!nmsgs) { - pop_command ("QUIT"); - return 0; + pop_close (server); + return (0); } mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666); if (mbfi < 0) { - pop_command ("QUIT"); - pfatal_and_delete (outfile); + pop_close (server); + error ("Error in open: %s, %s", strerror (errno), outfile); + return (1); } fchown (mbfi, getuid (), -1); if ((mbf = fdopen (mbfi, "w")) == NULL) { - pop_command ("QUIT"); - pfatal_and_delete (outfile); + pop_close (server); + error ("Error in fdopen: %s", strerror (errno)); + close (mbfi); + unlink (outfile); + return (1); } for (i = 1; i <= nmsgs; i++) { mbx_delimit_begin (mbf); - if (pop_retr (i, mbx_write, mbf) != OK) + if (pop_retr (server, i, mbx_write, mbf) != OK) { - pop_command ("QUIT"); + error (Errmsg); close (mbfi); - unlink (outfile); - fatal (Errmsg); + return (1); } mbx_delimit_end (mbf); fflush (mbf); + if (ferror (mbf)) + { + error ("Error in fflush: %s", strerror (errno)); + pop_close (server); + close (mbfi); + return (1); + } } + /* On AFS, a call to write only modifies the file in the local + * workstation's AFS cache. The changes are not written to the server + * until a call to fsync or close is made. Users with AFS home + * directories have lost mail when over quota because these checks were + * not made in previous versions of movemail. */ + if (fsync (mbfi) < 0) { - pop_command ("QUIT"); - pfatal_and_delete (outfile); + error ("Error in fsync: %s", strerror (errno)); + return (1); } if (close (mbfi) == -1) { - pop_command ("QUIT"); - pfatal_and_delete (outfile); + error ("Error in close: %s", strerror (errno)); + return (1); } for (i = 1; i <= nmsgs; i++) { - if (pop_command ("DELE %d", i) == NOTOK) + if (pop_delete (server, i)) { - /* Better to ignore this failure. */ + error (pop_error); + pop_close (server); + return (1); } } - pop_command ("QUIT"); + if (pop_quit (server)) + { + error (pop_error); + return (1); + } + return (0); } -pop_init (host) - char *host; +pop_retr (server, msgno, action, arg) + PopServer server; + int (*action)(); { - register struct hostent *hp; - register struct servent *sp; - int lport = IPPORT_RESERVED - 1; - struct sockaddr_in sin; - register int s; - - hp = gethostbyname (host); - if (hp == NULL) - { - sprintf (Errmsg, "MAILHOST unknown: %s", host); - return NOTOK; - } - - sp = getservbyname ("pop", "tcp"); - if (sp == 0) - { - strcpy (Errmsg, "tcp/pop: unknown service"); - return NOTOK; - } - - sin.sin_family = hp->h_addrtype; - bcopy (hp->h_addr, (char *)&sin.sin_addr, hp->h_length); - sin.sin_port = sp->s_port; - s = rresvport (&lport); - if (s < 0) - { - sprintf (Errmsg, "error creating socket: %s", get_errmsg ()); - return NOTOK; - } - - if (connect (s, (char *)&sin, sizeof sin) < 0) - { - sprintf (Errmsg, "error during connect: %s", get_errmsg ()); - close (s); - return NOTOK; - } - - sfi = fdopen (s, "r"); - sfo = fdopen (s, "w"); - if (sfi == NULL || sfo == NULL) - { - sprintf (Errmsg, "error in fdopen: %s", get_errmsg ()); - close (s); - return NOTOK; - } + extern char *strerror (); + char *line; + int ret; - return OK; -} - -pop_command (fmt, a, b, c, d) - char *fmt; -{ - char buf[128]; - char errmsg[64]; - - sprintf (buf, fmt, a, b, c, d); - - if (debug) fprintf (stderr, "---> %s\n", buf); - if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK; - - if (getline (buf, sizeof buf, sfi) != OK) - { - strcpy (Errmsg, buf); - return NOTOK; - } - - if (debug) - fprintf (stderr, "<--- %s\n", buf); - if (*buf != '+') + if (pop_retrieve_first (server, msgno, &line)) { - strcpy (Errmsg, buf); - return NOTOK; - } - else - { - return OK; - } -} - - -pop_stat (nmsgs, nbytes) - int *nmsgs, *nbytes; -{ - char buf[128]; - - if (debug) - fprintf (stderr, "---> STAT\n"); - if (putline ("STAT", Errmsg, sfo) == NOTOK) - return NOTOK; - - if (getline (buf, sizeof buf, sfi) != OK) - { - strcpy (Errmsg, buf); - return NOTOK; + strncpy (Errmsg, pop_error, sizeof (Errmsg)); + Errmsg[sizeof (Errmsg)-1] = '\0'; + return (NOTOK); } - if (debug) fprintf (stderr, "<--- %s\n", buf); - if (*buf != '+') + while (! (ret = pop_retrieve_next (server, &line))) { - strcpy (Errmsg, buf); - return NOTOK; - } - else - { - sscanf (buf, "+OK %d %d", nmsgs, nbytes); - return OK; - } -} + if (! line) + break; -pop_retr (msgno, action, arg) - int (*action)(); -{ - char buf[128]; - - sprintf (buf, "RETR %d", msgno); - if (debug) fprintf (stderr, "%s\n", buf); - if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK; - - if (getline (buf, sizeof buf, sfi) != OK) - { - strcpy (Errmsg, buf); - return NOTOK; + if ((*action)(line, arg) != OK) + { + strcpy (Errmsg, strerror (errno)); + pop_close (server); + return (NOTOK); + } } - while (1) + if (ret) { - switch (multiline (buf, sizeof buf, sfi)) - { - case OK: - (*action)(buf, arg); - break; - case DONE: - return OK; - case NOTOK: - strcpy (Errmsg, buf); - return NOTOK; - } + strncpy (Errmsg, pop_error, sizeof (Errmsg)); + Errmsg[sizeof (Errmsg)-1] = '\0'; + return (NOTOK); } + + return (OK); } -getline (buf, n, f) - char *buf; - register int n; - FILE *f; -{ - register char *p; - int c; - - p = buf; - while (--n > 0 && (c = fgetc (f)) != EOF) - if ((*p++ = c) == '\n') break; - - if (ferror (f)) - { - strcpy (buf, "error on connection"); - return NOTOK; - } - - if (c == EOF && p == buf) - { - strcpy (buf, "connection closed by foreign host"); - return DONE; - } - - *p = NULL; - if (*--p == '\n') *p = NULL; - if (*--p == '\r') *p = NULL; - return OK; -} +/* Do this as a macro instead of using strcmp to save on execution time. */ +#define IS_FROM_LINE(a) ((a[0] == 'F') \ + && (a[1] == 'r') \ + && (a[2] == 'o') \ + && (a[3] == 'm') \ + && (a[4] == ' ')) -multiline (buf, n, f) - char *buf; - register int n; - FILE *f; -{ - if (getline (buf, n, f) != OK) - return NOTOK; - if (*buf == '.') - { - if (*(buf+1) == NULL) - return DONE; - else - strcpy (buf, buf+1); - } - return OK; -} - -char * -get_errmsg () -{ - extern int errno; - extern char *strerror (); - return strerror (errno); -} - -putline (buf, err, f) - char *buf; - char *err; - FILE *f; -{ - fprintf (f, "%s\r\n", buf); - fflush (f); - if (ferror (f)) - { - strcpy (err, "lost connection"); - return NOTOK; - } - return OK; -} - +int mbx_write (line, mbf) char *line; FILE *mbf; { - fputs (line, mbf); - fputc (0x0a, mbf); + if (IS_FROM_LINE (line)) + { + if (fputc ('>', mbf) == EOF) + return (NOTOK); + } + if (fputs (line, mbf) == EOF) + return (NOTOK); + if (fputc (0x0a, mbf) == EOF) + return (NOTOK); + return (OK); } +int mbx_delimit_begin (mbf) FILE *mbf; { - fputs ("\f\n0, unseen,,\n", mbf); + if (fputs ("\f\n0, unseen,,\n", mbf) == EOF) + return (NOTOK); + return (OK); } mbx_delimit_end (mbf) FILE *mbf; { - putc ('\037', mbf); + if (putc ('\037', mbf) == EOF) + return (NOTOK); + return (OK); } #endif /* MAIL_USE_POP */