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