Article View: gnu.emacs.bug
Article #88017improved version of movemail
From: Jonathan I. Kamens
Date: Sun, 14 Aug 1994 09:48
Date: Sun, 14 Aug 1994 09:48
2167 lines
57508 bytes
57508 bytes
In GNU Emacs 19.25.2 of Fri Aug 12 1994 on gza-client1 (sparc-sun-sunos4.1.3)
The shar file at the end of this message contains two diff files,
Makefile.in.in-diff and movemail.c-diff, to be applied as patches in
the lib-src directory, as well as three new files, pop.c, pop.h and
strstr.c, to be added to the lib-src directory.
The two major enhancements in these improvements to movemail are (1)
better POP code which has been ripped out of movemail.c and moved into
a separate module, and (2) better error checking for failures to save
mail to the output file.
If this version of movemail is incorporated into the standard GNU
Emacs release, I will maintain it.
Jonathan Kamens | OpenVision Technologies, Inc. | jik@cam.ov.com
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: Makefile.in.in-diff movemail.c-diff pop.c pop.h strstr.c
# Wrapped by jik@gza-client1 on Sun Aug 14 00:01:42 1994
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile.in.in-diff' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'Makefile.in.in-diff'\"
else
echo shar: Extracting \"'Makefile.in.in-diff'\" \(2496 characters\)
sed "s/^X//" >'Makefile.in.in-diff' <<'END_OF_FILE'
X*** Makefile.in.in 1994/08/11 20:41:33 1.1.1.1
X--- Makefile.in.in 1994/08/11 21:43:53 1.5.1.1
X***************
X*** 101,106 ****
X--- 101,135 ----
X SOURCES = COPYING ChangeLog Makefile.in README aixcc.lex emacs.csh \
X makedoc.com *.[chy] rcs2log vcdiff
X
X+ # Additional -D flags for movemail (add to MOVE_FLAGS if desired):
X+ # MAIL_USE_POP Support mail retrieval from a POP mailbox.
X+ # MAIL_USE_MMDF Support MMDF mailboxes.
X+ # MAIL_USE_FLOCK Use flock() for file locking (see the comments
X+ # about locking in movemail.c)
X+ # MAIL_UNLINK_SPOOL Unlink the user's spool mailbox after reading
X+ # it (instead of just emptying it).
X+ # KERBEROS Support Kerberized POP.
X+ # KRB5 Support Kerberos Version 5 pop instead of
X+ # Version 4 (define this in addition to
X+ # KERBEROS).
X+ # HESIOD Support Hesiod lookups of user mailboxes.
X+ # LOCAL_STRSTR Need to provide strstr(), since it isn't in
X+ # the standard C library
X+ # MAILHOST A string, the host name of the default POP
X+ # mail host for the site.
X+ MOVE_FLAGS=
X+
X+ # Additional libraries for movemail:
X+ # For KERBEROS
X+ # MOVE_LIBS= -lkrb -ldes -lcom_err
X+ # For KERBEROS + KRB5
X+ # MOVE_LIBS= -lkrb5 -lcrypto -lisode -lcom_err
X+ # Add "-lhesiod" if HESIOD is defined.
X+ MOVE_LIBS=
X+
X+ MOVE_SOURCES = ${srcdir}/movemail.c ${srcdir}/pop.c ${srcdir}/strstr.c
X+ MOVE_OBJS = movemail.o pop.o strstr.o
X+
X # ========================== start of cpp stuff =======================
X /* From here on, comments must be done in C syntax. */
X
X***************
X*** 284,291 ****
X b2m: ${srcdir}/b2m.c ../src/config.h
X $(CC) ${ALL_CFLAGS} ${srcdir}/b2m.c $(LOADLIBES) -o b2m
X
X! movemail: ${srcdir}/movemail.c ../src/config.h
X! $(CC) ${ALL_CFLAGS} ${srcdir}/movemail.c $(LOADLIBES) -o movemail
X
X cvtmail: ${srcdir}/cvtmail.c
X $(CC) ${ALL_CFLAGS} ${srcdir}/cvtmail.c $(LOADLIBES) -o cvtmail
X--- 313,329 ----
X b2m: ${srcdir}/b2m.c ../src/config.h
X $(CC) ${ALL_CFLAGS} ${srcdir}/b2m.c $(LOADLIBES) -o b2m
X
X! movemail: ${MOVE_OBJS}
X! $(CC) ${LINK_CFLAGS} ${MOVE_FLAGS} ${MOVE_OBJS} $(LOADLIBES) $(MOVE_LIBS) -o movemail
X!
X! movemail.o: ${srcdir}/movemail.c ../src/config.h
X! $(CC) -c ${CPP_CFLAGS} ${MOVE_FLAGS} ${srcdir}/movemail.c
X!
X! pop.o: ${srcdir}/pop.c
X! $(CC) -c ${CPP_CFLAGS} ${MOVE_FLAGS} ${srcdir}/pop.c
X!
X! strstr.o: ${srcdir}/strstr.c
X! $(CC) -c ${CPP_CFLAGS} ${MOVE_FLAGS} ${srcdir}/strstr.c
X
X cvtmail: ${srcdir}/cvtmail.c
X $(CC) ${ALL_CFLAGS} ${srcdir}/cvtmail.c $(LOADLIBES) -o cvtmail
END_OF_FILE
if test 2496 -ne `wc -c <'Makefile.in.in-diff'`; then
echo shar: \"'Makefile.in.in-diff'\" unpacked with wrong size!
fi
# end of 'Makefile.in.in-diff'
fi
if test -f 'movemail.c-diff' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'movemail.c-diff'\"
else
echo shar: Extracting \"'movemail.c-diff'\" \(11173 characters\)
sed "s/^X//" >'movemail.c-diff' <<'END_OF_FILE'
X*** movemail.c 1994/08/11 20:57:36 1.1.1.3
X--- movemail.c 1994/08/11 21:03:39 1.7
X***************
X*** 46,51 ****
X--- 46,56 ----
X * New routines in movemail.c:
X * get_errmsg - return pointer to system error message
X *
X+ * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
X+ *
X+ * Move all of the POP code into a separate file, "pop.c".
X+ * Use strerror instead of get_errmsg.
X+ *
X */
X
X #include <sys/types.h>
X***************
X*** 55,60 ****
X--- 60,68 ----
X #define NO_SHORTNAMES /* Tell config not to load remap.h */
X #include <../src/config.h>
X #include <../src/syswait.h>
X+ #ifdef MAIL_USE_POP
X+ #include "pop.h"
X+ #endif
X
X #ifdef MSDOS
X #undef access
X***************
X*** 445,790 ****
X char *progname;
X FILE *sfi;
X FILE *sfo;
X char Errmsg[80];
X
X! static int debug = 0;
X!
X! char *get_errmsg ();
X! char *getenv ();
X! int mbx_write ();
X!
X! popmail (user, outfile)
X! char *user;
X! char *outfile;
X {
X- char *host;
X int nmsgs, nbytes;
X- char response[128];
X register int i;
X int mbfi;
X FILE *mbf;
X! struct passwd *pw = (struct passwd *) getpwuid (getuid ());
X! if (pw == NULL)
X! fatal ("cannot determine user name");
X!
X! host = getenv ("MAILHOST");
X! if (host == NULL)
X! {
X! fatal ("no MAILHOST defined");
X! }
X!
X! if (pop_init (host) == NOTOK)
X! {
X! fatal (Errmsg);
X! }
X!
X! if (getline (response, sizeof response, sfi) != OK)
X! {
X! fatal (response);
X! }
X!
X! if (pop_command ("USER %s", user) == NOTOK
X! || pop_command ("RPOP %s", pw->pw_name) == NOTOK)
X! {
X! pop_command ("QUIT");
X! fatal (Errmsg);
X! }
X
X! if (pop_stat (&nmsgs, &nbytes) == NOTOK)
X! {
X! pop_command ("QUIT");
X! fatal (Errmsg);
X! }
X
X if (!nmsgs)
X! {
X! pop_command ("QUIT");
X! return 0;
X! }
X
X mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
X if (mbfi < 0)
X- {
X- pop_command ("QUIT");
X- pfatal_and_delete (outfile);
X- }
X- fchown (mbfi, getuid (), -1);
X-
X- if ((mbf = fdopen (mbfi, "w")) == NULL)
X- {
X- pop_command ("QUIT");
X- pfatal_and_delete (outfile);
X- }
X-
X- for (i = 1; i <= nmsgs; i++)
X- {
X- mbx_delimit_begin (mbf);
X- if (pop_retr (i, mbx_write, mbf) != OK)
X- {
X- pop_command ("QUIT");
X- close (mbfi);
X- unlink (outfile);
X- fatal (Errmsg);
X- }
X- mbx_delimit_end (mbf);
X- fflush (mbf);
X- }
X-
X- if (fsync (mbfi) < 0)
X- {
X- pop_command ("QUIT");
X- pfatal_and_delete (outfile);
X- }
X-
X- if (close (mbfi) == -1)
X- {
X- pop_command ("QUIT");
X- pfatal_and_delete (outfile);
X- }
X-
X- for (i = 1; i <= nmsgs; i++)
X- {
X- if (pop_command ("DELE %d", i) == NOTOK)
X- {
X- /* Better to ignore this failure. */
X- }
X- }
X-
X- pop_command ("QUIT");
X- return (0);
X- }
X-
X- pop_init (host)
X- char *host;
X {
X! register struct hostent *hp;
X! register struct servent *sp;
X! int lport = IPPORT_RESERVED - 1;
X! struct sockaddr_in sin;
X! register int s;
X!
X! hp = gethostbyname (host);
X! if (hp == NULL)
X! {
X! sprintf (Errmsg, "MAILHOST unknown: %s", host);
X! return NOTOK;
X! }
X!
X! sp = getservbyname ("pop", "tcp");
X! if (sp == 0)
X! {
X! strcpy (Errmsg, "tcp/pop: unknown service");
X! return NOTOK;
X! }
X!
X! sin.sin_family = hp->h_addrtype;
X! bcopy (hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
X! sin.sin_port = sp->s_port;
X! s = rresvport (&lport);
X! if (s < 0)
X! {
X! sprintf (Errmsg, "error creating socket: %s", get_errmsg ());
X! return NOTOK;
X! }
X!
X! if (connect (s, (char *)&sin, sizeof sin) < 0)
X! {
X! sprintf (Errmsg, "error during connect: %s", get_errmsg ());
X! close (s);
X! return NOTOK;
X! }
X!
X! sfi = fdopen (s, "r");
X! sfo = fdopen (s, "w");
X! if (sfi == NULL || sfo == NULL)
X! {
X! sprintf (Errmsg, "error in fdopen: %s", get_errmsg ());
X! close (s);
X! return NOTOK;
X }
X!
X! return OK;
X! }
X!
X! pop_command (fmt, a, b, c, d)
X! char *fmt;
X! {
X! char buf[128];
X! char errmsg[64];
X!
X! sprintf (buf, fmt, a, b, c, d);
X
X! if (debug) fprintf (stderr, "---> %s\n", buf);
X! if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
X
X! if (getline (buf, sizeof buf, sfi) != OK)
X! {
X! strcpy (Errmsg, buf);
X! return NOTOK;
X! }
X
X! if (debug)
X! fprintf (stderr, "<--- %s\n", buf);
X! if (*buf != '+')
X! {
X! strcpy (Errmsg, buf);
X! return NOTOK;
X }
X! else
X! {
X! return OK;
X! }
X! }
X
X
X! pop_stat (nmsgs, nbytes)
X! int *nmsgs, *nbytes;
X! {
X! char buf[128];
X!
X! if (debug)
X! fprintf (stderr, "---> STAT\n");
X! if (putline ("STAT", Errmsg, sfo) == NOTOK)
X! return NOTOK;
X!
X! if (getline (buf, sizeof buf, sfi) != OK)
X! {
X! strcpy (Errmsg, buf);
X! return NOTOK;
X! }
X!
X! if (debug) fprintf (stderr, "<--- %s\n", buf);
X! if (*buf != '+')
X! {
X! strcpy (Errmsg, buf);
X! return NOTOK;
X! }
X! else
X! {
X! sscanf (buf, "+OK %d %d", nmsgs, nbytes);
X! return OK;
X! }
X! }
X!
X! pop_retr (msgno, action, arg)
X! int (*action)();
X! {
X! char buf[128];
X!
X! sprintf (buf, "RETR %d", msgno);
X! if (debug) fprintf (stderr, "%s\n", buf);
X! if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
X!
X! if (getline (buf, sizeof buf, sfi) != OK)
X! {
X! strcpy (Errmsg, buf);
X! return NOTOK;
X! }
X!
X! while (1)
X! {
X! switch (multiline (buf, sizeof buf, sfi))
X! {
X! case OK:
X! (*action)(buf, arg);
X! break;
X! case DONE:
X! return OK;
X! case NOTOK:
X! strcpy (Errmsg, buf);
X! return NOTOK;
X! }
X! }
X! }
X!
X! getline (buf, n, f)
X! char *buf;
X! register int n;
X! FILE *f;
X! {
X! register char *p;
X! int c;
X!
X! p = buf;
X! while (--n > 0 && (c = fgetc (f)) != EOF)
X! if ((*p++ = c) == '\n') break;
X!
X! if (ferror (f))
X! {
X! strcpy (buf, "error on connection");
X! return NOTOK;
X! }
X!
X! if (c == EOF && p == buf)
X! {
X! strcpy (buf, "connection closed by foreign host");
X! return DONE;
X! }
X!
X! *p = NULL;
X! if (*--p == '\n') *p = NULL;
X! if (*--p == '\r') *p = NULL;
X! return OK;
X! }
X!
X! multiline (buf, n, f)
X! char *buf;
X! register int n;
X! FILE *f;
X! {
X! if (getline (buf, n, f) != OK)
X! return NOTOK;
X! if (*buf == '.')
X! {
X! if (*(buf+1) == NULL)
X! return DONE;
X! else
X! strcpy (buf, buf+1);
X! }
X! return OK;
X }
X
X! char *
X! get_errmsg ()
X {
X! extern int errno;
X! extern char *strerror ();
X! return strerror (errno);
X! }
X
X! putline (buf, err, f)
X! char *buf;
X! char *err;
X! FILE *f;
X! {
X! fprintf (f, "%s\r\n", buf);
X! fflush (f);
X! if (ferror (f))
X! {
X! strcpy (err, "lost connection");
X! return NOTOK;
X! }
X! return OK;
X! }
X
X! mbx_write (line, mbf)
X! char *line;
X! FILE *mbf;
X! {
X! fputs (line, mbf);
X! fputc (0x0a, mbf);
X }
X
X! mbx_delimit_begin (mbf)
X! FILE *mbf;
X {
X! fputs ("\f\n0, unseen,,\n", mbf);
X }
X
X! mbx_delimit_end (mbf)
X! FILE *mbf;
X {
X! putc ('\037', mbf);
X }
X
X #endif /* MAIL_USE_POP */
X--- 453,629 ----
X char *progname;
X FILE *sfi;
X FILE *sfo;
X+ char ibuffer[BUFSIZ];
X+ char obuffer[BUFSIZ];
X char Errmsg[80];
X
X! popmail(user, outfile)
X! char *user;
X! char *outfile;
X {
X int nmsgs, nbytes;
X register int i;
X int mbfi;
X FILE *mbf;
X! char *getenv();
X! int mbx_write();
X! PopServer server;
X! extern char *strerror();
X!
X! server = pop_open(0, user, 0, POP_NO_GETPASS);
X! if (! server) {
X! error(pop_error);
X! return(1);
X! }
X
X! if (pop_stat(server, &nmsgs, &nbytes)) {
X! error(pop_error);
X! return(1);
X! }
X
X if (!nmsgs)
X! {
X! pop_close(server);
X! return(0);
X! }
X
X mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
X if (mbfi < 0)
X {
X! pop_close(server);
X! error("Error in open: %s, %s", strerror(errno), outfile);
X! return(1);
X! }
X! fchown(mbfi, getuid(), -1);
X!
X! if ((mbf = fdopen(mbfi, "w")) == NULL)
X! {
X! pop_close(server);
X! error("Error in fdopen: %s", strerror(errno));
X! close(mbfi);
X! (void) unlink(outfile);
X! return(1);
X! }
X!
X! for (i = 1; i <= nmsgs; i++) {
X! mbx_delimit_begin(mbf);
X! if (pop_retr(server, i, mbx_write, mbf) != OK) {
X! error(Errmsg);
X! close(mbfi);
X! return(1);
X! }
X! mbx_delimit_end(mbf);
X! fflush(mbf);
X! if (ferror(mbf)) {
X! error("Error in fflush: %s", strerror(errno));
X! pop_close(server);
X! close(mbfi);
X! return(1);
X }
X! }
X
X! /* On AFS, a call to write() only modifies the file in the local
X! * workstation's AFS cache. The changes are not written to the server
X! * until a call to fsync() or close() is made. Users with AFS home
X! * directories have lost mail when over quota because these checks were
X! * not made in previous versions of movemail. */
X!
X! if (fsync(mbfi) < 0) {
X! error("Error in fsync: %s", strerror(errno));
X! return(1);
X! }
X
X! if (close(mbfi) == -1) {
X! error("Error in close: %s", strerror(errno));
X! return(1);
X! }
X
X! for (i = 1; i <= nmsgs; i++) {
X! if (pop_delete(server, i)) {
X! error(pop_error);
X! pop_close(server);
X! return(1);
X }
X! }
X
X+ if (pop_quit(server)) {
X+ error(pop_error);
X+ return(1);
X+ }
X
X! return(0);
X }
X
X! pop_retr(server, msgno, action, arg)
X! PopServer server;
X! int (*action)();
X {
X! extern char *strerror();
X! char *line;
X! int ret;
X
X! if (pop_retrieve_first(server, msgno, &line)) {
X! strncpy(Errmsg, pop_error, sizeof(Errmsg));
X! Errmsg[sizeof(Errmsg)-1] = '\0';
X! return(NOTOK);
X! }
X!
X! while (! (ret = pop_retrieve_next(server, &line))) {
X! if (! line) {
X! break;
X! }
X! if ((*action)(line, arg) != OK) {
X! strcpy(Errmsg, strerror(errno));
X! pop_close(server);
X! return (NOTOK);
X! }
X! }
X
X! if (ret) {
X! strncpy(Errmsg, pop_error, sizeof(Errmsg));
X! Errmsg[sizeof(Errmsg)-1] = '\0';
X! return(NOTOK);
X! }
X!
X! return(OK);
X! }
X!
X! /* Do this as a macro instead of using strcmp to save on execution time. */
X! #define IS_FROM_LINE(a) ((a[0] == 'F') && \
X! (a[1] == 'r') && \
X! (a[2] == 'o') && \
X! (a[3] == 'm') && \
X! (a[4] == ' '))
X!
X! int mbx_write(line, mbf)
X! char *line;
X! FILE *mbf;
X! {
X! if (IS_FROM_LINE(line)) {
X! if (fputc('>', mbf) == EOF)
X! return(NOTOK);
X! }
X! if (fputs(line, mbf) == EOF)
X! return(NOTOK);
X! if (fputc(0x0a, mbf) == EOF)
X! return(NOTOK);
X! return(OK);
X }
X
X! int mbx_delimit_begin(mbf)
X! FILE *mbf;
X {
X! if (fputs("\f\n0, unseen,,\n", mbf) == EOF)
X! return(NOTOK);
X! return(OK);
X }
X
X! mbx_delimit_end(mbf)
X! FILE *mbf;
X {
X! if (putc('\037', mbf) == EOF)
X! return(NOTOK);
X! return(OK);
X }
X
X #endif /* MAIL_USE_POP */
END_OF_FILE
if test 11173 -ne `wc -c <'movemail.c-diff'`; then
echo shar: \"'movemail.c-diff'\" unpacked with wrong size!
fi
# end of 'movemail.c-diff'
fi
if test -f 'pop.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'pop.c'\"
else
echo shar: Extracting \"'pop.c'\" \(31552 characters\)
sed "s/^X//" >'pop.c' <<'END_OF_FILE'
X/*
X * pop.c: client routines for talking to a POP3-protocol post-office
X * server
X *
X * Copyright (c) 1991,1993 by Jonathan Kamens.
X *
X * Distribution and use of this file is governed by the GNU Public
X * License (version 1 or any later version).
X *
X * If you make any enhancements, bug fixes or porting changes to this
X * file, please make sure that they get sent to me so that I can
X * incorporate them into my master version of it. Try sending me
X * E-mail at jik@security.ov.com, or, if that fails, at
X * jik@athena.mit.edu.
X *
X * The current version of this file is $Revision: 1.2 $.
X */
X
X#ifdef MAIL_USE_POP
X
X#include <sys/types.h>
X#include <netinet/in.h>
X#include <sys/socket.h>
X#include <pop.h>
X#ifdef sun
X#include <malloc.h>
X#else
Xextern char *malloc(/* unsigned */);
Xextern char *realloc(/* char *, unsigned */);
Xextern void free(/* char * */);
X#endif
X#endif
X#ifdef HESIOD
X#include <hesiod.h>
X/*
X * It really shouldn't be necessary to put this declaration here, but
X * the version of hesiod.h that Athena has installed in release 7.2
X * doesn't declare this function; I don't know if the 7.3 version of
X * hesiod.h does.
X */
Xextern struct servent *hes_getservbyname(/* char *, char * */);
X#endif
X#include <pwd.h>
X#include <string.h>
X#include <strings.h>
X#ifdef LOCAL_STRSTR
Xextern char *strstr(/* char *, char * */);
X#endif
X#include <netdb.h>
X#include <errno.h>
X#include <stdio.h>
X#ifdef KERBEROS
X#ifndef KRB5
X#include <krb.h>
X#include <des.h>
X#else /* KRB5 */
X#include <krb5/krb5.h>
X#include <krb5/ext-proto.h>
X#include <ctype.h>
X#endif /* KRB5 */
X
Xextern char *getenv(/* char * */);
Xextern char *getlogin(/* void */);
Xextern char *getpass(/* char * */);
Xextern int getuid(/* void */);
X#ifndef bzero
Xextern void bzero(/* char *, int */);
X#endif
X#ifndef bcopy
Xbcopy(/* char *, char *, int */);
X#endif
Xextern int socket(/* int, int, int */);
Xextern int connect(/* int, struct sockaddr *, int */);
Xextern char *strerror(/* int */);
Xextern int close(/* int */);
Xextern int read(/* int, char *, int */), write(/* int, char *, int */);
Xextern int atoi(/* char * */);
X#if !(defined(vax) && defined(__GNUC__)) && !defined(_IBMR2)
Xextern int fprintf(/* FILE *, char *, ... */);
X#endif
X
X#ifdef KERBEROS
Xextern int krb_sendauth(/* long, int, KTEXT, char *, char *, char *,
X u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
X struct sockaddr_in *, struct sockaddr_in *,
X char * */);
Xextern char *krb_realmofhost(/* char * */);
X#endif
X
Xextern int h_errno;
X
Xstatic int socket_connection(/* char *, int */);
Xstatic char *getline(/* PopServer */);
Xstatic int sendline(/* PopServer, char * */);
Xstatic int fullwrite(/* int, char *, int */);
Xstatic int getok(/* PopServer */);
X#if 0
Xstatic int gettermination(/* PopServer */);
X#endif
Xstatic void pop_trash(/* PopServer */);
X
X#define ERROR_MAX 80 /* a pretty arbitrary size */
X#define POP_PORT 110
X#define KPOP_PORT 1109
X#define POP_SERVICE "pop"
X#ifdef KERBEROS
X#ifdef KRB5
X#define KPOP_SERVICE "k5pop";
X#else
X#define KPOP_SERVICE "kpop"
X#endif
X#endif
X
Xchar pop_error[ERROR_MAX];
Xint pop_debug = 0;
X
X#ifndef min
X#define min(a,b) (((a) < (b)) ? (a) : (b))
X#endif
X
X/*
X * Function: pop_open(char *host, char *username, char *password,
X * int flags)
X *
X * Purpose: Establishes a connection with a post-office server, and
X * completes the authorization portion of the session.
X *
X * Arguments:
X * host The server host with which the connection should be
X * established. Optional. If omitted, internal
X * heuristics will be used to determine the server host,
X * if possible.
X * username
X * The username of the mail-drop to access. Optional.
X * If omitted, internal heuristics will be used to
X * determine the username, if possible.
X * password
X * The password to use for authorization. If omitted,
X * internal heuristics will be used to determine the
X * password, if possible.
X * flags A bit mask containing flags controlling certain
X * functions of the routine. Valid flags are defined in
X * the file pop.h
X *
X * Return value: Upon successful establishment of a connection, a
X * non-null PopServer will be returned. Otherwise, null will be
X * returned, and the string variable pop_error will contain an
X * explanation of the error.
X */
XPopServer pop_open(host, username, password, flags)
Xchar *host;
Xchar *username;
Xchar *password;
Xint flags;
X{
X int sock;
X PopServer server;
X
X /* Determine the user name */
X if (! username) {
X username = getenv("USER");
X if (! (username && *username)) {
X username = getlogin();
X if (! (username && *username)) {
X struct passwd *passwd;
X passwd = getpwuid(getuid());
X if (passwd && passwd->pw_name && *passwd->pw_name) {
X username = passwd->pw_name;
X }
X else {
X strcpy(pop_error, "Could not determine username");
X return(0);
X }
X }
X }
X }
X
X /*
X * Determine the mail host.
X */
X
X if (! host) {
X host = getenv("MAILHOST");
X }
X
X#ifdef HESIOD
X if ((! host) && (! (flags & POP_NO_HESIOD))) {
X struct hes_postoffice *office;
X office = hes_getmailhost(username);
X if (office && office->po_type && (! strcmp(office->po_type, "POP"))
X && office->po_name && *office->po_name && office->po_host
X && *office->po_host) {
X host = office->po_host;
X username = office->po_name;
X }
X }
X#endif
X
X#ifdef MAILHOST
X if (! host) {
X host = MAILHOST;
X }
X#endif
X
X if (! host) {
X strcpy(pop_error, "Could not determine POP server");
X return(0);
X }
X
X /* Determine the password */
X#ifdef KERBEROS
X#define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
X#else
X#define DONT_NEED_PASSWORD 0
X#endif
X
X if ((! password) && (! DONT_NEED_PASSWORD)) {
X if (! (flags & POP_NO_GETPASS)) {
X password = getpass("Enter POP password:");
X }
X if (! password) {
X strcpy(pop_error, "Could not determine POP password");
X return(0);
X }
X }
X if (password)
X flags |= POP_NO_KERBEROS;
X else
X password = username;
X
X sock = socket_connection(host, flags);
X if (sock == -1)
X return(0);
X
X server = (PopServer) malloc(sizeof(struct _PopServer));
X if (! server) {
X strcpy(pop_error, "Out of memory in pop_open");
X return(0);
X }
X server->buffer = (char *) malloc(GETLINE_MIN);
X if (! server->buffer) {
X strcpy(pop_error, "Out of memory in pop_open");
X free((char *) server);
X return(0);
X }
X
X server->file = sock;
X server->data = 0;
X server->buffer_index = 0;
X server->buffer_size = GETLINE_MIN;
X server->in_multi = 0;
X
X if (getok(server))
X return(0);
X
X /*
X * I really shouldn't use the pop_error variable like this, but....
X */
X if (strlen(username) > ERROR_MAX - 6) {
X pop_close(server);
X strcpy(pop_error,
X "Username too long; recompile pop.c with larger ERROR_MAX");
X return(0);
X }
X sprintf(pop_error, "USER %s", username);
X
X if (sendline(server, pop_error) || getok(server)) {
X return(0);
X }
X
X if (strlen(password) > ERROR_MAX - 6) {
X pop_close(server);
X strcpy(pop_error,
X "Password too long; recompile pop.c with larger ERROR_MAX");
X return(0);
X }
X sprintf(pop_error, "PASS %s", password);
X
X if (sendline(server, pop_error) || getok(server)) {
X return(0);
X }
X
X return(server);
X}
X
X/*
X * Function: pop_stat
X *
X * Purpose: Issue the STAT command to the server and return (in the
X * value parameters) the number of messages in the maildrop and
X * the total size of the maildrop.
X *
X * Return value: 0 on success, or non-zero with an error in pop_error
X * in failure.
X *
X * Side effects: On failure, may make further operations on the
X * connection impossible.
X */
Xint pop_stat(server, count, size)
XPopServer server;
Xint *count;
Xint *size;
X{
X char *fromserver;
X
X if (server->in_multi) {
X strcpy(pop_error, "In multi-line query in pop_stat");
X return(-1);
X }
X
X if (sendline(server, "STAT") || (! (fromserver = getline(server))))
X return(-1);
X
X if (strncmp(fromserver, "+OK ", 4)) {
X if (0 == strncmp(fromserver, "-ERR", 4)) {
X strncpy(pop_error, fromserver, ERROR_MAX);
X }
X else {
X strcpy(pop_error,
X "Unexpected response from POP server in pop_stat");
X pop_trash(server);
X }
X return(-1);
X }
X
X *count = atoi(&fromserver[4]);
X
X fromserver = index(&fromserver[4], ' ');
X if (! fromserver) {
X strcpy(pop_error,
X "Badly formatted response from server in pop_stat");
X pop_trash(server);
X return(-1);
X }
X
X *size = atoi(fromserver + 1);
X
X return(0);
X}
X
X/*
X * Function: pop_list
X *
X * Purpose: Performs the POP "list" command and returns (in value
X * parameters) two malloc'd zero-terminated arrays -- one of
X * message IDs, and a parallel one of sizes.
X *
X * Arguments:
X * server The pop connection to talk to.
X * message The number of the one message about which to get
X * information, or 0 to get information about all
X * messages.
X *
X * Return value: 0 on success, non-zero with error in pop_error on
X * failure.
X *
X * Side effects: On failure, may make further operations on the
X * connection impossible.
X */
Xint pop_list(server, message, IDs, sizes)
XPopServer server;
Xint message;
Xint **IDs;
Xint **sizes;
X{
X int how_many, i;
X char *fromserver;
X
X if (server->in_multi) {
X strcpy(pop_error, "In multi-line query in pop_list");
X return(-1);
X }
X
X if (message)
X how_many = 1;
X else {
X int count, size;
X if (pop_stat(server, &count, &size))
X return(-1);
X how_many = count;
X }
X
X *IDs = (int *) malloc((how_many + 1) * sizeof(int));
X *sizes = (int *) malloc((how_many + 1) * sizeof(int));
X if (! (*IDs && *sizes)) {
X strcpy(pop_error, "Out of memory in pop_list");
X return(-1);
X }
X
X if (message) {
X sprintf(pop_error, "LIST %d", message);
X if (sendline(server, pop_error)) {
X free((char *) *IDs);
X free((char *) *sizes);
X return(-1);
X }
X if (! (fromserver = getline(server))) {
X free((char *) *IDs);
X free((char *) *sizes);
X return(-1);
X }
X if (strncmp(fromserver, "+OK ", 4)) {
X if (! strncmp(fromserver, "-ERR", 4))
X strncpy(pop_error, fromserver, ERROR_MAX);
X else {
X strcpy(pop_error,
X "Unexpected response from server in pop_list");
X pop_trash(server);
X }
X free((char *) *IDs);
X free((char *) *sizes);
X return(-1);
X }
X (*IDs)[0] = atoi(&fromserver[4]);
X fromserver = index(&fromserver[4], ' ');
X if (! fromserver) {
X strcpy(pop_error,
X "Badly formatted response from server in pop_list");
X pop_trash(server);
X free((char *) *IDs);
X free((char *) *sizes);
X return(-1);
X }
X (*sizes)[0] = atoi(fromserver);
X (*IDs)[1] = (*sizes)[1] = 0;
X return(0);
X }
X else {
X if (pop_multi_first(server, "LIST", &fromserver)) {
X free((char *) *IDs);
X free((char *) *sizes);
X return(-1);
X }
X for (i = 0; i < how_many; i++) {
X if (pop_multi_next(server, &fromserver)) {
X free((char *) *IDs);
X free((char *) *sizes);
X return(-1);
X }
X (*IDs)[i] = atoi(fromserver);
X fromserver = index(fromserver, ' ');
X if (! fromserver) {
X strcpy(pop_error,
X "Badly formatted response from server in pop_list");
X free((char *) *IDs);
X free((char *) *sizes);
X pop_trash(server);
X return(-1);
X }
X (*sizes)[i] = atoi(fromserver);
X }
X if (pop_multi_next(server, &fromserver)) {
X free((char *) *IDs);
X free((char *) *sizes);
X return(-1);
X }
X else if (fromserver) {
X strcpy(pop_error,
X "Too many response lines from server in pop_list");
X free((char *) *IDs);
X free((char *) *sizes);
X return(-1);
X }
X (*IDs)[i] = (*sizes)[i] = 0;
X return(0);
X }
X}
X
X/*
X * Function: pop_retrieve
X *
X * Purpose: Retrieve a specified message from the maildrop.
X *
X * Arguments:
X * server The server to retrieve from.
X * message The message number to retrieve.
X * markfrom
X * If true, then mark the string "From " at the beginning
X * of lines with '>'.
X *
X * Return value: A string pointing to the message, if successful, or
X * null with pop_error set if not.
X *
X * Side effects: May kill connection on error.
X */
Xchar *pop_retrieve(server, message, markfrom)
XPopServer server;
Xint message;
Xint markfrom;
X{
X int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
X char *ptr, *fromserver;
X int ret;
X
X if (server->in_multi) {
X strcpy(pop_error, "In multi-line query in pop_retrieve");
X return(0);
X }
X
X if (pop_list(server, message, &IDs, &sizes))
X return(0);
X
X if (pop_retrieve_first(server, message, &fromserver)) {
X return(0);
X }
X
X /*
X * The "5" below is an arbitrary constant -- I assume that if
X * there are "From" lines in the text to be marked, there
X * probably won't be more than 5 of them. If there are, I
X * allocate more space for them below.
X */
X bufsize = sizes[0] + (markfrom ? 5 : 0);
X ptr = malloc(bufsize);
X free((char *) IDs);
X free((char *) sizes);
X
X if (! ptr) {
X strcpy(pop_error, "Out of memory in pop_retrieve");
X pop_retrieve_flush(server);
X return(0);
X }
X
X while (! (ret = pop_retrieve_next(server, &fromserver))) {
X int linesize;
X
X if (! fromserver) {
X ptr[cp] = '\0';
X return(ptr);
X }
X if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
X fromserver[2] == 'o' && fromserver[3] == 'm' &&
X fromserver[4] == ' ') {
X if (++fromcount == 5) {
X bufsize += 5;
X ptr = realloc(ptr, bufsize);
X if (! ptr) {
X strcpy(pop_error, "Out of memory in pop_retrieve");
X pop_retrieve_flush(server);
X return(0);
X }
X fromcount = 0;
X }
X ptr[cp++] = '>';
X }
X linesize = strlen(fromserver);
X bcopy(fromserver, &ptr[cp], linesize);
X cp += linesize;
X ptr[cp++] = '\n';
X }
X
X if (ret) {
X free(ptr);
X return(0);
X }
X}
X
Xint pop_retrieve_first(server, message, response)
XPopServer server;
Xint message;
Xchar **response;
X{
X sprintf(pop_error, "RETR %d", message);
X return(pop_multi_first(server, pop_error, response));
X}
X
Xint pop_retrieve_next(server, line)
XPopServer server;
Xchar **line;
X{
X return(pop_multi_next(server, line));
X}
X
Xint pop_retrieve_flush(server)
XPopServer server;
X{
X return(pop_multi_flush(server));
X}
X
Xint pop_top_first(server, message, lines, response)
XPopServer server;
Xint message, lines;
Xchar **response;
X{
X sprintf(pop_error, "TOP %d %d", message, lines);
X return(pop_multi_first(server, pop_error, response));
X}
X
Xint pop_top_next(server, line)
XPopServer server;
Xchar **line;
X{
X return(pop_multi_next(server, line));
X}
X
Xint pop_top_flush(server)
XPopServer server;
X{
X return(pop_multi_flush(server));
X}
X
Xint pop_multi_first(server, command, response)
XPopServer server;
Xchar *command;
Xchar **response;
X{
X if (server->in_multi) {
X strcpy(pop_error,
X "Already in multi-line query in pop_multi_first");
X return(-1);
X }
X
X if (sendline(server, command) || (! (*response = getline(server)))) {
X return(-1);
X }
X
X if (0 == strncmp(*response, "-ERR", 4)) {
X strncpy(pop_error, *response, ERROR_MAX);
X return(-1);
X }
X else if (0 == strncmp(*response, "+OK", 3)) {
X for (*response += 3; **response == ' '; (*response)++) /* empty */;
X server->in_multi = 1;
X return(0);
X }
X else {
X strcpy(pop_error,
X "Unexpected response from server in pop_multi_first");
X return(-1);
X }
X}
X
Xint pop_multi_next(server, line)
XPopServer server;
Xchar **line;
X{
X char *fromserver;
X
X if (! server->in_multi) {
X strcpy(pop_error, "Not in multi-line query in pop_multi_next");
X return(-1);
X }
X
X fromserver = getline(server);
X if (! fromserver) {
X return(-1);
X }
X
X if (fromserver[0] == '.') {
X if (! fromserver[1]) {
X *line = 0;
X server->in_multi = 0;
X return(0);
X }
X else {
X *line = fromserver + 1;
X return(0);
X }
X }
X else {
X *line = fromserver;
X return(0);
X }
X}
X
Xint pop_multi_flush(server)
XPopServer server;
X{
X char *line;
X
X if (! server->in_multi) {
X return(0);
X }
X
X while (! pop_multi_next(server, &line)) {
X if (! line) {
X return(0);
X }
X }
X
X return(-1);
X}
X
X/* Function: pop_delete
X *
X * Purpose: Delete a specified message.
X *
X * Arguments:
X * server Server from which to delete the message.
X * message Message to delete.
X *
X * Return value: 0 on success, non-zero with error in pop_error
X * otherwise.
X */
Xint pop_delete(server, message)
XPopServer server;
Xint message;
X{
X if (server->in_multi) {
X strcpy(pop_error, "In multi-line query in pop_delete");
X return(-1);
X }
X
X sprintf(pop_error, "DELE %d", message);
X
X if (sendline(server, pop_error) || getok(server))
X return(-1);
X
X return(0);
X}
X
X/*
X * Function: pop_noop
X *
X * Purpose: Send a noop command to the server.
X *
X * Argument:
X * server The server to send to.
X *
X * Return value: 0 on success, non-zero with error in pop_error
X * otherwise.
X *
X * Side effects: Closes connection on error.
X */
Xint pop_noop(server)
XPopServer server;
X{
X if (server->in_multi) {
X strcpy(pop_error, "In multi-line query in pop_noop");
X return(-1);
X }
X
X if (sendline(server, "NOOP") || getok(server))
X return(-1);
X
X return(0);
X}
X
X/*
X * Function: pop_last
X *
X * Purpose: Find out the highest seen message from the server.
X *
X * Arguments:
X * server The server.
X *
X * Return value: If successful, the highest seen message, which is
X * greater than or equal to 0. Otherwise, a negative number with
X * the error explained in pop_error.
X *
X * Side effects: Closes the connection on error.
X */
Xint pop_last(server)
XPopServer server;
X{
X char *fromserver;
X
X if (server->in_multi) {
X strcpy(pop_error, "In multi-line query in pop_last");
X return(-1);
X }
X
X if (sendline(server, "LAST"))
X return(-1);
X
X if (! (fromserver = getline(server)))
X return(-1);
X
X if (! strncmp(fromserver, "-ERR", 4)) {
X strncpy(pop_error, fromserver, ERROR_MAX);
X return(-1);
X }
X else if (strncmp(fromserver, "+OK ", 4)) {
X strcpy(pop_error, "Unexpected response from server in pop_last");
X pop_trash(server);
X return(-1);
X }
X else {
X return(atoi(&fromserver[4]));
X }
X}
X
X/*
X * Function: pop_reset
X *
X * Purpose: Reset the server to its initial connect state
X *
X * Arguments:
X * server The server.
X *
X * Return value: 0 for success, non-0 with error in pop_error
X * otherwise.
X *
X * Side effects: Closes the connection on error.
X */
Xint pop_reset(server)
XPopServer server;
X{
X if (pop_retrieve_flush(server)) {
X return(-1);
X }
X
X if (sendline(server, "RSET") || getok(server))
X return(-1);
X
X return(0);
X}
X
X/*
X * Function: pop_quit
X *
X * Purpose: Quit the connection to the server,
X *
X * Arguments:
X * server The server to quit.
X *
X * Return value: 0 for success, non-zero otherwise with error in
X * pop_error.
X *
X * Side Effects: The PopServer passed in is unuseable after this
X * function is called, even if an error occurs.
X */
Xint pop_quit(server)
XPopServer server;
X{
X int ret = 0;
X
X if (server->file >= 0) {
X if (pop_retrieve_flush(server)) {
X ret = -1;
X }
X
X if (sendline(server, "QUIT") || getok(server)) {
X ret = -1;
X }
X
X close(server->file);
X }
X
X if (server->buffer)
X free(server->buffer);
X free((char *) server);
X
X return(ret);
X}
X
X/*
X * Function: socket_connection
X *
X * Purpose: Opens the network connection with the mail host, without
X * doing any sort of I/O with it or anything.
X *
X * Arguments:
X * host The host to which to connect.
X * flags Option flags.
X *
X * Return value: A file descriptor indicating the connection, or -1
X * indicating failure, in which case an error has been copied
X * into pop_error.
X */
Xstatic int socket_connection(host, flags)
Xchar *host;
Xint flags;
X{
X struct hostent *hostent;
X struct servent *servent;
X struct sockaddr_in addr;
X char found_port = 0;
X char *service;
X int sock;
X#ifdef KERBEROS
X#ifdef KRB5
X krb5_error_code rem;
X krb5_ccache ccdef;
X krb5_principal client, server;
X krb5_error *err_ret;
X register char *cp;
X#else
X KTEXT ticket;
X MSG_DAT msg_data;
X CREDENTIALS cred;
X Key_schedule schedule;
X int rem;
X#endif /* KRB5 */
X#endif /* KERBEROS */
X
X int try_count = 0;
X
X do {
X hostent = gethostbyname(host);
X try_count++;
X if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5))) {
X strcpy(pop_error, "Could not determine POP server's address");
X return(-1);
X }
X } while (! hostent);
X
X bzero((char *) &addr, sizeof(addr));
X addr.sin_family = AF_INET;
X
X#ifdef KERBEROS
X service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
X#else
X service = POP_SERVICE;
X#endif
X
X#ifdef HESIOD
X if (! (flags & POP_NO_HESIOD)) {
X servent = hes_getservbyname(service, "tcp");
X if (servent) {
X addr.sin_port = servent->s_port;
X found_port = 1;
X }
X }
X#endif
X if (! found_port) {
X servent = getservbyname(service, "tcp");
X if (servent) {
X addr.sin_port = servent->s_port;
X }
X else {
X#ifdef KERBEROS
X addr.sin_port = htons((flags & POP_NO_KERBEROS) ?
X POP_PORT : KPOP_PORT);
X#else
X addr.sin_port = htons(POP_PORT);
X#endif
X }
X }
X
X#define SOCKET_ERROR "Could not create socket for POP connection: "
X
X sock = socket(PF_INET, SOCK_STREAM, 0);
X if (sock < 0) {
X strcpy(pop_error, SOCKET_ERROR);
X strncat(pop_error, strerror(errno),
X ERROR_MAX - sizeof(SOCKET_ERROR));
X return(-1);
X
X }
X
X while (*hostent->h_addr_list) {
X bcopy(*hostent->h_addr_list, (char *) &addr.sin_addr,
X hostent->h_length);
X if (! connect(sock, (struct sockaddr *) &addr, sizeof(addr)))
X break;
X hostent->h_addr_list++;
X }
X
X#define CONNECT_ERROR "Could not connect to POP server: "
X
X if (! *hostent->h_addr_list) {
X (void) close(sock);
X strcpy(pop_error, CONNECT_ERROR);
X strncat(pop_error, strerror(errno),
X ERROR_MAX - sizeof(CONNECT_ERROR));
X return(-1);
X
X }
X
X#ifdef KERBEROS
X#define KRB_ERROR "Kerberos error connecting to POP server: "
X if (! (flags & POP_NO_KERBEROS)) {
X#ifdef KRB5
X krb5_init_ets();
X
X if (rem = krb5_cc_default(&ccdef)) {
X krb5error:
X strcpy(pop_error, KRB_ERROR);
X strncat(pop_error, error_message(rem),
X ERROR_MAX - sizeof(KRB_ERROR));
X (void) close(sock);
X return(-1);
X }
X
X if (rem = krb5_cc_get_principal(ccdef, &client)) {
X goto krb5error;
X }
X
X for (cp = hostent->h_name; *cp; cp++) {
X if (isupper(*cp)) {
X *cp = tolower(*cp);
X }
X }
X
X if (rem = krb5_sname_to_principal(hostent->h_name, POP_SERVICE,
X FALSE, &server)) {
X goto krb5error;
X }
X
X rem = krb5_sendauth((krb5_pointer) &sock, "KPOPV1.0", client, server,
X AP_OPTS_MUTUAL_REQUIRED,
X 0, /* no checksum */
X 0, /* no creds, use ccache instead */
X ccdef,
X 0, /* don't need seq # */
X 0, /* don't need subsession key */
X &err_ret,
X 0); /* don't need reply */
X krb5_free_principal(server);
X if (rem) {
X if (err_ret && err_ret->text.length) {
X strcpy(pop_error, KRB_ERROR);
X strncat(pop_error, error_message(rem),
X ERROR_MAX - sizeof(KRB_ERROR));
X strncat(pop_error, " [server says '",
X ERROR_MAX - strlen(pop_error) - 1);
X strncat(pop_error, err_ret->text.data,
X min(ERROR_MAX - strlen(pop_error) - 1,
X err_ret->text.length));
X strncat(pop_error, "']",
X ERROR_MAX - strlen(pop_error) - 1);
X }
X else {
X strcpy(pop_error, KRB_ERROR);
X strncat(pop_error, error_message(rem),
X ERROR_MAX - sizeof(KRB_ERROR));
X }
X if (err_ret) {
X krb5_free_error(err_ret);
X }
X (void) close(sock);
X return(-1);
X }
X#else /* ! KRB5 */
X ticket = (KTEXT) malloc(sizeof(KTEXT_ST));
X rem = krb_sendauth(0L, sock, ticket, "pop", hostent->h_name,
X (char *) krb_realmofhost(hostent->h_name),
X (unsigned long) 0, &msg_data, &cred, schedule,
X (struct sockaddr_in *) 0,
X (struct sockaddr_in *) 0,
X "KPOPV0.1");
X free((char *) ticket);
X if (rem != KSUCCESS) {
X strcpy(pop_error, KRB_ERROR);
X strncat(pop_error, krb_err_txt[rem],
X ERROR_MAX - sizeof(KRB_ERROR));
X (void) close(sock);
X return(-1);
X }
X#endif /* KRB5 */
X }
X#endif /* KERBEROS */
X
X return(sock);
X} /* socket_connection */
X
X/*
X * Function: getline
X *
X * Purpose: Get a line of text from the connection and return a
X * pointer to it. The carriage return and linefeed at the end of
X * the line are stripped, but periods at the beginnings of lines
X * are NOT dealt with in any special way.
X *
X * Arguments:
X * server The server from which to get the line of text.
X *
X * Returns: A non-null pointer if successful, or a null pointer on any
X * error, with an error message copied into pop_error.
X *
X * Notes: The line returned is overwritten with each call to getline.
X *
X * Side effects: Closes the connection on error.
X */
Xstatic char *getline(server)
XPopServer server;
X{
X#define GETLINE_ERROR "Error reading from server: "
X
X int ret;
X
X if (server->data) {
X char *cp = strstr(server->buffer + server->buffer_index, "\r\n");
X if (cp) {
X int found;
X int data_used;
X
X found = server->buffer_index;
X data_used = (cp + 2) - server->buffer - found;
X
X *cp = '\0'; /* terminate the string to be returned */
X server->data -= data_used;
X server->buffer_index += data_used;
X
X if (pop_debug)
X fprintf(stderr, "<<< %s\n", server->buffer + found);
X return(server->buffer + found);
X }
X else {
X bcopy(server->buffer + server->buffer_index,
X server->buffer, server->data);
X server->buffer_index = 0;
X }
X }
X else {
X server->buffer_index = 0;
X }
X
X while (1) {
X if (server->data == server->buffer_size) {
X server->buffer_size += GETLINE_INCR;
X server->buffer = realloc(server->buffer, server->buffer_size);
X if (! server->buffer) {
X strcpy(pop_error, "Out of memory in getline");
X pop_trash(server);
X return(0);
X }
X }
X ret = read(server->file, server->buffer + server->data,
X server->buffer_size - server->data);
X if (ret < 0) {
X strcpy(pop_error, GETLINE_ERROR);
X strncat(pop_error, strerror(errno),
X ERROR_MAX - sizeof(GETLINE_ERROR));
X pop_trash(server);
X return(0);
X }
X else if (ret == 0) {
X strcpy(pop_error, "Unexpected EOF from server in getline");
X pop_trash(server);
X return(0);
X }
X else {
X char *cp = strstr(server->buffer, "\r\n");
X server->data += ret;
X
X if (cp) {
X int data_used = (cp + 2) - server->buffer;
X *cp = '\0';
X server->data -= data_used;
X server->buffer_index = data_used;
X
X if (pop_debug)
X fprintf(stderr, "<<< %s\n", server->buffer);
X return(server->buffer);
X }
X }
X }
X
X /* NOTREACHED */
X}
X
X/*
X * Function: sendline
X *
X * Purpose: Sends a line of text to the POP server. The line of text
X * passed into this function should NOT have the carriage return
X * and linefeed on the end of it. Periods at beginnings of lines
X * will NOT be treated specially by this function.
X *
X * Arguments:
X * server The server to which to send the text.
X * line The line of text to send.
X *
X * Return value: Upon successful completion, a value of 0 will be
X * returned. Otherwise, a non-zero value will be returned, and
X * an error will be copied into pop_error.
X *
X * Side effects: Closes the connection on error.
X */
Xstatic int sendline(server, line)
XPopServer server;
Xchar *line;
X{
X#define SENDLINE_ERROR "Error writing to POP server: "
X int ret;
X
X ret = fullwrite(server->file, line, strlen(line));
X if (ret >= 0) { /* 0 indicates that a blank line was written */
X ret = fullwrite(server->file, "\r\n", 2);
X }
X
X if (ret < 0) {
X pop_trash(server);
X strcpy(pop_error, SENDLINE_ERROR);
X strncat(pop_error, strerror(errno),
X ERROR_MAX - sizeof(SENDLINE_ERROR));
X return(ret);
X }
X
X if (pop_debug)
X fprintf(stderr, ">>> %s\n", line);
X
X return(0);
X}
X
X/*
X * Procedure: fullwrite
X *
X * Purpose: Just like write, but keeps trying until the entire string
X * has been written.
X *
X * Return value: Same as write. Pop_error is not set.
X */
Xstatic int fullwrite(fd, buf, nbytes)
Xint fd;
Xchar *buf;
Xint nbytes;
X{
X char *cp;
X int ret;
X
X cp = buf;
X while ((ret = write(fd, cp, nbytes)) > 0) {
X cp += ret;
X nbytes -= ret;
X }
X
X return(ret);
X}
X
X/*
X * Procedure getok
X *
X * Purpose: Reads a line from the server. If the return indicator is
X * positive, return with a zero exit status. If not, return with
X * a negative exit status.
X *
X * Arguments:
X * server The server to read from.
X *
X * Returns: 0 for success, else for failure and puts error in pop_error.
X *
X * Side effects: On failure, may make the connection unuseable.
X */
Xstatic int getok(server)
XPopServer server;
X{
X char *fromline;
X
X if (! (fromline = getline(server))) {
X return(-1);
X }
X
X if (! strncmp(fromline, "+OK", 3))
X return(0);
X else if (! strncmp(fromline, "-ERR", 4)) {
X strncpy(pop_error, fromline, ERROR_MAX);
X pop_error[ERROR_MAX-1] = '\0';
X return(-1);
X }
X else {
X strcpy(pop_error,
X "Unexpected response from server; expecting +OK or -ERR");
X pop_trash(server);
X return(-1);
X }
X}
X
X#if 0
X/*
X * Function: gettermination
X *
X * Purpose: Gets the next line and verifies that it is a termination
X * line (nothing but a dot).
X *
X * Return value: 0 on success, non-zero with pop_error set on error.
X *
X * Side effects: Closes the connection on error.
X */
Xstatic int gettermination(server)
XPopServer server;
X{
X char *fromserver;
X
X fromserver = getline(server);
X if (! fromserver)
X return(-1);
X
X if (strcmp(fromserver, ".")) {
X strcpy(pop_error,
X "Unexpected response from server in gettermination");
X pop_trash(server);
X return(-1);
X }
X
X return(0);
X}
X#endif
X
X/*
X * Function pop_close
X *
X * Purpose: Close a pop connection, sending a "RSET" command to try to
X * preserve any changes that were made and a "QUIT" command to
X * try to get the server to quit, but ignoring any responses that
X * are received.
X *
X * Side effects: The server is unuseable after this function returns.
X * Changes made to the maildrop since the session was started (or
X * since the last pop_reset) may be lost.
X */
Xvoid pop_close(server)
XPopServer server;
X{
X pop_trash(server);
X free((char *) server);
X
X return;
X}
X
X/*
X * Function: pop_trash
X *
X * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
X * memory associated with the server. It is legal to call
X * pop_close or pop_quit after this function has been called.
X */
Xstatic void pop_trash(server)
XPopServer server;
X{
X if (server->file >= 0) {
X sendline(server, "RSET");
X sendline(server, "QUIT");
X
X close(server->file);
X server->file = -1;
X if (server->buffer) {
X free(server->buffer);
X server->buffer = 0;
X }
X }
X}
X
X#endif /* MAIL_USE_POP */
END_OF_FILE
if test 31552 -ne `wc -c <'pop.c'`; then
echo shar: \"'pop.c'\" unpacked with wrong size!
fi
# end of 'pop.c'
fi
if test -f 'pop.h' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'pop.h'\"
else
echo shar: Extracting \"'pop.h'\" \(2487 characters\)
sed "s/^X//" >'pop.h' <<'END_OF_FILE'
X/*
X * pop.h: Header file for the "pop.c" client POP3 protocol
X * implementation.
X *
X * Copyright (c) 1991,1993 by Jonathan Kamens.
X *
X * Distribution and use of this file is governed by the GNU Public
X * License (version 1 or any later version).
X *
X * If you make any enhancements, bug fixes or porting changes to this
X * file, please make sure that they get sent to me so that I can
X * incorporate them into my master version of it. Try sending me
X * E-mail at jik@security.ov.com, or, if that fails, at
X * jik@athena.mit.edu.
X *
X * The current version of this file is $Revision: 1.1 $.
X */
X
X#include <stdio.h>
X
X#define GETLINE_MIN 1024 /* the getline buffer starts out this */
X /* size */
X#define GETLINE_INCR 1024 /* the getline buffer is grown by this */
X /* size when it needs to grow */
X
Xextern char pop_error[];
Xextern int pop_debug;
X
Xtypedef struct _PopServer {
X int file, data;
X char *buffer;
X int buffer_size, buffer_index;
X int in_multi;
X} *PopServer;
X
X/*
X * Valid flags for the pop_open function.
X */
X
X#define POP_NO_KERBEROS (1<<0)
X#define POP_NO_HESIOD (1<<1)
X#define POP_NO_GETPASS (1<<2)
X
X#ifdef __STDC__
X#define _ARGS(a) a
X#else
X#define _ARGS(a) ()
X#endif
X
Xextern PopServer pop_open _ARGS((char *host, char *username, char *password,
X int flags));
Xextern int pop_stat _ARGS((PopServer server, int *count, int *size));
Xextern int pop_list _ARGS((PopServer server, int message, int **IDs,
X int **size));
Xextern char *pop_retrieve _ARGS((PopServer server, int message, int markfrom));
Xextern int pop_retrieve_first _ARGS((PopServer server, int message,
X char **response));
Xextern int pop_retrieve_next _ARGS((PopServer server, char **line));
Xextern int pop_retrieve_flush _ARGS((PopServer server));
Xextern int pop_top_first _ARGS((PopServer server, int message, int lines,
X char **response));
Xextern int pop_top_next _ARGS((PopServer server, char **line));
Xextern int pop_top_flush _ARGS((PopServer server));
Xextern int pop_multi_first _ARGS((PopServer server, char *command,
X char **response));
Xextern int pop_multi_next _ARGS((PopServer server, char **line));
Xextern int pop_multi_flush _ARGS((PopServer server));
Xextern int pop_delete _ARGS((PopServer server, int message));
Xextern int pop_noop _ARGS((PopServer server));
Xextern int pop_last _ARGS((PopServer server));
Xextern int pop_reset _ARGS((PopServer server));
Xextern int pop_quit _ARGS((PopServer server));
Xextern void pop_close _ARGS((PopServer));
X
X#undef _ARGS
END_OF_FILE
if test 2487 -ne `wc -c <'pop.h'`; then
echo shar: \"'pop.h'\" unpacked with wrong size!
fi
# end of 'pop.h'
fi
if test -f 'strstr.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'strstr.c'\"
else
echo shar: Extracting \"'strstr.c'\" \(1721 characters\)
sed "s/^X//" >'strstr.c' <<'END_OF_FILE'
X#ifdef LOCAL_STRSTR
X
X/*-
X * Copyright (c) 1990 The Regents of the University of California.
X * All rights reserved.
X *
X * This code is derived from software contributed to Berkeley by
X * Chris Torek.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that: (1) source distributions retain this entire copyright
X * notice and comment, and (2) distributions including binaries display
X * the following acknowledgement: ``This product includes software
X * developed by the University of California, Berkeley and its contributors''
X * in the documentation or other materials provided with the distribution
X * and in all advertising materials mentioning features or use of this
X * software. Neither the name of the University nor the names of its
X * contributors may be used to endorse or promote products derived
X * from this software without specific prior written permission.
X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
X * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
X */
X
X#if defined(LIBC_SCCS) && !defined(lint)
Xstatic char sccsid[] = "@(#)strstr.c 5.1 (Berkeley) 5/15/90";
X#endif /* LIBC_SCCS and not lint */
X
X#include <sys/types.h>
X#include <string.h>
X#include <stdio.h>
X
X#ifndef __STDC__
X#define const
X#endif
X
X/*
X * Find the first occurrence of find in s.
X */
Xchar *
Xstrstr(s, find)
X register const char *s, *find;
X{
X register char c, sc;
X register size_t len;
X
X if ((c = *find++) != 0) {
X len = strlen(find);
X do {
X do {
X if ((sc = *s++) == 0)
X return (NULL);
X } while (sc != c);
X } while (strncmp(s, find, len) != 0);
X s--;
X }
X return ((char *)s);
X}
X
X#endif
END_OF_FILE
if test 1721 -ne `wc -c <'strstr.c'`; then
echo shar: \"'strstr.c'\" unpacked with wrong size!
fi
# end of 'strstr.c'
fi
echo shar: End of shell archive.
exit 0
Technical Details
Message-ID:
<199408140405.AAA15778@gza-client1.aktis.com>Path:
arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!redstone.interpath.net!ddsw1!news.kei.com!eff!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!cam.ov.COM!jikShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fb2f1,e7ea9ba44f1caa65,start X-Google-Attributes: gidfb2f1,public X-Google-ArrivalTime: 1994-08-14 03:04:45 PST Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!redstone.interpath.net!ddsw1!news.kei.com!eff!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!cam.ov.COM!jik From: jik@cam.ov.COM (Jonathan I. Kamens) Newsgroups: gnu.emacs.bug Subject: improved version of movemail Date: 14 Aug 1994 05:48:33 -0400 Organization: GNUs Not Usenet Lines: 2168 Sender: daemon@cis.ohio-state.edu Approved: bug-gnu-emacs@prep.ai.mit.edu Distribution: gnu Message-ID: <199408140405.AAA15778@gza-client1.aktis.com>