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