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 sa_found_in_addressbook(const gchar *address)
202 gboolean found = FALSE;
208 addr = g_strdup(address);
209 extract_address(addr);
210 num_addr = complete_address(addr);
212 /* skip first item (this is the search string itself) */
214 for (; i < num_addr && !found; i++) {
215 gchar *caddr = get_complete_address(i);
216 extract_address(caddr);
217 if (strcasecmp(caddr, addr) == 0)
226 static gboolean mail_filtering_hook(gpointer source, gpointer data)
228 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
229 MsgInfo *msginfo = mail_filtering_data->msginfo;
230 gboolean is_spam = FALSE, error = FALSE;
231 static gboolean warned_error = FALSE;
236 /* SPAMASSASSIN_DISABLED : keep test for compatibility purpose */
237 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
238 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is disabled by its preferences.\n"));
241 debug_print("Filtering message %d\n", msginfo->msgnum);
242 if (message_callback != NULL)
243 message_callback(_("SpamAssassin: filtering message..."));
245 if ((fp = procmsg_open_message(msginfo)) == NULL) {
246 debug_print("failed to open message file\n");
250 if (config.whitelist_ab) {
251 gchar *ab_folderpath;
252 gboolean whitelisted = FALSE;
254 if (*config.whitelist_ab_folder == '\0' ||
255 strcasecmp(config.whitelist_ab_folder, "Any") == 0) {
256 /* match the whole addressbook */
257 ab_folderpath = NULL;
259 /* match the specific book/folder of the addressbook */
260 ab_folderpath = config.whitelist_ab_folder;
263 start_address_completion(ab_folderpath);
265 sa_found_in_addressbook(msginfo->from))
267 end_address_completion();
270 debug_print("message is ham (whitelisted)\n");
277 _exit(msg_is_spam(fp));
281 running |= CHILD_RUNNING;
283 g_timeout_add(50, timeout_func, &running);
284 running |= TIMEOUT_RUNNING;
286 while(running & CHILD_RUNNING) {
289 ret = waitpid(pid, &status, WNOHANG);
291 if (WIFEXITED(status)) {
292 MsgStatus result = MSG_IS_HAM;
293 running &= ~CHILD_RUNNING;
294 result = WEXITSTATUS(status);
295 is_spam = (result == MSG_IS_SPAM) ? TRUE : FALSE;
296 error = (result == MSG_FILTERING_ERROR);
299 running &= ~CHILD_RUNNING;
300 } /* ret == 0 continue */
302 g_main_context_iteration(NULL, TRUE);
305 while (running & TIMEOUT_RUNNING)
306 g_main_context_iteration(NULL, TRUE);
312 debug_print("message is spam\n");
313 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
314 if (config.receive_spam) {
315 FolderItem *save_folder = NULL;
317 if ((!config.save_folder) ||
318 (config.save_folder[0] == '\0') ||
319 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
320 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
321 save_folder = folder_find_item_from_identifier(
322 mail_filtering_data->account->trash_folder);
324 debug_print("found trash folder from account's advanced settings\n");
326 if (save_folder == NULL && mail_filtering_data->account &&
327 mail_filtering_data->account->folder) {
328 save_folder = mail_filtering_data->account->folder->trash;
330 debug_print("found trash folder from account's trash\n");
332 if (save_folder == NULL && mail_filtering_data->account &&
333 !mail_filtering_data->account->folder) {
334 if (mail_filtering_data->account->inbox) {
335 FolderItem *item = folder_find_item_from_identifier(
336 mail_filtering_data->account->inbox);
337 if (item && item->folder->trash) {
338 save_folder = item->folder->trash;
339 debug_print("found trash folder from account's inbox\n");
342 if (!save_folder && mail_filtering_data->account->local_inbox) {
343 FolderItem *item = folder_find_item_from_identifier(
344 mail_filtering_data->account->local_inbox);
345 if (item && item->folder->trash) {
346 save_folder = item->folder->trash;
347 debug_print("found trash folder from account's local_inbox\n");
351 if (save_folder == NULL) {
352 debug_print("using default trash folder\n");
353 save_folder = folder_get_default_trash();
356 if (config.mark_as_read)
357 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
358 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
359 msginfo->filter_op = IS_MOVE;
360 msginfo->to_filter_folder = save_folder;
362 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
367 debug_print("message is ham\n");
368 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
372 gchar *msg = _("The SpamAssassin plugin couldn't filter "
373 "a message. The probable cause of the error "
374 "is an unreachable spamd daemon. Please make "
375 "sure spamd is running and accessible.");
376 if (!prefs_common_get_prefs()->no_recv_err_panel) {
378 alertpanel_error("%s", msg);
382 log_error(LOG_PROTOCOL, "%s\n", msg);
389 SpamAssassinConfig *spamassassin_get_config(void)
394 gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
397 gchar *fname = get_tmp_file();
400 contents = g_strdup_printf(
401 "spamc -d %s -p %u -u %s -t %u -s %u -L %s<\"$*\";exit $?",
402 config.hostname, config.port,
403 config.username, config.timeout,
404 config.max_size * 1024, spam?"spam":"ham");
405 if (str_write_to_file(contents, fname) < 0) {
411 /* returned pointer must be free'ed by caller */
415 int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
419 const gchar *shell = g_getenv("SHELL");
420 gchar *spamc_wrapper = NULL;
422 if (msginfo == NULL && msglist == NULL) {
426 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
427 && prefs_common_get_prefs()->work_offline
428 && !inc_offline_should_override(TRUE,
429 _("Claws Mail needs network access in order "
430 "to feed this mail(s) to the remote learner."))) {
435 file = procmsg_get_message_file(msginfo);
439 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
440 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
441 if (spamc_wrapper != NULL) {
442 cmd = g_strconcat(shell?shell:"sh", " ",
443 spamc_wrapper, " ", file, NULL);
446 cmd = g_strdup_printf("sa-learn -u %s%s %s %s",
448 prefs_common_get_prefs()->work_offline?" -L":"",
449 spam?"--spam":"--ham", file);
453 GSList *cur = msglist;
456 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
457 /* execute n-times the spamc command */
458 for (; cur; cur = cur->next) {
459 info = (MsgInfo *)cur->data;
460 gchar *tmpcmd = NULL;
461 gchar *tmpfile = get_tmp_file();
463 if (spamc_wrapper == NULL) {
464 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
467 if (spamc_wrapper && tmpfile &&
468 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
469 tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
471 debug_print("%s\n", tmpcmd);
472 execute_command_line(tmpcmd, FALSE);
477 g_free(spamc_wrapper);
480 cmd = g_strdup_printf("sa-learn -u %s%s %s",
482 prefs_common_get_prefs()->work_offline?" -L":"",
483 spam?"--spam":"--ham");
485 /* concatenate all message tmpfiles to the sa-learn command-line */
486 for (; cur; cur = cur->next) {
487 info = (MsgInfo *)cur->data;
488 gchar *tmpcmd = NULL;
489 gchar *tmpfile = get_tmp_file();
492 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
493 tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
504 debug_print("%s\n", cmd);
505 /* only run sync calls to sa-learn/spamc to prevent system lockdown */
506 execute_command_line(cmd, FALSE);
508 g_free(spamc_wrapper);
513 void spamassassin_save_config(void)
518 debug_print("Saving SpamAssassin Page\n");
520 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
521 pfile = prefs_write_open(rcpath);
523 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
526 if (prefs_write_param(param, pfile->fp) < 0) {
527 g_warning("Failed to write SpamAssassin configuration to file");
528 prefs_file_close_revert(pfile);
531 if (fprintf(pfile->fp, "\n") < 0) {
532 FILE_OP_ERROR(rcpath, "fprintf");
533 prefs_file_close_revert(pfile);
535 prefs_file_close(pfile);
538 gboolean spamassassin_check_username(void)
540 if (config.username == NULL || config.username[0] == '\0') {
541 config.username = (gchar*)g_get_user_name();
542 if (config.username == NULL) {
544 spamassassin_unregister_hook();
546 procmsg_unregister_spam_learner(spamassassin_learn);
547 procmsg_spam_set_folder(NULL, NULL);
554 void spamassassin_set_message_callback(MessageCallback callback)
556 message_callback = callback;
559 gint plugin_init(gchar **error)
565 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
566 VERSION_NUMERIC, PLUGIN_NAME, error))
569 prefs_set_default(param);
570 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
571 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
573 if (!spamassassin_check_username()) {
574 *error = g_strdup(_("Failed to get username"));
577 spamassassin_gtk_init();
579 debug_print("SpamAssassin plugin loaded\n");
581 if (config.process_emails) {
582 spamassassin_register_hook();
585 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
586 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is loaded but disabled by its preferences.\n"));
589 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
590 debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
591 procmsg_register_spam_learner(spamassassin_learn);
592 procmsg_spam_set_folder(config.save_folder, spamassassin_get_spam_folder);
599 gboolean plugin_done(void)
602 spamassassin_unregister_hook();
604 g_free(config.hostname);
605 g_free(config.save_folder);
606 spamassassin_gtk_done();
607 procmsg_unregister_spam_learner(spamassassin_learn);
608 procmsg_spam_set_folder(NULL, NULL);
609 debug_print("SpamAssassin plugin unloaded\n");
613 const gchar *plugin_name(void)
618 const gchar *plugin_desc(void)
620 return _("This plugin can check all messages that are received from an "
621 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
622 "server. You will need a SpamAssassin Server (spamd) running "
625 "It can also be used for marking messages as Ham or Spam.\n"
627 "When a message is identified as spam it can be deleted or "
628 "saved in a specially designated folder.\n"
630 "Options can be found in /Configuration/Preferences/Plugins/SpamAssassin");
633 const gchar *plugin_type(void)
638 const gchar *plugin_licence(void)
643 const gchar *plugin_version(void)
648 struct PluginFeature *plugin_provides(void)
650 static struct PluginFeature features[] =
651 { {PLUGIN_FILTERING, N_("Spam detection")},
652 {PLUGIN_FILTERING, N_("Spam learning")},
653 {PLUGIN_NOTHING, NULL}};
657 void spamassassin_register_hook(void)
660 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
662 g_warning("Failed to register mail filtering hook");
663 config.process_emails = FALSE;
667 void spamassassin_unregister_hook(void)
670 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
675 FolderItem *spamassassin_get_spam_folder(MsgInfo *msginfo)
677 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
679 if (item || msginfo == NULL || msginfo->folder == NULL)
682 if (msginfo->folder->folder &&
683 msginfo->folder->folder->account &&
684 msginfo->folder->folder->account->set_trash_folder) {
685 item = folder_find_item_from_identifier(
686 msginfo->folder->folder->account->trash_folder);
690 msginfo->folder->folder &&
691 msginfo->folder->folder->trash)
692 item = msginfo->folder->folder->trash;
695 item = folder_get_default_trash();
697 debug_print("SA spam dir: %s\n", folder_item_get_path(item));