2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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/sylpheed.h"
31 #include "common/version.h"
39 #include "prefs_gtk.h"
41 #include "clamav_plugin.h"
44 static MessageCallback message_callback;
46 static ClamAvConfig config;
48 static PrefParam param[] = {
49 {"clamav_enable", "FALSE", &config.clamav_enable, P_BOOL,
51 {"clamav_enable_arc", "FALSE", &config.clamav_enable_arc, P_BOOL,
53 {"clamav_max_size", "1", &config.clamav_max_size, P_USHORT,
55 {"clamav_recv_infected", "TRUE", &config.clamav_recv_infected, P_BOOL,
57 {"clamav_save_folder", NULL, &config.clamav_save_folder, P_STRING,
60 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
63 static struct cl_node *cl_database;
64 struct scan_parameters {
67 struct cl_limits limits;
69 gboolean scan_archive;
72 static gboolean scan_func(GNode *node, gpointer data)
74 struct scan_parameters *params = (struct scan_parameters *) data;
75 MimeInfo *mimeinfo = (MimeInfo *) node->data;
78 unsigned long int size;
81 outfile = procmime_get_tmp_file_name(mimeinfo);
82 if (procmime_get_part(outfile, mimeinfo) < 0)
83 g_warning("Can't get the part of multipart message.");
85 debug_print("Scanning %s\n", outfile);
86 if ((ret = cl_scanfile(outfile, &virname, &size, params->root,
87 ¶ms->limits, params->scan_archive)) == CL_VIRUS) {
88 params->is_infected = TRUE;
89 debug_print("Detected %s virus.\n", virname);
91 debug_print("No virus detected.\n");
93 debug_print("Error: %s\n", cl_strerror(ret));
99 return params->is_infected;
102 static gboolean mail_filtering_hook(gpointer source, gpointer data)
104 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
105 MsgInfo *msginfo = mail_filtering_data->msginfo;
108 struct scan_parameters params;
110 if (!config.clamav_enable)
113 mimeinfo = procmime_scan_message(msginfo);
114 if (!mimeinfo) return FALSE;
116 debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
117 if (message_callback != NULL)
118 message_callback(_("ClamAV: scanning message..."));
120 params.is_infected = FALSE;
121 params.root = cl_database;
123 params.limits.maxfiles = 1000; /* max files */
124 params.limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
125 params.limits.maxreclevel = 8; /* maximum recursion level */
126 params.limits.maxratio = 200; /* maximal compression ratio */
127 params.limits.archivememlim = 0; /* disable memory limit for bzip2 scanner */
129 if (config.clamav_enable_arc)
130 params.scan_archive = TRUE;
132 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, ¶ms);
134 if (params.is_infected) {
135 if (config.clamav_recv_infected) {
136 FolderItem *clamav_save_folder;
138 if ((!config.clamav_save_folder) ||
139 (config.clamav_save_folder[0] == '\0') ||
140 ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
141 clamav_save_folder = folder_get_default_trash();
143 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
144 folder_item_move_msg(clamav_save_folder, msginfo);
146 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
150 procmime_mimeinfo_free_all(mimeinfo);
152 return params.is_infected;
155 ClamAvConfig *clamav_get_config(void)
160 void clamav_save_config(void)
165 debug_print("Saving ClamAV Page\n");
167 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
168 pfile = prefs_write_open(rcpath);
170 if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
173 if (prefs_write_param(param, pfile->fp) < 0) {
174 g_warning("failed to write ClamAV configuration to file\n");
175 prefs_file_close_revert(pfile);
178 fprintf(pfile->fp, "\n");
180 prefs_file_close(pfile);
183 void clamav_set_message_callback(MessageCallback callback)
185 message_callback = callback;
188 int cl_build(struct cl_node *root);
189 void cl_free(struct cl_node *root);
190 gint plugin_init(gchar **error)
196 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
197 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the ClamAV plugin was built with");
201 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
202 *error = g_strdup("Your version of Sylpheed-Claws is too old for the ClamAV plugin");
206 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
208 *error = g_strdup("Failed to register mail filtering hook");
212 prefs_set_default(param);
213 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
214 prefs_read_config(param, "ClamAV", rcpath, NULL);
219 if ((ret = cl_loaddbdir(cl_retdbdir(), &cl_database, &no))) {
220 debug_print("cl_loaddbdir: %s\n", cl_strerror(ret));
224 debug_print("Database loaded (containing in total %d signatures)\n", no);
226 if((ret = cl_build(cl_database))) {
227 debug_print("Database initialization error: %s\n", cl_strerror(ret));
231 debug_print("ClamAV plugin loaded\n");
237 void plugin_done(void)
239 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
240 g_free(config.clamav_save_folder);
241 cl_free(cl_database);
244 debug_print("ClamAV plugin unloaded\n");
247 const gchar *plugin_name(void)
249 return _("Clam AntiVirus");
252 const gchar *plugin_desc(void)
254 return _("This plugin uses Clam AntiVirus to scan all messages that are "
255 "received from an IMAP, LOCAL or POP account.\n"
257 "When a message attachment is found to contain a virus it can be "
258 "deleted or saved in a specially designated folder.\n"
262 const gchar *plugin_type(void)
267 const gchar *plugin_licence(void)
272 const gchar *plugin_version(void)