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 /* Look for a method */
82 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
84 /* See what we need to do with this */
86 the_method = icalproperty_get_method(method);
88 case ICAL_METHOD_REQUEST:
89 wprintf("This is a request.<BR>\n");
92 wprintf("I don't know what to do with this.<BR>\n");
97 /* If the component has subcomponents, recurse through them. */
98 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
100 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
101 /* Recursively process subcomponent */
102 cal_process_object(c, recursion_level+1);
109 * Deserialize a calendar object in a message so it can be processed.
110 * (This is the main entry point for these things)
112 void cal_process_attachment(char *part_source) {
115 wprintf("Processing calendar attachment<BR>\n");
116 cal = icalcomponent_new_from_string(part_source);
119 wprintf("Error parsing calendar object: %s<BR>\n",
120 icalerror_strerror(icalerrno));
124 cal_process_object(cal, 0);
126 /* Free the memory we obtained from libical's constructor */
127 icalcomponent_free(cal);
130 /*****************************************************************************/
135 * Display handlers for message reading
141 * If we're reading calendar items, just store them for now. We have to
142 * sort and re-output them later when we draw the calendar.
144 void display_individual_cal(icalcomponent *cal, long msgnum) {
148 WC->disp_cal = realloc(WC->disp_cal,
149 (sizeof(icalcomponent *) * WC->num_cal) );
150 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
152 WC->cal_msgnum = realloc(WC->cal_msgnum,
153 (sizeof(long) * WC->num_cal) );
154 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
160 * Display a task in the task list
162 void display_individual_task(icalcomponent *vtodo, long msgnum) {
165 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
166 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
168 escputs((char *)icalproperty_get_comment(p));
175 * Display a task by itself (for editing)
177 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
178 icalcomponent *vtodo;
180 struct icaltimetype t;
182 int created_new_vtodo = 0;
186 if (supplied_vtodo != NULL) {
187 vtodo = supplied_vtodo;
190 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
191 created_new_vtodo = 1;
195 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
196 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
198 "</FONT></TD></TR></TABLE><BR>\n"
201 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
202 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
206 "<INPUT TYPE=\"text\" NAME=\"summary\" "
207 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
208 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
210 escputs((char *)icalproperty_get_comment(p));
212 wprintf("\"><BR>\n");
214 wprintf("Start date: ");
215 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
217 t = icalproperty_get_dtstart(p);
220 t = icaltime_from_timet(now, 0);
222 display_icaltimetype_as_webform(&t, "dtstart");
225 wprintf("Due date: ");
226 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
228 t = icalproperty_get_due(p);
231 t = icaltime_from_timet(now, 0);
233 display_icaltimetype_as_webform(&t, "due");
236 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
237 "ROWS=10 COLS=80 WIDTH=80>\n"
239 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
241 escputs((char *)icalproperty_get_comment(p));
243 wprintf("</TEXTAREA><BR>\n");
245 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
247 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
249 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
253 wprintf("</FORM>\n");
257 if (created_new_vtodo) {
258 icalcomponent_free(vtodo);
263 * Save an edited task
265 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
267 int delete_existing = 0;
269 icalcomponent *vtodo;
270 int created_new_vtodo = 0;
272 if (supplied_vtodo != NULL) {
273 vtodo = supplied_vtodo;
276 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
277 created_new_vtodo = 1;
280 if (!strcasecmp(bstr("sc"), "Save")) {
282 /* Replace values in the component with ones from the form */
284 while (prop = icalcomponent_get_first_property(vtodo,
285 ICAL_SUMMARY_PROPERTY), prop != NULL) {
286 icalcomponent_remove_property(vtodo, prop);
288 icalcomponent_add_property(vtodo,
289 icalproperty_new_summary(bstr("summary")));
291 while (prop = icalcomponent_get_first_property(vtodo,
292 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
293 icalcomponent_remove_property(vtodo, prop);
295 icalcomponent_add_property(vtodo,
296 icalproperty_new_description(bstr("description")));
298 while (prop = icalcomponent_get_first_property(vtodo,
299 ICAL_DTSTART_PROPERTY), prop != NULL) {
300 icalcomponent_remove_property(vtodo, prop);
302 icalcomponent_add_property(vtodo,
303 icalproperty_new_dtstart(
304 icaltime_from_webform("dtstart")
308 while (prop = icalcomponent_get_first_property(vtodo,
309 ICAL_DUE_PROPERTY), prop != NULL) {
310 icalcomponent_remove_property(vtodo, prop);
312 icalcomponent_add_property(vtodo,
313 icalproperty_new_due(
314 icaltime_from_webform("due")
318 /* Serialize it and save it to the message base */
319 serv_puts("ENT0 1|||4");
322 serv_puts("Content-type: text/calendar");
324 serv_puts(icalcomponent_as_ical_string(vtodo));
331 * If the user clicked 'Delete' then delete it, period.
333 if (!strcasecmp(bstr("sc"), "Delete")) {
337 if ( (delete_existing) && (msgnum > 0L) ) {
338 serv_printf("DELE %ld", atol(bstr("msgnum")));
342 if (created_new_vtodo) {
343 icalcomponent_free(vtodo);
346 /* Go back to the task list */
353 * Code common to all display handlers. Given a message number and a MIME
354 * type, we load the message and hunt for that MIME type. If found, we load
355 * the relevant part, deserialize it into a libical component, filter it for
356 * the requested object type, and feed it to the specified handler.
358 void display_using_handler(long msgnum,
360 icalcomponent_kind which_kind,
361 void (*callback)(icalcomponent *, long)
364 char mime_partnum[SIZ];
365 char mime_filename[SIZ];
366 char mime_content_type[SIZ];
367 char mime_disposition[SIZ];
369 char relevant_partnum[SIZ];
370 char *relevant_source = NULL;
371 icalcomponent *cal, *c;
373 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
376 if (buf[0] != '1') return;
378 while (serv_gets(buf), strcmp(buf, "000")) {
379 if (!strncasecmp(buf, "part=", 5)) {
380 extract(mime_filename, &buf[5], 1);
381 extract(mime_partnum, &buf[5], 2);
382 extract(mime_disposition, &buf[5], 3);
383 extract(mime_content_type, &buf[5], 4);
384 mime_length = extract_int(&buf[5], 5);
386 if (!strcasecmp(mime_content_type, "text/calendar")) {
387 strcpy(relevant_partnum, mime_partnum);
393 if (strlen(relevant_partnum) > 0) {
394 relevant_source = load_mimepart(msgnum, relevant_partnum);
395 if (relevant_source != NULL) {
397 cal = icalcomponent_new_from_string(relevant_source);
400 /* Simple components of desired type */
401 if (icalcomponent_isa(cal) == which_kind) {
402 callback(cal, msgnum);
405 /* Subcomponents of desired type */
406 for (c = icalcomponent_get_first_component(cal,
409 c = icalcomponent_get_next_component(cal,
413 icalcomponent_free(cal);
415 free(relevant_source);
421 void display_calendar(long msgnum) {
422 display_using_handler(msgnum, "text/calendar",
423 ICAL_VEVENT_COMPONENT,
424 display_individual_cal);
427 void display_task(long msgnum) {
428 display_using_handler(msgnum, "text/calendar",
429 ICAL_VTODO_COMPONENT,
430 display_individual_task);
433 void display_edit_task(void) {
436 msgnum = atol(bstr("msgnum"));
439 display_using_handler(msgnum, "text/calendar",
440 ICAL_VTODO_COMPONENT,
441 display_edit_individual_task);
445 display_edit_individual_task(NULL, 0L);
449 void save_task(void) {
452 msgnum = atol(bstr("msgnum"));
454 display_using_handler(msgnum, "text/calendar",
455 ICAL_VTODO_COMPONENT,
456 save_individual_task);
459 save_individual_task(NULL, 0L);
463 void display_edit_event(void) {
466 msgnum = atol(bstr("msgnum"));
469 display_using_handler(msgnum, "text/calendar",
470 ICAL_VEVENT_COMPONENT,
471 display_edit_individual_event);
475 display_edit_individual_event(NULL, 0L);
479 void save_event(void) {
482 msgnum = atol(bstr("msgnum"));
485 display_using_handler(msgnum, "text/calendar",
486 ICAL_VEVENT_COMPONENT,
487 save_individual_event);
490 save_individual_event(NULL, 0L);
494 #endif /* HAVE_ICAL_H */