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, 0, 0, 0, 0, 0);
88 do_template("beginbox_nt");
89 wprintf("<h3> <IMG ALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
90 " Add or edit an event</h3>\n");
92 /************************************************************
93 * Uncomment this to see the UID in calendar events for debugging
95 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
97 escputs((char *)icalproperty_get_comment(p));
100 wprintf("SEQUENCE == %d<br />\n", sequence);
101 *************************************************************/
103 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
105 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
107 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
109 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
111 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
113 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
116 /* Put it in a borderless table so it lines up nicely */
117 wprintf("<TABLE border=0 width=100%%>\n");
119 wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
120 "<INPUT TYPE=\"text\" NAME=\"summary\" "
121 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
122 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
124 escputs((char *)icalproperty_get_comment(p));
126 wprintf("\"></TD></TR>\n");
128 wprintf("<TR><TD><B>Location</B></TD><TD>\n"
129 "<INPUT TYPE=\"text\" NAME=\"location\" "
130 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
131 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
133 escputs((char *)icalproperty_get_comment(p));
135 wprintf("\"></TD></TR>\n");
137 wprintf("<TR><TD><B>Start</B></TD><TD>\n");
138 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
140 t_start = icalproperty_get_dtstart(p);
141 if (t_start.is_date) {
148 localtime_r(&now, &tm_now);
149 if (strlen(bstr("year")) > 0) {
150 tm_now.tm_year = atoi(bstr("year")) - 1900;
151 tm_now.tm_mon = atoi(bstr("month")) - 1;
152 tm_now.tm_mday = atoi(bstr("day"));
154 if (strlen(bstr("hour")) > 0) {
155 tm_now.tm_hour = atoi(bstr("hour"));
156 tm_now.tm_min = atoi(bstr("minute"));
165 t_start = icaltime_from_timet_with_zone(
167 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
168 icaltimezone_get_utc_timezone()
173 display_icaltimetype_as_webform(&t_start, "dtstart");
175 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
176 "VALUE=\"yes\" onClick=\""
178 " if (this.checked) { "
179 " this.form.dtstart_hour.value='0'; "
180 " this.form.dtstart_hour.disabled = true; "
181 " this.form.dtstart_minute.value='0'; "
182 " this.form.dtstart_minute.disabled = true; "
183 " this.form.dtend_hour.value='0'; "
184 " this.form.dtend_hour.disabled = true; "
185 " this.form.dtend_minute.value='0'; "
186 " this.form.dtend_minute.disabled = true; "
187 " this.form.dtend_month.disabled = true; "
188 " this.form.dtend_day.disabled = true; "
189 " this.form.dtend_year.disabled = true; "
192 " this.form.dtstart_hour.disabled = false; "
193 " this.form.dtstart_minute.disabled = false; "
194 " this.form.dtend_hour.disabled = false; "
195 " this.form.dtend_minute.disabled = false; "
196 " this.form.dtend_month.disabled = false; "
197 " this.form.dtend_day.disabled = false; "
198 " this.form.dtend_year.disabled = false; "
202 " \" %s >All day event",
203 (t_start.is_date ? "CHECKED" : "" )
206 wprintf("</TD></TR>\n");
208 /* If this is an all-day-event, set the end time to be identical to
209 * the start time (the hour/minute/second will be set to midnight).
210 * Otherwise extract or create it.
212 wprintf("<TR><TD><B>End</B></TD><TD>\n");
213 if (t_start.is_date) {
217 p = icalcomponent_get_first_property(vevent,
218 ICAL_DTEND_PROPERTY);
220 t_end = icalproperty_get_dtend(p);
223 /* If this is not an all-day event and there is no
224 * end time specified, make the default one hour
225 * from the start time.
230 t_end = icaltime_normalize(t_end);
231 /* t_end = icaltime_from_timet(now, 0); */
234 display_icaltimetype_as_webform(&t_end, "dtend");
235 wprintf("</TD></TR>\n");
237 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
238 "<TEXTAREA NAME=\"description\" wrap=soft "
239 "ROWS=5 COLS=80 WIDTH=80>\n"
241 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
243 escputs((char *)icalproperty_get_comment(p));
245 wprintf("</TEXTAREA></TD></TR>");
247 /* For a new event, the user creating the event should be the
248 * organizer. Set this field accordingly.
250 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
252 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
253 icalcomponent_add_property(vevent,
254 icalproperty_new_organizer(organizer_string)
258 /* Determine who is the organizer of this event.
259 * We need to determine "me" or "not me."
261 organizer = icalcomponent_get_first_property(vevent,
262 ICAL_ORGANIZER_PROPERTY);
263 if (organizer != NULL) {
264 strcpy(organizer_string, icalproperty_get_organizer(organizer));
265 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
266 strcpy(organizer_string, &organizer_string[7]);
267 striplt(organizer_string);
268 lprintf(9, "ISME %s\n", organizer_string);
269 serv_printf("ISME %s", organizer_string);
271 lprintf(9, "%s\n", buf);
278 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
279 escputs(organizer_string);
280 if (organizer_is_me) {
281 wprintf(" <FONT SIZE=-1><I>"
282 "(you are the organizer)</I></FONT>\n");
286 * Transmit the organizer as a hidden field. We don't want the user
287 * to be able to change it, but we do want it fed back to the server,
288 * especially if this is a new event and there is no organizer already
289 * in the calendar object.
291 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
292 escputs(organizer_string);
295 wprintf("</TD></TR>\n");
298 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
300 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
302 /* No transparency found. Default to opaque (busy). */
303 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
305 icalcomponent_add_property(vevent, p);
309 v = icalproperty_get_value(p);
315 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
316 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
318 wprintf(">Free ");
320 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
321 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
325 wprintf("</TD></TR>\n");
328 wprintf("<TR><TD><B>Attendees</B><br />"
329 "<FONT SIZE=-2>(One per line)"
331 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
332 "ROWS=3 COLS=80 WIDTH=80>\n",
333 (organizer_is_me ? "" : "DISABLED ")
336 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
338 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
339 strcpy(attendee_string, icalproperty_get_attendee(attendee));
340 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
342 /* screen name or email address */
343 strcpy(attendee_string, &attendee_string[7]);
344 striplt(attendee_string);
345 if (i++) wprintf("\n");
346 escputs(attendee_string);
349 /* participant status */
350 partstat_as_string(buf, attendee);
354 wprintf("</TEXTAREA></TD></TR>\n");
356 /* Done with properties. */
357 wprintf("</TABLE>\n<CENTER>"
358 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
360 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
362 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
363 "VALUE=\"Check attendee availability\">\n"
365 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
369 wprintf("</FORM>\n");
371 wprintf("<script type=\"text/javascript\">"
373 "if (document.EventForm.alldayevent.checked) {"
374 "document.EventForm.dtstart_hour.value='0';"
375 "document.EventForm.dtstart_hour.disabled = true;"
376 "document.EventForm.dtstart_minute.value='0';"
377 "document.EventForm.dtstart_minute.disabled = true;"
378 "document.EventForm.dtend_hour.value='0';"
379 "document.EventForm.dtend_hour.disabled = true;"
380 "document.EventForm.dtend_minute.value='0';"
381 "document.EventForm.dtend_minute.disabled = true;"
382 "document.EventForm.dtend_month.disabled = true;"
383 "document.EventForm.dtend_day.disabled = true;"
384 "document.EventForm.dtend_year.disabled = true;"
387 "document.EventForm.dtstart_hour.disabled = false;"
388 "document.EventForm.dtstart_minute.disabled = false;"
389 "document.EventForm.dtend_hour.disabled = false;"
390 "document.EventForm.dtend_minute.disabled = false;"
391 "document.EventForm.dtend_month.disabled = false;"
392 "document.EventForm.dtend_day.disabled = false;"
393 "document.EventForm.dtend_year.disabled = false;"
399 do_template("endbox");
402 if (created_new_vevent) {
403 icalcomponent_free(vevent);
408 * Save an edited event
411 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
414 icalcomponent *vevent, *encaps;
415 int created_new_vevent = 0;
416 int all_day_event = 0;
417 struct icaltimetype event_start;
418 icalproperty *attendee = NULL;
419 char attendee_string[SIZ];
422 char form_attendees[SIZ];
423 char organizer_string[SIZ];
425 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
427 if (supplied_vevent != NULL) {
428 vevent = supplied_vevent;
429 /* If we're looking at a fully encapsulated VCALENDAR
430 * rather than a VEVENT component, attempt to use the first
431 * relevant VEVENT subcomponent. If there is none, the
432 * NULL returned by icalcomponent_get_first_component() will
433 * tell the next iteration of this function to create a
436 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
437 save_individual_event(
438 icalcomponent_get_first_component(
439 vevent, ICAL_VEVENT_COMPONENT
446 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
447 created_new_vevent = 1;
450 if ( (!strcasecmp(bstr("sc"), "Save"))
451 || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
453 /* Replace values in the component with ones from the form */
455 while (prop = icalcomponent_get_first_property(vevent,
456 ICAL_SUMMARY_PROPERTY), prop != NULL) {
457 icalcomponent_remove_property(vevent, prop);
458 icalproperty_free(prop);
460 icalcomponent_add_property(vevent,
461 icalproperty_new_summary(bstr("summary")));
463 while (prop = icalcomponent_get_first_property(vevent,
464 ICAL_LOCATION_PROPERTY), prop != NULL) {
465 icalcomponent_remove_property(vevent, prop);
466 icalproperty_free(prop);
468 icalcomponent_add_property(vevent,
469 icalproperty_new_location(bstr("location")));
471 while (prop = icalcomponent_get_first_property(vevent,
472 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
473 icalcomponent_remove_property(vevent, prop);
474 icalproperty_free(prop);
476 icalcomponent_add_property(vevent,
477 icalproperty_new_description(bstr("description")));
479 while (prop = icalcomponent_get_first_property(vevent,
480 ICAL_DTSTART_PROPERTY), prop != NULL) {
481 icalcomponent_remove_property(vevent, prop);
482 icalproperty_free(prop);
485 if (!strcmp(bstr("alldayevent"), "yes")) {
492 event_start = icaltime_from_webform("dtstart");
494 event_start.is_date = 1;
495 event_start.hour = 0;
496 event_start.minute = 0;
497 event_start.second = 0;
501 /* The following odd-looking snippet of code looks like it
502 * takes some unnecessary steps. It is done this way because
503 * libical incorrectly turns an "all day event" into a normal
504 * event starting at midnight (i.e. it serializes as date/time
505 * instead of just date) unless icalvalue_new_date() is used.
506 * So we force it, if this is an all day event.
508 prop = icalproperty_new_dtstart(event_start);
510 icalproperty_set_value(prop,
511 icalvalue_new_date(event_start)
515 if (prop) icalcomponent_add_property(vevent, prop);
516 else icalproperty_free(prop);
518 while (prop = icalcomponent_get_first_property(vevent,
519 ICAL_DTEND_PROPERTY), prop != NULL) {
520 icalcomponent_remove_property(vevent, prop);
521 icalproperty_free(prop);
523 while (prop = icalcomponent_get_first_property(vevent,
524 ICAL_DURATION_PROPERTY), prop != NULL) {
525 icalcomponent_remove_property(vevent, prop);
526 icalproperty_free(prop);
529 if (all_day_event == 0) {
530 icalcomponent_add_property(vevent,
531 icalproperty_new_dtend(icaltime_normalize(
532 icaltime_from_webform("dtend"))
537 /* See if transparency is indicated */
538 if (strlen(bstr("transp")) > 0) {
539 if (!strcasecmp(bstr("transp"), "opaque")) {
540 formtransp = ICAL_TRANSP_OPAQUE;
542 else if (!strcasecmp(bstr("transp"), "transparent")) {
543 formtransp = ICAL_TRANSP_TRANSPARENT;
546 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
548 icalcomponent_remove_property(vevent, prop);
549 icalproperty_free(prop);
552 lprintf(9, "adding new property...\n");
553 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
554 lprintf(9, "...added it.\n");
557 /* Give this event a UID if it doesn't have one. */
558 lprintf(9, "Give this event a UID if it doesn't have one.\n");
559 if (icalcomponent_get_first_property(vevent,
560 ICAL_UID_PROPERTY) == NULL) {
562 icalcomponent_add_property(vevent,
563 icalproperty_new_uid(buf)
567 /* Increment the sequence ID */
568 lprintf(9, "Increment the sequence ID\n");
569 while (prop = icalcomponent_get_first_property(vevent,
570 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
571 i = icalproperty_get_sequence(prop);
572 lprintf(9, "Sequence was %d\n", i);
573 if (i > sequence) sequence = i;
574 icalcomponent_remove_property(vevent, prop);
575 icalproperty_free(prop);
578 lprintf(9, "New sequence is %d. Adding...\n", sequence);
579 icalcomponent_add_property(vevent,
580 icalproperty_new_sequence(sequence)
583 /* Set the organizer, only if one does not already exist *and*
584 * the form is supplying one
586 lprintf(9, "Setting the organizer...\n");
587 strcpy(buf, bstr("organizer"));
588 if ( (icalcomponent_get_first_property(vevent,
589 ICAL_ORGANIZER_PROPERTY) == NULL)
590 && (strlen(buf) > 0) ) {
592 /* set new organizer */
593 sprintf(organizer_string, "MAILTO:%s", buf);
594 icalcomponent_add_property(vevent,
595 icalproperty_new_organizer(organizer_string)
601 * Add any new attendees listed in the web form
603 lprintf(9, "Add any new attendees\n");
605 /* First, strip out the parenthesized partstats. */
606 strcpy(form_attendees, bstr("attendees"));
607 stripout(form_attendees, '(', ')');
610 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
611 extract_token(buf, form_attendees, i, '\n');
613 if (strlen(buf) > 0) {
614 lprintf(9, "Attendee: <%s>\n", buf);
615 sprintf(attendee_string, "MAILTO:%s", buf);
618 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
619 if (!strcasecmp(attendee_string,
620 icalproperty_get_attendee(attendee)))
626 icalcomponent_add_property(vevent,
627 icalproperty_new_attendee(attendee_string)
634 * Remove any attendees *not* listed in the web form
636 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
637 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
638 strcpy(attendee_string, icalproperty_get_attendee(attendee));
639 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
640 strcpy(attendee_string, &attendee_string[7]);
641 striplt(attendee_string);
643 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
644 extract_token(buf, form_attendees, i, '\n');
646 if (!strcasecmp(buf, attendee_string)) ++foundit;
649 icalcomponent_remove_property(vevent, attendee);
650 icalproperty_free(attendee);
657 * Encapsulate event into full VCALENDAR component. Clone it first,
658 * for two reasons: one, it's easier to just free the whole thing
659 * when we're done instead of unbundling, but more importantly, we
660 * can't encapsulate something that may already be encapsulated
663 lprintf(9, "Encapsulating into full VCALENDAR component\n");
664 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
666 /* If the user clicked 'Save' then save it to the server. */
667 lprintf(9, "Serializing it for saving\n");
668 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
669 serv_puts("ENT0 1|||4");
672 serv_puts("Content-type: text/calendar");
674 serv_puts(icalcomponent_as_ical_string(encaps));
677 icalcomponent_free(encaps);
680 /* Or, check attendee availability if the user asked for that. */
681 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
683 /* Call this function, which does the real work */
684 check_attendee_availability(encaps);
686 /* This displays the form again, with our annotations */
687 display_edit_individual_event(encaps, msgnum);
689 icalcomponent_free(encaps);
695 * If the user clicked 'Delete' then delete it.
697 lprintf(9, "Checking to see if we have to delete an old event\n");
698 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
699 serv_printf("DELE %ld", atol(bstr("msgnum")));
703 if (created_new_vevent) {
704 icalcomponent_free(vevent);
707 /* If this was a save or deelete, go back to the calendar view. */
708 if (strcasecmp(bstr("sc"), "Check attendee availability")) {
714 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */