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(PyMAPIStoreDBObject *_self, PyObject *args)
93 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
94 enum MAPISTORE_ERROR retval;
95 enum MAPISTORE_DFLT_FOLDERS dflt_folder;
100 if (!PyArg_ParseTuple(args, "kss", &dflt_folder, &username, &ns)) {
104 retval = mapistoredb_get_mapistore_uri(self->mdb_ctx, dflt_folder, ns, username, &uri);
105 if (retval == MAPISTORE_SUCCESS) {
106 return PyString_FromString(uri);
113 static PyObject *py_MAPIStoreDB_get_new_fid(PyMAPIStoreDBObject *_self, PyObject *args)
115 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
116 enum MAPISTORE_ERROR retval;
120 if (!PyArg_ParseTuple(args, "s", &username)) {
124 retval = mapistoredb_get_new_fmid(self->mdb_ctx, (const char *)username, &fmid);
125 if (retval == MAPISTORE_SUCCESS) {
126 return PyLong_FromUnsignedLongLong(fmid);
132 static PyObject *py_MAPIStoreDB_get_new_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
134 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
135 enum MAPISTORE_ERROR retval;
138 uint64_t range_start = 0;
139 uint64_t range_end = 0;
141 if (!PyArg_ParseTuple(args, "sK", &username, &range)) {
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);
150 return Py_BuildValue("kKK", retval, range_start, range_start);
153 static PyObject *py_MAPIStoreDB_new_mailbox(PyMAPIStoreDBObject *_self, PyObject *args)
155 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
156 enum MAPISTORE_ERROR retval;
160 if (!PyArg_ParseTuple(args, "ss", &username, &mapistore_uri)) {
164 retval = mapistoredb_register_new_mailbox(self->mdb_ctx, username, mapistore_uri);
165 return PyInt_FromLong(retval);
168 static PyObject *py_MAPIStoreDB_set_mailbox_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
170 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
171 enum MAPISTORE_ERROR retval;
176 if (!PyArg_ParseTuple(args, "sKK", &username, &rstart, &rend)) {
180 retval = mapistoredb_register_new_mailbox_allocation_range(self->mdb_ctx, username, rstart, rend);
181 return PyInt_FromLong(retval);
184 static PyObject *PyMAPIStoreDB_getParameter(PyObject *_self, void *data)
186 PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
187 const char *attr = (const char *) data;
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));
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 },
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"},
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,
233 static PyMethodDef py_mapistoredb_global_methods[] = {
237 void initmapistoredb(void)
241 if (PyType_Ready(&PyMAPIStoreDB) < 0) {
245 m = Py_InitModule3("mapistoredb", py_mapistoredb_global_methods,
246 "An interface to MAPIStore database");
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));
285 PyModule_AddObject(m, "mapistoredb", (PyObject *)&PyMAPIStoreDB);