2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
23 #include "claws-features.h"
28 #include <sys/types.h>
33 #include <glib/gi18n.h>
39 #include "common/claws.h"
40 #include "common/version.h"
42 #include "common/utils.h"
47 #include "prefs_gtk.h"
49 #include "bogofilter.h"
52 #include "prefs_common.h"
53 #include "alertpanel.h"
54 #include "addr_compl.h"
56 #ifdef HAVE_SYSEXITS_H
62 #ifdef HAVE_SYS_ERRNO_H
63 #include <sys/errno.h>
68 #ifdef HAVE_SYS_TIME_H
81 #define PLUGIN_NAME (_("Bogofilter"))
83 static guint hook_id = -1;
84 static MessageCallback message_callback;
86 static BogofilterConfig config;
88 static PrefParam param[] = {
89 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
91 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
93 {"save_folder", NULL, &config.save_folder, P_STRING,
95 {"save_unsure", "FALSE", &config.save_unsure, P_BOOL,
97 {"save_unsure_folder", NULL, &config.save_unsure_folder, P_STRING,
99 {"max_size", "250", &config.max_size, P_INT,
101 {"bogopath", "bogofilter", &config.bogopath, P_STRING,
103 {"insert_header", "FALSE", &config.insert_header, P_BOOL,
105 {"whitelist_ab", "FALSE", &config.whitelist_ab, P_BOOL,
107 {"whitelist_ab_folder", N_("Any"), &config.whitelist_ab_folder, P_STRING,
109 {"learn_from_whitelist", "FALSE", &config.learn_from_whitelist, P_BOOL,
111 {"mark_as_read", "TRUE", &config.mark_as_read, P_BOOL,
114 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
118 * Helper function for spawn_with_input() - write an entire
128 gssize count = write (fd, buf, to_write);
144 typedef struct _BogoFilterData {
145 MailFilteringData *mail_filtering_data;
151 GSList *whitelisted_new_spams;
157 static BogoFilterData *to_filter_data = NULL;
159 static gboolean filter_th_done = FALSE;
160 static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
161 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
162 static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
165 static gboolean found_in_addressbook(const gchar *address)
168 gboolean found = FALSE;
174 addr = g_strdup(address);
175 extract_address(addr);
176 num_addr = complete_address(addr);
178 /* skip first item (this is the search string itself) */
180 for (; i < num_addr && !found; i++) {
181 gchar *caddr = get_complete_address(i);
182 extract_address(caddr);
183 if (strcasecmp(caddr, addr) == 0)
192 static void bogofilter_do_filter(BogoFilterData *data)
195 gint bogo_stdin, bogo_stdout;
196 GError *error = NULL;
197 gboolean bogo_forked;
201 int total = 0, curnum = 1;
205 total = g_slist_length(data->msglist);
207 bogo_forked = g_spawn_async_with_pipes(
208 NULL, data->bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
209 NULL, NULL, &bogo_pid, &bogo_stdin,
210 &bogo_stdout, NULL, &error);
212 if (bogo_forked == FALSE) {
213 g_warning("%s\n", error ? error->message:"ERROR???");
219 if (config.whitelist_ab) {
220 gchar *ab_folderpath;
222 if (*config.whitelist_ab_folder == '\0' ||
223 strcasecmp(config.whitelist_ab_folder, "Any") == 0) {
224 /* match the whole addressbook */
225 ab_folderpath = NULL;
227 /* match the specific book/folder of the addressbook */
228 ab_folderpath = config.whitelist_ab_folder;
231 start_address_completion(ab_folderpath);
234 for (cur = data->msglist; cur; cur = cur->next) {
235 gboolean whitelisted = FALSE;
236 msginfo = (MsgInfo *)cur->data;
237 debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
239 if (message_callback != NULL)
240 message_callback(NULL, total, curnum++, data->in_thread);
242 if (config.whitelist_ab && msginfo->from &&
243 found_in_addressbook(msginfo->from))
246 /* can set flags (SCANNED, ATTACHMENT) but that's ok
247 * as GUI updates are hooked not direct */
249 file = procmsg_get_message_file(msginfo);
252 gchar *tmp = g_strdup_printf("%s\n",file);
253 /* send filename to bogofilter */
254 write_all(bogo_stdin, tmp, strlen(tmp));
256 memset(buf, 0, sizeof(buf));
258 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
259 g_warning("bogofilter short read\n");
260 debug_print("message %d is ham\n", msginfo->msgnum);
261 data->mail_filtering_data->unfiltered = g_slist_prepend(
262 data->mail_filtering_data->unfiltered, msginfo);
263 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
265 gchar **parts = NULL;
267 buf[sizeof(buf) - 1] = '\0';
268 if (strchr(buf, '/')) {
269 tmp = strrchr(buf, '/')+1;
273 parts = g_strsplit(tmp, " ", 0);
274 debug_print("read %s\n", buf);
276 /* note the result if the header if needed */
277 if (parts && parts[0] && parts[1] && parts[2] &&
278 FOLDER_TYPE(msginfo->folder->folder) == F_MH &&
279 config.insert_header) {
280 gchar *tmpfile = get_tmp_file();
281 FILE *input = g_fopen(file, "r");
282 FILE *output = g_fopen(tmpfile, "w");
283 if (strstr(parts[2], "\n"))
284 *(strstr(parts[2], "\n")) = '\0';
285 if (input && !output)
287 else if (!input && output)
289 else if (input && output) {
290 gchar tmpbuf[BUFFSIZE];
291 gboolean err = FALSE;
292 const gchar *bogosity = *parts[1] == 'S' ? "Spam":
293 (*parts[1] == 'H' ? "Ham":"Unsure");
294 gchar *tmpstr = g_strdup_printf(
295 "X-Bogosity: %s, spamicity=%s%s\n",
297 whitelisted?" [whitelisted]":"");
298 if (fwrite(tmpstr, 1, strlen(tmpstr), output) < strlen(tmpstr)) {
301 while (fgets(tmpbuf, sizeof(buf), input)) {
302 if (fputs(tmpbuf, output) == EOF) {
309 if (fclose(output) == EOF)
312 move_file(tmpfile, file, TRUE);
319 if (!whitelisted && parts && parts[0] && parts[1] && *parts[1] == 'S') {
321 debug_print("message %d is spam\n", msginfo->msgnum);
322 /* Spam will be filtered away */
323 data->mail_filtering_data->filtered = g_slist_prepend(
324 data->mail_filtering_data->filtered, msginfo);
325 data->new_spams = g_slist_prepend(data->new_spams, msginfo);
327 } else if (whitelisted && parts && parts[0] && parts[1] &&
328 (*parts[1] == 'S' || *parts[1] == 'U')) {
330 debug_print("message %d is whitelisted %s\n", msginfo->msgnum,
331 *parts[1] == 'S' ? "spam":"unsure");
332 /* Whitelisted spam will *not* be filtered away, but continue
333 * their trip through filtering as if it was ham. */
334 data->mail_filtering_data->unfiltered = g_slist_prepend(
335 data->mail_filtering_data->unfiltered, msginfo);
336 /* But it gets put in a different list, so that we
337 * can still flag it and inform the user that it is
338 * considered a spam (so that he can teach bogo that
340 data->whitelisted_new_spams = g_slist_prepend(data->whitelisted_new_spams, msginfo);
342 } else if (config.save_unsure && parts && parts[0] && parts[1] && *parts[1] == 'U') {
344 debug_print("message %d is unsure\n", msginfo->msgnum);
345 /* Spam will be filtered away */
346 data->mail_filtering_data->filtered = g_slist_prepend(
347 data->mail_filtering_data->filtered, msginfo);
348 data->new_unsure = g_slist_prepend(data->new_unsure, msginfo);
352 debug_print("message %d is ham\n", msginfo->msgnum);
353 data->mail_filtering_data->unfiltered = g_slist_prepend(
354 data->mail_filtering_data->unfiltered, msginfo);
355 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
362 data->mail_filtering_data->unfiltered = g_slist_prepend(
363 data->mail_filtering_data->unfiltered, msginfo);
364 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
367 if (config.whitelist_ab)
368 end_address_completion();
373 waitpid(bogo_pid, &status, 0);
374 if (!WIFEXITED(status))
377 status = WEXITSTATUS(status);
380 to_filter_data->status = status;
384 static void *bogofilter_filtering_thread(void *data)
386 while (!filter_th_done) {
387 pthread_mutex_lock(&list_mutex);
388 if (to_filter_data == NULL || to_filter_data->done == TRUE) {
389 pthread_mutex_unlock(&list_mutex);
390 debug_print("thread is waiting for something to filter\n");
391 pthread_mutex_lock(&wait_mutex);
392 pthread_cond_wait(&wait_cond, &wait_mutex);
393 pthread_mutex_unlock(&wait_mutex);
395 debug_print("thread awaken with something to filter\n");
396 to_filter_data->done = FALSE;
397 bogofilter_do_filter(to_filter_data);
398 pthread_mutex_unlock(&list_mutex);
399 to_filter_data->done = TRUE;
406 static pthread_t filter_th = 0;
408 static void bogofilter_start_thread(void)
410 filter_th_done = FALSE;
411 if (filter_th != 0 || 1)
413 if (pthread_create(&filter_th, 0,
414 bogofilter_filtering_thread,
419 debug_print("thread created\n");
422 static void bogofilter_stop_thread(void)
425 while (pthread_mutex_trylock(&list_mutex) != 0) {
429 if (filter_th != 0) {
430 filter_th_done = TRUE;
431 debug_print("waking thread up\n");
432 pthread_mutex_lock(&wait_mutex);
433 pthread_cond_broadcast(&wait_cond);
434 pthread_mutex_unlock(&wait_mutex);
435 pthread_join(filter_th, &res);
438 pthread_mutex_unlock(&list_mutex);
439 debug_print("thread done\n");
443 static gboolean mail_filtering_hook(gpointer source, gpointer data)
445 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
446 MsgInfo *msginfo = mail_filtering_data->msginfo;
447 GSList *msglist = mail_filtering_data->msglist;
449 static gboolean warned_error = FALSE;
451 int total = 0, curnum = 0;
452 GSList *new_hams = NULL, *new_spams = NULL;
453 GSList *new_unsure, *whitelisted_new_spams = NULL;
454 gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
456 gboolean ok_to_thread = TRUE;
458 bogo_args[0] = bogo_exec;
463 if (!config.process_emails) {
467 if (msglist == NULL && msginfo != NULL) {
468 g_warning("wrong call to bogofilter mail_filtering_hook");
472 total = g_slist_length(msglist);
474 /* we have to make sure the mails are cached - or it'll break on IMAP */
475 if (message_callback != NULL)
476 message_callback(_("Bogofilter: fetching bodies..."), total, 0, FALSE);
477 for (cur = msglist; cur; cur = cur->next) {
478 gchar *file = procmsg_get_message_file((MsgInfo *)cur->data);
480 ok_to_thread = FALSE;
481 if (message_callback != NULL)
482 message_callback(NULL, total, curnum++, FALSE);
485 if (message_callback != NULL)
486 message_callback(NULL, 0, 0, FALSE);
488 if (message_callback != NULL)
489 message_callback(_("Bogofilter: filtering messages..."), total, 0, FALSE);
492 while (pthread_mutex_trylock(&list_mutex) != 0) {
497 to_filter_data = g_new0(BogoFilterData, 1);
498 to_filter_data->msglist = msglist;
499 to_filter_data->mail_filtering_data = mail_filtering_data;
500 to_filter_data->new_hams = NULL;
501 to_filter_data->new_unsure = NULL;
502 to_filter_data->new_spams = NULL;
503 to_filter_data->whitelisted_new_spams = NULL;
504 to_filter_data->done = FALSE;
505 to_filter_data->status = -1;
506 to_filter_data->bogo_args = bogo_args;
508 to_filter_data->in_thread = (filter_th != 0 && ok_to_thread);
510 to_filter_data->in_thread = FALSE;
514 pthread_mutex_unlock(&list_mutex);
516 if (filter_th != 0 && ok_to_thread) {
517 debug_print("waking thread to let it filter things\n");
518 pthread_mutex_lock(&wait_mutex);
519 pthread_cond_broadcast(&wait_cond);
520 pthread_mutex_unlock(&wait_mutex);
522 while (!to_filter_data->done) {
528 while (pthread_mutex_trylock(&list_mutex) != 0) {
533 if (filter_th == 0 || !ok_to_thread)
534 bogofilter_do_filter(to_filter_data);
536 bogofilter_do_filter(to_filter_data);
539 new_hams = to_filter_data->new_hams;
540 new_unsure = to_filter_data->new_unsure;
541 new_spams = to_filter_data->new_spams;
542 whitelisted_new_spams = to_filter_data->whitelisted_new_spams;
543 status = to_filter_data->status;
544 g_free(to_filter_data);
545 to_filter_data = NULL;
547 pthread_mutex_unlock(&list_mutex);
552 for (cur = new_hams; cur; cur = cur->next) {
553 MsgInfo *msginfo = (MsgInfo *)cur->data;
554 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
555 debug_print("unflagging ham: %d\n", msginfo->msgnum);
558 for (cur = new_unsure; cur; cur = cur->next) {
559 MsgInfo *msginfo = (MsgInfo *)cur->data;
560 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
561 debug_print("unflagging unsure: %d\n", msginfo->msgnum);
563 if (config.learn_from_whitelist && whitelisted_new_spams) {
564 /* flag whitelisted spams */
565 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
566 MsgInfo *msginfo = (MsgInfo *)cur->data;
567 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
568 debug_print("flagging whitelisted non-ham: %d\n", msginfo->msgnum);
571 bogofilter_learn(NULL, whitelisted_new_spams, FALSE);
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("unflagging whitelisted non-ham: %d\n", msginfo->msgnum);
580 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
581 MsgInfo *msginfo = (MsgInfo *)cur->data;
582 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
583 debug_print("not flagging whitelisted non-ham: %d\n", msginfo->msgnum);
587 /* flag spams and delete them if !config.receive_spam
588 * (if config.receive_spam is set, we'll move them later) */
589 for (cur = new_spams; cur; cur = cur->next) {
590 MsgInfo *msginfo = (MsgInfo *)cur->data;
591 if (config.receive_spam) {
592 if (config.mark_as_read)
593 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
594 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
596 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
600 if (status < 0 || status > 2) { /* I/O or other errors */
604 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
605 "a message. The probable cause of the "
606 "error is that it didn't learn from any mail.\n"
607 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
608 "ham\" to train Bogofilter with a few hundred "
609 "spam and ham messages."));
611 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
612 "a message. The command `%s %s %s` couldn't be run."),
613 bogo_args[0], bogo_args[1], bogo_args[2]);
614 if (!prefs_common.no_recv_err_panel) {
616 alertpanel_error("%s", msg);
620 log_error(LOG_PROTOCOL, "%s\n", msg);
624 if (status < 0 || status > 2) {
625 g_slist_free(mail_filtering_data->filtered);
626 g_slist_free(mail_filtering_data->unfiltered);
627 mail_filtering_data->filtered = NULL;
628 mail_filtering_data->unfiltered = NULL;
630 if (config.receive_spam && new_spams) {
631 FolderItem *save_folder = NULL;
633 if ((!config.save_folder) ||
634 (config.save_folder[0] == '\0') ||
635 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
636 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
637 save_folder = folder_find_item_from_identifier(
638 mail_filtering_data->account->trash_folder);
640 debug_print("found trash folder from account's advanced settings\n");
642 if (save_folder == NULL && mail_filtering_data->account &&
643 mail_filtering_data->account->folder) {
644 save_folder = mail_filtering_data->account->folder->trash;
646 debug_print("found trash folder from account's trash\n");
648 if (save_folder == NULL && mail_filtering_data->account &&
649 !mail_filtering_data->account->folder) {
650 if (mail_filtering_data->account->inbox) {
651 FolderItem *item = folder_find_item_from_identifier(
652 mail_filtering_data->account->inbox);
653 if (item && item->folder->trash) {
654 save_folder = item->folder->trash;
655 debug_print("found trash folder from account's inbox\n");
658 if (!save_folder && mail_filtering_data->account->local_inbox) {
659 FolderItem *item = folder_find_item_from_identifier(
660 mail_filtering_data->account->local_inbox);
661 if (item && item->folder->trash) {
662 save_folder = item->folder->trash;
663 debug_print("found trash folder from account's local_inbox\n");
667 if (save_folder == NULL) {
668 debug_print("using default trash folder\n");
669 save_folder = folder_get_default_trash();
673 for (cur = new_spams; cur; cur = cur->next) {
674 msginfo = (MsgInfo *)cur->data;
675 msginfo->filter_op = IS_MOVE;
676 msginfo->to_filter_folder = save_folder;
680 if (config.save_unsure && new_unsure) {
681 FolderItem *save_unsure_folder = NULL;
683 if ((!config.save_unsure_folder) ||
684 (config.save_unsure_folder[0] == '\0') ||
685 ((save_unsure_folder = folder_find_item_from_identifier(config.save_unsure_folder)) == NULL)) {
686 if (mail_filtering_data->account)
687 save_unsure_folder = folder_find_item_from_identifier(
688 mail_filtering_data->account->inbox);
689 if (save_unsure_folder == NULL && mail_filtering_data->account &&
690 mail_filtering_data->account->folder)
691 save_unsure_folder = mail_filtering_data->account->folder->inbox;
692 if (save_unsure_folder == NULL && mail_filtering_data->account &&
693 !mail_filtering_data->account->folder) {
694 if (mail_filtering_data->account->inbox) {
695 FolderItem *item = folder_find_item_from_identifier(
696 mail_filtering_data->account->inbox);
698 save_unsure_folder = item;
701 if (!save_unsure_folder && mail_filtering_data->account->local_inbox) {
702 FolderItem *item = folder_find_item_from_identifier(
703 mail_filtering_data->account->local_inbox);
705 save_unsure_folder = item;
709 if (save_unsure_folder == NULL)
710 save_unsure_folder = folder_get_default_inbox();
712 if (save_unsure_folder) {
713 for (cur = new_unsure; cur; cur = cur->next) {
714 msginfo = (MsgInfo *)cur->data;
715 msginfo->filter_op = IS_MOVE;
716 msginfo->to_filter_folder = save_unsure_folder;
721 g_slist_free(new_hams);
722 g_slist_free(new_unsure);
723 g_slist_free(new_spams);
724 g_slist_free(whitelisted_new_spams);
726 if (message_callback != NULL)
727 message_callback(NULL, 0, 0, FALSE);
728 mail_filtering_data->filtered = g_slist_reverse(
729 mail_filtering_data->filtered);
730 mail_filtering_data->unfiltered = g_slist_reverse(
731 mail_filtering_data->unfiltered);
736 BogofilterConfig *bogofilter_get_config(void)
741 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
745 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
748 if (msginfo == NULL && msglist == NULL) {
753 file = procmsg_get_message_file(msginfo);
757 if (message_callback != NULL)
758 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE);
761 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
762 else if (MSG_IS_SPAM(msginfo->flags))
763 /* correct bogofilter, this wasn't spam */
764 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
767 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
769 debug_print("%s\n", cmd);
770 if ((status = execute_command_line(cmd, FALSE)) != 0)
771 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
775 if (message_callback != NULL)
776 message_callback(NULL, 0, 0, FALSE);
778 } else if (msglist) {
779 GSList *cur = msglist;
781 int total = g_slist_length(msglist);
783 gboolean some_correction = FALSE, some_no_correction = FALSE;
785 if (message_callback != NULL)
786 message_callback(_("Bogofilter: learning from messages..."), total, 0, FALSE);
788 for (cur = msglist; cur && status == 0; cur = cur->next) {
789 info = (MsgInfo *)cur->data;
791 some_no_correction = TRUE;
792 else if (MSG_IS_SPAM(info->flags))
793 /* correct bogofilter, this wasn't spam */
794 some_correction = TRUE;
796 some_no_correction = TRUE;
800 if (some_correction && some_no_correction) {
801 /* we potentially have to do different stuff for every mail */
802 for (cur = msglist; cur && status == 0; cur = cur->next) {
803 info = (MsgInfo *)cur->data;
804 file = procmsg_get_message_file(info);
808 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
809 else if (MSG_IS_SPAM(info->flags))
810 /* correct bogofilter, this wasn't spam */
811 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
814 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
816 debug_print("%s\n", cmd);
817 if ((status = execute_command_line(cmd, FALSE)) != 0)
818 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
824 if (message_callback != NULL)
825 message_callback(NULL, total, done, FALSE);
827 } else if (some_correction || some_no_correction) {
833 GError *error = NULL;
834 gboolean bogo_forked;
836 bogo_args[0] = (gchar *)bogo_exec;
837 if (some_correction && !some_no_correction)
838 bogo_args[1] = "-Sn";
839 else if (some_no_correction && !some_correction)
840 bogo_args[1] = spam ? "-s":"-n";
843 debug_print("|%s %s %s ...\n", bogo_args[0], bogo_args[1], bogo_args[2]);
844 bogo_forked = g_spawn_async_with_pipes(
845 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
846 NULL, NULL, &bogo_pid, &bogo_stdin,
849 while (bogo_forked && cur) {
851 info = (MsgInfo *)cur->data;
852 file = procmsg_get_message_file(info);
854 tmp = g_strdup_printf("%s\n",
856 write_all(bogo_stdin, tmp, strlen(tmp));
861 if (message_callback != NULL)
862 message_callback(NULL, total, done, FALSE);
867 waitpid(bogo_pid, &status, 0);
868 if (!WIFEXITED(status))
871 status = WEXITSTATUS(status);
873 if (!bogo_forked || status != 0) {
874 log_error(LOG_PROTOCOL, _("Learning failed; `%s %s %s` returned with error:\n%s"),
875 bogo_args[0], bogo_args[1], bogo_args[2],
876 error ? error->message:_("Unknown error"));
883 if (message_callback != NULL)
884 message_callback(NULL, 0, 0, FALSE);
889 void bogofilter_save_config(void)
894 debug_print("Saving Bogofilter Page\n");
896 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
897 pfile = prefs_write_open(rcpath);
899 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
902 if (prefs_write_param(param, pfile->fp) < 0) {
903 g_warning("Failed to write Bogofilter configuration to file\n");
904 prefs_file_close_revert(pfile);
907 if (fprintf(pfile->fp, "\n") < 0) {
908 FILE_OP_ERROR(rcpath, "fprintf");
909 prefs_file_close_revert(pfile);
911 prefs_file_close(pfile);
914 void bogofilter_set_message_callback(MessageCallback callback)
916 message_callback = callback;
919 gint plugin_init(gchar **error)
925 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
926 VERSION_NUMERIC, PLUGIN_NAME, error))
929 prefs_set_default(param);
930 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
931 prefs_read_config(param, "Bogofilter", rcpath, NULL);
934 bogofilter_gtk_init();
936 debug_print("Bogofilter plugin loaded\n");
939 bogofilter_start_thread();
942 if (config.process_emails) {
943 bogofilter_register_hook();
946 procmsg_register_spam_learner(bogofilter_learn);
947 procmsg_spam_set_folder(config.save_folder, bogofilter_get_spam_folder);
953 FolderItem *bogofilter_get_spam_folder(MsgInfo *msginfo)
955 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
957 if (item || msginfo == NULL || msginfo->folder == NULL)
960 if (msginfo->folder->folder &&
961 msginfo->folder->folder->account &&
962 msginfo->folder->folder->account->set_trash_folder) {
963 item = folder_find_item_from_identifier(
964 msginfo->folder->folder->account->trash_folder);
968 msginfo->folder->folder &&
969 msginfo->folder->folder->trash)
970 item = msginfo->folder->folder->trash;
973 item = folder_get_default_trash();
975 debug_print("bogo spam dir: %s\n", folder_item_get_path(item));
979 gboolean plugin_done(void)
982 bogofilter_unregister_hook();
985 bogofilter_stop_thread();
987 g_free(config.save_folder);
988 bogofilter_gtk_done();
989 procmsg_unregister_spam_learner(bogofilter_learn);
990 procmsg_spam_set_folder(NULL, NULL);
991 debug_print("Bogofilter plugin unloaded\n");
995 const gchar *plugin_name(void)
1000 const gchar *plugin_desc(void)
1002 return _("This plugin can check all messages that are received from an "
1003 "IMAP, LOCAL or POP account for spam using Bogofilter. "
1004 "You will need Bogofilter installed locally.\n"
1006 "Before Bogofilter can recognize spam messages, you have to "
1007 "train it by marking a few hundred spam and ham messages "
1008 "with the use of \"/Mark/Mark as spam\" and \"/Mark/Mark as "
1011 "When a message is identified as spam it can be deleted or "
1012 "saved in a specially designated folder.\n"
1014 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
1017 const gchar *plugin_type(void)
1022 const gchar *plugin_licence(void)
1027 const gchar *plugin_version(void)
1032 struct PluginFeature *plugin_provides(void)
1034 static struct PluginFeature features[] =
1035 { {PLUGIN_FILTERING, N_("Spam detection")},
1036 {PLUGIN_FILTERING, N_("Spam learning")},
1037 {PLUGIN_NOTHING, NULL}};
1041 void bogofilter_register_hook(void)
1044 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
1045 if (hook_id == -1) {
1046 g_warning("Failed to register mail filtering hook");
1047 config.process_emails = FALSE;
1051 void bogofilter_unregister_hook(void)
1053 if (hook_id != -1) {
1054 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);