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/claws.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,
91 {"insert_header", "FALSE", &config.insert_header, P_BOOL,
94 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
98 * Helper function for spawn_with_input() - write an entire
108 gssize count = write (fd, buf, to_write);
124 typedef struct _BogoFilterData {
125 MailFilteringData *mail_filtering_data;
130 GSList *spams_to_receive;
136 static BogoFilterData *to_filter_data = NULL;
138 static gboolean filter_th_done = FALSE;
139 static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
140 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
141 static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
144 static void bogofilter_do_filter(BogoFilterData *data)
147 gint bogo_stdin, bogo_stdout;
148 GError *error = NULL;
149 gboolean bogo_forked;
153 int total = 0, curnum = 0;
157 total = g_slist_length(data->msglist);
159 bogo_forked = g_spawn_async_with_pipes(
160 NULL, data->bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
161 NULL, NULL, &bogo_pid, &bogo_stdin,
162 &bogo_stdout, NULL, &error);
164 if (bogo_forked == FALSE) {
165 g_warning("%s\n", error ? error->message:"ERROR???");
170 for (cur = data->msglist; cur; cur = cur->next) {
171 msginfo = (MsgInfo *)cur->data;
172 debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
174 if (message_callback != NULL)
175 message_callback(NULL, total, curnum++, data->in_thread);
177 /* can set flags (SCANNED, ATTACHMENT) but that's ok
178 * as GUI updates are hooked not direct */
179 file = procmsg_get_message_file(msginfo);
181 gchar *tmp = g_strdup_printf("%s\n",file);
182 write_all(bogo_stdin, tmp, strlen(tmp));
184 memset(buf, 0, sizeof(buf));
185 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
186 g_warning("bogofilter short read\n");
187 debug_print("message %d is ham\n", msginfo->msgnum);
188 data->mail_filtering_data->unfiltered = g_slist_prepend(
189 data->mail_filtering_data->unfiltered, msginfo);
190 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
192 gchar **parts = NULL;
193 if (strchr(buf, '/')) {
194 tmp = strrchr(buf, '/')+1;
198 parts = g_strsplit(tmp, " ", 0);
199 debug_print("read %s\n", buf);
200 if (parts && parts[0] && parts[1] && parts[2] &&
201 FOLDER_TYPE(msginfo->folder->folder) == F_MH &&
202 config.insert_header) {
203 gchar *tmpfile = get_tmp_file();
204 FILE *input = fopen(file, "r");
205 FILE *output = fopen(tmpfile, "w");
206 if (strstr(parts[2], "\n"))
207 *(strstr(parts[2], "\n")) = '\0';
208 if (input && !output)
210 else if (!input && output)
213 gchar tmpbuf[BUFFSIZE];
214 const gchar *bogosity = *parts[1] == 'S' ? "Spam":
215 (*parts[1] == 'H' ? "Ham":"Unsure");
216 gchar *tmpstr = g_strdup_printf(
217 "X-Claws-Bogosity: %s, spamicity=%s\n",
219 fwrite(tmpstr, 1, strlen(tmpstr), output);
220 while (fgets(tmpbuf, sizeof(buf), input))
221 fputs(tmpbuf, output);
224 move_file(tmpfile, file, TRUE);
229 if (parts && parts[0] && parts[1] && *parts[1] == 'S') {
230 debug_print("message %d is spam\n", msginfo->msgnum);
231 if (config.receive_spam) {
232 data->spams_to_receive = g_slist_prepend(data->spams_to_receive, msginfo);
235 data->mail_filtering_data->filtered = g_slist_prepend(
236 data->mail_filtering_data->filtered, msginfo);
237 data->new_spams = g_slist_prepend(data->new_spams, msginfo);
239 debug_print("message %d is ham\n", msginfo->msgnum);
240 data->mail_filtering_data->unfiltered = g_slist_prepend(
241 data->mail_filtering_data->unfiltered, msginfo);
242 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
248 data->mail_filtering_data->unfiltered = g_slist_prepend(
249 data->mail_filtering_data->unfiltered, msginfo);
250 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
257 waitpid(bogo_pid, &status, 0);
258 if (!WIFEXITED(status))
261 status = WEXITSTATUS(status);
264 to_filter_data->status = status;
268 static void *bogofilter_filtering_thread(void *data)
270 while (!filter_th_done) {
271 pthread_mutex_lock(&list_mutex);
272 if (to_filter_data == NULL || to_filter_data->done == TRUE) {
273 pthread_mutex_unlock(&list_mutex);
274 debug_print("thread is waiting for something to filter\n");
275 pthread_mutex_lock(&wait_mutex);
276 pthread_cond_wait(&wait_cond, &wait_mutex);
277 pthread_mutex_unlock(&wait_mutex);
279 debug_print("thread awaken with something to filter\n");
280 to_filter_data->done = FALSE;
281 bogofilter_do_filter(to_filter_data);
282 pthread_mutex_unlock(&list_mutex);
283 to_filter_data->done = TRUE;
290 static pthread_t filter_th = 0;
292 static void bogofilter_start_thread(void)
294 filter_th_done = FALSE;
295 if (filter_th != 0 || 1)
297 if (pthread_create(&filter_th, 0,
298 bogofilter_filtering_thread,
303 debug_print("thread created\n");
306 static void bogofilter_stop_thread(void)
309 while (pthread_mutex_trylock(&list_mutex) != 0) {
313 if (filter_th != 0) {
314 filter_th_done = TRUE;
315 debug_print("waking thread up\n");
316 pthread_mutex_lock(&wait_mutex);
317 pthread_cond_broadcast(&wait_cond);
318 pthread_mutex_unlock(&wait_mutex);
319 pthread_join(filter_th, &res);
322 pthread_mutex_unlock(&list_mutex);
323 debug_print("thread done\n");
327 static gboolean mail_filtering_hook(gpointer source, gpointer data)
329 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
330 MsgInfo *msginfo = mail_filtering_data->msginfo;
331 GSList *msglist = mail_filtering_data->msglist;
333 static gboolean warned_error = FALSE;
335 int total = 0, curnum = 0;
336 GSList *spams_to_receive = NULL, *new_hams = NULL, *new_spams = NULL;
337 gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
339 gboolean ok_to_thread = TRUE;
341 bogo_args[0] = bogo_exec;
346 if (!config.process_emails) {
350 if (msglist == NULL && msginfo != NULL) {
351 g_warning("wrong call to bogofilter mail_filtering_hook");
355 total = g_slist_length(msglist);
357 /* we have to make sure the mails are cached - or it'll break on IMAP */
358 if (message_callback != NULL)
359 message_callback(_("Bogofilter: fetching bodies..."), total, 0, FALSE);
360 for (cur = msglist; cur; cur = cur->next) {
361 gchar *file = procmsg_get_message_file((MsgInfo *)cur->data);
363 ok_to_thread = FALSE;
364 if (message_callback != NULL)
365 message_callback(NULL, total, curnum++, FALSE);
368 if (message_callback != NULL)
369 message_callback(NULL, 0, 0, FALSE);
371 if (message_callback != NULL)
372 message_callback(_("Bogofilter: filtering messages..."), total, 0, FALSE);
375 while (pthread_mutex_trylock(&list_mutex) != 0) {
380 to_filter_data = g_new0(BogoFilterData, 1);
381 to_filter_data->msglist = msglist;
382 to_filter_data->mail_filtering_data = mail_filtering_data;
383 to_filter_data->spams_to_receive = NULL;
384 to_filter_data->new_hams = NULL;
385 to_filter_data->new_spams = NULL;
386 to_filter_data->done = FALSE;
387 to_filter_data->status = -1;
388 to_filter_data->bogo_args = bogo_args;
390 to_filter_data->in_thread = (filter_th != 0 && ok_to_thread);
392 to_filter_data->in_thread = FALSE;
396 pthread_mutex_unlock(&list_mutex);
398 if (filter_th != 0 && ok_to_thread) {
399 debug_print("waking thread to let it filter things\n");
400 pthread_mutex_lock(&wait_mutex);
401 pthread_cond_broadcast(&wait_cond);
402 pthread_mutex_unlock(&wait_mutex);
404 while (!to_filter_data->done) {
410 while (pthread_mutex_trylock(&list_mutex) != 0) {
415 if (filter_th == 0 || !ok_to_thread)
416 bogofilter_do_filter(to_filter_data);
418 bogofilter_do_filter(to_filter_data);
421 spams_to_receive = to_filter_data->spams_to_receive;
422 new_hams = to_filter_data->new_hams;
423 new_spams = to_filter_data->new_spams;
424 status = to_filter_data->status;
425 g_free(to_filter_data);
426 to_filter_data = NULL;
428 pthread_mutex_unlock(&list_mutex);
433 for (cur = new_hams; cur; cur = cur->next) {
434 MsgInfo *msginfo = (MsgInfo *)cur->data;
435 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
437 g_slist_free(new_hams);
439 for (cur = new_spams; cur; cur = cur->next) {
440 MsgInfo *msginfo = (MsgInfo *)cur->data;
441 if (config.receive_spam) {
442 procmsg_msginfo_change_flags(msginfo, MSG_SPAM, 0, ~0, 0);
444 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
447 g_slist_free(new_spams);
449 if (status < 0 || status > 2) { /* I/O or other errors */
453 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
454 "a message. The probable cause of the "
455 "error is that it didn't learn from any mail.\n"
456 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
457 "ham\" to train Bogofilter with a few hundred "
458 "spam and ham messages."));
460 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
461 "a message. the command `%s %s %s` couldn't be run."),
462 bogo_args[0], bogo_args[1], bogo_args[2]);
463 if (!prefs_common.no_recv_err_panel) {
465 alertpanel_error(msg);
469 gchar *tmp = g_strdup_printf("%s\n", msg);
475 if (status < 0 || status > 2) {
476 g_slist_free(mail_filtering_data->filtered);
477 g_slist_free(mail_filtering_data->unfiltered);
478 g_slist_free(spams_to_receive);
479 mail_filtering_data->filtered = NULL;
480 mail_filtering_data->unfiltered = NULL;
481 } else if (config.receive_spam && spams_to_receive) {
482 FolderItem *save_folder;
484 if ((!config.save_folder) ||
485 (config.save_folder[0] == '\0') ||
486 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
487 save_folder = folder_get_default_trash();
489 for (cur = spams_to_receive; cur; cur = cur->next) {
490 msginfo = (MsgInfo *)cur->data;
491 msginfo->is_move = TRUE;
492 msginfo->to_filter_folder = save_folder;
497 if (message_callback != NULL)
498 message_callback(NULL, 0, 0, FALSE);
499 mail_filtering_data->filtered = g_slist_reverse(
500 mail_filtering_data->filtered);
501 mail_filtering_data->unfiltered = g_slist_reverse(
502 mail_filtering_data->unfiltered);
507 BogofilterConfig *bogofilter_get_config(void)
512 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
516 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
518 if (msginfo == NULL && msglist == NULL) {
523 file = procmsg_get_message_file(msginfo);
527 if (message_callback != NULL)
528 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE);
531 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
532 else if (MSG_IS_SPAM(msginfo->flags))
533 /* correct bogofilter, this wasn't spam */
534 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
537 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
538 if ((status = execute_command_line(cmd, FALSE)) != 0)
539 log_error(_("Learning failed; `%s` returned with status %d."),
543 if (message_callback != NULL)
544 message_callback(NULL, 0, 0, FALSE);
549 GSList *cur = msglist;
551 int total = g_slist_length(msglist);
553 gboolean some_correction = FALSE, some_no_correction = FALSE;
555 if (message_callback != NULL)
556 message_callback(_("Bogofilter: learning from messages..."), total, 0, FALSE);
558 for (cur = msglist; cur && status == 0; cur = cur->next) {
559 info = (MsgInfo *)cur->data;
561 some_no_correction = TRUE;
562 else if (MSG_IS_SPAM(info->flags))
563 /* correct bogofilter, this wasn't spam */
564 some_correction = TRUE;
566 some_no_correction = TRUE;
570 if (some_correction && some_no_correction) {
571 /* we potentially have to do different stuff for every mail */
572 for (cur = msglist; cur && status == 0; cur = cur->next) {
573 info = (MsgInfo *)cur->data;
574 file = procmsg_get_message_file(info);
578 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
579 else if (MSG_IS_SPAM(info->flags))
580 /* correct bogofilter, this wasn't spam */
581 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
584 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
586 if ((status = execute_command_line(cmd, FALSE)) != 0)
587 log_error(_("Learning failed; `%s` returned with status %d."),
593 if (message_callback != NULL)
594 message_callback(NULL, total, done, FALSE);
596 } else if (some_correction || some_no_correction) {
602 GError *error = NULL;
603 gboolean bogo_forked;
605 bogo_args[0] = (gchar *)bogo_exec;
606 if (some_correction && !some_no_correction)
607 bogo_args[1] = "-Sn";
608 else if (some_no_correction && !some_correction)
609 bogo_args[1] = spam ? "-s":"-n";
613 bogo_forked = g_spawn_async_with_pipes(
614 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
615 NULL, NULL, &bogo_pid, &bogo_stdin,
618 while (bogo_forked && cur) {
620 info = (MsgInfo *)cur->data;
621 file = procmsg_get_message_file(info);
623 tmp = g_strdup_printf("%s\n",
625 write_all(bogo_stdin, tmp, strlen(tmp));
630 if (message_callback != NULL)
631 message_callback(NULL, total, done, FALSE);
636 waitpid(bogo_pid, &status, 0);
637 if (!WIFEXITED(status))
640 status = WEXITSTATUS(status);
642 if (!bogo_forked || status != 0) {
643 log_error(_("Learning failed; `%s %s %s` returned with error:\n%s"),
644 bogo_args[0], bogo_args[1], bogo_args[2],
645 error ? error->message:_("Unknown error"));
652 if (message_callback != NULL)
653 message_callback(NULL, 0, 0, FALSE);
659 void bogofilter_save_config(void)
664 debug_print("Saving Bogofilter Page\n");
666 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
667 pfile = prefs_write_open(rcpath);
669 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
672 if (prefs_write_param(param, pfile->fp) < 0) {
673 g_warning("Failed to write Bogofilter configuration to file\n");
674 prefs_file_close_revert(pfile);
677 fprintf(pfile->fp, "\n");
679 prefs_file_close(pfile);
682 void bogofilter_set_message_callback(MessageCallback callback)
684 message_callback = callback;
687 gint plugin_init(gchar **error)
693 if ((claws_get_version() > VERSION_NUMERIC)) {
694 *error = g_strdup(_("Your version of Claws Mail is newer than the version the Bogofilter plugin was built with"));
698 if ((claws_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
699 *error = g_strdup(_("Your version of Claws Mail is too old for the Bogofilter plugin"));
703 prefs_set_default(param);
704 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
705 prefs_read_config(param, "Bogofilter", rcpath, NULL);
708 bogofilter_gtk_init();
710 debug_print("Bogofilter plugin loaded\n");
713 bogofilter_start_thread();
716 if (config.process_emails) {
717 bogofilter_register_hook();
720 procmsg_register_spam_learner(bogofilter_learn);
721 procmsg_spam_set_folder(config.save_folder);
727 void plugin_done(void)
730 bogofilter_unregister_hook();
733 bogofilter_stop_thread();
735 g_free(config.save_folder);
736 bogofilter_gtk_done();
737 procmsg_unregister_spam_learner(bogofilter_learn);
738 procmsg_spam_set_folder(NULL);
739 debug_print("Bogofilter plugin unloaded\n");
742 const gchar *plugin_name(void)
744 return _("Bogofilter");
747 const gchar *plugin_desc(void)
749 return _("This plugin can check all messages that are received from an "
750 "IMAP, LOCAL or POP account for spam using Bogofilter. "
751 "You will need Bogofilter installed locally.\n "
753 "Before Bogofilter can recognize spam messages, you have to "
754 "train it by marking a few hundred spam and ham messages. "
755 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as ham\" to "
756 "train Bogofilter.\n"
758 "When a message is identified as spam it can be deleted or "
759 "saved in a specially designated folder.\n"
761 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
764 const gchar *plugin_type(void)
769 const gchar *plugin_licence(void)
774 const gchar *plugin_version(void)
779 struct PluginFeature *plugin_provides(void)
781 static struct PluginFeature features[] =
782 { {PLUGIN_FILTERING, N_("Spam detection")},
783 {PLUGIN_FILTERING, N_("Spam learning")},
784 {PLUGIN_NOTHING, NULL}};
788 void bogofilter_register_hook(void)
791 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
793 g_warning("Failed to register mail filtering hook");
794 config.process_emails = FALSE;
798 void bogofilter_unregister_hook(void)
801 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);