From 9fe2de753b9865b0be5d9bd0367fbcc1e1f38958 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Mon, 8 Feb 2021 11:28:39 -0500 Subject: [PATCH] fix(calendar): try to repair VCALENDAR when parsing versit string It seems that Exchange 2010 and 2016 sometimes produce calendar invitation emails with text/calendar multipart parts in the email that have "END:VCALENDAR" at the end of the calendar data missing. --- SOPE/NGCards/iCalCalendar.m | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/SOPE/NGCards/iCalCalendar.m b/SOPE/NGCards/iCalCalendar.m index 1eab53ecd..0834ed555 100644 --- a/SOPE/NGCards/iCalCalendar.m +++ b/SOPE/NGCards/iCalCalendar.m @@ -22,6 +22,8 @@ #import #import +#import + #import "iCalEvent.h" #import "iCalToDo.h" #import "iCalJournal.h" @@ -39,6 +41,30 @@ @implementation iCalCalendar ++ (NSArray *) parseFromSource: (id) source +{ + NSArray *result; + NSMutableString *content; + + result = [super parseFromSource: source]; + + // If parsing doesn't return any card, check if the opening tag is present but the ending tag + // missing. Add the ending tag and parse the source again in this case. + if ([result count] == 0 && + [source length] && + [source hasPrefix: @"BEGIN:VCALENDAR"] && + !([source hasSuffix: @"END:VCALENDAR\r\n"] || [source hasSuffix: @"END:VCALENDAR"])) + { + content = [NSMutableString stringWithString: source]; + [content appendString: @"END:VCALENDAR"]; + result = [super parseFromSource: content]; + if ([result count] > 0) + [self logWithFormat: @"Successfully repaired VCALENDAR by adding ending tag"]; + } + + return result; +} + /* class mapping */ - (Class) classForTag: (NSString *) classTag {