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;
87 /* Leading HTML for the display of this object */
88 if (recursion_level == 0) {
89 wprintf("<CENTER><TABLE border=0 cellpadding=5>\n");
92 /* Look for a method */
93 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
95 /* See what we need to do with this */
97 the_method = icalproperty_get_method(method);
99 case ICAL_METHOD_REQUEST:
100 wprintf("<TR><TD COLSPAN=2>\n"
102 "SRC=\"/static/vcalendar.gif\">"
104 "<B>Meeting invitation</B>
108 case ICAL_METHOD_PUBLISH:
109 wprintf("<TR><TD COLSPAN=2>\n"
111 "SRC=\"/static/vcalendar.gif\">"
113 "<B>Published event</B>
118 wprintf("<TR><TD COLSPAN=2>"
119 "I don't know what to do with this.</TD></TR>"
125 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
127 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
128 escputs((char *)icalproperty_get_comment(p));
129 wprintf("</TD></TR>\n");
132 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
134 wprintf("<TR><TD><B>Location:</B></TD><TD>");
135 escputs((char *)icalproperty_get_comment(p));
136 wprintf("</TD></TR>\n");
140 * Only show start/end times if we're actually looking at the VEVENT
141 * component. Otherwise it shows bogus dates for things like timezone.
143 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
145 p = icalcomponent_get_first_property(cal,
146 ICAL_DTSTART_PROPERTY);
148 t = icalproperty_get_dtstart(p);
149 tt = icaltime_as_timet(t);
151 wprintf("<TR><TD><B>Starting date/time:</B></TD><TD>"
156 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
158 t = icalproperty_get_dtend(p);
159 tt = icaltime_as_timet(t);
161 wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
168 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
170 wprintf("<TR><TD><B>Description:</B></TD><TD>");
171 escputs((char *)icalproperty_get_comment(p));
172 wprintf("</TD></TR>\n");
175 /* If the component has subcomponents, recurse through them. */
176 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
178 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
179 /* Recursively process subcomponent */
180 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
183 /* If this is a REQUEST, display the Accept/Decline buttons */
184 if (the_method == ICAL_METHOD_REQUEST) {
185 wprintf("<TR><TD COLSPAN=2>"
186 "<FORM METHOD=\"GET\" "
187 "ACTION=\"/respond_to_request\">\n"
188 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
189 "VALUE=\"Accept\">\n"
191 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
192 "VALUE=\"Tentative\">\n"
194 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
195 "VALUE=\"Decline\">\n"
196 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
198 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
206 /* Trailing HTML for the display of this object */
207 if (recursion_level == 0) {
208 wprintf("</TD></TR></TABLE></CENTER>\n");
214 * Back end for cal_add() -- this writes it to the message base
216 void pencil_it_in(icalcomponent *cal) {
219 char *serialized_event;
221 /* Save the name of the room we're in */
222 strcpy(hold_rm, WC->wc_roomname);
224 /* Go find the user's calendar */
225 serv_printf("GOTO %s", CALENDAR_ROOM_NAME);
227 if (buf[0] != '2') return;
229 /* Enter the message */
230 serialized_event = icalcomponent_as_ical_string(cal);
231 if (serialized_event != NULL) {
232 sprintf(buf, "ENT0 1|||4||");
236 serv_puts("Content-type: text/calendar");
238 serv_write(serialized_event, strlen(serialized_event));
244 /* Return to the room we were in */
245 serv_printf("GOTO %s", hold_rm);
251 * Add a calendar object to the user's calendar
253 void cal_add(icalcomponent *cal, int recursion_level, int tentative) {
257 * The VEVENT subcomponent is the one we're interested in saving.
259 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
260 /* Save to the message base */
265 /* If the component has subcomponents, recurse through them. */
266 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
268 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
269 /* Recursively process subcomponent */
270 cal_add(c, recursion_level+1, tentative);
277 * Deserialize a calendar object in a message so it can be processed.
278 * (This is the main entry point for these things)
280 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
283 cal = icalcomponent_new_from_string(part_source);
286 wprintf("Error parsing calendar object: %s<BR>\n",
287 icalerror_strerror(icalerrno));
291 cal_process_object(cal, 0, msgnum, cal_partnum);
293 /* Free the memory we obtained from libical's constructor */
294 icalcomponent_free(cal);
301 * Respond to a meeting request
303 void respond_to_request(void) {
306 char *serialized_cal;
311 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
312 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
313 "<B>Respond to meeting request</B>"
314 "</FONT></TD></TR></TABLE><BR>\n"
317 sprintf(buf, "OPNA %s|%s", bstr("msgnum"), bstr("cal_partnum"));
321 wprintf("Error: %s<BR>\n", &buf[4]);
326 total_len = atoi(&buf[4]);
327 serialized_cal = malloc(total_len + 1);
329 read_server_binary(serialized_cal, total_len);
333 serialized_cal[total_len + 1] = 0;
336 cal = icalcomponent_new_from_string(serialized_cal);
337 free(serialized_cal);
340 wprintf("Error parsing calendar object: %s<BR>\n",
341 icalerror_strerror(icalerrno));
346 /* Save this in the user's calendar if necessary */
347 if (!strcasecmp(bstr("sc"), "Accept")) {
350 if (!strcasecmp(bstr("sc"), "Tentative")) {
354 /* Send a reply if necessary */
355 /* FIXME ... do this */
357 /* Free the memory we obtained from libical's constructor */
358 icalcomponent_free(cal);
360 /* Delete the message from the inbox */
361 /* FIXME ... do this */
364 wprintf("Done!<BR>\n");
366 /* ...and now we're done. */
373 /*****************************************************************************/
378 * Display handlers for message reading
384 * If we're reading calendar items, just store them for now. We have to
385 * sort and re-output them later when we draw the calendar.
387 void display_individual_cal(icalcomponent *cal, long msgnum) {
391 WC->disp_cal = realloc(WC->disp_cal,
392 (sizeof(icalcomponent *) * WC->num_cal) );
393 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
395 WC->cal_msgnum = realloc(WC->cal_msgnum,
396 (sizeof(long) * WC->num_cal) );
397 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
403 * Display a task in the task list
405 void display_individual_task(icalcomponent *vtodo, long msgnum) {
408 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
409 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
411 escputs((char *)icalproperty_get_comment(p));
418 * Display a task by itself (for editing)
420 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
421 icalcomponent *vtodo;
423 struct icaltimetype t;
425 int created_new_vtodo = 0;
429 if (supplied_vtodo != NULL) {
430 vtodo = supplied_vtodo;
433 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
434 created_new_vtodo = 1;
438 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
439 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
441 "</FONT></TD></TR></TABLE><BR>\n"
444 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
445 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
449 "<INPUT TYPE=\"text\" NAME=\"summary\" "
450 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
451 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
453 escputs((char *)icalproperty_get_comment(p));
455 wprintf("\"><BR>\n");
457 wprintf("Start date: ");
458 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
460 t = icalproperty_get_dtstart(p);
463 t = icaltime_from_timet(now, 0);
465 display_icaltimetype_as_webform(&t, "dtstart");
468 wprintf("Due date: ");
469 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
471 t = icalproperty_get_due(p);
474 t = icaltime_from_timet(now, 0);
476 display_icaltimetype_as_webform(&t, "due");
479 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
480 "ROWS=10 COLS=80 WIDTH=80>\n"
482 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
484 escputs((char *)icalproperty_get_comment(p));
486 wprintf("</TEXTAREA><BR>\n");
488 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
490 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
492 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
496 wprintf("</FORM>\n");
500 if (created_new_vtodo) {
501 icalcomponent_free(vtodo);
506 * Save an edited task
508 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
510 int delete_existing = 0;
512 icalcomponent *vtodo;
513 int created_new_vtodo = 0;
515 if (supplied_vtodo != NULL) {
516 vtodo = supplied_vtodo;
519 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
520 created_new_vtodo = 1;
523 if (!strcasecmp(bstr("sc"), "Save")) {
525 /* Replace values in the component with ones from the form */
527 while (prop = icalcomponent_get_first_property(vtodo,
528 ICAL_SUMMARY_PROPERTY), prop != NULL) {
529 icalcomponent_remove_property(vtodo, prop);
531 icalcomponent_add_property(vtodo,
532 icalproperty_new_summary(bstr("summary")));
534 while (prop = icalcomponent_get_first_property(vtodo,
535 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
536 icalcomponent_remove_property(vtodo, prop);
538 icalcomponent_add_property(vtodo,
539 icalproperty_new_description(bstr("description")));
541 while (prop = icalcomponent_get_first_property(vtodo,
542 ICAL_DTSTART_PROPERTY), prop != NULL) {
543 icalcomponent_remove_property(vtodo, prop);
545 icalcomponent_add_property(vtodo,
546 icalproperty_new_dtstart(
547 icaltime_from_webform("dtstart")
551 while (prop = icalcomponent_get_first_property(vtodo,
552 ICAL_DUE_PROPERTY), prop != NULL) {
553 icalcomponent_remove_property(vtodo, prop);
555 icalcomponent_add_property(vtodo,
556 icalproperty_new_due(
557 icaltime_from_webform("due")
561 /* Serialize it and save it to the message base */
562 serv_puts("ENT0 1|||4");
565 serv_puts("Content-type: text/calendar");
567 serv_puts(icalcomponent_as_ical_string(vtodo));
574 * If the user clicked 'Delete' then delete it, period.
576 if (!strcasecmp(bstr("sc"), "Delete")) {
580 if ( (delete_existing) && (msgnum > 0L) ) {
581 serv_printf("DELE %ld", atol(bstr("msgnum")));
585 if (created_new_vtodo) {
586 icalcomponent_free(vtodo);
589 /* Go back to the task list */
596 * Code common to all display handlers. Given a message number and a MIME
597 * type, we load the message and hunt for that MIME type. If found, we load
598 * the relevant part, deserialize it into a libical component, filter it for
599 * the requested object type, and feed it to the specified handler.
601 void display_using_handler(long msgnum,
603 icalcomponent_kind which_kind,
604 void (*callback)(icalcomponent *, long)
607 char mime_partnum[SIZ];
608 char mime_filename[SIZ];
609 char mime_content_type[SIZ];
610 char mime_disposition[SIZ];
612 char relevant_partnum[SIZ];
613 char *relevant_source = NULL;
614 icalcomponent *cal, *c;
616 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
619 if (buf[0] != '1') return;
621 while (serv_gets(buf), strcmp(buf, "000")) {
622 if (!strncasecmp(buf, "part=", 5)) {
623 extract(mime_filename, &buf[5], 1);
624 extract(mime_partnum, &buf[5], 2);
625 extract(mime_disposition, &buf[5], 3);
626 extract(mime_content_type, &buf[5], 4);
627 mime_length = extract_int(&buf[5], 5);
629 if (!strcasecmp(mime_content_type, "text/calendar")) {
630 strcpy(relevant_partnum, mime_partnum);
636 if (strlen(relevant_partnum) > 0) {
637 relevant_source = load_mimepart(msgnum, relevant_partnum);
638 if (relevant_source != NULL) {
640 cal = icalcomponent_new_from_string(relevant_source);
643 /* Simple components of desired type */
644 if (icalcomponent_isa(cal) == which_kind) {
645 callback(cal, msgnum);
648 /* Subcomponents of desired type */
649 for (c = icalcomponent_get_first_component(cal,
652 c = icalcomponent_get_next_component(cal,
656 icalcomponent_free(cal);
658 free(relevant_source);
664 void display_calendar(long msgnum) {
665 display_using_handler(msgnum, "text/calendar",
666 ICAL_VEVENT_COMPONENT,
667 display_individual_cal);
670 void display_task(long msgnum) {
671 display_using_handler(msgnum, "text/calendar",
672 ICAL_VTODO_COMPONENT,
673 display_individual_task);
676 void display_edit_task(void) {
679 msgnum = atol(bstr("msgnum"));
682 display_using_handler(msgnum, "text/calendar",
683 ICAL_VTODO_COMPONENT,
684 display_edit_individual_task);
688 display_edit_individual_task(NULL, 0L);
692 void save_task(void) {
695 msgnum = atol(bstr("msgnum"));
697 display_using_handler(msgnum, "text/calendar",
698 ICAL_VTODO_COMPONENT,
699 save_individual_task);
702 save_individual_task(NULL, 0L);
706 void display_edit_event(void) {
709 msgnum = atol(bstr("msgnum"));
712 display_using_handler(msgnum, "text/calendar",
713 ICAL_VEVENT_COMPONENT,
714 display_edit_individual_event);
718 display_edit_individual_event(NULL, 0L);
722 void save_event(void) {
725 msgnum = atol(bstr("msgnum"));
728 display_using_handler(msgnum, "text/calendar",
729 ICAL_VEVENT_COMPONENT,
730 save_individual_event);
733 save_individual_event(NULL, 0L);
737 #endif /* HAVE_ICAL_H */