Back to index

webcit  8.12-dfsg
ical_dezonify.c
Go to the documentation of this file.
00001 /* 
00002  * Function to go through an ical component set and convert all non-UTC
00003  * date/time properties to UTC.  It also strips out any VTIMEZONE
00004  * subcomponents afterwards, because they're irrelevant.
00005  *
00006  * Everything here will work on both a fully encapsulated VCALENDAR component
00007  * or any type of subcomponent.
00008  */
00009 
00010 #include "webcit.h"
00011 #include "webserver.h"
00012 
00013 /*
00014  * Figure out which time zone needs to be used for timestamps that are
00015  * not UTC and do not have a time zone specified.
00016  *
00017  */
00018 icaltimezone *get_default_icaltimezone(void) {
00019 
00020         icaltimezone *zone = NULL;
00021        const char *default_zone_name = ChrPtr(WC->serv_info->serv_default_cal_zone);
00022 
00023         if (!zone) {
00024                 zone = icaltimezone_get_builtin_timezone(default_zone_name);
00025         }
00026         if (!zone) {
00027               syslog(1, "Unable to load '%s' time zone.  Defaulting to UTC.\n", default_zone_name);
00028                 zone = icaltimezone_get_utc_timezone();
00029        }
00030        if (!zone) {
00031               syslog(1, "Unable to load UTC time zone!\n");
00032        }
00033         return zone;
00034 }
00035 
00036 
00037 /*
00038  * Back end function for ical_dezonify()
00039  *
00040  * We supply this with the master component, the relevant component,
00041  * and the property (which will be a DTSTART, DTEND, etc.)
00042  * which we want to convert to UTC.
00043  */
00044 void ical_dezonify_backend(icalcomponent *cal,
00045                      icalcomponent *rcal,
00046                      icalproperty *prop) {
00047 
00048        icaltimezone *t = NULL;
00049        icalparameter *param;
00050        const char *tzid = NULL;
00051        struct icaltimetype TheTime;
00052        int utc_declared_as_tzid = 0;      /* Component declared 'TZID=GMT' instead of using Z syntax */
00053 
00054        /* Give me nothing and I will give you nothing in return. */
00055        if (cal == NULL) return;
00056 
00057        /* Hunt for a TZID parameter in this property. */
00058        param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
00059 
00060        /* Get the stringish name of this TZID. */
00061        if (param != NULL) {
00062               tzid = icalparameter_get_tzid(param);
00063 
00064               /* Convert it to an icaltimezone type. */
00065               if (tzid != NULL) {
00066 #ifdef DBG_ICAL
00067                      syslog(9, "                * Stringy supplied timezone is: '%s'\n", tzid);
00068 #endif
00069                      if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
00070                             utc_declared_as_tzid = 1;
00071 #ifdef DBG_ICAL
00072                             syslog(9, "                * ...and we handle that internally.\n");
00073 #endif
00074                      }
00075                      else {
00076                             /* try attached first */
00077                             t = icalcomponent_get_timezone(cal, tzid);
00078 #ifdef DBG_ICAL
00079                             syslog(9, "                * ...and I %s have tzdata for that zone.\n",
00080                                    (t ? "DO" : "DO NOT")
00081                             );
00082 #endif
00083                             /* then try built-in timezones */
00084                             if (!t) {
00085                                    t = icaltimezone_get_builtin_timezone(tzid);
00086 #ifdef DBG_ICAL
00087                                    if (t) {
00088                                           syslog(9, "                * Using system tzdata!\n");
00089                                    }
00090 #endif
00091                             }
00092                      }
00093               }
00094 
00095        }
00096 
00097        /* Now we know the timezone.  Convert to UTC. */
00098 
00099        if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
00100               TheTime = icalproperty_get_dtstart(prop);
00101        }
00102        else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
00103               TheTime = icalproperty_get_dtend(prop);
00104        }
00105        else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
00106               TheTime = icalproperty_get_due(prop);
00107        }
00108        else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
00109               TheTime = icalproperty_get_exdate(prop);
00110        }
00111        else {
00112               return;
00113        }
00114 
00115 #ifdef DBG_ICAL
00116        syslog(9, "                * Was: %s\n", icaltime_as_ical_string(TheTime));
00117 #endif
00118 
00119        if (TheTime.is_utc) {
00120 #ifdef DBG_ICAL
00121               syslog(9, "                * This property is ALREADY UTC.\n");
00122 #endif
00123        }
00124 
00125        else if (utc_declared_as_tzid) {
00126 #ifdef DBG_ICAL
00127               syslog(9, "                * Replacing '%s' TZID with 'Z' suffix.\n", tzid);
00128 #endif
00129               TheTime.is_utc = 1;
00130        }
00131 
00132        else {
00133               /* Do the conversion. */
00134               if (t != NULL) {
00135 #ifdef DBG_ICAL
00136                      syslog(9, "                * Timezone prop found.  Converting to UTC.\n");
00137 #endif
00138               }
00139               else {
00140 #ifdef DBG_ICAL
00141                      syslog(9, "                * Converting default timezone to UTC.\n");
00142 #endif
00143               }
00144 
00145               if (t == NULL) {
00146                      t = get_default_icaltimezone();
00147               }
00148               icaltimezone_convert_time(&TheTime, t, icaltimezone_get_utc_timezone());
00149               TheTime.is_utc = 1;
00150        }
00151 
00152        icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
00153 #ifdef DBG_ICAL
00154        syslog(9, "                * Now: %s\n", icaltime_as_ical_string(TheTime));
00155 #endif
00156 
00157        /* Now add the converted property back in. */
00158        if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
00159               icalproperty_set_dtstart(prop, TheTime);
00160        }
00161        else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
00162               icalproperty_set_dtend(prop, TheTime);
00163        }
00164        else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
00165               icalproperty_set_due(prop, TheTime);
00166        }
00167        else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
00168               icalproperty_set_exdate(prop, TheTime);
00169        }
00170 }
00171 
00172 
00173 /*
00174  * Recursive portion of ical_dezonify()
00175  */
00176 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
00177        icalcomponent *c;
00178        icalproperty *p;
00179 
00180        /*
00181         * Recurse through all subcomponents *except* VTIMEZONE ones.
00182         */
00183        for (c=icalcomponent_get_first_component(
00184                                    rcal, ICAL_ANY_COMPONENT);
00185               c != NULL;
00186               c = icalcomponent_get_next_component(
00187                                    rcal, ICAL_ANY_COMPONENT)
00188        ) {
00189               if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
00190                      ical_dezonify_recurse(cal, c);
00191               }
00192        }
00193 
00194        /*
00195         * Now look for DTSTART and DTEND properties
00196         */
00197        for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
00198               p != NULL;
00199               p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
00200        ) {
00201               if (
00202                      (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
00203                      || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
00204                      || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
00205                      || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
00206                  ) {
00207                      ical_dezonify_backend(cal, rcal, p);
00208               }
00209        }
00210 }
00211 
00212 
00213 /*
00214  * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
00215  * This function will search any VTIMEZONE subcomponents to learn the
00216  * relevant timezone information.
00217  */
00218 void ical_dezonify(icalcomponent *cal) {
00219        icalcomponent *vt = NULL;
00220 
00221 #ifdef DBG_ICAL
00222        syslog(9, "ical_dezonify() started\n");
00223 #endif
00224 
00225        /* Convert all times to UTC */
00226        ical_dezonify_recurse(cal, cal);
00227 
00228        /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
00229        while (vt = icalcomponent_get_first_component(
00230                      cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
00231               icalcomponent_remove_component(cal, vt);
00232               icalcomponent_free(vt);
00233        }
00234 
00235 #ifdef DBG_ICAL
00236        syslog(9, "ical_dezonify() completed\n");
00237 #endif
00238 }
00239