2006-08-30 [colin] 2.4.0cvs113
[claws.git] / src / plugins / bogofilter / bogofilter.c
index 9ddba9614f36c3091a7bdf33a3c3faaf615469c7..0c6a38853eb18847aa96a8f402147a9edf010a63 100644 (file)
@@ -156,15 +156,22 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
        }
        
        if (status == 3) { /* I/O or other errors */
-               if (!warned_error) {
-                       alertpanel_error(_("The Bogofilter plugin couldn't filter "
+               gchar *msg = _("The Bogofilter plugin couldn't filter "
                                           "a message. The probable cause of the "
                                           "error is that it didn't learn from any mail.\n"
                                           "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
                                           "ham\" to train Bogofilter with a few hundred "
-                                          "spam and ham messages."));
+                                          "spam and ham messages.");
+               if (!prefs_common.no_recv_err_panel) {
+                       if (!warned_error) {
+                               alertpanel_error(msg);
+                       }
+                       warned_error = TRUE;
+               } else {
+                       gchar *tmp = g_strdup_printf("%s\n", msg);
+                       log_error(tmp);
+                       g_free(tmp);
                }
-               warned_error = TRUE;
        }
        
        return FALSE;
@@ -216,33 +223,92 @@ int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
                MsgInfo *info;
                int total = g_slist_length(msglist);
                int done = 0;
+               gboolean some_correction = FALSE, some_no_correction = FALSE;
+       
                if (message_callback != NULL)
                        message_callback(_("Bogofilter: learning from messages..."), total, 0);
                
-               for (; cur && status == 0; cur = cur->next) {
+               for (cur = msglist; cur && status == 0; cur = cur->next) {
                        info = (MsgInfo *)cur->data;
-                       file = procmsg_get_message_file(info);
-
                        if (spam)
-                               /* learn as spam */
-                               cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
+                               some_no_correction = TRUE;
                        else if (MSG_IS_SPAM(info->flags))
                                /* correct bogofilter, this wasn't spam */
-                               cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
+                               some_correction = TRUE;
                        else 
-                               /* learn as ham */
-                               cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
-       
-                       if ((status = execute_command_line(cmd, FALSE)) != 0)
-                               alertpanel_error(_("Learning failed; `%s` returned with status %d."),
-                                               cmd, status);
-
-                       g_free(cmd);
-                       g_free(file);
-                       done++;
-                       if (message_callback != NULL)
-                               message_callback(NULL, total, done);
+                               some_no_correction = TRUE;
+                       
+               }
+               
+               if (some_correction && some_no_correction) {
+                       /* we potentially have to do different stuff for every mail */
+                       for (cur = msglist; cur && status == 0; cur = cur->next) {
+                               info = (MsgInfo *)cur->data;
+                               file = procmsg_get_message_file(info);
+
+                               if (spam)
+                                       /* learn as spam */
+                                       cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
+                               else if (MSG_IS_SPAM(info->flags))
+                                       /* correct bogofilter, this wasn't spam */
+                                       cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
+                               else 
+                                       /* learn as ham */
+                                       cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
+
+                               if ((status = execute_command_line(cmd, FALSE)) != 0)
+                                       alertpanel_error(_("Learning failed; `%s` returned with status %d."),
+                                                       cmd, status);
+
+                               g_free(cmd);
+                               g_free(file);
+                               done++;
+                               if (message_callback != NULL)
+                                       message_callback(NULL, total, done);
+                       }
+               } else if (some_correction || some_no_correction) {
+                       int count = 0;
+                       gchar *file_list = NULL;
+                       cur = msglist;
+                       
+                       while (cur && status == 0) {
+                               gchar *tmp = NULL;
+                               info = (MsgInfo *)cur->data;
+                               file = procmsg_get_message_file(info);
+                               if (file) {
+                                       tmp = g_strdup_printf("%s%s'%s'", 
+                                               file_list?file_list:"",
+                                               file_list?" ":"",
+                                               file);
+                                       g_free(file_list);
+                                       file_list = tmp;
+                               }
+                               g_free(file);
+                               count ++;
+                               done++;
+                               if (count > 10 || cur->next == NULL) {
+                                       /* flush */
+                                       if (some_correction && !some_no_correction)
+                                               cmd = g_strdup_printf("%s -Sn -B %s", bogo_exec, file_list);
+                                       else if (some_no_correction && !some_correction)
+                                               cmd = g_strdup_printf("%s -%c -B %s", bogo_exec, spam?'s':'n', file_list);
+                                       else
+                                               g_warning("duuh bogofilter plugin shouldn't be there!\n");
+                                       if ((status = execute_command_line(cmd, FALSE)) != 0)
+                                               alertpanel_error(_("Learning failed; `%s` returned with status %d."),
+                                                               cmd, status);
+                                       count = 0;
+                                       g_free(cmd);
+                                       g_free(file_list);
+                                       file_list = NULL;
+                               }
+                               if (message_callback != NULL)
+                                       message_callback(NULL, total, done);
+                               cur = cur->next;
+                       }
+                       g_free(file_list);
                }
+
                if (message_callback != NULL)
                        message_callback(NULL, 0, 0);
                return 0;