4 * Functions which handle calendar objects and their processing/display.
12 * Process a calendar object. At this point it's already been deserialized by cal_process_attachment()
14 * cal: the calendar object
15 * recursion_level: Number of times we've recursed into this function
16 * msgnum: Message number on the Citadel server
17 * cal_partnum: MIME part number within that message containing the calendar object
19 void cal_process_object(icalcomponent *cal,
25 icalproperty *method = NULL;
26 icalproperty_method the_method = ICAL_METHOD_NONE;
28 struct icaltimetype t;
31 char conflict_name[256];
32 char conflict_message[256];
35 static int divcount = 0;
37 sprintf(divname, "rsvp%04x", ++divcount);
39 /* Leading HTML for the display of this object */
40 if (recursion_level == 0) {
41 wprintf("<div class=\"mimepart\">\n");
44 /* Look for a method */
45 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
47 /* See what we need to do with this */
49 the_method = icalproperty_get_method(method);
52 wprintf("<div id=\"%s_title\">", divname);
53 wprintf("<img src=\"static/calarea_48x.gif\">");
56 case ICAL_METHOD_REQUEST:
57 title = _("Meeting invitation");
59 case ICAL_METHOD_REPLY:
60 title = _("Attendee's reply to your invitation");
62 case ICAL_METHOD_PUBLISH:
63 title = _("Published event");
66 title = _("This is an unknown type of calendar item.");
71 wprintf(" %s",title);
76 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
79 wprintf(_("Summary:"));
81 escputs((char *)icalproperty_get_comment(p));
85 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
88 wprintf(_("Location:"));
90 escputs((char *)icalproperty_get_comment(p));
95 * Only show start/end times if we're actually looking at the VEVENT
96 * component. Otherwise it shows bogus dates for things like timezone.
98 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
100 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
102 t = icalproperty_get_dtstart(p);
107 memset(&d_tm, 0, sizeof d_tm);
108 d_tm.tm_year = t.year - 1900;
109 d_tm.tm_mon = t.month - 1;
110 d_tm.tm_mday = t.day;
111 wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
114 wprintf("</dt><dd>%s</dd>", d_str);
117 tt = icaltime_as_timet(t);
118 webcit_fmt_date(buf, tt, 0);
120 wprintf(_("Starting date/time:"));
121 wprintf("</dt><dd>%s</dd>", buf);
125 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
127 t = icalproperty_get_dtend(p);
128 tt = icaltime_as_timet(t);
129 webcit_fmt_date(buf, tt, 0);
131 wprintf(_("Ending date/time:"));
132 wprintf("</dt><dd>%s</dd>", buf);
137 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
140 wprintf(_("Description:"));
141 wprintf("</dt><dd>");
142 escputs((char *)icalproperty_get_comment(p));
146 /* If the component has attendees, iterate through them. */
147 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY);
149 p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
151 wprintf(_("Attendee:"));
152 wprintf("</dt><dd>");
153 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
154 if (!strncasecmp(buf, "MAILTO:", 7)) {
156 /** screen name or email address */
157 strcpy(buf, &buf[7]);
162 /** participant status */
163 partstat_as_string(buf, p);
169 /* If the component has subcomponents, recurse through them. */
170 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
172 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
173 /* Recursively process subcomponent */
174 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
177 /* If this is a REQUEST, display conflicts and buttons */
178 if (the_method == ICAL_METHOD_REQUEST) {
180 /* Check for conflicts */
181 lprintf(9, "Checking server calendar for conflicts...\n");
182 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
183 serv_getln(buf, sizeof buf);
185 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
186 extract_token(conflict_name, buf, 3, '|', sizeof conflict_name);
187 is_update = extract_int(buf, 4);
190 snprintf(conflict_message, sizeof conflict_message,
191 _("This is an update of '%s' which is already in your calendar."), conflict_name);
194 snprintf(conflict_message, sizeof conflict_message,
195 _("This event would conflict with '%s' which is already in your calendar."), conflict_name);
204 wprintf("</dt><dd>");
205 escputs(conflict_message);
209 lprintf(9, "...done.\n");
213 /* Display the Accept/Decline buttons */
214 wprintf("<p id=\"%s_question\">"
216 " <span class=\"button_link\"> "
217 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Accept');\">%s</a>"
218 "</span> <span class=\"button_link\">"
219 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Tentative');\">%s</a>"
220 "</span> <span class=\"button_link\">"
221 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Decline');\">%s</a>"
224 _("How would you like to respond to this invitation?"),
225 divname, divname, msgnum, cal_partnum, _("Accept"),
226 divname, divname, msgnum, cal_partnum, _("Tentative"),
227 divname, divname, msgnum, cal_partnum, _("Decline")
232 /* If this is a REPLY, display update button */
233 if (the_method == ICAL_METHOD_REPLY) {
235 /* In the future, if we want to validate this object before
236 * continuing, we can do it this way:
237 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
238 serv_getln(buf, sizeof buf);
242 /* Display the update buttons */
243 wprintf("<p id=\"%s_question\" >"
245 " <span class=\"button_link\"> "
246 "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Update');\">%s</a>"
247 "</span> <span class=\"button_link\">"
248 "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Ignore');\">%s</a>"
251 _("Click <i>Update</i> to accept this reply and update your calendar."),
252 divname, divname, msgnum, cal_partnum, _("Update"),
253 divname, divname, msgnum, cal_partnum, _("Ignore")
258 /* Trailing HTML for the display of this object */
259 if (recursion_level == 0) {
260 wprintf("<p> </p></div>\n");
266 * \brief process calendar mail atachment
267 * Deserialize a calendar object in a message so it can be processed.
268 * (This is the main entry point for these things)
269 * \param part_source the part of the message we want to parse
270 * \param msgnum number of the mesage in our db
271 * \param cal_partnum the number of the calendar item
273 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum)
277 cal = icalcomponent_new_from_string(part_source);
280 wprintf(_("There was an error parsing this calendar item."));
286 cal_process_object(cal, 0, msgnum, cal_partnum);
288 /* Free the memory we obtained from libical's constructor */
289 icalcomponent_free(cal);
296 * \brief accept/decline meeting
297 * Respond to a meeting request
299 void respond_to_request(void)
303 begin_ajax_response();
305 serv_printf("ICAL respond|%s|%s|%s|",
310 serv_getln(buf, sizeof buf);
313 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
314 if (!strcasecmp(bstr("sc"), "accept")) {
315 wprintf(_("You have accepted this meeting invitation. "
316 "It has been entered into your calendar.")
318 } else if (!strcasecmp(bstr("sc"), "tentative")) {
319 wprintf(_("You have tentatively accepted this meeting invitation. "
320 "It has been 'pencilled in' to your calendar.")
322 } else if (!strcasecmp(bstr("sc"), "decline")) {
323 wprintf(_("You have declined this meeting invitation. "
324 "It has <b>not</b> been entered into your calendar.")
328 wprintf(_("A reply has been sent to the meeting organizer."));
331 wprintf("<img align=\"center\" src=\"static/error.gif\"><span>");
332 wprintf("%s\n", &buf[4]);
342 * \brief Handle an incoming RSVP
344 void handle_rsvp(void)
348 begin_ajax_response();
350 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
355 serv_getln(buf, sizeof buf);
358 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
359 if (!strcasecmp(bstr("sc"), "update")) {
360 wprintf(_("Your calendar has been updated to reflect this RSVP."));
361 } else if (!strcasecmp(bstr("sc"), "ignore")) {
362 wprintf(_("You have chosen to ignore this RSVP. "
363 "Your calendar has <b>not</b> been updated.")
368 wprintf("<img src=\"static/error.gif\"><span> %s\n", &buf[4]);
378 /*-----------------------------------------------------------------------**/
383 * \defgroup MsgDisplayHandlers Display handlers for message reading
384 * \ingroup Calendaring
389 int Flathash(const char *str, long len)
391 if (len != sizeof (int))
393 else return *(int*)str;
399 * \brief clean up ical memory
400 * todo this could get trouble with future ical versions
402 void delete_cal(void *vCal)
404 disp_cal *Cal = (disp_cal*) vCal;
405 icalcomponent_free(Cal->cal);
411 * This is the meat-and-bones of the first part of our two-phase calendar display.
412 * As we encounter calendar items in messages being read from the server, we break out
413 * any iCalendar objects and store them in a hash table. Later on, the second phase will
414 * use this hash table to render the calendar for display.
416 void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unread, struct calview *calv)
418 icalproperty *ps = NULL;
419 struct icaltimetype dtstart, dtend;
420 struct icaldurationtype dur;
421 struct wcsession *WCC = WC;
424 time_t final_recurrence = 0;
426 /* recur variables */
427 icalproperty *rrule = NULL;
428 struct icalrecurrencetype recur;
429 icalrecur_iterator *ritr = NULL;
430 struct icaltimetype next;
433 dtstart = icaltime_null_time();
434 dtend = icaltime_null_time();
436 if (WCC->disp_cal_items == NULL)
437 WCC->disp_cal_items = NewHash(0, Flathash);
439 /* Note: anything we do here, we also have to do below for the recurrences. */
440 Cal = (disp_cal*) malloc(sizeof(disp_cal));
441 memset(Cal, 0, sizeof(disp_cal));
443 Cal->cal = icalcomponent_new_clone(cal);
444 Cal->unread = unread;
446 Cal->from = (char*)malloc(len+ 1);
447 memcpy(Cal->from, from, len + 1);
448 ical_dezonify(Cal->cal);
449 Cal->cal_msgnum = msgnum;
451 /* Precalculate the starting date and time of this event, and store it in our top-level
452 * structure. Later, when we are rendering the calendar, we can just peek at these values
453 * without having to break apart every calendar item.
455 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
457 dtstart = icalproperty_get_dtstart(ps);
458 Cal->event_start = icaltime_as_timet(dtstart);
461 /* Do the same for the ending date and time. It makes the day view much easier to render. */
462 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
464 dtend = icalproperty_get_dtstart(ps);
465 Cal->event_end = icaltime_as_timet(dtend);
468 /* Store it in the hash list. */
469 Put(WCC->disp_cal_items,
470 (char*) &Cal->event_start,
471 sizeof(Cal->event_start),
475 /* handle recurring events */
477 if (icaltime_is_null_time(dtstart)) return; /* Can't recur without a start time */
479 if (!icaltime_is_null_time(dtend)) { /* Need duration for recurrences */
480 dur = icaltime_subtract(dtend, dtstart);
484 * Just let libical iterate the recurrence, and keep looping back to the top of this function,
485 * adding new hash entries that all point back to the same msgnum, until either the iteration
486 * stops or some outer bound is reached. The display code will automatically do the Right Thing.
489 rrule = icalcomponent_get_first_property(Cal->cal, ICAL_RRULE_PROPERTY);
491 recur = icalproperty_get_rrule(rrule);
492 ritr = icalrecur_iterator_new(recur, dtstart);
496 while (next = icalrecur_iterator_next(ritr), ((!icaltime_is_null_time(next))&&(!stop_rr)) ) {
499 if (num_recur > 1) { /* Skip the first one. We already did it at the root. */
501 /* Note: anything we do here, we also have to do above for the root event. */
502 Cal = (disp_cal*) malloc(sizeof(disp_cal));
503 memset(Cal, 0, sizeof(disp_cal));
505 Cal->cal = icalcomponent_new_clone(cal);
506 Cal->unread = unread;
508 Cal->from = (char*)malloc(len+ 1);
509 memcpy(Cal->from, from, len + 1);
510 ical_dezonify(Cal->cal);
511 Cal->cal_msgnum = msgnum;
513 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
515 icalcomponent_remove_property(Cal->cal, ps);
516 ps = icalproperty_new_dtstart(next);
517 icalcomponent_add_property(Cal->cal, ps);
518 Cal->event_start = icaltime_as_timet(next);
519 final_recurrence = Cal->event_start;
522 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
524 icalcomponent_remove_property(Cal->cal, ps);
526 /* Make a new dtend */
527 ps = icalproperty_new_dtend(icaltime_add(next, dur));
529 /* and stick it somewhere */
530 icalcomponent_add_property(Cal->cal, ps);
533 if ( (Cal->event_start > calv->lower_bound)
534 && (Cal->event_start < calv->upper_bound) ) {
535 Put(WCC->disp_cal_items,
536 (char*) &Cal->event_start,
537 sizeof(Cal->event_start),
546 /* If an upper bound is set, stop when we go out of scope */
547 if (final_recurrence > calv->upper_bound) stop_rr = 1;
550 /* lprintf(9, "Performed %d recurrences; final one is %s", num_recur, ctime(&final_recurrence)); */
557 * Display a task by itself (for editing)
559 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from,
560 int unread, struct calview *calv)
562 icalcomponent *vtodo;
564 struct icaltimetype IcalTime;
566 int created_new_vtodo = 0;
567 icalproperty_status todoStatus;
571 if (supplied_vtodo != NULL) {
572 vtodo = supplied_vtodo;
575 * If we're looking at a fully encapsulated VCALENDAR
576 * rather than a VTODO component, attempt to use the first
577 * relevant VTODO subcomponent. If there is none, the
578 * NULL returned by icalcomponent_get_first_component() will
579 * tell the next iteration of this function to create a
582 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
583 display_edit_individual_task(
584 icalcomponent_get_first_component(
585 vtodo, ICAL_VTODO_COMPONENT
587 msgnum, from, unread, calv
593 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
594 created_new_vtodo = 1;
597 // TODO: Can we take all this and move it into a template?
598 output_headers(1, 1, 1, 0, 0, 0);
599 wprintf("<!-- start task edit form -->");
600 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
601 // Get summary early for title
602 wprintf("<div class=\"box\">\n");
603 wprintf("<div class=\"boxlabel\">");
604 wprintf(_("Edit task"));
607 escputs((char *)icalproperty_get_comment(p));
611 wprintf("<div class=\"boxcontent\">\n");
612 wprintf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
613 wprintf("<div style=\"display: none;\">\n ");
614 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
615 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
618 wprintf("<table class=\"calendar_background\"><tr><td>");
619 wprintf("<TABLE STYLE=\"border: none;\">\n");
622 wprintf(_("Summary:"));
624 "<INPUT TYPE=\"text\" NAME=\"summary\" "
625 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
626 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
628 escputs((char *)icalproperty_get_comment(p));
630 wprintf("\"></TD></TR>\n");
633 wprintf(_("Start date:"));
634 wprintf("</TD><TD>");
635 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
636 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
638 wprintf("CHECKED=\"CHECKED\"");
641 wprintf(_("No date"));
647 IcalTime = icalproperty_get_dtstart(p);
650 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
651 display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
652 wprintf("</TD></TR>\n");
655 wprintf(_("Due date:"));
656 wprintf("</TD><TD>");
657 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
658 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
660 wprintf("CHECKED=\"CHECKED\"");
663 wprintf(_("No date"));
668 IcalTime = icalproperty_get_due(p);
671 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
672 display_icaltimetype_as_webform(&IcalTime, "due", 0);
674 wprintf("</TD></TR>\n");
675 todoStatus = icalcomponent_get_status(vtodo);
676 wprintf("<TR><TD>\n");
677 wprintf(_("Completed:"));
678 wprintf("</TD><TD>");
679 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
680 if (todoStatus == ICAL_STATUS_COMPLETED) {
681 wprintf(" CHECKED=\"CHECKED\"");
684 wprintf("</TD></TR>");
685 // start category field
686 p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
688 wprintf(_("Category:"));
689 wprintf("</TD><TD>");
690 wprintf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
692 escputs((char *)icalproperty_get_categories(p));
695 wprintf("</TD></TR>\n ");
696 // end category field
698 wprintf(_("Description:"));
699 wprintf("</TD><TD>");
700 wprintf("<TEXTAREA NAME=\"description\" "
701 "ROWS=\"10\" COLS=\"80\">\n"
703 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
705 escputs((char *)icalproperty_get_comment(p));
707 wprintf("</TEXTAREA></TD></TR></TABLE>\n");
709 wprintf("<SPAN STYLE=\"text-align: center;\">"
710 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
712 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
714 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
720 wprintf("</td></tr></table>");
721 wprintf("</FORM>\n");
722 wprintf("</div></div></div>\n");
723 wprintf("<!-- end task edit form -->");
726 if (created_new_vtodo) {
727 icalcomponent_free(vtodo);
732 * \brief Save an edited task
733 * \param supplied_vtodo the task to save
734 * \param msgnum number of the mesage in our db
736 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
737 struct calview *calv)
740 int delete_existing = 0;
742 icalcomponent *vtodo, *encaps;
743 int created_new_vtodo = 0;
746 struct icaltimetype t;
748 if (supplied_vtodo != NULL) {
749 vtodo = supplied_vtodo;
751 * If we're looking at a fully encapsulated VCALENDAR
752 * rather than a VTODO component, attempt to use the first
753 * relevant VTODO subcomponent. If there is none, the
754 * NULL returned by icalcomponent_get_first_component() will
755 * tell the next iteration of this function to create a
758 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
759 save_individual_task(
760 icalcomponent_get_first_component(
761 vtodo, ICAL_VTODO_COMPONENT),
762 msgnum, from, unread, calv
768 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
769 created_new_vtodo = 1;
772 if (havebstr("save_button")) {
774 /** Replace values in the component with ones from the form */
776 while (prop = icalcomponent_get_first_property(vtodo,
777 ICAL_SUMMARY_PROPERTY), prop != NULL) {
778 icalcomponent_remove_property(vtodo, prop);
779 icalproperty_free(prop);
781 if (havebstr("summary")) {
783 icalcomponent_add_property(vtodo,
784 icalproperty_new_summary(bstr("summary")));
786 icalcomponent_add_property(vtodo,
787 icalproperty_new_summary("Untitled Task"));
790 while (prop = icalcomponent_get_first_property(vtodo,
791 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
792 icalcomponent_remove_property(vtodo, prop);
793 icalproperty_free(prop);
795 if (havebstr("description")) {
796 icalcomponent_add_property(vtodo,
797 icalproperty_new_description(bstr("description")));
800 while (prop = icalcomponent_get_first_property(vtodo,
801 ICAL_DTSTART_PROPERTY), prop != NULL) {
802 icalcomponent_remove_property(vtodo, prop);
803 icalproperty_free(prop);
805 if (IsEmptyStr(bstr("nodtstart"))) {
806 icaltime_from_webform(&t, "dtstart");
807 icalcomponent_add_property(vtodo,
808 icalproperty_new_dtstart(t)
811 while(prop = icalcomponent_get_first_property(vtodo,
812 ICAL_STATUS_PROPERTY), prop != NULL) {
813 icalcomponent_remove_property(vtodo,prop);
814 icalproperty_free(prop);
816 if (havebstr("status")) {
817 icalproperty_status taskStatus = icalproperty_string_to_status(
819 icalcomponent_set_status(vtodo, taskStatus);
821 while (prop = icalcomponent_get_first_property(vtodo,
822 ICAL_CATEGORIES_PROPERTY), prop != NULL) {
823 icalcomponent_remove_property(vtodo,prop);
824 icalproperty_free(prop);
826 if (!IsEmptyStr(bstr("category"))) {
827 prop = icalproperty_new_categories(bstr("category"));
828 icalcomponent_add_property(vtodo,prop);
830 while (prop = icalcomponent_get_first_property(vtodo,
831 ICAL_DUE_PROPERTY), prop != NULL) {
832 icalcomponent_remove_property(vtodo, prop);
833 icalproperty_free(prop);
835 if (IsEmptyStr(bstr("nodue"))) {
836 icaltime_from_webform(&t, "due");
837 icalcomponent_add_property(vtodo,
838 icalproperty_new_due(t)
841 /** Give this task a UID if it doesn't have one. */
842 lprintf(9, "Give this task a UID if it doesn't have one.\n");
843 if (icalcomponent_get_first_property(vtodo,
844 ICAL_UID_PROPERTY) == NULL) {
846 icalcomponent_add_property(vtodo,
847 icalproperty_new_uid(buf)
851 /** Increment the sequence ID */
852 lprintf(9, "Increment the sequence ID\n");
853 while (prop = icalcomponent_get_first_property(vtodo,
854 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
855 i = icalproperty_get_sequence(prop);
856 lprintf(9, "Sequence was %d\n", i);
857 if (i > sequence) sequence = i;
858 icalcomponent_remove_property(vtodo, prop);
859 icalproperty_free(prop);
862 lprintf(9, "New sequence is %d. Adding...\n", sequence);
863 icalcomponent_add_property(vtodo,
864 icalproperty_new_sequence(sequence)
868 * Encapsulate event into full VCALENDAR component. Clone it first,
869 * for two reasons: one, it's easier to just free the whole thing
870 * when we're done instead of unbundling, but more importantly, we
871 * can't encapsulate something that may already be encapsulated
874 lprintf(9, "Encapsulating into a full VCALENDAR component\n");
875 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
877 /* Serialize it and save it to the message base */
878 serv_puts("ENT0 1|||4");
879 serv_getln(buf, sizeof buf);
881 serv_puts("Content-type: text/calendar");
883 serv_puts(icalcomponent_as_ical_string(encaps));
887 * Probably not necessary; the server will see the UID
888 * of the object and delete the old one anyway, but
893 icalcomponent_free(encaps);
897 * If the user clicked 'Delete' then explicitly delete the message.
899 if (havebstr("delete_button")) {
903 if ( (delete_existing) && (msgnum > 0L) ) {
904 serv_printf("DELE %ld", lbstr("msgnum"));
905 serv_getln(buf, sizeof buf);
908 if (created_new_vtodo) {
909 icalcomponent_free(vtodo);
912 /** Go back to the task list */
919 * Code common to all icalendar display handlers. Given a message number and a MIME
920 * type, we load the message and hunt for that MIME type. If found, we load
921 * the relevant part, deserialize it into a libical component, filter it for
922 * the requested object type, and feed it to the specified handler.
924 void load_ical_object(long msgnum, int unread,
925 icalcomponent_kind which_kind,
926 void (*callback)(icalcomponent *, long, char*, int, struct calview *),
932 char mime_partnum[256];
933 char mime_filename[256];
934 char mime_content_type[256];
935 char mime_disposition[256];
937 char relevant_partnum[256];
938 char *relevant_source = NULL;
939 icalcomponent *cal, *c;
941 relevant_partnum[0] = '\0';
942 sprintf(buf, "MSG4 %ld", msgnum); /* we need the mime headers */
944 serv_getln(buf, sizeof buf);
945 if (buf[0] != '1') return;
947 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
948 if (!strncasecmp(buf, "part=", 5)) {
949 extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
950 extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
951 extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
952 extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
953 mime_length = extract_int(&buf[5], 5);
955 if ( (!strcasecmp(mime_content_type, "text/calendar"))
956 || (!strcasecmp(mime_content_type, "application/ics"))
957 || (!strcasecmp(mime_content_type, "text/vtodo"))
959 strcpy(relevant_partnum, mime_partnum);
962 else if (!strncasecmp(buf, "from=", 4)) {
963 extract_token(from, buf, 1, '=', sizeof(from));
967 if (!IsEmptyStr(relevant_partnum)) {
968 relevant_source = load_mimepart(msgnum, relevant_partnum);
969 if (relevant_source != NULL) {
971 cal = icalcomponent_new_from_string(relevant_source);
976 /* Simple components of desired type */
977 if (icalcomponent_isa(cal) == which_kind) {
978 callback(cal, msgnum, from, unread, calv);
981 /* Subcomponents of desired type */
982 for (c = icalcomponent_get_first_component(cal, which_kind);
984 c = icalcomponent_get_next_component(cal, which_kind)) {
985 callback(c, msgnum, from, unread, calv);
987 icalcomponent_free(cal);
989 free(relevant_source);
992 icalmemory_free_ring();
996 * Display a calendar item
998 void load_calendar_item(long msgnum, int unread, struct calview *c) {
999 load_ical_object(msgnum, unread, ICAL_VEVENT_COMPONENT, display_individual_cal, c);
1005 void display_task(long msgnum, int unread) {
1006 load_ical_object(msgnum, unread, ICAL_VTODO_COMPONENT, display_individual_cal, NULL);
1010 * Display the editor component for a task
1012 void display_edit_task(void) {
1015 /* Force change the room if we have to */
1016 if (havebstr("taskrm")) {
1017 gotoroom((char *)bstr("taskrm"));
1020 msgnum = lbstr("msgnum");
1023 load_ical_object(msgnum, 0,
1024 ICAL_VTODO_COMPONENT,
1025 display_edit_individual_task,
1031 display_edit_individual_task(NULL, 0L, "", 0, NULL);
1036 * save an edited task
1038 void save_task(void) {
1040 msgnum = lbstr("msgnum");
1042 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL);
1045 save_individual_task(NULL, 0L, "", 0, NULL);
1050 * display the editor component for an event
1052 void display_edit_event(void) {
1055 msgnum = lbstr("msgnum");
1057 /* existing event */
1058 load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, display_edit_individual_event, NULL);
1062 display_edit_individual_event(NULL, 0L, "", 0, NULL);
1067 * save an edited event
1069 void save_event(void) {
1072 msgnum = lbstr("msgnum");
1075 load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, save_individual_event, NULL);
1078 save_individual_event(NULL, 0L, "", 0, NULL);
1087 * Anonymous request of freebusy data for a user
1089 void do_freebusy(const char *req) {
1095 extract_token(who, req, 1, ' ', sizeof who);
1096 if (!strncasecmp(who, "/freebusy/", 10)) {
1097 strcpy(who, &who[10]);
1099 unescape_input(who);
1102 if ( (!strcasecmp(&who[len-4], ".vcf"))
1103 || (!strcasecmp(&who[len-4], ".ifb"))
1104 || (!strcasecmp(&who[len-4], ".vfb")) ) {
1108 lprintf(9, "freebusy requested for <%s>\n", who);
1109 serv_printf("ICAL freebusy|%s", who);
1110 serv_getln(buf, sizeof buf);
1112 if (buf[0] != '1') {
1113 hprintf("HTTP/1.1 404 %s\n", &buf[4]);
1114 output_headers(0, 0, 0, 0, 0, 0);
1115 hprintf("Content-Type: text/plain\r\n");
1116 wprintf("%s\n", &buf[4]);
1121 read_server_text(WC->WBuf, &lines);
1122 http_transmit_thing("text/calendar", 0);
1133 WebcitAddUrlHandler(HKEY("display_edit_task"), display_edit_task, 0);
1134 WebcitAddUrlHandler(HKEY("save_task"), save_task, 0);
1135 WebcitAddUrlHandler(HKEY("display_edit_event"), display_edit_event, 0);
1136 WebcitAddUrlHandler(HKEY("save_event"), save_event, 0);
1137 WebcitAddUrlHandler(HKEY("respond_to_request"), respond_to_request, 0);
1138 WebcitAddUrlHandler(HKEY("handle_rsvp"), handle_rsvp, 0);