/
SOGoCalendarComponent.m
1516 lines (1282 loc) · 45.5 KB
/
SOGoCalendarComponent.m
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
/* SOGoCalendarComponent.m - this file is part of SOGo
*
* Copyright (C) 2006-2019 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 2, 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/NSAutoreleasePool.h>
#import <Foundation/NSValue.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGObjWeb/SoSecurityManager.h>
#import <NGObjWeb/WOApplication.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <NGObjWeb/WORequest+So.h>
#import <NGExtensions/NSObject+Logs.h>
#import <NGExtensions/NGHashMap.h>
#import <NGExtensions/NGQuotedPrintableCoding.h>
#import <NGCards/iCalCalendar.h>
#import <NGCards/iCalDateTime.h>
#import <NGCards/iCalEvent.h>
#import <NGCards/iCalToDo.h>
#import <NGMime/NGMimeBodyPart.h>
#import <NGMime/NGMimeMultipartBody.h>
#import <NGMail/NGMimeMessage.h>
#import <GDLContentStore/GCSFolder.h>
#import <SOGo/NSCalendarDate+SOGo.h>
#import <SOGo/NSDictionary+Utilities.h>
#import <SOGo/NSObject+DAV.h>
#import <SOGo/NSObject+Utilities.h>
#import <SOGo/NSString+Crypto.h>
#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoBuild.h>
#import <SOGo/SOGoMailer.h>
#import <SOGo/SOGoSource.h>
#import <SOGo/SOGoPermissions.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserSettings.h>
#import <SOGo/SOGoSystemDefaults.h>
#import <SOGo/SOGoUserManager.h>
#import <SOGo/SOGoWebDAVAclManager.h>
#import <SOGo/WORequest+SOGo.h>
#import <Appointments/SOGoAppointmentFolder.h>
#import <Mailer/NSString+Mail.h>
#import "SOGoAptMailICalReply.h"
#import "SOGoAptMailReceipt.h"
#import "SOGoEMailAlarmsManager.h"
#import "iCalEntityObject+SOGo.h"
#import "iCalPerson+SOGo.h"
#import "iCalRepeatableEntityObject+SOGo.h"
#import "SOGoCalendarComponent.h"
@implementation SOGoCalendarComponent
+ (SOGoWebDAVAclManager *) webdavAclManager
{
static SOGoWebDAVAclManager *aclManager = nil;
NSString *nsD, *nsI;
if (!aclManager)
{
nsD = @"DAV:";
nsI = @"urn:inverse:params:xml:ns:inverse-dav";
aclManager = [SOGoWebDAVAclManager new];
[aclManager registerDAVPermission: davElement (@"read", nsD)
abstract: NO
withEquivalent: @"SOGoDAVReadPermission" /* hackish */
asChildOf: davElement (@"all", nsD)];
[aclManager registerDAVPermission: davElement (@"view-whole-component", nsI)
abstract: NO
withEquivalent: SOGoCalendarPerm_ViewAllComponent
asChildOf: davElement (@"all", nsD)];
[aclManager registerDAVPermission: davElement (@"view-date-and-time", nsI)
abstract: NO
withEquivalent: SOGoCalendarPerm_ViewDAndT
asChildOf: davElement (@"all", nsD)];
[aclManager registerDAVPermission: davElement (@"read-current-user-privilege-set", nsD)
abstract: NO
withEquivalent: SoPerm_WebDAVAccess
asChildOf: davElement (@"all", nsD)];
[aclManager registerDAVPermission: davElement (@"write", nsD)
abstract: NO
withEquivalent: SOGoCalendarPerm_ModifyComponent
asChildOf: davElement (@"all", nsD)];
[aclManager
registerDAVPermission: davElement (@"write-properties", nsD)
abstract: YES
withEquivalent: SoPerm_ChangePermissions /* hackish */
asChildOf: davElement (@"write", nsD)];
[aclManager
registerDAVPermission: davElement (@"write-content", nsD)
abstract: YES
withEquivalent: nil
asChildOf: davElement (@"write", nsD)];
[aclManager
registerDAVPermission: davElement (@"respond-to-component", nsI)
abstract: NO
withEquivalent: SOGoCalendarPerm_RespondToComponent
asChildOf: davElement (@"write-content", nsD)];
[aclManager registerDAVPermission: davElement (@"admin", nsI)
abstract: YES
withEquivalent: nil
asChildOf: davElement (@"all", nsD)];
[aclManager
registerDAVPermission: davElement (@"read-acl", nsD)
abstract: YES
withEquivalent: SOGoPerm_ReadAcls
asChildOf: davElement (@"admin", nsI)];
[aclManager
registerDAVPermission: davElement (@"write-acl", nsD)
abstract: YES
withEquivalent: nil
asChildOf: davElement (@"admin", nsI)];
}
return aclManager;
}
- (void) setIsNew: (BOOL) newIsNew
{
[super setIsNew: newIsNew];
}
- (BOOL) isNew
{
return [super isNew];
}
- (NSException *) changeParticipationStatus: (NSString *) newPartStat
withDelegate: (iCalPerson *) delegate
alarm: (iCalAlarm *) alarm
{
// Required for protocol <SOGoComponentOccurence>
return nil;
}
- (id) init
{
if ((self = [super init]))
{
fullCalendar = nil;
safeCalendar = nil;
originalCalendar = nil;
componentTag = nil;
}
return self;
}
- (void) dealloc
{
[fullCalendar release];
[safeCalendar release];
[originalCalendar release];
[componentTag release];
[super dealloc];
}
- (void) flush
{
DESTROY(fullCalendar);
DESTROY(safeCalendar);
DESTROY(originalCalendar);
}
- (Class *) parsingClass
{
return (Class *)[iCalCalendar class];
}
- (NSString *) davContentType
{
return @"text/calendar";
}
- (NSString *) componentTag
{
if (!componentTag)
[self subclassResponsibility: _cmd];
return componentTag;
}
- (void) setComponentTag: (NSString *) theTag
{
ASSIGN(componentTag, theTag);
}
- (void) _filterComponent: (iCalEntityObject *) component
{
NSString *type, *summary, *tag, *uid;
SOGoUserSettings *settings;
SOGoUser *calendarOwner;
NSEnumerator *children;
CardElement *element;
NSArray *tags;
int classification;
type = @"vtodo";
classification = 0;
calendarOwner = [SOGoUser userWithLogin: [self ownerInContext: context]];
settings = [calendarOwner userSettings];
if ([component isKindOfClass: [iCalEvent class]])
type = @"vevent";
if ([component symbolicAccessClass] == iCalAccessPrivate)
classification = 1;
else if ([component symbolicAccessClass] == iCalAccessConfidential)
classification = 2;
summary = [self labelForKey: [NSString stringWithFormat: @"%@_class%d",
type, classification]
inContext: context];
tags = [NSArray arrayWithObjects: @"DTSTAMP", @"DTSTART", @"DTEND", @"DUE", @"EXDATE", @"EXRULE", @"RRULE", @"RECURRENCE-ID", nil];
uid = [[component uid] asCryptedPassUsingScheme: @"ssha256"
withSalt: [[settings userSalt] dataUsingEncoding: NSASCIIStringEncoding]
andEncoding: encHex
keyPath: nil];
children = [[[[component children] copy] autorelease] objectEnumerator];
while ((element = [children nextObject]))
{
tag = [element tag];
if (![tags containsObject: [tag uppercaseString]])
[component removeChild: element];
}
[component setSummary: summary];
[component setUid: uid];
}
- (NSString *) secureContentAsString
{
iCalRepeatableEntityObject *tmpComponent;
iCalCalendar *tmpCalendar;
NSArray *allComponents;
SoSecurityManager *sm;
NSString *iCalString;
int i;
sm = [SoSecurityManager sharedSecurityManager];
if (activeUserIsOwner
|| [[self ownerInContext: context] isEqualToString: [[context activeUser] login]]
|| ![sm validatePermission: SOGoCalendarPerm_ViewAllComponent
onObject: self inContext: context])
iCalString = content;
else if (![sm validatePermission: SOGoCalendarPerm_ViewDAndT
onObject: self inContext: context])
{
tmpCalendar = [[self calendar: NO secure: NO] mutableCopy];
// We filter all components, in case we have RECURRENCE-ID
allComponents = [tmpCalendar childrenWithTag: [self componentTag]];
for (i = 0; i < [allComponents count]; i++)
{
tmpComponent = (iCalRepeatableEntityObject *)[allComponents objectAtIndex:i];
[self _filterComponent: tmpComponent];
// We add an additional header here to inform clients (if necessary) that
// we churned the content of the calendar.
[tmpComponent addChild: [CardElement simpleElementWithTag: @"X-SOGo-Secure"
value: @"YES"]];
}
iCalString = [tmpCalendar versitString];
[tmpCalendar release];
}
else
iCalString = nil;
return iCalString;
}
- (iCalRepeatableEntityObject *) lookupOccurrence: (NSString *) recID
{
[self subclassResponsibility: _cmd];
return nil;
}
- (SOGoComponentOccurence *) occurence: (iCalRepeatableEntityObject *) component
{
[self subclassResponsibility: _cmd];
return nil;
}
- (iCalRepeatableEntityObject *) newOccurenceWithID: (NSString *) recID
{
iCalRepeatableEntityObject *masterOccurence, *newOccurence;
iCalCalendar *calendar;
NSCalendarDate *recDate;
NSTimeZone *timeZone;
iCalPerson *organizer;
recDate = [NSCalendarDate dateWithTimeIntervalSince1970: [recID intValue]];
masterOccurence = [self component: NO secure: NO];
timeZone = [[[context activeUser] userDefaults] timeZone];
[recDate setTimeZone: timeZone];
if ([masterOccurence doesOccurOnDate: recDate])
{
newOccurence = [masterOccurence mutableCopy];
organizer = [masterOccurence organizer];
[newOccurence autorelease];
[newOccurence removeAllRecurrenceRules];
[newOccurence removeAllExceptionRules];
[newOccurence removeAllRecurrenceDates];
[newOccurence removeAllExceptionDates];
// It is important to set the organizer as some DAV clients (iCal
// and Thunderbird 10/Lightning 1.2) will prompt the event "edition"
// dialog instead of the event "invitation" (for accept/decline/tentative)
// if the organizer isn't found in a specific recurrence
[newOccurence setOrganizer: organizer];
[newOccurence setRecurrenceId: recDate];
calendar = [masterOccurence parent];
[calendar addChild: newOccurence];
}
else
newOccurence = nil;
return newOccurence;
}
- (id) toManyRelationshipKeys
{
return nil;
}
- (id) toOneRelationshipKeys
{
NSMutableArray *keys;
NSArray *occurences;
NSCalendarDate *recID;
unsigned int count, max, seconds;
keys = [NSMutableArray array];
[keys addObject: @"master"];
occurences = [[self calendar: NO secure: NO] allObjects];
max = [occurences count];
for (count = 1; count < max; count++)
{
recID = [[occurences objectAtIndex: count] recurrenceId];
if (recID)
{
seconds = [recID timeIntervalSince1970];
[keys addObject: [NSString stringWithFormat: @"occurence%d",
seconds]];
}
}
return keys;
}
- (id) lookupName: (NSString *) lookupName
inContext: (id) localContext
acquire: (BOOL) acquire
{
id obj;
iCalRepeatableEntityObject *occurence;
NSString *recID;
BOOL isNewOccurence;
obj = [super lookupName: lookupName
inContext: localContext
acquire: acquire];
if (!obj)
{
if ([lookupName isEqualToString: @"master"])
obj = [self occurence: [self component: NO secure: NO]];
else if ([lookupName hasPrefix: @"occurence"])
{
recID = [lookupName substringFromIndex: 9];
occurence = [self lookupOccurrence: recID];
if (occurence)
isNewOccurence = NO;
else
{
occurence = [self newOccurenceWithID: recID];
isNewOccurence = YES;
}
if (occurence)
{
obj = [self occurence: occurence];
if (isNewOccurence)
[obj setIsNew: isNewOccurence];
}
}
}
return obj;
}
- (NSString *) _secureContentWithoutAlarms
{
iCalCalendar *calendar;
NSArray *allComponents;
iCalEntityObject *currentComponent;
NSUInteger count, max;
calendar = [self calendar: NO secure: YES];
allComponents = [calendar childrenWithTag: [self componentTag]];
max = [allComponents count];
for (count = 0; count < max; count++)
{
currentComponent = [allComponents objectAtIndex: count];
[currentComponent removeAllAlarms];
}
return [calendar versitString];
}
- (NSString *) contentAsString
{
NSString *secureContent;
if ([[context request] isSoWebDAVRequest])
{
if ([container showCalendarAlarms])
secureContent = [self secureContentAsString];
else
secureContent = [self _secureContentWithoutAlarms];
}
else
secureContent = [super contentAsString];
return secureContent;
}
- (NSString *) davCalendarData
{
return [self contentAsString];
}
- (iCalCalendar *) calendar: (BOOL) create secure: (BOOL) secure
{
iCalRepeatableEntityObject *newComponent;
iCalCalendar **calendar, *returnedCopy;
NSString *iCalString, *tag, *prodID;
if (secure)
calendar = &safeCalendar;
else
calendar = &fullCalendar;
if (!*calendar)
{
if (secure)
iCalString = [self secureContentAsString];
else
iCalString = content;
if ([iCalString length] > 0)
{
ASSIGN (*calendar, [iCalCalendar parseSingleFromSource: iCalString]);
if (!secure)
originalCalendar = [*calendar copy];
}
else
{
if (create)
{
ASSIGN (*calendar, [iCalCalendar groupWithTag: @"vcalendar"]);
[*calendar setVersion: @"2.0"];
prodID = [NSString stringWithFormat:
@"-//Inverse inc./SOGo %@//EN",
SOGoVersion];
[*calendar setProdID: prodID];
tag = [[self componentTag] uppercaseString];
newComponent = [[*calendar classForTag: tag]
groupWithTag: tag];
[newComponent setUid: [self globallyUniqueObjectId]];
[*calendar addChild: newComponent];
}
}
}
returnedCopy = [*calendar mutableCopy];
[returnedCopy autorelease];
return returnedCopy;
}
- (id) component: (BOOL) create secure: (BOOL) secure
{
return [[self calendar: create secure: secure]
firstChildWithTag: [self componentTag]];
}
//
// Returs "YES" if a a group was decomposed among attendees.
//
// It can also return yes if an attendee was found in the list
// matching the organizer. In which case, it was removed.
//
- (BOOL) expandGroupsInEvent: (iCalEvent *) theEvent
{
NSString *organizerEmail, *domain;
NSMutableArray *allAttendees;
iCalPerson *currentAttendee;
NSEnumerator *enumerator;
NSAutoreleasePool *pool;
NSDictionary *dict;
BOOL eventWasModified;
unsigned int i, j;
domain = [[context activeUser] domain];
organizerEmail = [[theEvent organizer] rfc822Email];
eventWasModified = NO;
allAttendees = [NSMutableArray arrayWithArray: [theEvent attendees]];
enumerator = [[theEvent attendees] objectEnumerator];
j = 0;
pool = [[NSAutoreleasePool alloc] init];
while ((currentAttendee = [enumerator nextObject]))
{
if (j%5 == 0)
{
RELEASE(pool);
pool = [[NSAutoreleasePool alloc] init];
}
dict = [[SOGoUserManager sharedUserManager] contactInfosForUserWithUIDorEmail: [currentAttendee rfc822Email]
inDomain: domain];
if (dict && [[dict objectForKey: @"isGroup"] boolValue])
{
iCalPerson *person;
NSArray *members;
SOGoUser *user;
id <SOGoSource> source;
// We did decompose a group...
[allAttendees removeObject: currentAttendee];
source = [[SOGoUserManager sharedUserManager] sourceWithID: [dict objectForKey: @"SOGoSource"]];
if ([source conformsToProtocol:@protocol(SOGoMembershipSource)])
{
members = [(id<SOGoMembershipSource>)(source) membersForGroupWithUID: [dict objectForKey: @"c_uid"]];
for (i = 0; i < [members count]; i++)
{
user = [members objectAtIndex: i];
eventWasModified = YES;
// If the organizer is part of the group, we skip it from
// the addition to the attendees' list
if ([user hasEmail: organizerEmail])
continue;
person = [self iCalPersonWithUID: [user login]];
[person setTag: @"ATTENDEE"];
[person setParticipationStatus: [currentAttendee participationStatus]];
[person setRsvp: [currentAttendee rsvp]];
[person setRole: [currentAttendee role]];
if (![allAttendees containsObject: person])
[allAttendees addObject: person];
}
}
else
{
[self errorWithFormat: @"Inconsistency error - got group identifier (%@) from a source (%@) that does not support groups.", [currentAttendee rfc822Email], [dict objectForKey: @"SOGoSource"]];
RELEASE(pool);
return NO;
}
}
j++;
} // while (currentAttendee ...
if (eventWasModified)
[theEvent setAttendees: allAttendees];
RELEASE(pool);
return eventWasModified;
}
- (void) _updateRecurrenceIDsWithEvent: (iCalRepeatableEntityObject*) newEvent
{
iCalRepeatableEntityObject *oldMaster, *currentComponent;
iCalDateTime *currentDate;
int deltaSecs;
NSArray *components, *dates;
NSMutableArray *newDates;
unsigned int count, max;
NSCalendarDate *recID, *newDate;
// Compute time interval from previous event definition.
if (!originalCalendar)
{
if (content)
ASSIGN (originalCalendar, [iCalCalendar parseSingleFromSource: content]);
else
{
[self warnWithFormat: @"content not available, we don't update the event"];
return;
}
}
oldMaster = (iCalRepeatableEntityObject *)
[originalCalendar firstChildWithTag: [self componentTag]];
deltaSecs = [[newEvent startDate]
timeIntervalSinceDate: [oldMaster startDate]];
components = [[newEvent parent] events];
max = [components count];
if (max > 0)
{
// Update recurrence-id attribute of occurences.
for (count = 1; count < max; count++)
{
currentComponent = [components objectAtIndex: count];
recID = [[currentComponent recurrenceId] addTimeInterval: deltaSecs];
[currentComponent setRecurrenceId: recID];
}
// Update exception dates in master vEvent.
currentComponent = [components objectAtIndex: 0];
dates = [currentComponent childrenWithTag: @"exdate"];
max = [dates count];
if (max > 0)
{
newDates = [NSMutableArray arrayWithCapacity: max];
for (count = 0; count < max; count++)
{
currentDate = [dates objectAtIndex: count];
newDate = [[currentDate dateTime] addTimeInterval: deltaSecs];
[newDates addObject: newDate];
}
[currentComponent removeAllExceptionDates];
for (count = 0; count < max; count++)
[currentComponent addToExceptionDates: [newDates objectAtIndex: count]];
}
}
}
- (void) updateComponent: (iCalRepeatableEntityObject *) newObject
{
NSString *newUid;
if (!isNew
&& [newObject isRecurrent])
// We update an repeating event -- update exception dates
// and recurrence-ids.
[self _updateRecurrenceIDsWithEvent: newObject];
// As much as we can, we try to use c_name == c_uid in order
// to avoid tricky scenarios with some CalDAV clients. For example,
// if Alice invites Bob (both use SOGo) and Bob accepts the invitation
// using Lightning before having refreshed their calendar, they'll end up
// with a duplicate of the event in their database tables.
if (isNew)
{
newUid = nameInContainer;
if ([newUid hasSuffix: @".ics"])
newUid = [newUid substringToIndex: [newUid length]-4];
[newObject setUid: newUid];
}
}
- (NSException *) saveCalendar: (iCalCalendar *) newCalendar
{
[super saveComponent: newCalendar];
return nil;
}
- (NSException *) saveComponent: (iCalRepeatableEntityObject *) newObject
{
[newObject setLastModified: [NSCalendarDate calendarDate]];
return [self saveCalendar: [newObject parent]];
}
- (NSException *) saveComponent: (iCalRepeatableEntityObject *) newEvent
force: (BOOL) forceSave
{
return [self saveComponent: newEvent];
}
/* raw saving */
/* EMail Notifications */
- (NSString *) homePageURLForPerson: (iCalPerson *) _person
{
NSString *baseURL;
NSString *uid;
NSArray *traversalObjects;
/* generate URL from traversal stack */
traversalObjects = [context objectTraversalStack];
if ([traversalObjects count] > 0)
baseURL = [[traversalObjects objectAtIndex:0] baseURLInContext: context];
else
{
baseURL = @"http://localhost/";
[self warnWithFormat:@"Unable to create baseURL from context!"];
}
uid = [_person uid];
return ((uid)
? [NSString stringWithFormat:@"%@%@", baseURL, uid]
: nil);
}
- (NSTimeZone *) timeZoneForUser: (NSString *) email
{
NSString *uid;
SOGoUserDefaults *ud;
uid = [[SOGoUserManager sharedUserManager] getUIDForEmail: email];
ud = [[SOGoUser userWithLogin: uid] userDefaults];
return [ud timeZone];
}
- (NGMimeBodyPart *) _bodyPartForICalObject: (iCalRepeatableEntityObject *) object
{
NGMimeBodyPart *bodyPart;
NGMutableHashMap *headerMap;
NSString *iCalString, *header, *charset;
NSData *objectData;
iCalCalendar *parent;
parent = [object parent];
iCalString = [NSString stringWithFormat: @"%@\r\n", [parent versitString]];
objectData = [iCalString dataUsingEncoding: NSUTF8StringEncoding];
charset = @"UTF-8";
header = [NSString stringWithFormat: @"text/calendar; method=%@;"
@" charset=\"%@\"",
[(iCalCalendar *) [object parent] method], charset];
headerMap = [NGMutableHashMap hashMapWithCapacity: 3];
[headerMap setObject: @"urn:content-classes:calendarmessage" forKey: @"Content-Class"];
[headerMap setObject: header forKey: @"content-type"];
bodyPart = [NGMimeBodyPart bodyPartWithHeader: headerMap];
[bodyPart setBody: objectData];
return bodyPart;
}
//
//
//
- (void) sendEMailUsingTemplateNamed: (NSString *) newPageName
forObject: (iCalRepeatableEntityObject *) object
previousObject: (iCalRepeatableEntityObject *) previousObject
toAttendees: (NSArray *) attendees
withType: (NSString *) msgType
{
NSString *pageName;
NSString *senderEmail, *shortSenderEmail, *email;
WOApplication *app;
unsigned i, count;
iCalPerson *attendee;
NSString *recipient;
SOGoAptMailNotification *p;
NSString *mailDate, *subject, *text;
NGMutableHashMap *headerMap;
NGMimeMessage *msg;
NGMimeBodyPart *bodyPart, *eventBodyPart;
NGMimeMultipartBody *body;
SOGoUser *ownerUser;
SOGoDomainDefaults *dd;
// If defined, we return immediately. When not defined, we send the notifications correctly
if ([object firstChildWithTag: @"X-SOGo-Send-Appointment-Notifications"])
return;
// We never send IMIP inivitaton/deletion/update when the "initiator" is an EAS client.
// That is because Outlook, iOS and Android will always issue a SendMail command
// with the meeting details (ie., IMIP message with METHOD:REQUEST) so there's
// no need to send it twice. Moreover, Outlook users can also choose to NOT send
// the IMIP messsage at all, so SOGo won't send one without user's consent
if ([[[context request] requestHandlerKey] isEqualToString: @"Microsoft-Server-ActiveSync"])
return;
ownerUser = [SOGoUser userWithLogin: owner];
dd = [ownerUser domainDefaults];
if ([dd appointmentSendEMailNotifications] && [object isStillRelevant])
{
count = [attendees count];
if (count)
{
/* sender */
shortSenderEmail = [[object organizer] rfc822Email];
if ([shortSenderEmail length])
senderEmail = [[object organizer] mailAddress];
else
{
shortSenderEmail = [[previousObject organizer] rfc822Email];
senderEmail = [[previousObject organizer] mailAddress];
}
// No organizer, grab the event's owner
if (![senderEmail length])
senderEmail = shortSenderEmail = [[ownerUser primaryIdentity] objectForKey: @"email"];
/* calendar part */
eventBodyPart = [self _bodyPartForICalObject: object];
/* get WOApplication instance */
app = [WOApplication application];
/* generate dynamic message content */
for (i = 0; i < count; i++)
{
attendee = [attendees objectAtIndex: i];
// Don't send a notification to the event organizer nor a deletion
// notification to an attendee who already declined the invitation.
if (![[attendee uid] isEqualToString: owner] &&
!([[attendee partStat] compare: @"DECLINED"] == NSOrderedSame &&
[newPageName compare: @"Deletion"] == NSOrderedSame))
{
/* construct recipient */
recipient = [attendee mailAddress];
email = [attendee rfc822Email];
#warning this could be optimized in a class hierarchy common with the \
SOGoObject acl notification mechanism
/* create page name */
pageName = [NSString stringWithFormat: @"SOGoAptMail%@",
newPageName];
/* construct message content */
p = [app pageWithName: pageName inContext: context];
[p setApt: (iCalEvent *) object];
[p setPreviousApt: (iCalEvent *) previousObject];
[p setCurrentAttendee: attendee];
if ([[object organizer] cn] && [[[object organizer] cn] length])
{
[p setOrganizerName: [[object organizer] cn]];
}
else
{
[p setOrganizerName: [ownerUser cn]];
}
subject = [[p getSubject] asQPSubjectString: @"UTF-8"];
text = [p getBody];
/* construct message */
headerMap = [NGMutableHashMap hashMapWithCapacity: 5];
/* NOTE: multipart/alternative seems like the correct choice but
* unfortunately Thunderbird doesn't offer the rich content alternative
* at all. Mail.app shows the rich content alternative _only_
* so we'll stick with multipart/mixed for the time being.
*/
#warning SOPE is just plain stupid here - if you change the case of keys, it will break the encoding of fields
[headerMap setObject: @"multipart/mixed" forKey: @"content-type"];
[headerMap setObject: @"1.0" forKey: @"MIME-Version"];
[headerMap setObject: senderEmail forKey: @"from"];
[headerMap setObject: recipient forKey: @"to"];
mailDate = [[NSCalendarDate date] rfc822DateString];
[headerMap setObject: mailDate forKey: @"date"];
[headerMap setObject: subject forKey: @"subject"];
[headerMap setObject: [NSString generateMessageID] forKey: @"message-id"];
if ([msgType length] > 0)
[headerMap setObject: msgType forKey: @"x-sogo-message-type"];
msg = [NGMimeMessage messageWithHeader: headerMap];
/* multipart body */
body = [[NGMimeMultipartBody alloc] initWithPart: msg];
/* text part */
headerMap = [NGMutableHashMap hashMapWithCapacity: 1];
[headerMap setObject: @"text/html; charset=utf-8"
forKey: @"content-type"];
bodyPart = [NGMimeBodyPart bodyPartWithHeader: headerMap];
[bodyPart setBody: [text dataUsingEncoding: NSUTF8StringEncoding]];
/* attach text part to multipart body */
[body addBodyPart: bodyPart];
/* attach calendar part to multipart body only if the participation role is not NON-PARTICIPANT */
if ([[attendee role] caseInsensitiveCompare: @"NON-PARTICIPANT"] != NSOrderedSame)
[body addBodyPart: eventBodyPart];
/* attach multipart body to message */
[msg setBody: body];
[body release];
/* send the damn thing */
[[SOGoMailer mailerWithDomainDefaults: dd]
sendMimePart: msg
toRecipients: [NSArray arrayWithObject: email]
sender: shortSenderEmail
withAuthenticator: [self authenticatorInContext: context]
inContext: context];
}
}
}
}
}
//
//
//
- (void) _sendIMIPReplyForEvent: (iCalRepeatableEntityObject *) event
from: (SOGoUser *) from
to: (iCalPerson *) recipient
textOnly: (BOOL) textOnly
{
NSString *mailDate, *email;
WOApplication *app;
iCalPerson *attendee;
SOGoAptMailICalReply *p;
NGMutableHashMap *headerMap;
NGMimeMessage *msg;
NGMimeBodyPart *bodyPart;
NGMimeMultipartBody *body;
NSData *bodyData;
SOGoDomainDefaults *dd;
dd = [from domainDefaults];
/* get WOApplication instance */
app = [WOApplication application];
/* remove all alarms to avoid bug #3925 */
[event removeAllAlarms];
/* construct message content */
p = [app pageWithName: @"SOGoAptMailICalReply" inContext: context];
[p setApt: (iCalEvent *) event];
attendee = [event userAsAttendee: from];
[p setAttendee: attendee];
/* construct message */
headerMap = [NGMutableHashMap hashMapWithCapacity: 5];
/* NOTE: multipart/alternative seems like the correct choice but
* unfortunately Thunderbird doesn't offer the rich content alternative
* at all. Mail.app shows the rich content alternative _only_
* so we'll stick with multipart/mixed for the time being.
*/
#warning SOPE is just plain stupid here - if you change the case of keys, it will break the encoding of fields
[headerMap setObject: [attendee mailAddress] forKey: @"from"];
[headerMap setObject: [recipient mailAddress] forKey: @"to"];
mailDate = [[NSCalendarDate date] rfc822DateString];
[headerMap setObject: mailDate forKey: @"date"];
[headerMap setObject: [[p getSubject] asQPSubjectString: @"UTF-8"]
forKey: @"subject"];
[headerMap setObject: [NSString generateMessageID] forKey: @"message-id"];
[headerMap setObject: @"1.0" forKey: @"MIME-Version"];
if (textOnly)
[headerMap setObject: @"text/html; charset=utf-8"
forKey: @"content-type"];
else
[headerMap setObject: @"multipart/mixed" forKey: @"content-type"];
[headerMap setObject: @"calendar:invitation-reply" forKey: @"x-sogo-message-type"];
msg = [NGMimeMessage messageWithHeader: headerMap];
/* text part */
headerMap = [NGMutableHashMap hashMapWithCapacity: 1];
[headerMap setObject: @"text/html; charset=utf-8"
forKey: @"content-type"];
bodyPart = [NGMimeBodyPart bodyPartWithHeader: headerMap];
bodyData = [[p getBody] dataUsingEncoding: NSUTF8StringEncoding];
if (textOnly)
{
[msg setBody: bodyData];
}