2013-02-20 [colin] 3.9.0cvs93
[claws.git] / src / common / plugin.c
index 8fa623f6c1665aa5d1850048d1f8eb467479f823..762a89a584c831f582b991f3803bc426c6baa3be 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail Team
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 
  */
 
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <stdio.h>
+#ifdef HAVE_VALGRIND
+#include <valgrind.h>
+#endif
 
 #include "defs.h"
 #include <glib.h>
@@ -35,9 +44,6 @@
 #include "claws.h"
 #include "timing.h"
 
-#ifdef HAVE_VALGRIND
-#include "valgrind.h"
-#endif
 struct _Plugin
 {
        gchar   *filename;
@@ -52,6 +58,7 @@ struct _Plugin
        GSList *rdeps;
        gchar *error;
        gboolean unloaded_hidden;
+       gboolean in_prefix_dir;
 };
 
 const gchar *plugin_feature_names[] =
@@ -66,6 +73,24 @@ const gchar *plugin_feature_names[] =
          N_("things"),
          NULL };
 
+/* The plugin must be at least under one of these licences and have
+   the corresponding token returned by the plugin_licence function.
+ */
+const gchar const *plugin_licence_tokens[] = {
+  "LGPL2.1+", "LGPLv2.1+", "LGPL2.1", "LGPLv2.1",
+  "LGPL3+", "LGPLv3+", "LGPL3", "LGPLv3",
+  "GPL3+", "GPLv3+", "GPL3", "GPLv3",
+  "GPL2+", "GPLv2+",
+  "Apache2.0", "Apache 2.0", "Apache v2.0",
+  "2-clause BSD", "Simplified BSD", "FreeBSD",
+  "3-clause BSD", "New BSD", "Modified BSD",
+  NULL
+};
+
+/* Dual (or more) licences are allowed, must be separated by one of these.
+ */
+#define IS_LICENCE_SEP(a) ((a) == ',' || (a) == ';' || (a) == '|' || (a) == '/' || (a) == '\0')
+
 /**
  * List of all loaded plugins
  */
@@ -89,12 +114,27 @@ static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *file
           different directory.  I think it would be better to compare
           only the basename of the filename here (case-insensitive on
           W32). */
-       g_return_val_if_fail(plugin, 1);
-       g_return_val_if_fail(plugin->filename, 1);
-       g_return_val_if_fail(filename, 1);
+       cm_return_val_if_fail(plugin, 1);
+       cm_return_val_if_fail(plugin->filename, 1);
+       cm_return_val_if_fail(filename, 1);
        return strcmp(filename, plugin->filename);
 }
 
+static gboolean plugin_filename_is_standard_dir(const gchar *filename) {
+       return strncmp(filename, get_plugin_dir(), strlen(get_plugin_dir())) == 0;
+}
+
+static gchar * plugin_canonical_name(const Plugin *plugin)
+{
+       if (plugin->in_prefix_dir == TRUE) {
+               if (plugin_filename_is_standard_dir(plugin->filename) == TRUE) {
+                       gchar *plugin_name = g_path_get_basename(plugin->filename);
+                       return plugin_name;
+               }
+       }
+       return g_strdup(plugin->filename);
+}
+
 void plugin_save_list(void)
 {
        gchar *rcpath, *block;
@@ -123,9 +163,13 @@ void plugin_save_list(void)
                        if (plugin->unloaded_hidden)
                                continue;
 
-                       if (!strcmp(plugin->type(), type_cur->data))
-                               if (fprintf(pfile->fp, "%s\n", plugin->filename) < 0)
+                       if (!strcmp(plugin->type(), type_cur->data)) {
+                               gchar * name = plugin_canonical_name(plugin);
+                               int err = fprintf(pfile->fp, "%s\n", name);
+                               g_free(name);
+                               if (err < 0)
                                        goto revert;
+                       }
                }
                for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
                        plugin = (Plugin *) plugin_cur->data;
@@ -133,9 +177,13 @@ void plugin_save_list(void)
                        if (plugin->unloaded_hidden)
                                continue;
                        
-                       if (!strcmp(plugin->type(), type_cur->data))
-                               if (fprintf(pfile->fp, "%s\n", plugin->filename) < 0)
+                       if (!strcmp(plugin->type(), type_cur->data)) {
+                               gchar * name = plugin_canonical_name(plugin);
+                               int err = fprintf(pfile->fp, "%s\n", name);
+                               g_free(name);
+                               if (err < 0)
                                        goto revert;
+                       }
                }
                if (fprintf(pfile->fp, "\n") < 0)
                        goto revert;
@@ -214,8 +262,10 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
                        dep_plugin = plugin_load(path, error);
                        if (dep_plugin == NULL) {
                                g_free(path);
+                               fclose(fp);
                                return -1;
                        }
+                       dep_plugin->in_prefix_dir = TRUE;
                }
                g_free(path);
                if (!g_slist_find_custom(dep_plugin->rdeps, 
@@ -293,6 +343,69 @@ static gchar *plugin_check_features(struct PluginFeature *features) {
 
        return NULL;
 }
+
+static gboolean plugin_licence_check(const gchar *licence) {
+       gint i = 0;
+       gint len = 0;
+       
+       if (licence != NULL) {
+               len = strlen(licence);
+       }
+       if (len == 0) {
+               g_warning("plugin licence check failed: empty licence\n");
+               return FALSE;
+       }
+       while (plugin_licence_tokens[i] != NULL) {
+               gchar *found = g_strstr_len(licence, len, plugin_licence_tokens[i]);
+               if (found != NULL) {
+                       gint tlen = strlen(plugin_licence_tokens[i]);
+                       if (len != tlen) { /* not a single license */
+                               if (((found == licence) &&  (!IS_LICENCE_SEP(licence[tlen])))
+                                               || (!IS_LICENCE_SEP(*(found - 1))) 
+                                               || (!IS_LICENCE_SEP(*(found + tlen)))) {
+                                       debug_print("plugin licence check failed: invalid separator\n");
+                                       return FALSE;
+                               }
+                       }
+                       debug_print("plugin licence check passed: %s found\n", plugin_licence_tokens[i]);
+                       return TRUE;
+               }
+               ++i;
+       }
+       debug_print("plugin licence check failed: %s is not a valid licence\n", licence);
+       return FALSE;
+}
+
+static Plugin *plugin_load_in_default_dir(const gchar *filename, gchar **error)
+{
+       Plugin *plugin = NULL;
+       gchar *filename_default_dir = NULL;
+       gchar *default_error = NULL;
+       gchar *plugin_name = g_path_get_basename(filename);
+
+       filename_default_dir = g_strconcat(get_plugin_dir(), plugin_name, NULL);
+
+       debug_print("trying to load %s in default plugin directory %s\n",
+                   plugin_name, get_plugin_dir());
+
+       g_free(plugin_name);
+
+       plugin = plugin_load(filename_default_dir, &default_error);
+       
+       g_free(filename_default_dir);
+       
+       if (plugin) {
+               g_free(*error);
+               *error = NULL;
+               plugin->in_prefix_dir = TRUE;
+
+       } else {
+               g_free(default_error);
+       }
+
+       return plugin;
+}
+
 /**
  * Loads a plugin
  *
@@ -310,8 +423,8 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
        struct PluginFeature *(*plugin_provides)(void);
        gint ok;
        START_TIMING((filename?filename:"NULL plugin"));
-       g_return_val_if_fail(filename != NULL, NULL);
-       g_return_val_if_fail(error != NULL, NULL);
+       cm_return_val_if_fail(filename != NULL, NULL);
+       cm_return_val_if_fail(error != NULL, NULL);
 
        /* check duplicate plugin path name */
        if (plugin_is_loaded(filename)) {
@@ -340,8 +453,13 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
        if (plugin->module == NULL) {
                *error = g_strdup(g_module_error());
                g_free(plugin);
-               return NULL;
-       }
+               if (!plugin_filename_is_standard_dir(filename))
+                       return plugin_load_in_default_dir(filename, error);
+               else
+                       return NULL;
+       } else {
+               plugin->in_prefix_dir = FALSE;
+        }
 
 init_plugin:
        if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
@@ -359,9 +477,8 @@ init_plugin:
                return NULL;
        }
        
-       if (strcmp(plugin_licence(), "GPL2+") && strncmp(plugin_licence(), "GPL3", strlen("GPL3"))
-       &&  strncmp(plugin_licence(), "GPL2+-compatible", strlen("GPL2+-compatible"))) {
-               *error = g_strdup(_("This module is not licensed under a GPL v2 or later compatible license."));
+       if (plugin_licence_check(plugin_licence()) != TRUE) {
+               *error = g_strdup(_("This module is not licensed under a GPL v3 or later compatible license."));
                if (plugin->unloaded_hidden)
                        return NULL;
                g_module_close(plugin->module);
@@ -445,6 +562,28 @@ void plugin_unload(Plugin *plugin)
 
 }
 
+static void replace_old_plugin_name(gchar *plugin_name)
+{
+       gchar *old_name_end = g_strconcat("_plugin.", G_MODULE_SUFFIX, NULL);
+       gchar *matches = strstr(plugin_name, old_name_end);
+
+       if (!matches) {
+               g_free(old_name_end);
+               return;
+       } else if (plugin_name + strlen(plugin_name) != matches + strlen(matches)) {
+               g_free(old_name_end);
+               return;
+       } else {
+               gchar *new_name_end = g_strconcat(".", G_MODULE_SUFFIX, NULL);
+               int offset = strlen(plugin_name) - strlen(old_name_end);
+
+               debug_print("Replacing old plugin name %s\n", plugin_name);
+               
+               strncpy(plugin_name + offset, new_name_end, strlen(old_name_end) - 1);
+               debug_print(" to %s\n", plugin_name);
+       }
+}
+
 void plugin_load_all(const gchar *type)
 {
        gchar *rcpath;
@@ -463,6 +602,8 @@ void plugin_load_all(const gchar *type)
        if ((pfile = prefs_read_open(rcpath)) == NULL ||
            (prefs_set_block_label(pfile, block) < 0)) {
                g_free(rcpath);
+               if (pfile)
+                       prefs_file_close(pfile);
                return;
        }
        g_free(block);
@@ -472,6 +613,8 @@ void plugin_load_all(const gchar *type)
                        break;
 
                g_strstrip(buf);
+               replace_old_plugin_name(buf);
+
                if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
                        g_warning("plugin loading error: %s\n", error);
                        g_free(error);
@@ -563,6 +706,22 @@ GSList *plugin_get_list(void)
        return new;
 }
 
+Plugin *plugin_get_loaded_by_name(const gchar *name) 
+{
+       Plugin *plugin = NULL;
+       GSList *new, *cur; 
+       new = plugin_get_list();
+       for (cur = new; cur; cur = g_slist_next(cur)) {
+               plugin = (Plugin *)cur->data;
+               if (!g_ascii_strcasecmp(plugin->name(), name)) 
+                       break;
+               else 
+                       plugin = NULL;
+       }
+       g_slist_free(new);
+       return plugin;
+}
+
 GSList *plugin_get_unloaded_list(void)
 {
        return g_slist_copy(unloaded_plugins);