2006-03-10 [colin] 2.0.0cvs130
[claws.git] / src / plugins / spamassassin / spamassassin.c
index 755e20d754d46b443c24ed69d7c9246a8ec02579..c27b16f66a4bc8a720424f5e40c6d8d0661e62b9 100644 (file)
@@ -83,6 +83,8 @@ static MessageCallback message_callback;
 static SpamAssassinConfig config;
 
 static PrefParam param[] = {
+       {"enable", "FALSE", &config.enable, P_BOOL,
+       NULL, NULL, NULL},
        {"transport", "0", &config.transport, P_INT,
         NULL, NULL, NULL},
        {"hostname", "localhost", &config.hostname, P_STRING,
@@ -124,6 +126,9 @@ static gboolean msg_is_spam(FILE *fp)
        struct message m;
        gboolean is_spam = FALSE;
 
+       if (!config.enable)
+               return FALSE;
+
        transport_init(&trans);
        switch (config.transport) {
        case SPAMASSASSIN_TRANSPORT_LOCALHOST:
@@ -182,7 +187,8 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
        int pid = 0;
        int status;
 
-       if (config.transport == SPAMASSASSIN_DISABLED) {
+       /* SPAMASSASSIN_DISABLED : keep test for compatibility purpose */
+       if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
                log_error("Spamassassin plugin is disabled by its preferences.\n");
                return FALSE;
        }
@@ -263,7 +269,6 @@ gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
 {
        gchar *contents;
        gchar *fname = get_tmp_file();
-       GError *err;
 
        if (fname != NULL) {
                contents = g_strdup_printf(
@@ -271,10 +276,7 @@ gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
                                                config.hostname, config.port, 
                                                config.username, config.timeout,
                                                config.max_size * 1024, spam?"spam":"ham");
-               if (!g_file_set_contents(fname, contents,
-                                                       strlen(contents), &err)) {
-                       g_warning(err->message);
-                       g_error_free(err);
+               if (str_write_to_file(contents, fname) < 0) {
                        g_free(fname);
                        fname = NULL;
                }
@@ -288,7 +290,6 @@ int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
 {
        gchar *cmd = NULL;
        gchar *file = NULL;
-       gboolean async = FALSE;
        const gchar *shell = g_getenv("SHELL");
        gchar *spamc_wrapper = NULL;
 
@@ -298,7 +299,9 @@ int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
 
        if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
        &&  prefs_common.work_offline
-       &&  !inc_offline_should_override()) {
+       &&  !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to feed this mail(s) to the remote learner."))) {
                return -1;
        }
 
@@ -340,7 +343,7 @@ int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
                                        tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
                                                                                tmpfile, NULL);
                                        debug_print("%s\n", tmpcmd);
-                                       execute_command_line(tmpcmd, TRUE);
+                                       execute_command_line(tmpcmd, FALSE);
                                        g_free(tmpcmd);
                                }
                                if (tmpfile != NULL) {
@@ -373,17 +376,14 @@ int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
                                        g_free(tmpfile);
                                }
                        }
-                       async = TRUE;
                }
        }
        if (cmd == NULL) {
                return -1;
        }
        debug_print("%s\n", cmd);
-       /* only run async if we have a list, or we could end up
-        * forking lots of perl processes and bury the machine */
-       
-       execute_command_line(cmd, async);
+       /* only run sync calls to sa-learn/spamc to prevent system lockdown */
+       execute_command_line(cmd, FALSE);
        g_free(cmd);
        if (spamc_wrapper != NULL) {
                g_free(spamc_wrapper);
@@ -467,11 +467,10 @@ gint plugin_init(gchar **error)
                spamassassin_register_hook();
        }
 
-       if (config.transport == SPAMASSASSIN_DISABLED) {
+       if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
                log_error("Spamassassin plugin is loaded but disabled by its preferences.\n");
        }
-
-       if (config.transport != SPAMASSASSIN_DISABLED) {
+       else {
                if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
                        debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
                procmsg_register_spam_learner(spamassassin_learn);