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