pEpObjCAdapter/PEPSession.m
author Edouard Tisserant <edouard@pep-project.org>
Thu, 28 Sep 2017 18:40:44 +0200
changeset 251 0002fa8d58fa
parent 250 371630c05842
child 253 8b302fa1d5c6
permissions -rw-r--r--
Cleanup dead code
dirk@187
     1
//
dirk@187
     2
//  PEPSession.m
dirk@187
     3
//  pEpiOSAdapter
dirk@187
     4
//
dirk@187
     5
//  Created by Volker Birk on 08.07.15.
dirk@187
     6
//  Copyright (c) 2015 p≡p. All rights reserved.
dirk@187
     7
//
dirk@187
     8
dirk@187
     9
#import "PEPSession.h"
dirk@187
    10
#import "PEPSession+Internal.h"
dirk@187
    11
#import "PEPObjCAdapter.h"
dirk@187
    12
#import "PEPObjCAdapter+Internal.h"
dirk@187
    13
#import "PEPMessage.h"
dirk@187
    14
#import "PEPLanguage.h"
dirk@187
    15
#import "PEPCSVScanner.h"
dirk@187
    16
#import "NSArray+Extension.h"
dirk@216
    17
#import "NSDictionary+Extension.h"
dirk@187
    18
dirk@187
    19
@implementation PEPSession
dirk@187
    20
dirk@187
    21
