mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
on_message_received hook may return new message
This commit is contained in:
parent
c5e39e0f54
commit
17013d1a05
@ -17,6 +17,7 @@ module RubyTest
|
||||
def self.prof_on_message_received(jid, message)
|
||||
Prof::cons_show("RubyTest: on_message_received, " + jid + ", " + message)
|
||||
Prof::cons_alert
|
||||
return message + "[RUBY]"
|
||||
end
|
||||
|
||||
def self.cmd_ruby()
|
||||
|
@ -14,6 +14,7 @@ def prof_on_connect(account_name, fulljid):
|
||||
def prof_on_message_received(jid, message):
|
||||
prof.cons_show("python-test: on_message_received, " + jid + ", " + message)
|
||||
prof.cons_alert()
|
||||
return message + "[PYTHON]"
|
||||
|
||||
def cmd_python(msg):
|
||||
if msg:
|
||||
|
@ -1,8 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
static FILE * f = NULL;
|
||||
#include <string.h>
|
||||
|
||||
void
|
||||
prof_init (const char * const version, const char * const status)
|
||||
@ -22,10 +21,14 @@ prof_on_connect (const char * const account_name, const char * const fulljid)
|
||||
fprintf (stderr, "called %s with args=<%s, %s>\n", __func__, account_name, fulljid);
|
||||
}
|
||||
|
||||
void
|
||||
prof_on_message_received (const char * const jid, const char * const message)
|
||||
char *
|
||||
prof_on_message_received (const char * const jid, const char *message)
|
||||
{
|
||||
fprintf (stderr, "called %s with args=<%s, %s>\n", __func__, jid, message);
|
||||
char *result = malloc(strlen(message) + 4);
|
||||
sprintf(result, "%s%s", message, "[C]");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
@ -95,19 +95,18 @@ c_on_connect_hook (ProfPlugin *plugin, const char * const account_name, const ch
|
||||
}
|
||||
|
||||
|
||||
// FIXME wooow, if the message is cosntant, then how could I possibly fiddle around with it?
|
||||
void
|
||||
c_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char * const message)
|
||||
char *
|
||||
c_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message)
|
||||
{
|
||||
void * f = NULL;
|
||||
void (*func)(const char * const __jid, const char * const __message);
|
||||
char* (*func)(const char * const __jid, const char * __message);
|
||||
assert (plugin && plugin->module);
|
||||
|
||||
if (NULL == (f = dlsym (plugin->module, "prof_on_message_received")))
|
||||
return;
|
||||
return NULL;
|
||||
|
||||
func = (void (*)(const char * const, const char * const)) f;
|
||||
func (jid, message);
|
||||
func = (char* (*)(const char * const, const char *)) f;
|
||||
return func (jid, message);
|
||||
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,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_message_received_hook(ProfPlugin *plugin, const char * const jid, const char * const message);
|
||||
char * c_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message);
|
||||
void c_close_library (ProfPlugin * plugin);
|
||||
|
||||
#endif
|
||||
|
@ -20,6 +20,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "config/preferences.h"
|
||||
#include "plugins/callbacks.h"
|
||||
#include "plugins/plugins.h"
|
||||
@ -109,15 +112,25 @@ plugins_on_connect(const char * const account_name, const char * const fulljid)
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
plugins_on_message_received(const char * const jid, const char * const message)
|
||||
char *
|
||||
plugins_on_message_received(const char * const jid, const char *message)
|
||||
{
|
||||
GSList *curr = plugins;
|
||||
char *new_message = NULL;
|
||||
char *curr_message = strdup(message);
|
||||
|
||||
while (curr != NULL) {
|
||||
ProfPlugin *plugin = curr->data;
|
||||
plugin->on_message_received_func(plugin, jid, message);
|
||||
new_message = plugin->on_message_received_func(plugin, jid, 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;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -38,14 +38,14 @@ 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_message_received_func)(struct prof_plugin_t* plugin,
|
||||
char* (*on_message_received_func)(struct prof_plugin_t* plugin,
|
||||
const char * const jid, const char * const message);
|
||||
} ProfPlugin;
|
||||
|
||||
void plugins_init(void);
|
||||
void plugins_on_start(void);
|
||||
void plugins_on_connect(const char * const account_name, const char * const fulljid);
|
||||
void plugins_on_message_received(const char * const jid, const char * const message);
|
||||
char * plugins_on_message_received(const char * const jid, const char *message);
|
||||
void plugins_shutdown(void);
|
||||
gboolean plugins_run_command(const char * const cmd);
|
||||
void plugins_run_timed(void);
|
||||
|
@ -122,9 +122,9 @@ python_on_connect_hook(ProfPlugin *plugin, const char * const account_name,
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
char *
|
||||
python_on_message_received_hook(ProfPlugin *plugin, const char * const jid,
|
||||
const char * const message)
|
||||
const char *message)
|
||||
{
|
||||
PyObject *p_args = Py_BuildValue("ss", jid, message);
|
||||
PyObject *p_function;
|
||||
@ -134,11 +134,20 @@ python_on_message_received_hook(ProfPlugin *plugin, const char * const jid,
|
||||
p_function = PyObject_GetAttrString(p_module, "prof_on_message_received");
|
||||
python_check_error();
|
||||
if (p_function && PyCallable_Check(p_function)) {
|
||||
PyObject_CallObject(p_function, p_args);
|
||||
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;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -30,7 +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_message_received_hook(ProfPlugin *plugin, const char * const jid, const char * const message);
|
||||
char * python_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message);
|
||||
|
||||
void python_check_error(void);
|
||||
|
||||
|
@ -94,17 +94,26 @@ ruby_on_connect_hook(ProfPlugin *plugin, const char * const account_name,
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
char *
|
||||
ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid,
|
||||
const char * const message)
|
||||
const char *message)
|
||||
{
|
||||
VALUE v_jid = rb_str_new2(jid);
|
||||
VALUE v_message = rb_str_new2(message);
|
||||
|
||||
VALUE module = (VALUE) plugin->module;
|
||||
if (_method_exists(plugin, "prof_on_message_received")) {
|
||||
rb_funcall(module, rb_intern("prof_on_message_received"), 2, v_jid, v_message);
|
||||
VALUE result = rb_funcall(module, rb_intern("prof_on_message_received"), 2, v_jid, 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;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -30,7 +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_message_received_hook(ProfPlugin *plugin, const char * const jid, const char * const message);
|
||||
char * ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char *message);
|
||||
|
||||
void ruby_check_error(void);
|
||||
|
||||
|
@ -116,11 +116,6 @@ prof_handle_typing(char *from)
|
||||
void
|
||||
prof_handle_incoming_message(char *from, char *message, gboolean priv)
|
||||
{
|
||||
ui_incoming_msg(from, message, NULL, priv);
|
||||
ui_current_page_off();
|
||||
|
||||
plugins_on_message_received(from, message);
|
||||
|
||||
if (prefs_get_boolean(PREF_CHLOG) && !priv) {
|
||||
Jid *from_jid = jid_create(from);
|
||||
const char *jid = jabber_get_fulljid();
|
||||
@ -129,17 +124,19 @@ prof_handle_incoming_message(char *from, char *message, gboolean priv)
|
||||
jid_destroy(jidp);
|
||||
jid_destroy(from_jid);
|
||||
}
|
||||
|
||||
char *new_message = plugins_on_message_received(from, message);
|
||||
|
||||
ui_incoming_msg(from, new_message, NULL, priv);
|
||||
ui_current_page_off();
|
||||
|
||||
free(new_message);
|
||||
}
|
||||
|
||||
void
|
||||
prof_handle_delayed_message(char *from, char *message, GTimeVal tv_stamp,
|
||||
gboolean priv)
|
||||
{
|
||||
ui_incoming_msg(from, message, &tv_stamp, priv);
|
||||
ui_current_page_off();
|
||||
|
||||
plugins_on_message_received(from, message);
|
||||
|
||||
if (prefs_get_boolean(PREF_CHLOG) && !priv) {
|
||||
Jid *from_jid = jid_create(from);
|
||||
const char *jid = jabber_get_fulljid();
|
||||
@ -148,6 +145,13 @@ prof_handle_delayed_message(char *from, char *message, GTimeVal tv_stamp,
|
||||
jid_destroy(jidp);
|
||||
jid_destroy(from_jid);
|
||||
}
|
||||
|
||||
char * new_message = plugins_on_message_received(from, message);
|
||||
|
||||
ui_incoming_msg(from, new_message, &tv_stamp, priv);
|
||||
ui_current_page_off();
|
||||
|
||||
free(new_message);
|
||||
}
|
||||
|
||||
void
|
||||
|
Loading…
Reference in New Issue
Block a user