/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2002-2012 by the Claws Mail Team and Hiroyuki Yamamoto
+ * Copyright (C) 2002-2014 by the Claws Mail Team and Hiroyuki Yamamoto
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
{MATCHCRITERIA_NOT_TAGGED, "~tagged"},
{MATCHCRITERIA_AGE_GREATER, "age_greater"},
{MATCHCRITERIA_AGE_LOWER, "age_lower"},
+ {MATCHCRITERIA_AGE_GREATER_HOURS, "age_greater_hours"},
+ {MATCHCRITERIA_AGE_LOWER_HOURS, "age_lower_hours"},
{MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
{MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
+ {MATCHCRITERIA_MESSAGEID, "messageid"},
+ {MATCHCRITERIA_NOT_MESSAGEID, "~messageid"},
{MATCHCRITERIA_INREPLYTO, "inreplyto"},
{MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
{MATCHCRITERIA_REFERENCES, "references"},
{MATCHCRITERIA_NOT_HEADER, "~header"},
{MATCHCRITERIA_HEADERS_PART, "headers_part"},
{MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
+ {MATCHCRITERIA_HEADERS_CONT, "headers_cont"},
+ {MATCHCRITERIA_NOT_HEADERS_CONT, "~headers_cont"},
{MATCHCRITERIA_MESSAGE, "message"},
{MATCHCRITERIA_NOT_MESSAGE, "~message"},
{MATCHCRITERIA_BODY_PART, "body_part"},
CONTEXT_TO,
CONTEXT_CC,
CONTEXT_NEWSGROUPS,
+ CONTEXT_MESSAGEID,
CONTEXT_IN_REPLY_TO,
CONTEXT_REFERENCES,
CONTEXT_HEADER,
context_str[CONTEXT_TO] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
context_str[CONTEXT_CC] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
context_str[CONTEXT_NEWSGROUPS] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Newsgroups:"));
+ context_str[CONTEXT_MESSAGEID] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Message-ID:"));
context_str[CONTEXT_IN_REPLY_TO] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("In-Reply-To:"));
context_str[CONTEXT_REFERENCES] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("References:"));
context_str[CONTEXT_HEADER] = g_strdup(_("header"));
prop->expr = expr != NULL ? g_strdup(expr) : NULL;
prop->matchtype = matchtype;
+#ifndef G_OS_WIN32
prop->preg = NULL;
+#endif
prop->value = value;
prop->error = 0;
{
g_free(prop->expr);
g_free(prop->header);
+#ifndef G_OS_WIN32
if (prop->preg != NULL) {
regfree(prop->preg);
g_free(prop->preg);
}
+#endif
g_free(prop);
}
prop->expr = src->expr ? g_strdup(src->expr) : NULL;
prop->matchtype = src->matchtype;
+#ifndef G_OS_WIN32
prop->preg = NULL; /* will be re-evaluated */
+#endif
prop->value = src->value;
prop->error = src->error;
return prop;
switch (prop->matchtype) {
case MATCHTYPE_REGEXPCASE:
case MATCHTYPE_REGEXP:
-#ifndef G_OS_WIN32
if (!prop->preg && (prop->error == 0)) {
prop->preg = g_new0(regex_t, 1);
/* if regexp then don't use the escaped string */
g_free(stripped);
}
break;
-#endif
case MATCHTYPE_MATCHCASE:
case MATCHTYPE_MATCH:
ret = (strstr(str1, down_expr) != NULL);
}
static gboolean matcherprop_header_line_match(MatcherProp *prop, const gchar *hdr,
- const gchar *str, const gchar *debug_context)
+ const gchar *str, const gboolean both,
+ const gchar *debug_context)
{
- gchar *line = NULL;
gboolean res = FALSE;
if (hdr == NULL || str == NULL)
return FALSE;
- line = g_strdup_printf("%s %s", hdr, str);
- res = matcherprop_string_match(prop, line, debug_context);
- g_free(line);
-
+ if (both) {
+ /* Search in all header names and content.
+ */
+ gchar *line = g_strdup_printf("%s %s", hdr, str);
+ res = matcherprop_string_match(prop, line, debug_context);
+ g_free(line);
+ } else {
+ /* Search only in content and exclude private headers.
+ * E.g.: searching for "H foo" in folder x/foo would return
+ * *all* mail as SCF and RMID will match.
+ * Searching for "H sent" would return all resent messages
+ * as "Resent-From: whatever" will match.
+ */
+ if (procheader_header_is_internal(hdr))
+ return FALSE;
+ res = matcherprop_string_match(prop, str, debug_context);
+ }
+
return res;
}
MsgInfo *info)
{
time_t t;
+ gint age_mult_hours = 1;
switch(prop->criteria) {
case MATCHCRITERIA_ALL:
case MATCHCRITERIA_NOT_TAGGED:
return info->tags == NULL;
case MATCHCRITERIA_AGE_GREATER:
+ age_mult_hours = 24;
+ /* Fallthrough intended */
+ case MATCHCRITERIA_AGE_GREATER_HOURS:
{
gboolean ret;
gint age;
t = time(NULL);
- age = ((t - info->date_t) / (60 * 60 * 24));
+ age = ((t - info->date_t) / (60 * 60 * age_mult_hours));
ret = (age >= prop->value);
/* debug output */
return ret;
}
case MATCHCRITERIA_AGE_LOWER:
+ age_mult_hours = 24;
+ /* Fallthrough intended */
+ case MATCHCRITERIA_AGE_LOWER_HOURS:
{
gboolean ret;
gint age;
t = time(NULL);
- age = ((t - info->date_t) / (60 * 60 * 24));
+ age = ((t - info->date_t) / (60 * 60 * age_mult_hours));
ret = (age < prop->value);
/* debug output */
return matcherprop_string_match(prop, info->newsgroups, context_str[CONTEXT_NEWSGROUPS]);
case MATCHCRITERIA_NOT_NEWSGROUPS:
return !matcherprop_string_match(prop, info->newsgroups, context_str[CONTEXT_NEWSGROUPS]);
+ case MATCHCRITERIA_MESSAGEID:
+ return matcherprop_string_match(prop, info->msgid, context_str[CONTEXT_MESSAGEID]);
+ case MATCHCRITERIA_NOT_MESSAGEID:
+ return !matcherprop_string_match(prop, info->msgid, context_str[CONTEXT_MESSAGEID]);
case MATCHCRITERIA_INREPLYTO:
return matcherprop_string_match(prop, info->inreplyto, context_str[CONTEXT_IN_REPLY_TO]);
case MATCHCRITERIA_NOT_INREPLYTO:
return cond;
}
+#ifdef G_OS_UNIX
+/*!
+ *\brief Builds a single regular expresion from an array of srings.
+ *
+ *\param strings The lines containing the different sub-regexp.
+ *
+ *\return The newly allocated regexp string.
+ */
+static gchar *build_complete_regexp(gchar **strings)
+{
+ int i = 0;
+ gchar *expr = NULL;
+ while (strings && strings[i] && *strings[i]) {
+ int old_len = expr ? strlen(expr):0;
+ int new_len = 0;
+ gchar *tmpstr = NULL;
+
+ if (g_utf8_validate(strings[i], -1, NULL))
+ tmpstr = g_strdup(strings[i]);
+ else
+ tmpstr = conv_codeset_strdup(strings[i],
+ conv_get_locale_charset_str_no_utf8(),
+ CS_INTERNAL);
+
+ if (strstr(tmpstr, "\n"))
+ *(strstr(tmpstr, "\n")) = '\0';
+
+ new_len = strlen(tmpstr);
+
+ expr = g_realloc(expr,
+ expr ? (old_len + strlen("|()") + new_len + 1)
+ : (strlen("()") + new_len + 1));
+
+ if (old_len) {
+ strcpy(expr + old_len, "|(");
+ strcpy(expr + old_len + 2, tmpstr);
+ strcpy(expr + old_len + 2 + new_len, ")");
+ } else {
+ strcpy(expr+old_len, "(");
+ strcpy(expr+old_len + 1, tmpstr);
+ strcpy(expr+old_len + 1 + new_len, ")");
+ }
+ g_free(tmpstr);
+ i++;
+ }
+ return expr;
+}
+#endif
+
+/*!
+ *\brief Create a new list of matchers from a multi-line string
+ *
+ *\param lines String with "\n"-separated expressions
+ *\param bool_and Operator
+ *\param case_sensitive If the matching is case sensitive or not
+ *
+ *\return MatcherList * New matcher list
+ */
+MatcherList *matcherlist_new_from_lines(gchar *lines, gboolean bool_and,
+ gboolean case_sensitive)
+{
+ MatcherProp *m = NULL;
+ GSList *matchers = NULL;
+ gchar **strings = g_strsplit(lines, "\n", -1);
+
+#ifdef G_OS_UNIX
+ gchar *expr = NULL;
+ expr = build_complete_regexp(strings);
+ debug_print("building matcherprop for expr '%s'\n", expr?expr:"NULL");
+
+ m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL,
+ case_sensitive? MATCHTYPE_REGEXP: MATCHTYPE_REGEXPCASE,
+ expr, 0);
+ if (m == NULL) {
+ /* print error message */
+ debug_print("failed to allocate memory for matcherprop\n");
+ } else {
+ matchers = g_slist_append(matchers, m);
+ }
+
+ g_free(expr);
+#else
+ int i = 0;
+ while (strings && strings[i] && *strings[i]) {
+ m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL,
+ case_sensitive? MATCHTYPE_MATCH: MATCHTYPE_MATCHCASE,
+ strings[i], 0);
+ if (m == NULL) {
+ /* print error message */
+ debug_print("failed to allocate memory for matcherprop\n");
+ } else {
+ matchers = g_slist_append(matchers, m);
+ }
+ i++;
+ }
+#endif
+ g_strfreev(strings);
+
+ return matcherlist_new(matchers, bool_and);
+}
+
/*!
*\brief Frees a list of matchers
*
}
break;
case MATCHCRITERIA_HEADERS_PART:
+ case MATCHCRITERIA_HEADERS_CONT:
case MATCHCRITERIA_MESSAGE:
header = procheader_parse_header(buf);
if (!header)
return FALSE;
result = matcherprop_header_line_match(matcher,
- header->name, header->body, context_str[CONTEXT_HEADER_LINE]);
+ header->name, header->body,
+ (matcher->criteria == MATCHCRITERIA_HEADERS_PART),
+ context_str[CONTEXT_HEADER_LINE]);
procheader_header_free(header);
return result;
+ case MATCHCRITERIA_NOT_HEADERS_CONT:
case MATCHCRITERIA_NOT_HEADERS_PART:
case MATCHCRITERIA_NOT_MESSAGE:
header = procheader_parse_header(buf);
if (!header)
return FALSE;
result = !matcherprop_header_line_match(matcher,
- header->name, header->body, context_str[CONTEXT_HEADER_LINE]);
+ header->name, header->body,
+ (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART),
+ context_str[CONTEXT_HEADER_LINE]);
procheader_header_free(header);
return result;
case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
gint match = MATCH_ONE;
gboolean found = FALSE;
- /* how many address headers are me trying to mach? */
+ /* how many address headers are we trying to match? */
if (strcasecmp(matcher->header, "Any") == 0)
match = MATCH_ANY;
else if (strcasecmp(matcher->header, "All") == 0)
case MATCHCRITERIA_HEADER:
case MATCHCRITERIA_NOT_HEADER:
case MATCHCRITERIA_HEADERS_PART:
+ case MATCHCRITERIA_HEADERS_CONT:
case MATCHCRITERIA_NOT_HEADERS_PART:
+ case MATCHCRITERIA_NOT_HEADERS_CONT:
case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
return TRUE;
/* determine the match range (all, any are our concern here) */
if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
+ matcher->criteria == MATCHCRITERIA_NOT_HEADERS_CONT ||
matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
match = MATCH_ALL;
gchar buf[BUFFSIZE];
GSList *l;
- if (partinfo->type == MIMETYPE_TEXT)
+ if (!partinfo || partinfo->type == MIMETYPE_TEXT)
return FALSE;
else
outfp = procmime_get_binary_content(partinfo);
/* Don't scan non-text parts when looking in body, only
* when looking in whole message
*/
- if (partinfo && partinfo->type != MIMETYPE_TEXT &&
- (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
- matcher->criteria == MATCHCRITERIA_BODY_PART))
+ if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
+ matcher->criteria == MATCHCRITERIA_BODY_PART)
continue;
/* if the criteria is ~body_part or ~message, ZERO lines
if (partinfo->type == MIMETYPE_TEXT) {
first_text_found = TRUE;
if (matcherlist_match_text_content(matchers, partinfo)) {
- procmime_mimeinfo_free_all(mimeinfo);
+ procmime_mimeinfo_free_all(&mimeinfo);
return TRUE;
}
} else if (matcherlist_match_binary_content(matchers, partinfo)) {
- procmime_mimeinfo_free_all(mimeinfo);
+ procmime_mimeinfo_free_all(&mimeinfo);
return TRUE;
}
if (body_only && first_text_found)
break;
}
- procmime_mimeinfo_free_all(mimeinfo);
+ procmime_mimeinfo_free_all(&mimeinfo);
return FALSE;
}
case MATCHCRITERIA_NOT_TAGGED:
case MATCHCRITERIA_AGE_GREATER:
case MATCHCRITERIA_AGE_LOWER:
+ case MATCHCRITERIA_AGE_GREATER_HOURS:
+ case MATCHCRITERIA_AGE_LOWER_HOURS:
case MATCHCRITERIA_NEWSGROUPS:
case MATCHCRITERIA_NOT_NEWSGROUPS:
+ case MATCHCRITERIA_MESSAGEID:
+ case MATCHCRITERIA_NOT_MESSAGEID:
case MATCHCRITERIA_INREPLYTO:
case MATCHCRITERIA_NOT_INREPLYTO:
case MATCHCRITERIA_REFERENCES:
switch (matcher->criteria) {
case MATCHCRITERIA_AGE_GREATER:
case MATCHCRITERIA_AGE_LOWER:
+ case MATCHCRITERIA_AGE_GREATER_HOURS:
+ case MATCHCRITERIA_AGE_LOWER_HOURS:
case MATCHCRITERIA_SCORE_GREATER:
case MATCHCRITERIA_SCORE_LOWER:
case MATCHCRITERIA_SCORE_EQUAL:
MATCHER_RC, NULL);
if ((pfile = prefs_write_open(rcpath)) == NULL) {
- g_warning("failed to write configuration to file\n");
+ g_warning("failed to write configuration to file");
g_free(rcpath);
return;
}
g_free(rcpath);
if (prefs_matcher_save(pfile->fp) < 0) {
- g_warning("failed to write configuration to file\n");
+ g_warning("failed to write configuration to file");
prefs_file_close_revert(pfile);
} else if (prefs_file_close(pfile) < 0) {
- g_warning("failed to save configuration to file\n");
- }
-}
-
-/* ******************************************************************* */
-
-static void matcher_add_rulenames(const gchar *rcpath)
-{
- gchar *newpath = g_strconcat(rcpath, ".new", NULL);
- FILE *src = g_fopen(rcpath, "rb");
- FILE *dst = g_fopen(newpath, "wb");
- gchar buf[BUFFSIZE];
- int r;
- if (src == NULL) {
- perror("fopen");
- if (dst)
- fclose(dst);
- g_free(newpath);
- return;
- }
- if (dst == NULL) {
- perror("fopen");
- if (src)
- fclose(src);
- g_free(newpath);
- return;
- }
-
- while (fgets (buf, sizeof(buf), src) != NULL) {
- if (strlen(buf) > 2 && buf[0] != '['
- && strncmp(buf, "rulename \"", 10)
- && strncmp(buf, "enabled rulename \"", 18)
- && strncmp(buf, "disabled rulename \"", 18)) {
- r = fwrite("enabled rulename \"\" ",
- strlen("enabled rulename \"\" "), 1, dst);
- if (r != 1) {
- g_message("cannot fwrite rulename\n");
- }
- }
- r = fwrite(buf, strlen(buf), 1, dst);
- if (r != 1) {
- g_message("cannot fwrite rule\n");
- }
+ g_warning("failed to save configuration to file");
}
- fclose(dst);
- fclose(src);
- move_file(newpath, rcpath, TRUE);
- g_free(newpath);
}
/*!
void prefs_matcher_read_config(void)
{
gchar *rcpath;
- gchar *rc_old_format;
FILE *f;
create_matchparser_hashtab();
prefs_filtering_clear();
rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
- rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
- ".pre_names", NULL);
-
- if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
- /* backup file with no rules names, in case
- * anything goes wrong */
- copy_file(rcpath, rc_old_format, FALSE);
- /* now hack the file in order to have it to the new format */
- matcher_add_rulenames(rcpath);
- }
-
- g_free(rc_old_format);
f = g_fopen(rcpath, "rb");
g_free(rcpath);
matcher_parser_start_parsing(f);
fclose(matcher_parserin);
}
- else {
- /* previous version compatibility */
-
- /* g_print("reading filtering\n"); */
- rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- FILTERING_RC, NULL);
- f = g_fopen(rcpath, "rb");
- g_free(rcpath);
-
- if (f != NULL) {
- matcher_parser_start_parsing(f);
- fclose(matcher_parserin);
- }
-
- /* g_print("reading scoring\n"); */
- rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- SCORING_RC, NULL);
- f = g_fopen(rcpath, "rb");
- g_free(rcpath);
-
- if (f != NULL) {
- matcher_parser_start_parsing(f);
- fclose(matcher_parserin);
- }
- }
}