2006-09-25 [colin] 2.5.0cvs3
[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 "inc.h"
49 #include "log.h"
50 #include "prefs_common.h"
51 #include "alertpanel.h"
52
53 #ifdef HAVE_SYSEXITS_H
54 #include <sysexits.h>
55 #endif
56 #ifdef HAVE_ERRNO_H
57 #include <errno.h>
58 #endif
59 #ifdef HAVE_SYS_ERRNO_H
60 #include <sys/errno.h>
61 #endif
62 #ifdef HAVE_TIME_H
63 #include <time.h>
64 #endif
65 #ifdef HAVE_SYS_TIME_H
66 #include <sys/time.h>
67 #endif
68 #ifdef HAVE_SIGNAL_H
69 #include <signal.h>
70 #endif
71 #ifdef HAVE_PWD_H
72 #include <pwd.h>
73 #endif
74
75 enum {
76     CHILD_RUNNING = 1 << 0,
77     TIMEOUT_RUNNING = 1 << 1,
78 };
79
80 static guint hook_id = -1;
81 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
82 static MessageCallback message_callback;
83
84 static SpamAssassinConfig config;
85
86 static PrefParam param[] = {
87         {"enable", "FALSE", &config.enable, P_BOOL,
88         NULL, NULL, NULL},
89         {"transport", "0", &config.transport, P_INT,
90          NULL, NULL, NULL},
91         {"hostname", "localhost", &config.hostname, P_STRING,
92          NULL, NULL, NULL},
93         {"port", "783", &config.port, P_INT,
94          NULL, NULL, NULL},
95         {"socket", "", &config.socket, P_STRING,
96          NULL, NULL, NULL},
97         {"process_emails", "TRUE", &config.process_emails, P_BOOL,
98          NULL, NULL, NULL},
99         {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
100          NULL, NULL, NULL},
101         {"save_folder", NULL, &config.save_folder, P_STRING,
102          NULL, NULL, NULL},
103         {"max_size", "250", &config.max_size, P_INT,
104          NULL, NULL, NULL},
105         {"timeout", "30", &config.timeout, P_INT,
106          NULL, NULL, NULL},
107         {"username", "", &config.username, P_STRING,
108          NULL, NULL, NULL},
109
110         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
111 };
112
113 gboolean timeout_func(gpointer data)
114 {
115         gint *running = (gint *) data;
116
117         if (*running & CHILD_RUNNING)
118                 return TRUE;
119
120         *running &= ~TIMEOUT_RUNNING;
121         return FALSE;
122 }
123
124 typedef enum {
125         MSG_IS_HAM = 0,
126         MSG_IS_SPAM = 1,
127         MSG_FILTERING_ERROR = 2
128 } MsgStatus;
129
130 static MsgStatus msg_is_spam(FILE *fp)
131 {
132         struct transport trans;
133         struct message m;
134         gboolean is_spam = FALSE;
135
136         if (!config.enable)
137                 return MSG_IS_HAM;
138
139         transport_init(&trans);
140         switch (config.transport) {
141         case SPAMASSASSIN_TRANSPORT_LOCALHOST:
142                 trans.type = TRANSPORT_LOCALHOST;
143                 trans.port = config.port;
144                 break;
145         case SPAMASSASSIN_TRANSPORT_TCP:
146                 trans.type = TRANSPORT_TCP;
147                 trans.hostname = config.hostname;
148                 trans.port = config.port;
149                 break;
150         case SPAMASSASSIN_TRANSPORT_UNIX:
151                 trans.type = TRANSPORT_UNIX;
152                 trans.socketpath = config.socket;
153                 break;
154         default:
155                 return MSG_IS_HAM;
156         }
157
158         if (transport_setup(&trans, flags) != EX_OK) {
159                 log_error(_("SpamAssassin plugin couldn't connect to spamd.\n"));
160                 debug_print("failed to setup transport\n");
161                 return MSG_FILTERING_ERROR;
162         }
163
164         m.type = MESSAGE_NONE;
165         m.max_len = config.max_size * 1024;
166         m.timeout = config.timeout;
167
168         if (message_read(fileno(fp), flags, &m) != EX_OK) {
169                 debug_print("failed to read message\n");
170                 message_cleanup(&m);
171                 return MSG_FILTERING_ERROR;
172         }
173
174         if (message_filter(&trans, config.username, flags, &m) != EX_OK) {
175                 log_error(_("SpamAssassin plugin filtering failed.\n"));
176                 debug_print("filtering the message failed\n");
177                 message_cleanup(&m);
178                 return MSG_FILTERING_ERROR;
179         }
180
181         if (m.is_spam == EX_ISSPAM)
182                 is_spam = TRUE;
183
184         message_cleanup(&m);
185
186         return is_spam ? MSG_IS_SPAM:MSG_IS_HAM;
187 }
188
189 static gboolean mail_filtering_hook(gpointer source, gpointer data)
190 {
191         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
192         MsgInfo *msginfo = mail_filtering_data->msginfo;
193         gboolean is_spam = FALSE, error = FALSE;
194         static gboolean warned_error = FALSE;
195         FILE *fp = NULL;
196         int pid = 0;
197         int status;
198
199         /* SPAMASSASSIN_DISABLED : keep test for compatibility purpose */
200         if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
201                 log_warning(_("SpamAssassin plugin is disabled by its preferences.\n"));
202                 return FALSE;
203         }
204         debug_print("Filtering message %d\n", msginfo->msgnum);
205         if (message_callback != NULL)
206                 message_callback(_("SpamAssassin: filtering message..."));
207
208         if ((fp = procmsg_open_message(msginfo)) == NULL) {
209                 debug_print("failed to open message file\n");
210                 return FALSE;
211         }
212
213         pid = fork();
214         if (pid == 0) {
215                 _exit(msg_is_spam(fp));
216         } else {
217                 gint running = 0;
218
219                 running |= CHILD_RUNNING;
220
221                 g_timeout_add(50, timeout_func, &running);
222                 running |= TIMEOUT_RUNNING;
223
224                 while(running & CHILD_RUNNING) {
225                         int ret;
226
227                         ret = waitpid(pid, &status, WNOHANG);
228                         if (ret == pid) {
229                                 if (WIFEXITED(status)) {
230                                         MsgStatus result = MSG_IS_HAM;
231                                         running &= ~CHILD_RUNNING;
232                                         result = WEXITSTATUS(status);
233                                         is_spam = (result == MSG_IS_SPAM) ? TRUE : FALSE;
234                                         error = (result == MSG_FILTERING_ERROR);
235                                 }
236                         } if (ret < 0) {
237                                 running &= ~CHILD_RUNNING;
238                         } /* ret == 0 continue */
239             
240                         g_main_iteration(TRUE);
241                 }
242
243                 while (running & TIMEOUT_RUNNING)
244                         g_main_iteration(TRUE);
245         }
246
247         fclose(fp);
248
249         if (is_spam) {
250                 debug_print("message is spam\n");
251                 procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
252                 if (config.receive_spam) {
253                         FolderItem *save_folder;
254
255                         if ((!config.save_folder) ||
256                             (config.save_folder[0] == '\0') ||
257                             ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
258                                 save_folder = folder_get_default_trash();
259
260                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
261                         procmsg_msginfo_set_flags(msginfo, MSG_SPAM, 0);
262                         msginfo->is_move = TRUE;
263                         msginfo->to_filter_folder = save_folder;
264                 } else {
265                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
266                 }
267
268                 return TRUE;
269         } else {
270                 debug_print("message is ham\n");
271                 procmsg_msginfo_unset_flags(msginfo, MSG_SPAM, 0);
272         }
273         
274         if (error) {
275                 gchar *msg = _("The SpamAssassin plugin couldn't filter "
276                                            "a message. The probable cause of the error "
277                                            "is an unreachable spamd daemon. Please make "
278                                            "sure spamd is running and accessible.");
279                 if (!prefs_common.no_recv_err_panel) {
280                         if (!warned_error) {
281                                 alertpanel_error(msg);
282                         }
283                         warned_error = TRUE;
284                 } else {
285                         gchar *tmp = g_strdup_printf("%s\n", msg);
286                         log_error(tmp);
287                         g_free(tmp);
288                 }
289         }
290         
291         return FALSE;
292 }
293
294 SpamAssassinConfig *spamassassin_get_config(void)
295 {
296         return &config;
297 }
298
299 gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
300 {
301         gchar *contents;
302         gchar *fname = get_tmp_file();
303
304         if (fname != NULL) {
305                 contents = g_strdup_printf(
306                                                 "spamc -d %s -p %u -u %s -t %u -s %u -L %s<\"$*\";exit $?",
307                                                 config.hostname, config.port, 
308                                                 config.username, config.timeout,
309                                                 config.max_size * 1024, spam?"spam":"ham");
310                 if (str_write_to_file(contents, fname) < 0) {
311                         g_free(fname);
312                         fname = NULL;
313                 }
314                 g_free(contents);
315         }
316         /* returned pointer must be free'ed by caller */
317         return fname;
318 }
319
320 int spamassassin_learn(MsgInfo *msginfo, GSList *msglist, gboolean spam)
321 {
322         gchar *cmd = NULL;
323         gchar *file = NULL;
324         const gchar *shell = g_getenv("SHELL");
325         gchar *spamc_wrapper = NULL;
326
327         if (msginfo == NULL && msglist == NULL) {
328                 return -1;
329         }
330
331         if (config.transport == SPAMASSASSIN_TRANSPORT_TCP
332         &&  prefs_common.work_offline
333         &&  !inc_offline_should_override(
334                 _("Sylpheed-Claws needs network access in order "
335                   "to feed this mail(s) to the remote learner."))) {
336                 return -1;
337         }
338
339         if (msginfo) {
340                 file = procmsg_get_message_file(msginfo);
341                 if (file == NULL) {
342                         return -1;
343                 }
344                 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
345                         spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
346                         if (spamc_wrapper != NULL) {
347                                 cmd = g_strconcat(shell?shell:"sh", " ",
348                                                                 spamc_wrapper, " ", file, NULL);
349                         }
350                 } else {
351                         cmd = g_strdup_printf("sa-learn -u %s %s %s %s",
352                                                         config.username,
353                                                         prefs_common.work_offline?"-L":"",
354                                                         spam?"--spam":"--ham", file);
355                 }
356         }
357         if (msglist) {
358                 GSList *cur = msglist;
359                 MsgInfo *info;
360
361                 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP) {
362                         /* execute n-times the spamc command */
363                         for (; cur; cur = cur->next) {
364                                 info = (MsgInfo *)cur->data;
365                                 gchar *tmpcmd = NULL;
366                                 gchar *tmpfile = get_tmp_file();
367
368                                 if (spamc_wrapper == NULL) {
369                                         spamc_wrapper = spamassassin_create_tmp_spamc_wrapper(spam);
370                                 }
371
372                                 if (spamc_wrapper && tmpfile &&
373                                 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {
374                                         tmpcmd = g_strconcat(shell?shell:"sh", " ", spamc_wrapper, " ",
375                                                                                 tmpfile, NULL);
376                                         debug_print("%s\n", tmpcmd);
377                                         execute_command_line(tmpcmd, FALSE);
378                                         g_free(tmpcmd);
379                                 }
380                                 g_free(tmpfile);
381                         }
382                         g_free(spamc_wrapper);
383                         return 0;
384                 } else {
385                         cmd = g_strdup_printf("sa-learn -u %s %s %s",
386                                         config.username,
387                                         prefs_common.work_offline?"-L":"",
388                                         spam?"--spam":"--ham");
389
390                         /* concatenate all message tmpfiles to the sa-learn command-line */
391                         for (; cur; cur = cur->next) {
392                                 info = (MsgInfo *)cur->data;
393                                 gchar *tmpcmd = NULL;
394                                 gchar *tmpfile = get_tmp_file();
395
396                                 if (tmpfile &&
397                                 copy_file(procmsg_get_message_file(info), tmpfile, TRUE) == 0) {                        
398                                         tmpcmd = g_strconcat(cmd, " ", tmpfile, NULL);
399                                         g_free(cmd);
400                                         cmd = tmpcmd;
401                                 }
402                                 g_free(tmpfile);
403                         }
404                 }
405         }
406         if (cmd == NULL) {
407                 return -1;
408         }
409         debug_print("%s\n", cmd);
410         /* only run sync calls to sa-learn/spamc to prevent system lockdown */
411         execute_command_line(cmd, FALSE);
412         g_free(cmd);
413         g_free(spamc_wrapper);
414
415         return 0;
416 }
417
418 void spamassassin_save_config(void)
419 {
420         PrefFile *pfile;
421         gchar *rcpath;
422
423         debug_print("Saving SpamAssassin Page\n");
424
425         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
426         pfile = prefs_write_open(rcpath);
427         g_free(rcpath);
428         if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
429                 return;
430
431         if (prefs_write_param(param, pfile->fp) < 0) {
432                 g_warning("Failed to write SpamAssassin configuration to file\n");
433                 prefs_file_close_revert(pfile);
434                 return;
435         }
436         fprintf(pfile->fp, "\n");
437
438         prefs_file_close(pfile);
439 }
440
441 gboolean spamassassin_check_username(void)
442 {
443         if (config.username == NULL || config.username[0] == '\0') {
444                 config.username = (gchar*)g_get_user_name();
445                 if (config.username == NULL) {
446                         if (hook_id != -1) {
447                                 spamassassin_unregister_hook();
448                         }
449                         procmsg_unregister_spam_learner(spamassassin_learn);
450                         procmsg_spam_set_folder(NULL);
451                         return FALSE;
452                 }
453         }
454         return TRUE;
455 }
456
457 void spamassassin_set_message_callback(MessageCallback callback)
458 {
459         message_callback = callback;
460 }
461
462 gint plugin_init(gchar **error)
463 {
464         gchar *rcpath;
465
466         hook_id = -1;
467
468         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
469                 *error = g_strdup(_("Your version of Sylpheed-Claws is newer than the version the SpamAssassin plugin was built with"));
470                 return -1;
471         }
472
473         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
474                 *error = g_strdup(_("Your version of Sylpheed-Claws is too old for the SpamAssassin plugin"));
475                 return -1;
476         }
477
478         prefs_set_default(param);
479         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
480         prefs_read_config(param, "SpamAssassin", rcpath, NULL);
481         g_free(rcpath);
482         if (!spamassassin_check_username()) {
483                 *error = g_strdup(_("Failed to get username"));
484                 return -1;
485         }
486         spamassassin_gtk_init();
487                 
488         debug_print("SpamAssassin plugin loaded\n");
489
490         if (config.process_emails) {
491                 spamassassin_register_hook();
492         }
493
494         if (!config.enable || config.transport == SPAMASSASSIN_DISABLED) {
495                 log_warning(_("SpamAssassin plugin is loaded but disabled by its preferences.\n"));
496         }
497         else {
498                 if (config.transport == SPAMASSASSIN_TRANSPORT_TCP)
499                         debug_print("Enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
500                 procmsg_register_spam_learner(spamassassin_learn);
501                 procmsg_spam_set_folder(config.save_folder);
502         }
503
504         return 0;
505         
506 }
507
508 void plugin_done(void)
509 {
510         if (hook_id != -1) {
511                 spamassassin_unregister_hook();
512         }
513         g_free(config.hostname);
514         g_free(config.save_folder);
515         spamassassin_gtk_done();
516         procmsg_unregister_spam_learner(spamassassin_learn);
517         procmsg_spam_set_folder(NULL);
518         debug_print("SpamAssassin plugin unloaded\n");
519 }
520
521 const gchar *plugin_name(void)
522 {
523         return _("SpamAssassin");
524 }
525
526 const gchar *plugin_desc(void)
527 {
528         return _("This plugin can check all messages that are received from an "
529                  "IMAP, LOCAL or POP account for spam using a SpamAssassin "
530                  "server. You will need a SpamAssassin Server (spamd) running "
531                  "somewhere.\n"
532                  "\n"
533                  "It can also be used for marking messages as Ham or Spam.\n"
534                  "\n"
535                  "When a message is identified as spam it can be deleted or "
536                  "saved in a specially designated folder.\n"
537                  "\n"
538                  "Options can be found in /Configuration/Preferences/Plugins/SpamAssassin");
539 }
540
541 const gchar *plugin_type(void)
542 {
543         return "GTK2";
544 }
545
546 const gchar *plugin_licence(void)
547 {
548         return "GPL";
549 }
550
551 const gchar *plugin_version(void)
552 {
553         return VERSION;
554 }
555
556 struct PluginFeature *plugin_provides(void)
557 {
558         static struct PluginFeature features[] = 
559                 { {PLUGIN_FILTERING, N_("Spam detection")},
560                   {PLUGIN_FILTERING, N_("Spam learning")},
561                   {PLUGIN_NOTHING, NULL}};
562         return features;
563 }
564
565 void spamassassin_register_hook(void)
566 {
567         if (hook_id == -1)
568                 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
569         if (hook_id == -1) {
570                 g_warning("Failed to register mail filtering hook");
571                 config.process_emails = FALSE;
572         }
573 }
574
575 void spamassassin_unregister_hook(void)
576 {
577         if (hook_id != -1) {
578                 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
579         }
580         hook_id = -1;
581 }