2 OpenChange MAPI implementation.
4 Copyright (C) Fabien Le Mentec 2007.
5 Copyright (C) Julien Kerihuel 2007-2011.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libmapi/libmapi.h"
22 #include "libmapi/libmapi_private.h"
27 \brief mapi_object_t support functions
31 /* FIXME: mapi_object functions should return error codes */
35 keep intern to this file
37 #define INVALID_HANDLE_VALUE 0xffffffff
41 \details Reset a MAPI object structure
43 \param obj pointer on the MAPI object to reset
45 static void mapi_object_reset(mapi_object_t *obj)
47 obj->handle = INVALID_HANDLE_VALUE;
52 obj->private_data = NULL;
57 \details Initialize MAPI object
59 This function is required to be called before any
60 manipulation of this MAPI object.
62 \param obj the object to initialize
64 \return MAPI_E_SUCCESS on success, otherwise MAPI error.
66 \note Developers may also call GetLastError() to retrieve the last
67 MAPI error code. Possible MAPI error codes are:
68 - MAPI_E_NOT_INITIALIZED: MAPI subsystem has not been initialized.
70 \sa mapi_object_release
72 _PUBLIC_ enum MAPISTATUS mapi_object_init(mapi_object_t *obj)
74 mapi_object_reset(obj);
76 return MAPI_E_SUCCESS;
81 \details Release MAPI object
83 This function is required to be called when this MAPI object
84 is no longer required.
86 \param obj pointer on the MAPI object to release
88 \sa mapi_object_initialize, Release
90 _PUBLIC_ void mapi_object_release(mapi_object_t *obj)
92 enum MAPISTATUS retval;
95 if (obj->handle == INVALID_HANDLE_VALUE) return;
97 retval = Release(obj);
98 if (retval == MAPI_E_SUCCESS) {
99 if (obj->private_data) {
100 talloc_free(obj->private_data);
103 if (obj->store == true && obj->session) {
104 obj->session->logon_ids[obj->logon_id] = 0;
107 mapi_object_reset(obj);
113 \details Check if the supplied object has a valid handle
115 \param obj pointer on the MAPI object to test
117 \return 0 on success, otherwise 1
119 int mapi_object_is_invalid(mapi_object_t *obj)
121 if (mapi_object_get_handle(obj) == INVALID_HANDLE_VALUE) {
125 return MAPI_E_SUCCESS;
130 \details Copy MAPI object
132 This function copies mapi_object data from source to destination.
134 \param dst pointer on the destination MAPI object
135 \param src pointer on the source MAPI object
137 \return MAPI_E_SUCCESS on success, otherwise MAPI_E_NOT_INITIALIZED
140 _PUBLIC_ enum MAPISTATUS mapi_object_copy(mapi_object_t *dst, mapi_object_t *src)
142 mapi_object_reset(dst);
144 OPENCHANGE_RETVAL_IF(!dst || !src, MAPI_E_NOT_INITIALIZED, NULL);
147 dst->handle = src->handle;
148 dst->private_data = src->private_data;
149 dst->logon_id = src->logon_id;
150 dst->session = src->session;
152 return MAPI_E_SUCCESS;
157 \details Retrieve the session associated to the MAPI object
159 \param obj the object to get the session for
161 \return pointer on a MAPI session on success, otherwise NULL
163 _PUBLIC_ struct mapi_session *mapi_object_get_session(mapi_object_t *obj)
165 if (!obj) return NULL;
166 if (!obj->session) return NULL;
173 \details Set the session for a given MAPI object
175 \param obj pointer on the object to set the session for
176 \param session pointer on the MAPI session to associate to the MAPI
179 _PUBLIC_ void mapi_object_set_session(mapi_object_t *obj,
180 struct mapi_session *session)
183 obj->session = session;
189 \details Retrieve an object ID for a given MAPI object
191 \param obj pointer on the MAPI object to get the ID for
193 \return the object ID, or 0xFFFFFFFFFFFFFFFF if the object does not exist
195 _PUBLIC_ mapi_id_t mapi_object_get_id(mapi_object_t *obj)
197 return (!obj) ? -1 : obj->id;
202 \details Set the id for a given MAPI object
204 \param obj pointer on the MAPI object to set the session for
205 \param id Identifier to set to the object obj
207 void mapi_object_set_id(mapi_object_t *obj, mapi_id_t id)
214 \details Set the logon id for a given MAPI object
216 \param obj pointer to the object to set the logon id for
217 \param logon_id the logon identifier to associate to the MAPI
220 _PUBLIC_ void mapi_object_set_logon_id(mapi_object_t *obj,
224 obj->logon_id = logon_id;
230 \details Retrieve the logon id for a given MAPI object
232 \param obj pointer to the object to retrieve the logon id from
233 \param logon_id pointer to a variable to store the logon id
235 \return MAPI_E_SUCCESS on success, otherwise MAPI error.
237 _PUBLIC_ enum MAPISTATUS mapi_object_get_logon_id(mapi_object_t *obj, uint8_t *logon_id)
239 if (!obj || !logon_id)
240 return MAPI_E_INVALID_PARAMETER;
242 *logon_id = obj->logon_id;
244 return MAPI_E_SUCCESS;
249 \details Mark a MAPI object as a store object
251 \param obj pointer to the object to set the store boolean for
253 _PUBLIC_ void mapi_object_set_logon_store(mapi_object_t *obj)
262 \details Retrieve the handle associated to a MAPI object
264 \param obj pointer on the MAPI object to retrieve the handle from
266 \return a valid MAPI object handle on success, otherwise 0xFFFFFFFF.
268 mapi_handle_t mapi_object_get_handle(mapi_object_t *obj)
270 return (!obj) ? 0xFFFFFFFF : obj->handle;
275 \details Associate a handle to a MAPI object
277 \param obj pointer on the MAPI object on which handle has to be set
278 \param handle the MAPI handle value
280 void mapi_object_set_handle(mapi_object_t *obj, mapi_handle_t handle)
282 obj->handle = handle;
287 \details Dump a MAPI object (for debugging)
289 \param obj pointer on the MAPI object to dump out
291 _PUBLIC_ void mapi_object_debug(mapi_object_t *obj)
293 DEBUG(0, ("mapi_object {\n"));
294 DEBUG(0, (" .handle == 0x%x\n", obj->handle));
295 DEBUG(0, (" .id == 0x%"PRIx64"\n", obj->id));
301 \details Initialize MAPI object private data to store a MAPI object
304 \param mem_ctx pointer on the memory context
305 \param obj_table pointer on the MAPI object
307 void mapi_object_table_init(TALLOC_CTX *mem_ctx, mapi_object_t *obj_table)
309 mapi_object_table_t *table = NULL;
311 if (obj_table->private_data == NULL) {
312 obj_table->private_data = talloc_zero((TALLOC_CTX *)mem_ctx, mapi_object_table_t);
315 table = (mapi_object_table_t *) obj_table->private_data;
317 if (table->bookmark == NULL) {
318 table->bookmark = talloc_zero((TALLOC_CTX *)table, mapi_object_bookmark_t);
322 table->proptags.aulPropTag = 0;
323 table->proptags.cValues = 0;
324 /* start bookmark index after BOOKMARK_END */
330 \details Fetch a bookmark within a MAPI object table
332 \param obj_table pointer on the MAPI object table
333 \param bkPosition the bookmark position to find
334 \param bin pointer on the Sbinary_short the function fills
336 \return MAPI_E_SUCCESS on success, otherwise MAPI error.
338 enum MAPISTATUS mapi_object_bookmark_find(mapi_object_t *obj_table, uint32_t bkPosition,
339 struct SBinary_short *bin)
341 mapi_object_table_t *table;
342 mapi_object_bookmark_t *bookmark;
344 table = (mapi_object_table_t *)obj_table->private_data;
345 bookmark = table->bookmark;
348 OPENCHANGE_RETVAL_IF(!obj_table, MAPI_E_INVALID_PARAMETER, NULL);
349 OPENCHANGE_RETVAL_IF(!table, MAPI_E_NOT_INITIALIZED, NULL);
350 OPENCHANGE_RETVAL_IF(!bookmark, MAPI_E_NOT_INITIALIZED, NULL);
351 OPENCHANGE_RETVAL_IF(bkPosition > table->bk_last, MAPI_E_INVALID_BOOKMARK, NULL);
354 if (bookmark->index == bkPosition) {
355 bin->cb = bookmark->bin.cb;
356 bin->lpb = bookmark->bin.lpb;
357 return MAPI_E_SUCCESS;
359 bookmark = bookmark->next;
361 return MAPI_E_INVALID_BOOKMARK;
366 \details Retrieve the number of bookmarks stored in a MAPI object table
368 \param obj_table pointer to the MAPI object table
369 \param count pointer to the number of bookmarks to return
371 \return MAPI_E_SUCCESS on success, otherwise MAPI error.
373 _PUBLIC_ enum MAPISTATUS mapi_object_bookmark_get_count(mapi_object_t *obj_table,
376 mapi_object_table_t *table;
378 table = (mapi_object_table_t *)obj_table->private_data;
381 OPENCHANGE_RETVAL_IF(!obj_table, MAPI_E_INVALID_PARAMETER, NULL);
382 OPENCHANGE_RETVAL_IF(!table, MAPI_E_NOT_INITIALIZED, NULL);
384 *count = table->bk_last - 3;
386 return MAPI_E_SUCCESS;
391 \details Dump bookmarks associated to a MAPI object table
393 \param obj_table pointer on the MAPI object table
395 \return MAPI_E_SUCCESS on success, otherwise MAPI error.
397 _PUBLIC_ enum MAPISTATUS mapi_object_bookmark_debug(mapi_object_t *obj_table)
399 mapi_object_table_t *table;
400 mapi_object_bookmark_t *bookmark;
402 table = (mapi_object_table_t *)obj_table->private_data;
403 bookmark = table->bookmark;
406 OPENCHANGE_RETVAL_IF(!obj_table, MAPI_E_INVALID_PARAMETER, NULL);
407 OPENCHANGE_RETVAL_IF(!table, MAPI_E_NOT_INITIALIZED, NULL);
408 OPENCHANGE_RETVAL_IF(!bookmark, MAPI_E_NOT_INITIALIZED, NULL);
411 DEBUG(0, ("mapi_object_bookmark {\n"));
412 DEBUG(0, (".index == %u\n", bookmark->index));
413 dump_data(0, bookmark->bin.lpb, bookmark->bin.cb);
416 bookmark = bookmark->next;
418 return MAPI_E_SUCCESS;