2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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>
30 #include <glib/gi18n.h>
36 #include "common/sylpheed.h"
37 #include "common/version.h"
39 #include "common/utils.h"
44 #include "prefs_gtk.h"
46 #include "bogofilter.h"
49 #include "prefs_common.h"
50 #include "alertpanel.h"
52 #ifdef HAVE_SYSEXITS_H
58 #ifdef HAVE_SYS_ERRNO_H
59 #include <sys/errno.h>
64 #ifdef HAVE_SYS_TIME_H
75 CHILD_RUNNING = 1 << 0,
76 TIMEOUT_RUNNING = 1 << 1,
79 static guint hook_id = -1;
80 static MessageCallback message_callback;
82 static BogofilterConfig config;
84 static PrefParam param[] = {
85 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
87 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
89 {"save_folder", NULL, &config.save_folder, P_STRING,
91 {"max_size", "250", &config.max_size, P_INT,
93 {"bogopath", "bogofilter", &config.bogopath, P_STRING,
96 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
99 static gboolean mail_filtering_hook(gpointer source, gpointer data)
101 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
102 MsgInfo *msginfo = mail_filtering_data->msginfo;
103 gboolean is_spam = FALSE;
104 static gboolean warned_error = FALSE;
105 gchar *file = NULL, *cmd = NULL;
107 gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
109 if (!config.process_emails) {
112 debug_print("Filtering message %d\n", msginfo->msgnum);
113 if (message_callback != NULL)
114 message_callback(_("Bogofilter: filtering message..."), 0, 0);
116 file = procmsg_get_message_file(msginfo);
119 cmd = g_strdup_printf("%s -I %s", bogo_exec, file);
122 status = system(cmd);
127 status = WEXITSTATUS(status);
131 debug_print("bogofilter status %d\n", status);
132 is_spam = (status == 0);
135 debug_print("message is spam\n");
136 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
137 if (config.receive_spam) {
138 FolderItem *save_folder;
140 if ((!config.save_folder) ||
141 (config.save_folder[0] == '\0') ||
142 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
143 save_folder = folder_get_default_trash();
145 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
146 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
147 folder_item_move_msg(save_folder, msginfo);
149 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
154 debug_print("message is ham\n");
155 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
158 if (status == 3) { /* I/O or other errors */
160 alertpanel_error(_("The Bogofilter plugin couldn't filter "
161 "a message. The probable cause of the "
162 "error is that it didn't learn from any mail.\n"
163 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
164 "ham\" to train Bogofilter with a few hundred "
165 "spam and ham messages."));
173 BogofilterConfig *bogofilter_get_config(void)
178 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
182 const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
184 if (msginfo == NULL && msglist == NULL) {
189 file = procmsg_get_message_file(msginfo);
193 if (message_callback != NULL)
194 message_callback(_("Bogofilter: learning from message..."), 0, 0);
197 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
198 else if (MSG_IS_SPAM(msginfo->flags))
199 /* correct bogofilter, this wasn't spam */
200 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
203 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
204 if ((status = execute_command_line(cmd, FALSE)) != 0)
205 alertpanel_error(_("Learning failed; `%s` returned with status %d."),
209 if (message_callback != NULL)
210 message_callback(NULL, 0, 0);
215 GSList *cur = msglist;
217 int total = g_slist_length(msglist);
219 if (message_callback != NULL)
220 message_callback(_("Bogofilter: learning from messages..."), total, 0);
222 for (; cur && status == 0; cur = cur->next) {
223 info = (MsgInfo *)cur->data;
224 file = procmsg_get_message_file(info);
228 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
229 else if (MSG_IS_SPAM(info->flags))
230 /* correct bogofilter, this wasn't spam */
231 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
234 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
236 if ((status = execute_command_line(cmd, FALSE)) != 0)
237 alertpanel_error(_("Learning failed; `%s` returned with status %d."),
243 if (message_callback != NULL)
244 message_callback(NULL, total, done);
246 if (message_callback != NULL)
247 message_callback(NULL, 0, 0);
253 void bogofilter_save_config(void)
258 debug_print("Saving Bogofilter Page\n");
260 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
261 pfile = prefs_write_open(rcpath);
263 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
266 if (prefs_write_param(param, pfile->fp) < 0) {
267 g_warning("Failed to write Bogofilter configuration to file\n");
268 prefs_file_close_revert(pfile);
271 fprintf(pfile->fp, "\n");
273 prefs_file_close(pfile);
276 void bogofilter_set_message_callback(MessageCallback callback)
278 message_callback = callback;
281 gint plugin_init(gchar **error)
287 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
288 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the Bogofilter plugin was built with");
292 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
293 *error = g_strdup("Your version of Sylpheed-Claws is too old for the Bogofilter plugin");
297 prefs_set_default(param);
298 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
299 prefs_read_config(param, "Bogofilter", rcpath, NULL);
302 bogofilter_gtk_init();
304 debug_print("Bogofilter plugin loaded\n");
306 if (config.process_emails) {
307 bogofilter_register_hook();
310 procmsg_register_spam_learner(bogofilter_learn);
311 procmsg_spam_set_folder(config.save_folder);
317 void plugin_done(void)
320 bogofilter_unregister_hook();
322 g_free(config.save_folder);
323 bogofilter_gtk_done();
324 procmsg_unregister_spam_learner(bogofilter_learn);
325 procmsg_spam_set_folder(NULL);
326 debug_print("Bogofilter plugin unloaded\n");
329 const gchar *plugin_name(void)
331 return _("Bogofilter");
334 const gchar *plugin_desc(void)
336 return _("This plugin can check all messages that are received from an "
337 "IMAP, LOCAL or POP account for spam using Bogofilter. "
338 "You will need Bogofilter installed locally.\n "
340 "Before Bogofilter can recognize spam messages, you have to "
341 "train it by marking a few hundred spam and ham messages. "
342 "Use \"/Mark/Mark as Spam\" and \"/Mark/Mark as ham\" to "
343 "train Bogofilter.\n"
345 "When a message is identified as spam it can be deleted or "
346 "saved in a specially designated folder.\n"
348 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
351 const gchar *plugin_type(void)
356 const gchar *plugin_licence(void)
361 const gchar *plugin_version(void)
366 struct PluginFeature *plugin_provides(void)
368 static struct PluginFeature features[] =
369 { {PLUGIN_FILTERING, N_("Spam detection")},
370 {PLUGIN_FILTERING, N_("Spam learning")},
371 {PLUGIN_NOTHING, NULL}};
375 void bogofilter_register_hook(void)
377 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
379 g_warning("Failed to register mail filtering hook");
380 config.process_emails = FALSE;
384 void bogofilter_unregister_hook(void)
387 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);