2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2021 the Claws Mail team and Colin Leroy
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
27 #include <sys/types.h>
31 #include <glib/gi18n.h>
37 #include "common/claws.h"
38 #include "common/version.h"
40 #include "common/utils.h"
45 #include "prefs_gtk.h"
47 #include "bogofilter.h"
50 #include "prefs_common.h"
51 #include "alertpanel.h"
52 #include "addr_compl.h"
53 #include "file-utils.h"
55 #ifdef HAVE_SYSEXITS_H
61 #ifdef HAVE_SYS_ERRNO_H
62 #include <sys/errno.h>
67 #ifdef HAVE_SYS_TIME_H
80 #define PLUGIN_NAME (_("Bogofilter"))
82 static gulong hook_id
= HOOK_NONE
;
83 static MessageCallback message_callback
;
85 static BogofilterConfig config
;
87 static PrefParam param
[] = {
88 {"process_emails", "TRUE", &config
.process_emails
, P_BOOL
,
90 {"receive_spam", "1", &config
.receive_spam
, P_INT
,
92 {"save_folder", NULL
, &config
.save_folder
, P_STRING
,
94 {"save_unsure", "FALSE", &config
.save_unsure
, P_BOOL
,
96 {"save_unsure_folder", NULL
, &config
.save_unsure_folder
, P_STRING
,
98 {"max_size", "250", &config
.max_size
, P_INT
,
100 {"bogopath", "bogofilter", &config
.bogopath
, P_STRING
,
102 {"insert_header", "FALSE", &config
.insert_header
, P_BOOL
,
104 {"whitelist_ab", "FALSE", &config
.whitelist_ab
, P_BOOL
,
106 {"whitelist_ab_folder", N_("Any"), &config
.whitelist_ab_folder
, P_STRING
,
108 {"learn_from_whitelist", "FALSE", &config
.learn_from_whitelist
, P_BOOL
,
110 {"mark_as_read", "TRUE", &config
.mark_as_read
, P_BOOL
,
113 {NULL
, NULL
, NULL
, P_OTHER
, NULL
, NULL
, NULL
}
117 * Helper function for spawn_with_input() - write an entire
127 gssize count
= write (fd
, buf
, to_write
);
143 typedef struct _BogoFilterData
{
144 MailFilteringData
*mail_filtering_data
;
150 GSList
*whitelisted_new_spams
;
156 static BogoFilterData
*to_filter_data
= NULL
;
158 static gboolean filter_th_done
= FALSE
;
159 static pthread_mutex_t list_mutex
= PTHREAD_MUTEX_INITIALIZER
;
160 static pthread_mutex_t wait_mutex
= PTHREAD_MUTEX_INITIALIZER
;
161 static pthread_cond_t wait_cond
= PTHREAD_COND_INITIALIZER
;
164 static void bogofilter_do_filter(BogoFilterData
*data
)
167 gint bogo_stdin
, bogo_stdout
;
168 GError
*error
= NULL
;
169 gboolean bogo_forked
;
173 int total
= 0, curnum
= 1;
177 total
= g_slist_length(data
->msglist
);
179 bogo_forked
= g_spawn_async_with_pipes(
180 NULL
, data
->bogo_args
,NULL
, G_SPAWN_SEARCH_PATH
|G_SPAWN_DO_NOT_REAP_CHILD
,
181 NULL
, NULL
, &bogo_pid
, &bogo_stdin
,
182 &bogo_stdout
, NULL
, &error
);
184 if (bogo_forked
== FALSE
) {
185 g_warning("%s", error
? error
->message
:"ERROR???");
191 if (config
.whitelist_ab
) {
192 gchar
*ab_folderpath
;
194 if (*config
.whitelist_ab_folder
== '\0' ||
195 strcasecmp(config
.whitelist_ab_folder
, "Any") == 0) {
196 /* match the whole addressbook */
197 ab_folderpath
= NULL
;
199 /* match the specific book/folder of the addressbook */
200 ab_folderpath
= config
.whitelist_ab_folder
;
203 start_address_completion(ab_folderpath
);
206 for (cur
= data
->msglist
; cur
; cur
= cur
->next
) {
207 gboolean whitelisted
= FALSE
;
208 msginfo
= (MsgInfo
*)cur
->data
;
210 debug_print("Filtering message %d (%d/%d)\n", msginfo
->msgnum
, curnum
, total
);
212 if (message_callback
!= NULL
)
213 message_callback(NULL
, total
, curnum
++, data
->in_thread
);
215 if (config
.whitelist_ab
&& msginfo
->from
&&
216 found_in_addressbook(msginfo
->from
))
219 /* can set flags (SCANNED, ATTACHMENT) but that's ok
220 * as GUI updates are hooked not direct */
222 file
= procmsg_get_message_file(msginfo
);
225 gchar
*tmp
= g_strdup_printf("%s\n",file
);
226 /* send filename to bogofilter */
227 write_all(bogo_stdin
, tmp
, strlen(tmp
));
229 memset(buf
, 0, sizeof(buf
));
231 if ((n_read
= read(bogo_stdout
, buf
, sizeof(buf
)-1)) < 0) {
232 g_warning("bogofilter short read");
233 debug_print("message %d is ham\n", msginfo
->msgnum
);
234 data
->mail_filtering_data
->unfiltered
= g_slist_prepend(
235 data
->mail_filtering_data
->unfiltered
, msginfo
);
236 data
->new_hams
= g_slist_prepend(data
->new_hams
, msginfo
);
238 gchar
**parts
= NULL
;
241 if (strchr(buf
, '/')) {
242 tmp
= strrchr(buf
, '/')+1;
246 parts
= g_strsplit(tmp
, " ", 0);
247 debug_print("read '%s'\n", g_strchomp(buf
));
249 /* note the result if the header if needed */
250 if (parts
&& parts
[0] && parts
[1] && parts
[2] &&
251 FOLDER_TYPE(msginfo
->folder
->folder
) == F_MH
&&
252 config
.insert_header
) {
253 gchar
*tmpfile
= get_tmp_file();
254 FILE *input
= claws_fopen(file
, "r");
255 FILE *output
= claws_fopen(tmpfile
, "w");
256 if (strstr(parts
[2], "\n"))
257 *(strstr(parts
[2], "\n")) = '\0';
258 if (input
&& !output
)
259 claws_fclose (input
);
260 else if (!input
&& output
)
261 claws_fclose (output
);
262 else if (input
&& output
) {
263 gchar tmpbuf
[BUFFSIZE
];
264 gboolean err
= FALSE
;
265 const gchar
*bogosity
= *parts
[1] == 'S' ? "Spam":
266 (*parts
[1] == 'H' ? "Ham":"Unsure");
267 gchar
*tmpstr
= g_strdup_printf(
268 "X-Bogosity: %s, spamicity=%s%s\n",
270 whitelisted
?" [whitelisted]":"");
271 if (claws_fwrite(tmpstr
, 1, strlen(tmpstr
), output
) < strlen(tmpstr
)) {
274 while (claws_fgets(tmpbuf
, sizeof(tmpbuf
), input
)) {
275 if (claws_fputs(tmpbuf
, output
) == EOF
) {
282 if (claws_safe_fclose(output
) == EOF
)
285 move_file(tmpfile
, file
, TRUE
);
292 if (!whitelisted
&& parts
&& parts
[0] && parts
[1] && *parts
[1] == 'S') {
294 debug_print("message %d is spam\n", msginfo
->msgnum
);
295 /* Spam will be filtered away, unless we want "mark only".
296 * In that case, we want it among unfiltered messages, so
297 * it gets processed further. */
298 if (config
.receive_spam
== SPAM_MARK_ONLY
) {
299 data
->mail_filtering_data
->unfiltered
= g_slist_prepend(
300 data
->mail_filtering_data
->unfiltered
, msginfo
);
302 data
->mail_filtering_data
->filtered
= g_slist_prepend(
303 data
->mail_filtering_data
->filtered
, msginfo
);
305 data
->new_spams
= g_slist_prepend(data
->new_spams
, msginfo
);
307 } else if (whitelisted
&& parts
&& parts
[0] && parts
[1] &&
308 (*parts
[1] == 'S' || *parts
[1] == 'U')) {
310 debug_print("message %d is whitelisted %s\n", msginfo
->msgnum
,
311 *parts
[1] == 'S' ? "spam":"unsure");
312 /* Whitelisted spam will *not* be filtered away, but continue
313 * their trip through filtering as if it was ham. */
314 data
->mail_filtering_data
->unfiltered
= g_slist_prepend(
315 data
->mail_filtering_data
->unfiltered
, msginfo
);
316 /* But it gets put in a different list, so that we
317 * can still flag it and inform the user that it is
318 * considered a spam (so that he can teach bogo that
320 data
->whitelisted_new_spams
= g_slist_prepend(data
->whitelisted_new_spams
, msginfo
);
322 } else if (config
.save_unsure
&& parts
&& parts
[0] && parts
[1] && *parts
[1] == 'U') {
324 debug_print("message %d is unsure\n", msginfo
->msgnum
);
325 /* Spam will be filtered away */
326 data
->mail_filtering_data
->filtered
= g_slist_prepend(
327 data
->mail_filtering_data
->filtered
, msginfo
);
328 data
->new_unsure
= g_slist_prepend(data
->new_unsure
, msginfo
);
332 debug_print("message %d is ham\n", msginfo
->msgnum
);
333 data
->mail_filtering_data
->unfiltered
= g_slist_prepend(
334 data
->mail_filtering_data
->unfiltered
, msginfo
);
335 data
->new_hams
= g_slist_prepend(data
->new_hams
, msginfo
);
342 data
->mail_filtering_data
->unfiltered
= g_slist_prepend(
343 data
->mail_filtering_data
->unfiltered
, msginfo
);
344 data
->new_hams
= g_slist_prepend(data
->new_hams
, msginfo
);
347 if (config
.whitelist_ab
)
348 end_address_completion();
353 waitpid(bogo_pid
, &status
, 0);
354 if (!WIFEXITED(status
))
357 status
= WEXITSTATUS(status
);
360 to_filter_data
->status
= status
;
364 static void *bogofilter_filtering_thread(void *data
)
366 while (!filter_th_done
) {
367 pthread_mutex_lock(&list_mutex
);
368 if (to_filter_data
== NULL
|| to_filter_data
->done
== TRUE
) {
369 pthread_mutex_unlock(&list_mutex
);
370 debug_print("thread is waiting for something to filter\n");
371 pthread_mutex_lock(&wait_mutex
);
372 pthread_cond_wait(&wait_cond
, &wait_mutex
);
373 pthread_mutex_unlock(&wait_mutex
);
375 debug_print("thread awaken with something to filter\n");
376 to_filter_data
->done
= FALSE
;
377 bogofilter_do_filter(to_filter_data
);
378 pthread_mutex_unlock(&list_mutex
);
379 to_filter_data
->done
= TRUE
;
386 static pthread_t filter_th
= 0;
388 static void bogofilter_start_thread(void)
390 filter_th_done
= FALSE
;
391 if (filter_th
!= 0 || 1)
393 if (pthread_create(&filter_th
, NULL
,
394 bogofilter_filtering_thread
,
399 debug_print("thread created\n");
402 static void bogofilter_stop_thread(void)
405 while (pthread_mutex_trylock(&list_mutex
) != 0) {
409 if (filter_th
!= 0) {
410 filter_th_done
= TRUE
;
411 debug_print("waking thread up\n");
412 pthread_mutex_lock(&wait_mutex
);
413 pthread_cond_broadcast(&wait_cond
);
414 pthread_mutex_unlock(&wait_mutex
);
415 pthread_join(filter_th
, &res
);
418 pthread_mutex_unlock(&list_mutex
);
419 debug_print("thread done\n");
423 static gboolean
mail_filtering_hook(gpointer source
, gpointer data
)
425 MailFilteringData
*mail_filtering_data
= (MailFilteringData
*) source
;
426 MsgInfo
*msginfo
= mail_filtering_data
->msginfo
;
427 GSList
*msglist
= mail_filtering_data
->msglist
;
429 static gboolean warned_error
= FALSE
;
431 int total
= 0, curnum
= 0;
432 GSList
*new_hams
= NULL
, *new_spams
= NULL
;
433 GSList
*new_unsure
, *whitelisted_new_spams
= NULL
;
434 gchar
*bogo_exec
= (config
.bogopath
&& *config
.bogopath
) ? config
.bogopath
:"bogofilter";
436 gboolean ok_to_thread
= TRUE
;
438 bogo_args
[0] = bogo_exec
;
443 if (!config
.process_emails
) {
447 if (msglist
== NULL
&& msginfo
!= NULL
) {
448 g_warning("wrong call to bogofilter mail_filtering_hook");
452 total
= g_slist_length(msglist
);
454 /* we have to make sure the mails are cached - or it'll break on IMAP */
455 if (message_callback
!= NULL
)
456 message_callback(_("Bogofilter: fetching bodies..."), total
, 0, FALSE
);
457 for (cur
= msglist
; cur
; cur
= cur
->next
) {
458 gchar
*file
= procmsg_get_message_file((MsgInfo
*)cur
->data
);
460 ok_to_thread
= FALSE
;
461 if (message_callback
!= NULL
)
462 message_callback(NULL
, total
, curnum
++, FALSE
);
465 if (message_callback
!= NULL
)
466 message_callback(NULL
, 0, 0, FALSE
);
468 if (message_callback
!= NULL
)
469 message_callback(_("Bogofilter: filtering messages..."), total
, 0, FALSE
);
472 while (pthread_mutex_trylock(&list_mutex
) != 0) {
477 to_filter_data
= g_new0(BogoFilterData
, 1);
478 to_filter_data
->msglist
= msglist
;
479 to_filter_data
->mail_filtering_data
= mail_filtering_data
;
480 to_filter_data
->new_hams
= NULL
;
481 to_filter_data
->new_unsure
= NULL
;
482 to_filter_data
->new_spams
= NULL
;
483 to_filter_data
->whitelisted_new_spams
= NULL
;
484 to_filter_data
->done
= FALSE
;
485 to_filter_data
->status
= -1;
486 to_filter_data
->bogo_args
= bogo_args
;
488 to_filter_data
->in_thread
= (filter_th
!= 0 && ok_to_thread
);
490 to_filter_data
->in_thread
= FALSE
;
494 pthread_mutex_unlock(&list_mutex
);
496 if (filter_th
!= 0 && ok_to_thread
) {
497 debug_print("waking thread to let it filter things\n");
498 pthread_mutex_lock(&wait_mutex
);
499 pthread_cond_broadcast(&wait_cond
);
500 pthread_mutex_unlock(&wait_mutex
);
502 while (!to_filter_data
->done
) {
508 while (pthread_mutex_trylock(&list_mutex
) != 0) {
513 if (filter_th
== 0 || !ok_to_thread
)
514 bogofilter_do_filter(to_filter_data
);
516 bogofilter_do_filter(to_filter_data
);
519 new_hams
= to_filter_data
->new_hams
;
520 new_unsure
= to_filter_data
->new_unsure
;
521 new_spams
= to_filter_data
->new_spams
;
522 whitelisted_new_spams
= to_filter_data
->whitelisted_new_spams
;
523 status
= to_filter_data
->status
;
524 g_free(to_filter_data
);
525 to_filter_data
= NULL
;
527 pthread_mutex_unlock(&list_mutex
);
532 for (cur
= new_hams
; cur
; cur
= cur
->next
) {
533 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
534 procmsg_msginfo_unset_flags(msginfo
, MSG_SPAM
, 0);
535 debug_print("unflagging ham: %d\n", msginfo
->msgnum
);
538 for (cur
= new_unsure
; cur
; cur
= cur
->next
) {
539 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
540 procmsg_msginfo_unset_flags(msginfo
, MSG_SPAM
, 0);
541 debug_print("unflagging unsure: %d\n", msginfo
->msgnum
);
543 if (config
.learn_from_whitelist
&& whitelisted_new_spams
) {
544 /* flag whitelisted spams */
545 for (cur
= whitelisted_new_spams
; cur
; cur
= cur
->next
) {
546 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
547 procmsg_msginfo_set_flags(msginfo
, MSG_SPAM
, 0);
548 debug_print("flagging whitelisted non-ham: %d\n", msginfo
->msgnum
);
551 bogofilter_learn(NULL
, whitelisted_new_spams
, FALSE
);
554 for (cur
= whitelisted_new_spams
; cur
; cur
= cur
->next
) {
555 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
556 procmsg_msginfo_unset_flags(msginfo
, MSG_SPAM
, 0);
557 debug_print("unflagging whitelisted non-ham: %d\n", msginfo
->msgnum
);
560 for (cur
= whitelisted_new_spams
; cur
; cur
= cur
->next
) {
561 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
562 procmsg_msginfo_unset_flags(msginfo
, MSG_SPAM
, 0);
563 debug_print("not flagging whitelisted non-ham: %d\n", msginfo
->msgnum
);
567 /* flag spams and delete them if config.receive_spam == 0
568 * (if config.receive_spam is set to 1, we'll move them later,
569 * mark as spam only if set to 2) */
570 for (cur
= new_spams
; cur
; cur
= cur
->next
) {
571 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
572 if (config
.receive_spam
!= SPAM_DELETE
) {
573 if (config
.mark_as_read
)
574 procmsg_msginfo_unset_flags(msginfo
, ~0, 0);
575 procmsg_msginfo_set_flags(msginfo
, MSG_SPAM
, 0);
577 folder_item_remove_msg(msginfo
->folder
, msginfo
->msgnum
);
581 if (status
< 0 || status
> 2) { /* I/O or other errors */
585 msg
= g_strdup_printf(_("The Bogofilter plugin couldn't filter "
586 "a message. The probable cause of the "
587 "error is that it didn't learn from any mail.\n"
588 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
589 "ham\" to train Bogofilter with a few hundred "
590 "spam and ham messages."));
592 msg
= g_strdup_printf(_("The Bogofilter plugin couldn't filter "
593 "a message. The command `%s %s %s` couldn't be run."),
594 bogo_args
[0], bogo_args
[1], bogo_args
[2]);
595 if (!prefs_common_get_prefs()->no_recv_err_panel
) {
597 alertpanel_error("%s", msg
);
601 log_error(LOG_PROTOCOL
, "%s\n", msg
);
605 if (status
< 0 || status
> 2) {
606 g_slist_free(mail_filtering_data
->filtered
);
607 g_slist_free(mail_filtering_data
->unfiltered
);
608 mail_filtering_data
->filtered
= NULL
;
609 mail_filtering_data
->unfiltered
= NULL
;
611 if (config
.receive_spam
== SPAM_MARK_AND_SAVE
&& new_spams
) {
612 FolderItem
*save_folder
= NULL
;
614 if ((!config
.save_folder
) ||
615 (config
.save_folder
[0] == '\0') ||
616 ((save_folder
= folder_find_item_from_identifier(config
.save_folder
)) == NULL
)) {
617 if (mail_filtering_data
->account
&& mail_filtering_data
->account
->set_trash_folder
) {
618 save_folder
= folder_find_item_from_identifier(
619 mail_filtering_data
->account
->trash_folder
);
621 debug_print("found trash folder from account's advanced settings\n");
623 if (save_folder
== NULL
&& mail_filtering_data
->account
&&
624 mail_filtering_data
->account
->folder
) {
625 save_folder
= mail_filtering_data
->account
->folder
->trash
;
627 debug_print("found trash folder from account's trash\n");
629 if (save_folder
== NULL
&& mail_filtering_data
->account
&&
630 !mail_filtering_data
->account
->folder
) {
631 if (mail_filtering_data
->account
->inbox
) {
632 FolderItem
*item
= folder_find_item_from_identifier(
633 mail_filtering_data
->account
->inbox
);
634 if (item
&& item
->folder
->trash
) {
635 save_folder
= item
->folder
->trash
;
636 debug_print("found trash folder from account's inbox\n");
639 if (!save_folder
&& mail_filtering_data
->account
->local_inbox
) {
640 FolderItem
*item
= folder_find_item_from_identifier(
641 mail_filtering_data
->account
->local_inbox
);
642 if (item
&& item
->folder
->trash
) {
643 save_folder
= item
->folder
->trash
;
644 debug_print("found trash folder from account's local_inbox\n");
648 if (save_folder
== NULL
) {
649 debug_print("using default trash folder\n");
650 save_folder
= folder_get_default_trash();
654 for (cur
= new_spams
; cur
; cur
= cur
->next
) {
655 msginfo
= (MsgInfo
*)cur
->data
;
656 msginfo
->filter_op
= IS_MOVE
;
657 msginfo
->to_filter_folder
= save_folder
;
661 if (config
.save_unsure
&& new_unsure
) {
662 FolderItem
*save_unsure_folder
= NULL
;
664 if ((!config
.save_unsure_folder
) ||
665 (config
.save_unsure_folder
[0] == '\0') ||
666 ((save_unsure_folder
= folder_find_item_from_identifier(config
.save_unsure_folder
)) == NULL
)) {
667 if (mail_filtering_data
->account
)
668 save_unsure_folder
= folder_find_item_from_identifier(
669 mail_filtering_data
->account
->inbox
);
670 if (save_unsure_folder
== NULL
&& mail_filtering_data
->account
&&
671 mail_filtering_data
->account
->folder
)
672 save_unsure_folder
= mail_filtering_data
->account
->folder
->inbox
;
673 if (save_unsure_folder
== NULL
&& mail_filtering_data
->account
&&
674 !mail_filtering_data
->account
->folder
) {
675 if (mail_filtering_data
->account
->inbox
) {
676 FolderItem
*item
= folder_find_item_from_identifier(
677 mail_filtering_data
->account
->inbox
);
679 save_unsure_folder
= item
;
682 if (!save_unsure_folder
&& mail_filtering_data
->account
->local_inbox
) {
683 FolderItem
*item
= folder_find_item_from_identifier(
684 mail_filtering_data
->account
->local_inbox
);
686 save_unsure_folder
= item
;
690 if (save_unsure_folder
== NULL
)
691 save_unsure_folder
= folder_get_default_inbox();
693 if (save_unsure_folder
) {
694 for (cur
= new_unsure
; cur
; cur
= cur
->next
) {
695 msginfo
= (MsgInfo
*)cur
->data
;
696 msginfo
->filter_op
= IS_MOVE
;
697 msginfo
->to_filter_folder
= save_unsure_folder
;
702 g_slist_free(new_hams
);
703 g_slist_free(new_unsure
);
704 g_slist_free(new_spams
);
705 g_slist_free(whitelisted_new_spams
);
707 if (message_callback
!= NULL
)
708 message_callback(NULL
, 0, 0, FALSE
);
709 mail_filtering_data
->filtered
= g_slist_reverse(
710 mail_filtering_data
->filtered
);
711 mail_filtering_data
->unfiltered
= g_slist_reverse(
712 mail_filtering_data
->unfiltered
);
717 BogofilterConfig
*bogofilter_get_config(void)
722 int bogofilter_learn(MsgInfo
*msginfo
, GSList
*msglist
, gboolean spam
)
726 const gchar
*bogo_exec
= (config
.bogopath
&& *config
.bogopath
) ? config
.bogopath
:"bogofilter";
729 if (msginfo
== NULL
&& msglist
== NULL
) {
733 /* process *either* a msginfo or a msglist */
735 file
= procmsg_get_message_file(msginfo
);
739 if (message_callback
!= NULL
)
740 message_callback(_("Bogofilter: learning from message..."), 0, 0, FALSE
);
743 cmd
= g_strdup_printf("%s -s -I '%s'", bogo_exec
, file
);
744 else if (MSG_IS_SPAM(msginfo
->flags
))
745 /* correct bogofilter, this wasn't spam */
746 cmd
= g_strdup_printf("%s -Sn -I '%s'", bogo_exec
, file
);
749 cmd
= g_strdup_printf("%s -n -I '%s'", bogo_exec
, file
);
751 debug_print("%s\n", cmd
);
752 if ((status
= execute_command_line(cmd
, FALSE
, NULL
)) != 0)
753 log_error(LOG_PROTOCOL
, _("Learning failed; `%s` returned with status %d."),
757 if (message_callback
!= NULL
)
758 message_callback(NULL
, 0, 0, FALSE
);
760 } else if (msglist
) {
761 GSList
*cur
= msglist
;
763 int total
= g_slist_length(msglist
);
765 gboolean some_correction
= FALSE
, some_no_correction
= FALSE
;
767 if (message_callback
!= NULL
)
768 message_callback(_("Bogofilter: learning from messages..."), total
, 0, FALSE
);
770 for (cur
= msglist
; cur
&& status
== 0; cur
= cur
->next
) {
771 info
= (MsgInfo
*)cur
->data
;
773 some_no_correction
= TRUE
;
774 else if (MSG_IS_SPAM(info
->flags
))
775 /* correct bogofilter, this wasn't spam */
776 some_correction
= TRUE
;
778 some_no_correction
= TRUE
;
782 if (some_correction
&& some_no_correction
) {
783 /* we potentially have to do different stuff for every mail */
784 for (cur
= msglist
; cur
&& status
== 0; cur
= cur
->next
) {
785 info
= (MsgInfo
*)cur
->data
;
786 file
= procmsg_get_message_file(info
);
790 cmd
= g_strdup_printf("%s -s -I '%s'", bogo_exec
, file
);
791 else if (MSG_IS_SPAM(info
->flags
))
792 /* correct bogofilter, this wasn't spam */
793 cmd
= g_strdup_printf("%s -Sn -I '%s'", bogo_exec
, file
);
796 cmd
= g_strdup_printf("%s -n -I '%s'", bogo_exec
, file
);
798 debug_print("%s\n", cmd
);
799 if ((status
= execute_command_line(cmd
, FALSE
, NULL
)) != 0)
800 log_error(LOG_PROTOCOL
, _("Learning failed; `%s` returned with status %d."),
806 if (message_callback
!= NULL
)
807 message_callback(NULL
, total
, done
, FALSE
);
809 } else if (some_correction
|| some_no_correction
) {
815 GError
*error
= NULL
;
816 gboolean bogo_forked
;
818 bogo_args
[0] = (gchar
*)bogo_exec
;
819 if (some_correction
&& !some_no_correction
)
820 bogo_args
[1] = "-Sn";
821 else if (some_no_correction
&& !some_correction
)
822 bogo_args
[1] = spam
? "-s":"-n";
825 debug_print("|%s %s %s ...\n", bogo_args
[0], bogo_args
[1], bogo_args
[2]);
826 bogo_forked
= g_spawn_async_with_pipes(
827 NULL
, bogo_args
,NULL
, G_SPAWN_SEARCH_PATH
|G_SPAWN_DO_NOT_REAP_CHILD
,
828 NULL
, NULL
, &bogo_pid
, &bogo_stdin
,
831 while (bogo_forked
&& cur
) {
833 info
= (MsgInfo
*)cur
->data
;
834 file
= procmsg_get_message_file(info
);
836 tmp
= g_strdup_printf("%s\n",
838 write_all(bogo_stdin
, tmp
, strlen(tmp
));
843 if (message_callback
!= NULL
)
844 message_callback(NULL
, total
, done
, FALSE
);
849 waitpid(bogo_pid
, &status
, 0);
850 if (!WIFEXITED(status
))
853 status
= WEXITSTATUS(status
);
855 if (!bogo_forked
|| status
!= 0) {
856 log_error(LOG_PROTOCOL
, _("Learning failed; `%s %s %s` returned with error:\n%s"),
857 bogo_args
[0], bogo_args
[1], bogo_args
[2],
858 error
? error
->message
:_("Unknown error"));
865 if (message_callback
!= NULL
)
866 message_callback(NULL
, 0, 0, FALSE
);
871 void bogofilter_save_config(void)
876 debug_print("Saving Bogofilter Page\n");
878 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, COMMON_RC
, NULL
);
879 pfile
= prefs_write_open(rcpath
);
881 if (!pfile
|| (prefs_set_block_label(pfile
, "Bogofilter") < 0))
884 if (prefs_write_param(param
, pfile
->fp
) < 0) {
885 g_warning("failed to write Bogofilter configuration to file");
886 prefs_file_close_revert(pfile
);
889 if (fprintf(pfile
->fp
, "\n") < 0) {
890 FILE_OP_ERROR(rcpath
, "fprintf");
891 prefs_file_close_revert(pfile
);
893 prefs_file_close(pfile
);
896 void bogofilter_set_message_callback(MessageCallback callback
)
898 message_callback
= callback
;
901 gint
plugin_init(gchar
**error
)
907 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
908 VERSION_NUMERIC
, PLUGIN_NAME
, error
))
911 prefs_set_default(param
);
912 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, COMMON_RC
, NULL
);
913 prefs_read_config(param
, "Bogofilter", rcpath
, NULL
);
916 bogofilter_gtk_init();
918 debug_print("Bogofilter plugin loaded\n");
921 bogofilter_start_thread();
924 if (config
.process_emails
) {
925 bogofilter_register_hook();
928 procmsg_register_spam_learner(bogofilter_learn
);
929 procmsg_spam_set_folder(config
.save_folder
, bogofilter_get_spam_folder
);
935 FolderItem
*bogofilter_get_spam_folder(MsgInfo
*msginfo
)
937 FolderItem
*item
= folder_find_item_from_identifier(config
.save_folder
);
939 if (item
|| msginfo
== NULL
|| msginfo
->folder
== NULL
)
942 if (msginfo
->folder
->folder
&&
943 msginfo
->folder
->folder
->account
&&
944 msginfo
->folder
->folder
->account
->set_trash_folder
) {
945 item
= folder_find_item_from_identifier(
946 msginfo
->folder
->folder
->account
->trash_folder
);
950 msginfo
->folder
->folder
&&
951 msginfo
->folder
->folder
->trash
)
952 item
= msginfo
->folder
->folder
->trash
;
955 item
= folder_get_default_trash();
957 debug_print("bogo spam dir: %s\n", folder_item_get_path(item
));
961 gboolean
plugin_done(void)
963 if (hook_id
!= HOOK_NONE
) {
964 bogofilter_unregister_hook();
967 bogofilter_stop_thread();
969 g_free(config
.save_folder
);
970 bogofilter_gtk_done();
971 procmsg_unregister_spam_learner(bogofilter_learn
);
972 procmsg_spam_set_folder(NULL
, NULL
);
973 debug_print("Bogofilter plugin unloaded\n");
977 const gchar
*plugin_name(void)
982 const gchar
*plugin_desc(void)
984 return _("This plugin can check all messages that are received from an "
985 "IMAP, LOCAL or POP account for spam using Bogofilter. "
986 "You will need Bogofilter installed locally.\n"
988 "Before Bogofilter can recognize spam messages, you have to "
989 "train it by marking a few hundred spam and ham messages "
990 "with the use of \"/Mark/Mark as spam\" and \"/Mark/Mark as "
993 "When a message is identified as spam it can be deleted or "
994 "saved in a specially designated folder.\n"
996 "Options can be found in /Configuration/Preferences/Plugins/Bogofilter");
999 const gchar
*plugin_type(void)
1004 const gchar
*plugin_licence(void)
1009 const gchar
*plugin_version(void)
1014 struct PluginFeature
*plugin_provides(void)
1016 static struct PluginFeature features
[] =
1017 { {PLUGIN_FILTERING
, N_("Spam detection")},
1018 {PLUGIN_FILTERING
, N_("Spam learning")},
1019 {PLUGIN_NOTHING
, NULL
}};
1023 void bogofilter_register_hook(void)
1025 if (hook_id
== HOOK_NONE
)
1026 hook_id
= hooks_register_hook(MAIL_LISTFILTERING_HOOKLIST
, mail_filtering_hook
, NULL
);
1027 if (hook_id
== HOOK_NONE
) {
1028 g_warning("failed to register mail filtering hook");
1029 config
.process_emails
= FALSE
;
1033 void bogofilter_unregister_hook(void)
1035 if (hook_id
!= HOOK_NONE
) {
1036 hooks_unregister_hook(MAIL_LISTFILTERING_HOOKLIST
, hook_id
);
1038 hook_id
= HOOK_NONE
;