2008-05-12 [wwp] 3.4.0cvs30
[claws.git] / src / plugins / bogofilter / bogofilter.c
index 0ca6aec4dbd4ba5a46ffcc8d3540116d342a1a64..a1e843f9b66f5be18f45661e04931af4df56a29e 100644 (file)
@@ -5,7 +5,7 @@
  *
  * 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
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -14,8 +14,8 @@
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -100,7 +100,11 @@ static PrefParam param[] = {
         NULL, NULL, NULL},
        {"whitelist_ab", "FALSE", &config.whitelist_ab, P_BOOL,
         NULL, NULL, NULL},
-       {"whitelist_ab_folder", "Any", &config.whitelist_ab_folder, P_STRING,
+       {"whitelist_ab_folder", N_("Any"), &config.whitelist_ab_folder, P_STRING,
+        NULL, NULL, NULL},
+       {"learn_from_whitelist", "FALSE", &config.learn_from_whitelist, P_BOOL,
+        NULL, NULL, NULL},
+       {"mark_as_read", "TRUE", &config.mark_as_read, P_BOOL,
         NULL, NULL, NULL},
 
        {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
@@ -212,7 +216,7 @@ static void bogofilter_do_filter(BogoFilterData *data)
                        gchar *ab_folderpath;
 
                        if (*config.whitelist_ab_folder == '\0' ||
-                               strcasecmp(config.whitelist_ab_folder, _("Any")) == 0) {
+                               strcasecmp(config.whitelist_ab_folder, "Any") == 0) {
                                /* match the whole addressbook */
                                ab_folderpath = NULL;
                        } else {
@@ -278,18 +282,28 @@ static void bogofilter_do_filter(BogoFilterData *data)
                                                        fclose (output);
                                                else {
                                                        gchar tmpbuf[BUFFSIZE];
+                                                       gboolean err = FALSE;
                                                        const gchar *bogosity = *parts[1] == 'S' ? "Spam":
                                                                                 (*parts[1] == 'H' ? "Ham":"Unsure");
                                                        gchar *tmpstr = g_strdup_printf(
                                                                        "X-Claws-Bogosity: %s, spamicity=%s%s\n",
                                                                        bogosity, parts[2],
                                                                        whitelisted?" [whitelisted]":"");
-                                                       fwrite(tmpstr, 1, strlen(tmpstr), output);
-                                                       while (fgets(tmpbuf, sizeof(buf), input))
-                                                               fputs(tmpbuf, output);
+                                                       if (fwrite(tmpstr, 1, strlen(tmpstr), output) < strlen(tmpstr)) {
+                                                               err = TRUE;
+                                                       } else {
+                                                               while (fgets(tmpbuf, sizeof(buf), input)) {
+                                                                       if (fputs(tmpbuf, output) == EOF) {
+                                                                               err = TRUE;
+                                                                               break;
+                                                                       }
+                                                               }
+                                                       }
                                                        fclose(input);
-                                                       fclose(output);
-                                                       move_file(tmpfile, file, TRUE);
+                                                       if (fclose(output) == EOF)
+                                                               err = TRUE;
+                                                       if (!err)
+                                                               move_file(tmpfile, file, TRUE);
                                                        g_free(tmpstr);
                                                }
                                                g_free(tmpfile);
@@ -304,9 +318,11 @@ static void bogofilter_do_filter(BogoFilterData *data)
                                                        data->mail_filtering_data->filtered, msginfo);
                                                data->new_spams = g_slist_prepend(data->new_spams, msginfo);
 
-                                       } else if (whitelisted && parts && parts[0] && parts[1] && *parts[1] == 'S') {
+                                       } else if (whitelisted && parts && parts[0] && parts[1] && 
+                                                       (*parts[1] == 'S' || *parts[1] == 'U')) {
 
-                                               debug_print("message %d is whitelisted spam\n", msginfo->msgnum);
+                                               debug_print("message %d is whitelisted %s\n", msginfo->msgnum,
+                                                       *parts[1] == 'S' ? "spam":"unsure");
                                                /* Whitelisted spam will *not* be filtered away, but continue
                                                 * their trip through filtering as if it was ham. */
                                                data->mail_filtering_data->unfiltered = g_slist_prepend(
@@ -354,7 +370,7 @@ static void bogofilter_do_filter(BogoFilterData *data)
                else
                        status = WEXITSTATUS(status);
        }
-       
+
        to_filter_data->status = status; 
 }
 
@@ -530,23 +546,46 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
        for (cur = new_hams; cur; cur = cur->next) {
                MsgInfo *msginfo = (MsgInfo *)cur->data;
                procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
+               debug_print("unflagging ham: %d\n", msginfo->msgnum);
        }
        /* unflag unsure */
        for (cur = new_unsure; cur; cur = cur->next) {
                MsgInfo *msginfo = (MsgInfo *)cur->data;
                procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
+               debug_print("unflagging unsure: %d\n", msginfo->msgnum);
        }
-       /* flag whitelisted spams */
-       for (cur = whitelisted_new_spams; cur; cur = cur->next) {
-               MsgInfo *msginfo = (MsgInfo *)cur->data;
-               procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
+       if (config.learn_from_whitelist && whitelisted_new_spams) {
+               /* flag whitelisted spams */
+               for (cur = whitelisted_new_spams; cur; cur = cur->next) {
+                       MsgInfo *msginfo = (MsgInfo *)cur->data;
+                       procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
+                       debug_print("flagging whitelisted non-ham: %d\n", msginfo->msgnum);
+               }
+               /* correct bogo */
+               bogofilter_learn(NULL, whitelisted_new_spams, FALSE);
+
+               /* unflag them */
+               for (cur = whitelisted_new_spams; cur; cur = cur->next) {
+                       MsgInfo *msginfo = (MsgInfo *)cur->data;
+                       procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
+                       debug_print("unflagging whitelisted non-ham: %d\n", msginfo->msgnum);
+               }
+       } else {
+               for (cur = whitelisted_new_spams; cur; cur = cur->next) {
+                       MsgInfo *msginfo = (MsgInfo *)cur->data;
+                       procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
+                       debug_print("not flagging whitelisted non-ham: %d\n", msginfo->msgnum);
+               }
        }
+
        /* flag spams and delete them if !config.receive_spam 
         * (if config.receive_spam is set, we'll move them later) */
        for (cur = new_spams; cur; cur = cur->next) {
                MsgInfo *msginfo = (MsgInfo *)cur->data;
                if (config.receive_spam) {
-                       procmsg_msginfo_change_flags(msginfo, MSG_SPAM, 0, ~0, 0);
+                       if (config.mark_as_read)
+                               procmsg_msginfo_unset_flags(msginfo, ~0, 0);
+                       procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
                } else {
                        folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
                }
@@ -568,13 +607,11 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
                                           bogo_args[0], bogo_args[1], bogo_args[2]);
                if (!prefs_common.no_recv_err_panel) {
                        if (!warned_error) {
-                               alertpanel_error(msg);
+                               alertpanel_error("%s", msg);
                        }
                        warned_error = TRUE;
                } else {
-                       gchar *tmp = g_strdup_printf("%s\n", msg);
-                       log_error(LOG_PROTOCOL, tmp);
-                       g_free(tmp);
+                       log_error(LOG_PROTOCOL, "%s\n", msg);
                }
                g_free(msg);
        }
@@ -629,7 +666,7 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
                        if (save_folder) {
                                for (cur = new_spams; cur; cur = cur->next) {
                                        msginfo = (MsgInfo *)cur->data;
-                                       msginfo->is_move = TRUE;
+                                       msginfo->filter_op = IS_MOVE;
                                        msginfo->to_filter_folder = save_folder;
                                }
                        }
@@ -669,7 +706,7 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
                        if (save_unsure_folder) {
                                for (cur = new_unsure; cur; cur = cur->next) {
                                        msginfo = (MsgInfo *)cur->data;
-                                       msginfo->is_move = TRUE;
+                                       msginfo->filter_op = IS_MOVE;
                                        msginfo->to_filter_folder = save_unsure_folder;
                                }
                        }
@@ -863,9 +900,11 @@ void bogofilter_save_config(void)
                prefs_file_close_revert(pfile);
                return;
        }
