2007-06-21 [colin] 2.9.2cvs72
[claws.git] / src / common / plugin.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdio.h>
21
22 #include "defs.h"
23 #include <glib.h>
24 #ifdef ENABLE_NLS
25 #include <glib/gi18n.h>
26 #else
27 #define _(a) (a)
28 #define N_(a) (a)
29 #endif
30 #include <gmodule.h>
31
32 #include "utils.h"
33 #include "plugin.h"
34 #include "prefs.h"
35 #include "claws.h"
36 #ifdef HAVE_VALGRIND
37 #include "valgrind.h"
38 #endif
39 struct _Plugin
40 {
41         gchar   *filename;
42         GModule *module;
43         const gchar *(*name) (void);
44         const gchar *(*desc) (void);
45         const gchar *(*version) (void);
46         const gchar *(*type) (void);
47         const gchar *(*licence) (void);
48         struct PluginFeature *(*provides) (void);
49         
50         GSList *rdeps;
51         gchar *error;
52         gboolean unloaded_hidden;
53 };
54
55 const gchar *plugin_feature_names[] =
56         { N_("Nothing"),
57           N_("a viewer"),
58           N_("folders"),
59           N_("filtering"),
60           N_("a privacy interface"),
61           N_("a notifier"),
62           N_("an utility"),
63           N_("things"),
64           NULL };
65
66 /**
67  * List of all loaded plugins
68  */
69 GSList *plugins = NULL;
70 GSList *plugin_types = NULL;
71
72 /* 
73  * List of plugins unloaded for some fixable reason
74  */
75 static GSList *unloaded_plugins = NULL;
76
77 static gint list_find_by_string(gconstpointer data, gconstpointer str)
78 {
79         return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
80 }
81
82 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
83 {
84         /* FIXME: There is a problem in case of symlinks or when a
85            user tries to load a plugin with the same name from a
86            different directory.  I think it would be better to compare
87            only the basename of the filename here (case-insensitive on
88            W32). */
89         g_return_val_if_fail(plugin, 1);
90         g_return_val_if_fail(plugin->filename, 1);
91         g_return_val_if_fail(filename, 1);
92         return strcmp(filename, plugin->filename);
93 }
94
95 void plugin_save_list(void)
96 {
97         gchar *rcpath, *block;
98         PrefFile *pfile;
99         GSList *type_cur, *plugin_cur;
100         Plugin *plugin;
101
102         for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
103                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
104 #ifdef G_OS_WIN32
105                 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
106 #else
107                 block = g_strconcat("Plugins_", type_cur->data, NULL);
108 #endif
109                 if ((pfile = prefs_write_open(rcpath)) == NULL ||
110                     (prefs_set_block_label(pfile, block) < 0)) {
111                         g_warning("failed to write plugin list\n");
112                         g_free(rcpath);
113                         return;
114                 }
115                 g_free(block);
116
117                 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
118                         plugin = (Plugin *) plugin_cur->data;
119                         
120                         if (plugin->unloaded_hidden)
121                                 continue;
122
123                         if (!strcmp(plugin->type(), type_cur->data))
124                                 fprintf(pfile->fp, "%s\n", plugin->filename);
125                 }
126                 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
127                         plugin = (Plugin *) plugin_cur->data;
128
129                         if (plugin->unloaded_hidden)
130                                 continue;
131                         
132                         if (!strcmp(plugin->type(), type_cur->data))
133                                 fprintf(pfile->fp, "%s\n", plugin->filename);
134                 }
135                 fprintf(pfile->fp, "\n");
136
137                 if (prefs_file_close(pfile) < 0)
138                         g_warning("failed to write plugin list\n");
139
140                 g_free(rcpath); 
141         }
142 }
143
144 static gboolean plugin_is_loaded(const gchar *filename)
145 {
146         return (g_slist_find_custom(plugins, filename, 
147                   (GCompareFunc)list_find_by_plugin_filename) != NULL);
148 }
149
150 static Plugin *plugin_get_by_filename(const gchar *filename)
151 {
152         GSList *cur = plugins;
153         for(; cur; cur = cur->next) {
154                 Plugin *p = (Plugin *)cur->data;
155                 if (!strcmp(p->filename, filename)) {
156                         return p;
157                 } 
158         }
159         return NULL;
160 }
161
162 /** 
163  * Loads a plugin dependancies
164  * 
165  * Plugin dependancies are, optionnaly, listed in a file in
166  * get_plugin_dir()/$pluginname.deps.
167  * \param filename The filename of the plugin for which we have to load deps
168  * \param error The location where an error string can be stored
169  * \return 0 on success, -1 otherwise
170  */
171 static gint plugin_load_deps(const gchar *filename, gchar **error)
172 {
173         gchar *tmp;
174         gchar *deps_file = NULL;
175         FILE *fp = NULL;
176         gchar buf[BUFFSIZE];
177         gchar *p;
178
179         tmp = g_strdup(filename);
180         if( (p = strrchr(tmp, '.')) )
181           *p = '\0';
182         deps_file = g_strconcat(tmp, ".deps", NULL);
183         g_free(tmp);
184         
185         fp = g_fopen(deps_file, "rb");
186         g_free(deps_file);
187         
188         if (!fp)
189                 return 0;
190         
191         while (fgets(buf, sizeof(buf), fp) != NULL) {
192                 Plugin *dep_plugin = NULL;
193                 gchar *path = NULL;
194                 buf[strlen(buf)-1]='\0'; /* chop off \n */
195                 path = g_strconcat(get_plugin_dir(), buf,
196                                 ".", G_MODULE_SUFFIX, NULL);
197                 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
198                         debug_print("trying to load %s\n", path);
199                         dep_plugin = plugin_load(path, error);
200                         if (dep_plugin == NULL) {
201                                 g_free(path);
202                                 return -1;
203                         }
204                 }
205                 g_free(path);
206                 if (!g_slist_find_custom(dep_plugin->rdeps, 
207                                 (gpointer) filename, list_find_by_string)) {
208                         debug_print("adding %s to %s rdeps\n",
209                                 filename,
210                                 dep_plugin->filename);
211                         dep_plugin->rdeps = 
212                                 g_slist_append(dep_plugin->rdeps, 
213                                         g_strdup(filename));
214                 }
215         }
216         fclose(fp);
217         return 0;
218 }
219
220 static void plugin_unload_rdeps(Plugin *plugin)
221 {
222         GSList *cur = plugin->rdeps;
223         debug_print("removing %s rdeps\n", plugin->filename);
224         while (cur) {
225                 gchar *file = (gchar *)cur->data;
226                 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
227                 debug_print(" rdep %s: %p\n", file, rdep_plugin);
228                 if (rdep_plugin) {
229                         plugin_unload(rdep_plugin);
230                 }
231                 g_free(file);
232                 cur = cur->next;
233         }
234         g_slist_free(plugin->rdeps);
235         plugin->rdeps = NULL;
236 }
237
238 static void plugin_remove_from_unloaded_list (const gchar *filename)
239 {
240         GSList *item = g_slist_find_custom(unloaded_plugins, 
241                                 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
242         Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
243         if (unloaded_plugin != NULL) {
244                 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
245                 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
246                 g_module_close(unloaded_plugin->module);
247                 g_free(unloaded_plugin->filename);
248                 g_free(unloaded_plugin->error);
249                 g_free(unloaded_plugin);
250         }
251 }
252
253 static gchar *plugin_check_features(struct PluginFeature *features) {
254         int i = 0, j = 0;
255         GSList *cur = plugins;
256
257         if (features == NULL)
258                 return NULL;
259         for(; cur; cur = cur->next) {
260                 Plugin *p = (Plugin *)cur->data;
261                 struct PluginFeature *cur_features = p->provides();
262                 if (p->unloaded_hidden)
263                         continue;
264                 for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
265                         for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
266                                 if (cur_features[j].type == features[i].type &&
267                                     !strcmp(cur_features[j].subtype, features[i].subtype)) {
268                                         return g_strdup_printf(_(
269                                                 "This plugin provides %s (%s), which is "
270                                                 "already provided by the %s plugin."),
271                                                 _(plugin_feature_names[features[i].type]), 
272                                                 _(features[i].subtype),
273                                                 p->name());
274                                 }
275                         }
276                 }
277         }
278
279         return NULL;
280 }
281 /**
282  * Loads a plugin
283  *
284  * \param filename The filename of the plugin to load
285  * \param error The location where an error string can be stored
286  * \return the plugin on success, NULL otherwise
287  */
288 Plugin *plugin_load(const gchar *filename, gchar **error)
289 {
290         Plugin *plugin;
291         gint (*plugin_init) (gchar **error);
292         gpointer plugin_name, plugin_desc, plugin_version;
293         const gchar *(*plugin_type)(void);
294         const gchar *(*plugin_licence)(void);
295         struct PluginFeature *(*plugin_provides)(void);
296
297         gint ok;
298
299         g_return_val_if_fail(filename != NULL, NULL);
300         g_return_val_if_fail(error != NULL, NULL);
301
302         /* check duplicate plugin path name */
303         if (plugin_is_loaded(filename)) {
304                 plugin = plugin_get_by_filename(filename);
305                 if (plugin->unloaded_hidden) {
306                         /* reshow it */
307                         goto init_plugin;
308                 } else {
309                         *error = g_strdup(_("Plugin already loaded"));
310                         return NULL;            
311                 }
312         }                              
313         
314         plugin_remove_from_unloaded_list(filename);
315         
316         if (plugin_load_deps(filename, error) < 0)
317                 return NULL;
318         plugin = g_new0(Plugin, 1);
319         if (plugin == NULL) {
320                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
321                 return NULL;
322         }
323
324         debug_print("trying to load `%s'\n", filename);
325         plugin->module = g_module_open(filename, 0);
326         if (plugin->module == NULL) {
327                 *error = g_strdup(g_module_error());
328                 g_free(plugin);
329                 return NULL;
330         }
331
332 init_plugin:
333         if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
334             !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
335             !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
336             !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
337             !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
338             !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
339             !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
340                 *error = g_strdup(g_module_error());
341                 if (plugin->unloaded_hidden)
342                         return NULL;
343                 g_module_close(plugin->module);
344                 g_free(plugin);
345                 return NULL;
346         }
347         
348         if (strcmp(plugin_licence(), "GPL")
349         &&  strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
350                 *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
351                 if (plugin->unloaded_hidden)
352                         return NULL;
353                 g_module_close(plugin->module);
354                 g_free(plugin);
355                 return NULL;
356         }
357
358         if (!strcmp(plugin_type(), "GTK")) {
359                 *error = g_strdup(_("This module is for Claws Mail GTK1."));
360                 if (plugin->unloaded_hidden)
361                         return NULL;
362                 g_module_close(plugin->module);
363                 g_free(plugin);
364                 return NULL;
365         }
366
367         if ((*error = plugin_check_features(plugin_provides())) != NULL) {
368                 if (plugin->unloaded_hidden)
369                         return NULL;
370                 g_module_close(plugin->module);
371                 g_free(plugin);
372                 return NULL;
373         }
374         plugin->name = plugin_name;
375         plugin->desc = plugin_desc;
376         plugin->version = plugin_version;
377         plugin->type = plugin_type;
378         plugin->licence = plugin_licence;
379         plugin->provides = plugin_provides;
380         plugin->filename = g_strdup(filename);
381         plugin->error = NULL;
382
383         if ((ok = plugin_init(error)) < 0) {
384                 if (*error)
385                         plugin->error = g_strdup(*error);
386                 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
387                 return NULL;
388         }
389
390         if (!plugin->unloaded_hidden)
391                 plugins = g_slist_append(plugins, plugin);
392         plugin->unloaded_hidden = FALSE;
393
394         debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
395
396         return plugin;
397 }
398
399 void plugin_unload(Plugin *plugin)
400 {
401         gboolean (*plugin_done) (void);
402         gboolean can_unload = TRUE;
403
404         plugin_unload_rdeps(plugin);
405
406         if (plugin->unloaded_hidden)
407                 return;
408
409         if (plugin->error) {
410                 plugin_remove_from_unloaded_list(plugin->filename);
411                 return;
412         }
413         if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
414                 can_unload = plugin_done();
415         }
416
417         if (can_unload) {
418 #ifdef HAVE_VALGRIND
419                 if (!RUNNING_ON_VALGRIND) {
420                         g_module_close(plugin->module);
421                 }
422 #else
423                 g_module_close(plugin->module);
424 #endif
425                 plugins = g_slist_remove(plugins, plugin);
426                 g_free(plugin->filename);
427                 g_free(plugin);
428         } else {
429                 plugin->unloaded_hidden = TRUE;
430         }
431
432 }
433
434 void plugin_load_all(const gchar *type)
435 {
436         gchar *rcpath;
437         gchar buf[BUFFSIZE];
438         PrefFile *pfile;
439         gchar *error = NULL, *block;
440
441         plugin_types = g_slist_append(plugin_types, g_strdup(type));
442
443         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); 
444 #ifdef G_OS_WIN32
445         block = g_strconcat("PluginsWin32_", type, NULL);
446 #else
447         block = g_strconcat("Plugins_", type, NULL);
448 #endif
449         if ((pfile = prefs_read_open(rcpath)) == NULL ||
450             (prefs_set_block_label(pfile, block) < 0)) {
451                 g_free(rcpath);
452                 return;
453         }
454         g_free(block);
455
456         while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
457                 if (buf[0] == '[')
458                         break;
459
460                 g_strstrip(buf);
461                 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
462                         g_warning("plugin loading error: %s\n", error);
463                         g_free(error);
464                 }                                                       
465         }
466         prefs_file_close(pfile);
467
468         g_free(rcpath);
469 }
470
471 void plugin_unload_all(const gchar *type)
472 {
473         GSList *list, *cur;
474
475         list = g_slist_copy(plugins);
476         list = g_slist_reverse(list);
477
478         for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
479                 Plugin *plugin = (Plugin *) cur->data;
480                 
481                 if (!strcmp(type, plugin->type()))
482                         plugin_unload(plugin);
483         }
484         g_slist_free(list);
485
486         cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
487         if (cur) {
488                 g_free(cur->data);
489                 plugin_types = g_slist_remove(plugin_types, cur);
490         }
491 }
492
493
494 /* Load those plugins we always want to use.  No error output; just
495  * try. */
496 void plugin_load_standard_plugins (void)
497 {
498         static const char *names[] = {
499 #ifdef G_OS_WIN32 
500                 "pgpmime",
501                 "pgpinline",
502 #else
503                 /* post-2.5 maybe 
504                 "bogofilter", */
505 #endif
506                 NULL
507         };
508         int i;
509         gchar *error, *filename;
510         
511         for (i=0; names[i]; i++) {
512                 /* Simple hack to check whether the plugin has already
513                  * been loaded but checking only for the basename. */
514                 GSList *cur = plugins;
515                 for(; cur; cur = cur->next) {
516                         Plugin *p = (Plugin *)cur->data;
517                         if (strstr(p->filename, names[i]))
518                                 break;
519                 }
520                 if (!cur) { /* Not yet loaded. */
521                         /* FIXME: get_plugin_dir () returns with a trailing
522                          * (back)slash; this should be fixed so that we can use
523                          * g_module_build_path here. */
524 #ifdef G_OS_WIN32 
525                         filename = g_strconcat (get_plugin_dir(),
526                                                 names[i], NULL);
527 #else
528                         filename = g_strconcat (get_plugin_dir(),
529                                                 names[i], ".", G_MODULE_SUFFIX, NULL);
530 #endif
531                         error = NULL;
532                         plugin_load(filename, &error);
533                         g_free (error);
534                         g_free(filename);
535                 }
536         }
537 }
538
539 GSList *plugin_get_list(void)
540 {
541         GSList *new = NULL;
542         GSList *cur = plugins;
543         for (; cur; cur = cur->next) {
544                 Plugin *p = (Plugin *)cur->data;
545                 if (!p->unloaded_hidden)
546                         new = g_slist_prepend(new, p);
547         }
548         new = g_slist_reverse(new);
549         return new;
550 }
551
552 GSList *plugin_get_unloaded_list(void)
553 {
554         return g_slist_copy(unloaded_plugins);
555 }
556
557 const gchar *plugin_get_name(Plugin *plugin)
558 {
559         return plugin->name();
560 }
561
562 const gchar *plugin_get_desc(Plugin *plugin)
563 {
564         return plugin->desc();
565 }
566
567 const gchar *plugin_get_version(Plugin *plugin)
568 {
569         return plugin->version();
570 }
571
572 const gchar *plugin_get_error(Plugin *plugin)
573 {
574         return plugin->error;
575 }
576
577 /* Generally called in plugin_init() function of each plugin. It check the
578  * minimal and compiled version of claws binary required by the plugin.
579  * If (@minimum_claws_version == 0 || @compiled_claws_version == 0), don't
580  * check the corresponding version.
581  *
582  * If an error occurs {
583  *      If @error == NULL { don't allocate error string. }
584  *      If @error != NULL { error string is allocated and must be freed after 
585  *                              call function. }
586  * }
587  * Returns: FALSE if an error occurs, TRUE if all is OK.
588  */
589 gint check_plugin_version(guint32 minimum_claws_version,
590                          guint32 compiled_claws_version,
591                          const gchar *plugin_name,
592                          gchar **error)
593 {
594         guint32 claws_version = claws_get_version();
595
596         if (compiled_claws_version != 0 && claws_version > compiled_claws_version) {
597                 if (error != NULL) {
598                         *error = (plugin_name && *plugin_name)
599                                 ? g_strdup_printf(_("Your version of Claws Mail is newer than the "
600                                                         "version the '%s' plugin was built with."),
601                                                 plugin_name)
602                                 : g_strdup(_("Your version of Claws Mail is newer than the "
603                                                         "version the plugin was built with."));
604                 }
605                 return FALSE;
606         }
607
608         if (minimum_claws_version != 0 && claws_version < minimum_claws_version) {
609                 if (error != NULL) {
610                         *error = (plugin_name && *plugin_name)
611                                 ? g_strdup_printf(_("Your version of Claws Mail is too old for "
612                                                         "the '%s' plugin."), plugin_name)
613                                 : g_strdup(_("Your version of Claws Mail is too old for the plugin."));
614                 }
615                 return FALSE;
616         }
617         return TRUE;
618 }