2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "claws-features.h"
34 #include <glib/gi18n.h>
51 const gchar *(*name) (void);
52 const gchar *(*desc) (void);
53 const gchar *(*version) (void);
54 const gchar *(*type) (void);
55 const gchar *(*licence) (void);
56 struct PluginFeature *(*provides) (void);
60 gboolean unloaded_hidden;
63 const gchar *plugin_feature_names[] =
69 N_("a privacy interface"),
75 /* The plugin must be at least under one of these licences and have
76 the corresponding token returned by the plugin_licence function.
78 const gchar const *plugin_licence_tokens[] = {
79 "LGPL2.1+", "LGPLv2.1+", "LGPL2.1", "LGPLv2.1",
80 "LGPL3+", "LGPLv3+", "LGPL3", "LGPLv3",
81 "GPL3+", "GPLv3+", "GPL3", "GPLv3",
83 "Apache2.0", "Apache 2.0", "Apache v2.0",
84 "2-clause BSD", "Simplified BSD", "FreeBSD",
85 "3-clause BSD", "New BSD", "Modified BSD",
89 /* Dual (or more) licences are allowed, must be separated by one of these.
91 #define IS_LICENCE_SEP(a) ((a) == ',' || (a) == ';' || (a) == '|' || (a) == '/' || (a) == '\0')
94 * List of all loaded plugins
96 GSList *plugins = NULL;
97 GSList *plugin_types = NULL;
100 * List of plugins unloaded for some fixable reason
102 static GSList *unloaded_plugins = NULL;
104 static gint list_find_by_string(gconstpointer data, gconstpointer str)
106 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
109 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
111 /* FIXME: There is a problem in case of symlinks or when a
112 user tries to load a plugin with the same name from a
113 different directory. I think it would be better to compare
114 only the basename of the filename here (case-insensitive on
116 cm_return_val_if_fail(plugin, 1);
117 cm_return_val_if_fail(plugin->filename, 1);
118 cm_return_val_if_fail(filename, 1);
119 return strcmp(filename, plugin->filename);
122 void plugin_save_list(void)
124 gchar *rcpath, *block;
126 GSList *type_cur, *plugin_cur;
129 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
130 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
132 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
134 block = g_strconcat("Plugins_", type_cur->data, NULL);
136 if ((pfile = prefs_write_open(rcpath)) == NULL ||
137 (prefs_set_block_label(pfile, block) < 0)) {
138 g_warning("failed to write plugin list\n");
144 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
145 plugin = (Plugin *) plugin_cur->data;
147 if (plugin->unloaded_hidden)
150 if (!strcmp(plugin->type(), type_cur->data))
151 if (fprintf(pfile->fp, "%s\n", plugin->filename) < 0)
154 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
155 plugin = (Plugin *) plugin_cur->data;
157 if (plugin->unloaded_hidden)
160 if (!strcmp(plugin->type(), type_cur->data))
161 if (fprintf(pfile->fp, "%s\n", plugin->filename) < 0)
164 if (fprintf(pfile->fp, "\n") < 0)
167 if (prefs_file_close(pfile) < 0)
168 g_warning("failed to write plugin list\n");
175 g_warning("failed to write plugin list\n");
176 if (prefs_file_close_revert(pfile) < 0)
177 g_warning("failed to revert plugin list\n");
183 static gboolean plugin_is_loaded(const gchar *filename)
185 return (g_slist_find_custom(plugins, filename,
186 (GCompareFunc)list_find_by_plugin_filename) != NULL);
189 static Plugin *plugin_get_by_filename(const gchar *filename)
191 GSList *cur = plugins;
192 for(; cur; cur = cur->next) {
193 Plugin *p = (Plugin *)cur->data;
194 if (!strcmp(p->filename, filename)) {
202 * Loads a plugin dependancies
204 * Plugin dependancies are, optionnaly, listed in a file in
205 * get_plugin_dir()/$pluginname.deps.
206 * \param filename The filename of the plugin for which we have to load deps
207 * \param error The location where an error string can be stored
208 * \return 0 on success, -1 otherwise
210 static gint plugin_load_deps(const gchar *filename, gchar **error)
213 gchar *deps_file = NULL;
218 tmp = g_strdup(filename);
219 if( (p = strrchr(tmp, '.')) )
221 deps_file = g_strconcat(tmp, ".deps", NULL);
224 fp = g_fopen(deps_file, "rb");
230 while (fgets(buf, sizeof(buf), fp) != NULL) {
231 Plugin *dep_plugin = NULL;
233 buf[strlen(buf)-1]='\0'; /* chop off \n */
234 path = g_strconcat(get_plugin_dir(), buf,
235 ".", G_MODULE_SUFFIX, NULL);
236 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
237 debug_print("trying to load %s\n", path);
238 dep_plugin = plugin_load(path, error);
239 if (dep_plugin == NULL) {
246 if (!g_slist_find_custom(dep_plugin->rdeps,
247 (gpointer) filename, list_find_by_string)) {
248 debug_print("adding %s to %s rdeps\n",
250 dep_plugin->filename);
252 g_slist_append(dep_plugin->rdeps,
260 static void plugin_unload_rdeps(Plugin *plugin)
262 GSList *cur = plugin->rdeps;
263 debug_print("removing %s rdeps\n", plugin->filename);
265 gchar *file = (gchar *)cur->data;
266 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
267 debug_print(" rdep %s: %p\n", file, rdep_plugin);
269 plugin_unload(rdep_plugin);
274 g_slist_free(plugin->rdeps);
275 plugin->rdeps = NULL;
278 static void plugin_remove_from_unloaded_list (const gchar *filename)
280 GSList *item = g_slist_find_custom(unloaded_plugins,
281 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
282 Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
283 if (unloaded_plugin != NULL) {
284 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
285 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
286 g_module_close(unloaded_plugin->module);
287 g_free(unloaded_plugin->filename);
288 g_free(unloaded_plugin->error);
289 g_free(unloaded_plugin);
293 static gchar *plugin_check_features(struct PluginFeature *features) {
295 GSList *cur = plugins;
297 if (features == NULL)
299 for(; cur; cur = cur->next) {
300 Plugin *p = (Plugin *)cur->data;
301 struct PluginFeature *cur_features = p->provides();
302 if (p->unloaded_hidden)
304 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
305 for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
306 if (cur_features[j].type == features[i].type &&
307 !strcmp(cur_features[j].subtype, features[i].subtype)) {
308 return g_strdup_printf(_(
309 "This plugin provides %s (%s), which is "
310 "already provided by the %s plugin."),
311 _(plugin_feature_names[features[i].type]),
312 _(features[i].subtype),
322 static gboolean plugin_licence_check(const gchar *licence) {
326 if (licence != NULL) {
327 len = strlen(licence);
330 g_warning("plugin licence check failed: empty licence\n");
333 while (plugin_licence_tokens[i] != NULL) {
334 gchar *found = g_strstr_len(licence, len, plugin_licence_tokens[i]);
336 gint tlen = strlen(plugin_licence_tokens[i]);
337 if (len != tlen) { /* not a single license */
338 if (((found == licence) && (!IS_LICENCE_SEP(licence[tlen])))
339 || (!IS_LICENCE_SEP(*(found - 1)))
340 || (!IS_LICENCE_SEP(*(found + tlen)))) {
341 debug_print("plugin licence check failed: invalid separator\n");
345 debug_print("plugin licence check passed: %s found\n", plugin_licence_tokens[i]);
350 debug_print("plugin licence check failed: %s is not a valid licence\n", licence);
354 static gboolean plugin_filename_is_standard_dir(const gchar *filename) {
355 return strncmp(filename, get_plugin_dir(), strlen(get_plugin_dir())) == 0;
358 static Plugin *plugin_load_in_default_dir(const gchar *filename, gchar **error)
360 Plugin *plugin = NULL;
361 gchar *filename_default_dir = NULL;
362 gchar *default_error = NULL;
363 gchar *plugin_name = g_path_get_basename(filename);
365 filename_default_dir = g_strconcat(get_plugin_dir(), plugin_name, NULL);
367 debug_print("trying to load %s in default plugin directory %s\n",
368 plugin_name, get_plugin_dir());
372 plugin = plugin_load(filename_default_dir, &default_error);
374 g_free(filename_default_dir);
380 g_free(default_error);
389 * \param filename The filename of the plugin to load
390 * \param error The location where an error string can be stored
391 * \return the plugin on success, NULL otherwise
393 Plugin *plugin_load(const gchar *filename, gchar **error)
396 gint (*plugin_init) (gchar **error);
397 gpointer plugin_name, plugin_desc, plugin_version;
398 const gchar *(*plugin_type)(void);
399 const gchar *(*plugin_licence)(void);
400 struct PluginFeature *(*plugin_provides)(void);
402 START_TIMING((filename?filename:"NULL plugin"));
403 cm_return_val_if_fail(filename != NULL, NULL);
404 cm_return_val_if_fail(error != NULL, NULL);
406 /* check duplicate plugin path name */
407 if (plugin_is_loaded(filename)) {
408 plugin = plugin_get_by_filename(filename);
409 if (plugin->unloaded_hidden) {
413 *error = g_strdup(_("Plugin already loaded"));
418 plugin_remove_from_unloaded_list(filename);
420 if (plugin_load_deps(filename, error) < 0)
422 plugin = g_new0(Plugin, 1);
423 if (plugin == NULL) {
424 *error = g_strdup(_("Failed to allocate memory for Plugin"));
428 debug_print("trying to load `%s'\n", filename);
429 plugin->module = g_module_open(filename, 0);
430 if (plugin->module == NULL) {
431 *error = g_strdup(g_module_error());
433 if (!plugin_filename_is_standard_dir(filename))
434 return plugin_load_in_default_dir(filename, error);
440 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
441 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
442 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
443 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
444 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
445 !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
446 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
447 *error = g_strdup(g_module_error());
448 if (plugin->unloaded_hidden)
450 g_module_close(plugin->module);
455 if (plugin_licence_check(plugin_licence()) != TRUE) {
456 *error = g_strdup(_("This module is not licensed under a GPL v3 or later compatible license."));
457 if (plugin->unloaded_hidden)
459 g_module_close(plugin->module);
464 if (!strcmp(plugin_type(), "GTK")) {
465 *error = g_strdup(_("This module is for Claws Mail GTK1."));
466 if (plugin->unloaded_hidden)
468 g_module_close(plugin->module);
473 if ((*error = plugin_check_features(plugin_provides())) != NULL) {
474 if (plugin->unloaded_hidden)
476 g_module_close(plugin->module);
480 plugin->name = plugin_name;
481 plugin->desc = plugin_desc;
482 plugin->version = plugin_version;
483 plugin->type = plugin_type;
484 plugin->licence = plugin_licence;
485 plugin->provides = plugin_provides;
486 plugin->filename = g_strdup(filename);
487 plugin->error = NULL;
489 if ((ok = plugin_init(error)) < 0) {
491 plugin->error = g_strdup(*error);
492 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
496 if (!plugin->unloaded_hidden)
497 plugins = g_slist_append(plugins, plugin);
498 plugin->unloaded_hidden = FALSE;
500 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
505 void plugin_unload(Plugin *plugin)
507 gboolean (*plugin_done) (void);
508 gboolean can_unload = TRUE;
510 plugin_unload_rdeps(plugin);
512 if (plugin->unloaded_hidden)
516 plugin_remove_from_unloaded_list(plugin->filename);
519 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
520 can_unload = plugin_done();
525 if (!RUNNING_ON_VALGRIND) {
526 g_module_close(plugin->module);
529 g_module_close(plugin->module);
531 plugins = g_slist_remove(plugins, plugin);
532 g_free(plugin->filename);
535 plugin->unloaded_hidden = TRUE;
540 void plugin_load_all(const gchar *type)
545 gchar *error = NULL, *block;
547 plugin_types = g_slist_append(plugin_types, g_strdup(type));
549 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
551 block = g_strconcat("PluginsWin32_", type, NULL);
553 block = g_strconcat("Plugins_", type, NULL);
555 if ((pfile = prefs_read_open(rcpath)) == NULL ||
556 (prefs_set_block_label(pfile, block) < 0)) {
559 prefs_file_close(pfile);
564 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
569 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
570 g_warning("plugin loading error: %s\n", error);
574 prefs_file_close(pfile);
579 void plugin_unload_all(const gchar *type)
583 list = g_slist_copy(plugins);
584 list = g_slist_reverse(list);
586 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
587 Plugin *plugin = (Plugin *) cur->data;
589 if (!strcmp(type, plugin->type()))
590 plugin_unload(plugin);
594 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
597 plugin_types = g_slist_remove(plugin_types, cur);
602 /* Load those plugins we always want to use. No error output; just
604 void plugin_load_standard_plugins (void)
606 static const char *names[] = {
617 gchar *error, *filename;
619 for (i=0; names[i]; i++) {
620 /* Simple hack to check whether the plugin has already
621 * been loaded but checking only for the basename. */
622 GSList *cur = plugins;
623 for(; cur; cur = cur->next) {
624 Plugin *p = (Plugin *)cur->data;
625 if (strstr(p->filename, names[i]))
628 if (!cur) { /* Not yet loaded. */
629 /* FIXME: get_plugin_dir () returns with a trailing
630 * (back)slash; this should be fixed so that we can use
631 * g_module_build_path here. */
633 filename = g_strconcat (get_plugin_dir(),
636 filename = g_strconcat (get_plugin_dir(),
637 names[i], ".", G_MODULE_SUFFIX, NULL);
640 plugin_load(filename, &error);
647 GSList *plugin_get_list(void)
650 GSList *cur = plugins;
651 for (; cur; cur = cur->next) {
652 Plugin *p = (Plugin *)cur->data;
653 if (!p->unloaded_hidden)
654 new = g_slist_prepend(new, p);
656 new = g_slist_reverse(new);
660 Plugin *plugin_get_loaded_by_name(const gchar *name)
662 Plugin *plugin = NULL;
664 new = plugin_get_list();
665 for (cur = new; cur; cur = g_slist_next(cur)) {
666 plugin = (Plugin *)cur->data;
667 if (!g_ascii_strcasecmp(plugin->name(), name))
676 GSList *plugin_get_unloaded_list(void)
678 return g_slist_copy(unloaded_plugins);
681 const gchar *plugin_get_name(Plugin *plugin)
683 return plugin->name();
686 const gchar *plugin_get_desc(Plugin *plugin)
688 return plugin->desc();
691 const gchar *plugin_get_version(Plugin *plugin)
693 return plugin->version();
696 const gchar *plugin_get_error(Plugin *plugin)
698 return plugin->error;
701 /* Generally called in plugin_init() function of each plugin. It check the
702 * minimal and compiled version of claws binary required by the plugin.
703 * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
704 * check the corresponding version.
706 * If an error occurs {
707 * If @error == NULL { don't allocate error string. }
708 * If @error != NULL { error string is allocated and must be freed after
711 * Returns: FALSE if an error occurs, TRUE if all is OK.
713 gint check_plugin_version(guint32 minimum_claws_version,
714 guint32 compiled_claws_version,
715 const gchar *plugin_name,
718 guint32 claws_version = claws_get_version();
720 if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
722 *error = (plugin_name && *plugin_name)
723 ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
724 "version the '%s' plugin was built with."),
726 : g_strdup(_("Your version of Claws Mail is newer than the "
727 "version the plugin was built with."));
732 if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
734 *error = (plugin_name && *plugin_name)
735 ? g_strdup_printf(_("Your version of Claws Mail is too old for "
736 "the '%s' plugin."), plugin_name)
737 : g_strdup(_("Your version of Claws Mail is too old for the plugin."));