1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Added room message and private message received hooks

This commit is contained in:
James Booth 2013-09-05 23:26:52 +01:00
parent 017517e70e
commit a12085a3cc
9 changed files with 233 additions and 5 deletions

View File

@ -52,6 +52,8 @@ c_plugin_create(const char * const filename)
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_room_message_received_func = c_on_room_message_received_hook;
plugin->on_private_message_received_func = c_on_private_message_received_hook;
plugin->on_message_send_func = c_on_message_send_hook;
plugin->on_shutdown_func = c_on_shutdown_hook;
@ -134,7 +136,36 @@ c_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const cha
func = (char* (*)(const char * const, const char *)) f;
return func (jid, message);
}
char *
c_on_private_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message)
{
void * f = NULL;
char* (*func)(const char * const __room, const char * const __nick, const char * __message);
assert (plugin && plugin->module);
if (NULL == (f = dlsym (plugin->module, "prof_on_private_message_received")))
return NULL;
func = (char* (*)(const char * const, const char * const, const char *)) f;
return func (room, nick, message);
}
char *
c_on_room_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message)
{
void * f = NULL;
char* (*func)(const char * const __room, const char * const __nick, const char * __message);
assert (plugin && plugin->module);
if (NULL == (f = dlsym (plugin->module, "prof_on_room_message_received")))
return NULL;
func = (char* (*)(const char * const, const char * const, const char *)) f;
return func (room, nick, message);
}
char *

View File

@ -12,6 +12,10 @@ 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_private_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message);
char * c_on_room_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message);
char * c_on_message_send_hook(ProfPlugin *plugin, const char * const jid, const char *message);
void c_plugin_destroy(ProfPlugin * plugin);
void c_on_shutdown_hook(ProfPlugin *plugin);

View File

@ -167,6 +167,50 @@ plugins_on_message_received(const char * const jid, const char *message)
return curr_message;
}
char *
plugins_on_private_message_received(const char * const room, const char * const nick,
const char *message)
{
GSList *curr = plugins;
char *new_message = NULL;
char *curr_message = strdup(message);
while (curr != NULL) {
ProfPlugin *plugin = curr->data;
new_message = plugin->on_private_message_received_func(plugin, room, nick, 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_room_message_received(const char * const room, const char * const nick,
const char *message)
{
GSList *curr = plugins;
char *new_message = NULL;
char *curr_message = strdup(message);
while (curr != NULL) {
ProfPlugin *plugin = curr->data;
new_message = plugin->on_room_message_received_func(plugin, room, nick, 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_send(const char * const jid, const char *message)
{

View File

@ -42,6 +42,12 @@ typedef struct prof_plugin_t {
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_private_message_received_func)(struct prof_plugin_t* plugin,
const char * const room, const char * const nick,
const char * const message);
char* (*on_room_message_received_func)(struct prof_plugin_t* plugin,
const char * const room, const char * const nick,
const char * const message);
char* (*on_message_send_func)(struct prof_plugin_t* plugin,
const char * const jid, const char * const message);
void (*on_shutdown_func)(struct prof_plugin_t* plugin);
@ -54,6 +60,8 @@ 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_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);
char * plugins_on_message_send(const char * const jid, const char *message);
void plugins_on_shutdown(void);
void plugins_shutdown(void);

View File

@ -65,6 +65,8 @@ python_plugin_create(const char * const filename)
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_room_message_received_func = python_on_room_message_received_hook;
plugin->on_private_message_received_func = python_on_private_message_received_hook;
plugin->on_message_send_func = python_on_message_send_hook;
plugin->on_shutdown_func = python_on_shutdown_hook;
g_free(module_name);
@ -176,6 +178,62 @@ python_on_message_received_hook(ProfPlugin *plugin, const char * const jid,
return NULL;
}
char *
python_on_private_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message)
{
PyObject *p_args = Py_BuildValue("sss", room, nick, message);
PyObject *p_function;
PyObject *p_module = plugin->module;
if (PyObject_HasAttrString(p_module, "prof_on_private_message_received")) {
p_function = PyObject_GetAttrString(p_module, "prof_on_private_message_received");
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 = strdup(PyString_AsString(result));
Py_XDECREF(result);
return result_str;;
} else {
return NULL;
}
}
}
return NULL;
}
char *
python_on_room_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message)
{
PyObject *p_args = Py_BuildValue("sss", room, nick, message);
PyObject *p_function;
PyObject *p_module = plugin->module;
if (PyObject_HasAttrString(p_module, "prof_on_room_message_received")) {
p_function = PyObject_GetAttrString(p_module, "prof_on_room_message_received");
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 = strdup(PyString_AsString(result));
Py_XDECREF(result);
return result_str;;
} else {
return NULL;
}
}
}
return NULL;
}
char *
python_on_message_send_hook(ProfPlugin *plugin, const char * const jid,
const char *message)

