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