Citadel Server and libcitadel version 922
[citadel.git] / citadel / ical_dezonify.c
1 /*
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.
5  *
6  * Copyright (c) 1987-2017 by the citadel.org team
7  *
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.
10  *
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.
15  */
16
17
18 #include "sysdep.h"
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <syslog.h>
27 #include <libical/ical.h>
28 #include <libcitadel.h>
29 #include "citadel.h"
30 #include "server.h"
31 #include "citserver.h"
32 #include "sysdep_decls.h"
33 #include "support.h"
34 #include "config.h"
35 #include "ical_dezonify.h"
36 #include "ctdl_module.h"
37
38
39 /*
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.
42  */
43 icaltimezone *get_default_icaltimezone(void) {
44
45         icaltimezone *zone = NULL;
46         char *default_zone_name = CtdlGetConfigStr("c_default_cal_zone");
47
48         if (!zone) {
49                 zone = icaltimezone_get_builtin_timezone(default_zone_name);
50         }
51         if (!zone) {
52                 syslog(LOG_ERR, "ical: Unable to load '%s' time zone.  Defaulting to UTC.", default_zone_name);
53                 zone = icaltimezone_get_utc_timezone();
54         }
55         if (!zone) {
56                 syslog(LOG_ERR, "ical: unable to load UTC time zone!");
57         }
58         return zone;
59 }
60
61
62 /*
63  * Back end function for ical_dezonify()
64  *
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.
68  */
69 void ical_dezonify_backend(icalcomponent *cal,
70                         icalcomponent *rcal,
71                         icalproperty *prop) {
72
73         icaltimezone *t = NULL;
74         icalparameter *param;
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 */
78
79         /* Give me nothing and I will give you nothing in return. */
80         if (cal == NULL) return;
81
82         /* Hunt for a TZID parameter in this property. */
83         param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
84
85         /* Get the stringish name of this TZID. */
86         if (param != NULL) {
87                 tzid = icalparameter_get_tzid(param);
88
89                 /* Convert it to an icaltimezone type. */
90                 if (tzid != NULL) {
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"); */
95                         }
96                         else {
97                                 /* try attached first */
98                                 t = icalcomponent_get_timezone(cal, tzid);
99 /*
100                                 syslog(LOG_DEBUG, "                * ...and I %s have tzdata for that zone.\n",
101                                         (t ? "DO" : "DO NOT")
102                                 );
103 */
104                                 /* then try built-in timezones */
105                                 if (!t) {
106                                         t = icaltimezone_get_builtin_timezone(tzid);
107 /*
108                                         if (t) {
109                                                 syslog(LOG_DEBUG, "                * Using system tzdata!\n");
110                                         }
111 */
112                                 }
113                         }
114                 }
115
116         }
117
118         /* Now we know the timezone.  Convert to UTC. */
119
120         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
121                 TheTime = icalproperty_get_dtstart(prop);
122         }
123         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
124                 TheTime = icalproperty_get_dtend(prop);
125         }
126         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
127                 TheTime = icalproperty_get_due(prop);
128         }
129         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
130                 TheTime = icalproperty_get_exdate(prop);
131         }
132         else {
133                 return;
134         }
135
136         /* syslog(LOG_DEBUG, "                * Was: %s\n", icaltime_as_ical_string(TheTime)); */
137
138         if (icaltime_is_utc(TheTime)) {
139                 /* syslog(LOG_DEBUG, "                * This property is ALREADY UTC.\n"); */
140         }
141
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();
145         }
146
147         else {
148                 /* Do the conversion. */
149                 if (t != NULL) {
150                         /* syslog(LOG_DEBUG, "                * Timezone prop found.  Converting to UTC.\n"); */
151                 }
152                 else {
153                         /* syslog(LOG_DEBUG, "                * Converting default timezone to UTC.\n"); */
154                 }
155
156                 if (t == NULL) {
157                         t = get_default_icaltimezone();
158                 }
159
160                 icaltimezone_convert_time(&TheTime,
161                                         t,
162                                         icaltimezone_get_utc_timezone()
163                 );
164                 TheTime.zone = icaltimezone_get_utc_timezone();
165         }
166
167         icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
168         /* syslog(LOG_DEBUG, "                * Now: %s\n", icaltime_as_ical_string(TheTime)); */
169
170         /* Now add the converted property back in. */
171         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
172                 icalproperty_set_dtstart(prop, TheTime);
173         }
174         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
175                 icalproperty_set_dtend(prop, TheTime);
176         }
177         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
178                 icalproperty_set_due(prop, TheTime);
179         }
180         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
181                 icalproperty_set_exdate(prop, TheTime);
182         }
183 }
184
185
186 /*
187  * Recursive portion of ical_dezonify()
188  */
189 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
190         icalcomponent *c;
191         icalproperty *p;
192
193         /*
194          * Recurse through all subcomponents *except* VTIMEZONE ones.
195          */
196         for (c=icalcomponent_get_first_component(
197                                         rcal, ICAL_ANY_COMPONENT);
198                 c != NULL;
199                 c = icalcomponent_get_next_component(
200                                         rcal, ICAL_ANY_COMPONENT)
201         ) {
202                 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
203                         ical_dezonify_recurse(cal, c);
204                 }
205         }
206
207         /*
208          * Now look for DTSTART and DTEND properties
209          */
210         for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
211                 p != NULL;
212                 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
213         ) {
214                 if (
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)
219                    ) {
220                         ical_dezonify_backend(cal, rcal, p);
221                 }
222         }
223 }
224
225
226 /*
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.
230  */
231 void ical_dezonify(icalcomponent *cal) {
232         icalcomponent *vt = NULL;
233
234         /* syslog(LOG_DEBUG, "ical_dezonify() started\n"); */
235
236         /* Convert all times to UTC */
237         ical_dezonify_recurse(cal, cal);
238
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);
244         }
245
246         /* syslog(LOG_DEBUG, "ical_dezonify() completed\n"); */
247 }