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