Re: Maildir patch

Urs Janßen <[email protected]>
Newsgroups gmane.network.tin.devel
Organization tin.org
Message-ID <[email protected]>
Jessica Brennan [staff] wrote:
> I added the following patch to src/misc.c so that if the mailbox is a 
> maildir it will properly check to see if there is new mail.
> 
> --- src/misc.c.orig     2005-08-19 14:01:31.000000000 -0400
> +++ src/misc.c  2005-08-17 18:03:20.000000000 -0400
> @@ -51,6 +51,21 @@
>   #      include "rfc2046.h"
>   #endif /* !RFC2046_H */
> 
> +#ifdef HAVE_CONFIG_H
> +#       ifdef HAVE_DIRENT_H
> +#               include <dirent.h>
> +#               define DIR_BUF  struct dirent
> +#       else
> +#               ifdef HAVE_SYS_DIR_H
> +#                       include <sys/dir.h>
> +#               endif /* HAVE_SYS_DIR_H */
> +#               ifdef HAVE_SYS_NDIR_H
> +#                       include <sys/ndir.h>
> +#               endif /* HAVE_SYS_NDIR_H */
> +#               define DIR_BUF  struct direct
> +#       endif /* HAVE_DIRENT_H */
> +#endif /* HAVE_CONFIG_H */
> +

this is unnecessary as it's allready done in tin.h

>   /*
>    * defines to control GNKSA-checks behavior:
>    * - ENFORCE_RFC1034
> @@ -907,6 +922,10 @@
>   {
>          const char *mailbox_name;
>          struct stat buf;
> +        DIR *dirp;
> +        struct dirent *dp;
> +        char maildir_box[100];

using a fixed size is a bad idea...

> +
>   #ifdef M_AMIGA
>          static long mbox_size = 0;
>   #endif /* M_AMIGA */
> @@ -951,8 +970,30 @@
>                  }
>          }
>   #else
> -       if (mailbox_name != 0 && stat(mailbox_name, &buf) >= 0 && 
> buf.st_atime < buf.st_mtime && buf.st_size > 0)
> +       if (mailbox_name != 0 && stat(mailbox_name, &buf) >= 0)
> +        {
> +           /* For a maildir setup */
> +           if((int) (buf.st_mode & S_IFMT) == (int) S_IFDIR)
> +           {
> +              strncpy(maildir_box,  mailbox_name, 100);
> +              strcat(maildir_box, "/new");

as you overflow it here in case mailbox_name is longer than 96
chars...

below is a new version (diff is against the unstable tree,
backporting to the stable tree is trivial) which should not have the
buffer-overrun problem. as I don't do maildir I didn't test it.

--- misc.c.orig	2005-08-19 21:02:47.301495905 +0200
+++ misc.c	2005-08-19 21:25:52.611332616 +0200
@@ -912,6 +912,7 @@
  *
  * TODO: why not cache the mailbox_name?
  */
+#define MAILDIR_NEW	"/new"
 t_bool
 mail_check(
 	void)
@@ -921,8 +922,35 @@
 
 	mailbox_name = get_val("MAIL", mailbox);
 
-	if (mailbox_name != 0 && stat(mailbox_name, &buf) >= 0 && buf.st_atime < buf.st_mtime && buf.st_size > 0)
-		return TRUE;
+	if (mailbox_name != 0 && stat(mailbox_name, &buf) >= 0) {
+		if ((int) (buf.st_mode & S_IFMT) == (int) S_IFDIR) { /* maildir setup */
+			DIR *dirp;
+			char *maildir_box;
+			struct dirent *dp;
+
+			maildir_box = my_malloc(strlen(mailbox_name) + strlen(MAILDIR_NEW) + 1);
+#ifdef VMS
+			joindir(maildir_box, mailbox_name, MAILDIR_NEW);
+#else
+			joinpath(maildir_box, mailbox_name, MAILDIR_NEW);
+#endif /* VMS */
+			if (!(dirp = opendir(maildir_box))) {
+				free(maildir_box);
+				return FALSE;
+			}
+			free(maildir_box);
+			while ((dp = readdir(dirp)) != NULL) {
+				if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, ".."))) {
+					closedir(dirp);
+					return TRUE;
+				}
+			}
+			closedir(dirp);
+		} else {
+			if (buf.st_atime < buf.st_mtime && buf.st_size > 0)
+				return TRUE;
+		}
+	}
 	return FALSE;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.