View File

@ -32,6 +32,10 @@ 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_private_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message);
char * python_on_room_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, 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);

View File

@ -62,6 +62,8 @@ ruby_plugin_create(const char * const filename)
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_room_message_received_func = ruby_on_room_message_received_hook;
plugin->on_private_message_received_func = ruby_on_private_message_received_hook;
plugin->on_message_send_func = ruby_on_message_send_hook;
plugin->on_shutdown_func = ruby_on_shutdown_hook;
g_free(module_name);
@ -135,6 +137,52 @@ ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid,
return NULL;
}
char *
ruby_on_private_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message)
{
VALUE v_room = rb_str_new2(room);
VALUE v_nick = rb_str_new2(nick);
VALUE v_message = rb_str_new2(message);
VALUE module = (VALUE) plugin->module;
if (_method_exists(plugin, "prof_on_private_message_received")) {
VALUE result = rb_funcall(module, rb_intern("prof_on_private_message_received"), 3, v_room, v_nick, 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_room_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message)
{
VALUE v_room = rb_str_new2(room);
VALUE v_nick = rb_str_new2(nick);
VALUE v_message = rb_str_new2(message);
VALUE module = (VALUE) plugin->module;
if (_method_exists(plugin, "prof_on_room_message_received")) {
VALUE result = rb_funcall(module, rb_intern("prof_on_room_message_received"), 3, v_room, v_nick, 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_send_hook(ProfPlugin *plugin, const char * const jid,
const char *message)

View File

@ -32,6 +32,10 @@ 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_private_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, const char *message);
char * ruby_on_room_message_received_hook(ProfPlugin *plugin, const char * const room,
const char * const nick, 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);

View File

@ -116,7 +116,17 @@ prof_handle_typing(char *from)
void
prof_handle_incoming_message(char *from, char *message, gboolean priv)
{
char *new_message = plugins_on_message_received(from, message);
char *new_message = NULL;
if (priv) {
Jid *jid = jid_create(from);
char *room = jid->barejid;
char *nick = jid->resourcepart;
new_message = plugins_on_private_message_received(room, nick, message);
jid_destroy(jid);
} else {
new_message = plugins_on_message_received(from, message);
}
ui_incoming_msg(from, new_message, NULL, priv);
ui_current_page_off();
@ -130,7 +140,6 @@ prof_handle_incoming_message(char *from, char *message, gboolean priv)
jid_destroy(from_jid);
}
free(new_message);
}
@ -138,7 +147,17 @@ void
prof_handle_delayed_message(char *from, char *message, GTimeVal tv_stamp,
gboolean priv)
{
char * new_message = plugins_on_message_received(from, message);
char *new_message = NULL;
if (priv) {
Jid *jid = jid_create(from);
char *room = jid->barejid;
char *nick = jid->resourcepart;
new_message = plugins_on_private_message_received(room, nick, message);
jid_destroy(jid);
} else {
new_message = plugins_on_message_received(from, message);
}
ui_incoming_msg(from, new_message, &tv_stamp, priv);
ui_current_page_off();
@ -152,7 +171,6 @@ prof_handle_delayed_message(char *from, char *message, GTimeVal tv_stamp,
jid_destroy(from_jid);
}
free(new_message);
}
@ -326,7 +344,14 @@ void
prof_handle_room_message(const char * const room_jid, const char * const nick,
const char * const message)
{
ui_room_message(room_jid, nick, message);
char *new_message = NULL;
if (g_strcmp0(nick, muc_get_room_nick(room_jid)) != 0) {
new_message = plugins_on_room_message_received(room_jid, nick, message);
} else {
new_message = strdup(message);
}
ui_room_message(room_jid, nick, new_message);
ui_current_page_off();
if (prefs_get_boolean(PREF_GRLOG)) {
@ -334,6 +359,8 @@ prof_handle_room_message(const char * const room_jid, const char * const nick,
groupchat_log_chat(jid->barejid, room_jid, nick, message);
jid_destroy(jid);
}
free(new_message);
}
void