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,
94 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
97 static gboolean mail_filtering_hook(gpointer source, gpointer data)
99 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
100 MsgInfo *msginfo = mail_filtering_data->msginfo;
101 gboolean is_spam = FALSE;
102 static gboolean warned_error = FALSE;
103 gchar *file = NULL, *cmd = NULL;
106 if (!config.process_emails) {
109 debug_print("Filtering message %d\n", msginfo->msgnum);
110 if (message_callback != NULL)
111 message_callback(_("Bogofilter: filtering message..."), 0, 0);
113 file = procmsg_get_message_file(msginfo);
116 cmd = g_strdup_printf("bogofilter -I %s", file);
119 status = system(cmd);
124 status = WEXITSTATUS(status);
128 printf("bogofilter status %d\n", status);
129 is_spam = (status == 0);
132 debug_print("message is spam\n");
133 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
134 if (config.receive_spam) {
135 FolderItem *save_folder;
137 if ((!config.save_folder) ||
138 (config.save_folder[0] == '\0') ||
139 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
140 save_folder = folder_get_default_trash();
142 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
143 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
144 folder_item_move_msg(save_folder, msginfo);
146 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
151 debug_print("message is ham\n");
152 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
155 if (status == 3) { /* I/O or other errors */
157 alertpanel_error(_("The Bogofilter plugin couldn't filter "
158 "a message. The probable error cause is "
159 "that it didn't learn from any mail.\n"
160 "Use \"Mark/As Spam(Ham)\" from messages' "
161 "contextual menu to train Bogofilter with "
162 "a few hundreds spam and ham messages."));
170 BogofilterConfig *bogofilter_get_config(void)
175 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
180 if (msginfo == NULL && msglist == NULL) {
185 file = procmsg_get_message_file(msginfo);
189 if (message_callback != NULL)
190 message_callback(_("Bogofilter: learning from message..."), 0, 0);
191 cmd = g_strdup_printf("bogofilter -%c -I %s",
192 spam ? 's':'n', file);
193 execute_command_line(cmd, FALSE);
196 if (message_callback != NULL)
197 message_callback(NULL, 0, 0);
202 GSList *cur = msglist;
204 int total = g_slist_length(msglist);
206 if (message_callback != NULL)
207 message_callback(_("Bogofilter: learning from messages..."), total, 0);
209 for (; cur; cur = cur->next) {
210 info = (MsgInfo *)cur->data;
211 file = procmsg_get_message_file(info);
213 cmd = g_strdup_printf("bogofilter -%c -I %s",
214 spam ? 's':'n', file);
215 execute_command_line(cmd, FALSE);
219 if (message_callback != NULL)
220 message_callback(NULL, total, done);
222 if (message_callback != NULL)
223 message_callback(NULL, 0, 0);
229 void bogofilter_save_config(void)
234 debug_print("Saving Bogofilter Page\n");
236 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
237 pfile = prefs_write_open(rcpath);
239 if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
242 if (prefs_write_param(param, pfile->fp) < 0) {
243 g_warning("Failed to write Bogofilter configuration to file\n");
244 prefs_file_close_revert(pfile);
247 fprintf(pfile->fp, "\n");
249 prefs_file_close(pfile);
252 void bogofilter_set_message_callback(MessageCallback callback)
254 message_callback = callback;
257 gint plugin_init(gchar **error)
263 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
264 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the Bogofilter plugin was built with");
268 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
269 *error = g_strdup("Your version of Sylpheed-Claws is too old for the Bogofilter plugin");
273 prefs_set_default(param);
274 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
275 prefs_read_config(param, "Bogofilter", rcpath, NULL);
278 bogofilter_gtk_init();
280 debug_print("Bogofilter plugin loaded\n");
282 if (config.process_emails) {
283 bogofilter_register_hook();
286 procmsg_register_spam_learner(bogofilter_learn);
287 procmsg_spam_set_folder(config.save_folder);
293 void plugin_done(void)
296 bogofilter_unregister_hook();
298 g_free(config.save_folder);
299 bogofilter_gtk_done();
300 procmsg_unregister_spam_learner(bogofilter_learn);
301 procmsg_spam_set_folder(NULL);
302 debug_print("Bogofilter plugin unloaded\n");
305 const gchar *plugin_name(void)
307 return _("Bogofilter");
310 const gchar *plugin_desc(void)
312 return _("This plugin can check all messages that are received from an "
313 "IMAP, LOCAL or POP account for spam using a Bogofilter "
314 "server. You will need Bogofilter installed locally.\n "
316 "Before Bogofilter can recognize spam messages, you have to "
317 "train it by marking a few hundreds spam and ham messages. "
318 "Use \"Mark/As Spam(Ham)\" from messages' contextual menu "
319 "to train Bogofilter.\n"
321 "When a message is identified as spam it can be deleted or "
322 "saved in a specially designated folder.\n"
324 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
327 const gchar *plugin_type(void)
332 const gchar *plugin_licence(void)
337 const gchar *plugin_version(void)
342 struct PluginFeature *plugin_provides(void)
344 static struct PluginFeature features[] =
345 { {PLUGIN_FILTERING, N_("Spam detection")},
346 {PLUGIN_FILTERING, N_("Spam learning")},
347 {PLUGIN_NOTHING, NULL}};
351 void bogofilter_register_hook(void)
353 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
355 g_warning("Failed to register mail filtering hook");
356 config.process_emails = FALSE;
360 void bogofilter_unregister_hook(void)
363 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);