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