From 21b823750e4ada37d79401cd19e9d22275d48369 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 18 Aug 2013 23:54:56 +0100 Subject: [PATCH] Added ruby callbacks --- plugins/RubyTest.rb | 20 +++++++++++++++++++ src/plugins/ruby_api.c | 41 ++++++++++++++------------------------ src/plugins/ruby_plugins.c | 13 ------------ 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/plugins/RubyTest.rb b/plugins/RubyTest.rb index d41b94d8..ac320f41 100644 --- a/plugins/RubyTest.rb +++ b/plugins/RubyTest.rb @@ -1,6 +1,8 @@ module RubyTest def RubyTest.prof_init(version, status) Prof::cons_show("RubyTest: init, " + version + ", " + status) + Prof::register_command("/ruby", 0, 1, "/ruby", "RubyTest", "RubyTest", cmd_ruby) + Prof::register_timed(timer_test, 5) end def RubyTest.prof_on_start() @@ -14,4 +16,22 @@ module RubyTest def RubyTest.prof_on_message_received(jid, message) Prof::cons_show("RubyTest: on_message_received, " + jid + ", " + message) end + + def RubyTest.cmd_ruby() + return Proc.new { + | msg | + + if msg + Prof::cons_show("Ruby command called: " + msg) + else + Prof::cons_show("Ruby command called with no arg") + end + } + end + + def RubyTest.timer_test() + return Proc.new { + Prof::cons_show("Ruby timer fired.") + } + end end diff --git a/src/plugins/ruby_api.c b/src/plugins/ruby_api.c index 26005fcc..e9d17996 100644 --- a/src/plugins/ruby_api.c +++ b/src/plugins/ruby_api.c @@ -111,48 +111,37 @@ ruby_api_get_current_recipient(VALUE self) void ruby_command_callback(PluginCommand *command, gchar **args) { - PyObject *p_args = NULL; int num_args = g_strv_length(args); if (num_args == 0) { if (command->max_args == 1) { - p_args = Py_BuildValue("(O)", Py_BuildValue("")); - PyObject_CallObject(command->callback, p_args); - Py_XDECREF(p_args); + rb_funcall((VALUE)command->callback, rb_intern("call"), 1, Qnil); } else { - PyObject_CallObject(command->callback, p_args); + rb_funcall((VALUE)command->callback, rb_intern("call"), 0); } } else if (num_args == 1) { - p_args = Py_BuildValue("(s)", args[0]); - PyObject_CallObject(command->callback, p_args); - Py_XDECREF(p_args); + rb_funcall((VALUE)command->callback, rb_intern("call"), 1, + rb_str_new2(args[0])); } else if (num_args == 2) { - p_args = Py_BuildValue("ss", args[0], args[1]); - PyObject_CallObject(command->callback, p_args); - Py_XDECREF(p_args); + rb_funcall((VALUE)command->callback, rb_intern("call"), 1, + rb_str_new2(args[0]), rb_str_new2(args[1])); } else if (num_args == 3) { - p_args = Py_BuildValue("sss", args[0], args[1], args[2]); - PyObject_CallObject(command->callback, p_args); - Py_XDECREF(p_args); + rb_funcall((VALUE)command->callback, rb_intern("call"), 1, + rb_str_new2(args[0]), rb_str_new2(args[1]), rb_str_new2(args[2])); } else if (num_args == 4) { - p_args = Py_BuildValue("ssss", args[0], args[1], args[2], args[3]); - PyObject_CallObject(command->callback, p_args); - Py_XDECREF(p_args); + rb_funcall((VALUE)command->callback, rb_intern("call"), 1, + rb_str_new2(args[0]), rb_str_new2(args[1]), rb_str_new2(args[2]), + rb_str_new2(args[3])); } else if (num_args == 5) { - p_args = Py_BuildValue("sssss", args[0], args[1], args[2], args[3], args[4]); - PyObject_CallObject(command->callback, p_args); - Py_XDECREF(p_args); - } - - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); + rb_funcall((VALUE)command->callback, rb_intern("call"), 1, + rb_str_new2(args[0]), rb_str_new2(args[1]), rb_str_new2(args[2]), + rb_str_new2(args[3]), rb_str_new2(args[4])); } } void ruby_timed_callback(PluginTimedFunction *timed_function) { - PyObject_CallObject(timed_function->callback, NULL); + rb_funcall((VALUE)timed_function->callback, rb_intern("call"), 0); } static VALUE prof_module; diff --git a/src/plugins/ruby_plugins.c b/src/plugins/ruby_plugins.c index d591e3fc..c9fdf0d2 100644 --- a/src/plugins/ruby_plugins.c +++ b/src/plugins/ruby_plugins.c @@ -20,7 +20,6 @@ * */ -#include #include #include "config/preferences.h" @@ -38,12 +37,6 @@ ruby_env_init(void) ruby_init_loadpath(); ruby_api_init(); ruby_check_error(); - // TODO set loadpath for ruby interpreter - //GString *path = g_string_new(Py_GetPath()); - //g_string_append(path, ":./plugins/"); - //PySys_SetPath(path->str); - //ruby_check_error(); - //g_string_free(path, TRUE); } ProfPlugin * @@ -103,12 +96,6 @@ ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const void ruby_check_error(void) { -/* TODO - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - } -*/ } void