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