summaryrefslogtreecommitdiffstats
path: root/src/objects/chatnet-object.c
blob: 2cbbb70e3001689d6078af0ae779cc8253af9736 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <Python.h>
#include "pymodule.h"
#include "base-objects.h"
#include "chatnet-object.h"
#include "pyirssi.h"
#include "pycore.h"
#include "pyutils.h"

static void chatnet_cleanup(CHATNET_REC *cn)
{
    PyChatnet *pycn = signal_get_user_data();

    if (cn == pycn->data)
    {
        pycn->data = NULL;
        pycn->cleanup_installed = 0;
        signal_remove_data("chatnet destroyed", chatnet_cleanup, pycn);
    }
}

static void PyChatnet_dealloc(PyChatnet *self)
{
    if (self->cleanup_installed)
        signal_remove_data("chatnet destroyed", chatnet_cleanup, self);

    self->ob_type->tp_free((PyObject*)self);
}

/* Getters */
PyDoc_STRVAR(PyChatnet_name_doc,
    "name of chat network"
);
static PyObject *PyChatnet_name_get(PyChatnet *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->name);
}

PyDoc_STRVAR(PyChatnet_nick_doc,
    "if not empty, nick preferred in this network"
);
static PyObject *PyChatnet_nick_get(PyChatnet *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->nick);
}

PyDoc_STRVAR(PyChatnet_username_doc,
    "if not empty, username preferred in this network"
);
static PyObject *PyChatnet_username_get(PyChatnet *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->username);
}

PyDoc_STRVAR(PyChatnet_realname_doc,
    "if not empty, realname preferred in this network"
);
static PyObject *PyChatnet_realname_get(PyChatnet *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->realname);
}

PyDoc_STRVAR(PyChatnet_own_host_doc,
    "address to use when connecting to this network"
);
static PyObject *PyChatnet_own_host_get(PyChatnet *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->own_host);
}

PyDoc_STRVAR(PyChatnet_autosendcmd_doc,
    "command to send after connecting to this network"
);
static PyObject *PyChatnet_autosendcmd_get(PyChatnet *self, void *closure)
{
    RET_NULL_IF_INVALID(self->data);
    RET_AS_STRING_OR_NONE(self->data->autosendcmd);
}

/* specialized getters/setters */
static PyGetSetDef PyChatnet_getseters[] = {
    {"name", (getter)PyChatnet_name_get, NULL,
        PyChatnet_name_doc, NULL},
    {"nick", (getter)PyChatnet_nick_get, NULL,
        PyChatnet_nick_doc, NULL},
    {"username", (getter)PyChatnet_username_get, NULL,
        PyChatnet_username_doc, NULL},
    {"realname", (getter)PyChatnet_realname_get, NULL,
        PyChatnet_realname_doc, NULL},
    {"own_host", (getter)PyChatnet_own_host_get, NULL,
        PyChatnet_own_host_doc, NULL},
    {"autosendcmd", (getter)PyChatnet_autosendcmd_get, NULL,
        PyChatnet_autosendcmd_doc, NULL},
    {NULL}
};

static PyMethodDef PyChatnet_methods[] = {
    {NULL}  /* Sentinel */
};

PyTypeObject PyChatnetType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "irssi.Chatnet",            /*tp_name*/
    sizeof(PyChatnet),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)PyChatnet_dealloc, /*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*/
    "PyChatnet objects",           /* tp_doc */
    0,		               /* tp_traverse */
    0,		               /* tp_clear */
    0,		               /* tp_richcompare */
    0,		               /* tp_weaklistoffset */
    0,		               /* tp_iter */
    0,		               /* tp_iternext */
    PyChatnet_methods,             /* tp_methods */
    0,                      /* tp_members */
    PyChatnet_getseters,        /* tp_getset */
    &PyIrssiChatBaseType,          /* 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 */
};


/* chatnet factory function */
PyObject *pychatnet_sub_new(void *cn, PyTypeObject *subclass)
{
    static const char *name = "CHATNET";
    PyChatnet *pycn = NULL;

    pycn = py_instp(PyChatnet, subclass); 
    if (!pycn)
        return NULL;

    pycn->data = cn;
    pycn->base_name = name;
    signal_add_last_data("chatnet destroyed", chatnet_cleanup, pycn);
    pycn->cleanup_installed = 1;

    return (PyObject *)pycn;
}

PyObject *pychatnet_new(void *cn)
{
    return pychatnet_sub_new(cn, &PyChatnetType);
}

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

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

    return 1;
}