1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package davmail.exchange;
20
21 import davmail.Settings;
22 import davmail.util.StringUtil;
23 import org.apache.log4j.Logger;
24
25 import java.io.*;
26 import java.nio.charset.StandardCharsets;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.*;
30
31
32
33
34 public class VCalendar extends VObject {
35 protected static final Logger LOGGER = Logger.getLogger(VCalendar.class);
36 protected VObject firstVevent;
37 protected VObject vTimezone;
38 protected String email;
39
40
41
42
43
44
45
46
47
48 public VCalendar(BufferedReader reader, String email, VObject vTimezone) throws IOException {
49 super(reader);
50 if (!"VCALENDAR".equals(type)) {
51 throw new IOException("Invalid type: " + type);
52 }
53 this.email = email;
54
55 if (this.vTimezone == null && vTimezone != null) {
56 setTimezone(vTimezone);
57 }
58 }
59
60
61
62
63
64
65
66
67
68 public VCalendar(String vCalendarBody, String email, VObject vTimezone) throws IOException {
69 this(new ICSBufferedReader(new StringReader(vCalendarBody)), email, vTimezone);
70 }
71
72
73
74
75
76
77
78
79
80 public VCalendar(byte[] vCalendarContent, String email, VObject vTimezone) throws IOException {
81 this(new ICSBufferedReader(new InputStreamReader(new ByteArrayInputStream(vCalendarContent), StandardCharsets.UTF_8)), email, vTimezone);
82 }
83
84
85
86
87 public VCalendar() {
88 type = "VCALENDAR";
89 }
90
91
92
93
94
95
96 public void setTimezone(VObject vTimezone) {
97 if (vObjects == null) {
98 addVObject(vTimezone);
99 } else {
100 vObjects.add(0, vTimezone);
101 }
102 this.vTimezone = vTimezone;
103 }
104
105 @Override
106 public void addVObject(VObject vObject) {
107 if (firstVevent == null && ("VEVENT".equals(vObject.type) || "VTODO".equals(vObject.type))) {
108 firstVevent = vObject;
109 }
110 if ("VTIMEZONE".equals(vObject.type)) {
111 if (vTimezone == null) {
112 vTimezone = vObject;
113 } else if (vTimezone.getPropertyValue("TZID").equals(vObject.getPropertyValue("TZID"))){
114
115 vObject = null;
116 }
117 }
118 if (vObject != null) {
119 super.addVObject(vObject);
120 }
121 }
122
123 protected boolean isAllDay(VObject vObject) {
124 VProperty dtstart = vObject.getProperty("DTSTART");
125 return dtstart != null && dtstart.hasParam("VALUE", "DATE");
126 }
127
128 protected boolean isCdoAllDay(VObject vObject) {
129 return "TRUE".equals(vObject.getPropertyValue("X-MICROSOFT-CDO-ALLDAYEVENT"));
130 }
131
132
133
134
135
136
137 public boolean isCdoAllDay() {
138 return firstVevent != null && isCdoAllDay(firstVevent);
139 }
140
141
142
143
144
145
146
147 public String getEmailValue(VProperty property) {
148 if (property == null) {
149 return null;
150 }
151 String propertyValue = property.getValue();
152 if (propertyValue != null && (propertyValue.startsWith("MAILTO:") || propertyValue.startsWith("mailto:"))) {
153 return propertyValue.substring(7);
154 } else {
155 return propertyValue;
156 }
157 }
158
159 protected String getMethod() {
160 return getPropertyValue("METHOD");
161 }
162
163 protected void fixVCalendar(boolean fromServer) {
164
165 if (fromServer) {
166 setPropertyValue("X-CALENDARSERVER-ACCESS", getCalendarServerAccess());
167 }
168
169 if (fromServer && "PUBLISH".equals(getPropertyValue("METHOD"))) {
170 removeProperty("METHOD");
171 }
172
173
174 String calendarServerAccess = getPropertyValue("X-CALENDARSERVER-ACCESS");
175 String now = ExchangeSession.getZuluDateFormat().format(new Date());
176
177
178 if (!fromServer && getPropertyValue("METHOD") == null) {
179 setPropertyValue("METHOD", "PUBLISH");
180 }
181
182
183 String tzid = null;
184 if (fromServer) {
185
186 VObject vObject = vTimezone;
187 if (vObject != null) {
188 String currentTzid = vObject.getPropertyValue("TZID");
189
190 if (currentTzid != null && currentTzid.endsWith("\n")) {
191 currentTzid = currentTzid.substring(0, currentTzid.length() - 1);
192 vObject.setPropertyValue("TZID", currentTzid);
193 }
194 if (currentTzid != null && currentTzid.indexOf(' ') >= 0) {
195 try {
196 tzid = ResourceBundle.getBundle("timezones").getString(currentTzid);
197 vObject.setPropertyValue("TZID", tzid);
198 } catch (MissingResourceException e) {
199 LOGGER.debug("Timezone " + currentTzid + " not found in rename table");
200 }
201 }
202 }
203 }
204
205 if (!fromServer) {
206 fixTimezoneToServer();
207 }
208
209
210 for (VObject vObject : vObjects) {
211 if ("VEVENT".equals(vObject.type)) {
212 if (calendarServerAccess != null) {
213 vObject.setPropertyValue("CLASS", getEventClass(calendarServerAccess));
214
215 } else if (vObject.getPropertyValue("X-CALENDARSERVER-ACCESS") != null) {
216 vObject.setPropertyValue("CLASS", getEventClass(vObject.getPropertyValue("X-CALENDARSERVER-ACCESS")));
217 }
218 if (fromServer) {
219
220 if (vObject.getProperty("ATTENDEE") == null) {
221 vObject.setPropertyValue("ORGANIZER", null);
222 }
223
224 if (isCdoAllDay(vObject)) {
225 setClientAllday(vObject.getProperty("DTSTART"));
226 setClientAllday(vObject.getProperty("DTEND"));
227 setClientAllday(vObject.getProperty("RECURRENCE-ID"));
228 }
229 String cdoBusyStatus = vObject.getPropertyValue("X-MICROSOFT-CDO-BUSYSTATUS");
230 if (cdoBusyStatus != null) {
231
232 if ("TENTATIVE".equals(cdoBusyStatus)) {
233 vObject.setPropertyValue("STATUS", "TENTATIVE");
234 }
235
236 vObject.setPropertyValue("TRANSP",
237 !"FREE".equals(cdoBusyStatus) ? "OPAQUE" : "TRANSPARENT");
238 }
239
240
241
242 vObject.removeProperty("X-ENTOURAGE_UUID");
243
244 splitExDate(vObject);
245
246
247 if ("".equals(vObject.getPropertyValue("LOCATION"))) {
248 vObject.removeProperty("LOCATION");
249 }
250 if ("".equals(vObject.getPropertyValue("DESCRIPTION"))) {
251 vObject.removeProperty("DESCRIPTION");
252 }
253 if ("".equals(vObject.getPropertyValue("CLASS"))) {
254 vObject.removeProperty("CLASS");
255 }
256
257 if (tzid != null) {
258 VProperty dtStart = vObject.getProperty("DTSTART");
259 if (dtStart != null && dtStart.getParam("TZID") != null) {
260 dtStart.setParam("TZID", tzid);
261 }
262 VProperty dtEnd = vObject.getProperty("DTEND");
263 if (dtEnd != null && dtEnd.getParam("TZID") != null) {
264 dtEnd.setParam("TZID", tzid);
265 }
266 VProperty recurrenceId = vObject.getProperty("RECURRENCE-ID");
267 if (recurrenceId != null && recurrenceId.getParam("TZID") != null) {
268 recurrenceId.setParam("TZID", tzid);
269 }
270 VProperty exDate = vObject.getProperty("EXDATE");
271 if (exDate != null && exDate.getParam("TZID") != null) {
272 exDate.setParam("TZID", tzid);
273 }
274 }
275
276 if (vObject.getProperty("ATTACH") != null) {
277 List<String> toRemoveValues = null;
278 List<String> values = vObject.getProperty("ATTACH").getValues();
279 for (String value : values) {
280 if (value.contains("CID:")) {
281 if (toRemoveValues == null) {
282 toRemoveValues = new ArrayList<>();
283 }
284 toRemoveValues.add(value);
285 }
286 }
287 if (toRemoveValues != null) {
288 values.removeAll(toRemoveValues);
289 if (values.isEmpty()) {
290 vObject.removeProperty("ATTACH");
291 }
292 }
293 }
294 } else {
295
296 String organizer = getEmailValue(vObject.getProperty("ORGANIZER"));
297 if (organizer == null) {
298 vObject.setPropertyValue("ORGANIZER", "MAILTO:" + email);
299 } else if (!email.equalsIgnoreCase(organizer) && vObject.getProperty("X-MICROSOFT-CDO-REPLYTIME") == null) {
300 vObject.setPropertyValue("X-MICROSOFT-CDO-REPLYTIME", now);
301 }
302
303 vObject.setPropertyValue("X-MICROSOFT-CDO-ALLDAYEVENT", isAllDay(vObject) ? "TRUE" : "FALSE");
304 if (vObject.getPropertyValue("TRANSP") != null) {
305 vObject.setPropertyValue("X-MICROSOFT-CDO-BUSYSTATUS",
306 !"TRANSPARENT".equals(vObject.getPropertyValue("TRANSP")) ? "BUSY" : "FREE");
307 }
308
309 if (isAllDay(vObject)) {
310
311 setServerAllday(vObject.getProperty("DTSTART"));
312 setServerAllday(vObject.getProperty("DTEND"));
313 } else {
314 fixTzid(vObject.getProperty("DTSTART"));
315 fixTzid(vObject.getProperty("DTEND"));
316 }
317 }
318
319 fixAttendees(vObject, fromServer);
320
321 fixAlarm(vObject, fromServer);
322 }
323 }
324
325 }
326
327 private void fixTimezoneToServer() {
328 if (vTimezone != null && vTimezone.vObjects != null && vTimezone.vObjects.size() > 2) {
329 VObject standard = null;
330 VObject daylight = null;
331 for (VObject vObject : vTimezone.vObjects) {
332 if ("STANDARD".equals(vObject.type)) {
333 if (standard == null ||
334 (vObject.getPropertyValue("DTSTART").compareTo(standard.getPropertyValue("DTSTART")) > 0)) {
335 standard = vObject;
336 }
337 }
338 if ("DAYLIGHT".equals(vObject.type)) {
339 if (daylight == null ||
340 (vObject.getPropertyValue("DTSTART").compareTo(daylight.getPropertyValue("DTSTART")) > 0)) {
341 daylight = vObject;
342 }
343 }
344 }
345 vTimezone.vObjects.clear();
346 vTimezone.vObjects.add(standard);
347 vTimezone.vObjects.add(daylight);
348 }
349
350 if (vTimezone != null && vTimezone.vObjects != null) {
351 for (VObject vObject : vTimezone.vObjects) {
352 VProperty rrule = vObject.getProperty("RRULE");
353 if (rrule != null && rrule.getValues().size() == 3 && "BYDAY=-2SU".equals(rrule.getValues().get(1))) {
354 rrule.getValues().set(1, "BYDAY=4SU");
355 }
356
357 if (rrule != null && rrule.getValues().size() == 4 && "BYDAY=FR".equals(rrule.getValues().get(1))
358 && "BYMONTHDAY=23,24,25,26,27,28,29".equals(rrule.getValues().get(2))) {
359 rrule.getValues().set(1, "BYDAY=-1FR");
360 rrule.getValues().remove(2);
361 }
362 }
363 }
364
365
366 if (vTimezone != null && vTimezone.vObjects != null) {
367 for (VObject vObject : vTimezone.vObjects) {
368 VProperty rrule = vObject.getProperty("RRULE");
369 if (rrule != null) {
370 Map<String, String> rruleValueMap = rrule.getValuesAsMap();
371 if (rruleValueMap.containsKey("UNTIL") && rruleValueMap.containsKey("COUNT")) {
372 rrule.removeValue("UNTIL="+rruleValueMap.get("UNTIL"));
373 }
374 }
375 }
376 }
377
378
379
380 ResourceBundle tzBundle = ResourceBundle.getBundle("exchtimezones");
381 ResourceBundle tzidsBundle = ResourceBundle.getBundle("stdtimezones");
382 for (VObject vObject : vObjects) {
383 if (vObject.isVTimezone()) {
384 String tzid = vObject.getPropertyValue("TZID");
385
386 if (!tzidsBundle.containsKey(tzid)) {
387 String exchangeTzid = null;
388
389 if (tzBundle.containsKey(tzid)) {
390 exchangeTzid = tzBundle.getString(tzid);
391 } else {
392
393 for (VObject tzDefinition : vObject.vObjects) {
394 if ("STANDARD".equals(tzDefinition.type)) {
395 exchangeTzid = getTzidFromOffset(tzDefinition.getPropertyValue("TZOFFSETTO"));
396 }
397 }
398 }
399 if (exchangeTzid != null) {
400 vObject.setPropertyValue("TZID", exchangeTzid);
401
402 updateTzid(tzid, exchangeTzid);
403 }
404 }
405 }
406 }
407 }
408
409 protected void updateTzid(String tzid, String newTzid) {
410 for (VObject vObject : vObjects) {
411 if (vObject.isVEvent()) {
412 for (VProperty vProperty : vObject.properties) {
413 if (tzid.equalsIgnoreCase(vProperty.getParamValue("TZID"))) {
414 vProperty.setParam("TZID", newTzid);
415 }
416 }
417 }
418 }
419 }
420
421 private void fixTzid(VProperty property) {
422 if (property != null && !property.hasParam("TZID")) {
423 property.addParam("TZID", vTimezone.getPropertyValue("TZID"));
424 }
425 }
426
427 protected void splitExDate(VObject vObject) {
428 List<VProperty> exDateProperties = vObject.getProperties("EXDATE");
429 if (exDateProperties != null) {
430 for (VProperty property : exDateProperties) {
431 String value = property.getValue();
432 if (value.indexOf(',') >= 0) {
433
434 vObject.removeProperty(property);
435 for (String singleValue : value.split(",")) {
436 VProperty singleProperty = new VProperty("EXDATE", singleValue);
437 singleProperty.setParams(property.getParams());
438 vObject.addProperty(singleProperty);
439 }
440 }
441 }
442 }
443 }
444
445 protected void setServerAllday(VProperty property) {
446 if (vTimezone != null) {
447
448 if (!property.hasParam("TZID")) {
449 property.addParam("TZID", vTimezone.getPropertyValue("TZID"));
450 }
451
452 property.removeParam("VALUE");
453 String value = property.getValue();
454 if (value.length() != 8) {
455 LOGGER.warn("Invalid date value in allday event: " + value);
456 }
457 property.setValue(property.getValue() + "T000000");
458 }
459 }
460
461 protected void setClientAllday(VProperty property) {
462 if (property != null) {
463
464 if (!property.hasParam("VALUE")) {
465 property.addParam("VALUE", "DATE");
466 }
467
468 property.removeParam("TZID");
469 String value = property.getValue();
470 if (value.length() != 8) {
471
472 try {
473 Calendar calendar = Calendar.getInstance();
474 SimpleDateFormat dateParser = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
475 calendar.setTime(dateParser.parse(value));
476 calendar.add(Calendar.HOUR_OF_DAY, 12);
477 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
478 value = dateFormatter.format(calendar.getTime());
479 } catch (ParseException e) {
480 LOGGER.warn("Invalid date value in allday event: " + value);
481 }
482 }
483 property.setValue(value);
484 }
485 }
486
487 protected void fixAlarm(VObject vObject, boolean fromServer) {
488 if (vObject.vObjects != null) {
489 if (Settings.getBooleanProperty("davmail.caldavDisableReminders", false)) {
490 ArrayList<VObject> vAlarms = null;
491 for (VObject vAlarm : vObject.vObjects) {
492 if ("VALARM".equals(vAlarm.type)) {
493 if (vAlarms == null) {
494 vAlarms = new ArrayList<>();
495 }
496 vAlarms.add(vAlarm);
497 }
498 }
499
500 if (vAlarms != null) {
501 for (VObject vAlarm : vAlarms) {
502 vObject.vObjects.remove(vAlarm);
503 }
504 }
505
506 } else {
507 for (VObject vAlarm : vObject.vObjects) {
508 if ("VALARM".equals(vAlarm.type)) {
509 String action = vAlarm.getPropertyValue("ACTION");
510 if (fromServer && "DISPLAY".equals(action)
511
512 && Settings.getProperty("davmail.caldavAlarmSound") != null) {
513
514 vAlarm.setPropertyValue("ACTION", "AUDIO");
515
516 if (vAlarm.getPropertyValue("ATTACH") == null) {
517
518 VProperty vProperty = new VProperty("ATTACH", Settings.getProperty("davmail.caldavAlarmSound"));
519 vProperty.addParam("VALUE", "URI");
520 vAlarm.addProperty(vProperty);
521 }
522
523 } else if (!fromServer && "AUDIO".equals(action)) {
524
525
526 vAlarm.setPropertyValue("ACTION", "DISPLAY");
527 }
528 }
529 }
530 }
531 }
532 }
533
534
535
536
537
538
539
540 protected String replaceIcal4Principal(String value) {
541 final String principalPrefix = "/principals/__uuids__/";
542 final String principalAt = "__AT__";
543 if (value.contains(principalPrefix) && value.contains(principalAt)) {
544 return "mailto:" +
545 value.substring(value.indexOf(principalPrefix) + principalPrefix.length(), value.indexOf(principalAt)) +
546 "@" +
547 value.substring(value.indexOf(principalAt) + principalAt.length(), value.length() - 1);
548 } else {
549 return value;
550 }
551 }
552
553 private void fixAttendees(VObject vObject, boolean fromServer) {
554 if (vObject.properties != null) {
555 for (VProperty property : vObject.properties) {
556 if ("ATTENDEE".equalsIgnoreCase(property.getKey())) {
557 if (fromServer) {
558
559
560
561
562 if (isCurrentUser(property) && property.hasParam("RSVP", "TRUE")) {
563 if (!"NEEDS-ACTION".equals(property.getParamValue("PARTSTAT"))) {
564 property.removeParam("RSVP");
565 }
566 }
567 } else {
568 property.setValue(replaceIcal4Principal(property.getValue()));
569 }
570 }
571
572 }
573 }
574
575 }
576
577 private boolean isCurrentUser(VProperty property) {
578 return property.getValue().equalsIgnoreCase("mailto:" + email);
579 }
580
581
582
583
584
585
586 public VObject getVTimezone() {
587 return vTimezone;
588 }
589
590
591
592
593
594
595
596
597 protected String getEventClass(String calendarServerAccess) {
598 if ("PRIVATE".equalsIgnoreCase(calendarServerAccess)) {
599 return "CONFIDENTIAL";
600 } else if ("CONFIDENTIAL".equalsIgnoreCase(calendarServerAccess) || "RESTRICTED".equalsIgnoreCase(calendarServerAccess)) {
601 return "PRIVATE";
602 } else {
603 return null;
604 }
605 }
606
607
608
609
610
611
612
613 protected String getCalendarServerAccess() {
614 String eventClass = getFirstVeventPropertyValue("CLASS");
615 if ("PRIVATE".equalsIgnoreCase(eventClass)) {
616 return "CONFIDENTIAL";
617 } else if ("CONFIDENTIAL".equalsIgnoreCase(eventClass)) {
618 return "PRIVATE";
619 } else {
620 return null;
621 }
622 }
623
624
625
626
627
628
629
630 public String getFirstVeventPropertyValue(String name) {
631 if (firstVevent == null) {
632 return null;
633 } else {
634 return firstVevent.getPropertyValue(name);
635 }
636 }
637
638 protected VProperty getFirstVeventProperty(String name) {
639 if (firstVevent == null) {
640 return null;
641 } else {
642 return firstVevent.getProperty(name);
643 }
644 }
645
646
647
648
649
650
651
652
653 public List<VProperty> getFirstVeventProperties(String name) {
654 if (firstVevent == null) {
655 return null;
656 } else {
657 return firstVevent.getProperties(name);
658 }
659 }
660
661
662
663
664 public void removeVAlarm() {
665 if (vObjects != null) {
666 for (VObject vObject : vObjects) {
667 if ("VEVENT".equals(vObject.type)) {
668
669 if (vObject.vObjects != null) {
670 vObject.vObjects = null;
671 }
672 }
673 }
674 }
675 }
676
677
678
679
680
681
682 public boolean hasVAlarm() {
683 if (vObjects != null) {
684 for (VObject vObject : vObjects) {
685 if ("VEVENT".equals(vObject.type)) {
686 if (vObject.vObjects != null && !vObject.vObjects.isEmpty()) {
687 return vObject.vObjects.get(0).isVAlarm();
688 }
689 }
690 }
691 }
692 return false;
693 }
694
695 public String getReminderMinutesBeforeStart() {
696 String result = "0";
697 if (vObjects != null) {
698 for (VObject vObject : vObjects) {
699 if (vObject.vObjects != null && !vObject.vObjects.isEmpty() &&
700 vObject.vObjects.get(0).isVAlarm()) {
701 String trigger = vObject.vObjects.get(0).getPropertyValue("TRIGGER");
702 if (trigger != null) {
703 if (trigger.startsWith("-PT") && trigger.endsWith("M")) {
704 result = trigger.substring(3, trigger.length() - 1);
705 } else if (trigger.startsWith("-PT") && trigger.endsWith("H")) {
706 result = trigger.substring(3, trigger.length() - 1);
707
708 result = String.valueOf(Integer.parseInt(result) * 60);
709 } else if (trigger.startsWith("-P") && trigger.endsWith("D")) {
710 result = trigger.substring(2, trigger.length() - 1);
711
712 result = String.valueOf(Integer.parseInt(result) * 60 * 24);
713 } else if (trigger.startsWith("-P") && trigger.endsWith("W")) {
714 result = trigger.substring(2, trigger.length() - 1);
715
716 result = String.valueOf(Integer.parseInt(result) * 60 * 24 * 7);
717 }
718 }
719 }
720 }
721 }
722 return result;
723 }
724
725
726
727
728
729
730
731 public boolean isMeeting() {
732 return getFirstVeventProperty("ATTENDEE") != null;
733 }
734
735
736
737
738
739
740 public boolean isMeetingOrganizer() {
741 return email.equalsIgnoreCase(getEmailValue(getFirstVeventProperty("ORGANIZER")));
742 }
743
744
745
746
747
748
749
750 public void setFirstVeventPropertyValue(String propertyName, String propertyValue) {
751 firstVevent.setPropertyValue(propertyName, propertyValue);
752 }
753
754
755
756
757
758
759 public void addFirstVeventProperty(VProperty vProperty) {
760 firstVevent.addProperty(vProperty);
761 }
762
763
764
765
766
767
768 public boolean isTodo() {
769 return firstVevent != null && "VTODO".equals(firstVevent.type);
770 }
771
772
773
774
775
776 public String getCalendarEmail() {
777 return email;
778 }
779
780
781
782
783 public static class Recipients {
784
785
786
787 public String attendees;
788
789
790
791
792 public String optionalAttendees;
793
794
795
796
797 public String organizer;
798 }
799
800
801
802
803
804
805
806 public Recipients getRecipients(boolean isNotification) {
807
808 HashSet<String> attendees = new HashSet<>();
809 HashSet<String> optionalAttendees = new HashSet<>();
810
811
812 List<VProperty> attendeeProperties = getFirstVeventProperties("ATTENDEE");
813 if (attendeeProperties != null) {
814 for (VProperty property : attendeeProperties) {
815
816
817 String attendeeEmail = getEmailValue(property);
818 if (!email.equalsIgnoreCase(attendeeEmail) && attendeeEmail != null && attendeeEmail.indexOf('@') >= 0
819
820 && (!isNotification
821
822 || (property.hasParam("RSVP", "TRUE"))
823 || (
824
825 !(property.hasParam("RSVP", "FALSE")) &&
826 ((property.hasParam("PARTSTAT", "NEEDS-ACTION")
827
828 || property.hasParam("PARTSTAT", "ACCEPTED")
829 || property.hasParam("PARTSTAT", "DECLINED")
830 || property.hasParam("PARTSTAT", "TENTATIVE")))
831 ))) {
832 if (property.hasParam("ROLE", "OPT-PARTICIPANT")) {
833 optionalAttendees.add(attendeeEmail);
834 } else {
835 attendees.add(attendeeEmail);
836 }
837 }
838 }
839 }
840 Recipients recipients = new Recipients();
841 recipients.organizer = getEmailValue(getFirstVeventProperty("ORGANIZER"));
842 recipients.attendees = StringUtil.join(attendees, ", ");
843 recipients.optionalAttendees = StringUtil.join(optionalAttendees, ", ");
844 return recipients;
845 }
846
847 public String getAttendeeStatus() {
848 String status = null;
849 List<VProperty> attendeeProperties = getFirstVeventProperties("ATTENDEE");
850 if (attendeeProperties != null) {
851 for (VProperty property : attendeeProperties) {
852 String attendeeEmail = getEmailValue(property);
853 if (email.equalsIgnoreCase(attendeeEmail) && property.hasParam("PARTSTAT")) {
854
855 status = property.getParamValue("PARTSTAT");
856 break;
857 }
858 }
859 }
860 return status;
861 }
862
863
864
865
866
867
868 public VObject getFirstVevent() {
869 return firstVevent;
870 }
871
872
873
874
875
876
877 public List<VObject> getModifiedOccurrences() {
878 boolean first = true;
879 ArrayList<VObject> results = new ArrayList<>();
880 for (VObject vObject : vObjects) {
881 if ("VEVENT".equals(vObject.type)) {
882 if (first) {
883 first = false;
884 } else {
885 results.add(vObject);
886 }
887 }
888 }
889 return results;
890 }
891
892 public TimeZone getStandardTimezoneId(String tzid) {
893 String convertedTzid;
894
895 try {
896 convertedTzid = ResourceBundle.getBundle("timezones").getString(tzid);
897 } catch (MissingResourceException e) {
898 convertedTzid = tzid;
899
900 VObject vTimezone = getVTimezone();
901 for (VObject tzDefinition : vTimezone.vObjects) {
902 if ("STANDARD".equals(tzDefinition.type)) {
903 convertedTzid = getTzidFromOffset(tzDefinition.getPropertyValue("TZOFFSETTO"));
904 }
905 }
906 convertedTzid = ResourceBundle.getBundle("timezones").getString(convertedTzid);
907 }
908 return TimeZone.getTimeZone(convertedTzid);
909
910 }
911
912 private String getTzidFromOffset(String tzOffset) {
913 if (tzOffset == null) {
914 return null;
915 } else if (tzOffset.length() == 7) {
916 tzOffset = tzOffset.substring(0, 5);
917 }
918 return ResourceBundle.getBundle("tzoffsettimezones").getString(tzOffset);
919 }
920
921 public String convertCalendarDateToExchangeZulu(String vcalendarDateValue, String tzid) throws IOException {
922 String zuluDateValue = null;
923 TimeZone timeZone;
924 if (tzid == null) {
925 timeZone = ExchangeSession.GMT_TIMEZONE;
926 } else {
927 timeZone = getStandardTimezoneId(tzid);
928 }
929 if (vcalendarDateValue != null) {
930 try {
931 SimpleDateFormat dateParser;
932 if (vcalendarDateValue.length() == 8) {
933 dateParser = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
934 } else {
935 dateParser = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);
936 }
937 dateParser.setTimeZone(timeZone);
938 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
939 dateFormatter.setTimeZone(ExchangeSession.GMT_TIMEZONE);
940 zuluDateValue = dateFormatter.format(dateParser.parse(vcalendarDateValue));
941 } catch (ParseException e) {
942 throw new IOException("Invalid date " + vcalendarDateValue + " with tzid " + tzid);
943 }
944 }
945 return zuluDateValue;
946 }
947
948
949
950
951
952
953
954
955 public String convertCalendarDateToGraph(String vcalendarDateValue, String tzid) throws IOException {
956 String graphDateValue = null;
957 TimeZone timeZone;
958 if (tzid == null) {
959 timeZone = ExchangeSession.GMT_TIMEZONE;
960 } else {
961 timeZone = getStandardTimezoneId(tzid);
962 }
963 if (vcalendarDateValue != null) {
964 try {
965 SimpleDateFormat dateParser;
966 if (vcalendarDateValue.length() == 8) {
967 dateParser = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
968 } else {
969 dateParser = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);
970 }
971 dateParser.setTimeZone(timeZone);
972 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
973 dateFormatter.setTimeZone(timeZone);
974 graphDateValue = dateFormatter.format(dateParser.parse(vcalendarDateValue));
975 } catch (ParseException e) {
976 throw new IOException("Invalid date " + vcalendarDateValue + " with tzid " + tzid);
977 }
978 }
979 return graphDateValue;
980 }
981
982 }