Spamassassin: Sort -by-score feature for mutt.
Hi,
My colleague Bob just wrote the following patch. It implements sorting
by spamassassin score. There are "convert-to-mutt-score" configuration
hacks on the internet, but those are an incomplete hack.
The patch was built for our 1.3.8 debian-stable mutt, but applies
pretty cleanly to a 1.5.x tree.
Roger.
--
+-- Rogier Wolff -- www.harddisk-recovery.nl -- 0800 220 20 20 --
| Files foetsie, bestanden kwijt, alle data weg?!
| Blijf kalm en neem contact op met Harddisk-recovery.nl!
diff -ur mutt-1.3.28-new/commands.c mutt-1.3.28/commands.c
--- mutt-1.3.28-new/commands.c Wed Feb 25 15:06:13 2004
+++ mutt-1.3.28/commands.c Wed Feb 25 15:06:35 2004
@@ -459,9 +459,9 @@
int method = Sort; /* save the current method in case of abort */
switch (mutt_multi_choice (reverse ?
- _("Rev-Sort
(d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore?: ") :
- _("Sort
(d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore?: "),
- _("dfrsotuzc")))
+ _("Rev-Sort
(d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: ") :
+ _("Sort
(d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
+ _("dfrsotuzcp")))
{
case -1: /* abort - don't resort */
return -1;
@@ -500,6 +500,9 @@
case 9: /* s(c)ore */
Sort = SORT_SCORE;
+ break;
+ case 10: /* s(p)am */
+ Sort = SORT_SPAM;
break;
}
if (reverse)
diff -ur mutt-1.3.28-new/init.h mutt-1.3.28/init.h
--- mutt-1.3.28-new/init.h Wed Feb 25 15:06:15 2004
+++ mutt-1.3.28/init.h Wed Feb 25 15:09:32 2004
@@ -2381,6 +2381,7 @@
{ "threads", SORT_THREADS },
{ "to", SORT_TO },
{ "score", SORT_SCORE },
+ { "spam", SORT_SPAM },
{ NULL, 0 }
};
diff -ur mutt-1.3.28-new/mutt.h mutt-1.3.28/mutt.h
--- mutt-1.3.28-new/mutt.h Wed Feb 25 15:06:15 2004
+++ mutt-1.3.28/mutt.h Wed Feb 25 15:06:35 2004
@@ -527,6 +524,8 @@
LIST *references; /* message references (in reverse order) */
LIST *in_reply_to; /* in-reply-to header content */
LIST *userhdrs; /* user defined headers */
+
+ int x_spam_hits;
} ENVELOPE;
typedef struct parameter
diff -ur mutt-1.3.28-new/parse.c mutt-1.3.28/parse.c
--- mutt-1.3.28-new/parse.c Wed Feb 25 15:06:14 2004
+++ mutt-1.3.28/parse.c Wed Feb 25 15:07:36 2004
@@ -1207,6 +1207,14 @@
e->x_label = safe_strdup(p);
matched = 1;
}
+ else if (ascii_strcasecmp (line+1, "-Spam-Status") == 0)
+ {
+ char *s = strstr(p, "hits=");
+ if (s)
+ e->x_spam_hits = 100 * strtod(s + 5, NULL);
+
+ matched = 1;
+ }
default:
break;
diff -ur mutt-1.3.28-new/sort.c mutt-1.3.28/sort.c
--- mutt-1.3.28-new/sort.c Wed Feb 25 15:06:14 2004
+++ mutt-1.3.28/sort.c Wed Feb 25 15:06:35 2004
@@ -37,6 +37,17 @@
if (!code) \
code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
+int compare_spam (const void *a, const void *b)
+{
+ HEADER **pa = (HEADER **) a;
+ HEADER **pb = (HEADER **) b;
+
+ int result = (*pa)->env->x_spam_hits - (*pb)->env->x_spam_hits;
+ AUXSORT(result,a,b);
+ return (SORTCODE (result));
+}
+
+
int compare_score (const void *a, const void *b)
{
HEADER **pa = (HEADER **) a;
@@ -168,6 +179,8 @@
return (compare_to);
case SORT_SCORE:
return (compare_score);
+ case SORT_SPAM:
+ return (compare_spam);
default:
return (NULL);
}
diff -ur mutt-1.3.28-new/sort.h mutt-1.3.28/sort.h
--- mutt-1.3.28-new/sort.h Wed Feb 25 15:06:15 2004
+++ mutt-1.3.28/sort.h Wed Feb 25 15:06:35 2004
@@ -29,6 +29,7 @@
#define SORT_ADDRESS 11
#define SORT_KEYID 12
#define SORT_TRUST 13
+#define SORT_SPAM 14
#define SORT_MASK 0xf
#define SORT_REVERSE (1<<4)
#define SORT_LAST (1<<5)