2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <sys/types.h>
31 #include <glib/gi18n.h>
37 #include "common/sylpheed.h"
38 #include "common/version.h"
40 #include "common/utils.h"
45 #include "prefs_gtk.h"
47 #include "bogofilter.h"
50 #include "prefs_common.h"
51 #include "alertpanel.h"
53 #ifdef HAVE_SYSEXITS_H
59 #ifdef HAVE_SYS_ERRNO_H
60 #include <sys/errno.h>
65 #ifdef HAVE_SYS_TIME_H
75 static guint hook_id = -1;
76 static MessageCallback message_callback;
78 static BogofilterConfig config;
80 static PrefParam param[] = {
81 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
83 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
85 {"save_folder", NULL, &config.save_folder, P_STRING,
87 {"max_size", "250", &config.max_size, P_INT,
89 {"bogopath", "bogofilter", &config.bogopath, P_STRING,
92 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
96 * Helper function for spawn_with_input() - write an entire
106 gssize count = write (fd, buf, to_write);
122 typedef struct _BogoFilterData {
123 MailFilteringData *mail_filtering_data;
128 GSList *spams_to_receive;
134 static BogoFilterData *to_filter_data = NULL;
136 static gboolean filter_th_done = FALSE;
137 static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
138 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
139 static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
142 static void bogofilter_do_filter(BogoFilterData *data)
145 gint bogo_stdin, bogo_stdout;
146 GError *error = NULL;
147 gboolean bogo_forked;
151 int total = 0, curnum = 0;
155 total = g_slist_length(data->msglist);
157 bogo_forked = g_spawn_async_with_pipes(
158 NULL, data->bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
159 NULL, NULL, &bogo_pid, &bogo_stdin,
160 &bogo_stdout, NULL, &error);
162 if (bogo_forked == FALSE) {
163 g_warning("%s\n", error ? error->message:"ERROR???");
168 for (cur = data->msglist; cur; cur = cur->next) {
169 msginfo = (MsgInfo *)cur->data;
170 debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
172 if (message_callback != NULL)
173 message_callback(NULL, total, curnum++, data->in_thread);
175 /* can set flags (SCANNED, ATTACHMENT) but that's ok
176 * as GUI updates are hooked not direct */
177 file = procmsg_get_message_file(msginfo);
180 gchar *tmp = g_strdup_printf("%s\n",file);
181 write_all(bogo_stdin, tmp, strlen(tmp));
183 memset(buf, 0, sizeof(buf));
184 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
185 g_warning("bogofilter short read\n");
186 debug_print("message %d is ham\n", msginfo->msgnum);
187 data->mail_filtering_data->unfiltered = g_slist_prepend(
188 data->mail_filtering_data->unfiltered, msginfo);
189 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
191 gchar **parts = NULL;
192 if (strchr(buf, '/')) {
193 tmp = strrchr(buf, '/')+1;
197 parts = g_strsplit(tmp, " ", 0);
198 debug_print("read %s\n", buf);
199 if (parts && parts[0] && parts[1] && *parts[1] == 'S') {
200 debug_print("message %d is spam\n", msginfo->msgnum);
201 if (config.receive_spam) {
202 data->spams_to_receive = g_slist_prepend(data->spams_to_receive, msginfo);
205 data->mail_filtering_data->filtered = g_slist_prepend(
206 data->mail_filtering_data->filtered, msginfo);
207 data->new_spams = g_slist_prepend(data->new_spams, msginfo);
209 debug_print("message %d is ham\n", msginfo->msgnum);
210 data->mail_filtering_data->unfiltered = g_slist_prepend(
211 data->mail_filtering_data->unfiltered, msginfo);
212 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
218 data->mail_filtering_data->unfiltered = g_slist_prepend(
219 data->mail_filtering_data->unfiltered, msginfo);
220 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
227 waitpid(bogo_pid, &status, 0);
228 if (!WIFEXITED(status))
231 status = WEXITSTATUS(status);
234 to_filter_data->status = status;
238 static void *bogofilter_filtering_thread(void *data)
240 while (!filter_th_done) {
241 pthread_mutex_lock(&list_mutex);
242 if (to_filter_data == NULL || to_filter_data->done == TRUE) {
243 pthread_mutex_unlock(&list_mutex);
244 debug_print("thread is waiting for something to filter\n");
245 pthread_mutex_lock(&wait_mutex);
246 pthread_cond_wait(&wait_cond, &wait_mutex);
247 pthread_mutex_unlock(&wait_mutex);
249 debug_print("thread awaken with something to filter\n");
250 to_filter_data->done = FALSE;
251 bogofilter_do_filter(to_filter_data);
252 pthread_mutex_unlock(&list_mutex);
253 to_filter_data->done = TRUE;
260 static pthread_t filter_th = 0;
262 static void bogofilter_start_thread(void)
264 filter_th_done = FALSE;
265 if (filter_th != 0 || 1)
267 if (pthread_create(&filter_th, 0,
268 bogofilter_filtering_thread,
273 debug_print("thread created\n");
276 static void bogofilter_stop_thread(void)
279 while (pthread_mutex_trylock(&list_mutex) != 0) {
283 if (filter_th != 0) {
284 filter_th_done = TRUE;
285 debug_print("waking thread up\n");
286 pthread_mutex_lock(&wait_mutex);
287 pthread_cond_broadcast(&wait_cond);
288 pthread_mutex_unlock(&wait_mutex);
289 pthread_join(filter_th, &res);
292 pthread_mutex_unlock(&list_mutex);
293 debug_print("thread done\n");
297 static gboolean mail_filtering_hook(gpointer source, gpointer data)
299 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
300 MsgInfo *msginfo = mail_filtering_data->msginfo;
301 GSList *msglist = mail_filtering_data->msglist;
303 static gboolean warned_error = FALSE;
305 int total = 0, curnum = 0;
306 GSList *spams_to_receive = NULL, *new_hams = NULL, *new_spams = NULL;
307 gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
309 gboolean ok_to_thread = TRUE;
311 bogo_args[0] = bogo_exec;
316 if (!config.process_emails) {
320 if (msglist == NULL && msginfo != NULL) {
321 g_warning("wrong call to bogofilter mail_filtering_hook");
325 total = g_slist_length(msglist);
327 /* we have to make sure the mails are cached - or it'll break on IMAP */
328 if (message_callback != NULL)
329 message_callback(_("Bogofilter: fetching bodies..."), total, 0, FALSE);
330 for (cur = msglist; cur; cur = cur->next) {
331 gchar *file = procmsg_get_message_file((MsgInfo *)cur->data);
333 ok_to_thread = FALSE;
334 if (message_callback != NULL)
335 message_callback(NULL, total, curnum++, FALSE);
338 if (message_callback != NULL)
339 message_callback(NULL, 0, 0, FALSE);
341 if (message_callback != NULL)
342 message_callback(_("Bogofilter: filtering messages..."), total, 0, FALSE);
345 while (pthread_mutex_trylock(&list_mutex) != 0) {
350 to_filter_data = g_new0(BogoFilterData, 1);
351 to_filter_data->msglist = msglist;
352 to_filter_data->mail_filtering_data = mail_filtering_data;
353 to_filter_data->spams_to_receive = NULL;
354 to_filter_data->new_hams = NULL;
355 to_filter_data->new_spams = NULL;
356 to_filter_data->done = FALSE;
357 to_filter_data->status = -1;
358 to_filter_data->bogo_args = bogo_args;
360 to_filter_data->in_thread = (filter_th != 0 && ok_to_thread);
362 to_filter_data->in_thread = FALSE;
366 pthread_mutex_unlock(&list_mutex);
368 if (filter_th != 0 && ok_to_thread) {
369 debug_print("waking thread to let it filter things\n");
370 pthread_mutex_lock(&wait_mutex);
371 pthread_cond_broadcast(&wait_cond);
372 pthread_mutex_unlock(&wait_mutex);
374 while (!to_filter_data->done) {
380 while (pthread_mutex_trylock(&list_mutex) != 0) {
385 if (filter_th == 0 || !ok_to_thread)
386 bogofilter_do_filter(to_filter_data);
388 bogofilter_do_filter(to_filter_data);
391 spams_to_receive = to_filter_data->spams_to_receive;
392 new_hams = to_filter_data->new_hams;
393 new_spams = to_filter_data->new_spams;
394 status = to_filter_data->status;
395 g_free(to_filter_data);
396 to_filter_data = NULL;
398 pthread_mutex_unlock(&list_mutex);
403 for (cur = new_hams; cur; cur = cur->next) {
404 MsgInfo *msginfo = (MsgInfo *)cur->data;
405 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
407 g_slist_free(new_hams);
409 for (cur = new_spams; cur; cur = cur->next) {
410 MsgInfo *msginfo = (MsgInfo *)cur->data;
411 if (config.receive_spam) {
412 procmsg_msginfo_change_flags(msginfo, MSG_SPAM, 0, ~0, 0);
414 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
417 g_slist_free(new_spams);
419 if (status < 0 || status > 2) { /* I/O or other errors */
423 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
424 "a message. The probable cause of the "
425 "error is that it didn't learn from any mail.\n"
426 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
427 "ham\" to train Bogofilter with a few hundred "
428 "spam and ham messages."));
430 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
431 "a message. the command `%s %s %s` couldn't be run."),
432 bogo_args[0], bogo_args[1], bogo_args[2]);
433 if (!prefs_common.no_recv_err_panel) {
435 alertpanel_error(msg);
439 gchar *tmp = g_strdup_printf("%s\n", msg);
445 if (status < 0 || status > 2) {
446 g_slist_free(mail_filtering_data->filtered);
447 g_slist_free(mail_filtering_data->unfiltered);
448 g_slist_free(spams_to_receive);
449 mail_filtering_data->filtered = NULL;
450 mail_filtering_data->unfiltered = NULL;
451 } else if (config.receive_spam && spams_to_receive) {
452 FolderItem *save_folder;
454 if ((!config.save_folder) ||
455 (config.save_folder[0] == '\0') ||
456 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
457 save_folder = folder_get_default_trash();
459 for (cur = spams_to_receive; cur; cur = cur->next) {
460 msginfo = (MsgInfo *)cur->data;
461 msginfo->is_move = TRUE;
462 msginfo->to_filter_folder = save_folder;
467 if (message_callback != NULL)
468 message_callback(NULL, 0, 0, FALSE);
469 mail_filtering_data->filtered = g_slist_reverse(
470 mail_filtering_data->filtered);
471 mail_filtering_data->unfiltered = g_slist_reverse(
472 mail_filtering_data->unfiltered);
477 BogofilterConfig *bogofilter_get_config(void)
482 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
486 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
488 if (msginfo == NULL && msglist == NULL) {
493 file = procmsg_get_message_file(msginfo);
497 if (message_callback != NULL)
498 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE);
501 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
502 else if (MSG_IS_SPAM(msginfo->flags))
503 /* correct bogofilter, this wasn't spam */
504 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
507 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
508 if ((status = execute_command_line(cmd, FALSE)) != 0)
509 log_error(_("Learning failed; `%s` returned with status %d."),
513 if (message_callback != NULL)
514 message_callback(NULL, 0, 0, FALSE);
519 GSList *cur = msglist;
521 int total = g_slist_length(msglist);
523 gboolean some_correction = FALSE, some_no_correction = FALSE;
525 if (message_callback != NULL)
526 message_callback(_("Bogofilter: learning from messages..."), total, 0, FALSE);
528 for (cur = msglist; cur && status == 0; cur = cur->next) {
529 info = (MsgInfo *)cur->data;
531 some_no_correction = TRUE;
532 else if (MSG_IS_SPAM(info->flags))
533 /* correct bogofilter, this wasn't spam */
534 some_correction = TRUE;
536 some_no_correction = TRUE;
540 if (some_correction && some_no_correction) {
541 /* we potentially have to do different stuff for every mail */
542 for (cur = msglist; cur && status == 0; cur = cur->next) {
543 info = (MsgInfo *)cur->data;
544 file = procmsg_get_message_file(info);
548 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
549 else if (MSG_IS_SPAM(info->flags))
550 /* correct bogofilter, this wasn't spam */
551 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
554 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
556 if ((status = execute_command_line(cmd, FALSE)) != 0)
557 log_error(_("Learning failed; `%s` returned with status %d."),
563 if (message_callback != NULL)
564 message_callback(NULL, total, done, FALSE);
566 } else if (some_correction || some_no_correction) {
572 GError *error = NULL;
573 gboolean bogo_forked;
575 bogo_args[0] = (gchar *)bogo_exec;
576 if (some_correction && !some_no_correction)
577 bogo_args[1] = "-Sn";
578 else if (some_no_correction && !some_correction)
579 bogo_args[1] = spam ? "-s":"-n";
583 bogo_forked = g_spawn_async_with_pipes(
584 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
585 NULL, NULL, &bogo_pid, &bogo_stdin,
588 while (bogo_forked && cur) {
590 info = (MsgInfo *)cur->data;
591 file = procmsg_get_message_file(info);
593 tmp = g_strdup_printf("%s\n",
595 write_all(bogo_stdin, tmp, strlen(tmp));
600 if (message_callback != NULL)
601 message_callback(NULL, total, done, FALSE);
606 waitpid(bogo_pid, &status, 0);
607 if (!WIFEXITED(status))
610 status = WEXITSTATUS(status);
612 if (!bogo_forked || status != 0) {
613 log_error(_("Learning failed; `%s %s %s` returned with error:\n%s"),
614 bogo_args[0], bogo_args[1], bogo_args[2],
615 error ? error->message:_("Unknown error"));
622 if (message_callback != NULL)
623 message_callback(NULL, 0, 0, FALSE);
629 void bogofilter_save_config(void)
634 debug_print("Saving Bogofilter Page\n");
636 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
637 pfile = prefs_write_open(rcpath);
639 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
642 if (prefs_write_param(param, pfile->fp) < 0) {
643 g_warning("Failed to write Bogofilter configuration to file\n");
644 prefs_file_close_revert(pfile);
647 fprintf(pfile->fp, "\n");
649 prefs_file_close(pfile);
652 void bogofilter_set_message_callback(MessageCallback callback)
654 message_callback = callback;
657 gint plugin_init(gchar **error)
663 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
664 *error = g_strdup(_("Your version of Claws Mail is newer than the version the Bogofilter plugin was built with"));
668 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
669 *error = g_strdup(_("Your version of Claws Mail is too old for the Bogofilter plugin"));
673 prefs_set_default(param);
674 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
675 prefs_read_config(param, "Bogofilter", rcpath, NULL);
678 bogofilter_gtk_init();
680 debug_print("Bogofilter plugin loaded\n");
683 bogofilter_start_thread();
686 if (config.process_emails) {
687 bogofilter_register_hook();
690 procmsg_register_spam_learner(bogofilter_learn);
691 procmsg_spam_set_folder(config.save_folder);
697 void plugin_done(void)
700 bogofilter_unregister_hook();
703 bogofilter_stop_thread();
705 g_free(config.save_folder);
706 bogofilter_gtk_done();
707 procmsg_unregister_spam_learner(bogofilter_learn);
708 procmsg_spam_set_folder(NULL);
709 debug_print("Bogofilter plugin unloaded\n");
712 const gchar *plugin_name(void)
714 return _("Bogofilter");
717 const gchar *plugin_desc(void)
719 return _("This plugin can check all messages that are received from an "
720 "IMAP, LOCAL or POP account for spam using Bogofilter. "
721 "You will need Bogofilter installed locally.\n "
723 "Before Bogofilter can recognize spam messages, you have to "
724 "train it by marking a few hundred spam and ham messages. "
725 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as ham\" to "
726 "train Bogofilter.\n"
728 "When a message is identified as spam it can be deleted or "
729 "saved in a specially designated folder.\n"
731 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
734 const gchar *plugin_type(void)
739 const gchar *plugin_licence(void)
744 const gchar *plugin_version(void)
749 struct PluginFeature *plugin_provides(void)
751 static struct PluginFeature features[] =
752 { {PLUGIN_FILTERING, N_("Spam detection")},
753 {PLUGIN_FILTERING, N_("Spam learning")},
754 {PLUGIN_NOTHING, NULL}};
758 void bogofilter_register_hook(void)
761 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
763 g_warning("Failed to register mail filtering hook");
764 config.process_emails = FALSE;
768 void bogofilter_unregister_hook(void)
771 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);