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