2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2023 the Claws Mail team and
4 * Colin Leroy <colin@colino.net>
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.
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.
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/>.
23 #include "claws-features.h"
27 #include <glib/gi18n.h>
31 #include <sys/types.h>
39 #include "common/claws.h"
40 #include "common/version.h"
42 #include "common/utils.h"
47 #include "prefs_gtk.h"
54 #include "prefs_common.h"
55 #include "alertpanel.h"
56 #include "addr_compl.h"
58 #ifdef HAVE_SYSEXITS_H
64 #ifdef HAVE_SYS_ERRNO_H
65 #include <sys/errno.h>
70 #ifdef HAVE_SYS_TIME_H
80 #define PLUGIN_NAME (_("Bsfilter"))
82 static gulong hook_id
= HOOK_NONE
;
83 static MessageCallback message_callback
;
85 static BsfilterConfig config
;
87 extern SessionStats session_stats
;
89 static PrefParam param
[] = {
90 {"process_emails", "TRUE", &config
.process_emails
, P_BOOL
,
92 {"receive_spam", "TRUE", &config
.receive_spam
, P_BOOL
,
94 {"save_folder", NULL
, &config
.save_folder
, P_STRING
,
96 {"max_size", "250", &config
.max_size
, P_INT
,
99 {"bspath", "bsfilter", &config
.bspath
, P_STRING
,
102 {"bspath", "bsfilterw.exe", &config
.bspath
, P_STRING
,
105 {"whitelist_ab", "FALSE", &config
.whitelist_ab
, P_BOOL
,
107 {"whitelist_ab_folder", N_("Any"), &config
.whitelist_ab_folder
, P_STRING
,
109 {"learn_from_whitelist", "FALSE", &config
.learn_from_whitelist
, P_BOOL
,
111 {"mark_as_read", "TRUE", &config
.mark_as_read
, P_BOOL
,
114 {NULL
, NULL
, NULL
, P_OTHER
, NULL
, NULL
, NULL
}
117 typedef struct _BsFilterData
{
118 MailFilteringData
*mail_filtering_data
;
127 static BsFilterData
*to_filter_data
= NULL
;
129 static gboolean filter_th_done
= FALSE
;
130 static pthread_mutex_t list_mutex
= PTHREAD_MUTEX_INITIALIZER
;
131 static pthread_mutex_t wait_mutex
= PTHREAD_MUTEX_INITIALIZER
;
132 static pthread_cond_t wait_cond
= PTHREAD_COND_INITIALIZER
;
135 static void bsfilter_do_filter(BsFilterData
*data
)
139 gboolean whitelisted
= FALSE
;
140 MsgInfo
*msginfo
= to_filter_data
->msginfo
;
142 if (config
.whitelist_ab
) {
143 gchar
*ab_folderpath
;
145 if (*config
.whitelist_ab_folder
== '\0' ||
146 strcasecmp(config
.whitelist_ab_folder
, "Any") == 0) {
147 /* match the whole addressbook */
148 ab_folderpath
= NULL
;
150 /* match the specific book/folder of the addressbook */
151 ab_folderpath
= config
.whitelist_ab_folder
;
154 start_address_completion(ab_folderpath
);
157 debug_print("Filtering message %d\n", msginfo
->msgnum
);
159 if (config
.whitelist_ab
&& msginfo
->from
&&
160 found_in_addressbook(msginfo
->from
))
163 /* can set flags (SCANNED, ATTACHMENT) but that's ok
164 * as GUI updates are hooked not direct */
166 file
= procmsg_get_message_file(msginfo
);
170 gchar
*classify
= g_strconcat((config
.bspath
&& *config
.bspath
) ? config
.bspath
:"bsfilter",
171 " --homedir '",get_rc_dir(),"' '", file
, "'", NULL
);
173 gchar
*classify
= g_strconcat((config
.bspath
&& *config
.bspath
) ? config
.bspath
:"bsfilterw.exe",
174 " --homedir '",get_rc_dir(),"' '", file
, "'", NULL
);
176 status
= execute_command_line(classify
, FALSE
,
177 claws_get_startup_dir());
181 if (config
.whitelist_ab
)
182 end_address_completion();
184 to_filter_data
->status
= status
;
185 to_filter_data
->whitelisted
= whitelisted
;
189 static void *bsfilter_filtering_thread(void *data
)
191 while (!filter_th_done
) {
192 pthread_mutex_lock(&list_mutex
);
193 if (to_filter_data
== NULL
|| to_filter_data
->done
== TRUE
) {
194 pthread_mutex_unlock(&list_mutex
);
195 debug_print("thread is waiting for something to filter\n");
196 pthread_mutex_lock(&wait_mutex
);
197 pthread_cond_wait(&wait_cond
, &wait_mutex
);
198 pthread_mutex_unlock(&wait_mutex
);
200 debug_print("thread awaken with something to filter\n");
201 to_filter_data
->done
= FALSE
;
202 bsfilter_do_filter(to_filter_data
);
203 pthread_mutex_unlock(&list_mutex
);
204 to_filter_data
->done
= TRUE
;
211 static pthread_t filter_th
;
212 static int filter_th_started
= 0;
214 static void bsfilter_start_thread(void)
216 filter_th_done
= FALSE
;
217 if (filter_th_started
!= 0)
219 if (pthread_create(&filter_th
, NULL
,
220 bsfilter_filtering_thread
,
222 filter_th_started
= 0;
225 debug_print("thread created\n");
226 filter_th_started
= 1;
229 static void bsfilter_stop_thread(void)
232 while (pthread_mutex_trylock(&list_mutex
) != 0) {
236 if (filter_th_started
!= 0) {
237 filter_th_done
= TRUE
;
238 debug_print("waking thread up\n");
239 pthread_mutex_lock(&wait_mutex
);
240 pthread_cond_broadcast(&wait_cond
);
241 pthread_mutex_unlock(&wait_mutex
);
242 pthread_join(filter_th
, &res
);
243 filter_th_started
= 0;
245 pthread_mutex_unlock(&list_mutex
);
246 debug_print("thread done\n");
250 static gboolean
mail_filtering_hook(gpointer source
, gpointer data
)
252 MailFilteringData
*mail_filtering_data
= (MailFilteringData
*) source
;
253 MsgInfo
*msginfo
= mail_filtering_data
->msginfo
;
254 static gboolean warned_error
= FALSE
;
255 int status
= 0, whitelisted
= 0;
257 gchar
*bs_exec
= (config
.bspath
&& *config
.bspath
) ? config
.bspath
:"bsfilter";
259 gchar
*bs_exec
= (config
.bspath
&& *config
.bspath
) ? config
.bspath
:"bsfilterw.exe";
261 gboolean filtered
= FALSE
;
263 if (!config
.process_emails
) {
267 if (msginfo
== NULL
) {
268 g_warning("wrong call to bsfilter mail_filtering_hook");
272 /* we have to make sure the mails are cached - or it'll break on IMAP */
273 if (message_callback
!= NULL
)
274 message_callback(_("Bsfilter: fetching body..."), 0, 0, FALSE
);
276 gchar
*file
= procmsg_get_message_file(msginfo
);
279 if (message_callback
!= NULL
)
280 message_callback(NULL
, 0, 0, FALSE
);
282 if (message_callback
!= NULL
)
283 message_callback(_("Bsfilter: filtering message..."), 0, 0, FALSE
);
286 while (pthread_mutex_trylock(&list_mutex
) != 0) {
292 to_filter_data
= g_new0(BsFilterData
, 1);
293 to_filter_data
->msginfo
= msginfo
;
294 to_filter_data
->mail_filtering_data
= mail_filtering_data
;
295 to_filter_data
->done
= FALSE
;
296 to_filter_data
->status
= -1;
297 to_filter_data
->whitelisted
= 0;
299 to_filter_data
->in_thread
= (filter_th_started
!= 0);
301 to_filter_data
->in_thread
= FALSE
;
305 pthread_mutex_unlock(&list_mutex
);
307 if (filter_th_started
!= 0) {
308 debug_print("waking thread to let it filter things\n");
309 pthread_mutex_lock(&wait_mutex
);
310 pthread_cond_broadcast(&wait_cond
);
311 pthread_mutex_unlock(&wait_mutex
);
313 while (!to_filter_data
->done
) {
319 while (pthread_mutex_trylock(&list_mutex
) != 0) {
324 if (filter_th_started
== 0)
325 bsfilter_do_filter(to_filter_data
);
327 bsfilter_do_filter(to_filter_data
);
330 status
= to_filter_data
->status
;
331 whitelisted
= to_filter_data
->whitelisted
;
333 g_free(to_filter_data
);
334 to_filter_data
= NULL
;
336 pthread_mutex_unlock(&list_mutex
);
340 procmsg_msginfo_unset_flags(msginfo
, MSG_SPAM
, 0);
341 debug_print("unflagging ham: %d\n", msginfo
->msgnum
);
344 if (!whitelisted
|| (whitelisted
&& !config
.learn_from_whitelist
)) {
345 procmsg_msginfo_set_flags(msginfo
, MSG_SPAM
, 0);
346 session_stats
.spam
++;
347 debug_print("flagging spam: %d\n", msginfo
->msgnum
);
350 if (whitelisted
&& config
.learn_from_whitelist
) {
351 bsfilter_learn(msginfo
, NULL
, FALSE
);
352 procmsg_msginfo_unset_flags(msginfo
, MSG_SPAM
, 0);
353 debug_print("unflagging ham: %d\n", msginfo
->msgnum
);
356 if (MSG_IS_SPAM(msginfo
->flags
) && config
.receive_spam
) {
357 if (config
.receive_spam
&& config
.mark_as_read
)
358 procmsg_msginfo_unset_flags(msginfo
, (MSG_NEW
|MSG_UNREAD
), 0);
359 if (!config
.receive_spam
)
360 folder_item_remove_msg(msginfo
->folder
, msginfo
->msgnum
);
365 if (status
< 0 || status
> 2) { /* I/O or other errors */
369 msg
= g_strdup_printf(_("The Bsfilter plugin couldn't filter "
370 "a message. The probable cause of the "
371 "error is that it didn't learn from any mail.\n"
372 "Use \"/Mark/Mark as spam\" and \"/Mark/Mark as "
373 "ham\" to train Bsfilter with a few hundred "
374 "spam and ham messages."));
376 msg
= g_strdup_printf(_("The Bsfilter plugin couldn't filter "
377 "a message. The command `%s` couldn't be run."),
379 if (prefs_common_get_prefs()->show_recv_err_dialog
) {
381 alertpanel_error("%s", msg
);
385 log_error(LOG_PROTOCOL
, "%s\n", msg
);
391 if (config
.receive_spam
&& MSG_IS_SPAM(msginfo
->flags
)) {
392 FolderItem
*save_folder
= NULL
;
394 if ((!config
.save_folder
) ||
395 (config
.save_folder
[0] == '\0') ||
396 ((save_folder
= folder_find_item_from_identifier(config
.save_folder
)) == NULL
)) {
397 if (mail_filtering_data
->account
&& mail_filtering_data
->account
->set_trash_folder
) {
398 save_folder
= folder_find_item_from_identifier(
399 mail_filtering_data
->account
->trash_folder
);
401 debug_print("found trash folder from account's advanced settings\n");
403 if (save_folder
== NULL
&& mail_filtering_data
->account
&&
404 mail_filtering_data
->account
->folder
) {
405 save_folder
= mail_filtering_data
->account
->folder
->trash
;
407 debug_print("found trash folder from account's trash\n");
409 if (save_folder
== NULL
&& mail_filtering_data
->account
&&
410 !mail_filtering_data
->account
->folder
) {
411 if (mail_filtering_data
->account
->inbox
) {
412 FolderItem
*item
= folder_find_item_from_identifier(
413 mail_filtering_data
->account
->inbox
);
414 if (item
&& item
->folder
->trash
) {
415 save_folder
= item
->folder
->trash
;
416 debug_print("found trash folder from account's inbox\n");
419 if (!save_folder
&& mail_filtering_data
->account
->local_inbox
) {
420 FolderItem
*item
= folder_find_item_from_identifier(
421 mail_filtering_data
->account
->local_inbox
);
422 if (item
&& item
->folder
->trash
) {
423 save_folder
= item
->folder
->trash
;
424 debug_print("found trash folder from account's local_inbox\n");
428 if (save_folder
== NULL
) {
429 debug_print("using default trash folder\n");
430 save_folder
= folder_get_default_trash();
434 msginfo
->filter_op
= IS_MOVE
;
435 msginfo
->to_filter_folder
= save_folder
;
439 if (message_callback
!= NULL
)
440 message_callback(NULL
, 0, 0, FALSE
);
445 BsfilterConfig
*bsfilter_get_config(void)
450 int bsfilter_learn(MsgInfo
*msginfo
, GSList
*msglist
, gboolean spam
)
455 gchar
*bs_exec
= (config
.bspath
&& *config
.bspath
) ? config
.bspath
:"bsfilter";
457 gchar
*bs_exec
= (config
.bspath
&& *config
.bspath
) ? config
.bspath
:"bsfilterw.exe";
460 gboolean free_list
= FALSE
;
463 if (msginfo
== NULL
&& msglist
== NULL
) {
466 /* process *either* a msginfo or a msglist */
467 if (msginfo
!= NULL
&& msglist
== NULL
) {
468 msglist
= g_slist_append(NULL
, msginfo
);
471 for (cur
= msglist
; cur
; cur
= cur
->next
) {
472 msginfo
= (MsgInfo
*)cur
->data
;
473 file
= procmsg_get_message_file(msginfo
);
477 if (message_callback
!= NULL
)
478 message_callback(_("Bsfilter: learning from message..."), 0, 0, FALSE
);
481 cmd
= g_strdup_printf("%s --homedir '%s' -su '%s'", bs_exec
, get_rc_dir(), file
);
484 cmd
= g_strdup_printf("%s --homedir '%s' -cu '%s'", bs_exec
, get_rc_dir(), file
);
486 debug_print("%s\n", cmd
);
487 if ((status
= execute_command_line(cmd
, FALSE
,
488 claws_get_startup_dir())) != 0)
489 log_error(LOG_PROTOCOL
, _("Learning failed; `%s` returned with status %d."),
493 if (message_callback
!= NULL
)
494 message_callback(NULL
, 0, 0, FALSE
);
498 g_slist_free(msglist
);
503 void bsfilter_save_config(void)
508 debug_print("Saving Bsfilter Page\n");
510 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, COMMON_RC
, NULL
);
511 pfile
= prefs_write_open(rcpath
);
513 if (!pfile
|| (prefs_set_block_label(pfile
, "Bsfilter") < 0))
516 if (prefs_write_param(param
, pfile
->fp
) < 0) {
517 g_warning("failed to write Bsfilter configuration to file");
518 prefs_file_close_revert(pfile
);
521 if (fprintf(pfile
->fp
, "\n") < 0) {
522 FILE_OP_ERROR(rcpath
, "fprintf");
523 prefs_file_close_revert(pfile
);
525 prefs_file_close(pfile
);
528 void bsfilter_set_message_callback(MessageCallback callback
)
530 message_callback
= callback
;
533 gint
plugin_init(gchar
**error
)
538 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
539 VERSION_NUMERIC
, PLUGIN_NAME
, error
))
542 prefs_set_default(param
);
543 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, COMMON_RC
, NULL
);
544 prefs_read_config(param
, "Bsfilter", rcpath
, NULL
);
549 debug_print("Bsfilter plugin loaded\n");
552 bsfilter_start_thread();
555 if (config
.process_emails
) {
556 bsfilter_register_hook();
559 procmsg_register_spam_learner(bsfilter_learn
);
560 procmsg_spam_set_folder(config
.save_folder
, bsfilter_get_spam_folder
);
566 FolderItem
*bsfilter_get_spam_folder(MsgInfo
*msginfo
)
568 FolderItem
*item
= NULL
;
570 if (config
.save_folder
!= NULL
) {
571 item
= folder_find_item_from_identifier(config
.save_folder
);
574 if (item
|| msginfo
== NULL
|| msginfo
->folder
== NULL
)
577 if (msginfo
->folder
->folder
&&
578 msginfo
->folder
->folder
->account
&&
579 msginfo
->folder
->folder
->account
->set_trash_folder
) {
580 item
= folder_find_item_from_identifier(
581 msginfo
->folder
->folder
->account
->trash_folder
);
585 msginfo
->folder
->folder
&&
586 msginfo
->folder
->folder
->trash
)
587 item
= msginfo
->folder
->folder
->trash
;
590 item
= folder_get_default_trash();
592 debug_print("bs spam dir: %s\n", folder_item_get_path(item
));
596 gboolean
plugin_done(void)
598 if (hook_id
!= HOOK_NONE
) {
599 bsfilter_unregister_hook();
602 bsfilter_stop_thread();
604 g_free(config
.save_folder
);
606 procmsg_unregister_spam_learner(bsfilter_learn
);
607 procmsg_spam_set_folder(NULL
, NULL
);
608 debug_print("Bsfilter plugin unloaded\n");
612 const gchar
*plugin_name(void)
617 const gchar
*plugin_desc(void)
619 return _("This plugin can check all messages that are received from an "
620 "IMAP, LOCAL or POP account for spam using Bsfilter. "
621 "You will need Bsfilter installed locally.\n"
623 "Before Bsfilter can recognize spam messages, you have to "
624 "train it by marking a few hundred spam and ham messages "
625 "with the use of \"/Mark/Mark as spam\" and \"/Mark/Mark as "
628 "When a message is identified as spam it can be deleted or "
629 "saved in a specially designated folder.\n"
631 "Options can be found in /Configuration/Preferences/Plugins/Bsfilter");
634 const gchar
*plugin_type(void)
639 const gchar
*plugin_licence(void)
644 const gchar
*plugin_version(void)
649 struct PluginFeature
*plugin_provides(void)
651 static struct PluginFeature features
[] =
652 { {PLUGIN_FILTERING
, N_("Spam detection")},
653 {PLUGIN_FILTERING
, N_("Spam learning")},
654 {PLUGIN_NOTHING
, NULL
}};
658 void bsfilter_register_hook(void)
660 if (hook_id
== HOOK_NONE
)
661 hook_id
= hooks_register_hook(MAIL_FILTERING_HOOKLIST
, mail_filtering_hook
, NULL
);
662 if (hook_id
== HOOK_NONE
) {
663 g_warning("failed to register mail filtering hook");
664 config
.process_emails
= FALSE
;
668 void bsfilter_unregister_hook(void)
670 if (hook_id
!= HOOK_NONE
) {
671 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST
, hook_id
);