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;
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>
109 wprintf("<TR><TD COLSPAN=2>"
110 "I don't know what to do with this.</TD></TR>"
116 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
118 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
119 escputs((char *)icalproperty_get_comment(p));
120 wprintf("</TD></TR>\n");
123 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
125 wprintf("<TR><TD><B>Location:</B></TD><TD>");
126 escputs((char *)icalproperty_get_comment(p));
127 wprintf("</TD></TR>\n");
131 * Only show start/end times if we're actually looking at the VEVENT
132 * component. Otherwise it shows bogus dates for things like timezone.
134 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
136 p = icalcomponent_get_first_property(cal,
137 ICAL_DTSTART_PROPERTY);
139 t = icalproperty_get_dtstart(p);
140 tt = icaltime_as_timet(t);
142 wprintf("<TR><TD><B>Starting date/time:</B></TD><TD>"
147 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
149 t = icalproperty_get_dtend(p);
150 tt = icaltime_as_timet(t);
152 wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
159 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
161 wprintf("<TR><TD><B>Description:</B></TD><TD>");
162 escputs((char *)icalproperty_get_comment(p));
163 wprintf("</TD></TR>\n");
166 /* If the component has subcomponents, recurse through them. */
167 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
169 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
170 /* Recursively process subcomponent */
171 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
174 /* Trailing HTML for the display of this object */
175 if (recursion_level == 0) {
176 wprintf("<TR><TD COLSPAN=2>"
177 "<FORM METHOD=\"GET\" "
178 "ACTION=\"/respond_to_request\">\n"
179 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
180 "VALUE=\"Accept\">\n"
182 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
183 "VALUE=\"Tentative\">\n"
185 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
186 "VALUE=\"Decline\">\n"
187 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
189 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
192 "</TD></TR></TABLE></CENTER>\n",
200 * Back end for cal_add() -- this writes it to the message base
202 void pencil_it_in(icalcomponent *cal) {
205 char *serialized_event;
207 /* Save the name of the room we're in */
208 strcpy(hold_rm, WC->wc_roomname);
210 /* Go find the user's calendar */
211 serv_printf("GOTO %s", CALENDAR_ROOM_NAME);
213 if (buf[0] != '2') return;
215 /* Enter the message */
216 serialized_event = icalcomponent_as_ical_string(cal);
217 if (serialized_event != NULL) {
218 sprintf(buf, "ENT0 1|||4||");
222 serv_puts("Content-type: text/calendar");
224 serv_write(serialized_event, strlen(serialized_event));
230 /* Return to the room we were in */
231 serv_printf("GOTO %s", hold_rm);
237 * Add a calendar object to the user's calendar
239 void cal_add(icalcomponent *cal, int recursion_level, int tentative) {
243 * The VEVENT subcomponent is the one we're interested in saving.
245 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
246 /* Save to the message base */
251 /* If the component has subcomponents, recurse through them. */
252 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
254 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
255 /* Recursively process subcomponent */
256 cal_add(c, recursion_level+1, tentative);
263 * Deserialize a calendar object in a message so it can be processed.
264 * (This is the main entry point for these things)
266 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
269 cal = icalcomponent_new_from_string(part_source);
272 wprintf("Error parsing calendar object: %s<BR>\n",
273 icalerror_strerror(icalerrno));
277 cal_process_object(cal, 0, msgnum, cal_partnum);
279 /* Free the memory we obtained from libical's constructor */
280 icalcomponent_free(cal);
287 * Respond to a meeting request
289 void respond_to_request(void) {
292 char *serialized_cal;
297 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
298 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
299 "<B>Respond to meeting request</B>"
300 "</FONT></TD></TR></TABLE><BR>\n"
303 sprintf(buf, "OPNA %s|%s", bstr("msgnum"), bstr("cal_partnum"));
307 wprintf("Error: %s<BR>\n", &buf[4]);
312 total_len = atoi(&buf[4]);
313 serialized_cal = malloc(total_len + 1);
315 read_server_binary(serialized_cal, total_len);
319 serialized_cal[total_len + 1] = 0;
322 cal = icalcomponent_new_from_string(serialized_cal);
323 free(serialized_cal);
326 wprintf("Error parsing calendar object: %s<BR>\n",
327 icalerror_strerror(icalerrno));
332 /* Save this in the user's calendar if necessary */
333 if (!strcasecmp(bstr("sc"), "Accept")) {
336 if (!strcasecmp(bstr("sc"), "Tentative")) {
340 /* Send a reply if necessary */
341 /* FIXME ... do this */
343 /* Free the memory we obtained from libical's constructor */
344 icalcomponent_free(cal);
346 /* Delete the message from the inbox */
347 /* FIXME ... do this */
350 wprintf("Done!<BR>\n");
352 /* ...and now we're done. */
359 /*****************************************************************************/
364 * Display handlers for message reading
370 * If we're reading calendar items, just store them for now. We have to
371 * sort and re-output them later when we draw the calendar.
373 void display_individual_cal(icalcomponent *cal, long msgnum) {
377 WC->disp_cal = realloc(WC->disp_cal,
378 (sizeof(icalcomponent *) * WC->num_cal) );
379 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
381 WC->cal_msgnum = realloc(WC->cal_msgnum,
382 (sizeof(long) * WC->num_cal) );
383 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
389 * Display a task in the task list
391 void display_individual_task(icalcomponent *vtodo, long msgnum) {
394 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
395 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
397 escputs((char *)icalproperty_get_comment(p));
404 * Display a task by itself (for editing)
406 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
407 icalcomponent *vtodo;
409 struct icaltimetype t;
411 int created_new_vtodo = 0;
415 if (supplied_vtodo != NULL) {
416 vtodo = supplied_vtodo;
419 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
420 created_new_vtodo = 1;
424 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
425 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
427 "</FONT></TD></TR></TABLE><BR>\n"
430 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
431 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
435 "<INPUT TYPE=\"text\" NAME=\"summary\" "
436 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
437 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
439 escputs((char *)icalproperty_get_comment(p));
441 wprintf("\"><BR>\n");
443 wprintf("Start date: ");
444 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
446 t = icalproperty_get_dtstart(p);
449 t = icaltime_from_timet(now, 0);
451 display_icaltimetype_as_webform(&t, "dtstart");
454 wprintf("Due date: ");
455 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
457 t = icalproperty_get_due(p);
460 t = icaltime_from_timet(now, 0);
462 display_icaltimetype_as_webform(&t, "due");
465 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
466 "ROWS=10 COLS=80 WIDTH=80>\n"
468 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
470 escputs((char *)icalproperty_get_comment(p));
472 wprintf("</TEXTAREA><BR>\n");
474 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
476 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
478 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
482 wprintf("</FORM>\n");
486 if (created_new_vtodo) {
487 icalcomponent_free(vtodo);
492 * Save an edited task
494 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
496 int delete_existing = 0;
498 icalcomponent *vtodo;
499 int created_new_vtodo = 0;
501 if (supplied_vtodo != NULL) {
502 vtodo = supplied_vtodo;
505 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
506 created_new_vtodo = 1;
509 if (!strcasecmp(bstr("sc"), "Save")) {
511 /* Replace values in the component with ones from the form */
513 while (prop = icalcomponent_get_first_property(vtodo,
514 ICAL_SUMMARY_PROPERTY), prop != NULL) {
515 icalcomponent_remove_property(vtodo, prop);
517 icalcomponent_add_property(vtodo,
518 icalproperty_new_summary(bstr("summary")));
520 while (prop = icalcomponent_get_first_property(vtodo,
521 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
522 icalcomponent_remove_property(vtodo, prop);
524 icalcomponent_add_property(vtodo,
525 icalproperty_new_description(bstr("description")));
527 while (prop = icalcomponent_get_first_property(vtodo,
528 ICAL_DTSTART_PROPERTY), prop != NULL) {
529 icalcomponent_remove_property(vtodo, prop);
531 icalcomponent_add_property(vtodo,
532 icalproperty_new_dtstart(
533 icaltime_from_webform("dtstart")
537 while (prop = icalcomponent_get_first_property(vtodo,
538 ICAL_DUE_PROPERTY), prop != NULL) {
539 icalcomponent_remove_property(vtodo, prop);
541 icalcomponent_add_property(vtodo,
542 icalproperty_new_due(
543 icaltime_from_webform("due")
547 /* Serialize it and save it to the message base */
548 serv_puts("ENT0 1|||4");
551 serv_puts("Content-type: text/calendar");
553 serv_puts(icalcomponent_as_ical_string(vtodo));
560 * If the user clicked 'Delete' then delete it, period.
562 if (!strcasecmp(bstr("sc"), "Delete")) {
566 if ( (delete_existing) && (msgnum > 0L) ) {
567 serv_printf("DELE %ld", atol(bstr("msgnum")));
571 if (created_new_vtodo) {
572 icalcomponent_free(vtodo);
575 /* Go back to the task list */
582 * Code common to all display handlers. Given a message number and a MIME
583 * type, we load the message and hunt for that MIME type. If found, we load
584 * the relevant part, deserialize it into a libical component, filter it for
585 * the requested object type, and feed it to the specified handler.
587 void display_using_handler(long msgnum,
589 icalcomponent_kind which_kind,
590 void (*callback)(icalcomponent *, long)
593 char mime_partnum[SIZ];
594 char mime_filename[SIZ];
595 char mime_content_type[SIZ];
596 char mime_disposition[SIZ];
598 char relevant_partnum[SIZ];
599 char *relevant_source = NULL;
600 icalcomponent *cal, *c;
602 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
605 if (buf[0] != '1') return;
607 while (serv_gets(buf), strcmp(buf, "000")) {
608 if (!strncasecmp(buf, "part=", 5)) {
609 extract(mime_filename, &buf[5], 1);
610 extract(mime_partnum, &buf[5], 2);
611 extract(mime_disposition, &buf[5], 3);
612 extract(mime_content_type, &buf[5], 4);
613 mime_length = extract_int(&buf[5], 5);
615 if (!strcasecmp(mime_content_type, "text/calendar")) {
616 strcpy(relevant_partnum, mime_partnum);
622 if (strlen(relevant_partnum) > 0) {
623 relevant_source = load_mimepart(msgnum, relevant_partnum);
624 if (relevant_source != NULL) {
626 cal = icalcomponent_new_from_string(relevant_source);
629 /* Simple components of desired type */
630 if (icalcomponent_isa(cal) == which_kind) {
631 callback(cal, msgnum);
634 /* Subcomponents of desired type */
635 for (c = icalcomponent_get_first_component(cal,
638 c = icalcomponent_get_next_component(cal,
642 icalcomponent_free(cal);
644 free(relevant_source);
650 void display_calendar(long msgnum) {
651 display_using_handler(msgnum, "text/calendar",
652 ICAL_VEVENT_COMPONENT,
653 display_individual_cal);
656 void display_task(long msgnum) {
657 display_using_handler(msgnum, "text/calendar",
658 ICAL_VTODO_COMPONENT,
659 display_individual_task);
662 void display_edit_task(void) {
665 msgnum = atol(bstr("msgnum"));
668 display_using_handler(msgnum, "text/calendar",
669 ICAL_VTODO_COMPONENT,
670 display_edit_individual_task);
674 display_edit_individual_task(NULL, 0L);
678 void save_task(void) {
681 msgnum = atol(bstr("msgnum"));
683 display_using_handler(msgnum, "text/calendar",
684 ICAL_VTODO_COMPONENT,
685 save_individual_task);
688 save_individual_task(NULL, 0L);
692 void display_edit_event(void) {
695 msgnum = atol(bstr("msgnum"));
698 display_using_handler(msgnum, "text/calendar",
699 ICAL_VEVENT_COMPONENT,
700 display_edit_individual_event);
704 display_edit_individual_event(NULL, 0L);
708 void save_event(void) {
711 msgnum = atol(bstr("msgnum"));
714 display_using_handler(msgnum, "text/calendar",
715 ICAL_VEVENT_COMPONENT,
716 save_individual_event);
719 save_individual_event(NULL, 0L);
723 #endif /* HAVE_ICAL_H */