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