2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 the Claws Mail Team
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"
48 #include "spamassassin.h"
51 #include "prefs_common.h"
52 #include "alertpanel.h"
53 #include "addr_compl.h"
55 #ifdef HAVE_SYSEXITS_H
61 #ifdef HAVE_SYS_ERRNO_H
62 #include <sys/errno.h>
67 #ifdef HAVE_SYS_TIME_H
77 #define PLUGIN_NAME (_("SpamAssassin"))
80 CHILD_RUNNING = 1 << 0,
81 TIMEOUT_RUNNING = 1 << 1,
84 static guint hook_id = -1;
85 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
86 static MessageCallback message_callback;
88 static SpamAssassinConfig config;
90 static PrefParam param[] = {
91 {"enable", "FALSE", &config.enable, P_BOOL,
93 {"transport", "0", &config.transport, P_INT,
95 {"hostname", "localhost", &config.hostname, P_STRING,
97 {"port", "783", &config.port, P_INT,
99 {"socket", "", &config.socket, P_STRING,
101 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
103 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
105 {"save_folder", NULL, &config.save_folder, P_STRING,
107 {"max_size", "250", &config.max_size, P_INT,
109 {"timeout", "30", &config.timeout, P_INT,
111 {"username", "", &config.username, P_STRING,
113 {"mark_as_read", "TRUE", &config.mark_as_read, P_BOOL,
115 {"whitelist_ab", "FALSE", &config.whitelist_ab, P_BOOL,
117 {"whitelist_ab_folder", N_("Any"), &config.whitelist_ab_folder, P_STRING,
120 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
123 gboolean timeout_func(gpointer data)
125 gint *running = (gint *) data;
127 if (*running & CHILD_RUNNING)
130 *running &= ~TIMEOUT_RUNNING;
137 MSG_FILTERING_ERROR = 2
140 static MsgStatus msg_is_spam(FILE *fp)
142 struct transport trans;
144 gboolean is_spam = FALSE;
149 transport_init(&trans);
150 switch (config.transport) {
151 case SPAMASSASSIN_TRANSPORT_LOCALHOST:
152 trans.type = TRANSPORT_LOCALHOST;
153 trans.port = config.port;
155 case SPAMASSASSIN_TRANSPORT_TCP:
156 trans.type = TRANSPORT_TCP;
157 trans.hostname = config.hostname;
158 trans.port = config.port;
160 case SPAMASSASSIN_TRANSPORT_UNIX:
161 trans.type = TRANSPORT_UNIX;
162 trans.socketpath = config.socket;
168 if (transport_setup(&trans, flags) != EX_OK) {
169 log_error(LOG_PROTOCOL, _("SpamAssassin plugin couldn't connect to spamd.\n"));
170 debug_print("failed to setup transport\n");
171 return MSG_FILTERING_ERROR;
174 m.type = MESSAGE_NONE;
175 m.max_len = config.max_size * 1024;
176 m.timeout = config.timeout;
178 if (message_read(fileno(fp), flags, &m) != EX_OK) {
179 debug_print("failed to read message\n");
181 return MSG_FILTERING_ERROR;
184 if (message_filter(&trans, config.username, flags, &m) != EX_OK) {
185 log_error(LOG_PROTOCOL, _("SpamAssassin plugin filtering failed.\n"));
186 debug_print("filtering the message failed\n");
188 return MSG_FILTERING_ERROR;
191 if (m.is_spam == EX_ISSPAM)
196 return is_spam ? MSG_IS_SPAM:MSG_IS_HAM;
199 static gboolean mail_filtering_hook(gpointer source, gpointer data)
201 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
202 MsgInfo *msginfo = mail_filtering_data->msginfo;
203 gboolean is_spam = FALSE, error = FALSE;
204 static gboolean warned_error = FALSE;
209 /* SPAMASSASSIN_DISABLED : keep test for compatibility purpose */
210 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
211 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is disabled by its preferences.\n"));
214 debug_print("Filtering message %d\n", msginfo->msgnum);
215 if (message_callback != NULL)
216 message_callback(_("SpamAssassin: filtering message..."));
218 if ((fp = procmsg_open_message(msginfo)) == NULL) {
219 debug_print("failed to open message file\n");
223 if (config.whitelist_ab) {
224 gchar *ab_folderpath;
225 gboolean whitelisted = FALSE;
227 if (*config.whitelist_ab_folder == '\0' ||
228 strcasecmp(config.whitelist_ab_folder, "Any") == 0) {
229 /* match the whole addressbook */
230 ab_folderpath = NULL;
232 /* match the specific book/folder of the addressbook */
233 ab_folderpath = config.whitelist_ab_folder;
236 start_address_completion(ab_folderpath);
238 found_in_addressbook(msginfo->from))
240 end_address_completion();
243 debug_print("message is ham (whitelisted)\n");
250 _exit(msg_is_spam(fp));
254 running |= CHILD_RUNNING;
256 g_timeout_add(50, timeout_func, &running);
257 running |= TIMEOUT_RUNNING;
259 while(running & CHILD_RUNNING) {
262 ret = waitpid(pid, &status, WNOHANG);
264 if (WIFEXITED(status)) {
265 MsgStatus result = MSG_IS_HAM;
266 running &= ~CHILD_RUNNING;
267 result = WEXITSTATUS(status);
268 is_spam = (result == MSG_IS_SPAM) ? TRUE : FALSE;
269 error = (result == MSG_FILTERING_ERROR);
272 running &= ~CHILD_RUNNING;
273 } /* ret == 0 continue */
275 g_main_context_iteration(NULL, TRUE);
278 while (running & TIMEOUT_RUNNING)
279 g_main_context_iteration(NULL, TRUE);
285 debug_print("message is spam\n");
286 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
287 if (config.receive_spam) {
288 FolderItem *save_folder = NULL;
290 if ((!config.save_folder) ||
291 (config.save_folder[0] == '\0') ||
292 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
293 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
294 save_folder = folder_find_item_from_identifier(
295 mail_filtering_data->account->trash_folder);
297 debug_print("found trash folder from account's advanced settings\n");
299 if (save_folder == NULL && mail_filtering_data->account &&
300 mail_filtering_data->account->folder) {
301 save_folder = mail_filtering_data->account->folder->trash;
303 debug_print("found trash folder from account's trash\n");
305 if (save_folder == NULL && mail_filtering_data->account &&
306 !mail_filtering_data->account->folder) {
307 if (mail_filtering_data->account->inbox) {
308 FolderItem *item = folder_find_item_from_identifier(
309 mail_filtering_data->account->inbox);
310 if (item && item->folder->trash) {
311 save_folder = item->folder->trash;
312 debug_print("found trash folder from account's inbox\n");
315 if (!save_folder && mail_filtering_data->account->local_inbox) {
316 FolderItem *item = folder_find_item_from_identifier(
317 mail_filtering_data->account->local_inbox);
318 if (item && item->folder->trash) {
319 save_folder = item->folder->trash;
320 debug_print("found trash folder from account's local_inbox\n");
324 if (save_folder == NULL) {
325 debug_print("using default trash folder\n");
326 save_folder = folder_get_default_trash();
329 if (config.mark_as_read)
330 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
331 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
332 msginfo->filter_op = IS_MOVE;
333 msginfo->to_filter_folder = save_folder;
335 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
340 debug_print("message is ham\n");
341 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
345 gchar *msg = _("The SpamAssassin plugin couldn't filter "
346 "a message. The probable cause of the error "
347 "is an unreachable spamd daemon. Please make "
348 "sure spamd is running and accessible.");
349 if (!prefs_common_get_prefs()->no_recv_err_panel) {
351 alertpanel_error("%s", msg);
355 log_error(LOG_PROTOCOL, "%s\n", msg);
362 SpamAssassinConfig *spamassassin_get_config(void)
367 gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
370 gchar *fname = get_tmp_file();
373 contents = g_strdup_printf(
374 "spamc -d %s -p %u -u %s -t %u -s %u -L %s<\"$*\";exit $?",
375 config.hostname, config.port,
376 config.username, config.timeout,
377 config.max_size * 1024, spam?"spam":"ham");
378 if (str_write_to_file(contents, fname) < 0) {
384 /* returned pointer must be free'ed by caller */
388 int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
392 const gchar *shell = g_getenv("SHELL");
393 gchar *spamc_wrapper = NULL;
395 if (msginfo == NULL && msglist == NULL) {
399 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
400 && prefs_common_get_prefs()->work_offline
401 && !inc_offline_should_override(TRUE,
402 _("Claws Mail needs network access in order "
403 "to feed the mail to the remote learner."))) {
408 file = procmsg_get_message_file(msginfo);
412 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
413 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
414 if (spamc_wrapper != NULL) {
415 cmd = g_strconcat(shell?shell:"sh", " ",
416 spamc_wrapper, " ", file, NULL);
419 cmd = g_strdup_printf("sa-learn -u %s%s %s %s",
421 prefs_common_get_prefs()->work_offline?" -L":"",
422 spam?"--spam":"--ham", file);
426 GSList *cur = msglist;
429 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
430 /* execute n-times the spamc command */
431 for (; cur; cur = cur->next) {
432 info = (MsgInfo *)cur->data;
433 gchar *tmpcmd = NULL;
434 gchar *tmpfile = get_tmp_file();
436 if (spamc_wrapper == NULL) {
437 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
440 if (spamc_wrapper && tmpfile &&
441 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
442 tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
444 debug_print("%s\n", tmpcmd);
445 execute_command_line(tmpcmd, FALSE, NULL);
450 g_free(spamc_wrapper);
453 cmd = g_strdup_printf("sa-learn -u %s%s %s",
455 prefs_common_get_prefs()->work_offline?" -L":"",
456 spam?"--spam":"--ham");
458 /* concatenate all message tmpfiles to the sa-learn command-line */
459 for (; cur; cur = cur->next) {
460 info = (MsgInfo *)cur->data;
461 gchar *tmpcmd = NULL;
462 gchar *tmpfile = get_tmp_file();
465 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
466 tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
477 debug_print("%s\n", cmd);
478 /* only run sync calls to sa-learn/spamc to prevent system lockdown */
479 execute_command_line(cmd, FALSE, NULL);
481 g_free(spamc_wrapper);
486 void spamassassin_save_config(void)
491 debug_print("Saving SpamAssassin Page\n");
493 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
494 pfile = prefs_write_open(rcpath);
496 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
499 if (prefs_write_param(param, pfile->fp) < 0) {
500 g_warning("Failed to write SpamAssassin configuration to file");
501 prefs_file_close_revert(pfile);
504 if (fprintf(pfile->fp, "\n") < 0) {
505 FILE_OP_ERROR(rcpath, "fprintf");
506 prefs_file_close_revert(pfile);
508 prefs_file_close(pfile);
511 gboolean spamassassin_check_username(void)
513 if (config.username == NULL || config.username[0] == '\0') {
514 config.username = (gchar*)g_get_user_name();
515 if (config.username == NULL) {
516 if (hook_id != (guint) -1) {
517 spamassassin_unregister_hook();
519 procmsg_unregister_spam_learner(spamassassin_learn);
520 procmsg_spam_set_folder(NULL, NULL);
527 void spamassassin_set_message_callback(MessageCallback callback)
529 message_callback = callback;
532 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, "SpamAssassin", rcpath, NULL);
546 if (!spamassassin_check_username()) {
547 *error = g_strdup(_("Failed to get username"));
550 spamassassin_gtk_init();
552 debug_print("SpamAssassin plugin loaded\n");
554 if (config.process_emails) {
555 spamassassin_register_hook();
558 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
559 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is loaded but disabled by its preferences.\n"));
562 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
563 debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
564 procmsg_register_spam_learner(spamassassin_learn);
565 procmsg_spam_set_folder(config.save_folder, spamassassin_get_spam_folder);
572 gboolean plugin_done(void)
574 if (hook_id != (guint) -1) {
575 spamassassin_unregister_hook();
577 g_free(config.hostname);
578 g_free(config.save_folder);
579 spamassassin_gtk_done();
580 procmsg_unregister_spam_learner(spamassassin_learn);
581 procmsg_spam_set_folder(NULL, NULL);
582 debug_print("SpamAssassin plugin unloaded\n");
586 const gchar *plugin_name(void)
591 const gchar *plugin_desc(void)
593 return _("This plugin can check all messages that are received from an "
594 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
595 "server. You will need a SpamAssassin Server (spamd) running "
598 "It can also be used for marking messages as Ham or Spam.\n"
600 "When a message is identified as spam it can be deleted or "
601 "saved in a specially designated folder.\n"
603 "Options can be found in /Configuration/Preferences/Plugins/SpamAssassin");
606 const gchar *plugin_type(void)
611 const gchar *plugin_licence(void)
616 const gchar *plugin_version(void)
621 struct PluginFeature *plugin_provides(void)
623 static struct PluginFeature features[] =
624 { {PLUGIN_FILTERING, N_("Spam detection")},
625 {PLUGIN_FILTERING, N_("Spam learning")},
626 {PLUGIN_NOTHING, NULL}};
630 void spamassassin_register_hook(void)
632 if (hook_id == (guint) -1)
633 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
634 if (hook_id == (guint) -1) {
635 g_warning("Failed to register mail filtering hook");
636 config.process_emails = FALSE;
640 void spamassassin_unregister_hook(void)
642 if (hook_id != (guint) -1) {
643 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
648 FolderItem *spamassassin_get_spam_folder(MsgInfo *msginfo)
650 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
652 if (item || msginfo == NULL || msginfo->folder == NULL)
655 if (msginfo->folder->folder &&
656 msginfo->folder->folder->account &&
657 msginfo->folder->folder->account->set_trash_folder) {
658 item = folder_find_item_from_identifier(
659 msginfo->folder->folder->account->trash_folder);
663 msginfo->folder->folder &&
664 msginfo->folder->folder->trash)
665 item = msginfo->folder->folder->trash;
668 item = folder_get_default_trash();
670 debug_print("SA spam dir: %s\n", folder_item_get_path(item));