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>
43 const gchar *(*name) (void);
44 const gchar *(*desc) (void);
45 const gchar *(*version) (void);
46 const gchar *(*type) (void);
47 const gchar *(*licence) (void);
48 struct PluginFeature *(*provides) (void);
52 gboolean unloaded_hidden;
55 const gchar *plugin_feature_names[] =
60 N_("a privacy interface"),
67 * List of all loaded plugins
69 GSList *plugins = NULL;
70 GSList *plugin_types = NULL;
73 * List of plugins unloaded for some fixable reason
75 static GSList *unloaded_plugins = NULL;
77 static gint list_find_by_string(gconstpointer data, gconstpointer str)
79 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
82 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
84 /* FIXME: There is a problem in case of symlinks or when a
85 user tries to load a plugin with the same name from a
86 different directory. I think it would be better to compare
87 only the basename of the filename here (case-insensitive on
89 g_return_val_if_fail(plugin, 1);
90 g_return_val_if_fail(plugin->filename, 1);
91 g_return_val_if_fail(filename, 1);
92 return strcmp(filename, plugin->filename);
95 void plugin_save_list(void)
97 gchar *rcpath, *block;
99 GSList *type_cur, *plugin_cur;
102 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
103 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
105 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
107 block = g_strconcat("Plugins_", type_cur->data, NULL);
109 if ((pfile = prefs_write_open(rcpath)) == NULL ||
110 (prefs_set_block_label(pfile, block) < 0)) {
111 g_warning("failed to write plugin list\n");
117 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
118 plugin = (Plugin *) plugin_cur->data;
120 if (plugin->unloaded_hidden)
123 if (!strcmp(plugin->type(), type_cur->data))
124 fprintf(pfile->fp, "%s\n", plugin->filename);
126 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
127 plugin = (Plugin *) plugin_cur->data;
129 if (plugin->unloaded_hidden)
132 if (!strcmp(plugin->type(), type_cur->data))
133 fprintf(pfile->fp, "%s\n", plugin->filename);
135 fprintf(pfile->fp, "\n");
137 if (prefs_file_close(pfile) < 0)
138 g_warning("failed to write plugin list\n");
144 static gboolean plugin_is_loaded(const gchar *filename)
146 return (g_slist_find_custom(plugins, filename,
147 (GCompareFunc)list_find_by_plugin_filename) != NULL);
150 static Plugin *plugin_get_by_filename(const gchar *filename)
152 GSList *cur = plugins;
153 for(; cur; cur = cur->next) {
154 Plugin *p = (Plugin *)cur->data;
155 if (!strcmp(p->filename, filename)) {
163 * Loads a plugin dependancies
165 * Plugin dependancies are, optionnaly, listed in a file in
166 * get_plugin_dir()/$pluginname.deps.
167 * \param filename The filename of the plugin for which we have to load deps
168 * \param error The location where an error string can be stored
169 * \return 0 on success, -1 otherwise
171 static gint plugin_load_deps(const gchar *filename, gchar **error)
174 gchar *deps_file = NULL;
179 tmp = g_strdup(filename);
180 if( (p = strrchr(tmp, '.')) )
182 deps_file = g_strconcat(tmp, ".deps", NULL);
185 fp = g_fopen(deps_file, "rb");
191 while (fgets(buf, sizeof(buf), fp) != NULL) {
192 Plugin *dep_plugin = NULL;
194 buf[strlen(buf)-1]='\0'; /* chop off \n */
195 path = g_strconcat(get_plugin_dir(), buf,
196 ".", G_MODULE_SUFFIX, NULL);
197 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
198 debug_print("trying to load %s\n", path);
199 dep_plugin = plugin_load(path, error);
200 if (dep_plugin == NULL) {
206 if (!g_slist_find_custom(dep_plugin->rdeps,
207 (gpointer) filename, list_find_by_string)) {
208 debug_print("adding %s to %s rdeps\n",
210 dep_plugin->filename);
212 g_slist_append(dep_plugin->rdeps,
220 static void plugin_unload_rdeps(Plugin *plugin)
222 GSList *cur = plugin->rdeps;
223 debug_print("removing %s rdeps\n", plugin->filename);
225 gchar *file = (gchar *)cur->data;
226 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
227 debug_print(" rdep %s: %p\n", file, rdep_plugin);
229 plugin_unload(rdep_plugin);
234 g_slist_free(plugin->rdeps);
235 plugin->rdeps = NULL;
238 static void plugin_remove_from_unloaded_list (const gchar *filename)
240 GSList *item = g_slist_find_custom(unloaded_plugins,
241 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
242 Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
243 if (unloaded_plugin != NULL) {
244 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
245 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
246 g_module_close(unloaded_plugin->module);
247 g_free(unloaded_plugin->filename);
248 g_free(unloaded_plugin->error);
249 g_free(unloaded_plugin);
253 static gchar *plugin_check_features(struct PluginFeature *features) {
255 GSList *cur = plugins;
257 if (features == NULL)
259 for(; cur; cur = cur->next) {
260 Plugin *p = (Plugin *)cur->data;
261 struct PluginFeature *cur_features = p->provides();
262 if (p->unloaded_hidden)
264 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
265 for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
266 if (cur_features[j].type == features[i].type &&
267 !strcmp(cur_features[j].subtype, features[i].subtype)) {
268 return g_strdup_printf(_(
269 "This plugin provides %s (%s), which is "
270 "already provided by the %s plugin."),
271 _(plugin_feature_names[features[i].type]),
272 _(features[i].subtype),
284 * \param filename The filename of the plugin to load
285 * \param error The location where an error string can be stored
286 * \return the plugin on success, NULL otherwise
288 Plugin *plugin_load(const gchar *filename, gchar **error)
291 gint (*plugin_init) (gchar **error);
292 gpointer plugin_name, plugin_desc, plugin_version;
293 const gchar *(*plugin_type)(void);
294 const gchar *(*plugin_licence)(void);
295 struct PluginFeature *(*plugin_provides)(void);
299 g_return_val_if_fail(filename != NULL, NULL);
300 g_return_val_if_fail(error != NULL, NULL);
302 /* check duplicate plugin path name */
303 if (plugin_is_loaded(filename)) {
304 plugin = plugin_get_by_filename(filename);
305 if (plugin->unloaded_hidden) {
309 *error = g_strdup(_("Plugin already loaded"));
314 plugin_remove_from_unloaded_list(filename);
316 if (plugin_load_deps(filename, error) < 0)
318 plugin = g_new0(Plugin, 1);
319 if (plugin == NULL) {
320 *error = g_strdup(_("Failed to allocate memory for Plugin"));
324 debug_print("trying to load `%s'\n", filename);
325 plugin->module = g_module_open(filename, 0);
326 if (plugin->module == NULL) {
327 *error = g_strdup(g_module_error());
333 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
334 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
335 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
336 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
337 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
338 !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
339 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
340 *error = g_strdup(g_module_error());
341 if (plugin->unloaded_hidden)
343 g_module_close(plugin->module);
348 if (strcmp(plugin_licence(), "GPL")
349 && strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
350 *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
351 if (plugin->unloaded_hidden)
353 g_module_close(plugin->module);
358 if (!strcmp(plugin_type(), "GTK")) {
359 *error = g_strdup(_("This module is for Claws Mail GTK1."));
360 if (plugin->unloaded_hidden)
362 g_module_close(plugin->module);
367 if ((*error = plugin_check_features(plugin_provides())) != NULL) {
368 if (plugin->unloaded_hidden)
370 g_module_close(plugin->module);
374 plugin->name = plugin_name;
375 plugin->desc = plugin_desc;
376 plugin->version = plugin_version;
377 plugin->type = plugin_type;
378 plugin->licence = plugin_licence;
379 plugin->provides = plugin_provides;
380 plugin->filename = g_strdup(filename);
381 plugin->error = NULL;
383 if ((ok = plugin_init(error)) < 0) {
385 plugin->error = g_strdup(*error);
386 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
390 if (!plugin->unloaded_hidden)
391 plugins = g_slist_append(plugins, plugin);
392 plugin->unloaded_hidden = FALSE;
394 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
399 void plugin_unload(Plugin *plugin)
401 gboolean (*plugin_done) (void);
402 gboolean can_unload = TRUE;
404 plugin_unload_rdeps(plugin);
406 if (plugin->unloaded_hidden)
410 plugin_remove_from_unloaded_list(plugin->filename);
413 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
414 can_unload = plugin_done();
419 if (!RUNNING_ON_VALGRIND) {
420 g_module_close(plugin->module);
423 g_module_close(plugin->module);
425 plugins = g_slist_remove(plugins, plugin);
426 g_free(plugin->filename);
429 plugin->unloaded_hidden = TRUE;
434 void plugin_load_all(const gchar *type)
439 gchar *error = NULL, *block;
441 plugin_types = g_slist_append(plugin_types, g_strdup(type));
443 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
445 block = g_strconcat("PluginsWin32_", type, NULL);
447 block = g_strconcat("Plugins_", type, NULL);
449 if ((pfile = prefs_read_open(rcpath)) == NULL ||
450 (prefs_set_block_label(pfile, block) < 0)) {
456 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
461 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
462 g_warning("plugin loading error: %s\n", error);
466 prefs_file_close(pfile);
471 void plugin_unload_all(const gchar *type)
475 list = g_slist_copy(plugins);
476 list = g_slist_reverse(list);
478 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
479 Plugin *plugin = (Plugin *) cur->data;
481 if (!strcmp(type, plugin->type()))
482 plugin_unload(plugin);
486 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
489 plugin_types = g_slist_remove(plugin_types, cur);
494 /* Load those plugins we always want to use. No error output; just
496 void plugin_load_standard_plugins (void)
498 static const char *names[] = {
509 gchar *error, *filename;
511 for (i=0; names[i]; i++) {
512 /* Simple hack to check whether the plugin has already
513 * been loaded but checking only for the basename. */
514 GSList *cur = plugins;
515 for(; cur; cur = cur->next) {
516 Plugin *p = (Plugin *)cur->data;
517 if (strstr(p->filename, names[i]))
520 if (!cur) { /* Not yet loaded. */
521 /* FIXME: get_plugin_dir () returns with a trailing
522 * (back)slash; this should be fixed so that we can use
523 * g_module_build_path here. */
525 filename = g_strconcat (get_plugin_dir(),
528 filename = g_strconcat (get_plugin_dir(),
529 names[i], ".", G_MODULE_SUFFIX, NULL);
532 plugin_load(filename, &error);
539 GSList *plugin_get_list(void)
542 GSList *cur = plugins;
543 for (; cur; cur = cur->next) {
544 Plugin *p = (Plugin *)cur->data;
545 if (!p->unloaded_hidden)
546 new = g_slist_prepend(new, p);
548 new = g_slist_reverse(new);
552 GSList *plugin_get_unloaded_list(void)
554 return g_slist_copy(unloaded_plugins);
557 const gchar *plugin_get_name(Plugin *plugin)
559 return plugin->name();
562 const gchar *plugin_get_desc(Plugin *plugin)
564 return plugin->desc();
567 const gchar *plugin_get_version(Plugin *plugin)
569 return plugin->version();
572 const gchar *plugin_get_error(Plugin *plugin)
574 return plugin->error;
577 /* Generally called in plugin_init() function of each plugin. It check the
578 * minimal and compiled version of claws binary required by the plugin.
579 * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
580 * check the corresponding version.
582 * If an error occurs {
583 * If @error == NULL { don't allocate error string. }
584 * If @error != NULL { error string is allocated and must be freed after
587 * Returns: FALSE if an error occurs, TRUE if all is OK.
589 gint check_plugin_version(guint32 minimum_claws_version,
590 guint32 compiled_claws_version,
591 const gchar *plugin_name,
594 guint32 claws_version = claws_get_version();
596 if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
598 *error = (plugin_name && *plugin_name)
599 ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
600 "version the '%s' plugin was built with."),
602 : g_strdup(_("Your version of Claws Mail is newer than the "
603 "version the plugin was built with."));
608 if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
610 *error = (plugin_name && *plugin_name)
611 ? g_strdup_printf(_("Your version of Claws Mail is too old for "
612 "the '%s' plugin."), plugin_name)
613 : g_strdup(_("Your version of Claws Mail is too old for the plugin."));