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