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