5f8eff2dbdbddf52a7d5c4c532c77da036541de0
[claws.git] / src / plugins / bogofilter / bogofilter.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <sys/types.h>
27 #include <sys/wait.h>
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31
32 #if HAVE_LOCALE_H
33 #  include <locale.h>
34 #endif
35
36 #include "common/sylpheed.h"
37 #include "common/version.h"
38 #include "plugin.h"
39 #include "common/utils.h"
40 #include "hooks.h"
41 #include "procmsg.h"
42 #include "folder.h"
43 #include "prefs.h"
44 #include "prefs_gtk.h"
45
46 #include "bogofilter.h"
47 #include "inc.h"
48 #include "log.h"
49 #include "prefs_common.h"
50 #include "alertpanel.h"
51
52 #ifdef HAVE_SYSEXITS_H
53 #include <sysexits.h>
54 #endif
55 #ifdef HAVE_ERRNO_H
56 #include <errno.h>
57 #endif
58 #ifdef HAVE_SYS_ERRNO_H
59 #include <sys/errno.h>
60 #endif
61 #ifdef HAVE_TIME_H
62 #include <time.h>
63 #endif
64 #ifdef HAVE_SYS_TIME_H
65 #include <sys/time.h>
66 #endif
67 #ifdef HAVE_SIGNAL_H
68 #include <signal.h>
69 #endif
70 #ifdef HAVE_PWD_H
71 #include <pwd.h>
72 #endif
73
74 enum {
75     CHILD_RUNNING = 1 << 0,
76     TIMEOUT_RUNNING = 1 << 1,
77 };
78
79 static guint hook_id = -1;
80 static MessageCallback message_callback;
81
82 static BogofilterConfig config;
83
84 static PrefParam param[] = {
85         {"process_emails", "TRUE", &config.process_emails, P_BOOL,
86          NULL, NULL, NULL},
87         {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
88          NULL, NULL, NULL},
89         {"save_folder", NULL, &config.save_folder, P_STRING,
90          NULL, NULL, NULL},
91         {"max_size", "250", &config.max_size, P_INT,
92          NULL, NULL, NULL},
93
94         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
95 };
96
97 static gboolean mail_filtering_hook(gpointer source, gpointer data)
98 {
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;
104         int status = 3;
105
106         if (!config.process_emails) {
107                 return FALSE;
108         }
109         debug_print("Filtering message %d\n", msginfo->msgnum);
110         if (message_callback != NULL)
111                 message_callback(_("Bogofilter: filtering message..."), 0, 0);
112
113         file = procmsg_get_message_file(msginfo);
114
115         if (file)
116                 cmd = g_strdup_printf("bogofilter -I %s", file);
117         
118         if (cmd)
119                 status = system(cmd);
120         
121         if (status == -1)
122                 status = 3;
123         else 
124                 status = WEXITSTATUS(status);
125
126         g_free(cmd);
127         g_free(file);
128         printf("bogofilter status %d\n", status);
129         is_spam = (status == 0);
130         
131         if (is_spam) {
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;
136
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();
141
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);
145                 } else {
146                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
147                 }
148
149                 return TRUE;
150         } else {
151                 debug_print("message is ham\n");
152                 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
153         }
154         
155         if (status == 3) { /* I/O or other errors */
156                 if (!warned_error) {
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."));
163                 }
164                 warned_error = TRUE;
165         }
166         
167         return FALSE;
168 }
169
170 BogofilterConfig *bogofilter_get_config(void)
171 {
172         return &config;
173 }
174
175 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
176 {
177         gchar *cmd = NULL;
178         gchar *file = NULL;
179
180         if (msginfo == NULL && msglist == NULL) {
181                 return -1;
182         }
183
184         if (msginfo) {
185                 file = procmsg_get_message_file(msginfo);
186                 if (file == NULL) {
187                         return -1;
188                 } else {
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);
194                         g_free(cmd);
195                         g_free(file);
196                         if (message_callback != NULL)
197                                 message_callback(NULL, 0, 0);
198                         return 0;
199                 }
200         }
201         if (msglist) {
202                 GSList *cur = msglist;
203                 MsgInfo *info;
204                 int total = g_slist_length(msglist);
205                 int done = 0;
206                 if (message_callback != NULL)
207                         message_callback(_("Bogofilter: learning from messages..."), total, 0);
208                 
209                 for (; cur; cur = cur->next) {
210                         info = (MsgInfo *)cur->data;
211                         file = procmsg_get_message_file(info);
212
213                         cmd = g_strdup_printf("bogofilter -%c -I %s",
214                                                         spam ? 's':'n', file);
215                         execute_command_line(cmd, FALSE);
216                         g_free(cmd);
217                         g_free(file);
218                         done++;
219                         if (message_callback != NULL)
220                                 message_callback(NULL, total, done);
221                 }
222                 if (message_callback != NULL)
223                         message_callback(NULL, 0, 0);
224                 return 0;
225         }
226         return -1;
227 }
228
229 void bogofilter_save_config(void)
230 {
231         PrefFile *pfile;
232         gchar *rcpath;
233
234         debug_print("Saving Bogofilter Page\n");
235
236         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
237         pfile = prefs_write_open(rcpath);
238         g_free(rcpath);
239         if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
240                 return;
241
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);
245                 return;
246         }
247         fprintf(pfile->fp, "\n");
248
249         prefs_file_close(pfile);
250 }
251
252 void bogofilter_set_message_callback(MessageCallback callback)
253 {
254         message_callback = callback;
255 }
256
257 gint plugin_init(gchar **error)
258 {
259         gchar *rcpath;
260
261         hook_id = -1;
262
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");
265                 return -1;
266         }
267
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");
270                 return -1;
271         }
272
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);
276         g_free(rcpath);
277
278         bogofilter_gtk_init();
279                 
280         debug_print("Bogofilter plugin loaded\n");
281
282         if (config.process_emails) {
283                 bogofilter_register_hook();
284         }
285
286         procmsg_register_spam_learner(bogofilter_learn);
287         procmsg_spam_set_folder(config.save_folder);
288
289         return 0;
290         
291 }
292
293 void plugin_done(void)
294 {
295         if (hook_id != -1) {
296                 bogofilter_unregister_hook();
297         }
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");
303 }
304
305 const gchar *plugin_name(void)
306 {
307         return _("Bogofilter");
308 }
309
310 const gchar *plugin_desc(void)
311 {
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 "
315                  "\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"
320                  "\n"
321                  "When a message is identified as spam it can be deleted or "
322                  "saved in a specially designated folder.\n"
323                  "\n"
324                  "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
325 }
326
327 const gchar *plugin_type(void)
328 {
329         return "GTK2";
330 }
331
332 const gchar *plugin_licence(void)
333 {
334         return "GPL";
335 }
336
337 const gchar *plugin_version(void)
338 {
339         return VERSION;
340 }
341
342 struct PluginFeature *plugin_provides(void)
343 {
344         static struct PluginFeature features[] = 
345                 { {PLUGIN_FILTERING, N_("Spam detection")},
346                   {PLUGIN_FILTERING, N_("Spam learning")},
347                   {PLUGIN_NOTHING, NULL}};
348         return features;
349 }
350
351 void bogofilter_register_hook(void)
352 {
353         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
354         if (hook_id == -1) {
355                 g_warning("Failed to register mail filtering hook");
356                 config.process_emails = FALSE;
357         }
358 }
359
360 void bogofilter_unregister_hook(void)
361 {
362         if (hook_id != -1) {
363                 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
364         }
365 }