Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iCalendar recurrence and timezone handling #654

Merged
merged 2 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Update result diff generation at delta reports [#650](https://github.com/greenbone/gvmd/pull/650)

### Fixed

- Fix iCalendar recurrence and timezone handling [#654](https://github.com/greenbone/gvmd/pull/654)

### Removed
- The handling of NVT updates via OTP has been removed. [#575](https://github.com/greenbone/gvmd/pull/575)
Expand Down
88 changes: 57 additions & 31 deletions src/manage_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1584,46 +1584,67 @@ icalendar_next_time_from_recurrence (struct icalrecurrencetype recurrence,
icaltimetype recur_time, prev_time, next_time;
time_t rrule_time, rdates_time;

// Start iterating over rule-based times
recur_iter = icalrecur_iterator_new (recurrence, dtstart);

/* Get the first rule-based recurrence time, skipping ahead in case DTSTART
* is excluded by EXDATEs. */
recur_time = icalrecur_iterator_next (recur_iter);
while (icaltime_is_null_time (recur_time) == FALSE
&& icalendar_time_matches_array (recur_time, exdates))
{
recur_time = icalrecur_iterator_next (recur_iter);
}

// Set the first recur_time as either the previous or next time.
if (icaltime_compare (recur_time, reference_time) < 0)
if (icaltime_is_null_time (recur_time))
{
prev_time = recur_time;
// Use DTSTART if there are no recurrence rule times
if (icaltime_compare (dtstart, reference_time) < 0)
{
prev_time = dtstart;
next_time = icaltime_null_time ();
}
else
{
prev_time = icaltime_null_time ();
next_time = dtstart;
}
}
else
{
prev_time = icaltime_null_time ();
}
/* Handle rule-based recurrence times:
* Get the first rule-based recurrence time, skipping ahead in case
* DTSTART is excluded by EXDATEs. */

// Iterate over rule-based recurrences up to first time after reference time
while (icaltime_is_null_time (recur_time) == FALSE
&& icaltime_compare (recur_time, reference_time) < 0)
{
if (icalendar_time_matches_array (recur_time, exdates) == FALSE)
prev_time = recur_time;
while (icaltime_is_null_time (recur_time) == FALSE
&& icalendar_time_matches_array (recur_time, exdates))
{
recur_time = icalrecur_iterator_next (recur_iter);
}

recur_time = icalrecur_iterator_next (recur_iter);
}
// Set the first recur_time as either the previous or next time.
if (icaltime_compare (recur_time, reference_time) < 0)
{
prev_time = recur_time;
}
else
{
prev_time = icaltime_null_time ();
}

// Skip further ahead if last recurrence time is in EXDATEs
while (icaltime_is_null_time (recur_time) == FALSE
&& icalendar_time_matches_array (recur_time, exdates))
{
recur_time = icalrecur_iterator_next (recur_iter);
}
/* Iterate over rule-based recurrences up to first time after
* reference time */
while (icaltime_is_null_time (recur_time) == FALSE
&& icaltime_compare (recur_time, reference_time) < 0)
{
if (icalendar_time_matches_array (recur_time, exdates) == FALSE)
prev_time = recur_time;

// Select last recur_time as the next_time
next_time = recur_time;
recur_time = icalrecur_iterator_next (recur_iter);
}

// Skip further ahead if last recurrence time is in EXDATEs
while (icaltime_is_null_time (recur_time) == FALSE
&& icalendar_time_matches_array (recur_time, exdates))
{
recur_time = icalrecur_iterator_next (recur_iter);
}

// Select last recur_time as the next_time
next_time = recur_time;
}

// Get time from RDATEs
rdates_time = icalendar_next_time_from_rdates (rdates, reference_time, tz,
Expand Down Expand Up @@ -1666,7 +1687,7 @@ icalendar_next_time_from_vcalendar (icalcomponent *vcalendar,
int periods_offset)
{
icalcomponent *vevent;
icaltimetype dtstart, ical_now;
icaltimetype dtstart, dtstart_with_tz, ical_now;
icaltimezone *tz;
icalproperty *rrule_prop;
struct icalrecurrencetype recurrence;
Expand Down Expand Up @@ -1698,6 +1719,10 @@ icalendar_next_time_from_vcalendar (icalcomponent *vcalendar,
if (tz == NULL)
tz = icalendar_timezone_from_tzid (default_tzid);

dtstart_with_tz = dtstart;
// Set timezone in case the original DTSTART did not have any set.
icaltime_set_timezone (&dtstart_with_tz, tz);

// Get current time
ical_now = icaltime_current_time_with_zone (tz);
// Set timezone explicitly because icaltime_current_time_with_zone doesn't.
Expand All @@ -1719,7 +1744,8 @@ icalendar_next_time_from_vcalendar (icalcomponent *vcalendar,

// Calculate next time.
next_time = icalendar_next_time_from_recurrence (recurrence,
dtstart, ical_now, tz,
dtstart_with_tz,
ical_now, tz,
exdates, rdates,
periods_offset);

Expand Down