From f448072225a25bba74cab0ceedcf349951cdaab3 Mon Sep 17 00:00:00 2001
From: "Bernhard R. Link" <brlink@debian.org>
Date: Sat, 19 Dec 2009 13:23:35 +0100
Subject: Retry autodetection when mailbox is not statable

See http://bugs.debian.org/168910
---
 xbuffy.c | 72 +++++++++++++++++++++++++++++++++++++---------------------------
 xbuffy.h |  1 +
 2 files changed, 43 insertions(+), 30 deletions(-)

diff --git a/xbuffy.c b/xbuffy.c
index 7d89b2c..81dee2c 100644
--- a/xbuffy.c
+++ b/xbuffy.c
@@ -210,7 +210,7 @@ void CheckBox(i)
     }
 #endif                          /* USE_NNTP */
     if ((boxInfo[i].type == MAILBOX) || (boxInfo[i].type == MAILDIR) ||
-	(boxInfo[i].type == MHDIR))
+	(boxInfo[i].type == MHDIR) || (boxInfo[i].type == UNKNOWNMBOX))
     {
         num = CountUnixMail(currentBox, NULL, &beenTouched);
     }
@@ -570,7 +570,7 @@ void PopupHeader(w, i, event, cont)
 
     /* update the number on the box (in case there are new articles) */
     if ((currentBox->type == MAILBOX) || (currentBox->type == MAILDIR) ||
-	(currentBox->type == MHDIR))
+	(currentBox->type == MHDIR) || (currentBox->type == UNKNOWNMBOX))
     {
         number = CountUnixMail(currentBox, mailHeaders, &beenTouched);
 
@@ -584,7 +584,7 @@ void PopupHeader(w, i, event, cont)
     UpdateBoxNumber(&boxInfo[i]);
 
     /* if the number is zero, there's no header, so leave */
-    if (boxInfo[i].n == 0)
+    if (boxInfo[i].n <= 0)
     {
         return;
     }
@@ -699,6 +699,34 @@ void BreakPopup(w, i, event, cont)
     headerUp[i] = FALSE;
 }
 
+void determineMBoxType(BoxInfo_t *b)
+{
+      struct stat st;
+      char tmp[_POSIX_PATH_MAX];
+
+      if (stat (b->box, &st) != -1) {
+	if (S_ISDIR (st.st_mode)) {
+	  /* check for maildir mailb->box */
+	  sprintf(tmp, "%s/cur", b->box);
+	  if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode)) {
+	    b->type = MAILDIR;
+	  } else {
+	    /* check for mh mailb->box */
+	    sprintf(tmp, "%s/.mh_sequences", b->box);
+	    if (access (tmp, F_OK) == 0) {
+	      b->type = MHDIR;
+	    } else {
+	      sprintf(tmp, "%s/.xmhcache", b->box);
+	      if (access (tmp, F_OK) == 0) {
+		b->type = MHDIR;
+	      }
+	    }
+	  }
+	} else
+	      b->type = MAILBOX;
+      } else
+	      b->type = UNKNOWNMBOX;
+}
 
 void ExecuteCommand(w, i, event, cont)
     Widget w;
@@ -978,6 +1006,12 @@ int CountUnixMail(mailBox, headerString, beenTouched)
 
     *beenTouched = FALSE;
    
+    if (mailBox->type == UNKNOWNMBOX) {
+	determineMBoxType(mailBox);
+    	if (mailBox->type == UNKNOWNMBOX)
+		return -1;
+    }
+
     if (mailBox->type == MAILBOX) {
       if (isLocked(mailBox->box))
         return (mailBox->n);
@@ -1015,8 +1049,7 @@ int CountUnixMail(mailBox, headerString, beenTouched)
       case MHDIR:
 	count = CountDirMail(mailBox, headerString);
 	break;
-      case NNTPBOX:
-      case CHECKFILE:
+      default:
 	return(0);
     }
 
@@ -1117,29 +1150,7 @@ void initBox(box, BoxType, pollTime, headerTime, BoxNameType, command, audioCmd,
       tempBox.type = CHECKFILE;
     } else if (BoxType == MAILBOX) 
     {
-      struct stat st;
-      char tmp[_POSIX_PATH_MAX];
-
-      if (stat (box, &st) != -1) {
-	if (S_ISDIR (st.st_mode)) {
-	  /* check for maildir mailbox */
-	  sprintf(tmp, "%s/cur", box);
-	  if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode)) {
-	    tempBox.type = MAILDIR;
-	  } else {
-	    /* check for mh mailbox */
-	    sprintf(tmp, "%s/.mh_sequences", box);
-	    if (access (tmp, F_OK) == 0) {
-	      tempBox.type = MHDIR;
-	    } else {
-	      sprintf(tmp, "%s/.xmhcache", box);
-	      if (access (tmp, F_OK) == 0) {
-		tempBox.type = MHDIR;
-	      }
-	    }
-	  }
-	}
-      }
+      determineMBoxType(&tempBox);
     }
     if ((pollTime <= 0) || (pollTime >= 3600))
         tempBox.pollTime = envPolltime;
@@ -1335,7 +1346,8 @@ int makeBoxTitle(currentBox)
     line[0] = '\0';
 
     if ((currentBox->type == MAILBOX) || (currentBox->type == MAILDIR) ||
-	(currentBox->type == MHDIR) || (currentBox->type == CHECKFILE))
+	(currentBox->type == MHDIR) || (currentBox->type == CHECKFILE) ||
+	(currentBox->type == UNKNOWN))
     {
         switch (currentBox->BoxNameType)
         {
@@ -1658,7 +1670,7 @@ int main(argc, argv)
         headerUp[i] = FALSE;
 
         if ((boxInfo[i].type == MAILBOX) || (boxInfo[i].type == MAILDIR) ||
-	    (boxInfo[i].type == MHDIR))
+	    (boxInfo[i].type == MHDIR) || (boxInfo[i].type == UNKNOWNMBOX))
             boxInfo[i].n = CountUnixMail(&boxInfo[i], NULL, &dummy);
 
         if (boxInfo[i].type == CHECKFILE)
diff --git a/xbuffy.h b/xbuffy.h
index 2c02a1c..e713364 100644
--- a/xbuffy.h
+++ b/xbuffy.h
@@ -45,6 +45,7 @@ enum BoxType_e {
   MHDIR,
   NNTPBOX,
   CHECKFILE,
+  UNKNOWNMBOX,
 };
 
 typedef enum BoxType_e BoxType_t;
