2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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/>.
33 #include <glib/gi18n.h>
50 const gchar *(*name) (void);
51 const gchar *(*desc) (void);
52 const gchar *(*version) (void);
53 const gchar *(*type) (void);
54 const gchar *(*licence) (void);
55 struct PluginFeature *(*provides) (void);
59 gboolean unloaded_hidden;
62 const gchar *plugin_feature_names[] =
68 N_("a privacy interface"),
74 /* The plugin must be at least under one of these licences and have
75 the corresponding token returned by the plugin_licence function.
77 const gchar const *plugin_licence_tokens[] = {
78 "LGPL2.1+", "LGPLv2.1+", "LGPL2.1", "LGPLv2.1",
79 "LGPL3+", "LGPLv3+", "LGPL3", "LGPLv3",
80 "GPL3+", "GPLv3+", "GPL3", "GPLv3",
82 "Apache2.0", "Apache 2.0", "Apache v2.0",
86 /* Dual (or more) licences are allowed, must be separated by one of these.
88 #define IS_LICENCE_SEP(a) ((a) == ',' || (a) == ';' || (a) == '|' || (a) == '/' || (a) == '\0')
91 * List of all loaded plugins
93 GSList *plugins = NULL;
94 GSList *plugin_types = NULL;
97 * List of plugins unloaded for some fixable reason
99 static GSList *unloaded_plugins = NULL;
101 static gint list_find_by_string(gconstpointer data, gconstpointer str)
103 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
106 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
108 /* FIXME: There is a problem in case of symlinks or when a
109 user tries to load a plugin with the same name from a
110 different directory. I think it would be better to compare
111 only the basename of the filename here (case-insensitive on
113 cm_return_val_if_fail(plugin, 1);
114 cm_return_val_if_fail(plugin->filename, 1);
115 cm_return_val_if_fail(filename, 1);
116 return strcmp(filename, plugin->filename);
119 void plugin_save_list(void)
121 gchar *rcpath, *block;
123 GSList *type_cur, *plugin_cur;
126 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
127 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
129 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
131 block = g_strconcat("Plugins_", type_cur->data, NULL);
133 if ((pfile = prefs_write_open(rcpath)) == NULL ||
134 (prefs_set_block_label(pfile, block) < 0)) {
135 g_warning("failed to write plugin list\n");
141 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
142 plugin = (Plugin *) plugin_cur->data;
144 if (plugin->unloaded_hidden)
147 if (!strcmp(plugin->type(), type_cur->data))
148 if (fprintf(pfile->fp, "%s\n", plugin->filename) < 0)
151 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
152 plugin = (Plugin *) plugin_cur->data;
154 if (plugin->unloaded_hidden)
157 if (!strcmp(plugin->type(), type_cur->data))
158 if (fprintf(pfile->fp, "%s\n", plugin->filename) < 0)
161 if (fprintf(pfile->fp, "\n") < 0)
164 if (prefs_file_close(pfile) < 0)
165 g_warning("failed to write plugin list\n");
172 g_warning("failed to write plugin list\n");
173 if (prefs_file_close_revert(pfile) < 0)
174 g_warning("failed to revert plugin list\n");
180 static gboolean plugin_is_loaded(const gchar *filename)
182 return (g_slist_find_custom(plugins, filename,
183 (GCompareFunc)list_find_by_plugin_filename) != NULL);
186 static Plugin *plugin_get_by_filename(const gchar *filename)
188 GSList *cur = plugins;
189 for(; cur; cur = cur->next) {
190 Plugin *p = (Plugin *)cur->data;
191 if (!strcmp(p->filename, filename)) {
199 * Loads a plugin dependancies
201 * Plugin dependancies are, optionnaly, listed in a file in
202 * get_plugin_dir()/$pluginname.deps.
203 * \param filename The filename of the plugin for which we have to load deps
204 * \param error The location where an error string can be stored
205 * \return 0 on success, -1 otherwise
207 static gint plugin_load_deps(const gchar *filename, gchar **error)
210 gchar *deps_file = NULL;
215 tmp = g_strdup(filename);
216 if( (p = strrchr(tmp, '.')) )
218 deps_file = g_strconcat(tmp, ".deps", NULL);
221 fp = g_fopen(deps_file, "rb");
227 while (fgets(buf, sizeof(buf), fp) != NULL) {
228 Plugin *dep_plugin = NULL;
230 buf[strlen(buf)-1]='\0'; /* chop off \n */
231 path = g_strconcat(get_plugin_dir(), buf,
232 ".", G_MODULE_SUFFIX, NULL);
233 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
234 debug_print("trying to load %s\n", path);
235 dep_plugin = plugin_load(path, error);
236 if (dep_plugin == NULL) {
243 if (!g_slist_find_custom(dep_plugin->rdeps,
244 (gpointer) filename, list_find_by_string)) {
245 debug_print("adding %s to %s rdeps\n",
247 dep_plugin->filename);
249 g_slist_append(dep_plugin->rdeps,
257 static void plugin_unload_rdeps(Plugin *plugin)
259 GSList *cur = plugin->rdeps;
260 debug_print("removing %s rdeps\n", plugin->filename);
262 gchar *file = (gchar *)cur->data;
263 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
264 debug_print(" rdep %s: %p\n", file, rdep_plugin);
266 plugin_unload(rdep_plugin);
271 g_slist_free(plugin->rdeps);
272 plugin->rdeps = NULL;
275 static void plugin_remove_from_unloaded_list (const gchar *filename)
277 GSList *item = g_slist_find_custom(unloaded_plugins,
278 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
279 Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
280 if (unloaded_plugin != NULL) {
281 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
282 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
283 g_module_close(unloaded_plugin->module);
284 g_free(unloaded_plugin->filename);
285 g_free(unloaded_plugin->error);
286 g_free(unloaded_plugin);
290 static gchar *plugin_check_features(struct PluginFeature *features) {
292 GSList *cur = plugins;
294 if (features == NULL)
296 for(; cur; cur = cur->next) {
297 Plugin *p = (Plugin *)cur->data;
298 struct PluginFeature *cur_features = p->provides();
299 if (p->unloaded_hidden)
301 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
302 for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
303 if (cur_features[j].type == features[i].type &&
304 !strcmp(cur_features[j].subtype, features[i].subtype)) {
305 return g_strdup_printf(_(
306 "This plugin provides %s (%s), which is "
307 "already provided by the %s plugin."),
308 _(plugin_feature_names[features[i].type]),
309 _(features[i].subtype),
319 static gboolean plugin_licence_check(const gchar *licence) {
323 if (licence != NULL) {
324 len = strlen(licence);
327 g_warning("plugin licence check failed: empty licence\n");
330 while (plugin_licence_tokens[i] != NULL) {
331 gchar *found = g_strstr_len(licence, len, plugin_licence_tokens[i]);
333 gint tlen = strlen(plugin_licence_tokens[i]);
334 if (len != tlen) { /* not a single license */
335 if (((found == licence) && (!IS_LICENCE_SEP(licence[tlen])))
336 || (!IS_LICENCE_SEP(*(found - 1)))
337 || (!IS_LICENCE_SEP(*(found + tlen)))) {
338 debug_print("plugin licence check failed: invalid separator\n");
342 debug_print("plugin licence check passed: %s found\n", plugin_licence_tokens[i]);
347 debug_print("plugin licence check failed: %s is not a valid licence\n", licence);
354 * \param filename The filename of the plugin to load
355 * \param error The location where an error string can be stored
356 * \return the plugin on success, NULL otherwise
358 Plugin *plugin_load(const gchar *filename, gchar **error)
361 gint (*plugin_init) (gchar **error);
362 gpointer plugin_name, plugin_desc, plugin_version;
363 const gchar *(*plugin_type)(void);
364 const gchar *(*plugin_licence)(void);
365 struct PluginFeature *(*plugin_provides)(void);
367 START_TIMING((filename?filename:"NULL plugin"));
368 cm_return_val_if_fail(filename != NULL, NULL);
369 cm_return_val_if_fail(error != NULL, NULL);
371 /* check duplicate plugin path name */
372 if (plugin_is_loaded(filename)) {
373 plugin = plugin_get_by_filename(filename);
374 if (plugin->unloaded_hidden) {
378 *error = g_strdup(_("Plugin already loaded"));
383 plugin_remove_from_unloaded_list(filename);
385 if (plugin_load_deps(filename, error) < 0)
387 plugin = g_new0(Plugin, 1);
388 if (plugin == NULL) {
389 *error = g_strdup(_("Failed to allocate memory for Plugin"));
393 debug_print("trying to load `%s'\n", filename);
394 plugin->module = g_module_open(filename, 0);
395 if (plugin->module == NULL) {
396 *error = g_strdup(g_module_error());
402 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
403 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
404 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
405 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
406 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
407 !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
408 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
409 *error = g_strdup(g_module_error());
410 if (plugin->unloaded_hidden)
412 g_module_close(plugin->module);
417 if (plugin_licence_check(plugin_licence()) != TRUE) {
418 *error = g_strdup(_("This module is not licensed under a GPL v3 or later compatible license."));
419 if (plugin->unloaded_hidden)
421 g_module_close(plugin->module);
426 if (!strcmp(plugin_type(), "GTK")) {
427 *error = g_strdup(_("This module is for Claws Mail GTK1."));
428 if (plugin->unloaded_hidden)
430 g_module_close(plugin->module);
435 if ((*error = plugin_check_features(plugin_provides())) != NULL) {
436 if (plugin->unloaded_hidden)
438 g_module_close(plugin->module);
442 plugin->name = plugin_name;
443 plugin->desc = plugin_desc;
444 plugin->version = plugin_version;
445 plugin->type = plugin_type;
446 plugin->licence = plugin_licence;
447 plugin->provides = plugin_provides;
448 plugin->filename = g_strdup(filename);
449 plugin->error = NULL;
451 if ((ok = plugin_init(error)) < 0) {
453 plugin->error = g_strdup(*error);
454 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
458 if (!plugin->unloaded_hidden)
459 plugins = g_slist_append(plugins, plugin);
460 plugin->unloaded_hidden = FALSE;
462 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
467 void plugin_unload(Plugin *plugin)
469 gboolean (*plugin_done) (void);
470 gboolean can_unload = TRUE;
472 plugin_unload_rdeps(plugin);
474 if (plugin->unloaded_hidden)
478 plugin_remove_from_unloaded_list(plugin->filename);
481 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
482 can_unload = plugin_done();
487 if (!RUNNING_ON_VALGRIND) {
488 g_module_close(plugin->module);
491 g_module_close(plugin->module);
493 plugins = g_slist_remove(plugins, plugin);
494 g_free(plugin->filename);
497 plugin->unloaded_hidden = TRUE;
502 void plugin_load_all(const gchar *type)
507 gchar *error = NULL, *block;
509 plugin_types = g_slist_append(plugin_types, g_strdup(type));
511 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
513 block = g_strconcat("PluginsWin32_", type, NULL);
515 block = g_strconcat("Plugins_", type, NULL);
517 if ((pfile = prefs_read_open(rcpath)) == NULL ||
518 (prefs_set_block_label(pfile, block) < 0)) {
521 prefs_file_close(pfile);
526 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
531 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
532 g_warning("plugin loading error: %s\n", error);
536 prefs_file_close(pfile);
541 void plugin_unload_all(const gchar *type)
545 list = g_slist_copy(plugins);
546 list = g_slist_reverse(list);
548 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
549 Plugin *plugin = (Plugin *) cur->data;
551 if (!strcmp(type, plugin->type()))
552 plugin_unload(plugin);
556 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
559 plugin_types = g_slist_remove(plugin_types, cur);
564 /* Load those plugins we always want to use. No error output; just
566 void plugin_load_standard_plugins (void)
568 static const char *names[] = {
579 gchar *error, *filename;
581 for (i=0; names[i]; i++) {
582 /* Simple hack to check whether the plugin has already
583 * been loaded but checking only for the basename. */
584 GSList *cur = plugins;
585 for(; cur; cur = cur->next) {
586 Plugin *p = (Plugin *)cur->data;
587 if (strstr(p->filename, names[i]))
590 if (!cur) { /* Not yet loaded. */
591 /* FIXME: get_plugin_dir () returns with a trailing
592 * (back)slash; this should be fixed so that we can use
593 * g_module_build_path here. */
595 filename = g_strconcat (get_plugin_dir(),
598 filename = g_strconcat (get_plugin_dir(),
599 names[i], ".", G_MODULE_SUFFIX, NULL);
602 plugin_load(filename, &error);
609 GSList *plugin_get_list(void)
612 GSList *cur = plugins;
613 for (; cur; cur = cur->next) {
614 Plugin *p = (Plugin *)cur->data;
615 if (!p->unloaded_hidden)
616 new = g_slist_prepend(new, p);
618 new = g_slist_reverse(new);
622 Plugin *plugin_get_loaded_by_name(const gchar *name)
624 Plugin *plugin = NULL;
626 new = plugin_get_list();
627 for (cur = new; cur; cur = g_slist_next(cur)) {
628 plugin = (Plugin *)cur->data;
629 if (!g_ascii_strcasecmp(plugin->name(), name))
638 GSList *plugin_get_unloaded_list(void)
640 return g_slist_copy(unloaded_plugins);
643 const gchar *plugin_get_name(Plugin *plugin)
645 return plugin->name();
648 const gchar *plugin_get_desc(Plugin *plugin)
650 return plugin->desc();
653 const gchar *plugin_get_version(Plugin *plugin)
655 return plugin->version();
658 const gchar *plugin_get_error(Plugin *plugin)
660 return plugin->error;
663 /* Generally called in plugin_init() function of each plugin. It check the
664 * minimal and compiled version of claws binary required by the plugin.
665 * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
666 * check the corresponding version.
668 * If an error occurs {
669 * If @error == NULL { don't allocate error string. }
670 * If @error != NULL { error string is allocated and must be freed after
673 * Returns: FALSE if an error occurs, TRUE if all is OK.
675 gint check_plugin_version(guint32 minimum_claws_version,
676 guint32 compiled_claws_version,
677 const gchar *plugin_name,
680 guint32 claws_version = claws_get_version();
682 if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
684 *error = (plugin_name && *plugin_name)
685 ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
686 "version the '%s' plugin was built with."),
688 : g_strdup(_("Your version of Claws Mail is newer than the "
689 "version the plugin was built with."));
694 if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
696 *error = (plugin_name && *plugin_name)
697 ? g_strdup_printf(_("Your version of Claws Mail is too old for "
698 "the '%s' plugin."), plugin_name)
699 : g_strdup(_("Your version of Claws Mail is too old for the plugin."));