Andreas Krennmair:
[apps/madmutt.git] / imap / command.c
index c2d29fa..141af12 100644 (file)
 /* command.c: routines for sending commands to an IMAP server and parsing
  *  responses */
 
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "mutt.h"
 #include "imap_private.h"
 #include "message.h"
@@ -115,7 +119,7 @@ int imap_cmd_step (IMAP_DATA* idata)
     if (c <= 0)
     {
       dprint (1, (debugfile, "imap_cmd_step: Error reading server response.\n"));
-      cmd_handle_fatal (idata);
+      /* cmd_handle_fatal (idata); */
       return IMAP_CMD_BAD;
     }
 
@@ -206,6 +210,13 @@ int imap_exec (IMAP_DATA* idata, const char* cmd, int flags)
     rc = imap_cmd_step (idata);
   while (rc == IMAP_CMD_CONTINUE);
 
+  if (rc == IMAP_CMD_BAD) {
+    if (imap_reconnect(idata->ctx)!=0) {
+      return -1;
+    }
+    return 0;
+  }
+
   if (rc == IMAP_CMD_NO && (flags & IMAP_CMD_FAIL_OK))
     return -2;
 
@@ -286,12 +297,12 @@ static void cmd_handle_fatal (IMAP_DATA* idata)
       (idata->reopen & IMAP_REOPEN_ALLOW) &&
       !idata->ctx->closing)
   {
-    mx_fastclose_mailbox (idata->ctx);
-    /*mutt_error (_("Mailbox closed"));
+    /*mx_fastclose_mailbox (idata->ctx);*/
+    mutt_error (_("Mailbox closed"));
     mutt_sleep (1);
-    */
     idata->state = IMAP_DISCONNECTED;
-    imap_reconnect(idata->ctx);
+    if (imap_reconnect(idata->ctx)!=0)
+      mx_fastclose_mailbox(idata->ctx);
   }
 
   if (idata->state != IMAP_SELECTED)
@@ -375,12 +386,16 @@ static int cmd_handle_untagged (IMAP_DATA* idata)
     SKIPWS (s);
     mutt_error ("%s", s);
     idata->status = IMAP_BYE;
-    if (idata->state == IMAP_SELECTED)
-      mx_fastclose_mailbox (idata->ctx);
-    mutt_socket_close (idata->conn);
-    idata->state = IMAP_DISCONNECTED;
 
-    return -1;
+    /*if (imap_reconnect(idata->ctx)!=0) {
+        if (idata->state == IMAP_SELECTED)
+        mx_fastclose_mailbox (idata->ctx); */ /* XXX memleak? */
+      mutt_socket_close (idata->conn);
+      idata->state = IMAP_DISCONNECTED;
+      return -1;
+    /*} else {
+        return 0;
+      } */
   }
   else if (option (OPTIMAPSERVERNOISE) && (ascii_strncasecmp ("NO", s, 2) == 0))
   {
@@ -397,7 +412,7 @@ static int cmd_handle_untagged (IMAP_DATA* idata)
 /* cmd_make_sequence: make a tag suitable for starting an IMAP command */
 static void cmd_make_sequence (IMAP_DATA* idata)
 {
-  snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04d", idata->seqno++);
+  snprintf (idata->cmd.seq, sizeof (idata->cmd.seq), "a%04u", idata->seqno++);
 
   if (idata->seqno > 9999)
     idata->seqno = 0;
@@ -470,19 +485,20 @@ static void cmd_parse_fetch (IMAP_DATA* idata, char* s)
 
   msgno = atoi (s);
   
+  if (msgno <= idata->ctx->msgcount)
   /* see cmd_parse_expunge */
-  for (cur = 0; cur < idata->ctx->msgcount; cur++)
-  {
-    h = idata->ctx->hdrs[cur];
-    
-    if (h->active && h->index+1 == msgno)
+    for (cur = 0; cur < idata->ctx->msgcount; cur++)
     {
-      dprint (2, (debugfile, "Message UID %d updated\n", HEADER_DATA(h)->uid));
-      break;
+      h = idata->ctx->hdrs[cur];
+      
+      if (h->active && h->index+1 == msgno)
+      {
+       dprint (2, (debugfile, "Message UID %d updated\n", HEADER_DATA(h)->uid));
+       break;
+      }
+      
+      h = NULL;
     }
-
-    h = NULL;
-  }
   
   if (!h)
   {