gee
oldstatus
2005-09-19, Nathan Walp
* Gaim is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #define PLUGIN_EXT_WIN32 ".dll" #define PLUGIN_EXT_HPUX ".sl" #define PLUGIN_EXT_UNIX ".so" # define PLUGIN_EXT PLUGIN_EXT_WIN32 # define PLUGIN_EXT PLUGIN_EXT_HPUX # define PLUGIN_EXT PLUGIN_EXT_UNIX GaimSignalMarshalFunc marshal; static GList *loaded_plugins = NULL; static GList *plugins = NULL; static GList *plugin_loaders = NULL; static GList *protocol_plugins = NULL; static GList *load_queue = NULL; static size_t search_path_count = 0; static char **search_paths = NULL; static void (*probe_cb)(void *) = NULL; static void *probe_cb_data = NULL; static void (*load_cb)(GaimPlugin *, void *) = NULL; static void *load_cb_data = NULL; static void (*unload_cb)(GaimPlugin *, void *) = NULL; static void *unload_cb_data = NULL; gaim_plugins_get_handle(void) is_so_file(const char *filename, const char *ext) if (filename == NULL || *filename == '\0' || ext == NULL) len = strlen(filename) - extlen; return (!strncmp(filename + len, ext, extlen)); loader_supports_file(GaimPlugin *loader, const char *filename) for (exts = GAIM_PLUGIN_LOADER_INFO(loader)->exts; exts != NULL; exts = exts->next) { if (is_so_file(filename, (char *)exts->data)) { find_loader_for_plugin(const GaimPlugin *plugin) if (plugin->path == NULL) for (l = gaim_plugins_get_loaded(); l != NULL; l = l->next) { if (loader->info->type == GAIM_PLUGIN_LOADER && loader_supports_file(loader, plugin->path)) { #endif /* GAIM_PLUGINS */ compare_prpl(GaimPlugin *a, GaimPlugin *b) /* neg if a before b, 0 if equal, pos if a after b */ if(GAIM_IS_PROTOCOL_PLUGIN(a)) { if(GAIM_IS_PROTOCOL_PLUGIN(b)) return strcmp(a->info->name, b->info->name); if(GAIM_IS_PROTOCOL_PLUGIN(b)) gaim_plugin_new(gboolean native, const char *path) plugin = g_new0(GaimPlugin, 1); plugin->native_plugin = native; plugin->path = (path == NULL ? NULL : g_strdup(path)); gaim_plugin_probe(const char *filename) GaimPlugin *plugin = NULL; gboolean (*gaim_init_plugin)(GaimPlugin *); gaim_debug_misc("plugins", "probing %s\n", filename); g_return_val_if_fail(filename != NULL, NULL); if (!g_file_test(filename, G_FILE_TEST_EXISTS)) plugin = gaim_plugins_find_with_filename(filename); plugin = gaim_plugin_new(is_so_file(filename, PLUGIN_EXT), filename); if (plugin->native_plugin) { gpointer pgaim_init_plugin; plugin->handle = g_module_open(filename, 0); if (plugin->handle == NULL) { error = g_module_error(); gaim_debug(GAIM_DEBUG_ERROR, "plugins", "%s is unloadable: %s\n", plugin->path, error ? error : "Unknown error."); gaim_plugin_destroy(plugin); if (!g_module_symbol(plugin->handle, "gaim_init_plugin", g_module_close(plugin->handle); error = g_module_error(); gaim_debug(GAIM_DEBUG_ERROR, "plugins", "%s is unloadable: %s\n", plugin->path, error ? error : "Unknown error."); gaim_plugin_destroy(plugin); gaim_init_plugin = pgaim_init_plugin; loader = find_loader_for_plugin(plugin); gaim_plugin_destroy(plugin); gaim_init_plugin = GAIM_PLUGIN_LOADER_INFO(loader)->probe; if (!gaim_init_plugin(plugin) || plugin->info == NULL) { gaim_plugin_destroy(plugin); if (plugin->info->magic != GAIM_PLUGIN_MAGIC || plugin->info->major_version != GAIM_MAJOR_VERSION || plugin->info->minor_version > GAIM_MINOR_VERSION) gaim_debug(GAIM_DEBUG_ERROR, "plugins", "%s is unloadable: API version mismatch %d.%d.x (need %d.%d.x)\n", plugin->path, plugin->info->major_version, plugin->info->minor_version, GAIM_MAJOR_VERSION, GAIM_MINOR_VERSION); gaim_plugin_destroy(plugin); #endif /* !GAIM_PLUGINS */ gaim_plugin_load(GaimPlugin *plugin) g_return_val_if_fail(plugin != NULL, FALSE); g_return_val_if_fail(plugin->error == NULL, FALSE); if (gaim_plugin_is_loaded(plugin)) * Go through the list of the plugin's dependencies. * First pass: Make sure all the plugins needed are probed. for (l = plugin->info->dependencies; l != NULL; l = l->next) const char *dep_name = (const char *)l->data; dep_plugin = gaim_plugins_find_with_id(dep_name); g_snprintf(buf, sizeof(buf), _("The required plugin %s was not found. " "Please install this plugin and try again."), gaim_notify_error(NULL, NULL, _("Gaim was unable to load your plugin."), dep_list = g_list_append(dep_list, dep_plugin); /* Second pass: load all the required plugins. */ for (l = dep_list; l != NULL; l = l->next) GaimPlugin *dep_plugin = (GaimPlugin *)l->data; if (!gaim_plugin_is_loaded(dep_plugin)) if (!gaim_plugin_load(dep_plugin)) g_snprintf(buf, sizeof(buf), _("The required plugin %s was unable to load."), gaim_notify_error(NULL, NULL, _("Gaim was unable to load your plugin."), if (plugin->native_plugin) if (plugin->info != NULL && plugin->info->load != NULL) if (!plugin->info->load(plugin)) GaimPluginLoaderInfo *loader_info; loader = find_loader_for_plugin(plugin); loader_info = GAIM_PLUGIN_LOADER_INFO(loader); if (loader_info->load != NULL) if (!loader_info->load(plugin)) loaded_plugins = g_list_append(loaded_plugins, plugin); load_cb(plugin, load_cb_data); gaim_signal_emit(gaim_plugins_get_handle(), "plugin-load", plugin); #endif /* !GAIM_PLUGINS */ gaim_plugin_unload(GaimPlugin *plugin) g_return_val_if_fail(plugin != NULL, FALSE); loaded_plugins = g_list_remove(loaded_plugins, plugin); g_return_val_if_fail(gaim_plugin_is_loaded(plugin), FALSE); gaim_debug(GAIM_DEBUG_INFO, "plugins", "Unloading plugin %s\n", /* cancel any pending dialogs the plugin has */ gaim_request_close_with_handle(plugin); gaim_notify_close_with_handle(plugin); if (plugin->native_plugin) { if (plugin->info->unload != NULL) plugin->info->unload(plugin); if (plugin->info->type == GAIM_PLUGIN_PROTOCOL) { GaimPluginProtocolInfo *prpl_info; prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin); for (l = prpl_info->user_splits; l != NULL; l = l->next) gaim_account_user_split_destroy(l->data); for (l = prpl_info->protocol_options; l != NULL; l = l->next) gaim_account_option_destroy(l->data); if (prpl_info->user_splits != NULL) g_list_free(prpl_info->user_splits); if (prpl_info->protocol_options != NULL) g_list_free(prpl_info->protocol_options); GaimPluginLoaderInfo *loader_info; loader = find_loader_for_plugin(plugin); loader_info = GAIM_PLUGIN_LOADER_INFO(loader); if (loader_info->unload != NULL) loader_info->unload(plugin); gaim_signals_disconnect_by_handle(plugin); gaim_plugin_ipc_unregister_all(plugin); unload_cb(plugin, unload_cb_data); /* I suppose this is the right place to call this... */ gaim_signal_emit(gaim_plugins_get_handle(), "plugin-unload", plugin); #endif /* GAIM_PLUGINS */ gaim_plugin_reload(GaimPlugin *plugin) g_return_val_if_fail(plugin != NULL, FALSE); g_return_val_if_fail(gaim_plugin_is_loaded(plugin), FALSE); if (!gaim_plugin_unload(plugin)) if (!gaim_plugin_load(plugin)) #endif /* !GAIM_PLUGINS */ gaim_plugin_destroy(GaimPlugin *plugin) g_return_if_fail(plugin != NULL); if (gaim_plugin_is_loaded(plugin)) gaim_plugin_unload(plugin); plugins = g_list_remove(plugins, plugin); load_queue = g_list_remove(load_queue, plugin); /* true, this may leak a little memory if there is a major version * mismatch, but it's a lot better than trying to free something * we shouldn't, and crashing while trying to load an old plugin */ if(plugin->info == NULL || plugin->info->magic != GAIM_PLUGIN_MAGIC || plugin->info->major_version != GAIM_MAJOR_VERSION) { g_module_close(plugin->handle); if (plugin->info != NULL && plugin->info->dependencies != NULL) g_list_free(plugin->info->dependencies); if (plugin->native_plugin) if (plugin->info != NULL && plugin->info->type == GAIM_PLUGIN_LOADER) GaimPluginLoaderInfo *loader_info; GList *exts, *l, *next_l; loader_info = GAIM_PLUGIN_LOADER_INFO(plugin); if (loader_info != NULL && loader_info->exts != NULL) for (exts = GAIM_PLUGIN_LOADER_INFO(plugin)->exts; for (l = gaim_plugins_get_all(); l != NULL; l = next_l) is_so_file(p2->path, exts->data)) g_list_free(loader_info->exts); plugin_loaders = g_list_remove(plugin_loaders, plugin); if (plugin->info != NULL && plugin->info->destroy != NULL) plugin->info->destroy(plugin); if (plugin->handle != NULL) g_module_close(plugin->handle); GaimPluginLoaderInfo *loader_info; loader = find_loader_for_plugin(plugin); loader_info = GAIM_PLUGIN_LOADER_INFO(loader); if (loader_info->destroy != NULL) loader_info->destroy(plugin); if (plugin->path != NULL) g_free(plugin->path); if (plugin->error != NULL) g_free(plugin->error); #endif /* !GAIM_PLUGINS */ gaim_plugin_is_loaded(const GaimPlugin *plugin) g_return_val_if_fail(plugin != NULL, FALSE); /************************************************************************** **************************************************************************/ destroy_ipc_info(void *data) GaimPluginIpcCommand *ipc_command = (GaimPluginIpcCommand *)data; if (ipc_command->params != NULL) for (i = 0; i < ipc_command->num_params; i++) gaim_value_destroy(ipc_command->params[i]); g_free(ipc_command->params); if (ipc_command->ret_value != NULL) gaim_value_destroy(ipc_command->ret_value); gaim_plugin_ipc_register(GaimPlugin *plugin, const char *command, GaimCallback func, GaimSignalMarshalFunc marshal, GaimValue *ret_value, int num_params, ...) GaimPluginIpcInfo *ipc_info; GaimPluginIpcCommand *ipc_command; g_return_val_if_fail(plugin != NULL, FALSE); g_return_val_if_fail(command != NULL, FALSE); g_return_val_if_fail(func != NULL, FALSE); g_return_val_if_fail(marshal != NULL, FALSE); if (plugin->ipc_data == NULL) ipc_info = plugin->ipc_data = g_new0(GaimPluginIpcInfo, 1); ipc_info->commands = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, destroy_ipc_info); ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; ipc_command = g_new0(GaimPluginIpcCommand, 1); ipc_command->func = func; ipc_command->marshal = marshal; ipc_command->num_params = num_params; ipc_command->ret_value = ret_value; ipc_command->params = g_new0(GaimValue *, num_params); va_start(args, num_params); for (i = 0; i < num_params; i++) ipc_command->params[i] = va_arg(args, GaimValue *); g_hash_table_replace(ipc_info->commands, g_strdup(command), ipc_command); ipc_info->command_count++; gaim_plugin_ipc_unregister(GaimPlugin *plugin, const char *command) GaimPluginIpcInfo *ipc_info; g_return_if_fail(plugin != NULL); g_return_if_fail(command != NULL); ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; g_hash_table_lookup(ipc_info->commands, command) == NULL) gaim_debug_error("plugins", "IPC command '%s' was not registered for plugin %s\n", command, plugin->info->name); g_hash_table_remove(ipc_info->commands, command); ipc_info->command_count--; if (ipc_info->command_count == 0) g_hash_table_destroy(ipc_info->commands); gaim_plugin_ipc_unregister_all(GaimPlugin *plugin) GaimPluginIpcInfo *ipc_info; g_return_if_fail(plugin != NULL); if (plugin->ipc_data == NULL) return; /* Silently ignore it. */ ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; g_hash_table_destroy(ipc_info->commands); gaim_plugin_ipc_get_params(GaimPlugin *plugin, const char *command, GaimValue **ret_value, int *num_params, GaimPluginIpcInfo *ipc_info; GaimPluginIpcCommand *ipc_command; g_return_val_if_fail(plugin != NULL, FALSE); g_return_val_if_fail(command != NULL, FALSE); ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; (ipc_command = g_hash_table_lookup(ipc_info->commands, gaim_debug_error("plugins", "IPC command '%s' was not registered for plugin %s\n", command, plugin->info->name); *num_params = ipc_command->num_params; *params = ipc_command->params; *ret_value = ipc_command->ret_value; gaim_plugin_ipc_call(GaimPlugin *plugin, const char *command, GaimPluginIpcInfo *ipc_info; GaimPluginIpcCommand *ipc_command; g_return_val_if_fail(plugin != NULL, NULL); g_return_val_if_fail(command != NULL, NULL); ipc_info = (GaimPluginIpcInfo *)plugin->ipc_data; (ipc_command = g_hash_table_lookup(ipc_info->commands, gaim_debug_error("plugins", "IPC command '%s' was not registered for plugin %s\n", command, plugin->info->name); ipc_command->marshal(ipc_command->func, args, NULL, &ret_value); /************************************************************************** **************************************************************************/ gaim_plugins_set_search_paths(size_t count, char **paths) g_return_if_fail(count > 0); g_return_if_fail(paths != NULL); if (search_paths != NULL) { for (s = 0; s < search_path_count; s++) search_paths = g_new0(char *, count); for (s = 0; s < count; s++) { search_paths[s] = g_strdup(paths[s]); search_path_count = count; gaim_plugins_unload_all(void) while (loaded_plugins != NULL) gaim_plugin_unload(loaded_plugins->data); #endif /* GAIM_PLUGINS */ gaim_plugins_destroy_all(void) gaim_plugin_destroy(plugins->data); #endif /* GAIM_PLUGINS */ gaim_plugins_load_saved(const char *key) g_return_if_fail(key != NULL); files = gaim_prefs_get_string_list(key); for (f = files; f; f = f->next) * We don't know if the filename uses Windows or Unix path * separators (because people might be sharing a prefs.xml * file across systems), so we find the last occurrence basename = strrchr(filename, '/'); if ((basename == NULL) || (basename < strrchr(filename, '\\'))) basename = strrchr(filename, '\\'); if ((plugin = gaim_plugins_find_with_filename(filename)) != NULL) gaim_debug_info("plugins", "Loading saved plugin %s\n", gaim_plugin_load(plugin); else if ((plugin = gaim_plugins_find_with_basename(basename)) != NULL) gaim_debug_info("plugins", "Loading saved plugin %s\n", gaim_plugin_load(plugin); gaim_debug_error("plugins", "Unable to find saved plugin %s\n", #endif /* GAIM_PLUGINS */ gaim_plugins_probe(const char *ext) if (!g_module_supported()) handle = gaim_plugins_get_handle(); gaim_debug_info("plugins", "registering plugin-load signal\n"); gaim_signal_register(handle, "plugin-load", gaim_marshal_VOID__POINTER, NULL, 1, gaim_value_new(GAIM_TYPE_SUBTYPE, GAIM_SUBTYPE_PLUGIN)); gaim_debug_info("plugins", "registering plugin-unload signal\n"); gaim_signal_register(handle, "plugin-unload", gaim_marshal_VOID__POINTER, NULL, 1, gaim_value_new(GAIM_TYPE_SUBTYPE, GAIM_SUBTYPE_PLUGIN)); for (i = 0; i < search_path_count; i++) { if (search_paths[i] == NULL) dir = g_dir_open(search_paths[i], 0, NULL); while ((file = g_dir_read_name(dir)) != NULL) { path = g_build_filename(search_paths[i], file, NULL); if (ext == NULL || is_so_file(file, ext)) plugin = gaim_plugin_probe(path); /* See if we have any plugins waiting to load. */ while (load_queue != NULL) plugin = (GaimPlugin *)load_queue->data; load_queue = g_list_remove(load_queue, plugin); if (plugin == NULL || plugin->info == NULL) if (plugin->info->type == GAIM_PLUGIN_LOADER) /* We'll just load this right now. */ if (!gaim_plugin_load(plugin)) gaim_plugin_destroy(plugin); plugin_loaders = g_list_append(plugin_loaders, plugin); for (exts = GAIM_PLUGIN_LOADER_INFO(plugin)->exts; gaim_plugins_probe(exts->data); else if (plugin->info->type == GAIM_PLUGIN_PROTOCOL) /* We'll just load this right now. */ if (!gaim_plugin_load(plugin)) gaim_plugin_destroy(plugin); if (gaim_find_prpl(plugin->info->id)) /* Nothing to see here--move along, move along */ gaim_plugin_destroy(plugin); protocol_plugins = g_list_insert_sorted(protocol_plugins, plugin, (GCompareFunc)compare_prpl); #endif /* GAIM_PLUGINS */ gaim_plugin_register(GaimPlugin *plugin) g_return_val_if_fail(plugin != NULL, FALSE); if (g_list_find(plugins, plugin)) if (plugin->info->type == GAIM_PLUGIN_LOADER) GaimPluginLoaderInfo *loader_info; loader_info = GAIM_PLUGIN_LOADER_INFO(plugin); gaim_debug(GAIM_DEBUG_ERROR, "plugins", "%s is unloadable\n", load_queue = g_list_append(load_queue, plugin); else if (plugin->info->type == GAIM_PLUGIN_PROTOCOL) GaimPluginProtocolInfo *prpl_info; prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin); gaim_debug(GAIM_DEBUG_ERROR, "plugins", "%s is unloadable\n", load_queue = g_list_append(load_queue, plugin); plugins = g_list_append(plugins, plugin); gaim_plugins_enabled(void) gaim_plugins_register_probe_notify_cb(void (*func)(void *), void *data) gaim_plugins_unregister_probe_notify_cb(void (*func)(void *)) gaim_plugins_register_load_notify_cb(void (*func)(GaimPlugin *, void *), gaim_plugins_unregister_load_notify_cb(void (*func)(GaimPlugin *, void *)) gaim_plugins_register_unload_notify_cb(void (*func)(GaimPlugin *, void *), gaim_plugins_unregister_unload_notify_cb(void (*func)(GaimPlugin *, void *)) gaim_plugins_find_with_name(const char *name) for (l = plugins; l != NULL; l = l->next) { if (!strcmp(plugin->info->name, name)) gaim_plugins_find_with_filename(const char *filename) for (l = plugins; l != NULL; l = l->next) { if (plugin->path != NULL && !strcmp(plugin->path, filename)) is_native(const char *filename) last_period = strrchr(filename, '.'); return !(strcmp(last_period, PLUGIN_EXT_WIN32) & strcmp(last_period, PLUGIN_EXT_HPUX) & strcmp(last_period, PLUGIN_EXT_UNIX)); gaim_plugin_get_basename(const char *filename) basename = strrchr(filename, G_DIR_SEPARATOR); if (is_native(basename) && ((last_period = strrchr(basename, '.')) != NULL)) return g_strndup(basename, (last_period - basename)); return g_strdup(basename); gaim_plugins_find_with_basename(const char *basename) g_return_val_if_fail(basename != NULL, NULL); basename_no_ext = gaim_plugin_get_basename(basename); for (l = plugins; l != NULL; l = l->next) plugin = (GaimPlugin *)l->data; if (plugin->path != NULL) { tmp = gaim_plugin_get_basename(plugin->path); if (!strcmp(tmp, basename_no_ext)) gaim_plugins_find_with_id(const char *id) g_return_val_if_fail(id != NULL, NULL); for (l = plugins; l != NULL; l = l->next) if (plugin->info->id != NULL && !strcmp(plugin->info->id, id)) gaim_plugins_get_loaded(void) gaim_plugins_get_protocols(void) gaim_plugins_get_all(void) gaim_plugin_action_new(char* label, void (*callback)(GaimPluginAction *)) GaimPluginAction *act = g_new0(GaimPluginAction, 1); act->callback = callback;