2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 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/>.
26 #include <sys/types.h>
30 #include <glib/gi18n.h>
36 #include "common/claws.h"
37 #include "common/version.h"
39 #include "common/utils.h"
44 #include "prefs_gtk.h"
47 #include "spamassassin.h"
50 #include "prefs_common.h"
51 #include "alertpanel.h"
52 #include "addr_compl.h"
54 #ifdef HAVE_SYSEXITS_H
60 #ifdef HAVE_SYS_ERRNO_H
61 #include <sys/errno.h>
66 #ifdef HAVE_SYS_TIME_H
76 #define PLUGIN_NAME (_("SpamAssassin"))
79 CHILD_RUNNING = 1 << 0,
80 TIMEOUT_RUNNING = 1 << 1,
83 static guint hook_id = -1;
84 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
85 static MessageCallback message_callback;
87 static SpamAssassinConfig config;
89 static PrefParam param[] = {
90 {"enable", "FALSE", &config.enable, P_BOOL,
92 {"transport", "0", &config.transport, P_INT,
94 {"hostname", "localhost", &config.hostname, P_STRING,
96 {"port", "783", &config.port, P_INT,
98 {"socket", "", &config.socket, P_STRING,
100 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
102 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
104 {"save_folder", NULL, &config.save_folder, P_STRING,
106 {"max_size", "250", &config.max_size, P_INT,
108 {"timeout", "30", &config.timeout, P_INT,
110 {"username", "", &config.username, P_STRING,
112 {"mark_as_read", "TRUE", &config.mark_as_read, P_BOOL,
114 {"whitelist_ab", "FALSE", &config.whitelist_ab, P_BOOL,
116 {"whitelist_ab_folder", N_("Any"), &config.whitelist_ab_folder, P_STRING,
119 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
122 gboolean timeout_func(gpointer data)
124 gint *running = (gint *) data;
126 if (*running & CHILD_RUNNING)
129 *running &= ~TIMEOUT_RUNNING;
136 MSG_FILTERING_ERROR = 2
139 static MsgStatus msg_is_spam(FILE *fp)
141 struct transport trans;
143 gboolean is_spam = FALSE;
148 transport_init(&trans);
149 switch (config.transport) {
150 case SPAMASSASSIN_TRANSPORT_LOCALHOST:
151 trans.type = TRANSPORT_LOCALHOST;
152 trans.port = config.port;
154 case SPAMASSASSIN_TRANSPORT_TCP:
155 trans.type = TRANSPORT_TCP;
156 trans.hostname = config.hostname;
157 trans.port = config.port;
159 case SPAMASSASSIN_TRANSPORT_UNIX:
160 trans.type = TRANSPORT_UNIX;
161 trans.socketpath = config.socket;
167 if (transport_setup(&trans, flags) != EX_OK) {
168 log_error(LOG_PROTOCOL, _("SpamAssassin plugin couldn't connect to spamd.\n"));
169 debug_print("failed to setup transport\n");
170 return MSG_FILTERING_ERROR;
173 m.type = MESSAGE_NONE;
174 m.max_len = config.max_size * 1024;
175 m.timeout = config.timeout;
177 if (message_read(fileno(fp), flags, &m) != EX_OK) {
178 debug_print("failed to read message\n");
180 return MSG_FILTERING_ERROR;
183 if (message_filter(&trans, config.username, flags, &m) != EX_OK) {
184 log_error(LOG_PROTOCOL, _("SpamAssassin plugin filtering failed.\n"));
185 debug_print("filtering the message failed\n");
187 return MSG_FILTERING_ERROR;
190 if (m.is_spam == EX_ISSPAM)
195 return is_spam ? MSG_IS_SPAM:MSG_IS_HAM;
198 static gboolean sa_found_in_addressbook(const gchar *address)
201 gboolean found = FALSE;
207 addr = g_strdup(address);
208 extract_address(addr);
209 num_addr = complete_address(addr);
211 /* skip first item (this is the search string itself) */
213 for (; i < num_addr && !found; i++) {
214 gchar *caddr = get_complete_address(i);
215 extract_address(caddr);
216 if (strcasecmp(caddr, addr) == 0)
225 static gboolean mail_filtering_hook(gpointer source, gpointer data)
227 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
228 MsgInfo *msginfo = mail_filtering_data->msginfo;
229 gboolean is_spam = FALSE, error = FALSE;
230 static gboolean warned_error = FALSE;
235 /* SPAMASSASSIN_DISABLED : keep test for compatibility purpose */
236 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
237 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is disabled by its preferences.\n"));
240 debug_print("Filtering message %d\n", msginfo->msgnum);
241 if (message_callback != NULL)
242 message_callback(_("SpamAssassin: filtering message..."));
244 if ((fp = procmsg_open_message(msginfo)) == NULL) {
245 debug_print("failed to open message file\n");
249 if (config.whitelist_ab) {
250 gchar *ab_folderpath;
251 gboolean whitelisted = FALSE;
253 if (*config.whitelist_ab_folder == '\0' ||
254 strcasecmp(config.whitelist_ab_folder, "Any") == 0) {
255 /* match the whole addressbook */
256 ab_folderpath = NULL;
258 /* match the specific book/folder of the addressbook */
259 ab_folderpath = config.whitelist_ab_folder;
262 start_address_completion(ab_folderpath);
264 sa_found_in_addressbook(msginfo->from))
266 end_address_completion();
269 debug_print("message is ham (whitelisted)\n");
276 _exit(msg_is_spam(fp));
280 running |= CHILD_RUNNING;
282 g_timeout_add(50, timeout_func, &running);
283 running |= TIMEOUT_RUNNING;
285 while(running & CHILD_RUNNING) {
288 ret = waitpid(pid, &status, WNOHANG);
290 if (WIFEXITED(status)) {
291 MsgStatus result = MSG_IS_HAM;
292 running &= ~CHILD_RUNNING;
293 result = WEXITSTATUS(status);
294 is_spam = (result == MSG_IS_SPAM) ? TRUE : FALSE;
295 error = (result == MSG_FILTERING_ERROR);
298 running &= ~CHILD_RUNNING;
299 } /* ret == 0 continue */
301 g_main_context_iteration(NULL, TRUE);
304 while (running & TIMEOUT_RUNNING)
305 g_main_context_iteration(NULL, TRUE);
311 debug_print("message is spam\n");
312 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
313 if (config.receive_spam) {
314 FolderItem *save_folder = NULL;
316 if ((!config.save_folder) ||
317 (config.save_folder[0] == '\0') ||
318 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
319 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
320 save_folder = folder_find_item_from_identifier(
321 mail_filtering_data->account->trash_folder);
323 debug_print("found trash folder from account's advanced settings\n");
325 if (save_folder == NULL && mail_filtering_data->account &&
326 mail_filtering_data->account->folder) {
327 save_folder = mail_filtering_data->account->folder->trash;
329 debug_print("found trash folder from account's trash\n");
331 if (save_folder == NULL && mail_filtering_data->account &&
332 !mail_filtering_data->account->folder) {
333 if (mail_filtering_data->account->inbox) {
334 FolderItem *item = folder_find_item_from_identifier(
335 mail_filtering_data->account->inbox);
336 if (item && item->folder->trash) {
337 save_folder = item->folder->trash;
338 debug_print("found trash folder from account's inbox\n");
341 if (!save_folder && mail_filtering_data->account->local_inbox) {
342 FolderItem *item = folder_find_item_from_identifier(
343 mail_filtering_data->account->local_inbox);
344 if (item && item->folder->trash) {
345 save_folder = item->folder->trash;
346 debug_print("found trash folder from account's local_inbox\n");
350 if (save_folder == NULL) {
351 debug_print("using default trash folder\n");
352 save_folder = folder_get_default_trash();
355 if (config.mark_as_read)
356 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
357 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
358 msginfo->filter_op = IS_MOVE;
359 msginfo->to_filter_folder = save_folder;
361 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
366 debug_print("message is ham\n");
367 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
371 gchar *msg = _("The SpamAssassin plugin couldn't filter "
372 "a message. The probable cause of the error "
373 "is an unreachable spamd daemon. Please make "
374 "sure spamd is running and accessible.");
375 if (!prefs_common.no_recv_err_panel) {
377 alertpanel_error("%s", msg);
381 log_error(LOG_PROTOCOL, "%s\n", msg);
388 SpamAssassinConfig *spamassassin_get_config(void)
393 gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
396 gchar *fname = get_tmp_file();
399 contents = g_strdup_printf(
400 "spamc -d %s -p %u -u %s -t %u -s %u -L %s<\"$*\";exit $?",
401 config.hostname, config.port,
402 config.username, config.timeout,
403 config.max_size * 1024, spam?"spam":"ham");
404 if (str_write_to_file(contents, fname) < 0) {
410 /* returned pointer must be free'ed by caller */
414 int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
418 const gchar *shell = g_getenv("SHELL");
419 gchar *spamc_wrapper = NULL;
421 if (msginfo == NULL && msglist == NULL) {
425 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
426 && prefs_common.work_offline
427 && !inc_offline_should_override(TRUE,
428 _("Claws Mail needs network access in order "
429 "to feed this mail(s) to the remote learner."))) {
434 file = procmsg_get_message_file(msginfo);
438 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
439 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
440 if (spamc_wrapper != NULL) {
441 cmd = g_strconcat(shell?shell:"sh", " ",
442 spamc_wrapper, " ", file, NULL);
445 cmd = g_strdup_printf("sa-learn -u %s%s %s %s",
447 prefs_common.work_offline?" -L":"",
448 spam?"--spam":"--ham", file);
452 GSList *cur = msglist;
455 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
456 /* execute n-times the spamc command */
457 for (; cur; cur = cur->next) {
458 info = (MsgInfo *)cur->data;
459 gchar *tmpcmd = NULL;
460 gchar *tmpfile = get_tmp_file();
462 if (spamc_wrapper == NULL) {
463 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
466 if (spamc_wrapper && tmpfile &&
467 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
468 tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
470 debug_print("%s\n", tmpcmd);
471 execute_command_line(tmpcmd, FALSE);
476 g_free(spamc_wrapper);
479 cmd = g_strdup_printf("sa-learn -u %s%s %s",
481 prefs_common.work_offline?" -L":"",
482 spam?"--spam":"--ham");
484 /* concatenate all message tmpfiles to the sa-learn command-line */
485 for (; cur; cur = cur->next) {
486 info = (MsgInfo *)cur->data;
487 gchar *tmpcmd = NULL;
488 gchar *tmpfile = get_tmp_file();
491 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
492 tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
503 debug_print("%s\n", cmd);
504 /* only run sync calls to sa-learn/spamc to prevent system lockdown */
505 execute_command_line(cmd, FALSE);
507 g_free(spamc_wrapper);
512 void spamassassin_save_config(void)
517 debug_print("Saving SpamAssassin Page\n");
519 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
520 pfile = prefs_write_open(rcpath);
522 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
525 if (prefs_write_param(param, pfile->fp) < 0) {
526 g_warning("Failed to write SpamAssassin configuration to file\n");
527 prefs_file_close_revert(pfile);
530 if (fprintf(pfile->fp, "\n") < 0) {
531 FILE_OP_ERROR(rcpath, "fprintf");
532 prefs_file_close_revert(pfile);
534 prefs_file_close(pfile);
537 gboolean spamassassin_check_username(void)
539 if (config.username == NULL || config.username[0] == '\0') {
540 config.username = (gchar*)g_get_user_name();
541 if (config.username == NULL) {
543 spamassassin_unregister_hook();
545 procmsg_unregister_spam_learner(spamassassin_learn);
546 procmsg_spam_set_folder(NULL, NULL);
553 void spamassassin_set_message_callback(MessageCallback callback)
555 message_callback = callback;
558 gint plugin_init(gchar **error)
564 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
565 VERSION_NUMERIC, PLUGIN_NAME, error))
568 prefs_set_default(param);
569 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
570 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
572 if (!spamassassin_check_username()) {
573 *error = g_strdup(_("Failed to get username"));
576 spamassassin_gtk_init();
578 debug_print("SpamAssassin plugin loaded\n");
580 if (config.process_emails) {
581 spamassassin_register_hook();
584 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
585 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is loaded but disabled by its preferences.\n"));
588 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
589 debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
590 procmsg_register_spam_learner(spamassassin_learn);
591 procmsg_spam_set_folder(config.save_folder, spamassassin_get_spam_folder);
598 gboolean plugin_done(void)
601 spamassassin_unregister_hook();
603 g_free(config.hostname);
604 g_free(config.save_folder);
605 spamassassin_gtk_done();
606 procmsg_unregister_spam_learner(spamassassin_learn);
607 procmsg_spam_set_folder(NULL, NULL);
608 debug_print("SpamAssassin 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 a SpamAssassin "
621 "server. You will need a SpamAssassin Server (spamd) running "
624 "It can also be used for marking messages as Ham or Spam.\n"
626 "When a message is identified as spam it can be deleted or "
627 "saved in a specially designated folder.\n"
629 "Options can be found in /Configuration/Preferences/Plugins/SpamAssassin");
632 const gchar *plugin_type(void)
637 const gchar *plugin_licence(void)
642 const gchar *plugin_version(void)
647 struct PluginFeature *plugin_provides(void)
649 static struct PluginFeature features[] =
650 { {PLUGIN_FILTERING, N_("Spam detection")},
651 {PLUGIN_FILTERING, N_("Spam learning")},
652 {PLUGIN_NOTHING, NULL}};
656 void spamassassin_register_hook(void)
659 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
661 g_warning("Failed to register mail filtering hook");
662 config.process_emails = FALSE;
666 void spamassassin_unregister_hook(void)
669 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
674 FolderItem *spamassassin_get_spam_folder(MsgInfo *msginfo)
676 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
678 if (item || msginfo == NULL || msginfo->folder == NULL)
681 if (msginfo->folder->folder &&
682 msginfo->folder->folder->account &&
683 msginfo->folder->folder->account->set_trash_folder) {
684 item = folder_find_item_from_identifier(
685 msginfo->folder->folder->account->trash_folder);
689 msginfo->folder->folder &&
690 msginfo->folder->folder->trash)
691 item = msginfo->folder->folder->trash;
694 item = folder_get_default_trash();
696 debug_print("SA spam dir: %s\n", folder_item_get_path(item));