Added stanza send hooks for plugins

This commit is contained in:
James Booth
2016-03-26 15:50:16 +00:00
parent ce9b0836a0
commit d0397f3da5
11 changed files with 396 additions and 58 deletions

View File

@@ -97,6 +97,9 @@ c_plugin_create(const char * const filename)
plugin->post_priv_message_display = c_post_priv_message_display_hook;
plugin->pre_priv_message_send = c_pre_priv_message_send_hook;
plugin->post_priv_message_send = c_post_priv_message_send_hook;
plugin->on_message_stanza_send = c_on_message_stanza_send_hook;
plugin->on_presence_stanza_send = c_on_presence_stanza_send_hook;
plugin->on_iq_stanza_send = c_on_iq_stanza_send_hook;
g_string_free(path, TRUE);
g_free(module_name);
@@ -347,6 +350,48 @@ c_post_priv_message_send_hook(ProfPlugin *plugin, const char * const room, const
func (room, nick, message);
}
char *
c_on_message_stanza_send_hook(ProfPlugin *plugin, const char * const text)
{
void * f = NULL;
char* (*func)(const char * const __text);
assert (plugin && plugin->module);
if (NULL == (f = dlsym (plugin->module, "prof_on_message_stanza_send")))
return NULL;
func = (char* (*)(const char * const)) f;
return func (text);
}
char *
c_on_presence_stanza_send_hook(ProfPlugin *plugin, const char * const text)
{
void * f = NULL;
char* (*func)(const char * const __text);
assert (plugin && plugin->module);
if (NULL == (f = dlsym (plugin->module, "prof_on_presence_stanza_send")))
return NULL;
func = (char* (*)(const char * const)) f;
return func (text);
}
char *
c_on_iq_stanza_send_hook(ProfPlugin *plugin, const char * const text)
{
void * f = NULL;
char* (*func)(const char * const __text);
assert (plugin && plugin->module);
if (NULL == (f = dlsym (plugin->module, "prof_on_iq_stanza_send")))
return NULL;
func = (char* (*)(const char * const)) f;
return func (text);
}
void
c_plugin_destroy(ProfPlugin *plugin)
{

View File

@@ -64,4 +64,8 @@ void c_post_priv_message_display_hook(ProfPlugin *plugin, const char * const ro
char* c_pre_priv_message_send_hook(ProfPlugin *plugin, const char * const room, const char * const nick, const char * const message);
void c_post_priv_message_send_hook(ProfPlugin *plugin, const char * const room, const char * const nick, const char * const message);
char* c_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text);
char* c_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text);
char* c_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text);
#endif

View File

