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;
33
mem_ctx = talloc_new(NULL);
34
if (mem_ctx == NULL) {
39
mdb_ctx = mapistoredb_new(mem_ctx);
40
if (mdb_ctx == NULL) {
41
PyErr_SetString(PyExc_TypeError, "mapistoredb initialization failed");
45
mdbobj = PyObject_New(PyMAPIStoreDBObject, &PyMAPIStoreDB);
46
mdbobj->mem_ctx = mem_ctx;
47
mdbobj->mdb_ctx = mdb_ctx;
49
return (PyObject *) mdbobj;
52
static void py_MAPIStoreDB_dealloc(PyObject *_self)
54
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *)_self;
56
talloc_free(self->mem_ctx);
61
static PyObject *py_MAPIStoreDB_dump_configuration(PyMAPIStoreDBObject *self, PyObject *args)
63
mapistoredb_dump_conf(self->mdb_ctx);
64
return PyInt_FromLong(0);
67
static PyObject *py_MAPIStoreDB_initialize(PyMAPIStoreDBObject *self, PyObject *args, PyObject *kwargs)
69
enum MAPISTORE_ERROR retval;
70
const char *mpath = NULL;
71
char *kwnames[] = { "path", NULL };
73
/* Path is optional */
74
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z", kwnames, &mpath)) {
75
PyErr_SetString(PyExc_TypeError, "Invalid parameter");
79
retval = mapistoredb_init(self->mdb_ctx, mpath);
81
PyErr_SetString(PyExc_TypeError, "mapistoredb initialization failed");
83
return PyInt_FromLong(retval);
86
static PyObject *py_MAPIStoreDB_provision(PyMAPIStoreDBObject *self, PyObject *args, PyObject *kwargs)
88
const char *netbiosname;
91
char *kwnames[] = { "netbiosname", "firstorg", "firstou", NULL };
93
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sss", kwnames, &netbiosname, &firstorg, &firstou)) {
97
mapistoredb_set_netbiosname(self->mdb_ctx, netbiosname);
98
mapistoredb_set_firstorg(self->mdb_ctx, firstorg);
99
mapistoredb_set_firstou(self->mdb_ctx, firstou);
101
return PyInt_FromLong(mapistoredb_provision(self->mdb_ctx));
104
static PyObject *py_MAPIStoreDB_provision_named_properties(PyMAPIStoreDBObject *self, PyObject *args)
106
return PyInt_FromLong(mapistoredb_namedprops_provision(self->mdb_ctx));
109
static PyObject *py_MAPIStoreDB_get_mapistore_uri(PyObject *module, PyObject *args, PyObject *kwargs)
112
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) module;
113
const char * const kwnames[] = { "folder", "username", "namespace", NULL };
114
enum MAPISTORE_ERROR retval;
115
enum MAPISTORE_DFLT_FOLDERS dflt_folder;
117
const char *username;
121
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iss",
122
discard_const_p(char *, kwnames),
123
&folder_int, &username, &ns)) {
127
dflt_folder = (enum MAPISTORE_DFLT_FOLDERS)folder_int;
128
retval = mapistoredb_get_mapistore_uri(self->mdb_ctx, dflt_folder, ns, username, &uri);
129
if (retval == MAPISTORE_SUCCESS && uri != NULL) {
130
ret = PyString_FromString(uri);
138
static PyObject *py_MAPIStoreDB_get_new_fid(PyMAPIStoreDBObject *_self, PyObject *args)
140
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
141
enum MAPISTORE_ERROR retval;
145
if (!PyArg_ParseTuple(args, "s", &username)) {
149
retval = mapistoredb_get_new_fmid(self->mdb_ctx, (const char *)username, &fmid);
150
if (retval == MAPISTORE_SUCCESS) {
151
return PyLong_FromUnsignedLongLong(fmid);
157
static PyObject *py_MAPIStoreDB_get_new_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
159
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
160
enum MAPISTORE_ERROR retval;
163
uint64_t range_start = 0;
164
uint64_t range_end = 0;
166
if (!PyArg_ParseTuple(args, "sK", &username, &range)) {
170
retval = mapistoredb_get_new_allocation_range(self->mdb_ctx, (const char *)username, range, &range_start, &range_end);
171
if (retval == MAPISTORE_SUCCESS) {
172
return Py_BuildValue("kKK", retval, range_start, range_end);
175
return Py_BuildValue("kKK", retval, range_start, range_start);
178
static PyObject *py_MAPIStoreDB_new_mailbox(PyMAPIStoreDBObject *_self, PyObject *args)
180
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
181
enum MAPISTORE_ERROR retval;
185
if (!PyArg_ParseTuple(args, "ss", &username, &mapistore_uri)) {
189
retval = mapistoredb_register_new_mailbox(self->mdb_ctx, username, mapistore_uri);
190
return PyInt_FromLong(retval);
193
static PyObject *py_MAPIStoreDB_set_mailbox_allocation_range(PyMAPIStoreDBObject *_self, PyObject *args)
195
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
196
enum MAPISTORE_ERROR retval;
201
if (!PyArg_ParseTuple(args, "sKK", &username, &rstart, &rend)) {
205
retval = mapistoredb_register_new_mailbox_allocation_range(self->mdb_ctx, username, rstart, rend);
206
return PyInt_FromLong(retval);
209
static PyObject *py_MAPIStoreDB_release(PyMAPIStoreDBObject *_self, PyObject *args)
211
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
213
mapistoredb_release(self->mdb_ctx);
214
return PyInt_FromLong(MAPISTORE_SUCCESS);
217
static PyObject *py_MAPIStoreDB_errstr(PyMAPIStoreDBObject *_self, PyObject *args)
219
enum MAPISTORE_ERROR retval;
222
if (!PyArg_ParseTuple(args, "i", &ret)) {
226
retval = (enum MAPISTORE_ERROR) ret;
227
return PyString_FromString(mapistore_errstr(retval));
230
static PyObject *py_MAPIStoreDB_namedprops_get_default_id(PyMAPIStoreDBObject *_self, PyObject *args, PyObject *kwargs)
232
enum MAPISTORE_ERROR retval;
233
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
234
const char * const kwnames[] = { "type", NULL };
236
enum MAPISTORE_NAMEDPROPS_TYPE ntype;
239
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i",
240
discard_const_p(char *, kwnames),
245
ntype = (enum MAPISTORE_NAMEDPROPS_TYPE) type;
246
retval = mapistore_namedprops_get_default_id(self->mdb_ctx->mstore_ctx, ntype, &dflt_id);
248
return Py_BuildValue("ii", retval, dflt_id);
251
static PyObject *py_MAPIStoreDB_namedprops_provision_backends(PyMAPIStoreDBObject *_self, PyObject *args)
253
enum MAPISTORE_ERROR retval;
254
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
256
retval = mapistoredb_namedprops_provision_backends(self->mdb_ctx);
257
return PyInt_FromLong(retval);
260
static PyObject *py_MAPIStoreDB_namedprops_provision_user(PyMAPIStoreDBObject *_self, PyObject *args, PyObject *kwargs)
262
enum MAPISTORE_ERROR retval;
263
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
264
const char * const kwnames[] = { "username", NULL };
267
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s",
268
discard_const_p(char *, kwnames),
273
retval = mapistoredb_namedprops_provision_user(self->mdb_ctx, username);
275
return PyInt_FromLong(retval);
278
static PyObject *PyMAPIStoreDB_getParameter(PyObject *_self, void *data)
280
PyMAPIStoreDBObject *self = (PyMAPIStoreDBObject *) _self;
281
const char *attr = (const char *) data;
283
if (!strcmp(attr, "netbiosname")) {
284
return PyString_FromString(mapistoredb_get_netbiosname(self->mdb_ctx));
285
} else if (!strcmp(attr, "firstorg")) {
286
return PyString_FromString(mapistoredb_get_firstorg(self->mdb_ctx));
287
} else if (!strcmp(attr, "firstou")) {
288
return PyString_FromString(mapistoredb_get_firstou(self->mdb_ctx));
289
} else if (!strcmp(attr, "mapping_path")) {
290
return PyString_FromString(mapistore_get_mapping_path());
291
} else if (!strcmp(attr, "database_path")) {
292
return PyString_FromString(mapistore_get_database_path());
293
} else if (!strcmp(attr, "nprops_database_path")) {
294
return PyString_FromString(mapistore_get_named_properties_database_path());
297
PyErr_SetString(PyExc_TypeError, "Invalid parameter");
302
static int PyMAPIStoreDB_set_dbpath(PyMAPIStoreDBObject *self, PyObject *value, void *py_data)
304
enum MAPISTORE_ERROR retval;
307
if (self->mdb_ctx->mstore_ctx) {
308
PyErr_SetString(PyExc_AssertionError, "Already initialized");
312
if (!PyString_Check(value)) {
313
PyErr_SetString(PyExc_ValueError, "The attribute must be a string");
317
dbpath = PyString_AsString(value);
319
retval = mapistoredb_set_database_path(self->mdb_ctx, dbpath);
320
if (retval) return -1;
326
static int PyMAPIStoreDB_set_mapping_path(PyMAPIStoreDBObject *self, PyObject *value, void *py_data)
328
enum MAPISTORE_ERROR retval;
331
if (self->mdb_ctx->mstore_ctx) {
332
PyErr_SetString(PyExc_AssertionError, "Already initialized");
336
if (!PyString_Check(value)) {
337
PyErr_SetString(PyExc_ValueError, "The attribute must be a string");
341
mapping_path = PyString_AsString(value);
343
retval = mapistoredb_set_mapping_path(self->mdb_ctx, mapping_path);
344
if (retval) return -1;
350
static int PyMAPIStoreDB_set_nprops_dbpath(PyMAPIStoreDBObject *self, PyObject *value, void *py_data)
352
enum MAPISTORE_ERROR retval;
355
if (self->mdb_ctx->mstore_ctx) {
356
PyErr_SetString(PyExc_AssertionError, "Already initialized");
360
if (!PyString_Check(value)) {
361
PyErr_SetString(PyExc_ValueError, "The attribute must be a string");
365
nprops_dbpath = PyString_AsString(value);
367
retval = mapistoredb_set_named_properties_database_path(self->mdb_ctx, nprops_dbpath);
368
if (retval) return -1;
374
static PyMethodDef mapistoredb_methods[] = {
375
{ "initialize", (PyCFunction)py_MAPIStoreDB_initialize, METH_KEYWORDS },
376
{ "dump_configuration", (PyCFunction)py_MAPIStoreDB_dump_configuration, METH_VARARGS },
377
{ "provision", (PyCFunction)py_MAPIStoreDB_provision, METH_KEYWORDS },
378
{ "get_mapistore_uri", (PyCFunction)py_MAPIStoreDB_get_mapistore_uri, METH_KEYWORDS },
379
{ "get_new_fid", (PyCFunction)py_MAPIStoreDB_get_new_fid, METH_VARARGS },
380
{ "get_new_allocation_range", (PyCFunction)py_MAPIStoreDB_get_new_allocation_range, METH_VARARGS },
381
{ "new_mailbox", (PyCFunction)py_MAPIStoreDB_new_mailbox, METH_VARARGS },
382
{ "set_mailbox_allocation_range", (PyCFunction)py_MAPIStoreDB_set_mailbox_allocation_range, METH_VARARGS },
383
{ "release", (PyCFunction)py_MAPIStoreDB_release, METH_VARARGS },
384
{ "errstr", (PyCFunction)py_MAPIStoreDB_errstr, METH_VARARGS },
385
/* named properties functions */
386
{ "provision_named_properties", (PyCFunction)py_MAPIStoreDB_provision_named_properties, METH_VARARGS },
387
{ "namedprops_get_default_id", (PyCFunction)py_MAPIStoreDB_namedprops_get_default_id, METH_KEYWORDS },
388
{ "namedprops_provision_user", (PyCFunction)py_MAPIStoreDB_namedprops_provision_user, METH_KEYWORDS },
389
{ "namedprops_provision_backends", (PyCFunction)py_MAPIStoreDB_namedprops_provision_backends, METH_VARARGS },
393
static PyGetSetDef mapistoredb_getsetters[] = {
394
{ "netbiosname", (getter)PyMAPIStoreDB_getParameter,
395
(setter)NULL, "netbiosname", "netbiosname"},
396
{ "firstorg", (getter)PyMAPIStoreDB_getParameter,
397
(setter)NULL, "firstorg", "firstorg"},
398
{ "firstou", (getter)PyMAPIStoreDB_getParameter,
399
(setter)NULL, "firstou", "firstou"},
400
{ "mapping_path", (getter)PyMAPIStoreDB_getParameter,
401
(setter)PyMAPIStoreDB_set_mapping_path, "mapping_path", "mapping_path" },
402
{ "database_path", (getter)PyMAPIStoreDB_getParameter,
403
(setter)PyMAPIStoreDB_set_dbpath, "database_path", "database_path" },
404
{ "namedprops_database_path", (getter)PyMAPIStoreDB_getParameter,
405
(setter)PyMAPIStoreDB_set_nprops_dbpath, "namedprops_database_path", "namedprops_database_path" },
409
PyTypeObject PyMAPIStoreDB = {
410
PyObject_HEAD_INIT(NULL) 0,
411
.tp_name = "mapistoredb",
412
.tp_basicsize = sizeof (PyMAPIStoreDBObject),
413
.tp_methods = mapistoredb_methods,
414
.tp_getset = mapistoredb_getsetters,
415
.tp_doc = "mapistore database object",
416
.tp_new = py_MAPIStoreDB_new,
417
.tp_dealloc = (destructor) py_MAPIStoreDB_dealloc,
418
.tp_flags = Py_TPFLAGS_DEFAULT,
421
static PyMethodDef py_mapistoredb_global_methods[] = {
425
void initmapistoredb(void)
429
if (PyType_Ready(&PyMAPIStoreDB) < 0) {
433
m = Py_InitModule3("mapistoredb", py_mapistoredb_global_methods,
434
"An interface to MAPIStore database");
440
PyModule_AddObject(m, "MDB_ROOT_FOLDER", PyInt_FromLong((int)MDB_ROOT_FOLDER));
441
PyModule_AddObject(m, "MDB_DEFERRED_ACTIONS", PyInt_FromLong((int)MDB_DEFERRED_ACTIONS));
442
PyModule_AddObject(m, "MDB_SPOOLER_QUEUE", PyInt_FromLong((int)MDB_SPOOLER_QUEUE));
443
PyModule_AddObject(m, "MDB_TODO_SEARCH", PyInt_FromLong((int)MDB_TODO_SEARCH));
444
PyModule_AddObject(m, "MDB_IPM_SUBTREE", PyInt_FromLong((int)MDB_IPM_SUBTREE));
445
PyModule_AddObject(m, "MDB_INBOX", PyInt_FromLong((int)MDB_INBOX));
446
PyModule_AddObject(m, "MDB_OUTBOX", PyInt_FromLong((int)MDB_OUTBOX));
447
PyModule_AddObject(m, "MDB_SENT_ITEMS", PyInt_FromLong((int)MDB_SENT_ITEMS));
448
PyModule_AddObject(m, "MDB_DELETED_ITEMS", PyInt_FromLong((int)MDB_DELETED_ITEMS));
449
PyModule_AddObject(m, "MDB_COMMON_VIEWS", PyInt_FromLong((int)MDB_COMMON_VIEWS));
450
PyModule_AddObject(m, "MDB_SCHEDULE", PyInt_FromLong((int)MDB_SCHEDULE));
451
PyModule_AddObject(m, "MDB_SEARCH", PyInt_FromLong((int)MDB_SEARCH));
452
PyModule_AddObject(m, "MDB_VIEWS", PyInt_FromLong((int)MDB_VIEWS));
453
PyModule_AddObject(m, "MDB_SHORTCUTS", PyInt_FromLong((int)MDB_SHORTCUTS));
454
PyModule_AddObject(m, "MDB_REMINDERS", PyInt_FromLong((int)MDB_REMINDERS));
455
PyModule_AddObject(m, "MDB_CALENDAR", PyInt_FromLong((int)MDB_CALENDAR));
456
PyModule_AddObject(m, "MDB_CONTACTS", PyInt_FromLong((int)MDB_CONTACTS));
457
PyModule_AddObject(m, "MDB_JOURNAL", PyInt_FromLong((int)MDB_JOURNAL));
458
PyModule_AddObject(m, "MDB_NOTES", PyInt_FromLong((int)MDB_NOTES));
459
PyModule_AddObject(m, "MDB_TASKS", PyInt_FromLong((int)MDB_TASKS));
460
PyModule_AddObject(m, "MDB_DRAFTS", PyInt_FromLong((int)MDB_DRAFTS));
461
PyModule_AddObject(m, "MDB_TRACKED_MAIL", PyInt_FromLong((int)MDB_TRACKED_MAIL));
462
PyModule_AddObject(m, "MDB_SYNC_ISSUES", PyInt_FromLong((int)MDB_SYNC_ISSUES));
463
PyModule_AddObject(m, "MDB_CONFLICTS", PyInt_FromLong((int)MDB_CONFLICTS));
464
PyModule_AddObject(m, "MDB_LOCAL_FAILURES", PyInt_FromLong((int)MDB_LOCAL_FAILURES));
465
PyModule_AddObject(m, "MDB_SERVER_FAILURES", PyInt_FromLong((int)MDB_SERVER_FAILURES));
466
PyModule_AddObject(m, "MDB_JUNK_EMAIL", PyInt_FromLong((int)MDB_JUNK_EMAIL));
467
PyModule_AddObject(m, "MDB_RSS_FEEDS", PyInt_FromLong((int)MDB_RSS_FEEDS));
468
PyModule_AddObject(m, "MDB_CONVERSATION_ACT", PyInt_FromLong((int)MDB_CONVERSATION_ACT));
469
PyModule_AddObject(m, "MDB_LAST_SPECIALFOLDER", PyInt_FromLong((int)MDB_LAST_SPECIALFOLDER));
470
PyModule_AddObject(m, "MDB_CUSTOM", PyInt_FromLong((int)MDB_CUSTOM));
472
PyModule_AddObject(m, "MAPISTORE_NAMEDPROPS_INTERNAL", PyInt_FromLong((int)MAPISTORE_NAMEDPROPS_INTERNAL));
473
PyModule_AddObject(m, "MAPISTORE_NAMEDPROPS_EXTERNAL", PyInt_FromLong((int)MAPISTORE_NAMEDPROPS_EXTERNAL));
475
Py_INCREF(&PyMAPIStoreDB);
476
PyModule_AddObject(m, "mapistoredb", (PyObject *)&PyMAPIStoreDB);