More conversion to the new config system. WARNING BROKEN BUILD
[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-2015 by the citadel.org team
7  *
8  * This program is open source software; you can redistribute it and/or modify
9  * 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
37 #include "ctdl_module.h"
38
39
40 /*
41  * Figure out which time zone needs to be used for timestamps that are
42  * not UTC and do not have a time zone specified.
43  */
44 icaltimezone *get_default_icaltimezone(void) {
45
46         icaltimezone *zone = NULL;
47         char *default_zone_name = CtdlGetConfigStr("c_default_cal_zone");
48
49         if (!zone) {
50                 zone = icaltimezone_get_builtin_timezone(default_zone_name);
51         }
52         if (!zone) {
53                 syslog(LOG_ALERT,
54                         "Unable to load '%s' time zone.  Defaulting to UTC.\n",
55                         default_zone_name);
56                 zone = icaltimezone_get_utc_timezone();
57         }
58         if (!zone) {
59                 syslog(LOG_ALERT, "Unable to load UTC time zone!\n");
60         }
61         return zone;
62 }
63
64
65 /*
66  * Back end function for ical_dezonify()
67  *
68  * We supply this with the master component, the relevant component,
69  * and the property (which will be a DTSTART, DTEND, etc.)
70  * which we want to convert to UTC.
71  */
72 void ical_dezonify_backend(icalcomponent *cal,
73                         icalcomponent *rcal,
74                         icalproperty *prop) {
75
76         icaltimezone *t = NULL;
77         icalparameter *param;
78         const char *tzid = NULL;
79         struct icaltimetype TheTime;
80         int utc_declared_as_tzid = 0;   /**< Component declared 'TZID=GMT' instead of using Z syntax */
81
82         /* Give me nothing and I will give you nothing in return. */
83         if (cal == NULL) return;
84
85         /* Hunt for a TZID parameter in this property. */
86         param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
87
88         /* Get the stringish name of this TZID. */
89         if (param != NULL) {
90                 tzid = icalparameter_get_tzid(param);
91
92                 /* Convert it to an icaltimezone type. */
93                 if (tzid != NULL) {
94                         /* syslog(LOG_DEBUG, "                * Stringy supplied timezone is: '%s'\n", tzid); */
95                         if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
96                                 utc_declared_as_tzid = 1;
97                                 /* syslog(LOG_DEBUG, "                * ...and we handle that internally.\n"); */
98                         }
99                         else {
100                                 /* try attached first */
101                                 t = icalcomponent_get_timezone(cal, tzid);
102 /*
103                                 syslog(LOG_DEBUG, "                * ...and I %s have tzdata for that zone.\n",
104                                         (t ? "DO" : "DO NOT")
105                                 );
106 */
107                                 /* then try built-in timezones */
108                                 if (!t) {
109                                         t = icaltimezone_get_builtin_timezone(tzid);
110 /*
111                                         if (t) {
112                                                 syslog(LOG_DEBUG, "                * Using system tzdata!\n");
113                                         }
114 */
115                                 }
116                         }
117                 }
118
119         }
120
121         /* Now we know the timezone.  Convert to UTC. */
122
123         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
124                 TheTime = icalproperty_get_dtstart(prop);
125         }
126         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
127                 TheTime = icalproperty_get_dtend(prop);
128         }
129         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
130                 TheTime = icalproperty_get_due(prop);
131         }
132         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
133                 TheTime = icalproperty_get_exdate(prop);
134         }
135         else {
136                 return;
137         }
138
139         /* syslog(LOG_DEBUG, "                * Was: %s\n", icaltime_as_ical_string(TheTime)); */
140
141         if (TheTime.is_utc) {
142                 /* syslog(LOG_DEBUG, "                * This property is ALREADY UTC.\n"); */
143         }
144
145         else if (utc_declared_as_tzid) {
146                 /* syslog(LOG_DEBUG, "                * Replacing '%s' TZID with 'Z' suffix.\n", tzid); */
147                 TheTime.is_utc = 1;
148         }
149
150         else {
151                 /* Do the conversion. */
152                 if (t != NULL) {
153                         /* syslog(LOG_DEBUG, "                * Timezone prop found.  Converting to UTC.\n"); */
154                 }
155                 else {
156                         /* syslog(LOG_DEBUG, "                * Converting default timezone to UTC.\n"); */
157                 }
158
159                 if (t == NULL) {
160                         t = get_default_icaltimezone();
161                 }
162
163                 icaltimezone_convert_time(&TheTime,
164                                         t,
165                                         icaltimezone_get_utc_timezone()
166                 );
167                 TheTime.is_utc = 1;
168         }
169
170         icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
171         /* syslog(LOG_DEBUG, "                * Now: %s\n", icaltime_as_ical_string(TheTime)); */
172
173         /* Now add the converted property back in. */
174         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
175                 icalproperty_set_dtstart(prop, TheTime);
176         }
177         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
178                 icalproperty_set_dtend(prop, TheTime);
179         }
180         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
181                 icalproperty_set_due(prop, TheTime);
182         }
183         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
184                 icalproperty_set_exdate(prop, TheTime);
185         }
186 }
187
188
189 /*
190  * Recursive portion of ical_dezonify()
191  */
192 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
193         icalcomponent *c;
194         icalproperty *p;
195
196         /*
197          * Recurse through all subcomponents *except* VTIMEZONE ones.
198          */
199         for (c=icalcomponent_get_first_component(
200                                         rcal, ICAL_ANY_COMPONENT);
201                 c != NULL;
202                 c = icalcomponent_get_next_component(
203                                         rcal, ICAL_ANY_COMPONENT)
204         ) {
205                 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
206                         ical_dezonify_recurse(cal, c);
207                 }
208         }
209
210         /*
211          * Now look for DTSTART and DTEND properties
212          */
213         for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
214                 p != NULL;
215                 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
216         ) {
217                 if (
218                         (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
219                         || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
220                         || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
221                         || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
222                    ) {
223                         ical_dezonify_backend(cal, rcal, p);
224                 }
225         }
226 }
227
228
229 /*
230  * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
231  * This function will search any VTIMEZONE subcomponents to learn the
232  * relevant timezone information.
233  */
234 void ical_dezonify(icalcomponent *cal) {
235         icalcomponent *vt = NULL;
236
237         /* syslog(LOG_DEBUG, "ical_dezonify() started\n"); */
238
239         /* Convert all times to UTC */
240         ical_dezonify_recurse(cal, cal);
241
242         /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
243         while (vt = icalcomponent_get_first_component(
244                         cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
245                 icalcomponent_remove_component(cal, vt);
246                 icalcomponent_free(vt);
247         }
248
249         /* syslog(LOG_DEBUG, "ical_dezonify() completed\n"); */
250 }