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 lprintf(9, "Checking server calendar for conflicts...\n");
224 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
227 while (serv_gets(buf), strcmp(buf, "000")) {
228 extract(conflict_name, buf, 3);
229 is_update = extract_int(buf, 4);
230 wprintf("<TR><TD><B><I>%s</I></B></TD>"
241 "This is an update of" :
242 "This event would conflict with"
246 escputs(conflict_name);
247 wprintf(""</I> "
248 "which is already in your calendar."
252 lprintf(9, "...done.\n");
254 /* Display the Accept/Decline buttons */
255 wprintf("<TR><TD COLSPAN=2>"
256 "<FORM METHOD=\"GET\" "
257 "ACTION=\"/respond_to_request\">\n"
258 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
259 "VALUE=\"Accept\">\n"
261 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
262 "VALUE=\"Tentative\">\n"
264 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
265 "VALUE=\"Decline\">\n"
266 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
268 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
277 /* If this is a REPLY, display update button */
278 if (the_method == ICAL_METHOD_REPLY) {
281 * In the future, if we want to validate this object before
282 * continuing, we can do it this way:
283 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
288 /* Display the update buttons */
289 wprintf("<TR><TD COLSPAN=2>"
290 "Click <i>Update</i> to accept this reply and "
291 "update your calendar."
292 "<FORM METHOD=\"GET\" "
293 "ACTION=\"/handle_rsvp\">\n"
294 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
295 "VALUE=\"Update\">\n"
297 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
298 "VALUE=\"Ignore\">\n"
299 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
301 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
310 /* Trailing HTML for the display of this object */
311 if (recursion_level == 0) {
313 wprintf("</TR></TABLE></CENTER>\n");
319 * Deserialize a calendar object in a message so it can be processed.
320 * (This is the main entry point for these things)
322 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
325 cal = icalcomponent_new_from_string(part_source);
328 wprintf("Error parsing calendar object<BR>\n");
333 cal_process_object(cal, 0, msgnum, cal_partnum);
335 /* Free the memory we obtained from libical's constructor */
336 icalcomponent_free(cal);
343 * Respond to a meeting request
345 void respond_to_request(void) {
350 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
351 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
352 "<B>Respond to meeting request</B>"
353 "</FONT></TD></TR></TABLE><BR>\n"
356 serv_printf("ICAL respond|%s|%s|%s|",
364 wprintf("<TABLE BORDER=0><TR><TD>"
365 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
368 if (!strcasecmp(bstr("sc"), "accept")) {
369 wprintf("You have accepted this meeting invitation. "
370 "It has been entered into your calendar, "
372 } else if (!strcasecmp(bstr("sc"), "tentative")) {
373 wprintf("You have tentatively accepted this meeting invitation. "
374 "It has been 'pencilled in' to your calendar, "
376 } else if (!strcasecmp(bstr("sc"), "decline")) {
377 wprintf("You have declined this meeting invitation. "
378 "It has <b>not</b> been entered into your calendar, "
381 wprintf("and a reply has been sent to the meeting organizer."
382 "</TD></TR></TABLE>\n"
385 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
389 wprintf("<A HREF=\"/dotskip?room=");
390 urlescputs(WC->wc_roomname);
391 wprintf("\"><BR>Return to messages</A><BR>\n");
399 * Handle an incoming RSVP
401 void handle_rsvp(void) {
406 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
407 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
408 "<B>Update your calendar with this RSVP</B>"
409 "</FONT></TD></TR></TABLE><BR>\n"
412 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
420 wprintf("<TABLE BORDER=0><TR><TD>"
421 "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
424 if (!strcasecmp(bstr("sc"), "update")) {
425 wprintf("Your calendar has been updated "
426 "to reflect this RSVP."
428 } else if (!strcasecmp(bstr("sc"), "ignore")) {
429 wprintf("You have chosen to ignore this RSVP. "
430 "Your calendar has <b>not</b> been updated."
433 wprintf("</TD></TR></TABLE>\n"
436 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
440 wprintf("<A HREF=\"/dotskip?room=");
441 urlescputs(WC->wc_roomname);
442 wprintf("\"><BR>Return to messages</A><BR>\n");
450 /*****************************************************************************/
455 * Display handlers for message reading
461 * If we're reading calendar items, just store them for now. We have to
462 * sort and re-output them later when we draw the calendar.
464 void display_individual_cal(icalcomponent *cal, long msgnum) {
468 WC->disp_cal = realloc(WC->disp_cal,
469 (sizeof(icalcomponent *) * WC->num_cal) );
470 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
472 WC->cal_msgnum = realloc(WC->cal_msgnum,
473 (sizeof(long) * WC->num_cal) );
474 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
480 * Display a task in the task list
482 void display_individual_task(icalcomponent *vtodo, long msgnum) {
485 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
486 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld&taskrm=", msgnum);
487 urlescputs(WC->wc_roomname);
490 escputs((char *)icalproperty_get_comment(p));
497 * Display a task by itself (for editing)
499 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
500 icalcomponent *vtodo;
502 struct icaltimetype t;
504 int created_new_vtodo = 0;
508 if (supplied_vtodo != NULL) {
509 vtodo = supplied_vtodo;
512 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
513 created_new_vtodo = 1;
517 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
518 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
520 "</FONT></TD></TR></TABLE><BR>\n"
523 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
524 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
528 "<INPUT TYPE=\"text\" NAME=\"summary\" "
529 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
530 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
532 escputs((char *)icalproperty_get_comment(p));
534 wprintf("\"><BR>\n");
536 wprintf("Start date: ");
537 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
539 t = icalproperty_get_dtstart(p);
542 t = icaltime_from_timet(now, 0);
544 display_icaltimetype_as_webform(&t, "dtstart");
547 wprintf("Due date: ");
548 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
550 t = icalproperty_get_due(p);
553 t = icaltime_from_timet(now, 0);
555 display_icaltimetype_as_webform(&t, "due");
558 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
559 "ROWS=10 COLS=80 WIDTH=80>\n"
561 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
563 escputs((char *)icalproperty_get_comment(p));
565 wprintf("</TEXTAREA><BR>\n");
567 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
569 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
571 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
575 wprintf("</FORM>\n");
579 if (created_new_vtodo) {
580 icalcomponent_free(vtodo);
585 * Save an edited task
587 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
589 int delete_existing = 0;
591 icalcomponent *vtodo;
592 int created_new_vtodo = 0;
594 if (supplied_vtodo != NULL) {
595 vtodo = supplied_vtodo;
598 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
599 created_new_vtodo = 1;
602 if (!strcasecmp(bstr("sc"), "Save")) {
604 /* Replace values in the component with ones from the form */
606 while (prop = icalcomponent_get_first_property(vtodo,
607 ICAL_SUMMARY_PROPERTY), prop != NULL) {
608 icalcomponent_remove_property(vtodo, prop);
609 icalproperty_free(prop);
611 icalcomponent_add_property(vtodo,
612 icalproperty_new_summary(bstr("summary")));
614 while (prop = icalcomponent_get_first_property(vtodo,
615 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
616 icalcomponent_remove_property(vtodo, prop);
617 icalproperty_free(prop);
619 icalcomponent_add_property(vtodo,
620 icalproperty_new_description(bstr("description")));
622 while (prop = icalcomponent_get_first_property(vtodo,
623 ICAL_DTSTART_PROPERTY), prop != NULL) {
624 icalcomponent_remove_property(vtodo, prop);
625 icalproperty_free(prop);
627 icalcomponent_add_property(vtodo,
628 icalproperty_new_dtstart(
629 icaltime_from_webform("dtstart")
633 while (prop = icalcomponent_get_first_property(vtodo,
634 ICAL_DUE_PROPERTY), prop != NULL) {
635 icalcomponent_remove_property(vtodo, prop);
636 icalproperty_free(prop);
638 icalcomponent_add_property(vtodo,
639 icalproperty_new_due(
640 icaltime_from_webform("due")
644 /* Serialize it and save it to the message base */
645 serv_puts("ENT0 1|||4");
648 serv_puts("Content-type: text/calendar");
650 serv_puts(icalcomponent_as_ical_string(vtodo));
653 /* Probably not necessary; the server will see the UID
654 * of the object and delete the old one anyway, but
662 * If the user clicked 'Delete' then explicitly delete the message.
664 if (!strcasecmp(bstr("sc"), "Delete")) {
668 if ( (delete_existing) && (msgnum > 0L) ) {
669 serv_printf("DELE %ld", atol(bstr("msgnum")));
673 if (created_new_vtodo) {
674 icalcomponent_free(vtodo);
677 /* Go back to the task list */
684 * Code common to all display handlers. Given a message number and a MIME
685 * type, we load the message and hunt for that MIME type. If found, we load
686 * the relevant part, deserialize it into a libical component, filter it for
687 * the requested object type, and feed it to the specified handler.
689 void display_using_handler(long msgnum,
691 icalcomponent_kind which_kind,
692 void (*callback)(icalcomponent *, long)
695 char mime_partnum[SIZ];
696 char mime_filename[SIZ];
697 char mime_content_type[SIZ];
698 char mime_disposition[SIZ];
700 char relevant_partnum[SIZ];
701 char *relevant_source = NULL;
702 icalcomponent *cal, *c;
704 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
707 if (buf[0] != '1') return;
709 while (serv_gets(buf), strcmp(buf, "000")) {
710 if (!strncasecmp(buf, "part=", 5)) {
711 extract(mime_filename, &buf[5], 1);
712 extract(mime_partnum, &buf[5], 2);
713 extract(mime_disposition, &buf[5], 3);
714 extract(mime_content_type, &buf[5], 4);
715 mime_length = extract_int(&buf[5], 5);
717 if (!strcasecmp(mime_content_type, "text/calendar")) {
718 strcpy(relevant_partnum, mime_partnum);
724 if (strlen(relevant_partnum) > 0) {
725 relevant_source = load_mimepart(msgnum, relevant_partnum);
726 if (relevant_source != NULL) {
728 cal = icalcomponent_new_from_string(relevant_source);
733 /* Simple components of desired type */
734 if (icalcomponent_isa(cal) == which_kind) {
735 callback(cal, msgnum);
738 /* Subcomponents of desired type */
739 for (c = icalcomponent_get_first_component(cal,
742 c = icalcomponent_get_next_component(cal,
746 icalcomponent_free(cal);
748 free(relevant_source);
754 void display_calendar(long msgnum) {
755 display_using_handler(msgnum, "text/calendar",
756 ICAL_VEVENT_COMPONENT,
757 display_individual_cal);
760 void display_task(long msgnum) {
761 display_using_handler(msgnum, "text/calendar",
762 ICAL_VTODO_COMPONENT,
763 display_individual_task);
766 void display_edit_task(void) {
769 /* Force change the room if we have to */
770 if (strlen(bstr("taskrm")) > 0) {
771 gotoroom(bstr("taskrm"), 0);
774 msgnum = atol(bstr("msgnum"));
777 display_using_handler(msgnum, "text/calendar",
778 ICAL_VTODO_COMPONENT,
779 display_edit_individual_task);
783 display_edit_individual_task(NULL, 0L);
787 void save_task(void) {
790 msgnum = atol(bstr("msgnum"));
792 display_using_handler(msgnum, "text/calendar",
793 ICAL_VTODO_COMPONENT,
794 save_individual_task);
797 save_individual_task(NULL, 0L);
801 void display_edit_event(void) {
804 msgnum = atol(bstr("msgnum"));
807 display_using_handler(msgnum, "text/calendar",
808 ICAL_VEVENT_COMPONENT,
809 display_edit_individual_event);
813 display_edit_individual_event(NULL, 0L);
817 void save_event(void) {
820 msgnum = atol(bstr("msgnum"));
823 display_using_handler(msgnum, "text/calendar",
824 ICAL_VEVENT_COMPONENT,
825 save_individual_event);
828 save_individual_event(NULL, 0L);
832 #endif /* HAVE_ICAL_H */