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("<script type=\"text/javascript\">"
96 "function grey_all_day() { "
97 "if (document.EventForm.alldayevent.checked) {"
98 "document.EventForm.dtstart_hour.value='0';"
99 "document.EventForm.dtstart_hour.disabled = true;"
100 "document.EventForm.dtstart_minute.value='0';"
101 "document.EventForm.dtstart_minute.disabled = true;"
102 "document.EventForm.dtend_hour.value='0';"
103 "document.EventForm.dtend_hour.disabled = true;"
104 "document.EventForm.dtend_minute.value='0';"
105 "document.EventForm.dtend_minute.disabled = true;"
106 "document.EventForm.dtend_month.disabled = true;"
107 "document.EventForm.dtend_day.disabled = true;"
108 "document.EventForm.dtend_year.disabled = true;"
111 "document.EventForm.dtstart_hour.disabled = false;"
112 "document.EventForm.dtstart_minute.disabled = false;"
113 "document.EventForm.dtend_hour.disabled = false;"
114 "document.EventForm.dtend_minute.disabled = false;"
115 "document.EventForm.dtend_month.disabled = false;"
116 "document.EventForm.dtend_day.disabled = false;"
117 "document.EventForm.dtend_year.disabled = false;"
124 wprintf("<div id=\"fix_scrollbar_bug\">"
125 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
127 /************************************************************
128 * Uncomment this to see the UID in calendar events for debugging
130 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
132 escputs((char *)icalproperty_get_comment(p));
135 wprintf("SEQUENCE == %d<br />\n", sequence);
136 *************************************************************/
138 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
140 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
142 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
144 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
146 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
148 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
151 /* Put it in a borderless table so it lines up nicely */
152 wprintf("<TABLE border=0 width=100%%>\n");
154 wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
155 "<INPUT TYPE=\"text\" NAME=\"summary\" "
156 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
157 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
159 escputs((char *)icalproperty_get_comment(p));
161 wprintf("\"></TD></TR>\n");
163 wprintf("<TR><TD><B>Location</B></TD><TD>\n"
164 "<INPUT TYPE=\"text\" NAME=\"location\" "
165 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
166 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
168 escputs((char *)icalproperty_get_comment(p));
170 wprintf("\"></TD></TR>\n");
172 wprintf("<TR><TD><B>Start</B></TD><TD>\n");
173 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
175 t_start = icalproperty_get_dtstart(p);
176 if (t_start.is_date) {
183 localtime_r(&now, &tm_now);
184 if (strlen(bstr("year")) > 0) {
185 tm_now.tm_year = atoi(bstr("year")) - 1900;
186 tm_now.tm_mon = atoi(bstr("month")) - 1;
187 tm_now.tm_mday = atoi(bstr("day"));
189 if (strlen(bstr("hour")) > 0) {
190 tm_now.tm_hour = atoi(bstr("hour"));
191 tm_now.tm_min = atoi(bstr("minute"));
200 t_start = icaltime_from_timet_with_zone(
202 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
203 icaltimezone_get_utc_timezone()
208 display_icaltimetype_as_webform(&t_start, "dtstart");
210 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
211 "VALUE=\"yes\" onClick=\"grey_all_day();\""
212 " %s >All day event",
213 (t_start.is_date ? "CHECKED" : "" )
216 wprintf("</TD></TR>\n");
218 /* If this is an all-day-event, set the end time to be identical to
219 * the start time (the hour/minute/second will be set to midnight).
220 * Otherwise extract or create it.
222 wprintf("<TR><TD><B>End</B></TD><TD>\n");
223 if (t_start.is_date) {
227 p = icalcomponent_get_first_property(vevent,
228 ICAL_DTEND_PROPERTY);
230 t_end = icalproperty_get_dtend(p);
233 /* If this is not an all-day event and there is no
234 * end time specified, make the default one hour
235 * from the start time.
240 t_end = icaltime_normalize(t_end);
241 /* t_end = icaltime_from_timet(now, 0); */
244 display_icaltimetype_as_webform(&t_end, "dtend");
245 wprintf("</TD></TR>\n");
247 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
248 "<TEXTAREA NAME=\"description\" wrap=soft "
249 "ROWS=5 COLS=80 WIDTH=80>\n"
251 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
253 escputs((char *)icalproperty_get_comment(p));
255 wprintf("</TEXTAREA></TD></TR>");
257 /* For a new event, the user creating the event should be the
258 * organizer. Set this field accordingly.
260 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
262 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
263 icalcomponent_add_property(vevent,
264 icalproperty_new_organizer(organizer_string)
268 /* Determine who is the organizer of this event.
269 * We need to determine "me" or "not me."
271 organizer = icalcomponent_get_first_property(vevent,
272 ICAL_ORGANIZER_PROPERTY);
273 if (organizer != NULL) {
274 strcpy(organizer_string, icalproperty_get_organizer(organizer));
275 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
276 strcpy(organizer_string, &organizer_string[7]);
277 striplt(organizer_string);
278 lprintf(9, "ISME %s\n", organizer_string);
279 serv_printf("ISME %s", organizer_string);
280 serv_getln(buf, sizeof buf);
281 lprintf(9, "%s\n", buf);
288 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
289 escputs(organizer_string);
290 if (organizer_is_me) {
291 wprintf(" <FONT SIZE=-1><I>"
292 "(you are the organizer)</I></FONT>\n");
296 * Transmit the organizer as a hidden field. We don't want the user
297 * to be able to change it, but we do want it fed back to the server,
298 * especially if this is a new event and there is no organizer already
299 * in the calendar object.
301 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
302 escputs(organizer_string);
305 wprintf("</TD></TR>\n");
308 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
310 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
312 /* No transparency found. Default to opaque (busy). */
313 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
315 icalcomponent_add_property(vevent, p);
319 v = icalproperty_get_value(p);
325 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
326 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
328 wprintf(">Free ");
330 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
331 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
335 wprintf("</TD></TR>\n");
338 wprintf("<TR><TD><B>Attendees</B><br />"
339 "<FONT SIZE=-2>(One per line)"
341 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
342 "ROWS=3 COLS=80 WIDTH=80>\n",
343 (organizer_is_me ? "" : "DISABLED ")
346 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
348 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
349 strcpy(attendee_string, icalproperty_get_attendee(attendee));
350 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
352 /* screen name or email address */
353 strcpy(attendee_string, &attendee_string[7]);
354 striplt(attendee_string);
355 if (i++) wprintf("\n");
356 escputs(attendee_string);
359 /* participant status */
360 partstat_as_string(buf, attendee);
364 wprintf("</TEXTAREA></TD></TR>\n");
366 /* Done with properties. */
367 wprintf("</TABLE>\n<CENTER>"
368 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
370 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
372 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
373 "VALUE=\"Check attendee availability\">\n"
375 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
379 wprintf("</FORM>\n");
381 wprintf("</td></tr></table></div>\n");
382 wprintf("<script type=\"text/javascript\">"
388 if (created_new_vevent) {
389 icalcomponent_free(vevent);
394 * Save an edited event
397 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
400 icalcomponent *vevent, *encaps;
401 int created_new_vevent = 0;
402 int all_day_event = 0;
403 struct icaltimetype event_start, t;
404 icalproperty *attendee = NULL;
405 char attendee_string[SIZ];
408 char form_attendees[SIZ];
409 char organizer_string[SIZ];
411 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
413 if (supplied_vevent != NULL) {
414 vevent = supplied_vevent;
415 /* If we're looking at a fully encapsulated VCALENDAR
416 * rather than a VEVENT component, attempt to use the first
417 * relevant VEVENT subcomponent. If there is none, the
418 * NULL returned by icalcomponent_get_first_component() will
419 * tell the next iteration of this function to create a
422 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
423 save_individual_event(
424 icalcomponent_get_first_component(
425 vevent, ICAL_VEVENT_COMPONENT
432 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
433 created_new_vevent = 1;
436 if ( (!strcasecmp(bstr("sc"), "Save"))
437 || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
439 /* Replace values in the component with ones from the form */
441 while (prop = icalcomponent_get_first_property(vevent,
442 ICAL_SUMMARY_PROPERTY), prop != NULL) {
443 icalcomponent_remove_property(vevent, prop);
444 icalproperty_free(prop);
446 icalcomponent_add_property(vevent,
447 icalproperty_new_summary(bstr("summary")));
449 while (prop = icalcomponent_get_first_property(vevent,
450 ICAL_LOCATION_PROPERTY), prop != NULL) {
451 icalcomponent_remove_property(vevent, prop);
452 icalproperty_free(prop);
454 icalcomponent_add_property(vevent,
455 icalproperty_new_location(bstr("location")));
457 while (prop = icalcomponent_get_first_property(vevent,
458 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
459 icalcomponent_remove_property(vevent, prop);
460 icalproperty_free(prop);
462 icalcomponent_add_property(vevent,
463 icalproperty_new_description(bstr("description")));
465 while (prop = icalcomponent_get_first_property(vevent,
466 ICAL_DTSTART_PROPERTY), prop != NULL) {
467 icalcomponent_remove_property(vevent, prop);
468 icalproperty_free(prop);
471 if (!strcmp(bstr("alldayevent"), "yes")) {
479 icaltime_from_webform_dateonly(&event_start, "dtstart");
482 icaltime_from_webform(&event_start, "dtstart");
485 /* The following odd-looking snippet of code looks like it
486 * takes some unnecessary steps. It is done this way because
487 * libical incorrectly turns an "all day event" into a normal
488 * event starting at midnight (i.e. it serializes as date/time
489 * instead of just date) unless icalvalue_new_date() is used.
490 * So we force it, if this is an all day event.
492 prop = icalproperty_new_dtstart(event_start);
494 icalproperty_set_value(prop,
495 icalvalue_new_date(event_start)
499 if (prop) icalcomponent_add_property(vevent, prop);
500 else icalproperty_free(prop);
502 while (prop = icalcomponent_get_first_property(vevent,
503 ICAL_DTEND_PROPERTY), prop != NULL) {
504 icalcomponent_remove_property(vevent, prop);
505 icalproperty_free(prop);
507 while (prop = icalcomponent_get_first_property(vevent,
508 ICAL_DURATION_PROPERTY), prop != NULL) {
509 icalcomponent_remove_property(vevent, prop);
510 icalproperty_free(prop);
513 if (all_day_event == 0) {
514 icaltime_from_webform(&t, "dtend");
515 icalcomponent_add_property(vevent,
516 icalproperty_new_dtend(icaltime_normalize(t)
521 /* See if transparency is indicated */
522 if (strlen(bstr("transp")) > 0) {
523 if (!strcasecmp(bstr("transp"), "opaque")) {
524 formtransp = ICAL_TRANSP_OPAQUE;
526 else if (!strcasecmp(bstr("transp"), "transparent")) {
527 formtransp = ICAL_TRANSP_TRANSPARENT;
530 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
532 icalcomponent_remove_property(vevent, prop);
533 icalproperty_free(prop);
536 lprintf(9, "adding new property...\n");
537 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
538 lprintf(9, "...added it.\n");
541 /* Give this event a UID if it doesn't have one. */
542 lprintf(9, "Give this event a UID if it doesn't have one.\n");
543 if (icalcomponent_get_first_property(vevent,
544 ICAL_UID_PROPERTY) == NULL) {
546 icalcomponent_add_property(vevent,
547 icalproperty_new_uid(buf)
551 /* Increment the sequence ID */
552 lprintf(9, "Increment the sequence ID\n");
553 while (prop = icalcomponent_get_first_property(vevent,
554 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
555 i = icalproperty_get_sequence(prop);
556 lprintf(9, "Sequence was %d\n", i);
557 if (i > sequence) sequence = i;
558 icalcomponent_remove_property(vevent, prop);
559 icalproperty_free(prop);
562 lprintf(9, "New sequence is %d. Adding...\n", sequence);
563 icalcomponent_add_property(vevent,
564 icalproperty_new_sequence(sequence)
567 /* Set the organizer, only if one does not already exist *and*
568 * the form is supplying one
570 lprintf(9, "Setting the organizer...\n");
571 strcpy(buf, bstr("organizer"));
572 if ( (icalcomponent_get_first_property(vevent,
573 ICAL_ORGANIZER_PROPERTY) == NULL)
574 && (strlen(buf) > 0) ) {
576 /* set new organizer */
577 sprintf(organizer_string, "MAILTO:%s", buf);
578 icalcomponent_add_property(vevent,
579 icalproperty_new_organizer(organizer_string)
585 * Add any new attendees listed in the web form
587 lprintf(9, "Add any new attendees\n");
589 /* First, strip out the parenthesized partstats. */
590 strcpy(form_attendees, bstr("attendees"));
591 stripout(form_attendees, '(', ')');
594 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
595 extract_token(buf, form_attendees, i, '\n', sizeof buf);
597 if (strlen(buf) > 0) {
598 lprintf(9, "Attendee: <%s>\n", buf);
599 sprintf(attendee_string, "MAILTO:%s", buf);
602 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
603 if (!strcasecmp(attendee_string,
604 icalproperty_get_attendee(attendee)))
610 icalcomponent_add_property(vevent,
611 icalproperty_new_attendee(attendee_string)
618 * Remove any attendees *not* listed in the web form
620 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
621 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
622 strcpy(attendee_string, icalproperty_get_attendee(attendee));
623 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
624 strcpy(attendee_string, &attendee_string[7]);
625 striplt(attendee_string);
627 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
628 extract_token(buf, form_attendees, i, '\n', sizeof buf);
630 if (!strcasecmp(buf, attendee_string)) ++foundit;
633 icalcomponent_remove_property(vevent, attendee);
634 icalproperty_free(attendee);
641 * Encapsulate event into full VCALENDAR component. Clone it first,
642 * for two reasons: one, it's easier to just free the whole thing
643 * when we're done instead of unbundling, but more importantly, we
644 * can't encapsulate something that may already be encapsulated
647 lprintf(9, "Encapsulating into full VCALENDAR component\n");
648 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
650 /* If the user clicked 'Save' then save it to the server. */
651 lprintf(9, "Serializing it for saving\n");
652 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
653 serv_puts("ENT0 1|||4|||1|");
654 serv_getln(buf, sizeof buf);
656 serv_puts("Content-type: text/calendar");
658 serv_puts(icalcomponent_as_ical_string(encaps));
661 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
662 lprintf(9, "ENT0 REPLY: %s\n", buf);
664 icalcomponent_free(encaps);
667 /* Or, check attendee availability if the user asked for that. */
668 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
670 /* Call this function, which does the real work */
671 check_attendee_availability(encaps);
673 /* This displays the form again, with our annotations */
674 display_edit_individual_event(encaps, msgnum);
676 icalcomponent_free(encaps);
682 * If the user clicked 'Delete' then delete it.
684 lprintf(9, "Checking to see if we have to delete an old event\n");
685 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
686 serv_printf("DELE %ld", atol(bstr("msgnum")));
687 serv_getln(buf, sizeof buf);
690 if (created_new_vevent) {
691 icalcomponent_free(vevent);
694 /* If this was a save or deelete, go back to the calendar view. */
695 if (strcasecmp(bstr("sc"), "Check attendee availability")) {
701 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */