summaryrefslogtreecommitdiffstats
path: root/objects/irc-channel-object.c
blob: daf3329578392f79fec8808766b0089024acf6a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <Python.h>
#include "pyirssi_irc.h"
#include "pymodule.h"
#include "pycore.h"
#include "irc-channel-object.h"
#include "factory.h"

/* PyIrcChannel destructor is inherited from PyChannel */

/* specialized getters/setters */
static PyGetSetDef PyIrcChannel_getseters[] = {
    {NULL}
};

PyDoc_STRVAR(bans_doc,
    "Returns a list of bans in the channel."
);
static PyObject *PyIrcChannel_bans(PyIrcChannel *self, PyObject *args)
{
    return py_irssi_objlist_new(self->data->banlist, 1, (InitFunc)pyban_new);
}

PyDoc_STRVAR(ban_get_mask_doc,
    "Get ban mask for 'nick'."
);
static PyObject *PyIrcChannel_ban_get_mask(PyIrcChannel *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"nick", "ban_type", NULL};
    char *nick, *str;
    int ban_type = 0;
    PyObject *ret;

    RET_NULL_IF_INVALID(self->data);

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &nick, &ban_type))
        return NULL;

    str = ban_get_mask(self->data, nick, ban_type);
    if (!str)
        Py_RETURN_NONE;
    
    ret = PyString_FromString(str);
    g_free(str);

    return ret;
}

PyDoc_STRVAR(banlist_add_doc,
    "Add a new ban to channel. Return None if duplicate."
);
static PyObject *PyIrcChannel_banlist_add(PyIrcChannel *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"ban", "nick", "time", NULL};
    char *ban, *nick;
    time_t btime;
    BAN_REC *newban;

    RET_NULL_IF_INVALID(self->data);

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "ssk", kwlist, &ban, &nick, &btime))
        return NULL;

    newban = banlist_add(self->data, ban, nick, btime);
    /* XXX: return none or throw error? */
    if (!newban)
        Py_RETURN_NONE;

    return pyban_new(newban);
}

PyDoc_STRVAR(banlist_remove_doc,
    "Remove a new ban from channel."
);
static PyObject *PyIrcChannel_banlist_remove(PyIrcChannel *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"ban", "nick", NULL};
    char *ban, *nick;

    RET_NULL_IF_INVALID(self->data);

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss", kwlist, &ban, &nick))
        return NULL;

    banlist_remove(self->data, ban, nick);
    Py_RETURN_NONE;
}

/* Methods for object */
static PyMethodDef PyIrcChannel_methods[] = {
    {"bans", (PyCFunction)PyIrcChannel_bans, METH_NOARGS, 
        bans_doc},

    {"ban_get_mask", (PyCFunction)PyIrcChannel_ban_get_mask, METH_VARARGS | METH_KEYWORDS, 
        ban_get_mask_doc},

    {"banlist_add", (PyCFunction)PyIrcChannel_banlist_add, METH_VARARGS | METH_KEYWORDS, 
        banlist_add_doc},

    {"banlist_remove", (PyCFunction)PyIrcChannel_banlist_remove, METH_VARARGS | METH_KEYWORDS, 
        banlist_remove_doc},

    {NULL}  /* Sentinel */
};

PyTypeObject PyIrcChannelType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "IrcChannel",            /*tp_name*/
    sizeof(PyIrcChannel),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    0,                          /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "PyIrcChannel objects",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyIrcChannel_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyIrcChannel_getseters,        /* tp_getset */
    &PyChannelType,          /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    0,      /* tp_init */
    0,                         /* tp_alloc */
    0,                 /* tp_new */
};


/* irc channel factory function */
PyObject *pyirc_channel_new(void *chan)
{
    static const char *BASE_NAME = "CHANNEL";
    return pychannel_sub_new(chan, BASE_NAME, &PyIrcChannelType);
}

int irc_channel_object_init(void) 
{
    g_return_val_if_fail(py_module != NULL, 0);

    if (PyType_Ready(&PyIrcChannelType) < 0)
        return 0;
    
    Py_INCREF(&PyIrcChannelType);
    PyModule_AddObject(py_module, "IrcChannel", (PyObject *)&PyIrcChannelType);

    return 1;
}