"Master password" is now called "master passphrase".
[claws.git] / src / common / plugin.c
index 70e340ceed4e519a241945f89df58e8e8ef60cc5..9822d614e5248b26a24de004411a5d921022ffc9 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws Team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2002-2015 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#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>
@@ -32,6 +39,8 @@
 #include "utils.h"
 #include "plugin.h"
 #include "prefs.h"
+#include "claws.h"
+#include "timing.h"
 
 struct _Plugin
 {
@@ -42,15 +51,56 @@ struct _Plugin
        const gchar *(*version) (void);
        const gchar *(*type) (void);
        const gchar *(*licence) (void);
+       void (*master_passphrase_change) (const gchar *oldp, const gchar *newp);
+       struct PluginFeature *(*provides) (void);
+       
        GSList *rdeps;
+       gchar *error;
+       gboolean unloaded_hidden;
+       gboolean in_prefix_dir;
+};
+
+const gchar *plugin_feature_names[] =
+       { N_("Nothing"),
+         N_("a viewer"),
+         N_("a MIME parser"),
+         N_("folders"),
+         N_("filtering"),
+         N_("a privacy interface"),
+         N_("a notifier"),
+         N_("an utility"),
+         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
  */
 GSList *plugins = NULL;
 GSList *plugin_types = NULL;
 
+/* 
+ * List of plugins unloaded for some fixable reason
+ */
+static GSList *unloaded_plugins = NULL;
+
 static gint list_find_by_string(gconstpointer data, gconstpointer str)
 {
        return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
@@ -58,10 +108,30 @@ static gint list_find_by_string(gconstpointer data, gconstpointer str)
 
 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
 {
-        g_return_val_if_fail(plugin, 1);
-        g_return_val_if_fail(plugin->filename, 1);
-        g_return_val_if_fail(filename, 1);
-        return strcmp(filename, plugin->filename);
+       /* FIXME: There is a problem in case of symlinks or when a
+          user tries to load a plugin with the same name from a
+          different directory.  I think it would be better to compare
+          only the basename of the filename here (case-insensitive on
+          W32). */
+       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)
@@ -70,13 +140,17 @@ void plugin_save_list(void)
        PrefFile *pfile;
        GSList *type_cur, *plugin_cur;
        Plugin *plugin;
-
+       
        for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
                rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
+#ifdef G_OS_WIN32
+               block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
+#else
                block = g_strconcat("Plugins_", type_cur->data, NULL);
+#endif
                if ((pfile = prefs_write_open(rcpath)) == NULL ||
                    (prefs_set_block_label(pfile, block) < 0)) {
-                       g_warning("failed to write plugin list\n");
+                       g_warning("failed to write plugin list");
                        g_free(rcpath);
                        return;
                }
@@ -85,13 +159,45 @@ void plugin_save_list(void)
                for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
                        plugin = (Plugin *) plugin_cur->data;
                        
-                       if (!strcmp(plugin->type(), type_cur->data))
-                               fprintf(pfile->fp, "%s\n", plugin->filename);
+                       if (plugin->unloaded_hidden)
+                               continue;
+
+                       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;
+
+                       if (plugin->unloaded_hidden)
+                               continue;
+                       
+                       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;
+                       }
                }
-               fprintf(pfile->fp, "\n");
+               if (fprintf(pfile->fp, "\n") < 0)
+                       goto revert;
 
                if (prefs_file_close(pfile) < 0)
-                       g_warning("failed to write plugin list\n");
+                       g_warning("failed to write plugin list");
+
+               g_free(rcpath); 
+               
+               continue;
+
+revert:
+               g_warning("failed to write plugin list");
+               if (prefs_file_close_revert(pfile) < 0)
+                       g_warning("failed to revert plugin list");
 
                g_free(rcpath); 
        }
