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=\"Decline\">\n"
184 "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
186 "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
189 "</TD></TR></TABLE></CENTER>\n",
197 * Back end for cal_add() -- this writes it to the message base
199 void pencil_it_in(icalcomponent *cal) {
202 char *serialized_event;
204 /* Save the name of the room we're in */
205 strcpy(hold_rm, WC->wc_roomname);
207 /* Go find the user's calendar */
208 serv_printf("GOTO %s", CALENDAR_ROOM_NAME);
210 if (buf[0] != '2') return;
212 /* Enter the message */
213 serialized_event = icalcomponent_as_ical_string(cal);
214 if (serialized_event != NULL) {
215 sprintf(buf, "ENT0 1|||4||");
219 serv_puts("Content-type: text/calendar");
221 serv_write(serialized_event, strlen(serialized_event));
227 /* Return to the room we were in */
228 serv_printf("GOTO %s", hold_rm);
234 * Add a calendar object to the user's calendar
236 void cal_add(icalcomponent *cal, int recursion_level) {
240 * The VEVENT subcomponent is the one we're interested in saving.
242 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
243 /* Save to the message base */
248 /* If the component has subcomponents, recurse through them. */
249 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
251 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
252 /* Recursively process subcomponent */
253 cal_add(c, recursion_level+1);
260 * Deserialize a calendar object in a message so it can be processed.
261 * (This is the main entry point for these things)
263 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
266 cal = icalcomponent_new_from_string(part_source);
269 wprintf("Error parsing calendar object: %s<BR>\n",
270 icalerror_strerror(icalerrno));
274 cal_process_object(cal, 0, msgnum, cal_partnum);
276 /* Free the memory we obtained from libical's constructor */
277 icalcomponent_free(cal);
284 * Respond to a meeting request
286 void respond_to_request(void) {
289 char *serialized_cal;
294 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
295 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
296 "<B>Respond to meeting request</B>"
297 "</FONT></TD></TR></TABLE><BR>\n"
300 sprintf(buf, "OPNA %s|%s", bstr("msgnum"), bstr("cal_partnum"));
304 wprintf("Error: %s<BR>\n", &buf[4]);
309 total_len = atoi(&buf[4]);
310 serialized_cal = malloc(total_len + 1);
312 read_server_binary(serialized_cal, total_len);
316 serialized_cal[total_len + 1] = 0;
319 cal = icalcomponent_new_from_string(serialized_cal);
320 free(serialized_cal);
323 wprintf("Error parsing calendar object: %s<BR>\n",
324 icalerror_strerror(icalerrno));
329 /* Save this in the user's calendar if necessary */
330 if (!strcasecmp(bstr("sc"), "Accept")) {
334 /* Send a reply if necessary */
335 /* FIXME ... do this */
337 /* Free the memory we obtained from libical's constructor */
338 icalcomponent_free(cal);
340 /* Delete the message from the inbox */
341 /* FIXME ... do this */
344 wprintf("Done!<BR>\n");
346 /* ...and now we're done. */
353 /*****************************************************************************/
358 * Display handlers for message reading
364 * If we're reading calendar items, just store them for now. We have to
365 * sort and re-output them later when we draw the calendar.
367 void display_individual_cal(icalcomponent *cal, long msgnum) {
371 WC->disp_cal = realloc(WC->disp_cal,
372 (sizeof(icalcomponent *) * WC->num_cal) );
373 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
375 WC->cal_msgnum = realloc(WC->cal_msgnum,
376 (sizeof(long) * WC->num_cal) );
377 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
383 * Display a task in the task list
385 void display_individual_task(icalcomponent *vtodo, long msgnum) {
388 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
389 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
391 escputs((char *)icalproperty_get_comment(p));
398 * Display a task by itself (for editing)
400 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
401 icalcomponent *vtodo;
403 struct icaltimetype t;
405 int created_new_vtodo = 0;
409 if (supplied_vtodo != NULL) {
410 vtodo = supplied_vtodo;
413 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
414 created_new_vtodo = 1;
418 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
419 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
421 "</FONT></TD></TR></TABLE><BR>\n"
424 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
425 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
429 "<INPUT TYPE=\"text\" NAME=\"summary\" "
430 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
431 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
433 escputs((char *)icalproperty_get_comment(p));
435 wprintf("\"><BR>\n");
437 wprintf("Start date: ");
438 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
440 t = icalproperty_get_dtstart(p);
443 t = icaltime_from_timet(now, 0);
445 display_icaltimetype_as_webform(&t, "dtstart");
448 wprintf("Due date: ");
449 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
451 t = icalproperty_get_due(p);
454 t = icaltime_from_timet(now, 0);
456 display_icaltimetype_as_webform(&t, "due");
459 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
460 "ROWS=10 COLS=80 WIDTH=80>\n"
462 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
464 escputs((char *)icalproperty_get_comment(p));
466 wprintf("</TEXTAREA><BR>\n");
468 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
470 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
472 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
476 wprintf("</FORM>\n");
480 if (created_new_vtodo) {
481 icalcomponent_free(vtodo);
486 * Save an edited task
488 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
490 int delete_existing = 0;
492 icalcomponent *vtodo;
493 int created_new_vtodo = 0;
495 if (supplied_vtodo != NULL) {
496 vtodo = supplied_vtodo;
499 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
500 created_new_vtodo = 1;
503 if (!strcasecmp(bstr("sc"), "Save")) {
505 /* Replace values in the component with ones from the form */
507 while (prop = icalcomponent_get_first_property(vtodo,
508 ICAL_SUMMARY_PROPERTY), prop != NULL) {
509 icalcomponent_remove_property(vtodo, prop);
511 icalcomponent_add_property(vtodo,
512 icalproperty_new_summary(bstr("summary")));
514 while (prop = icalcomponent_get_first_property(vtodo,
515 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
516 icalcomponent_remove_property(vtodo, prop);
518 icalcomponent_add_property(vtodo,
519 icalproperty_new_description(bstr("description")));
521 while (prop = icalcomponent_get_first_property(vtodo,
522 ICAL_DTSTART_PROPERTY), prop != NULL) {
523 icalcomponent_remove_property(vtodo, prop);
525 icalcomponent_add_property(vtodo,
526 icalproperty_new_dtstart(
527 icaltime_from_webform("dtstart")
531 while (prop = icalcomponent_get_first_property(vtodo,
532 ICAL_DUE_PROPERTY), prop != NULL) {
533 icalcomponent_remove_property(vtodo, prop);
535 icalcomponent_add_property(vtodo,
536 icalproperty_new_due(
537 icaltime_from_webform("due")
541 /* Serialize it and save it to the message base */
542 serv_puts("ENT0 1|||4");
545 serv_puts("Content-type: text/calendar");
547 serv_puts(icalcomponent_as_ical_string(vtodo));
554 * If the user clicked 'Delete' then delete it, period.
556 if (!strcasecmp(bstr("sc"), "Delete")) {
560 if ( (delete_existing) && (msgnum > 0L) ) {
561 serv_printf("DELE %ld", atol(bstr("msgnum")));
565 if (created_new_vtodo) {
566 icalcomponent_free(vtodo);
569 /* Go back to the task list */
576 * Code common to all display handlers. Given a message number and a MIME
577 * type, we load the message and hunt for that MIME type. If found, we load
578 * the relevant part, deserialize it into a libical component, filter it for
579 * the requested object type, and feed it to the specified handler.
581 void display_using_handler(long msgnum,
583 icalcomponent_kind which_kind,
584 void (*callback)(icalcomponent *, long)
587 char mime_partnum[SIZ];
588 char mime_filename[SIZ];
589 char mime_content_type[SIZ];
590 char mime_disposition[SIZ];
592 char relevant_partnum[SIZ];
593 char *relevant_source = NULL;
594 icalcomponent *cal, *c;
596 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
599 if (buf[0] != '1') return;
601 while (serv_gets(buf), strcmp(buf, "000")) {
602 if (!strncasecmp(buf, "part=", 5)) {
603 extract(mime_filename, &buf[5], 1);
604 extract(mime_partnum, &buf[5], 2);
605 extract(mime_disposition, &buf[5], 3);
606 extract(mime_content_type, &buf[5], 4);
607 mime_length = extract_int(&buf[5], 5);
609 if (!strcasecmp(mime_content_type, "text/calendar")) {
610 strcpy(relevant_partnum, mime_partnum);
616 if (strlen(relevant_partnum) > 0) {
617 relevant_source = load_mimepart(msgnum, relevant_partnum);
618 if (relevant_source != NULL) {
620 cal = icalcomponent_new_from_string(relevant_source);
623 /* Simple components of desired type */
624 if (icalcomponent_isa(cal) == which_kind) {
625 callback(cal, msgnum);
628 /* Subcomponents of desired type */
629 for (c = icalcomponent_get_first_component(cal,
632 c = icalcomponent_get_next_component(cal,
636 icalcomponent_free(cal);
638 free(relevant_source);
644 void display_calendar(long msgnum) {
645 display_using_handler(msgnum, "text/calendar",
646 ICAL_VEVENT_COMPONENT,
647 display_individual_cal);
650 void display_task(long msgnum) {
651 display_using_handler(msgnum, "text/calendar",
652 ICAL_VTODO_COMPONENT,
653 display_individual_task);
656 void display_edit_task(void) {
659 msgnum = atol(bstr("msgnum"));
662 display_using_handler(msgnum, "text/calendar",
663 ICAL_VTODO_COMPONENT,
664 display_edit_individual_task);
668 display_edit_individual_task(NULL, 0L);
672 void save_task(void) {
675 msgnum = atol(bstr("msgnum"));
677 display_using_handler(msgnum, "text/calendar",
678 ICAL_VTODO_COMPONENT,
679 save_individual_task);
682 save_individual_task(NULL, 0L);
686 void display_edit_event(void) {
689 msgnum = atol(bstr("msgnum"));
692 display_using_handler(msgnum, "text/calendar",
693 ICAL_VEVENT_COMPONENT,
694 display_edit_individual_event);
698 display_edit_individual_event(NULL, 0L);
702 void save_event(void) {
705 msgnum = atol(bstr("msgnum"));
708 display_using_handler(msgnum, "text/calendar",
709 ICAL_VEVENT_COMPONENT,
710 save_individual_event);
713 save_individual_event(NULL, 0L);
717 #endif /* HAVE_ICAL_H */