2006-02-27 [colin] 2.0.0cvs85
[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 #include "log.h"
49 #include "prefs_common.h"
50
51 #ifdef HAVE_SYSEXITS_H
52 #include <sysexits.h>
53 #endif
54 #ifdef HAVE_ERRNO_H
55 #include <errno.h>
56 #endif
57 #ifdef HAVE_SYS_ERRNO_H
58 #include <sys/errno.h>
59 #endif
60 #ifdef HAVE_TIME_H
61 #include <time.h>
62 #endif
63 #ifdef HAVE_SYS_TIME_H
64 #include <sys/time.h>
65 #endif
66 #ifdef HAVE_SIGNAL_H
67 #include <signal.h>
68 #endif
69 #ifdef HAVE_PWD_H
70 #include <pwd.h>
71 #endif
72
73 enum {
74     CHILD_RUNNING = 1 << 0,
75     TIMEOUT_RUNNING = 1 << 1,
76 };
77
78 static guint hook_id;
79 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
80 static gchar *username = NULL;
81 static MessageCallback message_callback;
82
83 static SpamAssassinConfig config;
84
85 static PrefParam param[] = {
86         {"transport", "0", &config.transport, P_INT,
87          NULL, NULL, NULL},
88         {"hostname", "localhost", &config.hostname, P_STRING,
89          NULL, NULL, NULL},
90         {"port", "783", &config.port, P_INT,
91          NULL, NULL, NULL},
92         {"socket", "", &config.socket, P_STRING,
93          NULL, NULL, NULL},
94         {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
95          NULL, NULL, NULL},
96         {"save_folder", NULL, &config.save_folder, P_STRING,
97          NULL, NULL, NULL},
98         {"max_size", "250", &config.max_size, P_INT,
99          NULL, NULL, NULL},
100         {"timeout", "30", &config.timeout, P_INT,
101          NULL, NULL, NULL},
102
103         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
104 };
105
106 gboolean timeout_func(gpointer data)
107 {
108         gint *running = (gint *) data;
109
110         if (*running & CHILD_RUNNING)
111                 return TRUE;
112
113         *running &= ~TIMEOUT_RUNNING;
114         return FALSE;
115 }
116
117 static gboolean msg_is_spam(FILE *fp)
118 {
119         struct transport trans;
120         struct message m;
121         gboolean is_spam = FALSE;
122
123         transport_init(&trans);
124         switch (config.transport) {
125         case SPAMASSASSIN_TRANSPORT_LOCALHOST:
126                 trans.type = TRANSPORT_LOCALHOST;
127                 trans.port = config.port;
128                 break;
129         case SPAMASSASSIN_TRANSPORT_TCP:
130                 trans.type = TRANSPORT_TCP;
131                 trans.hostname = config.hostname;
132                 trans.port = config.port;
133                 break;
134         case SPAMASSASSIN_TRANSPORT_UNIX:
135                 trans.type = TRANSPORT_UNIX;
136                 trans.socketpath = config.socket;
137                 break;
138         default:
139                 return FALSE;
140         }
141
142         if (transport_setup(&trans, flags) != EX_OK) {
143                 log_error("Spamassassin plugin couldn't connect to spamd.\n");
144                 debug_print("failed to setup transport\n");
145                 return FALSE;
146         }
147
148         m.type = MESSAGE_NONE;
149         m.max_len = config.max_size * 1024;
150         m.timeout = config.timeout;
151
152         if (message_read(fileno(fp), flags, &m) != EX_OK) {
153                 debug_print("failed to read message\n");
154                 message_cleanup(&m);
155                 return FALSE;
156         }
157
158         if (message_filter(&trans, username, flags, &m) != EX_OK) {
159                 debug_print("filtering the message failed\n");
160                 message_cleanup(&m);
161                 return FALSE;
162         }
163
164         if (m.is_spam == EX_ISSPAM)
165                 is_spam = TRUE;
166
167         message_cleanup(&m);
168
169         return is_spam;
170 }
171
172 static gboolean mail_filtering_hook(gpointer source, gpointer data)
173 {
174         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
175         MsgInfo *msginfo = mail_filtering_data->msginfo;
176         gboolean is_spam = FALSE;
177         FILE *fp = NULL;
178         int pid = 0;
179         int status;
180
181         if (config.transport == SPAMASSASSIN_DISABLED) {
182                 log_error("Spamassassin plugin is disabled by its preferences.\n");
183                 return FALSE;
184         }
185         debug_print("Filtering message %d\n", msginfo->msgnum);
186         if (message_callback != NULL)
187                 message_callback(_("SpamAssassin: filtering message..."));
188
189         if ((fp = procmsg_open_message(msginfo)) == NULL) {
190                 debug_print("failed to open message file\n");
191                 return FALSE;
192         }
193
194         pid = fork();
195         if (pid == 0) {
196                 _exit(msg_is_spam(fp) ? 1 : 0);
197         } else {
198                 gint running = 0;
199
200                 running |= CHILD_RUNNING;
201
202                 g_timeout_add(50, timeout_func, &running);
203                 running |= TIMEOUT_RUNNING;
204
205                 while(running & CHILD_RUNNING) {
206                         int ret;
207
208                         ret = waitpid(pid, &status, WNOHANG);
209                         if (ret == pid) {
210                                 if (WIFEXITED(status)) {
211                                         running &= ~CHILD_RUNNING;
212                                         is_spam = WEXITSTATUS(status) == 1 ? TRUE : FALSE;
213                                 }
214                         } if (ret < 0) {
215                                 running &= ~CHILD_RUNNING;
216                         } /* ret == 0 continue */
217             
218                         g_main_iteration(TRUE);
219                 }
220
221                 while (running & TIMEOUT_RUNNING)
222                         g_main_iteration(TRUE);
223         }
224
225         fclose(fp);
226
227         if (is_spam) {
228                 debug_print("message is spam\n");
229                 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
230                 if (config.receive_spam) {
231                         FolderItem *save_folder;
232
233                         if ((!config.save_folder) ||
234                             (config.save_folder[0] == '\0') ||
235                             ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
236                                 save_folder = folder_get_default_trash();
237
238                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
239                         procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
240                         folder_item_move_msg(save_folder, msginfo);
241                 } else {
242                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
243                 }
244
245                 return TRUE;
246         } else {
247                 debug_print("message is ham\n");
248                 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
249         }
250         return FALSE;
251 }
252
253 SpamAssassinConfig *spamassassin_get_config(void)
254 {
255         return &config;
256 }
257
258 void spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
259 {
260         gchar *cmd = NULL;
261         gchar *file = NULL;
262         gboolean async = FALSE;
263
264         if (msginfo == NULL && msglist == NULL)
265                 return;
266
267         if (msginfo) {
268                 file = procmsg_get_message_file(msginfo);
269                 if (file == NULL)
270                         return;
271                 cmd = g_strdup_printf("sa-learn %s %s %s", 
272                         prefs_common.work_offline?"-L":"",
273                         spam?"--spam":"--ham", file);
274         }
275         if (msglist) {
276                 GSList *cur;
277                 MsgInfo *info;
278                 cmd = g_strdup_printf("sa-learn %s %s", 
279                                 prefs_common.work_offline?"-L":"",
280                                 spam?"--spam":"--ham");
281                 for (cur = msglist; cur; cur = cur->next) {
282                         info = (MsgInfo *)cur->data;
283                         gchar *tmpcmd = NULL;
284                         gchar *tmpfile = get_tmp_file();
285                         
286                         if (tmpfile &&
287                             copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {                    
288                                 tmpcmd = g_strconcat
289                                         (cmd, " ", tmpfile, NULL);
290                                 g_free(cmd);
291                                 cmd = tmpcmd;
292                         }
293                         if (tmpfile)
294                                 g_free(tmpfile);
295                 }
296                 async = TRUE;
297         }
298         if (cmd == NULL)
299                 return;
300         debug_print("%s\n",cmd);
301         /* only run async if we have a list, or we could end up
302          * forking lots of perl processes and bury the machine */
303         execute_command_line(cmd, async);
304         g_free(cmd);
305         
306 }
307
308 void spamassassin_save_config(void)
309 {
310         PrefFile *pfile;
311         gchar *rcpath;
312
313         debug_print("Saving SpamAssassin Page\n");
314
315         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
316         pfile = prefs_write_open(rcpath);
317         g_free(rcpath);
318         if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
319                 return;
320
321         if (prefs_write_param(param, pfile->fp) < 0) {
322                 g_warning("failed to write SpamAssassin configuration to file\n");
323                 prefs_file_close_revert(pfile);
324                 return;
325         }
326         fprintf(pfile->fp, "\n");
327
328         prefs_file_close(pfile);
329 }
330
331 void spamassassin_set_message_callback(MessageCallback callback)
332 {
333         message_callback = callback;
334 }
335
336 gint plugin_init(gchar **error)
337 {
338         gchar *rcpath;
339
340         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
341                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the SpamAssassin plugin was built with");
342                 return -1;
343         }
344
345         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
346                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the SpamAssassin plugin");
347                 return -1;
348         }
349
350         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
351         if (hook_id == -1) {
352                 *error = g_strdup("Failed to register mail filtering hook");
353                 return -1;
354         }
355
356         username = (gchar*)g_get_user_name();
357         if (username == NULL) {
358                 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
359                 *error = g_strdup("Failed to get username");
360                 return -1;
361         }
362
363         prefs_set_default(param);
364         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
365         prefs_read_config(param, "SpamAssassin", rcpath, NULL);
366         g_free(rcpath);
367         spamassassin_gtk_init();
368                 
369         debug_print("Spamassassin plugin loaded\n");
370
371         if (config.transport == SPAMASSASSIN_DISABLED) {
372                 log_error("Spamassassin plugin is loaded but disabled by its preferences.\n");
373         }
374         
375         if (config.transport != SPAMASSASSIN_DISABLED &&
376             config.transport != SPAMASSASSIN_TRANSPORT_TCP) {
377                 procmsg_register_spam_learner(spamassassin_learn);
378                 procmsg_spam_set_folder(config.save_folder);
379         } else if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
380                 debug_print("disabling learner as it only works locally\n");
381
382         return 0;
383         
384 }
385
386 void plugin_done(void)
387 {
388         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
389         g_free(config.hostname);
390         g_free(config.save_folder);
391         spamassassin_gtk_done();
392         procmsg_unregister_spam_learner(spamassassin_learn);
393         procmsg_spam_set_folder(NULL);
394         debug_print("Spamassassin plugin unloaded\n");
395 }
396
397 const gchar *plugin_name(void)
398 {
399         return _("SpamAssassin");
400 }
401
402 const gchar *plugin_desc(void)
403 {
404         return _("This plugin checks all messages that are received from an "
405                  "IMAP, LOCAL or POP account for spam using a SpamAssassin "
406                  "server. You will need a SpamAssassin Server (spamd) running "
407                  "somewhere.\n"
408                  "\n"
409                  "When a message is identified as spam it can be deleted or "
410                  "saved into a special folder.\n"
411                  "\n");
412 }
413
414 const gchar *plugin_type(void)
415 {
416         return "GTK2";
417 }
418
419 const gchar *plugin_licence(void)
420 {
421         return "GPL";
422 }
423
424 const gchar *plugin_version(void)
425 {
426         return VERSION;
427 }
428