summaryrefslogtreecommitdiff
path: root/src/backends/activesync/ActiveSyncSource.cpp
blob: b6971f743a89fc79df0718631a4d315b2eec6062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
 * Copyright (C) 2007-2009 Patrick Ohly <patrick.ohly@gmx.de>
 * Copyright (C) 2009 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef ENABLE_ACTIVESYNC

#include "ActiveSyncSource.h"

#include <eas-errors.h>

#include <stdlib.h>
#include <errno.h>

#include <boost/algorithm/string.hpp>
#include <boost/range/adaptors.hpp>

SE_GOBJECT_TYPE(EasSyncHandler)

/* #include <eas-connection-errors.h> */
#include <syncevo/declarations.h>
SE_BEGIN_CXX

void EASItemUnref(EasItemInfo *info) { g_object_unref(&info->parent_instance); }
void GStringUnref(char *str) { g_free(str); }
void EASFolderUnref(EasFolder *f) { g_object_unref(&f->parent_instance); }

void ActiveSyncSource::enableServerMode()
{
    SyncSourceAdmin::init(m_operations, this);
    SyncSourceBlob::init(m_operations, getCacheDir());
}
bool ActiveSyncSource::serverModeEnabled() const
{
    return m_operations.m_loadAdminData;
}

/* Recursively work out full path name */
std::string ActiveSyncSource::Collection::fullPath() {
    if (!pathFound) {
	if (parentId == "0") {
	    pathName = name;
	} else {
	    pathName = source->m_collections[parentId].fullPath() + "/" + name;
	}
	pathFound = true;
    }

    return pathName;
}

void ActiveSyncSource::findCollections(const std::string account, const bool force_update)
{
    GErrorCXX gerror;
    EasSyncHandlerCXX handler;
    EASFoldersCXX folders;
    
    if (!m_collections.empty()) {
	if (!force_update) return;
	m_collections.clear();
	m_folderPaths.clear();
    }
    
    /* Fetch the folders */
    handler = EasSyncHandlerCXX::steal(eas_sync_handler_new(account.c_str()));
    if (!handler) throwError("findCollections cannot allocate sync handler");
    
    if (!eas_sync_handler_get_folder_list (handler,
					   force_update,
					   folders,
					   NULL,
					   gerror)) {
	gerror.throwError("fetching folder list");
    }
    
    /* Save the Collections */
    BOOST_FOREACH(EasFolder *folder, folders) {
	m_collections[folder->folder_id].collectionId = folder->folder_id;
	m_collections[folder->folder_id].name = folder->display_name;
	m_collections[folder->folder_id].parentId = folder->parent_id;
	m_collections[folder->folder_id].type = folder->type;
	m_collections[folder->folder_id].source = this;
    }
    
    /* Save the full paths */
    BOOST_FOREACH(std::string id, m_collections | boost::adaptors::map_keys) {
	m_folderPaths[m_collections[id].fullPath()] = id;
    }
}

int ActiveSyncSource::Collection::getFolderType () {
    switch (type) {
    case EAS_FOLDER_TYPE_DEFAULT_INBOX:
    case EAS_FOLDER_TYPE_DEFAULT_DRAFTS:
    case EAS_FOLDER_TYPE_DEFAULT_DELETED_ITEMS:
    case EAS_FOLDER_TYPE_DEFAULT_SENT_ITEMS:
    case EAS_FOLDER_TYPE_DEFAULT_OUTBOX:
    case EAS_FOLDER_TYPE_USER_CREATED_MAIL:
	return EAS_ITEM_MAIL;
    case EAS_FOLDER_TYPE_DEFAULT_TASKS:
    case EAS_FOLDER_TYPE_USER_CREATED_TASKS:
	return EAS_ITEM_TODO;
    case EAS_FOLDER_TYPE_DEFAULT_CALENDAR:
    case EAS_FOLDER_TYPE_USER_CREATED_CALENDAR:
	return EAS_ITEM_CALENDAR;
    case EAS_FOLDER_TYPE_DEFAULT_CONTACTS:
    case EAS_FOLDER_TYPE_USER_CREATED_CONTACTS:
	return EAS_ITEM_CONTACT;
    case EAS_FOLDER_TYPE_DEFAULT_NOTES:
    case EAS_FOLDER_TYPE_USER_CREATED_NOTES:
	//TODO: implement memos
    case EAS_FOLDER_TYPE_DEFAULT_JOURNAL:
    case EAS_FOLDER_TYPE_USER_CREATED_JOURNAL:
    case EAS_FOLDER_TYPE_UNKNOWN:
    case EAS_FOLDER_TYPE_RECIPIENT_CACHE:
    default:
	return -1;
    }
}

