a782e4c3a910b404e11c88e47db0c8cafe9fbf59
[claws.git] / src / plugins / spamassassin / spamassassin.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27
28 #if HAVE_LOCALE_H
29 #  include <locale.h>
30 #endif
31
32 #include "plugin.h"
33 #include "common/utils.h"
34 #include "hooks.h"
35 #include "inc.h"
36 #include "procmsg.h"
37 #include "folder.h"
38 #include "prefs.h"
39 #include "prefs_gtk.h"
40
41 #include "libspamc.h"
42 #include "spamassassin.h"
43
44 #ifdef HAVE_SYSEXITS_H
45 #include <sysexits.h>
46 #endif
47 #ifdef HAVE_ERRNO_H
48 #include <errno.h>
49 #endif
50 #ifdef HAVE_SYS_ERRNO_H
51 #include <sys/errno.h>
52 #endif
53 #ifdef HAVE_TIME_H
54 #include <time.h>
55 #endif
56 #ifdef HAVE_SYS_TIME_H
57 #include <sys/time.h>
58 #endif
59 #ifdef HAVE_SIGNAL_H
60 #include <signal.h>
61 #endif
62 #ifdef HAVE_PWD_H
63 #include <pwd.h>
64 #endif
65
66 static gint hook_id;
67 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
68
69 gboolean spamassassin_enable;
70 gchar *spamassassin_hostname;
71 int spamassassin_port;
72 int spamassassin_max_size;
73 gboolean spamassassin_receive_spam;
74 gchar *spamassassin_save_folder;
75
76 static PrefParam param[] = {
77         {"enable", "FALSE", &spamassassin_enable, P_BOOL,
78          NULL, NULL, NULL},
79         {"hostname", "localhost", &spamassassin_hostname, P_STRING,
80          NULL, NULL, NULL},
81         {"port", "783", &spamassassin_port, P_USHORT,
82          NULL, NULL, NULL},
83         {"max_size", "250", &spamassassin_max_size, P_USHORT,
84          NULL, NULL, NULL},
85         {"receive_spam", "TRUE", &spamassassin_receive_spam, P_BOOL,
86          NULL, NULL, NULL},
87         {"save_folder", NULL, &spamassassin_save_folder, P_STRING,
88          NULL, NULL, NULL},
89
90         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
91 };
92
93 static gboolean mail_filtering_hook(gpointer source, gpointer data)
94 {
95         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
96         MsgInfo *msginfo = mail_filtering_data->msginfo;
97         gboolean is_spam = FALSE;
98         FILE *fp = NULL;
99         struct message m;
100         int ret;
101         gchar *username = NULL, *oldlocale = NULL;
102         struct sockaddr addr;
103
104         if (!spamassassin_enable)
105                 return FALSE;
106
107         debug_print("Filtering message %d\n", msginfo->msgnum);
108
109         oldlocale = g_strdup(setlocale(LC_ALL, NULL));
110         if (oldlocale == NULL) {
111                 debug_print("failed to get locale");
112                 goto CATCH;
113         }
114
115         setlocale(LC_ALL, "C");
116
117         ret = lookup_host(spamassassin_hostname, spamassassin_port, &addr);
118         if (ret != EX_OK) {
119                 debug_print("failed to look up spamd host");
120                 goto CATCH;
121         }
122
123         m.type = MESSAGE_NONE;
124         m.max_len = spamassassin_max_size * 1024;
125
126         username = g_get_user_name();
127         if (username == NULL) {
128                 debug_print("failed to get username");
129                 goto CATCH;
130         }
131
132         fp = procmsg_open_message(msginfo);
133         if (fp == NULL) {
134                 debug_print("failed to open message file");
135                 goto CATCH;
136         }
137
138         ret = message_read(fileno(fp), flags, &m);
139         if (ret != EX_OK) {
140                 debug_print("failed to read message");
141                 goto CATCH;
142         }
143
144         ret = message_filter(&addr, username, flags, &m);
145         if ((ret == EX_OK) && (m.is_spam == EX_ISSPAM))
146                 is_spam = TRUE;
147
148 CATCH:
149         if (fp != NULL)
150                 fclose(fp);
151         message_cleanup(&m);
152         if (oldlocale != NULL) {
153                 setlocale(LC_ALL, oldlocale);
154                 g_free(oldlocale);
155         }
156
157         if (is_spam) {
158                 if (spamassassin_receive_spam) {
159                         FolderItem *save_folder;
160
161                         debug_print("message is spam\n");
162                             
163                         if ((!spamassassin_save_folder) ||
164                             (spamassassin_save_folder[0] == '\0') ||
165                             ((save_folder = folder_find_item_from_identifier(spamassassin_save_folder)) == NULL))
166                                 save_folder = folder_get_default_trash();
167
168                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
169                         folder_item_move_msg(save_folder, msginfo);
170                 } else {
171                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
172                 }
173
174                 return TRUE;
175         }
176         
177         return FALSE;
178 }
179
180 void spamassassin_save_config()
181 {
182         PrefFile *pfile;
183         gchar *rcpath;
184
185         debug_print("Saving SpamAssassin Page\n");
186
187         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
188         pfile = prefs_write_open(rcpath);
189         g_free(rcpath);
190         if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
191                 return;
192
193         if (prefs_write_param(param, pfile->fp) < 0) {
194                 g_warning("failed to write SpamAssassin configuration to file\n");
195                 prefs_file_close_revert(pfile);
196                 return;
197         }
198         fprintf(pfile->fp, "\n");
199
200         prefs_file_close(pfile);
201 }
202
203 gint plugin_init(gchar **error)
204 {
205         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
206         if (hook_id == -1) {
207                 *error = g_strdup("Failed to register mail filtering hook");
208                 return -1;
209         }
210
211         prefs_set_default(param);
212         prefs_read_config(param, "SpamAssassin", COMMON_RC);
213
214         debug_print("Spamassassin plugin loaded\n");
215
216         return 0;
217         
218 }
219
220 void plugin_done()
221 {
222         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
223         g_free(spamassassin_hostname);
224
225         debug_print("Spamassassin plugin unloaded\n");
226 }
227
228 const gchar *plugin_name()
229 {
230         return "SpamAssassin";
231 }
232
233 const gchar *plugin_desc()
234 {
235         return "This plugin checks all messages that are received from a POP "
236                "account for spam using a SpamAssassin server. You will need "
237                "a SpamAssassin Server (spamd) running somewhere.\n"
238                "\n"
239                "When a message is identified as spam it can be deleted or "
240                "saved into a special folder.\n"
241                "\n"
242                "This plugin only contains the actual function for filtering "
243                "and deleting or moving the message. You probably want to load "
244                "a User Interface plugin too, otherwise you will have to "
245                "manually write the plugin configuration.\n";
246 }