2007-11-05 15:51:50 +01:00
|
|
|
/* NSObject+CardDAV.m - this file is part of SOGo
|
|
|
|
*
|
2015-10-21 16:54:15 +02:00
|
|
|
* Copyright (C) 2007-2015 Inverse inc.
|
2007-11-05 15:51:50 +01:00
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2013-09-13 17:16:28 +02:00
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
2007-11-07 16:59:01 +01:00
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
#import <NGObjWeb/WOContext.h>
|
2012-02-11 08:08:02 +01:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2007-11-05 15:51:50 +01:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
|
|
|
#import <NGObjWeb/WOResponse.h>
|
2010-08-12 16:02:21 +02:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2007-11-07 16:59:01 +01:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
|
|
|
#import <DOM/DOMProtocols.h>
|
2011-02-17 23:12:04 +01:00
|
|
|
#import <DOM/DOMNode.h>
|
2007-11-07 16:59:01 +01:00
|
|
|
#import <SaxObjC/SaxObjC.h>
|
|
|
|
#import <SaxObjC/XMLNamespaces.h>
|
|
|
|
|
2016-04-06 17:19:18 +02:00
|
|
|
#import <SOGo/NSString+Utilities.h>
|
2012-02-11 08:08:02 +01:00
|
|
|
#import <SOGo/SOGoUser.h>
|
2010-01-15 00:19:19 +01:00
|
|
|
#import <SOGo/WOResponse+SOGo.h>
|
|
|
|
|
2007-11-07 16:59:01 +01:00
|
|
|
#import "SOGoContactFolder.h"
|
|
|
|
#import "SOGoContactGCSEntry.h"
|
2007-11-05 15:51:50 +01:00
|
|
|
|
2007-11-08 00:34:25 +01:00
|
|
|
@implementation SOGoFolder (CardDAV)
|
2007-11-05 15:51:50 +01:00
|
|
|
|
2010-08-12 16:02:21 +02:00
|
|
|
- (void) _appendObject: (NSDictionary *) object
|
|
|
|
withBaseURL: (NSString *) baseURL
|
|
|
|
toREPORTResponse: (WOResponse *) r
|
|
|
|
{
|
|
|
|
id component;
|
|
|
|
NSString *name, *etagLine, *contactString;
|
|
|
|
|
|
|
|
name = [object objectForKey: @"c_name"];
|
|
|
|
if ([name length])
|
|
|
|
{
|
|
|
|
component = [self lookupName: name inContext: context acquire: NO];
|
|
|
|
if ([component isKindOfClass: [NSException class]])
|
|
|
|
{
|
|
|
|
[self logWithFormat: @"Object with name '%@' not found. You likely have a LDAP configuration issue.", name];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#warning we provide both "address-data" and "addressbook-data" for compatibility reasons, we should actually check which one has been queried
|
|
|
|
[r appendContentString: @"<D:response>"
|
|
|
|
@"<D:href>"];
|
|
|
|
[r appendContentString: baseURL];
|
|
|
|
if (![baseURL hasSuffix: @"/"])
|
2013-09-13 17:13:02 +02:00
|
|
|
[r appendContentString: @"/"];
|
2010-08-12 16:02:21 +02:00
|
|
|
[r appendContentString: name];
|
|
|
|
[r appendContentString: @"</D:href>"
|
|
|
|
@"<D:propstat>"
|
|
|
|
@"<D:prop>"];
|
|
|
|
etagLine = [NSString stringWithFormat: @"<D:getetag>%@</D:getetag>",
|
|
|
|
[component davEntityTag]];
|
|
|
|
[r appendContentString: etagLine];
|
2012-03-30 16:07:36 +02:00
|
|
|
[r appendContentString: @"<C:address-data>"];
|
2016-04-06 17:19:18 +02:00
|
|
|
contactString = [[component contentAsString] safeStringByEscapingXMLString];
|
2010-08-12 16:02:21 +02:00
|
|
|
[r appendContentString: contactString];
|
|
|
|
[r appendContentString: @"</C:address-data>"
|
2012-03-30 16:07:36 +02:00
|
|
|
@"<C:addressbook-data>"];
|
|
|
|
[r appendContentString: contactString];
|
|
|
|
[r appendContentString: @"</C:addressbook-data>"
|
|
|
|
@"</D:prop>"
|
|
|
|
@"<D:status>HTTP/1.1 200 OK</D:status>"
|
|
|
|
@"</D:propstat>"
|
|
|
|
@"</D:response>"];
|
2010-08-12 16:02:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
- (void) _appendComponentsMatchingFilters: (NSArray *) filters
|
|
|
|
toResponse: (WOResponse *) response
|
2013-09-13 17:13:02 +02:00
|
|
|
context: (id) localContext
|
2007-11-05 15:51:50 +01:00
|
|
|
{
|
2013-09-13 17:16:28 +02:00
|
|
|
unsigned int count,i , max;
|
|
|
|
NSAutoreleasePool *pool;
|
2007-11-05 15:51:50 +01:00
|
|
|
NSDictionary *currentFilter, *contact;
|
|
|
|
NSEnumerator *contacts;
|
2012-02-11 08:08:02 +01:00
|
|
|
NSString *baseURL, *domain;
|
2007-11-05 15:51:50 +01:00
|
|
|
|
2008-04-15 17:00:14 +02:00
|
|
|
baseURL = [self baseURLInContext: localContext];
|
2012-02-11 08:08:02 +01:00
|
|
|
domain = [[localContext activeUser] domain];
|
2008-01-21 17:35:39 +01:00
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
max = [filters count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
currentFilter = [filters objectAtIndex: count];
|
2013-09-13 17:16:28 +02:00
|
|
|
contacts =
|
|
|
|
[[(id<SOGoContactFolder>)self lookupContactsWithFilter: [[currentFilter allValues] lastObject]
|
|
|
|
onCriteria: @"name_or_address"
|
|
|
|
sortBy: @"c_givenname"
|
|
|
|
ordering: NSOrderedDescending
|
|
|
|
inDomain: domain] objectEnumerator];
|
|
|
|
|
|
|
|
pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
i = 0;
|
2007-11-05 15:51:50 +01:00
|
|
|
while ((contact = [contacts nextObject]))
|
2013-09-13 17:16:28 +02:00
|
|
|
{
|
|
|
|
[self _appendObject: contact withBaseURL: baseURL
|
|
|
|
toREPORTResponse: response];
|
|
|
|
if (i % 10 == 0)
|
|
|
|
{
|
|
|
|
RELEASE(pool);
|
|
|
|
pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
RELEASE(pool);
|
2007-11-05 15:51:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) _isValidFilter: (NSString *) theString
|
|
|
|
{
|
2008-01-21 17:35:39 +01:00
|
|
|
NSString *newString;
|
2007-11-05 15:51:50 +01:00
|
|
|
|
2008-01-21 17:35:39 +01:00
|
|
|
newString = [theString lowercaseString];
|
2007-11-05 15:51:50 +01:00
|
|
|
|
2011-12-30 21:10:32 +01:00
|
|
|
return ([newString isEqualToString: @"sn"]
|
2013-09-13 17:13:02 +02:00
|
|
|
|| [newString isEqualToString: @"givenname"]
|
|
|
|
|| [newString isEqualToString: @"email"]
|
|
|
|
|| [newString isEqualToString: @"mail"]
|
|
|
|
|| [newString isEqualToString: @"telephonenumber"]);
|
2007-11-05 15:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) _parseContactFilter: (id <DOMElement>) filterElement
|
|
|
|
{
|
|
|
|
NSMutableDictionary *filterData;
|
|
|
|
id <DOMNode> parentNode;
|
|
|
|
id <DOMNodeList> ranges;
|
|
|
|
|
2008-01-21 17:35:39 +01:00
|
|
|
filterData = nil;
|
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
parentNode = [filterElement parentNode];
|
|
|
|
|
2009-04-01 15:49:24 +02:00
|
|
|
if ([[(id)parentNode tagName] isEqualToString: @"filter"]
|
2008-01-21 17:35:39 +01:00
|
|
|
&& [self _isValidFilter: [filterElement attribute: @"name"]])
|
2007-11-05 15:51:50 +01:00
|
|
|
{
|
|
|
|
ranges = [filterElement getElementsByTagName: @"text-match"];
|
2008-01-18 23:59:43 +01:00
|
|
|
|
2008-01-21 17:35:39 +01:00
|
|
|
if ([(NSArray *) ranges count]
|
2013-09-13 17:13:02 +02:00
|
|
|
&& [(NSArray *) [[ranges objectAtIndex: 0] childNodes] count])
|
|
|
|
{
|
|
|
|
filterData = [NSMutableDictionary dictionary];
|
|
|
|
[filterData setObject: [(NGDOMNode *)[ranges objectAtIndex: 0] textValue]
|
|
|
|
forKey: [filterElement attribute: @"name"]];
|
|
|
|
}
|
2007-11-05 15:51:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return filterData;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) _parseContactFilters: (id <DOMElement>) parentNode
|
|
|
|
{
|
|
|
|
NSEnumerator *children;
|
2008-01-21 17:35:39 +01:00
|
|
|
id <DOMElement> node;
|
2007-11-05 15:51:50 +01:00
|
|
|
NSMutableArray *filters;
|
|
|
|
NSDictionary *filter;
|
|
|
|
|
2008-01-21 17:35:39 +01:00
|
|
|
filters = [NSMutableArray array];
|
2007-11-05 15:51:50 +01:00
|
|
|
|
2009-04-01 15:49:24 +02:00
|
|
|
children = [(NSArray *)[parentNode getElementsByTagName: @"prop-filter"]
|
2013-09-13 17:13:02 +02:00
|
|
|
objectEnumerator];
|
2008-01-21 17:35:39 +01:00
|
|
|
while ((node = [children nextObject]))
|
2007-11-05 15:51:50 +01:00
|
|
|
{
|
|
|
|
filter = [self _parseContactFilter: node];
|
|
|
|
if (filter)
|
|
|
|
[filters addObject: filter];
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:54:15 +02:00
|
|
|
// If no filters are provided, we return everything.
|
|
|
|
if (![filters count])
|
fix (revised) for issue with CardDAV sync with the DAVDroid sync client.
This is a revised fix for the issue raiased in sogo bug tracker 3370 and 3373. It supercedes the fix in commit 2c723070c69899055b456119f6e8f42c279d0c57 .
The fix was noted in NEWS with the comment "we now return all cards when we receive an empty addressbook-query REPORT". However it did not work for me and at least two others, as can be seen in the commit comments. In summary, only contacts with email addresses were synced. The suggested change from kwirk fixes the regular address book sync, but it completely breaks syncing of the read-only Group Directory (Corporate Directory). My suggested changes work in full (as far as I'm able to test).
I have done some fairly extensive testing of CardDAV sync (with DAVDroid only) and it seems to work 100% now. In addition to the obvious tests, I have tested with contacts that only have one field of data entered. The feilds I've tested (with all other fields empty) are as follows:
First name
Last name
Display name
email address
Work (telephone)
Home (telephone)
Fax (telephone)
Mobile (telephone)
Additionally, I tested syncing of a contact with only the Work Address fully populated. In the webmail, since the name fields are all missing, the "Organization" field of the Work Address takes the place of the name field in the 'Name' column. This does get synced to my phone and it also appears my Android contact list with 'Name' set to the 'Organization' field data. The address, organization and website fields being in tact also.
In addition, I tested a Group Directory (Corporate Directory) [SOGoUserSources->isAddressBook] sync. It seems contacts without email addresses do not sync. This seems to be the behaviour across the board with a "." search filter. This happens despite the filter in SOGoUserSources including ldap entries without a mail attribute. Nothing I can do to patch this in SOGoFolder+CardDAV.m, that would have to be fixed in the code that deals with the special "." search filter (I guess).
I think the contact search system needs some looking into, particularly the "." search filter behaviour. There is another bug related to contact search in the webmail address book view. I will make a bug report on that soon. It's a shame there isn't an "all" search filter, it would seem it would make various parts of SOGo easier to get the right behaviour.
2016-01-23 20:43:24 +01:00
|
|
|
{
|
|
|
|
[filters addObject: [NSDictionary dictionaryWithObject: @"." forKey: @"email"]];
|
|
|
|
[filters addObject: [NSDictionary dictionaryWithObject: @"%" forKey: @"name"]];
|
|
|
|
}
|
2015-10-21 16:54:15 +02:00
|
|
|
|
2007-11-05 15:51:50 +01:00
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) davAddressbookQuery: (id) queryContext
|
|
|
|
{
|
|
|
|
WOResponse *r;
|
|
|
|
NSArray *filters;
|
|
|
|
id <DOMDocument> document;
|
|
|
|
|
|
|
|
r = [queryContext response];
|
2010-01-15 00:19:19 +01:00
|
|
|
[r prepareDAVResponse];
|
2007-11-05 15:51:50 +01:00
|
|
|
[r appendContentString: @"<D:multistatus xmlns:D=\"DAV:\""
|
2008-10-22 17:54:02 +02:00
|
|
|
@" xmlns:C=\"urn:ietf:params:xml:ns:carddav\">"];
|
2007-11-05 15:51:50 +01:00
|
|
|
|
|
|
|
document = [[queryContext request] contentAsDOMDocument];
|
|
|
|
filters = [self _parseContactFilters: [document documentElement]];
|
|
|
|
|
|
|
|
[self _appendComponentsMatchingFilters: filters
|
|
|
|
toResponse: r
|
2013-09-13 17:13:02 +02:00
|
|
|
context: queryContext];
|
2010-08-12 16:02:21 +02:00
|
|
|
[r appendContentString: @"</D:multistatus>"];
|
2007-11-05 15:51:50 +01:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|