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) {
47 wprintf("<i>Cannot display calendar item</i><br>\n");
50 void display_task(long msgnum) {
51 wprintf("<i>Cannot display item from task list</i><br>\n");
54 #else /* HAVE_ICAL_H */
57 /****** End of handler stubs. Everything below this line is real. ******/
61 * Process a single calendar component.
62 * It won't be a compound component at this point because those have
63 * already been broken down by cal_process_object().
65 void cal_process_subcomponent(icalcomponent *cal) {
66 wprintf("cal_process_subcomponent() called<BR>\n");
67 wprintf("cal_process_subcomponent() exiting<BR>\n");
75 * Process a calendar object
76 * ...at this point it's already been deserialized by cal_process_attachment()
78 void cal_process_object(icalcomponent *cal) {
80 int num_subcomponents = 0;
82 wprintf("cal_process_object() called<BR>\n");
84 /* Iterate through all subcomponents */
85 wprintf("Iterating through all sub-components<BR>\n");
86 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
88 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
89 cal_process_subcomponent(c);
93 /* Iterate through all subcomponents */
94 wprintf("Iterating through VEVENTs<BR>\n");
95 for (c = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
97 c = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT)) {
98 cal_process_subcomponent(c);
102 /* Iterate through all subcomponents */
103 wprintf("Iterating through VTODOs<BR>\n");
104 for (c = icalcomponent_get_first_component(cal, ICAL_VTODO_COMPONENT);
106 c = icalcomponent_get_next_component(cal, ICAL_VTODO_COMPONENT)) {
107 cal_process_subcomponent(c);
111 /* Iterate through all subcomponents */
112 wprintf("Iterating through VJOURNALs<BR>\n");
113 for (c = icalcomponent_get_first_component(cal, ICAL_VJOURNAL_COMPONENT);
115 c = icalcomponent_get_next_component(cal, ICAL_VJOURNAL_COMPONENT)) {
116 cal_process_subcomponent(c);
120 /* Iterate through all subcomponents */
121 wprintf("Iterating through VCALENDARs<BR>\n");
122 for (c = icalcomponent_get_first_component(cal, ICAL_VCALENDAR_COMPONENT);
124 c = icalcomponent_get_next_component(cal, ICAL_VCALENDAR_COMPONENT)) {
125 cal_process_subcomponent(c);
129 /* Iterate through all subcomponents */
130 wprintf("Iterating through VFREEBUSYs<BR>\n");
131 for (c = icalcomponent_get_first_component(cal, ICAL_VFREEBUSY_COMPONENT);
133 c = icalcomponent_get_next_component(cal, ICAL_VFREEBUSY_COMPONENT)) {
134 cal_process_subcomponent(c);
138 /* Iterate through all subcomponents */
139 wprintf("Iterating through VALARMs<BR>\n");
140 for (c = icalcomponent_get_first_component(cal, ICAL_VALARM_COMPONENT);
142 c = icalcomponent_get_next_component(cal, ICAL_VALARM_COMPONENT)) {
143 cal_process_subcomponent(c);
147 /* Iterate through all subcomponents */
148 wprintf("Iterating through VTIMEZONEs<BR>\n");
149 for (c = icalcomponent_get_first_component(cal, ICAL_VTIMEZONE_COMPONENT);
151 c = icalcomponent_get_next_component(cal, ICAL_VTIMEZONE_COMPONENT)) {
152 cal_process_subcomponent(c);
156 /* Iterate through all subcomponents */
157 wprintf("Iterating through VSCHEDULEs<BR>\n");
158 for (c = icalcomponent_get_first_component(cal, ICAL_VSCHEDULE_COMPONENT);
160 c = icalcomponent_get_next_component(cal, ICAL_VSCHEDULE_COMPONENT)) {
161 cal_process_subcomponent(c);
165 /* Iterate through all subcomponents */
166 wprintf("Iterating through VQUERYs<BR>\n");
167 for (c = icalcomponent_get_first_component(cal, ICAL_VQUERY_COMPONENT);
169 c = icalcomponent_get_next_component(cal, ICAL_VQUERY_COMPONENT)) {
170 cal_process_subcomponent(c);
174 /* Iterate through all subcomponents */
175 wprintf("Iterating through VCARs<BR>\n");
176 for (c = icalcomponent_get_first_component(cal, ICAL_VCAR_COMPONENT);
178 c = icalcomponent_get_next_component(cal, ICAL_VCAR_COMPONENT)) {
179 cal_process_subcomponent(c);
183 /* Iterate through all subcomponents */
184 wprintf("Iterating through VCOMMANDs<BR>\n");
185 for (c = icalcomponent_get_first_component(cal, ICAL_VCOMMAND_COMPONENT);
187 c = icalcomponent_get_next_component(cal, ICAL_VCOMMAND_COMPONENT)) {
188 cal_process_subcomponent(c);
192 if (num_subcomponents != 0) {
193 wprintf("Warning: %d subcomponents unhandled<BR>\n",
197 wprintf("cal_process_object() exiting<BR>\n");
202 * Deserialize a calendar object in a message so it can be processed.
203 * (This is the main entry point for these things)
205 void cal_process_attachment(char *part_source) {
208 wprintf("Processing calendar attachment<BR>\n");
209 cal = icalcomponent_new_from_string(part_source);
212 wprintf("Error parsing calendar object: %s<BR>\n",
213 icalerror_strerror(icalerrno));
217 cal_process_object(cal);
219 /* Free the memory we obtained from libical's constructor */
220 icalcomponent_free(cal);
223 /*****************************************************************************/
228 * Display handlers for message reading
234 * If we're reading calendar items, just store them for now. We have to
235 * sort and re-output them later when we draw the calendar.
237 void display_individual_cal(icalcomponent *cal, long msgnum) {
240 WC->disp_cal = realloc(WC->disp_cal,
241 (sizeof(icalcomponent *) * WC->num_cal) );
242 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
248 * Display a task in the task list
250 void display_individual_task(icalcomponent *vtodo, long msgnum) {
253 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
254 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
256 escputs((char *)icalproperty_get_comment(p));
263 * Display a task by itself (for editing)
265 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
266 icalcomponent *vtodo;
268 struct icaltimetype t;
270 int created_new_vtodo = 0;
274 if (supplied_vtodo != NULL) {
275 vtodo = supplied_vtodo;
278 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
279 created_new_vtodo = 1;
283 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
284 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
286 "</FONT></TD></TR></TABLE><BR>\n"
289 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
290 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
294 "<INPUT TYPE=\"text\" NAME=\"summary\" "
295 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
296 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
298 escputs((char *)icalproperty_get_comment(p));
300 wprintf("\"><BR>\n");
302 wprintf("Start date: ");
303 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
305 t = icalproperty_get_dtstart(p);
308 t = icaltime_from_timet(now, 0);
310 display_icaltimetype_as_webform(&t, "dtstart");
313 wprintf("Due date: ");
314 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
316 t = icalproperty_get_due(p);
319 t = icaltime_from_timet(now, 0);
321 display_icaltimetype_as_webform(&t, "due");
324 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
325 "ROWS=10 COLS=80 WIDTH=80>\n"
327 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
329 escputs((char *)icalproperty_get_comment(p));
331 wprintf("</TEXTAREA><BR>\n");
333 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
335 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
337 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
341 wprintf("</FORM>\n");
345 if (created_new_vtodo) {
346 icalcomponent_free(vtodo);
351 * Save an edited task
353 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
355 int delete_existing = 0;
357 icalcomponent *vtodo;
358 int created_new_vtodo = 0;
360 if (supplied_vtodo != NULL) {
361 vtodo = supplied_vtodo;
364 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
365 created_new_vtodo = 1;
368 if (!strcasecmp(bstr("sc"), "Save")) {
370 /* Replace values in the component with ones from the form */
372 while (prop = icalcomponent_get_first_property(vtodo,
373 ICAL_SUMMARY_PROPERTY), prop != NULL) {
374 icalcomponent_remove_property(vtodo, prop);
376 icalcomponent_add_property(vtodo,
377 icalproperty_new_summary(bstr("summary")));
379 while (prop = icalcomponent_get_first_property(vtodo,
380 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
381 icalcomponent_remove_property(vtodo, prop);
383 icalcomponent_add_property(vtodo,
384 icalproperty_new_description(bstr("description")));
386 while (prop = icalcomponent_get_first_property(vtodo,
387 ICAL_DTSTART_PROPERTY), prop != NULL) {
388 icalcomponent_remove_property(vtodo, prop);
390 icalcomponent_add_property(vtodo,
391 icalproperty_new_dtstart(
392 icaltime_from_webform("dtstart")
396 while (prop = icalcomponent_get_first_property(vtodo,
397 ICAL_DUE_PROPERTY), prop != NULL) {
398 icalcomponent_remove_property(vtodo, prop);
400 icalcomponent_add_property(vtodo,
401 icalproperty_new_due(
402 icaltime_from_webform("due")
406 /* Serialize it and save it to the message base */
407 serv_puts("ENT0 1|||4");
410 serv_puts("Content-type: text/calendar");
412 serv_puts(icalcomponent_as_ical_string(vtodo));
419 * If the user clicked 'Delete' then delete it, period.
421 if (!strcasecmp(bstr("sc"), "Delete")) {
425 if ( (delete_existing) && (msgnum > 0L) ) {
426 serv_printf("DELE %ld", atol(bstr("msgnum")));
430 if (created_new_vtodo) {
431 icalcomponent_free(vtodo);
434 /* Go back to the task list */
441 * Code common to all display handlers. Given a message number and a MIME
442 * type, we load the message and hunt for that MIME type. If found, we load
443 * the relevant part, deserialize it into a libical component, filter it for
444 * the requested object type, and feed it to the specified handler.
446 void display_using_handler(long msgnum,
448 icalcomponent_kind which_kind,
449 void (*callback)(icalcomponent *, long)
452 char mime_partnum[SIZ];
453 char mime_filename[SIZ];
454 char mime_content_type[SIZ];
455 char mime_disposition[SIZ];
457 char relevant_partnum[SIZ];
458 char *relevant_source = NULL;
459 icalcomponent *cal, *c;
461 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
464 if (buf[0] != '1') return;
466 while (serv_gets(buf), strcmp(buf, "000")) {
467 if (!strncasecmp(buf, "part=", 5)) {
468 extract(mime_filename, &buf[5], 1);
469 extract(mime_partnum, &buf[5], 2);
470 extract(mime_disposition, &buf[5], 3);
471 extract(mime_content_type, &buf[5], 4);
472 mime_length = extract_int(&buf[5], 5);
474 if (!strcasecmp(mime_content_type, "text/calendar")) {
475 strcpy(relevant_partnum, mime_partnum);
481 if (strlen(relevant_partnum) > 0) {
482 relevant_source = load_mimepart(msgnum, relevant_partnum);
483 if (relevant_source != NULL) {
485 cal = icalcomponent_new_from_string(relevant_source);
488 /* Simple components of desired type */
489 if (icalcomponent_isa(cal) == which_kind) {
490 callback(cal, msgnum);
493 /* Subcomponents of desired type */
494 for (c = icalcomponent_get_first_component(cal,
497 c = icalcomponent_get_next_component(cal,
501 icalcomponent_free(cal);
503 free(relevant_source);
509 void display_calendar(long msgnum) {
510 display_using_handler(msgnum, "text/calendar",
511 ICAL_VEVENT_COMPONENT,
512 display_individual_cal);
515 void display_task(long msgnum) {
516 display_using_handler(msgnum, "text/calendar",
517 ICAL_VTODO_COMPONENT,
518 display_individual_task);
521 void display_edit_task(void) {
524 msgnum = atol(bstr("msgnum"));
527 display_using_handler(msgnum, "text/calendar",
528 ICAL_VTODO_COMPONENT,
529 display_edit_individual_task);
533 display_edit_individual_task(NULL, 0L);
537 void save_task(void) {
540 msgnum = atol(bstr("msgnum"));
542 display_using_handler(msgnum, "text/calendar",
543 ICAL_VTODO_COMPONENT,
544 save_individual_task);
547 save_individual_task(NULL, 0L);
551 #endif /* HAVE_ICAL_H */