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