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"
30 #ifndef WEBCIT_WITH_CALENDAR_SERVICE
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 /* WEBCIT_WITH_CALENDAR_SERVICE */
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 * ok for complete vcalendar objects
77 void cal_process_object(icalcomponent *cal,
83 icalproperty *method = NULL;
84 icalproperty_method the_method = ICAL_METHOD_NONE;
86 struct icaltimetype t;
89 char conflict_name[SIZ];
92 /* Leading HTML for the display of this object */
93 if (recursion_level == 0) {
94 wprintf("<CENTER><TABLE border=0>\n");
97 /* Look for a method */
98 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
100 /* See what we need to do with this */
101 if (method != NULL) {
102 the_method = icalproperty_get_method(method);
104 case ICAL_METHOD_REQUEST:
105 wprintf("<TR><TD COLSPAN=2>\n"
107 "SRC=\"/static/vcalendar.gif\">"
109 "<B>Meeting invitation</B>
113 case ICAL_METHOD_REPLY:
114 wprintf("<TR><TD COLSPAN=2>\n"
116 "SRC=\"/static/vcalendar.gif\">"
118 "<B>Attendee's reply to your invitation</B>
122 case ICAL_METHOD_PUBLISH:
123 wprintf("<TR><TD COLSPAN=2>\n"
125 "SRC=\"/static/vcalendar.gif\">"
127 "<B>Published event</B>
132 wprintf("<TR><TD COLSPAN=2>"
133 "I don't know what to do with this.</TD></TR>"
139 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
141 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
142 escputs((char *)icalproperty_get_comment(p));
143 wprintf("</TD></TR>\n");
146 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
148 wprintf("<TR><TD><B>Location:</B></TD><TD>");
149 escputs((char *)icalproperty_get_comment(p));
150 wprintf("</TD></TR>\n");
154 * Only show start/end times if we're actually looking at the VEVENT
155 * component. Otherwise it shows bogus dates for things like timezone.
157 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
159 p = icalcomponent_get_first_property(cal,
160 ICAL_DTSTART_PROPERTY);
162 t = icalproperty_get_dtstart(p);
165 wprintf("<TR><TD><B>Date:"
167 "%s %d, %d</TD></TR>",
173 tt = icaltime_as_timet(t);
175 wprintf("<TR><TD><B>Starting date/time:"
182 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
184 t = icalproperty_get_dtend(p);
185 tt = icaltime_as_timet(t);
187 wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
194 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
196 wprintf("<TR><TD><B>Description:</B></TD><TD>");
197 escputs((char *)icalproperty_get_comment(p));
198 wprintf("</TD></TR>\n");
201 /* If the component has attendees, iterate through them. */
202 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
203 wprintf("<TR><TD><B>Attendee:</B></TD><TD>");
204 strcpy(buf, icalproperty_get_attendee(p));
205 if (!strncasecmp(buf, "MAILTO:", 7)) {
207 /* screen name or email address */
208 strcpy(buf, &buf[7]);
213 /* participant status */
214 partstat_as_string(buf, p);
217 wprintf("</TD></TR>\n");
220 /* If the component has subcomponents, recurse through them. */
221 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
223 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
224 /* Recursively process subcomponent */
225 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
228 /* If this is a REQUEST, display conflicts and buttons */
229 if (the_method == ICAL_METHOD_REQUEST) {
231 /* Check for conflicts */
232 lprintf(9, "Checking server calendar for conflicts...\n");
233 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
236 while (serv_gets(buf), strcmp(buf, "000")) {
237 extract(conflict_name, buf, 3);
238 is_update = extract_int(buf, 4);
239 wprintf("<TR><TD><B><I>%s</I></B></TD>"
250 "This is an update of" :
251 "This event would conflict with"
255 escputs(conflict_name);
256 wprintf(""</I> "
257 "which is already in your calendar."
261 lprintf(9, "...done.\n");
263 /* Display the Accept/Decline buttons */
264 wprintf("<TR><TD COLSPAN=2>"
265 "<FORM METHOD=\"GET\" "
266 "ACTION=\"/respond_to_request\">\n"
267 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
268 "VALUE=\"Accept\">\n"
270 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
271 "VALUE=\"Tentative\">\n"
273 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
274 "VALUE=\"Decline\">\n"
275 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
277 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
286 /* If this is a REPLY, display update button */
287 if (the_method == ICAL_METHOD_REPLY) {
290 * In the future, if we want to validate this object before
291 * continuing, we can do it this way:
292 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
297 /* Display the update buttons */
298 wprintf("<TR><TD COLSPAN=2>"
299 "Click <i>Update</i> to accept this reply and "
300 "update your calendar."
301 "<FORM METHOD=\"GET\" "
302 "ACTION=\"/handle_rsvp\">\n"
303 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
304 "VALUE=\"Update\">\n"
306 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
307 "VALUE=\"Ignore\">\n"
308 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
310 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
319 /* Trailing HTML for the display of this object */
320 if (recursion_level == 0) {
322 wprintf("</TR></TABLE></CENTER>\n");
328 * Deserialize a calendar object in a message so it can be processed.
329 * (This is the main entry point for these things)
330 * ok for complete vcalendar objects
332 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
335 cal = icalcomponent_new_from_string(part_source);
338 wprintf("Error parsing calendar object<BR>\n");
343 cal_process_object(cal, 0, msgnum, cal_partnum);
345 /* Free the memory we obtained from libical's constructor */
346 icalcomponent_free(cal);
353 * Respond to a meeting request
355 void respond_to_request(void) {
360 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
361 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
362 "<B>Respond to meeting request</B>"
363 "</FONT></TD></TR></TABLE><BR>\n"
366 serv_printf("ICAL respond|%s|%s|%s|",
374 wprintf("<TABLE BORDER=0><TR><TD>"
375 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
378 if (!strcasecmp(bstr("sc"), "accept")) {
379 wprintf("You have accepted this meeting invitation. "
380 "It has been entered into your calendar, "
382 } else if (!strcasecmp(bstr("sc"), "tentative")) {
383 wprintf("You have tentatively accepted this meeting invitation. "
384 "It has been 'pencilled in' to your calendar, "
386 } else if (!strcasecmp(bstr("sc"), "decline")) {
387 wprintf("You have declined this meeting invitation. "
388 "It has <b>not</b> been entered into your calendar, "
391 wprintf("and a reply has been sent to the meeting organizer."
392 "</TD></TR></TABLE>\n"
395 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
399 wprintf("<A HREF=\"/dotskip?room=");
400 urlescputs(WC->wc_roomname);
401 wprintf("\"><BR>Return to messages</A><BR>\n");
409 * Handle an incoming RSVP
411 void handle_rsvp(void) {
416 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
417 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
418 "<B>Update your calendar with this RSVP</B>"
419 "</FONT></TD></TR></TABLE><BR>\n"
422 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
430 wprintf("<TABLE BORDER=0><TR><TD>"
431 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
434 if (!strcasecmp(bstr("sc"), "update")) {
435 wprintf("Your calendar has been updated "
436 "to reflect this RSVP."
438 } else if (!strcasecmp(bstr("sc"), "ignore")) {
439 wprintf("You have chosen to ignore this RSVP. "
440 "Your calendar has <b>not</b> been updated."
443 wprintf("</TD></TR></TABLE>\n"
446 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
450 wprintf("<A HREF=\"/dotskip?room=");
451 urlescputs(WC->wc_roomname);
452 wprintf("\"><BR>Return to messages</A><BR>\n");
460 /*****************************************************************************/
465 * Display handlers for message reading
471 * If we're reading calendar items, just store them for now. We have to
472 * sort and re-output them later when we draw the calendar.
474 void display_individual_cal(icalcomponent *cal, long msgnum) {
478 WC->disp_cal = realloc(WC->disp_cal,
479 (sizeof(icalcomponent *) * WC->num_cal) );
480 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
482 WC->cal_msgnum = realloc(WC->cal_msgnum,
483 (sizeof(long) * WC->num_cal) );
484 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
490 * Display a task in the task list
492 void display_individual_task(icalcomponent *vtodo, long msgnum) {
495 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
496 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld&taskrm=", msgnum);
497 urlescputs(WC->wc_roomname);
500 escputs((char *)icalproperty_get_comment(p));
507 * Display a task by itself (for editing)
509 * ok for complete vcalendar objects
511 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
512 icalcomponent *vtodo;
514 struct icaltimetype t;
516 int created_new_vtodo = 0;
520 if (supplied_vtodo != NULL) {
521 vtodo = supplied_vtodo;
523 /* If we're looking at a fully encapsulated VCALENDAR
524 * rather than a VTODO component, attempt to use the first
525 * relevant VTODO subcomponent. If there is none, the
526 * NULL returned by icalcomponent_get_first_component() will
527 * tell the next iteration of this function to create a
530 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
531 display_edit_individual_task(
532 icalcomponent_get_first_component(
533 vtodo, ICAL_VTODO_COMPONENT
540 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
541 created_new_vtodo = 1;
545 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
546 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
548 "</FONT></TD></TR></TABLE><BR>\n"
551 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
552 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
556 "<INPUT TYPE=\"text\" NAME=\"summary\" "
557 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
558 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
560 escputs((char *)icalproperty_get_comment(p));
562 wprintf("\"><BR>\n");
564 wprintf("Start date: ");
565 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
567 t = icalproperty_get_dtstart(p);
570 t = icaltime_from_timet(now, 0);
572 display_icaltimetype_as_webform(&t, "dtstart");
575 wprintf("Due date: ");
576 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
578 t = icalproperty_get_due(p);
581 t = icaltime_from_timet(now, 0);
583 display_icaltimetype_as_webform(&t, "due");
586 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
587 "ROWS=10 COLS=80 WIDTH=80>\n"
589 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
591 escputs((char *)icalproperty_get_comment(p));
593 wprintf("</TEXTAREA><BR>\n");
595 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
597 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
599 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
603 wprintf("</FORM>\n");
607 if (created_new_vtodo) {
608 icalcomponent_free(vtodo);
613 * Save an edited task
617 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
619 int delete_existing = 0;
621 icalcomponent *vtodo;
622 int created_new_vtodo = 0;
624 if (supplied_vtodo != NULL) {
625 vtodo = supplied_vtodo;
626 /* If we're looking at a fully encapsulated VCALENDAR
627 * rather than a VTODO component, attempt to use the first
628 * relevant VTODO subcomponent. If there is none, the
629 * NULL returned by icalcomponent_get_first_component() will
630 * tell the next iteration of this function to create a
633 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
634 save_individual_task(
635 icalcomponent_get_first_component(
636 vtodo, ICAL_VTODO_COMPONENT
643 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
644 created_new_vtodo = 1;
647 if (!strcasecmp(bstr("sc"), "Save")) {
649 /* Replace values in the component with ones from the form */
651 while (prop = icalcomponent_get_first_property(vtodo,
652 ICAL_SUMMARY_PROPERTY), prop != NULL) {
653 icalcomponent_remove_property(vtodo, prop);
654 icalproperty_free(prop);
656 icalcomponent_add_property(vtodo,
657 icalproperty_new_summary(bstr("summary")));
659 while (prop = icalcomponent_get_first_property(vtodo,
660 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
661 icalcomponent_remove_property(vtodo, prop);
662 icalproperty_free(prop);
664 icalcomponent_add_property(vtodo,
665 icalproperty_new_description(bstr("description")));
667 while (prop = icalcomponent_get_first_property(vtodo,
668 ICAL_DTSTART_PROPERTY), prop != NULL) {
669 icalcomponent_remove_property(vtodo, prop);
670 icalproperty_free(prop);
672 icalcomponent_add_property(vtodo,
673 icalproperty_new_dtstart(
674 icaltime_from_webform("dtstart")
678 while (prop = icalcomponent_get_first_property(vtodo,
679 ICAL_DUE_PROPERTY), prop != NULL) {
680 icalcomponent_remove_property(vtodo, prop);
681 icalproperty_free(prop);
683 icalcomponent_add_property(vtodo,
684 icalproperty_new_due(
685 icaltime_from_webform("due")
689 /* Serialize it and save it to the message base */
690 serv_puts("ENT0 1|||4");
693 serv_puts("Content-type: text/calendar");
695 serv_puts(icalcomponent_as_ical_string(vtodo));
698 /* Probably not necessary; the server will see the UID
699 * of the object and delete the old one anyway, but
707 * If the user clicked 'Delete' then explicitly delete the message.
709 if (!strcasecmp(bstr("sc"), "Delete")) {
713 if ( (delete_existing) && (msgnum > 0L) ) {
714 serv_printf("DELE %ld", atol(bstr("msgnum")));
718 if (created_new_vtodo) {
719 icalcomponent_free(vtodo);
722 /* Go back to the task list */
729 * Code common to all display handlers. Given a message number and a MIME
730 * type, we load the message and hunt for that MIME type. If found, we load
731 * the relevant part, deserialize it into a libical component, filter it for
732 * the requested object type, and feed it to the specified handler.
736 void display_using_handler(long msgnum,
738 icalcomponent_kind which_kind,
739 void (*callback)(icalcomponent *, long)
742 char mime_partnum[SIZ];
743 char mime_filename[SIZ];
744 char mime_content_type[SIZ];
745 char mime_disposition[SIZ];
747 char relevant_partnum[SIZ];
748 char *relevant_source = NULL;
749 icalcomponent *cal, *c;
751 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
754 if (buf[0] != '1') return;
756 while (serv_gets(buf), strcmp(buf, "000")) {
757 if (!strncasecmp(buf, "part=", 5)) {
758 extract(mime_filename, &buf[5], 1);
759 extract(mime_partnum, &buf[5], 2);
760 extract(mime_disposition, &buf[5], 3);
761 extract(mime_content_type, &buf[5], 4);
762 mime_length = extract_int(&buf[5], 5);
764 if (!strcasecmp(mime_content_type, "text/calendar")) {
765 strcpy(relevant_partnum, mime_partnum);
771 if (strlen(relevant_partnum) > 0) {
772 relevant_source = load_mimepart(msgnum, relevant_partnum);
773 if (relevant_source != NULL) {
775 cal = icalcomponent_new_from_string(relevant_source);
780 /* Simple components of desired type */
781 if (icalcomponent_isa(cal) == which_kind) {
782 callback(cal, msgnum);
785 /* Subcomponents of desired type */
786 for (c = icalcomponent_get_first_component(cal,
789 c = icalcomponent_get_next_component(cal,
793 icalcomponent_free(cal);
795 free(relevant_source);
801 void display_calendar(long msgnum) {
802 display_using_handler(msgnum, "text/calendar",
803 ICAL_VEVENT_COMPONENT,
804 display_individual_cal);
807 void display_task(long msgnum) {
808 display_using_handler(msgnum, "text/calendar",
809 ICAL_VTODO_COMPONENT,
810 display_individual_task);
813 void display_edit_task(void) {
816 /* Force change the room if we have to */
817 if (strlen(bstr("taskrm")) > 0) {
818 gotoroom(bstr("taskrm"), 0);
821 msgnum = atol(bstr("msgnum"));
824 display_using_handler(msgnum, "text/calendar",
825 ICAL_VTODO_COMPONENT,
826 display_edit_individual_task);
830 display_edit_individual_task(NULL, 0L);
834 void save_task(void) {
837 msgnum = atol(bstr("msgnum"));
839 display_using_handler(msgnum, "text/calendar",
840 ICAL_VTODO_COMPONENT,
841 save_individual_task);
844 save_individual_task(NULL, 0L);
848 void display_edit_event(void) {
851 msgnum = atol(bstr("msgnum"));
854 display_using_handler(msgnum, "text/calendar",
855 ICAL_VEVENT_COMPONENT,
856 display_edit_individual_event);
860 display_edit_individual_event(NULL, 0L);
864 void save_event(void) {
867 msgnum = atol(bstr("msgnum"));
870 display_using_handler(msgnum, "text/calendar",
871 ICAL_VEVENT_COMPONENT,
872 save_individual_event);
875 save_individual_event(NULL, 0L);
879 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */