2 OpenChange MAPI implementation.
4 Python interface to mapistore database
6 Copyright (C) Julien Kerihuel 2010-2011.
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.
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.
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/>.
23 #include "pyopenchange/pymapistore.h"
25 void initmapistoredb(void);
27 static PyObject *py_MAPIStoreDB_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
30 struct mapistoredb_context *mdb_ctx;
31 PyMAPIStoreDBObject *mdbobj;
32 char *kwnames[] = { "path", NULL };
33 const char *path = NULL;
35 /* Path is optional */
36 PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwnames, &path);
38 mem_ctx = talloc_new(NULL);
39 if (mem_ctx == NULL) {
44 mdb_ctx = mapistoredb_init(mem_ctx, path);
45 if (mdb_ctx == NULL) {
46 DEBUG(0, ("mapistoredb_init returned NULL\n"));
50 mdbobj = PyObject_New(PyMAPIStoreDBObject, &PyMAPIStoreDB);
51 mdbobj->mem_ctx = mem_ctx;
52 mdbobj->mdb_ctx = mdb_ctx;
54 return (PyObject *) mdbobj;
57 static void py_MAPIStoreDB_dealloc(PyObject *_self)
59 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *)_self;
61 talloc_free(self->mem_ctx);
66 static PyObject *py_MAPIStoreDB_dump_configuration(PyMAPIStoreDBObject *self, PyObject *args)
68 mapistoredb_dump_conf(self->mdb_ctx);
69 return PyInt_FromLong(0);
72 static PyObject *py_MAPIStoreDB_provision(PyMAPIStoreDBObject *self, PyObject *args, PyObject *kwargs)
74 const char *netbiosname;
77 char *kwnames[] = { "netbiosname", "firstorg", "firstou", NULL };
79 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sss", kwnames, &netbiosname, &firstorg, &firstou)) {
83 mapistoredb_set_netbiosname(self->mdb_ctx, netbiosname);
84 mapistoredb_set_firstorg(self->mdb_ctx, firstorg);
85 mapistoredb_set_firstou(self->mdb_ctx, firstou);
87 return PyInt_FromLong(mapistoredb_provision(self->mdb_ctx));
91 static PyObject *py_MAPIStoreDB_get_mapistore_uri(PyObject *module, PyObject *args, PyObject *kwargs)
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;
102 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iss",
103 discard_const_p(char *, kwnames),
104 &dflt_folder, &username, &ns)) {
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);
118 static PyObject *py_MAPIStoreDB_get_new_fid(PyMAPIStoreDBObject *_self, PyObject *args)
120 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
121 enum MAPISTORE_ERROR retval;
125 if (!PyArg_ParseTuple(args, "s", &username)) {
129 retval = mapistoredb_get_new_fmid(self->mdb_ctx, (const char *)username, &fmid);
130 if (retval == MAPISTORE_SUCCESS) {
131 return PyLong_FromUnsignedLongLong(fmid);
137 static PyObject *py_MAPIStoreDB_get_new_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
139 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
140 enum MAPISTORE_ERROR retval;
143 uint64_t range_start = 0;
144 uint64_t range_end = 0;
146 if (!PyArg_ParseTuple(args, "sK", &username, &range)) {
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);
155 return Py_BuildValue("kKK", retval, range_start, range_start);
158 static PyObject *py_MAPIStoreDB_new_mailbox(PyMAPIStoreDBObject *_self, PyObject *args)
160 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
161 enum MAPISTORE_ERROR retval;
165 if (!PyArg_ParseTuple(args, "ss", &username, &mapistore_uri)) {
169 retval = mapistoredb_register_new_mailbox(self->mdb_ctx, username, mapistore_uri);
170 return PyInt_FromLong(retval);
173 static PyObject *py_MAPIStoreDB_set_mailbox_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
175 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
176 enum MAPISTORE_ERROR retval;
181 if (!PyArg_ParseTuple(args, "sKK", &username, &rstart, &rend)) {
185 retval = mapistoredb_register_new_mailbox_allocation_range(self->mdb_ctx, username, rstart, rend);
186 return PyInt_FromLong(retval);
189 static PyObject *py_MAPIStoreDB_release(PyMAPIStoreDBObject *_self, PyObject *args)
191 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
193 mapistoredb_release(self->mdb_ctx);
194 return PyInt_FromLong(MAPISTORE_SUCCESS);
197 static PyObject *PyMAPIStoreDB_getParameter(PyObject *_self, void *data)
199 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
200 const char *attr = (const char *) data;
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));
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 },
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"},
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,
247 static PyMethodDef py_mapistoredb_global_methods[] = {
251 void initmapistoredb(void)
255 if (PyType_Ready(&PyMAPIStoreDB) < 0) {
259 m = Py_InitModule3("mapistoredb", py_mapistoredb_global_methods,
260 "An interface to MAPIStore database");
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));
299 Py_INCREF(&PyMAPIStoreDB);
300 PyModule_AddObject(m, "mapistoredb", (PyObject *)&PyMAPIStoreDB);