Sam Varshavchik <[EMAIL PROTECTED]> writes:

> Lloyd Zusman writes:
>>
>> [ ... ]
>>
>> How about if I just put something in there that sorts the file names in
>> ascending ASCII sequence?  Then, I could make use of prefixes like
>> "00-", "01-", etc. to cause the filters to be invoked in the order that
>> I desire.
>> I'll have some sort of patch within a couple days.
>
> Sounds reasonable.

OK.  Here's the patch.  I did a small amount of testing, and it seems to
work fine, but please double-check everything yourself.

Note that I reuse a static list that grows as needed.  I only free the
individual entries, but not the list itself.  The list will never have
more entries than the next multiple of 8 higher than the maximum number
of filters and allfilters; therefore, it's quite small.

--- courier/cdfilters.C.orig	2006-03-19 17:16:09.000000000 -0500
+++ courier/cdfilters.C	2006-03-19 19:03:15.000000000 -0500
@@ -26,4 +26,53 @@
 	void *);
 
+#define FILTER_LIST_INCREMENT	8
+
+static char **filterlist   = NULL;
+static int  filterlistsize = 0;
+static int  nfilters	   = 0;
+
+static void free_filters()
+{
+	if (filterlist != NULL)
+	{
+		for (int n = 0; n < nfilters; n++)
+		{
+			if (filterlist[n] != NULL)
+			{
+				free(filterlist[n]);
+			}
+		}
+	}
+	nfilters = 0;
+}
+
+static int add_filter(const char *filter)
+{
+	if (nfilters >= filterlistsize)
+	{
+		if (filterlist == NULL)
+		{
+			filterlist = (char **) malloc(sizeof (char *) *
+						      FILTER_LIST_INCREMENT);
+		}
+		else
+		{
+			filterlist = (char **) realloc(filterlist,
+						       sizeof (char *) *
+						       (filterlistsize +
+							FILTER_LIST_INCREMENT));
+		}
+		if (filterlist == NULL)
+		{
+			cout << "432 Out of memory when processing mail filters.\n"
+			     << flush;
+			return (1);
+		}
+		filterlistsize += FILTER_LIST_INCREMENT;
+	}
+	filterlist[nfilters++] = strdup(filter);
+	return (0);
+}
+
 int run_filter(const char *filename,
 	unsigned nmsgids,
@@ -40,4 +89,5 @@
 	if (!iswhitelisted)
 	{
+		free_filters();
 		dirp=opendir(FILTERSOCKETDIR);
 		while (dirp && (de=readdir(dirp)) != 0)
@@ -47,16 +97,29 @@
 			sockname = FILTERSOCKETDIR "/";
 			sockname += de->d_name;
-			if (dofilter( sockname,
-					filename, nmsgids,
-					msgidfunc,
-					funcarg))
+			if (add_filter(sockname) != 0)
 			{
-				closedir(dirp);
 				return (1);
 			}
 		}
 		if (dirp)	closedir(dirp);
+
+		qsort((void *) filterlist,
+		      (size_t) nfilters,
+		      sizeof (char *),
+		      (int (*)(const void*, const void*)) strcmp);
+
+		for (int n = 0; n < nfilters; n++)
+		{
+			if (dofilter( filterlist[n],
+				      filename, nmsgids,
+				      msgidfunc,
+				      funcarg))
+			{
+				return (1);
+			}
+		}
 	}
 
+	free_filters();
 	dirp=opendir(ALLFILTERSOCKETDIR);
 	while (dirp && (de=readdir(dirp)) != 0)
@@ -66,14 +129,27 @@
 		sockname = ALLFILTERSOCKETDIR "/";
 		sockname += de->d_name;
-		if (dofilter( sockname,
-				filename, nmsgids,
-				msgidfunc,
-				funcarg))
+		if (add_filter(sockname) != 0)
 		{
-			closedir(dirp);
 			return (1);
 		}
 	}
 	if (dirp)	closedir(dirp);
+
+	qsort((void *) filterlist,
+	      (size_t) nfilters,
+	      sizeof (char *),
+	      (int (*)(const void*, const void*)) strcmp);
+
+	for (int n = 0; n < nfilters; n++)
+	{
+		if (dofilter( filterlist[n],
+			      filename, nmsgids,
+			      msgidfunc,
+			      funcarg))
+		{
+			return (1);
+		}
+	}
+
 	return (0);
 }

-- 
 Lloyd Zusman
 [EMAIL PROTECTED]
 God bless you.

Reply via email to