~ubuntu-branches/debian/sid/openchange/sid

« back to all changes in this revision

Viewing changes to pyopenchange/mapistore/mgmt.c

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2012-04-12 20:07:57 UTC
  • mfrom: (11 sid)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20120412200757-k933d9trljmxj1l4
Tags: 1:1.0-4
* openchangeserver: Add dependency on openchangeproxy.
* Rebuild against newer version of Samba 4.
* Use dpkg-buildflags.
* Migrate to Git, update Vcs-Git header.
* Switch to debhelper 9.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        if (ret != MAPISTORE_SUCCESS) return (PyObject *)dict;
 
173
 
 
174
        PyDict_SetItemString(dict, "count", PyLong_FromLong(count));
 
175
        for (i = 0; i != count; i++) {
 
176
                item = PyDict_New();
 
177
                PyDict_SetItemString(item, "username", PyString_FromString(users[i]));
 
178
                PyDict_SetItemString(item, "mapistoreURI", PyString_FromString(MAPIStoreURI[i]));
 
179
                PyList_Append(userlist, item);
 
180
        }
 
181
        PyDict_SetItem(dict, PyString_FromString("infos"), userlist);
 
182
 
 
183
        return (PyObject *)dict;
 
184
}
 
185
 
 
186
static PyObject *py_MAPIStoreMGMT_registered_subscription(PyMAPIStoreMGMTObject *self, PyObject *args)
 
187
{
 
188
        int             ret;
 
189
        const char      *username;
 
190
        const char      *uri;
 
191
        uint16_t        type;
 
192
        uint16_t        NotificationFlags;
 
193
 
 
194
        if (!PyArg_ParseTuple(args, "sshh", &username, &uri, &type, &NotificationFlags)) {
 
195
                return NULL;
 
196
        }
 
197
 
 
198
        switch (type) {
 
199
        case MAPISTORE_FOLDER:
 
200
                ret = mapistore_mgmt_registered_folder_subscription(self->mgmt_ctx, username, uri, 
 
201
                                                                    NotificationFlags);
 
202
                return PyBool_FromLong((ret == MAPISTORE_SUCCESS) ? true : false);
 
203
                break;
 
204
        case MAPISTORE_MESSAGE:
 
205
                DEBUG(0, ("[%s:%d]: Unsupported subscription type\n", __FUNCTION__, __LINE__));
 
206
                return PyBool_FromLong(false);
 
207
                break;
 
208
        }
 
209
 
 
210
        return PyBool_FromLong(false);
 
211
}
 
212
 
 
213
static PyObject *py_MAPIStoreMGMT_send_newmail(PyMAPIStoreMGMTObject *self, PyObject *args)
 
214
{
 
215
        int             ret;
 
216
        const char      *username;
 
217
        const char      *storeuser;
 
218
        const char      *FolderURI;
 
219
        const char      *MessageURI;
 
220
        uint64_t        FolderID;
 
221
        uint64_t        MessageID;
 
222
        bool            softdeleted;
 
223
 
 
224
        if (!PyArg_ParseTuple(args, "ssss", &username, &storeuser, &FolderURI, &MessageURI)) {
 
225
                return NULL;
 
226
        }
 
227
 
 
228
        /* Turn MessageURI into MessageID from indexing database */
 
229
        ret = mapistore_indexing_record_get_fmid(self->mgmt_ctx->mstore_ctx, storeuser, MessageURI, 
 
230
                                                 false, &MessageID, &softdeleted);
 
231
        if (ret != MAPISTORE_SUCCESS || softdeleted == true) {
 
232
                return PyBool_FromLong(false);
 
233
        }
 
234
 
 
235
        /* Turn FolderURI into FolderID from openchangedb or indexing database */
 
236
        ret = openchangedb_get_fid(self->parent->ocdb_ctx, FolderURI, &FolderID);
 
237
        if (ret != MAPI_E_SUCCESS) {
 
238
                ret = mapistore_indexing_record_get_fmid(self->mgmt_ctx->mstore_ctx, username, FolderURI, false,
 
239
                                                         &FolderID, &softdeleted);
 
240
                if (ret != MAPISTORE_SUCCESS || softdeleted == true) {
 
241
                        return PyBool_FromLong(false);
 
242
                }
 
243
        }
 
244
 
 
245
        /* Send notification on user queue */
 
246
        ret = mapistore_mgmt_send_newmail_notification(self->mgmt_ctx, username, FolderID, 
 
247
                                                       MessageID, MessageURI);
 
248
 
 
249
        return PyBool_FromLong((ret == MAPISTORE_SUCCESS) ? true : false);
 
250
}
 
251
 
 
252
static PyObject *obj_get_verbose(PyMAPIStoreMGMTObject *self, void *closure)
 
253
{
 
254
        return PyBool_FromLong(self->mgmt_ctx->verbose);
 
255
}
 
256
 
 
257
static int obj_set_verbose(PyMAPIStoreMGMTObject *self, PyObject *verbose, void *closure)
 
258
{
 
259
        if (!PyBool_Check(verbose))
 
260
                return -1;
 
261
        if (mapistore_mgmt_set_verbosity(self->mgmt_ctx, PyLong_AsLong(verbose)) != MAPISTORE_SUCCESS) return -1;
 
262
        return 0;
 
263
}
 
264
 
 
265
static PyMethodDef mapistore_mgmt_methods[] = {
 
266
        { "registered_backend", (PyCFunction)py_MAPIStoreMGMT_registered_backend, METH_VARARGS },
 
267
        { "registered_users", (PyCFunction)py_MAPIStoreMGMT_registered_users, METH_VARARGS },
 
268
        { "registered_message", (PyCFunction)py_MAPIStoreMGMT_registered_message, METH_VARARGS },
 
269
        { "register_message", (PyCFunction)py_MAPIStoreMGMT_register_message, METH_VARARGS },
 
270
        { "registered_subscription", (PyCFunction)py_MAPIStoreMGMT_registered_subscription, METH_VARARGS },
 
271
        { "existing_users", (PyCFunction)py_MAPIStoreMGMT_existing_users, METH_VARARGS },
 
272
        { "send_newmail", (PyCFunction)py_MAPIStoreMGMT_send_newmail, METH_VARARGS },
 
273
        { NULL },
 
274
};
 
275
 
 
276
static PyGetSetDef mapistore_mgmt_getsetters[] = {
 
277
        { (char *)"verbose", (getter)obj_get_verbose, (setter)obj_set_verbose, 
 
278
          "Enable/Disable verbosity for management object" },
 
279
        { NULL }
 
280
};
 
281
 
 
282
PyTypeObject PyMAPIStoreMGMT = {
 
283
        PyObject_HEAD_INIT(NULL) 0,
 
284
        .tp_name = "mapistore_mgmt",
 
285
        .tp_basicsize = sizeof (PyMAPIStoreMGMTObject),
 
286
        .tp_methods = mapistore_mgmt_methods,
 
287
        .tp_getset = mapistore_mgmt_getsetters,
 
288
        .tp_doc = "mapistore management object",
 
289
        .tp_dealloc = (destructor)py_MAPIStoreMGMT_dealloc,
 
290
        .tp_flags = Py_TPFLAGS_DEFAULT,
 
291
};