]> Pileus Git - ~andy/fetchmail/blobdiff - imap.c
Abstract out host allocation.
[~andy/fetchmail] / imap.c
diff --git a/imap.c b/imap.c
index 87dfe19989d19a95c8d290bce75babb67d0ed503..ac13f068a6bcad94f78322f457a0d84809c096f4 100644 (file)
--- a/imap.c
+++ b/imap.c
  ***********************************************************************/
 
 #include  <config.h>
-#include  <varargs.h>
-
 #include  <stdio.h>
-#if defined(STDC_HEADERS)
 #include  <string.h>
-#endif
-#if defined(HAVE_UNISTD_H)
-#include  <unistd.h>
-#endif
-#include  <errno.h>
-
 #include  "socket.h"
 #include  "fetchmail.h"
 
-static int count, first;
-
 /*********************************************************************
 
  Method declarations for IMAP 
 
  *********************************************************************/
 
-static int exists, unseen, recent;
+static int count, seen;
 
-int imap_ok (argbuf,socket)
+int imap_ok (socket, argbuf)
 /* parse command response */
 char *argbuf;
 int socket;
 {
-  int ok;
-  char buf [POPBUFSIZE+1];
-  char *bufp;
-  int n;
-
-  do {
-    if (SockGets(socket, buf, sizeof(buf)) < 0)
-      return(PS_SOCKET);
-
-    if (outlevel == O_VERBOSE)
-      fprintf(stderr,"%s\n",buf);
-
-    /* interpret untagged status responses */
-    if (strstr(buf, "EXISTS"))
-       exists = atoi(buf+2);
-    if (strstr(buf, "RECENT"))
-       recent = atoi(buf+2);
-    if (sscanf(buf + 2, "OK [UNSEEN %d]", &n) == 1)
-       unseen = n;
-
-  } while
-      (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
-
-  if (tag[0] == '\0') {
-    strcpy(argbuf, buf);
-    return(0); 
-  }
-  else {
-    if (strncmp(buf + TAGLEN + 1, "OK", 2) == 0) {
-      strcpy(argbuf, buf + TAGLEN);
-      return(0);
+    int ok;
+    char buf [POPBUFSIZE+1];
+    char *bufp;
+    int n;
+
+    seen = 0;
+    do {
+       if (SockGets(socket, buf, sizeof(buf)) < 0)
+           return(PS_SOCKET);
+
+       if (outlevel == O_VERBOSE)
+           fprintf(stderr,"%s\n",buf);
+
+       /* interpret untagged status responses */
+       if (strstr(buf, "EXISTS"))
+           count = atoi(buf+2);
+       if (strstr(buf, "FLAGS"))
+           seen = (strstr(buf, "Seen") != (char *)NULL);
+    } while
+       (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
+
+    if (tag[0] == '\0')
+    {
+       strcpy(argbuf, buf);
+       return(0); 
     }
-    else if (strncmp(buf + TAGLEN + 1, "BAD", 2) == 0)
-      return(PS_ERROR);
     else
-      return(PS_PROTOCOL);
-  }
+    {
+       char    *cp;
+
+       /* skip the tag */
+       for (cp = buf; !isspace(*cp); cp++)
+           continue;
+       while (isspace(*cp))
+           cp++;
+
+       if (strncmp(cp, "OK", 2) == 0)
+       {
+           strcpy(argbuf, cp);
+           return(0);
+       }
+       else if (strncmp(cp, "BAD", 2) == 0)
+           return(PS_ERROR);
+       else
+           return(PS_PROTOCOL);
+    }
 }
 
 int imap_getauth(socket, queryctl, buf)
@@ -92,59 +90,50 @@ char *buf;
                  queryctl->remotename, queryctl->password));
 }
 
-static imap_getrange(socket, queryctl, countp, firstp)
+static imap_getrange(socket, queryctl, countp)
 /* get range of messages to be fetched */
 int socket;
 struct hostrec *queryctl;
 int *countp;
-int *firstp;
 {
     int ok;
 
     /* find out how many messages are waiting */
-    exists = unseen = recent = -1;
     ok = gen_transact(socket,
                  "SELECT %s",
-                 queryctl->remotefolder[0] ? queryctl->remotefolder : "INBOX");
+                 queryctl->mailbox[0] ? queryctl->mailbox : "INBOX");
     if (ok != 0)
        return(ok);
 
-    /* compute size of message run */
-    *countp = exists;
-    if (queryctl->fetchall)
-       *firstp = 1;
-    else {
-       if (exists > 0 && unseen == -1) {
-           fprintf(stderr,
-                   "no UNSEEN response; assuming all %d RECENT messages are unseen\n",
-                   recent);
-           *firstp = exists - recent + 1;
-       } else {
-           *firstp = unseen;
-       }
-    }
+    *countp = count;
 
     return(0);
 }
 
-static int imap_fetch(socket, number, limit, lenp)
+static imap_is_old(socket, queryctl, num)
+int socket;
+struct hostrec *queryctl;
+int num;
+{
+    char buf [POPBUFSIZE+1];
+    int ok;
+
+    if ((ok = gen_transact(socket, "FETCH %d FLAGS", num)) != 0)
+       exit(PS_ERROR);
+
+    return(seen);
+}
+
+static int imap_fetch(socket, number, lenp)
 /* request nth message */
 int socket;
 int number;
-int limit;
 int *lenp; 
 {
     char buf [POPBUFSIZE+1];
     int        num;
 
-    if (limit) 
-       gen_send(socket,
-                    "PARTIAL %d RFC822 0 %d",
-                    number, limit);
-    else 
-       gen_send(socket,
-                    "FETCH %d RFC822",
-                    number);
+    gen_send(socket, "FETCH %d RFC822", number);
 
     /* looking for FETCH response */
     do {
@@ -173,20 +162,30 @@ int number;
        return(0);
 }
 
-static struct method imap =
+static imap_delete(socket, queryctl, number)
+/* set delete flag for given message */
+int socket;
+struct hostrec *queryctl;
+int number;
+{
+    return(gen_transact(socket, "STORE %d +FLAGS (\\Deleted)", number));
+}
+
+const static struct method imap =
 {
-    "IMAP",                            /* Internet Message Access Protocol */
-    143,                               /* standard IMAP3bis/IMAP4 port */
-    1,                                 /* this is a tagged protocol */
-    0,                                 /* no message delimiter */
-    imap_ok,                           /* parse command response */
-    imap_getauth,                      /* get authorization */
-    imap_getrange,                     /* query range of messages */
-    imap_fetch,                                /* request given message */
-    imap_trail,                                /* eat message trailer */
-    "STORE %d +FLAGS (\\Deleted)",     /* set IMAP delete flag */
-    "EXPUNGE",                         /* the IMAP expunge command */
-    "LOGOUT",                          /* the IMAP exit command */
+    "IMAP",            /* Internet Message Access Protocol */
+    143,               /* standard IMAP2bis/IMAP4 port */
+    1,                 /* this is a tagged protocol */
+    0,                 /* no message delimiter */
+    imap_ok,           /* parse command response */
+    imap_getauth,      /* get authorization */
+    imap_getrange,     /* query range of messages */
+    imap_is_old,       /* no UID check */
+    imap_fetch,                /* request given message */
+    imap_trail,                /* eat message trailer */
+    imap_delete,       /* set IMAP delete flag */
+    "EXPUNGE",         /* the IMAP expunge command */
+    "LOGOUT",          /* the IMAP exit command */
 };
 
 int doIMAP (queryctl)
@@ -196,3 +195,4 @@ struct hostrec *queryctl;
     return(do_protocol(queryctl, &imap));
 }
 
+