@@ -409,6 +409,69 @@ plugins_post_priv_message_send(const char * const jid, const char * const messag
jid_destroy(jidp);
}
char*
plugins_on_message_stanza_send(const char *const text)
{
char *new_stanza = NULL;
char *curr_stanza = strdup(text);
GSList *curr = plugins;
while (curr) {
ProfPlugin *plugin = curr->data;
new_stanza = plugin->on_message_stanza_send(plugin, curr_stanza);
if (new_stanza) {
free(curr_stanza);
curr_stanza = strdup(new_stanza);
free(new_stanza);
}
curr = g_slist_next(curr);
}
return curr_stanza;
}
char*
plugins_on_presence_stanza_send(const char *const text)
{
char *new_stanza = NULL;
char *curr_stanza = strdup(text);
GSList *curr = plugins;
while (curr) {
ProfPlugin *plugin = curr->data;
new_stanza = plugin->on_presence_stanza_send(plugin, curr_stanza);
if (new_stanza) {
free(curr_stanza);
curr_stanza = strdup(new_stanza);
free(new_stanza);
}
curr = g_slist_next(curr);
}
return curr_stanza;
}
char*
plugins_on_iq_stanza_send(const char *const text)
{
char *new_stanza = NULL;
char *curr_stanza = strdup(text);
GSList *curr = plugins;
while (curr) {
ProfPlugin *plugin = curr->data;
new_stanza = plugin->on_iq_stanza_send(plugin, curr_stanza);
if (new_stanza) {
free(curr_stanza);
curr_stanza = strdup(new_stanza);
free(new_stanza);
}
curr = g_slist_next(curr);
}
return curr_stanza;
}
void
plugins_shutdown(void)
{

View File

@@ -70,6 +70,9 @@ typedef struct prof_plugin_t {
char* (*pre_priv_message_send)(struct prof_plugin_t* plugin, const char * const room, const char * const nick, const char * const message);
void (*post_priv_message_send)(struct prof_plugin_t* plugin, const char * const room, const char * const nick, const char * const message);
char* (*on_message_stanza_send)(struct prof_plugin_t* plugin, const char *const text);
char* (*on_presence_stanza_send)(struct prof_plugin_t* plugin, const char *const text);
char* (*on_iq_stanza_send)(struct prof_plugin_t* plugin, const char *const text);
} ProfPlugin;
void plugins_init(void);
@@ -100,12 +103,16 @@ void plugins_post_priv_message_display(const char * const jid, const char *mess
char* plugins_pre_priv_message_send(const char * const jid, const char * const message);
void plugins_post_priv_message_send(const char * const jid, const char * const message);
void plugins_win_process_line(char *win, const char * const line);
char* plugins_on_message_stanza_send(const char *const text);
char* plugins_on_presence_stanza_send(const char *const text);
char* plugins_on_iq_stanza_send(const char *const text);
gboolean plugins_run_command(const char * const cmd);
void plugins_run_timed(void);
GList* plugins_get_command_names(void);
gchar * plugins_get_dir(void);
CommandHelp* plugins_get_help(const char *const cmd);
void plugins_win_process_line(char *win, const char * const line);
#endif

View File

@@ -115,6 +115,9 @@ python_plugin_create(const char * const filename)
plugin->post_priv_message_display = python_post_priv_message_display_hook;
plugin->pre_priv_message_send = python_pre_priv_message_send_hook;
plugin->post_priv_message_send = python_post_priv_message_send_hook;
plugin->on_message_stanza_send = python_on_message_stanza_send_hook;
plugin->on_presence_stanza_send = python_on_presence_stanza_send_hook;
plugin->on_iq_stanza_send = python_on_iq_stanza_send_hook;
g_free(module_name);
allow_python_threads();
@@ -568,6 +571,114 @@ python_post_priv_message_send_hook(ProfPlugin *plugin, const char * const room,
allow_python_threads();
}
char*
python_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text)
{
disable_python_threads();
PyObject *p_args = Py_BuildValue("(s)", text);
PyObject *p_function;
PyObject *p_module = plugin->module;
if (PyObject_HasAttrString(p_module, "prof_on_message_stanza_send")) {
p_function = PyObject_GetAttrString(p_module, "prof_on_message_stanza_send");
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 (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result);
allow_python_threads();
return result_str;
} else if (result != Py_None) {
char *result_str = strdup(PyString_AsString(result));
Py_XDECREF(result);
allow_python_threads();
return result_str;
} else {
allow_python_threads();
return NULL;
}
}
}
allow_python_threads();
return NULL;
}
char*
python_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text)
{
disable_python_threads();
PyObject *p_args = Py_BuildValue("(s)", text);
PyObject *p_function;
PyObject *p_module = plugin->module;
if (PyObject_HasAttrString(p_module, "prof_on_presence_stanza_send")) {
p_function = PyObject_GetAttrString(p_module, "prof_on_presence_stanza_send");
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 (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result);
allow_python_threads();
return result_str;
} else if (result != Py_None) {
char *result_str = strdup(PyString_AsString(result));
Py_XDECREF(result);
allow_python_threads();
return result_str;
} else {
allow_python_threads();
return NULL;
}
}
}
allow_python_threads();
return NULL;
}
char*
python_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text)
{
disable_python_threads();
PyObject *p_args = Py_BuildValue("(s)", text);
PyObject *p_function;
PyObject *p_module = plugin->module;
if (PyObject_HasAttrString(p_module, "prof_on_iq_stanza_send")) {
p_function = PyObject_GetAttrString(p_module, "prof_on_iq_stanza_send");
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 (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result);
allow_python_threads();
return result_str;
} else if (result != Py_None) {
char *result_str = strdup(PyString_AsString(result));
Py_XDECREF(result);
allow_python_threads();
return result_str;
} else {
allow_python_threads();
return NULL;
}
}
}
allow_python_threads();
return NULL;
}
void
python_check_error(void)
{

View File

@@ -64,5 +64,8 @@ void python_post_priv_message_display_hook(ProfPlugin *plugin, const char * con
char* python_pre_priv_message_send_hook(ProfPlugin *plugin, const char * const room, const char * const nick, const char * const message);
void python_post_priv_message_send_hook(ProfPlugin *plugin, const char * const room, const char * const nick, const char * const message);
char* python_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text);
char* python_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text);
char* python_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text);
#endif