2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <doldaconnect/uilib.h>
22 #include <doldaconnect/uimisc.h>
23 #include <doldaconnect/utils.h>
28 struct dc_response *resp;
33 static void resptype_del(struct respobj *self)
35 dc_freeresp(self->resp);
36 self->ob_type->tp_free((PyObject *)self);
39 static PyObject *resp_getcode(struct respobj *self)
41 return(Py_BuildValue("i", self->resp->code));
44 static PyObject *resp_gettag(struct respobj *self)
46 return(Py_BuildValue("i", self->resp->tag));
49 static PyObject *resp_getcmd(struct respobj *self)
51 return(PyUnicode_FromWideChar(self->resp->cmdname, wcslen(self->resp->cmdname)));
54 static PyObject *resp_extract(struct respobj *self)
60 ret = PyList_New(self->resp->numlines);
61 for(i = 0; i < self->resp->numlines; i++) {
62 sl = PyList_New(self->resp->rlines[i].argc);
63 for(o = 0; o < self->resp->rlines[i].argc; o++) {
64 c = self->resp->rlines[i].argv[o];
65 PyList_SetItem(sl, o, PyUnicode_FromWideChar(c, wcslen(c)));
67 PyList_SetItem(ret, i, sl);
72 static PyObject *resp_intresp(struct respobj *self)
76 struct dc_intresp *ires;
79 self->resp->curline = 0;
80 while((ires = dc_interpret(self->resp)) != NULL) {
81 sl = PyList_New(ires->argc);
82 for(i = 0; i < ires->argc; i++) {
83 switch(ires->argv[i].type) {
85 PyList_SetItem(sl, i, PyUnicode_FromWideChar(ires->argv[i].val.str, wcslen(ires->argv[i].val.str)));
88 PyList_SetItem(sl, i, PyInt_FromLong(ires->argv[i].val.num));
91 PyList_SetItem(sl, i, PyFloat_FromDouble(ires->argv[i].val.flnum));
96 PyList_Append(ret, sl);
101 static PyMethodDef respmethods[] = {
102 {"getcode", (PyCFunction)resp_getcode, METH_NOARGS,
103 "Get the numerical code from the response"},
104 {"gettag", (PyCFunction)resp_gettag, METH_NOARGS,
105 "Get the tag from the response"},
106 {"getcmd", (PyCFunction)resp_getcmd, METH_NOARGS,
107 "Get the command name from the response"},
108 {"extract", (PyCFunction)resp_extract, METH_NOARGS,
109 "Extract the lines and columns from the response"},
110 {"intresp", (PyCFunction)resp_intresp, METH_NOARGS,
111 "Interpret all the lines from the response with native datatypes"},
112 {NULL, NULL, 0, NULL}
115 static PyTypeObject resptype = {
116 PyObject_HEAD_INIT(NULL)
117 .tp_name = "dolcon.Response",
118 .tp_basicsize = sizeof(struct respobj),
119 .tp_flags = Py_TPFLAGS_DEFAULT,
120 .tp_doc = "Dolda Connect response objects",
121 .tp_dealloc = (destructor)resptype_del,
122 .tp_methods = respmethods,
125 static struct respobj *makeresp(struct dc_response *resp)
129 new = (struct respobj *)resptype.tp_alloc(&resptype, 0);
134 static PyObject *mod_connect(PyObject *self, PyObject *args)
140 if(!PyArg_ParseTuple(args, "s|i", &host, &port))
144 if((fd = dc_connect(host, port)) < 0) {
145 PyErr_SetFromErrno(PyExc_OSError);
148 return(Py_BuildValue("i", fd));
151 static PyObject *mod_disconnect(PyObject *self, PyObject *args)
160 static PyObject *mod_connected(PyObject *self, PyObject *args)
168 static PyObject *mod_select(PyObject *self, PyObject *args)
174 if(!PyArg_ParseTuple(args, "|i", &timeout))
177 PyErr_SetString(PyExc_RuntimeError, "Not connected");
183 pfd.events |= POLLOUT;
184 if((ret = poll(&pfd, 1, timeout)) < 0) {
189 PyErr_SetFromErrno(PyExc_OSError);
192 if(((pfd.revents & POLLIN) && dc_handleread()) || ((pfd.revents & POLLOUT) && dc_handlewrite())) {
197 PyErr_SetFromErrno(PyExc_OSError);
204 static PyObject *mod_getresp(PyObject *self, PyObject *args)
207 struct dc_response *resp;
210 if(!PyArg_ParseTuple(args, "|i", &tag))
215 resp = dc_gettaggedresp(tag);
218 return((PyObject *)makeresp(resp));
221 static int qcmd_cb(struct dc_response *resp)
223 PyObject *pycb, *args, *ret;
226 args = Py_BuildValue("(N)", makeresp(resp));
227 ret = PyObject_Call(pycb, args, NULL);
234 static PyObject *mod_qcmd(PyObject *self, PyObject *args, PyObject *kwargs)
237 wchar_t **toks, *tok, *cmd;
238 size_t tokssize, toksdata, toksize;
239 PyObject *c, *n, *cb, *ret;
242 tokssize = toksdata = 0;
245 for(i = 0; i < PySequence_Size(args); i++) {
246 if((c = PySequence_GetItem(args, i)) == NULL)
248 if(!PyUnicode_Check(c)) {
249 n = PyUnicode_FromObject(c);
254 tok = smalloc((toksize = (PyUnicode_GetSize(c) + 1)) * sizeof(*tok));
255 tok[PyUnicode_AsWideChar((PyUnicodeObject *)c, tok, toksize)] = L'\0';
263 PyErr_SetString(PyExc_TypeError, "qcmd needs at least 1 argument");
266 addtobuf(toks, NULL);
268 if(PyMapping_HasKeyString(kwargs, "cb")) {
269 cb = PyMapping_GetItemString(kwargs, "cb");
270 if(PyCallable_Check(cb)) {
271 ret = PyInt_FromLong(dc_queuecmd(qcmd_cb, cb, cmd, L"%%a", toks, NULL));
273 PyErr_SetString(PyExc_TypeError, "Callback must be callable");
277 ret = PyInt_FromLong(dc_queuecmd(NULL, NULL, cmd, L"%%a", toks, NULL));
287 static void login_cb(int err, wchar_t *reason, PyObject *cb)
290 PyObject *args, *pyerr, *pyreason, *ret;
293 case DC_LOGIN_ERR_SUCCESS:
296 case DC_LOGIN_ERR_NOLOGIN:
299 case DC_LOGIN_ERR_SERVER:
302 case DC_LOGIN_ERR_USER:
305 case DC_LOGIN_ERR_CONV:
308 case DC_LOGIN_ERR_AUTHFAIL:
312 pyerr = PyString_FromString(errstr);
314 Py_INCREF(pyreason = Py_None);
316 pyreason = PyUnicode_FromWideChar(reason, wcslen(reason));
317 args = PyTuple_Pack(2, pyerr, pyreason);
318 Py_DECREF(pyerr); Py_DECREF(pyreason);
319 ret = PyObject_Call(cb, args, NULL);
325 static PyObject *mod_loginasync(PyObject *self, PyObject *args, PyObject *kwargs)
329 PyObject *o, *cb, *conv;
334 if(!PyArg_ParseTuple(args, "O|i", &cb, &useauthless))
336 if(!PyCallable_Check(cb)) {
337 PyErr_SetString(PyExc_TypeError, "Callback must be callable");
340 if(PyMapping_HasKeyString(kwargs, "username")) {
341 o = PyMapping_GetItemString(kwargs, "username");
342 username = PyString_AsString(o);
347 if(PyMapping_HasKeyString(kwargs, "conv")) {
348 conv = PyMapping_GetItemString(kwargs, "conv");
349 if(!PyCallable_Check(conv)) {
350 PyErr_SetString(PyExc_TypeError, "Conv callback must be callable");
353 PyErr_SetString(PyExc_NotImplementedError, "Custom conv functions are not yet supported by the Python interface");
357 dc_loginasync(username, useauthless, NULL, (void (*)(int, wchar_t *, void *))login_cb, cb);
361 static PyObject *mod_lexsexpr(PyObject *self, PyObject *args)
363 PyObject *arg, *se, *ret;
364 wchar_t **arr, **ap, *buf;
367 if(!PyArg_ParseTuple(args, "O", &arg))
369 if((se = PyUnicode_FromObject(arg)) == NULL)
371 buf = smalloc((bufsize = (PyUnicode_GetSize(se) + 1)) * sizeof(*buf));
372 buf[PyUnicode_AsWideChar((PyUnicodeObject *)se, buf, bufsize)] = L'\0';
373 arr = dc_lexsexpr(buf);
378 for(ap = arr; *ap; ap++)
379 PyList_Append(ret, PyUnicode_FromWideChar(*ap, wcslen(*ap)));
385 static PyMethodDef methods[] = {
386 {"connect", mod_connect, METH_VARARGS,
387 "Connect to a Dolda Connect server"},
388 {"disconnect", mod_disconnect, METH_VARARGS,
389 "Disconnect from the server"},
390 {"connected", mod_connected, METH_VARARGS,
391 "Return a boolean indicated whether there currently is a connection to a server"},
392 {"select", mod_select, METH_VARARGS,
393 "Handle data from the server connection, optionally blocking until something happens"},
394 {"getresp", mod_getresp, METH_VARARGS,
395 "Get a queued response object"},
396 {"qcmd", (PyCFunction)mod_qcmd, METH_VARARGS | METH_KEYWORDS,
397 "Queue a command to be sent to the server"},
398 {"loginasync", (PyCFunction)mod_loginasync, METH_VARARGS | METH_KEYWORDS,
399 "Perform an asynchronous login procedure"},
400 {"lexsexpr", mod_lexsexpr, METH_VARARGS,
401 "Use a standard algorithm to lex a search expression"},
402 {NULL, NULL, 0, NULL}
405 PyMODINIT_FUNC initdolmod(void)
409 if(PyType_Ready(&resptype) < 0)
411 m = Py_InitModule("dolmod", methods);
412 Py_INCREF(&resptype);
413 PyModule_AddObject(m, "Response", (PyObject *)&resptype);