pEpObjCAdapter/PEPMessageUtil.m
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 10 Sep 2018 12:06:49 +0200
changeset 649 090fa902a419
parent 597 aa68b6d6a966
child 672 3a007d6c6c40
permissions -rw-r--r--
IOSAD-99 message *PEP_messageToStruct(PEPMessage *message)
     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 "PEPIdentity.h"
    12 #import "PEPMessage.h"
    13 #import "PEPAttachment.h"
    14 #import "NSMutableDictionary+PEP.h"
    15 
    16 #import "pEp_string.h"
    17 
    18 #pragma mark -- Constants
    19 
    20 NSString *const kPepUsername = @"username";
    21 
    22 NSString *const kPepAddress = @"address";
    23 
    24 NSString *const kPepUserID = @"user_id";
    25 
    26 NSString *const kPepIsOwn = @"isOwn";
    27 
    28 NSString *const kPepFingerprint = @"fpr";
    29 
    30 NSString *const kPepFrom = @"from";
    31 
    32 NSString *const kPepTo = @"to";
    33 
    34 NSString *const kPepCC = @"cc";
    35 
    36 NSString *const kPepBCC = @"bcc";
    37 
    38 NSString *const kPepShortMessage = @"shortmsg";
    39 
    40 NSString *const kPepLongMessage = @"longmsg";
    41 
    42 NSString *const kPepLongMessageFormatted = @"longmsg_formatted";
    43 
    44 NSString *const kPepOutgoing = @"outgoing";
    45 
    46 NSString *const kPepSent = @"sent";
    47 
    48 NSString *const kPepReceived = @"recv";
    49 
    50 NSString *const kPepID = @"id";
    51 
    52 NSString *const kPepReceivedBy = @"recv_by";
    53 NSString *const kPepReplyTo = @"reply_to";
    54 NSString *const kPepInReplyTo = @"in_reply_to";
    55 NSString *const kPepReferences = @"references";
    56 NSString *const kPepKeywords = @"keywords";
    57 NSString *const kPepOptFields = @"opt_fields";
    58 
    59 NSString *const kPepAttachments = @"attachments";
    60 
    61 NSString *const kPepCommType = @"comm_type";
    62 
    63 NSString *const kPepRawMessage = @"raw_message";
    64 
    65 NSString *const kXpEpVersion = @"X-pEp-Version";
    66 
    67 NSString *const kXEncStatus = @"X-EncStatus";
    68 
    69 NSString *const kXKeylist = @"X-KeyList";
    70 
    71 NSString *const _Nonnull kPepIsOwnIdentity = @"kPepIsOwnIdentity";
    72 
    73 NSArray *PEP_arrayFromStringlist(stringlist_t *sl)
    74 {
    75     NSMutableArray *array = [NSMutableArray array];
    76     
    77     for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) {
    78         [array addObject:[NSString stringWithUTF8String:_sl->value]];
    79     }
    80     
    81     return array;
    82 }
    83 
    84 stringlist_t *PEP_arrayToStringlist(NSArray *array)
    85 {
    86     stringlist_t *sl = new_stringlist(NULL);
    87     if (!sl)
    88         return NULL;
    89     
    90     stringlist_t *_sl = sl;
    91     for (NSString *str in array) {
    92         _sl = stringlist_add(_sl, [[str precomposedStringWithCanonicalMapping] UTF8String]);
    93     }
    94     
    95     return sl;
    96 }
    97 
    98 NSArray *PEP_arrayFromStringPairlist(stringpair_list_t *sl)
    99 {
   100     NSMutableArray *array = [NSMutableArray array];
   101 
   102     for (stringpair_list_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) {
   103         [array addObject:[[NSMutableArray alloc ]initWithObjects:
   104                 [NSString stringWithUTF8String:_sl->value->key],
   105                 [NSString stringWithUTF8String:_sl->value->value],
   106                 nil]];
   107     }
   108 
   109     return array;
   110 }
   111 
   112 stringpair_list_t *PEP_arrayToStringPairlist(NSArray *array)
   113 {
   114     stringpair_list_t *sl = new_stringpair_list(NULL);
   115     if (!sl)
   116         return NULL;
   117     
   118     stringpair_list_t *_sl = sl;
   119     for (NSArray *pair in array) {
   120         stringpair_t *_sp = new_stringpair(
   121                [[pair[0] precomposedStringWithCanonicalMapping] UTF8String],
   122                [[pair[1] precomposedStringWithCanonicalMapping] UTF8String]);
   123         _sl = stringpair_list_add(_sl, _sp);
   124     }
   125     
   126     return sl;
   127 }
   128 
   129 
   130 NSArray *PEP_arrayFromBloblist(bloblist_t *bl)
   131 {
   132     NSMutableArray *array = [NSMutableArray array];
   133     
   134     for (bloblist_t *_bl = bl; _bl && _bl->value; _bl = _bl->next) {
   135         PEPAttachment* theAttachment = [[PEPAttachment alloc]
   136                                         initWithData:[NSData
   137                                                       dataWithBytes:_bl->value length:_bl->size]];
   138 
   139         if(_bl->filename && _bl->filename[0]) {
   140             theAttachment.filename = [NSString stringWithUTF8String:_bl->filename];
   141         }
   142 
   143         if(_bl->mime_type && _bl->mime_type[0]) {
   144             theAttachment.mimeType = [NSString stringWithUTF8String:_bl->mime_type];
   145         }
   146 
   147         theAttachment.contentDisposition = _bl->disposition;
   148         
   149         [array addObject:theAttachment];
   150     }
   151     return array;
   152 }
   153 
   154 bloblist_t *PEP_arrayToBloblist(NSArray *array)
   155 {
   156     if (array.count == 0) {
   157         return nil;
   158     }
   159 
   160     bloblist_t *_bl = new_bloblist(NULL, 0, NULL, NULL);
   161     bloblist_t *bl =_bl;
   162 
   163     // free() might be the default, but let's be explicit
   164     bl->release_value = (void (*) (char *)) free;
   165 
   166     for (PEPAttachment *theAttachment in array) {
   167         NSData *data = theAttachment.data;
   168         size_t size = [data length];
   169 
   170         char *buf = malloc(size);
   171         assert(buf);
   172         memcpy(buf, [data bytes], size);
   173         
   174         bl = bloblist_add(bl, buf, size,
   175                           [[theAttachment.mimeType
   176                             precomposedStringWithCanonicalMapping]
   177                            UTF8String],
   178                           [[theAttachment.filename
   179                             precomposedStringWithCanonicalMapping]
   180                            UTF8String]);
   181 
   182         bl->disposition = theAttachment.contentDisposition;
   183     }
   184     return _bl;
   185 }
   186 
   187 pEp_identity *PEP_identityDictToStruct(NSDictionary *dict)
   188 {
   189     pEp_identity *ident = new_identity([[[dict objectForKey:kPepAddress]
   190                                          precomposedStringWithCanonicalMapping] UTF8String],
   191                                        [[[dict objectForKey:kPepFingerprint]
   192                                          precomposedStringWithCanonicalMapping] UTF8String],
   193                                        [[[dict objectForKey:kPepUserID]
   194                                          precomposedStringWithCanonicalMapping] UTF8String],
   195                                        [[[dict objectForKey:kPepUsername]
   196                                          precomposedStringWithCanonicalMapping] UTF8String]);
   197     if ([dict objectForKey:kPepIsOwn]) {
   198         ident->me = ((NSNumber*)[dict objectForKey:kPepIsOwn]).boolValue;
   199     }
   200 
   201     if (dict && ident) {
   202         if ([dict objectForKey:@"lang"])
   203             strncpy(ident->lang, [[[dict objectForKey:@"lang"]
   204                                    precomposedStringWithCanonicalMapping] UTF8String], 2);
   205 
   206         if ([dict objectForKey:kPepCommType])
   207             ident->comm_type = [[dict objectForKey:kPepCommType] intValue];
   208     }
   209 
   210     return ident;
   211 }
   212 
   213 NSDictionary *PEP_identityDictFromStruct(pEp_identity *ident)
   214 {
   215     NSMutableDictionary *dict = [NSMutableDictionary new];
   216 
   217     if (ident) {
   218         if (ident->address && ident->address[0])
   219             [dict setObject:[NSString stringWithUTF8String:ident->address] forKey:kPepAddress];
   220 
   221         [dict setObject:[NSNumber numberWithBool: ident->me] forKey:kPepIsOwn];
   222         
   223         if (ident->fpr && ident->fpr[0])
   224             [dict setObject:[NSString stringWithUTF8String:ident->fpr] forKey:kPepFingerprint];
   225         
   226         if (ident->user_id && ident->user_id[0])
   227             [dict setObject:[NSString stringWithUTF8String:ident->user_id] forKey:kPepUserID];
   228         
   229         if (ident->username && ident->username[0])
   230             [dict setObject:[NSString stringWithUTF8String:ident->username] forKey:kPepUsername];
   231         
   232         if (ident->lang[0])
   233             [dict setObject:[NSString stringWithUTF8String:ident->lang] forKey:@"lang"];
   234         
   235         [dict setObject:[NSNumber numberWithInt: ident->comm_type] forKey:kPepCommType];
   236     }
   237     return dict;
   238 }
   239 
   240 pEp_identity *PEP_identityToStruct(PEPIdentity *identity)
   241 {
   242     pEp_identity *ident = new_identity([[identity.address
   243                                          precomposedStringWithCanonicalMapping] UTF8String],
   244                                        [[identity.fingerPrint
   245                                          precomposedStringWithCanonicalMapping] UTF8String],
   246                                        [[identity.userID
   247                                          precomposedStringWithCanonicalMapping] UTF8String],
   248                                        [[identity.userName
   249                                          precomposedStringWithCanonicalMapping] UTF8String]);
   250 
   251     ident->me = identity.isOwn;
   252 
   253     if (identity.language) {
   254         strncpy(ident->lang, [[identity.language
   255                                precomposedStringWithCanonicalMapping] UTF8String], 2);
   256     }
   257 
   258     ident->comm_type = (PEP_comm_type) identity.commType;
   259 
   260     return ident;
   261 }
   262 
   263 PEPIdentity *PEP_identityFromStruct(pEp_identity *ident)
   264 {
   265     PEPIdentity *identity = nil;
   266     if (ident->address && ident->address[0]) {
   267         identity = [[PEPIdentity alloc]
   268                     initWithAddress:[NSString stringWithUTF8String:ident->address]];
   269     }
   270 
   271     if (ident->fpr && ident->fpr[0]) {
   272         identity.fingerPrint = [NSString stringWithUTF8String:ident->fpr];
   273     }
   274 
   275     if (ident->user_id && ident->user_id[0]) {
   276         identity.userID = [NSString stringWithUTF8String:ident->user_id];
   277     }
   278 
   279     if (ident->username && ident->username[0]) {
   280         identity.userName = [NSString stringWithUTF8String:ident->username];
   281     }
   282 
   283     if (ident->lang[0]) {
   284         identity.language = [NSString stringWithUTF8String:ident->lang];
   285     }
   286 
   287     identity.commType = ident->comm_type;
   288 
   289     return identity;
   290 }
   291 
   292 NSArray *PEP_arrayFromIdentityList(identity_list *il)
   293 {
   294     NSMutableArray *array = [NSMutableArray array];
   295     
   296     for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
   297         [array addObject:PEP_identityFromStruct(il->ident)];
   298     }
   299     
   300     return array;
   301 }
   302 
   303 identity_list *PEP_arrayToIdentityList(NSArray *array)
   304 {
   305     identity_list *il = new_identity_list(NULL);
   306     if (!il)
   307         return NULL;
   308     
   309     identity_list *_il = il;
   310     for (NSDictionary *dict in array) {
   311         _il = identity_list_add(_il, PEP_identityDictToStruct(dict));
   312     }
   313     
   314     return il;
   315 }
   316 
   317 identity_list *PEP_identityArrayToList(NSArray *array)
   318 {
   319     identity_list *il = new_identity_list(NULL);
   320     if (!il)
   321         return NULL;
   322     
   323     identity_list *_il = il;
   324     for (NSMutableDictionary *address in array) {
   325         _il = identity_list_add(_il, PEP_identityDictToStruct(address));
   326     }
   327     
   328     return il;
   329 }
   330 
   331 NSArray *PEP_identityArrayFromList(identity_list *il)
   332 {
   333     NSMutableArray *array = [NSMutableArray array];
   334     
   335     for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
   336         PEPIdentity *ident = PEP_identityFromStruct(_il->ident);
   337         [array addObject:ident];
   338     }
   339     
   340     return array;
   341 }
   342 
   343 NSDictionary *PEP_messageDictFromStruct(message *msg)
   344 {
   345     NSMutableDictionary *dict = [NSMutableDictionary new];
   346     if (msg && dict) {
   347         [dict replaceWithMessage:msg];
   348     }
   349     return dict;
   350 }
   351 
   352 PEPMessage * _Nullable pEpMessageFromStruct(message * _Nullable msg)
   353 {
   354     if (!msg) {
   355         return nil;
   356     }
   357     NSDictionary *dict = PEP_messageDictFromStruct(msg);
   358     PEPMessage *theMessage = [PEPMessage new];
   359     [theMessage setValuesForKeysWithDictionary:dict];
   360     return theMessage;
   361 }
   362 
   363 message *PEP_messageToStruct(PEPMessage *message) {
   364     return PEP_messageDictToStruct((NSDictionary *) message);
   365 }
   366 
   367 message *PEP_messageDictToStruct(NSDictionary *dict)
   368 {
   369     // Direction default to incoming
   370     PEP_msg_direction dir = PEP_dir_incoming;
   371     
   372     if ([dict objectForKey:@"outgoing"])
   373         dir = [[dict objectForKey:@"outgoing"] boolValue] ? PEP_dir_outgoing : PEP_dir_incoming;
   374     
   375     message *msg = new_message(dir);
   376     
   377     if(!msg)
   378         return NULL;
   379     
   380     if ([dict objectForKey:@"id"])
   381         msg->id = new_string([[[dict objectForKey:@"id"] precomposedStringWithCanonicalMapping]
   382                               UTF8String], 0);
   383     
   384     if ([dict objectForKey:@"shortmsg"])
   385         msg->shortmsg = new_string([[[dict objectForKey:@"shortmsg"]
   386                                      precomposedStringWithCanonicalMapping] UTF8String], 0);
   387 
   388     if ([dict objectForKey:@"sent"])
   389         msg->sent = new_timestamp([[dict objectForKey:@"sent"] timeIntervalSince1970]);
   390     
   391     if ([dict objectForKey:@"recv"])
   392         msg->recv = new_timestamp([[dict objectForKey:@"recv"] timeIntervalSince1970]);
   393     
   394     if ([dict objectForKey:kPepFrom])
   395         msg->from = PEP_identityDictToStruct([dict objectForKey:kPepFrom]);
   396 
   397     if ([dict objectForKey:@"to"])
   398         msg->to = PEP_identityArrayToList([dict objectForKey:@"to"]);
   399 
   400     if ([dict objectForKey:@"recv_by"])
   401         msg->recv_by = PEP_identityDictToStruct([dict objectForKey:@"recv_by"]);
   402 
   403     if ([dict objectForKey:@"cc"])
   404         msg->cc = PEP_identityArrayToList([dict objectForKey:@"cc"]);
   405 
   406     if ([dict objectForKey:@"bcc"])
   407         msg->bcc = PEP_identityArrayToList([dict objectForKey:@"bcc"]);
   408     
   409     if ([dict objectForKey:@"reply_to"])
   410         msg->reply_to = PEP_identityArrayToList([dict objectForKey:@"reply_to"]);
   411     
   412     if ([dict objectForKey:@"in_reply_to"])
   413         msg->in_reply_to = PEP_arrayToStringlist([dict objectForKey:@"in_reply_to"]);
   414     
   415     if ([dict objectForKey:@"references"])
   416         msg->references = PEP_arrayToStringlist([dict objectForKey:@"references"]);
   417     
   418     if ([dict objectForKey:kPepKeywords])
   419         msg->keywords = PEP_arrayToStringlist([dict objectForKey:kPepKeywords]);
   420 
   421     if ([dict objectForKey:@"opt_fields"])
   422         msg->opt_fields = PEP_arrayToStringPairlist([dict objectForKey:@"opt_fields"]);
   423     
   424     if ([dict objectForKey:@"longmsg"])
   425         msg->longmsg = new_string([[[dict objectForKey:@"longmsg"]
   426                                     precomposedStringWithCanonicalMapping] UTF8String], 0);
   427     
   428     if ([dict objectForKey:@"longmsg_formatted"])
   429         msg->longmsg_formatted = new_string([[[dict objectForKey:@"longmsg_formatted"]
   430                                               precomposedStringWithCanonicalMapping]
   431                                              UTF8String], 0);
   432 
   433     if ([dict objectForKey:@"attachments"])
   434         msg->attachments = PEP_arrayToBloblist([dict objectForKey:@"attachments"]);
   435 
   436     return msg;
   437 }