]> Pileus Git - ~andy/fetchmail/blobdiff - uid.c
Bug fixes and internationalization improvements.
[~andy/fetchmail] / uid.c
diff --git a/uid.c b/uid.c
index 8b956fc1fc3d294054049165052bf5649734ddb4..8163761b1dbc010907f563000e940c3955543c6c 100644 (file)
--- a/uid.c
+++ b/uid.c
@@ -19,6 +19,7 @@
 #endif
 
 #include "fetchmail.h"
+#include "i18n.h"
 
 /*
  * Machinery for handling UID lists live here.  This is mainly to support
@@ -93,7 +94,7 @@ void initialize_saved_lists(struct query *hostlist, const char *idfile)
    if (lstat(idfile, &statbuf) < 0) {
      if (errno == ENOTDIR) 
     {
-      report(stderr, "lstat: %s: %s\n", idfile, strerror(errno));
+      report(stderr, _("lstat: %s: %s\n"), idfile, strerror(errno));
       exit(PS_IOERR);
     }
    }
@@ -113,58 +114,77 @@ void initialize_saved_lists(struct query *hostlist, const char *idfile)
 
        while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
        {
-         /*
-          * At this point, we assume the bug has two fields -- a user@host 
-          * part, and an ID part. Either field may contain spurious @ signs. 
-           * The previous version of this code presumed one could split at 
-           * the rightmost '@'.  This is not correct, as InterMail puts an 
-           * '@' in the UIDL.
-          */
+           /*
+            * At this point, we assume the bug has two fields -- a user@host 
+            * part, and an ID part. Either field may contain spurious @ signs.
+            * The previous version of this code presumed one could split at 
+            * the rightmost '@'.  This is not correct, as InterMail puts an 
+            * '@' in the UIDL.
+            */
          
-         /* very first, skip leading spaces */
-         user = buf + strspn(buf, " \t");
-
-         /* First, we split the buf into a userhost part and an id part */
-         if ( (delimp1 = strpbrk(user, " \t")) != NULL ) {
-               id = delimp1 + strspn(delimp1, " \t");  /* set pointer to id */
+           /* first, skip leading spaces */
+           user = buf + strspn(buf, " \t");
+
+           /*
+            * First, we split the buf into a userhost part and an id
+            * part ... but id doesn't necessarily start with a '<',
+            * espescially if the POP server returns an X-UIDL header
+            * instead of a Message-ID, as GMX's (www.gmx.net) POP3
+            * StreamProxy V1.0 does.
+            */
+           if ((id = strchr(user, ' ')) != NULL )
+           {
+               for (delimp1 = id; delimp1 >= user; delimp1--)
+                   if ((*delimp1 != ' ') && (*delimp1 != '\t'))
+                       break;
+
+               /* 
+                * It should be safe to assume that id starts after
+                * the " " - after all, we're writing the " "
+                * ourselves in write_saved_lists() :-)
+                */
+               id = id + strspn(id, " ");
+
+               delimp1++; /* but what if there is only white space ?!? */
                saveddelim1 = *delimp1; /* save char after token */
                *delimp1 = '\0';                /* delimit token with \0 */
-               if (id != NULL) {
-                       /* now remove trainling white space chars from id */
-                       if ( (delimp2 = strpbrk(id, " \t\n")) != NULL ) {
-                               saveddelim2 = *delimp2;
-                               *delimp2 = '\0';
-                       }
-                       atsign = strrchr(user, '@');
-                       if (atsign) {
-                               *atsign = '\0';
-                               host = atsign + 1;
-
-                       }
-                       for (ctl = hostlist; ctl; ctl = ctl->next) {
-                               if (ctl->server.truename &&
-                                   strcasecmp(host, ctl->server.truename) == 0
-                                   && strcasecmp(user, ctl->remotename) == 0) {
+               if (id != NULL) 
+               {
+                   /* now remove trailing white space chars from id */
+                   if ((delimp2 = strpbrk(id, " \t\n")) != NULL ) {
+                       saveddelim2 = *delimp2;
+                       *delimp2 = '\0';
+                   }
+                   atsign = strrchr(user, '@');
+                   if (atsign) {
+                       *atsign = '\0';
+                       host = atsign + 1;
+
+                   }
+                   for (ctl = hostlist; ctl; ctl = ctl->next) {
+                       if (ctl->server.truename &&
+                           strcasecmp(host, ctl->server.truename) == 0
+                           && strcasecmp(user, ctl->remotename) == 0) {
        
-                                       save_str(&ctl->oldsaved, id, UID_SEEN);
-                                       break;
-                               }
+                           save_str(&ctl->oldsaved, id, UID_SEEN);
+                           break;
                        }
-                       /* if it's not in a host we're querying,
-                       ** save it anyway */
-                       if (ctl == (struct query *)NULL) {
+                   }
+                   /* if it's not in a host we're querying,
+                   ** save it anyway */
+                   if (ctl == (struct query *)NULL) {
                                /* restore string */
-                               *delimp1 = saveddelim1;
-                               *atsign = '@';
-                               if (delimp2 != NULL) {
-                                       *delimp2 = saveddelim2;
-                               }
-                               save_str(&scratchlist, buf, UID_SEEN);
+                       *delimp1 = saveddelim1;
+                       *atsign = '@';
+                       if (delimp2 != NULL) {
+                           *delimp2 = saveddelim2;
                        }
+                       save_str(&scratchlist, buf, UID_SEEN);
+                   }
                }
            }
        }
-       fclose(tmpfp);
+       fclose(tmpfp);  /* not checking should be safe, mode was "r" */
     }
 
     if (outlevel >= O_DEBUG)
@@ -175,22 +195,23 @@ void initialize_saved_lists(struct query *hostlist, const char *idfile)
        for (ctl = hostlist; ctl; ctl = ctl->next)
            if (ctl->server.uidl)
            {
-               report_build(stdout, "Old UID list from %s:",ctl->server.pollname);
+               report_build(stdout, _("Old UID list from %s:"), 
+                            ctl->server.pollname);
                for (idp = ctl->oldsaved; idp; idp = idp->next)
                    report_build(stdout, " %s", idp->id);
                if (!idp)
-                   report_build(stdout, " <empty>");
+                   report_build(stdout, _(" <empty>"));
                report_complete(stdout, "\n");
                uidlcount++;
            }
 
        if (uidlcount)
        {
-           report_build(stdout, "Scratch list of UIDs:");
+           report_build(stdout, _("Scratch list of UIDs:"));
            for (idp = scratchlist; idp; idp = idp->next)
                report_build(stdout, " %s", idp->id);
            if (!idp)
-               report_build(stdout, " <empty>");
+               report_build(stdout, _(" <empty>"));
            report_complete(stdout, "\n");
        }
     }
@@ -365,6 +386,22 @@ int delete_str(struct idlist **idl, int num)
     return(0);
 }
 
