2.5.0-rc2 released
[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         {"bogopath", "bogofilter", &config.bogopath, P_STRING,
94          NULL, NULL, NULL},
95
96         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
97 };
98
99 static gboolean mail_filtering_hook(gpointer source, gpointer data)
100 {
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;
106         int status = 3;
107         gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
108
109         if (!config.process_emails) {
110                 return FALSE;
111         }
112         debug_print("Filtering message %d\n", msginfo->msgnum);
113         if (message_callback != NULL)
114                 message_callback(_("Bogofilter: filtering message..."), 0, 0);
115
116         file = procmsg_get_message_file(msginfo);
117
118         if (file)
119                 cmd = g_strdup_printf("%s -I %s", bogo_exec, file);
120         
121         if (cmd)
122                 status = system(cmd);
123         
124         if (status == -1)
125                 status = 3;
126         else 
127                 status = WEXITSTATUS(status);
128
129         g_free(cmd);
130         g_free(file);
131         debug_print("bogofilter status %d\n", status);
132         is_spam = (status == 0);
133         
134         if (is_spam) {
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;
139
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();
144
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);
148                 } else {
149                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
150                 }
151
152                 return TRUE;
153         } else {
154                 debug_print("message is ham\n");
155                 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
156         }
157         
158         if (status == 3) { /* I/O or other errors */
159                 if (!warned_error) {
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."));
166                 }
167                 warned_error = TRUE;
168         }
169         
170         return FALSE;
171 }
172
173 BogofilterConfig *bogofilter_get_config(void)
174 {
175         return &config;
176 }
177
178 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
179 {
180         gchar *cmd = NULL;
181         gchar *file = NULL;
182         const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
183         gint status = 0;
184         if (msginfo == NULL && msglist == NULL) {
185                 return -1;
186         }
187
188         if (msginfo) {
189                 file = procmsg_get_message_file(msginfo);
190                 if (file == NULL) {
191                         return -1;
192                 } else {
193                         if (message_callback != NULL)
194                                 message_callback(_("Bogofilter: learning from message..."), 0, 0);
195                         if (spam)
196                                 /* learn as spam */
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);
201                         else 
202                                 /* learn as ham */
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."),
206                                                 cmd, status);
207                         g_free(cmd);
208                         g_free(file);
209                         if (message_callback != NULL)
210                                 message_callback(NULL, 0, 0);
211                         return 0;
212                 }
213         }
214         if (msglist) {
215                 GSList *cur = msglist;
216                 MsgInfo *info;
217                 int total = g_slist_length(msglist);
218                 int done = 0;
219                 if (message_callback != NULL)
220                         message_callback(_("Bogofilter: learning from messages..."), total, 0);
221                 
222                 for (; cur && status == 0; cur = cur->next) {
223                         info = (MsgInfo *)cur->data;
224                         file = procmsg_get_message_file(info);
225
226                         if (spam)
227                                 /* learn as spam */
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);
232                         else 
233                                 /* learn as ham */
234                                 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
235         
236                         if ((status = execute_command_line(cmd, FALSE)) != 0)
237                                 alertpanel_error(_("Learning failed; `%s` returned with status %d."),
238                                                 cmd, status);
239
240                         g_free(cmd);
241                         g_free(file);
242                         done++;
243                         if (message_callback != NULL)
244                                 message_callback(NULL, total, done);
245                 }
246                 if (message_callback != NULL)
247                         message_callback(NULL, 0, 0);
248                 return 0;
249         }
250         return -1;
251 }
252
253 void bogofilter_save_config(void)
254 {
255         PrefFile *pfile;
256         gchar *rcpath;
257
258         debug_print("Saving Bogofilter Page\n");
259
260         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
261         pfile = prefs_write_open(rcpath);
262         g_free(rcpath);
263         if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
264                 return;
265
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);
269                 return;
270         }
271         fprintf(pfile->fp, "\n");
272
273         prefs_file_close(pfile);
274 }
275
276 void bogofilter_set_message_callback(MessageCallback callback)
277 {
278         message_callback = callback;
279 }
280
281 gint plugin_init(gchar **error)
282 {
283         gchar *rcpath;
284
285         hook_id = -1;
286
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");
289                 return -1;
290         }
291
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");
294                 return -1;
295         }
296
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);
300         g_free(rcpath);
301
302         bogofilter_gtk_init();
303                 
304         debug_print("Bogofilter plugin loaded\n");
305
306         if (config.process_emails) {
307                 bogofilter_register_hook();
308         }
309
310         procmsg_register_spam_learner(bogofilter_learn);
311         procmsg_spam_set_folder(config.save_folder);
312
313         return 0;
314         
315 }
316
317 void plugin_done(void)
318 {
319         if (hook_id != -1) {
320                 bogofilter_unregister_hook();
321         }
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");
327 }
328
329 const gchar *plugin_name(void)
330 {
331         return _("Bogofilter");
332 }
333
334 const gchar *plugin_desc(void)
335 {
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 "
339                  "\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"
344                  "\n"
345                  "When a message is identified as spam it can be deleted or "
346                  "saved in a specially designated folder.\n"
347                  "\n"
348                  "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
349 }
350
351 const gchar *plugin_type(void)
352 {
353         return "GTK2";
354 }
355
356 const gchar *plugin_licence(void)
357 {
358         return "GPL";
359 }
360
361 const gchar *plugin_version(void)
362 {
363         return VERSION;
364 }
365
366 struct PluginFeature *plugin_provides(void)
367 {
368         static struct PluginFeature features[] = 
369                 { {PLUGIN_FILTERING, N_("Spam detection")},
370                   {PLUGIN_FILTERING, N_("Spam learning")},
371                   {PLUGIN_NOTHING, NULL}};
372         return features;
373 }
374
375 void bogofilter_register_hook(void)
376 {
377         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
378         if (hook_id == -1) {
379                 g_warning("Failed to register mail filtering hook");
380                 config.process_emails = FALSE;
381         }
382 }
383
384 void bogofilter_unregister_hook(void)
385 {
386         if (hook_id != -1) {
387                 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
388         }
389 }