2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 debug_print("failed to setup transport\n");
145 m.type = MESSAGE_NONE;
146 m.max_len = config.max_size * 1024;
147 m.timeout = config.timeout;
149 if (message_read(fileno(fp), flags, &m) != EX_OK) {
150 debug_print("failed to read message\n");
155 if (message_filter(&trans, username, flags, &m) != EX_OK) {
156 debug_print("filtering the message failed\n");
161 if (m.is_spam == EX_ISSPAM)
169 static gboolean mail_filtering_hook(gpointer source, gpointer data)
171 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
172 MsgInfo *msginfo = mail_filtering_data->msginfo;
173 gboolean is_spam = FALSE;
178 if (config.transport == SPAMASSASSIN_DISABLED)
181 debug_print("Filtering message %d\n", msginfo->msgnum);
182 if (message_callback != NULL)
183 message_callback(_("SpamAssassin: filtering message..."));
185 if ((fp = procmsg_open_message(msginfo)) == NULL) {
186 debug_print("failed to open message file\n");
192 _exit(msg_is_spam(fp) ? 1 : 0);
196 running |= CHILD_RUNNING;
198 g_timeout_add(1000, timeout_func, &running);
199 running |= TIMEOUT_RUNNING;
201 while(running & CHILD_RUNNING) {
204 ret = waitpid(pid, &status, WNOHANG);
206 if (WIFEXITED(status)) {
207 running &= ~CHILD_RUNNING;
208 is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
211 running &= ~CHILD_RUNNING;
212 } /* ret == 0 continue */
214 g_main_iteration(TRUE);
217 while (running & TIMEOUT_RUNNING)
218 g_main_iteration(TRUE);
224 debug_print("message is spam\n");
226 if (config.receive_spam) {
227 FolderItem *save_folder;
229 if ((!config.save_folder) ||
230 (config.save_folder[0] == '\0') ||
231 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
232 save_folder = folder_get_default_trash();
234 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
235 folder_item_move_msg(save_folder, msginfo);
237 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
246 SpamAssassinConfig *spamassassin_get_config(void)
251 void spamassassin_save_config(void)
256 debug_print("Saving SpamAssassin Page\n");
258 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
259 pfile = prefs_write_open(rcpath);
261 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
264 if (prefs_write_param(param, pfile->fp) < 0) {
265 g_warning("failed to write SpamAssassin configuration to file\n");
266 prefs_file_close_revert(pfile);
269 fprintf(pfile->fp, "\n");
271 prefs_file_close(pfile);
274 void spamassassin_set_message_callback(MessageCallback callback)
276 message_callback = callback;
279 gint plugin_init(gchar **error)
283 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
284 *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
288 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
289 *error = g_strdup("Your sylpheed version is too old");
293 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
295 *error = g_strdup("Failed to register mail filtering hook");
299 username = g_get_user_name();
300 if (username == NULL) {
301 *error = g_strdup("Failed to get username");
305 prefs_set_default(param);
306 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
307 prefs_read_config(param, "SpamAssassin", rcpath, NULL);
310 debug_print("Spamassassin plugin loaded\n");
316 void plugin_done(void)
318 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
319 g_free(config.hostname);
320 g_free(config.save_folder);
322 debug_print("Spamassassin plugin unloaded\n");
325 const gchar *plugin_name(void)
327 return _("SpamAssassin");
330 const gchar *plugin_desc(void)
332 return _("This plugin checks all messages that are received from an "
333 "IMAP, LOCAL or POP account for spam using a SpamAssassin "
334 "server. You will need a SpamAssassin Server (spamd) running "
337 "When a message is identified as spam it can be deleted or "
338 "saved into a special folder.\n"
340 "This plugin only contains the actual function for filtering "
341 "and deleting or moving the message. You probably want to load "
342 "a User Interface plugin too, otherwise you will have to "
343 "manually write the plugin configuration.\n");
346 const gchar *plugin_type(void)