support image/x-eps format via pdf_viewer
[claws.git] / src / plugins / bsfilter / bsfilter.c
blob80976d1d832a49bbba3b568f3347ade9d26e250e
1 /*
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/>.
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #include "claws-features.h"
24 #endif
26 #include <glib.h>
27 #include <glib/gi18n.h>
29 #include "defs.h"
31 #include <sys/types.h>
32 #if HAVE_SYS_WAIT_H
33 #include <sys/wait.h>
34 #endif
35 #ifdef USE_PTHREAD
36 #include <pthread.h>
37 #endif
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 #include "utils.h"
50 #include "bsfilter.h"
51 #include "main.h"
52 #include "inc.h"
53 #include "log.h"
54 #include "prefs_common.h"
55 #include "alertpanel.h"
56 #include "addr_compl.h"
58 #ifdef HAVE_SYSEXITS_H
59 #include <sysexits.h>
60 #endif
61 #ifdef HAVE_ERRNO_H
62 #include <errno.h>
63 #endif
64 #ifdef HAVE_SYS_ERRNO_H
65 #include <sys/errno.h>
66 #endif
67 #ifdef HAVE_TIME_H
68 #include <time.h>
69 #endif
70 #ifdef HAVE_SYS_TIME_H
71 #include <sys/time.h>
72 #endif
73 #ifdef HAVE_SIGNAL_H
74 #include <signal.h>
75 #endif
76 #ifdef HAVE_PWD_H
77 #include <pwd.h>
78 #endif
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,
91 NULL, NULL, NULL},
92 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
93 NULL, NULL, NULL},
94 {"save_folder", NULL, &config.save_folder, P_STRING,
95 NULL, NULL, NULL},
96 {"max_size", "250", &config.max_size, P_INT,
97 NULL, NULL, NULL},
98 #ifndef G_OS_WIN32
99 {"bspath", "bsfilter", &config.bspath, P_STRING,
100 NULL, NULL, NULL},
101 #else
102 {"bspath", "bsfilterw.exe", &config.bspath, P_STRING,
103 NULL, NULL, NULL},
104 #endif
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},
114 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
117 typedef struct _BsFilterData {
118 MailFilteringData *mail_filtering_data;
119 gchar **bs_args;
120 MsgInfo *msginfo;
121 gboolean done;
122 int status;
123 int whitelisted;
124 gboolean in_thread;
125 } BsFilterData;
127 static BsFilterData *to_filter_data = NULL;
128 #ifdef USE_PTHREAD
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;
133 #endif
135 static void bsfilter_do_filter(BsFilterData *data)
137 int status = 0;
138 gchar *file = NULL;
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;
149 } else {
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))
161 whitelisted = TRUE;
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);
168 if (file) {
169 #ifndef G_OS_WIN32
170 gchar *classify = g_strconcat((config.bspath && *config.bspath) ? config.bspath:"bsfilter",
171 " --homedir '",get_rc_dir(),"' '", file, "'", NULL);
172 #else
173 gchar *classify = g_strconcat((config.bspath && *config.bspath) ? config.bspath:"bsfilterw.exe",
174 " --homedir '",get_rc_dir(),"' '", file, "'", NULL);
175 #endif
176 status = execute_command_line(classify, FALSE,
177 claws_get_startup_dir());
178 g_free(classify);
181 if (config.whitelist_ab)
182 end_address_completion();
184 to_filter_data->status = status;
185 to_filter_data->whitelisted = whitelisted;
188 #ifdef USE_PTHREAD
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);
199 } else {
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;
205 g_usleep(100);
208 return NULL;
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)
218 return;
219 if (pthread_create(&filter_th, NULL,
220 bsfilter_filtering_thread,
221 NULL) != 0) {
222 filter_th_started = 0;
223 return;
225 debug_print("thread created\n");
226 filter_th_started = 1;
229 static void bsfilter_stop_thread(void)
231 void *res;
232 while (pthread_mutex_trylock(&list_mutex) != 0) {
233 GTK_EVENTS_FLUSH();
234 g_usleep(100);
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");
248 #endif
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;
256 #ifndef G_OS_WIN32
257 gchar *bs_exec = (config.bspath && *config.bspath) ? config.bspath:"bsfilter";
258 #else
259 gchar *bs_exec = (config.bspath && *config.bspath) ? config.bspath:"bsfilterw.exe";
260 #endif
261 gboolean filtered = FALSE;
263 if (!config.process_emails) {
264 return filtered;
267 if (msginfo == NULL) {
268 g_warning("wrong call to bsfilter mail_filtering_hook");
269 return filtered;
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);
275 if (msginfo) {
276 gchar *file = procmsg_get_message_file(msginfo);
277 g_free(file);
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);
285 #ifdef USE_PTHREAD
286 while (pthread_mutex_trylock(&list_mutex) != 0) {
287 GTK_EVENTS_FLUSH();
288 g_usleep(100);
290 #endif
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;
298 #ifdef USE_PTHREAD
299 to_filter_data->in_thread = (filter_th_started != 0);
300 #else
301 to_filter_data->in_thread = FALSE;
302 #endif
304 #ifdef USE_PTHREAD
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) {
314 GTK_EVENTS_FLUSH();
315 g_usleep(100);
319 while (pthread_mutex_trylock(&list_mutex) != 0) {
320 GTK_EVENTS_FLUSH();
321 g_usleep(100);
324 if (filter_th_started == 0)
325 bsfilter_do_filter(to_filter_data);
326 #else
327 bsfilter_do_filter(to_filter_data);
328 #endif
330 status = to_filter_data->status;
331 whitelisted = to_filter_data->whitelisted;
333 g_free(to_filter_data);
334 to_filter_data = NULL;
335 #ifdef USE_PTHREAD
336 pthread_mutex_unlock(&list_mutex);
337 #endif
339 if (status == 1) {
340 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
341 debug_print("unflagging ham: %d\n", msginfo->msgnum);
342 filtered = FALSE;
343 } else {
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);
348 filtered = TRUE;
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);
354 filtered = FALSE;
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);
361 filtered = TRUE;
365 if (status < 0 || status > 2) { /* I/O or other errors */
366 gchar *msg = NULL;
368 if (status == 3)
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."));
375 else
376 msg = g_strdup_printf(_("The Bsfilter plugin couldn't filter "
377 "a message. The command `%s` couldn't be run."),
378 bs_exec);
379 if (prefs_common_get_prefs()->show_recv_err_dialog) {
380 if (!warned_error) {
381 alertpanel_error("%s", msg);
383 warned_error = TRUE;
384 } else {
385 log_error(LOG_PROTOCOL, "%s\n", msg);
387 g_free(msg);
390 if (status == 0) {
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);
400 if (save_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;
406 if (save_folder)
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();
433 if (save_folder) {
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);
442 return filtered;
445 BsfilterConfig *bsfilter_get_config(void)
447 return &config;
450 int bsfilter_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
452 gchar *cmd = NULL;
453 gchar *file = NULL;
454 #ifndef G_OS_WIN32
455 gchar *bs_exec = (config.bspath && *config.bspath) ? config.bspath:"bsfilter";
456 #else
457 gchar *bs_exec = (config.bspath && *config.bspath) ? config.bspath:"bsfilterw.exe";
458 #endif
459 gint status = 0;
460 gboolean free_list = FALSE;
461 GSList *cur = NULL;
463 if (msginfo == NULL && msglist == NULL) {
464 return -1;
466 /* process *either* a msginfo or a msglist */
467 if (msginfo != NULL && msglist == NULL) {
468 msglist = g_slist_append(NULL, msginfo);
469 free_list = TRUE;
471 for (cur = msglist; cur; cur = cur->next) {
472 msginfo = (MsgInfo *)cur->data;
473 file = procmsg_get_message_file(msginfo);
474 if (file == NULL) {
475 return -1;
476 } else {
477 if (message_callback != NULL)
478 message_callback(_("Bsfilter: learning from message..."), 0, 0, FALSE);
479 if (spam)
480 /* learn as spam */
481 cmd = g_strdup_printf("%s --homedir '%s' -su '%s'", bs_exec, get_rc_dir(), file);
482 else
483 /* learn as ham */
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."),
490 cmd, status);
491 g_free(cmd);
492 g_free(file);
493 if (message_callback != NULL)
494 message_callback(NULL, 0, 0, FALSE);
497 if (free_list)
498 g_slist_free(msglist);
500 return 0;
503 void bsfilter_save_config(void)
505 PrefFile *pfile;
506 gchar *rcpath;
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);
512 g_free(rcpath);
513 if (!pfile || (prefs_set_block_label(pfile, "Bsfilter") < 0))
514 return;
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);
519 return;
521 if (fprintf(pfile->fp, "\n") < 0) {
522 FILE_OP_ERROR(rcpath, "fprintf");
523 prefs_file_close_revert(pfile);
524 } else
525 prefs_file_close(pfile);
528 void bsfilter_set_message_callback(MessageCallback callback)
530 message_callback = callback;
533 gint plugin_init(gchar **error)
535 gchar *rcpath;
536 hook_id = HOOK_NONE;
538 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
539 VERSION_NUMERIC, PLUGIN_NAME, error))
540 return -1;
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);
545 g_free(rcpath);
547 bsfilter_gtk_init();
549 debug_print("Bsfilter plugin loaded\n");
551 #ifdef USE_PTHREAD
552 bsfilter_start_thread();
553 #endif
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);
562 return 0;
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)
575 return item;
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);
584 if (item == NULL &&
585 msginfo->folder->folder &&
586 msginfo->folder->folder->trash)
587 item = msginfo->folder->folder->trash;
589 if (item == NULL)
590 item = folder_get_default_trash();
592 debug_print("bs spam dir: %s\n", folder_item_get_path(item));
593 return item;
596 gboolean plugin_done(void)
598 if (hook_id != HOOK_NONE) {
599 bsfilter_unregister_hook();
601 #ifdef USE_PTHREAD
602 bsfilter_stop_thread();
603 #endif
604 g_free(config.save_folder);
605 bsfilter_gtk_done();
606 procmsg_unregister_spam_learner(bsfilter_learn);
607 procmsg_spam_set_folder(NULL, NULL);
608 debug_print("Bsfilter plugin unloaded\n");
609 return TRUE;
612 const gchar *plugin_name(void)
614 return PLUGIN_NAME;
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"
622 "\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 "
626 "ham\".\n"
627 "\n"
628 "When a message is identified as spam it can be deleted or "
629 "saved in a specially designated folder.\n"
630 "\n"
631 "Options can be found in /Configuration/Preferences/Plugins/Bsfilter");
634 const gchar *plugin_type(void)
636 return "GTK3";
639 const gchar *plugin_licence(void)
641 return "GPL3+";
644 const gchar *plugin_version(void)
646 return VERSION;
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}};
655 return features;
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);
673 hook_id = HOOK_NONE;