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) {
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,
78 icalproperty *method = NULL;
79 icalproperty_method the_method;
81 struct icaltimetype t;
85 /* Leading HTML for the display of this object */
86 if (recursion_level == 0) {
87 wprintf("<CENTER><TABLE border=0 cellpadding=5>\n");
90 /* Look for a method */
91 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
93 /* See what we need to do with this */
95 the_method = icalproperty_get_method(method);
97 case ICAL_METHOD_REQUEST:
98 wprintf("<TR><TD COLSPAN=2>\n"
100 "SRC=\"/static/vcalendar.gif\">"
102 "<B>Meeting invitation</B>
107 wprintf("<TR><TD COLSPAN=2>"
108 "I don't know what to do with this.</TD></TR>"
114 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
116 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
117 escputs((char *)icalproperty_get_comment(p));
118 wprintf("</TD></TR>\n");
121 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
123 wprintf("<TR><TD><B>Location:</B></TD><TD>");
124 escputs((char *)icalproperty_get_comment(p));
125 wprintf("</TD></TR>\n");
129 * Only show start/end times if we're actually looking at the VEVENT
130 * component. Otherwise it shows bogus dates for things like timezone.
132 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
134 p = icalcomponent_get_first_property(cal,
135 ICAL_DTSTART_PROPERTY);
137 t = icalproperty_get_dtstart(p);
138 tt = icaltime_as_timet(t);
140 wprintf("<TR><TD><B>Starting date/time:</B></TD><TD>"
145 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
147 t = icalproperty_get_dtend(p);
148 tt = icaltime_as_timet(t);
150 wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
157 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
159 wprintf("<TR><TD><B>Description:</B></TD><TD>");
160 escputs((char *)icalproperty_get_comment(p));
161 wprintf("</TD></TR>\n");
164 /* If the component has subcomponents, recurse through them. */
165 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
167 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
168 /* Recursively process subcomponent */
169 cal_process_object(c, recursion_level+1);
172 /* Trailing HTML for the display of this object */
173 if (recursion_level == 0) {
174 wprintf("<TR><TD COLSPAN=2>"
175 "<FORM METHOD=\"GET\" "
176 "ACTION=\"/respond_to_request\">\n"
177 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
180 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
183 "</TD></TR></TABLE></CENTER>\n"
190 * Deserialize a calendar object in a message so it can be processed.
191 * (This is the main entry point for these things)
193 void cal_process_attachment(char *part_source) {
196 cal = icalcomponent_new_from_string(part_source);
199 wprintf("Error parsing calendar object: %s<BR>\n",
200 icalerror_strerror(icalerrno));
204 cal_process_object(cal, 0);
206 /* Free the memory we obtained from libical's constructor */
207 icalcomponent_free(cal);
210 /*****************************************************************************/
215 * Display handlers for message reading
221 * If we're reading calendar items, just store them for now. We have to
222 * sort and re-output them later when we draw the calendar.
224 void display_individual_cal(icalcomponent *cal, long msgnum) {
228 WC->disp_cal = realloc(WC->disp_cal,
229 (sizeof(icalcomponent *) * WC->num_cal) );
230 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
232 WC->cal_msgnum = realloc(WC->cal_msgnum,
233 (sizeof(long) * WC->num_cal) );
234 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
240 * Display a task in the task list
242 void display_individual_task(icalcomponent *vtodo, long msgnum) {
245 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
246 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
248 escputs((char *)icalproperty_get_comment(p));
255 * Display a task by itself (for editing)
257 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
258 icalcomponent *vtodo;
260 struct icaltimetype t;
262 int created_new_vtodo = 0;
266 if (supplied_vtodo != NULL) {
267 vtodo = supplied_vtodo;
270 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
271 created_new_vtodo = 1;
275 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
276 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
278 "</FONT></TD></TR></TABLE><BR>\n"
281 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
282 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
286 "<INPUT TYPE=\"text\" NAME=\"summary\" "
287 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
288 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
290 escputs((char *)icalproperty_get_comment(p));
292 wprintf("\"><BR>\n");
294 wprintf("Start date: ");
295 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
297 t = icalproperty_get_dtstart(p);
300 t = icaltime_from_timet(now, 0);
302 display_icaltimetype_as_webform(&t, "dtstart");
305 wprintf("Due date: ");
306 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
308 t = icalproperty_get_due(p);
311 t = icaltime_from_timet(now, 0);
313 display_icaltimetype_as_webform(&t, "due");
316 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
317 "ROWS=10 COLS=80 WIDTH=80>\n"
319 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
321 escputs((char *)icalproperty_get_comment(p));
323 wprintf("</TEXTAREA><BR>\n");
325 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
327 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
329 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
333 wprintf("</FORM>\n");
337 if (created_new_vtodo) {
338 icalcomponent_free(vtodo);
343 * Save an edited task
345 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
347 int delete_existing = 0;
349 icalcomponent *vtodo;
350 int created_new_vtodo = 0;
352 if (supplied_vtodo != NULL) {
353 vtodo = supplied_vtodo;
356 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
357 created_new_vtodo = 1;
360 if (!strcasecmp(bstr("sc"), "Save")) {
362 /* Replace values in the component with ones from the form */
364 while (prop = icalcomponent_get_first_property(vtodo,
365 ICAL_SUMMARY_PROPERTY), prop != NULL) {
366 icalcomponent_remove_property(vtodo, prop);
368 icalcomponent_add_property(vtodo,
369 icalproperty_new_summary(bstr("summary")));
371 while (prop = icalcomponent_get_first_property(vtodo,
372 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
373 icalcomponent_remove_property(vtodo, prop);
375 icalcomponent_add_property(vtodo,
376 icalproperty_new_description(bstr("description")));
378 while (prop = icalcomponent_get_first_property(vtodo,
379 ICAL_DTSTART_PROPERTY), prop != NULL) {
380 icalcomponent_remove_property(vtodo, prop);
382 icalcomponent_add_property(vtodo,
383 icalproperty_new_dtstart(
384 icaltime_from_webform("dtstart")
388 while (prop = icalcomponent_get_first_property(vtodo,
389 ICAL_DUE_PROPERTY), prop != NULL) {
390 icalcomponent_remove_property(vtodo, prop);
392 icalcomponent_add_property(vtodo,
393 icalproperty_new_due(
394 icaltime_from_webform("due")
398 /* Serialize it and save it to the message base */
399 serv_puts("ENT0 1|||4");
402 serv_puts("Content-type: text/calendar");
404 serv_puts(icalcomponent_as_ical_string(vtodo));
411 * If the user clicked 'Delete' then delete it, period.
413 if (!strcasecmp(bstr("sc"), "Delete")) {
417 if ( (delete_existing) && (msgnum > 0L) ) {
418 serv_printf("DELE %ld", atol(bstr("msgnum")));
422 if (created_new_vtodo) {
423 icalcomponent_free(vtodo);
426 /* Go back to the task list */
433 * Code common to all display handlers. Given a message number and a MIME
434 * type, we load the message and hunt for that MIME type. If found, we load
435 * the relevant part, deserialize it into a libical component, filter it for
436 * the requested object type, and feed it to the specified handler.
438 void display_using_handler(long msgnum,
440 icalcomponent_kind which_kind,
441 void (*callback)(icalcomponent *, long)
444 char mime_partnum[SIZ];
445 char mime_filename[SIZ];
446 char mime_content_type[SIZ];
447 char mime_disposition[SIZ];
449 char relevant_partnum[SIZ];
450 char *relevant_source = NULL;
451 icalcomponent *cal, *c;
453 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
456 if (buf[0] != '1') return;
458 while (serv_gets(buf), strcmp(buf, "000")) {
459 if (!strncasecmp(buf, "part=", 5)) {
460 extract(mime_filename, &buf[5], 1);
461 extract(mime_partnum, &buf[5], 2);
462 extract(mime_disposition, &buf[5], 3);
463 extract(mime_content_type, &buf[5], 4);
464 mime_length = extract_int(&buf[5], 5);
466 if (!strcasecmp(mime_content_type, "text/calendar")) {
467 strcpy(relevant_partnum, mime_partnum);
473 if (strlen(relevant_partnum) > 0) {
474 relevant_source = load_mimepart(msgnum, relevant_partnum);
475 if (relevant_source != NULL) {
477 cal = icalcomponent_new_from_string(relevant_source);
480 /* Simple components of desired type */
481 if (icalcomponent_isa(cal) == which_kind) {
482 callback(cal, msgnum);
485 /* Subcomponents of desired type */
486 for (c = icalcomponent_get_first_component(cal,
489 c = icalcomponent_get_next_component(cal,
493 icalcomponent_free(cal);
495 free(relevant_source);
501 void display_calendar(long msgnum) {
502 display_using_handler(msgnum, "text/calendar",
503 ICAL_VEVENT_COMPONENT,
504 display_individual_cal);
507 void display_task(long msgnum) {
508 display_using_handler(msgnum, "text/calendar",
509 ICAL_VTODO_COMPONENT,
510 display_individual_task);
513 void display_edit_task(void) {
516 msgnum = atol(bstr("msgnum"));
519 display_using_handler(msgnum, "text/calendar",
520 ICAL_VTODO_COMPONENT,
521 display_edit_individual_task);
525 display_edit_individual_task(NULL, 0L);
529 void save_task(void) {
532 msgnum = atol(bstr("msgnum"));
534 display_using_handler(msgnum, "text/calendar",
535 ICAL_VTODO_COMPONENT,
536 save_individual_task);
539 save_individual_task(NULL, 0L);
543 void display_edit_event(void) {
546 msgnum = atol(bstr("msgnum"));
549 display_using_handler(msgnum, "text/calendar",
550 ICAL_VEVENT_COMPONENT,
551 display_edit_individual_event);
555 display_edit_individual_event(NULL, 0L);
559 void save_event(void) {
562 msgnum = atol(bstr("msgnum"));
565 display_using_handler(msgnum, "text/calendar",
566 ICAL_VEVENT_COMPONENT,
567 save_individual_event);
570 save_individual_event(NULL, 0L);
574 #endif /* HAVE_ICAL_H */