2006-02-24 [colin] 2.0.0cvs82
[claws.git] / src / plugins / spamassassin / spamassassin.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 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                 log_error("Spamassassin plugin couldn't connect to spamd.\n");
142                 debug_print("failed to setup transport\n");
143                 return FALSE;
144         }
145
146         m.type = MESSAGE_NONE;
147         m.max_len = config.max_size * 1024;
148         m.timeout = config.timeout;
149
150         if (message_read(fileno(fp), flags, &m) != EX_OK) {
151                 debug_print("failed to read message\n");
152                 message_cleanup(&m);
153                 return FALSE;
154         }
155
156         if (message_filter(&trans, username, flags, &m) != EX_OK) {
157                 debug_print("filtering the message failed\n");
158                 message_cleanup(&m);
159                 return FALSE;
160         }
161
162         if (m.is_spam == EX_ISSPAM)
163                 is_spam = TRUE;
164
165         message_cleanup(&m);
166
167         return is_spam;
168 }
169
170 static gboolean mail_filtering_hook(gpointer source, gpointer data)
171 {
172         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
173         MsgInfo *msginfo = mail_filtering_data->msginfo;
174         gboolean is_spam = FALSE;
175         FILE *fp = NULL;
176         int pid = 0;
177         int status;
178
179         if (config.transport == SPAMASSASSIN_DISABLED) {
180                 log_error("Spamassassin plugin is disabled by its preferences.\n");
181                 return FALSE;
182         }
183         debug_print("Filtering message %d\n", msginfo->msgnum);
184         if (message_callback != NULL)
185                 message_callback(_("SpamAssassin: filtering message..."));
186
187         if ((fp = procmsg_open_message(msginfo)) == NULL) {
188                 debug_print("failed to open message file\n");
189                 return FALSE;
190         }
191
192         pid = fork();
193         if (pid == 0) {
194                 _exit(msg_is_spam(fp) ? 1 : 0);
195         } else {
196                 gint running = 0;
197
198                 running |= CHILD_RUNNING;
199
200                 g_timeout_add(50, timeout_func, &running);
201                 running |= TIMEOUT_RUNNING;
202
203                 while(running & CHILD_RUNNING) {
204                         int ret;
205
206                         ret = waitpid(pid, &status, WNOHANG);
207                         if (ret == pid) {
208                                 if (WIFEXITED(status)) {
209                                         running &= ~CHILD_RUNNING;
210                                         is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
211                                 }
212                         } if (ret < 0) {
213                                 running &= ~CHILD_RUNNING;
214                         } /* ret == 0 continue */
215             
216                         g_main_iteration(TRUE);
217                 }
218
219                 while (running & TIMEOUT_RUNNING)
220                         g_main_iteration(TRUE);
221         }
222
223         fclose(fp);
224
225         if (is_spam) {
226                 debug_print("message is spam\n");
227                 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
228                 if (config.receive_spam) {
229                         FolderItem *save_folder;
230
231                         if ((!config.save_folder) ||
232                             (config.save_folder[0] == '\0') ||
233                             ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
234                                 save_folder = folder_get_default_trash();
235
236                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
237                         procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
238                         folder_item_move_msg(save_folder, msginfo);
239                 } else {
240                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
241                 }
242
243                 return TRUE;
244         } else {
245                 debug_print("message is ham\n");
246                 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
247         }
248         return FALSE;
249 }
250
251 SpamAssassinConfig *spamassassin_get_config(void)
252 {
253         return &config;
254 }
255
256 void spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
257 {
258         gchar *cmd = NULL;
259         gchar *file = NULL;
260         gboolean async = FALSE;
261
262         if (msginfo == NULL && msglist == NULL)
263                 return;
264
265         if (msginfo) {
266                 file = procmsg_get_message_file(msginfo);
267                 if (file == NULL)
268                         return;
269                 cmd = g_strdup_printf("sa-learn %s %s", 
270                         spam?"--spam":"--ham", file);
271         }
272         if (msglist) {
273                 GSList *cur;
274                 MsgInfo *info;
275                 cmd = g_strdup_printf("sa-learn %s", spam?"--spam":"--ham");
276                 for (cur = msglist; cur; cur = cur->next) {
277                         info = (MsgInfo *)cur->data;
278                         gchar *tmpcmd = NULL;
279                         gchar *tmpfile = get_tmp_file();
280                         
281                         if (tmpfile &&
282                             copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {                    
283                                 tmpcmd = g_strconcat
284                                         (cmd, " ", tmpfile, NULL);
285                                 g_free(cmd);
286                                 cmd = tmpcmd;
287                         }
288                         if (tmpfile)
289                                 g_free(tmpfile);
290                 }
291                 async = TRUE;
292         }
293         if (cmd == NULL)
294                 return;
295         debug_print("%s\n",cmd);
296         /* only run async if we have a list, or we could end up
297          * forking lots of perl processes and bury the machine */
298         execute_command_line(cmd, async);
299         g_free(cmd);
300         
301 }
302
303 void spamassassin_save_config(void)
304 {
305         PrefFile *pfile;
306         gchar *rcpath;
307
308         debug_print("Saving SpamAssassin Page\n");
309
310         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
311         pfile = prefs_write_open(rcpath);
312         g_free(rcpath);
313         if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
314                 return;
315
316         if (prefs_write_param(param, pfile->fp) < 0) {
317                 g_warning("failed to write SpamAssassin configuration to file\n");
318                 prefs_file_close_revert(pfile);
319                 return;
320         }
321         fprintf(pfile->fp, "\n");
322
323         prefs_file_close(pfile);
324 }
325
326 void spamassassin_set_message_callback(MessageCallback callback)
327 {
328         message_callback = callback;
329 }
330
331 gint plugin_init(gchar **error)
332 {
333         gchar *rcpath;
334
335         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
336                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the SpamAssassin plugin was built with");
337                 return -1;
338         }
339
340         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
341                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the SpamAssassin plugin");
342                 return -1;
343         }
344
345         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
346         if (hook_id == -1) {
347                 *error = g_strdup("Failed to register mail filtering hook");
348                 return -1;
349         }
350
351         username = (gchar*)g_get_user_name();
352         if (username == NULL) {
353                 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
354                 *error = g_strdup("Failed to get username");
355                 return -1;
356         }
357
358         prefs_set_default(param);
359         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
360         prefs_read_config(param, "SpamAssassin", rcpath, NULL);
361         g_free(rcpath);
362         spamassassin_gtk_init();
363         
364         procmsg_register_spam_learner(spamassassin_learn);
365         procmsg_spam_set_folder(config.save_folder);
366         
367         debug_print("Spamassassin plugin loaded\n");
368
369         if (config.transport == SPAMASSASSIN_DISABLED) {
370                 log_error("Spamassassin plugin is loaded but disabled by its preferences.\n");
371         }
372
373         return 0;
374         
375 }
376
377 void plugin_done(void)
378 {
379         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
380         g_free(config.hostname);
381         g_free(config.save_folder);
382         spamassassin_gtk_done();
383         procmsg_unregister_spam_learner(spamassassin_learn);
384         procmsg_spam_set_folder(NULL);
385         debug_print("Spamassassin plugin unloaded\n");
386 }
387
388 const gchar *plugin_name(void)
389 {
390         return _("SpamAssassin");
391 }
392
393 const gchar *plugin_desc(void)
394 {
395         return _("This plugin checks all messages that are received from an "
396                  "IMAP, LOCAL or POP account for spam using a SpamAssassin "
397                  "server. You will need a SpamAssassin Server (spamd) running "
398                  "somewhere.\n"
399                  "\n"
400                  "When a message is identified as spam it can be deleted or "
401                  "saved into a special folder.\n"
402                  "\n");
403 }
404
405 const gchar *plugin_type(void)
406 {
407         return "GTK2";
408 }
409
410 const gchar *plugin_licence(void)
411 {
412         return "GPL";
413 }
414
415 const gchar *plugin_version(void)
416 {
417         return VERSION;
418 }
419