From 83642a4bb4f15145e5bd60900df6d2ad43ecffbe Mon Sep 17 00:00:00 2001 From: Christoph Hohmann Date: Mon, 11 Aug 2003 21:47:44 +0000 Subject: [PATCH] 0.9.4claws18 * src/plugins/spamassassin/spamassassin.c run spam check in background process --- ChangeLog.claws | 5 ++ configure.ac | 2 +- src/plugins/spamassassin/spamassassin.c | 89 +++++++++++++++++++------ 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/ChangeLog.claws b/ChangeLog.claws index 1a5133f3e..a823f9086 100644 --- a/ChangeLog.claws +++ b/ChangeLog.claws @@ -1,3 +1,8 @@ +2003-08-11 [christoph] 0.9.4claws18 + + * src/plugins/spamassassin/spamassassin.c + run spam check in background process + 2003-08-10 [alfons] 0.9.4claws17 * src/summaryview.c diff --git a/configure.ac b/configure.ac index 95b8bd63b..395b42d3c 100644 --- a/configure.ac +++ b/configure.ac @@ -11,7 +11,7 @@ MINOR_VERSION=9 MICRO_VERSION=4 INTERFACE_AGE=0 BINARY_AGE=0 -EXTRA_VERSION=17 +EXTRA_VERSION=18 if test $EXTRA_VERSION -eq 0; then VERSION=${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}claws else diff --git a/src/plugins/spamassassin/spamassassin.c b/src/plugins/spamassassin/spamassassin.c index b42af74b8..8af60adcb 100644 --- a/src/plugins/spamassassin/spamassassin.c +++ b/src/plugins/spamassassin/spamassassin.c @@ -23,6 +23,9 @@ #include "defs.h" +#include +#include + #include #if HAVE_LOCALE_H @@ -65,6 +68,11 @@ #include #endif +enum { + CHILD_RUNNING = 1 << 0, + TIMEOUT_RUNNING = 1 << 1, +}; + static guint hook_id; static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY; static gchar *username = NULL; @@ -88,20 +96,22 @@ static PrefParam param[] = { {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL} }; -static gboolean mail_filtering_hook(gpointer source, gpointer data) +gboolean timeout_func(gpointer data) { - MailFilteringData *mail_filtering_data = (MailFilteringData *) source; - MsgInfo *msginfo = mail_filtering_data->msginfo; - gboolean is_spam = FALSE; - FILE *fp = NULL; - struct message m; - struct sockaddr addr; - int ret; + gint *running = (gint *) data; - if (!config.enable) - return FALSE; + if (*running & CHILD_RUNNING) + return TRUE; - debug_print("Filtering message %d\n", msginfo->msgnum); + *running &= ~TIMEOUT_RUNNING; + return FALSE; +} + +static gboolean msg_is_spam(FILE *fp) +{ + struct sockaddr addr; + struct message m; + gboolean is_spam = FALSE; if (lookup_host(config.hostname, config.port, &addr) != EX_OK) { debug_print("failed to look up spamd host\n"); @@ -112,21 +122,14 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data) m.max_len = config.max_size * 1024; m.timeout = 30; - if ((fp = procmsg_open_message(msginfo)) == NULL) { - debug_print("failed to open message file\n"); - return FALSE; - } - if (message_read(fileno(fp), flags, &m) != EX_OK) { debug_print("failed to read message\n"); - fclose(fp); message_cleanup(&m); return FALSE; } - if ((ret = message_filter(&addr, username, flags, &m)) != EX_OK) { + if (message_filter(&addr, username, flags, &m) != EX_OK) { debug_print("filtering the message failed\n"); - fclose(fp); message_cleanup(&m); return FALSE; } @@ -135,6 +138,54 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data) is_spam = TRUE; message_cleanup(&m); + + return is_spam; +} + +static gboolean mail_filtering_hook(gpointer source, gpointer data) +{ + MailFilteringData *mail_filtering_data = (MailFilteringData *) source; + MsgInfo *msginfo = mail_filtering_data->msginfo; + gboolean is_spam = FALSE; + FILE *fp = NULL; + int pid = 0; + int status; + + if (!config.enable) + return FALSE; + + debug_print("Filtering message %d\n", msginfo->msgnum); + + if ((fp = procmsg_open_message(msginfo)) == NULL) { + debug_print("failed to open message file\n"); + return FALSE; + } + + pid = fork(); + if (pid == 0) { + _exit(msg_is_spam(fp) ? 1 : 0); + } else { + gint running = 0; + + running |= CHILD_RUNNING; + + g_timeout_add(1000, timeout_func, &running); + running |= TIMEOUT_RUNNING; + + while(running & CHILD_RUNNING) { + waitpid(pid, &status, WNOHANG); + if (WIFEXITED(status)) { + running &= ~CHILD_RUNNING; + } + + g_main_iteration(TRUE); + } + + while (running & TIMEOUT_RUNNING) + g_main_iteration(TRUE); + } + is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE; + fclose(fp); if (is_spam) { -- 2.25.1