2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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/sylpheed.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"
52 #ifdef HAVE_SYSEXITS_H
58 #ifdef HAVE_SYS_ERRNO_H
59 #include <sys/errno.h>
64 #ifdef HAVE_SYS_TIME_H
75 CHILD_RUNNING = 1 << 0,
76 TIMEOUT_RUNNING = 1 << 1,
79 static guint hook_id = -1;
80 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
81 static MessageCallback message_callback;
83 static SpamAssassinConfig config;
85 static PrefParam param[] = {
86 {"enable", "FALSE", &config.enable, P_BOOL,
88 {"transport", "0", &config.transport, P_INT,
90 {"hostname", "localhost", &config.hostname, P_STRING,
92 {"port", "783", &config.port, P_INT,
94 {"socket", "", &config.socket, P_STRING,
96 {"process_emails", "TRUE", &config.process_emails, P_BOOL,
98 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
100 {"save_folder", NULL, &config.save_folder, P_STRING,
102 {"max_size", "250", &config.max_size, P_INT,
104 {"timeout", "30", &config.timeout, P_INT,
106 {"username", "", &config.username, P_STRING,
109 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
112 gboolean timeout_func(gpointer data)
114 gint *running = (gint *) data;
116 if (*running & CHILD_RUNNING)
119 *running &= ~TIMEOUT_RUNNING;
123 static gboolean msg_is_spam(FILE *fp)
125 struct transport trans;
127 gboolean is_spam = FALSE;
132 transport_init(&trans);
133 switch (config.transport) {
134 case SPAMASSASSIN_TRANSPORT_LOCALHOST:
135 trans.type = TRANSPORT_LOCALHOST;
136 trans.port = config.port;
138 case SPAMASSASSIN_TRANSPORT_TCP:
139 trans.type = TRANSPORT_TCP;
140 trans.hostname = config.hostname;
141 trans.port = config.port;
143 case SPAMASSASSIN_TRANSPORT_UNIX:
144 trans.type = TRANSPORT_UNIX;
145 trans.socketpath = config.socket;
151 if (transport_setup(&trans, flags) != EX_OK) {
152 log_error("Spamassassin plugin couldn't connect to spamd.\n");
153 debug_print("failed to setup transport\n");
157 m.type = MESSAGE_NONE;
158 m.max_len = config.max_size * 1024;
159 m.timeout = config.timeout;
161 if (message_read(fileno(fp), flags, &m) != EX_OK) {
162 debug_print("failed to read message\n");
167 if (message_filter(&trans, config.username, flags, &m) != EX_OK) {
168 debug_print("filtering the message failed\n");
173 if (m.is_spam == EX_ISSPAM)
181 static gboolean mail_filtering_hook(gpointer source, gpointer data)
183 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
184 MsgInfo *msginfo = mail_filtering_data->msginfo;
185 gboolean is_spam = FALSE;
190 /* SPAMASSASSIN_DISABLED : keep test for compatibility purpose */
191 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
192 log_error("Spamassassin plugin is disabled by its preferences.\n");
195 debug_print("Filtering message %d\n", msginfo->msgnum);
196 if (message_callback != NULL)
197 message_callback(_("SpamAssassin: filtering message..."));
199 if ((fp = procmsg_open_message(msginfo)) == NULL) {
200 debug_print("failed to open message file\n");
206 _exit(msg_is_spam(fp) ? 1 : 0);
210 running |= CHILD_RUNNING;
212 g_timeout_add(50, timeout_func, &running);
213 running |= TIMEOUT_RUNNING;
215 while(running & CHILD_RUNNING) {
218 ret = waitpid(pid, &status, WNOHANG);
220 if (WIFEXITED(status)) {
221 running &= ~CHILD_RUNNING;
222 is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
225 running &= ~CHILD_RUNNING;
226 } /* ret == 0 continue */
228 g_main_iteration(TRUE);
231 while (running & TIMEOUT_RUNNING)
232 g_main_iteration(TRUE);
238 debug_print("message is spam\n");
239 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
240 if (config.receive_spam) {
241 FolderItem *save_folder;
243 if ((!config.save_folder) ||
244 (config.save_folder[0] == '\0') ||
245 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
246 save_folder = folder_get_default_trash();
248 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
249 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
250 folder_item_move_msg(save_folder, msginfo);
252 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
257 debug_print("message is ham\n");
258 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
263 SpamAssassinConfig *spamassassin_get_config(void)
268 gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
271 gchar *fname = get_tmp_file();
274 contents = g_strdup_printf(
275 "spamc -d %s -p %u -u %s -t %u -s %u -L %s<\"$*\";exit $?",
276 config.hostname, config.port,
277 config.username, config.timeout,
278 config.max_size * 1024, spam?"spam":"ham");
279 if (str_write_to_file(contents, fname) < 0) {
285 /* returned pointer must be free'ed by caller */
289 int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
293 const gchar *shell = g_getenv("SHELL");
294 gchar *spamc_wrapper = NULL;
296 if (msginfo == NULL && msglist == NULL) {
300 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
301 && prefs_common.work_offline
302 && !inc_offline_should_override(
303 _("Sylpheed-Claws needs network access in order "
304 "to feed this mail(s) to the remote learner."))) {
309 file = procmsg_get_message_file(msginfo);
313 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
314 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
315 if (spamc_wrapper != NULL) {
316 cmd = g_strconcat(shell?shell:"sh", " ",
317 spamc_wrapper, " ", file, NULL);
320 cmd = g_strdup_printf("sa-learn -u %s %s %s %s",
322 prefs_common.work_offline?"-L":"",
323 spam?"--spam":"--ham", file);
327 GSList *cur = msglist;
330 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
331 /* execute n-times the spamc command */
332 for (; cur; cur = cur->next) {
333 info = (MsgInfo *)cur->data;
334 gchar *tmpcmd = NULL;
335 gchar *tmpfile = get_tmp_file();
337 if (spamc_wrapper == NULL) {
338 spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
341 if (spamc_wrapper && tmpfile &&
342 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
343 tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
345 debug_print("%s\n", tmpcmd);
346 execute_command_line(tmpcmd, FALSE);
349 if (tmpfile != NULL) {
353 if (spamc_wrapper != NULL) {
354 g_free(spamc_wrapper);
358 cmd = g_strdup_printf("sa-learn -u %s %s %s",
360 prefs_common.work_offline?"-L":"",
361 spam?"--spam":"--ham");
363 /* concatenate all message tmpfiles to the sa-learn command-line */
364 for (; cur; cur = cur->next) {
365 info = (MsgInfo *)cur->data;
366 gchar *tmpcmd = NULL;
367 gchar *tmpfile = get_tmp_file();
370 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
371 tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
375 if (tmpfile != NULL) {
384 debug_print("%s\n", cmd);
385 /* only run sync calls to sa-learn/spamc to prevent system lockdown */
386 execute_command_line(cmd, FALSE);
388 if (spamc_wrapper != NULL) {
389 g_free(spamc_wrapper);
394 void spamassassin_save_config(void)
399 debug_print("Saving SpamAssassin Page\n");
401 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
402 pfile = prefs_write_open(rcpath);
404 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
407 if (prefs_write_param(param, pfile->fp) < 0) {
408 g_warning("Failed to write SpamAssassin configuration to file\n");
409 prefs_file_close_revert(pfile);
412 fprintf(pfile->fp, "\n");
414 prefs_file_close(pfile);
417 gboolean spamassassin_check_username(void)
419 if (config.username == NULL || config.username[0] == '\0') {
420 config.username = (gchar*)g_get_user_name();
421 if (config.username == NULL) {
423 spamassassin_unregister_hook();
425 procmsg_unregister_spam_learner(spamassassin_learn);
426 procmsg_spam_set_folder(NULL);
433 void spamassassin_set_message_callback(MessageCallback callback)
435 message_callback = callback;
438 gint plugin_init(gchar **error)
444 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
445 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the SpamAssassin plugin was built with");
449 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
450 *error = g_strdup("Your version of Sylpheed-Claws is too old for the SpamAssassin plugin");
454 prefs_set_default(param);
455 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
456 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
458 if (!spamassassin_check_username()) {
459 *error = g_strdup("Failed to get username");
462 spamassassin_gtk_init();
464 debug_print("Spamassassin plugin loaded\n");
466 if (config.process_emails) {
467 spamassassin_register_hook();
470 if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
471 log_error("Spamassassin plugin is loaded but disabled by its preferences.\n");
474 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
475 debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
476 procmsg_register_spam_learner(spamassassin_learn);
477 procmsg_spam_set_folder(config.save_folder);
484 void plugin_done(void)
487 spamassassin_unregister_hook();
489 g_free(config.hostname);
490 g_free(config.save_folder);
491 spamassassin_gtk_done();
492 procmsg_unregister_spam_learner(spamassassin_learn);
493 procmsg_spam_set_folder(NULL);
494 debug_print("Spamassassin plugin unloaded\n");
497 const gchar *plugin_name(void)
499 return _("SpamAssassin");
502 const gchar *plugin_desc(void)
504 return _("This plugin checks all messages that are received from an "
505 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
506 "server. You will need a SpamAssassin Server (spamd) running "
509 "When a message is identified as spam it can be deleted or "
510 "saved into a special folder.\n"
514 const gchar *plugin_type(void)
519 const gchar *plugin_licence(void)
524 const gchar *plugin_version(void)
529 void spamassassin_register_hook(void)
531 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
533 g_warning("Failed to register mail filtering hook");
534 config.process_emails = FALSE;
538 void spamassassin_unregister_hook(void)
541 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);