send_motd python.
authorJelmer Vernooij <jelmer@samba.org>
Sun, 12 Apr 2009 01:50:05 +0000 (03:50 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 12 Apr 2009 01:50:05 +0000 (03:50 +0200)
python/irc.c
python/tests/test_irc.py

index b988d7cc3f3e413bcea24c38f737a0af63029905..88c1edd4fffe351f10e1f56bb458cab18a3359bc 100644 (file)
@@ -995,6 +995,36 @@ static PyObject *py_client_send_state(PyClientObject *self, PyNetworkStateObject
     Py_RETURN_NONE;
 }
 
+static PyObject *py_client_send_motd(PyClientObject *self, PyObject *py_motd)
+{
+    char **motd;
+    int i;
+    if (!PyList_Check(py_motd)) {
+        PyErr_SetNone(PyExc_TypeError);
+        return NULL;
+    }
+
+    motd = g_new0(char *, PyList_Size(py_motd) + 1);
+
+    for (i = 0; i < PyList_Size(py_motd); i++) {
+        PyObject *item = PyList_GetItem(py_motd, i);
+        if (!PyString_Check(item)) {
+            PyErr_SetNone(PyExc_TypeError);
+            g_free(motd);
+            return NULL;
+        }
+
+        motd[i] = PyString_AsString(item);
+    }
+    motd[i] = NULL;
+
+    client_send_motd(self->client, motd);
+
+    g_free(motd);
+
+    Py_RETURN_NONE;
+}
+
 static PyMethodDef py_client_methods[] = {
     { "set_charset", (PyCFunction)py_client_set_charset, 
         METH_VARARGS,
@@ -1007,6 +1037,9 @@ static PyMethodDef py_client_methods[] = {
     { "send_state", (PyCFunction)py_client_send_state,
         METH_O,
         "Send a network state to a client." },
+    { "send_motd", (PyCFunction)py_client_send_motd,
+        METH_O,
+        "Send a MOTD to a client." },
     { NULL }
 };
 
@@ -1218,7 +1251,11 @@ static int py_process_to_client(struct irc_client *client, const struct irc_line
 
     ret = PyObject_CallMethod(self, "process_to_client", "O", py_line);
     Py_DECREF(py_line);
-    Py_XDECREF(ret);
+    if (ret == NULL) {
+        PyErr_Clear(); /* FIXME: */
+        return -1;
+    }
+    Py_DECREF(ret);
 
     return 0;
 }
index afe73eef510c664f95aa13acb85201a7b1ce87a9..9a816dce89b4b0c3647254683b45df6a23e39032 100644 (file)
@@ -270,6 +270,9 @@ class DummyTransport(object):
     def __init__(self):
         self._sent_lines = []
 
+    def str_lines(self):
+        return [str(l) for l in self._sent_lines]
+
     def send_line(self, line):
         self._sent_lines.append(line)
 
@@ -305,6 +308,16 @@ class ClientTests(unittest.TestCase):
         c = irc.Client(DummyTransport(), "myorigin", "description")
         self.assertEquals(None, c.own_hostmask)
 
+    def test_send_motd(self):
+        t = DummyTransport()
+        c = irc.Client(t, "myorigin", "description")
+        c.send_motd(["bla", "blie bloe"])
+        self.assertEquals([
+            ':myorigin 375 * :Start of MOTD',
+            ':myorigin 372 * :bla',
+            ':myorigin 372 * :blie bloe',
+            ':myorigin 376 * :End of MOTD'], t.str_lines())
+
 
 class ClientSendStateTests(unittest.TestCase):