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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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"
53 #ifdef HAVE_SYSEXITS_H
59 #ifdef HAVE_SYS_ERRNO_H
60 #include <sys/errno.h>
65 #ifdef HAVE_SYS_TIME_H
75 #define PLUGIN_NAME (_("SpamAssassin"))
78 CHILD_RUNNING = 1 << 0,
79 TIMEOUT_RUNNING = 1 << 1,
82 static guint hook_id = -1;
83 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
84 static MessageCallback message_callback;
86 static SpamAssassinConfig config;
88 static PrefParam param[] = {
89 {"enable", "FALSE", &config.enable, P_BOOL,
91 {"transport", "0", &config.transport, P_INT,
93 {"hostname", "localhost", &config.hostname, P_STRING,
95 {"port", "783", &config.port, P_INT,
97 {"socket", "", &config.socket, P_STRING,
99 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
101 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
103 {"save_folder", NULL, &config.save_folder, P_STRING,
105 {"max_size", "250", &config.max_size, P_INT,
107 {"timeout", "30", &config.timeout, P_INT,
109 {"username", "", &config.username, P_STRING,
111 {"mark_as_read", "TRUE", &config.mark_as_read, P_BOOL,
114 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
117 gboolean timeout_func(gpointer data)
119 gint *running = (gint *) data;
121 if (*running & CHILD_RUNNING)
124 *running &= ~TIMEOUT_RUNNING;
131 MSG_FILTERING_ERROR = 2
134 static MsgStatus msg_is_spam(FILE *fp)
136 struct transport trans;
138 gboolean is_spam = FALSE;
143 transport_init(&trans);
144 switch (config.transport) {
145 case SPAMASSASSIN_TRANSPORT_LOCALHOST:
146 trans.type = TRANSPORT_LOCALHOST;
147 trans.port = config.port;
149 case SPAMASSASSIN_TRANSPORT_TCP:
150 trans.type = TRANSPORT_TCP;
151 trans.hostname = config.hostname;
152 trans.port = config.port;
154 case SPAMASSASSIN_TRANSPORT_UNIX:
155 trans.type = TRANSPORT_UNIX;
156 trans.socketpath = config.socket;
162 if (transport_setup(&trans, flags) != EX_OK) {
163 log_error(LOG_PROTOCOL, _("SpamAssassin plugin couldn't connect to spamd.\n"));
164 debug_print("failed to setup transport\n");
165 return MSG_FILTERING_ERROR;
168 m.type = MESSAGE_NONE;
169 m.max_len = config.max_size * 1024;
170 m.timeout = config.timeout;
172 if (message_read(fileno(fp), flags, &m) != EX_OK) {
173 debug_print("failed to read message\n");
175 return MSG_FILTERING_ERROR;
178 if (message_filter(&trans, config.username, flags, &m) != EX_OK) {
179 log_error(LOG_PROTOCOL, _("SpamAssassin plugin filtering failed.\n"));
180 debug_print("filtering the message failed\n");
182 return MSG_FILTERING_ERROR;
185 if (m.is_spam == EX_ISSPAM)
190 return is_spam ? MSG_IS_SPAM:MSG_IS_HAM;
193 static gboolean mail_filtering_hook(gpointer source, gpointer data)
195 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
196 MsgInfo *msginfo = mail_filtering_data->msginfo;
197 gboolean is_spam = FALSE, error = FALSE;
198 static gboolean warned_error = FALSE;
203 /* SPAMASSASSIN_DISABLED : keep test for compatibility purpose */
204 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
205 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is disabled by its preferences.\n"));
208 debug_print("Filtering message %d\n", msginfo->msgnum);
209 if (message_callback != NULL)
210 message_callback(_("SpamAssassin: filtering message..."));
212 if ((fp = procmsg_open_message(msginfo)) == NULL) {
213 debug_print("failed to open message file\n");
219 _exit(msg_is_spam(fp));
223 running |= CHILD_RUNNING;
225 g_timeout_add(50, timeout_func, &running);
226 running |= TIMEOUT_RUNNING;
228 while(running & CHILD_RUNNING) {
231 ret = waitpid(pid, &status, WNOHANG);
233 if (WIFEXITED(status)) {
234 MsgStatus result = MSG_IS_HAM;
235 running &= ~CHILD_RUNNING;
236 result = WEXITSTATUS(status);
237 is_spam = (result == MSG_IS_SPAM) ? TRUE : FALSE;
238 error = (result == MSG_FILTERING_ERROR);
241 running &= ~CHILD_RUNNING;
242 } /* ret == 0 continue */
244 g_main_iteration(TRUE);
247 while (running & TIMEOUT_RUNNING)
248 g_main_iteration(TRUE);
254 debug_print("message is spam\n");
255 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
256 if (config.receive_spam) {
257 FolderItem *save_folder = NULL;
259 if ((!config.save_folder) ||
260 (config.save_folder[0] == '\0') ||
261 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
262 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder) {
263 save_folder = folder_find_item_from_identifier(
264 mail_filtering_data->account->trash_folder);
266 debug_print("found trash folder from account's advanced settings\n");
268 if (save_folder == NULL && mail_filtering_data->account &&
269 mail_filtering_data->account->folder) {
270 save_folder = mail_filtering_data->account->folder->trash;
272 debug_print("found trash folder from account's trash\n");
274 if (save_folder == NULL && mail_filtering_data->account &&
275 !mail_filtering_data->account->folder) {
276 if (mail_filtering_data->account->inbox) {
277 FolderItem *item = folder_find_item_from_identifier(
278 mail_filtering_data->account->inbox);
279 if (item && item->folder->trash) {
280 save_folder = item->folder->trash;
281 debug_print("found trash folder from account's inbox\n");
284 if (!save_folder && mail_filtering_data->account->local_inbox) {
285 FolderItem *item = folder_find_item_from_identifier(
286 mail_filtering_data->account->local_inbox);
287 if (item && item->folder->trash) {
288 save_folder = item->folder->trash;
289 debug_print("found trash folder from account's local_inbox\n");
293 if (save_folder == NULL) {
294 debug_print("using default trash folder\n");
295 save_folder = folder_get_default_trash();
298 if (config.mark_as_read)
299 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
300 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
301 msginfo->is_move = TRUE;
302 msginfo->to_filter_folder = save_folder;
304 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
309 debug_print("message is ham\n");
310 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
314 gchar *msg = _("The SpamAssassin plugin couldn't filter "
315 "a message. The probable cause of the error "
316 "is an unreachable spamd daemon. Please make "
317 "sure spamd is running and accessible.");
318 if (!prefs_common.no_recv_err_panel) {
320 alertpanel_error(msg);
324 gchar *tmp = g_strdup_printf("%s\n", msg);
325 log_error(LOG_PROTOCOL, tmp);
333 SpamAssassinConfig *spamassassin_get_config(void)
338 gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
341 gchar *fname = get_tmp_file();
344 contents = g_strdup_printf(
345 "spamc -d %s -p %u -u %s -t %u -s %u -L %s<\"$*\";exit $?",
346 config.hostname, config.port,
347 config.username, config.timeout,
348 config.max_size * 1024, spam?"spam":"ham");
349 if (str_write_to_file(contents, fname) < 0) {
355 /* returned pointer must be free'ed by caller */
359 int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
363 const gchar *shell = g_getenv("SHELL");
364 gchar *spamc_wrapper = NULL;
366 if (msginfo == NULL && msglist == NULL) {
370 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
371 && prefs_common.work_offline
372 && !inc_offline_should_override(TRUE,
373 _("Claws Mail needs network access in order "
374 "to feed this mail(s) to the remote learner."))) {
379 file = procmsg_get_message_file(msginfo);
383 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
384 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
385 if (spamc_wrapper != NULL) {
386 cmd = g_strconcat(shell?shell:"sh", " ",
387 spamc_wrapper, " ", file, NULL);
390 cmd = g_strdup_printf("sa-learn -u %s %s %s %s",
392 prefs_common.work_offline?"-L":"",
393 spam?"--spam":"--ham", file);
397 GSList *cur = msglist;
400 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
401 /* execute n-times the spamc command */
402 for (; cur; cur = cur->next) {
403 info = (MsgInfo *)cur->data;
404 gchar *tmpcmd = NULL;
405 gchar *tmpfile = get_tmp_file();
407 if (spamc_wrapper == NULL) {
408 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
411 if (spamc_wrapper && tmpfile &&
412 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
413 tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
415 debug_print("%s\n", tmpcmd);
416 execute_command_line(tmpcmd, FALSE);
421 g_free(spamc_wrapper);
424 cmd = g_strdup_printf("sa-learn -u %s %s %s",
426 prefs_common.work_offline?"-L":"",
427 spam?"--spam":"--ham");
429 /* concatenate all message tmpfiles to the sa-learn command-line */
430 for (; cur; cur = cur->next) {
431 info = (MsgInfo *)cur->data;
432 gchar *tmpcmd = NULL;
433 gchar *tmpfile = get_tmp_file();
436 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
437 tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
448 debug_print("%s\n", cmd);
449 /* only run sync calls to sa-learn/spamc to prevent system lockdown */
450 execute_command_line(cmd, FALSE);
452 g_free(spamc_wrapper);
457 void spamassassin_save_config(void)
462 debug_print("Saving SpamAssassin Page\n");
464 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
465 pfile = prefs_write_open(rcpath);
467 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
470 if (prefs_write_param(param, pfile->fp) < 0) {
471 g_warning("Failed to write SpamAssassin configuration to file\n");
472 prefs_file_close_revert(pfile);
475 fprintf(pfile->fp, "\n");
477 prefs_file_close(pfile);
480 gboolean spamassassin_check_username(void)
482 if (config.username == NULL || config.username[0] == '\0') {
483 config.username = (gchar*)g_get_user_name();
484 if (config.username == NULL) {
486 spamassassin_unregister_hook();
488 procmsg_unregister_spam_learner(spamassassin_learn);
489 procmsg_spam_set_folder(NULL, NULL);
496 void spamassassin_set_message_callback(MessageCallback callback)
498 message_callback = callback;
501 gint plugin_init(gchar **error)
507 if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
508 VERSION_NUMERIC, PLUGIN_NAME, error))
511 prefs_set_default(param);
512 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
513 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
515 if (!spamassassin_check_username()) {
516 *error = g_strdup(_("Failed to get username"));
519 spamassassin_gtk_init();
521 debug_print("SpamAssassin plugin loaded\n");
523 if (config.process_emails) {
524 spamassassin_register_hook();
527 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
528 log_warning(LOG_PROTOCOL, _("SpamAssassin plugin is loaded but disabled by its preferences.\n"));
531 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
532 debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
533 procmsg_register_spam_learner(spamassassin_learn);
534 procmsg_spam_set_folder(config.save_folder, spamassassin_get_spam_folder);
541 void plugin_done(void)
544 spamassassin_unregister_hook();
546 g_free(config.hostname);
547 g_free(config.save_folder);
548 spamassassin_gtk_done();
549 procmsg_unregister_spam_learner(spamassassin_learn);
550 procmsg_spam_set_folder(NULL, NULL);
551 debug_print("SpamAssassin plugin unloaded\n");
554 const gchar *plugin_name(void)
559 const gchar *plugin_desc(void)
561 return _("This plugin can check all messages that are received from an "
562 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
563 "server. You will need a SpamAssassin Server (spamd) running "
566 "It can also be used for marking messages as Ham or Spam.\n"
568 "When a message is identified as spam it can be deleted or "
569 "saved in a specially designated folder.\n"
571 "Options can be found in /Configuration/Preferences/Plugins/SpamAssassin");
574 const gchar *plugin_type(void)
579 const gchar *plugin_licence(void)
584 const gchar *plugin_version(void)
589 struct PluginFeature *plugin_provides(void)
591 static struct PluginFeature features[] =
592 { {PLUGIN_FILTERING, N_("Spam detection")},
593 {PLUGIN_FILTERING, N_("Spam learning")},
594 {PLUGIN_NOTHING, NULL}};
598 void spamassassin_register_hook(void)
601 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
603 g_warning("Failed to register mail filtering hook");
604 config.process_emails = FALSE;
608 void spamassassin_unregister_hook(void)
611 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
616 FolderItem *spamassassin_get_spam_folder(MsgInfo *msginfo)
618 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
620 if (item || msginfo == NULL || msginfo->folder == NULL)
623 if (msginfo->folder->folder &&
624 msginfo->folder->folder->account &&
625 msginfo->folder->folder->account->set_trash_folder) {
626 item = folder_find_item_from_identifier(
627 msginfo->folder->folder->account->trash_folder);
631 msginfo->folder->folder &&
632 msginfo->folder->folder->trash)
633 item = msginfo->folder->folder->trash;
636 item = folder_get_default_trash();
638 debug_print("SA spam dir: %s\n", folder_item_get_path(item));