File: MAPIStoreMessage.m

package info (click to toggle)
sogo 2.2.9%2Bgit20141017-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 43,812 kB
  • sloc: objc: 115,592; python: 5,696; sh: 1,380; perl: 861; makefile: 240; xml: 114; sql: 53; php: 43
file content (994 lines) | stat: -rw-r--r-- 27,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/* MAPIStoreMessage.m - this file is part of SOGo
 *
 * Copyright (C) 2011-2012 Inverse inc
 *
 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <Foundation/NSArray.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSString.h>
#import <Foundation/NSURL.h>
#import <NGExtensions/NSObject+Logs.h>
#import <SOGo/SOGoObject.h>
#import <SOGo/SOGoPermissions.h>
#import <SOGo/SOGoUser.h>

#import "MAPIStoreActiveTables.h"
#import "MAPIStoreAttachment.h"
#import "MAPIStoreAttachmentTable.h"
#import "MAPIStoreContext.h"
#import "MAPIStoreEmbeddedMessage.h"
#import "MAPIStoreFolder.h"
#import "MAPIStoreMessageTable.h"
#import "MAPIStorePropertySelectors.h"
#import "MAPIStoreSamDBUtils.h"
#import "MAPIStoreTypes.h"
#import "MAPIStoreUserContext.h"
#import "NSData+MAPIStore.h"
#import "NSObject+MAPIStore.h"
#import "NSString+MAPIStore.h"
#import "RTFHandler.h"

#import "MAPIStoreMessage.h"

#undef DEBUG
#include <stdbool.h>
#include <gen_ndr/exchange.h>
#include <mapistore/mapistore.h>
#include <mapistore/mapistore_errors.h>

static Class MAPIStoreFolderK, MAPIStoreEmbeddedMessageK;

static NSString *resourcesDir = nil;

static NSData *
uncompressRTF (NSData *compressedRTF)
{
  NSData *rtfData = nil;
  DATA_BLOB *rtf;
  TALLOC_CTX *mem_ctx;

  mem_ctx = talloc_zero (NULL, TALLOC_CTX);
  rtf = talloc_zero (mem_ctx, DATA_BLOB);

  if (uncompress_rtf (mem_ctx,
                      (uint8_t *) [compressedRTF bytes], [compressedRTF length],
                      rtf)
      == MAPI_E_SUCCESS)
    rtfData = [NSData dataWithBytes: rtf->data length: rtf->length];

  talloc_free (mem_ctx);

  return rtfData;
}

static NSData *
rtf2html (NSData *compressedRTF)
{
  NSData *rtf;
  NSMutableData *html = nil;

  rtf = uncompressRTF (compressedRTF);
  if (rtf)
    {
      //html = [NSMutableData data];
      RTFHandler *handler;

      handler = [[RTFHandler alloc] initWithData: rtf];
      AUTORELEASE(handler);

      html = [handler parse];
    }

  return html;
}

@interface SOGoObject (MAPIStoreProtocol)

- (NSString *) davContentLength;

@end

@implementation MAPIStoreMessage

+ (void) initialize
{
  if (!resourcesDir)
    {
      resourcesDir = [[NSBundle bundleForClass: self] resourcePath];
      [resourcesDir retain];
    }
  MAPIStoreFolderK = [MAPIStoreFolder class];
  MAPIStoreEmbeddedMessageK = [MAPIStoreEmbeddedMessage class];
}

- (id) init
{
  //[self logWithFormat: @"METHOD '%s' (%d) (%d)", __FUNCTION__, __LINE__, self];

  if ((self = [super init]))
    {
      attachmentParts = [NSMutableDictionary new];
      activeTables = [NSMutableArray new];
      activeUserRoles = nil;
    }

  return self;
}

- (void) dealloc
{
  //[self logWithFormat: @"METHOD '%s' (%d) (%d)", __FUNCTION__, __LINE__, self];
  [activeUserRoles release];
  [attachmentKeys release];
  [attachmentParts release];
  [activeTables release];
  [super dealloc];
}

- (void) getMessageData: (struct mapistore_message **) dataPtr
               inMemCtx: (TALLOC_CTX *) memCtx
{
  void *propValue;
  struct mapistore_message *msgData;

  // [self logWithFormat: @"INCOMPLETE METHOD '%s' (%d): no recipient handling",
  //       __FUNCTION__, __LINE__];

  msgData = talloc_zero (memCtx, struct mapistore_message);
  
  if ([self getPidTagSubjectPrefix: &propValue
                          inMemCtx: msgData] == MAPISTORE_SUCCESS
      && propValue)
    msgData->subject_prefix = propValue;
  else
    msgData->subject_prefix = "";

  if ([self getPidTagNormalizedSubject: &propValue
                              inMemCtx: msgData] == MAPISTORE_SUCCESS
      && propValue)
    msgData->normalized_subject = propValue;
  else
    msgData->normalized_subject = "";

  msgData->columns = talloc_zero(msgData, struct SPropTagArray);
  msgData->recipients_count = 0;
  *dataPtr = msgData;
}

- (NSDictionary *) _convertRecipient: (struct mapistore_message_recipient *) recipient
                          andColumns: (struct SPropTagArray *) columns
{
  NSMutableDictionary *recipientProperties;
  SOGoUser *recipientUser;
  NSUInteger count;
  id value;

  recipientProperties = [NSMutableDictionary dictionaryWithCapacity: columns->cValues + 2];

  if (recipient->username)
    {
      value = [NSString stringWithUTF8String: recipient->username];
      [recipientProperties setObject: value forKey: @"x500dn"];

      recipientUser = [SOGoUser userWithLogin: [value lowercaseString]];
      if (recipientUser)
        {
          value = [recipientUser cn];
          if ([value length] > 0)
            [recipientProperties setObject: value forKey: @"fullName"];
          value = [[recipientUser allEmails] objectAtIndex: 0];
          if ([value length] > 0)
            [recipientProperties setObject: value forKey: @"email"];
        }
    }
  else
    {
      if (recipient->data[0])
        {
          value = [NSString stringWithUTF8String: recipient->data[0]];
          if ([value length] > 0)
            [recipientProperties setObject: value forKey: @"fullName"];
        }
      if (recipient->data[1])
        {
          value = [NSString stringWithUTF8String: recipient->data[1]];
          if ([value length] > 0)
            [recipientProperties setObject: value forKey: @"email"];
        }
    }

  for (count = 0; count < columns->cValues; count++)
    {
      if (recipient->data[count])
        {
          value = NSObjectFromValuePointer (columns->aulPropTag[count],
                                            recipient->data[count]);
          if (value)
            [recipientProperties setObject: value
                                    forKey: MAPIPropertyKey (columns->aulPropTag[count])];
        }
    }

  return recipientProperties;
}

- (int) modifyRecipientsWithRecipients: (struct mapistore_message_recipient *) newRecipients
                              andCount: (NSUInteger) max
                            andColumns: (struct SPropTagArray *) columns;
{
  static NSString *recTypes[] = { @"orig", @"to", @"cc", @"bcc" };
  NSDictionary *recipientProperties;
  NSMutableDictionary *recipients;
  NSMutableArray *list;
  NSString *recType;
  struct mapistore_message_recipient *recipient;
  NSUInteger count;

  //[self logWithFormat: @"METHOD '%s'", __FUNCTION__];

  recipients = [NSMutableDictionary new];
  recipientProperties = [NSDictionary dictionaryWithObject: recipients
                                                    forKey: @"recipients"];
  [recipients release];

  for (count = 0; count < max; count++)
    {
      recipient = newRecipients + count;

      if (recipient->type >= MAPI_ORIG && recipient->type <= MAPI_BCC)
        {
          recType = recTypes[recipient->type];
          list = [recipients objectForKey: recType];
          if (!list)
            {
              list = [NSMutableArray new];
              [recipients setObject: list forKey: recType];
              [list release];
            }
          [list addObject:
                  [self _convertRecipient: recipient andColumns: columns]];
        }
    }
  [self addProperties: recipientProperties];

  return MAPISTORE_SUCCESS;
}

- (int) addPropertiesFromRow: (struct SRow *) aRow
{
  enum mapistore_error rc;
  MAPIStoreContext *context;
  SOGoUser *ownerUser;
  BOOL userIsOwner;
  MAPIStoreMessage *mainMessage;

  context = [self context];
  ownerUser = [[self userContext] sogoUser];
  userIsOwner = [[context activeUser] isEqual: ownerUser];
  if (userIsOwner)
    mainMessage = nil;
  else if ([self isKindOfClass: MAPIStoreEmbeddedMessageK])
    mainMessage = (MAPIStoreMessage *) [[self container] container];
  else
    mainMessage = self;

  if (userIsOwner || [mainMessage subscriberCanModifyMessage])
    rc = [super addPropertiesFromRow: aRow];
  else
    rc = MAPISTORE_ERR_DENIED;

  return rc;
}

- (void) addProperties: (NSDictionary *) newNewProperties
{
  NSData *htmlData, *rtfData;
  static NSNumber *htmlKey = nil, *rtfKey = nil;

  /* we intercept any RTF content and convert it to HTML */
  [super addProperties: newNewProperties];

  if (!htmlKey)
    {
      htmlKey = MAPIPropertyKey (PR_HTML);
      [htmlKey retain];
    }

  if (!rtfKey)
    {
      rtfKey = MAPIPropertyKey (PR_RTF_COMPRESSED);
      [rtfKey retain];
    }

  if (![properties objectForKey: htmlKey])
    {
      rtfData = [properties objectForKey: rtfKey];
      if (rtfData)
        {
          htmlData = rtf2html (rtfData);
          [properties setObject: htmlData forKey: htmlKey];
          [properties removeObjectForKey: rtfKey];
          [properties removeObjectForKey: MAPIPropertyKey (PR_RTF_IN_SYNC)];
        }
    }
}

