-- if (STAT_CHECK)
-+ {
-+ if (STAT_CHECK || tmp->msgcount == 0)
- {
-- BuffyCount++;
-- tmp->new = 1;
-+ BUFFY b = *tmp;
-+ int msgcount = 0;
-+ int msg_unread = 0;
-+ /* parse the mailbox, to see how much mail there is */
-+ ctx = mx_open_mailbox( tmp->path, M_READONLY | M_QUIET | M_NOSORT | M_PEEK, NULL);
-+ if(ctx)
-+ {
-+ msgcount = ctx->msgcount;
-+ msg_unread = ctx->unread;
-+ mx_close_mailbox(ctx, 0);
-+ }
-+ *tmp = b;
-+ tmp->msgcount = msgcount;
-+ tmp->msg_unread = msg_unread;
-+ if(STAT_CHECK) {
-+ tmp->has_new = tmp->new = 1;
-+ BuffyCount++;
-+ }
- }
- else if (option(OPTCHECKMBOXSIZE))
- {
-@@ -366,35 +385,86 @@
- if (tmp->newly_created &&
- (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
- tmp->newly_created = 0;
--
++/* update message counts for the sidebar */
++void buffy_maildir_update (BUFFY* mailbox)
++{
++ char path[_POSIX_PATH_MAX];
++ DIR *dirp;
++ struct dirent *de;
++ char *p;
++
++ mailbox->msgcount = 0;
++ mailbox->msg_unread = 0;
++ mailbox->msg_flagged = 0;
++
++ snprintf (path, sizeof (path), "%s/new", mailbox->path);
++
++ if ((dirp = opendir (path)) == NULL)
++ {
++ mailbox->magic = 0;
++ return;
++ }
++
++ while ((de = readdir (dirp)) != NULL)
++ {
++ if (*de->d_name == '.')
++ continue;
++
++ if (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')) {
++ mailbox->new = 1;
++ mailbox->msgcount++;
++ mailbox->msg_unread++;
++ }
++ }
++
++ closedir (dirp);
++ snprintf (path, sizeof (path), "%s/cur", mailbox->path);
++
++ if ((dirp = opendir (path)) == NULL)
++ {
++ mailbox->magic = 0;
++ return;
++ }
++
++ while ((de = readdir (dirp)) != NULL)
++ {
++ if (*de->d_name == '.')
++ continue;
++
++ if (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')) {
++ mailbox->msgcount++;
++ if ((p = strstr (de->d_name, ":2,"))) {
++ if (!strchr (p + 3, 'T')) {
++ if (!strchr (p + 3, 'S'))
++ mailbox->msg_unread++;
++ if (strchr(p + 3, 'F'))
++ mailbox->msg_flagged++;