+ struct stat sb;
+ struct dirent *de;
+ DIR *dirp;
+ char path[_POSIX_PATH_MAX];
+ struct stat contex_sb;
+ time_t now, last1, last2;
+ CONTEXT *ctx;
+ int i = 0, local = 0, count = 0;
+
+ /* update postponed count as well, on force */
+ if (force == 1)
+ mutt_update_num_postponed ();
+
+ /* fastest return if there are no mailboxes */
+ if (!Incoming.len)
+ return 0;
+
+ now = time (NULL);
+ if (force == 0 && (now - BuffyTime < BuffyTimeout)
+ && (now - ImapBuffyTime < ImapBuffyTimeout))
+ return BuffyCount;
+
+ last1 = BuffyTime;
+ if (force == 1 || now - BuffyTime >= BuffyTimeout)
+ BuffyTime = now;
+ last2 = ImapBuffyTime;
+ if (force == 1 || now - ImapBuffyTime >= ImapBuffyTimeout)
+ ImapBuffyTime = now;
+ BuffyCount = 0;
+ BuffyNotify = 0;
+
+ count = sidebar_need_count();
+
+ if (!Context || !Context->path ||
+ (mx_is_local (Context->magic-1) && stat (Context->path, &contex_sb) != 0)) {
+ /* check device ID and serial number instead of comparing paths */
+ contex_sb.st_dev = 0;
+ contex_sb.st_ino = 0;
+ }
+
+ for (i = 0; i < Incoming.len; i++) {
+ BUFFY *tmp = Incoming.arr[i];
+ tmp->magic = mx_get_magic (tmp->path);
+ local = mx_is_local (tmp->magic-1);
+ if ((tmp->magic <= 0 || local) && (stat (tmp->path, &sb) != 0 || sb.st_size == 0)) {
+ /* if the mailbox still doesn't exist, set the newly created flag to
+ * be ready for when it does. */
+ tmp->newly_created = 1;
+ tmp->magic = -1;
+ continue;
+ }
+
+ /* check to see if the folder is the currently selected folder
+ * before polling */
+ if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev ||
+ sb.st_ino != contex_sb.st_ino) :
+ m_strcmp(tmp->path, Context->path))) {
+ switch (tmp->magic) {
+ case M_MBOX:
+ case M_MMDF:
+ /* only check on force or $mail_check reached */
+ if (force == 1 || (now - last1 >= BuffyTimeout)) {
+ if (!count) {
+ if (STAT_CHECK) {
+ BuffyCount++;
+ tmp->new = 1;
+ }
+ }
+ else if (STAT_CHECK || tmp->msgcount == 0) {
+ /* sidebar visible */
+ BuffyCount++;
+ if ((ctx =
+ mx_open_mailbox (tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_COUNT,
+ NULL)) != NULL) {
+ tmp->msgcount = ctx->msgcount;
+ tmp->new = ctx->new;
+ tmp->msg_unread = ctx->new; /* for sidebar, wtf? */
+ tmp->msg_flagged = ctx->flagged;
+ mx_close_mailbox (ctx, 0);
+ }
+ }
+ if (tmp->newly_created &&
+ (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
+ tmp->newly_created = 0;
+ }
+ else if (tmp->new > 0)
+ BuffyCount++;
+ break;
+
+ case M_MAILDIR:
+ /* only check on force or $mail_check reached */
+ if (force == 1 || (now - last1 >= BuffyTimeout)) {
+ snprintf (path, sizeof (path), "%s/new", tmp->path);
+ if ((dirp = opendir (path)) == NULL) {
+ tmp->magic = 0;
+ break;
+ }
+ tmp->new = 0;
+ tmp->msg_unread = 0;
+ tmp->msgcount = 0;
+ while ((de = readdir (dirp)) != NULL) {
+ char *p;
+
+ if (*de->d_name != '.' &&
+ (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T'))) {
+ /* one new and undeleted message is enough */
+ if (tmp->new == 0) {
+ BuffyCount++;
+ if (!count) {
+ /* if sidebar invisible -> done */
+ tmp->new = 1;
+ break;
+ }
+ }
+ tmp->msgcount++;
+ tmp->msg_unread++;
+ tmp->new++;
+ }
+ }
+ closedir (dirp);
+
+ if (count) {
+ /* only count total mail if sidebar visible */
+ snprintf (path, sizeof (path), "%s/cur", tmp->path);
+ if ((dirp = opendir (path)) == NULL) {
+ tmp->magic = 0;
+ break;
+ }
+ tmp->msg_flagged = 0;
+ while ((de = readdir (dirp)) != NULL) {
+ char *p;
+
+ if (*de->d_name != '.'
+ && (p = strstr (de->d_name, ":2,")) != NULL) {
+ if (!strchr (p + 3, 'T'))
+ tmp->msgcount++;
+ if (strchr (p + 3, 'F'))
+ tmp->msg_flagged++;
+ }
+ }
+ closedir (dirp);
+ }
+ }
+ else if (tmp->new > 0)
+ /* keep current stats if !force and !$mail_check reached */
+ BuffyCount++;
+ break;
+
+ case M_MH:
+ /* only check on force or $mail_check reached */
+ if (force == 1 || (now - last1 >= BuffyTimeout)) {
+ if ((tmp->new = mh_buffy (tmp->path)) > 0)
+ BuffyCount++;
+ if (count) {
+ DIR *dp;
+
+ if ((dp = opendir (path)) == NULL)
+ break;
+ tmp->new = 0;
+ tmp->msgcount = 0;
+ tmp->msg_unread = 0;
+ while ((de = readdir (dp))) {
+ if (mh_valid_message (de->d_name)) {
+ tmp->msgcount++;
+ tmp->msg_unread++;
+ tmp->new++;
+ }
+ }
+ closedir (dp);
+ }
+ }
+ else if (tmp->new > 0)
+ /* keep current stats if !force and !$mail_check reached */
+ BuffyCount++;
+ break;
+
+ case M_IMAP:
+ /* only check on force or $imap_mail_check reached */
+ if (force == 1 || (now - last2 >= ImapBuffyTimeout)) {
+ tmp->msgcount = imap_mailbox_check (tmp->path, 0);
+ tmp->new = imap_mailbox_check (tmp->path, 1);
+ tmp->msg_unread = imap_mailbox_check (tmp->path, 2);
+ if (tmp->new > 0)
+ BuffyCount++;
+ else
+ tmp->new = 0;
+ if (tmp->msg_unread < 0)
+ tmp->msg_unread = 0;
+ }
+ else if (tmp->new > 0)
+ /* keep current stats if !force and !$imap_mail_check reached */
+ BuffyCount++;
+ break;