From e094e3d052de8e39a7e3c3f7c46515025e9afa4c Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 3 Sep 2013 22:43:53 +0100 Subject: [PATCH] Added prof_on_disconnect hook --- src/plugins/c_plugins.c | 17 ++++++++++++++++- src/plugins/c_plugins.h | 1 + src/plugins/plugins.c | 11 +++++++++++ src/plugins/plugins.h | 3 +++ src/plugins/python_plugins.c | 20 ++++++++++++++++++++ src/plugins/python_plugins.h | 1 + src/plugins/ruby_plugins.c | 13 +++++++++++++ src/plugins/ruby_plugins.h | 1 + src/xmpp/connection.c | 7 +++++++ 9 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/plugins/c_plugins.c b/src/plugins/c_plugins.c index afde1b25..55833058 100644 --- a/src/plugins/c_plugins.c +++ b/src/plugins/c_plugins.c @@ -50,6 +50,7 @@ c_plugin_create(const char * const filename) plugin->init_func = c_init_hook; plugin->on_start_func = c_on_start_hook; plugin->on_connect_func = c_on_connect_hook; + plugin->on_disconnect_func = c_on_disconnect_hook; plugin->on_message_received_func = c_on_message_received_hook; plugin->on_message_send_func = c_on_message_send_hook; plugin->on_shutdown_func = c_on_shutdown_hook; @@ -94,7 +95,7 @@ c_on_start_hook (ProfPlugin *plugin) } void -c_on_connect_hook (ProfPlugin *plugin, const char * const account_name, const char * const fulljid) +c_on_connect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid) { void * f = NULL; void (*func)(const char * const __account_name, const char * const __fulljid); @@ -107,6 +108,20 @@ c_on_connect_hook (ProfPlugin *plugin, const char * const account_name, const ch func (account_name, fulljid); } +void +c_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid) +{ + void * f = NULL; + void (*func)(const char * const __account_name, const char * const __fulljid); + assert (plugin && plugin->module); + + if (NULL == (f = dlsym (plugin->module, "prof_on_disconnect"))) + return ; + + func = (void (*)(const char * const, const char * const)) f; + func (account_name, fulljid); +} + char * c_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message) { diff --git a/src/plugins/c_plugins.h b/src/plugins/c_plugins.h index 2506d6ea..574f90de 100644 --- a/src/plugins/c_plugins.h +++ b/src/plugins/c_plugins.h @@ -10,6 +10,7 @@ ProfPlugin* c_plugin_create(const char * const filename); void c_init_hook(ProfPlugin *plugin, const char * const version, const char * const status); void c_on_start_hook(ProfPlugin *plugin); void c_on_connect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid); +void c_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid); char * c_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message); char * c_on_message_send_hook(ProfPlugin *plugin, const char * const jid, const char *message); void c_plugin_destroy(ProfPlugin * plugin); diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 28555456..50a52313 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -135,6 +135,17 @@ plugins_on_connect(const char * const account_name, const char * const fulljid) } } +void +plugins_on_disconnect(const char * const account_name, const char * const fulljid) +{ + GSList *curr = plugins; + while (curr != NULL) { + ProfPlugin *plugin = curr->data; + plugin->on_disconnect_func(plugin, account_name, fulljid); + curr = g_slist_next(curr); + } +} + char * plugins_on_message_received(const char * const jid, const char *message) { diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index 678eea28..9b64d940 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -38,6 +38,8 @@ typedef struct prof_plugin_t { void (*on_start_func)(struct prof_plugin_t* plugin); void (*on_connect_func)(struct prof_plugin_t* plugin, const char * const account_name, const char * const fulljid); + void (*on_disconnect_func)(struct prof_plugin_t* plugin, + const char * const account_name, const char * const fulljid); char* (*on_message_received_func)(struct prof_plugin_t* plugin, const char * const jid, const char * const message); char* (*on_message_send_func)(struct prof_plugin_t* plugin, @@ -50,6 +52,7 @@ GSList * plugins_get_list(void); char * plugins_get_lang_string(ProfPlugin *plugin); void plugins_on_start(void); void plugins_on_connect(const char * const account_name, const char * const fulljid); +void plugins_on_disconnect(const char * const account_name, const char * const fulljid); char * plugins_on_message_received(const char * const jid, const char *message); char * plugins_on_message_send(const char * const jid, const char *message); void plugins_on_shutdown(void); diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index dc60b64f..4d1fa4bb 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -63,6 +63,7 @@ python_plugin_create(const char * const filename) plugin->init_func = python_init_hook; plugin->on_start_func = python_on_start_hook; plugin->on_connect_func = python_on_connect_hook; + plugin->on_disconnect_func = python_on_disconnect_hook; plugin->on_message_received_func = python_on_message_received_hook; plugin->on_message_send_func = python_on_message_send_hook; plugin->on_shutdown_func = python_on_shutdown_hook; @@ -128,6 +129,25 @@ python_on_connect_hook(ProfPlugin *plugin, const char * const account_name, } } +void +python_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, + const char * const fulljid) +{ + PyObject *p_args = Py_BuildValue("ss", account_name, fulljid); + PyObject *p_function; + + PyObject *p_module = plugin->module; + if (PyObject_HasAttrString(p_module, "prof_on_disconnect")) { + p_function = PyObject_GetAttrString(p_module, "prof_on_disconnect"); + python_check_error(); + if (p_function && PyCallable_Check(p_function)) { + PyObject_CallObject(p_function, p_args); + python_check_error(); + Py_XDECREF(p_function); + } + } +} + char * python_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message) diff --git a/src/plugins/python_plugins.h b/src/plugins/python_plugins.h index da1b84d2..89898bad 100644 --- a/src/plugins/python_plugins.h +++ b/src/plugins/python_plugins.h @@ -30,6 +30,7 @@ ProfPlugin* python_plugin_create(const char * const filename); void python_init_hook(ProfPlugin *plugin, const char * const version, const char * const status); void python_on_start_hook(ProfPlugin *plugin); void python_on_connect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid); +void python_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid); char * python_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message); char * python_on_message_send_hook(ProfPlugin *plugin, const char * const jid, const char *message); void python_on_shutdown_hook(ProfPlugin *plugin); diff --git a/src/plugins/ruby_plugins.c b/src/plugins/ruby_plugins.c index aed60887..033aa967 100644 --- a/src/plugins/ruby_plugins.c +++ b/src/plugins/ruby_plugins.c @@ -60,6 +60,7 @@ ruby_plugin_create(const char * const filename) plugin->init_func = ruby_init_hook; plugin->on_start_func = ruby_on_start_hook; plugin->on_connect_func = ruby_on_connect_hook; + plugin->on_disconnect_func = ruby_on_disconnect_hook; plugin->on_message_received_func = ruby_on_message_received_hook; plugin->on_message_send_func = ruby_on_message_send_hook; plugin->on_shutdown_func = ruby_on_shutdown_hook; @@ -100,6 +101,18 @@ ruby_on_connect_hook(ProfPlugin *plugin, const char * const account_name, } } +void +ruby_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, + const char * const fulljid) +{ + VALUE v_account_name = rb_str_new2(account_name); + VALUE v_fulljid = rb_str_new2(fulljid); + VALUE module = (VALUE) plugin->module; + if (_method_exists(plugin, "prof_on_disconnect")) { + rb_funcall(module, rb_intern("prof_on_disconnect"), 2, v_account_name, v_fulljid); + } +} + char * ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message) diff --git a/src/plugins/ruby_plugins.h b/src/plugins/ruby_plugins.h index ec49c41c..e562fc49 100644 --- a/src/plugins/ruby_plugins.h +++ b/src/plugins/ruby_plugins.h @@ -30,6 +30,7 @@ ProfPlugin* ruby_plugin_create(const char * const filename); void ruby_init_hook(ProfPlugin *plugin, const char * const version, const char * const status); void ruby_on_start_hook(ProfPlugin *plugin); void ruby_on_connect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid); +void ruby_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid); char * ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message); char * ruby_on_message_send_hook(ProfPlugin *plugin, const char * const jid, const char *message); void ruby_on_shutdown_hook(ProfPlugin *plugin); diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index e29ca86e..61610aa6 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -32,6 +32,7 @@ #include "jid.h" #include "log.h" #include "muc.h" +#include "plugins/plugins.h" #include "profanity.h" #include "xmpp/bookmark.h" #include "xmpp/capabilities.h" @@ -166,6 +167,9 @@ jabber_disconnect(void) { // if connected, send end stream and wait for response if (jabber_conn.conn_status == JABBER_CONNECTED) { + char *account_name = jabber_get_account_name(); + const char *fulljid = jabber_get_fulljid(); + plugins_on_disconnect(account_name, fulljid); log_info("Closing connection"); jabber_conn.conn_status = JABBER_DISCONNECTING; xmpp_disconnect(jabber_conn.conn); @@ -511,6 +515,9 @@ _connection_handler(xmpp_conn_t * const conn, // lost connection for unkown reason if (jabber_conn.conn_status == JABBER_CONNECTED) { log_debug("Connection handler: Lost connection for unknown reason"); + char *account_name = jabber_get_account_name(); + const char *fulljid = jabber_get_fulljid(); + plugins_on_disconnect(account_name, fulljid); prof_handle_lost_connection(); if (prefs_get_reconnect() != 0) { assert(reconnect_timer == NULL);