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