@@ -100,7 +206,7 @@ void plugin_save_list(void)
 static gboolean plugin_is_loaded(const gchar *filename)
 {
        return (g_slist_find_custom(plugins, filename, 
-                  (GCompareFunc)list_find_by_plugin_filename) != NULL);
+                 (GCompareFunc)list_find_by_plugin_filename) != NULL);
 }
 
 static Plugin *plugin_get_by_filename(const gchar *filename)
@@ -130,9 +236,11 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
        gchar *deps_file = NULL;
        FILE *fp = NULL;
        gchar buf[BUFFSIZE];
+       gchar *p;
 
-        tmp = g_strdup(filename);
-       *strrchr(tmp, '.') = '\0';
+       tmp = g_strdup(filename);
+       if( (p = strrchr(tmp, '.')) )
+         *p = '\0';
        deps_file = g_strconcat(tmp, ".deps", NULL);
        g_free(tmp);
        
@@ -153,8 +261,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, 
@@ -188,6 +298,113 @@ static void plugin_unload_rdeps(Plugin *plugin)
        g_slist_free(plugin->rdeps);
        plugin->rdeps = NULL;
 }
+
+static void plugin_remove_from_unloaded_list (const gchar *filename)
+{
+       GSList *item = g_slist_find_custom(unloaded_plugins, 
+                               (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
+       Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
+       if (unloaded_plugin != NULL) {
+               debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
+               unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
+               g_module_close(unloaded_plugin->module);
+               g_free(unloaded_plugin->filename);
+               g_free(unloaded_plugin->error);
+               g_free(unloaded_plugin);
+       }
+}
+
+static gchar *plugin_check_features(struct PluginFeature *features) {
+       int i = 0, j = 0;
+       GSList *cur = plugins;
+
+       if (features == NULL)
+               return NULL;
+       for(; cur; cur = cur->next) {
+               Plugin *p = (Plugin *)cur->data;
+               struct PluginFeature *cur_features = p->provides();
+               if (p->unloaded_hidden)
+                       continue;
+               for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
+                       for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
+                               if (cur_features[j].type == features[i].type &&
+                                   !strcmp(cur_features[j].subtype, features[i].subtype)) {
+                                       return g_strdup_printf(_(
+                                               "This plugin provides %s (%s), which is "
+                                               "already provided by the %s plugin."),
+                                               _(plugin_feature_names[features[i].type]), 
+                                               _(features[i].subtype),
+                                               p->name());
+                               }
+                       }
+               }
+       }
+
+       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");
+               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
  *
@@ -202,16 +419,27 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
        gpointer plugin_name, plugin_desc, plugin_version;
        const gchar *(*plugin_type)(void);
        const gchar *(*plugin_licence)(void);
-       gint ok;
+       struct PluginFeature *(*plugin_provides)(void);
+       void (*plugin_master_passphrase_change) (const gchar *oldp, const gchar *newp) = NULL;
 
-       g_return_val_if_fail(filename != NULL, NULL);
-       g_return_val_if_fail(error != NULL, NULL);
+       gint ok;
+       START_TIMING((filename?filename:"NULL plugin"));
+       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)) {
-               *error = g_strdup(_("Plugin already loaded"));
-               return NULL;                
-       }                               
+               plugin = plugin_get_by_filename(filename);
+               if (plugin->unloaded_hidden) {
+                       /* reshow it */
+                       goto init_plugin;
+               } else {
+                       *error = g_strdup(_("Plugin already loaded"));
+                       return NULL;            
+               }
+       }                              
+       
+       plugin_remove_from_unloaded_list(filename);
        
        if (plugin_load_deps(filename, error) < 0)
                return NULL;
@@ -221,75 +449,144 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
                return NULL;
        }
 
-        debug_print("trying to load `%s'\n", filename);
+       debug_print("trying to load `%s'\n", filename);
        plugin->module = g_module_open(filename, 0);
        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) ||
            !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
            !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
            !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
            !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
+           !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
            !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
                *error = g_strdup(g_module_error());
+               if (plugin->unloaded_hidden)
+                       return NULL;
                g_module_close(plugin->module);
                g_free(plugin);
                return NULL;
        }
