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"
49 #include "prefs_common.h"
51 #ifdef HAVE_SYSEXITS_H
57 #ifdef HAVE_SYS_ERRNO_H
58 #include <sys/errno.h>
63 #ifdef HAVE_SYS_TIME_H
74 CHILD_RUNNING = 1 << 0,
75 TIMEOUT_RUNNING = 1 << 1,
79 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
80 static MessageCallback message_callback;
82 static SpamAssassinConfig config;
84 static PrefParam param[] = {
85 {"transport", "0", &config.transport, P_INT,
87 {"hostname", "localhost", &config.hostname, P_STRING,
89 {"port", "783", &config.port, P_INT,
91 {"socket", "", &config.socket, P_STRING,
93 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
95 {"save_folder", NULL, &config.save_folder, P_STRING,
97 {"max_size", "250", &config.max_size, P_INT,
99 {"timeout", "30", &config.timeout, P_INT,
101 {"username", "", &config.username, P_STRING,
104 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
107 gboolean timeout_func(gpointer data)
109 gint *running = (gint *) data;
111 if (*running & CHILD_RUNNING)
114 *running &= ~TIMEOUT_RUNNING;
118 static gboolean msg_is_spam(FILE *fp)
120 struct transport trans;
122 gboolean is_spam = FALSE;
124 transport_init(&trans);
125 switch (config.transport) {
126 case SPAMASSASSIN_TRANSPORT_LOCALHOST:
127 trans.type = TRANSPORT_LOCALHOST;
128 trans.port = config.port;
130 case SPAMASSASSIN_TRANSPORT_TCP:
131 trans.type = TRANSPORT_TCP;
132 trans.hostname = config.hostname;
133 trans.port = config.port;
135 case SPAMASSASSIN_TRANSPORT_UNIX:
136 trans.type = TRANSPORT_UNIX;
137 trans.socketpath = config.socket;
143 if (transport_setup(&trans, flags) != EX_OK) {
144 log_error("Spamassassin plugin couldn't connect to spamd.\n");
145 debug_print("failed to setup transport\n");
149 m.type = MESSAGE_NONE;
150 m.max_len = config.max_size * 1024;
151 m.timeout = config.timeout;
153 if (message_read(fileno(fp), flags, &m) != EX_OK) {
154 debug_print("failed to read message\n");
159 if (message_filter(&trans, config.username, flags, &m) != EX_OK) {
160 debug_print("filtering the message failed\n");
165 if (m.is_spam == EX_ISSPAM)
173 static gboolean mail_filtering_hook(gpointer source, gpointer data)
175 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
176 MsgInfo *msginfo = mail_filtering_data->msginfo;
177 gboolean is_spam = FALSE;
182 if (config.transport == SPAMASSASSIN_DISABLED) {
183 log_error("Spamassassin plugin is disabled by its preferences.\n");
186 debug_print("Filtering message %d\n", msginfo->msgnum);
187 if (message_callback != NULL)
188 message_callback(_("SpamAssassin: filtering message..."));
190 if ((fp = procmsg_open_message(msginfo)) == NULL) {
191 debug_print("failed to open message file\n");
197 _exit(msg_is_spam(fp) ? 1 : 0);
201 running |= CHILD_RUNNING;
203 g_timeout_add(50, timeout_func, &running);
204 running |= TIMEOUT_RUNNING;
206 while(running & CHILD_RUNNING) {
209 ret = waitpid(pid, &status, WNOHANG);
211 if (WIFEXITED(status)) {
212 running &= ~CHILD_RUNNING;
213 is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
216 running &= ~CHILD_RUNNING;
217 } /* ret == 0 continue */
219 g_main_iteration(TRUE);
222 while (running & TIMEOUT_RUNNING)
223 g_main_iteration(TRUE);
229 debug_print("message is spam\n");
230 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
231 if (config.receive_spam) {
232 FolderItem *save_folder;
234 if ((!config.save_folder) ||
235 (config.save_folder[0] == '\0') ||
236 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
237 save_folder = folder_get_default_trash();
239 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
240 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
241 folder_item_move_msg(save_folder, msginfo);
243 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
248 debug_print("message is ham\n");
249 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
254 SpamAssassinConfig *spamassassin_get_config(void)
259 void spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
263 gboolean async = FALSE;
265 if (msginfo == NULL && msglist == NULL)
269 file = procmsg_get_message_file(msginfo);
272 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
273 cmd = g_strdup_printf("sa-learn -u %s %s %s %s",
275 prefs_common.work_offline?"-L":"",
276 spam?"--spam":"--ham", file);
278 cmd = g_strdup_printf("spamc -d %s -p %u -u %s -t %u -s %u -L %s < %s",
279 config.hostname, config.port,
280 config.username, config.timeout,
281 config.max_size * 1024, spam?"spam":"ham", file);
285 GSList *cur = msglist;
288 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
289 cmd = g_strdup_printf("spamc -d %s -p %u -u %s -t %u -s %u -L %s",
290 config.hostname, config.port,
291 config.username, config.timeout,
292 config.max_size * 1024, spam?"spam":"ham");
294 /* execute n-times the spamc command */
295 for (; cur; cur = cur->next) {
296 info = (MsgInfo *)cur->data;
297 gchar *tmpcmd = NULL;
298 gchar *tmpfile = get_tmp_file();
301 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
302 tmpcmd = g_strconcat(cmd, " < ", tmpfile, NULL);
303 debug_print("%s\n", tmpcmd);
304 execute_command_line(tmpcmd, FALSE);
315 cmd = g_strdup_printf("sa-learn -u %s %s %s",
317 prefs_common.work_offline?"-L":"",
318 spam?"--spam":"--ham");
320 /* concatenate all message tmpfiles to the sa-learn command-line */
321 for (; cur; cur = cur->next) {
322 info = (MsgInfo *)cur->data;
323 gchar *tmpcmd = NULL;
324 gchar *tmpfile = get_tmp_file();
327 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
328 tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
340 debug_print("%s\n", cmd);
341 /* only run async if we have a list, or we could end up
342 * forking lots of perl processes and bury the machine */
344 execute_command_line(cmd, async);
348 void spamassassin_save_config(void)
353 debug_print("Saving SpamAssassin Page\n");
355 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
356 pfile = prefs_write_open(rcpath);
358 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
361 if (prefs_write_param(param, pfile->fp) < 0) {
362 g_warning("failed to write SpamAssassin configuration to file\n");
363 prefs_file_close_revert(pfile);
366 fprintf(pfile->fp, "\n");
368 prefs_file_close(pfile);
371 gboolean spamassassin_check_username(void)
373 if (config.username == NULL || config.username[0] == '\0') {
374 config.username = (gchar*)g_get_user_name();
375 if (config.username == NULL) {
376 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
377 procmsg_unregister_spam_learner(spamassassin_learn);
378 procmsg_spam_set_folder(NULL);
385 void spamassassin_set_message_callback(MessageCallback callback)
387 message_callback = callback;
390 gint plugin_init(gchar **error)
394 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
395 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the SpamAssassin plugin was built with");
399 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
400 *error = g_strdup("Your version of Sylpheed-Claws is too old for the SpamAssassin plugin");
404 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
406 *error = g_strdup("Failed to register mail filtering hook");
410 prefs_set_default(param);
411 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
412 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
414 if (!spamassassin_check_username()) {
415 *error = g_strdup("Failed to get username");
418 spamassassin_gtk_init();
420 debug_print("Spamassassin plugin loaded\n");
422 if (config.transport == SPAMASSASSIN_DISABLED) {
423 log_error("Spamassassin plugin is loaded but disabled by its preferences.\n");
426 if (config.transport != SPAMASSASSIN_DISABLED) {
427 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
428 debug_print("enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
429 procmsg_register_spam_learner(spamassassin_learn);
430 procmsg_spam_set_folder(config.save_folder);
437 void plugin_done(void)
439 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
440 g_free(config.hostname);
441 g_free(config.save_folder);
442 spamassassin_gtk_done();
443 procmsg_unregister_spam_learner(spamassassin_learn);
444 procmsg_spam_set_folder(NULL);
445 debug_print("Spamassassin plugin unloaded\n");
448 const gchar *plugin_name(void)
450 return _("SpamAssassin");
453 const gchar *plugin_desc(void)
455 return _("This plugin checks all messages that are received from an "
456 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
457 "server. You will need a SpamAssassin Server (spamd) running "
460 "When a message is identified as spam it can be deleted or "
461 "saved into a special folder.\n"
465 const gchar *plugin_type(void)
470 const gchar *plugin_licence(void)
475 const gchar *plugin_version(void)