2005-12-15 [paul] 1.9.100cvs93
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <glib/gi18n.h>
31
32 #if HAVE_LOCALE_H
33 #  include <locale.h>
34 #endif
35
36 #include "common/sylpheed.h"
37 #include "common/version.h"
38 #include "plugin.h"
39 #include "common/utils.h"
40 #include "hooks.h"
41 #include "procmsg.h"
42 #include "folder.h"
43 #include "prefs.h"
44 #include "prefs_gtk.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 static MessageCallback message_callback;
80
81 static SpamAssassinConfig config;
82
83 static PrefParam param[] = {
84         {"transport", "0", &config.transport, P_INT,
85          NULL, NULL, NULL},
86         {"hostname", "localhost", &config.hostname, P_STRING,
87          NULL, NULL, NULL},
88         {"port", "783", &config.port, P_INT,
89          NULL, NULL, NULL},
90         {"socket", "", &config.socket, P_STRING,
91          NULL, NULL, NULL},
92         {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
93          NULL, NULL, NULL},
94         {"save_folder", NULL, &config.save_folder, P_STRING,
95          NULL, NULL, NULL},
96         {"max_size", "250", &config.max_size, P_INT,
97          NULL, NULL, NULL},
98         {"timeout", "30", &config.timeout, P_INT,
99          NULL, NULL, NULL},
100
101         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
102 };
103
104 gboolean timeout_func(gpointer data)
105 {
106         gint *running = (gint *) data;
107
108         if (*running & CHILD_RUNNING)
109                 return TRUE;
110
111         *running &= ~TIMEOUT_RUNNING;
112         return FALSE;
113 }
114
115 static gboolean msg_is_spam(FILE *fp)
116 {
117         struct transport trans;
118         struct message m;
119         gboolean is_spam = FALSE;
120
121         transport_init(&trans);
122         switch (config.transport) {
123         case SPAMASSASSIN_TRANSPORT_LOCALHOST:
124                 trans.type = TRANSPORT_LOCALHOST;
125                 trans.port = config.port;
126                 break;
127         case SPAMASSASSIN_TRANSPORT_TCP:
128                 trans.type = TRANSPORT_TCP;
129                 trans.hostname = config.hostname;
130                 trans.port = config.port;
131                 break;
132         case SPAMASSASSIN_TRANSPORT_UNIX:
133                 trans.type = TRANSPORT_UNIX;
134                 trans.socketpath = config.socket;
135                 break;
136         default:
137                 return FALSE;
138         }
139
140         if (transport_setup(&trans, flags) != EX_OK) {
141                 debug_print("failed to setup transport\n");
142                 return FALSE;
143         }
144
145         m.type = MESSAGE_NONE;
146         m.max_len = config.max_size * 1024;
147         m.timeout = config.timeout;
148
149         if (message_read(fileno(fp), flags, &m) != EX_OK) {
150                 debug_print("failed to read message\n");
151                 message_cleanup(&m);
152                 return FALSE;
153         }
154
155         if (message_filter(&trans, username, flags, &m) != EX_OK) {
156                 debug_print("filtering the message failed\n");
157                 message_cleanup(&m);
158                 return FALSE;
159         }
160
161         if (m.is_spam == EX_ISSPAM)
162                 is_spam = TRUE;
163
164         message_cleanup(&m);
165
166         return is_spam;
167 }
168
169 static gboolean mail_filtering_hook(gpointer source, gpointer data)
170 {
171         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
172         MsgInfo *msginfo = mail_filtering_data->msginfo;
173         gboolean is_spam = FALSE;
174         FILE *fp = NULL;
175         int pid = 0;
176         int status;
177
178         if (config.transport == SPAMASSASSIN_DISABLED)
179                 return FALSE;
180
181         debug_print("Filtering message %d\n", msginfo->msgnum);
182         if (message_callback != NULL)
183                 message_callback(_("SpamAssassin: filtering message..."));
184
185         if ((fp = procmsg_open_message(msginfo)) == NULL) {
186                 debug_print("failed to open message file\n");
187                 return FALSE;
188         }
189
190         pid = fork();
191         if (pid == 0) {
192                 _exit(msg_is_spam(fp) ? 1 : 0);
193         } else {
194                 gint running = 0;
195
196                 running |= CHILD_RUNNING;
197
198                 g_timeout_add(50, timeout_func, &running);
199                 running |= TIMEOUT_RUNNING;
200
201                 while(running & CHILD_RUNNING) {
202                         int ret;
203
204                         ret = waitpid(pid, &status, WNOHANG);
205                         if (ret == pid) {
206                                 if (WIFEXITED(status)) {
207                                         running &= ~CHILD_RUNNING;
208                                         is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
209                                 }
210                         } if (ret < 0) {
211                                 running &= ~CHILD_RUNNING;
212                         } /* ret == 0 continue */
213             
214                         g_main_iteration(TRUE);
215                 }
216
217                 while (running & TIMEOUT_RUNNING)
218                         g_main_iteration(TRUE);
219         }
220
221         fclose(fp);
222
223         if (is_spam) {
224                 debug_print("message is spam\n");
225                             
226                 if (config.receive_spam) {
227                         FolderItem *save_folder;
228
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();
233
234                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
235                         folder_item_move_msg(save_folder, msginfo);
236                 } else {
237                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
238                 }
239
240                 return TRUE;
241         }
242         
243         return FALSE;
244 }
245
246 SpamAssassinConfig *spamassassin_get_config(void)
247 {
248         return &config;
249 }
250
251 void spamassassin_save_config(void)
252 {
253         PrefFile *pfile;
254         gchar *rcpath;
255
256         debug_print("Saving SpamAssassin Page\n");
257
258         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
259         pfile = prefs_write_open(rcpath);
260         g_free(rcpath);
261         if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
262                 return;
263
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);
267                 return;
268         }
269         fprintf(pfile->fp, "\n");
270
271         prefs_file_close(pfile);
272 }
273
274 void spamassassin_set_message_callback(MessageCallback callback)
275 {
276         message_callback = callback;
277 }
278
279 gint plugin_init(gchar **error)
280 {
281         gchar *rcpath;
282
283         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
284                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the SpamAssassin plugin was built with");
285                 return -1;
286         }
287
288         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
289                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the SpamAssassin plugin");
290                 return -1;
291         }
292
293         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
294         if (hook_id == -1) {
295                 *error = g_strdup("Failed to register mail filtering hook");
296                 return -1;
297         }
298
299         username = (gchar*)g_get_user_name();
300         if (username == NULL) {
301                 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
302                 *error = g_strdup("Failed to get username");
303                 return -1;
304         }
305
306         prefs_set_default(param);
307         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
308         prefs_read_config(param, "SpamAssassin", rcpath, NULL);
309         g_free(rcpath);
310         spamassassin_gtk_init();
311
312         debug_print("Spamassassin plugin loaded\n");
313
314         return 0;
315         
316 }
317
318 void plugin_done(void)
319 {
320         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
321         g_free(config.hostname);
322         g_free(config.save_folder);
323         spamassassin_gtk_done();
324
325         debug_print("Spamassassin plugin unloaded\n");
326 }
327
328 const gchar *plugin_name(void)
329 {
330         return _("SpamAssassin");
331 }
332
333 const gchar *plugin_desc(void)
334 {
335         return _("This plugin checks all messages that are received from an "
336                  "IMAP, LOCAL or POP account for spam using a SpamAssassin "
337                  "server. You will need a SpamAssassin Server (spamd) running "
338                  "somewhere.\n"
339                  "\n"
340                  "When a message is identified as spam it can be deleted or "
341                  "saved into a special folder.\n"
342                  "\n");
343 }
344
345 const gchar *plugin_type(void)
346 {
347         return "GTK2";
348 }
349
350 const gchar *plugin_licence(void)
351 {
352         return "GPL";
353 }
354
355 const gchar *plugin_version(void)
356 {
357         return VERSION;
358 }
359