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