ClamAV, SA plugins: inform the user what is happening via the statusbar
[claws.git] / src / plugins / clamav / clamav_plugin.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 #include <clamav.h>
28
29 #include "common/sylpheed.h"
30 #include "common/version.h"
31 #include "intl.h"
32 #include "plugin.h"
33 #include "utils.h"
34 #include "hooks.h"
35 #include "inc.h"
36 #include "mimeview.h"
37 #include "folder.h"
38 #include "prefs.h"
39 #include "prefs_gtk.h"
40 #include "statusbar.h"
41
42 #include "clamav_plugin.h"
43
44 static guint hook_id;
45
46 static ClamAvConfig config;
47
48 static PrefParam param[] = {
49         {"clamav_enable", "FALSE", &config.clamav_enable, P_BOOL,
50          NULL, NULL, NULL},
51         {"clamav_enable_arc", "FALSE", &config.clamav_enable_arc, P_BOOL,
52          NULL, NULL, NULL},
53         {"clamav_max_size", "1", &config.clamav_max_size, P_USHORT,
54          NULL, NULL, NULL},
55         {"clamav_recv_infected", "TRUE", &config.clamav_recv_infected, P_BOOL,
56          NULL, NULL, NULL},
57         {"clamav_save_folder", NULL, &config.clamav_save_folder, P_STRING,
58          NULL, NULL, NULL},
59
60         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
61 };
62
63 struct scan_parameters {
64         gboolean is_infected;
65
66         struct cl_node *root;
67         struct cl_limits limits;
68         gboolean scan_archive;
69 };
70
71 static gboolean scan_func(GNode *node, gpointer data)
72 {
73         struct scan_parameters *params = (struct scan_parameters *) data;
74         MimeInfo *mimeinfo = (MimeInfo *) node->data;
75         gchar *outfile;
76         int ret;
77         unsigned long int size;
78         char *virname;
79
80         outfile = procmime_get_tmp_file_name(mimeinfo);
81         if (procmime_get_part(outfile, mimeinfo) < 0)
82                 g_warning("Can't get the part of multipart message.");
83         else {
84                 debug_print("Scanning %s\n", outfile);
85                 if ((ret = cl_scanfile(outfile, &virname, &size, params->root, 
86                                       &params->limits, params->scan_archive)) == CL_VIRUS) {
87                         params->is_infected = TRUE;
88                         debug_print("Detected %s virus.\n", virname); 
89                 } else {
90                         debug_print("No virus detected.\n");
91                         if (ret != CL_CLEAN)
92                                 debug_print("Error: %s\n", cl_strerror(ret));
93                 }
94
95                 unlink(outfile);
96         }
97
98         return params->is_infected;
99 }
100
101 static gboolean mail_filtering_hook(gpointer source, gpointer data)
102 {
103         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
104         MsgInfo *msginfo = mail_filtering_data->msginfo;
105         MimeInfo *mimeinfo;
106
107         int ret, no = 0;
108         struct scan_parameters params;
109
110         if (!config.clamav_enable)
111                 return FALSE;
112
113         mimeinfo = procmime_scan_message(msginfo);
114         if (!mimeinfo) return FALSE;
115
116         debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
117         statusbar_print_all(_("ClamAV: scanning message..."));
118
119         params.is_infected = FALSE;
120         params.root = NULL;
121
122         params.limits.maxfiles = 1000; /* max files */
123         params.limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
124         params.limits.maxreclevel = 8; /* maximum recursion level */
125
126         if (config.clamav_enable_arc)
127                 params.scan_archive = TRUE;
128
129         if((ret = cl_loaddbdir(cl_retdbdir(), &params.root, &no))) {
130                 debug_print("cl_loaddbdir: %s\n", cl_strerror(ret));
131                 exit(2);
132         }
133         debug_print("Database loaded (containing in total %d signatures)\n", no);
134
135         cl_buildtrie(params.root);
136
137         g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, &params);
138
139         if (params.is_infected) {
140                 if (config.clamav_recv_infected) {
141                         FolderItem *clamav_save_folder;
142
143                         if ((!config.clamav_save_folder) ||
144                             (config.clamav_save_folder[0] == '\0') ||
145                             ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
146                                     clamav_save_folder = folder_get_default_trash();
147
148                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
149                         folder_item_move_msg(clamav_save_folder, msginfo);
150                 } else {
151                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
152                 }
153         }
154         
155         cl_freetrie(params.root);
156         procmime_mimeinfo_free_all(mimeinfo);
157         
158         return params.is_infected;
159 }
160
161 ClamAvConfig *clamav_get_config(void)
162 {
163         return &config;
164 }
165
166 void clamav_save_config(void)
167 {
168         PrefFile *pfile;
169         gchar *rcpath;
170
171         debug_print("Saving ClamAV Page\n");
172
173         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
174         pfile = prefs_write_open(rcpath);
175         g_free(rcpath);
176         if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
177                 return;
178
179         if (prefs_write_param(param, pfile->fp) < 0) {
180                 g_warning("failed to write ClamAV configuration to file\n");
181                 prefs_file_close_revert(pfile);
182                 return;
183         }
184         fprintf(pfile->fp, "\n");
185
186         prefs_file_close(pfile);
187 }
188
189 gint plugin_init(gchar **error)
190 {
191         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
192                 *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
193                 return -1;
194         }
195
196         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
197                 *error = g_strdup("Your sylpheed version is too old");
198                 return -1;
199         }
200
201         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
202         if (hook_id == -1) {
203                 *error = g_strdup("Failed to register mail filtering hook");
204                 return -1;
205         }
206
207         prefs_set_default(param);
208         prefs_read_config(param, "ClamAV", COMMON_RC);
209
210         debug_print("ClamAV plugin loaded\n");
211
212         return 0;
213         
214 }
215
216 void plugin_done(void)
217 {
218         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
219         g_free(config.clamav_save_folder);
220         
221         debug_print("ClamAV plugin unloaded\n");
222 }
223
224 const gchar *plugin_name(void)
225 {
226         return _("Clam AntiVirus");
227 }
228
229 const gchar *plugin_desc(void)
230 {
231         return _("This plugin uses Clam AntiVirus to scan all messages that are "
232                "received from an IMAP, LOCAL or POP account.\n"
233                "\n"
234                "When a message attachment is found to contain a virus it can be "
235                "deleted or saved in a specially designated folder.\n"
236                "\n"
237                "This plugin only contains the actual function for scanning "
238                "and deleting or moving the message. You probably want to load "
239                "the Gtk+ User Interface plugin too, otherwise you will have to "
240                "manually write the plugin configuration.\n");
241 }
242
243 const gchar *plugin_type(void)
244 {
245         return "Common";
246 }