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) {
77 /* Iterate through all subcomponents */
78 wprintf("Iterating through all sub-components<BR>\n");
79 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
81 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
82 /* Recursively process subcomponent */
83 cal_process_object(c);
90 * Deserialize a calendar object in a message so it can be processed.
91 * (This is the main entry point for these things)
93 void cal_process_attachment(char *part_source) {
96 wprintf("Processing calendar attachment<BR>\n");
97 cal = icalcomponent_new_from_string(part_source);
100 wprintf("Error parsing calendar object: %s<BR>\n",
101 icalerror_strerror(icalerrno));
105 cal_process_object(cal);
107 /* Free the memory we obtained from libical's constructor */
108 icalcomponent_free(cal);
111 /*****************************************************************************/
116 * Display handlers for message reading
122 * If we're reading calendar items, just store them for now. We have to
123 * sort and re-output them later when we draw the calendar.
125 void display_individual_cal(icalcomponent *cal, long msgnum) {
129 WC->disp_cal = realloc(WC->disp_cal,
130 (sizeof(icalcomponent *) * WC->num_cal) );
131 WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
133 WC->cal_msgnum = realloc(WC->cal_msgnum,
134 (sizeof(long) * WC->num_cal) );
135 WC->cal_msgnum[WC->num_cal - 1] = msgnum;
141 * Display a task in the task list
143 void display_individual_task(icalcomponent *vtodo, long msgnum) {
146 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
147 wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
149 escputs((char *)icalproperty_get_comment(p));
156 * Display a task by itself (for editing)
158 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
159 icalcomponent *vtodo;
161 struct icaltimetype t;
163 int created_new_vtodo = 0;
167 if (supplied_vtodo != NULL) {
168 vtodo = supplied_vtodo;
171 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
172 created_new_vtodo = 1;
176 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
177 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
179 "</FONT></TD></TR></TABLE><BR>\n"
182 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
183 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
187 "<INPUT TYPE=\"text\" NAME=\"summary\" "
188 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
189 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
191 escputs((char *)icalproperty_get_comment(p));
193 wprintf("\"><BR>\n");
195 wprintf("Start date: ");
196 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
198 t = icalproperty_get_dtstart(p);
201 t = icaltime_from_timet(now, 0);
203 display_icaltimetype_as_webform(&t, "dtstart");
206 wprintf("Due date: ");
207 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
209 t = icalproperty_get_due(p);
212 t = icaltime_from_timet(now, 0);
214 display_icaltimetype_as_webform(&t, "due");
217 wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
218 "ROWS=10 COLS=80 WIDTH=80>\n"
220 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
222 escputs((char *)icalproperty_get_comment(p));
224 wprintf("</TEXTAREA><BR>\n");
226 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
228 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
230 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
234 wprintf("</FORM>\n");
238 if (created_new_vtodo) {
239 icalcomponent_free(vtodo);
244 * Save an edited task
246 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
248 int delete_existing = 0;
250 icalcomponent *vtodo;
251 int created_new_vtodo = 0;
253 if (supplied_vtodo != NULL) {
254 vtodo = supplied_vtodo;
257 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
258 created_new_vtodo = 1;
261 if (!strcasecmp(bstr("sc"), "Save")) {
263 /* Replace values in the component with ones from the form */
265 while (prop = icalcomponent_get_first_property(vtodo,
266 ICAL_SUMMARY_PROPERTY), prop != NULL) {
267 icalcomponent_remove_property(vtodo, prop);
269 icalcomponent_add_property(vtodo,
270 icalproperty_new_summary(bstr("summary")));
272 while (prop = icalcomponent_get_first_property(vtodo,
273 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
274 icalcomponent_remove_property(vtodo, prop);
276 icalcomponent_add_property(vtodo,
277 icalproperty_new_description(bstr("description")));
279 while (prop = icalcomponent_get_first_property(vtodo,
280 ICAL_DTSTART_PROPERTY), prop != NULL) {
281 icalcomponent_remove_property(vtodo, prop);
283 icalcomponent_add_property(vtodo,
284 icalproperty_new_dtstart(
285 icaltime_from_webform("dtstart")
289 while (prop = icalcomponent_get_first_property(vtodo,
290 ICAL_DUE_PROPERTY), prop != NULL) {
291 icalcomponent_remove_property(vtodo, prop);
293 icalcomponent_add_property(vtodo,
294 icalproperty_new_due(
295 icaltime_from_webform("due")
299 /* Serialize it and save it to the message base */
300 serv_puts("ENT0 1|||4");
303 serv_puts("Content-type: text/calendar");
305 serv_puts(icalcomponent_as_ical_string(vtodo));
312 * If the user clicked 'Delete' then delete it, period.
314 if (!strcasecmp(bstr("sc"), "Delete")) {
318 if ( (delete_existing) && (msgnum > 0L) ) {
319 serv_printf("DELE %ld", atol(bstr("msgnum")));
323 if (created_new_vtodo) {
324 icalcomponent_free(vtodo);
327 /* Go back to the task list */
334 * Code common to all display handlers. Given a message number and a MIME
335 * type, we load the message and hunt for that MIME type. If found, we load
336 * the relevant part, deserialize it into a libical component, filter it for
337 * the requested object type, and feed it to the specified handler.
339 void display_using_handler(long msgnum,
341 icalcomponent_kind which_kind,
342 void (*callback)(icalcomponent *, long)
345 char mime_partnum[SIZ];
346 char mime_filename[SIZ];
347 char mime_content_type[SIZ];
348 char mime_disposition[SIZ];
350 char relevant_partnum[SIZ];
351 char *relevant_source = NULL;
352 icalcomponent *cal, *c;
354 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
357 if (buf[0] != '1') return;
359 while (serv_gets(buf), strcmp(buf, "000")) {
360 if (!strncasecmp(buf, "part=", 5)) {
361 extract(mime_filename, &buf[5], 1);
362 extract(mime_partnum, &buf[5], 2);
363 extract(mime_disposition, &buf[5], 3);
364 extract(mime_content_type, &buf[5], 4);
365 mime_length = extract_int(&buf[5], 5);
367 if (!strcasecmp(mime_content_type, "text/calendar")) {
368 strcpy(relevant_partnum, mime_partnum);
374 if (strlen(relevant_partnum) > 0) {
375 relevant_source = load_mimepart(msgnum, relevant_partnum);
376 if (relevant_source != NULL) {
378 cal = icalcomponent_new_from_string(relevant_source);
381 /* Simple components of desired type */
382 if (icalcomponent_isa(cal) == which_kind) {
383 callback(cal, msgnum);
386 /* Subcomponents of desired type */
387 for (c = icalcomponent_get_first_component(cal,
390 c = icalcomponent_get_next_component(cal,
394 icalcomponent_free(cal);
396 free(relevant_source);
402 void display_calendar(long msgnum) {
403 display_using_handler(msgnum, "text/calendar",
404 ICAL_VEVENT_COMPONENT,
405 display_individual_cal);
408 void display_task(long msgnum) {
409 display_using_handler(msgnum, "text/calendar",
410 ICAL_VTODO_COMPONENT,
411 display_individual_task);
414 void display_edit_task(void) {
417 msgnum = atol(bstr("msgnum"));
420 display_using_handler(msgnum, "text/calendar",
421 ICAL_VTODO_COMPONENT,
422 display_edit_individual_task);
426 display_edit_individual_task(NULL, 0L);
430 void save_task(void) {
433 msgnum = atol(bstr("msgnum"));
435 display_using_handler(msgnum, "text/calendar",
436 ICAL_VTODO_COMPONENT,
437 save_individual_task);
440 save_individual_task(NULL, 0L);
444 void display_edit_event(void) {
447 msgnum = atol(bstr("msgnum"));
450 display_using_handler(msgnum, "text/calendar",
451 ICAL_VEVENT_COMPONENT,
452 display_edit_individual_event);
456 display_edit_individual_event(NULL, 0L);
460 void save_event(void) {
463 msgnum = atol(bstr("msgnum"));
466 display_using_handler(msgnum, "text/calendar",
467 ICAL_VEVENT_COMPONENT,
468 save_individual_event);
471 save_individual_event(NULL, 0L);
475 #endif /* HAVE_ICAL_H */