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()
75 void cal_process_object(icalcomponent *cal,
81 icalproperty *method = NULL;
82 icalproperty_method the_method = ICAL_METHOD_NONE;
84 struct icaltimetype t;
87 char conflict_name[SIZ];
90 /* Leading HTML for the display of this object */
91 if (recursion_level == 0) {
92 wprintf("<CENTER><TABLE border=0>\n");
95 /* Look for a method */
96 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
98 /* See what we need to do with this */
100 the_method = icalproperty_get_method(method);
102 case ICAL_METHOD_REQUEST:
103 wprintf("<TR><TD COLSPAN=2>\n"
105 "SRC=\"/static/vcalendar.gif\">"
107 "<B>Meeting invitation</B>"
111 case ICAL_METHOD_REPLY:
112 wprintf("<TR><TD COLSPAN=2>\n"
114 "SRC=\"/static/vcalendar.gif\">"
116 "<B>Attendee's reply to your invitation</B>"
120 case ICAL_METHOD_PUBLISH:
121 wprintf("<TR><TD COLSPAN=2>\n"
123 "SRC=\"/static/vcalendar.gif\">"
125 "<B>Published event</B>"
130 wprintf("<TR><TD COLSPAN=2>"
131 "I don't know what to do with this.</TD></TR>"
137 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
139 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
140 escputs((char *)icalproperty_get_comment(p));
141 wprintf("</TD></TR>\n");
144 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
146 wprintf("<TR><TD><B>Location:</B></TD><TD>");
147 escputs((char *)icalproperty_get_comment(p));
148 wprintf("</TD></TR>\n");
152 * Only show start/end times if we're actually looking at the VEVENT
153 * component. Otherwise it shows bogus dates for things like timezone.
155 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
157 p = icalcomponent_get_first_property(cal,
158 ICAL_DTSTART_PROPERTY);
160 t = icalproperty_get_dtstart(p);
163 wprintf("<TR><TD><B>Date:"
165 "%s %d, %d</TD></TR>",
171 tt = icaltime_as_timet(t);
173 wprintf("<TR><TD><B>Starting date/time:"
180 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
182 t = icalproperty_get_dtend(p);
183 tt = icaltime_as_timet(t);
185 wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
192 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
194 wprintf("<TR><TD><B>Description:</B></TD><TD>");
195 escputs((char *)icalproperty_get_comment(p));
196 wprintf("</TD></TR>\n");
199 /* If the component has attendees, iterate through them. */
200 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
201 wprintf("<TR><TD><B>Attendee:</B></TD><TD>");
202 strcpy(buf, icalproperty_get_attendee(p));
203 if (!strncasecmp(buf, "MAILTO:", 7)) {
205 /* screen name or email address */
206 strcpy(buf, &buf[7]);
211 /* participant status */
212 partstat_as_string(buf, p);
215 wprintf("</TD></TR>\n");
218 /* If the component has subcomponents, recurse through them. */
219 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
221 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
222 /* Recursively process subcomponent */
223 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
226 /* If this is a REQUEST, display conflicts and buttons */
227 if (the_method == ICAL_METHOD_REQUEST) {
229 /* Check for conflicts */
230 lprintf(9, "Checking server calendar for conflicts...\n");
231 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
234 while (serv_gets(buf), strcmp(buf, "000")) {
235 extract(conflict_name, buf, 3);
236 is_update = extract_int(buf, 4);
237 wprintf("<TR><TD><B><I>%s</I></B></TD>"
248 "This is an update of" :
249 "This event would conflict with"
253 escputs(conflict_name);
254 wprintf(""</I> "
255 "which is already in your calendar."
259 lprintf(9, "...done.\n");
261 /* Display the Accept/Decline buttons */
262 wprintf("<TR><TD COLSPAN=2>"
263 "<FORM METHOD=\"GET\" "
264 "ACTION=\"/respond_to_request\">\n"
265 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
266 "VALUE=\"Accept\">\n"
268 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
269 "VALUE=\"Tentative\">\n"
271 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
272 "VALUE=\"Decline\">\n"
273 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
275 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
284 /* If this is a REPLY, display update button */
285 if (the_method == ICAL_METHOD_REPLY) {
288 * In the future, if we want to validate this object before
289 * continuing, we can do it this way:
290 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
295 /* Display the update buttons */
296 wprintf("<TR><TD COLSPAN=2>"
297 "Click <i>Update</i> to accept this reply and "
298 "update your calendar."
299 "<FORM METHOD=\"GET\" "
300 "ACTION=\"/handle_rsvp\">\n"
301 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
302 "VALUE=\"Update\">\n"
304 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
305 "VALUE=\"Ignore\">\n"
306 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
308 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
317 /* Trailing HTML for the display of this object */
318 if (recursion_level == 0) {
320 wprintf("</TR></TABLE></CENTER>\n");
326 * Deserialize a calendar object in a message so it can be processed.
327 * (This is the main entry point for these things)
329 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
332 cal = icalcomponent_new_from_string(part_source);
335 wprintf("Error parsing calendar object<BR>\n");
340 cal_process_object(cal, 0, msgnum, cal_partnum);
342 /* Free the memory we obtained from libical's constructor */
343 icalcomponent_free(cal);
350 * Respond to a meeting request
352 void respond_to_request(void) {
357 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
358 "<SPAN CLASS=\"titlebar\">Respond to meeting request</SPAN>"
359 "</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=\"#444455\"><TR><TD>"
413 "<SPAN CLASS=\"titlebar\">"
414 "Update your calendar with this RSVP</SPAN>"
415 "</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(struct disp_cal) * WC->num_cal) );
476 WC->disp_cal[WC->num_cal - 1].cal = icalcomponent_new_clone(cal);
478 WC->disp_cal[WC->num_cal - 1].cal_msgnum = msgnum;
484 * Display a task by itself (for editing)
487 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
488 icalcomponent *vtodo;
490 struct icaltimetype t;
492 int created_new_vtodo = 0;
496 if (supplied_vtodo != NULL) {
497 vtodo = supplied_vtodo;
499 /* If we're looking at a fully encapsulated VCALENDAR
500 * rather than a VTODO component, attempt to use the first
501 * relevant VTODO subcomponent. If there is none, the
502 * NULL returned by icalcomponent_get_first_component() will
503 * tell the next iteration of this function to create a
506 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
507 display_edit_individual_task(
508 icalcomponent_get_first_component(
509 vtodo, ICAL_VTODO_COMPONENT
516 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
517 created_new_vtodo = 1;
521 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
522 "<SPAN CLASS=\"titlebar\">Edit task</SPAN>"
523 "</TD></TR></TABLE><BR>\n"
526 do_template("beginbox_nt");
528 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
529 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
532 wprintf("<TABLE border=0>\n");
534 wprintf("<TR><TD>Summary:</TD><TD>"
535 "<INPUT TYPE=\"text\" NAME=\"summary\" "
536 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
537 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
539 escputs((char *)icalproperty_get_comment(p));
541 wprintf("\"></TD></TR>\n");
543 wprintf("<TR><TD>Start date:</TD><TD>");
544 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
546 t = icalproperty_get_dtstart(p);
549 t = icaltime_from_timet(now, 0);
551 display_icaltimetype_as_webform(&t, "dtstart");
552 wprintf("</TD></TR>\n");
554 wprintf("<TR><TD>Due date:</TD><TD>");
555 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
557 t = icalproperty_get_due(p);
560 t = icaltime_from_timet(now, 0);
562 display_icaltimetype_as_webform(&t, "due");
563 wprintf("</TD></TR>\n");
564 wprintf("<TR><TD>Description:</TD><TD>");
565 wprintf("<TEXTAREA NAME=\"description\" wrap=soft "
566 "ROWS=10 COLS=80 WIDTH=80>\n"
568 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
570 escputs((char *)icalproperty_get_comment(p));
572 wprintf("</TEXTAREA></TD></TR></TABLE>\n");
575 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
577 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
579 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
583 wprintf("</FORM>\n");
585 do_template("endbox");
588 if (created_new_vtodo) {
589 icalcomponent_free(vtodo);
594 * Save an edited task
597 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
599 int delete_existing = 0;
601 icalcomponent *vtodo;
602 int created_new_vtodo = 0;
606 if (supplied_vtodo != NULL) {
607 vtodo = supplied_vtodo;
608 /* If we're looking at a fully encapsulated VCALENDAR
609 * rather than a VTODO component, attempt to use the first
610 * relevant VTODO subcomponent. If there is none, the
611 * NULL returned by icalcomponent_get_first_component() will
612 * tell the next iteration of this function to create a
615 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
616 save_individual_task(
617 icalcomponent_get_first_component(
618 vtodo, ICAL_VTODO_COMPONENT
625 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
626 created_new_vtodo = 1;
629 if (!strcasecmp(bstr("sc"), "Save")) {
631 /* Replace values in the component with ones from the form */
633 while (prop = icalcomponent_get_first_property(vtodo,
634 ICAL_SUMMARY_PROPERTY), prop != NULL) {
635 icalcomponent_remove_property(vtodo, prop);
636 icalproperty_free(prop);
638 icalcomponent_add_property(vtodo,
639 icalproperty_new_summary(bstr("summary")));
641 while (prop = icalcomponent_get_first_property(vtodo,
642 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
643 icalcomponent_remove_property(vtodo, prop);
644 icalproperty_free(prop);
646 icalcomponent_add_property(vtodo,
647 icalproperty_new_description(bstr("description")));
649 while (prop = icalcomponent_get_first_property(vtodo,
650 ICAL_DTSTART_PROPERTY), prop != NULL) {
651 icalcomponent_remove_property(vtodo, prop);
652 icalproperty_free(prop);
654 icalcomponent_add_property(vtodo,
655 icalproperty_new_dtstart(
656 icaltime_from_webform("dtstart")
660 while (prop = icalcomponent_get_first_property(vtodo,
661 ICAL_DUE_PROPERTY), prop != NULL) {
662 icalcomponent_remove_property(vtodo, prop);
663 icalproperty_free(prop);
665 icalcomponent_add_property(vtodo,
666 icalproperty_new_due(
667 icaltime_from_webform("due")
671 /* Give this task a UID if it doesn't have one. */
672 lprintf(9, "Give this task a UID if it doesn't have one.\n");
673 if (icalcomponent_get_first_property(vtodo,
674 ICAL_UID_PROPERTY) == NULL) {
676 icalcomponent_add_property(vtodo,
677 icalproperty_new_uid(buf)
681 /* Increment the sequence ID */
682 lprintf(9, "Increment the sequence ID\n");
683 while (prop = icalcomponent_get_first_property(vtodo,
684 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
685 i = icalproperty_get_sequence(prop);
686 lprintf(9, "Sequence was %d\n", i);
687 if (i > sequence) sequence = i;
688 icalcomponent_remove_property(vtodo, prop);
689 icalproperty_free(prop);
692 lprintf(9, "New sequence is %d. Adding...\n", sequence);
693 icalcomponent_add_property(vtodo,
694 icalproperty_new_sequence(sequence)
698 /* Serialize it and save it to the message base */
699 serv_puts("ENT0 1|||4");
702 serv_puts("Content-type: text/calendar");
704 serv_puts(icalcomponent_as_ical_string(vtodo));
707 /* Probably not necessary; the server will see the UID
708 * of the object and delete the old one anyway, but
716 * If the user clicked 'Delete' then explicitly delete the message.
718 if (!strcasecmp(bstr("sc"), "Delete")) {
722 if ( (delete_existing) && (msgnum > 0L) ) {
723 serv_printf("DELE %ld", atol(bstr("msgnum")));
727 if (created_new_vtodo) {
728 icalcomponent_free(vtodo);
731 /* Go back to the task list */
738 * Code common to all display handlers. Given a message number and a MIME
739 * type, we load the message and hunt for that MIME type. If found, we load
740 * the relevant part, deserialize it into a libical component, filter it for
741 * the requested object type, and feed it to the specified handler.
744 void display_using_handler(long msgnum,
746 icalcomponent_kind which_kind,
747 void (*callback)(icalcomponent *, long)
750 char mime_partnum[SIZ];
751 char mime_filename[SIZ];
752 char mime_content_type[SIZ];
753 char mime_disposition[SIZ];
755 char relevant_partnum[SIZ];
756 char *relevant_source = NULL;
757 icalcomponent *cal, *c;
759 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
762 if (buf[0] != '1') return;
764 while (serv_gets(buf), strcmp(buf, "000")) {
765 if (!strncasecmp(buf, "part=", 5)) {
766 extract(mime_filename, &buf[5], 1);
767 extract(mime_partnum, &buf[5], 2);
768 extract(mime_disposition, &buf[5], 3);
769 extract(mime_content_type, &buf[5], 4);
770 mime_length = extract_int(&buf[5], 5);
772 if (!strcasecmp(mime_content_type, "text/calendar")) {
773 strcpy(relevant_partnum, mime_partnum);
779 if (strlen(relevant_partnum) > 0) {
780 relevant_source = load_mimepart(msgnum, relevant_partnum);
781 if (relevant_source != NULL) {
783 cal = icalcomponent_new_from_string(relevant_source);
788 /* Simple components of desired type */
789 if (icalcomponent_isa(cal) == which_kind) {
790 callback(cal, msgnum);
793 /* Subcomponents of desired type */
794 for (c = icalcomponent_get_first_component(cal,
797 c = icalcomponent_get_next_component(cal,
801 icalcomponent_free(cal);
803 free(relevant_source);
809 void display_calendar(long msgnum) {
810 display_using_handler(msgnum, "text/calendar",
811 ICAL_VEVENT_COMPONENT,
812 display_individual_cal);
815 void display_task(long msgnum) {
816 display_using_handler(msgnum, "text/calendar",
817 ICAL_VTODO_COMPONENT,
818 display_individual_cal);
821 void display_edit_task(void) {
824 /* Force change the room if we have to */
825 if (strlen(bstr("taskrm")) > 0) {
826 gotoroom(bstr("taskrm"));
829 msgnum = atol(bstr("msgnum"));
832 display_using_handler(msgnum, "text/calendar",
833 ICAL_VTODO_COMPONENT,
834 display_edit_individual_task);
838 display_edit_individual_task(NULL, 0L);
842 void save_task(void) {
845 msgnum = atol(bstr("msgnum"));
847 display_using_handler(msgnum, "text/calendar",
848 ICAL_VTODO_COMPONENT,
849 save_individual_task);
852 save_individual_task(NULL, 0L);
856 void display_edit_event(void) {
859 msgnum = atol(bstr("msgnum"));
862 display_using_handler(msgnum, "text/calendar",
863 ICAL_VEVENT_COMPONENT,
864 display_edit_individual_event);
868 display_edit_individual_event(NULL, 0L);
872 void save_event(void) {
875 msgnum = atol(bstr("msgnum"));
878 display_using_handler(msgnum, "text/calendar",
879 ICAL_VEVENT_COMPONENT,
880 save_individual_event);
883 save_individual_event(NULL, 0L);
892 * freebusy display (for client software)
894 void do_freebusy(char *req) {
899 extract_token(who, req, 1, ' ');
900 if (!strncasecmp(who, "/freebusy/", 10)) {
901 strcpy(who, &who[10]);
905 if ( (!strcasecmp(&who[strlen(who)-4], ".vcf"))
906 || (!strcasecmp(&who[strlen(who)-4], ".vfb")) ) {
907 who[strlen(who)-4] = 0;
910 lprintf(9, "freebusy requested for <%s>\n", who);
911 serv_printf("ICAL freebusy|%s", who);
915 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
917 wprintf("Content-Type: text/plain\n");
919 wprintf("%s\n", &buf[4]);
923 fb = read_server_text();
924 http_transmit_thing(fb, strlen(fb), "text/calendar", 0);
930 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */