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"),
75 * List of all loaded plugins
77 GSList *plugins = NULL;
78 GSList *plugin_types = NULL;
81 * List of plugins unloaded for some fixable reason
83 static GSList *unloaded_plugins = NULL;
85 static gint list_find_by_string(gconstpointer data, gconstpointer str)
87 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
90 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
92 /* FIXME: There is a problem in case of symlinks or when a
93 user tries to load a plugin with the same name from a
94 different directory. I think it would be better to compare
95 only the basename of the filename here (case-insensitive on
97 cm_return_val_if_fail(plugin, 1);
98 cm_return_val_if_fail(plugin->filename, 1);
99 cm_return_val_if_fail(filename, 1);
100 return strcmp(filename, plugin->filename);
103 void plugin_save_list(void)
105 gchar *rcpath, *block;
107 GSList *type_cur, *plugin_cur;
110 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
111 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
113 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
115 block = g_strconcat("Plugins_", type_cur->data, NULL);
117 if ((pfile = prefs_write_open(rcpath)) == NULL ||
118 (prefs_set_block_label(pfile, block) < 0)) {
119 g_warning("failed to write plugin list\n");
125 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
126 plugin = (Plugin *) plugin_cur->data;
128 if (plugin->unloaded_hidden)
131 if (!strcmp(plugin->type(), type_cur->data))
132 if (fprintf(pfile->fp, "%s\n", plugin->filename) < 0)
135 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
136 plugin = (Plugin *) plugin_cur->data;
138 if (plugin->unloaded_hidden)
141 if (!strcmp(plugin->type(), type_cur->data))
142 if (fprintf(pfile->fp, "%s\n", plugin->filename) < 0)
145 if (fprintf(pfile->fp, "\n") < 0)
148 if (prefs_file_close(pfile) < 0)
149 g_warning("failed to write plugin list\n");
156 g_warning("failed to write plugin list\n");
157 if (prefs_file_close_revert(pfile) < 0)
158 g_warning("failed to revert plugin list\n");
164 static gboolean plugin_is_loaded(const gchar *filename)
166 return (g_slist_find_custom(plugins, filename,
167 (GCompareFunc)list_find_by_plugin_filename) != NULL);
170 static Plugin *plugin_get_by_filename(const gchar *filename)
172 GSList *cur = plugins;
173 for(; cur; cur = cur->next) {
174 Plugin *p = (Plugin *)cur->data;
175 if (!strcmp(p->filename, filename)) {
183 * Loads a plugin dependancies
185 * Plugin dependancies are, optionnaly, listed in a file in
186 * get_plugin_dir()/$pluginname.deps.
187 * \param filename The filename of the plugin for which we have to load deps
188 * \param error The location where an error string can be stored
189 * \return 0 on success, -1 otherwise
191 static gint plugin_load_deps(const gchar *filename, gchar **error)
194 gchar *deps_file = NULL;
199 tmp = g_strdup(filename);
200 if( (p = strrchr(tmp, '.')) )
202 deps_file = g_strconcat(tmp, ".deps", NULL);
205 fp = g_fopen(deps_file, "rb");
211 while (fgets(buf, sizeof(buf), fp) != NULL) {
212 Plugin *dep_plugin = NULL;
214 buf[strlen(buf)-1]='\0'; /* chop off \n */
215 path = g_strconcat(get_plugin_dir(), buf,
216 ".", G_MODULE_SUFFIX, NULL);
217 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
218 debug_print("trying to load %s\n", path);
219 dep_plugin = plugin_load(path, error);
220 if (dep_plugin == NULL) {
227 if (!g_slist_find_custom(dep_plugin->rdeps,
228 (gpointer) filename, list_find_by_string)) {
229 debug_print("adding %s to %s rdeps\n",
231 dep_plugin->filename);
233 g_slist_append(dep_plugin->rdeps,
241 static void plugin_unload_rdeps(Plugin *plugin)
243 GSList *cur = plugin->rdeps;
244 debug_print("removing %s rdeps\n", plugin->filename);
246 gchar *file = (gchar *)cur->data;
247 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
248 debug_print(" rdep %s: %p\n", file, rdep_plugin);
250 plugin_unload(rdep_plugin);
255 g_slist_free(plugin->rdeps);
256 plugin->rdeps = NULL;
259 static void plugin_remove_from_unloaded_list (const gchar *filename)
261 GSList *item = g_slist_find_custom(unloaded_plugins,
262 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
263 Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
264 if (unloaded_plugin != NULL) {
265 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
266 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
267 g_module_close(unloaded_plugin->module);
268 g_free(unloaded_plugin->filename);
269 g_free(unloaded_plugin->error);
270 g_free(unloaded_plugin);
274 static gchar *plugin_check_features(struct PluginFeature *features) {
276 GSList *cur = plugins;
278 if (features == NULL)
280 for(; cur; cur = cur->next) {
281 Plugin *p = (Plugin *)cur->data;
282 struct PluginFeature *cur_features = p->provides();
283 if (p->unloaded_hidden)
285 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
286 for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
287 if (cur_features[j].type == features[i].type &&
288 !strcmp(cur_features[j].subtype, features[i].subtype)) {
289 return g_strdup_printf(_(
290 "This plugin provides %s (%s), which is "
291 "already provided by the %s plugin."),
292 _(plugin_feature_names[features[i].type]),
293 _(features[i].subtype),
305 * \param filename The filename of the plugin to load
306 * \param error The location where an error string can be stored
307 * \return the plugin on success, NULL otherwise
309 Plugin *plugin_load(const gchar *filename, gchar **error)
312 gint (*plugin_init) (gchar **error);
313 gpointer plugin_name, plugin_desc, plugin_version;
314 const gchar *(*plugin_type)(void);
315 const gchar *(*plugin_licence)(void);
316 struct PluginFeature *(*plugin_provides)(void);
318 START_TIMING((filename?filename:"NULL plugin"));
319 cm_return_val_if_fail(filename != NULL, NULL);
320 cm_return_val_if_fail(error != NULL, NULL);
322 /* check duplicate plugin path name */
323 if (plugin_is_loaded(filename)) {
324 plugin = plugin_get_by_filename(filename);
325 if (plugin->unloaded_hidden) {
329 *error = g_strdup(_("Plugin already loaded"));
334 plugin_remove_from_unloaded_list(filename);
336 if (plugin_load_deps(filename, error) < 0)
338 plugin = g_new0(Plugin, 1);
339 if (plugin == NULL) {
340 *error = g_strdup(_("Failed to allocate memory for Plugin"));
344 debug_print("trying to load `%s'\n", filename);
345 plugin->module = g_module_open(filename, 0);
346 if (plugin->module == NULL) {
347 *error = g_strdup(g_module_error());
353 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
354 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
355 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
356 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
357 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
358 !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
359 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
360 *error = g_strdup(g_module_error());
361 if (plugin->unloaded_hidden)
363 g_module_close(plugin->module);
368 if (strcmp(plugin_licence(), "GPL2+") && strncmp(plugin_licence(), "GPL3", strlen("GPL3"))
369 && strncmp(plugin_licence(), "GPL2+-compatible", strlen("GPL2+-compatible"))) {
370 *error = g_strdup(_("This module is not licensed under a GPL v2 or later compatible license."));
371 if (plugin->unloaded_hidden)
373 g_module_close(plugin->module);
378 if (!strcmp(plugin_type(), "GTK")) {
379 *error = g_strdup(_("This module is for Claws Mail GTK1."));
380 if (plugin->unloaded_hidden)
382 g_module_close(plugin->module);
387 if ((*error = plugin_check_features(plugin_provides())) != NULL) {
388 if (plugin->unloaded_hidden)
390 g_module_close(plugin->module);
394 plugin->name = plugin_name;
395 plugin->desc = plugin_desc;
396 plugin->version = plugin_version;
397 plugin->type = plugin_type;
398 plugin->licence = plugin_licence;
399 plugin->provides = plugin_provides;
400 plugin->filename = g_strdup(filename);
401 plugin->error = NULL;
403 if ((ok = plugin_init(error)) < 0) {
405 plugin->error = g_strdup(*error);
406 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
410 if (!plugin->unloaded_hidden)
411 plugins = g_slist_append(plugins, plugin);
412 plugin->unloaded_hidden = FALSE;
414 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
419 void plugin_unload(Plugin *plugin)
421 gboolean (*plugin_done) (void);
422 gboolean can_unload = TRUE;
424 plugin_unload_rdeps(plugin);
426 if (plugin->unloaded_hidden)
430 plugin_remove_from_unloaded_list(plugin->filename);
433 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
434 can_unload = plugin_done();
439 if (!RUNNING_ON_VALGRIND) {
440 g_module_close(plugin->module);
443 g_module_close(plugin->module);
445 plugins = g_slist_remove(plugins, plugin);
446 g_free(plugin->filename);
449 plugin->unloaded_hidden = TRUE;
454 void plugin_load_all(const gchar *type)
459 gchar *error = NULL, *block;
461 plugin_types = g_slist_append(plugin_types, g_strdup(type));
463 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
465 block = g_strconcat("PluginsWin32_", type, NULL);
467 block = g_strconcat("Plugins_", type, NULL);
469 if ((pfile = prefs_read_open(rcpath)) == NULL ||
470 (prefs_set_block_label(pfile, block) < 0)) {
473 prefs_file_close(pfile);
478 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
483 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
484 g_warning("plugin loading error: %s\n", error);
488 prefs_file_close(pfile);
493 void plugin_unload_all(const gchar *type)
497 list = g_slist_copy(plugins);
498 list = g_slist_reverse(list);
500 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
501 Plugin *plugin = (Plugin *) cur->data;
503 if (!strcmp(type, plugin->type()))
504 plugin_unload(plugin);
508 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
511 plugin_types = g_slist_remove(plugin_types, cur);
516 /* Load those plugins we always want to use. No error output; just
518 void plugin_load_standard_plugins (void)
520 static const char *names[] = {
531 gchar *error, *filename;
533 for (i=0; names[i]; i++) {
534 /* Simple hack to check whether the plugin has already
535 * been loaded but checking only for the basename. */
536 GSList *cur = plugins;
537 for(; cur; cur = cur->next) {
538 Plugin *p = (Plugin *)cur->data;
539 if (strstr(p->filename, names[i]))
542 if (!cur) { /* Not yet loaded. */
543 /* FIXME: get_plugin_dir () returns with a trailing
544 * (back)slash; this should be fixed so that we can use
545 * g_module_build_path here. */
547 filename = g_strconcat (get_plugin_dir(),
550 filename = g_strconcat (get_plugin_dir(),
551 names[i], ".", G_MODULE_SUFFIX, NULL);
554 plugin_load(filename, &error);
561 GSList *plugin_get_list(void)
564 GSList *cur = plugins;
565 for (; cur; cur = cur->next) {
566 Plugin *p = (Plugin *)cur->data;
567 if (!p->unloaded_hidden)
568 new = g_slist_prepend(new, p);
570 new = g_slist_reverse(new);
574 Plugin *plugin_get_loaded_by_name(const gchar *name)
576 Plugin *plugin = NULL;
578 new = plugin_get_list();
579 for (cur = new; cur; cur = g_slist_next(cur)) {
580 plugin = (Plugin *)cur->data;
581 if (!g_ascii_strcasecmp(plugin->name(), name))
590 GSList *plugin_get_unloaded_list(void)
592 return g_slist_copy(unloaded_plugins);
595 const gchar *plugin_get_name(Plugin *plugin)
597 return plugin->name();
600 const gchar *plugin_get_desc(Plugin *plugin)
602 return plugin->desc();
605 const gchar *plugin_get_version(Plugin *plugin)
607 return plugin->version();
610 const gchar *plugin_get_error(Plugin *plugin)
612 return plugin->error;
615 /* Generally called in plugin_init() function of each plugin. It check the
616 * minimal and compiled version of claws binary required by the plugin.
617 * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
618 * check the corresponding version.
620 * If an error occurs {
621 * If @error == NULL { don't allocate error string. }
622 * If @error != NULL { error string is allocated and must be freed after
625 * Returns: FALSE if an error occurs, TRUE if all is OK.
627 gint check_plugin_version(guint32 minimum_claws_version,
628 guint32 compiled_claws_version,
629 const gchar *plugin_name,
632 guint32 claws_version = claws_get_version();
634 if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
636 *error = (plugin_name && *plugin_name)
637 ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
638 "version the '%s' plugin was built with."),
640 : g_strdup(_("Your version of Claws Mail is newer than the "
641 "version the plugin was built with."));
646 if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
648 *error = (plugin_name && *plugin_name)
649 ? g_strdup_printf(_("Your version of Claws Mail is too old for "
650 "the '%s' plugin."), plugin_name)
651 : g_strdup(_("Your version of Claws Mail is too old for the plugin."));