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