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