2005-07-20 [colin] 1.9.12cvs87
[claws.git] / src / plugins / clamav / clamav_plugin.c
index eac85ce429e1ed4f44bc9d5b820740655986d4ec..488fbaf9c24be1736b698cfc542d17f32f6c01a6 100644 (file)
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <clamav.h>
 
-#include "common/plugin.h"
-#include "common/utils.h"
+#include "common/sylpheed.h"
+#include "common/version.h"
+#include "plugin.h"
+#include "utils.h"
 #include "hooks.h"
 #include "inc.h"
 #include "mimeview.h"
@@ -37,7 +40,8 @@
 
 #include "clamav_plugin.h"
 
-static gint hook_id;
+static guint hook_id;
+static MessageCallback message_callback;
 
 static ClamAvConfig config;
 
@@ -56,115 +60,76 @@ static PrefParam param[] = {
        {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
 };
 
-#define IS_FIRST_PART_TEXT(info) \
-       ((info->mime_type == MIME_TEXT || info->mime_type == MIME_TEXT_HTML || \
-         info->mime_type == MIME_TEXT_ENRICHED) || \
-        (info->mime_type == MIME_MULTIPART && info->content_type && \
-         !strcasecmp(info->content_type, "multipart/alternative") && \
-         (info->children && \
-          (info->children->mime_type == MIME_TEXT || \
-           info->children->mime_type == MIME_TEXT_HTML || \
-           info->children->mime_type == MIME_TEXT_ENRICHED))))
+static struct cl_node *cl_database;
+struct scan_parameters {
+       gboolean is_infected;
+
+       struct cl_limits limits;
+       struct cl_node *root;
+       gboolean scan_archive;
+};
+
+static gboolean scan_func(GNode *node, gpointer data)
+{
+       struct scan_parameters *params = (struct scan_parameters *) data;
+       MimeInfo *mimeinfo = (MimeInfo *) node->data;
+       gchar *outfile;
+       int ret;
+       unsigned long int size;
+       const char *virname;
+
+       outfile = procmime_get_tmp_file_name(mimeinfo);
+       if (procmime_get_part(outfile, mimeinfo) < 0)
+               g_warning("Can't get the part of multipart message.");
+       else {
+               debug_print("Scanning %s\n", outfile);
+               if ((ret = cl_scanfile(outfile, &virname, &size, params->root, 
+                                     &params->limits, params->scan_archive)) == CL_VIRUS) {
+                       params->is_infected = TRUE;
+                       debug_print("Detected %s virus.\n", virname); 
+               } else {
+                       debug_print("No virus detected.\n");
+                       if (ret != CL_CLEAN)
+                               debug_print("Error: %s\n", cl_strerror(ret));
+               }
+
+               unlink(outfile);
+       }
+
+       return params->is_infected;
+}
 
 static gboolean mail_filtering_hook(gpointer source, gpointer data)
 {
        MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
        MsgInfo *msginfo = mail_filtering_data->msginfo;
-       gboolean is_infected = FALSE;
-       FILE  *fp;
        MimeInfo *mimeinfo;
-       MimeInfo *child;
-       gchar *infile;
-       gchar *outfile;
-       gint scan_archive = 0;
 
-       int ret, no;
-       unsigned long int size;
-       long double kb;
-       char *virname;
-       struct cl_node *root = NULL;
-       struct cl_limits limits;
+       struct scan_parameters params;
 
        if (!config.clamav_enable)
                return FALSE;
 
-       fp = procmsg_open_message(msginfo);
-       if (fp == NULL) {
-               debug_print("failed to open message file");
-       } 
-       
        mimeinfo = procmime_scan_message(msginfo);
        if (!mimeinfo) return FALSE;
 
-       child = mimeinfo->children;
-       if (!child || IS_FIRST_PART_TEXT(mimeinfo)) {
-               procmime_mimeinfo_free_all(mimeinfo);
-               return FALSE;
-       }
-
        debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
+       if (message_callback != NULL)
+               message_callback(_("ClamAV: scanning message..."));
 
-       if (IS_FIRST_PART_TEXT(child))
-               child = child->next;
-
-       infile = procmsg_get_message_file_path(msginfo);
-
-
-       if((ret = cl_loaddbdir(cl_retdbdir(), &root, &no))) {
-               debug_print("cl_loaddbdir: %s\n", cl_perror(ret));
-               exit(2);
-       }
-
-       debug_print("Loaded %d viruses.\n", no);
-
-       cl_buildtrie(root);
+       params.is_infected = FALSE;
+       params.root = cl_database;
 
-       limits.maxfiles = 1000; /* max files */
-       limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
-       limits.maxreclevel = 8; /* maximum recursion level */
+       params.limits.maxfiles = 1000; /* max files */
+       params.limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
+       params.limits.maxreclevel = 8; /* maximum recursion level */
 
        if (config.clamav_enable_arc)
-               scan_archive = TRUE;
+               params.scan_archive = TRUE;
 
-       while (child != NULL) {
-               if (child->children || child->mime_type == MIME_MULTIPART) {
-                       child = procmime_mimeinfo_next(child);
-                       continue;
-               }
-               if(child->parent && child->parent->parent
-               && !strcasecmp(child->parent->parent->content_type, "multipart/signed")
-               && child->mime_type == MIME_TEXT) {
-                       /* this is the main text part of a signed message */
-                       child = procmime_mimeinfo_next(child);
-                       continue;
-               }
-               outfile = procmime_get_tmp_file_name(child);
-               if (procmime_get_part(outfile, infile, child) < 0)
-                       g_warning("Can't get the part of multipart message.");
-               else {
-                       debug_print("Scanning %s\n", outfile);
-                       if ((ret = cl_scanfile(outfile, &virname, &size, root, 
-                                             &limits, scan_archive)) == CL_VIRUS) {
-                               is_infected = TRUE;
-                               debug_print("Detected %s virus.\n", virname); 
-                       } else {
-                               debug_print("No virus detected.\n");
-                               if (ret != CL_CLEAN)
-                                       debug_print("Error: %s\n", cl_perror(ret));
-                       }
-
-                       kb = size * (CL_COUNT_PRECISION / 1024);
-                       debug_print("Data scanned: %2.2Lf Kb\n", kb);
-    
-                       unlink(outfile);
-
-                       if (is_infected) break;
-                       child = child->next;
-               }
-       }
+       g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, &params);
 
-       if (is_infected) {
-               debug_print("message part(s) infected with %s\n", virname);
+       if (params.is_infected) {
                if (config.clamav_recv_infected) {
                        FolderItem *clamav_save_folder;
 
@@ -180,21 +145,17 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
                }
        }
        
-       cl_freetrie(root);
-       g_free(infile);
        procmime_mimeinfo_free_all(mimeinfo);
        
-       return is_infected;
+       return params.is_infected;
 }
 
-#undef IS_FIRST_PART_TEXT
-
-ClamAvConfig *clamav_get_config()
+ClamAvConfig *clamav_get_config(void)
 {
        return &config;
 }
 
-void clamav_save_config()
+void clamav_save_config(void)
 {
        PrefFile *pfile;
        gchar *rcpath;
@@ -217,8 +178,25 @@ void clamav_save_config()
        prefs_file_close(pfile);
 }
 
+void clamav_set_message_callback(MessageCallback callback)
+{
+       message_callback = callback;
+}
+
 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");
+               return -1;
+       }
+
+       if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
+               *error = g_strdup("Your sylpheed version is too old");
+               return -1;
+       }
+
        hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
        if (hook_id == -1) {
                *error = g_strdup("Failed to register mail filtering hook");
@@ -226,7 +204,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");
 
@@ -234,23 +223,23 @@ gint plugin_init(gchar **error)
        
 }
 
-void plugin_done()
+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");
 }
 
 const gchar *plugin_name(void)
 {
-       return "Clam AntiVirus";
+       return _("Clam AntiVirus");
 }
 
 const gchar *plugin_desc(void)
 {
-       return "This plugin uses Clam AntiVirus to scan all message attachments "
-              "that are received from a POP account.\n"
+       return _("This plugin uses Clam AntiVirus to scan all messages that are "
+              "received from an IMAP, LOCAL or POP account.\n"
               "\n"
               "When a message attachment is found to contain a virus it can be "
               "deleted or saved in a specially designated folder.\n"
@@ -258,7 +247,7 @@ const gchar *plugin_desc(void)
               "This plugin only contains the actual function for scanning "
               "and deleting or moving the message. You probably want to load "
               "the Gtk+ User Interface plugin too, otherwise you will have to "
-              "manually write the plugin configuration.\n";
+              "manually write the plugin configuration.\n");
 }
 
 const gchar *plugin_type(void)