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;
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 bogo_args[0] = bogo_exec;
162 bogo_forked = g_spawn_async_with_pipes(
163 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
164 NULL, NULL, &bogo_pid, &bogo_stdin,
165 &bogo_stdout, NULL, &error);
167 if (bogo_forked == FALSE) {
168 g_warning("%s\n", error ? error->message:"ERROR???");
173 for (cur = msglist; cur; cur = cur->next) {
174 msginfo = (MsgInfo *)cur->data;
175 debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
177 if (message_callback != NULL)
178 message_callback(NULL, total, curnum++);
180 file = procmsg_get_message_file(msginfo);
183 gchar *tmp = g_strdup_printf("%s\n",file);
184 write_all(bogo_stdin, tmp, strlen(tmp));
186 memset(buf, 0, sizeof(buf));
187 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
188 g_warning("bogofilter short read\n");
189 debug_print("message %d is ham\n", msginfo->msgnum);
190 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
191 mail_filtering_data->unfiltered = g_slist_prepend(
192 mail_filtering_data->unfiltered, msginfo);
194 gchar **parts = NULL;
195 if (strchr(buf, '/')) {
196 tmp = strrchr(buf, '/')+1;
200 parts = g_strsplit(tmp, " ", 0);
201 debug_print("read %s\n", buf);
202 if (parts && parts[0] && parts[1] && *parts[1] == 'S') {
203 debug_print("message %d is spam\n", msginfo->msgnum);
204 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
206 if (config.receive_spam) {
207 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
208 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
209 spams = g_slist_prepend(spams, msginfo);
211 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
214 mail_filtering_data->filtered = g_slist_prepend(
215 mail_filtering_data->filtered, msginfo);
217 debug_print("message %d is ham\n", msginfo->msgnum);
218 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
219 mail_filtering_data->unfiltered = g_slist_prepend(
220 mail_filtering_data->unfiltered, msginfo);
226 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
227 mail_filtering_data->unfiltered = g_slist_prepend(
228 mail_filtering_data->unfiltered, msginfo);
236 waitpid(bogo_pid, &status, 0);
237 if (!WIFEXITED(status))
240 status = WEXITSTATUS(status);
243 if (status < 0 || status > 2) { /* I/O or other errors */
247 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
248 "a message. The probable cause of the "
249 "error is that it didn't learn from any mail.\n"
250 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
251 "ham\" to train Bogofilter with a few hundred "
252 "spam and ham messages."));
254 msg = g_strdup_printf(_("The Bogofilter plugin couldn't filter "
255 "a message. the command `%s %s %s` couldn't be run."),
256 bogo_args[0], bogo_args[1], bogo_args[2]);
257 if (!prefs_common.no_recv_err_panel) {
259 alertpanel_error(msg);
263 gchar *tmp = g_strdup_printf("%s\n", msg);
269 if (status < 0 || status > 2) {
270 g_slist_free(mail_filtering_data->filtered);
271 g_slist_free(mail_filtering_data->unfiltered);
273 mail_filtering_data->filtered = NULL;
274 mail_filtering_data->unfiltered = NULL;
275 } else if (config.receive_spam && spams) {
276 FolderItem *save_folder;
278 if ((!config.save_folder) ||
279 (config.save_folder[0] == '\0') ||
280 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
281 save_folder = folder_get_default_trash();
283 for (cur = spams; cur; cur = cur->next) {
284 msginfo = (MsgInfo *)cur->data;
285 msginfo->is_move = TRUE;
286 msginfo->to_filter_folder = save_folder;
291 if (message_callback != NULL)
292 message_callback(NULL, 0, 0);
293 mail_filtering_data->filtered = g_slist_reverse(
294 mail_filtering_data->filtered);
295 mail_filtering_data->unfiltered = g_slist_reverse(
296 mail_filtering_data->unfiltered);
301 BogofilterConfig *bogofilter_get_config(void)
306 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
310 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
312 if (msginfo == NULL && msglist == NULL) {
317 file = procmsg_get_message_file(msginfo);
321 if (message_callback != NULL)
322 message_callback(_("Bogofilter: learning from message..."), 0, 0);
325 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
326 else if (MSG_IS_SPAM(msginfo->flags))
327 /* correct bogofilter, this wasn't spam */
328 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
331 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
332 if ((status = execute_command_line(cmd, FALSE)) != 0)
333 log_error(_("Learning failed; `%s` returned with status %d."),
337 if (message_callback != NULL)
338 message_callback(NULL, 0, 0);
343 GSList *cur = msglist;
345 int total = g_slist_length(msglist);
347 gboolean some_correction = FALSE, some_no_correction = FALSE;
349 if (message_callback != NULL)
350 message_callback(_("Bogofilter: learning from messages..."), total, 0);
352 for (cur = msglist; cur && status == 0; cur = cur->next) {
353 info = (MsgInfo *)cur->data;
355 some_no_correction = TRUE;
356 else if (MSG_IS_SPAM(info->flags))
357 /* correct bogofilter, this wasn't spam */
358 some_correction = TRUE;
360 some_no_correction = TRUE;
364 if (some_correction && some_no_correction) {
365 /* we potentially have to do different stuff for every mail */
366 for (cur = msglist; cur && status == 0; cur = cur->next) {
367 info = (MsgInfo *)cur->data;
368 file = procmsg_get_message_file(info);
372 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
373 else if (MSG_IS_SPAM(info->flags))
374 /* correct bogofilter, this wasn't spam */
375 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
378 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
380 if ((status = execute_command_line(cmd, FALSE)) != 0)
381 log_error(_("Learning failed; `%s` returned with status %d."),
387 if (message_callback != NULL)
388 message_callback(NULL, total, done);
390 } else if (some_correction || some_no_correction) {
396 GError *error = NULL;
397 gboolean bogo_forked;
399 bogo_args[0] = (gchar *)bogo_exec;
400 if (some_correction && !some_no_correction)
401 bogo_args[1] = "-Sn";
402 else if (some_no_correction && !some_correction)
403 bogo_args[1] = spam ? "-s":"-n";
407 bogo_forked = g_spawn_async_with_pipes(
408 NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
409 NULL, NULL, &bogo_pid, &bogo_stdin,
412 while (bogo_forked && cur) {
414 info = (MsgInfo *)cur->data;
415 file = procmsg_get_message_file(info);
417 tmp = g_strdup_printf("%s\n",
419 write_all(bogo_stdin, tmp, strlen(tmp));
424 if (message_callback != NULL)
425 message_callback(NULL, total, done);
430 waitpid(bogo_pid, &status, 0);
431 if (!WIFEXITED(status))
434 status = WEXITSTATUS(status);
436 if (!bogo_forked || status != 0) {
437 log_error(_("Learning failed; `%s %s %s` returned with error:\n%s"),
438 bogo_args[0], bogo_args[1], bogo_args[2],
439 error ? error->message:_("Unknown error"));
446 if (message_callback != NULL)
447 message_callback(NULL, 0, 0);
453 void bogofilter_save_config(void)
458 debug_print("Saving Bogofilter Page\n");
460 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
461 pfile = prefs_write_open(rcpath);
463 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
466 if (prefs_write_param(param, pfile->fp) < 0) {
467 g_warning("Failed to write Bogofilter configuration to file\n");
468 prefs_file_close_revert(pfile);
471 fprintf(pfile->fp, "\n");
473 prefs_file_close(pfile);
476 void bogofilter_set_message_callback(MessageCallback callback)
478 message_callback = callback;
481 gint plugin_init(gchar **error)
487 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
488 *error = g_strdup(_("Your version of Claws Mail is newer than the version the Bogofilter plugin was built with"));
492 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
493 *error = g_strdup(_("Your version of Claws Mail is too old for the Bogofilter plugin"));
497 prefs_set_default(param);
498 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
499 prefs_read_config(param, "Bogofilter", rcpath, NULL);
502 bogofilter_gtk_init();
504 debug_print("Bogofilter plugin loaded\n");
506 if (config.process_emails) {
507 bogofilter_register_hook();
510 procmsg_register_spam_learner(bogofilter_learn);
511 procmsg_spam_set_folder(config.save_folder);
517 void plugin_done(void)
520 bogofilter_unregister_hook();
522 g_free(config.save_folder);
523 bogofilter_gtk_done();
524 procmsg_unregister_spam_learner(bogofilter_learn);
525 procmsg_spam_set_folder(NULL);
526 debug_print("Bogofilter plugin unloaded\n");
529 const gchar *plugin_name(void)
531 return _("Bogofilter");
534 const gchar *plugin_desc(void)
536 return _("This plugin can check all messages that are received from an "
537 "IMAP, LOCAL or POP account for spam using Bogofilter. "
538 "You will need Bogofilter installed locally.\n "
540 "Before Bogofilter can recognize spam messages, you have to "
541 "train it by marking a few hundred spam and ham messages. "
542 "Use \"/Mark/Mark as Spam\" and \"/Mark/Mark as ham\" to "
543 "train Bogofilter.\n"
545 "When a message is identified as spam it can be deleted or "
546 "saved in a specially designated folder.\n"
548 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
551 const gchar *plugin_type(void)
556 const gchar *plugin_licence(void)
561 const gchar *plugin_version(void)
566 struct PluginFeature *plugin_provides(void)
568 static struct PluginFeature features[] =
569 { {PLUGIN_FILTERING, N_("Spam detection")},
570 {PLUGIN_FILTERING, N_("Spam learning")},
571 {PLUGIN_NOTHING, NULL}};
575 void bogofilter_register_hook(void)
578 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
580 g_warning("Failed to register mail filtering hook");
581 config.process_emails = FALSE;
585 void bogofilter_unregister_hook(void)
588 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);