bool ActiveSyncSource::Collection::collectionIsDefault () {
    return type == EAS_FOLDER_TYPE_DEFAULT_INBOX ||
	type == EAS_FOLDER_TYPE_DEFAULT_DRAFTS ||
	type == EAS_FOLDER_TYPE_DEFAULT_DELETED_ITEMS ||
	type == EAS_FOLDER_TYPE_DEFAULT_SENT_ITEMS ||
	type == EAS_FOLDER_TYPE_DEFAULT_OUTBOX ||
	type == EAS_FOLDER_TYPE_DEFAULT_TASKS ||
	type == EAS_FOLDER_TYPE_DEFAULT_CALENDAR ||
	type == EAS_FOLDER_TYPE_DEFAULT_CONTACTS ||
	type == EAS_FOLDER_TYPE_DEFAULT_NOTES ||
	type == EAS_FOLDER_TYPE_DEFAULT_JOURNAL;
}

ActiveSyncSource::Databases ActiveSyncSource::getDatabases()
{
    Databases result;
    // do a scan if username is set
    std::string account = m_context->getSyncUsername();

    if (!account.empty()) {

	findCollections(account, true);

	BOOST_FOREACH(Collection coll, m_collections | boost::adaptors::map_values) {
	    if (coll.getFolderType() == getEasType()) {
		result.push_back(Database(coll.pathName, coll.collectionId, coll.collectionIsDefault()));
	    }
	}

    } else {
	result.push_back(Database("to scan, specify --print-databases username=<account> backend=\""+getSourceType().m_backend+"\"",
                                  ""));
    }

    return result;
}

std::string ActiveSyncSource::lookupFolder(std::string folder) {
    // If folder matches a collectionId, use that
    if (m_collections.find(folder) != m_collections.end()) return folder;

    // If folder begins with /, drop it
    if (folder[0] == '/') folder.erase(0,1);

    // Lookup folder name
    if (m_folderPaths.find(folder) != m_folderPaths.end()) return m_folderPaths[folder];

    // Not found
    return "";
}

void ActiveSyncSource::open()
{
    // extract account ID and throw error if missing
    std::string username = m_context->getSyncUsername();
    std::string folder = getDatabaseID();
    SE_LOG_DEBUG(NULL, NULL,
                 "using eas sync account %s from config %s with folder %s",
                 username.c_str(),
                 m_context->getConfigName().c_str(),
		 folder.c_str());

    if (folder.empty()) { // Most common case is empty string
	m_folder = folder;
    } else { // Lookup folder name
	// Try using cached folder list
	findCollections(username, false);
	m_folder = lookupFolder(folder);
	if (m_folder.empty()) {
	    // Fetch latest folder list and try again
	    findCollections(username, true);
	    m_folder = lookupFolder(folder);
	}
	if (m_folder.empty()) {
	    throwError("could not find folder: "+folder);
	}
    }

    m_account = username;

    // create handler
    m_handler.set(eas_sync_handler_new(m_account.c_str()), "EAS handler");
}

void ActiveSyncSource::close()
{
    // free handler if not done already
    m_handler.set(NULL);
}

