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>
35 #include "common/sylpheed.h"
36 #include "common/version.h"
38 #include "common/utils.h"
43 #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;
80 static SpamAssassinConfig config;
82 static PrefParam param[] = {
83 {"enable", "FALSE", &config.enable, P_BOOL,
85 {"hostname", "localhost", &config.hostname, P_STRING,
87 {"port", "783", &config.port, P_INT,
89 {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
91 {"save_folder", NULL, &config.save_folder, P_STRING,
93 {"max_size", "250", &config.max_size, P_INT,
95 {"timeout", "30", &config.timeout, P_INT,
98 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
101 gboolean timeout_func(gpointer data)
103 gint *running = (gint *) data;
105 if (*running & CHILD_RUNNING)
108 *running &= ~TIMEOUT_RUNNING;
112 static gboolean msg_is_spam(FILE *fp)
114 struct transport trans;
116 gboolean is_spam = FALSE;
118 transport_init(&trans);
119 trans.type = TRANSPORT_TCP;
120 trans.hostname = config.hostname;
121 trans.port = config.port;
123 if (transport_setup(&trans, flags) != EX_OK) {
124 debug_print("failed to setup transport\n");
128 m.type = MESSAGE_NONE;
129 m.max_len = config.max_size * 1024;
130 m.timeout = config.timeout;
132 if (message_read(fileno(fp), flags, &m) != EX_OK) {
133 debug_print("failed to read message\n");
138 if (message_filter(&trans, username, flags, &m) != EX_OK) {
139 debug_print("filtering the message failed\n");
144 if (m.is_spam == EX_ISSPAM)
152 static gboolean mail_filtering_hook(gpointer source, gpointer data)
154 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
155 MsgInfo *msginfo = mail_filtering_data->msginfo;
156 gboolean is_spam = FALSE;
164 debug_print("Filtering message %d\n", msginfo->msgnum);
166 if ((fp = procmsg_open_message(msginfo)) == NULL) {
167 debug_print("failed to open message file\n");
173 _exit(msg_is_spam(fp) ? 1 : 0);
177 running |= CHILD_RUNNING;
179 g_timeout_add(1000, timeout_func, &running);
180 running |= TIMEOUT_RUNNING;
182 while(running & CHILD_RUNNING) {
183 waitpid(pid, &status, WNOHANG);
184 if (WIFEXITED(status)) {
185 running &= ~CHILD_RUNNING;
188 g_main_iteration(TRUE);
191 while (running & TIMEOUT_RUNNING)
192 g_main_iteration(TRUE);
194 is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
199 debug_print("message is spam\n");
201 if (config.receive_spam) {
202 FolderItem *save_folder;
204 if ((!config.save_folder) ||
205 (config.save_folder[0] == '\0') ||
206 ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
207 save_folder = folder_get_default_trash();
209 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
210 folder_item_move_msg(save_folder, msginfo);
212 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
221 SpamAssassinConfig *spamassassin_get_config(void)
226 void spamassassin_save_config(void)
231 debug_print("Saving SpamAssassin Page\n");
233 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
234 pfile = prefs_write_open(rcpath);
236 if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
239 if (prefs_write_param(param, pfile->fp) < 0) {
240 g_warning("failed to write SpamAssassin configuration to file\n");
241 prefs_file_close_revert(pfile);
244 fprintf(pfile->fp, "\n");
246 prefs_file_close(pfile);
249 gint plugin_init(gchar **error)
251 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
252 *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
256 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
257 *error = g_strdup("Your sylpheed version is too old");
261 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
263 *error = g_strdup("Failed to register mail filtering hook");
267 username = g_get_user_name();
268 if (username == NULL) {
269 *error = g_strdup("Failed to get username");
273 prefs_set_default(param);
274 prefs_read_config(param, "SpamAssassin", COMMON_RC);
276 debug_print("Spamassassin plugin loaded\n");
282 void plugin_done(void)
284 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
285 g_free(config.hostname);
286 g_free(config.save_folder);
288 debug_print("Spamassassin plugin unloaded\n");
291 const gchar *plugin_name(void)
293 return _("SpamAssassin");
296 const gchar *plugin_desc(void)
298 return _("This plugin checks all messages that are received from a POP "
299 "account for spam using a SpamAssassin server. You will need "
300 "a SpamAssassin Server (spamd) running somewhere.\n"
302 "When a message is identified as spam it can be deleted or "
303 "saved into a special folder.\n"
305 "This plugin only contains the actual function for filtering "
306 "and deleting or moving the message. You probably want to load "
307 "a User Interface plugin too, otherwise you will have to "
308 "manually write the plugin configuration.\n");
311 const gchar *plugin_type(void)