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