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");
330 cal_process_object(cal, 0, msgnum, cal_partnum);
332 /* Free the memory we obtained from libical's constructor */
333 icalcomponent_free(cal);
340 * Respond to a meeting request
342 void respond_to_request(void) {
347 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
348 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
349 "<B>Respond to meeting request</B>"
350 "</FONT></TD></TR></TABLE><BR>\n"
353 serv_printf("ICAL respond|%s|%s|%s|",
361 wprintf("<TABLE BORDER=0><TR><TD>"
362 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
365 if (!strcasecmp(bstr("sc"), "accept")) {
366 wprintf("You have accepted this meeting invitation. "
367 "It has been entered into your calendar, "
369 } else if (!strcasecmp(bstr("sc"), "tentative")) {
370 wprintf("You have tentatively accepted this meeting invitation. "
371 "It has been 'pencilled in' to your calendar, "
373 } else if (!strcasecmp(bstr("sc"), "decline")) {
374 wprintf("You have declined this meeting invitation. "
375 "It has <b>not</b> been entered into your calendar, "
378 wprintf("and a reply has been sent to the meeting organizer."
379 "</TD></TR></TABLE>\n"
382 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
386 wprintf("<A HREF=\"/dotskip?room=");
387 urlescputs(WC->wc_roomname);
388 wprintf("\"><BR>Return to messages</A><BR>\n");
396 * Handle an incoming RSVP
398 void handle_rsvp(void) {
403 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
404 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
405 "<B>Update your calendar with this RSVP</B>"
406 "</FONT></TD></TR></TABLE><BR>\n"
409 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
417 wprintf("<TABLE BORDER=0><TR><TD>"
418 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
421 if (!strcasecmp(bstr("sc"), "update")) {
422 wprintf("Your calendar has been updated "
423 "to reflect this RSVP."
425 } else if (!strcasecmp(bstr("sc"), "ignore")) {
426 wprintf("You have chosen to ignore this RSVP. "
427 "Your calendar has <b>not</b> been updated."
430 wprintf("</TD></TR></TABLE>\n"
433 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
437 wprintf("<A HREF=\"/dotskip?room=");
438 urlescputs(WC->wc_roomname);
439 wprintf("\"><BR>Return to messages</A><BR>\n");
447 /*****************************************************************************/
452 * Display handlers for message reading
458 * If we're reading calendar items, just store them for now. We have to
459 * sort and re-output them later when we draw the calendar.
461 void display_individual_cal(icalcomponent *cal, long msgnum) {
465 WC->disp_cal = realloc(WC->disp_cal,
466 (sizeof(icalcomponent *) * WC->num_cal) );
467 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
469 WC->cal_msgnum = realloc(WC->cal_msgnum,
470 (sizeof(long) * WC->num_cal) );
471 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
477 * Display a task in the task list
479 void display_individual_task(icalcomponent *vtodo, long msgnum) {
482 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
483 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
485 escputs((char *)icalproperty_get_comment(p));
492 * Display a task by itself (for editing)
494 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
495 icalcomponent *vtodo;
497 struct icaltimetype t;
499 int created_new_vtodo = 0;
503 if (supplied_vtodo != NULL) {
504 vtodo = supplied_vtodo;
507 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
508 created_new_vtodo = 1;
512 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
513 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
515 "</FONT></TD></TR></TABLE><BR>\n"
518 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
519 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
523 "<INPUT TYPE=\"text\" NAME=\"summary\" "
524 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
525 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
527 escputs((char *)icalproperty_get_comment(p));
529 wprintf("\"><BR>\n");
531 wprintf("Start date: ");
532 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
534 t = icalproperty_get_dtstart(p);
537 t = icaltime_from_timet(now, 0);
539 display_icaltimetype_as_webform(&t, "dtstart");
542 wprintf("Due date: ");
543 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
545 t = icalproperty_get_due(p);
548 t = icaltime_from_timet(now, 0);
550 display_icaltimetype_as_webform(&t, "due");
553 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
554 "ROWS=10 COLS=80 WIDTH=80>\n"
556 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
558 escputs((char *)icalproperty_get_comment(p));
560 wprintf("</TEXTAREA><BR>\n");
562 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
564 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
566 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
570 wprintf("</FORM>\n");
574 if (created_new_vtodo) {
575 icalcomponent_free(vtodo);
580 * Save an edited task
582 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
584 int delete_existing = 0;
586 icalcomponent *vtodo;
587 int created_new_vtodo = 0;
589 if (supplied_vtodo != NULL) {
590 vtodo = supplied_vtodo;
593 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
594 created_new_vtodo = 1;
597 if (!strcasecmp(bstr("sc"), "Save")) {
599 /* Replace values in the component with ones from the form */
601 while (prop = icalcomponent_get_first_property(vtodo,
602 ICAL_SUMMARY_PROPERTY), prop != NULL) {
603 icalcomponent_remove_property(vtodo, prop);
604 icalproperty_free(prop);
606 icalcomponent_add_property(vtodo,
607 icalproperty_new_summary(bstr("summary")));
609 while (prop = icalcomponent_get_first_property(vtodo,
610 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
611 icalcomponent_remove_property(vtodo, prop);
612 icalproperty_free(prop);
614 icalcomponent_add_property(vtodo,
615 icalproperty_new_description(bstr("description")));
617 while (prop = icalcomponent_get_first_property(vtodo,
618 ICAL_DTSTART_PROPERTY), prop != NULL) {
619 icalcomponent_remove_property(vtodo, prop);
620 icalproperty_free(prop);
622 icalcomponent_add_property(vtodo,
623 icalproperty_new_dtstart(
624 icaltime_from_webform("dtstart")
628 while (prop = icalcomponent_get_first_property(vtodo,
629 ICAL_DUE_PROPERTY), prop != NULL) {
630 icalcomponent_remove_property(vtodo, prop);
631 icalproperty_free(prop);
633 icalcomponent_add_property(vtodo,
634 icalproperty_new_due(
635 icaltime_from_webform("due")
639 /* Serialize it and save it to the message base */
640 serv_puts("ENT0 1|||4");
643 serv_puts("Content-type: text/calendar");
645 serv_puts(icalcomponent_as_ical_string(vtodo));
648 /* Probably not necessary; the server will see the UID
649 * of the object and delete the old one anyway, but
657 * If the user clicked 'Delete' then explicitly delete the message.
659 if (!strcasecmp(bstr("sc"), "Delete")) {
663 if ( (delete_existing) && (msgnum > 0L) ) {
664 serv_printf("DELE %ld", atol(bstr("msgnum")));
668 if (created_new_vtodo) {
669 icalcomponent_free(vtodo);
672 /* Go back to the task list */
679 * Code common to all display handlers. Given a message number and a MIME
680 * type, we load the message and hunt for that MIME type. If found, we load
681 * the relevant part, deserialize it into a libical component, filter it for
682 * the requested object type, and feed it to the specified handler.
684 void display_using_handler(long msgnum,
686 icalcomponent_kind which_kind,
687 void (*callback)(icalcomponent *, long)
690 char mime_partnum[SIZ];
691 char mime_filename[SIZ];
692 char mime_content_type[SIZ];
693 char mime_disposition[SIZ];
695 char relevant_partnum[SIZ];
696 char *relevant_source = NULL;
697 icalcomponent *cal, *c;
699 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
702 if (buf[0] != '1') return;
704 while (serv_gets(buf), strcmp(buf, "000")) {
705 if (!strncasecmp(buf, "part=", 5)) {
706 extract(mime_filename, &buf[5], 1);
707 extract(mime_partnum, &buf[5], 2);
708 extract(mime_disposition, &buf[5], 3);
709 extract(mime_content_type, &buf[5], 4);
710 mime_length = extract_int(&buf[5], 5);
712 if (!strcasecmp(mime_content_type, "text/calendar")) {
713 strcpy(relevant_partnum, mime_partnum);
719 if (strlen(relevant_partnum) > 0) {
720 relevant_source = load_mimepart(msgnum, relevant_partnum);
721 if (relevant_source != NULL) {
723 cal = icalcomponent_new_from_string(relevant_source);
726 /* Simple components of desired type */
727 if (icalcomponent_isa(cal) == which_kind) {
728 callback(cal, msgnum);
731 /* Subcomponents of desired type */
732 for (c = icalcomponent_get_first_component(cal,
735 c = icalcomponent_get_next_component(cal,
739 icalcomponent_free(cal);
741 free(relevant_source);
747 void display_calendar(long msgnum) {
748 display_using_handler(msgnum, "text/calendar",
749 ICAL_VEVENT_COMPONENT,
750 display_individual_cal);
753 void display_task(long msgnum) {
754 display_using_handler(msgnum, "text/calendar",
755 ICAL_VTODO_COMPONENT,
756 display_individual_task);
759 void display_edit_task(void) {
762 msgnum = atol(bstr("msgnum"));
765 display_using_handler(msgnum, "text/calendar",
766 ICAL_VTODO_COMPONENT,
767 display_edit_individual_task);
771 display_edit_individual_task(NULL, 0L);
775 void save_task(void) {
778 msgnum = atol(bstr("msgnum"));
780 display_using_handler(msgnum, "text/calendar",
781 ICAL_VTODO_COMPONENT,
782 save_individual_task);
785 save_individual_task(NULL, 0L);
789 void display_edit_event(void) {
792 msgnum = atol(bstr("msgnum"));
795 display_using_handler(msgnum, "text/calendar",
796 ICAL_VEVENT_COMPONENT,
797 display_edit_individual_event);
801 display_edit_individual_event(NULL, 0L);
805 void save_event(void) {
808 msgnum = atol(bstr("msgnum"));
811 display_using_handler(msgnum, "text/calendar",
812 ICAL_VEVENT_COMPONENT,
813 save_individual_event);
816 save_individual_event(NULL, 0L);
820 #endif /* HAVE_ICAL_H */