return 0;
}
-/* get a tuple with all users in it */
static PyObject*
-emb_get_users(UNUSED_ARG(PyObject *self), PyObject *args) {
+pyobj_from_dict_t(dict_t d) {
PyObject* retval;
- size_t num_clients, n = 0;
+ size_t n = 0;
struct _tuple_dict_extra extra;
- if (!PyArg_ParseTuple(args, ""))
- return NULL;
-
- num_clients = dict_size(clients);
- retval = PyTuple_New(num_clients);
- if (retval == NULL)
+ if ((retval = PyTuple_New(dict_size(d))) == NULL)
return NULL;
extra.extra = &n;
extra.data = retval;
- if (dict_foreach(clients, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
+ if (dict_foreach(d, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
pyobj_release_tuple(retval, n);
return NULL;
}
return retval;
}
-/* get a tuple with all channels in it */
+/* get a tuple with all users in it */
static PyObject*
-emb_get_channels(UNUSED_ARG(PyObject* self), PyObject* args) {
- PyObject* retval;
- size_t num_channels, n = 0;
- struct _tuple_dict_extra extra;
-
+emb_get_users(UNUSED_ARG(PyObject *self), PyObject *args) {
if (!PyArg_ParseTuple(args, ""))
return NULL;
- num_channels = dict_size(channels);
- retval = PyTuple_New(num_channels);
- if (retval == NULL)
- return NULL;
-
- extra.extra = &n;
- extra.data = retval;
+ return pyobj_from_dict_t(clients);
+}
- if (dict_foreach(channels, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
- pyobj_release_tuple(retval, n);
+/* get a tuple with all channels in it */
+static PyObject*
+emb_get_channels(UNUSED_ARG(PyObject* self), PyObject* args) {
+ if (!PyArg_ParseTuple(args, ""))
return NULL;
- }
- return retval;
+ return pyobj_from_dict_t(channels);
}
static PyObject*
emb_get_servers(UNUSED_ARG(PyObject* self), PyObject* args) {
- PyObject* retval;
- size_t n = 0;
- struct _tuple_dict_extra extra;
-
if (!PyArg_ParseTuple(args, ""))
return NULL;
- retval = PyTuple_New(dict_size(servers));
- if (retval == NULL)
- return NULL;
-
- extra.extra = &n;
- extra.data = retval;
-
- if (dict_foreach(servers, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
- pyobj_release_tuple(retval, n);
- return NULL;
- }
-
- return retval;
+ return pyobj_from_dict_t(servers);
}
static PyObject*
emb_get_accounts(UNUSED_ARG(PyObject* self), PyObject* args) {
- PyObject* retval;
- size_t n = 0;
- struct _tuple_dict_extra extra;
-
if (!PyArg_ParseTuple(args, ""))
return NULL;
- retval = PyTuple_New(dict_size(nickserv_handle_dict));
- if (retval == NULL)
- return NULL;
-
- extra.extra = &n;
- extra.data = retval;
-
- if (dict_foreach(nickserv_handle_dict, _dict_iter_fill_tuple, (void*)&extra) != NULL) {
- pyobj_release_tuple(retval, n);
- return NULL;
- }
-
- return retval;
+ return pyobj_from_dict_t(nickserv_handle_dict);
}
static PyObject*
return Py_None;
}
+struct py_timeq_extra {
+ PyObject* func;
+ PyObject* arg;
+};
+
+static
+void py_timeq_callback(void* data) {
+ struct py_timeq_extra* extra = (struct py_timeq_extra*)data;
+
+ PyObject* retval = PyObject_Call(extra->func, extra->arg, NULL);
+ Py_XDECREF(retval);
+
+ Py_DECREF(extra->func);
+ Py_DECREF(extra->arg);
+}
+
+static PyObject*
+emb_timeq_add(UNUSED_ARG(PyObject* self), PyObject* args) {
+ time_t when;
+ PyObject* func, *arg;
+ struct py_timeq_extra* extra;
+
+ if (!PyArg_ParseTuple(args, "lOO", &when, &func, &arg))
+ return NULL;
+
+ if (!PyFunction_Check(func)) {
+ PyErr_SetString(PyExc_Exception, "first argument must be a function");
+ return NULL;
+ }
+
+ if (!PyTuple_Check(arg)) {
+ PyErr_SetString(PyExc_Exception, "second argument must be a tuple");
+ return NULL;
+ }
+
+ extra = malloc(sizeof(struct py_timeq_extra));
+ if (extra == NULL) {
+ PyErr_SetString(PyExc_Exception, "out of memory");
+ return NULL;
+ }
+
+ Py_INCREF(func);
+ Py_INCREF(arg);
+
+ extra->func = func;
+ extra->arg = arg;
+
+ timeq_add(when, py_timeq_callback, (void*)extra);
+
+ return Py_None;
+}
+
static PyMethodDef EmbMethods[] = {
/* Communication methods */
{"dump", emb_dump, METH_VARARGS, "Dump raw P10 line to server"},
//TODO: {"get_config", emb_get_config, METH_VARARGS, "get x3.conf settings into a nested dict"},
//TODO: {"config_set", emb_config_set, METH_VARARGS, "change a config setting 'on-the-fly'."},
//
-//TODO: {"timeq_add", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."},
+ {"timeq_add", emb_timeq_add, METH_VARARGS, "add function to callback to the event system"},
//TODO: {"timeq_del", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."},
/* Information gathering methods */
{"get_user", emb_get_user, METH_VARARGS, "Get details about a nickname"},