- (MAPIStoreAttachment *) createAttachment
{
  MAPIStoreAttachment *newAttachment;
  uint32_t newAid;
  NSString *newKey;

  newAid = [[self attachmentKeys] count];

  newAttachment = [MAPIStoreAttachment mapiStoreObjectInContainer: self];
  // [newAttachment setIsNew: YES];
  [newAttachment setAID: newAid];
  newKey = [NSString stringWithFormat: @"%ul", newAid];
  [attachmentParts setObject: newAttachment
                      forKey: newKey];
  [attachmentKeys release];
  attachmentKeys = nil;

  return newAttachment;
}

- (int) createAttachment: (MAPIStoreAttachment **) attachmentPtr
                   inAID: (uint32_t *) aidPtr
{
  MAPIStoreAttachment *attachment;
  int rc = MAPISTORE_SUCCESS;

  attachment = [self createAttachment];
  if (attachment)
    {
      *attachmentPtr = attachment;
      *aidPtr = [attachment AID];
    }
  else
    rc = MAPISTORE_ERR_NOT_FOUND;

  return rc;
}

- (id) lookupAttachment: (NSString *) childKey
{
  [self subclassResponsibility: _cmd];

  return nil;
}

- (int) getAttachment: (MAPIStoreAttachment **) attachmentPtr
              withAID: (uint32_t) aid
{
  MAPIStoreAttachment *attachment;
  NSArray *keys;
  int rc = MAPISTORE_ERR_NOT_FOUND;

  keys = [self attachmentKeys];
  if (aid < [keys count])
    {
      attachment = [self lookupAttachment: [keys objectAtIndex: aid]];
      if (attachment)
        {
          *attachmentPtr = attachment;
          rc = MAPISTORE_SUCCESS;
        }
    }

  return rc;
}

