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