4 * Editing calendar events.
14 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
28 #include "webserver.h"
31 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
34 * Display an event by itself (for editing)
36 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
37 icalcomponent *vevent;
40 struct icaltimetype t_start, t_end;
43 int created_new_vevent = 0;
44 icalproperty *organizer = NULL;
45 char organizer_string[SIZ];
46 icalproperty *attendee = NULL;
47 char attendee_string[SIZ];
49 int organizer_is_me = 0;
54 strcpy(organizer_string, "");
55 strcpy(attendee_string, "");
57 if (supplied_vevent != NULL) {
58 vevent = supplied_vevent;
59 /* If we're looking at a fully encapsulated VCALENDAR
60 * rather than a VEVENT component, attempt to use the first
61 * relevant VEVENT subcomponent. If there is none, the
62 * NULL returned by icalcomponent_get_first_component() will
63 * tell the next iteration of this function to create a
66 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
67 display_edit_individual_event(
68 icalcomponent_get_first_component(
69 vevent, ICAL_VEVENT_COMPONENT
76 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
77 created_new_vevent = 1;
80 /* Learn the sequence */
81 p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
83 sequence = icalproperty_get_sequence(p);
87 output_headers(1, 1, 2, 0, 0, 0, 0);
88 wprintf("<div id=\"banner\">\n"
89 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
90 "<SPAN CLASS=\"titlebar\">Add or edit an event</SPAN>"
91 "</TD></TR></TABLE>\n"
92 "</div>\n<div id=\"content\">\n"
95 wprintf("<center><table border=0 width=99%% bgcolor=\"#ffffff\"><tr><td>\n");
97 /************************************************************
98 * Uncomment this to see the UID in calendar events for debugging
100 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
102 escputs((char *)icalproperty_get_comment(p));
105 wprintf("SEQUENCE == %d<br />\n", sequence);
106 *************************************************************/
108 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
110 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
112 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
114 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
116 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
118 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
121 /* Put it in a borderless table so it lines up nicely */
122 wprintf("<TABLE border=0 width=100%%>\n");
124 wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
125 "<INPUT TYPE=\"text\" NAME=\"summary\" "
126 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
127 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
129 escputs((char *)icalproperty_get_comment(p));
131 wprintf("\"></TD></TR>\n");
133 wprintf("<TR><TD><B>Location</B></TD><TD>\n"
134 "<INPUT TYPE=\"text\" NAME=\"location\" "
135 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
136 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
138 escputs((char *)icalproperty_get_comment(p));
140 wprintf("\"></TD></TR>\n");
142 wprintf("<TR><TD><B>Start</B></TD><TD>\n");
143 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
145 t_start = icalproperty_get_dtstart(p);
146 if (t_start.is_date) {
153 localtime_r(&now, &tm_now);
154 if (strlen(bstr("year")) > 0) {
155 tm_now.tm_year = atoi(bstr("year")) - 1900;
156 tm_now.tm_mon = atoi(bstr("month")) - 1;
157 tm_now.tm_mday = atoi(bstr("day"));
159 if (strlen(bstr("hour")) > 0) {
160 tm_now.tm_hour = atoi(bstr("hour"));
161 tm_now.tm_min = atoi(bstr("minute"));
170 t_start = icaltime_from_timet_with_zone(
172 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
173 icaltimezone_get_utc_timezone()
178 display_icaltimetype_as_webform(&t_start, "dtstart");
180 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
181 "VALUE=\"yes\" onClick=\""
183 " if (this.checked) { "
184 " this.form.dtstart_hour.value='0'; "
185 " this.form.dtstart_hour.disabled = true; "
186 " this.form.dtstart_minute.value='0'; "
187 " this.form.dtstart_minute.disabled = true; "
188 " this.form.dtend_hour.value='0'; "
189 " this.form.dtend_hour.disabled = true; "
190 " this.form.dtend_minute.value='0'; "
191 " this.form.dtend_minute.disabled = true; "
192 " this.form.dtend_month.disabled = true; "
193 " this.form.dtend_day.disabled = true; "
194 " this.form.dtend_year.disabled = true; "
197 " this.form.dtstart_hour.disabled = false; "
198 " this.form.dtstart_minute.disabled = false; "
199 " this.form.dtend_hour.disabled = false; "
200 " this.form.dtend_minute.disabled = false; "
201 " this.form.dtend_month.disabled = false; "
202 " this.form.dtend_day.disabled = false; "
203 " this.form.dtend_year.disabled = false; "
207 " \" %s >All day event",
208 (t_start.is_date ? "CHECKED" : "" )
211 wprintf("</TD></TR>\n");
213 /* If this is an all-day-event, set the end time to be identical to
214 * the start time (the hour/minute/second will be set to midnight).
215 * Otherwise extract or create it.
217 wprintf("<TR><TD><B>End</B></TD><TD>\n");
218 if (t_start.is_date) {
222 p = icalcomponent_get_first_property(vevent,
223 ICAL_DTEND_PROPERTY);
225 t_end = icalproperty_get_dtend(p);
228 /* If this is not an all-day event and there is no
229 * end time specified, make the default one hour
230 * from the start time.
235 t_end = icaltime_normalize(t_end);
236 /* t_end = icaltime_from_timet(now, 0); */
239 display_icaltimetype_as_webform(&t_end, "dtend");
240 wprintf("</TD></TR>\n");
242 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
243 "<TEXTAREA NAME=\"description\" wrap=soft "
244 "ROWS=5 COLS=80 WIDTH=80>\n"
246 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
248 escputs((char *)icalproperty_get_comment(p));
250 wprintf("</TEXTAREA></TD></TR>");
252 /* For a new event, the user creating the event should be the
253 * organizer. Set this field accordingly.
255 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
257 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
258 icalcomponent_add_property(vevent,
259 icalproperty_new_organizer(organizer_string)
263 /* Determine who is the organizer of this event.
264 * We need to determine "me" or "not me."
266 organizer = icalcomponent_get_first_property(vevent,
267 ICAL_ORGANIZER_PROPERTY);
268 if (organizer != NULL) {
269 strcpy(organizer_string, icalproperty_get_organizer(organizer));
270 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
271 strcpy(organizer_string, &organizer_string[7]);
272 striplt(organizer_string);
273 lprintf(9, "ISME %s\n", organizer_string);
274 serv_printf("ISME %s", organizer_string);
276 lprintf(9, "%s\n", buf);
283 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
284 escputs(organizer_string);
285 if (organizer_is_me) {
286 wprintf(" <FONT SIZE=-1><I>"
287 "(you are the organizer)</I></FONT>\n");
291 * Transmit the organizer as a hidden field. We don't want the user
292 * to be able to change it, but we do want it fed back to the server,
293 * especially if this is a new event and there is no organizer already
294 * in the calendar object.
296 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
297 escputs(organizer_string);
300 wprintf("</TD></TR>\n");
303 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
305 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
307 /* No transparency found. Default to opaque (busy). */
308 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
310 icalcomponent_add_property(vevent, p);
314 v = icalproperty_get_value(p);
320 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
321 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
323 wprintf(">Free ");
325 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
326 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
330 wprintf("</TD></TR>\n");
333 wprintf("<TR><TD><B>Attendees</B><br />"
334 "<FONT SIZE=-2>(One per line)"
336 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
337 "ROWS=3 COLS=80 WIDTH=80>\n",
338 (organizer_is_me ? "" : "DISABLED ")
341 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
343 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
344 strcpy(attendee_string, icalproperty_get_attendee(attendee));
345 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
347 /* screen name or email address */
348 strcpy(attendee_string, &attendee_string[7]);
349 striplt(attendee_string);
350 if (i++) wprintf("\n");
351 escputs(attendee_string);
354 /* participant status */
355 partstat_as_string(buf, attendee);
359 wprintf("</TEXTAREA></TD></TR>\n");
361 /* Done with properties. */
362 wprintf("</TABLE>\n<CENTER>"
363 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
365 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
367 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
368 "VALUE=\"Check attendee availability\">\n"
370 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
374 wprintf("</FORM>\n");
376 wprintf("<script type=\"text/javascript\">"
378 "if (document.EventForm.alldayevent.checked) {"
379 "document.EventForm.dtstart_hour.value='0';"
380 "document.EventForm.dtstart_hour.disabled = true;"
381 "document.EventForm.dtstart_minute.value='0';"
382 "document.EventForm.dtstart_minute.disabled = true;"
383 "document.EventForm.dtend_hour.value='0';"
384 "document.EventForm.dtend_hour.disabled = true;"
385 "document.EventForm.dtend_minute.value='0';"
386 "document.EventForm.dtend_minute.disabled = true;"
387 "document.EventForm.dtend_month.disabled = true;"
388 "document.EventForm.dtend_day.disabled = true;"
389 "document.EventForm.dtend_year.disabled = true;"
392 "document.EventForm.dtstart_hour.disabled = false;"
393 "document.EventForm.dtstart_minute.disabled = false;"
394 "document.EventForm.dtend_hour.disabled = false;"
395 "document.EventForm.dtend_minute.disabled = false;"
396 "document.EventForm.dtend_month.disabled = false;"
397 "document.EventForm.dtend_day.disabled = false;"
398 "document.EventForm.dtend_year.disabled = false;"
404 wprintf("</td></tr></table></center>\n");
407 if (created_new_vevent) {
408 icalcomponent_free(vevent);
413 * Save an edited event
416 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
419 icalcomponent *vevent, *encaps;
420 int created_new_vevent = 0;
421 int all_day_event = 0;
422 struct icaltimetype event_start;
423 icalproperty *attendee = NULL;
424 char attendee_string[SIZ];
427 char form_attendees[SIZ];
428 char organizer_string[SIZ];
430 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
432 if (supplied_vevent != NULL) {
433 vevent = supplied_vevent;
434 /* If we're looking at a fully encapsulated VCALENDAR
435 * rather than a VEVENT component, attempt to use the first
436 * relevant VEVENT subcomponent. If there is none, the
437 * NULL returned by icalcomponent_get_first_component() will
438 * tell the next iteration of this function to create a
441 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
442 save_individual_event(
443 icalcomponent_get_first_component(
444 vevent, ICAL_VEVENT_COMPONENT
451 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
452 created_new_vevent = 1;
455 if ( (!strcasecmp(bstr("sc"), "Save"))
456 || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
458 /* Replace values in the component with ones from the form */
460 while (prop = icalcomponent_get_first_property(vevent,
461 ICAL_SUMMARY_PROPERTY), prop != NULL) {
462 icalcomponent_remove_property(vevent, prop);
463 icalproperty_free(prop);
465 icalcomponent_add_property(vevent,
466 icalproperty_new_summary(bstr("summary")));
468 while (prop = icalcomponent_get_first_property(vevent,
469 ICAL_LOCATION_PROPERTY), prop != NULL) {
470 icalcomponent_remove_property(vevent, prop);
471 icalproperty_free(prop);
473 icalcomponent_add_property(vevent,
474 icalproperty_new_location(bstr("location")));
476 while (prop = icalcomponent_get_first_property(vevent,
477 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
478 icalcomponent_remove_property(vevent, prop);
479 icalproperty_free(prop);
481 icalcomponent_add_property(vevent,
482 icalproperty_new_description(bstr("description")));
484 while (prop = icalcomponent_get_first_property(vevent,
485 ICAL_DTSTART_PROPERTY), prop != NULL) {
486 icalcomponent_remove_property(vevent, prop);
487 icalproperty_free(prop);
490 if (!strcmp(bstr("alldayevent"), "yes")) {
497 event_start = icaltime_from_webform("dtstart");
499 event_start.is_date = 1;
500 event_start.hour = 0;
501 event_start.minute = 0;
502 event_start.second = 0;
506 /* The following odd-looking snippet of code looks like it
507 * takes some unnecessary steps. It is done this way because
508 * libical incorrectly turns an "all day event" into a normal
509 * event starting at midnight (i.e. it serializes as date/time
510 * instead of just date) unless icalvalue_new_date() is used.
511 * So we force it, if this is an all day event.
513 prop = icalproperty_new_dtstart(event_start);
515 icalproperty_set_value(prop,
516 icalvalue_new_date(event_start)
520 if (prop) icalcomponent_add_property(vevent, prop);
521 else icalproperty_free(prop);
523 while (prop = icalcomponent_get_first_property(vevent,
524 ICAL_DTEND_PROPERTY), prop != NULL) {
525 icalcomponent_remove_property(vevent, prop);
526 icalproperty_free(prop);
528 while (prop = icalcomponent_get_first_property(vevent,
529 ICAL_DURATION_PROPERTY), prop != NULL) {
530 icalcomponent_remove_property(vevent, prop);
531 icalproperty_free(prop);
534 if (all_day_event == 0) {
535 icalcomponent_add_property(vevent,
536 icalproperty_new_dtend(icaltime_normalize(
537 icaltime_from_webform("dtend"))
542 /* See if transparency is indicated */
543 if (strlen(bstr("transp")) > 0) {
544 if (!strcasecmp(bstr("transp"), "opaque")) {
545 formtransp = ICAL_TRANSP_OPAQUE;
547 else if (!strcasecmp(bstr("transp"), "transparent")) {
548 formtransp = ICAL_TRANSP_TRANSPARENT;
551 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
553 icalcomponent_remove_property(vevent, prop);
554 icalproperty_free(prop);
557 lprintf(9, "adding new property...\n");
558 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
559 lprintf(9, "...added it.\n");
562 /* Give this event a UID if it doesn't have one. */
563 lprintf(9, "Give this event a UID if it doesn't have one.\n");
564 if (icalcomponent_get_first_property(vevent,
565 ICAL_UID_PROPERTY) == NULL) {
567 icalcomponent_add_property(vevent,
568 icalproperty_new_uid(buf)
572 /* Increment the sequence ID */
573 lprintf(9, "Increment the sequence ID\n");
574 while (prop = icalcomponent_get_first_property(vevent,
575 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
576 i = icalproperty_get_sequence(prop);
577 lprintf(9, "Sequence was %d\n", i);
578 if (i > sequence) sequence = i;
579 icalcomponent_remove_property(vevent, prop);
580 icalproperty_free(prop);
583 lprintf(9, "New sequence is %d. Adding...\n", sequence);
584 icalcomponent_add_property(vevent,
585 icalproperty_new_sequence(sequence)
588 /* Set the organizer, only if one does not already exist *and*
589 * the form is supplying one
591 lprintf(9, "Setting the organizer...\n");
592 strcpy(buf, bstr("organizer"));
593 if ( (icalcomponent_get_first_property(vevent,
594 ICAL_ORGANIZER_PROPERTY) == NULL)
595 && (strlen(buf) > 0) ) {
597 /* set new organizer */
598 sprintf(organizer_string, "MAILTO:%s", buf);
599 icalcomponent_add_property(vevent,
600 icalproperty_new_organizer(organizer_string)
606 * Add any new attendees listed in the web form
608 lprintf(9, "Add any new attendees\n");
610 /* First, strip out the parenthesized partstats. */
611 strcpy(form_attendees, bstr("attendees"));
612 stripout(form_attendees, '(', ')');
615 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
616 extract_token(buf, form_attendees, i, '\n');
618 if (strlen(buf) > 0) {
619 lprintf(9, "Attendee: <%s>\n", buf);
620 sprintf(attendee_string, "MAILTO:%s", buf);
623 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
624 if (!strcasecmp(attendee_string,
625 icalproperty_get_attendee(attendee)))
631 icalcomponent_add_property(vevent,
632 icalproperty_new_attendee(attendee_string)
639 * Remove any attendees *not* listed in the web form
641 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
642 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
643 strcpy(attendee_string, icalproperty_get_attendee(attendee));
644 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
645 strcpy(attendee_string, &attendee_string[7]);
646 striplt(attendee_string);
648 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
649 extract_token(buf, form_attendees, i, '\n');
651 if (!strcasecmp(buf, attendee_string)) ++foundit;
654 icalcomponent_remove_property(vevent, attendee);
655 icalproperty_free(attendee);
662 * Encapsulate event into full VCALENDAR component. Clone it first,
663 * for two reasons: one, it's easier to just free the whole thing
664 * when we're done instead of unbundling, but more importantly, we
665 * can't encapsulate something that may already be encapsulated
668 lprintf(9, "Encapsulating into full VCALENDAR component\n");
669 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
671 /* If the user clicked 'Save' then save it to the server. */
672 lprintf(9, "Serializing it for saving\n");
673 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
674 serv_puts("ENT0 1|||4|||1|");
677 serv_puts("Content-type: text/calendar");
679 serv_puts(icalcomponent_as_ical_string(encaps));
682 while (serv_gets(buf), strcmp(buf, "000")) {
683 lprintf(9, "ENT0 REPLY: %s\n", buf);
685 icalcomponent_free(encaps);
688 /* Or, check attendee availability if the user asked for that. */
689 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
691 /* Call this function, which does the real work */
692 check_attendee_availability(encaps);
694 /* This displays the form again, with our annotations */
695 display_edit_individual_event(encaps, msgnum);
697 icalcomponent_free(encaps);
703 * If the user clicked 'Delete' then delete it.
705 lprintf(9, "Checking to see if we have to delete an old event\n");
706 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
707 serv_printf("DELE %ld", atol(bstr("msgnum")));
711 if (created_new_vevent) {
712 icalcomponent_free(vevent);
715 /* If this was a save or deelete, go back to the calendar view. */
716 if (strcasecmp(bstr("sc"), "Check attendee availability")) {
722 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */