195dfe5300392a0d745aea3e710985afabfac758
[claws.git] / src / plugins / spamassassin / spamassassin.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto and the Sylpheed-Claws Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <sys/types.h>
27 #include <sys/wait.h>
28
29 #include <glib.h>
30
31 #if HAVE_LOCALE_H
32 #  include <locale.h>
33 #endif
34
35 #include "common/sylpheed.h"
36 #include "common/version.h"
37 #include "plugin.h"
38 #include "common/utils.h"
39 #include "hooks.h"
40 #include "procmsg.h"
41 #include "folder.h"
42 #include "prefs.h"
43 #include "prefs_gtk.h"
44 #include "intl.h"
45
46 #include "libspamc.h"
47 #include "spamassassin.h"
48
49 #ifdef HAVE_SYSEXITS_H
50 #include <sysexits.h>
51 #endif
52 #ifdef HAVE_ERRNO_H
53 #include <errno.h>
54 #endif
55 #ifdef HAVE_SYS_ERRNO_H
56 #include <sys/errno.h>
57 #endif
58 #ifdef HAVE_TIME_H
59 #include <time.h>
60 #endif
61 #ifdef HAVE_SYS_TIME_H
62 #include <sys/time.h>
63 #endif
64 #ifdef HAVE_SIGNAL_H
65 #include <signal.h>
66 #endif
67 #ifdef HAVE_PWD_H
68 #include <pwd.h>
69 #endif
70
71 enum {
72     CHILD_RUNNING = 1 << 0,
73     TIMEOUT_RUNNING = 1 << 1,
74 };
75
76 static guint hook_id;
77 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
78 static gchar *username = NULL;
79
80 static SpamAssassinConfig config;
81
82 static PrefParam param[] = {
83         {"enable", "FALSE", &config.enable, P_BOOL,
84          NULL, NULL, NULL},
85         {"hostname", "localhost", &config.hostname, P_STRING,
86          NULL, NULL, NULL},
87         {"port", "783", &config.port, P_INT,
88          NULL, NULL, NULL},
89         {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
90          NULL, NULL, NULL},
91         {"save_folder", NULL, &config.save_folder, P_STRING,
92          NULL, NULL, NULL},
93         {"max_size", "250", &config.max_size, P_INT,
94          NULL, NULL, NULL},
95         {"timeout", "30", &config.timeout, P_INT,
96          NULL, NULL, NULL},
97
98         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
99 };
100
101 gboolean timeout_func(gpointer data)
102 {
103         gint *running = (gint *) data;
104
105         if (*running & CHILD_RUNNING)
106                 return TRUE;
107
108         *running &= ~TIMEOUT_RUNNING;
109         return FALSE;
110 }
111
112 static gboolean msg_is_spam(FILE *fp)
113 {
114         struct sockaddr addr;
115         struct message m;
116         gboolean is_spam = FALSE;
117
118         if (lookup_host(config.hostname, config.port, &addr) != EX_OK) {
119                 debug_print("failed to look up spamd host\n");
120                 return FALSE;
121         }
122
123         m.type = MESSAGE_NONE;
124         m.max_len = config.max_size * 1024;
125         m.timeout = config.timeout;
126
127         if (message_read(fileno(fp), flags, &m) != EX_OK) {
128                 debug_print("failed to read message\n");
129                 message_cleanup(&m);
130                 return FALSE;
131         }
132
133         if (message_filter(&addr, username, flags, &m) != EX_OK) {
134                 debug_print("filtering the message failed\n");
135                 message_cleanup(&m);
136                 return FALSE;
137         }
138
139         if (m.is_spam == EX_ISSPAM)
140                 is_spam = TRUE;
141
142         message_cleanup(&m);
143
144         return is_spam;
145 }
146
147 static gboolean mail_filtering_hook(gpointer source, gpointer data)
148 {
149         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
150         MsgInfo *msginfo = mail_filtering_data->msginfo;
151         gboolean is_spam = FALSE;
152         FILE *fp = NULL;
153         int pid = 0;
154         int status;
155
156         if (!config.enable)
157                 return FALSE;
158
159         debug_print("Filtering message %d\n", msginfo->msgnum);
160
161         if ((fp = procmsg_open_message(msginfo)) == NULL) {
162                 debug_print("failed to open message file\n");
163                 return FALSE;
164         }
165
166         pid = fork();
167         if (pid == 0) {
168                 _exit(msg_is_spam(fp) ? 1 : 0);
169         } else {
170                 gint running = 0;
171
172                 running |= CHILD_RUNNING;
173
174                 g_timeout_add(1000, timeout_func, &running);
175                 running |= TIMEOUT_RUNNING;
176
177                 while(running & CHILD_RUNNING) {
178                         waitpid(pid, &status, WNOHANG);
179                         if (WIFEXITED(status)) {
180                                 running &= ~CHILD_RUNNING;
181                         }
182             
183                         g_main_iteration(TRUE);
184                 }
185
186                 while (running & TIMEOUT_RUNNING)
187                         g_main_iteration(TRUE);
188         }
189         is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
190
191         fclose(fp);
192
193         if (is_spam) {
194                 debug_print("message is spam\n");
195                             
196                 if (config.receive_spam) {
197                         FolderItem *save_folder;
198
199                         if ((!config.save_folder) ||
200                             (config.save_folder[0] == '\0') ||
201                             ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
202                                 save_folder = folder_get_default_trash();
203
204                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
205                         folder_item_move_msg(save_folder, msginfo);
206                 } else {
207                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
208                 }
209
210                 return TRUE;
211         }
212         
213         return FALSE;
214 }
215
216 SpamAssassinConfig *spamassassin_get_config(void)
217 {
218         return &config;
219 }
220
221 void spamassassin_save_config(void)
222 {
223         PrefFile *pfile;
224         gchar *rcpath;
225
226         debug_print("Saving SpamAssassin Page\n");
227
228         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
229         pfile = prefs_write_open(rcpath);
230         g_free(rcpath);
231         if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
232                 return;
233
234         if (prefs_write_param(param, pfile->fp) < 0) {
235                 g_warning("failed to write SpamAssassin configuration to file\n");
236                 prefs_file_close_revert(pfile);
237                 return;
238         }
239         fprintf(pfile->fp, "\n");
240
241         prefs_file_close(pfile);
242 }
243
244 gint plugin_init(gchar **error)
245 {
246         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
247                 *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
248                 return -1;
249         }
250
251         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
252                 *error = g_strdup("Your sylpheed version is too old");
253                 return -1;
254         }
255
256         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
257         if (hook_id == -1) {
258                 *error = g_strdup("Failed to register mail filtering hook");
259                 return -1;
260         }
261
262         username = g_get_user_name();
263         if (username == NULL) {
264                 *error = g_strdup("Failed to get username");
265                 return -1;
266         }
267
268         prefs_set_default(param);
269         prefs_read_config(param, "SpamAssassin", COMMON_RC);
270
271         debug_print("Spamassassin plugin loaded\n");
272
273         return 0;
274         
275 }
276
277 void plugin_done(void)
278 {
279         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
280         g_free(config.hostname);
281         g_free(config.save_folder);
282
283         debug_print("Spamassassin plugin unloaded\n");
284 }
285
286 const gchar *plugin_name(void)
287 {
288         return _("SpamAssassin");
289 }
290
291 const gchar *plugin_desc(void)
292 {
293         return _("This plugin checks all messages that are received from a POP "
294                  "account for spam using a SpamAssassin server. You will need "
295                  "a SpamAssassin Server (spamd) running somewhere.\n"
296                  "\n"
297                  "When a message is identified as spam it can be deleted or "
298                  "saved into a special folder.\n"
299                  "\n"
300                  "This plugin only contains the actual function for filtering "
301                  "and deleting or moving the message. You probably want to load "
302                  "a User Interface plugin too, otherwise you will have to "
303                  "manually write the plugin configuration.\n");
304 }
305
306 const gchar *plugin_type(void)
307 {
308         return "Common";
309 }