2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <glib/gi18n.h>
42 const gchar *(*name) (void);
43 const gchar *(*desc) (void);
44 const gchar *(*version) (void);
45 const gchar *(*type) (void);
46 const gchar *(*licence) (void);
47 struct PluginFeature *(*provides) (void);
53 const gchar *plugin_feature_names[] =
58 N_("a privacy interface"),
65 * List of all loaded plugins
67 GSList *plugins = NULL;
68 GSList *plugin_types = NULL;
71 * List of plugins unloaded for some fixable reason
73 static GSList *unloaded_plugins = NULL;
75 static gint list_find_by_string(gconstpointer data, gconstpointer str)
77 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
80 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
82 /* FIXME: There is a problem in case of symlinks or when a
83 user tries to load a plugin with the same name from a
84 different directory. I think it would be better to compare
85 only the basename of the filename here (case-insensitive on
87 g_return_val_if_fail(plugin, 1);
88 g_return_val_if_fail(plugin->filename, 1);
89 g_return_val_if_fail(filename, 1);
90 return strcmp(filename, plugin->filename);
93 void plugin_save_list(void)
95 gchar *rcpath, *block;
97 GSList *type_cur, *plugin_cur;
100 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
101 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
103 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
105 block = g_strconcat("Plugins_", type_cur->data, NULL);
107 if ((pfile = prefs_write_open(rcpath)) == NULL ||
108 (prefs_set_block_label(pfile, block) < 0)) {
109 g_warning("failed to write plugin list\n");
115 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
116 plugin = (Plugin *) plugin_cur->data;
118 if (!strcmp(plugin->type(), type_cur->data))
119 fprintf(pfile->fp, "%s\n", plugin->filename);
121 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
122 plugin = (Plugin *) plugin_cur->data;
124 if (!strcmp(plugin->type(), type_cur->data))
125 fprintf(pfile->fp, "%s\n", plugin->filename);
127 fprintf(pfile->fp, "\n");
129 if (prefs_file_close(pfile) < 0)
130 g_warning("failed to write plugin list\n");
136 static gboolean plugin_is_loaded(const gchar *filename)
138 return (g_slist_find_custom(plugins, filename,
139 (GCompareFunc)list_find_by_plugin_filename) != NULL);
142 static Plugin *plugin_get_by_filename(const gchar *filename)
144 GSList *cur = plugins;
145 for(; cur; cur = cur->next) {
146 Plugin *p = (Plugin *)cur->data;
147 if (!strcmp(p->filename, filename)) {
155 * Loads a plugin dependancies
157 * Plugin dependancies are, optionnaly, listed in a file in
158 * get_plugin_dir()/$pluginname.deps.
159 * \param filename The filename of the plugin for which we have to load deps
160 * \param error The location where an error string can be stored
161 * \return 0 on success, -1 otherwise
163 static gint plugin_load_deps(const gchar *filename, gchar **error)
166 gchar *deps_file = NULL;
171 tmp = g_strdup(filename);
172 if( (p = strrchr(tmp, '.')) )
174 deps_file = g_strconcat(tmp, ".deps", NULL);
177 fp = g_fopen(deps_file, "rb");
183 while (fgets(buf, sizeof(buf), fp) != NULL) {
184 Plugin *dep_plugin = NULL;
186 buf[strlen(buf)-1]='\0'; /* chop off \n */
187 path = g_strconcat(get_plugin_dir(), buf,
188 ".", G_MODULE_SUFFIX, NULL);
189 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
190 debug_print("trying to load %s\n", path);
191 dep_plugin = plugin_load(path, error);
192 if (dep_plugin == NULL) {
198 if (!g_slist_find_custom(dep_plugin->rdeps,
199 (gpointer) filename, list_find_by_string)) {
200 debug_print("adding %s to %s rdeps\n",
202 dep_plugin->filename);
204 g_slist_append(dep_plugin->rdeps,
212 static void plugin_unload_rdeps(Plugin *plugin)
214 GSList *cur = plugin->rdeps;
215 debug_print("removing %s rdeps\n", plugin->filename);
217 gchar *file = (gchar *)cur->data;
218 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
219 debug_print(" rdep %s: %p\n", file, rdep_plugin);
221 plugin_unload(rdep_plugin);
226 g_slist_free(plugin->rdeps);
227 plugin->rdeps = NULL;
230 static void plugin_remove_from_unloaded_list (const gchar *filename)
232 GSList *item = g_slist_find_custom(unloaded_plugins,
233 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
234 Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
235 if (unloaded_plugin != NULL) {
236 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
237 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
238 g_module_close(unloaded_plugin->module);
239 g_free(unloaded_plugin->filename);
240 g_free(unloaded_plugin->error);
241 g_free(unloaded_plugin);
245 static gchar *plugin_check_features(struct PluginFeature *features) {
247 GSList *cur = plugins;
249 if (features == NULL)
251 for(; cur; cur = cur->next) {
252 Plugin *p = (Plugin *)cur->data;
253 struct PluginFeature *cur_features = p->provides();
254 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
255 for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
256 if (cur_features[j].type == features[i].type &&
257 !strcmp(cur_features[j].subtype, features[i].subtype)) {
258 return g_strdup_printf(_(
259 "This plugin provides %s (%s), which is "
260 "already provided by the %s plugin."),
261 _(plugin_feature_names[features[i].type]),
262 _(features[i].subtype),
274 * \param filename The filename of the plugin to load
275 * \param error The location where an error string can be stored
276 * \return the plugin on success, NULL otherwise
278 Plugin *plugin_load(const gchar *filename, gchar **error)
281 gint (*plugin_init) (gchar **error);
282 gpointer plugin_name, plugin_desc, plugin_version;
283 const gchar *(*plugin_type)(void);
284 const gchar *(*plugin_licence)(void);
285 struct PluginFeature *(*plugin_provides)(void);
289 g_return_val_if_fail(filename != NULL, NULL);
290 g_return_val_if_fail(error != NULL, NULL);
292 /* check duplicate plugin path name */
293 if (plugin_is_loaded(filename)) {
294 *error = g_strdup(_("Plugin already loaded"));
298 plugin_remove_from_unloaded_list(filename);
300 if (plugin_load_deps(filename, error) < 0)
302 plugin = g_new0(Plugin, 1);
303 if (plugin == NULL) {
304 *error = g_strdup(_("Failed to allocate memory for Plugin"));
308 debug_print("trying to load `%s'\n", filename);
309 plugin->module = g_module_open(filename, 0);
310 if (plugin->module == NULL) {
311 *error = g_strdup(g_module_error());
316 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
317 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
318 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
319 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
320 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
321 !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
322 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
323 *error = g_strdup(g_module_error());
324 g_module_close(plugin->module);
329 if (strcmp(plugin_licence(), "GPL")
330 && strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
331 *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
332 g_module_close(plugin->module);
337 if (!strcmp(plugin_type(), "GTK")) {
338 *error = g_strdup(_("This module is for Claws Mail GTK1."));
339 g_module_close(plugin->module);
344 if ((*error = plugin_check_features(plugin_provides())) != NULL) {
345 g_module_close(plugin->module);
349 plugin->name = plugin_name;
350 plugin->desc = plugin_desc;
351 plugin->version = plugin_version;
352 plugin->type = plugin_type;
353 plugin->licence = plugin_licence;
354 plugin->provides = plugin_provides;
355 plugin->filename = g_strdup(filename);
356 plugin->error = NULL;
357 if ((ok = plugin_init(error)) < 0) {
359 plugin->error = g_strdup(*error);
360 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
364 plugins = g_slist_append(plugins, plugin);
366 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
371 void plugin_unload(Plugin *plugin)
373 void (*plugin_done) (void);
375 plugin_unload_rdeps(plugin);
378 plugin_remove_from_unloaded_list(plugin->filename);
381 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
385 if (!RUNNING_ON_VALGRIND) {
386 g_module_close(plugin->module);
388 plugins = g_slist_remove(plugins, plugin);
389 g_free(plugin->filename);
393 void plugin_load_all(const gchar *type)
398 gchar *error = NULL, *block;
400 plugin_types = g_slist_append(plugin_types, g_strdup(type));
402 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
404 block = g_strconcat("PluginsWin32_", type, NULL);
406 block = g_strconcat("Plugins_", type, NULL);
408 if ((pfile = prefs_read_open(rcpath)) == NULL ||
409 (prefs_set_block_label(pfile, block) < 0)) {
415 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
420 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
421 g_warning("plugin loading error: %s\n", error);
425 prefs_file_close(pfile);
430 void plugin_unload_all(const gchar *type)
434 list = g_slist_copy(plugins);
435 list = g_slist_reverse(list);
437 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
438 Plugin *plugin = (Plugin *) cur->data;
440 if (!strcmp(type, plugin->type()))
441 plugin_unload(plugin);
445 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
448 plugin_types = g_slist_remove(plugin_types, cur);
453 /* Load those plugins we always want to use. No error output; just
455 void plugin_load_standard_plugins (void)
457 static const char *names[] = {
468 gchar *error, *filename;
470 for (i=0; names[i]; i++) {
471 /* Simple hack to check whether the plugin has already
472 * been loaded but checking only for the basename. */
473 GSList *cur = plugins;
474 for(; cur; cur = cur->next) {
475 Plugin *p = (Plugin *)cur->data;
476 if (strstr(p->filename, names[i]))
479 if (!cur) { /* Not yet loaded. */
480 /* FIXME: get_plugin_dir () returns with a trailing
481 * (back)slash; this should be fixed so that we can use
482 * g_module_build_path here. */
484 filename = g_strconcat (get_plugin_dir(),
487 filename = g_strconcat (get_plugin_dir(),
488 names[i], ".", G_MODULE_SUFFIX, NULL);
491 plugin_load(filename, &error);
498 GSList *plugin_get_list(void)
500 return g_slist_copy(plugins);
503 GSList *plugin_get_unloaded_list(void)
505 return g_slist_copy(unloaded_plugins);
508 const gchar *plugin_get_name(Plugin *plugin)
510 return plugin->name();
513 const gchar *plugin_get_desc(Plugin *plugin)
515 return plugin->desc();
518 const gchar *plugin_get_version(Plugin *plugin)
520 return plugin->version();
523 const gchar *plugin_get_error(Plugin *plugin)
525 return plugin->error;
528 /* Generally called in plugin_init() function of each plugin. It check the
529 * minimal and compiled version of claws binary required by the plugin.
530 * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
531 * check the corresponding version.
533 * If an error occurs {
534 * If @error == NULL { don't allocate error string. }
535 * If @error != NULL { error string is allocated and must be freed after
538 * Returns: FALSE if an error occurs, TRUE if all is OK.
540 gint check_plugin_version(guint32 minimum_claws_version,
541 guint32 compiled_claws_version,
542 const gchar *plugin_name,
545 guint32 claws_version = claws_get_version();
547 if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
549 *error = (plugin_name && *plugin_name)
550 ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
551 "version the '%s' plugin was built with."),
553 : g_strdup(_("Your version of Claws Mail is newer than the "
554 "version the plugin was built with."));
559 if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
561 *error = (plugin_name && *plugin_name)
562 ? g_strdup_printf(_("Your version of Claws Mail is too old for "
563 "the '%s' plugin."), plugin_name)
564 : g_strdup(_("Your version of Claws Mail is too old for the plugin."));