1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Added before_message_displayed hook

This commit is contained in:
James Booth 2013-09-19 22:10:11 +01:00
parent d38e2e73c2
commit 18a8be8935
10 changed files with 115 additions and 4 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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);

View File

@ -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)
{

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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);
}