* src/filter.c
[claws.git] / src / filter.c
index e2235c6eb491eff30f6cbe023eb9bc1a23cd961b..67d64a5d441bd77a136a1cd70ecd166676ed2def 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2001 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
@@ -25,6 +25,8 @@
 #include <string.h>
 #include <strings.h>
 #include <stdlib.h>
+#include <sys/types.h>
+#include <regex.h>
 
 #include "intl.h"
 #include "procheader.h"
@@ -50,12 +52,12 @@ FolderItem *filter_get_dest_folder(GSList *fltlist, const gchar *file)
                if (filter_match_condition(filter, hlist)) {
                        if (filter->action == FLT_NOTRECV) {
                                if (!dummy) {
-                                       dummy = folder_item_new(NULL, NULL);
+                                       dummy = g_new0(FolderItem, 0);
                                        dummy->path = g_strdup(FILTER_NOT_RECEIVE);
                                }
                                dest_folder = dummy;
                        } else
-                               dest_folder = folder_find_item_from_path
+                               dest_folder = folder_find_item_from_identifier
                                        (filter->dest);
                        break;
                }
@@ -86,6 +88,26 @@ static gboolean strcasenotfind(const gchar *haystack, const gchar *needle)
        return strcasestr(haystack, needle) != NULL ? FALSE : TRUE;
 }
 
+static gboolean strmatch_regex(const gchar *haystack, const gchar *needle)
+{
+       gint ret = 0;
+       regex_t preg;
+       regmatch_t pmatch[1];
+
+       ret = regcomp(&preg, needle, 0);
+       if (ret != 0) return FALSE;
+
+       ret = regexec(&preg, haystack, 1, pmatch, 0);
+       regfree(&preg);
+
+       if (ret == REG_NOMATCH) return FALSE;
+
+       if (pmatch[0].rm_so != -1)
+               return TRUE;
+       else
+               return FALSE;
+}
+
 gboolean filter_match_condition(Filter *filter, GSList *hlist)
 {
        Header *header;
@@ -94,13 +116,17 @@ gboolean filter_match_condition(Filter *filter, GSList *hlist)
 
        g_return_val_if_fail(filter->name1 != NULL, FALSE);
 
-       if (FLT_IS_CASE_SENS(filter->flag1))
+       if (FLT_IS_REGEX(filter->flag1))
+               StrFind1 = strmatch_regex;
+       else if (FLT_IS_CASE_SENS(filter->flag1))
                StrFind1 = FLT_IS_CONTAIN(filter->flag1)
                        ? strfind : strnotfind;
        else
                StrFind1 = FLT_IS_CONTAIN(filter->flag1)
                        ? strcasefind : strcasenotfind;
 
+       if (FLT_IS_REGEX(filter->flag2))
+               StrFind2 = strmatch_regex;
        if (FLT_IS_CASE_SENS(filter->flag2))
                StrFind2 = FLT_IS_CONTAIN(filter->flag2)
                        ? strfind : strnotfind;
@@ -189,8 +215,7 @@ Filter *filter_read_str(const gchar *str)
        gchar *name1, *body1, *op, *name2, *body2, *dest;
        gchar *flag1 = NULL, *flag2 = NULL, *action = NULL;
 
-       Xalloca(tmp, strlen(str) + 1, return NULL);
-       strcpy(tmp, str);
+       Xstrdup_a(tmp, str, return NULL);
 
        name1 = tmp;
        PARSE_ONE_PARAM(body1, name1);