-       fprintf(pfile->fp, "\n");
-
-       prefs_file_close(pfile);
+        if (fprintf(pfile->fp, "\n") < 0) {
+               FILE_OP_ERROR(rcpath, "fprintf");
+               prefs_file_close_revert(pfile);
+       } else
+               prefs_file_close(pfile);
 }
 
 void bogofilter_set_message_callback(MessageCallback callback)
@@ -879,7 +918,7 @@ gint plugin_init(gchar **error)
 
        hook_id = -1;
 
-       if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
+       if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
                                VERSION_NUMERIC, PLUGIN_NAME, error))
                return -1;
 
@@ -933,7 +972,7 @@ FolderItem *bogofilter_get_spam_folder(MsgInfo *msginfo)
        return item;
 }
 
-void plugin_done(void)
+gboolean plugin_done(void)
 {
        if (hook_id != -1) {
                bogofilter_unregister_hook();
@@ -946,6 +985,7 @@ void plugin_done(void)
        procmsg_unregister_spam_learner(bogofilter_learn);
        procmsg_spam_set_folder(NULL, NULL);
        debug_print("Bogofilter plugin unloaded\n");
+       return TRUE;
 }
 
 const gchar *plugin_name(void)
@@ -977,7 +1017,7 @@ const gchar *plugin_type(void)
 
 const gchar *plugin_licence(void)
 {
-       return "GPL";
+       return "GPL3+";
 }
 
 const gchar *plugin_version(void)