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