Merge in sogo branch changes r3949-3962
[jelmer/openchange.git] / pyopenchange / mapistore / mgmt.c
1 /*
2    OpenChange MAPI implementation.
3
4    Python interface to mapistore management
5
6    Copyright (C) Julien Kerihuel 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/mapistore/pymapistore.h"
24
25 static void py_MAPIStoreMGMT_dealloc(PyObject *_self)
26 {
27         PyMAPIStoreMGMTObject *self = (PyMAPIStoreMGMTObject *)_self;
28
29         printf("deallocate MGMT object\n");
30         mapistore_mgmt_release(self->mgmt_ctx);
31
32         Py_XDECREF(self->parent);
33         PyObject_Del(_self);
34 }
35
36 static PyObject *py_MAPIStoreMGMT_registered_backend(PyMAPIStoreMGMTObject *self, PyObject *args)
37 {
38         int             ret;
39         const char      *bname;
40
41         if (!PyArg_ParseTuple(args, "s", &bname)) {
42                 return NULL;
43         }
44
45         ret = mapistore_mgmt_registered_backend(self->mgmt_ctx, bname);
46         return PyBool_FromLong(ret == MAPISTORE_SUCCESS ? true : false);
47
48 }
49
50 static PyObject *py_MAPIStoreMGMT_registered_users(PyMAPIStoreMGMTObject *self, PyObject *args)
51 {
52         PyObject                                *dict;
53         PyObject                                *userlist;
54         const char                              *backend;
55         const char                              *vuser;
56         struct mapistore_mgmt_users_list        *ulist;
57         int                                     i;
58
59         if (!PyArg_ParseTuple(args, "ss", &backend, &vuser)) {
60                 return NULL;
61         }
62
63         dict = PyDict_New();
64         PyDict_SetItemString(dict, "backend", PyString_FromString(backend));
65         PyDict_SetItemString(dict, "user", PyString_FromString(vuser));
66         
67         ulist = mapistore_mgmt_registered_users(self->mgmt_ctx, backend, vuser);
68         userlist = PyList_New(0);
69
70         if (ulist && ulist->count != 0) {
71                 PyDict_SetItem(dict, PyString_FromString("count"), PyLong_FromLong(ulist->count));
72                 for (i = 0; i < ulist->count; i++) {
73                         PyList_Append(userlist, PyString_FromString(ulist->user[i]));
74                 }
75         } else {
76                 PyDict_SetItem(dict, PyString_FromString("count"), PyLong_FromLong(0));
77         }
78         PyDict_SetItem(dict, PyString_FromString("usernames"), userlist);
79
80         if (ulist) {
81                 talloc_free(ulist);
82         }
83
84         return (PyObject *)dict;
85 }
86
87 static PyObject *py_MAPIStoreMGMT_registered_message(PyMAPIStoreMGMTObject *self, PyObject *args)
88 {
89         const char      *backend;
90         const char      *sysuser;
91         const char      *vuser;
92         const char      *folder;
93         const char      *message;
94         const char      *rootURI;
95         int             ret;
96
97         if (!PyArg_ParseTuple(args, "ssszzs", &backend, &sysuser, &vuser, &folder, &rootURI, &message)) {
98                 return NULL;
99         }
100
101         ret = mapistore_mgmt_registered_message(self->mgmt_ctx, backend, sysuser, vuser, folder, rootURI, message);
102
103         return PyBool_FromLong(ret);
104 }
105
106 static PyObject *py_MAPIStoreMGMT_register_message(PyMAPIStoreMGMTObject *self, PyObject *args)
107 {
108         const char      *backend;
109         const char      *user;
110         const char      *uri;
111         const char      *messageID;
112         char            *registered_uri;
113         PyObject        *retlist;
114         uint64_t        mid;
115         int             ret;
116         PyMAPIStoreGlobals *globals;
117
118         if (!PyArg_ParseTuple(args, "ssss", &backend, &user, &uri, &messageID)) {
119                 return NULL;
120         }
121
122         retlist = PyList_New(0);
123
124         /* Gets a new message ID */
125         globals = get_PyMAPIStoreGlobals();
126         ret = openchangedb_get_new_folderID(globals->ocdb_ctx, &mid);
127         if (ret) return (PyObject *)retlist;
128
129         /* Register the message within specified user indexing database */
130         ret = mapistore_mgmt_register_message(self->mgmt_ctx, backend, user, mid, uri, messageID, &registered_uri);
131         if (ret) return (PyObject *)retlist;
132
133         PyList_Append(retlist, PyLong_FromLongLong(mid));
134         PyList_Append(retlist, PyString_FromString(registered_uri));
135         
136         talloc_free(registered_uri);
137         return (PyObject *) retlist;
138 }
139
140 static PyObject *py_MAPIStoreMGMT_existing_users(PyMAPIStoreMGMTObject *self, PyObject *args)
141 {
142         PyObject        *dict;
143         PyObject        *userlist;
144         PyObject        *item;
145         char            **MAPIStoreURI;
146         char            **users;
147         uint32_t        count;
148         char            *uri;
149         const char      *backend;
150         const char      *vuser;
151         const char      *folder;
152         int             ret;
153         int             i;
154         PyMAPIStoreGlobals *globals;
155
156         if (!PyArg_ParseTuple(args, "sss", &backend, &vuser, &folder)) {
157                 return NULL;
158         }       
159
160         dict = PyDict_New();
161         userlist = PyList_New(0);
162         PyDict_SetItemString(dict, "backend", PyString_FromString(backend));
163         PyDict_SetItemString(dict, "user", PyString_FromString(vuser));
164         PyDict_SetItemString(dict, "count", PyLong_FromLong(0));
165         PyDict_SetItem(dict, PyString_FromString("infos"), userlist);
166
167         ret = mapistore_mgmt_generate_uri(self->mgmt_ctx, backend, vuser, folder, NULL, NULL, &uri);
168         if (ret != MAPISTORE_SUCCESS) return (PyObject *)dict;
169         printf("=> uri: %s\n", uri);
170
171         globals = get_PyMAPIStoreGlobals();
172         ret = openchangedb_get_users_from_partial_uri(self->mgmt_ctx, globals->ocdb_ctx, uri, 
173                                                       &count, &MAPIStoreURI, &users);
174         printf("ret = 0x%x\n", ret);
175         if (ret != MAPISTORE_SUCCESS) return (PyObject *)dict;
176
177         PyDict_SetItemString(dict, "count", PyLong_FromLong(count));
178         for (i = 0; i != count; i++) {
179                 item = PyDict_New();
180                 PyDict_SetItemString(item, "username", PyString_FromString(users[i]));
181                 PyDict_SetItemString(item, "mapistoreURI", PyString_FromString(MAPIStoreURI[i]));
182                 PyList_Append(userlist, item);
183         }
184         PyDict_SetItem(dict, PyString_FromString("infos"), userlist);
185
186         return (PyObject *)dict;
187 }
188
189 static PyObject *py_MAPIStoreMGMT_registered_subscription(PyMAPIStoreMGMTObject *self, PyObject *args)
190 {
191         int             ret;
192         const char      *username;
193         const char      *uri;
194         uint16_t        type;
195         uint16_t        NotificationFlags;
196
197         if (!PyArg_ParseTuple(args, "sshh", &username, &uri, &type, &NotificationFlags)) {
198                 return NULL;
199         }
200
201         switch (type) {
202         case MAPISTORE_FOLDER:
203                 ret = mapistore_mgmt_registered_folder_subscription(self->mgmt_ctx, username, uri, 
204                                                                     NotificationFlags);
205                 return PyBool_FromLong((ret == MAPISTORE_SUCCESS) ? true : false);
206                 break;
207         case MAPISTORE_MESSAGE:
208                 DEBUG(0, ("[%s:%d]: Unsupported subscription type\n", __FUNCTION__, __LINE__));
209                 return PyBool_FromLong(false);
210                 break;
211         }
212
213         return PyBool_FromLong(false);
214 }
215
216 static PyObject *py_MAPIStoreMGMT_send_newmail(PyMAPIStoreMGMTObject *self, PyObject *args)
217 {
218         int             ret;
219         const char      *username;
220         const char      *storeuser;
221         const char      *FolderURI;
222         const char      *MessageURI;
223         uint64_t        FolderID;
224         uint64_t        MessageID;
225         bool            softdeleted;
226         PyMAPIStoreGlobals *globals;
227
228         if (!PyArg_ParseTuple(args, "ssss", &username, &storeuser, &FolderURI, &MessageURI)) {
229                 return NULL;
230         }
231
232         /* Turn MessageURI into MessageID from indexing database */
233         ret = mapistore_indexing_record_get_fmid(self->mgmt_ctx->mstore_ctx, storeuser, MessageURI, 
234                                                  false, &MessageID, &softdeleted);
235         if (ret != MAPISTORE_SUCCESS || softdeleted == true) {
236                 return PyBool_FromLong(false);
237         }
238
239         /* Turn FolderURI into FolderID from openchangedb or indexing database */
240         globals = get_PyMAPIStoreGlobals();
241         ret = openchangedb_get_fid(globals->ocdb_ctx, FolderURI, &FolderID);
242         if (ret != MAPI_E_SUCCESS) {
243                 ret = mapistore_indexing_record_get_fmid(self->mgmt_ctx->mstore_ctx, username, FolderURI, false,
244                                                          &FolderID, &softdeleted);
245                 if (ret != MAPISTORE_SUCCESS || softdeleted == true) {
246                         return PyBool_FromLong(false);
247                 }
248         }
249
250         /* Send notification on user queue */
251         ret = mapistore_mgmt_send_newmail_notification(self->mgmt_ctx, username, FolderID, 
252                                                        MessageID, MessageURI);
253
254         return PyBool_FromLong((ret == MAPISTORE_SUCCESS) ? true : false);
255 }
256
257 static PyObject *obj_get_verbose(PyMAPIStoreMGMTObject *self, void *closure)
258 {
259         return PyBool_FromLong(self->mgmt_ctx->verbose);
260 }
261
262 static int obj_set_verbose(PyMAPIStoreMGMTObject *self, PyObject *verbose, void *closure)
263 {
264         if (!PyBool_Check(verbose))
265                 return -1;
266         if (mapistore_mgmt_set_verbosity(self->mgmt_ctx, PyLong_AsLong(verbose)) != MAPISTORE_SUCCESS) return -1;
267         return 0;
268 }
269
270 static PyMethodDef mapistore_mgmt_methods[] = {
271         { "registered_backend", (PyCFunction)py_MAPIStoreMGMT_registered_backend, METH_VARARGS },
272         { "registered_users", (PyCFunction)py_MAPIStoreMGMT_registered_users, METH_VARARGS },
273         { "registered_message", (PyCFunction)py_MAPIStoreMGMT_registered_message, METH_VARARGS },
274         { "register_message", (PyCFunction)py_MAPIStoreMGMT_register_message, METH_VARARGS },
275         { "registered_subscription", (PyCFunction)py_MAPIStoreMGMT_registered_subscription, METH_VARARGS },
276         { "existing_users", (PyCFunction)py_MAPIStoreMGMT_existing_users, METH_VARARGS },
277         { "send_newmail", (PyCFunction)py_MAPIStoreMGMT_send_newmail, METH_VARARGS },
278         { NULL },
279 };
280
281 static PyGetSetDef mapistore_mgmt_getsetters[] = {
282         { (char *)"verbose", (getter)obj_get_verbose, (setter)obj_set_verbose, 
283           "Enable/Disable verbosity for management object" },
284         { NULL }
285 };
286
287 PyTypeObject PyMAPIStoreMGMT = {
288         PyObject_HEAD_INIT(NULL) 0,
289         .tp_name = "mapistore_mgmt",
290         .tp_basicsize = sizeof (PyMAPIStoreMGMTObject),
291         .tp_methods = mapistore_mgmt_methods,
292         .tp_getset = mapistore_mgmt_getsetters,
293         .tp_doc = "mapistore management object",
294         .tp_dealloc = (destructor)py_MAPIStoreMGMT_dealloc,
295         .tp_flags = Py_TPFLAGS_DEFAULT,
296 };
297
298 void initmapistore_mgmt(PyObject *m)
299 {
300         if (PyType_Ready(&PyMAPIStoreMGMT) < 0) {
301                 return;
302         }
303         Py_INCREF(&PyMAPIStoreMGMT);
304 }