+ (void)setupTrustWordsDB
dirk@187
    22
{
dirk@187
    23
    static dispatch_once_t once;
dirk@187
    24
    dispatch_once(&once, ^{
dirk@187
    25
        [PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
dirk@187
    26
    });
dirk@187
    27
}
dirk@187
    28
dirk@187
    29
- (id)init
dirk@187
    30
{
dirk@187
    31
    [PEPSession setupTrustWordsDB];
edouard@240
    32
    
edouard@251
    33
    PEP_STATUS status = init(&_session);
dirk@187
    34
edouard@251
    35
    if (status != PEP_STATUS_OK) {
edouard@251
    36
        return nil;
edouard@251
    37
    }
dirk@187
    38
edouard@251
    39
    [PEPObjCAdapter bindSession:self];
edouard@240
    40
dirk@187
    41
    return self;
dirk@187
    42
}
dirk@187
    43
dirk@187
    44
- (void)dealloc
dirk@187
    45
{
dirk@187
    46
    [PEPObjCAdapter unbindSession:self];
dirk@207
    47
edouard@251
    48
    release(_session);
edouard@251
    49
dirk@233
    50
}
dirk@233
    51
dirk@219
    52
/**
dirk@219
    53
 Saves the given message dict as a plist to the local filesystem
dirk@219
    54
 (directly under NSApplicationSupportDirectory).
dirk@219
    55
 Since the complete output file will be logged by `debugSaveToFilePath`,
dirk@219
    56
 you can get access to the files easily when it's the simulator.
dirk@219
    57
 */
dirk@216
    58
- (void)debugOutPutMessageDict:(nonnull PEPDict *)src
dirk@216
    59
{
dirk@216
    60
    NSString *from = src[kPepFrom][kPepAddress];
dirk@216
    61
    NSArray *tos = src[kPepTo];
dirk@216
    62
    NSString *to = tos[0][kPepAddress];
dirk@216
    63
    NSString *msgID = src[kPepID];
dirk@218
    64
    NSString *fileName = [NSString stringWithFormat:@"%@_from(%@)_%@",
dirk@218
    65
                          to, from, msgID];
dirk@216
    66
    [src debugSaveToFilePath:fileName];
dirk@216
    67
}
dirk@216
    68
dirk@206
    69
- (PEP_rating)decryptMessageDict:(nonnull PEPDict *)src
dirk@206
    70
                            dest:(PEPDict * _Nullable * _Nullable)dst
dirk@206
    71
                            keys:(PEPStringList * _Nullable * _Nullable)keys
dirk@187
    72
{
dirk@187
    73
    message * _src = PEP_messageDictToStruct(src);
dirk@187
    74
    message * _dst = NULL;
dirk@187
    75
    stringlist_t * _keys = NULL;
dirk@187
    76
    PEP_rating color = PEP_rating_undefined;
dirk@187
    77
    PEP_decrypt_flags_t flags = 0;
dirk@187
    78
dirk@187
    79
    @synchronized (self) {
dirk@187
    80
        decrypt_message(_session, _src, &_dst, &_keys, &color, &flags);
dirk@187
    81
    }
dirk@187
    82
dirk@187
    83
    NSDictionary * dst_;
dirk@187
    84
dirk@187
    85
    if (_dst) {
dirk@187
    86
        dst_ = PEP_messageDictFromStruct(_dst);
dirk@187
    87
    }
dirk@187
    88
    else {
dirk@187
    89
        dst_ = PEP_messageDictFromStruct(_src);
dirk@187
    90
    }
dirk@187
    91
dirk@187
    92
    NSArray * keys_ = nil;
dirk@187
    93
    if (_keys)
dirk@187
    94
        keys_ = PEP_arrayFromStringlist(_keys);
dirk@187
    95
dirk@187
    96
    free_message(_src);
dirk@187
    97
    free_message(_dst);
dirk@187
    98
    free_stringlist(_keys);
dirk@187
    99
dirk@187
   100
    if (dst) {
dirk@187
   101
        *dst = dst_;
dirk@187
   102
    }
dirk@187
   103
    if (keys) {
dirk@187
   104
        *keys = keys_;
dirk@187
   105
    }
dirk@187
   106
    return color;
dirk@187
   107
}
dirk@187
   108
dirk@206
   109
- (PEP_rating)reEvaluateMessageRating:(nonnull PEPDict *)src
edouard@196
   110
{
edouard@196
   111
    message * _src = PEP_messageDictToStruct(src);
edouard@196
   112
    PEP_rating color = PEP_rating_undefined;
dirk@207
   113
edouard@196
   114
    @synchronized (self) {
edouard@196
   115
        re_evaluate_message_rating(_session, _src, NULL, PEP_rating_undefined, &color);
edouard@196
   116
    }
dirk@207
   117
edouard@196
   118
    free_message(_src);
dirk@207
   119
edouard@196
   120
    return color;
edouard@196
   121
}
edouard@196
   122
dirk@206
   123
- (void)removeEmptyArrayKey:(NSString *)key inDict:(PEPMutableDict *)dict
dirk@187
   124
{
dirk@187
   125
    if ([[dict objectForKey:key] count] == 0) {
dirk@187
   126
        [dict removeObjectForKey:key];
dirk@187
   127
    }
dirk@187
   128
}
dirk@187
   129
dirk@206
   130
- (NSDictionary *)removeEmptyRecipients:(PEPDict *)src
dirk@187
   131
{
dirk@187
   132
    NSMutableDictionary *dest = src.mutableCopy;
dirk@187
   133
dirk@187
   134
    [self removeEmptyArrayKey:kPepTo inDict:dest];
dirk@187
   135
    [self removeEmptyArrayKey:kPepCC inDict:dest];
dirk@187
   136
    [self removeEmptyArrayKey:kPepBCC inDict:dest];
dirk@187
   137
dirk@187
   138
    return [NSDictionary dictionaryWithDictionary:dest];
dirk@187
   139
}
dirk@187
   140
dirk@206
   141
- (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
dirk@187
   142
                           extra:(nullable NSArray *)keys
dirk@206
   143
                            dest:(PEPDict * _Nullable * _Nullable)dst
dirk@187
   144
{
dirk@187
   145
    PEP_STATUS status;
dirk@187
   146
    PEP_encrypt_flags_t flags = 0;
dirk@187
   147
dirk@187
   148
    message * _src = PEP_messageDictToStruct([self removeEmptyRecipients:src]);
dirk@187
   149
    message * _dst = NULL;
dirk@187
   150
    stringlist_t * _keys = PEP_arrayToStringlist(keys);
dirk@187
   151
dirk@187
   152
    @synchronized (self) {
dirk@187
   153
        status = encrypt_message(_session, _src, _keys, &_dst, PEP_enc_PGP_MIME, flags);
dirk@187
   154
    }
dirk@187
   155
dirk@187
   156
    NSDictionary * dst_;
dirk@187
   157
dirk@187
   158
    if (_dst) {
dirk@187
   159
        dst_ = PEP_messageDictFromStruct(_dst);
dirk@187
   160
    }
dirk@187
   161
    else {
dirk@187
   162
        dst_ = PEP_messageDictFromStruct(_src);
dirk@187
   163
    }
dirk@187
   164
    if (dst) {
dirk@187
   165
        *dst = dst_;
dirk@187
   166
    }
dirk@187
   167
dirk@187
   168
    free_message(_src);
dirk@187
   169
    free_message(_dst);
dirk@187
   170
    free_stringlist(_keys);
dirk@207
   171
dirk@187
   172
    return status;
dirk@187
   173
}
dirk@187
   174
dirk@206
   175
- (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
dirk@206
   176
                        identity:(nonnull PEPDict *)identity
dirk@206
   177
                            dest:(PEPDict * _Nullable * _Nullable)dst
dirk@187
   178
{
dirk@187
   179
    PEP_STATUS status;
dirk@187
   180
    PEP_encrypt_flags_t flags = 0;
dirk@187
   181
dirk@187
   182
    message * _src = PEP_messageDictToStruct([self removeEmptyRecipients:src]);
dirk@187
   183
    pEp_identity *ident = PEP_identityDictToStruct(identity);
dirk@187
   184
    message * _dst = NULL;
dirk@187
   185
dirk@187
   186
    @synchronized (self) {
dirk@187
   187
        status = encrypt_message_for_self(_session, ident, _src, &_dst, PEP_enc_PGP_MIME, flags);
dirk@187
   188
    }
dirk@187
   189
dirk@187
   190
    NSDictionary * dst_;
dirk@187
   191
dirk@187
   192
    if (_dst) {
dirk@187
   193
        dst_ = PEP_messageDictFromStruct(_dst);
dirk@187
   194
    }
dirk@187
   195
    else {
dirk@187
   196
        dst_ = PEP_messageDictFromStruct(_src);
dirk@187
   197
    }
dirk@187
   198
dirk@187
   199
    if (dst) {
dirk@187
   200
        *dst = dst_;
dirk@187
   201
    }
dirk@187
   202
dirk@187
   203
    free_message(_src);
dirk@187
   204
    free_message(_dst);
dirk@187
   205
    free_identity(ident);
dirk@187
   206
dirk@187
   207
    return status;
dirk@187
   208
}
dirk@187
   209
dirk@206
   210
- (PEP_rating)outgoingMessageColor:(PEPDict *)msg
dirk@187
   211
{
dirk@187
   212
    message * _msg = PEP_messageDictToStruct(msg);
dirk@187
   213
    PEP_rating color = PEP_rating_undefined;
dirk@187
   214
dirk@187
   215
    @synchronized (self) {
dirk@187
   216
        outgoing_message_rating(_session, _msg, &color);
dirk@187
   217
    }
dirk@187
   218
dirk@187
   219
    free_message(_msg);
dirk@207
   220
dirk@187
   221
    return color;
dirk@187
   222
}
dirk@187
   223
dirk@206
   224
- (PEP_rating)identityRating:(nonnull PEPDict *)identity
dirk@187
   225
{
dirk@187
   226
    pEp_identity *ident = PEP_identityDictToStruct(identity);
dirk@187
   227
    PEP_rating color = PEP_rating_undefined;
dirk@207
   228
dirk@187
   229
    @synchronized (self) {
dirk@187
   230
        identity_rating(_session, ident, &color);
dirk@187
   231
    }
dirk@207
   232
dirk@187
   233
    free_identity(ident);
dirk@207
   234
dirk@187
   235
    return color;
dirk@187
   236
}
dirk@187
   237
dirk@207
   238
DYNAMIC_API PEP_STATUS identity_rating(PEP_SESSION session, pEp_identity *ident, PEP_rating *color);
dirk@187
   239
dirk@187
   240
dirk@187
   241
- (NSArray *)trustwords:(NSString *)fpr forLanguage:(NSString *)languageID shortened:(BOOL)shortened
dirk@187
   242
{
dirk@187
   243
    NSMutableArray *array = [NSMutableArray array];
dirk@187
   244
dirk@187
   245
    for (int i = 0; i < [fpr length]; i += 4) {
dirk@187
   246
        if (shortened && i >= 20)
dirk@187
   247
            break;
dirk@207
   248
dirk@187
   249
        NSString *str = [fpr substringWithRange:NSMakeRange(i, 4)];
dirk@187
   250
dirk@187
   251
        unsigned int value;
dirk@187
   252
        [[NSScanner scannerWithString:str] scanHexInt:&value];
dirk@207
   253
dirk@187
   254
        char *word;
dirk@187
   255
        size_t size;
dirk@187
   256
dirk@187
   257
        @synchronized (self) {
dirk@187
   258
            trustword(_session, value, [languageID UTF8String], &word, &size);
dirk@187
   259
        }
dirk@187
   260
dirk@187
   261
        [array addObject:[NSString stringWithUTF8String:word]];
dirk@187
   262
        free(word);
dirk@187
   263
    }
dirk@207
   264
dirk@187
   265
    return array;
dirk@187
   266
}
dirk@187
   267
dirk@206
   268
- (void)mySelf:(PEPMutableDict *)identity
dirk@187
   269
{
dirk@187
   270
    [identity removeObjectForKey:kPepUserID];
dirk@187
   271
dirk@187
   272
    pEp_identity *ident = PEP_identityDictToStruct(identity);
dirk@187
   273
dirk@187
   274
    @synchronized(self) {
dirk@187
   275
        myself(_session, ident);
dirk@187
   276
    }
dirk@187
   277
dirk@187
   278
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
dirk@187
   279
    free_identity(ident);
dirk@187
   280
}
dirk@187
   281
dirk@206
   282
- (void)updateIdentity:(PEPMutableDict *)identity
dirk@187
   283
{
dirk@187
   284
    pEp_identity *ident = PEP_identityDictToStruct(identity);
dirk@187
   285
dirk@187
   286
    @synchronized(self) {
dirk@187
   287
        update_identity(_session, ident);
dirk@187
   288
    }
dirk@187
   289
dirk@187
   290
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
dirk@187
   291
    free_identity(ident);
dirk@187
   292
}
dirk@187
   293
dirk@206
   294
- (void)trustPersonalKey:(PEPMutableDict *)identity
dirk@187
   295
{
dirk@187
   296
    pEp_identity *ident = PEP_identityDictToStruct(identity);
dirk@207
   297
dirk@187
   298
    @synchronized(self) {
dirk@187
   299
        trust_personal_key(_session, ident);
dirk@187
   300
    }
dirk@207
   301
dirk@187
   302
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
dirk@187
   303
    free_identity(ident);
dirk@187
   304
}
dirk@187
   305
dirk@206
   306
- (void)keyResetTrust:(PEPMutableDict *)identity
dirk@187
   307
{
dirk@187
   308
    pEp_identity *ident = PEP_identityDictToStruct(identity);
dirk@207
   309
dirk@187
   310
    @synchronized(self) {
dirk@187
   311
        key_reset_trust(_session, ident);
dirk@187
   312
    }
dirk@207
   313
dirk@187
   314
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
dirk@187
   315
    free_identity(ident);
dirk@187
   316
}
dirk@187
   317
dirk@206
   318
- (void)keyMistrusted:(PEPMutableDict *)identity
dirk@187
   319
{
dirk@187
   320
    pEp_identity *ident = PEP_identityDictToStruct(identity);
dirk@207
   321
dirk@187
   322
    @synchronized(self) {
dirk@187
   323
        key_mistrusted(_session, ident);
dirk@187
   324
    }
dirk@207
   325
dirk@187
   326
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
dirk@187
   327
    free_identity(ident);
dirk@187
   328
}
dirk@187
   329
dirk@187
   330
- (void)importKey:(NSString *)keydata
dirk@187
   331
{
dirk@187
   332
    @synchronized(self) {
dirk@187
   333
        import_key(_session, [keydata UTF8String], [keydata length], NULL);
dirk@187
   334
    }
dirk@187
   335
dirk@187
   336
}
dirk@187
   337
dirk@187
   338
- (void)logTitle:(nonnull NSString *)title entity:(nonnull NSString *)entity
dirk@187
   339
     description:(nullable NSString *)description comment:(nullable NSString *)comment
dirk@187
   340
{
edouard@245
   341
    @synchronized(self) {
edouard@245
   342
edouard@245
   343
        log_event(_session, [[title precomposedStringWithCanonicalMapping] UTF8String],
edouard@245
   344
                  [[entity precomposedStringWithCanonicalMapping] UTF8String],
edouard@245
   345
                  [[description precomposedStringWithCanonicalMapping] UTF8String],
edouard@245
   346
                  [[comment precomposedStringWithCanonicalMapping] UTF8String]);
edouard@245
   347
edouard@245
   348
    }
dirk@187
   349
}
dirk@187
   350
dirk@187
   351
- (nonnull NSString *)getLog
dirk@187
   352
{
dirk@187
   353
    char *data;
edouard@250
   354
    @synchronized(self) {
edouard@250
   355
        get_crashdump_log(_session, 0, &data);
edouard@250
   356
    }
edouard@250
   357
    
dirk@187
   358
    NSString *logString = [NSString stringWithUTF8String:data];
dirk@187
   359
    return logString;
dirk@187
   360
}
dirk@187
   361
dirk@206
   362
- (nullable NSString *)getTrustwordsIdentity1:(nonnull PEPDict *)identity1
dirk@206
   363
                                    identity2:(nonnull PEPDict *)identity2
dirk@187
   364
                                     language:(nullable NSString *)language
dirk@187
   365
                                         full:(BOOL)full
dirk@187
   366
{
dirk@187
   367
    NSString *result = nil;
dirk@187
   368
    char *trustwords = nil;
dirk@187
   369
    size_t sizeWritten = 0;
dirk@187
   370
dirk@187
   371
    pEp_identity *ident1 = PEP_identityDictToStruct(identity1);
dirk@187
   372
    pEp_identity *ident2 = PEP_identityDictToStruct(identity2);
edouard@245
   373
    PEP_STATUS status;
edouard@245
   374
    @synchronized(self) {
edouard@245
   375
edouard@245
   376
        status = get_trustwords(_session, ident1, ident2,
edouard@245
   377
                                           [[language precomposedStringWithCanonicalMapping]
edouard@245
   378
                                            UTF8String],
edouard@245
   379
                                           &trustwords, &sizeWritten, full);
edouard@245
   380
    }
dirk@202
   381
    if (status == PEP_STATUS_OK) {
dirk@202
   382
        result = [NSString stringWithCString:trustwords
dirk@202
   383
                                    encoding:NSUTF8StringEncoding];
dirk@202
   384
    }
dirk@202
   385
    if (trustwords) {
dirk@202
   386
        free(trustwords);
dirk@202
   387
    }
dirk@202
   388
    return result;
dirk@202
   389
}
dirk@202
   390
dirk@206
   391
- (nullable NSString *)getTrustwordsMessageDict:(nonnull PEPDict *)messageDict
dirk@206
   392
                                   receiverDict:(nonnull PEPDict *)receiverDict
dirk@206
   393
                                      keysArray:(PEPStringList * _Nullable)keysArray
dirk@202
   394
                                       language:(nullable NSString *)language
dirk@202
   395
                                           full:(BOOL)full
dirk@213
   396
                                resultingStatus:(PEP_STATUS * _Nullable)resultingStatus
dirk@202
   397
{
dirk@202
   398
    NSString *result = nil;
dirk@202
   399
    char *trustwords = nil;
dirk@202
   400
dirk@202
   401
    message *theMessage = PEP_messageDictToStruct(messageDict);
dirk@202
   402
dirk@202
   403
    stringlist_t *keyList = nil;
dirk@202
   404
    if (keysArray) {
dirk@202
   405
        keyList = PEP_arrayToStringlist(keysArray);
dirk@202
   406
    }
dirk@202
   407
dirk@202
   408
    pEp_identity *receiver = PEP_identityDictToStruct(receiverDict);
edouard@245
   409
    PEP_STATUS status;
edouard@245
   410
    @synchronized(self) {
edouard@245
   411
        status = get_message_trustwords(_session, theMessage, keyList, receiver,
edouard@245
   412
                                        [[language
edouard@245
   413
                                          precomposedStringWithCanonicalMapping] UTF8String],
edouard@245
   414
                                        &trustwords, full);
edouard@245
   415
    }
edouard@245
   416
    
dirk@213
   417
    if (resultingStatus) {
dirk@213
   418
        *resultingStatus = status;
dirk@213
   419
    }
dirk@213
   420
dirk@187
   421
    if (status == PEP_STATUS_OK) {
dirk@187
   422
        result = [NSString stringWithCString:trustwords
dirk@187
   423
                                    encoding:NSUTF8StringEncoding];
dirk@187
   424
    }
dirk@187
   425
    if (trustwords) {
dirk@187
   426
        free(trustwords);
dirk@187
   427
    }
dirk@187
   428
    return result;
dirk@187
   429
}
dirk@187
   430
dirk@187
   431
- (NSArray<PEPLanguage *> * _Nonnull)languageList
dirk@187
   432
{
dirk@187
   433
    char *chLangs;
edouard@250
   434
    @synchronized(self) {
edouard@250
   435
        get_languagelist(_session, &chLangs);
edouard@250
   436
    }
dirk@187
   437
    NSString *parserInput = [NSString stringWithUTF8String:chLangs];
dirk@187
   438
dirk@187
   439
    NSMutableArray<NSString *> *tokens = [NSMutableArray array];
dirk@187
   440
    PEPCSVScanner *scanner = [[PEPCSVScanner alloc] initWithString:parserInput];
dirk@187
   441
    while (YES) {
dirk@187
   442
        NSString *token = [scanner nextString];
dirk@187
   443
        if (!token) {
dirk@187
   444
            break;
dirk@187
   445
        }
dirk@187
   446
        [tokens addObject:token];
dirk@187
   447
    }
dirk@187
   448
dirk@187
   449
    NSArray *theTokens = [NSArray arrayWithArray:tokens];
dirk@187
   450
    NSMutableArray<PEPLanguage *> *langs = [NSMutableArray new];
dirk@187
   451
    while (YES) {
dirk@187
   452
        ArrayTake *take = [theTokens takeOrNil:3];
dirk@187
   453
        if (!take) {
dirk@187
   454
            break;
dirk@187
   455
        }
dirk@187
   456
        NSArray *elements = take.elements;
dirk@187
   457
        PEPLanguage *lang = [[PEPLanguage alloc]
dirk@187
   458
                             initWithCode:[elements objectAtIndex:0]
dirk@187
   459
                             name:[elements objectAtIndex:1]
dirk@187
   460
                             sentence:[elements objectAtIndex:2]];
dirk@187
   461
        [langs addObject:lang];
dirk@187
   462
        theTokens = take.rest;
dirk@187
   463
    }
dirk@207
   464
    
dirk@187
   465
    return [NSArray arrayWithArray:langs];
dirk@187
   466
}
dirk@187
   467
dirk@187
   468
@end