void ActiveSyncSource::beginSync(const std::string &lastToken, const std::string &resumeToken)
{
    // erase content which might have been set in a previous call
    reset();

    // claim item node for ids, if not done yet
    if (m_itemNode && !m_ids) {
        m_ids.swap(m_itemNode);
    }

    // incremental sync (non-empty token) or start from scratch
    m_startSyncKey = lastToken;
    if (lastToken.empty()) {
        // slow sync: wipe out cached list of IDs, will be filled anew below
        SE_LOG_DEBUG(NULL, getDisplayName(), "sync key empty, starting slow sync");
        m_ids->clear();
    } else {
        SE_LOG_DEBUG(NULL, getDisplayName(), "sync key %s for account '%s' folder '%s', starting incremental sync",
                     lastToken.c_str(),
                     m_account.c_str(),
                     m_folder.c_str());
    }

    gboolean moreAvailable = TRUE;

    m_currentSyncKey = m_startSyncKey;

    // same logic as in ActiveSyncCalendarSource::beginSync()

    bool slowSync = false;
    for (bool firstIteration = true;
         moreAvailable;
         firstIteration = false) {
        gchar *buffer = NULL;
        GErrorCXX gerror;
        EASItemsCXX created, updated;
        EASIdsCXX deleted;
        bool wasSlowSync = m_currentSyncKey.empty();

        if (!eas_sync_handler_get_items(m_handler,
                                        m_currentSyncKey.c_str(),
                                        &buffer,
                                        getEasType(),
                                        m_folder.c_str(),
                                        created, updated, deleted,
                                        &moreAvailable,
                                        gerror)) {
            if (gerror.m_gerror &&
                /*
                gerror.m_gerror->domain == EAS_TYPE_CONNECTION_ERROR &&
                gerror.m_gerror->code == EAS_CONNECTION_SYNC_ERROR_INVALIDSYNCKEY && */
                gerror.m_gerror->message &&
                strstr(gerror.m_gerror->message, "Sync error: Invalid synchronization key") &&
                firstIteration) {
                // fall back to slow sync
                slowSync = true;
                m_currentSyncKey = "";
                m_ids->clear();
                continue;
            }

            gerror.throwError("reading ActiveSync changes");
        }
        GStringPtr bufferOwner(buffer, "reading changes: empty sync key returned");

        // TODO: Test that we really get an empty token here for an unexpected slow
        // sync. If not, we'll start an incremental sync here and later the engine
        // will ask us for older, unmodified item content which we won't have.

        // populate ID lists and content cache
        BOOST_FOREACH(EasItemInfo *item, created) {
            if (!item->server_id) {
                throwError("no server ID for new eas item");
            }
            string luid(item->server_id);
            if (luid.empty()) {
                throwError("empty server ID for new eas item");
            }
            SE_LOG_DEBUG(NULL, getDisplayName(), "new item %s", luid.c_str());
            addItem(luid, NEW);
            m_ids->setProperty(luid, "1");
            if (!item->data) {
                throwError(StringPrintf("no body returned for new eas item %s", luid.c_str()));
            }
            m_items[luid] = item->data;
        }
        BOOST_FOREACH(EasItemInfo *item, updated) {
            if (!item->server_id) {
                throwError("no server ID for updated eas item");
            }
            string luid(item->server_id);
            if (luid.empty()) {
                throwError("empty server ID for updated eas item");
            }
            SE_LOG_DEBUG(NULL, getDisplayName(), "updated item %s", luid.c_str());
            addItem(luid, UPDATED);
            // m_ids.setProperty(luid, "1"); not necessary, should already exist (TODO: check?!)
            if (!item->data) {
                throwError(StringPrintf("no body returned for updated eas item %s", luid.c_str()));
            }
            m_items[luid] = item->data;
        }
        BOOST_FOREACH(const char *serverID, deleted) {
            if (!serverID) {
                throwError("no server ID for deleted eas item");
            }
            string luid(serverID);
            if (luid.empty()) {
                throwError("empty server ID for deleted eas item");
            }
            SE_LOG_DEBUG(NULL, getDisplayName(), "deleted item %s", luid.c_str());
            addItem(luid, DELETED);
            m_ids->removeProperty(luid);
        }

        // update key
        m_currentSyncKey = buffer;

        // Google  hack: if we started with an empty sync key (= slow sync)
        // and got no results (= existing items), then try one more time,
        // because Google only seems to report results when asked with
        // a valid sync key. As an additional sanity check make sure that
        // we have a valid sync key now.
        if (wasSlowSync &&
            created.empty() &&
            !m_currentSyncKey.empty()) {
            moreAvailable = true;
        }
    }

    // now also generate full list of all current items:
    // old items + new (added to m_ids above) - deleted (removed above)
    ConfigProps props;
    m_ids->readProperties(props);
    BOOST_FOREACH(const StringPair &entry, props) {
        const std::string &luid = entry.first;
        SE_LOG_DEBUG(NULL, getDisplayName(), "existing item %s", luid.c_str());
        addItem(luid, ANY);
    }

    if (slowSync) {
        // tell engine that we need a slow sync, if it didn't know already
        SE_THROW_EXCEPTION_STATUS(StatusException,
                                  "ActiveSync error: Invalid synchronization key",
                                  STATUS_SLOW_SYNC_508);
    }
}

