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 "<SPAN CLASS=\"titlebar\">Respond to meeting request</SPAN>"
362 "</TD></TR></TABLE><BR>\n"
365 serv_printf("ICAL respond|%s|%s|%s|",
373 wprintf("<TABLE BORDER=0><TR><TD>"
374 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
377 if (!strcasecmp(bstr("sc"), "accept")) {
378 wprintf("You have accepted this meeting invitation. "
379 "It has been entered into your calendar, "
381 } else if (!strcasecmp(bstr("sc"), "tentative")) {
382 wprintf("You have tentatively accepted this meeting invitation. "
383 "It has been 'pencilled in' to your calendar, "
385 } else if (!strcasecmp(bstr("sc"), "decline")) {
386 wprintf("You have declined this meeting invitation. "
387 "It has <b>not</b> been entered into your calendar, "
390 wprintf("and a reply has been sent to the meeting organizer."
391 "</TD></TR></TABLE>\n"
394 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
398 wprintf("<A HREF=\"/dotskip?room=");
399 urlescputs(WC->wc_roomname);
400 wprintf("\"><BR>Return to messages</A><BR>\n");
408 * Handle an incoming RSVP
410 void handle_rsvp(void) {
415 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#007700\"><TR><TD>"
416 "<SPAN CLASS=\"titlebar\">"
417 "Update your calendar with this RSVP</SPAN>"
418 "</TD></TR></TABLE><BR>\n"
421 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
429 wprintf("<TABLE BORDER=0><TR><TD>"
430 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
433 if (!strcasecmp(bstr("sc"), "update")) {
434 wprintf("Your calendar has been updated "
435 "to reflect this RSVP."
437 } else if (!strcasecmp(bstr("sc"), "ignore")) {
438 wprintf("You have chosen to ignore this RSVP. "
439 "Your calendar has <b>not</b> been updated."
442 wprintf("</TD></TR></TABLE>\n"
445 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
449 wprintf("<A HREF=\"/dotskip?room=");
450 urlescputs(WC->wc_roomname);
451 wprintf("\"><BR>Return to messages</A><BR>\n");
459 /*****************************************************************************/
464 * Display handlers for message reading
470 * If we're reading calendar items, just store them for now. We have to
471 * sort and re-output them later when we draw the calendar.
473 void display_individual_cal(icalcomponent *cal, long msgnum) {
477 WC->disp_cal = realloc(WC->disp_cal,
478 (sizeof(icalcomponent *) * WC->num_cal) );
479 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
481 WC->cal_msgnum = realloc(WC->cal_msgnum,
482 (sizeof(long) * WC->num_cal) );
483 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
489 * Display a task in the task list
491 void display_individual_task(icalcomponent *vtodo, long msgnum) {
494 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
495 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld&taskrm=", msgnum);
496 urlescputs(WC->wc_roomname);
499 escputs((char *)icalproperty_get_comment(p));
506 * Display a task by itself (for editing)
508 * ok for complete vcalendar objects
510 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
511 icalcomponent *vtodo;
513 struct icaltimetype t;
515 int created_new_vtodo = 0;
519 if (supplied_vtodo != NULL) {
520 vtodo = supplied_vtodo;
522 /* If we're looking at a fully encapsulated VCALENDAR
523 * rather than a VTODO component, attempt to use the first
524 * relevant VTODO subcomponent. If there is none, the
525 * NULL returned by icalcomponent_get_first_component() will
526 * tell the next iteration of this function to create a
529 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
530 display_edit_individual_task(
531 icalcomponent_get_first_component(
532 vtodo, ICAL_VTODO_COMPONENT
539 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
540 created_new_vtodo = 1;
544 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#007700\"><TR><TD>"
545 "<SPAN CLASS=\"titlebar\">Edit task</SPAN>"
546 "</TD></TR></TABLE><BR>\n"
549 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
550 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
554 "<INPUT TYPE=\"text\" NAME=\"summary\" "
555 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
556 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
558 escputs((char *)icalproperty_get_comment(p));
560 wprintf("\"><BR>\n");
562 wprintf("Start date: ");
563 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
565 t = icalproperty_get_dtstart(p);
568 t = icaltime_from_timet(now, 0);
570 display_icaltimetype_as_webform(&t, "dtstart");
573 wprintf("Due date: ");
574 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
576 t = icalproperty_get_due(p);
579 t = icaltime_from_timet(now, 0);
581 display_icaltimetype_as_webform(&t, "due");
584 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
585 "ROWS=10 COLS=80 WIDTH=80>\n"
587 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
589 escputs((char *)icalproperty_get_comment(p));
591 wprintf("</TEXTAREA><BR>\n");
593 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
595 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
597 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
601 wprintf("</FORM>\n");
605 if (created_new_vtodo) {
606 icalcomponent_free(vtodo);
611 * Save an edited task
615 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
617 int delete_existing = 0;
619 icalcomponent *vtodo;
620 int created_new_vtodo = 0;
622 if (supplied_vtodo != NULL) {
623 vtodo = supplied_vtodo;
624 /* If we're looking at a fully encapsulated VCALENDAR
625 * rather than a VTODO component, attempt to use the first
626 * relevant VTODO subcomponent. If there is none, the
627 * NULL returned by icalcomponent_get_first_component() will
628 * tell the next iteration of this function to create a
631 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
632 save_individual_task(
633 icalcomponent_get_first_component(
634 vtodo, ICAL_VTODO_COMPONENT
641 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
642 created_new_vtodo = 1;
645 if (!strcasecmp(bstr("sc"), "Save")) {
647 /* Replace values in the component with ones from the form */
649 while (prop = icalcomponent_get_first_property(vtodo,
650 ICAL_SUMMARY_PROPERTY), prop != NULL) {
651 icalcomponent_remove_property(vtodo, prop);
652 icalproperty_free(prop);
654 icalcomponent_add_property(vtodo,
655 icalproperty_new_summary(bstr("summary")));
657 while (prop = icalcomponent_get_first_property(vtodo,
658 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
659 icalcomponent_remove_property(vtodo, prop);
660 icalproperty_free(prop);
662 icalcomponent_add_property(vtodo,
663 icalproperty_new_description(bstr("description")));
665 while (prop = icalcomponent_get_first_property(vtodo,
666 ICAL_DTSTART_PROPERTY), prop != NULL) {
667 icalcomponent_remove_property(vtodo, prop);
668 icalproperty_free(prop);
670 icalcomponent_add_property(vtodo,
671 icalproperty_new_dtstart(
672 icaltime_from_webform("dtstart")
676 while (prop = icalcomponent_get_first_property(vtodo,
677 ICAL_DUE_PROPERTY), prop != NULL) {
678 icalcomponent_remove_property(vtodo, prop);
679 icalproperty_free(prop);
681 icalcomponent_add_property(vtodo,
682 icalproperty_new_due(
683 icaltime_from_webform("due")
687 /* Serialize it and save it to the message base */
688 serv_puts("ENT0 1|||4");
691 serv_puts("Content-type: text/calendar");
693 serv_puts(icalcomponent_as_ical_string(vtodo));
696 /* Probably not necessary; the server will see the UID
697 * of the object and delete the old one anyway, but
705 * If the user clicked 'Delete' then explicitly delete the message.
707 if (!strcasecmp(bstr("sc"), "Delete")) {
711 if ( (delete_existing) && (msgnum > 0L) ) {
712 serv_printf("DELE %ld", atol(bstr("msgnum")));
716 if (created_new_vtodo) {
717 icalcomponent_free(vtodo);
720 /* Go back to the task list */
727 * Code common to all display handlers. Given a message number and a MIME
728 * type, we load the message and hunt for that MIME type. If found, we load
729 * the relevant part, deserialize it into a libical component, filter it for
730 * the requested object type, and feed it to the specified handler.
734 void display_using_handler(long msgnum,
736 icalcomponent_kind which_kind,
737 void (*callback)(icalcomponent *, long)
740 char mime_partnum[SIZ];
741 char mime_filename[SIZ];
742 char mime_content_type[SIZ];
743 char mime_disposition[SIZ];
745 char relevant_partnum[SIZ];
746 char *relevant_source = NULL;
747 icalcomponent *cal, *c;
749 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
752 if (buf[0] != '1') return;
754 while (serv_gets(buf), strcmp(buf, "000")) {
755 if (!strncasecmp(buf, "part=", 5)) {
756 extract(mime_filename, &buf[5], 1);
757 extract(mime_partnum, &buf[5], 2);
758 extract(mime_disposition, &buf[5], 3);
759 extract(mime_content_type, &buf[5], 4);
760 mime_length = extract_int(&buf[5], 5);
762 if (!strcasecmp(mime_content_type, "text/calendar")) {
763 strcpy(relevant_partnum, mime_partnum);
769 if (strlen(relevant_partnum) > 0) {
770 relevant_source = load_mimepart(msgnum, relevant_partnum);
771 if (relevant_source != NULL) {
773 cal = icalcomponent_new_from_string(relevant_source);
778 /* Simple components of desired type */
779 if (icalcomponent_isa(cal) == which_kind) {
780 callback(cal, msgnum);
783 /* Subcomponents of desired type */
784 for (c = icalcomponent_get_first_component(cal,
787 c = icalcomponent_get_next_component(cal,
791 icalcomponent_free(cal);
793 free(relevant_source);
799 void display_calendar(long msgnum) {
800 display_using_handler(msgnum, "text/calendar",
801 ICAL_VEVENT_COMPONENT,
802 display_individual_cal);
805 void display_task(long msgnum) {
806 display_using_handler(msgnum, "text/calendar",
807 ICAL_VTODO_COMPONENT,
808 display_individual_task);
811 void display_edit_task(void) {
814 /* Force change the room if we have to */
815 if (strlen(bstr("taskrm")) > 0) {
816 gotoroom(bstr("taskrm"), 0);
819 msgnum = atol(bstr("msgnum"));
822 display_using_handler(msgnum, "text/calendar",
823 ICAL_VTODO_COMPONENT,
824 display_edit_individual_task);
828 display_edit_individual_task(NULL, 0L);
832 void save_task(void) {
835 msgnum = atol(bstr("msgnum"));
837 display_using_handler(msgnum, "text/calendar",
838 ICAL_VTODO_COMPONENT,
839 save_individual_task);
842 save_individual_task(NULL, 0L);
846 void display_edit_event(void) {
849 msgnum = atol(bstr("msgnum"));
852 display_using_handler(msgnum, "text/calendar",
853 ICAL_VEVENT_COMPONENT,
854 display_edit_individual_event);
858 display_edit_individual_event(NULL, 0L);
862 void save_event(void) {
865 msgnum = atol(bstr("msgnum"));
868 display_using_handler(msgnum, "text/calendar",
869 ICAL_VEVENT_COMPONENT,
870 save_individual_event);
873 save_individual_event(NULL, 0L);
877 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */