2 * Function to go through an ical component set and convert all non-UTC
3 * date/time properties to UTC. It also strips out any VTIMEZONE
4 * subcomponents afterwards, because they're irrelevant.
6 * Copyright (c) 1987-2017 by the citadel.org team
8 * This program is open source software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 #include <sys/types.h>
27 #include <libical/ical.h>
28 #include <libcitadel.h>
31 #include "citserver.h"
32 #include "sysdep_decls.h"
35 #include "ical_dezonify.h"
36 #include "ctdl_module.h"
40 * Figure out which time zone needs to be used for timestamps that are
41 * not UTC and do not have a time zone specified.
43 icaltimezone *get_default_icaltimezone(void) {
45 icaltimezone *zone = NULL;
46 char *default_zone_name = CtdlGetConfigStr("c_default_cal_zone");
49 zone = icaltimezone_get_builtin_timezone(default_zone_name);
52 syslog(LOG_ERR, "ical: Unable to load '%s' time zone. Defaulting to UTC.", default_zone_name);
53 zone = icaltimezone_get_utc_timezone();
56 syslog(LOG_ERR, "ical: unable to load UTC time zone!");
63 * Back end function for ical_dezonify()
65 * We supply this with the master component, the relevant component,
66 * and the property (which will be a DTSTART, DTEND, etc.)
67 * which we want to convert to UTC.
69 void ical_dezonify_backend(icalcomponent *cal,
73 icaltimezone *t = NULL;
75 const char *tzid = NULL;
76 struct icaltimetype TheTime;
77 int utc_declared_as_tzid = 0; /**< Component declared 'TZID=GMT' instead of using Z syntax */
79 /* Give me nothing and I will give you nothing in return. */
80 if (cal == NULL) return;
82 /* Hunt for a TZID parameter in this property. */
83 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
85 /* Get the stringish name of this TZID. */
87 tzid = icalparameter_get_tzid(param);
89 /* Convert it to an icaltimezone type. */
91 /* syslog(LOG_DEBUG, " * Stringy supplied timezone is: '%s'\n", tzid); */
92 if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
93 utc_declared_as_tzid = 1;
94 /* syslog(LOG_DEBUG, " * ...and we handle that internally.\n"); */
97 /* try attached first */
98 t = icalcomponent_get_timezone(cal, tzid);
100 syslog(LOG_DEBUG, " * ...and I %s have tzdata for that zone.\n",
101 (t ? "DO" : "DO NOT")
104 /* then try built-in timezones */
106 t = icaltimezone_get_builtin_timezone(tzid);
109 syslog(LOG_DEBUG, " * Using system tzdata!\n");
118 /* Now we know the timezone. Convert to UTC. */
120 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
121 TheTime = icalproperty_get_dtstart(prop);
123 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
124 TheTime = icalproperty_get_dtend(prop);
126 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
127 TheTime = icalproperty_get_due(prop);
129 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
130 TheTime = icalproperty_get_exdate(prop);
136 /* syslog(LOG_DEBUG, " * Was: %s\n", icaltime_as_ical_string(TheTime)); */
138 if (icaltime_is_utc(TheTime)) {
139 /* syslog(LOG_DEBUG, " * This property is ALREADY UTC.\n"); */
142 else if (utc_declared_as_tzid) {
143 /* syslog(LOG_DEBUG, " * Replacing '%s' TZID with 'Z' suffix.\n", tzid); */
144 TheTime.zone = icaltimezone_get_utc_timezone();
148 /* Do the conversion. */
150 /* syslog(LOG_DEBUG, " * Timezone prop found. Converting to UTC.\n"); */
153 /* syslog(LOG_DEBUG, " * Converting default timezone to UTC.\n"); */
157 t = get_default_icaltimezone();
160 icaltimezone_convert_time(&TheTime,
162 icaltimezone_get_utc_timezone()
164 TheTime.zone = icaltimezone_get_utc_timezone();
167 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
168 /* syslog(LOG_DEBUG, " * Now: %s\n", icaltime_as_ical_string(TheTime)); */
170 /* Now add the converted property back in. */
171 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
172 icalproperty_set_dtstart(prop, TheTime);
174 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
175 icalproperty_set_dtend(prop, TheTime);
177 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
178 icalproperty_set_due(prop, TheTime);
180 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
181 icalproperty_set_exdate(prop, TheTime);
187 * Recursive portion of ical_dezonify()
189 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
194 * Recurse through all subcomponents *except* VTIMEZONE ones.
196 for (c=icalcomponent_get_first_component(
197 rcal, ICAL_ANY_COMPONENT);
199 c = icalcomponent_get_next_component(
200 rcal, ICAL_ANY_COMPONENT)
202 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
203 ical_dezonify_recurse(cal, c);
208 * Now look for DTSTART and DTEND properties
210 for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
212 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
215 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
216 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
217 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
218 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
220 ical_dezonify_backend(cal, rcal, p);
227 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
228 * This function will search any VTIMEZONE subcomponents to learn the
229 * relevant timezone information.
231 void ical_dezonify(icalcomponent *cal) {
232 icalcomponent *vt = NULL;
234 /* syslog(LOG_DEBUG, "ical_dezonify() started\n"); */
236 /* Convert all times to UTC */
237 ical_dezonify_recurse(cal, cal);
239 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
240 while (vt = icalcomponent_get_first_component(
241 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
242 icalcomponent_remove_component(cal, vt);
243 icalcomponent_free(vt);
246 /* syslog(LOG_DEBUG, "ical_dezonify() completed\n"); */