plug another memleak reported by Martin Kluge
[claws.git] / src / matcher.c
index 33dfa4b4a822baae7312bbf49d958fa6a092d164..f68eb5b1b1de822a973062ab203971faad16751c 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2002 by the Sylpheed Claws 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
+ * 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.
+ */
+
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
@@ -56,6 +75,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"},
@@ -87,18 +110,12 @@ static MatchParser matchparser_tab[] = {
        {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
        {MATCHACTION_EXECUTE, "execute"},
        {MATCHACTION_COLOR, "color"},
-       {MATCHACTION_BOUNCE, "bounce"}
+       {MATCHACTION_REDIRECT, "redirect"},
+       {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;
@@ -111,8 +128,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);
@@ -127,14 +174,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;
                        }                               
@@ -146,7 +202,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)
@@ -155,14 +212,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;
@@ -171,9 +236,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);
@@ -185,8 +259,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;
@@ -200,12 +274,14 @@ 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)
                                    ? REG_ICASE : 0)) != 0) {
                                prop->error = 1;
                                g_free(prop->preg);
+                               prop->preg = NULL;
                        }
                }
                if (prop->preg == NULL)
@@ -217,11 +293,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);
@@ -237,16 +314,20 @@ 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: %d\n", retval);
+
+       return (retval == 0);
 }
 
 /* match a message and his headers, hlist can be NULL if you don't
@@ -317,6 +398,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:
@@ -602,7 +692,7 @@ gboolean matcherlist_match_file(MatcherList * matchers, MsgInfo * 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;
@@ -627,8 +717,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;
@@ -640,7 +730,8 @@ gboolean matcherlist_match_file(MatcherList * matchers, MsgInfo * info,
                                        result = FALSE;
                                        break;
                                }
-               }
+                       }
+               }                       
        }
 
        g_free(file);
@@ -702,6 +793,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)) {
@@ -758,8 +852,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:
@@ -774,6 +870,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;
@@ -791,52 +890,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;
@@ -873,7 +936,9 @@ gchar * matcherlist_to_string(MatcherList * matchers)
        return result;
 }
 
-
+#define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
+#define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
+/* matching_build_command() - preferably cmd should be unescaped */
 gchar * matching_build_command(gchar * cmd, MsgInfo * info)
 {
        gchar * s = cmd;
@@ -882,9 +947,16 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
        gchar * p;
        gint size;
 
-       matcher_unescape_str(cmd);
+       const gchar *const no_subject    = _("(none)") ;
+       const gchar *const no_from       = _("(none)") ;
+       const gchar *const no_to         = _("(none)") ;
+       const gchar *const no_cc         = _("(none)") ;
+       const gchar *const no_date       = _("(none)") ;
+       const gchar *const no_msgid      = _("(none)") ;
+       const gchar *const no_newsgroups = _("(none)") ;
+       const gchar *const no_references = _("(none)") ;
 
-       size = strlen(cmd) + 1;
+       size = STRLEN_ZERO(cmd) + 1;
        while (*s != '\0') {
                if (*s == '%') {
                        s++;
@@ -893,28 +965,28 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
                                size -= 1;
                                break;
                        case 's': /* subject */
-                               size += strlen(info->subject) - 2;
+                               size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
                                break;
                        case 'f': /* from */
-                               size += strlen(info->from) - 2;
+                               size += STRLEN_DEFAULT(info->from, no_from) - 2;
                                break;
                        case 't': /* to */
-                               size += strlen(info->to) - 2;
+                               size += STRLEN_DEFAULT(info->to, no_to) - 2;
                                break;
                        case 'c': /* cc */
-                               size += strlen(info->cc) - 2;
+                               size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
                                break;
                        case 'd': /* date */
-                               size += strlen(info->date) - 2;
+                               size += STRLEN_DEFAULT(info->date, no_date) - 2;
                                break;
                        case 'i': /* message-id */
-                               size += strlen(info->msgid) - 2;
+                               size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
                                break;
                        case 'n': /* newsgroups */
-                               size += strlen(info->newsgroups) - 2;
+                               size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
                                break;
                        case 'r': /* references */
-                               size += strlen(info->references) - 2;
+                               size += STRLEN_DEFAULT(info->references, no_references) - 2;
                                break;
                        case 'F': /* file */
                                filename = folder_item_fetch_msg(info->folder,
@@ -950,61 +1022,62 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
                                if (info->subject != NULL)
                                        strcpy(p, info->subject);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, no_subject);
                                p += strlen(p);
                                break;
                        case 'f': /* from */
                                if (info->from != NULL)
                                        strcpy(p, info->from);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, no_from);
                                p += strlen(p);
                                break;
                        case 't': /* to */
                                if (info->to != NULL)
                                        strcpy(p, info->to);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, no_to);
                                p += strlen(p);
                                break;
                        case 'c': /* cc */
                                if (info->cc != NULL)
                                        strcpy(p, info->cc);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, no_cc);
                                p += strlen(p);
                                break;
                        case 'd': /* date */
                                if (info->date != NULL)
                                        strcpy(p, info->date);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, no_date);
                                p += strlen(p);
                                break;
                        case 'i': /* message-id */
                                if (info->msgid != NULL)
                                        strcpy(p, info->msgid);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, no_msgid);
                                p += strlen(p);
                                break;
                        case 'n': /* newsgroups */
                                if (info->newsgroups != NULL)
                                        strcpy(p, info->newsgroups);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, no_newsgroups);
                                p += strlen(p);
                                break;
                        case 'r': /* references */
                                if (info->references != NULL)
                                        strcpy(p, info->references);
                                else
-                                       strcpy(p, "(none)");
+                                       strcpy(p, no_references);
                                p += strlen(p);
                                break;
                        case 'F': /* file */
                                strcpy(p, filename);
                                p += strlen(p);
+                               g_free(filename);
                                break;
                        default:
                                *p = '%';
@@ -1025,15 +1098,11 @@ gchar * matching_build_command(gchar * cmd, MsgInfo * info)
        debug_print("*** exec string \"%s\"\n", processed_cmd);
        return processed_cmd;
 }
+#undef STRLEN_DEFAULT
+#undef STRLEN_ZERO
 
-
-/* ************************************************************ */
-/* ************************************************************ */
-/* ************************************************************ */
-/* ************************************************************ */
 /* ************************************************************ */
 
-
 static void prefs_scoring_write(FILE * fp, GSList * prefs_scoring)
 {
        GSList * cur;
@@ -1062,8 +1131,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) {
@@ -1137,7 +1209,7 @@ void prefs_matcher_write_config(void)
        GSList *cur;
        ScoringProp * prop;
 
-       debug_print(_("Writing matcher configuration...\n"));
+       debug_print("Writing matcher configuration...\n");
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                             MATCHER_RC, NULL);
@@ -1159,11 +1231,6 @@ void prefs_matcher_write_config(void)
        }
 }
 
-
-
-
-
-
 /* ******************************************************************* */
 
 void prefs_matcher_read_config(void)
@@ -1175,18 +1242,20 @@ 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)
+       if (f != NULL) {
                matcher_parser_start_parsing(f);
+               fclose(f);
+       }
        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) {
@@ -1194,10 +1263,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) {