ad3d27f4824b324bd61a1bd8deb642500123d857
[claws.git] / src / common / plugin.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2015 the Claws Mail Team
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include <stdio.h>
25 #ifdef HAVE_VALGRIND
26 #include <valgrind.h>
27 #endif
28
29 #include "defs.h"
30 #include <glib.h>
31 #ifdef ENABLE_NLS
32 #include <glib/gi18n.h>
33 #else
34 #define _(a) (a)
35 #define N_(a) (a)
36 #endif
37 #include <gmodule.h>
38
39 #include "utils.h"
40 #include "plugin.h"
41 #include "prefs.h"
42 #include "claws.h"
43 #include "timing.h"
44
45 struct _Plugin
46 {
47         gchar   *filename;
48         GModule *module;
49         const gchar *(*name) (void);
50         const gchar *(*desc) (void);
51         const gchar *(*version) (void);
52         const gchar *(*type) (void);
53         const gchar *(*licence) (void);
54         struct PluginFeature *(*provides) (void);
55         
56         GSList *rdeps;
57         gchar *error;
58         gboolean unloaded_hidden;
59         gboolean in_prefix_dir;
60 };
61
62 const gchar *plugin_feature_names[] =
63         { N_("Nothing"),
64           N_("a viewer"),
65           N_("a MIME parser"),
66           N_("folders"),
67           N_("filtering"),
68           N_("a privacy interface"),
69           N_("a notifier"),
70           N_("an utility"),
71           N_("things"),
72           NULL };
73
74 /* The plugin must be at least under one of these licences and have
75    the corresponding token returned by the plugin_licence function.
76  */
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",
81   "GPL2+", "GPLv2+",
82   "Apache2.0", "Apache 2.0", "Apache v2.0",
83   "2-clause BSD", "Simplified BSD", "FreeBSD",
84   "3-clause BSD", "New BSD", "Modified BSD",
85   NULL
86 };
87
88 /* Dual (or more) licences are allowed, must be separated by one of these.
89  */
90 #define IS_LICENCE_SEP(a) ((a) == ',' || (a) == ';' || (a) == '|' || (a) == '/' || (a) == '\0')
91
92 /**
93  * List of all loaded plugins
94  */
95 GSList *plugins = NULL;
96 GSList *plugin_types = NULL;
97
98 /* 
99  * List of plugins unloaded for some fixable reason
100  */
101 static GSList *unloaded_plugins = NULL;
102
103 static gint list_find_by_string(gconstpointer data, gconstpointer str)
104 {
105         return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
106 }
107
108 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
109 {
110         /* FIXME: There is a problem in case of symlinks or when a
111            user tries to load a plugin with the same name from a
112            different directory.  I think it would be better to compare
113            only the basename of the filename here (case-insensitive on
114            W32). */
115         cm_return_val_if_fail(plugin, 1);
116         cm_return_val_if_fail(plugin->filename, 1);
117         cm_return_val_if_fail(filename, 1);
118         return strcmp(filename, plugin->filename);
119 }
120
121 static gboolean plugin_filename_is_standard_dir(const gchar *filename) {
122         return strncmp(filename, get_plugin_dir(), strlen(get_plugin_dir())) == 0;
123 }
124
125 static gchar * plugin_canonical_name(const Plugin *plugin)
126 {
127         if (plugin->in_prefix_dir == TRUE) {
128                 if (plugin_filename_is_standard_dir(plugin->filename) == TRUE) {
129                         gchar *plugin_name = g_path_get_basename(plugin->filename);
130                         return plugin_name;
131                 }
132         }
133         return g_strdup(plugin->filename);
134 }
135
136 void plugin_save_list(void)
137 {
138         gchar *rcpath, *block;
139         PrefFile *pfile;
140         GSList *type_cur, *plugin_cur;
141         Plugin *plugin;
142         
143         for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
144                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
145 #ifdef G_OS_WIN32
146                 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
147 #else
148                 block = g_strconcat("Plugins_", type_cur->data, NULL);
149 #endif
150                 if ((pfile = prefs_write_open(rcpath)) == NULL ||
151                     (prefs_set_block_label(pfile, block) < 0)) {
152                         g_warning("failed to write plugin list");
153                         g_free(rcpath);
154                         return;
155                 }
156                 g_free(block);
157
158                 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
159                         plugin = (Plugin *) plugin_cur->data;
160                         
161                         if (plugin->unloaded_hidden)
162                                 continue;
163
164                         if (!strcmp(plugin->type(), type_cur->data)) {
165                                 gchar * name = plugin_canonical_name(plugin);
166                                 int err = fprintf(pfile->fp, "%s\n", name);
167                                 g_free(name);
168                                 if (err < 0)
169                                         goto revert;
170                         }
171                 }
172                 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
173                         plugin = (Plugin *) plugin_cur->data;
174
175                         if (plugin->unloaded_hidden)
176                                 continue;
177                         
178                         if (!strcmp(plugin->type(), type_cur->data)) {
179                                 gchar * name = plugin_canonical_name(plugin);
180                                 int err = fprintf(pfile->fp, "%s\n", name);
181                                 g_free(name);
182                                 if (err < 0)
183                                         goto revert;
184                         }
185                 }
186                 if (fprintf(pfile->fp, "\n") < 0)
187                         goto revert;
188
189                 if (prefs_file_close(pfile) < 0)
190                         g_warning("failed to write plugin list");
191
192                 g_free(rcpath); 
193                 
194                 continue;
195
196 revert:
197                 g_warning("failed to write plugin list");
198                 if (prefs_file_close_revert(pfile) < 0)
199                         g_warning("failed to revert plugin list");
200
201                 g_free(rcpath); 
202         }
203 }
204
205 static gboolean plugin_is_loaded(const gchar *filename)
206 {
207         return (g_slist_find_custom(plugins, filename, 
208                   (GCompareFunc)list_find_by_plugin_filename) != NULL);
209 }
210
211 static Plugin *plugin_get_by_filename(const gchar *filename)
212 {
213         GSList *cur = plugins;
214         for(; cur; cur = cur->next) {
215                 Plugin *p = (Plugin *)cur->data;
216                 if (!strcmp(p->filename, filename)) {
217                         return p;
218                 } 
219         }
220         return NULL;
221 }
222
223 /** 
224  * Loads a plugin dependancies
225  * 
226  * Plugin dependancies are, optionnaly, listed in a file in
227  * get_plugin_dir()/$pluginname.deps.
228  * \param filename The filename of the plugin for which we have to load deps
229  * \param error The location where an error string can be stored
230  * \return 0 on success, -1 otherwise
231  */
232 static gint plugin_load_deps(const gchar *filename, gchar **error)
233 {
234         gchar *tmp;
235         gchar *deps_file = NULL;
236         FILE *fp = NULL;
237         gchar buf[BUFFSIZE];
238         gchar *p;
239
240         tmp = g_strdup(filename);
241         if( (p = strrchr(tmp, '.')) )
242           *p = '\0';
243         deps_file = g_strconcat(tmp, ".deps", NULL);
244         g_free(tmp);
245         
246         fp = g_fopen(deps_file, "rb");
247         g_free(deps_file);
248         
249         if (!fp)
250                 return 0;
251         
252         while (fgets(buf, sizeof(buf), fp) != NULL) {
253                 Plugin *dep_plugin = NULL;
254                 gchar *path = NULL;
255                 buf[strlen(buf)-1]='\0'; /* chop off \n */
256                 path = g_strconcat(get_plugin_dir(), buf,
257                                 ".", G_MODULE_SUFFIX, NULL);
258                 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
259                         debug_print("trying to load %s\n", path);
260                         dep_plugin = plugin_load(path, error);
261                         if (dep_plugin == NULL) {
262                                 g_free(path);
263                                 fclose(fp);
264                                 return -1;
265                         }
266                         dep_plugin->in_prefix_dir = TRUE;
267                 }
268                 g_free(path);
269                 if (!g_slist_find_custom(dep_plugin->rdeps, 
270                                 (gpointer) filename, list_find_by_string)) {
271                         debug_print("adding %s to %s rdeps\n",
272                                 filename,
273                                 dep_plugin->filename);
274                         dep_plugin->rdeps = 
275                                 g_slist_append(dep_plugin->rdeps, 
276                                         g_strdup(filename));
277                 }
278         }
279         fclose(fp);
280         return 0;
281 }
282
283 static void plugin_unload_rdeps(Plugin *plugin)
284 {
285         GSList *cur = plugin->rdeps;
286         debug_print("removing %s rdeps\n", plugin->filename);
287         while (cur) {
288                 gchar *file = (gchar *)cur->data;
289                 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
290                 debug_print(" rdep %s: %p\n", file, rdep_plugin);
291                 if (rdep_plugin) {
292                         plugin_unload(rdep_plugin);
293                 }
294                 g_free(file);
295                 cur = cur->next;
296         }
297         g_slist_free(plugin->rdeps);
298         plugin->rdeps = NULL;
299 }
300
301 static void plugin_remove_from_unloaded_list (const gchar *filename)
302 {
303         GSList *item = g_slist_find_custom(unloaded_plugins, 
304                                 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
305         Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
306         if (unloaded_plugin != NULL) {
307                 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
308                 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
309                 g_module_close(unloaded_plugin->module);
310                 g_free(unloaded_plugin->filename);
311                 g_free(unloaded_plugin->error);
312                 g_free(unloaded_plugin);
313         }
314 }
315
316 static gchar *plugin_check_features(struct PluginFeature *features) {
317         int i = 0, j = 0;
318         GSList *cur = plugins;
319
320         if (features == NULL)
321                 return NULL;
322         for(; cur; cur = cur->next) {
323                 Plugin *p = (Plugin *)cur->data;
324                 struct PluginFeature *cur_features = p->provides();
325                 if (p->unloaded_hidden)
326                         continue;
327                 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
328                         for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
329                                 if (cur_features[j].type == features[i].type &&
330                                     !strcmp(cur_features[j].subtype, features[i].subtype)) {
331                                         return g_strdup_printf(_(
332                                                 "This plugin provides %s (%s), which is "
333                                                 "already provided by the %s plugin."),
334                                                 _(plugin_feature_names[features[i].type]), 
335                                                 _(features[i].subtype),
336                                                 p->name());
337                                 }
338                         }
339                 }
340         }
341
342         return NULL;
343 }
344
345 static gboolean plugin_licence_check(const gchar *licence) {
346         gint i = 0;
347         gint len = 0;
348         
349         if (licence != NULL) {
350                 len = strlen(licence);
351         }
352         if (len == 0) {
353                 g_warning("plugin licence check failed: empty licence");
354                 return FALSE;
355         }
356         while (plugin_licence_tokens[i] != NULL) {
357                 gchar *found = g_strstr_len(licence, len, plugin_licence_tokens[i]);
358                 if (found != NULL) {
359                         gint tlen = strlen(plugin_licence_tokens[i]);
360                         if (len != tlen) { /* not a single license */
361                                 if (((found == licence) &&  (!IS_LICENCE_SEP(licence[tlen])))
362                                                 || (!IS_LICENCE_SEP(*(found - 1))) 
363                                                 || (!IS_LICENCE_SEP(*(found + tlen)))) {
364                                         debug_print("plugin licence check failed: invalid separator\n");
365                                         return FALSE;
366                                 }
367                         }
368                         debug_print("plugin licence check passed: %s found\n", plugin_licence_tokens[i]);
369                         return TRUE;
370                 }
371                 ++i;
372         }
373         debug_print("plugin licence check failed: %s is not a valid licence\n", licence);
374         return FALSE;
375 }
376
377 static Plugin *plugin_load_in_default_dir(const gchar *filename, gchar **error)
378 {
379         Plugin *plugin = NULL;
380         gchar *filename_default_dir = NULL;
381         gchar *default_error = NULL;
382         gchar *plugin_name = g_path_get_basename(filename);
383
384         filename_default_dir = g_strconcat(get_plugin_dir(), plugin_name, NULL);
385
386         debug_print("trying to load %s in default plugin directory %s\n",
387                     plugin_name, get_plugin_dir());
388
389         g_free(plugin_name);
390
391         plugin = plugin_load(filename_default_dir, &default_error);
392         
393         g_free(filename_default_dir);
394         
395         if (plugin) {
396                 g_free(*error);
397                 *error = NULL;
398                 plugin->in_prefix_dir = TRUE;
399
400         } else {
401                 g_free(default_error);
402         }
403
404         return plugin;
405 }
406
407 /**
408  * Loads a plugin
409  *
410  * \param filename The filename of the plugin to load
411  * \param error The location where an error string can be stored
412  * \return the plugin on success, NULL otherwise
413  */
414 Plugin *plugin_load(const gchar *filename, gchar **error)
415 {
416         Plugin *plugin;
417         gint (*plugin_init) (gchar **error);
418         gpointer plugin_name, plugin_desc, plugin_version;
419         const gchar *(*plugin_type)(void);
420         const gchar *(*plugin_licence)(void);
421         struct PluginFeature *(*plugin_provides)(void);
422         gint ok;
423         START_TIMING((filename?filename:"NULL plugin"));
424         cm_return_val_if_fail(filename != NULL, NULL);
425         cm_return_val_if_fail(error != NULL, NULL);
426
427         /* check duplicate plugin path name */
428         if (plugin_is_loaded(filename)) {
429                 plugin = plugin_get_by_filename(filename);
430                 if (plugin->unloaded_hidden) {
431                         /* reshow it */
432                         goto init_plugin;
433                 } else {
434                         *error = g_strdup(_("Plugin already loaded"));
435                         return NULL;            
436                 }
437         }                              
438         
439         plugin_remove_from_unloaded_list(filename);
440         
441         if (plugin_load_deps(filename, error) < 0)
442                 return NULL;
443         plugin = g_new0(Plugin, 1);
444         if (plugin == NULL) {
445                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
446                 return NULL;
447         }
448
449         debug_print("trying to load `%s'\n", filename);
450         plugin->module = g_module_open(filename, 0);
451         if (plugin->module == NULL) {
452                 *error = g_strdup(g_module_error());
453                 g_free(plugin);
454                 if (!plugin_filename_is_standard_dir(filename))
455                         return plugin_load_in_default_dir(filename, error);
456                 else
457                         return NULL;
458         } else {
459                 plugin->in_prefix_dir = FALSE;
460         }
461
462 init_plugin:
463         if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
464             !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
465             !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
466             !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
467             !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
468             !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
469             !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
470                 *error = g_strdup(g_module_error());
471                 if (plugin->unloaded_hidden)
472                         return NULL;
473                 g_module_close(plugin->module);
474                 g_free(plugin);
475                 return NULL;
476         }
477         
478         if (plugin_licence_check(plugin_licence()) != TRUE) {
479                 *error = g_strdup(_("This module is not licensed under a GPL v3 or later compatible license."));
480                 if (plugin->unloaded_hidden)
481                         return NULL;
482                 g_module_close(plugin->module);
483                 g_free(plugin);
484                 return NULL;
485         }
486
487         if (!strcmp(plugin_type(), "GTK")) {
488                 *error = g_strdup(_("This module is for Claws Mail GTK1."));
489                 if (plugin->unloaded_hidden)
490                         return NULL;
491                 g_module_close(plugin->module);
492                 g_free(plugin);
493                 return NULL;
494         }
495
496         if ((*error = plugin_check_features(plugin_provides())) != NULL) {
497                 if (plugin->unloaded_hidden)
498                         return NULL;
499                 g_module_close(plugin->module);
500                 g_free(plugin);
501                 return NULL;
502         }
503         plugin->name = plugin_name;
504         plugin->desc = plugin_desc;
505         plugin->version = plugin_version;
506         plugin->type = plugin_type;
507         plugin->licence = plugin_licence;
508         plugin->provides = plugin_provides;
509         plugin->filename = g_strdup(filename);
510         plugin->error = NULL;
511
512         if ((ok = plugin_init(error)) < 0) {
513                 if (*error)
514                         plugin->error = g_strdup(*error);
515                 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
516                 return NULL;
517         }
518
519         if (!plugin->unloaded_hidden)
520                 plugins = g_slist_append(plugins, plugin);
521         plugin->unloaded_hidden = FALSE;
522
523         debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
524         END_TIMING();
525         return plugin;
526 }
527
528 void plugin_unload(Plugin *plugin)
529 {
530         gboolean (*plugin_done) (void);
531         gboolean can_unload = TRUE;
532
533         plugin_unload_rdeps(plugin);
534
535         if (plugin->unloaded_hidden)
536                 return;
537
538         if (plugin->error) {
539                 plugin_remove_from_unloaded_list(plugin->filename);
540                 return;
541         }
542         if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
543                 can_unload = plugin_done();
544         }
545
546         if (can_unload) {
547 #ifdef HAVE_VALGRIND
548                 if (!RUNNING_ON_VALGRIND) {
549                         g_module_close(plugin->module);
550                 }
551 #else
552                 g_module_close(plugin->module);
553 #endif
554                 plugins = g_slist_remove(plugins, plugin);
555                 g_free(plugin->filename);
556                 g_free(plugin);
557         } else {
558                 plugin->unloaded_hidden = TRUE;
559         }
560
561 }
562
563 static void replace_old_plugin_name(gchar *plugin_name)
564 {
565         gchar *old_name_end = g_strconcat("_plugin.", G_MODULE_SUFFIX, NULL);
566         gchar *matches = strstr(plugin_name, old_name_end);
567
568         if (!matches) {
569                 g_free(old_name_end);
570                 return;
571         } else if (plugin_name + strlen(plugin_name) != matches + strlen(matches)) {
572                 g_free(old_name_end);
573                 return;
574         } else {
575                 gchar *new_name_end = g_strconcat(".", G_MODULE_SUFFIX, NULL);
576                 int offset = strlen(plugin_name) - strlen(old_name_end);
577
578                 debug_print("Replacing old plugin name %s\n", plugin_name);
579                 
580                 strncpy(plugin_name + offset, new_name_end, strlen(old_name_end) - 1);
581                 debug_print(" to %s\n", plugin_name);
582         }
583 }
584
585 void plugin_load_all(const gchar *type)
586 {
587         gchar *rcpath;
588         gchar buf[BUFFSIZE];
589         PrefFile *pfile;
590         gchar *error = NULL, *block;
591
592         plugin_types = g_slist_append(plugin_types, g_strdup(type));
593
594         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); 
595 #ifdef G_OS_WIN32
596         block = g_strconcat("PluginsWin32_", type, NULL);
597 #else
598         block = g_strconcat("Plugins_", type, NULL);
599 #endif
600         if ((pfile = prefs_read_open(rcpath)) == NULL ||
601             (prefs_set_block_label(pfile, block) < 0)) {
602                 g_free(rcpath);
603                 if (pfile)
604                         prefs_file_close(pfile);
605                 return;
606         }
607         g_free(block);
608
609         while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
610                 if (buf[0] == '[')
611                         break;
612
613                 g_strstrip(buf);
614                 replace_old_plugin_name(buf);
615
616                 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
617                         g_warning("plugin loading error: %s", error);
618                         g_free(error);
619                 }                                                       
620         }
621         prefs_file_close(pfile);
622
623         g_free(rcpath);
624 }
625
626 void plugin_unload_all(const gchar *type)
627 {
628         GSList *list, *cur;
629
630         list = g_slist_copy(plugins);
631         list = g_slist_reverse(list);
632
633         for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
634                 Plugin *plugin = (Plugin *) cur->data;
635                 
636                 if (!strcmp(type, plugin->type()))
637                         plugin_unload(plugin);
638         }
639         g_slist_free(list);
640
641         cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
642         if (cur) {
643                 g_free(cur->data);
644                 plugin_types = g_slist_remove(plugin_types, cur);
645         }
646 }
647
648
649 /* Load those plugins we always want to use.  No error output; just
650  * try. */
651 void plugin_load_standard_plugins (void)
652 {
653         static const char *names[] = {
654 #ifdef G_OS_WIN32 
655                 "pgpmime",
656                 "pgpinline",
657 #else
658                 /* post-2.5 maybe 
659                 "bogofilter", */
660 #endif
661                 NULL
662         };
663         int i;
664         gchar *error, *filename;
665         
666         for (i=0; names[i]; i++) {
667                 /* Simple hack to check whether the plugin has already
668                  * been loaded but checking only for the basename. */
669                 GSList *cur = plugins;
670                 for(; cur; cur = cur->next) {
671                         Plugin *p = (Plugin *)cur->data;
672                         if (strstr(p->filename, names[i]))
673                                 break;
674                 }
675                 if (!cur) { /* Not yet loaded. */
676                         /* FIXME: get_plugin_dir () returns with a trailing
677                          * (back)slash; this should be fixed so that we can use
678                          * g_module_build_path here. */
679 #ifdef G_OS_WIN32 
680                         filename = g_strconcat (get_plugin_dir(),
681                                                 names[i], NULL);
682 #else
683                         filename = g_strconcat (get_plugin_dir(),
684                                                 names[i], ".", G_MODULE_SUFFIX, NULL);
685 #endif
686                         error = NULL;
687                         plugin_load(filename, &error);
688                         g_free (error);
689                         g_free(filename);
690                 }
691         }
692 }
693
694 GSList *plugin_get_list(void)
695 {
696         GSList *new = NULL;
697         GSList *cur = plugins;
698         for (; cur; cur = cur->next) {
699                 Plugin *p = (Plugin *)cur->data;
700                 if (!p->unloaded_hidden)
701                         new = g_slist_prepend(new, p);
702         }
703         new = g_slist_reverse(new);
704         return new;
705 }
706
707 Plugin *plugin_get_loaded_by_name(const gchar *name) 
708 {
709         Plugin *plugin = NULL;
710         GSList *new, *cur; 
711         new = plugin_get_list();
712         for (cur = new; cur; cur = g_slist_next(cur)) {
713                 plugin = (Plugin *)cur->data;
714                 if (!g_ascii_strcasecmp(plugin->name(), name)) 
715                         break;
716                 else 
717                         plugin = NULL;
718         }
719         g_slist_free(new);
720         return plugin;
721 }
722
723 GSList *plugin_get_unloaded_list(void)
724 {
725         return g_slist_copy(unloaded_plugins);
726 }
727
728 const gchar *plugin_get_name(Plugin *plugin)
729 {
730         return plugin->name();
731 }
732
733 const gchar *plugin_get_desc(Plugin *plugin)
734 {
735         return plugin->desc();
736 }
737
738 const gchar *plugin_get_version(Plugin *plugin)
739 {
740         return plugin->version();
741 }
742
743 const gchar *plugin_get_error(Plugin *plugin)
744 {
745         return plugin->error;
746 }
747
748 /* Generally called in plugin_init() function of each plugin. It check the
749  * minimal and compiled version of claws binary required by the plugin.
750  * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
751  * check the corresponding version.
752  *
753  * If an error occurs {
754  *      If @error == NULL { don't allocate error string. }
755  *      If @error != NULL { error string is allocated and must be freed after 
756  *                              call function. }
757  * }
758  * Returns: FALSE if an error occurs, TRUE if all is OK.
759  */
760 gint check_plugin_version(guint32 minimum_claws_version,
761                          guint32 compiled_claws_version,
762                          const gchar *plugin_name,
763                          gchar **error)
764 {
765         guint32 claws_version = claws_get_version();
766
767         if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
768                 if (error != NULL) {
769                         *error = (plugin_name && *plugin_name)
770                                 ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
771                                                         "version the '%s' plugin was built with."),
772                                                 plugin_name)
773                                 : g_strdup(_("Your version of Claws Mail is newer than the "
774                                                         "version the plugin was built with."));
775                 }
776                 return FALSE;
777         }
778
779         if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
780                 if (error != NULL) {
781                         *error = (plugin_name && *plugin_name)
782                                 ? g_strdup_printf(_("Your version of Claws Mail is too old for "
783                                                         "the '%s' plugin."), plugin_name)
784                                 : g_strdup(_("Your version of Claws Mail is too old for the plugin."));
785                 }
786                 return FALSE;
787         }
788         return TRUE;
789 }