From 18a8be89358291c0c1b837061416da9f3994c6f6 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 19 Sep 2013 22:10:11 +0100 Subject: [PATCH] Added before_message_displayed hook --- configure.ac | 3 ++- src/plugins/lua_plugins.c | 28 ++++++++++++++++++++++++++++ src/plugins/lua_plugins.h | 1 + src/plugins/plugins.c | 21 +++++++++++++++++++++ src/plugins/plugins.h | 3 +++ src/plugins/python_plugins.c | 29 +++++++++++++++++++++++++++++ src/plugins/python_plugins.h | 1 + src/plugins/ruby_plugins.c | 21 +++++++++++++++++++++ src/plugins/ruby_plugins.h | 1 + src/ui/core.c | 11 ++++++++--- 10 files changed, 115 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index a88ae822..da6e384d 100644 --- a/configure.ac +++ b/configure.ac @@ -142,7 +142,8 @@ fi AM_CFLAGS="-Wall -export-dynamic" if test "x$PACKAGE_STATUS" = xdevelopment; then - AM_CFLAGS="$AM_CFLAGS -Wunused -Werror" +# AM_CFLAGS="$AM_CFLAGS -Wunused -Werror" + AM_CFLAGS="$AM_CFLAGS -Wunused" fi LIBS="$LIBS $DEPS_LIBS $NOTIFY_LIBS" diff --git a/src/plugins/lua_plugins.c b/src/plugins/lua_plugins.c index a742ac52..0914929b 100644 --- a/src/plugins/lua_plugins.c +++ b/src/plugins/lua_plugins.c @@ -79,6 +79,7 @@ lua_plugin_create(const char * const filename) plugin->on_start_func = lua_on_start_hook; plugin->on_connect_func = lua_on_connect_hook; plugin->on_disconnect_func = lua_on_disconnect_hook; + plugin->before_message_displayed_func = lua_before_message_displayed_hook; plugin->on_message_received_func = lua_on_message_received_hook; plugin->on_room_message_received_func = lua_on_room_message_received_hook; plugin->on_private_message_received_func = lua_on_private_message_received_hook; @@ -170,6 +171,33 @@ lua_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, } } +char * +lua_before_message_displayed_hook(ProfPlugin *plugin, const char *message) +{ + int *p_ref = (int *)plugin->module; + lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref); + lua_pushstring(L, "prof_before_message_displayed"); + lua_gettable(L, -2); + if (!lua_isnil(L, -1)) { + lua_pushstring(L, message); + int res2 = lua_pcall(L, 1, 1, 0); + lua_check_error(res2); + + char *result = NULL; + if (lua_isstring(L, -1)) { + result = strdup(lua_tostring(L, -1)); + } + l_stackdump(L); + //lua_pop(L, 2); + + return result; + } else { + l_stackdump(L); + //lua_pop(L, 2); + return NULL; + } +} + char * lua_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message) diff --git a/src/plugins/lua_plugins.h b/src/plugins/lua_plugins.h index efee50c0..27342866 100644 --- a/src/plugins/lua_plugins.h +++ b/src/plugins/lua_plugins.h @@ -31,6 +31,7 @@ void lua_init_hook(ProfPlugin *plugin, const char * const version, const char * void lua_on_start_hook(ProfPlugin *plugin); void lua_on_connect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid); void lua_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, const char * const fulljid); +char * lua_before_message_displayed_hook(ProfPlugin *plugin, const char *message); char * lua_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message); char * lua_on_private_message_received_hook(ProfPlugin *plugin, const char * const room, const char * const nick, const char *message); diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 93b4912c..23d1438c 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -189,6 +189,27 @@ plugins_on_disconnect(const char * const account_name, const char * const fullji } } +char * +plugins_before_message_displayed(const char * const message) +{ + GSList *curr = plugins; + char *new_message = NULL; + char *curr_message = strdup(message); + + while (curr != NULL) { + ProfPlugin *plugin = curr->data; + new_message = plugin->before_message_displayed_func(plugin, curr_message); + if (new_message != NULL) { + free(curr_message); + curr_message = strdup(new_message); + free(new_message); + } + curr = g_slist_next(curr); + } + + return curr_message; +} + 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 a1526e5c..39e3af41 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -41,6 +41,8 @@ typedef struct prof_plugin_t { 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* (*before_message_displayed_func)(struct prof_plugin_t* plugin, + const char * const message); char* (*on_message_received_func)(struct prof_plugin_t* plugin, const char * const jid, const char * const message); char* (*on_private_message_received_func)(struct prof_plugin_t* plugin, @@ -65,6 +67,7 @@ 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_before_message_displayed(const char * const message); char * plugins_on_message_received(const char * const jid, const char *message); char * plugins_on_room_message_received(const char * const room, const char * const nick, const char *message); char * plugins_on_private_message_received(const char * const room, const char * const nick, const char *message); diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index ddfa7280..79b67e2c 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->on_start_func = python_on_start_hook; plugin->on_connect_func = python_on_connect_hook; plugin->on_disconnect_func = python_on_disconnect_hook; + plugin->before_message_displayed_func = python_before_message_displayed_hook; plugin->on_message_received_func = python_on_message_received_hook; plugin->on_room_message_received_func = python_on_room_message_received_hook; plugin->on_private_message_received_func = python_on_private_message_received_hook; @@ -151,6 +152,34 @@ python_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, } } +char * +python_before_message_displayed_hook(ProfPlugin *plugin, const char *message) +{ + PyObject *p_args = Py_BuildValue("(s)", message); + PyObject *p_function; + + PyObject *p_module = plugin->module; + if (PyObject_HasAttrString(p_module, "prof_before_message_displayed")) { + p_function = PyObject_GetAttrString(p_module, "prof_before_message_displayed"); + python_check_error(); + if (p_function && PyCallable_Check(p_function)) { + PyObject *result = PyObject_CallObject(p_function, p_args); + python_check_error(); + Py_XDECREF(p_function); + if (result != Py_None) { + char *result_str = NULL; + PyArg_Parse(result, "(s)", result_str); + Py_XDECREF(result); + return result_str;; + } else { + return NULL; + } + } + } + + return NULL; +} + 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 b4f4cc22..f1894105 100644 --- a/src/plugins/python_plugins.h +++ b/src/plugins/python_plugins.h @@ -31,6 +31,7 @@ void python_init_hook(ProfPlugin *plugin, const char * const version, const char 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_before_message_displayed_hook(ProfPlugin *plugin, const char *message); char * python_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message); char * python_on_private_message_received_hook(ProfPlugin *plugin, const char * const room, const char * const nick, const char *message); diff --git a/src/plugins/ruby_plugins.c b/src/plugins/ruby_plugins.c index b4f06347..98bf8114 100644 --- a/src/plugins/ruby_plugins.c +++ b/src/plugins/ruby_plugins.c @@ -61,6 +61,7 @@ ruby_plugin_create(const char * const filename) 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->before_message_displayed_func = ruby_before_message_displayed_hook; plugin->on_message_received_func = ruby_on_message_received_hook; plugin->on_room_message_received_func = ruby_on_room_message_received_hook; plugin->on_private_message_received_func = ruby_on_private_message_received_hook; @@ -117,6 +118,26 @@ ruby_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name, } } +char * +ruby_before_message_displayed_hook(ProfPlugin *plugin, const char *message) +{ + VALUE v_message = rb_str_new2(message); + + VALUE module = (VALUE) plugin->module; + if (_method_exists(plugin, "prof_before_message_displayed")) { + VALUE result = rb_funcall(module, rb_intern("prof_before_message_displayed"), 1, v_message); + if (TYPE(result) != T_NIL) { + char *result_str = STR2CSTR(result); + rb_gc_unregister_address(&result); + return result_str; + } else { + return NULL; + } + } + + return NULL; +} + 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 05ec461d..591f87d5 100644 --- a/src/plugins/ruby_plugins.h +++ b/src/plugins/ruby_plugins.h @@ -31,6 +31,7 @@ void ruby_init_hook(ProfPlugin *plugin, const char * const version, const char * 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_before_message_displayed_hook(ProfPlugin *plugin, const char *message); char * ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message); char * ruby_on_private_message_received_hook(ProfPlugin *plugin, const char * const room, const char * const nick, const char *message); diff --git a/src/ui/core.c b/src/ui/core.c index 2c8591cb..53f4776b 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -52,6 +52,7 @@ #include "ui/window.h" #include "ui/windows.h" #include "xmpp/xmpp.h" +#include "plugins/plugins.h" static char *win_title; @@ -248,6 +249,8 @@ void ui_incoming_msg(const char * const from, const char * const message, GTimeVal *tv_stamp, gboolean priv) { + char *new_message = plugins_before_message_displayed(message); + gboolean win_created = FALSE; char *display_from = NULL; win_type_t win_type; @@ -299,7 +302,7 @@ ui_incoming_msg(const char * const from, const char * const message, wattroff(window->win, COLOUR_THEM); } else { _win_show_user(window->win, display_from, 1); - _win_show_message(window->win, message); + _win_show_message(window->win, new_message); } title_bar_set_typing(FALSE); title_bar_draw(); @@ -345,7 +348,7 @@ ui_incoming_msg(const char * const from, const char * const message, wattroff(window->win, COLOUR_THEM); } else { _win_show_user(window->win, display_from, 1); - _win_show_message(window->win, message); + _win_show_message(window->win, new_message); } } @@ -969,6 +972,8 @@ void ui_outgoing_msg(const char * const from, const char * const to, const char * const message) { + char *new_message = plugins_before_message_displayed(message); + PContact contact = roster_get_contact(to); ProfWin *window = wins_get_by_recipient(to); int num = 0; @@ -1012,7 +1017,7 @@ ui_outgoing_msg(const char * const from, const char * const to, wattroff(window->win, COLOUR_ME); } else { _win_show_user(window->win, from, 0); - _win_show_message(window->win, message); + _win_show_message(window->win, new_message); } ui_switch_win(num); }