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

Basic ruby api functions

This commit is contained in:
James Booth 2013-08-18 17:34:32 +01:00
parent fd3419b8af
commit d5818f49db
2 changed files with 62 additions and 62 deletions

View File

@ -29,100 +29,82 @@
#include "plugins/ruby_api.h" #include "plugins/ruby_api.h"
#include "plugins/callbacks.h" #include "plugins/callbacks.h"
static PyObject* static VALUE
ruby_api_cons_alert(PyObject *self, PyObject *args) ruby_api_cons_alert(VALUE self)
{ {
api_cons_alert(); api_cons_alert();
return Py_BuildValue(""); return Qnil;
} }
static VALUE static VALUE
ruby_api_cons_show(VALUE self, const char * const message) ruby_api_cons_show(VALUE self, VALUE v_message)
{ {
char *message = STR2CSTR(v_message);
if (message != NULL) { if (message != NULL) {
api_cons_show(message); api_cons_show(message);
} }
return self; return self;
} }
static PyObject* static VALUE
ruby_api_register_command(PyObject *self, PyObject *args) ruby_api_register_command(VALUE self, VALUE v_command_name, VALUE v_min_args,
VALUE v_max_args, VALUE v_usage, VALUE v_short_help, VALUE v_long_help,
VALUE v_callback)
{ {
const char *command_name = NULL; const char *command_name = STR2CSTR(v_command_name);
int min_args = 0; int min_args = NUM2INT(v_min_args);
int max_args = 0; int max_args = NUM2INT(v_max_args);
const char *usage = NULL; const char *usage = STR2CSTR(v_usage);
const char *short_help = NULL; const char *short_help = STR2CSTR(v_short_help);
const char *long_help = NULL; const char *long_help = STR2CSTR(v_long_help);
PyObject *p_callback = NULL;
if (!PyArg_ParseTuple(args, "siisssO", &command_name, &min_args, &max_args,
&usage, &short_help, &long_help, &p_callback)) {
return NULL;
}
if (p_callback && PyCallable_Check(p_callback)) {
api_register_command(command_name, min_args, max_args, usage, api_register_command(command_name, min_args, max_args, usage,
short_help, long_help, p_callback, ruby_command_callback); short_help, long_help, (void *)v_callback, ruby_command_callback);
}
return Py_BuildValue(""); return Qnil;
} }
static PyObject * static VALUE
ruby_api_register_timed(PyObject *self, PyObject *args) ruby_api_register_timed(VALUE self, VALUE v_callback, VALUE v_interval_seconds)
{ {
PyObject *p_callback = NULL; int interval_seconds = NUM2INT(v_interval_seconds);
int interval_seconds = 0;
if (!PyArg_ParseTuple(args, "Oi", &p_callback, &interval_seconds)) { api_register_timed((void*)v_callback, interval_seconds, ruby_timed_callback);
return NULL;
}
if (p_callback && PyCallable_Check(p_callback)) { return Qnil;
api_register_timed(p_callback, interval_seconds, ruby_timed_callback);
}
return Py_BuildValue("");
} }
static PyObject* static VALUE
ruby_api_notify(PyObject *self, PyObject *args) ruby_api_notify(VALUE self, VALUE v_message, VALUE v_timeout_ms, VALUE v_category)
{ {
const char *message = NULL; const char *message = STR2CSTR(v_message);
const char *category = NULL; const char *category = STR2CSTR(v_category);
int timeout_ms = 5000; int timeout_ms = NUM2INT(v_timeout_ms);
if (!PyArg_ParseTuple(args, "sis", &message, &timeout_ms, &category)) {
return NULL;
}
api_notify(message, category, timeout_ms); api_notify(message, category, timeout_ms);
return Py_BuildValue(""); return Qnil;
} }
static PyObject* static VALUE
ruby_api_send_line(PyObject *self, PyObject *args) ruby_api_send_line(VALUE self, VALUE v_line)
{ {
char *line = NULL; char *line = STR2CSTR(v_line);
if (!PyArg_ParseTuple(args, "s", &line)) {
return NULL;
}
api_send_line(line); api_send_line(line);
return Py_BuildValue(""); return Qnil;
} }
static PyObject * static VALUE
ruby_api_get_current_recipient(PyObject *self, PyObject *args) ruby_api_get_current_recipient(VALUE self)
{ {
char *recipient = api_get_current_recipient(); char *recipient = api_get_current_recipient();
if (recipient != NULL) { if (recipient != NULL) {
return Py_BuildValue("s", recipient); return rb_str_new2(recipient);
} else { } else {
return Py_BuildValue(""); return Qnil;
} }
} }
@ -179,5 +161,11 @@ void
ruby_api_init(void) ruby_api_init(void)
{ {
prof_module = rb_define_module("prof"); prof_module = rb_define_module("prof");
rb_define_module_function(prof_module, "cons_alert", RUBY_METHOD_FUNC(ruby_api_cons_alert), 0);
rb_define_module_function(prof_module, "cons_show", RUBY_METHOD_FUNC(ruby_api_cons_show), 1); rb_define_module_function(prof_module, "cons_show", RUBY_METHOD_FUNC(ruby_api_cons_show), 1);
rb_define_module_function(prof_module, "register_command", RUBY_METHOD_FUNC(ruby_api_register_command), 7);
rb_define_module_function(prof_module, "register_timed", RUBY_METHOD_FUNC(ruby_api_register_timed), 2);
rb_define_module_function(prof_module, "send_line", RUBY_METHOD_FUNC(ruby_api_send_line), 1);
rb_define_module_function(prof_module, "notify", RUBY_METHOD_FUNC(ruby_api_notify), 3);
rb_define_module_function(prof_module, "get_current_recipient", RUBY_METHOD_FUNC(ruby_api_get_current_recipient), 0);
} }

View File

@ -38,12 +38,12 @@ ruby_env_init(void)
ruby_check_error(); ruby_check_error();
ruby_api_init(); ruby_api_init();
ruby_check_error(); ruby_check_error();
// TODO change to use XDG spec // TODO set loadpath for ruby interpreter
GString *path = g_string_new(Py_GetPath()); //GString *path = g_string_new(Py_GetPath());
g_string_append(path, ":./plugins/"); //g_string_append(path, ":./plugins/");
PySys_SetPath(path->str); //PySys_SetPath(path->str);
ruby_check_error(); //ruby_check_error();
g_string_free(path, TRUE); //g_string_free(path, TRUE);
} }
ProfPlugin * ProfPlugin *
@ -72,6 +72,7 @@ ruby_plugin_create(const char * const filename)
void void
ruby_init_hook(ProfPlugin *plugin, const char * const version, const char * const status) ruby_init_hook(ProfPlugin *plugin, const char * const version, const char * const status)
{ {
/* TODO
PyObject *p_args = Py_BuildValue("ss", version, status); PyObject *p_args = Py_BuildValue("ss", version, status);
PyObject *p_function; PyObject *p_function;
@ -85,11 +86,13 @@ ruby_init_hook(ProfPlugin *plugin, const char * const version, const char * cons
Py_XDECREF(p_function); Py_XDECREF(p_function);
} }
} }
*/
} }
void void
ruby_on_start_hook(ProfPlugin *plugin) ruby_on_start_hook(ProfPlugin *plugin)
{ {
/*
PyObject *p_function; PyObject *p_function;
PyObject *p_module = plugin->module; PyObject *p_module = plugin->module;
@ -102,11 +105,13 @@ ruby_on_start_hook(ProfPlugin *plugin)
Py_XDECREF(p_function); Py_XDECREF(p_function);
} }
} }
*/
} }
void void
ruby_on_connect_hook(ProfPlugin *plugin) ruby_on_connect_hook(ProfPlugin *plugin)
{ {
/*
PyObject *p_function; PyObject *p_function;
PyObject *p_module = plugin->module; PyObject *p_module = plugin->module;
@ -119,11 +124,13 @@ ruby_on_connect_hook(ProfPlugin *plugin)
Py_XDECREF(p_function); Py_XDECREF(p_function);
} }
} }
*/
} }
void void
ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char * const message) ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const char * const message)
{ {
/* TODO
PyObject *p_args = Py_BuildValue("ss", jid, message); PyObject *p_args = Py_BuildValue("ss", jid, message);
PyObject *p_function; PyObject *p_function;
@ -137,19 +144,24 @@ ruby_on_message_received_hook(ProfPlugin *plugin, const char * const jid, const
Py_XDECREF(p_function); Py_XDECREF(p_function);
} }
} }
*/
} }
void void
ruby_check_error(void) ruby_check_error(void)
{ {
/* TODO
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_Print(); PyErr_Print();
PyErr_Clear(); PyErr_Clear();
} }
*/
} }
void void
ruby_shutdown(void) ruby_shutdown(void)
{ {
/* TODO
Py_Finalize(); Py_Finalize();
*/
} }