- (int) getAttachmentTable: (MAPIStoreAttachmentTable **) tablePtr
               andRowCount: (uint32_t *) countPtr
{
  MAPIStoreAttachmentTable *attTable;
  int rc = MAPISTORE_SUCCESS;

  attTable = [self attachmentTable];
  if (attTable)
    {
      *tablePtr = attTable;
      *countPtr = [[attTable childKeys] count];
    }
  else
    rc = MAPISTORE_ERR_NOT_FOUND;

  return rc;
}

- (NSArray *) activeContainerMessageTables
{
  return [[MAPIStoreActiveTables activeTables]
           activeTablesForFMID: [container objectId]
                       andType: MAPISTORE_MESSAGE_TABLE];
}

- (void) copyToMessage: (MAPIStoreMessage *) newMessage  inMemCtx: (TALLOC_CTX *) memCtx;
  
{
  //TALLOC_CTX *memCtx;
  struct mapistore_message *messageData;
  NSArray *keys;
  NSUInteger count, max;
  NSString *key;
  MAPIStoreAttachment *attachment, *newAttachment;

  //[self logWithFormat: @"METHOD '%s' (%d) (%d)", __FUNCTION__, __LINE__, self];
  
  //memCtx = talloc_zero (NULL, TALLOC_CTX);

  /* message headers and recipients */
  [self getMessageData: &messageData inMemCtx: memCtx];
  [newMessage modifyRecipientsWithRecipients: messageData->recipients
                                    andCount: messageData->recipients_count
                                  andColumns: messageData->columns];

  /* properties */
  [self copyPropertiesToObject: newMessage  inMemCtx: memCtx];
  
  /* attachments */
  keys = [self attachmentKeys];
  max = [keys count];
  for (count = 0; count < max; count++)
    {
      key = [keys objectAtIndex: count];
      attachment = [self lookupAttachment: key];
      newAttachment = [newMessage createAttachment];
      [attachment copyToAttachment: newAttachment  inMemCtx: memCtx];
    }

  //talloc_free (memCtx);
}

