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\">", msgnum);
488 escputs((char *)icalproperty_get_comment(p));
495 * Display a task by itself (for editing)
497 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
498 icalcomponent *vtodo;
500 struct icaltimetype t;
502 int created_new_vtodo = 0;
506 if (supplied_vtodo != NULL) {
507 vtodo = supplied_vtodo;
510 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
511 created_new_vtodo = 1;
515 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
516 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
518 "</FONT></TD></TR></TABLE><BR>\n"
521 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
522 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
526 "<INPUT TYPE=\"text\" NAME=\"summary\" "
527 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
528 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
530 escputs((char *)icalproperty_get_comment(p));
532 wprintf("\"><BR>\n");
534 wprintf("Start date: ");
535 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
537 t = icalproperty_get_dtstart(p);
540 t = icaltime_from_timet(now, 0);
542 display_icaltimetype_as_webform(&t, "dtstart");
545 wprintf("Due date: ");
546 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
548 t = icalproperty_get_due(p);
551 t = icaltime_from_timet(now, 0);
553 display_icaltimetype_as_webform(&t, "due");
556 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
557 "ROWS=10 COLS=80 WIDTH=80>\n"
559 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
561 escputs((char *)icalproperty_get_comment(p));
563 wprintf("</TEXTAREA><BR>\n");
565 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
567 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
569 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
573 wprintf("</FORM>\n");
577 if (created_new_vtodo) {
578 icalcomponent_free(vtodo);
583 * Save an edited task
585 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
587 int delete_existing = 0;
589 icalcomponent *vtodo;
590 int created_new_vtodo = 0;
592 if (supplied_vtodo != NULL) {
593 vtodo = supplied_vtodo;
596 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
597 created_new_vtodo = 1;
600 if (!strcasecmp(bstr("sc"), "Save")) {
602 /* Replace values in the component with ones from the form */
604 while (prop = icalcomponent_get_first_property(vtodo,
605 ICAL_SUMMARY_PROPERTY), prop != NULL) {
606 icalcomponent_remove_property(vtodo, prop);
607 icalproperty_free(prop);
609 icalcomponent_add_property(vtodo,
610 icalproperty_new_summary(bstr("summary")));
612 while (prop = icalcomponent_get_first_property(vtodo,
613 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
614 icalcomponent_remove_property(vtodo, prop);
615 icalproperty_free(prop);
617 icalcomponent_add_property(vtodo,
618 icalproperty_new_description(bstr("description")));
620 while (prop = icalcomponent_get_first_property(vtodo,
621 ICAL_DTSTART_PROPERTY), prop != NULL) {
622 icalcomponent_remove_property(vtodo, prop);
623 icalproperty_free(prop);
625 icalcomponent_add_property(vtodo,
626 icalproperty_new_dtstart(
627 icaltime_from_webform("dtstart")
631 while (prop = icalcomponent_get_first_property(vtodo,
632 ICAL_DUE_PROPERTY), prop != NULL) {
633 icalcomponent_remove_property(vtodo, prop);
634 icalproperty_free(prop);
636 icalcomponent_add_property(vtodo,
637 icalproperty_new_due(
638 icaltime_from_webform("due")
642 /* Serialize it and save it to the message base */
643 serv_puts("ENT0 1|||4");
646 serv_puts("Content-type: text/calendar");
648 serv_puts(icalcomponent_as_ical_string(vtodo));
651 /* Probably not necessary; the server will see the UID
652 * of the object and delete the old one anyway, but
660 * If the user clicked 'Delete' then explicitly delete the message.
662 if (!strcasecmp(bstr("sc"), "Delete")) {
666 if ( (delete_existing) && (msgnum > 0L) ) {
667 serv_printf("DELE %ld", atol(bstr("msgnum")));
671 if (created_new_vtodo) {
672 icalcomponent_free(vtodo);
675 /* Go back to the task list */
682 * Code common to all display handlers. Given a message number and a MIME
683 * type, we load the message and hunt for that MIME type. If found, we load
684 * the relevant part, deserialize it into a libical component, filter it for
685 * the requested object type, and feed it to the specified handler.
687 void display_using_handler(long msgnum,
689 icalcomponent_kind which_kind,
690 void (*callback)(icalcomponent *, long)
693 char mime_partnum[SIZ];
694 char mime_filename[SIZ];
695 char mime_content_type[SIZ];
696 char mime_disposition[SIZ];
698 char relevant_partnum[SIZ];
699 char *relevant_source = NULL;
700 icalcomponent *cal, *c;
702 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
705 if (buf[0] != '1') return;
707 while (serv_gets(buf), strcmp(buf, "000")) {
708 if (!strncasecmp(buf, "part=", 5)) {
709 extract(mime_filename, &buf[5], 1);
710 extract(mime_partnum, &buf[5], 2);
711 extract(mime_disposition, &buf[5], 3);
712 extract(mime_content_type, &buf[5], 4);
713 mime_length = extract_int(&buf[5], 5);
715 if (!strcasecmp(mime_content_type, "text/calendar")) {
716 strcpy(relevant_partnum, mime_partnum);
722 if (strlen(relevant_partnum) > 0) {
723 relevant_source = load_mimepart(msgnum, relevant_partnum);
724 if (relevant_source != NULL) {
726 cal = icalcomponent_new_from_string(relevant_source);
731 /* Simple components of desired type */
732 if (icalcomponent_isa(cal) == which_kind) {
733 callback(cal, msgnum);
736 /* Subcomponents of desired type */
737 for (c = icalcomponent_get_first_component(cal,
740 c = icalcomponent_get_next_component(cal,
744 icalcomponent_free(cal);
746 free(relevant_source);
752 void display_calendar(long msgnum) {
753 display_using_handler(msgnum, "text/calendar",
754 ICAL_VEVENT_COMPONENT,
755 display_individual_cal);
758 void display_task(long msgnum) {
759 display_using_handler(msgnum, "text/calendar",
760 ICAL_VTODO_COMPONENT,
761 display_individual_task);
764 void display_edit_task(void) {
767 msgnum = atol(bstr("msgnum"));
770 display_using_handler(msgnum, "text/calendar",
771 ICAL_VTODO_COMPONENT,
772 display_edit_individual_task);
776 display_edit_individual_task(NULL, 0L);
780 void save_task(void) {
783 msgnum = atol(bstr("msgnum"));
785 display_using_handler(msgnum, "text/calendar",
786 ICAL_VTODO_COMPONENT,
787 save_individual_task);
790 save_individual_task(NULL, 0L);
794 void display_edit_event(void) {
797 msgnum = atol(bstr("msgnum"));
800 display_using_handler(msgnum, "text/calendar",
801 ICAL_VEVENT_COMPONENT,
802 display_edit_individual_event);
806 display_edit_individual_event(NULL, 0L);
810 void save_event(void) {
813 msgnum = atol(bstr("msgnum"));
816 display_using_handler(msgnum, "text/calendar",
817 ICAL_VEVENT_COMPONENT,
818 save_individual_event);
821 save_individual_event(NULL, 0L);
825 #endif /* HAVE_ICAL_H */