Providing setters for netbiosname, firstorg and firstou is messy.
[jelmer/openchange.git] / pyopenchange / pymapistoredb.c
1 /*
2    OpenChange MAPI implementation.
3
4    Python interface to mapistore database
5
6    Copyright (C) Julien Kerihuel 2010-2011.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <Python.h>
23 #include "pyopenchange/pymapistore.h"
24
25 void initmapistoredb(void);
26
27 static PyObject *py_MAPIStoreDB_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
28 {
29         TALLOC_CTX                      *mem_ctx;
30         struct mapistoredb_context      *mdb_ctx;
31         PyMAPIStoreDBObject             *mdbobj;
32         char                            *kwnames[] = { "path", NULL };
33         const char                      *path = NULL;
34
35         /* Path is optional */
36         PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwnames, &path);
37
38         mem_ctx = talloc_new(NULL);
39         if (mem_ctx == NULL) {
40                 PyErr_NoMemory();
41                 return NULL;
42         }
43
44         mdb_ctx = mapistoredb_init(mem_ctx, path);
45         if (mdb_ctx == NULL) {
46                 DEBUG(0, ("mapistoredb_init returned NULL\n"));
47                 return NULL;
48         }
49
50         mdbobj = PyObject_New(PyMAPIStoreDBObject, &PyMAPIStoreDB);
51         mdbobj->mem_ctx = mem_ctx;
52         mdbobj->mdb_ctx = mdb_ctx;
53
54         return (PyObject *) mdbobj;
55 }
56
57 static void py_MAPIStoreDB_dealloc(PyObject *_self)
58 {
59         PyMAPIStoreDBObject     *self = (PyMAPIStoreDBObject *)_self;
60
61         talloc_free(self->mem_ctx);
62         PyObject_Del(_self);
63 }
64
65
66 static PyObject *py_MAPIStoreDB_dump_configuration(PyMAPIStoreDBObject *self, PyObject *args)
67 {
68         mapistoredb_dump_conf(self->mdb_ctx);
69         return PyInt_FromLong(0);
70 }
71
72 static PyObject *py_MAPIStoreDB_provision(PyMAPIStoreDBObject *self, PyObject *args, PyObject *kwargs)
73 {
74         const char              *netbiosname;
75         const char              *firstorg;
76         const char              *firstou;
77         char                    *kwnames[] = { "netbiosname", "firstorg", "firstou", NULL };
78
79         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sss", kwnames, &netbiosname, &firstorg, &firstou)) {
80                 return NULL;
81         }
82
83         mapistoredb_set_netbiosname(self->mdb_ctx, netbiosname);
84         mapistoredb_set_firstorg(self->mdb_ctx, firstorg);
85         mapistoredb_set_firstou(self->mdb_ctx, firstou);
86
87         return PyInt_FromLong(mapistoredb_provision(self->mdb_ctx));
88 }
89
90
91 static PyObject *py_MAPIStoreDB_get_mapistore_uri(PyMAPIStoreDBObject *_self, PyObject *args)
92 {
93         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
94         enum MAPISTORE_ERROR            retval;
95         enum MAPISTORE_DFLT_FOLDERS     dflt_folder;
96         const char                      *username;
97         const char                      *ns;
98         char                            *uri;
99
100         if (!PyArg_ParseTuple(args, "kss", &dflt_folder, &username, &ns)) {
101                 return NULL;
102         }
103
104         retval = mapistoredb_get_mapistore_uri(self->mdb_ctx, dflt_folder, ns, username, &uri);
105         if (retval == MAPISTORE_SUCCESS) {
106                 return PyString_FromString(uri);
107         }
108
109         return NULL;
110 }
111
112
113 static PyObject *py_MAPIStoreDB_get_new_fid(PyMAPIStoreDBObject *_self, PyObject *args)
114 {
115         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
116         enum MAPISTORE_ERROR            retval;
117         uint64_t                        fmid = 0;
118         char                            *username;
119
120         if (!PyArg_ParseTuple(args, "s", &username)) {
121                 return NULL;
122         }
123
124         retval = mapistoredb_get_new_fmid(self->mdb_ctx, (const char *)username, &fmid);
125         if (retval == MAPISTORE_SUCCESS) {
126                 return PyLong_FromUnsignedLongLong(fmid);
127         }
128
129         return NULL;
130 }
131
132 static PyObject *py_MAPIStoreDB_get_new_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
133 {
134         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
135         enum MAPISTORE_ERROR            retval;
136         char                            *username;
137         uint64_t                        range;
138         uint64_t                        range_start = 0;
139         uint64_t                        range_end = 0;
140
141         if (!PyArg_ParseTuple(args, "sK", &username, &range)) {
142                 return NULL;
143         }
144
145         retval = mapistoredb_get_new_allocation_range(self->mdb_ctx, (const char *)username, range, &range_start, &range_end);
146         if (retval == MAPISTORE_SUCCESS) {
147                 return Py_BuildValue("kKK", retval, range_start, range_end);
148         }
149
150         return Py_BuildValue("kKK", retval, range_start, range_start);
151 }
152
153 static PyObject *py_MAPIStoreDB_new_mailbox(PyMAPIStoreDBObject *_self, PyObject *args)
154 {
155         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
156         enum MAPISTORE_ERROR            retval;
157         char                            *username;
158         char                            *mapistore_uri;
159
160         if (!PyArg_ParseTuple(args, "ss", &username, &mapistore_uri)) {
161                 return NULL;
162         }
163
164         retval = mapistoredb_register_new_mailbox(self->mdb_ctx, username, mapistore_uri);
165         return PyInt_FromLong(retval);
166 }
167
168 static PyObject *py_MAPIStoreDB_set_mailbox_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
169 {
170         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
171         enum MAPISTORE_ERROR            retval;
172         uint64_t                        rstart;
173         uint64_t                        rend;
174         char                            *username;
175
176         if (!PyArg_ParseTuple(args, "sKK", &username, &rstart, &rend)) {
177                 return NULL;
178         }
179
180         retval = mapistoredb_register_new_mailbox_allocation_range(self->mdb_ctx, username, rstart, rend);
181         return PyInt_FromLong(retval);
182 }
183
184 static PyObject *PyMAPIStoreDB_getParameter(PyObject *_self, void *data)
185 {
186         PyMAPIStoreDBObject     *self = (PyMAPIStoreDBObject *) _self;
187         const char              *attr = (const char *) data;
188
189         if (!strcmp(attr, "netbiosname")) {
190                 return PyString_FromString(mapistoredb_get_netbiosname(self->mdb_ctx));
191         } else if (!strcmp(attr, "firstorg")) {
192                 return PyString_FromString(mapistoredb_get_firstorg(self->mdb_ctx));
193         } else if (!strcmp(attr, "firstou")) {
194                 return PyString_FromString(mapistoredb_get_firstou(self->mdb_ctx));
195         }
196
197         return NULL;
198 }
199
200 static PyMethodDef mapistoredb_methods[] = {
201         { "dump_configuration", (PyCFunction)py_MAPIStoreDB_dump_configuration, METH_VARARGS },
202         { "provision", (PyCFunction)py_MAPIStoreDB_provision, METH_KEYWORDS },
203         { "get_mapistore_uri", (PyCFunction)py_MAPIStoreDB_get_mapistore_uri, METH_VARARGS },
204         { "get_new_fid", (PyCFunction)py_MAPIStoreDB_get_new_fid, METH_VARARGS },
205         { "get_new_allocation_range", (PyCFunction)py_MAPIStoreDB_get_new_allocation_range, METH_VARARGS },
206         { "new_mailbox", (PyCFunction)py_MAPIStoreDB_new_mailbox, METH_VARARGS },
207         { "set_mailbox_allocation_range", (PyCFunction)py_MAPIStoreDB_set_mailbox_allocation_range, METH_VARARGS },
208         { NULL },
209 };
210
211 static PyGetSetDef mapistoredb_getsetters[] = {
212         { "netbiosname", (getter)PyMAPIStoreDB_getParameter,
213           (setter)NULL, "netbiosname", "netbiosname"},
214         { "firstorg", (getter)PyMAPIStoreDB_getParameter,
215           (setter)NULL, "firstorg", "firstorg"},
216         { "firstou", (getter)PyMAPIStoreDB_getParameter,
217           (setter)NULL, "firstou", "firstou"},
218         { NULL },
219 };
220
221 PyTypeObject PyMAPIStoreDB = {
222         PyObject_HEAD_INIT(NULL) 0,
223         .tp_name = "mapistoredb",
224         .tp_basicsize = sizeof (PyMAPIStoreDBObject),
225         .tp_methods = mapistoredb_methods,
226         .tp_getset = mapistoredb_getsetters,
227         .tp_doc = "mapistore database object",
228         .tp_new = py_MAPIStoreDB_new,
229         .tp_dealloc = (destructor) py_MAPIStoreDB_dealloc,
230         .tp_flags = Py_TPFLAGS_DEFAULT,
231 };
232
233 static PyMethodDef py_mapistoredb_global_methods[] = {
234         { NULL },
235 };
236
237 void initmapistoredb(void)
238 {
239         PyObject        *m;
240
241         if (PyType_Ready(&PyMAPIStoreDB) < 0) {
242                 return;
243         }
244
245         m = Py_InitModule3("mapistoredb", py_mapistoredb_global_methods,
246                            "An interface to MAPIStore database");
247
248         if (m == NULL) {
249                 return;
250         }
251
252         PyModule_AddObject(m, "MDB_ROOT_FOLDER", PyInt_FromLong((int)MDB_ROOT_FOLDER));
253         PyModule_AddObject(m, "MDB_DEFERRED_ACTIONS", PyInt_FromLong((int)MDB_DEFERRED_ACTIONS));
254         PyModule_AddObject(m, "MDB_SPOOLER_QUEUE", PyInt_FromLong((int)MDB_SPOOLER_QUEUE));
255         PyModule_AddObject(m, "MDB_TODO_SEARCH", PyInt_FromLong((int)MDB_TODO_SEARCH));
256         PyModule_AddObject(m, "MDB_IPM_SUBTREE", PyInt_FromLong((int)MDB_IPM_SUBTREE));
257         PyModule_AddObject(m, "MDB_INBOX", PyInt_FromLong((int)MDB_INBOX));
258         PyModule_AddObject(m, "MDB_OUTBOX", PyInt_FromLong((int)MDB_OUTBOX));
259         PyModule_AddObject(m, "MDB_SENT_ITEMS", PyInt_FromLong((int)MDB_SENT_ITEMS));
260         PyModule_AddObject(m, "MDB_DELETED_ITEMS", PyInt_FromLong((int)MDB_DELETED_ITEMS));
261         PyModule_AddObject(m, "MDB_COMMON_VIEWS", PyInt_FromLong((int)MDB_COMMON_VIEWS));
262         PyModule_AddObject(m, "MDB_SCHEDULE", PyInt_FromLong((int)MDB_SCHEDULE));
263         PyModule_AddObject(m, "MDB_SEARCH", PyInt_FromLong((int)MDB_SEARCH));
264         PyModule_AddObject(m, "MDB_VIEWS", PyInt_FromLong((int)MDB_VIEWS));
265         PyModule_AddObject(m, "MDB_SHORTCUTS", PyInt_FromLong((int)MDB_SHORTCUTS));
266         PyModule_AddObject(m, "MDB_REMINDERS", PyInt_FromLong((int)MDB_REMINDERS));
267         PyModule_AddObject(m, "MDB_CALENDAR", PyInt_FromLong((int)MDB_CALENDAR));
268         PyModule_AddObject(m, "MDB_CONTACTS", PyInt_FromLong((int)MDB_CONTACTS));
269         PyModule_AddObject(m, "MDB_JOURNAL", PyInt_FromLong((int)MDB_JOURNAL));
270         PyModule_AddObject(m, "MDB_NOTES", PyInt_FromLong((int)MDB_NOTES));
271         PyModule_AddObject(m, "MDB_TASKS", PyInt_FromLong((int)MDB_TASKS));
272         PyModule_AddObject(m, "MDB_DRAFTS", PyInt_FromLong((int)MDB_DRAFTS));
273         PyModule_AddObject(m, "MDB_TRACKED_MAIL", PyInt_FromLong((int)MDB_TRACKED_MAIL));
274         PyModule_AddObject(m, "MDB_SYNC_ISSUES", PyInt_FromLong((int)MDB_SYNC_ISSUES));
275         PyModule_AddObject(m, "MDB_CONFLICTS", PyInt_FromLong((int)MDB_CONFLICTS));
276         PyModule_AddObject(m, "MDB_LOCAL_FAILURES", PyInt_FromLong((int)MDB_LOCAL_FAILURES));
277         PyModule_AddObject(m, "MDB_SERVER_FAILURES", PyInt_FromLong((int)MDB_SERVER_FAILURES));
278         PyModule_AddObject(m, "MDB_JUNK_EMAIL", PyInt_FromLong((int)MDB_JUNK_EMAIL));
279         PyModule_AddObject(m, "MDB_RSS_FEEDS", PyInt_FromLong((int)MDB_RSS_FEEDS));
280         PyModule_AddObject(m, "MDB_CONVERSATION_ACT", PyInt_FromLong((int)MDB_CONVERSATION_ACT));
281         PyModule_AddObject(m, "MDB_LAST_SPECIALFOLDER", PyInt_FromLong((int)MDB_LAST_SPECIALFOLDER));
282         PyModule_AddObject(m, "MDB_CUSTOM", PyInt_FromLong((int)MDB_CUSTOM));
283         
284
285         PyModule_AddObject(m, "mapistoredb", (PyObject *)&PyMAPIStoreDB);
286 }