pEpObjCAdapter/PEPMessageUtil.m
author Dirk Zimmermann <dz@pep.security>
Tue, 23 Jul 2019 16:05:32 +0200
branchIOSAD-123
changeset 1096 94a663ebfb1a
parent 1089 d2f893399df2
child 1097 e4bd8bed57d9
permissions -rw-r--r--
IOSAD-123 Eliminate duplicate (and buggy) method.
     1 //
     2 //  PEPMessageUtil.m
     3 //  pEpiOSAdapter
     4 //
     5 //  Created by Volker Birk on 09.07.15.
     6 //  Copyright (c) 2015 p≡p. All rights reserved.
     7 //
     8 
     9 #import "PEPMessageUtil.h"
    10 
    11 #import "PEPConstants.h"
    12 #import "PEPIdentity.h"
    13 
    14 #import "PEPMessage.h"
    15 #import "PEPAttachment.h"
    16 #import "NSMutableDictionary+PEP.h"
    17 
    18 #import "pEp_string.h"
    19 
    20 NSArray *PEP_arrayFromStringlist(stringlist_t *sl)
    21 {
    22     NSMutableArray *array = [NSMutableArray array];
    23     
    24     for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) {
    25         [array addObject:[NSString stringWithUTF8String:_sl->value]];
    26     }
    27     
    28     return array;
    29 }
    30 
    31 stringlist_t *PEP_arrayToStringlist(NSArray *array)
    32 {
    33     stringlist_t *sl = new_stringlist(NULL);
    34     if (!sl)
    35         return NULL;
    36     
    37     stringlist_t *_sl = sl;
    38     for (NSString *str in array) {
    39         _sl = stringlist_add(_sl, [[str precomposedStringWithCanonicalMapping] UTF8String]);
    40     }
    41     
    42     return sl;
    43 }
    44 
    45 NSArray *PEP_arrayFromStringPairlist(stringpair_list_t *sl)
    46 {
    47     NSMutableArray *array = [NSMutableArray array];
    48 
    49     for (stringpair_list_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) {
    50         [array addObject:[[NSMutableArray alloc ]initWithObjects:
    51                 [NSString stringWithUTF8String:_sl->value->key],
    52                 [NSString stringWithUTF8String:_sl->value->value],
    53                 nil]];
    54     }
    55 
    56     return array;
    57 }
    58 
    59 stringpair_list_t *PEP_arrayToStringPairlist(NSArray *array)
    60 {
    61     stringpair_list_t *sl = new_stringpair_list(NULL);
    62     if (!sl)
    63         return NULL;
    64     
    65     stringpair_list_t *_sl = sl;
    66     for (NSArray *pair in array) {
    67         stringpair_t *_sp = new_stringpair(
    68                [[pair[0] precomposedStringWithCanonicalMapping] UTF8String],
    69                [[pair[1] precomposedStringWithCanonicalMapping] UTF8String]);
    70         _sl = stringpair_list_add(_sl, _sp);
    71     }
    72     
    73     return sl;
    74 }
    75 
    76 
    77 NSArray *PEP_arrayFromBloblist(bloblist_t *bl)
    78 {
    79     NSMutableArray *array = [NSMutableArray array];
    80     
    81     for (bloblist_t *_bl = bl; _bl && _bl->value; _bl = _bl->next) {
    82         PEPAttachment* theAttachment = [[PEPAttachment alloc]
    83                                         initWithData:[NSData
    84                                                       dataWithBytes:_bl->value length:_bl->size]];
    85 
    86         if(_bl->filename && _bl->filename[0]) {
    87             theAttachment.filename = [NSString stringWithUTF8String:_bl->filename];
    88         }
    89 
    90         if(_bl->mime_type && _bl->mime_type[0]) {
    91             theAttachment.mimeType = [NSString stringWithUTF8String:_bl->mime_type];
    92         }
    93 
    94         theAttachment.contentDisposition = (PEPContentDisposition) _bl->disposition;
    95         
    96         [array addObject:theAttachment];
    97     }
    98     return array;
    99 }
   100 
   101 bloblist_t *PEP_arrayToBloblist(NSArray *array)
   102 {
   103     if (array.count == 0) {
   104         return nil;
   105     }
   106 
   107     bloblist_t *_bl = new_bloblist(NULL, 0, NULL, NULL);
   108     bloblist_t *bl =_bl;
   109 
   110     // free() might be the default, but let's be explicit
   111     bl->release_value = (void (*) (char *)) free;
   112 
   113     for (PEPAttachment *theAttachment in array) {
   114         NSData *data = theAttachment.data;
   115         size_t size = [data length];
   116 
   117         char *buf = malloc(size);
   118         assert(buf);
   119         memcpy(buf, [data bytes], size);
   120         
   121         bl = bloblist_add(bl, buf, size,
   122                           [[theAttachment.mimeType
   123                             precomposedStringWithCanonicalMapping]
   124                            UTF8String],
   125                           [[theAttachment.filename
   126                             precomposedStringWithCanonicalMapping]
   127                            UTF8String]);
   128 
   129         bl->disposition = (content_disposition_type) theAttachment.contentDisposition;
   130     }
   131     return _bl;
   132 }
   133 
   134 pEp_identity *PEP_identityDictToStruct(NSDictionary *dict)
   135 {
   136     pEp_identity *ident = new_identity([[[dict objectForKey:kPepAddress]
   137                                          precomposedStringWithCanonicalMapping] UTF8String],
   138                                        [[[dict objectForKey:kPepFingerprint]
   139                                          precomposedStringWithCanonicalMapping] UTF8String],
   140                                        [[[dict objectForKey:kPepUserID]
   141                                          precomposedStringWithCanonicalMapping] UTF8String],
   142                                        [[[dict objectForKey:kPepUsername]
   143                                          precomposedStringWithCanonicalMapping] UTF8String]);
   144     if ([dict objectForKey:kPepIsOwn]) {
   145         ident->me = ((NSNumber*)[dict objectForKey:kPepIsOwn]).boolValue;
   146     }
   147 
   148     if (dict && ident) {
   149         if ([dict objectForKey:@"lang"])
   150             strncpy(ident->lang, [[[dict objectForKey:@"lang"]
   151                                    precomposedStringWithCanonicalMapping] UTF8String], 2);
   152 
   153         if ([dict objectForKey:kPepCommType])
   154             ident->comm_type = [[dict objectForKey:kPepCommType] intValue];
   155     }
   156 
   157     return ident;
   158 }
   159 
   160 NSDictionary *PEP_identityDictFromStruct(pEp_identity *ident)
   161 {
   162     NSMutableDictionary *dict = [NSMutableDictionary new];
   163 
   164     if (ident) {
   165         if (ident->address && ident->address[0])
   166             [dict setObject:[NSString stringWithUTF8String:ident->address] forKey:kPepAddress];
   167 
   168         [dict setObject:[NSNumber numberWithBool: ident->me] forKey:kPepIsOwn];
   169         
   170         if (ident->fpr && ident->fpr[0])
   171             [dict setObject:[NSString stringWithUTF8String:ident->fpr] forKey:kPepFingerprint];
   172         
   173         if (ident->user_id && ident->user_id[0])
   174             [dict setObject:[NSString stringWithUTF8String:ident->user_id] forKey:kPepUserID];
   175         
   176         if (ident->username && ident->username[0])
   177             [dict setObject:[NSString stringWithUTF8String:ident->username] forKey:kPepUsername];
   178         
   179         if (ident->lang[0])
   180             [dict setObject:[NSString stringWithUTF8String:ident->lang] forKey:@"lang"];
   181         
   182         [dict setObject:[NSNumber numberWithInt: ident->comm_type] forKey:kPepCommType];
   183     }
   184     return dict;
   185 }
   186 
   187 //!!!: move. PEP_identityToStruct is not PEPMessage(Util) related. Should be an internal method on PEPIdentity. Somthing like .toStruct()
   188 //!!!: also check for other methods here. 
   189 pEp_identity *PEP_identityToStruct(PEPIdentity *identity)
   190 {
   191     pEp_identity *ident = new_identity([[identity.address
   192                                          precomposedStringWithCanonicalMapping] UTF8String],
   193                                        [[identity.fingerPrint
   194                                          precomposedStringWithCanonicalMapping] UTF8String],
   195                                        [[identity.userID
   196                                          precomposedStringWithCanonicalMapping] UTF8String],
   197                                        [[identity.userName
   198                                          precomposedStringWithCanonicalMapping] UTF8String]);
   199 
   200     ident->me = identity.isOwn;
   201 
   202     if (identity.language) {
   203         strncpy(ident->lang, [[identity.language
   204                                precomposedStringWithCanonicalMapping] UTF8String], 2);
   205     }
   206 
   207     ident->comm_type = (PEP_comm_type) identity.commType;
   208 
   209     return ident;
   210 }
   211 
   212 PEPIdentity *PEP_identityFromStruct(pEp_identity *ident)
   213 {
   214     PEPIdentity *identity = nil;
   215     if (ident->address && ident->address[0] && ident->user_id && ident->user_id[0]) {
   216         identity = [[PEPIdentity alloc]
   217                     initWithAddress:[NSString stringWithUTF8String:ident->address]
   218                     userID:[NSString stringWithUTF8String:ident->user_id]];
   219     }
   220 
   221     if (ident->fpr && ident->fpr[0]) {
   222         identity.fingerPrint = [NSString stringWithUTF8String:ident->fpr];
   223     }
   224 
   225     if (ident->username && ident->username[0]) {
   226         identity.userName = [NSString stringWithUTF8String:ident->username];
   227     }
   228 
   229     if (ident->lang[0]) {
   230         identity.language = [NSString stringWithUTF8String:ident->lang];
   231     }
   232 
   233     identity.commType = (PEPCommType) ident->comm_type;
   234 
   235     identity.isOwn = ident->me;
   236 
   237     return identity;
   238 }
   239 
   240 NSArray<PEPIdentity *> *PEP_arrayFromIdentityList(identity_list *il)
   241 {
   242     NSMutableArray *array = [NSMutableArray array];
   243     
   244     for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
   245         PEPIdentity *someIdent = PEP_identityFromStruct(il->ident);
   246         if (someIdent != nil) {
   247             [array addObject:someIdent];
   248         } else {
   249             NSLog(@"Odd nil identity");
   250         }
   251     }
   252     
   253     return array;
   254 }
   255 
   256 identity_list *PEP_identityArrayToList(NSArray<PEPIdentity *> *array)
   257 {
   258     identity_list *il = new_identity_list(NULL);
   259     if (!il)
   260         return NULL;
   261     
   262     identity_list *_il = il;
   263     for (NSMutableDictionary *address in array) {
   264         _il = identity_list_add(_il, PEP_identityDictToStruct(address));
   265     }
   266     
   267     return il;
   268 }
   269 
   270 NSDictionary *PEP_messageDictFromStruct(message *msg)
   271 {
   272     NSMutableDictionary *dict = [NSMutableDictionary new];
   273     if (msg && dict) {
   274         [dict replaceWithMessage:msg];
   275     }
   276     return dict;
   277 }
   278 
   279 PEPMessage * _Nullable pEpMessageFromStruct(message * _Nullable msg)
   280 {
   281     if (!msg) {
   282         return nil;
   283     }
   284     NSDictionary *dict = PEP_messageDictFromStruct(msg);
   285     PEPMessage *theMessage = [PEPMessage new];
   286     [theMessage setValuesForKeysWithDictionary:dict];
   287     return theMessage;
   288 }
   289 
   290 message *PEP_messageToStruct(PEPMessage *message) {
   291     return PEP_messageDictToStruct((NSDictionary *) message);
   292 }
   293 
   294 message *PEP_messageDictToStruct(NSDictionary *dict)
   295 {
   296     // Direction default to incoming
   297     PEP_msg_direction dir = PEP_dir_incoming;
   298     
   299     if ([dict objectForKey:@"outgoing"])
   300         dir = [[dict objectForKey:@"outgoing"] boolValue] ? PEP_dir_outgoing : PEP_dir_incoming;
   301     
   302     message *msg = new_message(dir);
   303     
   304     if(!msg)
   305         return NULL;
   306     
   307     if ([dict objectForKey:@"id"])
   308         msg->id = new_string([[[dict objectForKey:@"id"] precomposedStringWithCanonicalMapping]
   309                               UTF8String], 0);
   310     
   311     if ([dict objectForKey:@"shortmsg"])
   312         msg->shortmsg = new_string([[[dict objectForKey:@"shortmsg"]
   313                                      precomposedStringWithCanonicalMapping] UTF8String], 0);
   314 
   315     if ([dict objectForKey:@"sent"])
   316         msg->sent = new_timestamp([[dict objectForKey:@"sent"] timeIntervalSince1970]);
   317     
   318     if ([dict objectForKey:@"recv"])
   319         msg->recv = new_timestamp([[dict objectForKey:@"recv"] timeIntervalSince1970]);
   320     
   321     if ([dict objectForKey:kPepFrom])
   322         msg->from = PEP_identityDictToStruct([dict objectForKey:kPepFrom]);
   323 
   324     if ([dict objectForKey:@"to"])
   325         msg->to = PEP_identityArrayToList([dict objectForKey:@"to"]);
   326 
   327     if ([dict objectForKey:@"recv_by"])
   328         msg->recv_by = PEP_identityDictToStruct([dict objectForKey:@"recv_by"]);
   329 
   330     if ([dict objectForKey:@"cc"])
   331         msg->cc = PEP_identityArrayToList([dict objectForKey:@"cc"]);
   332 
   333     if ([dict objectForKey:@"bcc"])
   334         msg->bcc = PEP_identityArrayToList([dict objectForKey:@"bcc"]);
   335     
   336     if ([dict objectForKey:@"reply_to"])
   337         msg->reply_to = PEP_identityArrayToList([dict objectForKey:@"reply_to"]);
   338     
   339     if ([dict objectForKey:@"in_reply_to"])
   340         msg->in_reply_to = PEP_arrayToStringlist([dict objectForKey:@"in_reply_to"]);
   341     
   342     if ([dict objectForKey:@"references"])
   343         msg->references = PEP_arrayToStringlist([dict objectForKey:@"references"]);
   344     
   345     if ([dict objectForKey:kPepKeywords])
   346         msg->keywords = PEP_arrayToStringlist([dict objectForKey:kPepKeywords]);
   347 
   348     if ([dict objectForKey:@"opt_fields"])
   349         msg->opt_fields = PEP_arrayToStringPairlist([dict objectForKey:@"opt_fields"]);
   350     
   351     if ([dict objectForKey:@"longmsg"])
   352         msg->longmsg = new_string([[[dict objectForKey:@"longmsg"]
   353                                     precomposedStringWithCanonicalMapping] UTF8String], 0);
   354     
   355     if ([dict objectForKey:@"longmsg_formatted"])
   356         msg->longmsg_formatted = new_string([[[dict objectForKey:@"longmsg_formatted"]
   357                                               precomposedStringWithCanonicalMapping]
   358                                              UTF8String], 0);
   359 
   360     if ([dict objectForKey:@"attachments"])
   361         msg->attachments = PEP_arrayToBloblist([dict objectForKey:@"attachments"]);
   362 
   363     return msg;
   364 }