4 * Editing calendar events.
12 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
15 * Display an event by itself (for editing)
17 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
18 icalcomponent *vevent;
21 struct icaltimetype t_start, t_end;
24 int created_new_vevent = 0;
25 icalproperty *organizer = NULL;
26 char organizer_string[SIZ];
27 icalproperty *attendee = NULL;
28 char attendee_string[SIZ];
30 int organizer_is_me = 0;
35 strcpy(organizer_string, "");
36 strcpy(attendee_string, "");
38 if (supplied_vevent != NULL) {
39 vevent = supplied_vevent;
40 /* If we're looking at a fully encapsulated VCALENDAR
41 * rather than a VEVENT component, attempt to use the first
42 * relevant VEVENT subcomponent. If there is none, the
43 * NULL returned by icalcomponent_get_first_component() will
44 * tell the next iteration of this function to create a
47 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
48 display_edit_individual_event(
49 icalcomponent_get_first_component(
50 vevent, ICAL_VEVENT_COMPONENT
57 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
58 created_new_vevent = 1;
61 /* Learn the sequence */
62 p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
64 sequence = icalproperty_get_sequence(p);
68 output_headers(1, 1, 2, 0, 0, 0, 0);
69 wprintf("<div id=\"banner\">\n"
70 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
71 "<SPAN CLASS=\"titlebar\">Add or edit an event</SPAN>"
72 "</TD></TR></TABLE>\n"
73 "</div>\n<div id=\"content\">\n"
76 wprintf("<script type=\"text/javascript\">"
77 "function grey_all_day() { "
78 "if (document.EventForm.alldayevent.checked) {"
79 "document.EventForm.dtstart_hour.value='0';"
80 "document.EventForm.dtstart_hour.disabled = true;"
81 "document.EventForm.dtstart_minute.value='0';"
82 "document.EventForm.dtstart_minute.disabled = true;"
83 "document.EventForm.dtend_hour.value='0';"
84 "document.EventForm.dtend_hour.disabled = true;"
85 "document.EventForm.dtend_minute.value='0';"
86 "document.EventForm.dtend_minute.disabled = true;"
87 "document.EventForm.dtend_month.disabled = true;"
88 "document.EventForm.dtend_day.disabled = true;"
89 "document.EventForm.dtend_year.disabled = true;"
92 "document.EventForm.dtstart_hour.disabled = false;"
93 "document.EventForm.dtstart_minute.disabled = false;"
94 "document.EventForm.dtend_hour.disabled = false;"
95 "document.EventForm.dtend_minute.disabled = false;"
96 "document.EventForm.dtend_month.disabled = false;"
97 "document.EventForm.dtend_day.disabled = false;"
98 "document.EventForm.dtend_year.disabled = false;"
105 wprintf("<div id=\"fix_scrollbar_bug\">"
106 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
108 /************************************************************
109 * Uncomment this to see the UID in calendar events for debugging
111 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
113 escputs((char *)icalproperty_get_comment(p));
116 wprintf("SEQUENCE == %d<br />\n", sequence);
117 *************************************************************/
119 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
121 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
123 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
125 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
127 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
129 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
132 /* Put it in a borderless table so it lines up nicely */
133 wprintf("<TABLE border=0 width=100%%>\n");
135 wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
136 "<INPUT TYPE=\"text\" NAME=\"summary\" "
137 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
138 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
140 escputs((char *)icalproperty_get_comment(p));
142 wprintf("\"></TD></TR>\n");
144 wprintf("<TR><TD><B>Location</B></TD><TD>\n"
145 "<INPUT TYPE=\"text\" NAME=\"location\" "
146 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
147 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
149 escputs((char *)icalproperty_get_comment(p));
151 wprintf("\"></TD></TR>\n");
153 wprintf("<TR><TD><B>Start</B></TD><TD>\n");
154 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
156 t_start = icalproperty_get_dtstart(p);
157 if (t_start.is_date) {
164 localtime_r(&now, &tm_now);
165 if (strlen(bstr("year")) > 0) {
166 tm_now.tm_year = atoi(bstr("year")) - 1900;
167 tm_now.tm_mon = atoi(bstr("month")) - 1;
168 tm_now.tm_mday = atoi(bstr("day"));
170 if (strlen(bstr("hour")) > 0) {
171 tm_now.tm_hour = atoi(bstr("hour"));
172 tm_now.tm_min = atoi(bstr("minute"));
181 t_start = icaltime_from_timet_with_zone(
183 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
184 icaltimezone_get_utc_timezone()
189 display_icaltimetype_as_webform(&t_start, "dtstart");
191 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
192 "VALUE=\"yes\" onClick=\"grey_all_day();\""
193 " %s >All day event",
194 (t_start.is_date ? "CHECKED" : "" )
197 wprintf("</TD></TR>\n");
199 /* If this is an all-day-event, set the end time to be identical to
200 * the start time (the hour/minute/second will be set to midnight).
201 * Otherwise extract or create it.
203 wprintf("<TR><TD><B>End</B></TD><TD>\n");
204 if (t_start.is_date) {
208 p = icalcomponent_get_first_property(vevent,
209 ICAL_DTEND_PROPERTY);
211 t_end = icalproperty_get_dtend(p);
214 /* If this is not an all-day event and there is no
215 * end time specified, make the default one hour
216 * from the start time.
221 t_end = icaltime_normalize(t_end);
222 /* t_end = icaltime_from_timet(now, 0); */
225 display_icaltimetype_as_webform(&t_end, "dtend");
226 wprintf("</TD></TR>\n");
228 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
229 "<TEXTAREA NAME=\"description\" wrap=soft "
230 "ROWS=5 COLS=80 WIDTH=80>\n"
232 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
234 escputs((char *)icalproperty_get_comment(p));
236 wprintf("</TEXTAREA></TD></TR>");
238 /* For a new event, the user creating the event should be the
239 * organizer. Set this field accordingly.
241 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
243 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
244 icalcomponent_add_property(vevent,
245 icalproperty_new_organizer(organizer_string)
249 /* Determine who is the organizer of this event.
250 * We need to determine "me" or "not me."
252 organizer = icalcomponent_get_first_property(vevent,
253 ICAL_ORGANIZER_PROPERTY);
254 if (organizer != NULL) {
255 strcpy(organizer_string, icalproperty_get_organizer(organizer));
256 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
257 strcpy(organizer_string, &organizer_string[7]);
258 striplt(organizer_string);
259 lprintf(9, "ISME %s\n", organizer_string);
260 serv_printf("ISME %s", organizer_string);
261 serv_getln(buf, sizeof buf);
262 lprintf(9, "%s\n", buf);
269 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
270 escputs(organizer_string);
271 if (organizer_is_me) {
272 wprintf(" <FONT SIZE=-1><I>"
273 "(you are the organizer)</I></FONT>\n");
277 * Transmit the organizer as a hidden field. We don't want the user
278 * to be able to change it, but we do want it fed back to the server,
279 * especially if this is a new event and there is no organizer already
280 * in the calendar object.
282 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
283 escputs(organizer_string);
286 wprintf("</TD></TR>\n");
289 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
291 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
293 /* No transparency found. Default to opaque (busy). */
294 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
296 icalcomponent_add_property(vevent, p);
300 v = icalproperty_get_value(p);
306 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
307 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
309 wprintf(">Free ");
311 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
312 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
316 wprintf("</TD></TR>\n");
319 wprintf("<TR><TD><B>Attendees</B><br />"
320 "<FONT SIZE=-2>(One per line)"
322 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
323 "ROWS=3 COLS=80 WIDTH=80>\n",
324 (organizer_is_me ? "" : "DISABLED ")
327 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
329 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
330 strcpy(attendee_string, icalproperty_get_attendee(attendee));
331 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
333 /* screen name or email address */
334 strcpy(attendee_string, &attendee_string[7]);
335 striplt(attendee_string);
336 if (i++) wprintf("\n");
337 escputs(attendee_string);
340 /* participant status */
341 partstat_as_string(buf, attendee);
345 wprintf("</TEXTAREA></TD></TR>\n");
347 /* Done with properties. */
348 wprintf("</TABLE>\n<CENTER>"
349 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
351 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
353 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
354 "VALUE=\"Check attendee availability\">\n"
356 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
360 wprintf("</FORM>\n");
362 wprintf("</td></tr></table></div>\n");
363 wprintf("<script type=\"text/javascript\">"
369 if (created_new_vevent) {
370 icalcomponent_free(vevent);
375 * Save an edited event
378 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
381 icalcomponent *vevent, *encaps;
382 int created_new_vevent = 0;
383 int all_day_event = 0;
384 struct icaltimetype event_start, t;
385 icalproperty *attendee = NULL;
386 char attendee_string[SIZ];
389 char form_attendees[SIZ];
390 char organizer_string[SIZ];
392 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
394 if (supplied_vevent != NULL) {
395 vevent = supplied_vevent;
396 /* If we're looking at a fully encapsulated VCALENDAR
397 * rather than a VEVENT component, attempt to use the first
398 * relevant VEVENT subcomponent. If there is none, the
399 * NULL returned by icalcomponent_get_first_component() will
400 * tell the next iteration of this function to create a
403 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
404 save_individual_event(
405 icalcomponent_get_first_component(
406 vevent, ICAL_VEVENT_COMPONENT
413 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
414 created_new_vevent = 1;
417 if ( (!strcasecmp(bstr("sc"), "Save"))
418 || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
420 /* Replace values in the component with ones from the form */
422 while (prop = icalcomponent_get_first_property(vevent,
423 ICAL_SUMMARY_PROPERTY), prop != NULL) {
424 icalcomponent_remove_property(vevent, prop);
425 icalproperty_free(prop);
427 icalcomponent_add_property(vevent,
428 icalproperty_new_summary(bstr("summary")));
430 while (prop = icalcomponent_get_first_property(vevent,
431 ICAL_LOCATION_PROPERTY), prop != NULL) {
432 icalcomponent_remove_property(vevent, prop);
433 icalproperty_free(prop);
435 icalcomponent_add_property(vevent,
436 icalproperty_new_location(bstr("location")));
438 while (prop = icalcomponent_get_first_property(vevent,
439 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
440 icalcomponent_remove_property(vevent, prop);
441 icalproperty_free(prop);
443 icalcomponent_add_property(vevent,
444 icalproperty_new_description(bstr("description")));
446 while (prop = icalcomponent_get_first_property(vevent,
447 ICAL_DTSTART_PROPERTY), prop != NULL) {
448 icalcomponent_remove_property(vevent, prop);
449 icalproperty_free(prop);
452 if (!strcmp(bstr("alldayevent"), "yes")) {
460 icaltime_from_webform_dateonly(&event_start, "dtstart");
463 icaltime_from_webform(&event_start, "dtstart");
466 /* The following odd-looking snippet of code looks like it
467 * takes some unnecessary steps. It is done this way because
468 * libical incorrectly turns an "all day event" into a normal
469 * event starting at midnight (i.e. it serializes as date/time
470 * instead of just date) unless icalvalue_new_date() is used.
471 * So we force it, if this is an all day event.
473 prop = icalproperty_new_dtstart(event_start);
475 icalproperty_set_value(prop,
476 icalvalue_new_date(event_start)
480 if (prop) icalcomponent_add_property(vevent, prop);
481 else icalproperty_free(prop);
483 while (prop = icalcomponent_get_first_property(vevent,
484 ICAL_DTEND_PROPERTY), prop != NULL) {
485 icalcomponent_remove_property(vevent, prop);
486 icalproperty_free(prop);
488 while (prop = icalcomponent_get_first_property(vevent,
489 ICAL_DURATION_PROPERTY), prop != NULL) {
490 icalcomponent_remove_property(vevent, prop);
491 icalproperty_free(prop);
494 if (all_day_event == 0) {
495 icaltime_from_webform(&t, "dtend");
496 icalcomponent_add_property(vevent,
497 icalproperty_new_dtend(icaltime_normalize(t)
502 /* See if transparency is indicated */
503 if (strlen(bstr("transp")) > 0) {
504 if (!strcasecmp(bstr("transp"), "opaque")) {
505 formtransp = ICAL_TRANSP_OPAQUE;
507 else if (!strcasecmp(bstr("transp"), "transparent")) {
508 formtransp = ICAL_TRANSP_TRANSPARENT;
511 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
513 icalcomponent_remove_property(vevent, prop);
514 icalproperty_free(prop);
517 lprintf(9, "adding new property...\n");
518 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
519 lprintf(9, "...added it.\n");
522 /* Give this event a UID if it doesn't have one. */
523 lprintf(9, "Give this event a UID if it doesn't have one.\n");
524 if (icalcomponent_get_first_property(vevent,
525 ICAL_UID_PROPERTY) == NULL) {
527 icalcomponent_add_property(vevent,
528 icalproperty_new_uid(buf)
532 /* Increment the sequence ID */
533 lprintf(9, "Increment the sequence ID\n");
534 while (prop = icalcomponent_get_first_property(vevent,
535 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
536 i = icalproperty_get_sequence(prop);
537 lprintf(9, "Sequence was %d\n", i);
538 if (i > sequence) sequence = i;
539 icalcomponent_remove_property(vevent, prop);
540 icalproperty_free(prop);
543 lprintf(9, "New sequence is %d. Adding...\n", sequence);
544 icalcomponent_add_property(vevent,
545 icalproperty_new_sequence(sequence)
548 /* Set the organizer, only if one does not already exist *and*
549 * the form is supplying one
551 lprintf(9, "Setting the organizer...\n");
552 strcpy(buf, bstr("organizer"));
553 if ( (icalcomponent_get_first_property(vevent,
554 ICAL_ORGANIZER_PROPERTY) == NULL)
555 && (strlen(buf) > 0) ) {
557 /* set new organizer */
558 sprintf(organizer_string, "MAILTO:%s", buf);
559 icalcomponent_add_property(vevent,
560 icalproperty_new_organizer(organizer_string)
566 * Add any new attendees listed in the web form
568 lprintf(9, "Add any new attendees\n");
570 /* First, strip out the parenthesized partstats. */
571 strcpy(form_attendees, bstr("attendees"));
572 stripout(form_attendees, '(', ')');
575 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
576 extract_token(buf, form_attendees, i, '\n', sizeof buf);
578 if (strlen(buf) > 0) {
579 lprintf(9, "Attendee: <%s>\n", buf);
580 sprintf(attendee_string, "MAILTO:%s", buf);
583 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
584 if (!strcasecmp(attendee_string,
585 icalproperty_get_attendee(attendee)))
591 icalcomponent_add_property(vevent,
592 icalproperty_new_attendee(attendee_string)
599 * Remove any attendees *not* listed in the web form
601 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
602 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
603 strcpy(attendee_string, icalproperty_get_attendee(attendee));
604 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
605 strcpy(attendee_string, &attendee_string[7]);
606 striplt(attendee_string);
608 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
609 extract_token(buf, form_attendees, i, '\n', sizeof buf);
611 if (!strcasecmp(buf, attendee_string)) ++foundit;
614 icalcomponent_remove_property(vevent, attendee);
615 icalproperty_free(attendee);
622 * Encapsulate event into full VCALENDAR component. Clone it first,
623 * for two reasons: one, it's easier to just free the whole thing
624 * when we're done instead of unbundling, but more importantly, we
625 * can't encapsulate something that may already be encapsulated
628 lprintf(9, "Encapsulating into full VCALENDAR component\n");
629 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
631 /* If the user clicked 'Save' then save it to the server. */
632 lprintf(9, "Serializing it for saving\n");
633 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
634 serv_puts("ENT0 1|||4|||1|");
635 serv_getln(buf, sizeof buf);
637 serv_puts("Content-type: text/calendar");
639 serv_puts(icalcomponent_as_ical_string(encaps));
642 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
643 lprintf(9, "ENT0 REPLY: %s\n", buf);
645 icalcomponent_free(encaps);
648 /* Or, check attendee availability if the user asked for that. */
649 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
651 /* Call this function, which does the real work */
652 check_attendee_availability(encaps);
654 /* This displays the form again, with our annotations */
655 display_edit_individual_event(encaps, msgnum);
657 icalcomponent_free(encaps);
663 * If the user clicked 'Delete' then delete it.
665 lprintf(9, "Checking to see if we have to delete an old event\n");
666 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
667 serv_printf("DELE %ld", atol(bstr("msgnum")));
668 serv_getln(buf, sizeof buf);
671 if (created_new_vevent) {
672 icalcomponent_free(vevent);
675 /* If this was a save or deelete, go back to the calendar view. */
676 if (strcasecmp(bstr("sc"), "Check attendee availability")) {
682 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */