a39342d4c9ab658d4a807cebdc51660325267349
[citadel.git] / webcit / ical_dezonify.c
1 // Go through an ical component set and convert all non-UTC date/time properties to UTC.
2 // It also strips out any VTIMEZONE subcomponents afterwards, because they are now unreferenced.
3 //
4 // Everything here will work on both components and subcomponents.  If subcomponents are discovered it will recurse through them.
5 //
6 // Copyright (c) 2002-2024 by the citadel.org team (Art Cancro et al)
7 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
8
9 #include "webcit.h"
10 #include "webserver.h"
11
12 // Figure out which time zone needs to be used for timestamps that are not UTC and do not have a time zone specified.
13 icaltimezone *get_default_icaltimezone(void) {
14
15         icaltimezone *zone = NULL;
16
17         if (!zone) {
18                 zone = icaltimezone_get_builtin_timezone(default_zone_name);
19         }
20         if (!zone) {
21                 syslog(LOG_WARNING, "ical_dezonify: unable to load '%s' time zone, defaulting to UTC", default_zone_name);
22                 zone = icaltimezone_get_utc_timezone();
23         }
24         if (!zone) {
25                 syslog(LOG_ERR, "ical_dezonify: unable to load UTC time zone!");
26         }
27         return zone;
28 }
29
30
31 // Back end function for ical_dezonify()
32 //
33 // We supply this with the master component, the relevant component, and the property
34 // (which will be a DTSTART, DTEND, etc.) which we want to convert to UTC.
35 void ical_dezonify_backend(icalcomponent *cal, icalcomponent *rcal, icalproperty *prop) {
36
37         icaltimezone *t = NULL;
38         icalparameter *param;
39         const char *tzid = NULL;
40         struct icaltimetype TheTime;
41         int utc_declared_as_tzid = 0;   // Component declared 'TZID=GMT' instead of using Z syntax
42
43         // Give me nothing and I will give you nothing in return.
44         if (cal == NULL) return;
45
46         // Hunt for a TZID parameter in this property.
47         param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
48
49         // Get the stringish name of this TZID.
50         if (param != NULL) {
51                 tzid = icalparameter_get_tzid(param);
52
53                 // Convert it to an icaltimezone type.
54                 if (tzid != NULL) {
55 #ifdef DBG_ICAL
56                         syslog(LOG_DEBUG, "ical_dezonify: Stringy supplied timezone is: '%s'\n", tzid);
57 #endif
58                         if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
59                                 utc_declared_as_tzid = 1;
60 #ifdef DBG_ICAL
61                                 syslog(LOG_DEBUG, "ical_dezonify: ...and we handle that internally.\n");
62 #endif
63                         }
64                         else {
65                                 // try attached first
66                                 t = icalcomponent_get_timezone(cal, tzid);
67 #ifdef DBG_ICAL
68                                 syslog(LOG_DEBUG, "ical_dezonify: ...and I %s have tzdata for that zone.\n",
69                                         (t ? "DO" : "DO NOT")
70                                 );
71 #endif
72                                 // then try built-in timezones
73                                 if (!t) {
74                                         t = icaltimezone_get_builtin_timezone(tzid);
75 #ifdef DBG_ICAL
76                                         if (t) {
77                                                 syslog(LOG_DEBUG, "ical_dezonify: Using system tzdata!\n");
78                                         }
79 #endif
80                                 }
81                         }
82                 }
83
84         }
85
86         // Now we know the timezone.  Convert to UTC.
87
88         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
89                 TheTime = icalproperty_get_dtstart(prop);
90         }
91         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
92                 TheTime = icalproperty_get_dtend(prop);
93         }
94         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
95                 TheTime = icalproperty_get_due(prop);
96         }
97         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
98                 TheTime = icalproperty_get_exdate(prop);
99         }
100         else {
101                 return;
102         }
103
104 #ifdef DBG_ICAL
105         syslog(LOG_DEBUG, "ical_dezonify: was: %s\n", icaltime_as_ical_string(TheTime));
106 #endif
107
108         if (icaltime_is_utc(TheTime)) {
109 #ifdef DBG_ICAL
110                 syslog(LOG_DEBUG, "ical_dezonify: this property is ALREADY UTC");
111 #endif
112         }
113
114         else if (utc_declared_as_tzid) {
115 #ifdef DBG_ICAL
116                 syslog(LOG_DEBUG, "ical_dezonify: replacing '%s' TZID with 'Z' suffix", tzid);
117 #endif
118                 TheTime.zone = icaltimezone_get_utc_timezone();
119         }
120
121         else {
122                 // Do the conversion.
123                 if (t != NULL) {
124 #ifdef DBG_ICAL
125                         syslog(LOG_DEBUG, "ical_dezonify: timezone prop found, converting to UTC");
126 #endif
127                 }
128                 else {
129 #ifdef DBG_ICAL
130                         syslog(LOG_DEBUG, "ical_dezonify: converting default timezone to UTC");
131 #endif
132                 }
133
134                 if (t == NULL) {
135                         t = get_default_icaltimezone();
136                 }
137                 icaltimezone_convert_time(&TheTime, t, icaltimezone_get_utc_timezone());
138                 TheTime.zone = icaltimezone_get_utc_timezone();
139         }
140
141         icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
142 #ifdef DBG_ICAL
143         syslog(LOG_DEBUG, "ical_dezonify: now: %s", icaltime_as_ical_string(TheTime));
144 #endif
145
146         // Now add the converted property back in.
147         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
148                 icalproperty_set_dtstart(prop, TheTime);
149         }
150         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
151                 icalproperty_set_dtend(prop, TheTime);
152         }
153         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
154                 icalproperty_set_due(prop, TheTime);
155         }
156         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
157                 icalproperty_set_exdate(prop, TheTime);
158         }
159 }
160
161
162 // Recursive portion of ical_dezonify()
163 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
164         icalcomponent *c;
165         icalproperty *p;
166
167         // Recurse through all subcomponents *except* VTIMEZONE ones.
168         for (   c=icalcomponent_get_first_component(rcal, ICAL_ANY_COMPONENT);
169                 c != NULL;
170                 c = icalcomponent_get_next_component(rcal, ICAL_ANY_COMPONENT)
171         ) {
172                 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
173                         ical_dezonify_recurse(cal, c);
174                 }
175         }
176
177         // Now look for DTSTART and DTEND properties
178         for (   p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
179                 p != NULL;
180                 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
181         ) {
182                 if (
183                         (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
184                         || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
185                         || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
186                         || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
187                 ) {
188                         ical_dezonify_backend(cal, rcal, p);
189                 }
190         }
191 }
192
193
194 // Convert all DTSTART and DTEND properties in all subcomponents to UTC.
195 // This function will search any VTIMEZONE subcomponents to learn the
196 // relevant timezone information.
197 void ical_dezonify(icalcomponent *cal) {
198         icalcomponent *vt = NULL;
199
200 #ifdef DBG_ICAL
201         syslog(LOG_DEBUG, "ical_dezonify() started");
202 #endif
203
204         // Convert all times to UTC
205         ical_dezonify_recurse(cal, cal);
206
207         // Strip out VTIMEZONE subcomponents -- we don't need them anymore.
208         while (vt = icalcomponent_get_first_component(cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
209                 icalcomponent_remove_component(cal, vt);
210                 icalcomponent_free(vt);
211         }
212
213 #ifdef DBG_ICAL
214         syslog(LOG_DEBUG, "ical_dezonify() completed");
215 #endif
216 }