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