aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKjartan Maraas <kmaraas@gnome.org>2001-02-05 21:57:00 +0800
committerKjartan Maraas <kmaraas@src.gnome.org>2001-02-05 21:57:00 +0800
commit4afe7dc7708bfce195bc7434f46bddc2f62559c2 (patch)
tree782481df4a8a214f2e3f58582e8f64c8d0543173
parent764b9e26b00bf10e474c2684f74c8c9497de30d9 (diff)
downloadgsoc2013-evolution-4afe7dc7708bfce195bc7434f46bddc2f62559c2.tar
gsoc2013-evolution-4afe7dc7708bfce195bc7434f46bddc2f62559c2.tar.gz
gsoc2013-evolution-4afe7dc7708bfce195bc7434f46bddc2f62559c2.tar.bz2
gsoc2013-evolution-4afe7dc7708bfce195bc7434f46bddc2f62559c2.tar.lz
gsoc2013-evolution-4afe7dc7708bfce195bc7434f46bddc2f62559c2.tar.xz
gsoc2013-evolution-4afe7dc7708bfce195bc7434f46bddc2f62559c2.tar.zst
gsoc2013-evolution-4afe7dc7708bfce195bc7434f46bddc2f62559c2.zip
Add #include <config.h> and mark some strings for translation.
2001-02-05 Kjartan Maraas <kmaraas@gnome.org> * filter-code.c, filter-colour.c, filter-context.c, filter-element.c, filter-input.c, filter-score.c, filter-system-flag.c, filter-url.c: Add #include <config.h> and mark some strings for translation. svn path=/trunk/; revision=7967
-rw-r--r--filter/ChangeLog7
-rw-r--r--filter/filter-code.c4
-rw-r--r--filter/filter-colour.c4
-rw-r--r--filter/filter-context.c4
-rw-r--r--filter/filter-driver.c778
-rw-r--r--filter/filter-element.c4
-rw-r--r--filter/filter-input.c4
-rw-r--r--filter/filter-message-search.c806
-rw-r--r--filter/filter-score.c4
-rw-r--r--filter/filter-system-flag.c10
-rw-r--r--filter/filter-url.c4
-rw-r--r--filter/rule-context.c4
-rw-r--r--filter/score-context.c4
-rw-r--r--filter/vfolder-context.c4
14 files changed, 1638 insertions, 3 deletions
diff --git a/filter/ChangeLog b/filter/ChangeLog
index 6233a44820..4530e42589 100644
--- a/filter/ChangeLog
+++ b/filter/ChangeLog
@@ -1,3 +1,10 @@
+2001-02-05 Kjartan Maraas <kmaraas@gnome.org>
+
+ * filter-code.c, filter-colour.c, filter-context.c,
+ filter-element.c, filter-input.c, filter-score.c,
+ filter-system-flag.c, filter-url.c: Add #include <config.h>
+ and mark some strings for translation.
+
2001-01-22 Not Zed <NotZed@Ximian.com>
* filter-message-search.[ch]: Removed, now lives in
diff --git a/filter/filter-code.c b/filter/filter-code.c
index cbab60359e..c008043a5f 100644
--- a/filter/filter-code.c
+++ b/filter/filter-code.c
@@ -18,6 +18,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
diff --git a/filter/filter-colour.c b/filter/filter-colour.c
index 202908d016..ffa6489cfb 100644
--- a/filter/filter-colour.c
+++ b/filter/filter-colour.c
@@ -18,6 +18,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
#include <gnome-xml/xmlmemory.h>
diff --git a/filter/filter-context.c b/filter/filter-context.c
index c31eba205a..c6fa69f38a 100644
--- a/filter/filter-context.c
+++ b/filter/filter-context.c
@@ -18,6 +18,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
diff --git a/filter/filter-driver.c b/filter/filter-driver.c
new file mode 100644
index 0000000000..bc941a9c01
--- /dev/null
+++ b/filter/filter-driver.c
@@ -0,0 +1,778 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2000 Helix Code Inc.
+ *
+ * Authors: Michael Zucchi <notzed@helixcode.com>
+ * Jeffrey Stedfast <fejj@helixcode.com>
+ *
+ * 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
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "filter-driver.h"
+#include "filter-message-search.h"
+
+#include <glib.h>
+#include <gtk/gtk.h>
+#include <gnome.h>
+#include <gtkhtml/gtkhtml.h>
+
+#include <time.h>
+
+#include <gnome-xml/tree.h>
+#include <gnome-xml/parser.h>
+
+#include <camel/camel.h>
+#include "filter-context.h"
+#include "filter-filter.h"
+#include "e-util/e-sexp.h"
+#include "e-util/e-memory.h"
+
+#define d(x)
+
+/* type of status for a log report */
+enum filter_log_t {
+ FILTER_LOG_NONE,
+ FILTER_LOG_START, /* start of new log entry */
+ FILTER_LOG_ACTION, /* an action performed */
+ FILTER_LOG_END, /* end of log */
+};
+
+struct _FilterDriverPrivate {
+ GHashTable *globals; /* global variables */
+
+ CamelFolder *defaultfolder; /* defualt folder */
+
+ FDStatusFunc *statusfunc; /* status callback */
+ void *statusdata; /* status callback data */
+
+ FilterContext *context;
+
+ /* for callback */
+ FilterGetFolderFunc get_folder;
+ void *data;
+
+ /* run-time data */
+ GHashTable *folders; /* folders that message has been copied to */
+ GHashTable *forwards; /* addresses that have been forwarded the message */
+
+ gboolean terminated; /* message processing was terminated */
+ gboolean deleted; /* message was marked for deletion */
+ gboolean copied; /* message was copied to some folder or another */
+
+ CamelMimeMessage *message; /* input message */
+ CamelMessageInfo *info; /* message summary info */
+
+ FILE *logfile; /* log file */
+
+ CamelException *ex;
+
+ /* evaluator */
+ ESExp *eval;
+};
+
+#define _PRIVATE(o) (((FilterDriver *)(o))->priv)
+
+static void filter_driver_class_init (FilterDriverClass *klass);
+static void filter_driver_init (FilterDriver *obj);
+static void filter_driver_finalise (GtkObject *obj);
+
+static void filter_driver_log (FilterDriver *driver, enum filter_log_t status, const char *desc, ...);
+
+static CamelFolder *open_folder (FilterDriver *d, const char *folder_url);
+static int close_folders (FilterDriver *d);
+
+static ESExpResult *do_delete (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *);
+static ESExpResult *mark_forward (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *);
+static ESExpResult *do_copy (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *);
+static ESExpResult *do_move (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *);
+static ESExpResult *do_stop (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *);
+static ESExpResult *do_colour (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *);
+static ESExpResult *do_score (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *);
+static ESExpResult *do_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *);
+
+/* these are our filter actions - each must have a callback */
+static struct {
+ char *name;
+ ESExpFunc *func;
+ int type; /* set to 1 if a function can perform shortcut evaluation, or
+ doesn't execute everything, 0 otherwise */
+} symbols[] = {
+ { "delete", (ESExpFunc *) do_delete, 0 },
+ { "forward-to", (ESExpFunc *) mark_forward, 0 },
+ { "copy-to", (ESExpFunc *) do_copy, 0 },
+ { "move-to", (ESExpFunc *) do_move, 0 },
+ { "stop", (ESExpFunc *) do_stop, 0 },
+ { "set-colour", (ESExpFunc *) do_colour, 0 },
+ { "set-score", (ESExpFunc *) do_score, 0 },
+ { "set-system-flag", (ESExpFunc *) do_flag, 0 }
+};
+
+static GtkObjectClass *filter_driver_parent;
+
+enum SIGNALS {
+ LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+guint
+filter_driver_get_type (void)
+{
+ static guint type = 0;
+
+ if (!type) {
+ GtkTypeInfo type_info = {
+ "FilterDriver",
+ sizeof (FilterDriver),
+ sizeof (FilterDriverClass),
+ (GtkClassInitFunc) filter_driver_class_init,
+ (GtkObjectInitFunc) filter_driver_init,
+ (GtkArgSetFunc) NULL,
+ (GtkArgGetFunc) NULL
+ };
+
+ type = gtk_type_unique (gtk_object_get_type (), &type_info);
+ }
+
+ return type;
+}
+
+static void
+filter_driver_class_init (FilterDriverClass *klass)
+{
+ GtkObjectClass *object_class = (GtkObjectClass *) klass;
+
+ filter_driver_parent = gtk_type_class (gtk_object_get_type ());
+
+ object_class->finalize = filter_driver_finalise;
+
+ gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
+}
+
+static void
+filter_driver_init (FilterDriver *obj)
+{
+ struct _FilterDriverPrivate *p;
+ int i;
+
+ p = _PRIVATE (obj) = g_malloc0 (sizeof (*p));
+
+ p->eval = e_sexp_new ();
+ /* Load in builtin symbols */
+ for (i = 0; i < sizeof (symbols) / sizeof (symbols[0]); i++) {
+ if (symbols[i].type == 1) {
+ e_sexp_add_ifunction (p->eval, 0, symbols[i].name, (ESExpIFunc *)symbols[i].func, obj);
+ } else {
+ e_sexp_add_function (p->eval, 0, symbols[i].name, symbols[i].func, obj);
+ }
+ }
+
+ p->globals = g_hash_table_new (g_str_hash, g_str_equal);
+
+ p->folders = g_hash_table_new (g_str_hash, g_str_equal);
+}
+
+static void
+free_hash_strings (void *key, void *value, void *data)
+{
+ g_free (key);
+ g_free (value);
+}
+
+static void
+filter_driver_finalise (GtkObject *obj)
+{
+ FilterDriver *driver = (FilterDriver *) obj;
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ /* close all folders that were opened for appending */
+ close_folders (driver);
+ g_hash_table_destroy (p->folders);
+
+ g_hash_table_foreach (p->globals, free_hash_strings, driver);
+ g_hash_table_destroy (p->globals);
+
+ e_sexp_unref(p->eval);
+
+ if (p->defaultfolder)
+ camel_object_unref (CAMEL_OBJECT (p->defaultfolder));
+
+ g_free (p);
+
+ ((GtkObjectClass *)(filter_driver_parent))->finalize (GTK_OBJECT (obj));
+}
+
+/**
+ * filter_driver_new:
+ * @system: path to system rules
+ * @user: path to user rules
+ * @get_folder: function to call to fetch folders
+ *
+ * Create a new FilterDriver object.
+ *
+ * Return value: A new FilterDriver widget.
+ **/
+FilterDriver *
+filter_driver_new (FilterContext *context, FilterGetFolderFunc get_folder, void *data)
+{
+ FilterDriver *new;
+ struct _FilterDriverPrivate *p;
+
+ new = FILTER_DRIVER (gtk_type_new (filter_driver_get_type ()));
+ p = _PRIVATE (new);
+
+ p->get_folder = get_folder;
+ p->data = data;
+ p->context = context;
+ gtk_object_ref (GTK_OBJECT (context));
+
+ return new;
+}
+
+void
+filter_driver_set_logfile (FilterDriver *d, FILE *logfile)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (d);
+
+ p->logfile = logfile;
+}
+
+void
+filter_driver_set_status_func (FilterDriver *d, FDStatusFunc *func, void *data)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (d);
+
+ p->statusfunc = func;
+ p->statusdata = data;
+}
+
+void
+filter_driver_set_default_folder (FilterDriver *d, CamelFolder *def)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (d);
+
+ if (p->defaultfolder)
+ camel_object_unref (CAMEL_OBJECT (p->defaultfolder));
+
+ p->defaultfolder = def;
+
+ if (p->defaultfolder)
+ camel_object_ref (CAMEL_OBJECT (p->defaultfolder));
+}
+
+static void
+report_status (FilterDriver *driver, enum filter_status_t status, const char *desc, ...)
+{
+ /* call user-defined status report function */
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+ va_list ap;
+ char *str;
+
+ if (p->statusfunc) {
+ va_start (ap, desc);
+ str = g_strdup_vprintf (desc, ap);
+ p->statusfunc (driver, status, str, p->statusdata);
+ g_free (str);
+ }
+}
+
+
+#if 0
+void
+filter_driver_set_global (FilterDriver *d, const char *name, const char *value)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (d);
+ char *oldkey, *oldvalue;
+
+ if (g_hash_table_lookup_extended (p->globals, name, (void *)&oldkey, (void *)&oldvalue)) {
+ g_free (oldvalue);
+ g_hash_table_insert (p->globals, oldkey, g_strdup (value));
+ } else {
+ g_hash_table_insert (p->globals, g_strdup (name), g_strdup (value));
+ }
+}
+#endif
+
+static ESExpResult *
+do_delete (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *driver)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ d(fprintf (stderr, "doing delete\n"));
+ p->deleted = TRUE;
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Delete");
+
+ return NULL;
+}
+
+static ESExpResult *
+mark_forward (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *driver)
+{
+ /*struct _FilterDriverPrivate *p = _PRIVATE (driver);*/
+
+ d(fprintf (stderr, "marking message for forwarding\n"));
+ /* FIXME: do stuff here */
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Forward");
+
+ return NULL;
+}
+
+static ESExpResult *
+do_copy (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *driver)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+ int i;
+
+ d(fprintf (stderr, "copying message...\n"));
+
+ for (i = 0; i < argc; i++) {
+ if (argv[i]->type == ESEXP_RES_STRING) {
+ /* open folders we intent to copy to */
+ char *folder = argv[i]->value.string;
+ char *service_url;
+ CamelFolder *outbox;
+
+ outbox = open_folder (driver, folder);
+ if (!outbox)
+ continue;
+
+ p->copied = TRUE;
+ camel_folder_append_message (outbox, p->message, p->info, p->ex);
+
+ service_url = camel_service_get_url (CAMEL_SERVICE (camel_folder_get_parent_store (outbox)));
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Copy to folder %s", service_url);
+ g_free (service_url);
+ }
+ }
+
+ return NULL;
+}
+
+static ESExpResult *
+do_move (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *driver)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+ int i;
+
+ d(fprintf (stderr, "moving message...\n"));
+
+ for (i = 0; i < argc; i++) {
+ if (argv[i]->type == ESEXP_RES_STRING) {
+ /* open folders we intent to move to */
+ char *folder = argv[i]->value.string;
+ char *service_url;
+ CamelFolder *outbox;
+
+ outbox = open_folder (driver, folder);
+ if (!outbox)
+ continue;
+
+ p->copied = TRUE;
+ p->deleted = TRUE; /* a 'move' is a copy & delete */
+
+ camel_folder_append_message (outbox, p->message, p->info, p->ex);
+
+ service_url = camel_service_get_url (CAMEL_SERVICE (camel_folder_get_parent_store (outbox)));
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Move to folder %s", service_url);
+ g_free (service_url);
+ }
+ }
+
+ return NULL;
+}
+
+static ESExpResult *
+do_stop (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *driver)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Stopped processing");
+ d(fprintf (stderr, "terminating message processing\n"));
+ p->terminated = TRUE;
+
+ return NULL;
+}
+
+static ESExpResult *
+do_colour (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *driver)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ d(fprintf (stderr, "setting colour tag\n"));
+ if (argc > 0 && argv[0]->type == ESEXP_RES_STRING) {
+ camel_tag_set (&p->info->user_tags, "colour", argv[0]->value.string);
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Set colour to %s", argv[0]->value.string);
+ }
+
+ return NULL;
+}
+
+static ESExpResult *
+do_score (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *driver)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ d(fprintf (stderr, "setting score tag\n"));
+ if (argc > 0 && argv[0]->type == ESEXP_RES_INT) {
+ char *value;
+
+ value = g_strdup_printf ("%d", argv[0]->value.number);
+ camel_tag_set (&p->info->user_tags, "score", value);
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Set score to %d", argv[0]->value.number);
+ g_free (value);
+ }
+
+ return NULL;
+}
+
+static ESExpResult *
+do_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterDriver *driver)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ d(fprintf (stderr, "setting flag\n"));
+ if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
+ p->info->flags |= camel_system_flag (argv[0]->value.string) | CAMEL_MESSAGE_FOLDER_FLAGGED;
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Set %s flag", argv[0]->value.string);
+ }
+
+ return NULL;
+}
+
+static CamelFolder *
+open_folder (FilterDriver *driver, const char *folder_url)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+ CamelFolder *camelfolder;
+
+ /* we have a lookup table of currently open folders */
+ camelfolder = g_hash_table_lookup (p->folders, folder_url);
+ if (camelfolder)
+ return camelfolder;
+
+ camelfolder = p->get_folder (driver, folder_url, p->data);
+
+ if (camelfolder) {
+ g_hash_table_insert (p->folders, g_strdup (folder_url), camelfolder);
+ camel_folder_freeze (camelfolder);
+ }
+
+ return camelfolder;
+}
+
+static void
+close_folder (void *key, void *value, void *data)
+{
+ CamelFolder *folder = value;
+ FilterDriver *driver = data;
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ g_free (key);
+ camel_folder_sync (folder, FALSE, p->ex);
+ camel_folder_thaw (folder);
+ camel_object_unref (CAMEL_OBJECT (folder));
+}
+
+/* flush/close all folders */
+static int
+close_folders (FilterDriver *driver)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ g_hash_table_foreach (p->folders, close_folder, driver);
+ g_hash_table_destroy (p->folders);
+ p->folders = g_hash_table_new (g_str_hash, g_str_equal);
+
+ /* FIXME: status from driver */
+ return 0;
+}
+
+#if 0
+static void
+free_key (gpointer key, gpointer value, gpointer user_data)
+{
+ g_free (key);
+}
+#endif
+
+
+static void
+filter_driver_log (FilterDriver *driver, enum filter_log_t status, const char *desc, ...)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+
+ if (p->logfile) {
+ char *str = NULL;
+
+ if (desc) {
+ va_list ap;
+
+ va_start (ap, desc);
+ str = g_strdup_vprintf (desc, ap);
+ }
+
+ switch (status) {
+ case FILTER_LOG_START: {
+ /* write log header */
+ const char *subject = NULL;
+ char *fromstr;
+ const CamelInternetAddress *from;
+ char date[50];
+ time_t t;
+
+ /* FIXME: does this need locking? Probably */
+
+ from = camel_mime_message_get_from (p->message);
+ fromstr = camel_address_format((CamelAddress *)from);
+ subject = camel_mime_message_get_subject (p->message);
+
+ time (&t);
+ strftime (date, 49, "%a, %d %b %Y %H:%M:%S", localtime (&t));
+ fprintf (p->logfile, "Applied filter \"%s\" to message from %s - \"%s\" at %s\n",
+ str, fromstr ? fromstr : "unknown", subject ? subject : "", date);
+ g_free(fromstr);
+ break;
+ }
+ case FILTER_LOG_ACTION:
+ fprintf (p->logfile, "Action: %s\n", str);
+ break;
+ case FILTER_LOG_END:
+ fprintf (p->logfile, "\n");
+ break;
+ default:
+ /* nothing else is loggable */
+ break;
+ }
+
+ g_free (str);
+ }
+}
+
+
+/* will filter only an mbox - is more efficient as it doesn't need to open the folder through camel directly */
+void
+filter_driver_filter_mbox (FilterDriver *driver, const char *mbox, const char *source, CamelException *ex)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+ CamelMimeParser *mp = NULL;
+ char *source_url = NULL;
+ int fd = -1;
+ int i = 0;
+ struct stat st;
+
+ fd = open (mbox, O_RDONLY);
+ if (fd == -1) {
+ camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, "Unable to open spool folder");
+ goto fail;
+ }
+ /* to get the filesize */
+ fstat (fd, &st);
+
+ mp = camel_mime_parser_new ();
+ camel_mime_parser_scan_from (mp, TRUE);
+ if (camel_mime_parser_init_with_fd (mp, fd) == -1) {
+ camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, "Unable to process spool folder");
+ goto fail;
+ }
+ fd = -1;
+
+ source_url = g_strdup_printf ("file://%s", mbox);
+
+ while (camel_mime_parser_step (mp, 0, 0) == HSCAN_FROM) {
+ CamelMimeMessage *msg;
+ int pc = 0;
+
+ if (st.st_size > 0)
+ pc = (int)(100.0 * ((double)camel_mime_parser_tell (mp) / (double)st.st_size));
+
+ report_status (driver, FILTER_STATUS_START, "Getting message %d (%d%% of file)", i, pc);
+
+ msg = camel_mime_message_new ();
+ if (camel_mime_part_construct_from_parser (CAMEL_MIME_PART (msg), mp) == -1) {
+ report_status (driver, FILTER_STATUS_END, "Failed message %d", i);
+ camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, "Cannot open message");
+ camel_object_unref (CAMEL_OBJECT (msg));
+ goto fail;
+ }
+
+ filter_driver_filter_message (driver, msg, NULL, source_url, source, ex);
+ camel_object_unref (CAMEL_OBJECT (msg));
+ if (camel_exception_is_set (ex)) {
+ report_status (driver, FILTER_STATUS_END, "Failed message %d", i);
+ goto fail;
+ }
+
+ report_status (driver, FILTER_STATUS_END, "Finished message %d", i);
+ i++;
+
+ /* skip over the FROM_END state */
+ camel_mime_parser_step (mp, 0, 0);
+ }
+
+ if (p->defaultfolder)
+ camel_folder_sync (p->defaultfolder, FALSE, ex);
+
+fail:
+ g_free (source_url);
+ if (fd != -1)
+ close (fd);
+ if (mp)
+ camel_object_unref (CAMEL_OBJECT (mp));
+}
+
+/* will filter a folder */
+void
+filter_driver_filter_folder (FilterDriver *driver, CamelFolder *folder, const char *source,
+ GPtrArray *uids, gboolean remove, CamelException *ex)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+ int i;
+ int freeuids = FALSE;
+ CamelMimeMessage *message;
+ const CamelMessageInfo *info;
+ char *source_url, *service_url;
+
+ service_url = camel_service_get_url (CAMEL_SERVICE (camel_folder_get_parent_store (folder)));
+ source_url = g_strdup_printf ("%s%s", service_url, camel_folder_get_full_name (folder));
+ g_free (service_url);
+
+ if (uids == NULL) {
+ uids = camel_folder_get_uids (folder);
+ freeuids = TRUE;
+ }
+
+ for (i = 0; i < uids->len; i++) {
+ report_status (driver, FILTER_STATUS_START, "Getting message %d of %d", i+1, uids->len);
+
+ message = camel_folder_get_message (folder, uids->pdata[i], ex);
+ if (camel_exception_is_set (ex)) {
+ report_status (driver, FILTER_STATUS_END, "Failed at message %d of %d", i+1, uids->len);
+ break;
+ }
+
+ if (camel_folder_has_summary_capability (folder))
+ info = camel_folder_get_message_info (folder, uids->pdata[i]);
+ else
+ info = NULL;
+
+ filter_driver_filter_message (driver, message, (CamelMessageInfo *)info, source_url, source, ex);
+ if (camel_exception_is_set (ex)) {
+ report_status (driver, FILTER_STATUS_END, "Failed at message %d of %d", i+1, uids->len);
+ break;
+ }
+
+ if (remove)
+ camel_folder_set_message_flags (folder, uids->pdata[i],
+ CAMEL_MESSAGE_DELETED, CAMEL_MESSAGE_DELETED);
+
+ camel_object_unref (CAMEL_OBJECT (message));
+ }
+
+ if (freeuids)
+ camel_folder_free_uids (folder, uids);
+
+ if (p->defaultfolder)
+ camel_folder_sync (p->defaultfolder, FALSE, ex);
+
+ g_free (source_url);
+}
+
+void
+filter_driver_filter_message (FilterDriver *driver, CamelMimeMessage *message, CamelMessageInfo *info,
+ const char *source_url, const char *source, CamelException *ex)
+{
+ struct _FilterDriverPrivate *p = _PRIVATE (driver);
+ ESExpResult *r;
+ GString *fsearch, *faction;
+ FilterFilter *rule;
+ gboolean freeinfo = FALSE;
+ gboolean filtered = FALSE;
+
+ if (info == NULL) {
+ struct _header_raw *h = CAMEL_MIME_PART (message)->headers;
+
+ info = camel_message_info_new_from_header (h);
+ freeinfo = TRUE;
+ } else {
+ if (info->flags & CAMEL_MESSAGE_DELETED)
+ return;
+ }
+
+ p->ex = ex;
+ p->terminated = FALSE;
+ p->deleted = FALSE;
+ p->copied = FALSE;
+ p->message = message;
+ p->info = info;
+
+ fsearch = g_string_new ("");
+ faction = g_string_new ("");
+
+ rule = NULL;
+ while ((rule = (FilterFilter *)rule_context_next_rule ((RuleContext *)p->context, (FilterRule *)rule, source))) {
+ gboolean matched;
+
+ g_string_truncate (fsearch, 0);
+ g_string_truncate (faction, 0);
+
+ filter_rule_build_code (FILTER_RULE (rule), fsearch);
+ filter_filter_build_action (rule, faction);
+
+ d(fprintf (stderr, "applying rule %s\n action %s\n", fsearch->str, faction->str));
+
+ matched = filter_message_search (p->message, p->info, source_url, fsearch->str, p->ex);
+
+ if (matched) {
+ filtered = TRUE;
+ filter_driver_log (driver, FILTER_LOG_START, FILTER_RULE (rule)->name);
+#ifndef NO_WARNINGS
+#warning "Must check expression parsed and executed properly?"
+#endif
+ /* perform necessary filtering actions */
+ e_sexp_input_text (p->eval, faction->str, strlen (faction->str));
+ e_sexp_parse (p->eval);
+ r = e_sexp_eval (p->eval);
+ e_sexp_result_free (r);
+ if (p->terminated)
+ break;
+ }
+ }
+
+ g_string_free (fsearch, TRUE);
+ g_string_free (faction, TRUE);
+
+ /* *Now* we can set the DELETED flag... */
+ if (p->deleted)
+ info->flags = info->flags | CAMEL_MESSAGE_DELETED | CAMEL_MESSAGE_FOLDER_FLAGGED;
+
+ /* Logic: if !Moved and there exists a default folder... */
+ if (!(p->copied && p->deleted) && p->defaultfolder) {
+ /* copy it to the default inbox */
+ filtered = TRUE;
+ filter_driver_log (driver, FILTER_LOG_ACTION, "Copy to default folder");
+ camel_folder_append_message (p->defaultfolder, p->message, p->info, p->ex);
+ }
+
+ if (freeinfo)
+ camel_message_info_free (info);
+
+ if (filtered)
+ filter_driver_log (driver, FILTER_LOG_END, NULL);
+}
diff --git a/filter/filter-element.c b/filter/filter-element.c
index 0c4e72249c..56f47a8161 100644
--- a/filter/filter-element.c
+++ b/filter/filter-element.c
@@ -18,6 +18,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
diff --git a/filter/filter-input.c b/filter/filter-input.c
index d154e9c898..2304b39635 100644
--- a/filter/filter-input.c
+++ b/filter/filter-input.c
@@ -18,6 +18,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
#include <regex.h>
diff --git a/filter/filter-message-search.c b/filter/filter-message-search.c
new file mode 100644
index 0000000000..415bdb170c
--- /dev/null
+++ b/filter/filter-message-search.c
@@ -0,0 +1,806 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Authors: Jeffrey Stedfast <fejj@helixcode.com>
+ *
+ * Copyright 2000 Helix Code, Inc. (www.helixcode.com)
+ *
+ * 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 Street #330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "filter-message-search.h"
+#include <gal/widgets/e-unicode.h>
+#include <e-util/e-sexp.h>
+#include <regex.h>
+#include <string.h>
+#include <ctype.h>
+
+typedef struct {
+ CamelMimeMessage *message;
+ CamelMessageInfo *info;
+ const char *source;
+ CamelException *ex;
+} FilterMessageSearch;
+
+/* ESExp callbacks */
+static ESExpResult *header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *header_matches (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *header_soundex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *header_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *header_full_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *match_all (struct _ESExp *f, int argc, struct _ESExpTerm **argv, FilterMessageSearch *fms);
+static ESExpResult *body_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *body_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *user_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *user_tag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *get_sent_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *get_received_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *get_current_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *get_score (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+static ESExpResult *get_source (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms);
+
+/* builtin functions */
+static struct {
+ char *name;
+ ESExpFunc *func;
+ int type; /* set to 1 if a function can perform shortcut evaluation, or
+ doesn't execute everything, 0 otherwise */
+} symbols[] = {
+ { "match-all", (ESExpFunc *) match_all, 0 },
+ { "body-contains", (ESExpFunc *) body_contains, 0 },
+ { "body-regex", (ESExpFunc *) body_regex, 0 },
+ { "header-contains", (ESExpFunc *) header_contains, 0 },
+ { "header-matches", (ESExpFunc *) header_matches, 0 },
+ { "header-starts-with", (ESExpFunc *) header_starts_with, 0 },
+ { "header-ends-with", (ESExpFunc *) header_ends_with, 0 },
+ { "header-exists", (ESExpFunc *) header_exists, 0 },
+ { "header-soundex", (ESExpFunc *) header_soundex, 0 },
+ { "header-regex", (ESExpFunc *) header_regex, 0 },
+ { "header-full-regex", (ESExpFunc *) header_full_regex, 0 },
+ { "user-tag", (ESExpFunc *) user_tag, 0 },
+ { "user-flag", (ESExpFunc *) user_flag, 0 },
+ { "system-flag", (ESExpFunc *) system_flag, 0 },
+ { "get-sent-date", (ESExpFunc *) get_sent_date, 0 },
+ { "get-received-date", (ESExpFunc *) get_received_date, 0 },
+ { "get-current-date", (ESExpFunc *) get_current_date, 0 },
+ { "get-score", (ESExpFunc *) get_score, 0 },
+ { "get-source", (ESExpFunc *) get_source, 0 },
+};
+
+static ESExpResult *
+header_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc == 2) {
+ char *header = (argv[0])->value.string;
+ char *match = (argv[1])->value.string;
+ const char *contents;
+
+ contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header);
+
+ if (contents) {
+ if (e_utf8_strstrcase (contents, match))
+ matched = TRUE;
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static ESExpResult *
+header_matches (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc == 2) {
+ char *header = (argv[0])->value.string;
+ char *match = (argv[1])->value.string;
+ const char *contents;
+
+ contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header);
+
+ if (contents) {
+ /* danw says to use search-engine style matching...
+ * This means that if the search match string is
+ * lowercase then compare case-insensitive else
+ * compare case-sensitive. */
+ gboolean is_lowercase = TRUE;
+ char *c;
+
+ /* remove any leading white space... */
+ for ( ; *contents && isspace (*contents); contents++);
+
+ for (c = match; *c; c++) {
+ if (isalpha (*c) && isupper (*c)) {
+ is_lowercase = FALSE;
+ break;
+ }
+ }
+
+ if (is_lowercase) {
+ if (!g_strcasecmp (contents, match))
+ matched = TRUE;
+ } else {
+ if (!strcmp (contents, match))
+ matched = TRUE;
+ }
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static ESExpResult *
+header_starts_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc == 2) {
+ char *header = (argv[0])->value.string;
+ char *match = (argv[1])->value.string;
+ const char *contents;
+
+ contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header);
+
+ if (contents && strlen (contents) >= strlen (match)) {
+ /* danw says to use search-engine style matching...
+ * This means that if the search match string is
+ * lowercase then compare case-insensitive else
+ * compare case-sensitive. */
+ gboolean is_lowercase = TRUE;
+ char *c;
+
+ /* remove any leading white space... */
+ for ( ; *contents && isspace (*contents); contents++);
+
+ for (c = match; *c; c++) {
+ if (isalpha (*c) && isupper (*c)) {
+ is_lowercase = FALSE;
+ break;
+ }
+ }
+
+ if (is_lowercase) {
+ if (!g_strncasecmp (contents, match, strlen (match)))
+ matched = TRUE;
+ } else {
+ if (!strncmp (contents, match, strlen (match)))
+ matched = TRUE;
+ }
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static ESExpResult *
+header_ends_with (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc == 2) {
+ char *header = (argv[0])->value.string;
+ char *match = (argv[1])->value.string;
+ const char *contents;
+
+ contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header);
+
+ if (contents && strlen (contents) >= strlen (match)) {
+ /* danw says to use search-engine style matching...
+ * This means that if the search match string is
+ * lowercase then compare case-insensitive else
+ * compare case-sensitive. */
+ gboolean is_lowercase = TRUE;
+ char *c, *end;
+
+ /* remove any leading white space... */
+ for ( ; *contents && isspace (*contents); contents++);
+
+ for (c = match; *c; c++) {
+ if (isalpha (*c) && isupper (*c)) {
+ is_lowercase = FALSE;
+ break;
+ }
+ }
+
+ end = (char *) contents + strlen (contents) - strlen (match);
+
+ if (is_lowercase) {
+ if (!g_strcasecmp (end, match))
+ matched = TRUE;
+ } else {
+ if (!strcmp (end, match))
+ matched = TRUE;
+ }
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static ESExpResult *
+header_exists (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc == 1) {
+ char *header = (argv[0])->value.string;
+ const char *contents;
+
+ contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header);
+
+ if (contents)
+ matched = TRUE;
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static unsigned char soundex_table[256] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 49, 50, 51, 0, 49, 50, 0, 0, 50, 50, 52, 53, 53, 0,
+ 49, 50, 54, 50, 51, 0, 49, 0, 50, 0, 50, 0, 0, 0, 0, 0,
+ 0, 0, 49, 50, 51, 0, 49, 50, 0, 0, 50, 50, 52, 53, 53, 0,
+ 49, 50, 54, 50, 51, 0, 49, 0, 50, 0, 50, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+};
+
+static void
+soundexify (const gchar *sound, gchar code[5])
+{
+ guchar *c, last = '\0';
+ gint n;
+
+ for (c = (guchar *) sound; *c && !isalpha (*c); c++);
+ code[0] = toupper (*c);
+ memset (code + 1, '0', 3);
+ for (n = 1; *c && n < 5; c++) {
+ guchar ch = soundex_table[*c];
+
+ if (ch && ch != last) {
+ code[n++] = ch;
+ last = ch;
+ }
+ }
+ code[4] = '\0';
+}
+
+static gint
+soundexcmp (const gchar *sound1, const gchar *sound2)
+{
+ gchar code1[5], code2[5];
+
+ soundexify (sound1, code1);
+ soundexify (sound2, code2);
+
+ return strcmp (code1, code2);
+}
+
+static ESExpResult *
+header_soundex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc == 2) {
+ char *header = (argv[0])->value.string;
+ char *match = (argv[1])->value.string;
+ const char *contents;
+
+ contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header);
+
+ if (contents) {
+ /* remove any leading white space... */
+ for ( ; *contents && isspace (*contents); contents++);
+
+ if (!soundexcmp (contents, match))
+ matched = TRUE;
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static ESExpResult *
+header_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc == 2) {
+ char *header = (argv[0])->value.string;
+ char *match = (argv[1])->value.string;
+ regex_t regexpat; /* regex patern */
+ regmatch_t *fltmatch;
+ gint regerr = 0;
+ size_t reglen = 0;
+ gchar *regmsg;
+ const char *contents;
+
+ contents = camel_medium_get_header (CAMEL_MEDIUM (fms->message), header);
+
+ regerr = regcomp (&regexpat, match, REG_EXTENDED | REG_NEWLINE | REG_ICASE);
+ if (regerr) {
+ /* regerror gets called twice to get the full error string
+ length to do proper posix error reporting */
+ reglen = regerror (regerr, &regexpat, 0, 0);
+ regmsg = g_malloc0 (reglen + 1);
+ regerror (regerr, &regexpat, regmsg, reglen);
+ camel_exception_setv (fms->ex, CAMEL_EXCEPTION_SYSTEM,
+ _("Failed to perform regex search on message header: %s"),
+ regmsg);
+ g_free (regmsg);
+ regfree (&regexpat);
+ } else {
+ if (contents) {
+ fltmatch = g_new0 (regmatch_t, regexpat.re_nsub);
+
+ if (!regexec (&regexpat, contents, regexpat.re_nsub, fltmatch, 0))
+ matched = TRUE;
+
+ g_free (fltmatch);
+ regfree (&regexpat);
+ }
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static gchar *
+get_full_header (CamelMimeMessage *message)
+{
+ CamelMimePart *mp = CAMEL_MIME_PART (message);
+ GString *str = g_string_new ("");
+ char *ret;
+ struct _header_raw *h;
+
+ for (h = mp->headers; h; h = h->next) {
+ if (h->value != NULL)
+ g_string_sprintfa (str, "%s%s%s\n", h->name,
+ isspace (h->value[0]) ? ":" : ": ", h->value);
+ }
+
+ ret = str->str;
+ g_string_free (str, FALSE);
+
+ return ret;
+}
+
+static ESExpResult *
+header_full_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc == 1) {
+ char *match = (argv[0])->value.string;
+ regex_t regexpat; /* regex patern */
+ regmatch_t *fltmatch;
+ gint regerr = 0;
+ size_t reglen = 0;
+ gchar *regmsg;
+ char *contents;
+
+ contents = get_full_header (fms->message);
+
+ regerr = regcomp (&regexpat, match, REG_EXTENDED | REG_NEWLINE | REG_ICASE);
+ if (regerr) {
+ /* regerror gets called twice to get the full error string
+ length to do proper posix error reporting */
+ reglen = regerror (regerr, &regexpat, 0, 0);
+ regmsg = g_malloc0 (reglen + 1);
+ regerror (regerr, &regexpat, regmsg, reglen);
+ camel_exception_setv (fms->ex, CAMEL_EXCEPTION_SYSTEM,
+ _("Failed to perform regex search on message header: %s"),
+ regmsg);
+ g_free (regmsg);
+ regfree (&regexpat);
+ } else {
+ if (contents) {
+ fltmatch = g_new0 (regmatch_t, regexpat.re_nsub);
+
+ if (!regexec (&regexpat, contents, regexpat.re_nsub, fltmatch, 0))
+ matched = TRUE;
+
+ g_free (fltmatch);
+ regfree (&regexpat);
+ }
+ }
+ g_free (contents);
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static ESExpResult *
+match_all (struct _ESExp *f, int argc, struct _ESExpTerm **argv, FilterMessageSearch *fms)
+{
+ /* match-all: when dealing with single messages is a no-op */
+ ESExpResult *r;
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ if (argv[0]->type == ESEXP_RES_BOOL)
+ r->value.bool = argv[0]->value.bool;
+ else
+ r->value.bool = FALSE;
+
+ return r;
+}
+
+static gboolean
+mime_part_matches (CamelMimePart *mime_part, const char *match, gboolean regex, CamelException *ex)
+{
+ CamelStream *stream;
+ GByteArray *array;
+ char *text;
+ regex_t regexpat; /* regex patern */
+ regmatch_t *fltmatch;
+ gint regerr = 0;
+ size_t reglen = 0;
+ gchar *regmsg;
+ gboolean matched = FALSE;
+
+ array = g_byte_array_new ();
+ stream = camel_stream_mem_new_with_byte_array (array);
+ camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (mime_part), stream);
+ camel_object_unref (CAMEL_OBJECT (stream));
+
+ g_byte_array_append (array, "", 1);
+
+ text = array->data;
+
+ if (regex) {
+ regerr = regcomp (&regexpat, match, REG_EXTENDED | REG_NEWLINE | REG_ICASE);
+ if (regerr) {
+ /* regerror gets called twice to get the full error string
+ length to do proper posix error reporting */
+ reglen = regerror (regerr, &regexpat, 0, 0);
+ regmsg = g_malloc0 (reglen + 1);
+ regerror (regerr, &regexpat, regmsg, reglen);
+ camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
+ "Failed to perform regex search on message body: %s",
+ regmsg);
+ g_free (regmsg);
+ regfree (&regexpat);
+ } else {
+ fltmatch = g_new0 (regmatch_t, regexpat.re_nsub);
+
+ if (!regexec (&regexpat, text, regexpat.re_nsub, fltmatch, 0))
+ matched = TRUE;
+
+ g_free (fltmatch);
+ regfree (&regexpat);
+ }
+ } else {
+ if (strstr (text, match))
+ matched = TRUE;
+ }
+
+ g_byte_array_free (array, TRUE);
+
+ return matched;
+}
+
+static gboolean
+handle_multipart (CamelMultipart *multipart, const char *match, gboolean regex, CamelException *ex)
+{
+ gboolean matched = FALSE;
+ int i, nparts;
+
+ nparts = camel_multipart_get_number (multipart);
+
+ for (i = 0; i < nparts && !matched; i++) {
+ CamelContentType *content;
+ CamelMimePart *mime_part;
+
+ mime_part = camel_multipart_get_part (multipart, i);
+ content = camel_mime_part_get_content_type (mime_part);
+
+ if (header_content_type_is (content, "text", "*")) {
+ /* we only want to match text parts */
+ matched = mime_part_matches (mime_part, match, regex, ex);
+
+ if (camel_exception_is_set (ex))
+ break;
+ } else if (header_content_type_is (content, "multipart", "*")) {
+ CamelDataWrapper *wrapper;
+ CamelMultipart *mpart;
+
+ wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (mime_part));
+ mpart = CAMEL_MULTIPART (wrapper);
+
+ matched = handle_multipart (mpart, match, regex, ex);
+
+ if (camel_exception_is_set (ex))
+ break;
+ }
+ }
+
+ return matched;
+}
+
+static ESExpResult *
+body_contains (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc > 0) {
+ CamelContentType *content;
+ char *match;
+
+ match = (*argv)->value.string;
+
+ content = camel_mime_part_get_content_type (CAMEL_MIME_PART (fms->message));
+
+ if (header_content_type_is (content, "multipart", "*")) {
+ CamelDataWrapper *wrapper;
+ CamelMultipart *multipart;
+
+ wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (CAMEL_MIME_PART (fms->message)));
+ multipart = CAMEL_MULTIPART (wrapper);
+
+ matched = handle_multipart (multipart, match, FALSE, fms->ex);
+ } else {
+ /* single-part message so just search the entire message */
+ matched = mime_part_matches (CAMEL_MIME_PART (fms->message), match, FALSE, fms->ex);
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static ESExpResult *
+body_regex (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ gboolean matched = FALSE;
+ ESExpResult *r;
+
+ if (argc > 0) {
+ CamelContentType *content;
+ char *match;
+
+ match = (*argv)->value.string;
+
+ content = camel_mime_part_get_content_type (CAMEL_MIME_PART (fms->message));
+
+ if (header_content_type_is (content, "multipart", "*")) {
+ CamelDataWrapper *wrapper;
+ CamelMultipart *multipart;
+
+ wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (CAMEL_MIME_PART (fms->message)));
+ multipart = CAMEL_MULTIPART (wrapper);
+
+ matched = handle_multipart (multipart, match, TRUE, fms->ex);
+ } else {
+ /* single-part message so just search the entire message */
+ matched = mime_part_matches (CAMEL_MIME_PART (fms->message), match, TRUE, fms->ex);
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = matched;
+
+ return r;
+}
+
+static ESExpResult *
+user_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ ESExpResult *r;
+ gboolean truth = FALSE;
+ int i;
+
+ /* performs an OR of all words */
+ for (i = 0; i < argc && !truth; i++) {
+ if (argv[i]->type == ESEXP_RES_STRING
+ && camel_flag_get (&fms->info->user_flags, argv[i]->value.string)) {
+ truth = TRUE;
+ break;
+ }
+ }
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = truth;
+
+ return r;
+}
+
+static ESExpResult *
+system_flag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ ESExpResult *r;
+ gboolean truth = FALSE;
+
+ if (argc == 1)
+ truth = camel_system_flag_get (fms->info->flags, argv[0]->value.string);
+
+ r = e_sexp_result_new (ESEXP_RES_BOOL);
+ r->value.bool = truth;
+
+ return r;
+}
+
+static ESExpResult *
+user_tag (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ ESExpResult *r;
+ const char *tag;
+
+ tag = camel_tag_get (&fms->info->user_tags, argv[0]->value.string);
+
+ r = e_sexp_result_new (ESEXP_RES_STRING);
+ r->value.string = g_strdup (tag ? tag : "");
+
+ return r;
+}
+
+static ESExpResult *
+get_sent_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ ESExpResult *r;
+
+ r = e_sexp_result_new(ESEXP_RES_INT);
+ r->value.number = camel_mime_message_get_date(fms->message, NULL);
+
+ return r;
+}
+
+static ESExpResult *
+get_received_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ ESExpResult *r;
+
+ r = e_sexp_result_new(ESEXP_RES_INT);
+ r->value.number = camel_mime_message_get_date_received(fms->message, NULL);
+
+ return r;
+}
+
+static ESExpResult *
+get_current_date (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ ESExpResult *r;
+
+ r = e_sexp_result_new (ESEXP_RES_INT);
+ r->value.number = time (NULL);
+
+ return r;
+}
+
+static ESExpResult *
+get_score (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ ESExpResult *r;
+ const char *tag;
+
+ tag = camel_tag_get (&fms->info->user_tags, "score");
+
+ r = e_sexp_result_new (ESEXP_RES_INT);
+ if (tag)
+ r->value.number = atoi (tag);
+ else
+ r->value.number = 0;
+
+ return r;
+}
+
+static ESExpResult *
+get_source (struct _ESExp *f, int argc, struct _ESExpResult **argv, FilterMessageSearch *fms)
+{
+ ESExpResult *r;
+
+ r = e_sexp_result_new (ESEXP_RES_STRING);
+ r->value.string = g_strdup (fms->source);
+
+ return r;
+}
+
+gboolean
+filter_message_search (CamelMimeMessage *message, CamelMessageInfo *info,
+ const char *source, const char *expression, CamelException *ex)
+{
+ FilterMessageSearch *fms;
+ ESExp *sexp;
+ ESExpResult *result;
+ gboolean retval;
+ int i;
+
+ fms = g_new (FilterMessageSearch, 1);
+ fms->message = message;
+ fms->info = info;
+ fms->source = source;
+ fms->ex = ex;
+
+ sexp = e_sexp_new ();
+
+ for (i = 0; i < sizeof (symbols) / sizeof (symbols[0]); i++) {
+ if (symbols[i].type == 1) {
+ e_sexp_add_ifunction (sexp, 0, symbols[i].name,
+ (ESExpIFunc *)symbols[i].func, fms);
+ } else {
+ e_sexp_add_function (sexp, 0, symbols[i].name,
+ symbols[i].func, fms);
+ }
+ }
+
+ e_sexp_input_text (sexp, expression, strlen (expression));
+ e_sexp_parse (sexp);
+ result = e_sexp_eval (sexp);
+
+ g_free (fms);
+
+ if (result->type == ESEXP_RES_BOOL)
+ retval = result->value.bool;
+ else
+ retval = FALSE;
+
+ e_sexp_unref(sexp);
+ e_sexp_result_free (result);
+
+ return retval;
+}
diff --git a/filter/filter-score.c b/filter/filter-score.c
index 5053998444..722ac5bb65 100644
--- a/filter/filter-score.c
+++ b/filter/filter-score.c
@@ -20,6 +20,10 @@
*
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
#include <gnome-xml/xmlmemory.h>
diff --git a/filter/filter-system-flag.c b/filter/filter-system-flag.c
index 3667876e23..a74850ded1 100644
--- a/filter/filter-system-flag.c
+++ b/filter/filter-system-flag.c
@@ -21,6 +21,10 @@
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
#include <gnome-xml/xmlmemory.h>
@@ -60,11 +64,11 @@ struct _system_flag {
char *title;
char *value;
} system_flags[] = {
- { _("Replied to"), "Answered" },
+ { N_("Replied to"), "Answered" },
/*{ _("Deleted"), "Deleted" },*/
/*{ _("Draft"), "Draft" },*/
- { _("Important"), "Flagged" },
- { _("Read"), "Seen" },
+ { N_("Important"), "Flagged" },
+ { N_("Read"), "Seen" },
{ NULL, NULL }
};
diff --git a/filter/filter-url.c b/filter/filter-url.c
index 7f507d0dca..a983d81f78 100644
--- a/filter/filter-url.c
+++ b/filter/filter-url.c
@@ -21,6 +21,10 @@
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
#include <gnome-xml/xmlmemory.h>
diff --git a/filter/rule-context.c b/filter/rule-context.c
index 7bb0b8fb65..386e2cb7ab 100644
--- a/filter/rule-context.c
+++ b/filter/rule-context.c
@@ -18,6 +18,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <errno.h>
#include <gtk/gtk.h>
#include <gnome.h>
diff --git a/filter/score-context.c b/filter/score-context.c
index a171bd1240..b4c8654ff8 100644
--- a/filter/score-context.c
+++ b/filter/score-context.c
@@ -18,6 +18,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>
diff --git a/filter/vfolder-context.c b/filter/vfolder-context.c
index 41453ea310..d895676404 100644
--- a/filter/vfolder-context.c
+++ b/filter/vfolder-context.c
@@ -18,6 +18,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <gtk/gtk.h>
#include <gnome.h>