+
+       /* Optional methods */
+       g_module_symbol(plugin->module, "plugin_master_passphrase_change", (gpointer)&plugin_master_passphrase_change);
        
-       if (strcmp(plugin_licence(), "GPL")
-       &&  strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
-               *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
+       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);
                g_free(plugin);
                return NULL;
        }
 
        if (!strcmp(plugin_type(), "GTK")) {
-               *error = g_strdup(_("This module is for Sylpheed-Claws GTK1."));
+               *error = g_strdup(_("This module is for Claws Mail GTK1."));
+               if (plugin->unloaded_hidden)
+                       return NULL;
                g_module_close(plugin->module);
                g_free(plugin);
                return NULL;
        }
 
-       if ((ok = plugin_init(error)) < 0) {
+       if ((*error = plugin_check_features(plugin_provides())) != NULL) {
+               if (plugin->unloaded_hidden)
+                       return NULL;
                g_module_close(plugin->module);
                g_free(plugin);
                return NULL;
        }
-
        plugin->name = plugin_name;
        plugin->desc = plugin_desc;
        plugin->version = plugin_version;
        plugin->type = plugin_type;
        plugin->licence = plugin_licence;
+       plugin->provides = plugin_provides;
+       plugin->master_passphrase_change = plugin_master_passphrase_change;
        plugin->filename = g_strdup(filename);
+       plugin->error = NULL;
 
-       plugins = g_slist_append(plugins, plugin);
+       if ((ok = plugin_init(error)) < 0) {
+               if (*error)
+                       plugin->error = g_strdup(*error);
+               unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
+               return NULL;
+       }
 
-       debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
+       if (!plugin->unloaded_hidden)
+               plugins = g_slist_append(plugins, plugin);
+       plugin->unloaded_hidden = FALSE;
 
+       debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
+       END_TIMING();
        return plugin;
 }
 
 void plugin_unload(Plugin *plugin)
 {
-       void (*plugin_done) (void);
+       gboolean (*plugin_done) (void);
+       gboolean can_unload = TRUE;
 
        plugin_unload_rdeps(plugin);
 
+       if (plugin->unloaded_hidden)
+               return;
+
+       if (plugin->error) {
+               plugin_remove_from_unloaded_list(plugin->filename);
+               return;
+       }
        if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
-               plugin_done();
+               can_unload = plugin_done();
        }
 
-       g_module_close(plugin->module);
-       plugins = g_slist_remove(plugins, plugin);
-       g_free(plugin->filename);
-       g_free(plugin);
+       if (can_unload) {
+#ifdef HAVE_VALGRIND
+               if (!RUNNING_ON_VALGRIND) {
+                       g_module_close(plugin->module);
+               }
+#else
+               g_module_close(plugin->module);
+#endif
+               plugins = g_slist_remove(plugins, plugin);
+               g_free(plugin->filename);
+               g_free(plugin);
+       } else {
+               plugin->unloaded_hidden = TRUE;
+       }
+
+}
+
+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)
@@ -297,15 +594,21 @@ void plugin_load_all(const gchar *type)
        gchar *rcpath;
        gchar buf[BUFFSIZE];
        PrefFile *pfile;
-       gchar *error, *block;
+       gchar *error = NULL, *block;
 
        plugin_types = g_slist_append(plugin_types, g_strdup(type));
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); 
+#ifdef G_OS_WIN32
+       block = g_strconcat("PluginsWin32_", type, NULL);
+#else
        block = g_strconcat("Plugins_", type, NULL);
+#endif
        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);
@@ -315,8 +618,10 @@ 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_warning("plugin loading error: %s", error);
                        g_free(error);
                }                                                       
        }
