2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 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>
41 const gchar *(*name) (void);
42 const gchar *(*desc) (void);
43 const gchar *(*version) (void);
44 const gchar *(*type) (void);
45 const gchar *(*licence) (void);
46 struct PluginFeature *(*provides) (void);
52 const gchar *plugin_feature_names[] =
57 N_("a privacy interface"),
64 * List of all loaded plugins
66 GSList *plugins = NULL;
67 GSList *plugin_types = NULL;
70 * List of plugins unloaded for some fixable reason
72 static GSList *unloaded_plugins = NULL;
74 static gint list_find_by_string(gconstpointer data, gconstpointer str)
76 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
79 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
81 /* FIXME: There is a problem in case of symlinks or when a
82 user tries to load a plugin with the same name from a
83 different directory. I think it would be better to compare
84 only the basename of the filename here (case-insensitive on
86 g_return_val_if_fail(plugin, 1);
87 g_return_val_if_fail(plugin->filename, 1);
88 g_return_val_if_fail(filename, 1);
89 return strcmp(filename, plugin->filename);
92 void plugin_save_list(void)
94 gchar *rcpath, *block;
96 GSList *type_cur, *plugin_cur;
99 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
100 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
102 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
104 block = g_strconcat("Plugins_", type_cur->data, NULL);
106 if ((pfile = prefs_write_open(rcpath)) == NULL ||
107 (prefs_set_block_label(pfile, block) < 0)) {
108 g_warning("failed to write plugin list\n");
114 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
115 plugin = (Plugin *) plugin_cur->data;
117 if (!strcmp(plugin->type(), type_cur->data))
118 fprintf(pfile->fp, "%s\n", plugin->filename);
120 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
121 plugin = (Plugin *) plugin_cur->data;
123 if (!strcmp(plugin->type(), type_cur->data))
124 fprintf(pfile->fp, "%s\n", plugin->filename);
126 fprintf(pfile->fp, "\n");
128 if (prefs_file_close(pfile) < 0)
129 g_warning("failed to write plugin list\n");
135 static gboolean plugin_is_loaded(const gchar *filename)
137 return (g_slist_find_custom(plugins, filename,
138 (GCompareFunc)list_find_by_plugin_filename) != NULL);
141 static Plugin *plugin_get_by_filename(const gchar *filename)
143 GSList *cur = plugins;
144 for(; cur; cur = cur->next) {
145 Plugin *p = (Plugin *)cur->data;
146 if (!strcmp(p->filename, filename)) {
154 * Loads a plugin dependancies
156 * Plugin dependancies are, optionnaly, listed in a file in
157 * get_plugin_dir()/$pluginname.deps.
158 * \param filename The filename of the plugin for which we have to load deps
159 * \param error The location where an error string can be stored
160 * \return 0 on success, -1 otherwise
162 static gint plugin_load_deps(const gchar *filename, gchar **error)
165 gchar *deps_file = NULL;
170 tmp = g_strdup(filename);
171 if( (p = strrchr(tmp, '.')) )
173 deps_file = g_strconcat(tmp, ".deps", NULL);
176 fp = g_fopen(deps_file, "rb");
182 while (fgets(buf, sizeof(buf), fp) != NULL) {
183 Plugin *dep_plugin = NULL;
185 buf[strlen(buf)-1]='\0'; /* chop off \n */
186 path = g_strconcat(get_plugin_dir(), buf,
187 ".", G_MODULE_SUFFIX, NULL);
188 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
189 debug_print("trying to load %s\n", path);
190 dep_plugin = plugin_load(path, error);
191 if (dep_plugin == NULL) {
197 if (!g_slist_find_custom(dep_plugin->rdeps,
198 (gpointer) filename, list_find_by_string)) {
199 debug_print("adding %s to %s rdeps\n",
201 dep_plugin->filename);
203 g_slist_append(dep_plugin->rdeps,
211 static void plugin_unload_rdeps(Plugin *plugin)
213 GSList *cur = plugin->rdeps;
214 debug_print("removing %s rdeps\n", plugin->filename);
216 gchar *file = (gchar *)cur->data;
217 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
218 debug_print(" rdep %s: %p\n", file, rdep_plugin);
220 plugin_unload(rdep_plugin);
225 g_slist_free(plugin->rdeps);
226 plugin->rdeps = NULL;
229 static void plugin_remove_from_unloaded_list (const gchar *filename)
231 GSList *item = g_slist_find_custom(unloaded_plugins,
232 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
233 Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
234 if (unloaded_plugin != NULL) {
235 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
236 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
237 g_module_close(unloaded_plugin->module);
238 g_free(unloaded_plugin->filename);
239 g_free(unloaded_plugin->error);
240 g_free(unloaded_plugin);
244 static gchar *plugin_check_features(struct PluginFeature *features) {
246 GSList *cur = plugins;
248 if (features == NULL)
250 for(; cur; cur = cur->next) {
251 Plugin *p = (Plugin *)cur->data;
252 struct PluginFeature *cur_features = p->provides();
253 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
254 for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
255 if (cur_features[j].type == features[i].type &&
256 !strcmp(cur_features[j].subtype, features[i].subtype)) {
257 return g_strdup_printf(_(
258 "This plugin provides %s (%s), which is "
259 "already provided by the %s plugin."),
260 _(plugin_feature_names[features[i].type]),
261 _(features[i].subtype),
273 * \param filename The filename of the plugin to load
274 * \param error The location where an error string can be stored
275 * \return the plugin on success, NULL otherwise
277 Plugin *plugin_load(const gchar *filename, gchar **error)
280 gint (*plugin_init) (gchar **error);
281 gpointer plugin_name, plugin_desc, plugin_version;
282 const gchar *(*plugin_type)(void);
283 const gchar *(*plugin_licence)(void);
284 struct PluginFeature *(*plugin_provides)(void);
288 g_return_val_if_fail(filename != NULL, NULL);
289 g_return_val_if_fail(error != NULL, NULL);
291 /* check duplicate plugin path name */
292 if (plugin_is_loaded(filename)) {
293 *error = g_strdup(_("Plugin already loaded"));
297 plugin_remove_from_unloaded_list(filename);
299 if (plugin_load_deps(filename, error) < 0)
301 plugin = g_new0(Plugin, 1);
302 if (plugin == NULL) {
303 *error = g_strdup(_("Failed to allocate memory for Plugin"));
307 debug_print("trying to load `%s'\n", filename);
308 plugin->module = g_module_open(filename, 0);
309 if (plugin->module == NULL) {
310 *error = g_strdup(g_module_error());
315 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
316 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
317 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
318 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
319 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
320 !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
321 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
322 *error = g_strdup(g_module_error());
323 g_module_close(plugin->module);
328 if (strcmp(plugin_licence(), "GPL")
329 && strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
330 *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
331 g_module_close(plugin->module);
336 if (!strcmp(plugin_type(), "GTK")) {
337 *error = g_strdup(_("This module is for Claws Mail GTK1."));
338 g_module_close(plugin->module);
343 if ((*error = plugin_check_features(plugin_provides())) != NULL) {
344 g_module_close(plugin->module);
348 plugin->name = plugin_name;
349 plugin->desc = plugin_desc;
350 plugin->version = plugin_version;
351 plugin->type = plugin_type;
352 plugin->licence = plugin_licence;
353 plugin->provides = plugin_provides;
354 plugin->filename = g_strdup(filename);
355 plugin->error = NULL;
356 if ((ok = plugin_init(error)) < 0) {
358 plugin->error = g_strdup(*error);
359 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
363 plugins = g_slist_append(plugins, plugin);
365 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
370 void plugin_unload(Plugin *plugin)
372 void (*plugin_done) (void);
374 plugin_unload_rdeps(plugin);
377 plugin_remove_from_unloaded_list(plugin->filename);
380 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
384 g_module_close(plugin->module);
385 plugins = g_slist_remove(plugins, plugin);
386 g_free(plugin->filename);
390 void plugin_load_all(const gchar *type)
395 gchar *error = NULL, *block;
397 plugin_types = g_slist_append(plugin_types, g_strdup(type));
399 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
401 block = g_strconcat("PluginsWin32_", type, NULL);
403 block = g_strconcat("Plugins_", type, NULL);
405 if ((pfile = prefs_read_open(rcpath)) == NULL ||
406 (prefs_set_block_label(pfile, block) < 0)) {
412 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
417 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
418 g_warning("plugin loading error: %s\n", error);
422 prefs_file_close(pfile);
427 void plugin_unload_all(const gchar *type)
431 list = g_slist_copy(plugins);
432 list = g_slist_reverse(list);
434 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
435 Plugin *plugin = (Plugin *) cur->data;
437 if (!strcmp(type, plugin->type()))
438 plugin_unload(plugin);
442 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
445 plugin_types = g_slist_remove(plugin_types, cur);
450 /* Load those plugins we always want to use. No error output; just
452 void plugin_load_standard_plugins (void)
454 static const char *names[] = {
465 gchar *error, *filename;
467 for (i=0; names[i]; i++) {
468 /* Simple hack to check whether the plugin has already
469 * been loaded but checking only for the basename. */
470 GSList *cur = plugins;
471 for(; cur; cur = cur->next) {
472 Plugin *p = (Plugin *)cur->data;
473 if (strstr(p->filename, names[i]))
476 if (!cur) { /* Not yet loaded. */
477 /* FIXME: get_plugin_dir () returns with a trailing
478 * (back)slash; this should be fixed so that we can use
479 * g_module_build_path here. */
481 filename = g_strconcat (get_plugin_dir(),
484 filename = g_strconcat (get_plugin_dir(),
485 names[i], ".", G_MODULE_SUFFIX, NULL);
488 plugin_load(filename, &error);
495 GSList *plugin_get_list(void)
497 return g_slist_copy(plugins);
500 GSList *plugin_get_unloaded_list(void)
502 return g_slist_copy(unloaded_plugins);
505 const gchar *plugin_get_name(Plugin *plugin)
507 return plugin->name();
510 const gchar *plugin_get_desc(Plugin *plugin)
512 return plugin->desc();
515 const gchar *plugin_get_version(Plugin *plugin)
517 return plugin->version();
520 const gchar *plugin_get_error(Plugin *plugin)
522 return plugin->error;
525 /* Generally called in plugin_init() function of each plugin. It check the
526 * minimal and compiled version of claws binary required by the plugin.
527 * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
528 * check the corresponding version.
530 * If an error occurs {
531 * If @error == NULL { don't allocate error string. }
532 * If @error != NULL { error string is allocated and must be freed after
535 * Returns: FALSE if an error occurs, TRUE if all is OK.
537 gint check_plugin_version(guint32 minimum_claws_version,
538 guint32 compiled_claws_version,
539 const gchar *plugin_name,
542 guint32 claws_version = claws_get_version();
544 if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
546 *error = (plugin_name && *plugin_name)
547 ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
548 "version the '%s' plugin was built with."),
550 : g_strdup(_("Your version of Claws Mail is newer than the "
551 "version the plugin was built with."));
556 if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
558 *error = (plugin_name && *plugin_name)
559 ? g_strdup_printf(_("Your version of Claws Mail is too old for "
560 "the '%s' plugin."), plugin_name)
561 : g_strdup(_("Your version of Claws Mail is too old for the plugin."));