std::string ActiveSyncSource::endSync(bool success)
{
    // store current set of items
    if (!success) {
        m_ids->clear();
    }
    m_ids->flush();

    // let engine do incremental sync next time or start from scratch
    // in case of failure
    std::string newSyncKey = success ? m_currentSyncKey : "";
    SE_LOG_DEBUG(NULL, getDisplayName(), "next sync key %s", newSyncKey.empty() ? "empty" : newSyncKey.c_str());
    return newSyncKey;
}

void ActiveSyncSource::deleteItem(const string &luid)
{
    // asking to delete a non-existent item via ActiveSync does not
    // trigger an error; this is expected by the caller, so detect
    // the problem by looking up the item in our list (and keep the
    // list up-to-date elsewhere)
    if (m_ids && m_ids->readProperty(luid).empty()) {
        throwError(STATUS_NOT_FOUND, "item not found: " + luid);
    }

    // send delete request
    // TODO (?): batch delete requests
    GListCXX<char, GSList> items;
    items.push_back((char *)luid.c_str());

    GErrorCXX gerror;
    char *buffer;
    if (!eas_sync_handler_delete_items(m_handler,
                                       m_currentSyncKey.c_str(),
                                       &buffer,
                                       getEasType(),
                                       m_folder.c_str(),
                                       items,
                                       gerror)) {
        gerror.throwError("deleting eas item");
    }
    GStringPtr bufferOwner(buffer, "delete items: empty sync key returned");

    // remove from item list
    if (m_ids) {
        m_items.erase(luid);
        m_ids->removeProperty(luid);
    }

    // update key
    m_currentSyncKey = buffer;
}

