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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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", "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 const gchar *bogosity = *parts[1] == 'S' ? "Spam":
284 (*parts[1] == 'H' ? "Ham":"Unsure");
285 gchar *tmpstr = g_strdup_printf(
286 "X-Claws-Bogosity: %s, spamicity=%s%s\n",
288 whitelisted?" [whitelisted]":"");
289 fwrite(tmpstr, 1, strlen(tmpstr), output);
290 while (fgets(tmpbuf, sizeof(buf), input))
291 fputs(tmpbuf, output);
294 move_file(tmpfile, file, TRUE);
301 if (!whitelisted && parts && parts[0] && parts[1] && *parts[1] == 'S') {
303 debug_print("message %d is spam\n", msginfo->msgnum);
304 /* Spam will be filtered away */
305 data->mail_filtering_data->filtered = g_slist_prepend(
306 data->mail_filtering_data->filtered, msginfo);
307 data->new_spams = g_slist_prepend(data->new_spams, msginfo);
309 } else if (whitelisted && parts && parts[0] && parts[1] && *parts[1] == 'S') {
311 debug_print("message %d is whitelisted spam\n", msginfo->msgnum);
312 /* Whitelisted spam will *not* be filtered away, but continue
313 * their trip through filtering as if it was ham. */
314 data->mail_filtering_data->unfiltered = g_slist_prepend(
315 data->mail_filtering_data->unfiltered, msginfo);
316 /* But it gets put in a different list, so that we
317 * can still flag it and inform the user that it is
318 * considered a spam (so that he can teach bogo that
320 data->whitelisted_new_spams = g_slist_prepend(data->whitelisted_new_spams, msginfo);
322 } else if (config.save_unsure && parts && parts[0] && parts[1] && *parts[1] == 'U') {
324 debug_print("message %d is unsure\n", msginfo->msgnum);
325 /* Spam will be filtered away */
326 data->mail_filtering_data->filtered = g_slist_prepend(
327 data->mail_filtering_data->filtered, msginfo);
328 data->new_unsure = g_slist_prepend(data->new_unsure, msginfo);
332 debug_print("message %d is ham\n", msginfo->msgnum);
333 data->mail_filtering_data->unfiltered = g_slist_prepend(
334 data->mail_filtering_data->unfiltered, msginfo);
335 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
342 data->mail_filtering_data->unfiltered = g_slist_prepend(
343 data->mail_filtering_data->unfiltered, msginfo);
344 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
347 if (config.whitelist_ab)
348 end_address_completion();
353 waitpid(bogo_pid, &status, 0);
354 if (!WIFEXITED(status))
357 status = WEXITSTATUS(status);
360 to_filter_data->status = status;
364 static void *bogofilter_filtering_thread(void *data)
366 while (!filter_th_done) {
367 pthread_mutex_lock(&list_mutex);
368 if (to_filter_data == NULL || to_filter_data->done == TRUE) {
369 pthread_mutex_unlock(&list_mutex);
370 debug_print("thread is waiting for something to filter\n");
371 pthread_mutex_lock(&wait_mutex);
372 pthread_cond_wait(&wait_cond, &wait_mutex);
373 pthread_mutex_unlock(&wait_mutex);
375 debug_print("thread awaken with something to filter\n");
376 to_filter_data->done = FALSE;
377 bogofilter_do_filter(to_filter_data);
378 pthread_mutex_unlock(&list_mutex);
379 to_filter_data->done = TRUE;
386 static pthread_t filter_th = 0;
388 static void bogofilter_start_thread(void)
390 filter_th_done = FALSE;
391 if (filter_th != 0 || 1)
393 if (pthread_create(&filter_th, 0,
394 bogofilter_filtering_thread,
399 debug_print("thread created\n");
402 static void bogofilter_stop_thread(void)
405 while (pthread_mutex_trylock(&list_mutex) != 0) {
409 if (filter_th != 0) {
410 filter_th_done = TRUE;
411 debug_print("waking thread up\n");
412 pthread_mutex_lock(&wait_mutex);
413 pthread_cond_broadcast(&wait_cond);
414 pthread_mutex_unlock(&wait_mutex);
415 pthread_join(filter_th, &res);
418 pthread_mutex_unlock(&list_mutex);
419 debug_print("thread done\n");
423 static gboolean mail_filtering_hook(gpointer source, gpointer data)
425 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
426 MsgInfo *msginfo = mail_filtering_data->msginfo;
427 GSList *msglist = mail_filtering_data->msglist;
429 static gboolean warned_error = FALSE;
431 int total = 0, curnum = 0;
432 GSList *new_hams = NULL, *new_spams = NULL;
433 GSList *new_unsure, *whitelisted_new_spams = NULL;
434 gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
436 gboolean ok_to_thread = TRUE;
438 bogo_args[0] = bogo_exec;
443 if (!config.process_emails) {
447 if (msglist == NULL && msginfo != NULL) {
448 g_warning("wrong call to bogofilter mail_filtering_hook");
452 total = g_slist_length(msglist);
454 /* we have to make sure the mails are cached - or it'll break on IMAP */
455 if (message_callback != NULL)
456 message_callback(_("Bogofilter: fetching bodies..."), total, 0, FALSE);
457 for (cur = msglist; cur; cur = cur->next) {
458 gchar *file = procmsg_get_message_file((MsgInfo *)cur->data);
460 ok_to_thread = FALSE;
461 if (message_callback != NULL)
462 message_callback(NULL, total, curnum++, FALSE);
465 if (message_callback != NULL)
466 message_callback(NULL, 0, 0, FALSE);
468 if (message_callback != NULL)
469 message_callback(_("Bogofilter: filtering messages..."), total, 0, FALSE);
472 while (pthread_mutex_trylock(&list_mutex) != 0) {
477 to_filter_data = g_new0(BogoFilterData, 1);
478 to_filter_data->msglist = msglist;
479 to_filter_data->mail_filtering_data = mail_filtering_data;
480 to_filter_data->new_hams = NULL;
481 to_filter_data->new_unsure = NULL;
482 to_filter_data->new_spams = NULL;
483 to_filter_data->whitelisted_new_spams = NULL;
484 to_filter_data->done = FALSE;
485 to_filter_data->status = -1;
486 to_filter_data->bogo_args = bogo_args;
488 to_filter_data->in_thread = (filter_th != 0 && ok_to_thread);
490 to_filter_data->in_thread = FALSE;
494 pthread_mutex_unlock(&list_mutex);
496 if (filter_th != 0 && ok_to_thread) {
497 debug_print("waking thread to let it filter things\n");
498 pthread_mutex_lock(&wait_mutex);
499 pthread_cond_broadcast(&wait_cond);
500 pthread_mutex_unlock(&wait_mutex);
502 while (!to_filter_data->done) {
508 while (pthread_mutex_trylock(&list_mutex) != 0) {
513 if (filter_th == 0 || !ok_to_thread)
514 bogofilter_do_filter(to_filter_data);
516 bogofilter_do_filter(to_filter_data);
519 new_hams = to_filter_data->new_hams;
520 new_unsure = to_filter_data->new_unsure;
521 new_spams = to_filter_data->new_spams;
522 whitelisted_new_spams = to_filter_data->whitelisted_new_spams;
523 status = to_filter_data->status;
524 g_free(to_filter_data);
525 to_filter_data = NULL;
527 pthread_mutex_unlock(&list_mutex);
532 for (cur = new_hams; cur; cur = cur->next) {
533 MsgInfo *msginfo = (MsgInfo *)cur->data;
534 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
537 for (cur = new_unsure; cur; cur = cur->next) {
538 MsgInfo *msginfo = (MsgInfo *)cur->data;
539 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
541 /* flag whitelisted spams */
542 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
543 MsgInfo *msginfo = (MsgInfo *)cur->data;
544 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
546 /* flag spams and delete them if !config.receive_spam
547 * (if config.receive_spam is set, we'll move them later) */
548 for (cur = new_spams; cur; cur = cur->next) {
549 MsgInfo *msginfo = (MsgInfo *)cur->data;
550 if (config.receive_spam) {
551 if (config.mark_as_read)
552 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
553 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
555 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
559 if (status < 0 || status > 2) { /* I/O or other errors */
563 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
564 "a message. The probable cause of the "
565 "error is that it didn't learn from any mail.\n"
566 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
567 "ham\" to train Bogofilter with a few hundred "
568 "spam and ham messages."));
570 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
571 "a message. The command `%s %s %s` couldn't be run."),
572 bogo_args[0], bogo_args[1], bogo_args[2]);
573 if (!prefs_common.no_recv_err_panel) {
575 alertpanel_error(msg);
579 gchar *tmp = g_strdup_printf("%s\n", msg);
580 log_error(LOG_PROTOCOL, tmp);
585 if (status < 0 || status > 2) {
586 g_slist_free(mail_filtering_data->filtered);
587 g_slist_free(mail_filtering_data->unfiltered);
588 mail_filtering_data->filtered = NULL;
589 mail_filtering_data->unfiltered = NULL;
591 if (config.receive_spam && new_spams) {
592 FolderItem *save_folder = NULL;
594 if ((!config.save_folder) ||
595 (config.save_folder[0] == '\0') ||
596 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
597 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
598 save_folder = folder_find_item_from_identifier(
599 mail_filtering_data->account->trash_folder);
601 debug_print("found trash folder from account's advanced settings\n");
603 if (save_folder == NULL && mail_filtering_data->account &&
604 mail_filtering_data->account->folder) {
605 save_folder = mail_filtering_data->account->folder->trash;
607 debug_print("found trash folder from account's trash\n");
609 if (save_folder == NULL && mail_filtering_data->account &&
610 !mail_filtering_data->account->folder) {
611 if (mail_filtering_data->account->inbox) {
612 FolderItem *item = folder_find_item_from_identifier(
613 mail_filtering_data->account->inbox);
614 if (item && item->folder->trash) {
615 save_folder = item->folder->trash;
616 debug_print("found trash folder from account's inbox\n");
619 if (!save_folder && mail_filtering_data->account->local_inbox) {
620 FolderItem *item = folder_find_item_from_identifier(
621 mail_filtering_data->account->local_inbox);
622 if (item && item->folder->trash) {
623 save_folder = item->folder->trash;
624 debug_print("found trash folder from account's local_inbox\n");
628 if (save_folder == NULL) {
629 debug_print("using default trash folder\n");
630 save_folder = folder_get_default_trash();
634 for (cur = new_spams; cur; cur = cur->next) {
635 msginfo = (MsgInfo *)cur->data;
636 msginfo->is_move = TRUE;
637 msginfo->to_filter_folder = save_folder;
641 if (config.save_unsure && new_unsure) {
642 FolderItem *save_unsure_folder = NULL;
644 if ((!config.save_unsure_folder) ||
645 (config.save_unsure_folder[0] == '\0') ||
646 ((save_unsure_folder = folder_find_item_from_identifier(config.save_unsure_folder)) == NULL)) {
647 if (mail_filtering_data->account)
648 save_unsure_folder = folder_find_item_from_identifier(
649 mail_filtering_data->account->inbox);
650 if (save_unsure_folder == NULL && mail_filtering_data->account &&
651 mail_filtering_data->account->folder)
652 save_unsure_folder = mail_filtering_data->account->folder->inbox;
653 if (save_unsure_folder == NULL && mail_filtering_data->account &&
654 !mail_filtering_data->account->folder) {
655 if (mail_filtering_data->account->inbox) {
656 FolderItem *item = folder_find_item_from_identifier(
657 mail_filtering_data->account->inbox);
659 save_unsure_folder = item;
662 if (!save_unsure_folder && mail_filtering_data->account->local_inbox) {
663 FolderItem *item = folder_find_item_from_identifier(
664 mail_filtering_data->account->local_inbox);
666 save_unsure_folder = item;
670 if (save_unsure_folder == NULL)
671 save_unsure_folder = folder_get_default_inbox();
673 if (save_unsure_folder) {
674 for (cur = new_unsure; cur; cur = cur->next) {
675 msginfo = (MsgInfo *)cur->data;
676 msginfo->is_move = TRUE;
677 msginfo->to_filter_folder = save_unsure_folder;
682 g_slist_free(new_hams);
683 g_slist_free(new_unsure);
684 g_slist_free(new_spams);
685 g_slist_free(whitelisted_new_spams);
687 if (message_callback != NULL)
688 message_callback(NULL, 0, 0, FALSE);
689 mail_filtering_data->filtered = g_slist_reverse(
690 mail_filtering_data->filtered);
691 mail_filtering_data->unfiltered = g_slist_reverse(
692 mail_filtering_data->unfiltered);
697 BogofilterConfig *bogofilter_get_config(void)
702 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
706 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
708 if (msginfo == NULL && msglist == NULL) {
713 file = procmsg_get_message_file(msginfo);
717 if (message_callback != NULL)
718 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE);
721 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
722 else if (MSG_IS_SPAM(msginfo->flags))
723 /* correct bogofilter, this wasn't spam */
724 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
727 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
729 debug_print("%s\n", cmd);
730 if ((status = execute_command_line(cmd, FALSE)) != 0)
731 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
735 if (message_callback != NULL)
736 message_callback(NULL, 0, 0, FALSE);
741 GSList *cur = msglist;
743 int total = g_slist_length(msglist);
745 gboolean some_correction = FALSE, some_no_correction = FALSE;
747 if (message_callback != NULL)
748 message_callback(_("Bogofilter: learning from messages..."), total, 0, FALSE);
750 for (cur = msglist; cur && status == 0; cur = cur->next) {
751 info = (MsgInfo *)cur->data;
753 some_no_correction = TRUE;
754 else if (MSG_IS_SPAM(info->flags))
755 /* correct bogofilter, this wasn't spam */
756 some_correction = TRUE;
758 some_no_correction = TRUE;
762 if (some_correction && some_no_correction) {
763 /* we potentially have to do different stuff for every mail */
764 for (cur = msglist; cur && status == 0; cur = cur->next) {
765 info = (MsgInfo *)cur->data;
766 file = procmsg_get_message_file(info);
770 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
771 else if (MSG_IS_SPAM(info->flags))
772 /* correct bogofilter, this wasn't spam */
773 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
776 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
778 debug_print("%s\n", cmd);
779 if ((status = execute_command_line(cmd, FALSE)) != 0)
780 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
786 if (message_callback != NULL)
787 message_callback(NULL, total, done, FALSE);
789 } else if (some_correction || some_no_correction) {
795 GError *error = NULL;
796 gboolean bogo_forked;
798 bogo_args[0] = (gchar *)bogo_exec;
799 if (some_correction && !some_no_correction)
800 bogo_args[1] = "-Sn";
801 else if (some_no_correction && !some_correction)
802 bogo_args[1] = spam ? "-s":"-n";
805 debug_print("|%s %s %s ...\n", bogo_args[0], bogo_args[1], bogo_args[2]);
806 bogo_forked = g_spawn_async_with_pipes(
807 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
808 NULL, NULL, &bogo_pid, &bogo_stdin,
811 while (bogo_forked && cur) {
813 info = (MsgInfo *)cur->data;
814 file = procmsg_get_message_file(info);
816 tmp = g_strdup_printf("%s\n",
818 write_all(bogo_stdin, tmp, strlen(tmp));
823 if (message_callback != NULL)
824 message_callback(NULL, total, done, FALSE);
829 waitpid(bogo_pid, &status, 0);
830 if (!WIFEXITED(status))
833 status = WEXITSTATUS(status);
835 if (!bogo_forked || status != 0) {
836 log_error(LOG_PROTOCOL, _("Learning failed; `%s %s %s` returned with error:\n%s"),
837 bogo_args[0], bogo_args[1], bogo_args[2],
838 error ? error->message:_("Unknown error"));
845 if (message_callback != NULL)
846 message_callback(NULL, 0, 0, FALSE);
852 void bogofilter_save_config(void)
857 debug_print("Saving Bogofilter Page\n");
859 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
860 pfile = prefs_write_open(rcpath);
862 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
865 if (prefs_write_param(param, pfile->fp) < 0) {
866 g_warning("Failed to write Bogofilter configuration to file\n");
867 prefs_file_close_revert(pfile);
870 fprintf(pfile->fp, "\n");
872 prefs_file_close(pfile);
875 void bogofilter_set_message_callback(MessageCallback callback)
877 message_callback = callback;
880 gint plugin_init(gchar **error)
886 if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
887 VERSION_NUMERIC, PLUGIN_NAME, error))
890 prefs_set_default(param);
891 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
892 prefs_read_config(param, "Bogofilter", rcpath, NULL);
895 bogofilter_gtk_init();
897 debug_print("Bogofilter plugin loaded\n");
900 bogofilter_start_thread();
903 if (config.process_emails) {
904 bogofilter_register_hook();
907 procmsg_register_spam_learner(bogofilter_learn);
908 procmsg_spam_set_folder(config.save_folder, bogofilter_get_spam_folder);
914 FolderItem *bogofilter_get_spam_folder(MsgInfo *msginfo)
916 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
918 if (item || msginfo == NULL || msginfo->folder == NULL)
921 if (msginfo->folder->folder &&
922 msginfo->folder->folder->account &&
923 msginfo->folder->folder->account->set_trash_folder) {
924 item = folder_find_item_from_identifier(
925 msginfo->folder->folder->account->trash_folder);
929 msginfo->folder->folder &&
930 msginfo->folder->folder->trash)
931 item = msginfo->folder->folder->trash;
934 item = folder_get_default_trash();
936 debug_print("bogo spam dir: %s\n", folder_item_get_path(item));
940 void plugin_done(void)
943 bogofilter_unregister_hook();
946 bogofilter_stop_thread();
948 g_free(config.save_folder);
949 bogofilter_gtk_done();
950 procmsg_unregister_spam_learner(bogofilter_learn);
951 procmsg_spam_set_folder(NULL, NULL);
952 debug_print("Bogofilter plugin unloaded\n");
955 const gchar *plugin_name(void)
960 const gchar *plugin_desc(void)
962 return _("This plugin can check all messages that are received from an "
963 "IMAP, LOCAL or POP account for spam using Bogofilter. "
964 "You will need Bogofilter installed locally.\n"
966 "Before Bogofilter can recognize spam messages, you have to "
967 "train it by marking a few hundred spam and ham messages "
968 "with the use of \"/Mark/Mark as spam\" and \"/Mark/Mark as "
971 "When a message is identified as spam it can be deleted or "
972 "saved in a specially designated folder.\n"
974 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
977 const gchar *plugin_type(void)
982 const gchar *plugin_licence(void)
987 const gchar *plugin_version(void)
992 struct PluginFeature *plugin_provides(void)
994 static struct PluginFeature features[] =
995 { {PLUGIN_FILTERING, N_("Spam detection")},
996 {PLUGIN_FILTERING, N_("Spam learning")},
997 {PLUGIN_NOTHING, NULL}};
1001 void bogofilter_register_hook(void)
1004 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
1005 if (hook_id == -1) {
1006 g_warning("Failed to register mail filtering hook");
1007 config.process_emails = FALSE;
1011 void bogofilter_unregister_hook(void)
1013 if (hook_id != -1) {
1014 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);