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