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 lprintf(9, "NOW NOW NOW!!!! %ld\n", now);
150 if (strlen(bstr("year")) > 0) {
151 tm_now.tm_year = atoi(bstr("year")) - 1900;
152 tm_now.tm_mon = atoi(bstr("month")) - 1;
153 tm_now.tm_mday = atoi(bstr("day"));
155 if (strlen(bstr("hour")) > 0) {
156 tm_now.tm_hour = atoi(bstr("hour"));
157 tm_now.tm_min = atoi(bstr("minute"));
166 t_start = icaltime_from_timet_with_zone(
168 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
169 icaltimezone_get_utc_timezone()
174 display_icaltimetype_as_webform(&t_start, "dtstart");
176 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
177 "VALUE=\"yes\" onClick=\""
179 " if (this.checked) { "
180 " this.form.dtstart_hour.value='0'; "
181 " this.form.dtstart_hour.disabled = true; "
182 " this.form.dtstart_minute.value='0'; "
183 " this.form.dtstart_minute.disabled = true; "
184 " this.form.dtend_hour.value='0'; "
185 " this.form.dtend_hour.disabled = true; "
186 " this.form.dtend_minute.value='0'; "
187 " this.form.dtend_minute.disabled = true; "
188 " this.form.dtend_month.disabled = true; "
189 " this.form.dtend_day.disabled = true; "
190 " this.form.dtend_year.disabled = true; "
193 " this.form.dtstart_hour.disabled = false; "
194 " this.form.dtstart_minute.disabled = false; "
195 " this.form.dtend_hour.disabled = false; "
196 " this.form.dtend_minute.disabled = false; "
197 " this.form.dtend_month.disabled = false; "
198 " this.form.dtend_day.disabled = false; "
199 " this.form.dtend_year.disabled = false; "
203 " \" %s >All day event",
204 (t_start.is_date ? "CHECKED" : "" )
207 wprintf("</TD></TR>\n");
209 /* If this is an all-day-event, set the end time to be identical to
210 * the start time (the hour/minute/second will be set to midnight).
211 * Otherwise extract or create it.
213 wprintf("<TR><TD><B>End</B></TD><TD>\n");
214 if (t_start.is_date) {
218 p = icalcomponent_get_first_property(vevent,
219 ICAL_DTEND_PROPERTY);
221 t_end = icalproperty_get_dtend(p);
224 /* If this is not an all-day event and there is no
225 * end time specified, make the default one hour
226 * from the start time.
231 t_end = icaltime_normalize(t_end);
232 /* t_end = icaltime_from_timet(now, 0); */
235 display_icaltimetype_as_webform(&t_end, "dtend");
236 wprintf("</TD></TR>\n");
238 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
239 "<TEXTAREA NAME=\"description\" wrap=soft "
240 "ROWS=5 COLS=80 WIDTH=80>\n"
242 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
244 escputs((char *)icalproperty_get_comment(p));
246 wprintf("</TEXTAREA></TD></TR>");
248 /* For a new event, the user creating the event should be the
249 * organizer. Set this field accordingly.
251 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
253 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
254 icalcomponent_add_property(vevent,
255 icalproperty_new_organizer(organizer_string)
259 /* Determine who is the organizer of this event.
260 * We need to determine "me" or "not me."
262 organizer = icalcomponent_get_first_property(vevent,
263 ICAL_ORGANIZER_PROPERTY);
264 if (organizer != NULL) {
265 strcpy(organizer_string, icalproperty_get_organizer(organizer));
266 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
267 strcpy(organizer_string, &organizer_string[7]);
268 striplt(organizer_string);
269 lprintf(9, "ISME %s\n", organizer_string);
270 serv_printf("ISME %s", organizer_string);
272 lprintf(9, "%s\n", buf);
279 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
280 escputs(organizer_string);
281 if (organizer_is_me) {
282 wprintf(" <FONT SIZE=-1><I>"
283 "(you are the organizer)</I></FONT>\n");
287 * Transmit the organizer as a hidden field. We don't want the user
288 * to be able to change it, but we do want it fed back to the server,
289 * especially if this is a new event and there is no organizer already
290 * in the calendar object.
292 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
293 escputs(organizer_string);
296 wprintf("</TD></TR>\n");
299 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
301 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
303 /* No transparency found. Default to opaque (busy). */
304 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
306 icalcomponent_add_property(vevent, p);
310 v = icalproperty_get_value(p);
316 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
317 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
319 wprintf(">Free ");
321 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
322 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
326 wprintf("</TD></TR>\n");
329 wprintf("<TR><TD><B>Attendees</B><br />"
330 "<FONT SIZE=-2>(One per line)"
332 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
333 "ROWS=3 COLS=80 WIDTH=80>\n",
334 (organizer_is_me ? "" : "DISABLED ")
337 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
339 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
340 strcpy(attendee_string, icalproperty_get_attendee(attendee));
341 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
343 /* screen name or email address */
344 strcpy(attendee_string, &attendee_string[7]);
345 striplt(attendee_string);
346 if (i++) wprintf("\n");
347 escputs(attendee_string);
350 /* participant status */
351 partstat_as_string(buf, attendee);
355 wprintf("</TEXTAREA></TD></TR>\n");
357 /* Done with properties. */
358 wprintf("</TABLE>\n<CENTER>"
359 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
361 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
363 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
364 "VALUE=\"Check attendee availability\">\n"
366 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
370 wprintf("</FORM>\n");
372 wprintf("<script type=\"text/javascript\">"
374 "if (document.EventForm.alldayevent.checked) {"
375 "document.EventForm.dtstart_hour.value='0';"
376 "document.EventForm.dtstart_hour.disabled = true;"
377 "document.EventForm.dtstart_minute.value='0';"
378 "document.EventForm.dtstart_minute.disabled = true;"
379 "document.EventForm.dtend_hour.value='0';"
380 "document.EventForm.dtend_hour.disabled = true;"
381 "document.EventForm.dtend_minute.value='0';"
382 "document.EventForm.dtend_minute.disabled = true;"
383 "document.EventForm.dtend_month.disabled = true;"
384 "document.EventForm.dtend_day.disabled = true;"
385 "document.EventForm.dtend_year.disabled = true;"
388 "document.EventForm.dtstart_hour.disabled = false;"
389 "document.EventForm.dtstart_minute.disabled = false;"
390 "document.EventForm.dtend_hour.disabled = false;"
391 "document.EventForm.dtend_minute.disabled = false;"
392 "document.EventForm.dtend_month.disabled = false;"
393 "document.EventForm.dtend_day.disabled = false;"
394 "document.EventForm.dtend_year.disabled = false;"
400 do_template("endbox");
403 if (created_new_vevent) {
404 icalcomponent_free(vevent);
409 * Save an edited event
412 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
415 icalcomponent *vevent, *encaps;
416 int created_new_vevent = 0;
417 int all_day_event = 0;
418 struct icaltimetype event_start;
419 icalproperty *attendee = NULL;
420 char attendee_string[SIZ];
423 char form_attendees[SIZ];
424 char organizer_string[SIZ];
426 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
428 if (supplied_vevent != NULL) {
429 vevent = supplied_vevent;
430 /* If we're looking at a fully encapsulated VCALENDAR
431 * rather than a VEVENT component, attempt to use the first
432 * relevant VEVENT subcomponent. If there is none, the
433 * NULL returned by icalcomponent_get_first_component() will
434 * tell the next iteration of this function to create a
437 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
438 save_individual_event(
439 icalcomponent_get_first_component(
440 vevent, ICAL_VEVENT_COMPONENT
447 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
448 created_new_vevent = 1;
451 if ( (!strcasecmp(bstr("sc"), "Save"))
452 || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
454 /* Replace values in the component with ones from the form */
456 while (prop = icalcomponent_get_first_property(vevent,
457 ICAL_SUMMARY_PROPERTY), prop != NULL) {
458 icalcomponent_remove_property(vevent, prop);
459 icalproperty_free(prop);
461 icalcomponent_add_property(vevent,
462 icalproperty_new_summary(bstr("summary")));
464 while (prop = icalcomponent_get_first_property(vevent,
465 ICAL_LOCATION_PROPERTY), prop != NULL) {
466 icalcomponent_remove_property(vevent, prop);
467 icalproperty_free(prop);
469 icalcomponent_add_property(vevent,
470 icalproperty_new_location(bstr("location")));
472 while (prop = icalcomponent_get_first_property(vevent,
473 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
474 icalcomponent_remove_property(vevent, prop);
475 icalproperty_free(prop);
477 icalcomponent_add_property(vevent,
478 icalproperty_new_description(bstr("description")));
480 while (prop = icalcomponent_get_first_property(vevent,
481 ICAL_DTSTART_PROPERTY), prop != NULL) {
482 icalcomponent_remove_property(vevent, prop);
483 icalproperty_free(prop);
486 if (!strcmp(bstr("alldayevent"), "yes")) {
493 event_start = icaltime_from_webform("dtstart");
495 event_start.is_date = 1;
496 event_start.hour = 0;
497 event_start.minute = 0;
498 event_start.second = 0;
502 /* The following odd-looking snippet of code looks like it
503 * takes some unnecessary steps. It is done this way because
504 * libical incorrectly turns an "all day event" into a normal
505 * event starting at midnight (i.e. it serializes as date/time
506 * instead of just date) unless icalvalue_new_date() is used.
507 * So we force it, if this is an all day event.
509 prop = icalproperty_new_dtstart(event_start);
511 icalproperty_set_value(prop,
512 icalvalue_new_date(event_start)
516 if (prop) icalcomponent_add_property(vevent, prop);
517 else icalproperty_free(prop);
519 while (prop = icalcomponent_get_first_property(vevent,
520 ICAL_DTEND_PROPERTY), prop != NULL) {
521 icalcomponent_remove_property(vevent, prop);
522 icalproperty_free(prop);
524 while (prop = icalcomponent_get_first_property(vevent,
525 ICAL_DURATION_PROPERTY), prop != NULL) {
526 icalcomponent_remove_property(vevent, prop);
527 icalproperty_free(prop);
530 if (all_day_event == 0) {
531 icalcomponent_add_property(vevent,
532 icalproperty_new_dtend(icaltime_normalize(
533 icaltime_from_webform("dtend"))
538 /* See if transparency is indicated */
539 if (strlen(bstr("transp")) > 0) {
540 if (!strcasecmp(bstr("transp"), "opaque")) {
541 formtransp = ICAL_TRANSP_OPAQUE;
543 else if (!strcasecmp(bstr("transp"), "transparent")) {
544 formtransp = ICAL_TRANSP_TRANSPARENT;
547 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
549 icalcomponent_remove_property(vevent, prop);
550 icalproperty_free(prop);
553 lprintf(9, "adding new property...\n");
554 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
555 lprintf(9, "...added it.\n");
558 /* Give this event a UID if it doesn't have one. */
559 lprintf(9, "Give this event a UID if it doesn't have one.\n");
560 if (icalcomponent_get_first_property(vevent,
561 ICAL_UID_PROPERTY) == NULL) {
563 icalcomponent_add_property(vevent,
564 icalproperty_new_uid(buf)
568 /* Increment the sequence ID */
569 lprintf(9, "Increment the sequence ID\n");
570 while (prop = icalcomponent_get_first_property(vevent,
571 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
572 i = icalproperty_get_sequence(prop);
573 lprintf(9, "Sequence was %d\n", i);
574 if (i > sequence) sequence = i;
575 icalcomponent_remove_property(vevent, prop);
576 icalproperty_free(prop);
579 lprintf(9, "New sequence is %d. Adding...\n", sequence);
580 icalcomponent_add_property(vevent,
581 icalproperty_new_sequence(sequence)
584 /* Set the organizer, only if one does not already exist *and*
585 * the form is supplying one
587 lprintf(9, "Setting the organizer...\n");
588 strcpy(buf, bstr("organizer"));
589 if ( (icalcomponent_get_first_property(vevent,
590 ICAL_ORGANIZER_PROPERTY) == NULL)
591 && (strlen(buf) > 0) ) {
593 /* set new organizer */
594 sprintf(organizer_string, "MAILTO:%s", buf);
595 icalcomponent_add_property(vevent,
596 icalproperty_new_organizer(organizer_string)
602 * Add any new attendees listed in the web form
604 lprintf(9, "Add any new attendees\n");
606 /* First, strip out the parenthesized partstats. */
607 strcpy(form_attendees, bstr("attendees"));
608 stripout(form_attendees, '(', ')');
611 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
612 extract_token(buf, form_attendees, i, '\n');
614 if (strlen(buf) > 0) {
615 lprintf(9, "Attendee: <%s>\n", buf);
616 sprintf(attendee_string, "MAILTO:%s", buf);
619 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
620 if (!strcasecmp(attendee_string,
621 icalproperty_get_attendee(attendee)))
627 icalcomponent_add_property(vevent,
628 icalproperty_new_attendee(attendee_string)
635 * Remove any attendees *not* listed in the web form
637 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
638 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
639 strcpy(attendee_string, icalproperty_get_attendee(attendee));
640 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
641 strcpy(attendee_string, &attendee_string[7]);
642 striplt(attendee_string);
644 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
645 extract_token(buf, form_attendees, i, '\n');
647 if (!strcasecmp(buf, attendee_string)) ++foundit;
650 icalcomponent_remove_property(vevent, attendee);
651 icalproperty_free(attendee);
658 * Encapsulate event into full VCALENDAR component. Clone it first,
659 * for two reasons: one, it's easier to just free the whole thing
660 * when we're done instead of unbundling, but more importantly, we
661 * can't encapsulate something that may already be encapsulated
664 lprintf(9, "Encapsulating into full VCALENDAR component\n");
665 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
667 /* If the user clicked 'Save' then save it to the server. */
668 lprintf(9, "Serializing it for saving\n");
669 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
670 serv_puts("ENT0 1|||4");
673 serv_puts("Content-type: text/calendar");
675 serv_puts(icalcomponent_as_ical_string(encaps));
678 icalcomponent_free(encaps);
681 /* Or, check attendee availability if the user asked for that. */
682 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
684 /* Call this function, which does the real work */
685 check_attendee_availability(encaps);
687 /* This displays the form again, with our annotations */
688 display_edit_individual_event(encaps, msgnum);
690 icalcomponent_free(encaps);
696 * If the user clicked 'Delete' then delete it.
698 lprintf(9, "Checking to see if we have to delete an old event\n");
699 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
700 serv_printf("DELE %ld", atol(bstr("msgnum")));
704 if (created_new_vevent) {
705 icalcomponent_free(vevent);
708 /* If this was a save or deelete, go back to the calendar view. */
709 if (strcasecmp(bstr("sc"), "Check attendee availability")) {
715 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */