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 #ifdef HAVE_SYSEXITS_H
55 #ifdef HAVE_SYS_ERRNO_H
56 #include <sys/errno.h>
61 #ifdef HAVE_SYS_TIME_H
72 CHILD_RUNNING = 1 << 0,
73 TIMEOUT_RUNNING = 1 << 1,
77 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
78 static gchar *username = NULL;
79 static MessageCallback message_callback;
81 static SpamAssassinConfig config;
83 static PrefParam param[] = {
84 {"transport", "0", &config.transport, P_INT,
86 {"hostname", "localhost", &config.hostname, P_STRING,
88 {"port", "783", &config.port, P_INT,
90 {"socket", "", &config.socket, P_STRING,
92 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
94 {"save_folder", NULL, &config.save_folder, P_STRING,
96 {"max_size", "250", &config.max_size, P_INT,
98 {"timeout", "30", &config.timeout, P_INT,
101 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
104 gboolean timeout_func(gpointer data)
106 gint *running = (gint *) data;
108 if (*running & CHILD_RUNNING)
111 *running &= ~TIMEOUT_RUNNING;
115 static gboolean msg_is_spam(FILE *fp)
117 struct transport trans;
119 gboolean is_spam = FALSE;
121 transport_init(&trans);
122 switch (config.transport) {
123 case SPAMASSASSIN_TRANSPORT_LOCALHOST:
124 trans.type = TRANSPORT_LOCALHOST;
125 trans.port = config.port;
127 case SPAMASSASSIN_TRANSPORT_TCP:
128 trans.type = TRANSPORT_TCP;
129 trans.hostname = config.hostname;
130 trans.port = config.port;
132 case SPAMASSASSIN_TRANSPORT_UNIX:
133 trans.type = TRANSPORT_UNIX;
134 trans.socketpath = config.socket;
140 if (transport_setup(&trans, flags) != EX_OK) {
141 log_error("Spamassassin plugin couldn't connect to spamd.\n");
142 debug_print("failed to setup transport\n");
146 m.type = MESSAGE_NONE;
147 m.max_len = config.max_size * 1024;
148 m.timeout = config.timeout;
150 if (message_read(fileno(fp), flags, &m) != EX_OK) {
151 debug_print("failed to read message\n");
156 if (message_filter(&trans, username, flags, &m) != EX_OK) {
157 debug_print("filtering the message failed\n");
162 if (m.is_spam == EX_ISSPAM)
170 static gboolean mail_filtering_hook(gpointer source, gpointer data)
172 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
173 MsgInfo *msginfo = mail_filtering_data->msginfo;
174 gboolean is_spam = FALSE;
179 if (config.transport == SPAMASSASSIN_DISABLED) {
180 log_error("Spamassassin plugin is disabled by its preferences.\n");
183 debug_print("Filtering message %d\n", msginfo->msgnum);
184 if (message_callback != NULL)
185 message_callback(_("SpamAssassin: filtering message..."));
187 if ((fp = procmsg_open_message(msginfo)) == NULL) {
188 debug_print("failed to open message file\n");
194 _exit(msg_is_spam(fp) ? 1 : 0);
198 running |= CHILD_RUNNING;
200 g_timeout_add(50, timeout_func, &running);
201 running |= TIMEOUT_RUNNING;
203 while(running & CHILD_RUNNING) {
206 ret = waitpid(pid, &status, WNOHANG);
208 if (WIFEXITED(status)) {
209 running &= ~CHILD_RUNNING;
210 is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
213 running &= ~CHILD_RUNNING;
214 } /* ret == 0 continue */
216 g_main_iteration(TRUE);
219 while (running & TIMEOUT_RUNNING)
220 g_main_iteration(TRUE);
226 debug_print("message is spam\n");
227 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
228 if (config.receive_spam) {
229 FolderItem *save_folder;
231 if ((!config.save_folder) ||
232 (config.save_folder[0] == '\0') ||
233 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
234 save_folder = folder_get_default_trash();
236 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
237 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
238 folder_item_move_msg(save_folder, msginfo);
240 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
245 debug_print("message is ham\n");
246 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
251 SpamAssassinConfig *spamassassin_get_config(void)
256 void spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
260 gboolean async = FALSE;
262 if (msginfo == NULL && msglist == NULL)
266 file = procmsg_get_message_file(msginfo);
269 cmd = g_strdup_printf("sa-learn %s %s",
270 spam?"--spam":"--ham", file);
275 cmd = g_strdup_printf("sa-learn %s", spam?"--spam":"--ham");
276 for (cur = msglist; cur; cur = cur->next) {
277 info = (MsgInfo *)cur->data;
278 gchar *tmpcmd = NULL;
279 gchar *tmpfile = get_tmp_file();
282 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
284 (cmd, " ", tmpfile, NULL);
295 debug_print("%s\n",cmd);
296 /* only run async if we have a list, or we could end up
297 * forking lots of perl processes and bury the machine */
298 execute_command_line(cmd, async);
303 void spamassassin_save_config(void)
308 debug_print("Saving SpamAssassin Page\n");
310 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
311 pfile = prefs_write_open(rcpath);
313 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
316 if (prefs_write_param(param, pfile->fp) < 0) {
317 g_warning("failed to write SpamAssassin configuration to file\n");
318 prefs_file_close_revert(pfile);
321 fprintf(pfile->fp, "\n");
323 prefs_file_close(pfile);
326 void spamassassin_set_message_callback(MessageCallback callback)
328 message_callback = callback;
331 gint plugin_init(gchar **error)
335 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
336 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the SpamAssassin plugin was built with");
340 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
341 *error = g_strdup("Your version of Sylpheed-Claws is too old for the SpamAssassin plugin");
345 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
347 *error = g_strdup("Failed to register mail filtering hook");
351 username = (gchar*)g_get_user_name();
352 if (username == NULL) {
353 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
354 *error = g_strdup("Failed to get username");
358 prefs_set_default(param);
359 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
360 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
362 spamassassin_gtk_init();
364 procmsg_register_spam_learner(spamassassin_learn);
365 procmsg_spam_set_folder(config.save_folder);
367 debug_print("Spamassassin plugin loaded\n");
369 if (config.transport == SPAMASSASSIN_DISABLED) {
370 log_error("Spamassassin plugin is loaded but disabled by its preferences.\n");
377 void plugin_done(void)
379 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
380 g_free(config.hostname);
381 g_free(config.save_folder);
382 spamassassin_gtk_done();
383 procmsg_unregister_spam_learner(spamassassin_learn);
384 procmsg_spam_set_folder(NULL);
385 debug_print("Spamassassin plugin unloaded\n");
388 const gchar *plugin_name(void)
390 return _("SpamAssassin");
393 const gchar *plugin_desc(void)
395 return _("This plugin checks all messages that are received from an "
396 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
397 "server. You will need a SpamAssassin Server (spamd) running "
400 "When a message is identified as spam it can be deleted or "
401 "saved into a special folder.\n"
405 const gchar *plugin_type(void)
410 const gchar *plugin_licence(void)
415 const gchar *plugin_version(void)