2005-10-27 [paul] 1.9.15cvs116
[claws.git] / src / plugins / clamav / clamav_plugin.c
index 35be5cf22f40d0b2a3e3ac6e94b2e458641a8e22..c4fc5beda2f519e54e1f729f8bbd75eb5cdbf8e0 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <clamav.h>
 
 #include "common/sylpheed.h"
 #include "common/version.h"
-#include "intl.h"
 #include "plugin.h"
 #include "utils.h"
 #include "hooks.h"
@@ -41,6 +41,7 @@
 #include "clamav_plugin.h"
 
 static guint hook_id;
+static MessageCallback message_callback;
 
 static ClamAvConfig config;
 
@@ -59,11 +60,12 @@ static PrefParam param[] = {
        {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
 };
 
+static struct cl_node *cl_database;
 struct scan_parameters {
        gboolean is_infected;
 
-       struct cl_node *root;
        struct cl_limits limits;
+       struct cl_node *root;
        gboolean scan_archive;
 };
 
@@ -91,7 +93,7 @@ static gboolean scan_func(GNode *node, gpointer data)
                                debug_print("Error: %s\n", cl_strerror(ret));
                }
 
-               unlink(outfile);
+               g_unlink(outfile);
        }
 
        return params->is_infected;
@@ -103,7 +105,6 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
        MsgInfo *msginfo = mail_filtering_data->msginfo;
        MimeInfo *mimeinfo;
 
-       int ret, no = 0;
        struct scan_parameters params;
 
        if (!config.clamav_enable)
@@ -113,10 +114,11 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
        if (!mimeinfo) return FALSE;
 
        debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
-       statusbar_print_all(_("ClamAV: scanning message..."));
+       if (message_callback != NULL)
+               message_callback(_("ClamAV: scanning message..."));
 
        params.is_infected = FALSE;
-       params.root = NULL;
+       params.root = cl_database;
 
        params.limits.maxfiles = 1000; /* max files */
        params.limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
@@ -125,15 +127,6 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
        if (config.clamav_enable_arc)
                params.scan_archive = TRUE;
 
-       if ((ret = cl_loaddbdir(cl_retdbdir(), &params.root, &no))) {
-               debug_print("cl_loaddbdir: %s\n", cl_strerror(ret));
-               return FALSE;
-       }
-
-       debug_print("Database loaded (containing in total %d signatures)\n", no);
-
-       cl_buildtrie(params.root);
-
        g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, &params);
 
        if (params.is_infected) {
@@ -152,7 +145,6 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
                }
        }
        
-       cl_freetrie(params.root);
        procmime_mimeinfo_free_all(mimeinfo);
        
        return params.is_infected;
@@ -186,15 +178,24 @@ void clamav_save_config(void)
        prefs_file_close(pfile);
 }
 
+void clamav_set_message_callback(MessageCallback callback)
+{
+       message_callback = callback;
+}
+
+void cl_buildtrie(struct cl_node *root);
+void cl_freetrie(struct cl_node *root);
 gint plugin_init(gchar **error)
 {
+       gchar *rcpath;
+       int no, ret;
        if ((sylpheed_get_version() > VERSION_NUMERIC)) {
-               *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
+               *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the ClamAV plugin was built with");
                return -1;
        }
 
        if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
-               *error = g_strdup("Your sylpheed version is too old");
+               *error = g_strdup("Your version of Sylpheed-Claws is too old for the ClamAV plugin");
                return -1;
        }
 
@@ -205,7 +206,18 @@ gint plugin_init(gchar **error)
        }
 
        prefs_set_default(param);
-       prefs_read_config(param, "ClamAV", COMMON_RC);
+       rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
+       prefs_read_config(param, "ClamAV", rcpath, NULL);
+       g_free(rcpath);
+
+       if ((ret = cl_loaddbdir(cl_retdbdir(), &cl_database, &no)) != 0) {
+               debug_print("cl_loaddbdir: %s\n", cl_strerror(ret));
+               return -1;
+       }
+
+       debug_print("Database loaded (containing in total %d signatures)\n", no);
+
+       cl_buildtrie(cl_database);
 
        debug_print("ClamAV plugin loaded\n");
 
@@ -217,7 +229,7 @@ void plugin_done(void)
 {
        hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
        g_free(config.clamav_save_folder);
-       
+       cl_freetrie(cl_database);
        debug_print("ClamAV plugin unloaded\n");
 }