"Master password" is now called "master passphrase".
[claws.git] / src / common / plugin.c
index dd53ef63f1e39438509be53d88eba70da3b50f34..9822d614e5248b26a24de004411a5d921022ffc9 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
-
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
 #include "claws-features.h"
@@ -53,11 +51,13 @@ 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[] =
@@ -119,6 +119,21 @@ static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *file
        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;
@@ -135,7 +150,7 @@ void plugin_save_list(void)
 #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;
                }
@@ -147,9 +162,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;
@@ -157,24 +176,28 @@ 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;
 
                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\n");
+               g_warning("failed to write plugin list");
                if (prefs_file_close_revert(pfile) < 0)
-                       g_warning("failed to revert plugin list\n");
+                       g_warning("failed to revert plugin list");
 
                g_free(rcpath); 
        }
@@ -241,6 +264,7 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
                                fclose(fp);
                                return -1;
                        }
+                       dep_plugin->in_prefix_dir = TRUE;
                }
                g_free(path);
                if (!g_slist_find_custom(dep_plugin->rdeps, 
@@ -327,7 +351,7 @@ static gboolean plugin_licence_check(const gchar *licence) {
                len = strlen(licence);
        }
        if (len == 0) {
-               g_warning("plugin licence check failed: empty licence\n");
+               g_warning("plugin licence check failed: empty licence");
                return FALSE;
        }
        while (plugin_licence_tokens[i] != NULL) {
@@ -351,6 +375,36 @@ static gboolean plugin_licence_check(const gchar *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
  *
@@ -366,6 +420,8 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
        const gchar *(*plugin_type)(void);
        const gchar *(*plugin_licence)(void);
        struct PluginFeature *(*plugin_provides)(void);
+       void (*plugin_master_passphrase_change) (const gchar *oldp, const gchar *newp) = NULL;
+
        gint ok;
        START_TIMING((filename?filename:"NULL plugin"));
        cm_return_val_if_fail(filename != NULL, NULL);
@@ -398,8 +454,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) ||
@@ -416,6 +477,9 @@ init_plugin:
                g_free(plugin);
                return NULL;
        }
+
+       /* Optional methods */
+       g_module_symbol(plugin->module, "plugin_master_passphrase_change", (gpointer)&plugin_master_passphrase_change);
        
        if (plugin_licence_check(plugin_licence()) != TRUE) {
                *error = g_strdup(_("This module is not licensed under a GPL v3 or later compatible license."));
@@ -448,6 +512,7 @@ init_plugin:
        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;
 
@@ -502,6 +567,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;
@@ -531,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);
                }                                                       
        }
@@ -663,6 +752,17 @@ 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