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 #define MAILS_PER_BATCH 20
77 static guint hook_id = -1;
78 static MessageCallback message_callback;
80 static BogofilterConfig config;
82 static PrefParam param[] = {
83 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
85 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
87 {"save_folder", NULL, &config.save_folder, P_STRING,
89 {"max_size", "250", &config.max_size, P_INT,
91 {"bogopath", "bogofilter", &config.bogopath, P_STRING,
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 static gboolean mail_filtering_hook(gpointer source, gpointer data)
126 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
127 MsgInfo *msginfo = mail_filtering_data->msginfo;
128 GSList *msglist = mail_filtering_data->msglist;
130 static gboolean warned_error = FALSE;
133 gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
134 int total = 0, curnum = 0;
135 GSList *spams = NULL, *new_hams = NULL, *new_spams = NULL;
140 gint bogo_stdin, bogo_stdout;
141 GError *error = NULL;
142 gboolean bogo_forked;
144 if (!config.process_emails) {
148 if (msglist == NULL && msginfo != NULL) {
149 g_warning("wrong call to bogofilter mail_filtering_hook");
153 total = g_slist_length(msglist);
154 if (message_callback != NULL)
155 message_callback(_("Bogofilter: filtering messages..."), total, 0);
157 /* determine spam status - should be threaded */
158 bogo_args[0] = bogo_exec;
163 bogo_forked = g_spawn_async_with_pipes(
164 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
165 NULL, NULL, &bogo_pid, &bogo_stdin,
166 &bogo_stdout, NULL, &error);
168 if (bogo_forked == FALSE) {
169 g_warning("%s\n", error ? error->message:"ERROR???");
174 for (cur = msglist; cur; cur = cur->next) {
175 msginfo = (MsgInfo *)cur->data;
176 debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
178 if (message_callback != NULL)
179 message_callback(NULL, total, curnum++);
181 /* can set flags (SCANNED, ATTACHMENT) but that's ok
182 * as GUI updates are hooked not direct */
183 file = procmsg_get_message_file(msginfo);
186 gchar *tmp = g_strdup_printf("%s\n",file);
187 write_all(bogo_stdin, tmp, strlen(tmp));
189 memset(buf, 0, sizeof(buf));
190 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
191 g_warning("bogofilter short read\n");
192 debug_print("message %d is ham\n", msginfo->msgnum);
193 mail_filtering_data->unfiltered = g_slist_prepend(
194 mail_filtering_data->unfiltered, msginfo);
195 new_hams = g_slist_prepend(new_hams, msginfo);
197 gchar **parts = NULL;
198 if (strchr(buf, '/')) {
199 tmp = strrchr(buf, '/')+1;
203 parts = g_strsplit(tmp, " ", 0);
204 debug_print("read %s\n", buf);
205 if (parts && parts[0] && parts[1] && *parts[1] == 'S') {
206 debug_print("message %d is spam\n", msginfo->msgnum);
207 if (config.receive_spam) {
208 spams = g_slist_prepend(spams, msginfo);
211 mail_filtering_data->filtered = g_slist_prepend(
212 mail_filtering_data->filtered, msginfo);
213 new_spams = g_slist_prepend(new_spams, msginfo);
215 debug_print("message %d is ham\n", msginfo->msgnum);
216 mail_filtering_data->unfiltered = g_slist_prepend(
217 mail_filtering_data->unfiltered, msginfo);
218 new_hams = g_slist_prepend(new_hams, msginfo);
224 mail_filtering_data->unfiltered = g_slist_prepend(
225 mail_filtering_data->unfiltered, msginfo);
226 new_hams = g_slist_prepend(new_hams, msginfo);
234 waitpid(bogo_pid, &status, 0);
235 if (!WIFEXITED(status))
238 status = WEXITSTATUS(status);
243 for (cur = new_hams; cur; cur = cur->next) {
244 MsgInfo *msginfo = (MsgInfo *)cur->data;
245 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
247 g_slist_free(new_hams);
249 for (cur = new_spams; cur; cur = cur->next) {
250 MsgInfo *msginfo = (MsgInfo *)cur->data;
251 if (config.receive_spam) {
252 procmsg_msginfo_change_flags(msginfo, MSG_SPAM, 0, ~0, 0);
254 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
257 g_slist_free(new_spams);
259 if (status < 0 || status > 2) { /* I/O or other errors */
263 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
264 "a message. The probable cause of the "
265 "error is that it didn't learn from any mail.\n"
266 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
267 "ham\" to train Bogofilter with a few hundred "
268 "spam and ham messages."));
270 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
271 "a message. the command `%s %s %s` couldn't be run."),
272 bogo_args[0], bogo_args[1], bogo_args[2]);
273 if (!prefs_common.no_recv_err_panel) {
275 alertpanel_error(msg);
279 gchar *tmp = g_strdup_printf("%s\n", msg);
285 if (status < 0 || status > 2) {
286 g_slist_free(mail_filtering_data->filtered);
287 g_slist_free(mail_filtering_data->unfiltered);
289 mail_filtering_data->filtered = NULL;
290 mail_filtering_data->unfiltered = NULL;
291 } else if (config.receive_spam && spams) {
292 FolderItem *save_folder;
294 if ((!config.save_folder) ||
295 (config.save_folder[0] == '\0') ||
296 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
297 save_folder = folder_get_default_trash();
299 for (cur = spams; cur; cur = cur->next) {
300 msginfo = (MsgInfo *)cur->data;
301 msginfo->is_move = TRUE;
302 msginfo->to_filter_folder = save_folder;
307 if (message_callback != NULL)
308 message_callback(NULL, 0, 0);
309 mail_filtering_data->filtered = g_slist_reverse(
310 mail_filtering_data->filtered);
311 mail_filtering_data->unfiltered = g_slist_reverse(
312 mail_filtering_data->unfiltered);
317 BogofilterConfig *bogofilter_get_config(void)
322 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
326 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
328 if (msginfo == NULL && msglist == NULL) {
333 file = procmsg_get_message_file(msginfo);
337 if (message_callback != NULL)
338 message_callback(_("Bogofilter: learning from message..."), 0, 0);
341 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
342 else if (MSG_IS_SPAM(msginfo->flags))
343 /* correct bogofilter, this wasn't spam */
344 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
347 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
348 if ((status = execute_command_line(cmd, FALSE)) != 0)
349 log_error(_("Learning failed; `%s` returned with status %d."),
353 if (message_callback != NULL)
354 message_callback(NULL, 0, 0);
359 GSList *cur = msglist;
361 int total = g_slist_length(msglist);
363 gboolean some_correction = FALSE, some_no_correction = FALSE;
365 if (message_callback != NULL)
366 message_callback(_("Bogofilter: learning from messages..."), total, 0);
368 for (cur = msglist; cur && status == 0; cur = cur->next) {
369 info = (MsgInfo *)cur->data;
371 some_no_correction = TRUE;
372 else if (MSG_IS_SPAM(info->flags))
373 /* correct bogofilter, this wasn't spam */
374 some_correction = TRUE;
376 some_no_correction = TRUE;
380 if (some_correction && some_no_correction) {
381 /* we potentially have to do different stuff for every mail */
382 for (cur = msglist; cur && status == 0; cur = cur->next) {
383 info = (MsgInfo *)cur->data;
384 file = procmsg_get_message_file(info);
388 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
389 else if (MSG_IS_SPAM(info->flags))
390 /* correct bogofilter, this wasn't spam */
391 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
394 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
396 if ((status = execute_command_line(cmd, FALSE)) != 0)
397 log_error(_("Learning failed; `%s` returned with status %d."),
403 if (message_callback != NULL)
404 message_callback(NULL, total, done);
406 } else if (some_correction || some_no_correction) {
412 GError *error = NULL;
413 gboolean bogo_forked;
415 bogo_args[0] = (gchar *)bogo_exec;
416 if (some_correction && !some_no_correction)
417 bogo_args[1] = "-Sn";
418 else if (some_no_correction && !some_correction)
419 bogo_args[1] = spam ? "-s":"-n";
423 bogo_forked = g_spawn_async_with_pipes(
424 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
425 NULL, NULL, &bogo_pid, &bogo_stdin,
428 while (bogo_forked && cur) {
430 info = (MsgInfo *)cur->data;
431 file = procmsg_get_message_file(info);
433 tmp = g_strdup_printf("%s\n",
435 write_all(bogo_stdin, tmp, strlen(tmp));
440 if (message_callback != NULL)
441 message_callback(NULL, total, done);
446 waitpid(bogo_pid, &status, 0);
447 if (!WIFEXITED(status))
450 status = WEXITSTATUS(status);
452 if (!bogo_forked || status != 0) {
453 log_error(_("Learning failed; `%s %s %s` returned with error:\n%s"),
454 bogo_args[0], bogo_args[1], bogo_args[2],
455 error ? error->message:_("Unknown error"));
462 if (message_callback != NULL)
463 message_callback(NULL, 0, 0);
469 void bogofilter_save_config(void)
474 debug_print("Saving Bogofilter Page\n");
476 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
477 pfile = prefs_write_open(rcpath);
479 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
482 if (prefs_write_param(param, pfile->fp) < 0) {
483 g_warning("Failed to write Bogofilter configuration to file\n");
484 prefs_file_close_revert(pfile);
487 fprintf(pfile->fp, "\n");
489 prefs_file_close(pfile);
492 void bogofilter_set_message_callback(MessageCallback callback)
494 message_callback = callback;
497 gint plugin_init(gchar **error)
503 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
504 *error = g_strdup(_("Your version of Claws Mail is newer than the version the Bogofilter plugin was built with"));
508 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
509 *error = g_strdup(_("Your version of Claws Mail is too old for the Bogofilter plugin"));
513 prefs_set_default(param);
514 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
515 prefs_read_config(param, "Bogofilter", rcpath, NULL);
518 bogofilter_gtk_init();
520 debug_print("Bogofilter plugin loaded\n");
522 if (config.process_emails) {
523 bogofilter_register_hook();
526 procmsg_register_spam_learner(bogofilter_learn);
527 procmsg_spam_set_folder(config.save_folder);
533 void plugin_done(void)
536 bogofilter_unregister_hook();
538 g_free(config.save_folder);
539 bogofilter_gtk_done();
540 procmsg_unregister_spam_learner(bogofilter_learn);
541 procmsg_spam_set_folder(NULL);
542 debug_print("Bogofilter plugin unloaded\n");
545 const gchar *plugin_name(void)
547 return _("Bogofilter");
550 const gchar *plugin_desc(void)
552 return _("This plugin can check all messages that are received from an "
553 "IMAP, LOCAL or POP account for spam using Bogofilter. "
554 "You will need Bogofilter installed locally.\n "
556 "Before Bogofilter can recognize spam messages, you have to "
557 "train it by marking a few hundred spam and ham messages. "
558 "Use \"/Mark/Mark as Spam\" and \"/Mark/Mark as ham\" to "
559 "train Bogofilter.\n"
561 "When a message is identified as spam it can be deleted or "
562 "saved in a specially designated folder.\n"
564 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
567 const gchar *plugin_type(void)
572 const gchar *plugin_licence(void)
577 const gchar *plugin_version(void)
582 struct PluginFeature *plugin_provides(void)
584 static struct PluginFeature features[] =
585 { {PLUGIN_FILTERING, N_("Spam detection")},
586 {PLUGIN_FILTERING, N_("Spam learning")},
587 {PLUGIN_NOTHING, NULL}};
591 void bogofilter_register_hook(void)
594 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
596 g_warning("Failed to register mail filtering hook");
597 config.process_emails = FALSE;
601 void bogofilter_unregister_hook(void)
604 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);