From: St?phane Corth?sy <dav...@us...> - 2003-08-29 18:03:42
|
Update of /cvsroot/macgpg/GPGME In directory sc8-pr-cvs1:/tmp/cvs-serv1078 Modified Files: Tag: GPGME_0_4 GPGContext.m Log Message: Now GPGProgressNotification notification is always posted in main thread; corrected -keyFromFingerprint:secretKey: to return nil when no matching key is found; corrected -_keyWithFpr:isSecret: and its usage to handle case when no matching key is found; now correctly unescapes data coming keyservers; corrected documentation of -asyncSearchServerKeysForPatterns:options: Index: GPGContext.m =================================================================== RCS file: /cvsroot/macgpg/GPGME/GPGContext.m,v retrieving revision 1.18.2.6 retrieving revision 1.18.2.7 diff -u -d -r1.18.2.6 -r1.18.2.7 --- GPGContext.m 9 Aug 2003 16:31:55 -0000 1.18.2.6 +++ GPGContext.m 29 Aug 2003 18:03:37 -0000 1.18.2.7 @@ -38,6 +38,7 @@ #define _context ((gpgme_ctx_t)_internalRepresentation) +#warning TODO: post distributed notification (object and userInfo objects must be propert lists) NSString * const GPGKeyringChangedNotification = @"GPGKeyringChangedNotification"; NSString * const GPGContextKey = @"GPGContextKey"; NSString * const GPGChangesKey = @"GPGChangesKey"; @@ -348,16 +349,26 @@ return error; } +- (void) postNotificationInMainThread:(NSNotification *)notification +{ + [[NSNotificationCenter defaultCenter] postNotification:notification]; +} + static void progressCallback(void *object, const char *description, int type, int current, int total) { // The <type> parameter is the letter printed during key generation - NSString *aDescription = nil; - unichar typeChar = type; + NSString *aDescription = nil; + unichar typeChar = type; + NSNotification *aNotification; + GPGContext *aContext = (GPGContext *)object; + NSAutoreleasePool *localAP = [[NSAutoreleasePool alloc] init]; if(description != NULL) aDescription = [NSString stringWithUTF8String:description]; - [[NSNotificationCenter defaultCenter] postNotificationName:GPGProgressNotification object:object userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithCharacters:&typeChar length:1], @"type", [NSNumber numberWithInt:current], @"current", [NSNumber numberWithInt:total], @"total", aDescription, @"description", nil]]; + aNotification = [NSNotification notificationWithName:GPGProgressNotification object:aContext userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithCharacters:&typeChar length:1], @"type", [NSNumber numberWithInt:current], @"current", [NSNumber numberWithInt:total], @"total", aDescription, @"description", nil]]; // Note that if aDescription is nil, it will not be put into dictionary (ends argument list). + [aContext performSelectorOnMainThread:@selector(postNotificationInMainThread:) withObject:aNotification waitUntilDone:NO]; + [localAP release]; } - (void) setPassphraseDelegate:(id)delegate @@ -671,6 +682,7 @@ statusDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:importStatus->status] forKey:@"status"]; else statusDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:importStatus->status], @"status", [NSNumber numberWithUnsignedInt:importStatus->result], @"error", nil]; + NSAssert1(aKey != nil, @"### Unable to retrieve key matching fpr %s", importStatus->fpr); [keys setObject:statusDict forKey:aKey]; importStatus = importStatus->next; } @@ -686,7 +698,9 @@ NSDictionary *keyChangesDict; secretKey = [self _keyWithFpr:result->fpr isSecret:YES]; + NSAssert1(secretKey != nil, @"### Unable to retrieve key matching fpr %s", result->fpr); publicKey = [self _keyWithFpr:result->fpr isSecret:NO]; + NSAssert1(publicKey != nil, @"### Unable to retrieve key matching fpr %s", result->fpr); keyChangesDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:(GPGImportNewKeyMask | GPGImportSecretKeyMask)] forKey:@"status"], secretKey, [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:GPGImportNewKeyMask] forKey:@"status"], publicKey, nil]; [operationResults setObject:keyChangesDict forKey:GPGChangesKey]; } @@ -1356,16 +1370,21 @@ // invalid. GPGContext *localContext = [[GPGContext alloc] init]; gpgme_error_t anError; - gpgme_key_t aKey; - GPGKey *returnedKey; + gpgme_key_t aKey = NULL; + GPGKey *returnedKey = nil; anError = gpgme_get_key([localContext gpgmeContext], fpr, &aKey, isSecret); if(anError != GPG_ERR_NO_ERROR){ - [localContext release]; - [[NSException exceptionWithGPGError:anError userInfo:nil] raise]; + if(gpgme_err_code(anError) == GPG_ERR_EOF) + aKey = NULL; + else{ + [localContext release]; + [[NSException exceptionWithGPGError:anError userInfo:nil] raise]; + } } - returnedKey = [[GPGKey alloc] initWithInternalRepresentation:aKey]; + if(aKey != NULL) + returnedKey = [[GPGKey alloc] initWithInternalRepresentation:aKey]; [localContext release]; return [returnedKey autorelease]; @@ -1406,6 +1425,7 @@ GPGKey *aKey = [self _keyWithFpr:importStatus->fpr isSecret:isSecret]; NSDictionary *statusDict; + NSAssert1(aKey != nil, @"### Unable to retrieve key matching fpr %s", importStatus->fpr); if(importStatus->result == GPG_ERR_NO_ERROR) statusDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:importStatus->status] forKey:@"status"]; else @@ -1596,14 +1616,18 @@ "*/ { gpgme_error_t anError; - gpgme_key_t aKey; + gpgme_key_t aKey = NULL; NSParameterAssert(fingerprint != nil); anError = gpgme_get_key(_context, [fingerprint UTF8String], &aKey, secretKey); [self setOperationMask:SingleKeyListingOperation]; [_operationData setObject:[NSNumber numberWithUnsignedInt:anError] forKey:GPGErrorKey]; - if(anError != GPG_ERR_NO_ERROR) - [[NSException exceptionWithGPGError:anError userInfo:nil] raise]; + if(anError != GPG_ERR_NO_ERROR){ + if(gpgme_err_code(anError) == GPG_ERR_EOF) + aKey = NULL; + else + [[NSException exceptionWithGPGError:anError userInfo:nil] raise]; + } if(aKey != NULL) return [[[GPGKey alloc] initWithInternalRepresentation:aKey] autorelease]; @@ -1741,6 +1765,7 @@ - (id) initWithColonOutputStrings:(NSArray *)strings; - (NSArray *) colonFormatStrings; +- (NSString *) unescapedString:(NSString *)string; @end @@ -1788,6 +1813,37 @@ return _colonFormatStrings; } +- (NSString *) unescapedString:(NSString *)string +{ + // Replaces \xXX sequences with ASCII character matching hexcode XX + NSMutableString *newString = [NSMutableString stringWithString:string]; + NSRange aRange; + + while((aRange = [newString rangeOfString:@"\\x"]).length > 0){ + NSString *hexCodeString = [newString substringWithRange:NSMakeRange(aRange.location + 2, 2)]; + unichar hiChar = [hexCodeString characterAtIndex:0]; + unichar loChar = [hexCodeString characterAtIndex:1]; + + if(hiChar >= 'a') + hiChar = hiChar - 'a' + 10; + else if(hiChar >= 'A') + hiChar = hiChar - 'A' + 10; + else + hiChar -= '0'; + if(loChar >= 'a') + loChar = loChar - 'a' + 10; + else if(loChar >= 'A') + loChar = loChar - 'A' + 10; + else + loChar -= '0'; + hiChar = hiChar * 16 + loChar; + + [newString replaceCharactersInRange:NSMakeRange(aRange.location, 4) withString:[NSString stringWithCharacters:&hiChar length:1]]; + } + + return newString; +} + - (gpgme_key_t) gpgmeKey { [NSException raise:NSGenericException format:@"### Not a real key!"]; @@ -1852,7 +1908,7 @@ - (NSString *) algorithmDescription { - return [[[_colonFormatStrings objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:6]; // Not always available, not localized + return [self unescapedString:[[[_colonFormatStrings objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:6]]; // Not always available, not localized } - (unsigned int) length @@ -1986,7 +2042,7 @@ - (NSString *) userID { - return [[[[(GPGRemoteKey *)_key colonFormatStrings] objectAtIndex:_index] componentsSeparatedByString:@":"] objectAtIndex:1]; + return [(GPGRemoteKey *)_key unescapedString:[[[[(GPGRemoteKey *)_key colonFormatStrings] objectAtIndex:_index] componentsSeparatedByString:@":"] objectAtIndex:1]]; } - (NSString *) name @@ -2404,10 +2460,11 @@ * Method cannot be used yet to search CMS keys. * * Can raise a #GPGException: - * _{GPGErrorInvalidValue gpg is not configured correctly. - * More information in #GPGAdditionalReasonKey userInfo key} - * _{GPGErrorGeneralError An unknown error occurred during search. - * More information in #GPGAdditionalReasonKey userInfo key} + * _{GPGErrorKeyServerError gpg is not configured correctly. More information + * in #GPGAdditionalReasonKey userInfo key} + * _{GPGErrorGeneralError An unknown error occurred during search. More + * information in #GPGAdditionalReasonKey userInfo + * key} "*/ { NSParameterAssert(searchPatterns != nil && [searchPatterns count] > 0); |