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