2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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 {"mark_as_read", "TRUE", &config.mark_as_read, P_BOOL,
108 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
112 * Helper function for spawn_with_input() - write an entire
122 gssize count = write (fd, buf, to_write);
138 typedef struct _BogoFilterData {
139 MailFilteringData *mail_filtering_data;
145 GSList *whitelisted_new_spams;
151 static BogoFilterData *to_filter_data = NULL;
153 static gboolean filter_th_done = FALSE;
154 static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
155 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
156 static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
159 static gboolean found_in_addressbook(const gchar *address)
162 gboolean found = FALSE;
168 addr = g_strdup(address);
169 extract_address(addr);
170 num_addr = complete_address(addr);
172 /* skip first item (this is the search string itself) */
174 for (; i < num_addr && !found; i++) {
175 gchar *caddr = get_complete_address(i);
176 extract_address(caddr);
177 if (strcasecmp(caddr, addr) == 0)
186 static void bogofilter_do_filter(BogoFilterData *data)
189 gint bogo_stdin, bogo_stdout;
190 GError *error = NULL;
191 gboolean bogo_forked;
195 int total = 0, curnum = 0;
199 total = g_slist_length(data->msglist);
201 bogo_forked = g_spawn_async_with_pipes(
202 NULL, data->bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
203 NULL, NULL, &bogo_pid, &bogo_stdin,
204 &bogo_stdout, NULL, &error);
206 if (bogo_forked == FALSE) {
207 g_warning("%s\n", error ? error->message:"ERROR???");
213 if (config.whitelist_ab) {
214 gchar *ab_folderpath;
216 if (*config.whitelist_ab_folder == '\0' ||
217 strcasecmp(config.whitelist_ab_folder, _("Any")) == 0) {
218 /* match the whole addressbook */
219 ab_folderpath = NULL;
221 /* match the specific book/folder of the addressbook */
222 ab_folderpath = config.whitelist_ab_folder;
225 start_address_completion(ab_folderpath);
228 for (cur = data->msglist; cur; cur = cur->next) {
229 gboolean whitelisted = FALSE;
230 msginfo = (MsgInfo *)cur->data;
231 debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
233 if (message_callback != NULL)
234 message_callback(NULL, total, curnum++, data->in_thread);
236 if (config.whitelist_ab && msginfo->from &&
237 found_in_addressbook(msginfo->from))
240 /* can set flags (SCANNED, ATTACHMENT) but that's ok
241 * as GUI updates are hooked not direct */
243 file = procmsg_get_message_file(msginfo);
246 gchar *tmp = g_strdup_printf("%s\n",file);
247 /* send filename to bogofilter */
248 write_all(bogo_stdin, tmp, strlen(tmp));
250 memset(buf, 0, sizeof(buf));
252 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
253 g_warning("bogofilter short read\n");
254 debug_print("message %d is ham\n", msginfo->msgnum);
255 data->mail_filtering_data->unfiltered = g_slist_prepend(
256 data->mail_filtering_data->unfiltered, msginfo);
257 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
259 gchar **parts = NULL;
260 if (strchr(buf, '/')) {
261 tmp = strrchr(buf, '/')+1;
265 parts = g_strsplit(tmp, " ", 0);
266 debug_print("read %s\n", buf);
268 /* note the result if the header if needed */
269 if (parts && parts[0] && parts[1] && parts[2] &&
270 FOLDER_TYPE(msginfo->folder->folder) == F_MH &&
271 config.insert_header) {
272 gchar *tmpfile = get_tmp_file();
273 FILE *input = fopen(file, "r");
274 FILE *output = fopen(tmpfile, "w");
275 if (strstr(parts[2], "\n"))
276 *(strstr(parts[2], "\n")) = '\0';
277 if (input && !output)
279 else if (!input && output)
282 gchar tmpbuf[BUFFSIZE];
283 gboolean err = FALSE;
284 const gchar *bogosity = *parts[1] == 'S' ? "Spam":
285 (*parts[1] == 'H' ? "Ham":"Unsure");
286 gchar *tmpstr = g_strdup_printf(
287 "X-Claws-Bogosity: %s, spamicity=%s%s\n",
289 whitelisted?" [whitelisted]":"");
290 if (fwrite(tmpstr, 1, strlen(tmpstr), output) < strlen(tmpstr)) {
293 while (fgets(tmpbuf, sizeof(buf), input)) {
294 if (fputs(tmpbuf, output) == EOF) {
301 if (fclose(output) == EOF)
304 move_file(tmpfile, file, TRUE);
311 if (!whitelisted && parts && parts[0] && parts[1] && *parts[1] == 'S') {
313 debug_print("message %d is spam\n", msginfo->msgnum);
314 /* Spam will be filtered away */
315 data->mail_filtering_data->filtered = g_slist_prepend(
316 data->mail_filtering_data->filtered, msginfo);
317 data->new_spams = g_slist_prepend(data->new_spams, msginfo);
319 } else if (whitelisted && parts && parts[0] && parts[1] && *parts[1] == 'S') {
321 debug_print("message %d is whitelisted spam\n", msginfo->msgnum);
322 /* Whitelisted spam will *not* be filtered away, but continue
323 * their trip through filtering as if it was ham. */
324 data->mail_filtering_data->unfiltered = g_slist_prepend(
325 data->mail_filtering_data->unfiltered, msginfo);
326 /* But it gets put in a different list, so that we
327 * can still flag it and inform the user that it is
328 * considered a spam (so that he can teach bogo that
330 data->whitelisted_new_spams = g_slist_prepend(data->whitelisted_new_spams, msginfo);
332 } else if (config.save_unsure && parts && parts[0] && parts[1] && *parts[1] == 'U') {
334 debug_print("message %d is unsure\n", msginfo->msgnum);
335 /* Spam will be filtered away */
336 data->mail_filtering_data->filtered = g_slist_prepend(
337 data->mail_filtering_data->filtered, msginfo);
338 data->new_unsure = g_slist_prepend(data->new_unsure, msginfo);
342 debug_print("message %d is ham\n", msginfo->msgnum);
343 data->mail_filtering_data->unfiltered = g_slist_prepend(
344 data->mail_filtering_data->unfiltered, msginfo);
345 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
352 data->mail_filtering_data->unfiltered = g_slist_prepend(
353 data->mail_filtering_data->unfiltered, msginfo);
354 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
357 if (config.whitelist_ab)
358 end_address_completion();
363 waitpid(bogo_pid, &status, 0);
364 if (!WIFEXITED(status))
367 status = WEXITSTATUS(status);
370 to_filter_data->status = status;
374 static void *bogofilter_filtering_thread(void *data)
376 while (!filter_th_done) {
377 pthread_mutex_lock(&list_mutex);
378 if (to_filter_data == NULL || to_filter_data->done == TRUE) {
379 pthread_mutex_unlock(&list_mutex);
380 debug_print("thread is waiting for something to filter\n");
381 pthread_mutex_lock(&wait_mutex);
382 pthread_cond_wait(&wait_cond, &wait_mutex);
383 pthread_mutex_unlock(&wait_mutex);
385 debug_print("thread awaken with something to filter\n");
386 to_filter_data->done = FALSE;
387 bogofilter_do_filter(to_filter_data);
388 pthread_mutex_unlock(&list_mutex);
389 to_filter_data->done = TRUE;
396 static pthread_t filter_th = 0;
398 static void bogofilter_start_thread(void)
400 filter_th_done = FALSE;
401 if (filter_th != 0 || 1)
403 if (pthread_create(&filter_th, 0,
404 bogofilter_filtering_thread,
409 debug_print("thread created\n");
412 static void bogofilter_stop_thread(void)
415 while (pthread_mutex_trylock(&list_mutex) != 0) {
419 if (filter_th != 0) {
420 filter_th_done = TRUE;
421 debug_print("waking thread up\n");
422 pthread_mutex_lock(&wait_mutex);
423 pthread_cond_broadcast(&wait_cond);
424 pthread_mutex_unlock(&wait_mutex);
425 pthread_join(filter_th, &res);
428 pthread_mutex_unlock(&list_mutex);
429 debug_print("thread done\n");
433 static gboolean mail_filtering_hook(gpointer source, gpointer data)
435 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
436 MsgInfo *msginfo = mail_filtering_data->msginfo;
437 GSList *msglist = mail_filtering_data->msglist;
439 static gboolean warned_error = FALSE;
441 int total = 0, curnum = 0;
442 GSList *new_hams = NULL, *new_spams = NULL;
443 GSList *new_unsure, *whitelisted_new_spams = NULL;
444 gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
446 gboolean ok_to_thread = TRUE;
448 bogo_args[0] = bogo_exec;
453 if (!config.process_emails) {
457 if (msglist == NULL && msginfo != NULL) {
458 g_warning("wrong call to bogofilter mail_filtering_hook");
462 total = g_slist_length(msglist);
464 /* we have to make sure the mails are cached - or it'll break on IMAP */
465 if (message_callback != NULL)
466 message_callback(_("Bogofilter: fetching bodies..."), total, 0, FALSE);
467 for (cur = msglist; cur; cur = cur->next) {
468 gchar *file = procmsg_get_message_file((MsgInfo *)cur->data);
470 ok_to_thread = FALSE;
471 if (message_callback != NULL)
472 message_callback(NULL, total, curnum++, FALSE);
475 if (message_callback != NULL)
476 message_callback(NULL, 0, 0, FALSE);
478 if (message_callback != NULL)
479 message_callback(_("Bogofilter: filtering messages..."), total, 0, FALSE);
482 while (pthread_mutex_trylock(&list_mutex) != 0) {
487 to_filter_data = g_new0(BogoFilterData, 1);
488 to_filter_data->msglist = msglist;
489 to_filter_data->mail_filtering_data = mail_filtering_data;
490 to_filter_data->new_hams = NULL;
491 to_filter_data->new_unsure = NULL;
492 to_filter_data->new_spams = NULL;
493 to_filter_data->whitelisted_new_spams = NULL;
494 to_filter_data->done = FALSE;
495 to_filter_data->status = -1;
496 to_filter_data->bogo_args = bogo_args;
498 to_filter_data->in_thread = (filter_th != 0 && ok_to_thread);
500 to_filter_data->in_thread = FALSE;
504 pthread_mutex_unlock(&list_mutex);
506 if (filter_th != 0 && ok_to_thread) {
507 debug_print("waking thread to let it filter things\n");
508 pthread_mutex_lock(&wait_mutex);
509 pthread_cond_broadcast(&wait_cond);
510 pthread_mutex_unlock(&wait_mutex);
512 while (!to_filter_data->done) {
518 while (pthread_mutex_trylock(&list_mutex) != 0) {
523 if (filter_th == 0 || !ok_to_thread)
524 bogofilter_do_filter(to_filter_data);
526 bogofilter_do_filter(to_filter_data);
529 new_hams = to_filter_data->new_hams;
530 new_unsure = to_filter_data->new_unsure;
531 new_spams = to_filter_data->new_spams;
532 whitelisted_new_spams = to_filter_data->whitelisted_new_spams;
533 status = to_filter_data->status;
534 g_free(to_filter_data);
535 to_filter_data = NULL;
537 pthread_mutex_unlock(&list_mutex);
542 for (cur = new_hams; cur; cur = cur->next) {
543 MsgInfo *msginfo = (MsgInfo *)cur->data;
544 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
547 for (cur = new_unsure; cur; cur = cur->next) {
548 MsgInfo *msginfo = (MsgInfo *)cur->data;
549 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
551 /* flag whitelisted spams */
552 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
553 MsgInfo *msginfo = (MsgInfo *)cur->data;
554 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
556 /* flag spams and delete them if !config.receive_spam
557 * (if config.receive_spam is set, we'll move them later) */
558 for (cur = new_spams; cur; cur = cur->next) {
559 MsgInfo *msginfo = (MsgInfo *)cur->data;
560 if (config.receive_spam) {
561 if (config.mark_as_read)
562 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
563 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
565 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
569 if (status < 0 || status > 2) { /* I/O or other errors */
573 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
574 "a message. The probable cause of the "
575 "error is that it didn't learn from any mail.\n"
576 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
577 "ham\" to train Bogofilter with a few hundred "
578 "spam and ham messages."));
580 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
581 "a message. The command `%s %s %s` couldn't be run."),
582 bogo_args[0], bogo_args[1], bogo_args[2]);
583 if (!prefs_common.no_recv_err_panel) {
585 alertpanel_error("%s", msg);
589 log_error(LOG_PROTOCOL, "%s\n", msg);
593 if (status < 0 || status > 2) {
594 g_slist_free(mail_filtering_data->filtered);
595 g_slist_free(mail_filtering_data->unfiltered);
596 mail_filtering_data->filtered = NULL;
597 mail_filtering_data->unfiltered = NULL;
599 if (config.receive_spam && new_spams) {
600 FolderItem *save_folder = NULL;
602 if ((!config.save_folder) ||
603 (config.save_folder[0] == '\0') ||
604 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
605 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
606 save_folder = folder_find_item_from_identifier(
607 mail_filtering_data->account->trash_folder);
609 debug_print("found trash folder from account's advanced settings\n");
611 if (save_folder == NULL && mail_filtering_data->account &&
612 mail_filtering_data->account->folder) {
613 save_folder = mail_filtering_data->account->folder->trash;
615 debug_print("found trash folder from account's trash\n");
617 if (save_folder == NULL && mail_filtering_data->account &&
618 !mail_filtering_data->account->folder) {
619 if (mail_filtering_data->account->inbox) {
620 FolderItem *item = folder_find_item_from_identifier(
621 mail_filtering_data->account->inbox);
622 if (item && item->folder->trash) {
623 save_folder = item->folder->trash;
624 debug_print("found trash folder from account's inbox\n");
627 if (!save_folder && mail_filtering_data->account->local_inbox) {
628 FolderItem *item = folder_find_item_from_identifier(
629 mail_filtering_data->account->local_inbox);
630 if (item && item->folder->trash) {
631 save_folder = item->folder->trash;
632 debug_print("found trash folder from account's local_inbox\n");
636 if (save_folder == NULL) {
637 debug_print("using default trash folder\n");
638 save_folder = folder_get_default_trash();
642 for (cur = new_spams; cur; cur = cur->next) {
643 msginfo = (MsgInfo *)cur->data;
644 msginfo->is_move = TRUE;
645 msginfo->to_filter_folder = save_folder;
649 if (config.save_unsure && new_unsure) {
650 FolderItem *save_unsure_folder = NULL;
652 if ((!config.save_unsure_folder) ||
653 (config.save_unsure_folder[0] == '\0') ||
654 ((save_unsure_folder = folder_find_item_from_identifier(config.save_unsure_folder)) == NULL)) {
655 if (mail_filtering_data->account)
656 save_unsure_folder = folder_find_item_from_identifier(
657 mail_filtering_data->account->inbox);
658 if (save_unsure_folder == NULL && mail_filtering_data->account &&
659 mail_filtering_data->account->folder)
660 save_unsure_folder = mail_filtering_data->account->folder->inbox;
661 if (save_unsure_folder == NULL && mail_filtering_data->account &&
662 !mail_filtering_data->account->folder) {
663 if (mail_filtering_data->account->inbox) {
664 FolderItem *item = folder_find_item_from_identifier(
665 mail_filtering_data->account->inbox);
667 save_unsure_folder = item;
670 if (!save_unsure_folder && mail_filtering_data->account->local_inbox) {
671 FolderItem *item = folder_find_item_from_identifier(
672 mail_filtering_data->account->local_inbox);
674 save_unsure_folder = item;
678 if (save_unsure_folder == NULL)
679 save_unsure_folder = folder_get_default_inbox();
681 if (save_unsure_folder) {
682 for (cur = new_unsure; cur; cur = cur->next) {
683 msginfo = (MsgInfo *)cur->data;
684 msginfo->is_move = TRUE;
685 msginfo->to_filter_folder = save_unsure_folder;
690 g_slist_free(new_hams);
691 g_slist_free(new_unsure);
692 g_slist_free(new_spams);
693 g_slist_free(whitelisted_new_spams);
695 if (message_callback != NULL)
696 message_callback(NULL, 0, 0, FALSE);
697 mail_filtering_data->filtered = g_slist_reverse(
698 mail_filtering_data->filtered);
699 mail_filtering_data->unfiltered = g_slist_reverse(
700 mail_filtering_data->unfiltered);
705 BogofilterConfig *bogofilter_get_config(void)
710 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
714 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
716 if (msginfo == NULL && msglist == NULL) {
721 file = procmsg_get_message_file(msginfo);
725 if (message_callback != NULL)
726 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE);
729 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
730 else if (MSG_IS_SPAM(msginfo->flags))
731 /* correct bogofilter, this wasn't spam */
732 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
735 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
737 debug_print("%s\n", cmd);
738 if ((status = execute_command_line(cmd, FALSE)) != 0)
739 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
743 if (message_callback != NULL)
744 message_callback(NULL, 0, 0, FALSE);
749 GSList *cur = msglist;
751 int total = g_slist_length(msglist);
753 gboolean some_correction = FALSE, some_no_correction = FALSE;
755 if (message_callback != NULL)
756 message_callback(_("Bogofilter: learning from messages..."), total, 0, FALSE);
758 for (cur = msglist; cur && status == 0; cur = cur->next) {
759 info = (MsgInfo *)cur->data;
761 some_no_correction = TRUE;
762 else if (MSG_IS_SPAM(info->flags))
763 /* correct bogofilter, this wasn't spam */
764 some_correction = TRUE;
766 some_no_correction = TRUE;
770 if (some_correction && some_no_correction) {
771 /* we potentially have to do different stuff for every mail */
772 for (cur = msglist; cur && status == 0; cur = cur->next) {
773 info = (MsgInfo *)cur->data;
774 file = procmsg_get_message_file(info);
778 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
779 else if (MSG_IS_SPAM(info->flags))
780 /* correct bogofilter, this wasn't spam */
781 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
784 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
786 debug_print("%s\n", cmd);
787 if ((status = execute_command_line(cmd, FALSE)) != 0)
788 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
794 if (message_callback != NULL)
795 message_callback(NULL, total, done, FALSE);
797 } else if (some_correction || some_no_correction) {
803 GError *error = NULL;
804 gboolean bogo_forked;
806 bogo_args[0] = (gchar *)bogo_exec;
807 if (some_correction && !some_no_correction)
808 bogo_args[1] = "-Sn";
809 else if (some_no_correction && !some_correction)
810 bogo_args[1] = spam ? "-s":"-n";
813 debug_print("|%s %s %s ...\n", bogo_args[0], bogo_args[1], bogo_args[2]);
814 bogo_forked = g_spawn_async_with_pipes(
815 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
816 NULL, NULL, &bogo_pid, &bogo_stdin,
819 while (bogo_forked && cur) {
821 info = (MsgInfo *)cur->data;
822 file = procmsg_get_message_file(info);
824 tmp = g_strdup_printf("%s\n",
826 write_all(bogo_stdin, tmp, strlen(tmp));
831 if (message_callback != NULL)
832 message_callback(NULL, total, done, FALSE);
837 waitpid(bogo_pid, &status, 0);
838 if (!WIFEXITED(status))
841 status = WEXITSTATUS(status);
843 if (!bogo_forked || status != 0) {
844 log_error(LOG_PROTOCOL, _("Learning failed; `%s %s %s` returned with error:\n%s"),
845 bogo_args[0], bogo_args[1], bogo_args[2],
846 error ? error->message:_("Unknown error"));
853 if (message_callback != NULL)
854 message_callback(NULL, 0, 0, FALSE);
860 void bogofilter_save_config(void)
865 debug_print("Saving Bogofilter Page\n");
867 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
868 pfile = prefs_write_open(rcpath);
870 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
873 if (prefs_write_param(param, pfile->fp) < 0) {
874 g_warning("Failed to write Bogofilter configuration to file\n");
875 prefs_file_close_revert(pfile);
878 if (fprintf(pfile->fp, "\n") < 0) {
879 FILE_OP_ERROR(rcpath, "fprintf");
880 prefs_file_close_revert(pfile);
882 prefs_file_close(pfile);
885 void bogofilter_set_message_callback(MessageCallback callback)
887 message_callback = callback;
890 gint plugin_init(gchar **error)
896 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
897 VERSION_NUMERIC, PLUGIN_NAME, error))
900 prefs_set_default(param);
901 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
902 prefs_read_config(param, "Bogofilter", rcpath, NULL);
905 bogofilter_gtk_init();
907 debug_print("Bogofilter plugin loaded\n");
910 bogofilter_start_thread();
913 if (config.process_emails) {
914 bogofilter_register_hook();
917 procmsg_register_spam_learner(bogofilter_learn);
918 procmsg_spam_set_folder(config.save_folder, bogofilter_get_spam_folder);
924 FolderItem *bogofilter_get_spam_folder(MsgInfo *msginfo)
926 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
928 if (item || msginfo == NULL || msginfo->folder == NULL)
931 if (msginfo->folder->folder &&
932 msginfo->folder->folder->account &&
933 msginfo->folder->folder->account->set_trash_folder) {
934 item = folder_find_item_from_identifier(
935 msginfo->folder->folder->account->trash_folder);
939 msginfo->folder->folder &&
940 msginfo->folder->folder->trash)
941 item = msginfo->folder->folder->trash;
944 item = folder_get_default_trash();
946 debug_print("bogo spam dir: %s\n", folder_item_get_path(item));
950 gboolean plugin_done(void)
953 bogofilter_unregister_hook();
956 bogofilter_stop_thread();
958 g_free(config.save_folder);
959 bogofilter_gtk_done();
960 procmsg_unregister_spam_learner(bogofilter_learn);
961 procmsg_spam_set_folder(NULL, NULL);
962 debug_print("Bogofilter plugin unloaded\n");
966 const gchar *plugin_name(void)
971 const gchar *plugin_desc(void)
973 return _("This plugin can check all messages that are received from an "
974 "IMAP, LOCAL or POP account for spam using Bogofilter. "
975 "You will need Bogofilter installed locally.\n"
977 "Before Bogofilter can recognize spam messages, you have to "
978 "train it by marking a few hundred spam and ham messages "
979 "with the use of \"/Mark/Mark as spam\" and \"/Mark/Mark as "
982 "When a message is identified as spam it can be deleted or "
983 "saved in a specially designated folder.\n"
985 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
988 const gchar *plugin_type(void)
993 const gchar *plugin_licence(void)
998 const gchar *plugin_version(void)
1003 struct PluginFeature *plugin_provides(void)
1005 static struct PluginFeature features[] =
1006 { {PLUGIN_FILTERING, N_("Spam detection")},
1007 {PLUGIN_FILTERING, N_("Spam learning")},
1008 {PLUGIN_NOTHING, NULL}};
1012 void bogofilter_register_hook(void)
1015 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
1016 if (hook_id == -1) {
1017 g_warning("Failed to register mail filtering hook");
1018 config.process_emails = FALSE;
1022 void bogofilter_unregister_hook(void)
1024 if (hook_id != -1) {
1025 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);