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