SyncSourceSerialize::InsertItemResult ActiveSyncSource::insertItem(const std::string &luid, const std::string &data)
{
    SyncSourceSerialize::InsertItemResult res;

    EASItemPtr tmp(eas_item_info_new(), "EasItem");
    EasItemInfo *item = tmp.get();
    if (!luid.empty()) {
        // update
        item->server_id = g_strdup(luid.c_str());
    } else {
        // add
        // TODO: is a local id needed? We don't have one.
    }
    item->data = g_strdup(data.c_str());
    EASItemsCXX items;
    items.push_front(tmp.release());

    GErrorCXX gerror;
    char *buffer;

    // distinguish between update (existing luid)
    // or creation (empty luid)
    if (luid.empty()) {
        // send item to server
        if (!eas_sync_handler_add_items(m_handler,
                                        m_currentSyncKey.c_str(),
                                        &buffer,
                                        getEasType(),
                                        m_folder.c_str(),
                                        items,
                                        gerror)) {
            gerror.throwError("adding eas item");
        }
        if (!item->server_id) {
            throwError("no server ID for new eas item");
        }
        // get new ID from updated item
        res.m_luid = item->server_id;
        if (res.m_luid.empty()) {
            throwError("empty server ID for new eas item");
        }

        // TODO: if someone else has inserted a new calendar item
        // with the same UID as the one we are trying to insert here,
        // what will happen? Does the ActiveSync server prevent
        // adding our own version of the item or does it merge?
        // res.m_merged = ???
    } else {
        // update item on server
        if (!eas_sync_handler_update_items(m_handler,
                                           m_currentSyncKey.c_str(),
                                           &buffer,
                                           getEasType(),
                                           m_folder.c_str(),
                                           items,
                                           gerror)) {
            gerror.throwError("updating eas item");
        }
        res.m_luid = luid;
    }
    GStringPtr bufferOwner(buffer, "insert item: empty sync key returned");

    // add/update in cache
    if (m_ids) {
        m_items[res.m_luid] = data;
        m_ids->setProperty(res.m_luid, "1");
    }

    // update key
    m_currentSyncKey = buffer;

    return res;
}

void ActiveSyncSource::readItem(const std::string &luid, std::string &item)
{
    // return straight from cache?
    std::map<std::string, std::string>::iterator it = m_items.find(luid);
    if (it == m_items.end()) {
        // no, must fetch
        EASItemPtr tmp(eas_item_info_new(), "EasItem");
        GErrorCXX gerror;
        if (!eas_sync_handler_fetch_item(m_handler,
                                         m_folder.c_str(),
                                         luid.c_str(),
                                         tmp,
                                         getEasType(),
                                         gerror)) {
            if (gerror.m_gerror->message &&
                strstr(gerror.m_gerror->message, "ObjectNotFound")
                /* gerror.matches(EAS_CONNECTION_ERROR, EAS_CONNECTION_ITEMOPERATIONS_ERROR_OBJECTNOTFOUND)
                   (gdb) p *m_gerror
                   $7 = {domain = 156, code = 36, 
                   message = 0xda2940 "GDBus.Error:org.meego.activesyncd.ItemOperationsError.ObjectNotFound: Document library - The object was not found or access denied."}

                */) {
                throwError(STATUS_NOT_FOUND, "item not found: " + luid);
            } else {
                gerror.throwError(StringPrintf("reading eas item %s", luid.c_str()));
            }
        }
        if (!tmp->data) {
            throwError(StringPrintf("no body returned for eas item %s", luid.c_str()));
        }
        item = tmp->data;
    } else {
        item = it->second;
    }
}

void ActiveSyncSource::getSynthesisInfo(SynthesisInfo &info,
                                        XMLConfigFragments &fragments)
{
    TestingSyncSource::getSynthesisInfo(info, fragments);

    /**
     * Disable reading of existing item by engine before updating
     * it by pretending to do the merging ourselves. This works
     * as long as the local side is able to store all data that
     * activesyncd gives to us and updates on the ActiveSync
     * server.
     *
     * Probably some Exchange-specific extensions currently get
     * lost because activesyncd does not know how to represent
     * them as vCard and does not tell the ActiveSync server that
     * it cannot handle them.
     */
    boost::replace_first(info.m_datastoreOptions,
                         "<updateallfields>true</updateallfields>",
                         "");

    /**
     * no ActiveSync specific rules yet, use condensed format as
     * if we were storing locally, with all extensions enabled
     */
    info.m_backendRule = "LOCALSTORAGE";

    /**
     * access to data must be done early so that a slow sync can be
     * enforced when the ActiveSync sync key turns out to be
     * invalid
     */
    info.m_earlyStartDataRead = true;
}

SE_END_CXX

#endif /* ENABLE_ACTIVESYNC */

#ifdef ENABLE_MODULES
# include "ActiveSyncSourceRegister.cpp"
#endif