Mercurial > pidgin
annotate libgaim/plugin.c @ 14725:62366c6a10eb
[gaim-migrate @ 17479]
Make sure plugins are marked not-loadable if their ui-requirement can't be met.
You are going to need to rebuild all the non-core plugins.
The UI specific plugins for Gaim specifies their ui-requirement in the plugin
structure. So it'd make sense to load such plugins only if the ui-requirement
can be met. As it happens, gntgf (a gnt-plugin for gaim-text) can be loaded from
Gaim, which has been reported to have caused Gaim to freeze. gntgf does mark
itself as a gnt-plugin. So I think it should be upto libgaim to make sure
plugins are marked not-loadable if the ui-requirements can not be met.
This commit does exactly that. In doing so, it changes the string plugins
use to specify their ui-requirement (the change is from "gtk" to GAIM_GTK_UI --
which is "gtk-gaim"). So this will require all the non-core plugins to be
rebuilt.
committer: Tailor Script <tailor@pidgin.im>
| author | Sadrul Habib Chowdhury <imadil@gmail.com> |
|---|---|
| date | Sat, 14 Oct 2006 05:29:11 +0000 |
| parents | 60b1bc8dbf37 |
| children | f93d632ac8d8 |
| rev | line source |
|---|---|
| 14192 | 1 /* |
| 2 * gaim | |
| 3 * | |
| 4 * Gaim is the legal property of its developers, whose names are too numerous | |
| 5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 6 * source distribution. | |
| 7 * | |
| 8 * This program is free software; you can redistribute it and/or modify | |
| 9 * it under the terms of the GNU General Public License as published by | |
| 10 * the Free Software Foundation; either version 2 of the License, or | |
| 11 * (at your option) any later version. | |
| 12 * | |
| 13 * This program is distributed in the hope that it will be useful, | |
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 * GNU General Public License for more details. | |
| 17 * | |
| 18 * You should have received a copy of the GNU General Public License | |
| 19 * along with this program; if not, write to the Free Software | |
| 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 21 */ | |
| 22 #include "internal.h" | |
| 23 | |
| 24 #include "accountopt.h" | |
|
14725
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
25 #include "core.h" |
| 14192 | 26 #include "dbus-maybe.h" |
| 27 #include "debug.h" | |
| 28 #include "notify.h" | |
| 29 #include "prefs.h" | |
| 30 #include "prpl.h" | |
| 31 #include "request.h" | |
| 32 #include "signals.h" | |
| 33 #include "util.h" | |
| 34 #include "version.h" | |
| 35 | |
| 36 typedef struct | |
| 37 { | |
| 38 GHashTable *commands; | |
| 39 size_t command_count; | |
| 40 | |
| 41 } GaimPluginIpcInfo; | |
| 42 | |
| 43 typedef struct | |
| 44 { | |
| 45 GaimCallback func; | |
| 46 GaimSignalMarshalFunc marshal; | |
| 47 | |
| 48 int num_params; | |
| 49 GaimValue **params; | |
| 50 GaimValue *ret_value; | |
| 51 | |
| 52 } GaimPluginIpcCommand; | |
| 53 | |
| 54 static GList *search_paths = NULL; | |
| 55 static GList *plugins = NULL; | |
| 56 static GList *loaded_plugins = NULL; | |
| 57 static GList *protocol_plugins = NULL; | |
| 58 #ifdef GAIM_PLUGINS | |
| 59 static GList *load_queue = NULL; | |
| 60 static GList *plugin_loaders = NULL; | |
| 61 #endif | |
| 62 | |
| 63 /* | |
| 64 * TODO: I think the intention was to allow multiple load and unload | |
| 65 * callback functions. Perhaps using a GList instead of a | |
| 66 * pointer to a single function. | |
| 67 */ | |
| 68 static void (*probe_cb)(void *) = NULL; | |
| 69 static void *probe_cb_data = NULL; | |
| 70 static void (*load_cb)(GaimPlugin *, void *) = NULL; | |
| 71 static void *load_cb_data = NULL; | |
| 72 static void (*unload_cb)(GaimPlugin *, void *) = NULL; | |
| 73 static void *unload_cb_data = NULL; | |
| 74 | |
| 75 #ifdef GAIM_PLUGINS | |
| 76 | |
| 77 static gboolean | |
| 78 has_file_extension(const char *filename, const char *ext) | |
| 79 { | |
| 80 int len, extlen; | |
| 81 | |
| 82 if (filename == NULL || *filename == '\0' || ext == NULL) | |
| 83 return 0; | |
| 84 | |
| 85 extlen = strlen(ext); | |
| 86 len = strlen(filename) - extlen; | |
| 87 | |
| 88 if (len < 0) | |
| 89 return 0; | |
| 90 | |
| 91 return (strncmp(filename + len, ext, extlen) == 0); | |
| 92 } | |
| 93 | |
| 94 static gboolean | |
| 95 is_native(const char *filename) | |
| 96 { | |
| 97 const char *last_period; | |
| 98 | |
| 99 last_period = strrchr(filename, '.'); | |
| 100 if (last_period == NULL) | |
| 101 return FALSE; | |
| 102 | |
| 103 return !(strcmp(last_period, ".dll") & | |
| 104 strcmp(last_period, ".sl") & | |
| 105 strcmp(last_period, ".so")); | |
| 106 } | |
| 107 | |
| 108 static char * | |
| 109 gaim_plugin_get_basename(const char *filename) | |
| 110 { | |
| 111 const char *basename; | |
| 112 const char *last_period; | |
| 113 | |
| 114 basename = strrchr(filename, G_DIR_SEPARATOR); | |
| 115 if (basename != NULL) | |
| 116 basename++; | |
| 117 else | |
| 118 basename = filename; | |
| 119 | |
| 120 if (is_native(basename) && | |
| 121 ((last_period = strrchr(basename, '.')) != NULL)) | |
| 122 return g_strndup(basename, (last_period - basename)); | |
| 123 | |
| 124 return g_strdup(basename); | |
| 125 } | |
| 126 | |
| 127 static gboolean | |
| 128 loader_supports_file(GaimPlugin *loader, const char *filename) | |
| 129 { | |
| 130 GList *exts; | |
| 131 | |
| 132 for (exts = GAIM_PLUGIN_LOADER_INFO(loader)->exts; exts != NULL; exts = exts->next) { | |
| 133 if (has_file_extension(filename, (char *)exts->data)) { | |
| 134 return TRUE; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 return FALSE; | |
| 139 } | |
| 140 | |
| 141 static GaimPlugin * | |
| 142 find_loader_for_plugin(const GaimPlugin *plugin) | |
| 143 { | |
| 144 GaimPlugin *loader; | |
| 145 GList *l; | |
| 146 | |
| 147 if (plugin->path == NULL) | |
| 148 return NULL; | |
| 149 | |
| 150 for (l = gaim_plugins_get_loaded(); l != NULL; l = l->next) { | |
| 151 loader = l->data; | |
| 152 | |
| 153 if (loader->info->type == GAIM_PLUGIN_LOADER && | |
| 154 loader_supports_file(loader, plugin->path)) { | |
| 155 | |
| 156 return loader; | |
| 157 } | |
| 158 | |
| 159 loader = NULL; | |
| 160 } | |
| 161 | |
| 162 return NULL; | |
| 163 } | |
| 164 | |
| 165 #endif /* GAIM_PLUGINS */ | |
| 166 | |
| 167 /** | |
| 168 * Negative if a before b, 0 if equal, positive if a after b. | |
| 169 */ | |
| 170 static gint | |
| 171 compare_prpl(GaimPlugin *a, GaimPlugin *b) | |
| 172 { | |
| 173 if(GAIM_IS_PROTOCOL_PLUGIN(a)) { | |
| 174 if(GAIM_IS_PROTOCOL_PLUGIN(b)) | |
| 175 return strcmp(a->info->name, b->info->name); | |
| 176 else | |
| 177 return -1; | |
| 178 } else { | |
| 179 if(GAIM_IS_PROTOCOL_PLUGIN(b)) | |
| 180 return 1; | |
| 181 else | |
| 182 return 0; | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 GaimPlugin * | |
| 187 gaim_plugin_new(gboolean native, const char *path) | |
| 188 { | |
| 189 GaimPlugin *plugin; | |
| 190 | |
| 191 plugin = g_new0(GaimPlugin, 1); | |
| 192 | |
| 193 plugin->native_plugin = native; | |
| 194 plugin->path = g_strdup(path); | |
| 195 | |
| 196 GAIM_DBUS_REGISTER_POINTER(plugin, GaimPlugin); | |
| 197 | |
| 198 return plugin; | |
| 199 } | |
| 200 | |
| 201 GaimPlugin * | |
| 202 gaim_plugin_probe(const char *filename) | |
| 203 { | |
| 204 #ifdef GAIM_PLUGINS | |
| 205 GaimPlugin *plugin = NULL; | |
| 206 GaimPlugin *loader; | |
| 207 gpointer unpunned; | |
| 208 gchar *basename = NULL; | |
| 209 gboolean (*gaim_init_plugin)(GaimPlugin *); | |
| 210 | |
| 211 gaim_debug_misc("plugins", "probing %s\n", filename); | |
| 212 g_return_val_if_fail(filename != NULL, NULL); | |
| 213 | |
| 214 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) | |
| 215 return NULL; | |
| 216 | |
| 217 /* If this plugin has already been probed then exit */ | |
| 218 basename = gaim_plugin_get_basename(filename); | |
| 219 plugin = gaim_plugins_find_with_basename(basename); | |
| 220 g_free(basename); | |
| 221 if (plugin != NULL) | |
| 222 { | |
| 223 if (!strcmp(filename, plugin->path)) | |
| 224 return plugin; | |
| 225 else if (!gaim_plugin_is_unloadable(plugin)) | |
| 226 { | |
| 227 gaim_debug_info("plugins", "Not loading %s. " | |
| 228 "Another plugin with the same name (%s) has already been loaded.\n", | |
| 229 filename, plugin->path); | |
| 230 return plugin; | |
| 231 } | |
| 232 else | |
| 233 { | |
| 234 /* The old plugin was a different file and it was unloadable. | |
| 235 * There's no guarantee that this new file with the same name | |
| 236 * will be loadable, but unless it fails in one of the silent | |
| 237 * ways and the first one didn't, it's not any worse. The user | |
| 238 * will still see a greyed-out plugin, which is what we want. */ | |
| 239 gaim_plugin_destroy(plugin); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 plugin = gaim_plugin_new(has_file_extension(filename, G_MODULE_SUFFIX), filename); | |
| 244 | |
| 245 if (plugin->native_plugin) { | |
| 246 const char *error; | |
| 247 #ifdef _WIN32 | |
| 248 /* Suppress error popups for failing to load plugins */ | |
| 249 UINT old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS); | |
| 250 #endif | |
| 251 | |
| 252 /* | |
| 253 * We pass G_MODULE_BIND_LOCAL here to prevent symbols from | |
| 254 * plugins being added to the global name space. | |
| 255 * | |
| 256 * G_MODULE_BIND_LOCAL was added in glib 2.3.3. | |
| 257 * TODO: I guess there's nothing we can do about that? | |
| 258 */ | |
| 259 #if GLIB_CHECK_VERSION(2,3,3) | |
| 260 plugin->handle = g_module_open(filename, G_MODULE_BIND_LOCAL); | |
| 261 #else | |
| 262 plugin->handle = g_module_open(filename, 0); | |
| 263 #endif | |
| 264 | |
| 265 if (plugin->handle == NULL) | |
| 266 { | |
| 267 const char *error = g_module_error(); | |
| 268 if (error != NULL && gaim_str_has_prefix(error, filename)) | |
| 269 { | |
| 270 error = error + strlen(filename); | |
| 271 | |
| 272 /* These are just so we don't crash. If we | |
| 273 * got this far, they should always be true. */ | |
| 274 if (*error == ':') | |
| 275 error++; | |
| 276 if (*error == ' ') | |
| 277 error++; | |
| 278 } | |
| 279 | |
| 280 if (error == NULL || !*error) | |
| 281 { | |
| 282 plugin->error = g_strdup(_("Unknown error")); | |
| 283 gaim_debug_error("plugins", "%s is unloadable: Unknown error\n", | |
| 284 plugin->path); | |
| 285 } | |
| 286 else | |
| 287 { | |
| 288 plugin->error = g_strdup(error); | |
| 289 gaim_debug_error("plugins", "%s is unloadable: %s\n", | |
| 290 plugin->path, plugin->error); | |
| 291 } | |
| 292 #if GLIB_CHECK_VERSION(2,3,3) | |
| 293 plugin->handle = g_module_open(filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL); | |
| 294 #else | |
| 295 plugin->handle = g_module_open(filename, G_MODULE_BIND_LAZY); | |
| 296 #endif | |
| 297 | |
| 298 if (plugin->handle == NULL) | |
| 299 { | |
| 300 #ifdef _WIN32 | |
| 301 /* Restore the original error mode */ | |
| 302 SetErrorMode(old_error_mode); | |
| 303 #endif | |
| 304 gaim_plugin_destroy(plugin); | |
| 305 return NULL; | |
| 306 } | |
| 307 else | |
| 308 { | |
| 309 /* We were able to load the plugin with lazy symbol binding. | |
| 310 * This means we're missing some symbol. Mark it as | |
| 311 * unloadable and keep going so we get the info to display | |
| 312 * to the user so they know to rebuild this plugin. */ | |
| 313 plugin->unloadable = TRUE; | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 if (!g_module_symbol(plugin->handle, "gaim_init_plugin", | |
| 318 &unpunned)) | |
| 319 { | |
| 320 gaim_debug_error("plugins", "%s is not usable because the " | |
| 321 "'gaim_init_plugin' symbol could not be " | |
| 322 "found. Does the plugin call the " | |
| 323 "GAIM_INIT_PLUGIN() macro?\n", plugin->path); | |
| 324 | |
| 325 g_module_close(plugin->handle); | |
| 326 error = g_module_error(); | |
| 327 if (error != NULL) | |
| 328 gaim_debug_error("plugins", "Error closing module %s: %s\n", | |
| 329 plugin->path, error); | |
| 330 plugin->handle = NULL; | |
| 331 | |
| 332 #ifdef _WIN32 | |
| 333 /* Restore the original error mode */ | |
| 334 SetErrorMode(old_error_mode); | |
| 335 #endif | |
| 336 gaim_plugin_destroy(plugin); | |
| 337 return NULL; | |
| 338 } | |
| 339 gaim_init_plugin = unpunned; | |
| 340 | |
| 341 #ifdef _WIN32 | |
| 342 /* Restore the original error mode */ | |
| 343 SetErrorMode(old_error_mode); | |
| 344 #endif | |
| 345 } | |
| 346 else { | |
| 347 loader = find_loader_for_plugin(plugin); | |
| 348 | |
| 349 if (loader == NULL) { | |
| 350 gaim_plugin_destroy(plugin); | |
| 351 return NULL; | |
| 352 } | |
| 353 | |
| 354 gaim_init_plugin = GAIM_PLUGIN_LOADER_INFO(loader)->probe; | |
| 355 } | |
| 356 | |
| 357 if (!gaim_init_plugin(plugin) || plugin->info == NULL) | |
| 358 { | |
| 359 gaim_plugin_destroy(plugin); | |
| 360 return NULL; | |
| 361 } | |
|
14725
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
362 else if (plugin->info->ui_requirement && |
|
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
363 strcmp(plugin->info->ui_requirement, gaim_core_get_ui())) |
|
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
364 { |
|
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
365 plugin->error = g_strdup_printf("The UI requirement for this plugin is not met."); |
|
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
366 gaim_debug_error("plugins", "%s is not loadable: The UI requirement is not met.", plugin->path); |
|
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
367 plugin->unloadable = TRUE; |
|
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
368 return plugin; |
|
62366c6a10eb
[gaim-migrate @ 17479]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14192
diff
changeset
|
369 } |
| 14192 | 370 |
| 371 /* Really old plugins. */ | |
| 372 if (plugin->info->magic != GAIM_PLUGIN_MAGIC) | |
| 373 { | |
| 374 if (plugin->info->magic >= 2 && plugin->info->magic <= 4) | |
| 375 { | |
| 376 struct _GaimPluginInfo2 | |
| 377 { | |
| 378 unsigned int api_version; | |
| 379 GaimPluginType type; | |
| 380 char *ui_requirement; | |
| 381 unsigned long flags; | |
| 382 GList *dependencies; | |
| 383 GaimPluginPriority priority; | |
| 384 | |
| 385 char *id; | |
| 386 char *name; | |
| 387 char *version; | |
| 388 char *summary; | |
| 389 char *description; | |
| 390 char *author; | |
| 391 char *homepage; | |
| 392 | |
| 393 gboolean (*load)(GaimPlugin *plugin); | |
| 394 gboolean (*unload)(GaimPlugin *plugin); | |
| 395 void (*destroy)(GaimPlugin *plugin); | |
| 396 | |
| 397 void *ui_info; | |
| 398 void *extra_info; | |
| 399 GaimPluginUiInfo *prefs_info; | |
| 400 GList *(*actions)(GaimPlugin *plugin, gpointer context); | |
| 401 } *info2 = (struct _GaimPluginInfo2 *)plugin->info; | |
| 402 | |
| 403 /* This leaks... but only for ancient plugins, so deal with it. */ | |
| 404 plugin->info = g_new0(GaimPluginInfo, 1); | |
| 405 | |
| 406 /* We don't really need all these to display the plugin info, but | |
| 407 * I'm copying them all for good measure. */ | |
| 408 plugin->info->magic = info2->api_version; | |
| 409 plugin->info->type = info2->type; | |
| 410 plugin->info->ui_requirement = info2->ui_requirement; | |
| 411 plugin->info->flags = info2->flags; | |
| 412 plugin->info->dependencies = info2->dependencies; | |
| 413 plugin->info->id = info2->id; | |
| 414 plugin->info->name = info2->name; | |
| 415 plugin->info->version = info2->version; | |
| 416 plugin->info->summary = info2->summary; | |
| 417 plugin->info->description = info2->description; | |
| 418 plugin->info->author = info2->author; | |
| 419 plugin->info->homepage = info2->homepage; | |
| 420 plugin->info->load = info2->load; | |
| 421 plugin->info->unload = info2->unload; | |
| 422 plugin->info->destroy = info2->destroy; | |
| 423 plugin->info->ui_info = info2->ui_info; | |
| 424 plugin->info->extra_info = info2->extra_info; | |
| 425 | |
| 426 if (info2->api_version >= 3) | |
| 427 plugin->info->prefs_info = info2->prefs_info; | |
| 428 | |
| 429 if (info2->api_version >= 4) | |
| 430 plugin->info->actions = info2->actions; | |
| 431 | |
| 432 | |
| 433 plugin->error = g_strdup_printf(_("Plugin magic mismatch %d (need %d)"), | |
| 434 plugin->info->magic, GAIM_PLUGIN_MAGIC); | |
| 435 gaim_debug_error("plugins", "%s is unloadable: Plugin magic mismatch %d (need %d)\n", | |
| 436 plugin->path, plugin->info->magic, GAIM_PLUGIN_MAGIC); | |
| 437 plugin->unloadable = TRUE; | |
| 438 return plugin; | |
| 439 } | |
| 440 | |
| 441 gaim_debug_error("plugins", "%s is unloadable: Plugin magic mismatch %d (need %d)\n", | |
| 442 plugin->path, plugin->info->magic, GAIM_PLUGIN_MAGIC); | |
| 443 gaim_plugin_destroy(plugin); | |
| 444 return NULL; | |
| 445 } | |
| 446 | |
| 447 if (plugin->info->major_version != GAIM_MAJOR_VERSION || | |
| 448 plugin->info->minor_version > GAIM_MINOR_VERSION) | |
| 449 { | |
| 450 plugin->error = g_strdup_printf(_("ABI version mismatch %d.%d.x (need %d.%d.x)"), | |
| 451 plugin->info->major_version, plugin->info->minor_version, | |
| 452 GAIM_MAJOR_VERSION, GAIM_MINOR_VERSION); | |
| 453 gaim_debug_error("plugins", "%s is unloadable: ABI version mismatch %d.%d.x (need %d.%d.x)\n", | |
| 454 plugin->path, plugin->info->major_version, plugin->info->minor_version, | |
| 455 GAIM_MAJOR_VERSION, GAIM_MINOR_VERSION); | |
| 456 plugin->unloadable = TRUE; | |
| 457 return plugin; | |
| 458 } | |
| 459 | |
| 460 if (plugin->info->type == GAIM_PLUGIN_PROTOCOL) | |
| 461 { | |
| 462 /* If plugin is a PRPL, make sure it implements the required functions */ | |
| 463 if ((GAIM_PLUGIN_PROTOCOL_INFO(plugin)->list_icon == NULL) || | |
| 464 (GAIM_PLUGIN_PROTOCOL_INFO(plugin)->login == NULL) || | |
| 465 (GAIM_PLUGIN_PROTOCOL_INFO(plugin)->close == NULL)) | |
| 466 { | |
| 467 plugin->error = g_strdup(_("Plugin does not implement all required functions")); | |
| 468 gaim_debug_error("plugins", "%s is unloadable: Plugin does not implement all required functions\n", | |
| 469 plugin->path); | |
| 470 plugin->unloadable = TRUE; | |
| 471 return plugin; | |
| 472 } | |
| 473 | |
| 474 /* For debugging, let's warn about prpl prefs. */ | |
| 475 if (plugin->info->prefs_info != NULL) | |
| 476 { | |
| 477 gaim_debug_error("plugins", "%s has a prefs_info, but is a prpl. This is no longer supported.\n", | |
| 478 plugin->path); | |
| 479 } | |
| 480 } | |
| 481 | |
| 482 return plugin; | |
| 483 #else | |
| 484 return NULL; | |
| 485 #endif /* !GAIM_PLUGINS */ | |
| 486 } | |
| 487 | |
| 488 #ifdef GAIM_PLUGINS | |
| 489 static gint | |
| 490 compare_plugins(gconstpointer a, gconstpointer b) | |
| 491 { | |
| 492 const GaimPlugin *plugina = a; | |
| 493 const GaimPlugin *pluginb = b; | |
| 494 | |
| 495 return strcmp(plugina->info->name, pluginb->info->name); | |
| 496 } | |
| 497 #endif /* GAIM_PLUGINS */ | |
| 498 | |
| 499 gboolean | |
| 500 gaim_plugin_load(GaimPlugin *plugin) | |
| 501 { | |
| 502 #ifdef GAIM_PLUGINS | |
| 503 GList *dep_list = NULL; | |
| 504 GList *l; | |
| 505 | |
| 506 g_return_val_if_fail(plugin != NULL, FALSE); | |
| 507 | |
| 508 if (gaim_plugin_is_loaded(plugin)) | |
| 509 return TRUE; | |
| 510 | |
| 511 if (gaim_plugin_is_unloadable(plugin)) | |
| 512 return FALSE; | |
| 513 | |
| 514 g_return_val_if_fail(plugin->error == NULL, FALSE); | |
| 515 | |
| 516 /* | |
| 517 * Go through the list of the plugin's dependencies. | |
| 518 * | |
| 519 * First pass: Make sure all the plugins needed are probed. | |
| 520 */ | |
| 521 for (l = plugin->info->dependencies; l != NULL; l = l->next) | |
| 522 { | |
| 523 const char *dep_name = (const char *)l->data; | |
| 524 GaimPlugin *dep_plugin; | |
| 525 | |
| 526 dep_plugin = gaim_plugins_find_with_id(dep_name); | |
| 527 | |
| 528 if (dep_plugin == NULL) | |
| 529 { | |
| 530 char *tmp; | |
| 531 | |
| 532 tmp = g_strdup_printf(_("The required plugin %s was not found. " | |
| 533 "Please install this plugin and try again."), | |
| 534 dep_name); | |
| 535 | |
| 536 gaim_notify_error(NULL, NULL, | |
| 537 _("Gaim encountered errors loading the plugin."), tmp); | |
| 538 g_free(tmp); | |
| 539 | |
| 540 g_list_free(dep_list); | |
| 541 | |
| 542 return FALSE; | |
| 543 } | |
| 544 | |
| 545 dep_list = g_list_append(dep_list, dep_plugin); | |
| 546 } | |
| 547 | |
| 548 /* Second pass: load all the required plugins. */ | |
| 549 for (l = dep_list; l != NULL; l = l->next) | |
| 550 { | |
| 551 GaimPlugin *dep_plugin = (GaimPlugin *)l->data; | |
| 552 | |
| 553 if (!gaim_plugin_is_loaded(dep_plugin)) | |
| 554 { | |
| 555 if (!gaim_plugin_load(dep_plugin)) | |
| 556 { | |
| 557 char *tmp; | |
| 558 | |
| 559 tmp = g_strdup_printf(_("The required plugin %s was unable to load."), | |
| 560 plugin->info->name); | |
| 561 | |
| 562 gaim_notify_error(NULL, NULL, | |
| 563 _("Gaim was unable to load your plugin."), tmp); | |
| 564 g_free(tmp); | |
| 565 | |
| 566 g_list_free(dep_list); | |
| 567 | |
| 568 return FALSE; | |
| 569 } | |
| 570 } | |
| 571 } | |
| 572 | |
| 573 /* Third pass: note that other plugins are dependencies of this plugin. | |
| 574 * This is done separately in case we had to bail out earlier. */ | |
| 575 for (l = dep_list; l != NULL; l = l->next) | |
| 576 { | |
| 577 GaimPlugin *dep_plugin = (GaimPlugin *)l->data; | |
| 578 dep_plugin->dependent_plugins = g_list_prepend(dep_plugin->dependent_plugins, plugin->info->id); | |
| 579 } | |
| 580 | |
| 581 g_list_free(dep_list); | |
| 582 | |
| 583 if (plugin->native_plugin) | |
| 584 { | |
| 585 if (plugin->info != NULL && plugin->info->load != NULL) | |
| 586 { | |
| 587 if (!plugin->info->load(plugin)) | |
| 588 return FALSE; | |
| 589 } | |
| 590 } | |
| 591 else { | |
| 592 GaimPlugin *loader; | |
| 593 GaimPluginLoaderInfo *loader_info; | |
| 594 | |
| 595 loader = find_loader_for_plugin(plugin); | |
| 596 | |
| 597 if (loader == NULL) | |
| 598 return FALSE; | |
| 599 | |
| 600 loader_info = GAIM_PLUGIN_LOADER_INFO(loader); | |
| 601 | |
| 602 if (loader_info->load != NULL) | |
| 603 { | |
| 604 if (!loader_info->load(plugin)) | |
| 605 return FALSE; | |
| 606 } | |
| 607 } | |
| 608 | |
| 609 loaded_plugins = g_list_insert_sorted(loaded_plugins, plugin, compare_plugins); | |
| 610 | |
| 611 plugin->loaded = TRUE; | |
| 612 | |
| 613 /* TODO */ | |
| 614 if (load_cb != NULL) | |
| 615 load_cb(plugin, load_cb_data); | |
| 616 | |
| 617 gaim_signal_emit(gaim_plugins_get_handle(), "plugin-load", plugin); | |
| 618 | |
| 619 return TRUE; | |
| 620 | |
| 621 #else | |
| 622 return TRUE; | |
| 623 #endif /* !GAIM_PLUGINS */ | |
| 624 } | |
| 625 | |
| 626 gboolean | |
| 627 gaim_plugin_unload(GaimPlugin *plugin) | |
| 628 { | |
| 629 #ifdef GAIM_PLUGINS | |
| 630 GList *l; | |
| 631 | |
| 632 g_return_val_if_fail(plugin != NULL, FALSE); | |
| 633 | |
| 634 loaded_plugins = g_list_remove(loaded_plugins, plugin); | |
| 635 if ((plugin->info != NULL) && GAIM_IS_PROTOCOL_PLUGIN(plugin)) | |
| 636 protocol_plugins = g_list_remove(protocol_plugins, plugin); | |
| 637 | |
| 638 g_return_val_if_fail(gaim_plugin_is_loaded(plugin), FALSE); | |
| 639 | |
| 640 gaim_debug_info("plugins", "Unloading plugin %s\n", plugin->info->name); | |
| 641 | |
| 642 /* cancel any pending dialogs the plugin has */ | |
| 643 gaim_request_close_with_handle(plugin); | |
| 644 gaim_notify_close_with_handle(plugin); | |
| 645 | |
| 646 plugin->loaded = FALSE; | |
| 647 | |
| 648 /* Unload all plugins that depend on this plugin. */ | |
| 649 while ((l = plugin->dependent_plugins) != NULL) | |
| 650 { | |
| 651 const char * dep_name = (const char *)l->data; | |
| 652 GaimPlugin *dep_plugin; | |
| 653 | |
| 654 dep_plugin = gaim_plugins_find_with_id(dep_name); | |
| 655 | |
| 656 if (dep_plugin != NULL && gaim_plugin_is_loaded(dep_plugin)) | |
| 657 { | |
| 658 if (!gaim_plugin_unload(dep_plugin)) | |
| 659 { | |
| 660 char *translated_name = g_strdup(_(dep_plugin->info->name)); | |
| 661 char *tmp; | |
| 662 | |
| 663 tmp = g_strdup_printf(_("The dependent plugin %s failed to unload."), | |
| 664 translated_name); | |
| 665 g_free(translated_name); | |
| 666 | |
| 667 gaim_notify_error(NULL, NULL, | |
| 668 _("Gaim encountered errors unloading the plugin."), tmp); | |
| 669 g_free(tmp); | |
| 670 } | |
| 671 } | |
| 672 } | |
| 673 | |
| 674 /* Remove this plugin from each dependency's dependent_plugins list. */ | |
| 675 for (l = plugin->info->dependencies; l != NULL; l = l->next) | |
| 676 { | |
| 677 const char *dep_name = (const char *)l->data; | |
| 678 GaimPlugin *dependency; | |
| 679 | |
| 680 dependency = gaim_plugins_find_with_id(dep_name); | |
| 681 | |
| 682 dependency->dependent_plugins = g_list_remove(dependency->dependent_plugins, plugin->info->id); | |
| 683 } | |
| 684 | |
| 685 if (plugin->native_plugin) { | |
| 686 if (plugin->info->unload != NULL) | |
| 687 plugin->info->unload(plugin); | |
| 688 | |
| 689 if (plugin->info->type == GAIM_PLUGIN_PROTOCOL) { | |
| 690 GaimPluginProtocolInfo *prpl_info; | |
| 691 GList *l; | |
| 692 | |
| 693 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin); | |
| 694 | |
| 695 for (l = prpl_info->user_splits; l != NULL; l = l->next) | |
| 696 gaim_account_user_split_destroy(l->data); | |
| 697 | |
| 698 for (l = prpl_info->protocol_options; l != NULL; l = l->next) | |
| 699 gaim_account_option_destroy(l->data); | |
| 700 | |
| 701 if (prpl_info->user_splits != NULL) { | |
| 702 g_list_free(prpl_info->user_splits); | |
| 703 prpl_info->user_splits = NULL; | |
| 704 } | |
| 705 | |
| 706 if (prpl_info->protocol_options != NULL) { | |
| 707 g_list_free(prpl_info->protocol_options); | |
| 708 prpl_info->protocol_options = NULL; | |
| 709 } | |
| 710 } | |
| 711 } | |
| 712 else { | |
| 713 GaimPlugin *loader; | |
| 714 GaimPluginLoaderInfo *loader_info; | |
| 715 | |
| 716 loader = find_loader_for_plugin(plugin); | |
| 717 | |
| 718 if (loader == NULL) | |
| 719 return FALSE; | |
| 720 | |
| 721 loader_info = GAIM_PLUGIN_LOADER_INFO(loader); | |
| 722 | |
| 723 if (loader_info->unload != NULL) | |
| 724 loader_info->unload(plugin); | |
| 725 } | |
| 726 | |
| 727 gaim_signals_disconnect_by_handle(plugin); | |
| 728 gaim_plugin_ipc_unregister_all(plugin); | |
| 729 | |
| 730 /* TODO */ | |
| 731 if (unload_cb != NULL) | |
| 732 unload_cb(plugin, unload_cb_data); | |
| 733 | |
| 734 gaim_signal_emit(gaim_plugins_get_handle(), "plugin-unload", plugin); | |
| 735 | |
| 736 gaim_prefs_disconnect_by_handle(plugin); | |
| 737 | |
| 738 return TRUE; | |
| 739 #else | |
| 740 return TRUE; | |
| 741 #endif /* GAIM_PLUGINS */ | |
| 742 } | |
| 743 | |
| 744 gboolean | |
| 745 gaim_plugin_reload(GaimPlugin *plugin) | |
| 746 { | |
| 747 #ifdef GAIM_PLUGINS | |
| 748 g_return_val_if_fail(plugin != NULL, FALSE); | |
| 749 g_return_val_if_fail(gaim_plugin_is_loaded(plugin), FALSE); | |
| 750 | |
| 751 if (!gaim_plugin_unload(plugin)) | |
| 752 return FALSE; | |
| 753 | |
| 754 if (!gaim_plugin_load(plugin)) | |
| 755 return FALSE; | |
| 756 | |
| 757 return TRUE; | |
| 758 #else | |
| 759 return TRUE; | |
| 760 #endif /* !GAIM_PLUGINS */ | |
| 761 } | |
| 762 | |
| 763 void | |
| 764 gaim_plugin_destroy(GaimPlugin *plugin) | |
| 765 { | |
| 766 #ifdef GAIM_PLUGINS | |
| 767 g_return_if_fail(plugin != NULL); | |
| 768 | |
| 769 if (gaim_plugin_is_loaded(plugin)) | |
| 770 gaim_plugin_unload(plugin); | |
| 771 | |
| 772 plugins = g_list_remove(plugins, plugin); | |
| 773 | |
| 774 if (load_queue != NULL) | |
| 775 load_queue = g_list_remove(load_queue, plugin); | |
| 776 | |
| 777 /* true, this may leak a little memory if there is a major version | |
| 778 * mismatch, but it's a lot better than trying to free something | |
| 779 * we shouldn't, and crashing while trying to load an old plugin */ | |
| 780 if(plugin->info == NULL || plugin->info->magic != GAIM_PLUGIN_MAGIC || | |
| 781 plugin->info->major_version != GAIM_MAJOR_VERSION) | |
| 782 { | |
| 783 if(plugin->handle) | |
| 784 g_module_close(plugin->handle); | |
| 785 | |
| 786 g_free(plugin->path); | |
| 787 g_free(plugin->error); | |
| 788 | |
| 789 GAIM_DBUS_UNREGISTER_POINTER(plugin); | |
| 790 | |
| 791 g_free(plugin); | |
| 792 return; | |
| 793 } | |
| 794 | |
| 795 if (plugin->info != NULL) | |
| 796 g_list_free(plugin->info->dependencies); | |
| 797 | |
| 798 if (plugin->native_plugin) | |
| 799 { | |
| 800 if (plugin->info != NULL && plugin->info->type == GAIM_PLUGIN_LOADER) | |
| 801 { | |
| 802 GaimPluginLoaderInfo *loader_info; | |
| 803 GList *exts, *l, *next_l; | |
| 804 GaimPlugin *p2; | |
| 805 | |
| 806 loader_info = GAIM_PLUGIN_LOADER_INFO(plugin); | |
| 807 | |
| 808 if (loader_info != NULL && loader_info->exts != NULL) | |
| 809 { | |
| 810 for (exts = GAIM_PLUGIN_LOADER_INFO(plugin)->exts; | |
| 811 exts != NULL; | |
| 812 exts = exts->next) { | |
| 813 | |
| 814 for (l = gaim_plugins_get_all(); l != NULL; l = next_l) | |
| 815 { | |
| 816 next_l = l->next; | |
| 817 | |
| 818 p2 = l->data; | |
| 819 | |
| 820 if (p2->path != NULL && | |
| 821 has_file_extension(p2->path, exts->data)) | |
| 822 { | |
| 823 gaim_plugin_destroy(p2); | |
| 824 } | |
| 825 } | |
| 826 } | |
| 827 | |
| 828 g_list_free(loader_info->exts); | |
| 829 } | |
| 830 | |
| 831 plugin_loaders = g_list_remove(plugin_loaders, plugin); | |
| 832 } | |
| 833 | |
| 834 if (plugin->info != NULL && plugin->info->destroy != NULL) | |
| 835 plugin->info->destroy(plugin); | |
| 836 | |
| 837 if (plugin->handle != NULL) | |
| 838 g_module_close(plugin->handle); | |
| 839 } | |
| 840 else | |
| 841 { | |
| 842 GaimPlugin *loader; | |
| 843 GaimPluginLoaderInfo *loader_info; | |
| 844 | |
| 845 loader = find_loader_for_plugin(plugin); | |
| 846 | |
| 847 if (loader != NULL) | |
| 848 { | |
| 849 loader_info = GAIM_PLUGIN_LOADER_INFO(loader); | |
| 850 | |
| 851 if (loader_info->destroy != NULL) | |
| 852 loader_info->destroy(plugin); | |
| 853 } | |
| 854 } | |
| 855 | |
| 856 g_free(plugin->path); | |
| 857 g_free(plugin->error); | |
| 858 | |
| 859 GAIM_DBUS_UNREGISTER_POINTER(plugin); | |
| 860 | |
| 861 g_free(plugin); | |
| 862 #endif /* !GAIM_PLUGINS */ | |
| 863 } | |
| 864 | |
| 865 gboolean | |
| 866 gaim_plugin_is_loaded(const GaimPlugin *plugin) | |
| 867 { | |
| 868 g_return_val_if_fail(plugin != NULL, FALSE); | |
| 869 | |
| 870 return plugin->loaded; | |
| 871 } | |
| 872 | |
| 873 gboolean | |
| 874 gaim_plugin_is_unloadable(const GaimPlugin *plugin) | |
| 875 { | |
| 876 g_return_val_if_fail(plugin != NULL, FALSE); | |
| 877 | |
| 878 return plugin->unloadable; | |
| 879 } | |
| 880 | |
| 881 const gchar * | |
| 882 gaim_plugin_get_id(const GaimPlugin *plugin) { | |
| 883 g_return_val_if_fail(plugin, NULL); | |
| 884 g_return_val_if_fail(plugin->info, NULL); | |
| 885 | |
| 886 return plugin->info->id; | |
| 887 } | |
| 888 | |
| 889 const gchar * | |
| 890 gaim_plugin_get_name(const GaimPlugin *plugin) { | |
| 891 g_return_val_if_fail(plugin, NULL); | |
| 892 g_return_val_if_fail(plugin->info, NULL); | |
| 893 | |
| 894 return plugin->info->name; | |
| 895 } | |
| 896 | |
| 897 const gchar * | |
| 898 gaim_plugin_get_version(const GaimPlugin *plugin) { | |
| 899 g_return_val_if_fail(plugin, NULL); | |
| 900 g_return_val_if_fail(plugin->info, NULL); | |
| 901 | |
| 902 return plugin->info->version; | |
| 903 } | |
| 904 | |
| 905 const gchar * | |
| 906 gaim_plugin_get_summary(const GaimPlugin *plugin) { | |
| 907 g_return_val_if_fail(plugin, NULL); | |
| 908 g_return_val_if_fail(plugin->info, NULL); | |
| 909 | |
| 910 return plugin->info->summary; | |
| 911 } | |
| 912 | |
| 913 const gchar * | |
| 914 gaim_plugin_get_description(const GaimPlugin *plugin) { | |
| 915 g_return_val_if_fail(plugin, NULL); | |
| 916 g_return_val_if_fail(plugin->info, NULL); | |
| 917 | |
| 918 return plugin->info->description; | |
| 919 } | |
| 920 | |
| 921 const gchar * | |
| 922 gaim_plugin_get_author(const GaimPlugin *plugin) { | |
| 923 g_return_val_if_fail(plugin, NULL); | |
| 924 g_return_val_if_fail(plugin->info, NULL); | |
| 925 | |
| 926 return plugin->info->author; | |
| 927 } | |
| 928 | |
| 929 const gchar * | |
| 930 gaim_plugin_get_homepage(const GaimPlugin *plugin) { | |
| 931 g_return_val_if_fail(plugin, NULL); | |
| 932 g_return_val_if_fail(plugin->info, NULL); | |
| 933 | |
| 934 return plugin->info->homepage; | |
| 935 } | |
| 936 | |
| 937 /************************************************************************** | |
| 938 * Plugin IPC | |
| 939 **************************************************************************/ | |
| 940 static void | |
| 941 destroy_ipc_info(void *data) | |
| 942 { | |
| 943 GaimPluginIpcCommand *ipc_command = (GaimPluginIpcCommand *)data; | |
| 944 int i; | |
| 945 | |
| 946 if (ipc_command->params != NULL) | |
| 947 { | |
| 948 for (i = 0; i < ipc_command->num_params; i++) | |
| 949 gaim_value_destroy(ipc_command->params[i]); | |
| 950 | |
| 951 g_free(ipc_command->params); | |
| 952 } | |
| 953 | |
| 954 if (ipc_command->ret_value != NULL) | |
| 955 gaim_value_destroy(ipc_command->ret_value); | |
| 956 | |
| 957 g_free(ipc_command); | |
| 958 } | |
| 959 | |
| 960 gboolean | |
| 961 gaim_plugin_ipc_register(GaimPlugin *plugin, const char *command, | |
| 962 GaimCallback func, GaimSignalMarshalFunc marshal, | |
| 963 GaimValue *ret_value, int num_params, ...) | |
| 964 { | |
| 965 GaimPluginIpcInfo *ipc_info; | |
| 966 GaimPluginIpcCommand *ipc_command; | |
| 967 | |
| 968 g_return_val_if_fail(plugin != NULL, FALSE); | |
| 969 g_return_val_if_fail(command != NULL, FALSE); | |
| 970 g_return_val_if_fail(func != NULL, FALSE); | |
| 971 g_return_val_if_fail(marshal != NULL, FALSE); | |
| 972 | |
| 973 if (plugin->ipc_data == NULL) | |
| 974 { | |
| 975 ipc_info = plugin->ipc_data = g_new0(GaimPluginIpcInfo, 1); | |
| 976 ipc_info->commands = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 977 g_free, destroy_ipc_info); | |
| 978 } | |
| 979 else | |
| 980 ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; | |
| 981 | |
| 982 ipc_command = g_new0(GaimPluginIpcCommand, 1); | |
| 983 ipc_command->func = func; | |
| 984 ipc_command->marshal = marshal; | |
| 985 ipc_command->num_params = num_params; | |
| 986 ipc_command->ret_value = ret_value; | |
| 987 | |
| 988 if (num_params > 0) | |
| 989 { | |
| 990 va_list args; | |
| 991 int i; | |
| 992 | |
| 993 ipc_command->params = g_new0(GaimValue *, num_params); | |
| 994 | |
| 995 va_start(args, num_params); | |
| 996 | |
| 997 for (i = 0; i < num_params; i++) | |
| 998 ipc_command->params[i] = va_arg(args, GaimValue *); | |
| 999 | |
| 1000 va_end(args); | |
| 1001 } | |
| 1002 | |
| 1003 g_hash_table_replace(ipc_info->commands, g_strdup(command), ipc_command); | |
| 1004 | |
| 1005 ipc_info->command_count++; | |
| 1006 | |
| 1007 return TRUE; | |
| 1008 } | |
| 1009 | |
| 1010 void | |
| 1011 gaim_plugin_ipc_unregister(GaimPlugin *plugin, const char *command) | |
| 1012 { | |
| 1013 GaimPluginIpcInfo *ipc_info; | |
| 1014 | |
| 1015 g_return_if_fail(plugin != NULL); | |
| 1016 g_return_if_fail(command != NULL); | |
| 1017 | |
| 1018 ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; | |
| 1019 | |
| 1020 if (ipc_info == NULL || | |
| 1021 g_hash_table_lookup(ipc_info->commands, command) == NULL) | |
| 1022 { | |
| 1023 gaim_debug_error("plugins", | |
| 1024 "IPC command '%s' was not registered for plugin %s\n", | |
| 1025 command, plugin->info->name); | |
| 1026 return; | |
| 1027 } | |
| 1028 | |
| 1029 g_hash_table_remove(ipc_info->commands, command); | |
| 1030 | |
| 1031 ipc_info->command_count--; | |
| 1032 | |
| 1033 if (ipc_info->command_count == 0) | |
| 1034 { | |
| 1035 g_hash_table_destroy(ipc_info->commands); | |
| 1036 g_free(ipc_info); | |
| 1037 | |
| 1038 plugin->ipc_data = NULL; | |
| 1039 } | |
| 1040 } | |
| 1041 | |
| 1042 void | |
| 1043 gaim_plugin_ipc_unregister_all(GaimPlugin *plugin) | |
| 1044 { | |
| 1045 GaimPluginIpcInfo *ipc_info; | |
| 1046 | |
| 1047 g_return_if_fail(plugin != NULL); | |
| 1048 | |
| 1049 if (plugin->ipc_data == NULL) | |
| 1050 return; /* Silently ignore it. */ | |
| 1051 | |
| 1052 ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; | |
| 1053 | |
| 1054 g_hash_table_destroy(ipc_info->commands); | |
| 1055 g_free(ipc_info); | |
| 1056 | |
| 1057 plugin->ipc_data = NULL; | |
| 1058 } | |
| 1059 | |
| 1060 gboolean | |
| 1061 gaim_plugin_ipc_get_params(GaimPlugin *plugin, const char *command, | |
| 1062 GaimValue **ret_value, int *num_params, | |
| 1063 GaimValue ***params) | |
| 1064 { | |
| 1065 GaimPluginIpcInfo *ipc_info; | |
| 1066 GaimPluginIpcCommand *ipc_command; | |
| 1067 | |
| 1068 g_return_val_if_fail(plugin != NULL, FALSE); | |
| 1069 g_return_val_if_fail(command != NULL, FALSE); | |
| 1070 | |
| 1071 ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; | |
| 1072 | |
| 1073 if (ipc_info == NULL || | |
| 1074 (ipc_command = g_hash_table_lookup(ipc_info->commands, | |
| 1075 command)) == NULL) | |
| 1076 { | |
| 1077 gaim_debug_error("plugins", | |
| 1078 "IPC command '%s' was not registered for plugin %s\n", | |
| 1079 command, plugin->info->name); | |
| 1080 | |
| 1081 return FALSE; | |
| 1082 } | |
| 1083 | |
| 1084 if (num_params != NULL) | |
| 1085 *num_params = ipc_command->num_params; | |
| 1086 | |
| 1087 if (params != NULL) | |
| 1088 *params = ipc_command->params; | |
| 1089 | |
| 1090 if (ret_value != NULL) | |
| 1091 *ret_value = ipc_command->ret_value; | |
| 1092 | |
| 1093 return TRUE; | |
| 1094 } | |
| 1095 | |
| 1096 void * | |
| 1097 gaim_plugin_ipc_call(GaimPlugin *plugin, const char *command, | |
| 1098 gboolean *ok, ...) | |
| 1099 { | |
| 1100 GaimPluginIpcInfo *ipc_info; | |
| 1101 GaimPluginIpcCommand *ipc_command; | |
| 1102 va_list args; | |
| 1103 void *ret_value; | |
| 1104 | |
| 1105 if (ok != NULL) | |
| 1106 *ok = FALSE; | |
| 1107 | |
| 1108 g_return_val_if_fail(plugin != NULL, NULL); | |
| 1109 g_return_val_if_fail(command != NULL, NULL); | |
| 1110 | |
| 1111 ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; | |
| 1112 | |
| 1113 if (ipc_info == NULL || | |
| 1114 (ipc_command = g_hash_table_lookup(ipc_info->commands, | |
| 1115 command)) == NULL) | |
| 1116 { | |
| 1117 gaim_debug_error("plugins", | |
| 1118 "IPC command '%s' was not registered for plugin %s\n", | |
| 1119 command, plugin->info->name); | |
| 1120 | |
| 1121 return NULL; | |
| 1122 } | |
| 1123 | |
| 1124 va_start(args, ok); | |
| 1125 ipc_command->marshal(ipc_command->func, args, NULL, &ret_value); | |
| 1126 va_end(args); | |
| 1127 | |
| 1128 if (ok != NULL) | |
| 1129 *ok = TRUE; | |
| 1130 | |
| 1131 return ret_value; | |
| 1132 } | |
| 1133 | |
| 1134 /************************************************************************** | |
| 1135 * Plugins subsystem | |
| 1136 **************************************************************************/ | |
| 1137 void * | |
| 1138 gaim_plugins_get_handle(void) { | |
| 1139 static int handle; | |
| 1140 | |
| 1141 return &handle; | |
| 1142 } | |
| 1143 | |
| 1144 void | |
| 1145 gaim_plugins_init(void) { | |
| 1146 void *handle = gaim_plugins_get_handle(); | |
| 1147 | |
| 1148 gaim_signal_register(handle, "plugin-load", | |
| 1149 gaim_marshal_VOID__POINTER, | |
| 1150 NULL, 1, | |
| 1151 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
| 1152 GAIM_SUBTYPE_PLUGIN)); | |
| 1153 gaim_signal_register(handle, "plugin-unload", | |
| 1154 gaim_marshal_VOID__POINTER, | |
| 1155 NULL, 1, | |
| 1156 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
| 1157 GAIM_SUBTYPE_PLUGIN)); | |
| 1158 } | |
| 1159 | |
| 1160 void | |
| 1161 gaim_plugins_uninit(void) { | |
| 1162 gaim_signals_disconnect_by_handle(gaim_plugins_get_handle()); | |
| 1163 } | |
| 1164 | |
| 1165 /************************************************************************** | |
| 1166 * Plugins API | |
| 1167 **************************************************************************/ | |
| 1168 void | |
| 1169 gaim_plugins_add_search_path(const char *path) | |
| 1170 { | |
| 1171 g_return_if_fail(path != NULL); | |
| 1172 | |
| 1173 if (g_list_find_custom(search_paths, path, (GCompareFunc)strcmp)) | |
| 1174 return; | |
| 1175 | |
| 1176 search_paths = g_list_append(search_paths, strdup(path)); | |
| 1177 } | |
| 1178 | |
| 1179 void | |
| 1180 gaim_plugins_unload_all(void) | |
| 1181 { | |
| 1182 #ifdef GAIM_PLUGINS | |
| 1183 | |
| 1184 while (loaded_plugins != NULL) | |
| 1185 gaim_plugin_unload(loaded_plugins->data); | |
| 1186 | |
| 1187 #endif /* GAIM_PLUGINS */ | |
| 1188 } | |
| 1189 | |
| 1190 void | |
| 1191 gaim_plugins_destroy_all(void) | |
| 1192 { | |
| 1193 #ifdef GAIM_PLUGINS | |
| 1194 | |
| 1195 while (plugins != NULL) | |
| 1196 gaim_plugin_destroy(plugins->data); | |
| 1197 | |
| 1198 #endif /* GAIM_PLUGINS */ | |
| 1199 } | |
| 1200 | |
| 1201 void | |
| 1202 gaim_plugins_load_saved(const char *key) | |
| 1203 { | |
| 1204 #ifdef GAIM_PLUGINS | |
| 1205 GList *f, *files; | |
| 1206 | |
| 1207 g_return_if_fail(key != NULL); | |
| 1208 | |
| 1209 files = gaim_prefs_get_string_list(key); | |
| 1210 | |
| 1211 for (f = files; f; f = f->next) | |
| 1212 { | |
| 1213 char *filename; | |
| 1214 char *basename; | |
| 1215 GaimPlugin *plugin; | |
| 1216 | |
| 1217 if (f->data == NULL) | |
| 1218 continue; | |
| 1219 | |
| 1220 filename = f->data; | |
| 1221 | |
| 1222 /* | |
| 1223 * We don't know if the filename uses Windows or Unix path | |
| 1224 * separators (because people might be sharing a prefs.xml | |
| 1225 * file across systems), so we find the last occurrence | |
| 1226 * of either. | |
| 1227 */ | |
| 1228 basename = strrchr(filename, '/'); | |
| 1229 if ((basename == NULL) || (basename < strrchr(filename, '\\'))) | |
| 1230 basename = strrchr(filename, '\\'); | |
| 1231 if (basename != NULL) | |
| 1232 basename++; | |
| 1233 | |
| 1234 /* Strip the extension */ | |
| 1235 if (basename) | |
| 1236 basename = gaim_plugin_get_basename(filename); | |
| 1237 | |
| 1238 if ((plugin = gaim_plugins_find_with_filename(filename)) != NULL) | |
| 1239 { | |
| 1240 gaim_debug_info("plugins", "Loading saved plugin %s\n", | |
| 1241 plugin->path); | |
| 1242 gaim_plugin_load(plugin); | |
| 1243 } | |
| 1244 else if (basename && (plugin = gaim_plugins_find_with_basename(basename)) != NULL) | |
| 1245 { | |
| 1246 gaim_debug_info("plugins", "Loading saved plugin %s\n", | |
| 1247 plugin->path); | |
| 1248 gaim_plugin_load(plugin); | |
| 1249 } | |
| 1250 else | |
| 1251 { | |
| 1252 gaim_debug_error("plugins", "Unable to find saved plugin %s\n", | |
| 1253 filename); | |
| 1254 } | |
| 1255 | |
| 1256 g_free(basename); | |
| 1257 | |
| 1258 g_free(f->data); | |
| 1259 } | |
| 1260 | |
| 1261 g_list_free(files); | |
| 1262 #endif /* GAIM_PLUGINS */ | |
| 1263 } | |
| 1264 | |
| 1265 | |
| 1266 void | |
| 1267 gaim_plugins_probe(const char *ext) | |
| 1268 { | |
| 1269 #ifdef GAIM_PLUGINS | |
| 1270 GDir *dir; | |
| 1271 const gchar *file; | |
| 1272 gchar *path; | |
| 1273 GaimPlugin *plugin; | |
| 1274 GList *cur; | |
| 1275 const char *search_path; | |
| 1276 | |
| 1277 if (!g_module_supported()) | |
| 1278 return; | |
| 1279 | |
| 1280 /* Probe plugins */ | |
| 1281 for (cur = search_paths; cur != NULL; cur = cur->next) | |
| 1282 { | |
| 1283 search_path = cur->data; | |
| 1284 | |
| 1285 dir = g_dir_open(search_path, 0, NULL); | |
| 1286 | |
| 1287 if (dir != NULL) | |
| 1288 { | |
| 1289 while ((file = g_dir_read_name(dir)) != NULL) | |
| 1290 { | |
| 1291 path = g_build_filename(search_path, file, NULL); | |
| 1292 | |
| 1293 if (ext == NULL || has_file_extension(file, ext)) | |
| 1294 plugin = gaim_plugin_probe(path); | |
| 1295 | |
| 1296 g_free(path); | |
| 1297 } | |
| 1298 | |
| 1299 g_dir_close(dir); | |
| 1300 } | |
| 1301 } | |
| 1302 | |
| 1303 /* See if we have any plugins waiting to load */ | |
| 1304 while (load_queue != NULL) | |
| 1305 { | |
| 1306 plugin = (GaimPlugin *)load_queue->data; | |
| 1307 | |
| 1308 load_queue = g_list_remove(load_queue, plugin); | |
| 1309 | |
| 1310 if (plugin == NULL || plugin->info == NULL) | |
| 1311 continue; | |
| 1312 | |
| 1313 if (plugin->info->type == GAIM_PLUGIN_LOADER) | |
| 1314 { | |
| 1315 /* We'll just load this right now. */ | |
| 1316 if (!gaim_plugin_load(plugin)) | |
| 1317 { | |
| 1318 gaim_plugin_destroy(plugin); | |
| 1319 | |
| 1320 continue; | |
| 1321 } | |
| 1322 | |
| 1323 plugin_loaders = g_list_append(plugin_loaders, plugin); | |
| 1324 | |
| 1325 for (cur = GAIM_PLUGIN_LOADER_INFO(plugin)->exts; | |
| 1326 cur != NULL; | |
| 1327 cur = cur->next) | |
| 1328 { | |
| 1329 gaim_plugins_probe(cur->data); | |
| 1330 } | |
| 1331 } | |
| 1332 else if (plugin->info->type == GAIM_PLUGIN_PROTOCOL) | |
| 1333 { | |
| 1334 /* We'll just load this right now. */ | |
| 1335 if (!gaim_plugin_load(plugin)) | |
| 1336 { | |
| 1337 gaim_plugin_destroy(plugin); | |
| 1338 | |
| 1339 continue; | |
| 1340 } | |
| 1341 | |
| 1342 /* Make sure we don't load two PRPLs with the same name? */ | |
| 1343 if (gaim_find_prpl(plugin->info->id)) | |
| 1344 { | |
| 1345 /* Nothing to see here--move along, move along */ | |
| 1346 gaim_plugin_destroy(plugin); | |
| 1347 | |
| 1348 continue; | |
| 1349 } | |
| 1350 | |
| 1351 protocol_plugins = g_list_insert_sorted(protocol_plugins, plugin, | |
| 1352 (GCompareFunc)compare_prpl); | |
| 1353 } | |
| 1354 } | |
| 1355 | |
| 1356 if (probe_cb != NULL) | |
| 1357 probe_cb(probe_cb_data); | |
| 1358 #endif /* GAIM_PLUGINS */ | |
| 1359 } | |
| 1360 | |
| 1361 gboolean | |
| 1362 gaim_plugin_register(GaimPlugin *plugin) | |
| 1363 { | |
| 1364 g_return_val_if_fail(plugin != NULL, FALSE); | |
| 1365 | |
| 1366 /* If this plugin has been registered already then exit */ | |
| 1367 if (g_list_find(plugins, plugin)) | |
| 1368 return TRUE; | |
| 1369 | |
| 1370 /* Ensure the plugin has the requisite information */ | |
| 1371 if (plugin->info->type == GAIM_PLUGIN_LOADER) | |
| 1372 { | |
| 1373 GaimPluginLoaderInfo *loader_info; | |
| 1374 | |
| 1375 loader_info = GAIM_PLUGIN_LOADER_INFO(plugin); | |
| 1376 | |
| 1377 if (loader_info == NULL) | |
| 1378 { | |
| 1379 gaim_debug_error("plugins", "%s is unloadable\n", | |
| 1380 plugin->path); | |
| 1381 return FALSE; | |
| 1382 } | |
| 1383 } | |
| 1384 else if (plugin->info->type == GAIM_PLUGIN_PROTOCOL) | |
| 1385 { | |
| 1386 GaimPluginProtocolInfo *prpl_info; | |
| 1387 | |
| 1388 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin); | |
| 1389 | |
| 1390 if (prpl_info == NULL) | |
| 1391 { | |
| 1392 gaim_debug_error("plugins", "%s is unloadable\n", | |
| 1393 plugin->path); | |
| 1394 return FALSE; | |
| 1395 } | |
| 1396 } | |
| 1397 | |
| 1398 #ifdef GAIM_PLUGINS | |
| 1399 /* This plugin should be probed and maybe loaded--add it to the queue */ | |
| 1400 load_queue = g_list_append(load_queue, plugin); | |
| 1401 #else | |
| 1402 if (plugin->info != NULL) | |
| 1403 { | |
| 1404 if (plugin->info->type == GAIM_PLUGIN_PROTOCOL) | |
| 1405 protocol_plugins = g_list_insert_sorted(protocol_plugins, plugin, | |
| 1406 (GCompareFunc)compare_prpl); | |
| 1407 if (plugin->info->load != NULL) | |
| 1408 if (!plugin->info->load(plugin)) | |
| 1409 return FALSE; | |
| 1410 } | |
| 1411 #endif | |
| 1412 | |
| 1413 plugins = g_list_append(plugins, plugin); | |
| 1414 | |
| 1415 return TRUE; | |
| 1416 } | |
| 1417 | |
| 1418 gboolean | |
| 1419 gaim_plugins_enabled(void) | |
| 1420 { | |
| 1421 #ifdef GAIM_PLUGINS | |
| 1422 return TRUE; | |
| 1423 #else | |
| 1424 return FALSE; | |
| 1425 #endif | |
| 1426 } | |
| 1427 | |
| 1428 void | |
| 1429 gaim_plugins_register_probe_notify_cb(void (*func)(void *), void *data) | |
| 1430 { | |
| 1431 /* TODO */ | |
| 1432 probe_cb = func; | |
| 1433 probe_cb_data = data; | |
| 1434 } | |
| 1435 | |
| 1436 void | |
| 1437 gaim_plugins_unregister_probe_notify_cb(void (*func)(void *)) | |
| 1438 { | |
| 1439 /* TODO */ | |
| 1440 probe_cb = NULL; | |
| 1441 probe_cb_data = NULL; | |
| 1442 } | |
| 1443 | |
| 1444 void | |
| 1445 gaim_plugins_register_load_notify_cb(void (*func)(GaimPlugin *, void *), | |
| 1446 void *data) | |
| 1447 { | |
| 1448 /* TODO */ | |
| 1449 load_cb = func; | |
| 1450 load_cb_data = data; | |
| 1451 } | |
| 1452 | |
| 1453 void | |
| 1454 gaim_plugins_unregister_load_notify_cb(void (*func)(GaimPlugin *, void *)) | |
| 1455 { | |
| 1456 /* TODO */ | |
| 1457 load_cb = NULL; | |
| 1458 load_cb_data = NULL; | |
| 1459 } | |
| 1460 | |
| 1461 void | |
| 1462 gaim_plugins_register_unload_notify_cb(void (*func)(GaimPlugin *, void *), | |
| 1463 void *data) | |
| 1464 { | |
| 1465 /* TODO */ | |
| 1466 unload_cb = func; | |
| 1467 unload_cb_data = data; | |
| 1468 } | |
| 1469 | |
| 1470 void | |
| 1471 gaim_plugins_unregister_unload_notify_cb(void (*func)(GaimPlugin *, void *)) | |
| 1472 { | |
| 1473 /* TODO */ | |
| 1474 unload_cb = NULL; | |
| 1475 unload_cb_data = NULL; | |
| 1476 } | |
| 1477 | |
| 1478 GaimPlugin * | |
| 1479 gaim_plugins_find_with_name(const char *name) | |
| 1480 { | |
| 1481 GaimPlugin *plugin; | |
| 1482 GList *l; | |
| 1483 | |
| 1484 for (l = plugins; l != NULL; l = l->next) { | |
| 1485 plugin = l->data; | |
| 1486 | |
| 1487 if (!strcmp(plugin->info->name, name)) | |
| 1488 return plugin; | |
| 1489 } | |
| 1490 | |
| 1491 return NULL; | |
| 1492 } | |
| 1493 | |
| 1494 GaimPlugin * | |
| 1495 gaim_plugins_find_with_filename(const char *filename) | |
| 1496 { | |
| 1497 GaimPlugin *plugin; | |
| 1498 GList *l; | |
| 1499 | |
| 1500 for (l = plugins; l != NULL; l = l->next) { | |
| 1501 plugin = l->data; | |
| 1502 | |
| 1503 if (plugin->path != NULL && !strcmp(plugin->path, filename)) | |
| 1504 return plugin; | |
| 1505 } | |
| 1506 | |
| 1507 return NULL; | |
| 1508 } | |
| 1509 | |
| 1510 GaimPlugin * | |
| 1511 gaim_plugins_find_with_basename(const char *basename) | |
| 1512 { | |
| 1513 #ifdef GAIM_PLUGINS | |
| 1514 GaimPlugin *plugin; | |
| 1515 GList *l; | |
| 1516 char *tmp; | |
| 1517 | |
| 1518 g_return_val_if_fail(basename != NULL, NULL); | |
| 1519 | |
| 1520 for (l = plugins; l != NULL; l = l->next) | |
| 1521 { | |
| 1522 plugin = (GaimPlugin *)l->data; | |
| 1523 | |
| 1524 if (plugin->path != NULL) { | |
| 1525 tmp = gaim_plugin_get_basename(plugin->path); | |
| 1526 if (!strcmp(tmp, basename)) | |
| 1527 { | |
| 1528 g_free(tmp); | |
| 1529 return plugin; | |
| 1530 } | |
| 1531 g_free(tmp); | |
| 1532 } | |
| 1533 } | |
| 1534 | |
| 1535 #endif /* GAIM_PLUGINS */ | |
| 1536 | |
| 1537 return NULL; | |
| 1538 } | |
| 1539 | |
| 1540 GaimPlugin * | |
| 1541 gaim_plugins_find_with_id(const char *id) | |
| 1542 { | |
| 1543 GaimPlugin *plugin; | |
| 1544 GList *l; | |
| 1545 | |
| 1546 g_return_val_if_fail(id != NULL, NULL); | |
| 1547 | |
| 1548 for (l = plugins; l != NULL; l = l->next) | |
| 1549 { | |
| 1550 plugin = l->data; | |
| 1551 | |
| 1552 if (plugin->info->id != NULL && !strcmp(plugin->info->id, id)) | |
| 1553 return plugin; | |
| 1554 } | |
| 1555 | |
| 1556 return NULL; | |
| 1557 } | |
| 1558 | |
| 1559 GList * | |
| 1560 gaim_plugins_get_loaded(void) | |
| 1561 { | |
| 1562 return loaded_plugins; | |
| 1563 } | |
| 1564 | |
| 1565 GList * | |
| 1566 gaim_plugins_get_protocols(void) | |
| 1567 { | |
| 1568 return protocol_plugins; | |
| 1569 } | |
| 1570 | |
| 1571 GList * | |
| 1572 gaim_plugins_get_all(void) | |
| 1573 { | |
| 1574 return plugins; | |
| 1575 } | |
| 1576 | |
| 1577 | |
| 1578 GaimPluginAction * | |
| 1579 gaim_plugin_action_new(const char* label, void (*callback)(GaimPluginAction *)) | |
| 1580 { | |
| 1581 GaimPluginAction *act = g_new0(GaimPluginAction, 1); | |
| 1582 | |
| 1583 act->label = g_strdup(label); | |
| 1584 act->callback = callback; | |
| 1585 | |
| 1586 return act; | |
| 1587 } | |
| 1588 | |
| 1589 void | |
| 1590 gaim_plugin_action_free(GaimPluginAction *action) | |
| 1591 { | |
| 1592 g_return_if_fail(action != NULL); | |
| 1593 | |
| 1594 g_free(action->label); | |
| 1595 g_free(action); | |
| 1596 } |
