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)
38 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
39 icalcomponent *vevent;
42 struct icaltimetype t_start, t_end;
45 int created_new_vevent = 0;
46 icalproperty *organizer = NULL;
47 char organizer_string[SIZ];
48 icalproperty *attendee = NULL;
49 char attendee_string[SIZ];
52 int organizer_is_me = 0;
55 now = time(NULL) % 60; /* mod 60 to force :00 seconds */
56 strcpy(organizer_string, "");
57 strcpy(attendee_string, "");
59 if (supplied_vevent != NULL) {
60 vevent = supplied_vevent;
61 /* If we're looking at a fully encapsulated VCALENDAR
62 * rather than a VTODO component, attempt to use the first
63 * relevant VTODO subcomponent. If there is none, the
64 * NULL returned by icalcomponent_get_first_component() will
65 * tell the next iteration of this function to create a
68 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
69 display_edit_individual_event(
70 icalcomponent_get_first_component(
71 vevent, ICAL_VTODO_COMPONENT
78 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
79 created_new_vevent = 1;
82 /* Learn the sequence */
83 p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
85 sequence = icalproperty_get_sequence(p);
90 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#007700\"><TR><TD>"
91 "<IMG ALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
92 "<SPAN CLASS=\"titlebar\">Edit event</SPAN>"
93 "</TD></TR></TABLE><BR>\n"
96 /************************************************************
97 * Uncomment this to see the UID in calendar events for debugging
99 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
101 escputs((char *)icalproperty_get_comment(p));
104 wprintf("SEQUENCE == %d<BR>\n", sequence);
105 *************************************************************/
107 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
109 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
111 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
113 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
115 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
117 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
120 /* Put it in a borderless table so it lines up nicely */
121 wprintf("<TABLE border=0 width=100%%>\n");
123 wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
124 "<INPUT TYPE=\"text\" NAME=\"summary\" "
125 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
126 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
128 escputs((char *)icalproperty_get_comment(p));
130 wprintf("\"></TD></TR>\n");
132 wprintf("<TR><TD><B>Location</B></TD><TD>\n"
133 "<INPUT TYPE=\"text\" NAME=\"location\" "
134 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
135 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
137 escputs((char *)icalproperty_get_comment(p));
139 wprintf("\"></TD></TR>\n");
141 wprintf("<TR><TD><B>Start</B></TD><TD>\n");
142 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
144 t_start = icalproperty_get_dtstart(p);
145 if (t_start.is_date) {
152 memcpy(&tm_now, localtime(&now), sizeof(struct tm));
153 tm_now.tm_year = atoi(bstr("year")) - 1900;
154 tm_now.tm_mon = atoi(bstr("month")) - 1;
155 tm_now.tm_mday = atoi(bstr("day"));
156 if (strlen(bstr("hour")) > 0) {
157 tm_now.tm_hour = atoi(bstr("hour"));
158 tm_now.tm_min = atoi(bstr("minute"));
167 t_start = icaltime_from_timet_with_zone(
169 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
170 icaltimezone_get_utc_timezone
175 display_icaltimetype_as_webform(&t_start, "dtstart");
177 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
178 "VALUE=\"yes\" onClick=\"
181 this.form.dtstart_hour.value='0';
182 this.form.dtstart_hour.disabled = true;
183 this.form.dtstart_minute.value='0';
184 this.form.dtstart_minute.disabled = true;
185 this.form.dtend_hour.value='0';
186 this.form.dtend_hour.disabled = true;
187 this.form.dtend_minute.value='0';
188 this.form.dtend_minute.disabled = true;
189 this.form.dtend_month.disabled = true;
190 this.form.dtend_day.disabled = true;
191 this.form.dtend_year.disabled = true;
194 this.form.dtstart_hour.disabled = false;
195 this.form.dtstart_minute.disabled = false;
196 this.form.dtend_hour.disabled = false;
197 this.form.dtend_minute.disabled = false;
198 this.form.dtend_month.disabled = false;
199 this.form.dtend_day.disabled = false;
200 this.form.dtend_year.disabled = false;
204 \" %s >All day event",
205 (t_start.is_date ? "CHECKED" : "" )
208 wprintf("</TD></TR>\n");
210 /* If this is an all-day-event, set the end time to be identical to
211 * the start time (the hour/minute/second will be set to midnight).
212 * Otherwise extract or create it.
214 wprintf("<TR><TD><B>End</B></TD><TD>\n");
215 if (t_start.is_date) {
219 p = icalcomponent_get_first_property(vevent,
220 ICAL_DTEND_PROPERTY);
222 t_end = icalproperty_get_dtend(p);
225 /* If this is not an all-day event and there is no
226 * end time specified, make the default one hour
227 * from the start time.
232 t_end = icaltime_normalize(t_end);
233 /* t_end = icaltime_from_timet(now, 0); */
236 display_icaltimetype_as_webform(&t_end, "dtend");
237 wprintf("</TD></TR>\n");
239 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
240 "<TEXTAREA NAME=\"description\" wrap=soft "
241 "ROWS=10 COLS=80 WIDTH=80>\n"
243 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
245 escputs((char *)icalproperty_get_comment(p));
247 wprintf("</TEXTAREA></TD></TR>");
249 /* For a new event, the user creating the event should be the
250 * organizer. Set this field accordingly.
252 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
254 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
255 icalcomponent_add_property(vevent,
256 icalproperty_new_organizer(organizer_string)
260 /* Determine who is the organizer of this event.
261 * We need to determine "me" or "not me."
263 organizer = icalcomponent_get_first_property(vevent,
264 ICAL_ORGANIZER_PROPERTY);
265 if (organizer != NULL) {
266 strcpy(organizer_string, icalproperty_get_organizer(organizer));
267 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
268 strcpy(organizer_string, &organizer_string[7]);
269 striplt(organizer_string);
270 lprintf(9, "ISME %s\n", organizer_string);
271 serv_printf("ISME %s", organizer_string);
273 lprintf(9, "%s\n", buf);
280 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
281 escputs(organizer_string);
282 if (organizer_is_me) {
283 wprintf(" <FONT SIZE=-1><I>"
284 "(you are the organizer)</I></FONT>\n");
288 * Transmit the organizer as a hidden field. We don't want the user
289 * to be able to change it, but we do want it fed back to the server,
290 * especially if this is a new event and there is no organizer already
291 * in the calendar object.
293 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
294 escputs(organizer_string);
297 wprintf("</TD></TR>\n");
300 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
302 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
304 /* No transparency found. Default to opaque (busy). */
305 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
307 icalcomponent_add_property(vevent, p);
311 v = icalproperty_get_value(p);
317 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
318 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
320 wprintf(">Free ");
322 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
323 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
327 wprintf("</TD></TR>\n");
330 wprintf("<TR><TD><B>Attendees</B><BR>"
331 "<FONT SIZE=-2>(Separate multiple attendees with commas)"
333 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
334 "ROWS=3 COLS=80 WIDTH=80>\n",
335 (organizer_is_me ? "" : "DISABLED ")
338 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; 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(", ");
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\" VALUE=\"Cancel\">\n"
366 wprintf("</FORM>\n");
368 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;
399 if (created_new_vevent) {
400 icalcomponent_free(vevent);
405 * Save an edited event
408 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
411 icalcomponent *vevent, *encaps;
412 int created_new_vevent = 0;
413 int all_day_event = 0;
414 struct icaltimetype event_start;
415 icalproperty *attendee = NULL;
416 char attendee_string[SIZ];
419 char form_attendees[SIZ];
420 char organizer_string[SIZ];
422 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
424 if (supplied_vevent != NULL) {
425 vevent = supplied_vevent;
426 /* If we're looking at a fully encapsulated VCALENDAR
427 * rather than a VTODO component, attempt to use the first
428 * relevant VTODO subcomponent. If there is none, the
429 * NULL returned by icalcomponent_get_first_component() will
430 * tell the next iteration of this function to create a
433 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
434 save_individual_event(
435 icalcomponent_get_first_component(
436 vevent, ICAL_VTODO_COMPONENT
443 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
444 created_new_vevent = 1;
447 if (!strcasecmp(bstr("sc"), "Save")) {
449 /* Replace values in the component with ones from the form */
451 while (prop = icalcomponent_get_first_property(vevent,
452 ICAL_SUMMARY_PROPERTY), prop != NULL) {
453 icalcomponent_remove_property(vevent, prop);
454 icalproperty_free(prop);
456 icalcomponent_add_property(vevent,
457 icalproperty_new_summary(bstr("summary")));
459 while (prop = icalcomponent_get_first_property(vevent,
460 ICAL_LOCATION_PROPERTY), prop != NULL) {
461 icalcomponent_remove_property(vevent, prop);
462 icalproperty_free(prop);
464 icalcomponent_add_property(vevent,
465 icalproperty_new_location(bstr("location")));
467 while (prop = icalcomponent_get_first_property(vevent,
468 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
469 icalcomponent_remove_property(vevent, prop);
470 icalproperty_free(prop);
472 icalcomponent_add_property(vevent,
473 icalproperty_new_description(bstr("description")));
475 while (prop = icalcomponent_get_first_property(vevent,
476 ICAL_DTSTART_PROPERTY), prop != NULL) {
477 icalcomponent_remove_property(vevent, prop);
478 icalproperty_free(prop);
481 if (!strcmp(bstr("alldayevent"), "yes")) {
488 event_start = icaltime_from_webform("dtstart");
490 event_start.is_date = 1;
491 event_start.hour = 0;
492 event_start.minute = 0;
493 event_start.second = 0;
497 /* The following odd-looking snippet of code looks like it
498 * takes some unnecessary steps. It is done this way because
499 * libical incorrectly turns an "all day event" into a normal
500 * event starting at midnight (i.e. it serializes as date/time
501 * instead of just date) unless icalvalue_new_date() is used.
502 * So we force it, if this is an all day event.
504 prop = icalproperty_new_dtstart(event_start);
506 icalproperty_set_value(prop,
507 icalvalue_new_date(event_start)
511 if (prop) icalcomponent_add_property(vevent, prop);
512 else icalproperty_free(prop);
514 while (prop = icalcomponent_get_first_property(vevent,
515 ICAL_DTEND_PROPERTY), prop != NULL) {
516 icalcomponent_remove_property(vevent, prop);
517 icalproperty_free(prop);
519 while (prop = icalcomponent_get_first_property(vevent,
520 ICAL_DURATION_PROPERTY), prop != NULL) {
521 icalcomponent_remove_property(vevent, prop);
522 icalproperty_free(prop);
525 if (all_day_event == 0) {
526 icalcomponent_add_property(vevent,
527 icalproperty_new_dtend(icaltime_normalize(
528 icaltime_from_webform("dtend"))
533 /* See if transparency is indicated */
534 if (strlen(bstr("transp")) > 0) {
535 if (!strcasecmp(bstr("transp"), "opaque")) {
536 formtransp = ICAL_TRANSP_OPAQUE;
538 else if (!strcasecmp(bstr("transp"), "transparent")) {
539 formtransp = ICAL_TRANSP_TRANSPARENT;
542 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
544 icalcomponent_remove_property(vevent, prop);
545 icalproperty_free(prop);
548 lprintf(9, "adding new property...\n");
549 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
550 lprintf(9, "...added it.\n");
553 /* Give this event a UID if it doesn't have one. */
554 lprintf(9, "Give this event a UID if it doesn't have one.\n");
555 if (icalcomponent_get_first_property(vevent,
556 ICAL_UID_PROPERTY) == NULL) {
557 generate_new_uid(buf);
558 icalcomponent_add_property(vevent,
559 icalproperty_new_uid(buf)
563 /* Increment the sequence ID */
564 lprintf(9, "Increment the sequence ID\n");
565 while (prop = icalcomponent_get_first_property(vevent,
566 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
567 i = icalproperty_get_sequence(prop);
568 lprintf(9, "Sequence was %d\n", i);
569 if (i > sequence) sequence = i;
570 icalcomponent_remove_property(vevent, prop);
571 icalproperty_free(prop);
574 lprintf(9, "New sequence is %d. Adding...\n", sequence);
575 icalcomponent_add_property(vevent,
576 icalproperty_new_sequence(sequence)
579 /* Set the organizer, only if one does not already exist *and*
580 * the form is supplying one
582 lprintf(9, "Setting the organizer...\n");
583 strcpy(buf, bstr("organizer"));
584 if ( (icalcomponent_get_first_property(vevent,
585 ICAL_ORGANIZER_PROPERTY) == NULL)
586 && (strlen(buf) > 0) ) {
588 /* set new organizer */
589 sprintf(organizer_string, "MAILTO:%s", buf);
590 icalcomponent_add_property(vevent,
591 icalproperty_new_organizer(organizer_string)
597 * Add any new attendees listed in the web form
599 lprintf(9, "Add any new attendees\n");
601 /* First, strip out the parenthesized partstats. */
602 strcpy(form_attendees, bstr("attendees"));
603 stripout(form_attendees, '(', ')');
606 for (i=0; i<num_tokens(form_attendees, ','); ++i) {
607 extract_token(buf, form_attendees, i, ',');
609 if (strlen(buf) > 0) {
610 lprintf(9, "Attendee: <%s>\n", buf);
611 sprintf(attendee_string, "MAILTO:%s", buf);
614 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
615 if (!strcasecmp(attendee_string,
616 icalproperty_get_attendee(attendee)))
622 icalcomponent_add_property(vevent,
623 icalproperty_new_attendee(attendee_string)
630 * Remove any attendees *not* listed in the web form
632 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
633 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
634 strcpy(attendee_string, icalproperty_get_attendee(attendee));
635 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
636 strcpy(attendee_string, &attendee_string[7]);
637 striplt(attendee_string);
639 for (i=0; i<num_tokens(form_attendees, ','); ++i) {
640 extract_token(buf, form_attendees, i, ',');
642 if (!strcasecmp(buf, attendee_string)) ++foundit;
645 icalcomponent_remove_property(vevent, attendee);
646 icalproperty_free(attendee);
653 * Serialize it and save it to the message base. We clone it first,
654 * for two reasons: one, it's easier to just free the whole thing
655 * when we're done instead of unbundling, but more importantly, we
656 * can't encapsulate something that may already be encapsulated
659 lprintf(9, "Encapsulating into full VCALENDAR component\n");
660 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
661 lprintf(9, "Serializing it for saving\n");
662 if (encaps != NULL) {
663 serv_puts("ENT0 1|||4");
666 serv_puts("Content-type: text/calendar");
668 serv_puts(icalcomponent_as_ical_string(encaps));
671 icalcomponent_free(encaps);
676 * If the user clicked 'Delete' then delete it.
678 lprintf(9, "Checking to see if we have to delete an old event\n");
679 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
680 serv_printf("DELE %ld", atol(bstr("msgnum")));
684 if (created_new_vevent) {
685 icalcomponent_free(vevent);
688 /* Go back to the event list */
693 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */