bbea649dee7952eb67b8205a02f65ed85f66a2fd
[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 "procmsg.h"
36 #include "folder.h"
37 #include "prefs.h"
38 #include "prefs_gtk.h"
39 #include "intl.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 static gchar *username = NULL;
69
70 static SpamAssassinConfig config;
71
72 static PrefParam param[] = {
73         {"enable", "FALSE", &config.enable, P_BOOL,
74          NULL, NULL, NULL},
75         {"hostname", "localhost", &config.hostname, P_STRING,
76          NULL, NULL, NULL},
77         {"port", "783", &config.port, P_INT,
78          NULL, NULL, NULL},
79         {"max_size", "250", &config.max_size, P_INT,
80          NULL, NULL, NULL},
81         {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
82          NULL, NULL, NULL},
83         {"save_folder", NULL, &config.save_folder, P_STRING,
84          NULL, NULL, NULL},
85
86         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
87 };
88
89 static gboolean mail_filtering_hook(gpointer source, gpointer data)
90 {
91         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
92         MsgInfo *msginfo = mail_filtering_data->msginfo;
93         gboolean is_spam = FALSE;
94         FILE *fp = NULL;
95         struct message m;
96         gchar *oldlocale = NULL;
97         struct sockaddr addr;
98         int ret;
99
100         if (!config.enable)
101                 return FALSE;
102
103         debug_print("Filtering message %d\n", msginfo->msgnum);
104
105         /* remember old locale and set it to C */
106         Xstrdup_a(oldlocale, setlocale(LC_ALL, NULL), return FALSE);
107         setlocale(LC_ALL, "C");
108
109         if (lookup_host(config.hostname, config.port, &addr) != EX_OK) {
110                 debug_print("failed to look up spamd host\n");
111                 return FALSE;
112         }
113
114         m.type = MESSAGE_NONE;
115         m.max_len = config.max_size * 1024;
116         m.timeout = 30;
117
118         if ((fp = procmsg_open_message(msginfo)) == NULL) {
119                 debug_print("failed to open message file\n");
120                 return FALSE;
121         }
122
123         if (message_read(fileno(fp), flags, &m) != EX_OK) {
124                 debug_print("failed to read message\n");
125                 fclose(fp);
126                 message_cleanup(&m);
127                 return FALSE;
128         }
129
130         if ((ret = message_filter(&addr, username, flags, &m)) != EX_OK) {
131                 debug_print("filtering the message failed\n");
132                 fclose(fp);
133                 message_cleanup(&m);
134                 return FALSE;
135         }
136
137         if (m.is_spam == EX_ISSPAM)
138                 is_spam = TRUE;
139
140         message_cleanup(&m);
141         fclose(fp);
142         setlocale(LC_ALL, oldlocale);
143
144         if (is_spam) {
145                 debug_print("message is spam\n");
146                             
147                 if (config.receive_spam) {
148                         FolderItem *save_folder;
149
150                         if ((!config.save_folder) ||
151                             (config.save_folder[0] == '\0') ||
152                             ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
153                                 save_folder = folder_get_default_trash();
154
155                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
156                         folder_item_move_msg(save_folder, msginfo);
157                 } else {
158                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
159                 }
160
161                 return TRUE;
162         }
163         
164         return FALSE;
165 }
166
167 SpamAssassinConfig *spamassassin_get_config(void)
168 {
169         return &config;
170 }
171
172 void spamassassin_save_config(void)
173 {
174         PrefFile *pfile;
175         gchar *rcpath;
176
177         debug_print("Saving SpamAssassin Page\n");
178
179         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
180         pfile = prefs_write_open(rcpath);
181         g_free(rcpath);
182         if (!pfile || (prefs_set_block_label(pfile, "SpamAssassin") < 0))
183                 return;
184
185         if (prefs_write_param(param, pfile->fp) < 0) {
186                 g_warning("failed to write SpamAssassin configuration to file\n");
187                 prefs_file_close_revert(pfile);
188                 return;
189         }
190         fprintf(pfile->fp, "\n");
191
192         prefs_file_close(pfile);
193 }
194
195 gint plugin_init(gchar **error)
196 {
197         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
198         if (hook_id == -1) {
199                 *error = g_strdup("Failed to register mail filtering hook");
200                 return -1;
201         }
202
203         username = g_get_user_name();
204         if (username == NULL) {
205                 *error = g_strdup("Failed to get username");
206                 return -1;
207         }
208
209         prefs_set_default(param);
210         prefs_read_config(param, "SpamAssassin", COMMON_RC);
211
212         debug_print("Spamassassin plugin loaded\n");
213
214         return 0;
215         
216 }
217
218 void plugin_done(void)
219 {
220         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
221         g_free(config.hostname);
222         g_free(config.save_folder);
223
224         debug_print("Spamassassin plugin unloaded\n");
225 }
226
227 const gchar *plugin_name(void)
228 {
229         return _("SpamAssassin");
230 }
231
232 const gchar *plugin_desc(void)
233 {
234         return _("This plugin checks all messages that are received from a POP "
235                  "account for spam using a SpamAssassin server. You will need "
236                  "a SpamAssassin Server (spamd) running somewhere.\n"
237                  "\n"
238                  "When a message is identified as spam it can be deleted or "
239                  "saved into a special folder.\n"
240                  "\n"
241                  "This plugin only contains the actual function for filtering "
242                  "and deleting or moving the message. You probably want to load "
243                  "a User Interface plugin too, otherwise you will have to "
244                  "manually write the plugin configuration.\n");
245 }
246
247 const gchar *plugin_type(void)
248 {
249         return "Common";
250 }