2e422f1a1803703a44e577651986f2e653bbef5e
[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 Claws Mail 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 #include <errno.h>
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32
33 #if HAVE_LOCALE_H
34 #  include <locale.h>
35 #endif
36
37 #include "common/claws.h"
38 #include "common/version.h"
39 #include "plugin.h"
40 #include "common/utils.h"
41 #include "hooks.h"
42 #include "procmsg.h"
43 #include "folder.h"
44 #include "prefs.h"
45 #include "prefs_gtk.h"
46
47 #include "bogofilter.h"
48 #include "inc.h"
49 #include "log.h"
50 #include "prefs_common.h"
51 #include "alertpanel.h"
52
53 #ifdef HAVE_SYSEXITS_H
54 #include <sysexits.h>
55 #endif
56 #ifdef HAVE_ERRNO_H
57 #include <errno.h>
58 #endif
59 #ifdef HAVE_SYS_ERRNO_H
60 #include <sys/errno.h>
61 #endif
62 #ifdef HAVE_TIME_H
63 #include <time.h>
64 #endif
65 #ifdef HAVE_SYS_TIME_H
66 #include <sys/time.h>
67 #endif
68 #ifdef HAVE_SIGNAL_H
69 #include <signal.h>
70 #endif
71 #ifdef HAVE_PWD_H
72 #include <pwd.h>
73 #endif
74
75 static guint hook_id = -1;
76 static MessageCallback message_callback;
77
78 static BogofilterConfig config;
79
80 static PrefParam param[] = {
81         {"process_emails", "TRUE", &config.process_emails, P_BOOL,
82          NULL, NULL, NULL},
83         {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
84          NULL, NULL, NULL},
85         {"save_folder", NULL, &config.save_folder, P_STRING,
86          NULL, NULL, NULL},
87         {"max_size", "250", &config.max_size, P_INT,
88          NULL, NULL, NULL},
89         {"bogopath", "bogofilter", &config.bogopath, P_STRING,
90          NULL, NULL, NULL},
91
92         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
93 };
94
95 /*
96  * Helper function for spawn_with_input() - write an entire
97  * string to a fd.
98  */
99 static gboolean
100 write_all (int         fd,
101            const char *buf,
102            gsize       to_write)
103 {
104   while (to_write > 0)
105     {
106       gssize count = write (fd, buf, to_write);
107       if (count < 0)
108         {
109           if (errno != EINTR)
110             return FALSE;
111         }
112       else
113         {
114           to_write -= count;
115           buf += count;
116         }
117     }
118
119   return TRUE;
120 }
121
122 typedef struct _BogoFilterData {
123         MailFilteringData *mail_filtering_data;
124         gchar **bogo_args;
125         GSList *msglist;
126         GSList *new_hams;
127         GSList *new_spams;
128         GSList *spams_to_receive;
129         gboolean done;
130         int status;
131         gboolean in_thread;
132 } BogoFilterData;
133
134 static BogoFilterData *to_filter_data = NULL;
135 #ifdef USE_PTHREAD
136 static gboolean filter_th_done = FALSE;
137 static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
138 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER; 
139 static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER; 
140 #endif
141
142 static void bogofilter_do_filter(BogoFilterData *data)
143 {
144         GPid bogo_pid;
145         gint bogo_stdin, bogo_stdout;
146         GError *error = NULL;
147         gboolean bogo_forked;
148         int status = 0;
149         MsgInfo *msginfo;
150         GSList *cur = NULL;
151         int total = 0, curnum = 0;
152         gchar *file = NULL;
153         gchar buf[BUFSIZ];
154
155         total = g_slist_length(data->msglist);
156
157         bogo_forked = g_spawn_async_with_pipes(
158                         NULL, data->bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
159                         NULL, NULL, &bogo_pid, &bogo_stdin,
160                         &bogo_stdout, NULL, &error);
161                 
162         if (bogo_forked == FALSE) {
163                 g_warning("%s\n", error ? error->message:"ERROR???");
164                 g_error_free(error);
165                 error = NULL;
166                 status = -1;
167         } else {
168                 for (cur = data->msglist; cur; cur = cur->next) {
169                         msginfo = (MsgInfo *)cur->data;
170                         debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
171
172                         if (message_callback != NULL)
173                                 message_callback(NULL, total, curnum++, data->in_thread);
174
175                         /* can set flags (SCANNED, ATTACHMENT) but that's ok 
176                          * as GUI updates are hooked not direct */
177                         file = procmsg_get_message_file(msginfo);
178
179                         if (file) {
180                                 gchar *tmp = g_strdup_printf("%s\n",file);
181                                 write_all(bogo_stdin, tmp, strlen(tmp));
182                                 g_free(tmp);
183                                 memset(buf, 0, sizeof(buf));
184                                 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
185                                         g_warning("bogofilter short read\n");
186                                         debug_print("message %d is ham\n", msginfo->msgnum);
187                                         data->mail_filtering_data->unfiltered = g_slist_prepend(
188                                                 data->mail_filtering_data->unfiltered, msginfo);
189                                         data->new_hams = g_slist_prepend(data->new_hams, msginfo);
190                                 } else {
191                                         gchar **parts = NULL;
192                                         if (strchr(buf, '/')) {
193                                                 tmp = strrchr(buf, '/')+1;
194                                         } else {
195                                                 tmp = buf;
196                                         }
197                                         parts = g_strsplit(tmp, " ", 0);
198                                         debug_print("read %s\n", buf);
199                                         if (parts && parts[0] && parts[1] && *parts[1] == 'S') {
200                                                 debug_print("message %d is spam\n", msginfo->msgnum);
201                                                 if (config.receive_spam) {
202                                                         data->spams_to_receive = g_slist_prepend(data->spams_to_receive, msginfo);
203                                                 } 
204
205                                                 data->mail_filtering_data->filtered = g_slist_prepend(
206                                                         data->mail_filtering_data->filtered, msginfo);
207                                                 data->new_spams = g_slist_prepend(data->new_spams, msginfo);
208                                         } else {
209                                                 debug_print("message %d is ham\n", msginfo->msgnum);
210                                                 data->mail_filtering_data->unfiltered = g_slist_prepend(
211                                                         data->mail_filtering_data->unfiltered, msginfo);
212                                                 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
213                                         }
214                                         g_strfreev(parts);
215                                 }
216                                 g_free(file);
217                         } else {
218                                 data->mail_filtering_data->unfiltered = g_slist_prepend(
219                                         data->mail_filtering_data->unfiltered, msginfo);
220                                 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
221                         }
222                 }
223         }
224         if (status != -1) {
225                 close(bogo_stdout);
226                 close(bogo_stdin);
227                 waitpid(bogo_pid, &status, 0);
228                 if (!WIFEXITED(status))
229                         status = -1;
230                 else
231                         status = WEXITSTATUS(status);
232         }
233         
234         to_filter_data->status = status; 
235 }
236
237 #ifdef USE_PTHREAD
238 static void *bogofilter_filtering_thread(void *data) 
239 {
240         while (!filter_th_done) {
241                 pthread_mutex_lock(&list_mutex);
242                 if (to_filter_data == NULL || to_filter_data->done == TRUE) {
243                         pthread_mutex_unlock(&list_mutex);
244                         debug_print("thread is waiting for something to filter\n");
245                         pthread_mutex_lock(&wait_mutex);
246                         pthread_cond_wait(&wait_cond, &wait_mutex);
247                         pthread_mutex_unlock(&wait_mutex);
248                 } else {
249                         debug_print("thread awaken with something to filter\n");
250                         to_filter_data->done = FALSE;
251                         bogofilter_do_filter(to_filter_data);
252                         pthread_mutex_unlock(&list_mutex);
253                         to_filter_data->done = TRUE;
254                         usleep(100);
255                 }
256         }
257         return NULL;
258 }
259
260 static pthread_t filter_th = 0;
261
262 static void bogofilter_start_thread(void)
263 {
264         filter_th_done = FALSE;
265         if (filter_th != 0 || 1)
266                 return;
267         if (pthread_create(&filter_th, 0, 
268                         bogofilter_filtering_thread, 
269                         NULL) != 0) {
270                 filter_th = 0;
271                 return;
272         }
273         debug_print("thread created\n");
274 }
275
276 static void bogofilter_stop_thread(void)
277 {
278         void *res;
279         while (pthread_mutex_trylock(&list_mutex) != 0) {
280                 GTK_EVENTS_FLUSH();
281                 usleep(100);
282         }
283         if (filter_th != 0) {
284                 filter_th_done = TRUE;
285                 debug_print("waking thread up\n");
286                 pthread_mutex_lock(&wait_mutex);
287                 pthread_cond_broadcast(&wait_cond);
288                 pthread_mutex_unlock(&wait_mutex);
289                 pthread_join(filter_th, &res);
290                 filter_th = 0;
291         }
292         pthread_mutex_unlock(&list_mutex);
293         debug_print("thread done\n");
294 }
295 #endif
296
297 static gboolean mail_filtering_hook(gpointer source, gpointer data)
298 {
299         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
300         MsgInfo *msginfo = mail_filtering_data->msginfo;
301         GSList *msglist = mail_filtering_data->msglist;
302         GSList *cur = NULL;
303         static gboolean warned_error = FALSE;
304         int status = 0;
305         int total = 0, curnum = 0;
306         GSList *spams_to_receive = NULL, *new_hams = NULL, *new_spams = NULL;
307         gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
308         gchar *bogo_args[4];
309         gboolean ok_to_thread = TRUE;
310
311         bogo_args[0] = bogo_exec;
312         bogo_args[1] = "-T";
313         bogo_args[2] = "-b";
314         bogo_args[3] = NULL;
315         
316         if (!config.process_emails) {
317                 return FALSE;
318         }
319         
320         if (msglist == NULL && msginfo != NULL) {
321                 g_warning("wrong call to bogofilter mail_filtering_hook");
322                 return FALSE;
323         }
324         
325         total = g_slist_length(msglist);
326         
327         /* we have to make sure the mails are cached - or it'll break on IMAP */
328         if (message_callback != NULL)
329                 message_callback(_("Bogofilter: fetching bodies..."), total, 0, FALSE);
330         for (cur = msglist; cur; cur = cur->next) {
331                 gchar *file = procmsg_get_message_file((MsgInfo *)cur->data);
332                 if (file == NULL)
333                         ok_to_thread = FALSE;
334                 if (message_callback != NULL)
335                         message_callback(NULL, total, curnum++, FALSE);
336                 g_free(file);
337         }
338         if (message_callback != NULL)
339                 message_callback(NULL, 0, 0, FALSE);
340
341         if (message_callback != NULL)
342                 message_callback(_("Bogofilter: filtering messages..."), total, 0, FALSE);
343
344 #ifdef USE_PTHREAD
345         while (pthread_mutex_trylock(&list_mutex) != 0) {
346                 GTK_EVENTS_FLUSH();
347                 usleep(100);
348         }
349 #endif
350         to_filter_data = g_new0(BogoFilterData, 1);
351         to_filter_data->msglist = msglist;
352         to_filter_data->mail_filtering_data = mail_filtering_data;
353         to_filter_data->spams_to_receive = NULL;
354         to_filter_data->new_hams = NULL;
355         to_filter_data->new_spams = NULL;
356         to_filter_data->done = FALSE;
357         to_filter_data->status = -1;
358         to_filter_data->bogo_args = bogo_args;
359 #ifdef USE_PTHREAD
360         to_filter_data->in_thread = (filter_th != 0 && ok_to_thread);
361 #else
362         to_filter_data->in_thread = FALSE;
363 #endif
364
365 #ifdef USE_PTHREAD
366         pthread_mutex_unlock(&list_mutex);
367         
368         if (filter_th != 0 && ok_to_thread) {
369                 debug_print("waking thread to let it filter things\n");
370                 pthread_mutex_lock(&wait_mutex);
371                 pthread_cond_broadcast(&wait_cond);
372                 pthread_mutex_unlock(&wait_mutex);
373
374                 while (!to_filter_data->done) {
375                         GTK_EVENTS_FLUSH();
376                         usleep(100);
377                 }
378         }
379
380         while (pthread_mutex_trylock(&list_mutex) != 0) {
381                 GTK_EVENTS_FLUSH();
382                 usleep(100);
383
384         }
385         if (filter_th == 0 || !ok_to_thread)
386                 bogofilter_do_filter(to_filter_data);
387 #else
388         bogofilter_do_filter(to_filter_data);   
389 #endif
390
391         spams_to_receive = to_filter_data->spams_to_receive;
392         new_hams = to_filter_data->new_hams;
393         new_spams = to_filter_data->new_spams;
394         status = to_filter_data->status;
395         g_free(to_filter_data);
396         to_filter_data = NULL;
397 #ifdef USE_PTHREAD
398         pthread_mutex_unlock(&list_mutex);
399 #endif
400
401
402         /* flag hams */
403         for (cur = new_hams; cur; cur = cur->next) {
404                 MsgInfo *msginfo = (MsgInfo *)cur->data;
405                 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
406         }
407         g_slist_free(new_hams);
408         /* flag spams */
409         for (cur = new_spams; cur; cur = cur->next) {
410                 MsgInfo *msginfo = (MsgInfo *)cur->data;
411                 if (config.receive_spam) {
412                         procmsg_msginfo_change_flags(msginfo, MSG_SPAM, 0, ~0, 0);
413                 } else {
414                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
415                 }
416         }
417         g_slist_free(new_spams);
418         
419         if (status < 0 || status > 2) { /* I/O or other errors */
420                 gchar *msg = NULL;
421                 
422                 if (status == 3)
423                         msg =  g_strdup_printf(_("The Bogofilter plugin couldn't filter "
424                                            "a message. The probable cause of the "
425                                            "error is that it didn't learn from any mail.\n"
426                                            "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
427                                            "ham\" to train Bogofilter with a few hundred "
428                                            "spam and ham messages."));
429                 else
430                         msg =  g_strdup_printf(_("The Bogofilter plugin couldn't filter "
431                                            "a message. the command `%s %s %s` couldn't be run."), 
432                                            bogo_args[0], bogo_args[1], bogo_args[2]);
433                 if (!prefs_common.no_recv_err_panel) {
434                         if (!warned_error) {
435                                 alertpanel_error(msg);
436                         }
437                         warned_error = TRUE;
438                 } else {
439                         gchar *tmp = g_strdup_printf("%s\n", msg);
440                         log_error(tmp);
441                         g_free(tmp);
442                 }
443                 g_free(msg);
444         }
445         if (status < 0 || status > 2) {
446                 g_slist_free(mail_filtering_data->filtered);
447                 g_slist_free(mail_filtering_data->unfiltered);
448                 g_slist_free(spams_to_receive);
449                 mail_filtering_data->filtered = NULL;
450                 mail_filtering_data->unfiltered = NULL;
451         } else if (config.receive_spam && spams_to_receive) {
452                 FolderItem *save_folder;
453
454                 if ((!config.save_folder) ||
455                     (config.save_folder[0] == '\0') ||
456                     ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
457                         save_folder = folder_get_default_trash();
458                 if (save_folder) {
459                         for (cur = spams_to_receive; cur; cur = cur->next) {
460                                 msginfo = (MsgInfo *)cur->data;
461                                 msginfo->is_move = TRUE;
462                                 msginfo->to_filter_folder = save_folder;
463                         }
464                 }
465         } 
466
467         if (message_callback != NULL)
468                 message_callback(NULL, 0, 0, FALSE);
469         mail_filtering_data->filtered = g_slist_reverse(
470                 mail_filtering_data->filtered);
471         mail_filtering_data->unfiltered = g_slist_reverse(
472                 mail_filtering_data->unfiltered);
473         
474         return FALSE;
475 }
476
477 BogofilterConfig *bogofilter_get_config(void)
478 {
479         return &config;
480 }
481
482 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
483 {
484         gchar *cmd = NULL;
485         gchar *file = NULL;
486         const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
487         gint status = 0;
488         if (msginfo == NULL && msglist == NULL) {
489                 return -1;
490         }
491
492         if (msginfo) {
493                 file = procmsg_get_message_file(msginfo);
494                 if (file == NULL) {
495                         return -1;
496                 } else {
497                         if (message_callback != NULL)
498                                 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE);
499                         if (spam)
500                                 /* learn as spam */
501                                 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
502                         else if (MSG_IS_SPAM(msginfo->flags))
503                                 /* correct bogofilter, this wasn't spam */
504                                 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
505                         else 
506                                 /* learn as ham */
507                                 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
508                         if ((status = execute_command_line(cmd, FALSE)) != 0)
509                                 log_error(_("Learning failed; `%s` returned with status %d."),
510                                                 cmd, status);
511                         g_free(cmd);
512                         g_free(file);
513                         if (message_callback != NULL)
514                                 message_callback(NULL, 0, 0, FALSE);
515                         return 0;
516                 }
517         }
518         if (msglist) {
519                 GSList *cur = msglist;
520                 MsgInfo *info;
521                 int total = g_slist_length(msglist);
522                 int done = 0;
523                 gboolean some_correction = FALSE, some_no_correction = FALSE;
524         
525                 if (message_callback != NULL)
526                         message_callback(_("Bogofilter: learning from messages..."), total, 0, FALSE);
527                 
528                 for (cur = msglist; cur && status == 0; cur = cur->next) {
529                         info = (MsgInfo *)cur->data;
530                         if (spam)
531                                 some_no_correction = TRUE;
532                         else if (MSG_IS_SPAM(info->flags))
533                                 /* correct bogofilter, this wasn't spam */
534                                 some_correction = TRUE;
535                         else 
536                                 some_no_correction = TRUE;
537                         
538                 }
539                 
540                 if (some_correction && some_no_correction) {
541                         /* we potentially have to do different stuff for every mail */
542                         for (cur = msglist; cur && status == 0; cur = cur->next) {
543                                 info = (MsgInfo *)cur->data;
544                                 file = procmsg_get_message_file(info);
545
546                                 if (spam)
547                                         /* learn as spam */
548                                         cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
549                                 else if (MSG_IS_SPAM(info->flags))
550                                         /* correct bogofilter, this wasn't spam */
551                                         cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
552                                 else 
553                                         /* learn as ham */
554                                         cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
555
556                                 if ((status = execute_command_line(cmd, FALSE)) != 0)
557                                         log_error(_("Learning failed; `%s` returned with status %d."),
558                                                         cmd, status);
559
560                                 g_free(cmd);
561                                 g_free(file);
562                                 done++;
563                                 if (message_callback != NULL)
564                                         message_callback(NULL, total, done, FALSE);
565                         }
566                 } else if (some_correction || some_no_correction) {
567                         cur = msglist;
568                         
569                         gchar *bogo_args[4];
570                         GPid bogo_pid;
571                         gint bogo_stdin;
572                         GError *error = NULL;
573                         gboolean bogo_forked;
574
575                         bogo_args[0] = (gchar *)bogo_exec;
576                         if (some_correction && !some_no_correction)
577                                 bogo_args[1] = "-Sn";
578                         else if (some_no_correction && !some_correction)
579                                 bogo_args[1] = spam ? "-s":"-n";
580                         bogo_args[2] = "-b";
581                         bogo_args[3] = NULL;
582
583                         bogo_forked = g_spawn_async_with_pipes(
584                                         NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
585                                         NULL, NULL, &bogo_pid, &bogo_stdin,
586                                         NULL, NULL, &error);
587
588                         while (bogo_forked && cur) {
589                                 gchar *tmp = NULL;
590                                 info = (MsgInfo *)cur->data;
591                                 file = procmsg_get_message_file(info);
592                                 if (file) {
593                                         tmp = g_strdup_printf("%s\n", 
594                                                 file);
595                                         write_all(bogo_stdin, tmp, strlen(tmp));
596                                         g_free(tmp);
597                                 }
598                                 g_free(file);
599                                 done++;
600                                 if (message_callback != NULL)
601                                         message_callback(NULL, total, done, FALSE);
602                                 cur = cur->next;
603                         }
604                         if (bogo_forked) {
605                                 close(bogo_stdin);
606                                 waitpid(bogo_pid, &status, 0);
607                                 if (!WIFEXITED(status))
608                                         status = -1;
609                                 else
610                                         status = WEXITSTATUS(status);
611                         }
612                         if (!bogo_forked || status != 0) {
613                                 log_error(_("Learning failed; `%s %s %s` returned with error:\n%s"),
614                                                 bogo_args[0], bogo_args[1], bogo_args[2], 
615                                                 error ? error->message:_("Unknown error"));
616                                 if (error)
617                                         g_error_free(error);
618                         }
619
620                 }
621
622                 if (message_callback != NULL)
623                         message_callback(NULL, 0, 0, FALSE);
624                 return 0;
625         }
626         return -1;
627 }
628
629 void bogofilter_save_config(void)
630 {
631         PrefFile *pfile;
632         gchar *rcpath;
633
634         debug_print("Saving Bogofilter Page\n");
635
636         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
637         pfile = prefs_write_open(rcpath);
638         g_free(rcpath);
639         if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
640                 return;
641
642         if (prefs_write_param(param, pfile->fp) < 0) {
643                 g_warning("Failed to write Bogofilter configuration to file\n");
644                 prefs_file_close_revert(pfile);
645                 return;
646         }
647         fprintf(pfile->fp, "\n");
648
649         prefs_file_close(pfile);
650 }
651
652 void bogofilter_set_message_callback(MessageCallback callback)
653 {
654         message_callback = callback;
655 }
656
657 gint plugin_init(gchar **error)
658 {
659         gchar *rcpath;
660
661         hook_id = -1;
662
663         if ((claws_get_version() > VERSION_NUMERIC)) {
664                 *error = g_strdup(_("Your version of Claws Mail is newer than the version the Bogofilter plugin was built with"));
665                 return -1;
666         }
667
668         if ((claws_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
669                 *error = g_strdup(_("Your version of Claws Mail is too old for the Bogofilter plugin"));
670                 return -1;
671         }
672
673         prefs_set_default(param);
674         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
675         prefs_read_config(param, "Bogofilter", rcpath, NULL);
676         g_free(rcpath);
677
678         bogofilter_gtk_init();
679                 
680         debug_print("Bogofilter plugin loaded\n");
681
682 #ifdef USE_PTHREAD
683         bogofilter_start_thread();
684 #endif
685
686         if (config.process_emails) {
687                 bogofilter_register_hook();
688         }
689
690         procmsg_register_spam_learner(bogofilter_learn);
691         procmsg_spam_set_folder(config.save_folder);
692
693         return 0;
694         
695 }
696
697 void plugin_done(void)
698 {
699         if (hook_id != -1) {
700                 bogofilter_unregister_hook();
701         }
702 #ifdef USE_PTHREAD
703         bogofilter_stop_thread();
704 #endif
705         g_free(config.save_folder);
706         bogofilter_gtk_done();
707         procmsg_unregister_spam_learner(bogofilter_learn);
708         procmsg_spam_set_folder(NULL);
709         debug_print("Bogofilter plugin unloaded\n");
710 }
711
712 const gchar *plugin_name(void)
713 {
714         return _("Bogofilter");
715 }
716
717 const gchar *plugin_desc(void)
718 {
719         return _("This plugin can check all messages that are received from an "
720                  "IMAP, LOCAL or POP account for spam using Bogofilter. "
721                  "You will need Bogofilter installed locally.\n "
722                  "\n"
723                  "Before Bogofilter can recognize spam messages, you have to "
724                  "train it by marking a few hundred spam and ham messages. "
725                  "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as ham\" to "
726                  "train Bogofilter.\n"
727                  "\n"
728                  "When a message is identified as spam it can be deleted or "
729                  "saved in a specially designated folder.\n"
730                  "\n"
731                  "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
732 }
733
734 const gchar *plugin_type(void)
735 {
736         return "GTK2";
737 }
738
739 const gchar *plugin_licence(void)
740 {
741         return "GPL";
742 }
743
744 const gchar *plugin_version(void)
745 {
746         return VERSION;
747 }
748
749 struct PluginFeature *plugin_provides(void)
750 {
751         static struct PluginFeature features[] = 
752                 { {PLUGIN_FILTERING, N_("Spam detection")},
753                   {PLUGIN_FILTERING, N_("Spam learning")},
754                   {PLUGIN_NOTHING, NULL}};
755         return features;
756 }
757
758 void bogofilter_register_hook(void)
759 {
760         if (hook_id == -1)
761                 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
762         if (hook_id == -1) {
763                 g_warning("Failed to register mail filtering hook");
764                 config.process_emails = FALSE;
765         }
766 }
767
768 void bogofilter_unregister_hook(void)
769 {
770         if (hook_id != -1) {
771                 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);
772         }
773         hook_id = -1;
774 }