Harmonise GUI maximum value with libspamc's one
[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 #include "file-utils.h"
45
46 #ifdef G_OS_WIN32
47 #define PLUGINS_BLOCK_PREFIX "PluginsWin32_"
48 #else
49 #define PLUGINS_BLOCK_PREFIX "Plugins_"
50 #endif
51
52 struct _Plugin
53 {
54         gchar   *filename;
55         GModule *module;
56         const gchar *(*name) (void);
57         const gchar *(*desc) (void);
58         const gchar *(*version) (void);
59         const gchar *(*type) (void);
60         const gchar *(*licence) (void);
61         struct PluginFeature *(*provides) (void);
62         
63         GSList *rdeps;
64         gchar *error;
65         gboolean unloaded_hidden;
66         gboolean in_prefix_dir;
67 };
68
69 const gchar *plugin_feature_names[] =
70         { N_("Nothing"),
71           N_("a viewer"),
72           N_("a MIME parser"),
73           N_("folders"),
74           N_("filtering"),
75           N_("a privacy interface"),
76           N_("a notifier"),
77           N_("an utility"),
78           N_("things"),
79           NULL };
80
81 /* The plugin must be at least under one of these licences and have
82    the corresponding token returned by the plugin_licence function.
83  */
84 const gchar *plugin_licence_tokens[] = {
85   "LGPL2.1+", "LGPLv2.1+", "LGPL2.1", "LGPLv2.1",
86   "LGPL3+", "LGPLv3+", "LGPL3", "LGPLv3",
87   "GPL3+", "GPLv3+", "GPL3", "GPLv3",
88   "GPL2+", "GPLv2+",
89   "Apache2.0", "Apache 2.0", "Apache v2.0",
90   "2-clause BSD", "Simplified BSD", "FreeBSD",
91   "3-clause BSD", "New BSD", "Modified BSD",
92   NULL
93 };
94
95 /* Dual (or more) licences are allowed, must be separated by one of these.
96  */
97 #define IS_LICENCE_SEP(a) ((a) == ',' || (a) == ';' || (a) == '|' || (a) == '/' || (a) == '\0')
98
99 /**
100  * List of all loaded plugins
101  */
102 GSList *plugins = NULL;
103 GSList *plugin_types = NULL;
104
105 /* 
106  * List of plugins unloaded for some fixable reason
107  */
108 static GSList *unloaded_plugins = NULL;
109
110 static gint list_find_by_string(gconstpointer data, gconstpointer str)
111 {
112         return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
113 }
114
115 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
116 {
117         /* FIXME: There is a problem in case of symlinks or when a
118            user tries to load a plugin with the same name from a
119            different directory.  I think it would be better to compare
120            only the basename of the filename here (case-insensitive on
121            W32). */
122         cm_return_val_if_fail(plugin, 1);
123         cm_return_val_if_fail(plugin->filename, 1);
124         cm_return_val_if_fail(filename, 1);
125         return strcmp(filename, plugin->filename);
126 }
127
128 static gboolean plugin_filename_is_standard_dir(const gchar *filename) {
129         return strncmp(filename, get_plugin_dir(), strlen(get_plugin_dir())) == 0;
130 }
131
132 static gchar * plugin_canonical_name(const Plugin *plugin)
133 {
134         if (plugin->in_prefix_dir == TRUE) {
135                 if (plugin_filename_is_standard_dir(plugin->filename) == TRUE) {
136                         gchar *plugin_name = g_path_get_basename(plugin->filename);
137                         return plugin_name;
138                 }
139         }
140         return g_strdup(plugin->filename);
141 }
142
143 void plugin_save_list(void)
144 {
145         gchar *rcpath, *block;
146         PrefFile *pfile;
147         GSList *type_cur, *plugin_cur;
148         Plugin *plugin;
149         
150         for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
151                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
152                 block = g_strconcat(PLUGINS_BLOCK_PREFIX, type_cur->data, NULL);
153                 if ((pfile = prefs_write_open(rcpath)) == NULL ||
154                     (prefs_set_block_label(pfile, block) < 0)) {
155                         g_warning("failed to write plugin list");
156                         g_free(rcpath);
157                         return;
158                 }
159                 g_free(block);
160
161                 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
162                         plugin = (Plugin *) plugin_cur->data;
163                         
164                         if (plugin->unloaded_hidden)
165                                 continue;
166
167                         if (!strcmp(plugin->type(), type_cur->data)) {
168                                 gchar * name = plugin_canonical_name(plugin);
169                                 int err = fprintf(pfile->fp, "%s\n", name);
170                                 g_free(name);
171                                 if (err < 0)
172                                         goto revert;
173                         }
174                 }
175                 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
176                         plugin = (Plugin *) plugin_cur->data;
177
178                         if (plugin->unloaded_hidden)
179                                 continue;
180                         
181                         if (!strcmp(plugin->type(), type_cur->data)) {
182                                 gchar * name = plugin_canonical_name(plugin);
183                                 int err = fprintf(pfile->fp, "%s\n", name);
184                                 g_free(name);
185                                 if (err < 0)
186                                         goto revert;
187                         }
188                 }
189                 if (fprintf(pfile->fp, "\n") < 0)
190                         goto revert;
191
192                 if (prefs_file_close(pfile) < 0)
193                         g_warning("failed to write plugin list");
194
195                 g_free(rcpath); 
196                 
197                 continue;
198
199 revert:
200                 g_warning("failed to write plugin list");
201                 if (prefs_file_close_revert(pfile) < 0)
202                         g_warning("failed to revert plugin list");
203
204                 g_free(rcpath); 
205         }
206 }
207
208 static gboolean plugin_is_loaded(const gchar *filename)
209 {
210         return (g_slist_find_custom(plugins, filename, 
211                   (GCompareFunc)list_find_by_plugin_filename) != NULL);
212 }
213
214 static Plugin *plugin_get_by_filename(const gchar *filename)
215 {
216         GSList *cur = plugins;
217         for(; cur; cur = cur->next) {
218                 Plugin *p = (Plugin *)cur->data;
219                 if (!strcmp(p->filename, filename)) {
220                         return p;
221                 } 
222         }
223         return NULL;
224 }
225
226 /** 
227  * Loads a plugin dependancies
228  * 
229  * Plugin dependancies are, optionnaly, listed in a file in
230  * get_plugin_dir()/$pluginname.deps.
231  * \param filename The filename of the plugin for which we have to load deps
232  * \param error The location where an error string can be stored
233  * \return 0 on success, -1 otherwise
234  */
235 static gint plugin_load_deps(const gchar *filename, gchar **error)
236 {
237         gchar *tmp;
238         gchar *deps_file = NULL;
239         FILE *fp = NULL;
240         gchar buf[BUFFSIZE];
241         gchar *p;
242
243         tmp = g_strdup(filename);
244         if( (p = strrchr(tmp, '.')) )
245           *p = '\0';
246         deps_file = g_strconcat(tmp, ".deps", NULL);
247         g_free(tmp);
248         
249         fp = claws_fopen(deps_file, "rb");
250         g_free(deps_file);
251         
252         if (!fp)
253                 return 0;
254         
255         while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
256                 Plugin *dep_plugin = NULL;
257                 gchar *path = NULL;
258                 buf[strlen(buf)-1]='\0'; /* chop off \n */
259                 path = g_strconcat(get_plugin_dir(), buf,
260                                 ".", G_MODULE_SUFFIX, NULL);
261                 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
262                         debug_print("trying to load %s\n", path);
263                         dep_plugin = plugin_load(path, error);
264                         if (dep_plugin == NULL) {
265                                 g_free(path);
266                                 claws_fclose(fp);
267                                 return -1;
268                         }
269                         dep_plugin->in_prefix_dir = TRUE;
270                 }
271                 g_free(path);
272                 if (!g_slist_find_custom(dep_plugin->rdeps, 
273                                 (gpointer) filename, list_find_by_string)) {
274                         debug_print("adding %s to %s rdeps\n",
275                                 filename,
276                                 dep_plugin->filename);
277                         dep_plugin->rdeps = 
278                                 g_slist_append(dep_plugin->rdeps, 
279                                         g_strdup(filename));
280                 }
281         }
282         claws_fclose(fp);
283         return 0;
284 }
285
286 static void plugin_unload_rdeps(Plugin *plugin)
287 {
288         GSList *cur = plugin->rdeps;
289         debug_print("removing %s rdeps\n", plugin->filename);
290         while (cur) {
291                 gchar *file = (gchar *)cur->data;
292                 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
293                 debug_print(" rdep %s: %p\n", file, rdep_plugin);
294                 if (rdep_plugin) {
295                         plugin_unload(rdep_plugin);
296                 }
297                 g_free(file);
298                 cur = cur->next;
299         }
300         g_slist_free(plugin->rdeps);
301         plugin->rdeps = NULL;
302 }
303
304 static void plugin_remove_from_unloaded_list (const gchar *filename)
305 {
306         GSList *item = g_slist_find_custom(unloaded_plugins, 
307                                 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
308         Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
309         if (unloaded_plugin != NULL) {
310                 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
311                 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
312                 g_module_close(unloaded_plugin->module);
313                 g_free(unloaded_plugin->filename);
314                 g_free(unloaded_plugin->error);
315                 g_free(unloaded_plugin);
316         }
317 }
318
319 static gchar *plugin_check_features(struct PluginFeature *features) {
320         int i = 0, j = 0;
321         GSList *cur = plugins;
322
323         if (features == NULL)
324                 return NULL;
325         for(; cur; cur = cur->next) {
326                 Plugin *p = (Plugin *)cur->data;
327                 struct PluginFeature *cur_features = p->provides();
328                 if (p->unloaded_hidden)
329                         continue;
330                 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
331                         for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
332                                 if (cur_features[j].type == features[i].type &&
333                                     !strcmp(cur_features[j].subtype, features[i].subtype)) {
334                                         return g_strdup_printf(_(
335                                                 "This plugin provides %s (%s), which is "
336                                                 "already provided by the %s plugin."),
337                                                 _(plugin_feature_names[features[i].type]), 
338                                                 _(features[i].subtype),
339                                                 p->name());
340                                 }
341                         }
342                 }
343         }
344
345         return NULL;
346 }
347
348 static gboolean plugin_licence_check(const gchar *licence) {
349         gint i = 0;
350         gint len = 0;
351         
352         if (licence != NULL) {
353                 len = strlen(licence);
354         }
355         if (len == 0) {
356                 g_warning("plugin licence check failed: empty licence");
357                 return FALSE;
358         }
359         while (plugin_licence_tokens[i] != NULL) {
360                 gchar *found = g_strstr_len(licence, len, plugin_licence_tokens[i]);
361                 if (found != NULL) {
362                         gint tlen = strlen(plugin_licence_tokens[i]);
363                         if (len != tlen) { /* not a single license */
364                                 if (((found == licence) &&  (!IS_LICENCE_SEP(licence[tlen])))
365                                                 || (!IS_LICENCE_SEP(*(found - 1))) 
366                                                 || (!IS_LICENCE_SEP(*(found + tlen)))) {
367                                         debug_print("plugin licence check failed: invalid separator\n");
368                                         return FALSE;
369                                 }
370                         }
371                         debug_print("plugin licence check passed: %s found\n", plugin_licence_tokens[i]);
372                         return TRUE;
373                 }
374                 ++i;
375         }
376         debug_print("plugin licence check failed: %s is not a valid licence\n", licence);
377         return FALSE;
378 }
379
380 static Plugin *plugin_load_in_default_dir(const gchar *filename, gchar **error)
381 {
382         Plugin *plugin = NULL;
383         gchar *filename_default_dir = NULL;
384         gchar *default_error = NULL;
385         gchar *plugin_name = g_path_get_basename(filename);
386
387         filename_default_dir = g_strconcat(get_plugin_dir(), plugin_name, NULL);
388
389         debug_print("trying to load %s in default plugin directory %s\n",
390                     plugin_name, get_plugin_dir());
391
392         g_free(plugin_name);
393
394         plugin = plugin_load(filename_default_dir, &default_error);
395         
396         g_free(filename_default_dir);
397         
398         if (plugin) {
399                 g_free(*error);
400                 *error = NULL;
401                 plugin->in_prefix_dir = TRUE;
402
403         } else {
404                 g_free(default_error);
405         }
406
407         return plugin;
408 }
409
410 /**
411  * Loads a plugin
412  *
413  * \param filename The filename of the plugin to load
414  * \param error The location where an error string can be stored
415  * \return the plugin on success, NULL otherwise
416  */
417 Plugin *plugin_load(const gchar *filename, gchar **error)
418 {
419         Plugin *plugin;
420         gint (*plugin_init) (gchar **error);
421         gpointer plugin_name, plugin_desc, plugin_version;
422         const gchar *(*plugin_type)(void);
423         const gchar *(*plugin_licence)(void);
424         struct PluginFeature *(*plugin_provides)(void);
425
426         gint ok;
427         START_TIMING((filename?filename:"NULL plugin"));
428         cm_return_val_if_fail(filename != NULL, NULL);
429         cm_return_val_if_fail(error != NULL, NULL);
430
431         /* check duplicate plugin path name */
432         if (plugin_is_loaded(filename)) {
433                 plugin = plugin_get_by_filename(filename);
434                 if (plugin->unloaded_hidden) {
435                         /* reshow it */
436                         goto init_plugin;
437                 } else {
438                         *error = g_strdup(_("Plugin already loaded"));
439                         return NULL;            
440                 }
441         }                              
442         
443         plugin_remove_from_unloaded_list(filename);
444         
445         if (plugin_load_deps(filename, error) < 0)
446                 return NULL;
447         plugin = g_new0(Plugin, 1);
448         if (plugin == NULL) {
449                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
450                 return NULL;
451         }
452
453         debug_print("trying to load `%s'\n", filename);
454         plugin->module = g_module_open(filename, 0);
455         if (plugin->module == NULL) {
456                 *error = g_strdup(g_module_error());
457                 g_free(plugin);
458                 if (!plugin_filename_is_standard_dir(filename))
459                         return plugin_load_in_default_dir(filename, error);
460                 else
461                         return NULL;
462         } else {
463                 plugin->in_prefix_dir = FALSE;
464         }
465
466 init_plugin:
467         if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
468             !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
469             !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
470             !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
471             !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
472             !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
473             !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
474                 *error = g_strdup(g_module_error());
475                 if (plugin->unloaded_hidden)
476                         return NULL;
477                 g_module_close(plugin->module);
478                 g_free(plugin);
479                 return NULL;
480         }
481
482         if (plugin_licence_check(plugin_licence()) != TRUE) {
483                 *error = g_strdup(_("This module is not licensed under a GPL v3 or later compatible license."));
484                 if (plugin->unloaded_hidden)
485                         return NULL;
486                 g_module_close(plugin->module);
487                 g_free(plugin);
488                 return NULL;
489         }
490
491         if (!strcmp(plugin_type(), "GTK")) {
492                 *error = g_strdup(_("This module is for Claws Mail GTK1."));
493                 if (plugin->unloaded_hidden)
494                         return NULL;
495                 g_module_close(plugin->module);
496                 g_free(plugin);
497                 return NULL;
498         }
499
500         if ((*error = plugin_check_features(plugin_provides())) != NULL) {
501                 if (plugin->unloaded_hidden)
502                         return NULL;
503                 g_module_close(plugin->module);
504                 g_free(plugin);
505                 return NULL;
506         }
507         plugin->name = plugin_name;
508         plugin->desc = plugin_desc;
509         plugin->version = plugin_version;
510         plugin->type = plugin_type;
511         plugin->licence = plugin_licence;
512         plugin->provides = plugin_provides;
513         plugin->filename = g_strdup(filename);
514         plugin->error = NULL;
515
516         if ((ok = plugin_init(error)) < 0) {
517                 if (*error)
518                         plugin->error = g_strdup(*error);
519                 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
520                 return NULL;
521         }
522
523         if (!plugin->unloaded_hidden)
524                 plugins = g_slist_append(plugins, plugin);
525         plugin->unloaded_hidden = FALSE;
526
527         debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
528         END_TIMING();
529         return plugin;
530 }
531
532 void plugin_unload(Plugin *plugin)
533 {
534         gboolean (*plugin_done) (void);
535         gboolean can_unload = TRUE;
536
537         plugin_unload_rdeps(plugin);
538
539         if (plugin->unloaded_hidden)
540                 return;
541
542         if (plugin->error) {
543                 plugin_remove_from_unloaded_list(plugin->filename);
544                 return;
545         }
546         if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
547                 can_unload = plugin_done();
548         }
549
550         if (can_unload) {
551 #ifdef HAVE_VALGRIND
552                 if (!RUNNING_ON_VALGRIND) {
553                         g_module_close(plugin->module);
554                 }
555 #else
556                 g_module_close(plugin->module);
557 #endif
558                 plugins = g_slist_remove(plugins, plugin);
559                 g_free(plugin->filename);
560                 g_free(plugin);
561         } else {
562                 plugin->unloaded_hidden = TRUE;
563         }
564
565 }
566
567 static void replace_old_plugin_name(gchar *plugin_name)
568 {
569         gchar *old_name_end = g_strconcat("_plugin.", G_MODULE_SUFFIX, NULL);
570         gchar *matches = strstr(plugin_name, old_name_end);
571
572         if (!matches) {
573                 g_free(old_name_end);
574                 return;
575         } else if (plugin_name + strlen(plugin_name) != matches + strlen(matches)) {
576                 g_free(old_name_end);
577                 return;
578         } else {
579                 gchar *new_name_end = g_strconcat(".", G_MODULE_SUFFIX, NULL);
580                 int offset = strlen(plugin_name) - strlen(old_name_end);
581
582                 debug_print("Replacing old plugin name %s\n", plugin_name);
583                 
584                 strncpy(plugin_name + offset, new_name_end, strlen(old_name_end) - 1);
585                 debug_print(" to %s\n", plugin_name);
586         }
587 }
588
589 void plugin_load_all(const gchar *type)
590 {
591         gchar *rcpath;
592         gchar buf[BUFFSIZE];
593         PrefFile *pfile;
594         gchar *error = NULL, *block;
595
596         plugin_types = g_slist_append(plugin_types, g_strdup(type));
597
598         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); 
599         block = g_strconcat(PLUGINS_BLOCK_PREFIX, type, NULL);
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 (claws_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 }