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