- (enum mapistore_error) saveMessage: (TALLOC_CTX *) memCtx
{
  enum mapistore_error rc;
  NSArray *containerTables = nil /* Silence GCC warning */;
  NSUInteger count, max = 0 /* Silence GCC warning */;
  struct mapistore_object_notification_parameters *notif_parameters;
  uint64_t folderId;
  struct mapistore_context *mstoreCtx;
  MAPIStoreContext *context;
  SOGoUser *ownerUser;
  BOOL userIsOwner;
  MAPIStoreMessage *mainMessage;

  //[self logWithFormat: @"METHOD '%s' (%d)", __FUNCTION__, __LINE__];
  
  context = [self context];
  ownerUser = [[self userContext] sogoUser];
  userIsOwner = [[context activeUser] isEqual: ownerUser];
  if (userIsOwner)
    mainMessage = nil;
  else if ([self isKindOfClass: MAPIStoreEmbeddedMessageK])
    mainMessage = (MAPIStoreMessage *) [[self container] container];
  else
    mainMessage = self;

  if (userIsOwner
      || ([self isKindOfClass: MAPIStoreEmbeddedMessageK]
          && [mainMessage subscriberCanModifyMessage])
      || (![self isKindOfClass: MAPIStoreEmbeddedMessageK]
          && ((isNew
               && [(MAPIStoreFolder *) container subscriberCanCreateMessages])
              || (!isNew && [self subscriberCanModifyMessage]))))
    {
      /* notifications */
      if ([container isKindOfClass: MAPIStoreFolderK])
        {
          folderId = [(MAPIStoreFolder *) container objectId];
          mstoreCtx = [[self context] connectionInfo]->mstore_ctx;

          /* folder modified */
          notif_parameters
            = talloc_zero(memCtx, struct mapistore_object_notification_parameters);
          notif_parameters->object_id = folderId;
          if (isNew)
            {
              notif_parameters->tag_count = 3;
              notif_parameters->tags = talloc_array (notif_parameters,
                                                     enum MAPITAGS, 3);
              notif_parameters->tags[0] = PR_CONTENT_COUNT;
              notif_parameters->tags[1] = PR_MESSAGE_SIZE;
              notif_parameters->tags[2] = PR_NORMAL_MESSAGE_SIZE;
              notif_parameters->new_message_count = true;
              notif_parameters->message_count
                = [[(MAPIStoreFolder *) container messageKeys] count] + 1;
            }
          mapistore_push_notification (mstoreCtx,
                                       MAPISTORE_FOLDER,
                                       MAPISTORE_OBJECT_MODIFIED,
                                       notif_parameters);
          talloc_free (notif_parameters);

          /* message created */
          if (isNew)
            {
              notif_parameters
                = talloc_zero(memCtx,
                              struct mapistore_object_notification_parameters);
              notif_parameters->object_id = [self objectId];
              notif_parameters->folder_id = folderId;
      
              notif_parameters->tag_count = 0xffff;
              mapistore_push_notification (mstoreCtx,
                                           MAPISTORE_MESSAGE, MAPISTORE_OBJECT_CREATED,
                                           notif_parameters);
              talloc_free (notif_parameters);
            }

          /* we ensure the table caches are loaded so that old and new state
             can be compared */
          containerTables = [self activeContainerMessageTables];
          max = [containerTables count];
          for (count = 0; count < max; count++)
            [[containerTables objectAtIndex: count] restrictedChildKeys];
        }
  
      [self save: memCtx];
      /* We make sure that any change-related properties are removes from the
         properties dictionary, to make sure that related methods will be
         invoked the next time they are requested. */
      [properties removeObjectForKey: MAPIPropertyKey (PidTagChangeKey)];
      [properties removeObjectForKey: MAPIPropertyKey (PidTagChangeNumber)];

      if ([container isKindOfClass: MAPIStoreFolderK])
        {
          /* table modified */
          for (count = 0; count < max; count++)
	    {
	      id table;

	      table = [containerTables objectAtIndex: count];
	      
	      /* Safety check here as we could have MAPIStorePermissionsTable instances
		 in our containerTables array. This code might need to be reworked later */
	      if ([table respondsToSelector: @selector(notifyChangesForChild:)])
		[table notifyChangesForChild: self];
	    }
          [container cleanupCaches];
        }
      [self setIsNew: NO];
      rc = MAPISTORE_SUCCESS;
    }
  else
    rc = MAPISTORE_ERR_DENIED;

  return rc;
}

