2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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");
275 _exit(msg_is_spam(fp));
279 running |= CHILD_RUNNING;
281 g_timeout_add(50, timeout_func, &running);
282 running |= TIMEOUT_RUNNING;
284 while(running & CHILD_RUNNING) {
287 ret = waitpid(pid, &status, WNOHANG);
289 if (WIFEXITED(status)) {
290 MsgStatus result = MSG_IS_HAM;
291 running &= ~CHILD_RUNNING;
292 result = WEXITSTATUS(status);
293 is_spam = (result == MSG_IS_SPAM) ? TRUE : FALSE;
294 error = (result == MSG_FILTERING_ERROR);
297 running &= ~CHILD_RUNNING;
298 } /* ret == 0 continue */
300 g_main_iteration(TRUE);
303 while (running & TIMEOUT_RUNNING)
304 g_main_iteration(TRUE);
310 debug_print("message is spam\n");
311 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
312 if (config.receive_spam) {
313 FolderItem *save_folder = NULL;
315 if ((!config.save_folder) ||
316 (config.save_folder[0] == '\0') ||
317 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
318 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
319 save_folder = folder_find_item_from_identifier(
320 mail_filtering_data->account->trash_folder);
322 debug_print("found trash folder from account's advanced settings\n");
324 if (save_folder == NULL && mail_filtering_data->account &&
325 mail_filtering_data->account->folder) {
326 save_folder = mail_filtering_data->account->folder->trash;
328 debug_print("found trash folder from account's trash\n");
330 if (save_folder == NULL && mail_filtering_data->account &&
331 !mail_filtering_data->account->folder) {
332 if (mail_filtering_data->account->inbox) {
333 FolderItem *item = folder_find_item_from_identifier(
334 mail_filtering_data->account->inbox);
335 if (item && item->folder->trash) {
336 save_folder = item->folder->trash;
337 debug_print("found trash folder from account's inbox\n");
340 if (!save_folder && mail_filtering_data->account->local_inbox) {
341 FolderItem *item = folder_find_item_from_identifier(
342 mail_filtering_data->account->local_inbox);
343 if (item && item->folder->trash) {
344 save_folder = item->folder->trash;
345 debug_print("found trash folder from account's local_inbox\n");
349 if (save_folder == NULL) {
350 debug_print("using default trash folder\n");
351 save_folder = folder_get_default_trash();
354 if (config.mark_as_read)
355 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
356 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
357 msginfo->filter_op = IS_MOVE;
358 msginfo->to_filter_folder = save_folder;
360 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
365 debug_print("message is ham\n");
366 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
370 gchar *msg = _("The SpamAssassin plugin couldn't filter "
371 "a message. The probable cause of the error "
372 "is an unreachable spamd daemon. Please make "
373 "sure spamd is running and accessible.");
374 if (!prefs_common.no_recv_err_panel) {
376 alertpanel_error("%s", msg);
380 log_error(LOG_PROTOCOL, "%s\n", msg);
387 SpamAssassinConfig *spamassassin_get_config(void)
392 gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
395 gchar *fname = get_tmp_file();
398 contents = g_strdup_printf(
399 "spamc -d %s -p %u -u %s -t %u -s %u -L %s<\"$*\";exit $?",
400 config.hostname, config.port,
401 config.username, config.timeout,
402 config.max_size * 1024, spam?"spam":"ham");
403 if (str_write_to_file(contents, fname) < 0) {
409 /* returned pointer must be free'ed by caller */
413 int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
417 const gchar *shell = g_getenv("SHELL");
418 gchar *spamc_wrapper = NULL;
420 if (msginfo == NULL && msglist == NULL) {
424 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
425 && prefs_common.work_offline
426 && !inc_offline_should_override(TRUE,
427 _("Claws Mail needs network access in order "
428 "to feed this mail(s) to the remote learner."))) {
433 file = procmsg_get_message_file(msginfo);
437 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
438 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
439 if (spamc_wrapper != NULL) {
440 cmd = g_strconcat(shell?shell:"sh", " ",
441 spamc_wrapper, " ", file, NULL);
444 cmd = g_strdup_printf("sa-learn -u %s %s %s %s",
446 prefs_common.work_offline?"-L":"",
447 spam?"--spam":"--ham", file);
451 GSList *cur = msglist;
454 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
455 /* execute n-times the spamc command */
456 for (; cur; cur = cur->next) {
457 info = (MsgInfo *)cur->data;
458 gchar *tmpcmd = NULL;
459 gchar *tmpfile = get_tmp_file();
461 if (spamc_wrapper == NULL) {
462 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
465 if (spamc_wrapper && tmpfile &&
466 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
467 tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
469 debug_print("%s\n", tmpcmd);
470 execute_command_line(tmpcmd, FALSE);
475 g_free(spamc_wrapper);
478 cmd = g_strdup_printf("sa-learn -u %s %s %s",
480 prefs_common.work_offline?"-L":"",
481 spam?"--spam":"--ham");
483 /* concatenate all message tmpfiles to the sa-learn command-line */
484 for (; cur; cur = cur->next) {
485 info = (MsgInfo *)cur->data;
486 gchar *tmpcmd = NULL;
487 gchar *tmpfile = get_tmp_file();
490 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
491 tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
502 debug_print("%s\n", cmd);
503 /* only run sync calls to sa-learn/spamc to prevent system lockdown */
504 execute_command_line(cmd, FALSE);
506 g_free(spamc_wrapper);
511 void spamassassin_save_config(void)
516 debug_print("Saving SpamAssassin Page\n");
518 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
519 pfile = prefs_write_open(rcpath);
521 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
524 if (prefs_write_param(param, pfile->fp) < 0) {
525 g_warning("Failed to write SpamAssassin configuration to file\n");
526 prefs_file_close_revert(pfile);
529 if (fprintf(pfile->fp, "\n") < 0) {
530 FILE_OP_ERROR(rcpath, "fprintf");
531 prefs_file_close_revert(pfile);
533 prefs_file_close(pfile);
536 gboolean spamassassin_check_username(void)
538 if (config.username == NULL || config.username[0] == '\0') {
539 config.username = (gchar*)g_get_user_name();
540 if (config.username == NULL) {
542 spamassassin_unregister_hook();
544 procmsg_unregister_spam_learner(spamassassin_learn);
545 procmsg_spam_set_folder(NULL, NULL);
552 void spamassassin_set_message_callback(MessageCallback callback)
554 message_callback = callback;
557 gint plugin_init(gchar **error)
563 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
564 VERSION_NUMERIC, PLUGIN_NAME, error))
567 prefs_set_default(param);
568 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
569 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
571 if (!spamassassin_check_username()) {
572 *error = g_strdup(_("Failed to get username"));
575 spamassassin_gtk_init();
577 debug_print("SpamAssassin plugin loaded\n");
579 if (config.process_emails) {
580 spamassassin_register_hook();
583 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
584 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is loaded but disabled by its preferences.\n"));
587 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
588 debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
589 procmsg_register_spam_learner(spamassassin_learn);
590 procmsg_spam_set_folder(config.save_folder, spamassassin_get_spam_folder);
597 gboolean plugin_done(void)
600 spamassassin_unregister_hook();
602 g_free(config.hostname);
603 g_free(config.save_folder);
604 spamassassin_gtk_done();
605 procmsg_unregister_spam_learner(spamassassin_learn);
606 procmsg_spam_set_folder(NULL, NULL);
607 debug_print("SpamAssassin plugin unloaded\n");
611 const gchar *plugin_name(void)
616 const gchar *plugin_desc(void)
618 return _("This plugin can check all messages that are received from an "
619 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
620 "server. You will need a SpamAssassin Server (spamd) running "
623 "It can also be used for marking messages as Ham or Spam.\n"
625 "When a message is identified as spam it can be deleted or "
626 "saved in a specially designated folder.\n"
628 "Options can be found in /Configuration/Preferences/Plugins/SpamAssassin");
631 const gchar *plugin_type(void)
636 const gchar *plugin_licence(void)
641 const gchar *plugin_version(void)
646 struct PluginFeature *plugin_provides(void)
648 static struct PluginFeature features[] =
649 { {PLUGIN_FILTERING, N_("Spam detection")},
650 {PLUGIN_FILTERING, N_("Spam learning")},
651 {PLUGIN_NOTHING, NULL}};
655 void spamassassin_register_hook(void)
658 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
660 g_warning("Failed to register mail filtering hook");
661 config.process_emails = FALSE;
665 void spamassassin_unregister_hook(void)
668 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
673 FolderItem *spamassassin_get_spam_folder(MsgInfo *msginfo)
675 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
677 if (item || msginfo == NULL || msginfo->folder == NULL)
680 if (msginfo->folder->folder &&
681 msginfo->folder->folder->account &&
682 msginfo->folder->folder->account->set_trash_folder) {
683 item = folder_find_item_from_identifier(
684 msginfo->folder->folder->account->trash_folder);
688 msginfo->folder->folder &&
689 msginfo->folder->folder->trash)
690 item = msginfo->folder->folder->trash;
693 item = folder_get_default_trash();
695 debug_print("SA spam dir: %s\n", folder_item_get_path(item));