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