+struct idlist *copy_str_list(struct idlist *idl)
+/* copy the given UID list */
+{
+    struct idlist *newnode ;
+
+    if (idl == (struct idlist *)NULL)
+       return(NULL);
+    else
+    {
+       newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
+       memcpy(newnode, idl, sizeof(struct idlist));
+       newnode->next = copy_str_list(idl->next);
+       return(newnode);
+    }
+}
+
 void append_str_list(struct idlist **idl, struct idlist **nidl)
 /* append nidl to idl (does not copy *) */
 {
@@ -389,27 +426,39 @@ void expunge_uids(struct query *ctl)
            idl->val.status.mark = UID_EXPUNGED;
 }
 
-void uid_end_query(struct query *ctl) 
+void uid_swap_lists(struct query *ctl) 
 /* finish a query */
 {
-    /* old state of mailbox is now irrelevant */
-    free_str_list(&ctl->oldsaved);
-    free_str_list(&scratchlist);
-    ctl->oldsaved = ctl->newsaved;
-    ctl->newsaved = (struct idlist *) NULL;
-
     /* debugging code */
     if (ctl->server.uidl && outlevel >= O_DEBUG)
     {
        struct idlist *idp;
 
-       report_build(stdout, "New UID list from %s:", ctl->server.pollname);
-       for (idp = ctl->oldsaved; idp; idp = idp->next)
+       report_build(stdout, _("New UID list from %s:"), ctl->server.pollname);
+       for (idp = ctl->newsaved; idp; idp = idp->next)
            report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
        if (!idp)
-           report_build(stdout, " <empty>");
+           report_build(stdout, _(" <empty>"));
        report_complete(stdout, "\n");
     }
+
+    /*
+     * Don't swap UID lists unless we've actually seen UIDLs.
+     * This is necessary in order to keep UIDL information
+     * from being heedlessly deleted later on.
+     */
+    if (ctl->newsaved)
+    {
+       /* old state of mailbox may now be irrelevant */
+       if (outlevel >= O_DEBUG)
+           report(stdout, _("swapping UID lists\n"));
+       free_str_list(&ctl->oldsaved);
+       free_str_list(&scratchlist);
+       ctl->oldsaved = ctl->newsaved;
+       ctl->newsaved = (struct idlist *) NULL;
+    }
+    else if (outlevel >= O_DEBUG)
+       report(stdout, _("not swapping UID lists, no UIDs seen this query\n"));
 }
 
 void write_saved_lists(struct query *hostlist, const char *idfile)
@@ -423,18 +472,23 @@ void write_saved_lists(struct query *hostlist, const char *idfile)
     /* if all lists are empty, nuke the file */
     idcount = 0;
     for (ctl = hostlist; ctl; ctl = ctl->next) {
-       if (ctl->oldsaved)
-           idcount++;
+        for (idp = ctl->oldsaved; idp; idp = idp->next)
+            if (idp->val.status.mark == UID_SEEN
+                               || idp->val.status.mark == UID_DELETED)
+                idcount++;
     }
 
     /* either nuke the file or write updated last-seen IDs */
     if (!idcount && !scratchlist)
     {
        if (outlevel >= O_DEBUG)
-           report(stdout, "Deleting fetchids file.\n");
+           report(stdout, _("Deleting fetchids file.\n"));
        unlink(idfile);
     }
     else
+    {
+       if (outlevel >= O_DEBUG)
+           report(stdout, _("Writing fetchids file.\n"));
        if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
            for (ctl = hostlist; ctl; ctl = ctl->next) {
                for (idp = ctl->oldsaved; idp; idp = idp->next)
@@ -447,6 +501,7 @@ void write_saved_lists(struct query *hostlist, const char *idfile)
                fputs(idp->id, tmpfp);
            fclose(tmpfp);
        }
+    }
 }
 #endif /* POP3_ENABLE */