/* getters */
- (int) getPidTagInstID: (void **) data // TODO: DOUBT
               inMemCtx: (TALLOC_CTX *) memCtx
{
  /* we return a unique id based on the key */
  *data = MAPILongLongValue (memCtx, [[sogoObject nameInContainer] hash]);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagInstanceNum: (void **) data // TODO: DOUBT
                    inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getLongZero: data inMemCtx: memCtx];
}

- (int) getPidTagRowType: (void **) data // TODO: DOUBT
                inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = MAPILongValue (memCtx, TBL_LEAF_ROW);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagDepth: (void **) data // TODO: DOUBT
              inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = MAPILongLongValue (memCtx, 0);

  return MAPISTORE_SUCCESS;
}

/*
  Possible values are:
  
  0x00000001 Modify
  0x00000002 Read
  0x00000004 Delete
  0x00000008 Create Hierarchy Table
  0x00000010 Create Contents Table
  0x00000020 Create Associated Contents Table
*/
- (int) getPidTagAccess: (void **) data
               inMemCtx: (TALLOC_CTX *) memCtx
{
  uint32_t access = 0;
  BOOL userIsOwner;
  MAPIStoreContext *context;
  SOGoUser *ownerUser;
  MAPIStoreMessage *mainMessage;

  context = [self context];
  ownerUser = [[self userContext] sogoUser];
  userIsOwner = [[context activeUser] isEqual: ownerUser];
  if (userIsOwner)
    mainMessage = nil;
  else if ([self isKindOfClass: MAPIStoreEmbeddedMessageK])
    mainMessage = (MAPIStoreMessage *) [[self container] container];
  else
    mainMessage = self;

  if (userIsOwner || [mainMessage subscriberCanModifyMessage])
    access |= 0x01;
  if (userIsOwner || [mainMessage subscriberCanReadMessage])
    access |= 0x02;
  if (userIsOwner
      || ([self isKindOfClass: MAPIStoreEmbeddedMessageK]
          && [mainMessage subscriberCanModifyMessage])
      || [(MAPIStoreFolder *)
           [mainMessage container] subscriberCanDeleteMessages])
    access |= 0x04;
  
  *data = MAPILongValue (memCtx, access);

  return MAPISTORE_SUCCESS;
}

