]> Pileus Git - ~andy/fetchmail/blobdiff - pop3.c
Ready to ship.
[~andy/fetchmail] / pop3.c
diff --git a/pop3.c b/pop3.c
index 8f5a1a16d2cca803e9b2c909c0c528e8c1408d29..bf91bc9206c7dcbb1867b89202f993e06847ef6f 100644 (file)
--- a/pop3.c
+++ b/pop3.c
 #include  <stdlib.h>
 #endif
  
-#include  "socket.h"
 #include  "fetchmail.h"
+#include  "socket.h"
 
-#define PROTOCOL_ERROR {fputs("fetchmail: protocol error\n", stderr); return(PS_ERROR);}
+#define PROTOCOL_ERROR {error(0, 0, "protocol error"); return(PS_ERROR);}
+
+extern char *strstr(); /* needed on sysV68 R3V7.1. */
 
 static int last;
 
@@ -30,10 +32,8 @@ int pop3_ok (FILE *sockfp, char *argbuf)
     char buf [POPBUFSIZE+1];
     char *bufp;
 
-    if (SockGets(buf, sizeof(buf), sockfp) >= 0) {
-       if (outlevel == O_VERBOSE)
-           fprintf(stderr,"%s\n",buf);
-
+    if ((ok = gen_recv(sockfp, buf, sizeof(buf))) == 0)
+    {
        bufp = buf;
        if (*bufp == '+' || *bufp == '-')
            bufp++;
@@ -54,8 +54,6 @@ int pop3_ok (FILE *sockfp, char *argbuf)
        if (argbuf != NULL)
            strcpy(argbuf,bufp);
     }
-    else 
-       ok = PS_SOCKET;
 
     return(ok);
 }
@@ -64,7 +62,7 @@ int pop3_getauth(FILE *sockfp, struct query *ctl, char *greeting)
 /* apply for connection authorization */
 {
     /* build MD5 digest from greeting timestamp + password */
-    if (ctl->protocol == P_APOP) 
+    if (ctl->server.protocol == P_APOP) 
     {
        char *start,*end;
        char *msg;
@@ -73,7 +71,7 @@ int pop3_getauth(FILE *sockfp, struct query *ctl, char *greeting)
        for (start = greeting;  *start != 0 && *start != '<';  start++)
            continue;
        if (*start == 0) {
-           fprintf(stderr,"Required APOP timestamp not found in greeting\n");
+           error(0, 0, "Required APOP timestamp not found in greeting");
            return(PS_AUTHFAIL);
        }
 
@@ -81,7 +79,7 @@ int pop3_getauth(FILE *sockfp, struct query *ctl, char *greeting)
        for (end = start;  *end != 0  && *end != '>';  end++)
            continue;
        if (*end == 0 || end == start + 1) {
-           fprintf(stderr,"Timestamp syntax error in greeting\n");
+           error(0, 0, "Timestamp syntax error in greeting");
            return(PS_AUTHFAIL);
        }
        else
@@ -96,9 +94,9 @@ int pop3_getauth(FILE *sockfp, struct query *ctl, char *greeting)
        free(msg);
     }
 
-    switch (ctl->protocol) {
+    switch (ctl->server.protocol) {
     case P_POP3:
-       if ((gen_transact(sockfp,"USER %s", ctl->remotename)) != 0)
+       if ((gen_transact(sockfp, "USER %s", ctl->remotename)) != 0)
            PROTOCOL_ERROR
 
        if ((gen_transact(sockfp, "PASS %s", ctl->password)) != 0)
@@ -111,8 +109,16 @@ int pop3_getauth(FILE *sockfp, struct query *ctl, char *greeting)
            PROTOCOL_ERROR
        break;
 
+    case P_RPOP:
+       if ((gen_transact(sockfp,"USER %s", ctl->remotename)) != 0)
+           PROTOCOL_ERROR
+
+       if ((gen_transact(sockfp, "RPOP %s", ctl->password)) != 0)
+           PROTOCOL_ERROR
+       break;
+
     default:
-       fprintf(stderr,"Undefined protocol request in POP3_auth\n");
+       error(0, 0, "Undefined protocol request in POP3_auth");
     }
 
     /* we're approved */
@@ -165,15 +171,15 @@ static int pop3_getrange(FILE *sockfp, struct query *ctl, int*countp, int*newp)
                int     num;
 
                *newp = 0;
-               while (SockGets(buf, sizeof(buf), sockfp) >= 0)
+               while ((ok = gen_recv(sockfp, buf, sizeof(buf))) == 0)
                {
-                   if (outlevel == O_VERBOSE)
-                       fprintf(stderr,"%s\n",buf);
                    if (buf[0] == '.')
                        break;
                    else if (sscanf(buf, "%d %s", &num, id) == 2)
                    {
                        save_str(&ctl->newsaved, num, id);
+
+                       /* note: ID comparison is caseblind */
                        if (!str_in_list(&ctl->oldsaved, id))
                            (*newp)++;
                    }
@@ -196,12 +202,10 @@ static int pop3_getsizes(FILE *sockfp, int count, int *sizes)
     {
        char buf [POPBUFSIZE+1];
 
-       while (SockGets(buf, sizeof(buf), sockfp) >= 0)
+       while ((ok = gen_recv(sockfp, buf, sizeof(buf))) == 0)
        {
            int num, size;
 
-           if (outlevel == O_VERBOSE)
-               fprintf(stderr,"%s\n",buf);
            if (buf[0] == '.')
                break;
            else if (sscanf(buf, "%d %d", &num, &size) == 2)
@@ -210,7 +214,7 @@ static int pop3_getsizes(FILE *sockfp, int count, int *sizes)
                sizes[num - 1] = -1;
        }
 
-       return(0);
+       return(ok);
     }
 }
 
@@ -220,11 +224,12 @@ static int pop3_is_old(FILE *sockfp, struct query *ctl, int num)
     if (!ctl->oldsaved)
        return (num <= last);
     else
+       /* note: ID comparison is caseblind */
         return (str_in_list(&ctl->oldsaved,
                            str_find (&ctl->newsaved, num)));
 }
 
-static int pop3_fetch(FILE *sockfp, int number, int *lenp)
+static int pop3_fetch(FILE *sockfp, struct query *ctl, int number, int *lenp)
 /* request nth message */
 {
     int ok;
@@ -238,9 +243,9 @@ static int pop3_fetch(FILE *sockfp, int number, int *lenp)
        *lenp = 0;
     else
     {
-       while (--cp > buf && isdigit(*cp))
+       while (--cp >= buf && isdigit(*cp))
            continue;
-       *lenp = atoi(cp);
+       *lenp = atoi(++cp);
     }
     return(0);
 }
@@ -265,17 +270,17 @@ const static struct method pop3 =
     pop3_fetch,                /* request given message */
     NULL,              /* no message trailer */
     pop3_delete,       /* how to delete a message */
-    NULL,              /* no POP3 expunge command */
     "QUIT",            /* the POP3 exit command */
 };
 
 int doPOP3 (struct query *ctl)
 /* retrieve messages using POP3 */
 {
-    if (ctl->mailbox[0]) {
+    if (ctl->mailbox) {
        fprintf(stderr,"Option --remote is not supported with POP3\n");
        return(PS_SYNTAX);
     }
+    peek_capable = FALSE;
     return(do_protocol(ctl, &pop3));
 }