2006-08-30 [colin] 2.4.0cvs113
[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                 gchar *msg = _("The Bogofilter plugin couldn't filter "
160                                            "a message. The probable cause of the "
161                                            "error is that it didn't learn from any mail.\n"
162                                            "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
163                                            "ham\" to train Bogofilter with a few hundred "
164                                            "spam and ham messages.");
165                 if (!prefs_common.no_recv_err_panel) {
166                         if (!warned_error) {
167                                 alertpanel_error(msg);
168                         }
169                         warned_error = TRUE;
170                 } else {
171                         gchar *tmp = g_strdup_printf("%s\n", msg);
172                         log_error(tmp);
173                         g_free(tmp);
174                 }
175         }
176         
177         return FALSE;
178 }
179
180 BogofilterConfig *bogofilter_get_config(void)
181 {
182         return &config;
183 }
184
185 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
186 {
187         gchar *cmd = NULL;
188         gchar *file = NULL;
189         const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
190         gint status = 0;
191         if (msginfo == NULL && msglist == NULL) {
192                 return -1;
193         }
194
195         if (msginfo) {
196                 file = procmsg_get_message_file(msginfo);
197                 if (file == NULL) {
198                         return -1;
199                 } else {
200                         if (message_callback != NULL)
201                                 message_callback(_("Bogofilter: learning from message..."), 0, 0);
202                         if (spam)
203                                 /* learn as spam */
204                                 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
205                         else if (MSG_IS_SPAM(msginfo->flags))
206                                 /* correct bogofilter, this wasn't spam */
207                                 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
208                         else 
209                                 /* learn as ham */
210                                 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
211                         if ((status = execute_command_line(cmd, FALSE)) != 0)
212                                 alertpanel_error(_("Learning failed; `%s` returned with status %d."),
213                                                 cmd, status);
214                         g_free(cmd);
215                         g_free(file);
216                         if (message_callback != NULL)
217                                 message_callback(NULL, 0, 0);
218                         return 0;
219                 }
220         }
221         if (msglist) {
222                 GSList *cur = msglist;
223                 MsgInfo *info;
224                 int total = g_slist_length(msglist);
225                 int done = 0;
226                 gboolean some_correction = FALSE, some_no_correction = FALSE;
227         
228                 if (message_callback != NULL)
229                         message_callback(_("Bogofilter: learning from messages..."), total, 0);
230                 
231                 for (cur = msglist; cur && status == 0; cur = cur->next) {
232                         info = (MsgInfo *)cur->data;
233                         if (spam)
234                                 some_no_correction = TRUE;
235                         else if (MSG_IS_SPAM(info->flags))
236                                 /* correct bogofilter, this wasn't spam */
237                                 some_correction = TRUE;
238                         else 
239                                 some_no_correction = TRUE;
240                         
241                 }
242                 
243                 if (some_correction && some_no_correction) {
244                         /* we potentially have to do different stuff for every mail */
245                         for (cur = msglist; cur && status == 0; cur = cur->next) {
246                                 info = (MsgInfo *)cur->data;
247                                 file = procmsg_get_message_file(info);
248
249                                 if (spam)
250                                         /* learn as spam */
251                                         cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
252                                 else if (MSG_IS_SPAM(info->flags))
253                                         /* correct bogofilter, this wasn't spam */
254                                         cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
255                                 else 
256                                         /* learn as ham */
257                                         cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
258
259                                 if ((status = execute_command_line(cmd, FALSE)) != 0)
260                                         alertpanel_error(_("Learning failed; `%s` returned with status %d."),
261                                                         cmd, status);
262
263                                 g_free(cmd);
264                                 g_free(file);
265                                 done++;
266                                 if (message_callback != NULL)
267                                         message_callback(NULL, total, done);
268                         }
269                 } else if (some_correction || some_no_correction) {
270                         int count = 0;
271                         gchar *file_list = NULL;
272                         cur = msglist;
273                         
274                         while (cur && status == 0) {
275                                 gchar *tmp = NULL;
276                                 info = (MsgInfo *)cur->data;
277                                 file = procmsg_get_message_file(info);
278                                 if (file) {
279                                         tmp = g_strdup_printf("%s%s'%s'", 
280                                                 file_list?file_list:"",
281                                                 file_list?" ":"",
282                                                 file);
283                                         g_free(file_list);
284                                         file_list = tmp;
285                                 }
286                                 g_free(file);
287                                 count ++;
288                                 done++;
289                                 if (count > 10 || cur->next == NULL) {
290                                         /* flush */
291                                         if (some_correction && !some_no_correction)
292                                                 cmd = g_strdup_printf("%s -Sn -B %s", bogo_exec, file_list);
293                                         else if (some_no_correction && !some_correction)
294                                                 cmd = g_strdup_printf("%s -%c -B %s", bogo_exec, spam?'s':'n', file_list);
295                                         else
296                                                 g_warning("duuh bogofilter plugin shouldn't be there!\n");
297                                         if ((status = execute_command_line(cmd, FALSE)) != 0)
298                                                 alertpanel_error(_("Learning failed; `%s` returned with status %d."),
299                                                                 cmd, status);
300                                         count = 0;
301                                         g_free(cmd);
302                                         g_free(file_list);
303                                         file_list = NULL;
304                                 }
305                                 if (message_callback != NULL)
306                                         message_callback(NULL, total, done);
307                                 cur = cur->next;
308                         }
309                         g_free(file_list);
310                 }
311
312                 if (message_callback != NULL)
313                         message_callback(NULL, 0, 0);
314                 return 0;
315         }
316         return -1;
317 }
318
319 void bogofilter_save_config(void)
320 {
321         PrefFile *pfile;
322         gchar *rcpath;
323
324         debug_print("Saving Bogofilter Page\n");
325
326         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
327         pfile = prefs_write_open(rcpath);
328         g_free(rcpath);
329         if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
330                 return;
331
332         if (prefs_write_param(param, pfile->fp) < 0) {
333                 g_warning("Failed to write Bogofilter configuration to file\n");
334                 prefs_file_close_revert(pfile);
335                 return;
336         }
337         fprintf(pfile->fp, "\n");
338
339         prefs_file_close(pfile);
340 }
341
342 void bogofilter_set_message_callback(MessageCallback callback)
343 {
344         message_callback = callback;
345 }
346
347 gint plugin_init(gchar **error)
348 {
349         gchar *rcpath;
350
351         hook_id = -1;
352
353         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
354                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the Bogofilter plugin was built with");
355                 return -1;
356         }
357
358         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
359                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the Bogofilter plugin");
360                 return -1;
361         }
362
363         prefs_set_default(param);
364         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
365         prefs_read_config(param, "Bogofilter", rcpath, NULL);
366         g_free(rcpath);
367
368         bogofilter_gtk_init();
369                 
370         debug_print("Bogofilter plugin loaded\n");
371
372         if (config.process_emails) {
373                 bogofilter_register_hook();
374         }
375
376         procmsg_register_spam_learner(bogofilter_learn);
377         procmsg_spam_set_folder(config.save_folder);
378
379         return 0;
380         
381 }
382
383 void plugin_done(void)
384 {
385         if (hook_id != -1) {
386                 bogofilter_unregister_hook();
387         }
388         g_free(config.save_folder);
389         bogofilter_gtk_done();
390         procmsg_unregister_spam_learner(bogofilter_learn);
391         procmsg_spam_set_folder(NULL);
392         debug_print("Bogofilter plugin unloaded\n");
393 }
394
395 const gchar *plugin_name(void)
396 {
397         return _("Bogofilter");
398 }
399
400 const gchar *plugin_desc(void)
401 {
402         return _("This plugin can check all messages that are received from an "
403                  "IMAP, LOCAL or POP account for spam using Bogofilter. "
404                  "You will need Bogofilter installed locally.\n "
405                  "\n"
406                  "Before Bogofilter can recognize spam messages, you have to "
407                  "train it by marking a few hundred spam and ham messages. "
408                  "Use \"/Mark/Mark as Spam\" and \"/Mark/Mark as ham\" to "
409                  "train Bogofilter.\n"
410                  "\n"
411                  "When a message is identified as spam it can be deleted or "
412                  "saved in a specially designated folder.\n"
413                  "\n"
414                  "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
415 }
416
417 const gchar *plugin_type(void)
418 {
419         return "GTK2";
420 }
421
422 const gchar *plugin_licence(void)
423 {
424         return "GPL";
425 }
426
427 const gchar *plugin_version(void)
428 {
429         return VERSION;
430 }
431
432 struct PluginFeature *plugin_provides(void)
433 {
434         static struct PluginFeature features[] = 
435                 { {PLUGIN_FILTERING, N_("Spam detection")},
436                   {PLUGIN_FILTERING, N_("Spam learning")},
437                   {PLUGIN_NOTHING, NULL}};
438         return features;
439 }
440
441 void bogofilter_register_hook(void)
442 {
443         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
444         if (hook_id == -1) {
445                 g_warning("Failed to register mail filtering hook");
446                 config.process_emails = FALSE;
447         }
448 }
449
450 void bogofilter_unregister_hook(void)
451 {
452         if (hook_id != -1) {
453                 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
454         }
455 }