@@ -343,13 +648,88 @@ void plugin_unload_all(const gchar *type)
        cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
        if (cur) {
                g_free(cur->data);
-               g_slist_remove(plugin_types, cur);
+               plugin_types = g_slist_remove(plugin_types, cur);
+       }
+}
+
+
+/* Load those plugins we always want to use.  No error output; just
+ * try. */
+void plugin_load_standard_plugins (void)
+{
+       static const char *names[] = {
+#ifdef G_OS_WIN32 
+               "pgpmime",
+               "pgpinline",
+#else
+               /* post-2.5 maybe 
+               "bogofilter", */
+#endif
+               NULL
+       };
+       int i;
+       gchar *error, *filename;
+       
+       for (i=0; names[i]; i++) {
+               /* Simple hack to check whether the plugin has already
+                * been loaded but checking only for the basename. */
+               GSList *cur = plugins;
+               for(; cur; cur = cur->next) {
+                       Plugin *p = (Plugin *)cur->data;
+                       if (strstr(p->filename, names[i]))
+                               break;
+               }
+               if (!cur) { /* Not yet loaded. */
+                       /* FIXME: get_plugin_dir () returns with a trailing
+                        * (back)slash; this should be fixed so that we can use
+                        * g_module_build_path here. */
+#ifdef G_OS_WIN32 
+                       filename = g_strconcat (get_plugin_dir(),
+                                               names[i], NULL);
+#else
+                       filename = g_strconcat (get_plugin_dir(),
+                                               names[i], ".", G_MODULE_SUFFIX, NULL);
+#endif
+                       error = NULL;
+                       plugin_load(filename, &error);
+                       g_free (error);
+                       g_free(filename);
+               }
        }
 }
 
 GSList *plugin_get_list(void)
 {
-       return g_slist_copy(plugins);
+       GSList *new = NULL;
+       GSList *cur = plugins;
+       for (; cur; cur = cur->next) {
+               Plugin *p = (Plugin *)cur->data;
+               if (!p->unloaded_hidden)
+                       new = g_slist_prepend(new, p);
+       }
+       new = g_slist_reverse(new);
+       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);
 }
 
 const gchar *plugin_get_name(Plugin *plugin)
@@ -366,3 +746,62 @@ const gchar *plugin_get_version(Plugin *plugin)
 {
        return plugin->version();
 }
+
+const gchar *plugin_get_error(Plugin *plugin)
+{
+       return plugin->error;
+}
+
+void plugins_master_passphrase_change(const gchar *oldp, const gchar *newp) {
+       Plugin *plugin = NULL;
+       GSList *cur;
+       for (cur = plugin_get_list(); cur; cur = g_slist_next(cur)) {
+               plugin = (Plugin *)cur->data;
+               if (plugin->master_passphrase_change != NULL) {
+                       plugin->master_passphrase_change(oldp, newp);
+               }
+       }
+}
+
+/* Generally called in plugin_init() function of each plugin. It check the
+ * minimal and compiled version of claws binary required by the plugin.
+ * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
+ * check the corresponding version.
+ *
+ * If an error occurs {
+ *     If @error == NULL { don't allocate error string. }
+ *     If @error != NULL { error string is allocated and must be freed after 
+ *                             call function. }
+ * }
+ * Returns: FALSE if an error occurs, TRUE if all is OK.
+ */
+gint check_plugin_version(guint32 minimum_claws_version,
+                        guint32 compiled_claws_version,
+                        const gchar *plugin_name,
+                        gchar **error)
+{
+       guint32 claws_version = claws_get_version();
+
+       if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
+               if (error != NULL) {
+                       *error = (plugin_name && *plugin_name)
+                               ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
+                                                       "version the '%s' plugin was built with."),
+                                               plugin_name)
+                               : g_strdup(_("Your version of Claws Mail is newer than the "
+                                                       "version the plugin was built with."));
+               }
+               return FALSE;
+       }
+
+       if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
+               if (error != NULL) {
+                       *error = (plugin_name && *plugin_name)
+                               ? g_strdup_printf(_("Your version of Claws Mail is too old for "
+                                                       "the '%s' plugin."), plugin_name)
+                               : g_strdup(_("Your version of Claws Mail is too old for the plugin."));
+               }
+               return FALSE;
+       }
+       return TRUE;
+}