2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 Colin Leroy <colin@colino.net> and
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
32 #include <glib/gi18n.h>
38 #include "common/claws.h"
39 #include "common/version.h"
41 #include "common/utils.h"
46 #include "prefs_gtk.h"
48 #include "bogofilter.h"
51 #include "prefs_common.h"
52 #include "alertpanel.h"
53 #include "addr_compl.h"
55 #ifdef HAVE_SYSEXITS_H
61 #ifdef HAVE_SYS_ERRNO_H
62 #include <sys/errno.h>
67 #ifdef HAVE_SYS_TIME_H
77 #define PLUGIN_NAME (_("Bogofilter"))
79 static guint hook_id = -1;
80 static MessageCallback message_callback;
82 static BogofilterConfig config;
84 static PrefParam param[] = {
85 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
87 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
89 {"save_folder", NULL, &config.save_folder, P_STRING,
91 {"save_unsure", "FALSE", &config.save_unsure, P_BOOL,
93 {"save_unsure_folder", NULL, &config.save_unsure_folder, P_STRING,
95 {"max_size", "250", &config.max_size, P_INT,
97 {"bogopath", "bogofilter", &config.bogopath, P_STRING,
99 {"insert_header", "FALSE", &config.insert_header, P_BOOL,
101 {"whitelist_ab", "FALSE", &config.whitelist_ab, P_BOOL,
103 {"whitelist_ab_folder", N_("Any"), &config.whitelist_ab_folder, P_STRING,
105 {"learn_from_whitelist", "FALSE", &config.learn_from_whitelist, P_BOOL,
107 {"mark_as_read", "TRUE", &config.mark_as_read, P_BOOL,
110 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
114 * Helper function for spawn_with_input() - write an entire
124 gssize count = write (fd, buf, to_write);
140 typedef struct _BogoFilterData {
141 MailFilteringData *mail_filtering_data;
147 GSList *whitelisted_new_spams;
153 static BogoFilterData *to_filter_data = NULL;
155 static gboolean filter_th_done = FALSE;
156 static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
157 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
158 static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
161 static gboolean found_in_addressbook(const gchar *address)
164 gboolean found = FALSE;
170 addr = g_strdup(address);
171 extract_address(addr);
172 num_addr = complete_address(addr);
174 /* skip first item (this is the search string itself) */
176 for (; i < num_addr && !found; i++) {
177 gchar *caddr = get_complete_address(i);
178 extract_address(caddr);
179 if (strcasecmp(caddr, addr) == 0)
188 static void bogofilter_do_filter(BogoFilterData *data)
191 gint bogo_stdin, bogo_stdout;
192 GError *error = NULL;
193 gboolean bogo_forked;
197 int total = 0, curnum = 0;
201 total = g_slist_length(data->msglist);
203 bogo_forked = g_spawn_async_with_pipes(
204 NULL, data->bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
205 NULL, NULL, &bogo_pid, &bogo_stdin,
206 &bogo_stdout, NULL, &error);
208 if (bogo_forked == FALSE) {
209 g_warning("%s\n", error ? error->message:"ERROR???");
215 if (config.whitelist_ab) {
216 gchar *ab_folderpath;
218 if (*config.whitelist_ab_folder == '\0' ||
219 strcasecmp(config.whitelist_ab_folder, "Any") == 0) {
220 /* match the whole addressbook */
221 ab_folderpath = NULL;
223 /* match the specific book/folder of the addressbook */
224 ab_folderpath = config.whitelist_ab_folder;
227 start_address_completion(ab_folderpath);
230 for (cur = data->msglist; cur; cur = cur->next) {
231 gboolean whitelisted = FALSE;
232 msginfo = (MsgInfo *)cur->data;
233 debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
235 if (message_callback != NULL)
236 message_callback(NULL, total, curnum++, data->in_thread);
238 if (config.whitelist_ab && msginfo->from &&
239 found_in_addressbook(msginfo->from))
242 /* can set flags (SCANNED, ATTACHMENT) but that's ok
243 * as GUI updates are hooked not direct */
245 file = procmsg_get_message_file(msginfo);
248 gchar *tmp = g_strdup_printf("%s\n",file);
249 /* send filename to bogofilter */
250 write_all(bogo_stdin, tmp, strlen(tmp));
252 memset(buf, 0, sizeof(buf));
254 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
255 g_warning("bogofilter short read\n");
256 debug_print("message %d is ham\n", msginfo->msgnum);
257 data->mail_filtering_data->unfiltered = g_slist_prepend(
258 data->mail_filtering_data->unfiltered, msginfo);
259 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
261 gchar **parts = NULL;
262 if (strchr(buf, '/')) {
263 tmp = strrchr(buf, '/')+1;
267 parts = g_strsplit(tmp, " ", 0);
268 debug_print("read %s\n", buf);
270 /* note the result if the header if needed */
271 if (parts && parts[0] && parts[1] && parts[2] &&
272 FOLDER_TYPE(msginfo->folder->folder) == F_MH &&
273 config.insert_header) {
274 gchar *tmpfile = get_tmp_file();
275 FILE *input = g_fopen(file, "r");
276 FILE *output = g_fopen(tmpfile, "w");
277 if (strstr(parts[2], "\n"))
278 *(strstr(parts[2], "\n")) = '\0';
279 if (input && !output)
281 else if (!input && output)
284 gchar tmpbuf[BUFFSIZE];
285 gboolean err = FALSE;
286 const gchar *bogosity = *parts[1] == 'S' ? "Spam":
287 (*parts[1] == 'H' ? "Ham":"Unsure");
288 gchar *tmpstr = g_strdup_printf(
289 "X-Claws-Bogosity: %s, spamicity=%s%s\n",
291 whitelisted?" [whitelisted]":"");
292 if (fwrite(tmpstr, 1, strlen(tmpstr), output) < strlen(tmpstr)) {
295 while (fgets(tmpbuf, sizeof(buf), input)) {
296 if (fputs(tmpbuf, output) == EOF) {
303 if (fclose(output) == EOF)
306 move_file(tmpfile, file, TRUE);
313 if (!whitelisted && parts && parts[0] && parts[1] && *parts[1] == 'S') {
315 debug_print("message %d is spam\n", msginfo->msgnum);
316 /* Spam will be filtered away */
317 data->mail_filtering_data->filtered = g_slist_prepend(
318 data->mail_filtering_data->filtered, msginfo);
319 data->new_spams = g_slist_prepend(data->new_spams, msginfo);
321 } else if (whitelisted && parts && parts[0] && parts[1] &&
322 (*parts[1] == 'S' || *parts[1] == 'U')) {
324 debug_print("message %d is whitelisted %s\n", msginfo->msgnum,
325 *parts[1] == 'S' ? "spam":"unsure");
326 /* Whitelisted spam will *not* be filtered away, but continue
327 * their trip through filtering as if it was ham. */
328 data->mail_filtering_data->unfiltered = g_slist_prepend(
329 data->mail_filtering_data->unfiltered, msginfo);
330 /* But it gets put in a different list, so that we
331 * can still flag it and inform the user that it is
332 * considered a spam (so that he can teach bogo that
334 data->whitelisted_new_spams = g_slist_prepend(data->whitelisted_new_spams, msginfo);
336 } else if (config.save_unsure && parts && parts[0] && parts[1] && *parts[1] == 'U') {
338 debug_print("message %d is unsure\n", msginfo->msgnum);
339 /* Spam will be filtered away */
340 data->mail_filtering_data->filtered = g_slist_prepend(
341 data->mail_filtering_data->filtered, msginfo);
342 data->new_unsure = g_slist_prepend(data->new_unsure, msginfo);
346 debug_print("message %d is ham\n", msginfo->msgnum);
347 data->mail_filtering_data->unfiltered = g_slist_prepend(
348 data->mail_filtering_data->unfiltered, msginfo);
349 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
356 data->mail_filtering_data->unfiltered = g_slist_prepend(
357 data->mail_filtering_data->unfiltered, msginfo);
358 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
361 if (config.whitelist_ab)
362 end_address_completion();
367 waitpid(bogo_pid, &status, 0);
368 if (!WIFEXITED(status))
371 status = WEXITSTATUS(status);
374 to_filter_data->status = status;
378 static void *bogofilter_filtering_thread(void *data)
380 while (!filter_th_done) {
381 pthread_mutex_lock(&list_mutex);
382 if (to_filter_data == NULL || to_filter_data->done == TRUE) {
383 pthread_mutex_unlock(&list_mutex);
384 debug_print("thread is waiting for something to filter\n");
385 pthread_mutex_lock(&wait_mutex);
386 pthread_cond_wait(&wait_cond, &wait_mutex);
387 pthread_mutex_unlock(&wait_mutex);
389 debug_print("thread awaken with something to filter\n");
390 to_filter_data->done = FALSE;
391 bogofilter_do_filter(to_filter_data);
392 pthread_mutex_unlock(&list_mutex);
393 to_filter_data->done = TRUE;
400 static pthread_t filter_th = 0;
402 static void bogofilter_start_thread(void)
404 filter_th_done = FALSE;
405 if (filter_th != 0 || 1)
407 if (pthread_create(&filter_th, 0,
408 bogofilter_filtering_thread,
413 debug_print("thread created\n");
416 static void bogofilter_stop_thread(void)
419 while (pthread_mutex_trylock(&list_mutex) != 0) {
423 if (filter_th != 0) {
424 filter_th_done = TRUE;
425 debug_print("waking thread up\n");
426 pthread_mutex_lock(&wait_mutex);
427 pthread_cond_broadcast(&wait_cond);
428 pthread_mutex_unlock(&wait_mutex);
429 pthread_join(filter_th, &res);
432 pthread_mutex_unlock(&list_mutex);
433 debug_print("thread done\n");
437 static gboolean mail_filtering_hook(gpointer source, gpointer data)
439 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
440 MsgInfo *msginfo = mail_filtering_data->msginfo;
441 GSList *msglist = mail_filtering_data->msglist;
443 static gboolean warned_error = FALSE;
445 int total = 0, curnum = 0;
446 GSList *new_hams = NULL, *new_spams = NULL;
447 GSList *new_unsure, *whitelisted_new_spams = NULL;
448 gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
450 gboolean ok_to_thread = TRUE;
452 bogo_args[0] = bogo_exec;
457 if (!config.process_emails) {
461 if (msglist == NULL && msginfo != NULL) {
462 g_warning("wrong call to bogofilter mail_filtering_hook");
466 total = g_slist_length(msglist);
468 /* we have to make sure the mails are cached - or it'll break on IMAP */
469 if (message_callback != NULL)
470 message_callback(_("Bogofilter: fetching bodies..."), total, 0, FALSE);
471 for (cur = msglist; cur; cur = cur->next) {
472 gchar *file = procmsg_get_message_file((MsgInfo *)cur->data);
474 ok_to_thread = FALSE;
475 if (message_callback != NULL)
476 message_callback(NULL, total, curnum++, FALSE);
479 if (message_callback != NULL)
480 message_callback(NULL, 0, 0, FALSE);
482 if (message_callback != NULL)
483 message_callback(_("Bogofilter: filtering messages..."), total, 0, FALSE);
486 while (pthread_mutex_trylock(&list_mutex) != 0) {
491 to_filter_data = g_new0(BogoFilterData, 1);
492 to_filter_data->msglist = msglist;
493 to_filter_data->mail_filtering_data = mail_filtering_data;
494 to_filter_data->new_hams = NULL;
495 to_filter_data->new_unsure = NULL;
496 to_filter_data->new_spams = NULL;
497 to_filter_data->whitelisted_new_spams = NULL;
498 to_filter_data->done = FALSE;
499 to_filter_data->status = -1;
500 to_filter_data->bogo_args = bogo_args;
502 to_filter_data->in_thread = (filter_th != 0 && ok_to_thread);
504 to_filter_data->in_thread = FALSE;
508 pthread_mutex_unlock(&list_mutex);
510 if (filter_th != 0 && ok_to_thread) {
511 debug_print("waking thread to let it filter things\n");
512 pthread_mutex_lock(&wait_mutex);
513 pthread_cond_broadcast(&wait_cond);
514 pthread_mutex_unlock(&wait_mutex);
516 while (!to_filter_data->done) {
522 while (pthread_mutex_trylock(&list_mutex) != 0) {
527 if (filter_th == 0 || !ok_to_thread)
528 bogofilter_do_filter(to_filter_data);
530 bogofilter_do_filter(to_filter_data);
533 new_hams = to_filter_data->new_hams;
534 new_unsure = to_filter_data->new_unsure;
535 new_spams = to_filter_data->new_spams;
536 whitelisted_new_spams = to_filter_data->whitelisted_new_spams;
537 status = to_filter_data->status;
538 g_free(to_filter_data);
539 to_filter_data = NULL;
541 pthread_mutex_unlock(&list_mutex);
546 for (cur = new_hams; cur; cur = cur->next) {
547 MsgInfo *msginfo = (MsgInfo *)cur->data;
548 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
549 debug_print("unflagging ham: %d\n", msginfo->msgnum);
552 for (cur = new_unsure; cur; cur = cur->next) {
553 MsgInfo *msginfo = (MsgInfo *)cur->data;
554 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
555 debug_print("unflagging unsure: %d\n", msginfo->msgnum);
557 if (config.learn_from_whitelist && whitelisted_new_spams) {
558 /* flag whitelisted spams */
559 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
560 MsgInfo *msginfo = (MsgInfo *)cur->data;
561 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
562 debug_print("flagging whitelisted non-ham: %d\n", msginfo->msgnum);
565 bogofilter_learn(NULL, whitelisted_new_spams, FALSE);
568 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
569 MsgInfo *msginfo = (MsgInfo *)cur->data;
570 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
571 debug_print("unflagging whitelisted non-ham: %d\n", msginfo->msgnum);
574 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
575 MsgInfo *msginfo = (MsgInfo *)cur->data;
576 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
577 debug_print("not flagging whitelisted non-ham: %d\n", msginfo->msgnum);
581 /* flag spams and delete them if !config.receive_spam
582 * (if config.receive_spam is set, we'll move them later) */
583 for (cur = new_spams; cur; cur = cur->next) {
584 MsgInfo *msginfo = (MsgInfo *)cur->data;
585 if (config.receive_spam) {
586 if (config.mark_as_read)
587 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
588 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
590 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
594 if (status < 0 || status > 2) { /* I/O or other errors */
598 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
599 "a message. The probable cause of the "
600 "error is that it didn't learn from any mail.\n"
601 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
602 "ham\" to train Bogofilter with a few hundred "
603 "spam and ham messages."));
605 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
606 "a message. The command `%s %s %s` couldn't be run."),
607 bogo_args[0], bogo_args[1], bogo_args[2]);
608 if (!prefs_common.no_recv_err_panel) {
610 alertpanel_error("%s", msg);
614 log_error(LOG_PROTOCOL, "%s\n", msg);
618 if (status < 0 || status > 2) {
619 g_slist_free(mail_filtering_data->filtered);
620 g_slist_free(mail_filtering_data->unfiltered);
621 mail_filtering_data->filtered = NULL;
622 mail_filtering_data->unfiltered = NULL;
624 if (config.receive_spam && new_spams) {
625 FolderItem *save_folder = NULL;
627 if ((!config.save_folder) ||
628 (config.save_folder[0] == '\0') ||
629 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
630 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
631 save_folder = folder_find_item_from_identifier(
632 mail_filtering_data->account->trash_folder);
634 debug_print("found trash folder from account's advanced settings\n");
636 if (save_folder == NULL && mail_filtering_data->account &&
637 mail_filtering_data->account->folder) {
638 save_folder = mail_filtering_data->account->folder->trash;
640 debug_print("found trash folder from account's trash\n");
642 if (save_folder == NULL && mail_filtering_data->account &&
643 !mail_filtering_data->account->folder) {
644 if (mail_filtering_data->account->inbox) {
645 FolderItem *item = folder_find_item_from_identifier(
646 mail_filtering_data->account->inbox);
647 if (item && item->folder->trash) {
648 save_folder = item->folder->trash;
649 debug_print("found trash folder from account's inbox\n");
652 if (!save_folder && mail_filtering_data->account->local_inbox) {
653 FolderItem *item = folder_find_item_from_identifier(
654 mail_filtering_data->account->local_inbox);
655 if (item && item->folder->trash) {
656 save_folder = item->folder->trash;
657 debug_print("found trash folder from account's local_inbox\n");
661 if (save_folder == NULL) {
662 debug_print("using default trash folder\n");
663 save_folder = folder_get_default_trash();
667 for (cur = new_spams; cur; cur = cur->next) {
668 msginfo = (MsgInfo *)cur->data;
669 msginfo->filter_op = IS_MOVE;
670 msginfo->to_filter_folder = save_folder;
674 if (config.save_unsure && new_unsure) {
675 FolderItem *save_unsure_folder = NULL;
677 if ((!config.save_unsure_folder) ||
678 (config.save_unsure_folder[0] == '\0') ||
679 ((save_unsure_folder = folder_find_item_from_identifier(config.save_unsure_folder)) == NULL)) {
680 if (mail_filtering_data->account)
681 save_unsure_folder = folder_find_item_from_identifier(
682 mail_filtering_data->account->inbox);
683 if (save_unsure_folder == NULL && mail_filtering_data->account &&
684 mail_filtering_data->account->folder)
685 save_unsure_folder = mail_filtering_data->account->folder->inbox;
686 if (save_unsure_folder == NULL && mail_filtering_data->account &&
687 !mail_filtering_data->account->folder) {
688 if (mail_filtering_data->account->inbox) {
689 FolderItem *item = folder_find_item_from_identifier(
690 mail_filtering_data->account->inbox);
692 save_unsure_folder = item;
695 if (!save_unsure_folder && mail_filtering_data->account->local_inbox) {
696 FolderItem *item = folder_find_item_from_identifier(
697 mail_filtering_data->account->local_inbox);
699 save_unsure_folder = item;
703 if (save_unsure_folder == NULL)
704 save_unsure_folder = folder_get_default_inbox();
706 if (save_unsure_folder) {
707 for (cur = new_unsure; cur; cur = cur->next) {
708 msginfo = (MsgInfo *)cur->data;
709 msginfo->filter_op = IS_MOVE;
710 msginfo->to_filter_folder = save_unsure_folder;
715 g_slist_free(new_hams);
716 g_slist_free(new_unsure);
717 g_slist_free(new_spams);
718 g_slist_free(whitelisted_new_spams);
720 if (message_callback != NULL)
721 message_callback(NULL, 0, 0, FALSE);
722 mail_filtering_data->filtered = g_slist_reverse(
723 mail_filtering_data->filtered);
724 mail_filtering_data->unfiltered = g_slist_reverse(
725 mail_filtering_data->unfiltered);
730 BogofilterConfig *bogofilter_get_config(void)
735 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
739 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
741 if (msginfo == NULL && msglist == NULL) {
746 file = procmsg_get_message_file(msginfo);
750 if (message_callback != NULL)
751 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE);
754 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
755 else if (MSG_IS_SPAM(msginfo->flags))
756 /* correct bogofilter, this wasn't spam */
757 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
760 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
762 debug_print("%s\n", cmd);
763 if ((status = execute_command_line(cmd, FALSE)) != 0)
764 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
768 if (message_callback != NULL)
769 message_callback(NULL, 0, 0, FALSE);
774 GSList *cur = msglist;
776 int total = g_slist_length(msglist);
778 gboolean some_correction = FALSE, some_no_correction = FALSE;
780 if (message_callback != NULL)
781 message_callback(_("Bogofilter: learning from messages..."), total, 0, FALSE);
783 for (cur = msglist; cur && status == 0; cur = cur->next) {
784 info = (MsgInfo *)cur->data;
786 some_no_correction = TRUE;
787 else if (MSG_IS_SPAM(info->flags))
788 /* correct bogofilter, this wasn't spam */
789 some_correction = TRUE;
791 some_no_correction = TRUE;
795 if (some_correction && some_no_correction) {
796 /* we potentially have to do different stuff for every mail */
797 for (cur = msglist; cur && status == 0; cur = cur->next) {
798 info = (MsgInfo *)cur->data;
799 file = procmsg_get_message_file(info);
803 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
804 else if (MSG_IS_SPAM(info->flags))
805 /* correct bogofilter, this wasn't spam */
806 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
809 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
811 debug_print("%s\n", cmd);
812 if ((status = execute_command_line(cmd, FALSE)) != 0)
813 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
819 if (message_callback != NULL)
820 message_callback(NULL, total, done, FALSE);
822 } else if (some_correction || some_no_correction) {
828 GError *error = NULL;
829 gboolean bogo_forked;
831 bogo_args[0] = (gchar *)bogo_exec;
832 if (some_correction && !some_no_correction)
833 bogo_args[1] = "-Sn";
834 else if (some_no_correction && !some_correction)
835 bogo_args[1] = spam ? "-s":"-n";
838 debug_print("|%s %s %s ...\n", bogo_args[0], bogo_args[1], bogo_args[2]);
839 bogo_forked = g_spawn_async_with_pipes(
840 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
841 NULL, NULL, &bogo_pid, &bogo_stdin,
844 while (bogo_forked && cur) {
846 info = (MsgInfo *)cur->data;
847 file = procmsg_get_message_file(info);
849 tmp = g_strdup_printf("%s\n",
851 write_all(bogo_stdin, tmp, strlen(tmp));
856 if (message_callback != NULL)
857 message_callback(NULL, total, done, FALSE);
862 waitpid(bogo_pid, &status, 0);
863 if (!WIFEXITED(status))
866 status = WEXITSTATUS(status);
868 if (!bogo_forked || status != 0) {
869 log_error(LOG_PROTOCOL, _("Learning failed; `%s %s %s` returned with error:\n%s"),
870 bogo_args[0], bogo_args[1], bogo_args[2],
871 error ? error->message:_("Unknown error"));
878 if (message_callback != NULL)
879 message_callback(NULL, 0, 0, FALSE);
885 void bogofilter_save_config(void)
890 debug_print("Saving Bogofilter Page\n");
892 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
893 pfile = prefs_write_open(rcpath);
895 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
898 if (prefs_write_param(param, pfile->fp) < 0) {
899 g_warning("Failed to write Bogofilter configuration to file\n");
900 prefs_file_close_revert(pfile);
903 if (fprintf(pfile->fp, "\n") < 0) {
904 FILE_OP_ERROR(rcpath, "fprintf");
905 prefs_file_close_revert(pfile);
907 prefs_file_close(pfile);
910 void bogofilter_set_message_callback(MessageCallback callback)
912 message_callback = callback;
915 gint plugin_init(gchar **error)
921 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
922 VERSION_NUMERIC, PLUGIN_NAME, error))
925 prefs_set_default(param);
926 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
927 prefs_read_config(param, "Bogofilter", rcpath, NULL);
930 bogofilter_gtk_init();
932 debug_print("Bogofilter plugin loaded\n");
935 bogofilter_start_thread();
938 if (config.process_emails) {
939 bogofilter_register_hook();
942 procmsg_register_spam_learner(bogofilter_learn);
943 procmsg_spam_set_folder(config.save_folder, bogofilter_get_spam_folder);
949 FolderItem *bogofilter_get_spam_folder(MsgInfo *msginfo)
951 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
953 if (item || msginfo == NULL || msginfo->folder == NULL)
956 if (msginfo->folder->folder &&
957 msginfo->folder->folder->account &&
958 msginfo->folder->folder->account->set_trash_folder) {
959 item = folder_find_item_from_identifier(
960 msginfo->folder->folder->account->trash_folder);
964 msginfo->folder->folder &&
965 msginfo->folder->folder->trash)
966 item = msginfo->folder->folder->trash;
969 item = folder_get_default_trash();
971 debug_print("bogo spam dir: %s\n", folder_item_get_path(item));
975 gboolean plugin_done(void)
978 bogofilter_unregister_hook();
981 bogofilter_stop_thread();
983 g_free(config.save_folder);
984 bogofilter_gtk_done();
985 procmsg_unregister_spam_learner(bogofilter_learn);
986 procmsg_spam_set_folder(NULL, NULL);
987 debug_print("Bogofilter plugin unloaded\n");
991 const gchar *plugin_name(void)
996 const gchar *plugin_desc(void)
998 return _("This plugin can check all messages that are received from an "
999 "IMAP, LOCAL or POP account for spam using Bogofilter. "
1000 "You will need Bogofilter installed locally.\n"
1002 "Before Bogofilter can recognize spam messages, you have to "
1003 "train it by marking a few hundred spam and ham messages "
1004 "with the use of \"/Mark/Mark as spam\" and \"/Mark/Mark as "
1007 "When a message is identified as spam it can be deleted or "
1008 "saved in a specially designated folder.\n"
1010 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
1013 const gchar *plugin_type(void)
1018 const gchar *plugin_licence(void)
1023 const gchar *plugin_version(void)
1028 struct PluginFeature *plugin_provides(void)
1030 static struct PluginFeature features[] =
1031 { {PLUGIN_FILTERING, N_("Spam detection")},
1032 {PLUGIN_FILTERING, N_("Spam learning")},
1033 {PLUGIN_NOTHING, NULL}};
1037 void bogofilter_register_hook(void)
1040 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
1041 if (hook_id == -1) {
1042 g_warning("Failed to register mail filtering hook");
1043 config.process_emails = FALSE;
1047 void bogofilter_unregister_hook(void)
1049 if (hook_id != -1) {
1050 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);