+/*
+ * 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>
#include "utils.h"
#include "procheader.h"
#include "matcher.h"
+#include "intl.h"
struct _MatchParser {
gint id;
{MATCHING_NOT_MESSAGE, "~message"},
{MATCHING_BODY_PART, "body_part"},
{MATCHING_NOT_BODY_PART, "~body_part"},
+ {MATCHING_EXECUTE, "execute"},
+ {MATCHING_NOT_EXECUTE, "~execute"},
/* match type */
{MATCHING_MATCHCASE, "matchcase"},
{MATCHING_ACTION_MARK_AS_READ, "mark_as_read"},
{MATCHING_ACTION_MARK_AS_UNREAD, "mark_as_unread"},
{MATCHING_ACTION_FORWARD, "forward"},
- {MATCHING_ACTION_FORWARD_AS_ATTACHEMENT, "forward_as_attachement"},
- {MATCHING_ACTION_FORWARD_NEWS, "forward_news"},
- {MATCHING_ACTION_FORWARD_NEWS_AS_ATTACHEMENT, "forward_news_as_attachement"}
+ {MATCHING_ACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
+ {MATCHING_ACTION_COLOR, "color"}
+ /* {MATCHING_EXECUTE, "execute"}, */
};
gchar * get_matchparser_tab_str(gint id)
case MATCHING_NOT_INREPLYTO:
case MATCHING_MESSAGE:
case MATCHING_NOT_MESSAGE:
+ case MATCHING_EXECUTE:
+ case MATCHING_NOT_EXECUTE:
case MATCHING_HEADERS_PART:
case MATCHING_NOT_HEADERS_PART:
case MATCHING_BODY_PART:
return g_strdup(start);
}
+/* matcher_parse_str() - parses a string until it hits a
+ * terminating \". to unescape characters use \. The string
+ * should not be used directly: matcher_unescape_str()
+ * returns a string that can be used directly. */
gchar * matcher_parse_str(gchar ** str)
{
gchar * p;
p ++;
start = p;
dest = p;
- while (*p != '"') {
+
+ for ( ; *p && *p != '\"'; p++, dest++) {
if (*p == '\\') {
- p++;
+ *dest++ = *p++;
*dest = *p;
}
- else
+ else
*dest = *p;
- dest++;
- p++;
}
*dest = '\0';
-
+
*str += dest - dup + 2;
return g_strdup(start);
}
+gchar *matcher_unescape_str(gchar *str)
+{
+ gchar *tmp = alloca(strlen(str) + 1);
+ register gchar *src = tmp;
+ register gchar *dst = str;
+
+ strcpy(tmp, str);
+
+ for ( ; *src; src++) {
+ if (*src != '\\')
+ *dst++ = *src;
+ else {
+ src++;
+ if (*src == '\\')
+ *dst++ = '\\';
+ else if (*src == 'n')
+ *dst++ = '\n';
+ else if (*src == 'r')
+ *dst++ = '\r';
+ else if (*src == '\'' || *src == '\"')
+ *dst++ = *src;
+ else {
+ src--;
+ *dst++ = *src;
+ }
+ }
+ }
+ *dst = 0;
+ return str;
+}
+
/* **************** data structure allocation **************** */
}
}
+gboolean matcherprop_match_execute(MatcherProp * prop, MsgInfo * info)
+{
+ gchar * cmd;
+
+ cmd = matching_build_command(prop->expr, info);
+ if (cmd == NULL)
+ return FALSE;
+
+ return (system(cmd) == 0);
+}
+
/* match a message and his headers, hlist can be NULL if you don't
want to use headers */
return matcherprop_string_match(prop, info->references);
case MATCHING_NOT_REFERENCES:
return !matcherprop_string_match(prop, info->references);
- case MATCHING_HEADER:
+ case MATCHING_EXECUTE:
+ return matcherprop_match_execute(prop, info);
+ case MATCHING_NOT_EXECUTE:
+ return !matcherprop_match_execute(prop, info);
default:
return 0;
}
procheader_header_free(header);
return result;
}
+ else {
+ procheader_header_free(header);
+ }
break;
case MATCHING_HEADERS_PART:
case MATCHING_MESSAGE:
case MATCHING_NOT_HEADER:
case MATCHING_HEADERS_PART:
case MATCHING_NOT_HEADERS_PART:
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
+static gboolean matcherprop_criteria_message(MatcherProp * matcher)
+{
+ switch(matcher->criteria) {
case MATCHING_MESSAGE:
case MATCHING_NOT_MESSAGE:
return TRUE;
/*
matcherlist_match_one_header
- returns TRUE if buf matchs the MatchersList criteria
+ returns TRUE if match should stop
*/
static gboolean matcherlist_match_one_header(MatcherList * matchers,
- gchar * buf)
+ gchar * buf)
{
GSList * l;
- gboolean result;
- if (matchers->bool_and)
- result = TRUE;
- else
- result = FALSE;
-
for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
MatcherProp * matcher = (MatcherProp *) l->data;
- if (matcherprop_criteria_headers(matcher)) {
+ if (matcherprop_criteria_headers(matcher) ||
+ matcherprop_criteria_message(matcher)) {
if (matcherprop_match_one_header(matcher, buf)) {
+ matcher->result = TRUE;
+ }
+ }
+
+ if (matcherprop_criteria_headers(matcher)) {
+ if (matcher->result) {
if (!matchers->bool_and)
return TRUE;
}
- else {
- if (matchers->bool_and)
- return FALSE;
- }
}
}
- return result;
+ return FALSE;
}
/*
switch(matcher->criteria) {
case MATCHING_BODY_PART:
case MATCHING_NOT_BODY_PART:
- case MATCHING_MESSAGE:
- case MATCHING_NOT_MESSAGE:
return TRUE;
default:
return FALSE;
static gboolean matcherlist_match_line(MatcherList * matchers, gchar * line)
{
GSList * l;
- gboolean result;
-
- if (matchers->bool_and)
- result = TRUE;
- else
- result = FALSE;
for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
MatcherProp * matcher = (MatcherProp *) l->data;
- if (matcherprop_criteria_body(matcher)) {
+ if (matcherprop_criteria_body(matcher) ||
+ matcherprop_criteria_message(matcher)) {
if (matcherprop_match_line(matcher, line)) {
- if (!matchers->bool_and)
- return TRUE;
- }
- else {
- if (matchers->bool_and)
- return FALSE;
+ matcher->result = TRUE;
}
}
+
+ if (matcher->result) {
+ if (!matchers->bool_and)
+ return TRUE;
+ }
}
- return result;
+ return FALSE;
}
/*
GSList * l;
FILE * fp;
gchar * file;
-
- /* file need to be read ? */
-
+ gboolean is_incorporating = MSG_IS_FILTERING(info->flags);
+
+ /* check which things we need to do */
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;
read_headers = TRUE;
if (matcherprop_criteria_body(matcher))
read_body = TRUE;
+ if (matcherprop_criteria_message(matcher)) {
+ read_headers = TRUE;
+ read_body = TRUE;
+ }
+ matcher->result = FALSE;
}
if (!read_headers && !read_body)
return result;
- file = procmsg_get_message_file(info);
+ file = is_incorporating ? g_strdup(info->folder)
+ : procmsg_get_message_file(info);
+
if (file == NULL)
return FALSE;
/* read the headers */
if (read_headers) {
- if (matcherlist_match_headers(matchers, fp)) {
- if (!matchers->bool_and)
- result = TRUE;
- }
- else {
- if (matchers->bool_and)
- result = FALSE;
- }
+ if (matcherlist_match_headers(matchers, fp))
+ read_body = FALSE;
}
else {
matcherlist_skip_headers(fp);
/* read the body */
if (read_body) {
- if (matcherlist_match_body(matchers, fp)) {
- if (!matchers->bool_and)
- result = TRUE;
- }
- else {
- if (matchers->bool_and)
- result = FALSE;
+ matcherlist_match_body(matchers, fp);
+ }
+
+ for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
+ MatcherProp * matcher = (MatcherProp *) l->data;
+
+ if (matcherprop_criteria_headers(matcher) ||
+ matcherprop_criteria_body(matcher) ||
+ matcherprop_criteria_message(matcher))
+ if (matcher->result) {
+ if (!matchers->bool_and) {
+ result = TRUE;
+ break;
+ }
+ }
+ else {
+ if (matchers->bool_and) {
+ result = FALSE;
+ break;
+ }
}
}
else
result = FALSE;
- /* test the condition on the file */
-
- if (matcherlist_match_file(matchers, info, result)) {
- if (!matchers->bool_and)
- return TRUE;
- }
- else {
- if (matchers->bool_and)
- return FALSE;
- }
-
/* test the cached elements */
for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
case MATCHING_NOT_REFERENCES:
case MATCHING_SCORE_GREATER:
case MATCHING_SCORE_LOWER:
+ case MATCHING_EXECUTE:
+ case MATCHING_NOT_EXECUTE:
if (matcherprop_match(matcher, info)) {
if (!matchers->bool_and) {
- result = TRUE;
- break;
+ return TRUE;
}
}
else {
if (matchers->bool_and) {
- result = FALSE;
- break;
+ return FALSE;
}
}
}
}
+ /* test the condition on the file */
+
+ if (matcherlist_match_file(matchers, info, result)) {
+ if (!matchers->bool_and)
+ return TRUE;
+ }
+ else {
+ if (matchers->bool_and)
+ return FALSE;
+ }
+
return result;
}
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)
+
+gchar * matching_build_command(gchar * cmd, MsgInfo * info)
+{
+ gchar * s = cmd;
+ gchar * filename = NULL;
+ gchar * processed_cmd;
+ gchar * p;
+ gint size;
+
+ matcher_unescape_str(cmd);
+
+ size = strlen(cmd) + 1;
+ while (*s != '\0') {
+ if (*s == '%') {
+ s++;
+ switch (*s) {
+ case '%':
+ size -= 1;
+ break;
+ case 's': /* subject */
+ size += STRLEN_WITH_CHECK(info->subject) - 2;
+ break;
+ case 'f': /* from */
+ size += STRLEN_WITH_CHECK(info->from) - 2;
+ break;
+ case 't': /* to */
+ size += STRLEN_WITH_CHECK(info->to) - 2;
+ break;
+ case 'c': /* cc */
+ size += STRLEN_WITH_CHECK(info->cc) - 2;
+ break;
+ case 'd': /* date */
+ size += STRLEN_WITH_CHECK(info->date) - 2;
+ break;
+ case 'i': /* message-id */
+ size += STRLEN_WITH_CHECK(info->msgid) - 2;
+ break;
+ case 'n': /* newsgroups */
+ size += STRLEN_WITH_CHECK(info->newsgroups) - 2;
+ break;
+ case 'r': /* references */
+ size += STRLEN_WITH_CHECK(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);
+
+ if (filename == NULL) {
+ debug_print(_("%s(%d) - filename is not set"), __FILE__, __LINE__);
+ return NULL;
+ }
+ else
+ size += strlen(filename) - 2;
+ break;
+ }
+ s++;
+ }
+ else s++;
+ }
+
+ processed_cmd = g_new0(gchar, size);
+ s = cmd;
+ p = processed_cmd;
+
+ while (*s != '\0') {
+ if (*s == '%') {
+ s++;
+ switch (*s) {
+ case '%':
+ *p = '%';
+ p++;
+ break;
+ case 's': /* subject */
+ if (info->subject != NULL)
+ strcpy(p, info->subject);
+ else
+ strcpy(p, "(none)");
+ p += strlen(p);
+ break;
+ case 'f': /* from */
+ if (info->from != NULL)
+ strcpy(p, info->from);
+ else
+ strcpy(p, "(none)");
+ p += strlen(p);
+ break;
+ case 't': /* to */
+ if (info->to != NULL)
+ strcpy(p, info->to);
+ else
+ strcpy(p, "(none)");
+ p += strlen(p);
+ break;
+ case 'c': /* cc */
+ if (info->cc != NULL)
+ strcpy(p, info->cc);
+ else
+ strcpy(p, "(none)");
+ p += strlen(p);
+ break;
+ case 'd': /* date */
+ if (info->date != NULL)
+ strcpy(p, info->date);
+ else
+ strcpy(p, "(none)");
+ p += strlen(p);
+ break;
+ case 'i': /* message-id */
+ if (info->msgid != NULL)
+ strcpy(p, info->msgid);
+ else
+ strcpy(p, "(none)");
+ p += strlen(p);
+ break;
+ case 'n': /* newsgroups */
+ if (info->newsgroups != NULL)
+ strcpy(p, info->newsgroups);
+ else
+ strcpy(p, "(none)");
+ p += strlen(p);
+ break;
+ case 'r': /* references */
+ if (info->references != NULL)
+ strcpy(p, info->references);
+ else
+ strcpy(p, "(none)");
+ p += strlen(p);
+ break;
+ case 'F': /* file */
+ strcpy(p, filename);
+ p += strlen(p);
+ break;
+ default:
+ *p = '%';
+ p++;
+ *p = *s;
+ p++;
+ break;
+ }
+ s++;
+ }
+ else {
+ *p = *s;
+ p++;
+ s++;
+ }
+ }
+
+ debug_print("*** exec string \"%s\"\n", processed_cmd);
+
+ g_free(filename);
+ return processed_cmd;
+}
+
+
+/* ************************************************************ */
+
+/*
+static void matcher_parse (gchar * str)
+{
+ matcher_parser_scan_string(str);
+ matcher_parserparse();
+}
+*/