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