0.8.8claws26
[claws.git] / src / plugins / spamassassin / spamassassin.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 <glib.h>
25
26 #if HAVE_LOCALE_H
27 #  include <locale.h>
28 #endif
29
30 #include "plugin.h"
31 #include "common/utils.h"
32 #include "hooks.h"
33 #include "inc.h"
34 #include "procmsg.h"
35 #include "folder.h"
36
37 #include "libspamc.h"
38
39 #ifdef HAVE_SYSEXITS_H
40 #include <sysexits.h>
41 #endif
42 #ifdef HAVE_ERRNO_H
43 #include <errno.h>
44 #endif
45 #ifdef HAVE_SYS_ERRNO_H
46 #include <sys/errno.h>
47 #endif
48 #ifdef HAVE_TIME_H
49 #include <time.h>
50 #endif
51 #ifdef HAVE_SYS_TIME_H
52 #include <sys/time.h>
53 #endif
54 #ifdef HAVE_SIGNAL_H
55 #include <signal.h>
56 #endif
57 #ifdef HAVE_PWD_H
58 #include <pwd.h>
59 #endif
60
61 static gint hook_id;
62 static int max_size;
63 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
64 static gchar *hostname = NULL;
65 static int port;
66
67 static gboolean mail_filtering_hook(gpointer source, gpointer data)
68 {
69         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
70         MsgInfo *msginfo = mail_filtering_data->msginfo;
71         gboolean is_spam = FALSE;
72         FILE *fp = NULL;
73         struct message m;
74         int ret;
75         gchar *username = NULL, *oldlocale = NULL;
76         struct sockaddr addr;
77         
78         debug_print("Filtering message %d\n", msginfo->msgnum);
79
80         oldlocale = g_strdup(setlocale(LC_ALL, NULL));
81         if (oldlocale == NULL)
82                 goto CATCH;
83
84         setlocale(LC_ALL, "C");
85
86         ret = lookup_host(hostname, port, &addr);
87         if (ret != EX_OK)
88                 goto CATCH;
89
90         m.type = MESSAGE_NONE;
91         m.max_len = max_size;
92
93         username = g_get_user_name();
94         if (username == NULL)
95                 goto CATCH;
96
97         fp = procmsg_open_message(msginfo);
98         if (fp == NULL)
99                 goto CATCH;
100
101         ret = message_read(fileno(fp), flags, &m);
102         if (ret != EX_OK)
103                 goto CATCH;
104
105         ret = message_filter(&addr, username, flags, &m);
106         if ((ret == EX_OK) && (m.is_spam == EX_ISSPAM))
107                 is_spam = TRUE;
108
109 CATCH:
110         if (fp != NULL)
111                 fclose(fp);
112         message_cleanup(&m);
113         if (oldlocale != NULL) {
114                 setlocale(LC_ALL, oldlocale);
115                 g_free(oldlocale);
116         }
117
118         if (is_spam) {
119                 debug_print("Message is spam\n");
120
121                 folder_item_move_msg(folder_get_default_trash(), msginfo);
122                 return TRUE;
123         }
124         
125         return FALSE;
126 }
127
128 static void spamassassin_read_config()
129 {
130         max_size = 250*1024;
131         hostname = "127.0.0.1";
132         port = 783;
133 }
134
135 gint plugin_init(gchar **error)
136 {
137         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
138         if (hook_id == -1) {
139                 *error = g_strdup("Failed to register mail filtering hook");
140                 return -1;
141         }
142
143         spamassassin_read_config();
144
145         debug_print("Spamassassin plugin loaded\n");
146
147         return 0;
148         
149 }
150
151 void plugin_done()
152 {
153         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
154
155         debug_print("Spamassassin plugin unloaded\n");
156 }
157
158 const gchar *plugin_name()
159 {
160         return "Spamassassin Plugin";
161 }
162
163 const gchar *plugin_desc()
164 {
165         return "Check incoming mails for spam with spamassassin";
166 }
167