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