4 * Functions which handle calendar objects and their processing/display.
14 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
28 #include "webserver.h"
33 * Handler stubs for builds with no calendar library available
35 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
37 wprintf("<I>This message contains calendaring/scheduling information,"
38 " but support for calendars is not available on this "
39 "particular system. Please ask your system administrator to "
40 "install a new version of the Citadel web service with "
41 "calendaring enabled.</I><BR>\n"
46 void display_calendar(long msgnum) {
48 "Cannot display calendar item. You are seeing this error "
49 "because your WebCit service has not been installed with "
50 "calendar support. Please contact your system administrator."
54 void display_task(long msgnum) {
56 "Cannot display to-do item. You are seeing this error "
57 "because your WebCit service has not been installed with "
58 "calendar support. Please contact your system administrator."
62 #else /* HAVE_ICAL_H */
65 /****** End of handler stubs. Everything below this line is real. ******/
71 * Process a calendar object
72 * ...at this point it's already been deserialized by cal_process_attachment()
74 void cal_process_object(icalcomponent *cal,
80 icalproperty *method = NULL;
81 icalproperty_method the_method = ICAL_METHOD_NONE;
83 struct icaltimetype t;
86 char conflict_name[SIZ];
89 /* Leading HTML for the display of this object */
90 if (recursion_level == 0) {
91 wprintf("<CENTER><TABLE border=0>\n");
94 /* Look for a method */
95 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
97 /* See what we need to do with this */
99 the_method = icalproperty_get_method(method);
101 case ICAL_METHOD_REQUEST:
102 wprintf("<TR><TD COLSPAN=2>\n"
104 "SRC=\"/static/vcalendar.gif\">"
106 "<B>Meeting invitation</B>
110 case ICAL_METHOD_REPLY:
111 wprintf("<TR><TD COLSPAN=2>\n"
113 "SRC=\"/static/vcalendar.gif\">"
115 "<B>Attendee's reply to your invitation</B>
119 case ICAL_METHOD_PUBLISH:
120 wprintf("<TR><TD COLSPAN=2>\n"
122 "SRC=\"/static/vcalendar.gif\">"
124 "<B>Published event</B>
129 wprintf("<TR><TD COLSPAN=2>"
130 "I don't know what to do with this.</TD></TR>"
136 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
138 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
139 escputs((char *)icalproperty_get_comment(p));
140 wprintf("</TD></TR>\n");
143 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
145 wprintf("<TR><TD><B>Location:</B></TD><TD>");
146 escputs((char *)icalproperty_get_comment(p));
147 wprintf("</TD></TR>\n");
151 * Only show start/end times if we're actually looking at the VEVENT
152 * component. Otherwise it shows bogus dates for things like timezone.
154 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
156 p = icalcomponent_get_first_property(cal,
157 ICAL_DTSTART_PROPERTY);
159 t = icalproperty_get_dtstart(p);
162 wprintf("<TR><TD><B>Date:"
164 "%s %d, %d</TD></TR>",
170 tt = icaltime_as_timet(t);
172 wprintf("<TR><TD><B>Starting date/time:"
179 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
181 t = icalproperty_get_dtend(p);
182 tt = icaltime_as_timet(t);
184 wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
191 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
193 wprintf("<TR><TD><B>Description:</B></TD><TD>");
194 escputs((char *)icalproperty_get_comment(p));
195 wprintf("</TD></TR>\n");
198 /* If the component has attendees, iterate through them. */
199 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
200 wprintf("<TR><TD><B>Attendee:</B></TD><TD>");
201 strcpy(buf, icalproperty_get_attendee(p));
202 if (!strncasecmp(buf, "MAILTO:", 7)) {
204 /* screen name or email address */
205 strcpy(buf, &buf[7]);
210 /* participant status */
211 partstat_as_string(buf, p);
214 wprintf("</TD></TR>\n");
217 /* If the component has subcomponents, recurse through them. */
218 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
220 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
221 /* Recursively process subcomponent */
222 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
225 /* If this is a REQUEST, display conflicts and buttons */
226 if (the_method == ICAL_METHOD_REQUEST) {
228 /* Check for conflicts */
229 lprintf(9, "Checking server calendar for conflicts...\n");
230 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
233 while (serv_gets(buf), strcmp(buf, "000")) {
234 extract(conflict_name, buf, 3);
235 is_update = extract_int(buf, 4);
236 wprintf("<TR><TD><B><I>%s</I></B></TD>"
247 "This is an update of" :
248 "This event would conflict with"
252 escputs(conflict_name);
253 wprintf(""</I> "
254 "which is already in your calendar."
258 lprintf(9, "...done.\n");
260 /* Display the Accept/Decline buttons */
261 wprintf("<TR><TD COLSPAN=2>"
262 "<FORM METHOD=\"GET\" "
263 "ACTION=\"/respond_to_request\">\n"
264 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
265 "VALUE=\"Accept\">\n"
267 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
268 "VALUE=\"Tentative\">\n"
270 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
271 "VALUE=\"Decline\">\n"
272 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
274 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
283 /* If this is a REPLY, display update button */
284 if (the_method == ICAL_METHOD_REPLY) {
287 * In the future, if we want to validate this object before
288 * continuing, we can do it this way:
289 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
294 /* Display the update buttons */
295 wprintf("<TR><TD COLSPAN=2>"
296 "Click <i>Update</i> to accept this reply and "
297 "update your calendar."
298 "<FORM METHOD=\"GET\" "
299 "ACTION=\"/handle_rsvp\">\n"
300 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
301 "VALUE=\"Update\">\n"
303 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
304 "VALUE=\"Ignore\">\n"
305 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
307 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
316 /* Trailing HTML for the display of this object */
317 if (recursion_level == 0) {
319 wprintf("</TR></TABLE></CENTER>\n");
325 * Deserialize a calendar object in a message so it can be processed.
326 * (This is the main entry point for these things)
328 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
331 cal = icalcomponent_new_from_string(part_source);
334 wprintf("Error parsing calendar object<BR>\n");
339 cal_process_object(cal, 0, msgnum, cal_partnum);
341 /* Free the memory we obtained from libical's constructor */
342 icalcomponent_free(cal);
349 * Respond to a meeting request
351 void respond_to_request(void) {
356 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
357 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
358 "<B>Respond to meeting request</B>"
359 "</FONT></TD></TR></TABLE><BR>\n"
362 serv_printf("ICAL respond|%s|%s|%s|",
370 wprintf("<TABLE BORDER=0><TR><TD>"
371 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
374 if (!strcasecmp(bstr("sc"), "accept")) {
375 wprintf("You have accepted this meeting invitation. "
376 "It has been entered into your calendar, "
378 } else if (!strcasecmp(bstr("sc"), "tentative")) {
379 wprintf("You have tentatively accepted this meeting invitation. "
380 "It has been 'pencilled in' to your calendar, "
382 } else if (!strcasecmp(bstr("sc"), "decline")) {
383 wprintf("You have declined this meeting invitation. "
384 "It has <b>not</b> been entered into your calendar, "
387 wprintf("and a reply has been sent to the meeting organizer."
388 "</TD></TR></TABLE>\n"
391 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
395 wprintf("<A HREF=\"/dotskip?room=");
396 urlescputs(WC->wc_roomname);
397 wprintf("\"><BR>Return to messages</A><BR>\n");
405 * Handle an incoming RSVP
407 void handle_rsvp(void) {
412 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
413 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
414 "<B>Update your calendar with this RSVP</B>"
415 "</FONT></TD></TR></TABLE><BR>\n"
418 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
426 wprintf("<TABLE BORDER=0><TR><TD>"
427 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
430 if (!strcasecmp(bstr("sc"), "update")) {
431 wprintf("Your calendar has been updated "
432 "to reflect this RSVP."
434 } else if (!strcasecmp(bstr("sc"), "ignore")) {
435 wprintf("You have chosen to ignore this RSVP. "
436 "Your calendar has <b>not</b> been updated."
439 wprintf("</TD></TR></TABLE>\n"
442 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
446 wprintf("<A HREF=\"/dotskip?room=");
447 urlescputs(WC->wc_roomname);
448 wprintf("\"><BR>Return to messages</A><BR>\n");
456 /*****************************************************************************/
461 * Display handlers for message reading
467 * If we're reading calendar items, just store them for now. We have to
468 * sort and re-output them later when we draw the calendar.
470 void display_individual_cal(icalcomponent *cal, long msgnum) {
474 WC->disp_cal = realloc(WC->disp_cal,
475 (sizeof(icalcomponent *) * WC->num_cal) );
476 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
478 WC->cal_msgnum = realloc(WC->cal_msgnum,
479 (sizeof(long) * WC->num_cal) );
480 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
486 * Display a task in the task list
488 void display_individual_task(icalcomponent *vtodo, long msgnum) {
491 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
492 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld&taskrm=", msgnum);
493 urlescputs(WC->wc_roomname);
496 escputs((char *)icalproperty_get_comment(p));
503 * Display a task by itself (for editing)
505 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
506 icalcomponent *vtodo;
508 struct icaltimetype t;
510 int created_new_vtodo = 0;
514 if (supplied_vtodo != NULL) {
515 vtodo = supplied_vtodo;
518 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
519 created_new_vtodo = 1;
523 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
524 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
526 "</FONT></TD></TR></TABLE><BR>\n"
529 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
530 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
534 "<INPUT TYPE=\"text\" NAME=\"summary\" "
535 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
536 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
538 escputs((char *)icalproperty_get_comment(p));
540 wprintf("\"><BR>\n");
542 wprintf("Start date: ");
543 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
545 t = icalproperty_get_dtstart(p);
548 t = icaltime_from_timet(now, 0);
550 display_icaltimetype_as_webform(&t, "dtstart");
553 wprintf("Due date: ");
554 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
556 t = icalproperty_get_due(p);
559 t = icaltime_from_timet(now, 0);
561 display_icaltimetype_as_webform(&t, "due");
564 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
565 "ROWS=10 COLS=80 WIDTH=80>\n"
567 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
569 escputs((char *)icalproperty_get_comment(p));
571 wprintf("</TEXTAREA><BR>\n");
573 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
575 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
577 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
581 wprintf("</FORM>\n");
585 if (created_new_vtodo) {
586 icalcomponent_free(vtodo);
591 * Save an edited task
593 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
595 int delete_existing = 0;
597 icalcomponent *vtodo;
598 int created_new_vtodo = 0;
600 if (supplied_vtodo != NULL) {
601 vtodo = supplied_vtodo;
604 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
605 created_new_vtodo = 1;
608 if (!strcasecmp(bstr("sc"), "Save")) {
610 /* Replace values in the component with ones from the form */
612 while (prop = icalcomponent_get_first_property(vtodo,
613 ICAL_SUMMARY_PROPERTY), prop != NULL) {
614 icalcomponent_remove_property(vtodo, prop);
615 icalproperty_free(prop);
617 icalcomponent_add_property(vtodo,
618 icalproperty_new_summary(bstr("summary")));
620 while (prop = icalcomponent_get_first_property(vtodo,
621 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
622 icalcomponent_remove_property(vtodo, prop);
623 icalproperty_free(prop);
625 icalcomponent_add_property(vtodo,
626 icalproperty_new_description(bstr("description")));
628 while (prop = icalcomponent_get_first_property(vtodo,
629 ICAL_DTSTART_PROPERTY), prop != NULL) {
630 icalcomponent_remove_property(vtodo, prop);
631 icalproperty_free(prop);
633 icalcomponent_add_property(vtodo,
634 icalproperty_new_dtstart(
635 icaltime_from_webform("dtstart")
639 while (prop = icalcomponent_get_first_property(vtodo,
640 ICAL_DUE_PROPERTY), prop != NULL) {
641 icalcomponent_remove_property(vtodo, prop);
642 icalproperty_free(prop);
644 icalcomponent_add_property(vtodo,
645 icalproperty_new_due(
646 icaltime_from_webform("due")
650 /* Serialize it and save it to the message base */
651 serv_puts("ENT0 1|||4");
654 serv_puts("Content-type: text/calendar");
656 serv_puts(icalcomponent_as_ical_string(vtodo));
659 /* Probably not necessary; the server will see the UID
660 * of the object and delete the old one anyway, but
668 * If the user clicked 'Delete' then explicitly delete the message.
670 if (!strcasecmp(bstr("sc"), "Delete")) {
674 if ( (delete_existing) && (msgnum > 0L) ) {
675 serv_printf("DELE %ld", atol(bstr("msgnum")));
679 if (created_new_vtodo) {
680 icalcomponent_free(vtodo);
683 /* Go back to the task list */
690 * Code common to all display handlers. Given a message number and a MIME
691 * type, we load the message and hunt for that MIME type. If found, we load
692 * the relevant part, deserialize it into a libical component, filter it for
693 * the requested object type, and feed it to the specified handler.
695 void display_using_handler(long msgnum,
697 icalcomponent_kind which_kind,
698 void (*callback)(icalcomponent *, long)
701 char mime_partnum[SIZ];
702 char mime_filename[SIZ];
703 char mime_content_type[SIZ];
704 char mime_disposition[SIZ];
706 char relevant_partnum[SIZ];
707 char *relevant_source = NULL;
708 icalcomponent *cal, *c;
710 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
713 if (buf[0] != '1') return;
715 while (serv_gets(buf), strcmp(buf, "000")) {
716 if (!strncasecmp(buf, "part=", 5)) {
717 extract(mime_filename, &buf[5], 1);
718 extract(mime_partnum, &buf[5], 2);
719 extract(mime_disposition, &buf[5], 3);
720 extract(mime_content_type, &buf[5], 4);
721 mime_length = extract_int(&buf[5], 5);
723 if (!strcasecmp(mime_content_type, "text/calendar")) {
724 strcpy(relevant_partnum, mime_partnum);
730 if (strlen(relevant_partnum) > 0) {
731 relevant_source = load_mimepart(msgnum, relevant_partnum);
732 if (relevant_source != NULL) {
734 cal = icalcomponent_new_from_string(relevant_source);
739 /* Simple components of desired type */
740 if (icalcomponent_isa(cal) == which_kind) {
741 callback(cal, msgnum);
744 /* Subcomponents of desired type */
745 for (c = icalcomponent_get_first_component(cal,
748 c = icalcomponent_get_next_component(cal,
752 icalcomponent_free(cal);
754 free(relevant_source);
760 void display_calendar(long msgnum) {
761 display_using_handler(msgnum, "text/calendar",
762 ICAL_VEVENT_COMPONENT,
763 display_individual_cal);
766 void display_task(long msgnum) {
767 display_using_handler(msgnum, "text/calendar",
768 ICAL_VTODO_COMPONENT,
769 display_individual_task);
772 void display_edit_task(void) {
775 /* Force change the room if we have to */
776 if (strlen(bstr("taskrm")) > 0) {
777 gotoroom(bstr("taskrm"), 0);
780 msgnum = atol(bstr("msgnum"));
783 display_using_handler(msgnum, "text/calendar",
784 ICAL_VTODO_COMPONENT,
785 display_edit_individual_task);
789 display_edit_individual_task(NULL, 0L);
793 void save_task(void) {
796 msgnum = atol(bstr("msgnum"));
798 display_using_handler(msgnum, "text/calendar",
799 ICAL_VTODO_COMPONENT,
800 save_individual_task);
803 save_individual_task(NULL, 0L);
807 void display_edit_event(void) {
810 msgnum = atol(bstr("msgnum"));
813 display_using_handler(msgnum, "text/calendar",
814 ICAL_VEVENT_COMPONENT,
815 display_edit_individual_event);
819 display_edit_individual_event(NULL, 0L);
823 void save_event(void) {
826 msgnum = atol(bstr("msgnum"));
829 display_using_handler(msgnum, "text/calendar",
830 ICAL_VEVENT_COMPONENT,
831 save_individual_event);
834 save_individual_event(NULL, 0L);
838 #endif /* HAVE_ICAL_H */