1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Use hash table to store plugins

This commit is contained in:
James Booth 2016-06-30 23:58:04 +01:00
parent bb33522e4d
commit 3bd3de036f
4 changed files with 143 additions and 109 deletions

View File

@ -1894,13 +1894,13 @@ _plugins_autocomplete(ProfWin *window, const char *const input)
if (strncmp(input, "/plugins unload ", 16) == 0) {
if (plugins_unload_ac == NULL) {
plugins_unload_ac = autocomplete_new();
GSList *plugins = plugins_loaded_list();
GSList *curr = plugins;
GList *plugins = plugins_loaded_list();
GList *curr = plugins;
while (curr) {
autocomplete_add(plugins_unload_ac, curr->data);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_slist_free(plugins);
g_list_free(plugins);
}
result = autocomplete_param_with_ac(input, "/plugins unload", plugins_unload_ac, TRUE);
if (result) {

View File

@ -6053,19 +6053,19 @@ cmd_plugins(ProfWin *window, const char *const command, gchar **args)
return TRUE;
} else {
GSList *plugins = plugins_get_list();
GSList *curr = plugins;
if (curr == NULL) {
GList *plugins = plugins_loaded_list();
if (plugins == NULL) {
cons_show("No plugins installed.");
} else {
cons_show("Installed plugins:");
while (curr) {
ProfPlugin *plugin = curr->data;
cons_show(" %s", plugin->name);
curr = g_slist_next(curr);
}
return TRUE;
}
g_slist_free(curr);
GList *curr = plugins;
cons_show("Installed plugins:");
while (curr) {
cons_show(" %s", curr->data);
curr = g_list_next(curr);
}
g_list_free(plugins);
return TRUE;
}

View File

@ -60,12 +60,12 @@
#include "ui/ui.h"
static GSList* plugins;
static GHashTable *plugins;
void
plugins_init(void)
{
plugins = NULL;
plugins = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
callbacks_init();
autocompleters_init();
@ -91,7 +91,7 @@ plugins_init(void)
if (g_str_has_suffix(filename, ".py")) {
ProfPlugin *plugin = python_plugin_create(filename);
if (plugin) {
plugins = g_slist_append(plugins, plugin);
g_hash_table_insert(plugins, strdup(filename), plugin);
loaded = TRUE;
}
}
@ -100,7 +100,7 @@ plugins_init(void)
if (g_str_has_suffix(filename, ".so")) {
ProfPlugin *plugin = c_plugin_create(filename);
if (plugin) {
plugins = g_slist_append(plugins, plugin);
g_hash_table_insert(plugins, strdup(filename), plugin);
loaded = TRUE;
}
}
@ -113,12 +113,15 @@ plugins_init(void)
}
// initialise plugins
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->init_func(plugin, PACKAGE_VERSION, PACKAGE_STATUS, NULL, NULL);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
prefs_free_plugins(plugins_pref);
@ -126,25 +129,15 @@ plugins_init(void)
return;
}
gboolean
_find_by_name(gconstpointer pluginp, gconstpointer namep)
{
char *name = (char*)namep;
ProfPlugin *plugin = (ProfPlugin*)pluginp;
return g_strcmp0(name, plugin->name);
}
gboolean
plugins_load(const char *const name)
{
GSList *found = g_slist_find_custom(plugins, name, (GCompareFunc)_find_by_name);
if (found) {
ProfPlugin *plugin = g_hash_table_lookup(plugins, name);
if (plugin) {
log_info("Failed to load plugin: %s, plugin already loaded", name);
return FALSE;
}
ProfPlugin *plugin = NULL;
#ifdef HAVE_PYTHON
if (g_str_has_suffix(name, ".py")) {
plugin = python_plugin_create(name);
@ -156,7 +149,7 @@ plugins_load(const char *const name)
}
#endif
if (plugin) {
plugins = g_slist_append(plugins, plugin);
g_hash_table_insert(plugins, strdup(name), plugin);
if (connection_get_status() == JABBER_CONNECTED) {
const char *account_name = session_get_account_name();
const char *fulljid = connection_get_fulljid();
@ -179,12 +172,6 @@ plugins_unload(const char *const name)
return FALSE;
}
GSList *
plugins_get_list(void)
{
return plugins;
}
static gchar*
_get_plugins_dir(void)
{
@ -205,7 +192,7 @@ _plugins_unloaded_list_dir(const gchar *const dir, GSList **result)
const gchar *plugin = g_dir_read_name(plugins_dir);
while (plugin) {
GSList *found = g_slist_find_custom(plugins, plugin, (GCompareFunc)_find_by_name);
ProfPlugin *found = g_hash_table_lookup(plugins, plugin);
if ((g_str_has_suffix(plugin, ".so") || g_str_has_suffix(plugin, ".py")) && !found) {
*result = g_slist_append(*result, strdup(plugin));
}
@ -225,18 +212,10 @@ plugins_unloaded_list(void)
return result;
}
GSList*
GList*
plugins_loaded_list(void)
{
GSList *result = NULL;
GSList *curr = plugins;
while (curr) {
ProfPlugin *plugin = curr->data;
result = g_slist_append(result, plugin->name);
curr = g_slist_next(curr);
}
return result;
return g_hash_table_get_keys(plugins);
}
char *
@ -261,45 +240,53 @@ plugins_win_process_line(char *win, const char * const line)
void
plugins_on_start(void)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_start_func(plugin);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
plugins_on_shutdown(void)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_shutdown_func(plugin);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
plugins_on_connect(const char * const account_name, const char * const fulljid)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_connect_func(plugin, account_name, fulljid);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
plugins_on_disconnect(const char * const account_name, const char * const fulljid)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_disconnect_func(plugin, account_name, fulljid);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
char*
@ -308,7 +295,8 @@ plugins_pre_chat_message_display(const char * const jid, const char *message)
char *new_message = NULL;
char *curr_message = strdup(message);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_message = plugin->pre_chat_message_display(plugin, jid, curr_message);
@ -317,8 +305,9 @@ plugins_pre_chat_message_display(const char * const jid, const char *message)
curr_message = strdup(new_message);
free(new_message);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return curr_message;
}
@ -326,12 +315,14 @@ plugins_pre_chat_message_display(const char * const jid, const char *message)
void
plugins_post_chat_message_display(const char * const jid, const char *message)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->post_chat_message_display(plugin, jid, message);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
char*
@ -340,7 +331,8 @@ plugins_pre_chat_message_send(const char * const jid, const char *message)
char *new_message = NULL;
char *curr_message = strdup(message);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_message = plugin->pre_chat_message_send(plugin, jid, curr_message);
@ -349,8 +341,9 @@ plugins_pre_chat_message_send(const char * const jid, const char *message)
curr_message = strdup(new_message);
free(new_message);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return curr_message;
}
@ -358,12 +351,14 @@ plugins_pre_chat_message_send(const char * const jid, const char *message)
void
plugins_post_chat_message_send(const char * const jid, const char *message)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->post_chat_message_send(plugin, jid, message);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
char*
@ -372,7 +367,8 @@ plugins_pre_room_message_display(const char * const room, const char * const nic
char *new_message = NULL;
char *curr_message = strdup(message);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_message = plugin->pre_room_message_display(plugin, room, nick, curr_message);
@ -381,8 +377,9 @@ plugins_pre_room_message_display(const char * const room, const char * const nic
curr_message = strdup(new_message);
free(new_message);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return curr_message;
}
@ -390,12 +387,14 @@ plugins_pre_room_message_display(const char * const room, const char * const nic
void
plugins_post_room_message_display(const char * const room, const char * const nick, const char *message)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->post_room_message_display(plugin, room, nick, message);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
char*
@ -404,7 +403,8 @@ plugins_pre_room_message_send(const char * const room, const char *message)
char *new_message = NULL;
char *curr_message = strdup(message);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_message = plugin->pre_room_message_send(plugin, room, curr_message);
@ -413,8 +413,9 @@ plugins_pre_room_message_send(const char * const room, const char *message)
curr_message = strdup(new_message);
free(new_message);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return curr_message;
}
@ -422,12 +423,14 @@ plugins_pre_room_message_send(const char * const room, const char *message)
void
plugins_post_room_message_send(const char * const room, const char *message)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->post_room_message_send(plugin, room, message);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
@ -441,12 +444,14 @@ plugins_on_room_history_message(const char *const room, const char *const nick,
timestamp_str = g_time_val_to_iso8601(&timestamp_tv);
}
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_room_history_message(plugin, room, nick, message, timestamp_str);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
free(timestamp_str);
}
@ -458,7 +463,8 @@ plugins_pre_priv_message_display(const char * const jid, const char *message)
char *new_message = NULL;
char *curr_message = strdup(message);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_message = plugin->pre_priv_message_display(plugin, jidp->barejid, jidp->resourcepart, curr_message);
@ -467,8 +473,9 @@ plugins_pre_priv_message_display(const char * const jid, const char *message)
curr_message = strdup(new_message);
free(new_message);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
jid_destroy(jidp);
return curr_message;
@ -479,12 +486,14 @@ plugins_post_priv_message_display(const char * const jid, const char *message)
{
Jid *jidp = jid_create(jid);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->post_priv_message_display(plugin, jidp->barejid, jidp->resourcepart, message);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
jid_destroy(jidp);
}
@ -496,7 +505,8 @@ plugins_pre_priv_message_send(const char * const jid, const char * const message
char *new_message = NULL;
char *curr_message = strdup(message);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_message = plugin->pre_priv_message_send(plugin, jidp->barejid, jidp->resourcepart, curr_message);
@ -505,8 +515,9 @@ plugins_pre_priv_message_send(const char * const jid, const char * const message
curr_message = strdup(new_message);
free(new_message);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
jid_destroy(jidp);
return curr_message;
@ -517,12 +528,14 @@ plugins_post_priv_message_send(const char * const jid, const char * const messag
{
Jid *jidp = jid_create(jid);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->post_priv_message_send(plugin, jidp->barejid, jidp->resourcepart, message);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
jid_destroy(jidp);
}
@ -533,7 +546,8 @@ plugins_on_message_stanza_send(const char *const text)
char *new_stanza = NULL;
char *curr_stanza = strdup(text);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_stanza = plugin->on_message_stanza_send(plugin, curr_stanza);
@ -542,8 +556,9 @@ plugins_on_message_stanza_send(const char *const text)
curr_stanza = strdup(new_stanza);
free(new_stanza);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return curr_stanza;
}
@ -553,15 +568,17 @@ plugins_on_message_stanza_receive(const char *const text)
{
gboolean cont = TRUE;
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
gboolean res = plugin->on_message_stanza_receive(plugin, text);
if (res == FALSE) {
cont = FALSE;
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return cont;
}
@ -572,7 +589,8 @@ plugins_on_presence_stanza_send(const char *const text)
char *new_stanza = NULL;
char *curr_stanza = strdup(text);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_stanza = plugin->on_presence_stanza_send(plugin, curr_stanza);
@ -581,8 +599,9 @@ plugins_on_presence_stanza_send(const char *const text)
curr_stanza = strdup(new_stanza);
free(new_stanza);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return curr_stanza;
}
@ -592,15 +611,17 @@ plugins_on_presence_stanza_receive(const char *const text)
{
gboolean cont = TRUE;
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
gboolean res = plugin->on_presence_stanza_receive(plugin, text);
if (res == FALSE) {
cont = FALSE;
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return cont;
}
@ -611,7 +632,8 @@ plugins_on_iq_stanza_send(const char *const text)
char *new_stanza = NULL;
char *curr_stanza = strdup(text);
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
new_stanza = plugin->on_iq_stanza_send(plugin, curr_stanza);
@ -620,8 +642,9 @@ plugins_on_iq_stanza_send(const char *const text)
curr_stanza = strdup(new_stanza);
free(new_stanza);
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return curr_stanza;
}
@ -631,15 +654,17 @@ plugins_on_iq_stanza_receive(const char *const text)
{
gboolean cont = TRUE;
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
gboolean res = plugin->on_iq_stanza_receive(plugin, text);
if (res == FALSE) {
cont = FALSE;
}
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
return cont;
}
@ -647,45 +672,53 @@ plugins_on_iq_stanza_receive(const char *const text)
void
plugins_on_contact_offline(const char *const barejid, const char *const resource, const char *const status)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_contact_offline(plugin, barejid, resource, status);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
plugins_on_contact_presence(const char *const barejid, const char *const resource, const char *const presence, const char *const status, const int priority)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_contact_presence(plugin, barejid, resource, presence, status, priority);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
plugins_on_chat_win_focus(const char *const barejid)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_chat_win_focus(plugin, barejid);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
void
plugins_on_room_win_focus(const char *const roomjid)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
ProfPlugin *plugin = curr->data;
plugin->on_room_win_focus(plugin, roomjid);
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
}
GList*
@ -697,7 +730,8 @@ plugins_get_disco_features(void)
void
plugins_shutdown(void)
{
GSList *curr = plugins;
GList *values = g_hash_table_get_values(plugins);
GList *curr = values;
while (curr) {
#ifdef HAVE_PYTHON
@ -711,8 +745,9 @@ plugins_shutdown(void)
}
#endif
curr = g_slist_next(curr);
curr = g_list_next(curr);
}
g_list_free(values);
#ifdef HAVE_PYTHON
python_shutdown();
#endif

View File

@ -98,9 +98,8 @@ typedef struct prof_plugin_t {
} ProfPlugin;
void plugins_init(void);
GSList* plugins_get_list(void);
GSList *plugins_unloaded_list(void);
GSList *plugins_loaded_list(void);
GList *plugins_loaded_list(void);
char* plugins_autocomplete(const char *const input);
void plugins_reset_autocomplete(void);
void plugins_shutdown(void);