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 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
|
/* MAPIStoreMailFolder.m - this file is part of SOGo
*
* Copyright (C) 2011-2013 Inverse inc
*
* 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.
*/
#include <talloc.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSString.h>
#import <Foundation/NSURL.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <EOControl/EOQualifier.h>
#import <EOControl/EOSortOrdering.h>
#import <NGExtensions/NSObject+Logs.h>
#import <NGExtensions/NSObject+Values.h>
#import <NGExtensions/NSString+misc.h>
#import <NGImap4/NGImap4Connection.h>
#import <NGImap4/NGImap4Client.h>
#import <NGImap4/NSString+Imap4.h>
#import <Mailer/SOGoDraftsFolder.h>
#import <Mailer/SOGoMailAccount.h>
#import <Mailer/SOGoMailAccounts.h>
#import <Mailer/SOGoMailFolder.h>
#import <Mailer/SOGoMailObject.h>
#import <Mailer/SOGoSentFolder.h>
#import <Mailer/SOGoTrashFolder.h>
#import <SOGo/NSArray+Utilities.h>
#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoPermissions.h>
#import "MAPIApplication.h"
#import "MAPIStoreAppointmentWrapper.h"
#import "MAPIStoreContext.h"
#import "MAPIStoreFAIMessage.h"
#import "MAPIStoreMailContext.h"
#import "MAPIStoreMailMessage.h"
#import "MAPIStoreMailMessageTable.h"
#import "MAPIStoreMapping.h"
#import "MAPIStoreTypes.h"
#import "MAPIStoreUserContext.h"
#import "NSData+MAPIStore.h"
#import "NSString+MAPIStore.h"
#import "SOGoMAPIDBMessage.h"
#import <SOGo/SOGoCacheGCSFolder.h>
#import "MAPIStoreMailVolatileMessage.h"
#import "MAPIStoreMailFolder.h"
static Class SOGoMailFolderK, MAPIStoreMailFolderK, MAPIStoreOutboxFolderK;
#undef DEBUG
#include <util/attr.h>
#include <libmapi/libmapi.h>
#include <mapistore/mapistore.h>
#include <mapistore/mapistore_errors.h>
@implementation MAPIStoreMailFolder
+ (void) initialize
{
SOGoMailFolderK = [SOGoMailFolder class];
MAPIStoreMailFolderK = [MAPIStoreMailFolder class];
MAPIStoreOutboxFolderK = [MAPIStoreOutboxFolder class];
[MAPIStoreAppointmentWrapper class];
}
- (id) init
{
if ((self = [super init]))
{
versionsMessage = nil;
bodyData = [NSMutableDictionary new];
}
return self;
}
- (void) dealloc
{
[versionsMessage release];
[bodyData release];
[super dealloc];
}
- (void) setupVersionsMessage
{
ASSIGN (versionsMessage,
[SOGoMAPIDBMessage objectWithName: @"versions.plist"
inContainer: dbFolder]);
[versionsMessage setObjectType: MAPIInternalCacheObject];
}
- (BOOL) ensureFolderExists
{
return [(SOGoMailFolder *) sogoObject exists] || [sogoObject create];
}
- (void) addProperties: (NSDictionary *) newProperties
{
NSString *newDisplayName, *newNameInContainer;
NSMutableDictionary *propsCopy;
NSNumber *key;
uint64_t fid;
key = MAPIPropertyKey (PR_DISPLAY_NAME_UNICODE);
newDisplayName = [newProperties objectForKey: key];
if (newDisplayName
&& ![self isKindOfClass: MAPIStoreOutboxFolderK]
&& ![[(SOGoMailFolder *) sogoObject displayName]
isEqualToString: newDisplayName])
{
fid = [self objectId];
[(SOGoMailFolder *) sogoObject renameTo: newDisplayName];
newNameInContainer = [sogoObject nameInContainer];
if (!container)
[(MAPIStoreMailContext *) context
updateURLWithFolderName: newNameInContainer];
[[self mapping] updateID: fid withURL: [self url]];
[dbFolder setNameInContainer: newNameInContainer];
[self cleanupCaches];
propsCopy = [newProperties mutableCopy];
[propsCopy removeObjectForKey: key];
[propsCopy autorelease];
newProperties = propsCopy;
}
[super addProperties: newProperties];
}
- (MAPIStoreMessageTable *) messageTable
{
[self synchroniseCache];
return [MAPIStoreMailMessageTable tableForContainer: self];
}
- (enum mapistore_error) createFolder: (struct SRow *) aRow
withFID: (uint64_t) newFID
andKey: (NSString **) newKeyP
{
enum mapistore_error rc;
NSString *folderName, *nameInContainer;
SOGoMailFolder *newFolder;
int i;
nameInContainer = nil;
folderName = nil;
for (i = 0; !folderName && i < aRow->cValues; i++)
{
if (aRow->lpProps[i].ulPropTag == PR_DISPLAY_NAME_UNICODE)
folderName = [NSString stringWithUTF8String: aRow->lpProps[i].value.lpszW];
else if (aRow->lpProps[i].ulPropTag == PR_DISPLAY_NAME)
folderName = [NSString stringWithUTF8String: aRow->lpProps[i].value.lpszA];
}
if (folderName)
{
nameInContainer = [NSString stringWithFormat: @"folder%@",
[[folderName stringByEncodingImap4FolderName] asCSSIdentifier]];
newFolder = [SOGoMailFolderK objectWithName: nameInContainer
inContainer: sogoObject];
if ([newFolder create])
{
*newKeyP = nameInContainer;
rc = MAPISTORE_SUCCESS;
}
else if ([newFolder exists])
rc = MAPISTORE_ERR_EXIST;
else
rc = MAPISTORE_ERR_DENIED;
}
else
rc = MAPISTORE_ERROR;
return rc;
}
- (int) deleteFolder
{
int rc;
NSException *error;
NSString *name;
name = [self nameInContainer];
if ([name isEqualToString: @"folderINBOX"])
rc = MAPISTORE_ERR_DENIED;
else
{
error = [(SOGoMailFolder *) sogoObject delete];
if (error)
rc = MAPISTORE_ERROR;
else
{
if (![versionsMessage delete])
rc = MAPISTORE_SUCCESS;
else
rc = MAPISTORE_ERROR;
}
}
return (rc == MAPISTORE_SUCCESS) ? [super deleteFolder] : rc;
}
- (int) getPidTagContentUnreadCount: (void **) data
inMemCtx: (TALLOC_CTX *) memCtx
{
EOQualifier *searchQualifier;
uint32_t longValue;
searchQualifier
= [EOQualifier qualifierWithQualifierFormat: @"flags = %@", @"unseen"];
longValue = [[sogoObject fetchUIDsMatchingQualifier: searchQualifier
sortOrdering: nil]
count];
*data = MAPILongValue (memCtx, longValue);
return MAPISTORE_SUCCESS;
}
- (int) getPidTagContainerClass: (void **) data
inMemCtx: (TALLOC_CTX *) memCtx
{
*data = [@"IPF.Note" asUnicodeInMemCtx: memCtx];
return MAPISTORE_SUCCESS;
}
- (EOQualifier *) nonDeletedQualifier
{
static EOQualifier *nonDeletedQualifier = nil;
EOQualifier *deletedQualifier;
if (!nonDeletedQualifier)
{
deletedQualifier
= [[EOKeyValueQualifier alloc]
initWithKey: @"FLAGS"
operatorSelector: EOQualifierOperatorContains
value: [NSArray arrayWithObject: @"Deleted"]];
nonDeletedQualifier = [[EONotQualifier alloc]
initWithQualifier: deletedQualifier];
[deletedQualifier release];
}
return nonDeletedQualifier;
}
- (NSArray *) messageKeysMatchingQualifier: (EOQualifier *) qualifier
andSortOrderings: (NSArray *) sortOrderings
{
NSArray *uidKeys;
EOQualifier *fetchQualifier;
if ([self ensureFolderExists])
{
if (!sortOrderings)
sortOrderings = [NSArray arrayWithObject: @"ARRIVAL"];
if (qualifier)
{
fetchQualifier
= [[EOAndQualifier alloc] initWithQualifiers:
[self nonDeletedQualifier], qualifier,
nil];
[fetchQualifier autorelease];
}
else
fetchQualifier = [self nonDeletedQualifier];
uidKeys = [[sogoObject fetchUIDsMatchingQualifier: fetchQualifier
sortOrdering: sortOrderings]
stringsWithFormat: @"%@.eml"];
}
else
uidKeys = nil;
return uidKeys;
}
- (NSMutableString *) _imapFolderNameRepresentation: (NSString *) subfolderName
{
NSMutableString *representation;
NSString *nameInContainer, *strippedName;
nameInContainer = [self nameInContainer];
if (container)
representation = [(MAPIStoreMailFolder *) container
_imapFolderNameRepresentation: nameInContainer];
else
{
if (![nameInContainer hasPrefix: @"folder"])
abort ();
strippedName = [nameInContainer substringFromIndex: 6];
representation = [NSMutableString stringWithString: strippedName];
}
if (![subfolderName hasPrefix: @"folder"])
abort ();
strippedName = [[subfolderName substringFromIndex: 6] stringByDecodingImap4FolderName];
[representation appendFormat: @"/%@", strippedName];
return representation;
}
- (void) _cleanupSubfolderKeys: (NSMutableArray *) subfolderKeys
{
SOGoMailAccount *account;
NSString *draftsFolderName, *sentFolderName, *trashFolderName;
NSString *subfolderKey, *cmpString;
NSUInteger count, max;
NSMutableArray *keysToRemove;
account = [(SOGoMailFolder *) sogoObject mailAccountFolder];
draftsFolderName = [account draftsFolderNameInContext: nil];
sentFolderName = [account sentFolderNameInContext: nil];
trashFolderName = [account trashFolderNameInContext: nil];
max = [subfolderKeys count];
keysToRemove = [NSMutableArray arrayWithCapacity: max];
for (count = 0; count < max; count++)
{
subfolderKey = [subfolderKeys objectAtIndex: count];
cmpString = [self _imapFolderNameRepresentation: subfolderKey];
if ([cmpString isEqualToString: draftsFolderName]
|| [cmpString isEqualToString: sentFolderName]
|| [cmpString isEqualToString: trashFolderName])
[keysToRemove addObject: subfolderKey];
}
[subfolderKeys removeObjectsInArray: keysToRemove];
}
- (NSArray *) folderKeysMatchingQualifier: (EOQualifier *) qualifier
andSortOrderings: (NSArray *) sortOrderings
{
NSMutableArray *subfolderKeys;
if ([self ensureFolderExists])
{
if (qualifier)
[self errorWithFormat: @"qualifier is not used for folders"];
if (sortOrderings)
[self errorWithFormat: @"sort orderings are not used for folders"];
subfolderKeys = [[sogoObject toManyRelationshipKeys] mutableCopy];
[subfolderKeys autorelease];
[self _cleanupSubfolderKeys: subfolderKeys];
}
else
subfolderKeys = nil;
return subfolderKeys;
}
- (NSDate *) creationTime
{
return [NSCalendarDate dateWithTimeIntervalSince1970: 0x4dbb2dbe]; /* oc_version_time */
}
- (NSDate *) lastMessageModificationTime
{
NSNumber *ti;
NSDate *value = nil;
[self synchroniseCache];
ti = [[versionsMessage properties]
objectForKey: @"SyncLastSynchronisationDate"];
if (ti)
value = [NSDate dateWithTimeIntervalSince1970: [ti doubleValue]];
else
value = [NSDate date];
//[self logWithFormat: @"lastMessageModificationTime: %@", value];
return value;
}
- (SOGoFolder *) aclFolder
{
return (SOGoFolder *) sogoObject;
}
- (NSArray *) permissionEntries
{
NSArray *permissionEntries;
if ([self ensureFolderExists])
permissionEntries = [super permissionEntries];
else
permissionEntries = nil;
return permissionEntries;
}
- (BOOL) supportsSubFolders
{
BOOL supportsSubFolders;
MAPIStoreUserContext *userContext;
if ([[self nameInContainer] isEqualToString: @"folderINBOX"])
{
userContext = [self userContext];
supportsSubFolders = ![userContext inboxHasNoInferiors];
}
else
supportsSubFolders = YES;
return supportsSubFolders;
}
/* synchronisation */
/* Tree:
{
SyncLastModseq = x;
SyncLastSynchronisationDate = x; ** not updated until something changed
Messages = {
MessageKey = {
Version = x;
Modseq = x;
Deleted = b;
};
...
};
VersionMapping = {
Version = MessageKey;
...
}
}
*/
static NSComparisonResult
_compareFetchResultsByMODSEQ (id entry1, id entry2, void *data)
{
static NSNumber *zeroNumber = nil;
NSNumber *modseq1, *modseq2;
if (!zeroNumber)
{
zeroNumber = [NSNumber numberWithUnsignedLongLong: 0];
[zeroNumber retain];
}
modseq1 = [entry1 objectForKey: @"modseq"];
if (!modseq1)
modseq1 = zeroNumber;
modseq2 = [entry2 objectForKey: @"modseq"];
if (!modseq2)
modseq2 = zeroNumber;
return [modseq1 compare: modseq2];
}
- (void) _setChangeKey: (NSData *) changeKey
forMessageEntry: (NSMutableDictionary *) messageEntry
{
struct XID *xid;
NSString *guid;
NSData *globCnt;
NSDictionary *changeKeyDict;
NSMutableDictionary *changeList;
xid = [changeKey asXIDInMemCtx: NULL];
guid = [NSString stringWithGUID: &xid->GUID];
globCnt = [NSData dataWithBytes: xid->Data length: xid->Size];
talloc_free (xid);
/* 1. set change key association */
changeKeyDict = [NSDictionary dictionaryWithObjectsAndKeys:
guid, @"GUID",
globCnt, @"LocalId",
nil];
[messageEntry setObject: changeKeyDict forKey: @"ChangeKey"];
/* 2. append/update predecessor change list */
changeList = [messageEntry objectForKey: @"PredecessorChangeList"];
if (!changeList)
{
changeList = [NSMutableDictionary new];
[messageEntry setObject: changeList
forKey: @"PredecessorChangeList"];
[changeList release];
}
[changeList setObject: globCnt forKey: guid];
}
- (BOOL) synchroniseCache
{
BOOL rc = YES;
uint64_t newChangeNum;
NSNumber *ti, *modseq, *initialLastModseq, *lastModseq,
*nextModseq;
NSString *changeNumber, *uid, *messageKey;
uint64_t lastModseqNbr;
EOQualifier *kvQualifier, *searchQualifier;
NSArray *uids, *changeNumbers;
NSUInteger count, max;
NSArray *fetchResults;
NSDictionary *result;
NSData *changeKey;
NSMutableArray *messageKeys;
NSMutableDictionary *currentProperties, *messages, *mapping, *messageEntry;
NSCalendarDate *now;
BOOL foundChange = NO;
/* NOTE: we are using NSString instance for "uid" and "changeNumber" because
NSNumber proved to give very bad performances when used as NSDictionary
keys with GNUstep 1.22.1. The bug seems to be solved with 1.24 but many
distros still ship an older version. */
now = [NSCalendarDate date];
[now setTimeZone: utcTZ];
[versionsMessage reloadIfNeeded];
currentProperties = [versionsMessage properties];
messages = [currentProperties objectForKey: @"Messages"];
if (!messages)
{
messages = [NSMutableDictionary new];
[currentProperties setObject: messages forKey: @"Messages"];
[messages release];
}
mapping = [currentProperties objectForKey: @"VersionMapping"];
if (!mapping)
{
mapping = [NSMutableDictionary new];
[currentProperties setObject: mapping forKey: @"VersionMapping"];
[mapping release];
}
lastModseq = [currentProperties objectForKey: @"SyncLastModseq"];
initialLastModseq = lastModseq;
if (lastModseq)
{
lastModseqNbr = [lastModseq unsignedLongLongValue];
nextModseq = [NSNumber numberWithUnsignedLongLong: lastModseqNbr + 1];
kvQualifier = [[EOKeyValueQualifier alloc]
initWithKey: @"modseq"
operatorSelector: EOQualifierOperatorGreaterThanOrEqualTo
value: nextModseq];
searchQualifier = [[EOAndQualifier alloc]
initWithQualifiers:
kvQualifier, [self nonDeletedQualifier], nil];
[kvQualifier release];
[searchQualifier autorelease];
}
else
{
lastModseqNbr = 0;
searchQualifier = [self nonDeletedQualifier];
}
/* 1. we fetch modified or added uids */
uids = [sogoObject fetchUIDsMatchingQualifier: searchQualifier
sortOrdering: nil];
max = [uids count];
if (max > 0)
{
messageKeys = [NSMutableArray arrayWithCapacity: max];
for (count = 0; count < max; count++)
{
messageKey = [NSString stringWithFormat: @"%@.eml",
[uids objectAtIndex: count]];
[messageKeys addObject: messageKey];
}
[self ensureIDsForChildKeys: messageKeys];
changeNumbers = [[self context] getNewChangeNumbers: max];
fetchResults
= [(NSDictionary *) [sogoObject fetchUIDs: uids
parts: [NSArray arrayWithObject: @"modseq"]]
objectForKey: @"fetch"];
/* NOTE: we sort items manually because Cyrus does not properly sort
entries with a MODSEQ of 0 */
fetchResults
= [fetchResults sortedArrayUsingFunction: _compareFetchResultsByMODSEQ
context: NULL];
for (count = 0; count < max; count++)
{
result = [fetchResults objectAtIndex: count];
uid = [[result objectForKey: @"uid"] stringValue];
modseq = [result objectForKey: @"modseq"];
newChangeNum = [[changeNumbers objectAtIndex: count]
unsignedLongLongValue];
changeNumber = [NSString stringWithUnsignedLongLong: newChangeNum];
messageEntry = [NSMutableDictionary new];
[messages setObject: messageEntry forKey: uid];
[messageEntry release];
[messageEntry setObject: modseq forKey: @"modseq"];
[messageEntry setObject: changeNumber forKey: @"version"];
//[self logWithFormat: @"added message entry for uid %@, modseq %@,"
// @" version %@", uid, modseq, changeNumber];
changeKey = [self getReplicaKeyFromGlobCnt: newChangeNum >> 16];
[self _setChangeKey: changeKey forMessageEntry: messageEntry];
[mapping setObject: modseq forKey: changeNumber];
if (!lastModseq
|| ([lastModseq compare: modseq] == NSOrderedAscending))
lastModseq = modseq;
}
[currentProperties setObject: lastModseq forKey: @"SyncLastModseq"];
foundChange = YES;
}
/* 2. we synchronise deleted UIDs */
if (initialLastModseq)
{
fetchResults = [(SOGoMailFolder *) sogoObject
fetchUIDsOfVanishedItems: lastModseqNbr];
max = [fetchResults count];
changeNumbers = [[self context] getNewChangeNumbers: max];
changeNumber = nil;
for (count = 0; count < max; count++)
{
uid = [fetchResults objectAtIndex: count];
if ([messages objectForKey: uid])
{
newChangeNum = [[changeNumbers objectAtIndex: count]
unsignedLongLongValue];
changeNumber = [NSString stringWithUnsignedLongLong: newChangeNum];
[messages removeObjectForKey: uid];
[self logWithFormat: @"removed message entry for uid %@", uid];
}
}
if (changeNumber)
{
[currentProperties setObject: changeNumber
forKey: @"SyncLastDeleteChangeNumber"];
foundChange = YES;
}
}
if (foundChange)
{
ti = [NSNumber numberWithDouble: [now timeIntervalSince1970]];
[currentProperties setObject: ti
forKey: @"SyncLastSynchronisationDate"];
[versionsMessage save];
}
return rc;
}
- (NSNumber *) modseqFromMessageChangeNumber: (NSString *) changeNum
{
NSDictionary *mapping;
NSNumber *modseq;
mapping = [[versionsMessage properties] objectForKey: @"VersionMapping"];
modseq = [mapping objectForKey: changeNum];
return modseq;
}
- (NSString *) messageUIDFromMessageKey: (NSString *) messageKey
{
NSString *messageUid;
NSRange dotRange;
dotRange = [messageKey rangeOfString: @".eml"];
if (dotRange.location != NSNotFound)
messageUid = [messageKey substringToIndex: dotRange.location];
else
messageUid = nil;
return messageUid;
}
- (NSString *) changeNumberForMessageUID: (NSString *) messageUid
{
NSDictionary *messages;
NSString *changeNumber;
messages = [[versionsMessage properties] objectForKey: @"Messages"];
changeNumber = [[messages objectForKey: messageUid]
objectForKey: @"version"];
return changeNumber;
}
- (void) setChangeKey: (NSData *) changeKey
forMessageWithKey: (NSString *) messageKey
{
NSMutableDictionary *messages, *messageEntry;
NSString *messageUid;
messageUid = [self messageUIDFromMessageKey: messageKey];
messages = [[versionsMessage properties] objectForKey: @"Messages"];
messageEntry = [messages objectForKey: messageUid];
if (!messageEntry)
abort ();
[self _setChangeKey: changeKey forMessageEntry: messageEntry];
[versionsMessage save];
}
- (NSData *) changeKeyForMessageWithKey: (NSString *) messageKey
{
NSDictionary *messages, *changeKeyDict;
NSString *guid, *messageUid;
NSData *globCnt, *changeKey = nil;
messageUid = [self messageUIDFromMessageKey: messageKey];
messages = [[versionsMessage properties] objectForKey: @"Messages"];
changeKeyDict = [[messages objectForKey: messageUid]
objectForKey: @"ChangeKey"];
if (changeKeyDict)
{
guid = [changeKeyDict objectForKey: @"GUID"];
globCnt = [changeKeyDict objectForKey: @"LocalId"];
changeKey = [NSData dataWithChangeKeyGUID: guid andCnt: globCnt];
}
return changeKey;
}
- (NSData *) predecessorChangeListForMessageWithKey: (NSString *) messageKey
{
NSMutableData *list = nil;
NSDictionary *messages, *changeListDict;
NSArray *keys;
NSMutableArray *changeKeys;
NSUInteger count, max;
NSData *changeKey;
NSString *guid, *messageUid;
NSData *globCnt;
messageUid = [self messageUIDFromMessageKey: messageKey];
messages = [[versionsMessage properties] objectForKey: @"Messages"];
changeListDict = [[messages objectForKey: messageUid]
objectForKey: @"PredecessorChangeList"];
if (changeListDict)
{
keys = [changeListDict allKeys];
max = [keys count];
changeKeys = [NSMutableArray arrayWithCapacity: max];
for (count = 0; count < max; count++)
{
guid = [keys objectAtIndex: count];
globCnt = [changeListDict objectForKey: guid];
changeKey = [NSData dataWithChangeKeyGUID: guid andCnt: globCnt];
[changeKeys addObject: changeKey];
}
[changeKeys sortUsingFunction: MAPIChangeKeyGUIDCompare
context: nil];
list = [NSMutableData data];
for (count = 0; count < max; count++)
{
changeKey = [changeKeys objectAtIndex: count];
[list appendUInt8: [changeKey length]];
[list appendData: changeKey];
}
}
return list;
}
- (NSArray *) getDeletedKeysFromChangeNumber: (uint64_t) changeNum
andCN: (NSNumber **) cnNbr
inTableType: (uint8_t) tableType
{
NSArray *deletedKeys, *deletedUIDs;
NSString *changeNumber;
uint64_t modseq;
NSDictionary *versionProperties;
if (tableType == MAPISTORE_MESSAGE_TABLE)
{
changeNumber = [NSString stringWithFormat: @"0x%.16llx", changeNum];
modseq = [[self modseqFromMessageChangeNumber: changeNumber]
unsignedLongLongValue];
if (modseq > 0)
{
deletedUIDs = [(SOGoMailFolder *) sogoObject
fetchUIDsOfVanishedItems: modseq];
deletedKeys = [deletedUIDs stringsWithFormat: @"%@.eml"];
if ([deletedUIDs count] > 0)
{
versionProperties = [versionsMessage properties];
changeNumber = [versionProperties
objectForKey: @"SyncLastDeleteChangeNumber"];
*cnNbr = [NSNumber numberWithUnsignedLongLong:
[changeNumber unsignedLongLongValue]];
[versionsMessage save];
}
}
else
deletedKeys = [NSArray array];
}
else
deletedKeys = [super getDeletedKeysFromChangeNumber: changeNum
andCN: cnNbr
inTableType: tableType];
return deletedKeys;
}
static void
_appendIMAPRange (NSMutableArray *UIDs, uint32_t low, uint32_t high)
{
uint32_t count;
for (count = low; count < high + 1; count++)
[UIDs addObject: [NSNumber numberWithUnsignedLong: count]];
}
static NSUInteger
_parseUID (const unichar *uniString, uint32_t *newUidP)
{
NSUInteger count = 0;
uint32_t newUid = 0;
while (uniString[count] >= '0' && uniString[count] <= '9')
{
newUid = newUid * 10 + (uniString[count] - 48);
count++;
}
*newUidP = newUid;
return count;
}
static NSUInteger
_parseIMAPRange (const unichar *uniString, NSArray **UIDsP)
{
NSMutableArray *UIDs;
NSUInteger count = 0;
uint32_t currentUid, rangeMin = 0;
BOOL done = NO, inRange = NO;
UIDs = [NSMutableArray array];
while (!done)
{
count += _parseUID (uniString + count, ¤tUid);
switch (uniString[count])
{
case ':':
inRange = YES;
rangeMin = currentUid;
break;
case ' ':
case 0:
done = YES;
case ',':
if (inRange)
{
_appendIMAPRange (UIDs, rangeMin, currentUid);
inRange = NO;
}
else
[UIDs addObject: [NSNumber numberWithUnsignedLong: currentUid]];
break;
default:
abort ();
}
count++;
}
*UIDsP = UIDs;
return count;
}
static void
_parseCOPYUID (NSString *line, NSArray **destUIDsP)
{
unichar *uniString;
NSUInteger count = 0, max;
// char state = 'i'; /* i = init, v = validity, s = source range, d = dest range */
/* sample: 1 OK [COPYUID 1311899334 1:3 11:13] Completed */
max = [line length];
uniString = NSZoneMalloc (NULL, sizeof (unichar) * (max + 1));
[line getCharacters: uniString];
uniString[max] = 0;
while (count < max && uniString[count] != ' ')
count++;
count++;
while (count < max && uniString[count] != ' ')
count++;
count++;
while (count < max && uniString[count] != ' ')
count++;
count++;
if (count < max)
count += _parseIMAPRange (uniString + count, destUIDsP);
NSZoneFree (NULL, uniString);
}
//
// Move (or eventually copy) the mails identified by
// "srcMids" from the source folder into this folder.
//
- (int) moveCopyMessagesWithMIDs: (uint64_t *) srcMids
andCount: (uint32_t) midCount
fromFolder: (MAPIStoreFolder *) sourceFolder
withMIDs: (uint64_t *) targetMids
andChangeKeys: (struct Binary_r **) targetChangeKeys
wantCopy: (uint8_t) wantCopy
inMemCtx: (TALLOC_CTX *) memCtx
{
NGImap4Connection *connection;
NGImap4Client *client;
NSString *sourceFolderName, *targetFolderName, *messageURL, *messageKey,
*uid, *v;
NSMutableArray *uids, *oldMessageURLs;
NSArray *destUIDs;
MAPIStoreMapping *mapping;
NSDictionary *result;
NSUInteger count;
NSArray *a;
NSData *changeKey;
if (![sourceFolder isKindOfClass: [MAPIStoreMailFolder class]])
return [super moveCopyMessagesWithMIDs: srcMids andCount: midCount
fromFolder: sourceFolder withMIDs: targetMids
andChangeKeys: targetChangeKeys
wantCopy: wantCopy
inMemCtx: memCtx];
/* Conversion of mids to IMAP uids */
mapping = [self mapping];
uids = [NSMutableArray arrayWithCapacity: midCount];
oldMessageURLs = [NSMutableArray arrayWithCapacity: midCount];
for (count = 0; count < midCount; count++)
{
messageURL = [mapping urlFromID: srcMids[count]];
if (messageURL)
{
uid = [self messageUIDFromMessageKey: [messageURL lastPathComponent]];
[uids addObject: uid];
[oldMessageURLs addObject: messageURL];
}
else
return MAPISTORE_ERROR;
}
/* IMAP COPY */
connection = [sogoObject imap4Connection];
sourceFolderName = [connection
imap4FolderNameForURL: [[sourceFolder sogoObject] imap4URL]];
targetFolderName = [connection
imap4FolderNameForURL: [sogoObject imap4URL]];
client = [connection client];
[client select: sourceFolderName];
result = [client copyUids: uids toFolder: targetFolderName];
if (![[result objectForKey: @"result"] boolValue])
return MAPISTORE_ERROR;
/* "Move" treatment: Store \Deleted and unregister urls */
if (!wantCopy)
{
[client storeFlags: [NSArray arrayWithObject: @"Deleted"] forUIDs: uids
addOrRemove: YES];
for (count = 0; count < midCount; count++)
[mapping unregisterURLWithID: srcMids[count]];
}
/* Registration of target messages */
//
// We use the UIDPLUS IMAP extension here in order to speedup UID retrieval
// If supported by the server, we'll get something like: COPYUID 1315425789 1 8
//
// Sometimes COPYUID isn't returned at all by Cyrus or in case the server doesn't
// support the UIDPLUS IMAP extension, we fallback to a simple UID search.
//
v = [[[result objectForKey: @"RawResponse"] objectForKey: @"ResponseResult"] objectForKey: @"flag"];
if (v)
{
destUIDs = nil;
_parseCOPYUID (v, &destUIDs);
}
else
{
/* FIXME: this may fail if new messages are appended to the folder
between the COPY and SORT operations */
[client select: targetFolderName];
a = [[client sort: @"ARRIVAL" qualifier: nil encoding: @"UTF-8"]
objectForKey: @"sort"];
destUIDs = [[a sortedArrayUsingSelector: @selector (compare:)]
subarrayWithRange: NSMakeRange ([a count] - midCount, midCount)];
}
for (count = 0; count < midCount; count++)
{
messageURL = [NSString stringWithFormat: @"%@%@.eml",
[self url],
[destUIDs objectAtIndex: count]];
[mapping registerURL: messageURL withID: targetMids[count]];
}
/* Update the change keys */
if (targetChangeKeys)
{
[self synchroniseCache];
for (count = 0; count < midCount; count++)
{
changeKey = [NSData dataWithBinary: targetChangeKeys[count]];
messageKey = [NSString stringWithFormat: @"%@.eml",
[destUIDs objectAtIndex: count]];
[self setChangeKey: changeKey
forMessageWithKey: messageKey];
}
}
[self postNotificationsForMoveCopyMessagesWithMIDs: srcMids
andMessageURLs: oldMessageURLs
andCount: midCount
fromFolder: sourceFolder
withMIDs: targetMids
wantCopy: wantCopy];
// We cleanup cache of our source and destination folders
[self cleanupCaches];
[sourceFolder cleanupCaches];
return MAPISTORE_SUCCESS;
}
- (enum mapistore_error) moveCopyToFolder: (MAPIStoreFolder *) targetFolder
withNewName: (NSString *) newFolderName
isMove: (BOOL) isMove
isRecursive: (BOOL) isRecursive
inMemCtx: (TALLOC_CTX *) memCtx
{
enum mapistore_error rc;
NSURL *folderURL, *newFolderURL;
struct SRow folderRow;
struct SPropValue nameProperty;
MAPIStoreMailFolder *newFolder;
SOGoMailFolder *targetSOGoFolder;
NSMutableArray *uids;
NSArray *childKeys;
NSUInteger count, max;
NGImap4Connection *connection;
NGImap4Client *client;
NSString *newURL, *parentDBFolderPath, *childKey, *folderIMAPName,
*urlNamePart, *newFolderIMAPName;
NSException *error;
MAPIStoreMapping *mapping;
NSDictionary *result;
if ([targetFolder isKindOfClass: MAPIStoreMailFolderK])
{
folderURL = [sogoObject imap4URL];
if (!newFolderName)
newFolderName = [sogoObject displayName];
targetSOGoFolder = [targetFolder sogoObject];
if (isMove)
{
urlNamePart = [newFolderName stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
newFolderURL = [NSURL URLWithString: urlNamePart
relativeToURL: [targetSOGoFolder imap4URL]];
error = [[sogoObject imap4Connection]
moveMailboxAtURL: folderURL
toURL: newFolderURL];
if (error)
rc = MAPISTORE_ERR_DENIED;
else
{
rc = MAPISTORE_SUCCESS;
mapping = [self mapping];
newURL = [NSString stringWithFormat: @"%@folder%@/",
[targetFolder url], urlNamePart];
[mapping updateID: [self objectId] withURL: newURL];
parentDBFolderPath = [[targetFolder dbFolder] path];
if (!parentDBFolderPath)
parentDBFolderPath = @"";
[dbFolder changePathTo: [NSString stringWithFormat:
@"%@/folder%@",
parentDBFolderPath,
newFolderName]];
}
}
else
{
nameProperty.ulPropTag = PidTagDisplayName;
nameProperty.value.lpszW = [newFolderName UTF8String];
folderRow.lpProps = &nameProperty;
folderRow.cValues = 1;
rc = [targetFolder createFolder: &folderRow
withFID: -1
andKey: &childKey];
if (rc == MAPISTORE_SUCCESS)
{
newFolder = [targetFolder lookupFolder: childKey];
connection = [sogoObject imap4Connection];
folderIMAPName = [connection
imap4FolderNameForURL: [sogoObject imap4URL]];
newFolderIMAPName = [connection
imap4FolderNameForURL: [[newFolder sogoObject] imap4URL]];
client = [connection client];
[client select: folderIMAPName];
childKeys = [self messageKeys];
max = [childKeys count];
uids = [NSMutableArray arrayWithCapacity: max];
for (count = 0; count < max; count++)
{
childKey = [childKeys objectAtIndex: count];
[uids addObject: [self messageUIDFromMessageKey: childKey]];
}
result = [client copyUids: uids
toFolder: newFolderIMAPName];
if ([[result objectForKey: @"result"] boolValue])
{
if (isRecursive)
{
childKeys = [self folderKeys];
max = [childKeys count];
for (count = 0; count < max; count++)
{
childKey = [childKeys objectAtIndex: count];
[[self lookupFolder: childKey]
moveCopyToFolder: newFolder
withNewName: nil
isMove: NO
isRecursive: YES
inMemCtx: memCtx];
}
}
}
else
rc = MAPISTORE_ERROR;
}
}
[targetFolder cleanupCaches];
}
else
rc = [super moveCopyToFolder: targetFolder withNewName: newFolderName
isMove: isMove
isRecursive: isRecursive
inMemCtx: memCtx];
return rc;
}
- (MAPIStoreMessage *) createMessage
{
SOGoCacheObject *childObject;
childObject = [SOGoCacheObject objectWithName: [SOGoCacheObject
globallyUniqueObjectId]
inContainer: sogoObject];
return [MAPIStoreMailVolatileMessage
mapiStoreObjectWithSOGoObject: childObject
inContainer: self];
}
- (id) lookupMessage: (NSString *) messageKey
{
MAPIStoreMailMessage *message;
NSData *rawBodyData;
message = [super lookupMessage: messageKey];
if (message)
{
rawBodyData = [bodyData objectForKey: messageKey];
if (rawBodyData)
[message setBodyContentFromRawData: rawBodyData];
}
return message;
}
- (NSArray *) rolesForExchangeRights: (uint32_t) rights
{
NSMutableArray *roles;
roles = [NSMutableArray arrayWithCapacity: 6];
if (rights & RoleOwner)
[roles addObject: SOGoMailRole_Administrator];
if (rights & RightsCreateItems)
{
[roles addObject: SOGoRole_ObjectCreator];
[roles addObject: SOGoMailRole_Writer];
[roles addObject: SOGoMailRole_Poster];
}
if (rights & RightsDeleteAll)
{
[roles addObject: SOGoRole_ObjectEraser];
[roles addObject: SOGoRole_FolderEraser];
[roles addObject: SOGoMailRole_Expunger];
}
if (rights & RightsEditAll)
[roles addObject: SOGoRole_ObjectEditor];
if (rights & RightsReadItems)
[roles addObject: SOGoRole_ObjectViewer];
if (rights & RightsCreateSubfolders)
[roles addObject: SOGoRole_FolderCreator];
// [self logWithFormat: @"roles for rights %.8x = (%@)", rights, roles];
return roles;
}
- (uint32_t) exchangeRightsForRoles: (NSArray *) roles
{
uint32_t rights = 0;
if ([roles containsObject: SOGoMailRole_Administrator])
rights |= (RoleOwner ^ RightsAll);
if ([roles containsObject: SOGoRole_ObjectCreator])
rights |= RightsCreateItems;
if ([roles containsObject: SOGoRole_ObjectEraser]
&& [roles containsObject: SOGoRole_FolderEraser])
rights |= RightsDeleteAll;
if ([roles containsObject: SOGoRole_ObjectEditor])
rights |= RightsEditAll;
if ([roles containsObject: SOGoRole_ObjectViewer])
rights |= RightsReadItems;
if ([roles containsObject: SOGoRole_FolderCreator])
rights |= RightsCreateSubfolders;
if (rights != 0)
rights |= RoleNone; /* actually "folder visible" */
// [self logWithFormat: @"rights for roles (%@) = %.8x", roles, rights];
return rights;
}
- (enum mapistore_error) preloadMessageBodiesWithKeys: (NSArray *) keys
ofTableType: (enum mapistore_table_type) tableType
{
MAPIStoreMailMessage *message;
NSMutableSet *bodyPartKeys;
NSMutableDictionary *keyAssoc;
NSDictionary *response;
NSUInteger count, max;
NSString *messageKey, *messageUid, *bodyPartKey;
NGImap4Client *client;
NSArray *fetch;
NSData *bodyContent;
if (tableType == MAPISTORE_MESSAGE_TABLE)
{
[bodyData removeAllObjects];
max = [keys count];
if (max > 0)
{
bodyPartKeys = [NSMutableSet setWithCapacity: max];
keyAssoc = [NSMutableDictionary dictionaryWithCapacity: max];
for (count = 0; count < max; count++)
{
messageKey = [keys objectAtIndex: count];
message = [self lookupMessage: messageKey];
if (message)
{
bodyPartKey = [message bodyContentPartKey];
if (bodyPartKey)
{
[bodyPartKeys addObject: bodyPartKey];
messageUid = [self messageUIDFromMessageKey: messageKey];
[keyAssoc setObject: bodyPartKey forKey: messageUid];
}
}
}
client = [[(SOGoMailFolder *) sogoObject imap4Connection] client];
[client select: [sogoObject absoluteImap4Name]];
response = [client fetchUids: [keyAssoc allKeys]
parts: [bodyPartKeys allObjects]];
fetch = [response objectForKey: @"fetch"];
max = [fetch count];
for (count = 0; count < max; count++)
{
response = [fetch objectAtIndex: count];
messageUid = [[response objectForKey: @"uid"] stringValue];
bodyPartKey = [keyAssoc objectForKey: messageUid];
if (bodyPartKey)
{
bodyContent = [[response objectForKey: bodyPartKey]
objectForKey: @"data"];
if (bodyContent)
{
messageKey = [NSString stringWithFormat: @"%@.eml",
messageUid];
[bodyData setObject: bodyContent forKey: messageKey];
}
}
}
}
}
return MAPISTORE_SUCCESS;
}
@end
@implementation MAPIStoreOutboxFolder
- (int) getPidTagDisplayName: (void **) data
inMemCtx: (TALLOC_CTX *) memCtx
{
*data = [@"Outbox" asUnicodeInMemCtx: memCtx];
return MAPISTORE_SUCCESS;
}
@end
|