9343c2ed9d70d28583fed2b8a4ee2f9adfba5d02
[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(PyObject *module, PyObject *args, PyObject *kwargs)
92 {
93         PyObject                        *ret;
94         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) module;
95         const char * const              kwnames[] = { "folder", "username", "namespace", NULL };
96         enum MAPISTORE_ERROR            retval;
97         enum MAPISTORE_DFLT_FOLDERS     dflt_folder;
98         const char                      *username;
99         const char                      *ns;
100         char                            *uri;
101
102         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iss", 
103                                          discard_const_p(char *, kwnames), 
104                                          &dflt_folder, &username, &ns)) {
105                 return NULL;
106         }
107
108         retval = mapistoredb_get_mapistore_uri(self->mdb_ctx, dflt_folder, ns, username, &uri);
109         if (retval == MAPISTORE_SUCCESS && uri != NULL) {
110                 ret = PyString_FromString(uri);
111                 return ret;
112         }
113
114         return NULL;
115 }
116
117
118 static PyObject *py_MAPIStoreDB_get_new_fid(PyMAPIStoreDBObject *_self, PyObject *args)
119 {
120         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
121         enum MAPISTORE_ERROR            retval;
122         uint64_t                        fmid = 0;
123         char                            *username;
124
125         if (!PyArg_ParseTuple(args, "s", &username)) {
126                 return NULL;
127         }
128
129         retval = mapistoredb_get_new_fmid(self->mdb_ctx, (const char *)username, &fmid);
130         if (retval == MAPISTORE_SUCCESS) {
131                 return PyLong_FromUnsignedLongLong(fmid);
132         }
133
134         return NULL;
135 }
136
137 static PyObject *py_MAPIStoreDB_get_new_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
138 {
139         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
140         enum MAPISTORE_ERROR            retval;
141         char                            *username;
142         uint64_t                        range;
143         uint64_t                        range_start = 0;
144         uint64_t                        range_end = 0;
145
146         if (!PyArg_ParseTuple(args, "sK", &username, &range)) {
147                 return NULL;
148         }
149
150         retval = mapistoredb_get_new_allocation_range(self->mdb_ctx, (const char *)username, range, &range_start, &range_end);
151         if (retval == MAPISTORE_SUCCESS) {
152                 return Py_BuildValue("kKK", retval, range_start, range_end);
153         }
154
155         return Py_BuildValue("kKK", retval, range_start, range_start);
156 }
157
158 static PyObject *py_MAPIStoreDB_new_mailbox(PyMAPIStoreDBObject *_self, PyObject *args)
159 {
160         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
161         enum MAPISTORE_ERROR            retval;
162         char                            *username;
163         char                            *mapistore_uri;
164
165         if (!PyArg_ParseTuple(args, "ss", &username, &mapistore_uri)) {
166                 return NULL;
167         }
168
169         retval = mapistoredb_register_new_mailbox(self->mdb_ctx, username, mapistore_uri);
170         return PyInt_FromLong(retval);
171 }
172
173 static PyObject *py_MAPIStoreDB_set_mailbox_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
174 {
175         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
176         enum MAPISTORE_ERROR            retval;
177         uint64_t                        rstart;
178         uint64_t                        rend;
179         char                            *username;
180
181         if (!PyArg_ParseTuple(args, "sKK", &username, &rstart, &rend)) {
182                 return NULL;
183         }
184
185         retval = mapistoredb_register_new_mailbox_allocation_range(self->mdb_ctx, username, rstart, rend);
186         return PyInt_FromLong(retval);
187 }
188
189 static PyObject *py_MAPIStoreDB_release(PyMAPIStoreDBObject *_self, PyObject *args)
190 {
191         PyMAPIStoreDBObject             *self = (PyMAPIStoreDBObject *) _self;
192
193         mapistoredb_release(self->mdb_ctx);
194         return PyInt_FromLong(MAPISTORE_SUCCESS);
195 }
196
197 static PyObject *PyMAPIStoreDB_getParameter(PyObject *_self, void *data)
198 {
199         PyMAPIStoreDBObject     *self = (PyMAPIStoreDBObject *) _self;
200         const char              *attr = (const char *) data;
201
202         if (!strcmp(attr, "netbiosname")) {
203                 return PyString_FromString(mapistoredb_get_netbiosname(self->mdb_ctx));
204         } else if (!strcmp(attr, "firstorg")) {
205                 return PyString_FromString(mapistoredb_get_firstorg(self->mdb_ctx));
206         } else if (!strcmp(attr, "firstou")) {
207                 return PyString_FromString(mapistoredb_get_firstou(self->mdb_ctx));
208         }
209
210         return NULL;
211 }
212
213 static PyMethodDef mapistoredb_methods[] = {
214         { "dump_configuration", (PyCFunction)py_MAPIStoreDB_dump_configuration, METH_VARARGS },
215         { "provision", (PyCFunction)py_MAPIStoreDB_provision, METH_KEYWORDS },
216         { "get_mapistore_uri", (PyCFunction)py_MAPIStoreDB_get_mapistore_uri, METH_KEYWORDS },
217         { "get_new_fid", (PyCFunction)py_MAPIStoreDB_get_new_fid, METH_VARARGS },
218         { "get_new_allocation_range", (PyCFunction)py_MAPIStoreDB_get_new_allocation_range, METH_VARARGS },
219         { "new_mailbox", (PyCFunction)py_MAPIStoreDB_new_mailbox, METH_VARARGS },
220         { "set_mailbox_allocation_range", (PyCFunction)py_MAPIStoreDB_set_mailbox_allocation_range, METH_VARARGS },
221         { "release", (PyCFunction)py_MAPIStoreDB_release, METH_VARARGS },
222         { NULL },
223 };
224
225 static PyGetSetDef mapistoredb_getsetters[] = {
226         { "netbiosname", (getter)PyMAPIStoreDB_getParameter,
227           (setter)NULL, "netbiosname", "netbiosname"},
228         { "firstorg", (getter)PyMAPIStoreDB_getParameter,
229           (setter)NULL, "firstorg", "firstorg"},
230         { "firstou", (getter)PyMAPIStoreDB_getParameter,
231           (setter)NULL, "firstou", "firstou"},
232         { NULL },
233 };
234
235 PyTypeObject PyMAPIStoreDB = {
236         PyObject_HEAD_INIT(NULL) 0,
237         .tp_name = "mapistoredb",
238         .tp_basicsize = sizeof (PyMAPIStoreDBObject),
239         .tp_methods = mapistoredb_methods,
240         .tp_getset = mapistoredb_getsetters,
241         .tp_doc = "mapistore database object",
242         .tp_new = py_MAPIStoreDB_new,
243         .tp_dealloc = (destructor) py_MAPIStoreDB_dealloc,
244         .tp_flags = Py_TPFLAGS_DEFAULT,
245 };
246
247 static PyMethodDef py_mapistoredb_global_methods[] = {
248         { NULL },
249 };
250
251 void initmapistoredb(void)
252 {
253         PyObject        *m;
254
255         if (PyType_Ready(&PyMAPIStoreDB) < 0) {
256                 return;
257         }
258
259         m = Py_InitModule3("mapistoredb", py_mapistoredb_global_methods,
260                            "An interface to MAPIStore database");
261
262         if (m == NULL) {
263                 return;
264         }
265
266         PyModule_AddObject(m, "MDB_ROOT_FOLDER", PyInt_FromLong((int)MDB_ROOT_FOLDER));
267         PyModule_AddObject(m, "MDB_DEFERRED_ACTIONS", PyInt_FromLong((int)MDB_DEFERRED_ACTIONS));
268         PyModule_AddObject(m, "MDB_SPOOLER_QUEUE", PyInt_FromLong((int)MDB_SPOOLER_QUEUE));
269         PyModule_AddObject(m, "MDB_TODO_SEARCH", PyInt_FromLong((int)MDB_TODO_SEARCH));
270         PyModule_AddObject(m, "MDB_IPM_SUBTREE", PyInt_FromLong((int)MDB_IPM_SUBTREE));
271         PyModule_AddObject(m, "MDB_INBOX", PyInt_FromLong((int)MDB_INBOX));
272         PyModule_AddObject(m, "MDB_OUTBOX", PyInt_FromLong((int)MDB_OUTBOX));
273         PyModule_AddObject(m, "MDB_SENT_ITEMS", PyInt_FromLong((int)MDB_SENT_ITEMS));
274         PyModule_AddObject(m, "MDB_DELETED_ITEMS", PyInt_FromLong((int)MDB_DELETED_ITEMS));
275         PyModule_AddObject(m, "MDB_COMMON_VIEWS", PyInt_FromLong((int)MDB_COMMON_VIEWS));
276         PyModule_AddObject(m, "MDB_SCHEDULE", PyInt_FromLong((int)MDB_SCHEDULE));
277         PyModule_AddObject(m, "MDB_SEARCH", PyInt_FromLong((int)MDB_SEARCH));
278         PyModule_AddObject(m, "MDB_VIEWS", PyInt_FromLong((int)MDB_VIEWS));
279         PyModule_AddObject(m, "MDB_SHORTCUTS", PyInt_FromLong((int)MDB_SHORTCUTS));
280         PyModule_AddObject(m, "MDB_REMINDERS", PyInt_FromLong((int)MDB_REMINDERS));
281         PyModule_AddObject(m, "MDB_CALENDAR", PyInt_FromLong((int)MDB_CALENDAR));
282         PyModule_AddObject(m, "MDB_CONTACTS", PyInt_FromLong((int)MDB_CONTACTS));
283         PyModule_AddObject(m, "MDB_JOURNAL", PyInt_FromLong((int)MDB_JOURNAL));
284         PyModule_AddObject(m, "MDB_NOTES", PyInt_FromLong((int)MDB_NOTES));
285         PyModule_AddObject(m, "MDB_TASKS", PyInt_FromLong((int)MDB_TASKS));
286         PyModule_AddObject(m, "MDB_DRAFTS", PyInt_FromLong((int)MDB_DRAFTS));
287         PyModule_AddObject(m, "MDB_TRACKED_MAIL", PyInt_FromLong((int)MDB_TRACKED_MAIL));
288         PyModule_AddObject(m, "MDB_SYNC_ISSUES", PyInt_FromLong((int)MDB_SYNC_ISSUES));
289         PyModule_AddObject(m, "MDB_CONFLICTS", PyInt_FromLong((int)MDB_CONFLICTS));
290         PyModule_AddObject(m, "MDB_LOCAL_FAILURES", PyInt_FromLong((int)MDB_LOCAL_FAILURES));
291         PyModule_AddObject(m, "MDB_SERVER_FAILURES", PyInt_FromLong((int)MDB_SERVER_FAILURES));
292         PyModule_AddObject(m, "MDB_JUNK_EMAIL", PyInt_FromLong((int)MDB_JUNK_EMAIL));
293         PyModule_AddObject(m, "MDB_RSS_FEEDS", PyInt_FromLong((int)MDB_RSS_FEEDS));
294         PyModule_AddObject(m, "MDB_CONVERSATION_ACT", PyInt_FromLong((int)MDB_CONVERSATION_ACT));
295         PyModule_AddObject(m, "MDB_LAST_SPECIALFOLDER", PyInt_FromLong((int)MDB_LAST_SPECIALFOLDER));
296         PyModule_AddObject(m, "MDB_CUSTOM", PyInt_FromLong((int)MDB_CUSTOM));
297         
298
299         Py_INCREF(&PyMAPIStoreDB);
300         PyModule_AddObject(m, "mapistoredb", (PyObject *)&PyMAPIStoreDB);
301 }