/*
  Possible values are:

  0x00000000 Read-Only
  0x00000001 Modify
*/
- (int) getPidTagAccessLevel: (void **) data
                    inMemCtx: (TALLOC_CTX *) memCtx
{
  uint32_t access = 0;
  BOOL userIsOwner;
  MAPIStoreContext *context;
  SOGoUser *ownerUser;

  context = [self context];
  ownerUser = [[self userContext] sogoUser];
  userIsOwner = [[context activeUser] isEqual: ownerUser];
  if (userIsOwner || [self subscriberCanModifyMessage])
    access = 0x01;
  else
    access = 0;
  *data = MAPILongValue (memCtx, access);

  return MAPISTORE_SUCCESS;
}

- (int ) getPidTagHasNamedProperties: (void **) data
                            inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getYes: data inMemCtx: memCtx];
}

- (int) getPidLidSideEffects: (void **) data // TODO
                    inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getLongZero: data inMemCtx: memCtx];
}

- (int) getPidLidCurrentVersion: (void **) data
                       inMemCtx: (TALLOC_CTX *) memCtx
{
  // *data = MAPILongValue (memCtx, 115608); // Outlook 11.5608
  *data = MAPILongValue (memCtx, 0x1ce3a); // Outlook 11.8330

  return MAPISTORE_SUCCESS;
}

- (int) getPidLidCurrentVersionName: (void **) data
                           inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = [@"11.0" asUnicodeInMemCtx: memCtx];

  return MAPISTORE_SUCCESS;
}

- (int) getPidLidAutoProcessState: (void **) data
                         inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = MAPILongValue (memCtx, 0x00000000);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagFolderId: (void **) data
                 inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = MAPILongLongValue (memCtx, [container objectId]);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagMid: (void **) data
            inMemCtx: (TALLOC_CTX *) memCtx
{
  int rc;
  uint64_t obId;

  obId = [self objectId];
  if (obId == ULLONG_MAX)
    rc = MAPISTORE_ERR_NOT_FOUND;
  else
    {
      *data = MAPILongLongValue (memCtx, obId);
      rc = MAPISTORE_SUCCESS;
    }

  return rc;
}

- (int) getPidTagMessageLocaleId: (void **) data
                        inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = MAPILongValue (memCtx, 0x0409);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagMessageFlags: (void **) data // TODO
                     inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = MAPILongValue (memCtx, MSGFLAG_FROMME | MSGFLAG_READ | MSGFLAG_UNMODIFIED);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagMessageSize: (void **) data // TODO
                    inMemCtx: (TALLOC_CTX *) memCtx
{
  /* TODO: choose another name in SOGo for that method */
  *data = MAPILongValue (memCtx, [[sogoObject davContentLength] intValue]);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagImportance: (void **) data // TODO -> subclass?
                   inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = MAPILongValue (memCtx, 1);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagPriority: (void **) data // TODO -> subclass?
                 inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getLongZero: data inMemCtx: memCtx];
}

- (int) getPidTagSensitivity: (void **) data // TODO -> subclass in calendar
                    inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getLongZero: data inMemCtx: memCtx];
}

- (int) getPidTagSubject: (void **) data
                inMemCtx: (TALLOC_CTX *) memCtx
{
  int rc;
  TALLOC_CTX *localMemCtx;
  char *prefix, *normalizedSubject;

  localMemCtx = talloc_zero (NULL, TALLOC_CTX);
  if ([self getProperty: (void **) &prefix
                withTag: PidTagSubjectPrefix
               inMemCtx: localMemCtx]
      != MAPISTORE_SUCCESS)
    prefix = "";
  rc = [self getProperty: (void **) &normalizedSubject
                 withTag: PidTagNormalizedSubject
                inMemCtx: localMemCtx];
  if (rc == MAPISTORE_SUCCESS)
    *data = talloc_asprintf (memCtx, "%s%s", prefix, normalizedSubject);

  return rc;
}

- (int) getPidTagNormalizedSubject: (void **) data
                          inMemCtx: (TALLOC_CTX *) memCtx
{
  return MAPISTORE_ERR_NOT_FOUND;
}

- (int) getPidTagOriginalSubject: (void **) data
                        inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getPidTagSubject: data inMemCtx: memCtx];
}

