* do linebuffered/non-blocking reads from http requests
[citadel.git] / webcit / calendar.c
index fadadf6c18ec8710bf01d498d208d99704f53c8a..b966c264bf02c049d32c65d8abdcef751dc1a837 100644 (file)
@@ -9,12 +9,12 @@
 
 
 /*
- * \brief Process a calendar object
- * ...at this point it's already been deserialized by cal_process_attachment()
- * \param cal the calendar object
- * \param recursion_level call stack depth ??????
- * \param msgnum number of the mesage in our db
- * \param cal_partnum of the calendar object ???? 
+ * Process a calendar object.  At this point it's already been deserialized by cal_process_attachment()
+ *
+ * cal:                        the calendar object
+ * recursion_level:    Number of times we've recursed into this function
+ * msgnum:             Message number on the Citadel server
+ * cal_partnum:                MIME part number within that message containing the calendar object
  */
 void cal_process_object(icalcomponent *cal,
                        int recursion_level,
@@ -36,15 +36,15 @@ void cal_process_object(icalcomponent *cal,
 
        sprintf(divname, "rsvp%04x", ++divcount);
 
-       /** Leading HTML for the display of this object */
+       /* Leading HTML for the display of this object */
        if (recursion_level == 0) {
                wprintf("<div class=\"mimepart\">\n");
        }
 
-       /** Look for a method */
+       /* Look for a method */
        method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
 
-       /** See what we need to do with this */
+       /* See what we need to do with this */
        if (method != NULL) {
                the_method = icalproperty_get_method(method);
                char *title;
@@ -91,14 +91,13 @@ void cal_process_object(icalcomponent *cal,
                wprintf("</dd>\n");
        }
 
-       /**
+       /*
         * Only show start/end times if we're actually looking at the VEVENT
         * component.  Otherwise it shows bogus dates for things like timezone.
         */
        if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
 
-               p = icalcomponent_get_first_property(cal,
-                                                    ICAL_DTSTART_PROPERTY);
+               p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
                if (p != NULL) {
                        t = icalproperty_get_dtstart(p);
 
@@ -144,7 +143,7 @@ void cal_process_object(icalcomponent *cal,
                wprintf("</dd>\n");
        }
 
-       /** If the component has attendees, iterate through them. */
+       /* If the component has attendees, iterate through them. */
        for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); 
             (p != NULL); 
             p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
@@ -167,7 +166,7 @@ void cal_process_object(icalcomponent *cal,
                wprintf("</dd>\n");
        }
 
-       /** If the component has subcomponents, recurse through them. */
+       /* If the component has subcomponents, recurse through them. */
        for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
             (c != 0);
             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
@@ -175,7 +174,7 @@ void cal_process_object(icalcomponent *cal,
                cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
        }
 
-       /** If this is a REQUEST, display conflicts and buttons */
+       /* If this is a REQUEST, display conflicts and buttons */
        if (the_method == ICAL_METHOD_REQUEST) {
 
                /* Check for conflicts */
@@ -211,7 +210,7 @@ void cal_process_object(icalcomponent *cal,
 
                wprintf("</dl>");
 
-               /** Display the Accept/Decline buttons */
+               /* Display the Accept/Decline buttons */
                wprintf("<p id=\"%s_question\">"
                        "%s "
                        "&nbsp;&nbsp;&nbsp;<span class=\"button_link\"> "
@@ -230,17 +229,17 @@ void cal_process_object(icalcomponent *cal,
 
        }
 
-       /** If this is a REPLY, display update button */
+       /* If this is a REPLY, display update button */
        if (the_method == ICAL_METHOD_REPLY) {
 
-               /** \todo  In the future, if we want to validate this object before \
+               /* In the future, if we want to validate this object before
                 * continuing, we can do it this way:
                 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
                 serv_getln(buf, sizeof buf);
                 }
                ***********/
 
-               /** Display the update buttons */
+               /* Display the update buttons */
                wprintf("<p id=\"%s_question\" >"
                        "%s "
                        "&nbsp;&nbsp;&nbsp;<span class=\"button_link\"> "
@@ -256,7 +255,7 @@ void cal_process_object(icalcomponent *cal,
        
        }
        
-       /** Trailing HTML for the display of this object */
+       /* Trailing HTML for the display of this object */
        if (recursion_level == 0) {
                wprintf("<p>&nbsp;</p></div>\n");
        }
@@ -387,44 +386,173 @@ void handle_rsvp(void)
 
 /*@{*/
 
+int Flathash(const char *str, long len)
+{
+       if (len != sizeof (int))
+               return 0;
+       else return *(int*)str;
+}
+
 
 
 /**
- * \brief get items, keep them.
- * If we're reading calendar items, just store them for now.  We have to
- * sort and re-output them later when we draw the calendar.
- * \param cal Our calendar to process
- * \param msgnum number of the mesage in our db
+ * \brief clean up ical memory
+ * todo this could get trouble with future ical versions 
+ */
+void delete_cal(void *vCal)
+{
+       disp_cal *Cal = (disp_cal*) vCal;
+       icalcomponent_free(Cal->cal);
+       free(Cal->from);
+       free(Cal);
+}
+
+/*
+ * This is the meat-and-bones of the first part of our two-phase calendar display.
+ * As we encounter calendar items in messages being read from the server, we break out
+ * any iCalendar objects and store them in a hash table.  Later on, the second phase will
+ * use this hash table to render the calendar for display.
  */
 void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unread)
 {
-       struct wcsession *WCC = WC;     /* stack this for faster access (WC is a function) */
-
-       WCC->num_cal += 1;
-       WCC->disp_cal = realloc(WC->disp_cal, (sizeof(struct disp_cal) * WCC->num_cal) );
-       WCC->disp_cal[WCC->num_cal - 1].cal = icalcomponent_new_clone(cal);
-       WCC->disp_cal[WCC->num_cal - 1].unread = unread;
-       WCC->disp_cal[WCC->num_cal - 1].from = malloc (strlen(from) + 1);
-       strcpy (WCC->disp_cal[WCC->num_cal - 1].from, from);
-       ical_dezonify(WCC->disp_cal[WCC->num_cal - 1].cal);
-       WCC->disp_cal[WCC->num_cal - 1].cal_msgnum = msgnum;
+       icalproperty *ps = NULL;
+       struct icaltimetype dtstart, dtend;
+       struct icaldurationtype dur;
+       struct wcsession *WCC = WC;
+       disp_cal *Cal;
+       size_t len;
+       time_t final_recurrence = 0;
+
+       /* recur variables */
+       icalproperty *rrule = NULL;
+       struct icalrecurrencetype recur;
+       icalrecur_iterator *ritr = NULL;
+       struct icaltimetype next;
+       int num_recur = 0;
+
+       dtstart = icaltime_null_time();
+       dtend = icaltime_null_time();
+       
+       if (WCC->disp_cal_items == NULL)
+               WCC->disp_cal_items = NewHash(0, Flathash);
+
+       /* Note: anything we do here, we also have to do below for the recurrences. */
+       Cal = (disp_cal*) malloc(sizeof(disp_cal));
+       memset(Cal, 0, sizeof(disp_cal));
+
+       Cal->cal = icalcomponent_new_clone(cal);
+       Cal->unread = unread;
+       len = strlen(from);
+       Cal->from = (char*)malloc(len+ 1);
+       memcpy(Cal->from, from, len + 1);
+       ical_dezonify(Cal->cal);
+       Cal->cal_msgnum = msgnum;
+
+       /* Precalculate the starting date and time of this event, and store it in our top-level
+        * structure.  Later, when we are rendering the calendar, we can just peek at these values
+        * without having to break apart every calendar item.
+        */
+       ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
+       if (ps != NULL) {
+               dtstart = icalproperty_get_dtstart(ps);
+               Cal->event_start = icaltime_as_timet(dtstart);
+       }
+
+       /* Do the same for the ending date and time.  It makes the day view much easier to render. */
+       ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
+       if (ps != NULL) {
+               dtend = icalproperty_get_dtstart(ps);
+               Cal->event_end = icaltime_as_timet(dtend);
+       }
+
+       /* Store it in the hash list. */
+       Put(WCC->disp_cal_items, 
+           (char*) &Cal->event_start,
+           sizeof(Cal->event_start), 
+           Cal, 
+           delete_cal);
+
+       /* handle recurring events */
+
+       if (icaltime_is_null_time(dtstart)) return;     /* Can't recur without a start time */
+
+       if (!icaltime_is_null_time(dtend)) {            /* Need duration for recurrences */
+               dur = icaltime_subtract(dtend, dtstart);
+       }
+
+       /*
+        * Just let libical iterate the recurrence, and keep looping back to the top of this function,
+        * adding new hash entries that all point back to the same msgnum, until either the iteration
+        * stops or some outer bound is reached.  The display code will automatically do the Right Thing.
+        */
+
+       rrule = icalcomponent_get_first_property(Cal->cal, ICAL_RRULE_PROPERTY);
+       if (!rrule) return;
+       recur = icalproperty_get_rrule(rrule);
+       ritr = icalrecur_iterator_new(recur, dtstart);
+       if (!ritr) return;
+
+       while (next = icalrecur_iterator_next(ritr), !icaltime_is_null_time(next) ) {
+               ++num_recur;
+
+               if (num_recur > 1) {            /* Skip the first one.  We already did it at the root. */
+
+                       /* Note: anything we do here, we also have to do above for the root event. */
+                       Cal = (disp_cal*) malloc(sizeof(disp_cal));
+                       memset(Cal, 0, sizeof(disp_cal));
+               
+                       Cal->cal = icalcomponent_new_clone(cal);
+                       Cal->unread = unread;
+                       len = strlen(from);
+                       Cal->from = (char*)malloc(len+ 1);
+                       memcpy(Cal->from, from, len + 1);
+                       ical_dezonify(Cal->cal);
+                       Cal->cal_msgnum = msgnum;
+       
+                       ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
+                       if (ps != NULL) {
+                               icalcomponent_remove_property(Cal->cal, ps);
+                               ps = icalproperty_new_dtstart(next);
+                               icalcomponent_add_property(Cal->cal, ps);
+                               Cal->event_start = icaltime_as_timet(next);
+                               final_recurrence = Cal->event_start;
+                       }
+       
+                       ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
+                       if (ps != NULL) {
+                               icalcomponent_remove_property(Cal->cal, ps);
+
+                               /* Make a new dtend */
+                               ps = icalproperty_new_dtend(icaltime_add(next, dur));
+       
+                               /* and stick it somewhere */
+                               icalcomponent_add_property(Cal->cal, ps);
+                       }
+       
+                       Put(WCC->disp_cal_items, 
+                               (char*) &Cal->event_start,
+                               sizeof(Cal->event_start), 
+                               Cal, 
+                               delete_cal);
+               }
+       }
+       lprintf(9, "Performed %d recurrences; final one is %s", num_recur, ctime(&final_recurrence));
+
 }
 
 
 
-/**
- * \brief edit a task
+/*
  * Display a task by itself (for editing)
- * \param supplied_vtodo the todo item we want to edit
- * \param msgnum number of the mesage in our db
  */
 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from, int unread) 
 {
        icalcomponent *vtodo;
        icalproperty *p;
-       struct icaltimetype t;
+       struct icaltimetype IcalTime;
        time_t now;
        int created_new_vtodo = 0;
+       icalproperty_status todoStatus;
 
        now = time(NULL);
 
@@ -472,7 +600,7 @@ void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, ch
        wprintf("<div class=\"boxcontent\">\n");
        wprintf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
        wprintf("<div style=\"display: none;\">\n       ");
-       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
+       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
        wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
                msgnum);
        wprintf("</div>");
@@ -505,9 +633,11 @@ void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, ch
        wprintf(_("or"));
        wprintf(" ");
        if (p != NULL) {
-               t = icalproperty_get_dtstart(p);
+               IcalTime = icalproperty_get_dtstart(p);
        }
-       display_icaltimetype_as_webform(&t, "dtstart");
+       else
+               IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
+       display_icaltimetype_as_webform(&IcalTime, "dtstart");
        wprintf("</TD></TR>\n");
 
        wprintf("<TR><TD>");
@@ -524,16 +654,18 @@ void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, ch
        wprintf(_("or"));
        wprintf(" ");
        if (p != NULL) {
-               t = icalproperty_get_due(p);
+               IcalTime = icalproperty_get_due(p);
        }
-       display_icaltimetype_as_webform(&t, "due");
+       else
+               IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
+       display_icaltimetype_as_webform(&IcalTime, "due");
                
        wprintf("</TD></TR>\n");
-       icalproperty_status todoStatus = icalcomponent_get_status(vtodo);
+       todoStatus = icalcomponent_get_status(vtodo);
        wprintf("<TR><TD>\n");
        wprintf(_("Completed:"));
        wprintf("</TD><TD>");
-       wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"STATUS\" VALUE=\"COMPLETED\"");
+       wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
        if (todoStatus == ICAL_STATUS_COMPLETED) {
                wprintf(" CHECKED=\"CHECKED\"");
        } 
@@ -648,7 +780,7 @@ void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from
                        icalcomponent_remove_property(vtodo, prop);
                        icalproperty_free(prop);
                }
-               if (!IsEmptyStr(bstr("description"))) {
+               if (havebstr("description")) {
                        icalcomponent_add_property(vtodo,
                                                   icalproperty_new_description(bstr("description")));
                }
@@ -669,7 +801,7 @@ void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from
                        icalcomponent_remove_property(vtodo,prop);
                        icalproperty_free(prop);
                }
-               if (!IsEmptyStr(bstr("status"))) {
+               if (havebstr("status")) {
                        icalproperty_status taskStatus = icalproperty_string_to_status(
                                bstr("status"));
                        icalcomponent_set_status(vtodo, taskStatus);
@@ -771,17 +903,11 @@ void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from
 
 
 
-/**
- * \brief generic item handler
- * Code common to all display handlers.  Given a message number and a MIME
+/*
+ * Code common to all icalendar display handlers.  Given a message number and a MIME
  * type, we load the message and hunt for that MIME type.  If found, we load
  * the relevant part, deserialize it into a libical component, filter it for
  * the requested object type, and feed it to the specified handler.
- * \param mimetype mimetyp of our object
- * \param which_kind sort of ical type
- * \param msgnum number of the mesage in our db
- * \param callback a funcion \todo
- *
  */
 void display_using_handler(long msgnum, int unread,
                           icalcomponent_kind which_kind,
@@ -834,17 +960,15 @@ void display_using_handler(long msgnum, int unread,
 
                                ical_dezonify(cal);
 
-                               /** Simple components of desired type */
+                               /* Simple components of desired type */
                                if (icalcomponent_isa(cal) == which_kind) {
                                        callback(cal, msgnum, from, unread);
                                }
 
-                               /** Subcomponents of desired type */
-                               for (c = icalcomponent_get_first_component(cal,
-                                                                          which_kind);
+                               /* Subcomponents of desired type */
+                               for (c = icalcomponent_get_first_component(cal, which_kind);
                                     (c != 0);
-                                    c = icalcomponent_get_next_component(cal,
-                                                                         which_kind)) {
+                                    c = icalcomponent_get_next_component(cal, which_kind)) {
                                        callback(c, msgnum, from, unread);
                                }
                                icalcomponent_free(cal);
@@ -855,69 +979,61 @@ void display_using_handler(long msgnum, int unread,
        icalmemory_free_ring();
 }
 
-/**
- * \brief display whole calendar
- * \param msgnum number of the mesage in our db
+/*
+ * Display a calendar item
  */
 void display_calendar(long msgnum, int unread) {
-       display_using_handler(msgnum, unread,
-                             ICAL_VEVENT_COMPONENT,
-                             display_individual_cal);
+       display_using_handler(msgnum, unread, ICAL_VEVENT_COMPONENT, display_individual_cal);
 }
 
-/**
- * \brief display whole taksview
- * \param msgnum number of the mesage in our db
+/*
+ * Display task view
  */
 void display_task(long msgnum, int unread) {
-       display_using_handler(msgnum, unread,
-                             ICAL_VTODO_COMPONENT,
-                             display_individual_cal);
+       display_using_handler(msgnum, unread, ICAL_VTODO_COMPONENT, display_individual_cal);
 }
 
-/**
- * \brief display the editor component for a task
+/*
+ * Display the editor component for a task
  */
 void display_edit_task(void) {
        long msgnum = 0L;
                        
-       /** Force change the room if we have to */
+       /* Force change the room if we have to */
        if (havebstr("taskrm")) {
                gotoroom((char *)bstr("taskrm"));
        }
 
        msgnum = lbstr("msgnum");
        if (msgnum > 0L) {
-               /** existing task */
+               /* existing task */
                display_using_handler(msgnum, 0,
                                      ICAL_VTODO_COMPONENT,
                                      display_edit_individual_task);
        }
        else {
-               /** new task */
+               /* new task */
                display_edit_individual_task(NULL, 0L, "", 0);
        }
 }
 
-/**
- *\brief save an edited task
+/*
+ * save an edited task
  */
 void save_task(void) {
        long msgnum = 0L;
 
        msgnum = lbstr("msgnum");
        if (msgnum > 0L) {
-               display_using_handler(msgnum, 0,
-                                     ICAL_VTODO_COMPONENT,
-                                     save_individual_task);
+               display_using_handler(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task);
        }
        else {
                save_individual_task(NULL, 0L, "", 0);
        }
 }
 
-/**
- * \brief display the editor component for an event
+/*
+ * display the editor component for an event
  */
 void display_edit_event(void) {
        long msgnum = 0L;
@@ -925,9 +1041,7 @@ void display_edit_event(void) {
        msgnum = lbstr("msgnum");
        if (msgnum > 0L) {
                /* existing event */
-               display_using_handler(msgnum, 0,
-                                     ICAL_VEVENT_COMPONENT,
-                                     display_edit_individual_event);
+               display_using_handler(msgnum, 0, ICAL_VEVENT_COMPONENT, display_edit_individual_event);
        }
        else {
                /* new event */
@@ -935,8 +1049,8 @@ void display_edit_event(void) {
        }
 }
 
-/**
- * \brief save an edited event
+/*
+ * save an edited event
  */
 void save_event(void) {
        long msgnum = 0L;
@@ -944,9 +1058,7 @@ void save_event(void) {
        msgnum = lbstr("msgnum");
 
        if (msgnum > 0L) {
-               display_using_handler(msgnum, 0,
-                                     ICAL_VEVENT_COMPONENT,
-                                     save_individual_event);
+               display_using_handler(msgnum, 0, ICAL_VEVENT_COMPONENT, save_individual_event);
        }
        else {
                save_individual_event(NULL, 0L, "", 0);
@@ -957,15 +1069,14 @@ void save_event(void) {
 
 
 
-/**
- * \brief freebusy display (for client software)
- * \param req dunno. ?????
+/*
+ * Anonymous request of freebusy data for a user
  */
-void do_freebusy(char *req) {
+void do_freebusy(const char *req) {
        char who[SIZ];
        char buf[SIZ];
-       char *fb;
        int len;
+       long lines;
 
        extract_token(who, req, 1, ' ', sizeof who);
        if (!strncasecmp(who, "/freebusy/", 10)) {
@@ -985,16 +1096,30 @@ void do_freebusy(char *req) {
        serv_getln(buf, sizeof buf);
 
        if (buf[0] != '1') {
-               wprintf("HTTP/1.1 404 %s\n", &buf[4]);
+               hprintf("HTTP/1.1 404 %s\n", &buf[4]);
                output_headers(0, 0, 0, 0, 0, 0);
-               wprintf("Content-Type: text/plain\r\n");
-               wprintf("\r\n");
+               hprintf("Content-Type: text/plain\r\n");
                wprintf("%s\n", &buf[4]);
+               end_burst();
                return;
        }
 
-       fb = read_server_text();
-       http_transmit_thing(fb, strlen(fb), "text/calendar", 0);
-       free(fb);
+       read_server_text(WC->WBuf, &lines);
+       http_transmit_thing("text/calendar", 0);
 }
 
+
+
+
+
+void 
+InitModule_CALENDAR
+(void)
+{
+       WebcitAddUrlHandler(HKEY("display_edit_task"), display_edit_task, 0);
+       WebcitAddUrlHandler(HKEY("save_task"), save_task, 0);
+       WebcitAddUrlHandler(HKEY("display_edit_event"), display_edit_event, 0);
+       WebcitAddUrlHandler(HKEY("save_event"), save_event, 0);
+       WebcitAddUrlHandler(HKEY("respond_to_request"), respond_to_request, 0);
+       WebcitAddUrlHandler(HKEY("handle_rsvp"), handle_rsvp, 0);
+}