From ba3744e5c73066aeaf922d901dd8e4540e26d03a Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Fri, 27 Oct 2000 22:20:55 +0000 Subject: Added header-starts-with, header-ends-with, and header-exists menu items. 2000-10-27 Jeffrey Stedfast * filtertypes.xml: Added header-starts-with, header-ends-with, and header-exists menu items. * filter-message-search.c (header_starts_with): New callback to match the beginnings of headers. (header_ends_with): New callback to match the ends of headers. (header_exists): New callback to determine if a header exists which is useful when filtering out all those pesky bug-buddy emails! svn path=/trunk/; revision=6242 --- filter/ChangeLog | 12 ++++ filter/filter-message-search.c | 144 +++++++++++++++++++++++++++++++++++++---- filter/filtertypes.xml | 115 ++++++++++++++++++++++++++++++++ filter/libfilter-i18n.h | 8 ++- 4 files changed, 264 insertions(+), 15 deletions(-) diff --git a/filter/ChangeLog b/filter/ChangeLog index 8d2f6cdcf8..f91063269a 100644 --- a/filter/ChangeLog +++ b/filter/ChangeLog @@ -1,3 +1,15 @@ +2000-10-27 Jeffrey Stedfast + + * filtertypes.xml: Added header-starts-with, header-ends-with, and + header-exists menu items. + + * filter-message-search.c (header_starts_with): New callback to + match the beginnings of headers. + (header_ends_with): New callback to match the ends of headers. + (header_exists): New callback to determine if a header exists + which is useful when filtering out all those pesky bug-buddy + emails! + 2000-10-27 Jeffrey Stedfast * filtertypes.xml: Add header-matches expressions ("is" / "is not"). diff --git a/filter/filter-message-search.c b/filter/filter-message-search.c index c8a85df71d..81955671d1 100644 --- a/filter/filter-message-search.c +++ b/filter/filter-message-search.c @@ -36,6 +36,9 @@ typedef struct { /* ESExp callbacks */ static ESExpResult *header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms); static ESExpResult *header_matches (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms); +static ESExpResult *header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms); +static ESExpResult *header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms); +static ESExpResult *header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms); static ESExpResult *header_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms); static ESExpResult *match_all (struct _ESExp *f, int argc, struct _ESExpTerm **argv, FilterMessageSearch *fms); static ESExpResult *body_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms); @@ -55,19 +58,22 @@ static struct { int type; /* set to 1 if a function can perform shortcut evaluation, or doesn't execute everything, 0 otherwise */ } symbols[] = { - { "match-all", (ESExpFunc *) match_all, 0 }, - { "body-contains", (ESExpFunc *) body_contains, 0 }, - { "body-regex", (ESExpFunc *) body_regex, 0 }, - { "header-contains", (ESExpFunc *) header_contains, 0 }, - { "header-matches", (ESExpFunc *) header_matches, 0 }, - { "header-regex", (ESExpFunc *) header_regex, 0 }, - { "user-tag", (ESExpFunc *) user_tag, 0 }, - { "user-flag", (ESExpFunc *) user_flag, 0 }, - { "get-sent-date", (ESExpFunc *) get_sent_date, 0 }, - { "get-received-date", (ESExpFunc *) get_received_date, 0 }, - { "get-current-date", (ESExpFunc *) get_current_date, 0 }, - { "get-score", (ESExpFunc *) get_score, 0 }, - { "get-source", (ESExpFunc *) get_source, 0 }, + { "match-all", (ESExpFunc *) match_all, 0 }, + { "body-contains", (ESExpFunc *) body_contains, 0 }, + { "body-regex", (ESExpFunc *) body_regex, 0 }, + { "header-contains", (ESExpFunc *) header_contains, 0 }, + { "header-matches", (ESExpFunc *) header_matches, 0 }, + { "header-starts-with", (ESExpFunc *) header_starts_with, 0 }, + { "header-ends-with", (ESExpFunc *) header_ends_with, 0 }, + { "header-exists", (ESExpFunc *) header_exists, 0 }, + { "header-regex", (ESExpFunc *) header_regex, 0 }, + { "user-tag", (ESExpFunc *) user_tag, 0 }, + { "user-flag", (ESExpFunc *) user_flag, 0 }, + { "get-sent-date", (ESExpFunc *) get_sent_date, 0 }, + { "get-received-date", (ESExpFunc *) get_received_date, 0 }, + { "get-current-date", (ESExpFunc *) get_current_date, 0 }, + { "get-score", (ESExpFunc *) get_score, 0 }, + { "get-source", (ESExpFunc *) get_source, 0 }, }; static ESExpResult * @@ -139,6 +145,118 @@ header_matches (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMe return r; } +static ESExpResult * +header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms) +{ + gboolean matched = FALSE; + ESExpResult *r; + + if (argc == 2) { + char *header = (argv[0])->value.string; + char *match = (argv[1])->value.string; + const char *contents; + + contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header); + + if (contents) { + /* danw says to use search-engine style matching... + * This means that if the search match string is + * lowercase then compare case-insensitive else + * compare case-sensitive. */ + gboolean is_lowercase = TRUE; + char *c; + + for (c = match; *c; c++) { + if (isalpha (*c) && isupper (*c)) { + is_lowercase = FALSE; + break; + } + } + + if (is_lowercase) { + if (g_strncasecmp (contents, match, strlen (match))) + matched = TRUE; + } else { + if (strncmp (contents, match, strlen (match))) + matched = TRUE; + } + } + } + + r = e_sexp_result_new (ESEXP_RES_BOOL); + r->value.bool = matched; + + return r; +} + +static ESExpResult * +header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms) +{ + gboolean matched = FALSE; + ESExpResult *r; + + if (argc == 2) { + char *header = (argv[0])->value.string; + char *match = (argv[1])->value.string; + const char *contents; + + contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header); + + if (contents && strlen (contents) >= strlen (match)) { + /* danw says to use search-engine style matching... + * This means that if the search match string is + * lowercase then compare case-insensitive else + * compare case-sensitive. */ + gboolean is_lowercase = TRUE; + char *c, *end; + + for (c = match; *c; c++) { + if (isalpha (*c) && isupper (*c)) { + is_lowercase = FALSE; + break; + } + } + + end = (char *) contents + strlen (contents) - strlen (match); + + if (is_lowercase) { + if (g_strcasecmp (end, match)) + matched = TRUE; + } else { + if (strcmp (end, match)) + matched = TRUE; + } + } + } + + r = e_sexp_result_new (ESEXP_RES_BOOL); + r->value.bool = matched; + + return r; +} + +static ESExpResult * +header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms) +{ + gboolean matched = FALSE; + ESExpResult *r; + + if (argc == 1) { + char *header = (argv[0])->value.string; + const char *contents; + + contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header); + + if (contents) + matched = TRUE; + } + + r = e_sexp_result_new (ESEXP_RES_BOOL); + r->value.bool = matched; + + return r; +} + static ESExpResult * header_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms) { diff --git a/filter/filtertypes.xml b/filter/filtertypes.xml index f1aa389ab1..38723dbf98 100644 --- a/filter/filtertypes.xml +++ b/filter/filtertypes.xml @@ -28,6 +28,31 @@ (match-all (not (header-matches "From" ${sender}))) + + + + + + + + + + + + + + + + + + +