* src/matcher.c
[claws.git] / src / matcher.c
index 0b6410560f4d691e3374afb29611ee150c6350e0..f4765782b2f3e13b5917299b2a33331396b63328 100644 (file)
@@ -1,30 +1,3 @@
-/*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto & The Sylpheed Claws Team
- *
- * 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
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-/* 
- * initial     Hoa             initial
- *
- * 07/18/01    Alfons          when we want a file name from a MsgInfo, get that
- *                             from MsgInfo->folder if the message is being filtered
- *                             from incorporation. also some more safe string checking.
- */
-
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
@@ -83,6 +56,10 @@ static MatchParser matchparser_tab[] = {
        {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
        {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
 
+       {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
+       {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
+       {MATCHCRITERIA_SIZE_EQUAL,   "size_equal"},
+
        /* content have to be read */
        {MATCHCRITERIA_HEADER, "header"},
        {MATCHCRITERIA_NOT_HEADER, "~header"},
@@ -114,18 +91,12 @@ static MatchParser matchparser_tab[] = {
        {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
        {MATCHACTION_EXECUTE, "execute"},
        {MATCHACTION_COLOR, "color"},
-       {MATCHACTION_BOUNCE, "bounce"}
+       {MATCHACTION_BOUNCE, "bounce"},
+       {MATCHACTION_DELETE_ON_SERVER, "delete_on_server"}
 };
 
-/*
-  syntax for matcher
-
-  header "x-mailing" match "toto" score -10
-  subject match "regexp" & to regexp "regexp" score 50
-  subject match "regexp" | to regexpcase "regexp" | age_sup 5 score 30
-
-*/
-
+/* get_matchparser_tab_str() - used by filtering.c to translate 
+ * actions to debug strings */
 gchar * get_matchparser_tab_str(gint id)
 {
        gint i;
@@ -138,8 +109,38 @@ gchar * get_matchparser_tab_str(gint id)
        return NULL;
 }
 
+/* matcher_escape_str() - escapes a string returns newly allocated escaped string */
+gchar *matcher_escape_str(const gchar *str)
+{
+       register const gchar *walk;
+       register int escape;
+       gchar *res;
+       register char *reswalk;
+
+       if (str == NULL)
+               return NULL;
+
+       for (escape = 0, walk = str; *walk; walk++)
+               if (*walk == '\'' || *walk == '\"')
+                       escape++;
 
+       if (!escape)
+               return g_strdup(str);
+       
+       reswalk = res = g_new0(gchar, (walk - str) + escape + 1);
+       for (walk = str; *walk; walk++, reswalk++) {
+               if (*walk == '\'' || *walk == '\"')
+                       *reswalk++ = '\\';
+               *reswalk = *walk;
+       }
 
+       *reswalk = 0;
+       return res;
+}
+
+/* matcher_unescape_str() - assumes that unescaping frees up room
+ * in the string, so it returns the unescaped string in the 
+ * source */
 gchar *matcher_unescape_str(gchar *str)
 {
        gchar *tmp = alloca(strlen(str) + 1);
@@ -154,14 +155,23 @@ gchar *matcher_unescape_str(gchar *str)
                else {
                        src++;
                        if (*src == '\\')
-                               *dst++ = '\\';
-                       else if (*src == 'n')
+                               *dst++ = '\\';                          /* insert backslash */
+                       else if (*src == 'n')                           /* insert control characters */
                                *dst++ = '\n';
                        else if (*src == 'r') 
                                *dst++ = '\r';
-                       else if (*src == '\'' || *src == '\"')
+                       else if (*src == 't') 
+                               *dst++ = '\t';
+                       else if (*src == 'r') 
+                               *dst++ = '\r';
+                       else if (*src == 'b')
+                               *dst++ = '\b';
+                       else if (*src == 'f')
+                               *dst++ = '\f';
+                       else if (*src == '\'' || *src == '\"')          /* insert \' or \" */
                                *dst++ = *src;
                        else {
+                               /* FIXME: should perhaps escape character... */
                                src--;
                                *dst++ = *src;
                        }                               
@@ -173,7 +183,8 @@ gchar *matcher_unescape_str(gchar *str)
 
 /* **************** data structure allocation **************** */
 
-
+/* matcherprop_new() - allocates a structure for one condition
+ */
 MatcherProp * matcherprop_new(gint criteria, gchar * header,
                              gint matchtype, gchar * expr,
                              int value)
@@ -182,14 +193,22 @@ MatcherProp * matcherprop_new(gint criteria, gchar * header,
 
        prop = g_new0(MatcherProp, 1);
        prop->criteria = criteria;
-       if (header != NULL)
-               prop->header = g_strdup(header);
-       else
-               prop->header = NULL;
-       if (expr != NULL)
-               prop->expr = g_strdup(expr);
-       else
-               prop->expr = NULL;
+       if (header != NULL) {
+               prop->header       = g_strdup(header);
+               prop->unesc_header = matcher_unescape_str(g_strdup(header)); 
+       }       
+       else {
+               prop->header       = NULL;
+               prop->unesc_header = NULL;
+       }       
+       if (expr != NULL) {
+               prop->expr       = g_strdup(expr);
+               prop->unesc_expr = matcher_unescape_str(g_strdup(expr));
+       }       
+       else {
+               prop->expr       = NULL;
+               prop->unesc_expr = NULL;
+       }       
        prop->matchtype = matchtype;
        prop->preg = NULL;
        prop->value = value;
@@ -198,9 +217,18 @@ MatcherProp * matcherprop_new(gint criteria, gchar * header,
        return prop;
 }
 
+/* matcherprop_free()
+ */
 void matcherprop_free(MatcherProp * prop)
 {
-       g_free(prop->expr);
+       if (prop->expr) 
+               g_free(prop->expr);
+       if (prop->unesc_expr) 
+               g_free(prop->unesc_expr);
+       if (prop->header)
+               g_free(prop->header);
+       if (prop->unesc_header) 
+               g_free(prop->unesc_header);
        if (prop->preg != NULL) {
                regfree(prop->preg);
                g_free(prop->preg);
@@ -212,8 +240,8 @@ void matcherprop_free(MatcherProp * prop)
 /* ************** match ******************************/
 
 
-/* match the given string */
-
+/* matcherprop_string_match() - finds out if a string matches
+ * with a criterium */
 static gboolean matcherprop_string_match(MatcherProp * prop, gchar * str)
 {
        gchar * str1;
@@ -227,6 +255,7 @@ static gboolean matcherprop_string_match(MatcherProp * prop, gchar * str)
        case MATCHTYPE_REGEXP:
                if (!prop->preg && (prop->error == 0)) {
                        prop->preg = g_new0(regex_t, 1);
+                       /* if regexp then don't use the escaped string */
                        if (regcomp(prop->preg, prop->expr,
                                    REG_NOSUB | REG_EXTENDED
                                    | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
@@ -244,11 +273,12 @@ static gboolean matcherprop_string_match(MatcherProp * prop, gchar * str)
                        return FALSE;
 
        case MATCHTYPE_MATCH:
-               return (strstr(str, prop->expr) != NULL);
+               return (strstr(str, prop->unesc_expr) != NULL);
 
+       /* FIXME: put upper in unesc_str */
        case MATCHTYPE_MATCHCASE:
-               str2 = alloca(strlen(prop->expr) + 1);
-               strcpy(str2, prop->expr);
+               str2 = alloca(strlen(prop->unesc_expr) + 1);
+               strcpy(str2, prop->unesc_expr);
                g_strup(str2);
                str1 = alloca(strlen(str) + 1);
                strcpy(str1, str);
@@ -262,13 +292,22 @@ static gboolean matcherprop_string_match(MatcherProp * prop, gchar * str)
 
 gboolean matcherprop_match_execute(MatcherProp * prop, MsgInfo * info)
 {
+       gchar * file;
        gchar * cmd;
+       gint retval;
+
+       file = procmsg_get_message_file(info);
+       if (file == NULL)
+               return FALSE;
 
-       cmd = matching_build_command(prop->expr, info);
+       cmd = matching_build_command(prop->unesc_expr, info);
        if (cmd == NULL)
                return FALSE;
 
-       return (system(cmd) == 0);
+       retval = system(cmd);
+       debug_print(_("Command exit code: %i\n"), retval);
+
+       return (retval == 0);
 }
 
 /* match a message and his headers, hlist can be NULL if you don't
@@ -339,6 +378,15 @@ gboolean matcherprop_match(MatcherProp * prop, MsgInfo * info)
                return info->score <= prop->value;
        case MATCHCRITERIA_SCORE_EQUAL:
                return info->score == prop->value;
+       case MATCHCRITERIA_SIZE_GREATER:
+               /* FIXME: info->size is an off_t */
+               return info->size > (off_t) prop->value;
+       case MATCHCRITERIA_SIZE_EQUAL:
+               /* FIXME: info->size is an off_t */
+               return info->size == (off_t) prop->value;
+       case MATCHCRITERIA_SIZE_SMALLER:
+               /* FIXME: info->size is an off_t */
+               return info->size <  (off_t) prop->value;
        case MATCHCRITERIA_NEWSGROUPS:
                return matcherprop_string_match(prop, info->newsgroups);
        case MATCHCRITERIA_NOT_NEWSGROUPS:
@@ -598,12 +646,11 @@ gboolean matcherlist_match_file(MatcherList * matchers, MsgInfo * info,
        GSList * l;
        FILE * fp;
        gchar * file;
-       gboolean is_incorporating = MSG_IS_FILTERING(info->flags);
-       
-       /* check which things we need to do */
+
+       /* file need to be read ? */
+
        read_headers = FALSE;
-       read_body    = FALSE;
-       
+       read_body = FALSE;
        for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
                MatcherProp * matcher = (MatcherProp *) l->data;
 
@@ -621,13 +668,11 @@ gboolean matcherlist_match_file(MatcherList * matchers, MsgInfo * info,
        if (!read_headers && !read_body)
                return result;
 
-       file = is_incorporating ? g_strdup(info->folder) 
-               : procmsg_get_message_file(info);
-               
+       file = procmsg_get_message_file(info);
        if (file == NULL)
                return FALSE;
 
-       if ((fp = fopen(file, "r")) == NULL) {
+       if ((fp = fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                g_free(file);
                return result;
@@ -652,8 +697,8 @@ gboolean matcherlist_match_file(MatcherList * matchers, MsgInfo * info,
                MatcherProp * matcher = (MatcherProp *) l->data;
 
                if (matcherprop_criteria_headers(matcher) ||
-                   matcherprop_criteria_body(matcher) ||
-                   matcherprop_criteria_message(matcher))
+                   matcherprop_criteria_body(matcher)    ||
+                   matcherprop_criteria_message(matcher)) {
                        if (matcher->result) {
                                if (!matchers->bool_and) {
                                        result = TRUE;
@@ -665,7 +710,8 @@ gboolean matcherlist_match_file(MatcherList * matchers, MsgInfo * info,
                                        result = FALSE;
                                        break;
                                }
-               }
+                       }
+               }                       
        }
 
        g_free(file);
@@ -727,6 +773,9 @@ gboolean matcherlist_match(MatcherList * matchers, MsgInfo * info)
                case MATCHCRITERIA_SCORE_GREATER:
                case MATCHCRITERIA_SCORE_LOWER:
                case MATCHCRITERIA_SCORE_EQUAL:
+               case MATCHCRITERIA_SIZE_GREATER:
+               case MATCHCRITERIA_SIZE_SMALLER:
+               case MATCHCRITERIA_SIZE_EQUAL:
                case MATCHCRITERIA_EXECUTE:
                case MATCHCRITERIA_NOT_EXECUTE:
                        if (matcherprop_match(matcher, info)) {
@@ -783,8 +832,10 @@ gchar * matcherprop_to_string(MatcherProp * matcher)
        case MATCHCRITERIA_SCORE_GREATER:
        case MATCHCRITERIA_SCORE_LOWER:
        case MATCHCRITERIA_SCORE_EQUAL:
+       case MATCHCRITERIA_SIZE_GREATER:
+       case MATCHCRITERIA_SIZE_SMALLER:
+       case MATCHCRITERIA_SIZE_EQUAL:
                return g_strdup_printf("%s %i", criteria_str, matcher->value);
-               break;
        case MATCHCRITERIA_ALL:
        case MATCHCRITERIA_UNREAD:
        case MATCHCRITERIA_NOT_UNREAD:
@@ -799,6 +850,9 @@ gchar * matcherprop_to_string(MatcherProp * matcher)
        case MATCHCRITERIA_FORWARDED:
        case MATCHCRITERIA_NOT_FORWARDED:
                return g_strdup(criteria_str);
+       case MATCHCRITERIA_EXECUTE:
+       case MATCHCRITERIA_NOT_EXECUTE:
+               return g_strdup_printf("%s \"%s\"", criteria_str, matcher->expr);
        }
 
        matchtype_str = NULL;
@@ -816,52 +870,16 @@ gchar * matcherprop_to_string(MatcherProp * matcher)
        case MATCHTYPE_MATCHCASE:
        case MATCHTYPE_REGEXP:
        case MATCHTYPE_REGEXPCASE:
-               count = 0;
-               for(p = matcher->expr; *p != 0 ; p++)
-                       if (*p == '\"') count ++;
-               
-               expr_str = g_new(char, strlen(matcher->expr) + count + 1);
-
-               for(p = matcher->expr, out = expr_str ; *p != 0 ; p++, out++) {
-                       if (*p == '\"') {
-                               *out = '\\'; out++;
-                               *out = '\"';
-                       }
-                       else
-                               *out = *p;
-               }
-               * out = '\0';
-
                if (matcher->header)
                        matcher_str =
                                g_strdup_printf("%s \"%s\" %s \"%s\"",
                                           criteria_str, matcher->header,
-                                          matchtype_str, expr_str);
+                                          matchtype_str, matcher->expr);
                else
                        matcher_str =
                                g_strdup_printf("%s %s \"%s\"", criteria_str,
-                                               matchtype_str, expr_str);
-               
-               g_free(expr_str);
-               
-               break;
-
-               /*
-       case MATCHTYPE_REGEXP:
-       case MATCHTYPE_REGEXPCASE:
-
-               if (matcher->header)
-                       matcher_str =
-                               g_strdup_printf("%s \"%s\" %s /%s/",
-                                               criteria_str, matcher->header,
                                                matchtype_str, matcher->expr);
-               else
-                       matcher_str =
-                               g_strdup_printf("%s %s /%s/", criteria_str,
-                                               matchtype_str, matcher->expr);
-
                break;
-               */
        }
 
        return matcher_str;
@@ -898,19 +916,7 @@ gchar * matcherlist_to_string(MatcherList * matchers)
        return result;
 }
 
-static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
-{
-       if (str) 
-               return strlen(str);
-       else {
-               debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
-               return 0;
-       }
-}
-
-#define STRLEN_WITH_CHECK(expr) \
-       strlen_with_check(#expr, __LINE__, expr)
-
+/* matching_build_command() - preferably cmd should be unescaped */
 gchar * matching_build_command(gchar * cmd, MsgInfo * info)
 {
        gchar * s = cmd;
@@ -919,8 +925,6 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
        gchar * p;
        gint size;
 
-       matcher_unescape_str(cmd);
-
        size = strlen(cmd) + 1;
        while (*s != '\0') {
                if (*s == '%') {
@@ -930,37 +934,35 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
                                size -= 1;
                                break;
                        case 's': /* subject */
-                               size += STRLEN_WITH_CHECK(info->subject) - 2;
+                               size += strlen(info->subject) - 2;
                                break;
                        case 'f': /* from */
-                               size += STRLEN_WITH_CHECK(info->from) - 2;
+                               size += strlen(info->from) - 2;
                                break;
                        case 't': /* to */
-                               size += STRLEN_WITH_CHECK(info->to) - 2;
+                               size += strlen(info->to) - 2;
                                break;
                        case 'c': /* cc */
-                               size += STRLEN_WITH_CHECK(info->cc) - 2;
+                               size += strlen(info->cc) - 2;
                                break;
                        case 'd': /* date */
-                               size += STRLEN_WITH_CHECK(info->date) - 2;
+                               size += strlen(info->date) - 2;
                                break;
                        case 'i': /* message-id */
-                               size += STRLEN_WITH_CHECK(info->msgid) - 2;
+                               size += strlen(info->msgid) - 2;
                                break;
                        case 'n': /* newsgroups */
-                               size += STRLEN_WITH_CHECK(info->newsgroups) - 2;
+                               size += strlen(info->newsgroups) - 2;
                                break;
                        case 'r': /* references */
-                               size += STRLEN_WITH_CHECK(info->references) - 2;
+                               size += strlen(info->references) - 2;
                                break;
                        case 'F': /* file */
-                               if (MSG_IS_FILTERING(info->flags))
-                                       filename = g_strdup(info->folder);
-                               else                                            
-                                       filename = folder_item_fetch_msg(info->folder, info->msgnum);
+                               filename = folder_item_fetch_msg(info->folder,
+                                                                info->msgnum);
                                
                                if (filename == NULL) {
-                                       debug_print(_("%s(%d) - filename is not set"), __FILE__, __LINE__);
+                                       g_warning(_("filename is not set"));
                                        return NULL;
                                }
                                else
@@ -972,6 +974,7 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
                else s++;
        }
 
+
        processed_cmd = g_new0(gchar, size);
        s = cmd;
        p = processed_cmd;
@@ -988,56 +991,56 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
                                if (info->subject != NULL)
                                        strcpy(p, info->subject);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, _("(none)"));
                                p += strlen(p);
                                break;
                        case 'f': /* from */
                                if (info->from != NULL)
                                        strcpy(p, info->from);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, _("(none)"));
                                p += strlen(p);
                                break;
                        case 't': /* to */
                                if (info->to != NULL)
                                        strcpy(p, info->to);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, _("(none)"));
                                p += strlen(p);
                                break;
                        case 'c': /* cc */
                                if (info->cc != NULL)
                                        strcpy(p, info->cc);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, _("(none)"));
                                p += strlen(p);
                                break;
                        case 'd': /* date */
                                if (info->date != NULL)
                                        strcpy(p, info->date);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, _("(none)"));
                                p += strlen(p);
                                break;
                        case 'i': /* message-id */
                                if (info->msgid != NULL)
                                        strcpy(p, info->msgid);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, _("(none)"));
                                p += strlen(p);
                                break;
                        case 'n': /* newsgroups */
                                if (info->newsgroups != NULL)
                                        strcpy(p, info->newsgroups);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, _("(none)"));
                                p += strlen(p);
                                break;
                        case 'r': /* references */
                                if (info->references != NULL)
                                        strcpy(p, info->references);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, _("(none)"));
                                p += strlen(p);
                                break;
                        case 'F': /* file */
@@ -1061,19 +1064,11 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
        }
 
        debug_print("*** exec string \"%s\"\n", processed_cmd);
-
-       g_free(filename);
        return processed_cmd;
 }
 
-
-/* ************************************************************ */
-/* ************************************************************ */
-/* ************************************************************ */
-/* ************************************************************ */
 /* ************************************************************ */
 
-
 static void prefs_scoring_write(FILE * fp, GSList * prefs_scoring)
 {
        GSList * cur;
@@ -1102,8 +1097,11 @@ static void prefs_filtering_write(FILE * fp, GSList * prefs_scoring)
                gchar *filtering_str;
                FilteringProp * prop;
 
-               prop = (FilteringProp *) cur->data;
-               filtering_str = filteringprop_to_string(prop);
+               if (NULL == (prop = (FilteringProp *) cur->data))
+                       continue;
+               
+               if (NULL == (filtering_str = filteringprop_to_string(prop)))
+                       continue;
                
                if (fputs(filtering_str, fp) == EOF ||
                    fputc('\n', fp) == EOF) {
@@ -1199,11 +1197,6 @@ void prefs_matcher_write_config(void)
        }
 }
 
-
-
-
-
-
 /* ******************************************************************* */
 
 void prefs_matcher_read_config(void)
@@ -1215,7 +1208,7 @@ void prefs_matcher_read_config(void)
        prefs_filtering_clear();
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
-       f = fopen(rcpath, "r");
+       f = fopen(rcpath, "rb");
        g_free(rcpath);
 
        if (f != NULL)
@@ -1223,10 +1216,10 @@ void prefs_matcher_read_config(void)
        else {
                /* previous version compatibily */
 
-               printf("reading filtering\n");
+               /* printf("reading filtering\n"); */
                rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                     FILTERING_RC, NULL);
-               f = fopen(rcpath, "r");
+               f = fopen(rcpath, "rb");
                g_free(rcpath);
                
                if (f != NULL) {
@@ -1234,10 +1227,10 @@ void prefs_matcher_read_config(void)
                        fclose(matcher_parserin);
                }
                
-               printf("reading scoring\n");
+               /* printf("reading scoring\n"); */
                rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                     SCORING_RC, NULL);
-               f = fopen(rcpath, "r");
+               f = fopen(rcpath, "rb");
                g_free(rcpath);
                
                if (f != NULL) {