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