2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and 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,
112 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
115 gboolean timeout_func(gpointer data)
117 gint *running = (gint *) data;
119 if (*running & CHILD_RUNNING)
122 *running &= ~TIMEOUT_RUNNING;
129 MSG_FILTERING_ERROR = 2
132 static MsgStatus msg_is_spam(FILE *fp)
134 struct transport trans;
136 gboolean is_spam = FALSE;
141 transport_init(&trans);
142 switch (config.transport) {
143 case SPAMASSASSIN_TRANSPORT_LOCALHOST:
144 trans.type = TRANSPORT_LOCALHOST;
145 trans.port = config.port;
147 case SPAMASSASSIN_TRANSPORT_TCP:
148 trans.type = TRANSPORT_TCP;
149 trans.hostname = config.hostname;
150 trans.port = config.port;
152 case SPAMASSASSIN_TRANSPORT_UNIX:
153 trans.type = TRANSPORT_UNIX;
154 trans.socketpath = config.socket;
160 if (transport_setup(&trans, flags) != EX_OK) {
161 log_error(_("SpamAssassin plugin couldn't connect to spamd.\n"));
162 debug_print("failed to setup transport\n");
163 return MSG_FILTERING_ERROR;
166 m.type = MESSAGE_NONE;
167 m.max_len = config.max_size * 1024;
168 m.timeout = config.timeout;
170 if (message_read(fileno(fp), flags, &m) != EX_OK) {
171 debug_print("failed to read message\n");
173 return MSG_FILTERING_ERROR;
176 if (message_filter(&trans, config.username, flags, &m) != EX_OK) {
177 log_error(_("SpamAssassin plugin filtering failed.\n"));
178 debug_print("filtering the message failed\n");
180 return MSG_FILTERING_ERROR;
183 if (m.is_spam == EX_ISSPAM)
188 return is_spam ? MSG_IS_SPAM:MSG_IS_HAM;
191 static gboolean mail_filtering_hook(gpointer source, gpointer data)
193 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
194 MsgInfo *msginfo = mail_filtering_data->msginfo;
195 gboolean is_spam = FALSE, error = FALSE;
196 static gboolean warned_error = FALSE;
201 /* SPAMASSASSIN_DISABLED : keep test for compatibility purpose */
202 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
203 log_warning(_("SpamAssassin plugin is disabled by its preferences.\n"));
206 debug_print("Filtering message %d\n", msginfo->msgnum);
207 if (message_callback != NULL)
208 message_callback(_("SpamAssassin: filtering message..."));
210 if ((fp = procmsg_open_message(msginfo)) == NULL) {
211 debug_print("failed to open message file\n");
217 _exit(msg_is_spam(fp));
221 running |= CHILD_RUNNING;
223 g_timeout_add(50, timeout_func, &running);
224 running |= TIMEOUT_RUNNING;
226 while(running & CHILD_RUNNING) {
229 ret = waitpid(pid, &status, WNOHANG);
231 if (WIFEXITED(status)) {
232 MsgStatus result = MSG_IS_HAM;
233 running &= ~CHILD_RUNNING;
234 result = WEXITSTATUS(status);
235 is_spam = (result == MSG_IS_SPAM) ? TRUE : FALSE;
236 error = (result == MSG_FILTERING_ERROR);
239 running &= ~CHILD_RUNNING;
240 } /* ret == 0 continue */
242 g_main_iteration(TRUE);
245 while (running & TIMEOUT_RUNNING)
246 g_main_iteration(TRUE);
252 debug_print("message is spam\n");
253 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
254 if (config.receive_spam) {
255 FolderItem *save_folder = NULL;
257 if ((!config.save_folder) ||
258 (config.save_folder[0] == '\0') ||
259 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL)) {
260 if (mail_filtering_data->account && mail_filtering_data->account->set_trash_folder)
261 save_folder = folder_find_item_from_identifier(
262 mail_filtering_data->account->trash_folder);
263 if (save_folder == NULL && mail_filtering_data->account &&
264 mail_filtering_data->account->folder)
265 save_folder = mail_filtering_data->account->folder->trash;
266 if (save_folder == NULL)
267 save_folder = folder_get_default_trash();
269 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
270 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
271 msginfo->is_move = TRUE;
272 msginfo->to_filter_folder = save_folder;
274 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
279 debug_print("message is ham\n");
280 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
284 gchar *msg = _("The SpamAssassin plugin couldn't filter "
285 "a message. The probable cause of the error "
286 "is an unreachable spamd daemon. Please make "
287 "sure spamd is running and accessible.");
288 if (!prefs_common.no_recv_err_panel) {
290 alertpanel_error(msg);
294 gchar *tmp = g_strdup_printf("%s\n", msg);
303 SpamAssassinConfig *spamassassin_get_config(void)
308 gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
311 gchar *fname = get_tmp_file();
314 contents = g_strdup_printf(
315 "spamc -d %s -p %u -u %s -t %u -s %u -L %s<\"$*\";exit $?",
316 config.hostname, config.port,
317 config.username, config.timeout,
318 config.max_size * 1024, spam?"spam":"ham");
319 if (str_write_to_file(contents, fname) < 0) {
325 /* returned pointer must be free'ed by caller */
329 int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
333 const gchar *shell = g_getenv("SHELL");
334 gchar *spamc_wrapper = NULL;
336 if (msginfo == NULL && msglist == NULL) {
340 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
341 && prefs_common.work_offline
342 && !inc_offline_should_override(
343 _("Claws Mail needs network access in order "
344 "to feed this mail(s) to the remote learner."))) {
349 file = procmsg_get_message_file(msginfo);
353 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
354 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
355 if (spamc_wrapper != NULL) {
356 cmd = g_strconcat(shell?shell:"sh", " ",
357 spamc_wrapper, " ", file, NULL);
360 cmd = g_strdup_printf("sa-learn -u %s %s %s %s",
362 prefs_common.work_offline?"-L":"",
363 spam?"--spam":"--ham", file);
367 GSList *cur = msglist;
370 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
371 /* execute n-times the spamc command */
372 for (; cur; cur = cur->next) {
373 info = (MsgInfo *)cur->data;
374 gchar *tmpcmd = NULL;
375 gchar *tmpfile = get_tmp_file();
377 if (spamc_wrapper == NULL) {
378 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
381 if (spamc_wrapper && tmpfile &&
382 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
383 tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
385 debug_print("%s\n", tmpcmd);
386 execute_command_line(tmpcmd, FALSE);
391 g_free(spamc_wrapper);
394 cmd = g_strdup_printf("sa-learn -u %s %s %s",
396 prefs_common.work_offline?"-L":"",
397 spam?"--spam":"--ham");
399 /* concatenate all message tmpfiles to the sa-learn command-line */
400 for (; cur; cur = cur->next) {
401 info = (MsgInfo *)cur->data;
402 gchar *tmpcmd = NULL;
403 gchar *tmpfile = get_tmp_file();
406 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
407 tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
418 debug_print("%s\n", cmd);
419 /* only run sync calls to sa-learn/spamc to prevent system lockdown */
420 execute_command_line(cmd, FALSE);
422 g_free(spamc_wrapper);
427 void spamassassin_save_config(void)
432 debug_print("Saving SpamAssassin Page\n");
434 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
435 pfile = prefs_write_open(rcpath);
437 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
440 if (prefs_write_param(param, pfile->fp) < 0) {
441 g_warning("Failed to write SpamAssassin configuration to file\n");
442 prefs_file_close_revert(pfile);
445 fprintf(pfile->fp, "\n");
447 prefs_file_close(pfile);
450 gboolean spamassassin_check_username(void)
452 if (config.username == NULL || config.username[0] == '\0') {
453 config.username = (gchar*)g_get_user_name();
454 if (config.username == NULL) {
456 spamassassin_unregister_hook();
458 procmsg_unregister_spam_learner(spamassassin_learn);
459 procmsg_spam_set_folder(NULL, NULL);
466 void spamassassin_set_message_callback(MessageCallback callback)
468 message_callback = callback;
471 gint plugin_init(gchar **error)
477 if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
478 VERSION_NUMERIC, PLUGIN_NAME, error))
481 prefs_set_default(param);
482 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
483 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
485 if (!spamassassin_check_username()) {
486 *error = g_strdup(_("Failed to get username"));
489 spamassassin_gtk_init();
491 debug_print("SpamAssassin plugin loaded\n");
493 if (config.process_emails) {
494 spamassassin_register_hook();
497 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
498 log_warning(_("SpamAssassin plugin is loaded but disabled by its preferences.\n"));
501 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
502 debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
503 procmsg_register_spam_learner(spamassassin_learn);
504 procmsg_spam_set_folder(config.save_folder, spamassassin_get_spam_folder);
511 void plugin_done(void)
514 spamassassin_unregister_hook();
516 g_free(config.hostname);
517 g_free(config.save_folder);
518 spamassassin_gtk_done();
519 procmsg_unregister_spam_learner(spamassassin_learn);
520 procmsg_spam_set_folder(NULL, NULL);
521 debug_print("SpamAssassin plugin unloaded\n");
524 const gchar *plugin_name(void)
529 const gchar *plugin_desc(void)
531 return _("This plugin can check all messages that are received from an "
532 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
533 "server. You will need a SpamAssassin Server (spamd) running "
536 "It can also be used for marking messages as Ham or Spam.\n"
538 "When a message is identified as spam it can be deleted or "
539 "saved in a specially designated folder.\n"
541 "Options can be found in /Configuration/Preferences/Plugins/SpamAssassin");
544 const gchar *plugin_type(void)
549 const gchar *plugin_licence(void)
554 const gchar *plugin_version(void)
559 struct PluginFeature *plugin_provides(void)
561 static struct PluginFeature features[] =
562 { {PLUGIN_FILTERING, N_("Spam detection")},
563 {PLUGIN_FILTERING, N_("Spam learning")},
564 {PLUGIN_NOTHING, NULL}};
568 void spamassassin_register_hook(void)
571 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
573 g_warning("Failed to register mail filtering hook");
574 config.process_emails = FALSE;
578 void spamassassin_unregister_hook(void)
581 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
586 FolderItem *spamassassin_get_spam_folder(MsgInfo *msginfo)
588 FolderItem *item = folder_find_item_from_identifier(config.save_folder);
590 if (item || msginfo == NULL || msginfo->folder == NULL)
593 if (msginfo->folder->folder &&
594 msginfo->folder->folder->account &&
595 msginfo->folder->folder->account->set_trash_folder) {
596 item = folder_find_item_from_identifier(
597 msginfo->folder->folder->account->trash_folder);
601 msginfo->folder->folder &&
602 msginfo->folder->folder->trash)
603 item = msginfo->folder->folder->trash;
606 item = folder_get_default_trash();
608 debug_print("SA spam dir: %s\n", folder_item_get_path(item));