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