85c5ddb194feb7f783645f07c3535010b6678753
[claws.git] / src / plugins / bogofilter / bogofilter.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> and 
4  * the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include "defs.h"
27
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <errno.h>
31
32 #include <glib.h>
33 #include <glib/gi18n.h>
34
35 #if HAVE_LOCALE_H
36 #  include <locale.h>
37 #endif
38
39 #include "common/claws.h"
40 #include "common/version.h"
41 #include "plugin.h"
42 #include "common/utils.h"
43 #include "hooks.h"
44 #include "procmsg.h"
45 #include "folder.h"
46 #include "prefs.h"
47 #include "prefs_gtk.h"
48
49 #include "bogofilter.h"
50 #include "inc.h"
51 #include "log.h"
52 #include "prefs_common.h"
53 #include "alertpanel.h"
54 #include "addr_compl.h"
55
56 #ifdef HAVE_SYSEXITS_H
57 #include <sysexits.h>
58 #endif
59 #ifdef HAVE_ERRNO_H
60 #include <errno.h>
61 #endif
62 #ifdef HAVE_SYS_ERRNO_H
63 #include <sys/errno.h>
64 #endif
65 #ifdef HAVE_TIME_H
66 #include <time.h>
67 #endif
68 #ifdef HAVE_SYS_TIME_H
69 #include <sys/time.h>
70 #endif
71 #ifdef HAVE_SIGNAL_H
72 #include <signal.h>
73 #endif
74 #ifdef HAVE_PWD_H
75 #include <pwd.h>
76 #endif
77 #ifdef USE_PTHREAD
78 #include <pthread.h>
79 #endif
80
81 #define PLUGIN_NAME (_("Bogofilter"))
82
83 static guint hook_id = -1;
84 static MessageCallback message_callback;
85
86 static BogofilterConfig config;
87
88 static PrefParam param[] = {
89         {"process_emails", "TRUE", &config.process_emails, P_BOOL,
90          NULL, NULL, NULL},
91         {"receive_spam", "1", &config.receive_spam, P_INT,
92          NULL, NULL, NULL},
93         {"save_folder", NULL, &config.save_folder, P_STRING,
94          NULL, NULL, NULL},
95         {"save_unsure", "FALSE", &config.save_unsure, P_BOOL,
96          NULL, NULL, NULL},
97         {"save_unsure_folder", NULL, &config.save_unsure_folder, P_STRING,
98          NULL, NULL, NULL},
99         {"max_size", "250", &config.max_size, P_INT,
100          NULL, NULL, NULL},
101         {"bogopath", "bogofilter", &config.bogopath, P_STRING,
102          NULL, NULL, NULL},
103         {"insert_header", "FALSE", &config.insert_header, P_BOOL,
104          NULL, NULL, NULL},
105         {"whitelist_ab", "FALSE", &config.whitelist_ab, P_BOOL,
106          NULL, NULL, NULL},
107         {"whitelist_ab_folder", N_("Any"), &config.whitelist_ab_folder, P_STRING,
108          NULL, NULL, NULL},
109         {"learn_from_whitelist", "FALSE", &config.learn_from_whitelist, P_BOOL,
110          NULL, NULL, NULL},
111         {"mark_as_read", "TRUE", &config.mark_as_read, P_BOOL,
112          NULL, NULL, NULL},
113
114         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
115 };
116
117 /*
118  * Helper function for spawn_with_input() - write an entire
119  * string to a fd.
120  */
121 static gboolean
122 write_all (int         fd,
123            const char *buf,
124            gsize       to_write)
125 {
126   while (to_write > 0)
127     {
128       gssize count = write (fd, buf, to_write);
129       if (count < 0)
130         {
131           if (errno != EINTR)
132             return FALSE;
133         }
134       else
135         {
136           to_write -= count;
137           buf += count;
138         }
139     }
140
141   return TRUE;
142 }
143
144 typedef struct _BogoFilterData {
145         MailFilteringData *mail_filtering_data;
146         gchar **bogo_args;
147         GSList *msglist;
148         GSList *new_hams;
149         GSList *new_unsure;
150         GSList *new_spams;
151         GSList *whitelisted_new_spams;
152         gboolean done;
153         int status;
154         gboolean in_thread;
155 } BogoFilterData;
156
157 static BogoFilterData *to_filter_data = NULL;
158 #ifdef USE_PTHREAD
159 static gboolean filter_th_done = FALSE;
160 static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
161 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER; 
162 static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER; 
163 #endif
164
165 static void bogofilter_do_filter(BogoFilterData *data)
166 {
167         GPid bogo_pid;
168         gint bogo_stdin, bogo_stdout;
169         GError *error = NULL;
170         gboolean bogo_forked;
171         int status = 0;
172         MsgInfo *msginfo;
173         GSList *cur = NULL;
174         int total = 0, curnum = 1;
175         gchar *file = NULL;
176         gchar buf[BUFSIZ];
177
178         total = g_slist_length(data->msglist);
179
180         bogo_forked = g_spawn_async_with_pipes(
181                         NULL, data->bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
182                         NULL, NULL, &bogo_pid, &bogo_stdin,
183                         &bogo_stdout, NULL, &error);
184                 
185         if (bogo_forked == FALSE) {
186                 g_warning("%s", error ? error->message:"ERROR???");
187                 g_error_free(error);
188                 error = NULL;
189                 status = -1;
190         } else {
191         
192                 if (config.whitelist_ab) {
193                         gchar *ab_folderpath;
194
195                         if (*config.whitelist_ab_folder == '\0' ||
196                                 strcasecmp(config.whitelist_ab_folder, "Any") == 0) {
197                                 /* match the whole addressbook */
198                                 ab_folderpath = NULL;
199                         } else {
200                                 /* match the specific book/folder of the addressbook */
201                                 ab_folderpath = config.whitelist_ab_folder;
202                         }
203
204                         start_address_completion(ab_folderpath);
205                 }
206
207                 for (cur = data->msglist; cur; cur = cur->next) {
208                         gboolean whitelisted = FALSE;
209                         msginfo = (MsgInfo *)cur->data;
210                         debug_print("Filtering message %d (%d/%d)\n", msginfo->msgnum, curnum, total);
211
212                         if (message_callback != NULL)
213                                 message_callback(NULL, total, curnum++, data->in_thread);
214
215                         if (config.whitelist_ab && msginfo->from && 
216                             found_in_addressbook(msginfo->from))
217                                 whitelisted = TRUE;
218
219                         /* can set flags (SCANNED, ATTACHMENT) but that's ok 
220                          * as GUI updates are hooked not direct */
221
222                         file = procmsg_get_message_file(msginfo);
223
224                         if (file) {
225                                 gchar *tmp = g_strdup_printf("%s\n",file);
226                                 /* send filename to bogofilter */
227                                 write_all(bogo_stdin, tmp, strlen(tmp));
228                                 g_free(tmp);
229                                 memset(buf, 0, sizeof(buf));
230                                 /* get the result */
231                                 if (read(bogo_stdout, buf, sizeof(buf)-1) < 0) {
232                                         g_warning("bogofilter short read");
233                                         debug_print("message %d is ham\n", msginfo->msgnum);
234                                         data->mail_filtering_data->unfiltered = g_slist_prepend(
235                                                 data->mail_filtering_data->unfiltered, msginfo);
236                                         data->new_hams = g_slist_prepend(data->new_hams, msginfo);
237                                 } else {
238                                         gchar **parts = NULL;
239
240                                         buf[sizeof(buf) - 1] = '\0';
241                                         if (strchr(buf, '/')) {
242                                                 tmp = strrchr(buf, '/')+1;
243                                         } else {
244                                                 tmp = buf;
245                                         }
246                                         parts = g_strsplit(tmp, " ", 0);
247                                         debug_print("read %s\n", buf);
248                                         
249                                         /* note the result if the header if needed */
250                                         if (parts && parts[0] && parts[1] && parts[2] && 
251                                             FOLDER_TYPE(msginfo->folder->folder) == F_MH &&
252                                             config.insert_header) {
253                                                 gchar *tmpfile = get_tmp_file();
254                                                 FILE *input = g_fopen(file, "r");
255                                                 FILE *output = g_fopen(tmpfile, "w");
256                                                 if (strstr(parts[2], "\n"))
257                                                         *(strstr(parts[2], "\n")) = '\0';
258                                                 if (input && !output) 
259                                                         fclose (input);
260                                                 else if (!input && output)
261                                                         fclose (output);
262                                                 else if (input && output) {
263                                                         gchar tmpbuf[BUFFSIZE];
264                                                         gboolean err = FALSE;
265                                                         const gchar *bogosity = *parts[1] == 'S' ? "Spam":
266                                                                                  (*parts[1] == 'H' ? "Ham":"Unsure");
267                                                         gchar *tmpstr = g_strdup_printf(
268                                                                         "X-Bogosity: %s, spamicity=%s%s\n",
269                                                                         bogosity, parts[2],
270                                                                         whitelisted?" [whitelisted]":"");
271                                                         if (fwrite(tmpstr, 1, strlen(tmpstr), output) < strlen(tmpstr)) {
272                                                                 err = TRUE;
273                                                         } else {
274                                                                 while (fgets(tmpbuf, sizeof(buf), input)) {
275                                                                         if (fputs(tmpbuf, output) == EOF) {
276                                                                                 err = TRUE;
277                                                                                 break;
278                                                                         }
279                                                                 }
280                                                         }
281                                                         fclose(input);
282                                                         if (fclose(output) == EOF)
283                                                                 err = TRUE;
284                                                         if (!err)
285                                                                 move_file(tmpfile, file, TRUE);
286                                                         g_free(tmpstr);
287                                                 }
288                                                 g_free(tmpfile);
289                                         }
290
291                                         /* file the mail */
292                                         if (!whitelisted && parts && parts[0] && parts[1] && *parts[1] == 'S') {
293
294                                                 debug_print("message %d is spam\n", msginfo->msgnum);
295                                                 /* Spam will be filtered away, unless we want "mark only".
296                                                  * In that case, we want it among unfiltered messages, so
297                                                  * it gets processed further. */
298                                                 if (config.receive_spam == SPAM_MARK_ONLY) {
299                                                         data->mail_filtering_data->unfiltered = g_slist_prepend(
300                                                                 data->mail_filtering_data->unfiltered, msginfo);
301                                                 } else {
302                                                         data->mail_filtering_data->filtered = g_slist_prepend(
303                                                                 data->mail_filtering_data->filtered, msginfo);
304                                                 }
305                                                 data->new_spams = g_slist_prepend(data->new_spams, msginfo);
306
307                                         } else if (whitelisted && parts && parts[0] && parts[1] && 
308                                                         (*parts[1] == 'S' || *parts[1] == 'U')) {
309
310                                                 debug_print("message %d is whitelisted %s\n", msginfo->msgnum,
311                                                         *parts[1] == 'S' ? "spam":"unsure");
312                                                 /* Whitelisted spam will *not* be filtered away, but continue
313                                                  * their trip through filtering as if it was ham. */
314                                                 data->mail_filtering_data->unfiltered = g_slist_prepend(
315                                                         data->mail_filtering_data->unfiltered, msginfo);
316                                                 /* But it gets put in a different list, so that we 
317                                                  * can still flag it and inform the user that it is
318                                                  * considered a spam (so that he can teach bogo that 
319                                                  * it was not). */
320                                                 data->whitelisted_new_spams = g_slist_prepend(data->whitelisted_new_spams, msginfo);
321
322                                         } else if (config.save_unsure && parts && parts[0] && parts[1] && *parts[1] == 'U') {
323                                                 
324                                                 debug_print("message %d is unsure\n", msginfo->msgnum);
325                                                 /* Spam will be filtered away */
326                                                 data->mail_filtering_data->filtered = g_slist_prepend(
327                                                         data->mail_filtering_data->filtered, msginfo);
328                                                 data->new_unsure = g_slist_prepend(data->new_unsure, msginfo);
329
330                                         } else {
331                                                 
332                                                 debug_print("message %d is ham\n", msginfo->msgnum);
333                                                 data->mail_filtering_data->unfiltered = g_slist_prepend(
334                                                         data->mail_filtering_data->unfiltered, msginfo);
335                                                 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
336
337                                         }
338                                         g_strfreev(parts);
339                                 }
340                                 g_free(file);
341                         } else {
342                                 data->mail_filtering_data->unfiltered = g_slist_prepend(
343                                         data->mail_filtering_data->unfiltered, msginfo);
344                                 data->new_hams = g_slist_prepend(data->new_hams, msginfo);
345                         }
346                 }
347                 if (config.whitelist_ab)
348                         end_address_completion();
349         }
350         if (status != -1) {
351                 close(bogo_stdout);
352                 close(bogo_stdin);
353                 waitpid(bogo_pid, &status, 0);
354                 if (!WIFEXITED(status))
355                         status = -1;
356                 else
357                         status = WEXITSTATUS(status);
358         }
359
360         to_filter_data->status = status; 
361 }
362
363 #ifdef USE_PTHREAD
364 static void *bogofilter_filtering_thread(void *data) 
365 {
366         while (!filter_th_done) {
367                 pthread_mutex_lock(&list_mutex);
368                 if (to_filter_data == NULL || to_filter_data->done == TRUE) {
369                         pthread_mutex_unlock(&list_mutex);
370                         debug_print("thread is waiting for something to filter\n");
371                         pthread_mutex_lock(&wait_mutex);
372                         pthread_cond_wait(&wait_cond, &wait_mutex);
373                         pthread_mutex_unlock(&wait_mutex);
374                 } else {
375                         debug_print("thread awaken with something to filter\n");
376                         to_filter_data->done = FALSE;
377                         bogofilter_do_filter(to_filter_data);
378                         pthread_mutex_unlock(&list_mutex);
379                         to_filter_data->done = TRUE;
380                         usleep(100);
381                 }
382         }
383         return NULL;
384 }
385
386 static pthread_t filter_th = 0;
387
388 static void bogofilter_start_thread(void)
389 {
390         filter_th_done = FALSE;
391         if (filter_th != 0 || 1)
392                 return;
393         if (pthread_create(&filter_th, NULL,
394                         bogofilter_filtering_thread, 
395                         NULL) != 0) {
396                 filter_th = 0;
397                 return;
398         }
399         debug_print("thread created\n");
400 }
401
402 static void bogofilter_stop_thread(void)
403 {
404         void *res;
405         while (pthread_mutex_trylock(&list_mutex) != 0) {
406                 GTK_EVENTS_FLUSH();
407                 usleep(100);
408         }
409         if (filter_th != 0) {
410                 filter_th_done = TRUE;
411                 debug_print("waking thread up\n");
412                 pthread_mutex_lock(&wait_mutex);
413                 pthread_cond_broadcast(&wait_cond);
414                 pthread_mutex_unlock(&wait_mutex);
415                 pthread_join(filter_th, &res);
416                 filter_th = 0;
417         }
418         pthread_mutex_unlock(&list_mutex);
419         debug_print("thread done\n");
420 }
421 #endif
422
423 static gboolean mail_filtering_hook(gpointer source, gpointer data)
424 {
425         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
426         MsgInfo *msginfo = mail_filtering_data->msginfo;
427         GSList *msglist = mail_filtering_data->msglist;
428         GSList *cur = NULL;
429         static gboolean warned_error = FALSE;
430         int status = 0;
431         int total = 0, curnum = 0;
432         GSList *new_hams = NULL, *new_spams = NULL;
433         GSList *new_unsure, *whitelisted_new_spams = NULL;
434         gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
435         gchar *bogo_args[4];
436         gboolean ok_to_thread = TRUE;
437
438         bogo_args[0] = bogo_exec;
439         bogo_args[1] = "-T";
440         bogo_args[2] = "-b";
441         bogo_args[3] = NULL;
442         
443         if (!config.process_emails) {
444                 return FALSE;
445         }
446         
447         if (msglist == NULL && msginfo != NULL) {
448                 g_warning("wrong call to bogofilter mail_filtering_hook");
449                 return FALSE;
450         }
451         
452         total = g_slist_length(msglist);
453         
454         /* we have to make sure the mails are cached - or it'll break on IMAP */
455         if (message_callback != NULL)
456                 message_callback(_("Bogofilter: fetching bodies..."), total, 0, FALSE);
457         for (cur = msglist; cur; cur = cur->next) {
458                 gchar *file = procmsg_get_message_file((MsgInfo *)cur->data);
459                 if (file == NULL)
460                         ok_to_thread = FALSE;
461                 if (message_callback != NULL)
462                         message_callback(NULL, total, curnum++, FALSE);
463                 g_free(file);
464         }
465         if (message_callback != NULL)
466                 message_callback(NULL, 0, 0, FALSE);
467
468         if (message_callback != NULL)
469                 message_callback(_("Bogofilter: filtering messages..."), total, 0, FALSE);
470
471 #ifdef USE_PTHREAD
472         while (pthread_mutex_trylock(&list_mutex) != 0) {
473                 GTK_EVENTS_FLUSH();
474                 usleep(100);
475         }
476 #endif
477         to_filter_data = g_new0(BogoFilterData, 1);
478         to_filter_data->msglist = msglist;
479         to_filter_data->mail_filtering_data = mail_filtering_data;
480         to_filter_data->new_hams = NULL;
481         to_filter_data->new_unsure = NULL;
482         to_filter_data->new_spams = NULL;
483         to_filter_data->whitelisted_new_spams = NULL;
484         to_filter_data->done = FALSE;
485         to_filter_data->status = -1;
486         to_filter_data->bogo_args = bogo_args;
487 #ifdef USE_PTHREAD
488         to_filter_data->in_thread = (filter_th != 0 && ok_to_thread);
489 #else
490         to_filter_data->in_thread = FALSE;
491 #endif
492
493 #ifdef USE_PTHREAD
494         pthread_mutex_unlock(&list_mutex);
495         
496         if (filter_th != 0 && ok_to_thread) {
497                 debug_print("waking thread to let it filter things\n");
498                 pthread_mutex_lock(&wait_mutex);
499                 pthread_cond_broadcast(&wait_cond);
500                 pthread_mutex_unlock(&wait_mutex);
501
502                 while (!to_filter_data->done) {
503                         GTK_EVENTS_FLUSH();
504                         usleep(100);
505                 }
506         }
507
508         while (pthread_mutex_trylock(&list_mutex) != 0) {
509                 GTK_EVENTS_FLUSH();
510                 usleep(100);
511
512         }
513         if (filter_th == 0 || !ok_to_thread)
514                 bogofilter_do_filter(to_filter_data);
515 #else
516         bogofilter_do_filter(to_filter_data);   
517 #endif
518
519         new_hams = to_filter_data->new_hams;
520         new_unsure = to_filter_data->new_unsure;
521         new_spams = to_filter_data->new_spams;
522         whitelisted_new_spams = to_filter_data->whitelisted_new_spams;
523         status = to_filter_data->status;
524         g_free(to_filter_data);
525         to_filter_data = NULL;
526 #ifdef USE_PTHREAD
527         pthread_mutex_unlock(&list_mutex);
528 #endif
529
530
531         /* unflag hams */
532         for (cur = new_hams; cur; cur = cur->next) {
533                 MsgInfo *msginfo = (MsgInfo *)cur->data;
534                 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
535                 debug_print("unflagging ham: %d\n", msginfo->msgnum);
536         }
537         /* unflag unsure */
538         for (cur = new_unsure; cur; cur = cur->next) {
539                 MsgInfo *msginfo = (MsgInfo *)cur->data;
540                 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
541                 debug_print("unflagging unsure: %d\n", msginfo->msgnum);
542         }
543         if (config.learn_from_whitelist && whitelisted_new_spams) {
544                 /* flag whitelisted spams */
545                 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
546                         MsgInfo *msginfo = (MsgInfo *)cur->data;
547                         procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
548                         debug_print("flagging whitelisted non-ham: %d\n", msginfo->msgnum);
549                 }
550                 /* correct bogo */
551                 bogofilter_learn(NULL, whitelisted_new_spams, FALSE);
552
553                 /* unflag them */
554                 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
555                         MsgInfo *msginfo = (MsgInfo *)cur->data;
556                         procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
557                         debug_print("unflagging whitelisted non-ham: %d\n", msginfo->msgnum);
558                 }
559         } else {
560                 for (cur = whitelisted_new_spams; cur; cur = cur->next) {
561                         MsgInfo *msginfo = (MsgInfo *)cur->data;
562                         procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
563                         debug_print("not flagging whitelisted non-ham: %d\n", msginfo->msgnum);
564                 }
565         }
566
567         /* flag spams and delete them if config.receive_spam == 0
568          * (if config.receive_spam is set to 1, we'll move them later,
569          * mark as spam only if set to 2) */
570         for (cur = new_spams; cur; cur = cur->next) {
571                 MsgInfo *msginfo = (MsgInfo *)cur->data;
572                 if (config.receive_spam != SPAM_DELETE) {
573                         if (config.mark_as_read)
574                                 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
575                         procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
576                 } else {
577                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
578                 }
579         }
580         
581         if (status < 0 || status > 2) { /* I/O or other errors */
582                 gchar *msg = NULL;
583                 
584                 if (status == 3)
585                         msg =  g_strdup_printf(_("The Bogofilter plugin couldn't filter "
586                                            "a message. The probable cause of the "
587                                            "error is that it didn't learn from any mail.\n"
588                                            "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
589                                            "ham\" to train Bogofilter with a few hundred "
590                                            "spam and ham messages."));
591                 else
592                         msg =  g_strdup_printf(_("The Bogofilter plugin couldn't filter "
593                                            "a message. The command `%s %s %s` couldn't be run."), 
594                                            bogo_args[0], bogo_args[1], bogo_args[2]);
595                 if (!prefs_common_get_prefs()->no_recv_err_panel) {
596                         if (!warned_error) {
597                                 alertpanel_error("%s", msg);
598                         }
599                         warned_error = TRUE;
600                 } else {
601                         log_error(LOG_PROTOCOL, "%s\n", msg);
602                 }
603                 g_free(msg);
604         }
605         if (status < 0 || status > 2) {
606                 g_slist_free(mail_filtering_data->filtered);
607                 g_slist_free(mail_filtering_data->unfiltered);
608                 mail_filtering_data->filtered = NULL;
609                 mail_filtering_data->unfiltered = NULL;
610         } else {
611                 if (config.receive_spam == SPAM_MARK_AND_SAVE && new_spams) {
612                         FolderItem *save_folder = NULL;
613
614                         if ((!config.save_folder) ||
615                             (config.save_folder[0] == '\0') ||
616                             ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
617                                 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
618                                         save_folder = folder_find_item_from_identifier(
619                                                 mail_filtering_data->account->trash_folder);
620                                         if (save_folder)
621                                                 debug_print("found trash folder from account's advanced settings\n");
622                                 }
623                                 if (save_folder == NULL && mail_filtering_data->account &&
624                                     mail_filtering_data->account->folder) {
625                                         save_folder = mail_filtering_data->account->folder->trash;
626                                         if (save_folder)
627                                                 debug_print("found trash folder from account's trash\n");
628                                 }
629                                 if (save_folder == NULL && mail_filtering_data->account &&
630                                     !mail_filtering_data->account->folder)  {
631                                         if (mail_filtering_data->account->inbox) {
632                                                 FolderItem *item = folder_find_item_from_identifier(
633                                                         mail_filtering_data->account->inbox);
634                                                 if (item && item->folder->trash) {
635                                                         save_folder = item->folder->trash;
636                                                         debug_print("found trash folder from account's inbox\n");
637                                                 }
638                                         } 
639                                         if (!save_folder && mail_filtering_data->account->local_inbox) {
640                                                 FolderItem *item = folder_find_item_from_identifier(
641                                                         mail_filtering_data->account->local_inbox);
642                                                 if (item && item->folder->trash) {
643                                                         save_folder = item->folder->trash;
644                                                         debug_print("found trash folder from account's local_inbox\n");
645                                                 }
646                                         }
647                                 }
648                                 if (save_folder == NULL) {
649                                         debug_print("using default trash folder\n");
650                                         save_folder = folder_get_default_trash();
651                                 }
652                         }
653                         if (save_folder) {
654                                 for (cur = new_spams; cur; cur = cur->next) {
655                                         msginfo = (MsgInfo *)cur->data;
656                                         msginfo->filter_op = IS_MOVE;
657                                         msginfo->to_filter_folder = save_folder;
658                                 }
659                         }
660                 }
661                 if (config.save_unsure && new_unsure) {
662                         FolderItem *save_unsure_folder = NULL;
663
664                         if ((!config.save_unsure_folder) ||
665                             (config.save_unsure_folder[0] == '\0') ||
666                             ((save_unsure_folder = folder_find_item_from_identifier(config.save_unsure_folder)) == NULL)) {
667                                 if (mail_filtering_data->account)
668                                         save_unsure_folder = folder_find_item_from_identifier(
669                                                 mail_filtering_data->account->inbox);
670                                 if (save_unsure_folder == NULL && mail_filtering_data->account &&
671                                     mail_filtering_data->account->folder)
672                                         save_unsure_folder = mail_filtering_data->account->folder->inbox;
673                                 if (save_unsure_folder == NULL && mail_filtering_data->account &&
674                                     !mail_filtering_data->account->folder)  {
675                                         if (mail_filtering_data->account->inbox) {
676                                                 FolderItem *item = folder_find_item_from_identifier(
677                                                         mail_filtering_data->account->inbox);
678                                                 if (item) {
679                                                         save_unsure_folder = item;
680                                                 }
681                                         } 
682                                         if (!save_unsure_folder && mail_filtering_data->account->local_inbox) {
683                                                 FolderItem *item = folder_find_item_from_identifier(
684                                                         mail_filtering_data->account->local_inbox);
685                                                 if (item) {
686                                                         save_unsure_folder = item;
687                                                 }
688                                         }
689                                 }
690                                 if (save_unsure_folder == NULL)
691                                         save_unsure_folder = folder_get_default_inbox();
692                         }
693                         if (save_unsure_folder) {
694                                 for (cur = new_unsure; cur; cur = cur->next) {
695                                         msginfo = (MsgInfo *)cur->data;
696                                         msginfo->filter_op = IS_MOVE;
697                                         msginfo->to_filter_folder = save_unsure_folder;
698                                 }
699                         }
700                 }
701         } 
702         g_slist_free(new_hams);
703         g_slist_free(new_unsure);
704         g_slist_free(new_spams);
705         g_slist_free(whitelisted_new_spams);
706
707         if (message_callback != NULL)
708                 message_callback(NULL, 0, 0, FALSE);
709         mail_filtering_data->filtered   = g_slist_reverse(
710                 mail_filtering_data->filtered);
711         mail_filtering_data->unfiltered = g_slist_reverse(
712                 mail_filtering_data->unfiltered);
713         
714         return FALSE;
715 }
716
717 BogofilterConfig *bogofilter_get_config(void)
718 {
719         return &config;
720 }
721
722 int bogofilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
723 {
724         gchar *cmd = NULL;
725         gchar *file = NULL;
726         const gchar *bogo_exec = (config.bogopath && *config.bogopath) ? config.bogopath:"bogofilter";
727         gint status = 0;
728
729         if (msginfo == NULL && msglist == NULL) {
730                 return -1;
731         }
732
733         if (msginfo) {
734                 file = procmsg_get_message_file(msginfo);
735                 if (file == NULL) {
736                         return -1;
737                 } else {
738                         if (message_callback != NULL)
739                                 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE);
740                         if (spam)
741                                 /* learn as spam */
742                                 cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
743                         else if (MSG_IS_SPAM(msginfo->flags))
744                                 /* correct bogofilter, this wasn't spam */
745                                 cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
746                         else 
747                                 /* learn as ham */
748                                 cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
749                                 
750                         debug_print("%s\n", cmd);
751                         if ((status = execute_command_line(cmd, FALSE, NULL)) != 0)
752                                 log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
753                                                 cmd, status);
754                         g_free(cmd);
755                         g_free(file);
756                         if (message_callback != NULL)
757                                 message_callback(NULL, 0, 0, FALSE);
758                 }
759         } else if (msglist) {
760                 GSList *cur = msglist;
761                 MsgInfo *info;
762                 int total = g_slist_length(msglist);
763                 int done = 0;
764                 gboolean some_correction = FALSE, some_no_correction = FALSE;
765         
766                 if (message_callback != NULL)
767                         message_callback(_("Bogofilter: learning from messages..."), total, 0, FALSE);
768                 
769                 for (cur = msglist; cur && status == 0; cur = cur->next) {
770                         info = (MsgInfo *)cur->data;
771                         if (spam)
772                                 some_no_correction = TRUE;
773                         else if (MSG_IS_SPAM(info->flags))
774                                 /* correct bogofilter, this wasn't spam */
775                                 some_correction = TRUE;
776                         else 
777                                 some_no_correction = TRUE;
778                         
779                 }
780                 
781                 if (some_correction && some_no_correction) {
782                         /* we potentially have to do different stuff for every mail */
783                         for (cur = msglist; cur && status == 0; cur = cur->next) {
784                                 info = (MsgInfo *)cur->data;
785                                 file = procmsg_get_message_file(info);
786
787                                 if (spam)
788                                         /* learn as spam */
789                                         cmd = g_strdup_printf("%s -s -I '%s'", bogo_exec, file);
790                                 else if (MSG_IS_SPAM(info->flags))
791                                         /* correct bogofilter, this wasn't spam */
792                                         cmd = g_strdup_printf("%s -Sn -I '%s'", bogo_exec, file);
793                                 else 
794                                         /* learn as ham */
795                                         cmd = g_strdup_printf("%s -n -I '%s'", bogo_exec, file);
796                                 
797                                 debug_print("%s\n", cmd);
798                                 if ((status = execute_command_line(cmd, FALSE, NULL)) != 0)
799                                         log_error(LOG_PROTOCOL, _("Learning failed; `%s` returned with status %d."),
800                                                         cmd, status);
801
802                                 g_free(cmd);
803                                 g_free(file);
804                                 done++;
805                                 if (message_callback != NULL)
806                                         message_callback(NULL, total, done, FALSE);
807                         }
808                 } else if (some_correction || some_no_correction) {
809                         cur = msglist;
810                         
811                         gchar *bogo_args[4];
812                         GPid bogo_pid;
813                         gint bogo_stdin;
814                         GError *error = NULL;
815                         gboolean bogo_forked;
816
817                         bogo_args[0] = (gchar *)bogo_exec;
818                         if (some_correction && !some_no_correction)
819                                 bogo_args[1] = "-Sn";
820                         else if (some_no_correction && !some_correction)
821                                 bogo_args[1] = spam ? "-s":"-n";
822                         bogo_args[2] = "-b";
823                         bogo_args[3] = NULL;
824                         debug_print("|%s %s %s ...\n", bogo_args[0], bogo_args[1], bogo_args[2]);
825                         bogo_forked = g_spawn_async_with_pipes(
826                                         NULL, bogo_args,NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
827                                         NULL, NULL, &bogo_pid, &bogo_stdin,
828                                         NULL, NULL, &error);
829
830                         while (bogo_forked && cur) {
831                                 gchar *tmp = NULL;
832                                 info = (MsgInfo *)cur->data;
833                                 file = procmsg_get_message_file(info);
834                                 if (file) {
835                                         tmp = g_strdup_printf("%s\n", 
836                                                 file);
837                                         write_all(bogo_stdin, tmp, strlen(tmp));
838                                         g_free(tmp);
839                                 }
840                                 g_free(file);
841                                 done++;
842                                 if (message_callback != NULL)
843                                         message_callback(NULL, total, done, FALSE);
844                                 cur = cur->next;
845                         }
846                         if (bogo_forked) {
847                                 close(bogo_stdin);
848                                 waitpid(bogo_pid, &status, 0);
849                                 if (!WIFEXITED(status))
850                                         status = -1;
851                                 else
852                                         status = WEXITSTATUS(status);
853                         }
854                         if (!bogo_forked || status != 0) {
855                                 log_error(LOG_PROTOCOL, _("Learning failed; `%s %s %s` returned with error:\n%s"),
856                                                 bogo_args[0], bogo_args[1], bogo_args[2], 
857                                                 error ? error->message:_("Unknown error"));
858                                 if (error)
859                                         g_error_free(error);
860                         }
861
862                 }
863
864                 if (message_callback != NULL)
865                         message_callback(NULL, 0, 0, FALSE);
866         }
867         return 0;
868 }
869
870 void bogofilter_save_config(void)
871 {
872         PrefFile *pfile;
873         gchar *rcpath;
874
875         debug_print("Saving Bogofilter Page\n");
876
877         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
878         pfile = prefs_write_open(rcpath);
879         g_free(rcpath);
880         if (!pfile || (prefs_set_block_label(pfile, "Bogofilter") < 0))
881                 return;
882
883         if (prefs_write_param(param, pfile->fp) < 0) {
884                 g_warning("Failed to write Bogofilter configuration to file");
885                 prefs_file_close_revert(pfile);
886                 return;
887         }
888         if (fprintf(pfile->fp, "\n") < 0) {
889                 FILE_OP_ERROR(rcpath, "fprintf");
890                 prefs_file_close_revert(pfile);
891         } else
892                 prefs_file_close(pfile);
893 }
894
895 void bogofilter_set_message_callback(MessageCallback callback)
896 {
897         message_callback = callback;
898 }
899
900 gint plugin_init(gchar **error)
901 {
902         gchar *rcpath;
903
904         hook_id = -1;
905
906         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
907                                 VERSION_NUMERIC, PLUGIN_NAME, error))
908                 return -1;
909
910         prefs_set_default(param);
911         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
912         prefs_read_config(param, "Bogofilter", rcpath, NULL);
913         g_free(rcpath);
914
915         bogofilter_gtk_init();
916                 
917         debug_print("Bogofilter plugin loaded\n");
918
919 #ifdef USE_PTHREAD
920         bogofilter_start_thread();
921 #endif
922
923         if (config.process_emails) {
924                 bogofilter_register_hook();
925         }
926
927         procmsg_register_spam_learner(bogofilter_learn);
928         procmsg_spam_set_folder(config.save_folder, bogofilter_get_spam_folder);
929
930         return 0;
931         
932 }
933
934 FolderItem *bogofilter_get_spam_folder(MsgInfo *msginfo)
935 {
936         FolderItem *item = folder_find_item_from_identifier(config.save_folder);
937
938         if (item || msginfo == NULL || msginfo->folder == NULL)
939                 return item;
940
941         if (msginfo->folder->folder &&
942             msginfo->folder->folder->account && 
943             msginfo->folder->folder->account->set_trash_folder) {
944                 item = folder_find_item_from_identifier(
945                         msginfo->folder->folder->account->trash_folder);
946         }
947
948         if (item == NULL && 
949             msginfo->folder->folder &&
950             msginfo->folder->folder->trash)
951                 item = msginfo->folder->folder->trash;
952                 
953         if (item == NULL)
954                 item = folder_get_default_trash();
955                 
956         debug_print("bogo spam dir: %s\n", folder_item_get_path(item));
957         return item;
958 }
959
960 gboolean plugin_done(void)
961 {
962         if (hook_id != (guint) -1) {
963                 bogofilter_unregister_hook();
964         }
965 #ifdef USE_PTHREAD
966         bogofilter_stop_thread();
967 #endif
968         g_free(config.save_folder);
969         bogofilter_gtk_done();
970         procmsg_unregister_spam_learner(bogofilter_learn);
971         procmsg_spam_set_folder(NULL, NULL);
972         debug_print("Bogofilter plugin unloaded\n");
973         return TRUE;
974 }
975
976 const gchar *plugin_name(void)
977 {
978         return PLUGIN_NAME;
979 }
980
981 const gchar *plugin_desc(void)
982 {
983         return _("This plugin can check all messages that are received from an "
984                  "IMAP, LOCAL or POP account for spam using Bogofilter. "
985                  "You will need Bogofilter installed locally.\n"
986                  "\n"
987                  "Before Bogofilter can recognize spam messages, you have to "
988                  "train it by marking a few hundred spam and ham messages "
989                  "with the use of \"/Mark/Mark as spam\" and \"/Mark/Mark as "
990                  "ham\".\n"
991                  "\n"
992                  "When a message is identified as spam it can be deleted or "
993                  "saved in a specially designated folder.\n"
994                  "\n"
995                  "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
996 }
997
998 const gchar *plugin_type(void)
999 {
1000         return "GTK2";
1001 }
1002
1003 const gchar *plugin_licence(void)
1004 {
1005         return "GPL3+";
1006 }
1007
1008 const gchar *plugin_version(void)
1009 {
1010         return VERSION;
1011 }
1012
1013 struct PluginFeature *plugin_provides(void)
1014 {
1015         static struct PluginFeature features[] = 
1016                 { {PLUGIN_FILTERING, N_("Spam detection")},
1017                   {PLUGIN_FILTERING, N_("Spam learning")},
1018                   {PLUGIN_NOTHING, NULL}};
1019         return features;
1020 }
1021
1022 void bogofilter_register_hook(void)
1023 {
1024         if (hook_id == (guint) -1)
1025                 hook_id = hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST, mail_filtering_hook, NULL);
1026         if (hook_id == (guint) -1) {
1027                 g_warning("Failed to register mail filtering hook");
1028                 config.process_emails = FALSE;
1029         }
1030 }
1031
1032 void bogofilter_unregister_hook(void)
1033 {
1034         if (hook_id != (guint) -1) {
1035                 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST, hook_id);
1036         }
1037         hook_id = -1;
1038 }