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)) {
203 strcpy(buf, &buf[7]);
207 /* FIXME add status */
208 wprintf("</TD></TR>\n");
211 /* If the component has subcomponents, recurse through them. */
212 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
214 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
215 /* Recursively process subcomponent */
216 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
219 /* If this is a REQUEST, display conflicts and buttons */
220 if (the_method == ICAL_METHOD_REQUEST) {
222 /* Check for conflicts */
223 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
226 while (serv_gets(buf), strcmp(buf, "000")) {
227 extract(conflict_name, buf, 3);
228 is_update = extract_int(buf, 4);
229 wprintf("<TR><TD><B><I>%s</I></B></TD>"
240 "This is an update of" :
241 "This event would conflict with"
245 escputs(conflict_name);
246 wprintf(""</I> "
247 "which is already in your calendar."
252 /* Display the Accept/Decline buttons */
253 wprintf("<TR><TD COLSPAN=2>"
254 "<FORM METHOD=\"GET\" "
255 "ACTION=\"/respond_to_request\">\n"
256 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
257 "VALUE=\"Accept\">\n"
259 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
260 "VALUE=\"Tentative\">\n"
262 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
263 "VALUE=\"Decline\">\n"
264 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
266 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
275 /* If this is a REPLY, display update button */
276 if (the_method == ICAL_METHOD_REPLY) {
279 * In the future, if we want to validate this object before
280 * continuing, we can do it this way:
281 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
286 /* Display the update buttons */
287 wprintf("<TR><TD COLSPAN=2>"
288 "Click <i>Update</i> to accept this reply and "
289 "update your calendar."
290 "<FORM METHOD=\"GET\" "
291 "ACTION=\"/handle_rsvp\">\n"
292 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
293 "VALUE=\"Update\">\n"
295 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
296 "VALUE=\"Ignore\">\n"
297 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
299 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
308 /* Trailing HTML for the display of this object */
309 if (recursion_level == 0) {
311 wprintf("</TR></TABLE></CENTER>\n");
317 * Deserialize a calendar object in a message so it can be processed.
318 * (This is the main entry point for these things)
320 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
323 cal = icalcomponent_new_from_string(part_source);
326 wprintf("Error parsing calendar object<BR>\n");
331 cal_process_object(cal, 0, msgnum, cal_partnum);
333 /* Free the memory we obtained from libical's constructor */
334 icalcomponent_free(cal);
341 * Respond to a meeting request
343 void respond_to_request(void) {
348 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
349 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
350 "<B>Respond to meeting request</B>"
351 "</FONT></TD></TR></TABLE><BR>\n"
354 serv_printf("ICAL respond|%s|%s|%s|",
362 wprintf("<TABLE BORDER=0><TR><TD>"
363 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
366 if (!strcasecmp(bstr("sc"), "accept")) {
367 wprintf("You have accepted this meeting invitation. "
368 "It has been entered into your calendar, "
370 } else if (!strcasecmp(bstr("sc"), "tentative")) {
371 wprintf("You have tentatively accepted this meeting invitation. "
372 "It has been 'pencilled in' to your calendar, "
374 } else if (!strcasecmp(bstr("sc"), "decline")) {
375 wprintf("You have declined this meeting invitation. "
376 "It has <b>not</b> been entered into your calendar, "
379 wprintf("and a reply has been sent to the meeting organizer."
380 "</TD></TR></TABLE>\n"
383 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
387 wprintf("<A HREF=\"/dotskip?room=");
388 urlescputs(WC->wc_roomname);
389 wprintf("\"><BR>Return to messages</A><BR>\n");
397 * Handle an incoming RSVP
399 void handle_rsvp(void) {
404 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
405 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
406 "<B>Update your calendar with this RSVP</B>"
407 "</FONT></TD></TR></TABLE><BR>\n"
410 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
418 wprintf("<TABLE BORDER=0><TR><TD>"
419 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
422 if (!strcasecmp(bstr("sc"), "update")) {
423 wprintf("Your calendar has been updated "
424 "to reflect this RSVP."
426 } else if (!strcasecmp(bstr("sc"), "ignore")) {
427 wprintf("You have chosen to ignore this RSVP. "
428 "Your calendar has <b>not</b> been updated."
431 wprintf("</TD></TR></TABLE>\n"
434 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
438 wprintf("<A HREF=\"/dotskip?room=");
439 urlescputs(WC->wc_roomname);
440 wprintf("\"><BR>Return to messages</A><BR>\n");
448 /*****************************************************************************/
453 * Display handlers for message reading
459 * If we're reading calendar items, just store them for now. We have to
460 * sort and re-output them later when we draw the calendar.
462 void display_individual_cal(icalcomponent *cal, long msgnum) {
466 WC->disp_cal = realloc(WC->disp_cal,
467 (sizeof(icalcomponent *) * WC->num_cal) );
468 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
470 WC->cal_msgnum = realloc(WC->cal_msgnum,
471 (sizeof(long) * WC->num_cal) );
472 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
478 * Display a task in the task list
480 void display_individual_task(icalcomponent *vtodo, long msgnum) {
483 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
484 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
486 escputs((char *)icalproperty_get_comment(p));
493 * Display a task by itself (for editing)
495 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
496 icalcomponent *vtodo;
498 struct icaltimetype t;
500 int created_new_vtodo = 0;
504 if (supplied_vtodo != NULL) {
505 vtodo = supplied_vtodo;
508 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
509 created_new_vtodo = 1;
513 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
514 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
516 "</FONT></TD></TR></TABLE><BR>\n"
519 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
520 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
524 "<INPUT TYPE=\"text\" NAME=\"summary\" "
525 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
526 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
528 escputs((char *)icalproperty_get_comment(p));
530 wprintf("\"><BR>\n");
532 wprintf("Start date: ");
533 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
535 t = icalproperty_get_dtstart(p);
538 t = icaltime_from_timet(now, 0);
540 display_icaltimetype_as_webform(&t, "dtstart");
543 wprintf("Due date: ");
544 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
546 t = icalproperty_get_due(p);
549 t = icaltime_from_timet(now, 0);
551 display_icaltimetype_as_webform(&t, "due");
554 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
555 "ROWS=10 COLS=80 WIDTH=80>\n"
557 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
559 escputs((char *)icalproperty_get_comment(p));
561 wprintf("</TEXTAREA><BR>\n");
563 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
565 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
567 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
571 wprintf("</FORM>\n");
575 if (created_new_vtodo) {
576 icalcomponent_free(vtodo);
581 * Save an edited task
583 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
585 int delete_existing = 0;
587 icalcomponent *vtodo;
588 int created_new_vtodo = 0;
590 if (supplied_vtodo != NULL) {
591 vtodo = supplied_vtodo;
594 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
595 created_new_vtodo = 1;
598 if (!strcasecmp(bstr("sc"), "Save")) {
600 /* Replace values in the component with ones from the form */
602 while (prop = icalcomponent_get_first_property(vtodo,
603 ICAL_SUMMARY_PROPERTY), prop != NULL) {
604 icalcomponent_remove_property(vtodo, prop);
605 icalproperty_free(prop);
607 icalcomponent_add_property(vtodo,
608 icalproperty_new_summary(bstr("summary")));
610 while (prop = icalcomponent_get_first_property(vtodo,
611 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
612 icalcomponent_remove_property(vtodo, prop);
613 icalproperty_free(prop);
615 icalcomponent_add_property(vtodo,
616 icalproperty_new_description(bstr("description")));
618 while (prop = icalcomponent_get_first_property(vtodo,
619 ICAL_DTSTART_PROPERTY), prop != NULL) {
620 icalcomponent_remove_property(vtodo, prop);
621 icalproperty_free(prop);
623 icalcomponent_add_property(vtodo,
624 icalproperty_new_dtstart(
625 icaltime_from_webform("dtstart")
629 while (prop = icalcomponent_get_first_property(vtodo,
630 ICAL_DUE_PROPERTY), prop != NULL) {
631 icalcomponent_remove_property(vtodo, prop);
632 icalproperty_free(prop);
634 icalcomponent_add_property(vtodo,
635 icalproperty_new_due(
636 icaltime_from_webform("due")
640 /* Serialize it and save it to the message base */
641 serv_puts("ENT0 1|||4");
644 serv_puts("Content-type: text/calendar");
646 serv_puts(icalcomponent_as_ical_string(vtodo));
649 /* Probably not necessary; the server will see the UID
650 * of the object and delete the old one anyway, but
658 * If the user clicked 'Delete' then explicitly delete the message.
660 if (!strcasecmp(bstr("sc"), "Delete")) {
664 if ( (delete_existing) && (msgnum > 0L) ) {
665 serv_printf("DELE %ld", atol(bstr("msgnum")));
669 if (created_new_vtodo) {
670 icalcomponent_free(vtodo);
673 /* Go back to the task list */
680 * Code common to all display handlers. Given a message number and a MIME
681 * type, we load the message and hunt for that MIME type. If found, we load
682 * the relevant part, deserialize it into a libical component, filter it for
683 * the requested object type, and feed it to the specified handler.
685 void display_using_handler(long msgnum,
687 icalcomponent_kind which_kind,
688 void (*callback)(icalcomponent *, long)
691 char mime_partnum[SIZ];
692 char mime_filename[SIZ];
693 char mime_content_type[SIZ];
694 char mime_disposition[SIZ];
696 char relevant_partnum[SIZ];
697 char *relevant_source = NULL;
698 icalcomponent *cal, *c;
700 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
703 if (buf[0] != '1') return;
705 while (serv_gets(buf), strcmp(buf, "000")) {
706 if (!strncasecmp(buf, "part=", 5)) {
707 extract(mime_filename, &buf[5], 1);
708 extract(mime_partnum, &buf[5], 2);
709 extract(mime_disposition, &buf[5], 3);
710 extract(mime_content_type, &buf[5], 4);
711 mime_length = extract_int(&buf[5], 5);
713 if (!strcasecmp(mime_content_type, "text/calendar")) {
714 strcpy(relevant_partnum, mime_partnum);
720 if (strlen(relevant_partnum) > 0) {
721 relevant_source = load_mimepart(msgnum, relevant_partnum);
722 if (relevant_source != NULL) {
724 cal = icalcomponent_new_from_string(relevant_source);
729 /* Simple components of desired type */
730 if (icalcomponent_isa(cal) == which_kind) {
731 callback(cal, msgnum);
734 /* Subcomponents of desired type */
735 for (c = icalcomponent_get_first_component(cal,
738 c = icalcomponent_get_next_component(cal,
742 icalcomponent_free(cal);
744 free(relevant_source);
750 void display_calendar(long msgnum) {
751 display_using_handler(msgnum, "text/calendar",
752 ICAL_VEVENT_COMPONENT,
753 display_individual_cal);
756 void display_task(long msgnum) {
757 display_using_handler(msgnum, "text/calendar",
758 ICAL_VTODO_COMPONENT,
759 display_individual_task);
762 void display_edit_task(void) {
765 msgnum = atol(bstr("msgnum"));
768 display_using_handler(msgnum, "text/calendar",
769 ICAL_VTODO_COMPONENT,
770 display_edit_individual_task);
774 display_edit_individual_task(NULL, 0L);
778 void save_task(void) {
781 msgnum = atol(bstr("msgnum"));
783 display_using_handler(msgnum, "text/calendar",
784 ICAL_VTODO_COMPONENT,
785 save_individual_task);
788 save_individual_task(NULL, 0L);
792 void display_edit_event(void) {
795 msgnum = atol(bstr("msgnum"));
798 display_using_handler(msgnum, "text/calendar",
799 ICAL_VEVENT_COMPONENT,
800 display_edit_individual_event);
804 display_edit_individual_event(NULL, 0L);
808 void save_event(void) {
811 msgnum = atol(bstr("msgnum"));
814 display_using_handler(msgnum, "text/calendar",
815 ICAL_VEVENT_COMPONENT,
816 save_individual_event);
819 save_individual_event(NULL, 0L);
823 #endif /* HAVE_ICAL_H */