1 /* ctrlproxy: A modular IRC proxy
2 * (c) 2002-2009 Jelmer Vernooij <jelmer@nl.linux.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "ctrlproxy.h"
23 #include "libirc/python/irc.h"
25 static int py_linestack_dealloc(PyLinestackObject *self)
27 if (self->parent != NULL)
28 Py_DECREF(self->parent);
30 free_linestack_context(self->linestack);
31 PyObject_Del((PyObject *)self);
35 static PyObject *py_linestack_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
37 char *kwnames[] = { "path", "state", "truncate", NULL };
38 PyLinestackObject *self;
42 struct irc_network_state *state;
43 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|i", kwnames,
44 &data_dir, &py_state, &truncate))
47 self = (PyLinestackObject *)type->tp_alloc(type, 0);
53 state = PyObject_AsNetworkState(py_state);
59 self->linestack = create_linestack(data_dir, truncate, state);
61 return (PyObject *)self;
64 static PyObject *py_linestack_insert_line(PyLinestackObject *self, PyObject *args)
68 enum data_direction dir;
69 PyObject *py_line, *py_state;
70 struct irc_network_state *state;
72 if (!PyArg_ParseTuple(args, "OiO", &py_line, &dir, &py_state))
75 l = PyObject_AsLine(py_line);
80 state = PyObject_AsNetworkState(py_state);
85 ret = linestack_insert_line(self->linestack, l, dir, state);
89 PyErr_SetNone(PyExc_RuntimeError);
96 static PyObject *py_linestack_replay(PyLinestackObject *self, PyObject *args)
101 struct irc_network_state *state;
102 if (!PyArg_ParseTuple(args, "OLL", &py_state, &from, &to))
105 state = PyObject_AsNetworkState(py_state);
110 ret = linestack_replay(self->linestack, &from, &to, state);
112 PyErr_SetNone(PyExc_RuntimeError);
119 static PyObject *py_linestack_get_marker(PyLinestackObject *self)
121 linestack_marker m = linestack_get_marker(self->linestack);
123 PyErr_SetNone(PyExc_RuntimeError);
126 return PyLong_FromLong(*m);
129 static PyObject *py_linestack_send(PyLinestackObject *self, PyObject *args)
132 gboolean dataonly = FALSE;
133 gboolean timed = FALSE;
136 struct irc_client *client;
140 if (!PyArg_ParseTuple(args, "LLO|ziii", &from, &to, &py_client,
141 &object, &dataonly, &timed, &time_offset))
144 client = PyObject_AsClient(py_client);
145 if (client == NULL) {
149 if (object != NULL) {
150 ret = linestack_send_object(self->linestack, object, &from, &to, client,
151 dataonly, timed, time_offset);
153 ret = linestack_send(self->linestack, &from, &to, client, dataonly,
157 PyErr_SetNone(PyExc_RuntimeError);
166 PyLinestackObject *parent;
168 } PyLinestackIterObject;
170 static int py_linestack_iter_dealloc(PyLinestackIterObject *self)
172 Py_DECREF(self->parent);
177 static PyObject *py_linestack_iter_next(PyLinestackIterObject *self)
180 struct irc_line *line = NULL;
182 PyLineObject *py_line;
184 if (self->from == self->to) {
185 PyErr_SetNone(PyExc_StopIteration);
189 ret = linestack_read_entry(self->parent->linestack, self->from,
192 PyErr_SetNone(PyExc_RuntimeError);
196 py_line = PyObject_New(PyLineObject, &PyLineType);
197 py_line->line = line;
199 py_ret = Py_BuildValue("(Nl)", py_line, time);
205 PyTypeObject PyLinestackIterType = {
206 .tp_name = "LinestackIter",
207 .tp_flags = Py_TPFLAGS_DEFAULT,
208 .tp_basicsize = sizeof(PyLinestackIterObject),
209 .tp_iter = PyObject_SelfIter,
210 .tp_iternext = (iternextfunc)py_linestack_iter_next,
211 .tp_dealloc = (destructor)py_linestack_iter_dealloc,
214 static PyObject *py_linestack_traverse(PyLinestackObject *self, PyObject *args)
216 PyLinestackIterObject *ret;
217 ret = PyObject_New(PyLinestackIterObject, &PyLinestackIterType);
226 if (!PyArg_ParseTuple(args, "LL", &ret->from, &ret->to)) {
231 return (PyObject *)ret;
234 static PyMethodDef py_linestack_methods[] = {
235 { "insert_line", (PyCFunction)py_linestack_insert_line,
236 METH_VARARGS, "Insert line" },
237 { "replay", (PyCFunction)py_linestack_replay,
238 METH_VARARGS, "Replay" },
239 { "get_marker", (PyCFunction)py_linestack_get_marker,
240 METH_NOARGS, "Get marker" },
241 { "send", (PyCFunction)py_linestack_send, METH_VARARGS,
243 { "traverse", (PyCFunction)py_linestack_traverse, METH_VARARGS,
248 PyTypeObject PyLinestackType = {
249 .tp_name = "Linestack",
250 .tp_flags = Py_TPFLAGS_DEFAULT,
251 .tp_new = py_linestack_new,
252 .tp_methods = py_linestack_methods,
253 .tp_basicsize = sizeof(PyLinestackObject),
254 .tp_dealloc = (destructor)py_linestack_dealloc,