2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2007 the Claws Mail Team
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
30 #include "common/claws.h"
31 #include "common/version.h"
39 #include "prefs_gtk.h"
41 #include "clamav_plugin.h"
43 #define PLUGIN_NAME (_("Clam AntiVirus"))
46 static MessageCallback message_callback;
48 static ClamAvConfig config;
50 static PrefParam param[] = {
51 {"clamav_enable", "FALSE", &config.clamav_enable, P_BOOL,
53 {"clamav_enable_arc", "FALSE", &config.clamav_enable_arc, P_BOOL,
55 {"clamav_max_size", "1", &config.clamav_max_size, P_USHORT,
57 {"clamav_recv_infected", "TRUE", &config.clamav_recv_infected, P_BOOL,
59 {"clamav_save_folder", NULL, &config.clamav_save_folder, P_STRING,
62 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
65 static struct cl_node *cl_database;
66 struct scan_parameters {
69 struct cl_limits limits;
71 gboolean scan_archive;
74 static gboolean scan_func(GNode *node, gpointer data)
76 struct scan_parameters *params = (struct scan_parameters *) data;
77 MimeInfo *mimeinfo = (MimeInfo *) node->data;
80 unsigned long int size;
83 outfile = procmime_get_tmp_file_name(mimeinfo);
84 if (procmime_get_part(outfile, mimeinfo) < 0)
85 g_warning("Can't get the part of multipart message.");
87 debug_print("Scanning %s\n", outfile);
88 if ((ret = cl_scanfile(outfile, &virname, &size, params->root,
89 ¶ms->limits, params->scan_archive)) == CL_VIRUS) {
90 params->is_infected = TRUE;
91 debug_print("Detected %s virus.\n", virname);
93 debug_print("No virus detected.\n");
95 debug_print("Error: %s\n", cl_strerror(ret));
101 return params->is_infected;
104 static gboolean mail_filtering_hook(gpointer source, gpointer data)
106 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
107 MsgInfo *msginfo = mail_filtering_data->msginfo;
110 struct scan_parameters params;
112 if (!config.clamav_enable)
115 mimeinfo = procmime_scan_message(msginfo);
116 if (!mimeinfo) return FALSE;
118 debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
119 if (message_callback != NULL)
120 message_callback(_("ClamAV: scanning message..."));
122 params.is_infected = FALSE;
123 params.root = cl_database;
125 params.limits.maxfiles = 1000; /* max files */
126 params.limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
127 params.limits.maxreclevel = 8; /* maximum recursion level */
128 params.limits.maxratio = 200; /* maximal compression ratio */
129 params.limits.archivememlim = 0; /* disable memory limit for bzip2 scanner */
131 if (config.clamav_enable_arc)
132 params.scan_archive = TRUE;
134 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, ¶ms);
136 if (params.is_infected) {
137 if (config.clamav_recv_infected) {
138 FolderItem *clamav_save_folder;
140 if ((!config.clamav_save_folder) ||
141 (config.clamav_save_folder[0] == '\0') ||
142 ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
143 clamav_save_folder = folder_get_default_trash();
145 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
146 msginfo->is_move = TRUE;
147 msginfo->to_filter_folder = clamav_save_folder;
149 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
153 procmime_mimeinfo_free_all(mimeinfo);
155 return params.is_infected;
158 ClamAvConfig *clamav_get_config(void)
163 void clamav_save_config(void)
168 debug_print("Saving ClamAV Page\n");
170 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
171 pfile = prefs_write_open(rcpath);
173 if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
176 if (prefs_write_param(param, pfile->fp) < 0) {
177 g_warning("failed to write ClamAV configuration to file\n");
178 prefs_file_close_revert(pfile);
181 fprintf(pfile->fp, "\n");
183 prefs_file_close(pfile);
186 void clamav_set_message_callback(MessageCallback callback)
188 message_callback = callback;
191 int cl_build(struct cl_node *root);
192 void cl_free(struct cl_node *root);
193 gint plugin_init(gchar **error)
199 if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
200 VERSION_NUMERIC, PLUGIN_NAME, error))
203 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
205 *error = g_strdup(_("Failed to register mail filtering hook"));
209 prefs_set_default(param);
210 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
211 prefs_read_config(param, "ClamAV", rcpath, NULL);
216 if ((ret = cl_loaddbdir(cl_retdbdir(), &cl_database, &no))) {
217 debug_print("cl_loaddbdir: %s\n", cl_strerror(ret));
218 *error = g_strdup_printf("cl_loaddbdir: %s\n", cl_strerror(ret));
222 debug_print("Database loaded (containing in total %d signatures)\n", no);
224 if((ret = cl_build(cl_database))) {
225 debug_print("Database initialization error: %s\n", cl_strerror(ret));
226 *error = g_strdup_printf("Database initialization error: %s\n", cl_strerror(ret));
230 debug_print("ClamAV plugin loaded\n");
236 void plugin_done(void)
238 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
239 g_free(config.clamav_save_folder);
240 cl_free(cl_database);
243 debug_print("ClamAV plugin unloaded\n");
246 const gchar *plugin_name(void)
251 const gchar *plugin_desc(void)
253 return _("This plugin uses Clam AntiVirus to scan all messages that are "
254 "received from an IMAP, LOCAL or POP account.\n"
256 "When a message attachment is found to contain a virus it can be "
257 "deleted or saved in a specially designated folder.\n"
259 "Options can be found in /Configuration/Preferences/Plugins/Clam AntiVirus");
262 const gchar *plugin_type(void)
267 const gchar *plugin_licence(void)
272 const gchar *plugin_version(void)
277 struct PluginFeature *plugin_provides(void)
279 static struct PluginFeature features[] =
280 { {PLUGIN_FILTERING, N_("Virus detection")},
281 {PLUGIN_NOTHING, NULL}};