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;
53 now = time(NULL) % 60; /* mod 60 to force :00 seconds */
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);
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 tm_now.tm_year = atoi(bstr("year")) - 1900;
150 tm_now.tm_mon = atoi(bstr("month")) - 1;
151 tm_now.tm_mday = atoi(bstr("day"));
152 if (strlen(bstr("hour")) > 0) {
153 tm_now.tm_hour = atoi(bstr("hour"));
154 tm_now.tm_min = atoi(bstr("minute"));
163 t_start = icaltime_from_timet_with_zone(
165 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
166 icaltimezone_get_utc_timezone()
171 display_icaltimetype_as_webform(&t_start, "dtstart");
173 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
174 "VALUE=\"yes\" onClick=\""
176 " if (this.checked) { "
177 " this.form.dtstart_hour.value='0'; "
178 " this.form.dtstart_hour.disabled = true; "
179 " this.form.dtstart_minute.value='0'; "
180 " this.form.dtstart_minute.disabled = true; "
181 " this.form.dtend_hour.value='0'; "
182 " this.form.dtend_hour.disabled = true; "
183 " this.form.dtend_minute.value='0'; "
184 " this.form.dtend_minute.disabled = true; "
185 " this.form.dtend_month.disabled = true; "
186 " this.form.dtend_day.disabled = true; "
187 " this.form.dtend_year.disabled = true; "
190 " this.form.dtstart_hour.disabled = false; "
191 " this.form.dtstart_minute.disabled = false; "
192 " this.form.dtend_hour.disabled = false; "
193 " this.form.dtend_minute.disabled = false; "
194 " this.form.dtend_month.disabled = false; "
195 " this.form.dtend_day.disabled = false; "
196 " this.form.dtend_year.disabled = false; "
200 " \" %s >All day event",
201 (t_start.is_date ? "CHECKED" : "" )
204 wprintf("</TD></TR>\n");
206 /* If this is an all-day-event, set the end time to be identical to
207 * the start time (the hour/minute/second will be set to midnight).
208 * Otherwise extract or create it.
210 wprintf("<TR><TD><B>End</B></TD><TD>\n");
211 if (t_start.is_date) {
215 p = icalcomponent_get_first_property(vevent,
216 ICAL_DTEND_PROPERTY);
218 t_end = icalproperty_get_dtend(p);
221 /* If this is not an all-day event and there is no
222 * end time specified, make the default one hour
223 * from the start time.
228 t_end = icaltime_normalize(t_end);
229 /* t_end = icaltime_from_timet(now, 0); */
232 display_icaltimetype_as_webform(&t_end, "dtend");
233 wprintf("</TD></TR>\n");
235 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
236 "<TEXTAREA NAME=\"description\" wrap=soft "
237 "ROWS=5 COLS=80 WIDTH=80>\n"
239 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
241 escputs((char *)icalproperty_get_comment(p));
243 wprintf("</TEXTAREA></TD></TR>");
245 /* For a new event, the user creating the event should be the
246 * organizer. Set this field accordingly.
248 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
250 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
251 icalcomponent_add_property(vevent,
252 icalproperty_new_organizer(organizer_string)
256 /* Determine who is the organizer of this event.
257 * We need to determine "me" or "not me."
259 organizer = icalcomponent_get_first_property(vevent,
260 ICAL_ORGANIZER_PROPERTY);
261 if (organizer != NULL) {
262 strcpy(organizer_string, icalproperty_get_organizer(organizer));
263 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
264 strcpy(organizer_string, &organizer_string[7]);
265 striplt(organizer_string);
266 lprintf(9, "ISME %s\n", organizer_string);
267 serv_printf("ISME %s", organizer_string);
269 lprintf(9, "%s\n", buf);
276 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
277 escputs(organizer_string);
278 if (organizer_is_me) {
279 wprintf(" <FONT SIZE=-1><I>"
280 "(you are the organizer)</I></FONT>\n");
284 * Transmit the organizer as a hidden field. We don't want the user
285 * to be able to change it, but we do want it fed back to the server,
286 * especially if this is a new event and there is no organizer already
287 * in the calendar object.
289 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
290 escputs(organizer_string);
293 wprintf("</TD></TR>\n");
296 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
298 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
300 /* No transparency found. Default to opaque (busy). */
301 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
303 icalcomponent_add_property(vevent, p);
307 v = icalproperty_get_value(p);
313 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
314 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
316 wprintf(">Free ");
318 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
319 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
323 wprintf("</TD></TR>\n");
326 wprintf("<TR><TD><B>Attendees</B><BR>"
327 "<FONT SIZE=-2>(One per line)"
329 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
330 "ROWS=3 COLS=80 WIDTH=80>\n",
331 (organizer_is_me ? "" : "DISABLED ")
334 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
336 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
337 strcpy(attendee_string, icalproperty_get_attendee(attendee));
338 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
340 /* screen name or email address */
341 strcpy(attendee_string, &attendee_string[7]);
342 striplt(attendee_string);
343 if (i++) wprintf("\n");
344 escputs(attendee_string);
347 /* participant status */
348 partstat_as_string(buf, attendee);
352 wprintf("</TEXTAREA></TD></TR>\n");
354 /* Done with properties. */
355 wprintf("</TABLE>\n<CENTER>"
356 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
358 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
360 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
361 "VALUE=\"Check attendee availability\">\n"
363 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
367 wprintf("</FORM>\n");
369 wprintf("<SCRIPT language=\"javascript\">"
371 "if (document.EventForm.alldayevent.checked) {"
372 "document.EventForm.dtstart_hour.value='0';"
373 "document.EventForm.dtstart_hour.disabled = true;"
374 "document.EventForm.dtstart_minute.value='0';"
375 "document.EventForm.dtstart_minute.disabled = true;"
376 "document.EventForm.dtend_hour.value='0';"
377 "document.EventForm.dtend_hour.disabled = true;"
378 "document.EventForm.dtend_minute.value='0';"
379 "document.EventForm.dtend_minute.disabled = true;"
380 "document.EventForm.dtend_month.disabled = true;"
381 "document.EventForm.dtend_day.disabled = true;"
382 "document.EventForm.dtend_year.disabled = true;"
385 "document.EventForm.dtstart_hour.disabled = false;"
386 "document.EventForm.dtstart_minute.disabled = false;"
387 "document.EventForm.dtend_hour.disabled = false;"
388 "document.EventForm.dtend_minute.disabled = false;"
389 "document.EventForm.dtend_month.disabled = false;"
390 "document.EventForm.dtend_day.disabled = false;"
391 "document.EventForm.dtend_year.disabled = false;"
397 do_template("endbox");
400 if (created_new_vevent) {
401 icalcomponent_free(vevent);
406 * Save an edited event
409 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
412 icalcomponent *vevent, *encaps;
413 int created_new_vevent = 0;
414 int all_day_event = 0;
415 struct icaltimetype event_start;
416 icalproperty *attendee = NULL;
417 char attendee_string[SIZ];
420 char form_attendees[SIZ];
421 char organizer_string[SIZ];
423 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
425 if (supplied_vevent != NULL) {
426 vevent = supplied_vevent;
427 /* If we're looking at a fully encapsulated VCALENDAR
428 * rather than a VEVENT component, attempt to use the first
429 * relevant VEVENT subcomponent. If there is none, the
430 * NULL returned by icalcomponent_get_first_component() will
431 * tell the next iteration of this function to create a
434 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
435 save_individual_event(
436 icalcomponent_get_first_component(
437 vevent, ICAL_VEVENT_COMPONENT
444 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
445 created_new_vevent = 1;
448 if ( (!strcasecmp(bstr("sc"), "Save"))
449 || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
451 /* Replace values in the component with ones from the form */
453 while (prop = icalcomponent_get_first_property(vevent,
454 ICAL_SUMMARY_PROPERTY), prop != NULL) {
455 icalcomponent_remove_property(vevent, prop);
456 icalproperty_free(prop);
458 icalcomponent_add_property(vevent,
459 icalproperty_new_summary(bstr("summary")));
461 while (prop = icalcomponent_get_first_property(vevent,
462 ICAL_LOCATION_PROPERTY), prop != NULL) {
463 icalcomponent_remove_property(vevent, prop);
464 icalproperty_free(prop);
466 icalcomponent_add_property(vevent,
467 icalproperty_new_location(bstr("location")));
469 while (prop = icalcomponent_get_first_property(vevent,
470 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
471 icalcomponent_remove_property(vevent, prop);
472 icalproperty_free(prop);
474 icalcomponent_add_property(vevent,
475 icalproperty_new_description(bstr("description")));
477 while (prop = icalcomponent_get_first_property(vevent,
478 ICAL_DTSTART_PROPERTY), prop != NULL) {
479 icalcomponent_remove_property(vevent, prop);
480 icalproperty_free(prop);
483 if (!strcmp(bstr("alldayevent"), "yes")) {
490 event_start = icaltime_from_webform("dtstart");
492 event_start.is_date = 1;
493 event_start.hour = 0;
494 event_start.minute = 0;
495 event_start.second = 0;
499 /* The following odd-looking snippet of code looks like it
500 * takes some unnecessary steps. It is done this way because
501 * libical incorrectly turns an "all day event" into a normal
502 * event starting at midnight (i.e. it serializes as date/time
503 * instead of just date) unless icalvalue_new_date() is used.
504 * So we force it, if this is an all day event.
506 prop = icalproperty_new_dtstart(event_start);
508 icalproperty_set_value(prop,
509 icalvalue_new_date(event_start)
513 if (prop) icalcomponent_add_property(vevent, prop);
514 else icalproperty_free(prop);
516 while (prop = icalcomponent_get_first_property(vevent,
517 ICAL_DTEND_PROPERTY), prop != NULL) {
518 icalcomponent_remove_property(vevent, prop);
519 icalproperty_free(prop);
521 while (prop = icalcomponent_get_first_property(vevent,
522 ICAL_DURATION_PROPERTY), prop != NULL) {
523 icalcomponent_remove_property(vevent, prop);
524 icalproperty_free(prop);
527 if (all_day_event == 0) {
528 icalcomponent_add_property(vevent,
529 icalproperty_new_dtend(icaltime_normalize(
530 icaltime_from_webform("dtend"))
535 /* See if transparency is indicated */
536 if (strlen(bstr("transp")) > 0) {
537 if (!strcasecmp(bstr("transp"), "opaque")) {
538 formtransp = ICAL_TRANSP_OPAQUE;
540 else if (!strcasecmp(bstr("transp"), "transparent")) {
541 formtransp = ICAL_TRANSP_TRANSPARENT;
544 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
546 icalcomponent_remove_property(vevent, prop);
547 icalproperty_free(prop);
550 lprintf(9, "adding new property...\n");
551 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
552 lprintf(9, "...added it.\n");
555 /* Give this event a UID if it doesn't have one. */
556 lprintf(9, "Give this event a UID if it doesn't have one.\n");
557 if (icalcomponent_get_first_property(vevent,
558 ICAL_UID_PROPERTY) == NULL) {
560 icalcomponent_add_property(vevent,
561 icalproperty_new_uid(buf)
565 /* Increment the sequence ID */
566 lprintf(9, "Increment the sequence ID\n");
567 while (prop = icalcomponent_get_first_property(vevent,
568 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
569 i = icalproperty_get_sequence(prop);
570 lprintf(9, "Sequence was %d\n", i);
571 if (i > sequence) sequence = i;
572 icalcomponent_remove_property(vevent, prop);
573 icalproperty_free(prop);
576 lprintf(9, "New sequence is %d. Adding...\n", sequence);
577 icalcomponent_add_property(vevent,
578 icalproperty_new_sequence(sequence)
581 /* Set the organizer, only if one does not already exist *and*
582 * the form is supplying one
584 lprintf(9, "Setting the organizer...\n");
585 strcpy(buf, bstr("organizer"));
586 if ( (icalcomponent_get_first_property(vevent,
587 ICAL_ORGANIZER_PROPERTY) == NULL)
588 && (strlen(buf) > 0) ) {
590 /* set new organizer */
591 sprintf(organizer_string, "MAILTO:%s", buf);
592 icalcomponent_add_property(vevent,
593 icalproperty_new_organizer(organizer_string)
599 * Add any new attendees listed in the web form
601 lprintf(9, "Add any new attendees\n");
603 /* First, strip out the parenthesized partstats. */
604 strcpy(form_attendees, bstr("attendees"));
605 stripout(form_attendees, '(', ')');
608 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
609 extract_token(buf, form_attendees, i, '\n');
611 if (strlen(buf) > 0) {
612 lprintf(9, "Attendee: <%s>\n", buf);
613 sprintf(attendee_string, "MAILTO:%s", buf);
616 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
617 if (!strcasecmp(attendee_string,
618 icalproperty_get_attendee(attendee)))
624 icalcomponent_add_property(vevent,
625 icalproperty_new_attendee(attendee_string)
632 * Remove any attendees *not* listed in the web form
634 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
635 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
636 strcpy(attendee_string, icalproperty_get_attendee(attendee));
637 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
638 strcpy(attendee_string, &attendee_string[7]);
639 striplt(attendee_string);
641 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
642 extract_token(buf, form_attendees, i, '\n');
644 if (!strcasecmp(buf, attendee_string)) ++foundit;
647 icalcomponent_remove_property(vevent, attendee);
648 icalproperty_free(attendee);
655 * Encapsulate event into full VCALENDAR component. Clone it first,
656 * for two reasons: one, it's easier to just free the whole thing
657 * when we're done instead of unbundling, but more importantly, we
658 * can't encapsulate something that may already be encapsulated
661 lprintf(9, "Encapsulating into full VCALENDAR component\n");
662 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
664 /* If the user clicked 'Save' then save it to the server. */
665 lprintf(9, "Serializing it for saving\n");
666 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
667 serv_puts("ENT0 1|||4");
670 serv_puts("Content-type: text/calendar");
672 serv_puts(icalcomponent_as_ical_string(encaps));
675 icalcomponent_free(encaps);
678 /* Or, check attendee availability if the user asked for that. */
679 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
681 /* Call this function, which does the real work */
682 check_attendee_availability(encaps);
684 /* This displays the form again, with our annotations */
685 display_edit_individual_event(encaps, msgnum);
687 icalcomponent_free(encaps);
693 * If the user clicked 'Delete' then delete it.
695 lprintf(9, "Checking to see if we have to delete an old event\n");
696 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
697 serv_printf("DELE %ld", atol(bstr("msgnum")));
701 if (created_new_vevent) {
702 icalcomponent_free(vevent);
705 /* If this was a save or deelete, go back to the calendar view. */
706 if (strcasecmp(bstr("sc"), "Check attendee availability")) {
712 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */