indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / ical_dezonify.c
1
2 /* 
3  * Function to go through an ical component set and convert all non-UTC
4  * date/time properties to UTC.  It also strips out any VTIMEZONE
5  * subcomponents afterwards, because they're irrelevant.
6  *
7  * Everything here will work on both a fully encapsulated VCALENDAR component
8  * or any type of subcomponent.
9  */
10
11 #include "webcit.h"
12 #include "webserver.h"
13
14 /*
15  * Figure out which time zone needs to be used for timestamps that are
16  * not UTC and do not have a time zone specified.
17  *
18  */
19 icaltimezone *get_default_icaltimezone(void) {
20
21         icaltimezone *zone = NULL;
22         const char *default_zone_name = ChrPtr(WC->serv_info->serv_default_cal_zone);
23
24         if (!zone) {
25                 zone = icaltimezone_get_builtin_timezone(default_zone_name);
26         }
27         if (!zone) {
28                 syslog(LOG_WARNING, "Unable to load '%s' time zone.  Defaulting to UTC.\n", default_zone_name);
29                 zone = icaltimezone_get_utc_timezone();
30         }
31         if (!zone) {
32                 syslog(LOG_ERR, "Unable to load UTC time zone!\n");
33         }
34         return zone;
35 }
36
37
38 /*
39  * Back end function for ical_dezonify()
40  *
41  * We supply this with the master component, the relevant component,
42  * and the property (which will be a DTSTART, DTEND, etc.)
43  * which we want to convert to UTC.
44  */
45 void ical_dezonify_backend(icalcomponent * cal, icalcomponent * rcal, icalproperty * prop) {
46
47         icaltimezone *t = NULL;
48         icalparameter *param;
49         const char *tzid = NULL;
50         struct icaltimetype TheTime;
51         int utc_declared_as_tzid = 0;   /* Component declared 'TZID=GMT' instead of using Z syntax */
52
53         /* Give me nothing and I will give you nothing in return. */
54         if (cal == NULL)
55                 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(rcal, ICAL_ANY_COMPONENT);
184              c != NULL; c = icalcomponent_get_next_component(rcal, ICAL_ANY_COMPONENT)
185             ) {
186                 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
187                         ical_dezonify_recurse(cal, c);
188                 }
189         }
190
191         /*
192          * Now look for DTSTART and DTEND properties
193          */
194         for (p = icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
195              p != NULL; p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
196             ) {
197                 if ((icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
198                     || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
199                     || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
200                     || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
201                     ) {
202                         ical_dezonify_backend(cal, rcal, p);
203                 }
204         }
205 }
206
207
208 /*
209  * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
210  * This function will search any VTIMEZONE subcomponents to learn the
211  * relevant timezone information.
212  */
213 void ical_dezonify(icalcomponent * cal) {
214         icalcomponent *vt = NULL;
215
216 #ifdef DBG_ICAL
217         syslog(LOG_DEBUG, "ical_dezonify() started\n");
218 #endif
219
220         /* Convert all times to UTC */
221         ical_dezonify_recurse(cal, cal);
222
223         /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
224         while (vt = icalcomponent_get_first_component(cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
225                 icalcomponent_remove_component(cal, vt);
226                 icalcomponent_free(vt);
227         }
228
229 #ifdef DBG_ICAL
230         syslog(LOG_DEBUG, "ical_dezonify() completed\n");
231 #endif
232 }