sync 099claws
[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         {"transport", "0", &config.transport, P_INT,
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         {"socket", "", &config.socket, P_STRING,
90          NULL, NULL, NULL},
91         {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
92          NULL, NULL, NULL},
93         {"save_folder", NULL, &config.save_folder, P_STRING,
94          NULL, NULL, NULL},
95         {"max_size", "250", &config.max_size, P_INT,
96          NULL, NULL, NULL},
97         {"timeout", "30", &config.timeout, P_INT,
98          NULL, NULL, NULL},
99
100         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
101 };
102
103 gboolean timeout_func(gpointer data)
104 {
105         gint *running = (gint *) data;
106
107         if (*running & CHILD_RUNNING)
108                 return TRUE;
109
110         *running &= ~TIMEOUT_RUNNING;
111         return FALSE;
112 }
113
114 static gboolean msg_is_spam(FILE *fp)
115 {
116         struct transport trans;
117         struct message m;
118         gboolean is_spam = FALSE;
119
120         transport_init(&trans);
121         switch (config.transport) {
122         case SPAMASSASSIN_TRANSPORT_LOCALHOST:
123                 trans.type = TRANSPORT_LOCALHOST;
124                 trans.port = config.port;
125                 break;
126         case SPAMASSASSIN_TRANSPORT_TCP:
127                 trans.type = TRANSPORT_TCP;
128                 trans.hostname = config.hostname;
129                 trans.port = config.port;
130                 break;
131         case SPAMASSASSIN_TRANSPORT_UNIX:
132                 trans.type = TRANSPORT_UNIX;
133                 trans.socketpath = config.socket;
134                 break;
135         default:
136                 return FALSE;
137         }
138
139         if (transport_setup(&trans, flags) != EX_OK) {
140                 debug_print("failed to setup transport\n");
141                 return FALSE;
142         }
143
144         m.type = MESSAGE_NONE;
145         m.max_len = config.max_size * 1024;
146         m.timeout = config.timeout;
147
148         if (message_read(fileno(fp), flags, &m) != EX_OK) {
149                 debug_print("failed to read message\n");
150                 message_cleanup(&m);
151                 return FALSE;
152         }
153
154         if (message_filter(&trans, username, flags, &m) != EX_OK) {
155                 debug_print("filtering the message failed\n");
156                 message_cleanup(&m);
157                 return FALSE;
158         }
159
160         if (m.is_spam == EX_ISSPAM)
161                 is_spam = TRUE;
162
163         message_cleanup(&m);
164
165         return is_spam;
166 }
167
168 static gboolean mail_filtering_hook(gpointer source, gpointer data)
169 {
170         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
171         MsgInfo *msginfo = mail_filtering_data->msginfo;
172         gboolean is_spam = FALSE;
173         FILE *fp = NULL;
174         int pid = 0;
175         int status;
176
177         if (config.transport == SPAMASSASSIN_DISABLED)
178                 return FALSE;
179
180         debug_print("Filtering message %d\n", msginfo->msgnum);
181         statusbar_print_all(_("SpamAssassin: filtering message..."));
182
183         if ((fp = procmsg_open_message(msginfo)) == NULL) {
184                 debug_print("failed to open message file\n");
185                 return FALSE;
186         }
187
188         pid = fork();
189         if (pid == 0) {
190                 _exit(msg_is_spam(fp) ? 1 : 0);
191         } else {
192                 gint running = 0;
193
194                 running |= CHILD_RUNNING;
195
196                 g_timeout_add(1000, timeout_func, &running);
197                 running |= TIMEOUT_RUNNING;
198
199                 while(running & CHILD_RUNNING) {
200                         waitpid(pid, &status, WNOHANG);
201                         if (WIFEXITED(status)) {
202                                 running &= ~CHILD_RUNNING;
203                         }
204             
205                         g_main_iteration(TRUE);
206                 }
207
208                 while (running & TIMEOUT_RUNNING)
209                         g_main_iteration(TRUE);
210         }
211         is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
212
213         fclose(fp);
214
215         if (is_spam) {
216                 debug_print("message is spam\n");
217                             
218                 if (config.receive_spam) {
219                         FolderItem *save_folder;
220
221                         if ((!config.save_folder) ||
222                             (config.save_folder[0] == '\0') ||
223                             ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
224                                 save_folder = folder_get_default_trash();
225
226                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
227                         folder_item_move_msg(save_folder, msginfo);
228                 } else {
229                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
230                 }
231
232                 return TRUE;
233         }
234         
235         return FALSE;
236 }
237
238 SpamAssassinConfig *spamassassin_get_config(void)
239 {
240         return &config;
241 }
242
243 void spamassassin_save_config(void)
244 {
245         PrefFile *pfile;
246         gchar *rcpath;
247
248         debug_print("Saving SpamAssassin Page\n");
249
250         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
251         pfile = prefs_write_open(rcpath);
252         g_free(rcpath);
253         if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
254                 return;
255
256         if (prefs_write_param(param, pfile->fp) < 0) {
257                 g_warning("failed to write SpamAssassin configuration to file\n");
258                 prefs_file_close_revert(pfile);
259                 return;
260         }
261         fprintf(pfile->fp, "\n");
262
263         prefs_file_close(pfile);
264 }
265
266 gint plugin_init(gchar **error)
267 {
268         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
269                 *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
270                 return -1;
271         }
272
273         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
274                 *error = g_strdup("Your sylpheed version is too old");
275                 return -1;
276         }
277
278         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
279         if (hook_id == -1) {
280                 *error = g_strdup("Failed to register mail filtering hook");
281                 return -1;
282         }
283
284         username = g_get_user_name();
285         if (username == NULL) {
286                 *error = g_strdup("Failed to get username");
287                 return -1;
288         }
289
290         prefs_set_default(param);
291         prefs_read_config(param, "SpamAssassin", COMMON_RC);
292
293         debug_print("Spamassassin plugin loaded\n");
294
295         return 0;
296         
297 }
298
299 void plugin_done(void)
300 {
301         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
302         g_free(config.hostname);
303         g_free(config.save_folder);
304
305         debug_print("Spamassassin plugin unloaded\n");
306 }
307
308 const gchar *plugin_name(void)
309 {
310         return _("SpamAssassin");
311 }
312
313 const gchar *plugin_desc(void)
314 {
315         return _("This plugin checks all messages that are received from an "
316                  "IMAP, LOCAL or POP account for spam using a SpamAssassin "
317                  "server. You will need a SpamAssassin Server (spamd) running "
318                  "somewhere.\n"
319                  "\n"
320                  "When a message is identified as spam it can be deleted or "
321                  "saved into a special folder.\n"
322                  "\n"
323                  "This plugin only contains the actual function for filtering "
324                  "and deleting or moving the message. You probably want to load "
325                  "a User Interface plugin too, otherwise you will have to "
326                  "manually write the plugin configuration.\n");
327 }
328
329 const gchar *plugin_type(void)
330 {
331         return "Common";
332 }