- (int) getPidTagConversationTopic: (void **) data
                          inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getPidTagNormalizedSubject: data inMemCtx: memCtx];
}

- (int) getPidTagSubjectPrefix: (void **) data
                      inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getEmptyString: data inMemCtx: memCtx];
}

- (int) getPidTagDeleteAfterSubmit: (void **) data // TODO
                          inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getNo: data inMemCtx: memCtx];
}

- (int) getPidTagDisplayTo: (void **) data
                  inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getEmptyString: data inMemCtx: memCtx];
}

- (int) getPidTagDisplayCc: (void **) data
                  inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getEmptyString: data inMemCtx: memCtx];
}

- (int) getPidTagDisplayBcc: (void **) data
                   inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getEmptyString: data inMemCtx: memCtx];
}

// - (int) getPidTagOriginalDisplayTo: (void **) data
// {
//   return [self getPidTagDisplayTo: data];
// }

// - (int) getPidTagOriginalDisplayCc: (void **) data
// {
//   return [self getPidTagDisplayCc: data];
// }

// - (int) getPidTagOriginalDisplayBcc: (void **) data
// {
//   return [self getPidTagDisplayBcc: data];
// }

- (int) getPidTagLastModifierName: (void **) data
                         inMemCtx: (TALLOC_CTX *) memCtx
{
  NSURL *contextUrl;

  contextUrl = (NSURL *) [[self context] url];
  *data = [[contextUrl user] asUnicodeInMemCtx: memCtx];

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagMessageClass: (void **) data
                     inMemCtx: (TALLOC_CTX *) memCtx
{
  [self subclassResponsibility: _cmd];

  return MAPISTORE_ERR_NOT_FOUND;
}

- (int) getPidTagOriginalMessageClass: (void **) data
                             inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getProperty: data withTag: PidTagMessageClass inMemCtx: memCtx];
}

- (int) getPidTagHasAttachments: (void **) data
                       inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = MAPIBoolValue (memCtx,
                         [[self attachmentKeys] count] > 0);

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagAssociated: (void **) data
                   inMemCtx: (TALLOC_CTX *) memCtx
{
  return [self getNo: data inMemCtx: memCtx];;
}

- (int) setReadFlag: (uint8_t) flag
{
  // [self subclassResponsibility: _cmd];

  return MAPISTORE_ERROR;
}

- (void) save: (TALLOC_CTX *) memCtx
{
  [self subclassResponsibility: _cmd];
}

- (NSArray *) attachmentKeys
{
  if (!attachmentKeys)
    {
      attachmentKeys = [self attachmentKeysMatchingQualifier: nil
                                            andSortOrderings: nil];
      [attachmentKeys retain];
    }

  return attachmentKeys;
}

- (NSArray *) attachmentKeysMatchingQualifier: (EOQualifier *) qualifier
                             andSortOrderings: (NSArray *) sortOrderings
{
  if (qualifier)
    [self errorWithFormat: @"qualifier is not used for attachments"];
  if (sortOrderings)
    [self errorWithFormat: @"sort orderings are not used for attachments"];
  
  return [attachmentParts allKeys];
}

- (MAPIStoreAttachmentTable *) attachmentTable
{
  return [MAPIStoreAttachmentTable tableForContainer: self];
}

- (void) addActiveTable: (MAPIStoreTable *) activeTable
{
  [activeTables addObject: activeTable];
}

- (void) removeActiveTable: (MAPIStoreTable *) activeTable
{
  [activeTables removeObject: activeTable];
}

- (NSArray *) activeUserRoles
{
  MAPIStoreContext *context;
  MAPIStoreUserContext *userContext;

  if (!activeUserRoles)
    {
      context = [self context];
      userContext = [self userContext];
      activeUserRoles = [[context activeUser]
                          rolesForObject: sogoObject
                               inContext: [userContext woContext]];
      [activeUserRoles retain];
    }

  return activeUserRoles;
}

- (BOOL) subscriberCanReadMessage
{
  return NO;
}

- (BOOL) subscriberCanModifyMessage
{
  return NO;
}

@end