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];
50 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 VTODO component, attempt to use the first
61 * relevant VTODO 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 memcpy(&tm_now, localtime(&now), sizeof(struct tm));
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=\"
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\" VALUE=\"Cancel\">\n"
364 wprintf("</FORM>\n");
366 wprintf("<SCRIPT language=\"javascript\">
368 if (document.EventForm.alldayevent.checked) {
369 document.EventForm.dtstart_hour.value='0';
370 document.EventForm.dtstart_hour.disabled = true;
371 document.EventForm.dtstart_minute.value='0';
372 document.EventForm.dtstart_minute.disabled = true;
373 document.EventForm.dtend_hour.value='0';
374 document.EventForm.dtend_hour.disabled = true;
375 document.EventForm.dtend_minute.value='0';
376 document.EventForm.dtend_minute.disabled = true;
377 document.EventForm.dtend_month.disabled = true;
378 document.EventForm.dtend_day.disabled = true;
379 document.EventForm.dtend_year.disabled = true;
382 document.EventForm.dtstart_hour.disabled = false;
383 document.EventForm.dtstart_minute.disabled = false;
384 document.EventForm.dtend_hour.disabled = false;
385 document.EventForm.dtend_minute.disabled = false;
386 document.EventForm.dtend_month.disabled = false;
387 document.EventForm.dtend_day.disabled = false;
388 document.EventForm.dtend_year.disabled = false;
394 do_template("endbox");
397 if (created_new_vevent) {
398 icalcomponent_free(vevent);
403 * Save an edited event
406 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
409 icalcomponent *vevent, *encaps;
410 int created_new_vevent = 0;
411 int all_day_event = 0;
412 struct icaltimetype event_start;
413 icalproperty *attendee = NULL;
414 char attendee_string[SIZ];
417 char form_attendees[SIZ];
418 char organizer_string[SIZ];
420 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
422 if (supplied_vevent != NULL) {
423 vevent = supplied_vevent;
424 /* If we're looking at a fully encapsulated VCALENDAR
425 * rather than a VTODO component, attempt to use the first
426 * relevant VTODO subcomponent. If there is none, the
427 * NULL returned by icalcomponent_get_first_component() will
428 * tell the next iteration of this function to create a
431 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
432 save_individual_event(
433 icalcomponent_get_first_component(
434 vevent, ICAL_VTODO_COMPONENT
441 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
442 created_new_vevent = 1;
445 if (!strcasecmp(bstr("sc"), "Save")) {
447 /* Replace values in the component with ones from the form */
449 while (prop = icalcomponent_get_first_property(vevent,
450 ICAL_SUMMARY_PROPERTY), prop != NULL) {
451 icalcomponent_remove_property(vevent, prop);
452 icalproperty_free(prop);
454 icalcomponent_add_property(vevent,
455 icalproperty_new_summary(bstr("summary")));
457 while (prop = icalcomponent_get_first_property(vevent,
458 ICAL_LOCATION_PROPERTY), prop != NULL) {
459 icalcomponent_remove_property(vevent, prop);
460 icalproperty_free(prop);
462 icalcomponent_add_property(vevent,
463 icalproperty_new_location(bstr("location")));
465 while (prop = icalcomponent_get_first_property(vevent,
466 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
467 icalcomponent_remove_property(vevent, prop);
468 icalproperty_free(prop);
470 icalcomponent_add_property(vevent,
471 icalproperty_new_description(bstr("description")));
473 while (prop = icalcomponent_get_first_property(vevent,
474 ICAL_DTSTART_PROPERTY), prop != NULL) {
475 icalcomponent_remove_property(vevent, prop);
476 icalproperty_free(prop);
479 if (!strcmp(bstr("alldayevent"), "yes")) {
486 event_start = icaltime_from_webform("dtstart");
488 event_start.is_date = 1;
489 event_start.hour = 0;
490 event_start.minute = 0;
491 event_start.second = 0;
495 /* The following odd-looking snippet of code looks like it
496 * takes some unnecessary steps. It is done this way because
497 * libical incorrectly turns an "all day event" into a normal
498 * event starting at midnight (i.e. it serializes as date/time
499 * instead of just date) unless icalvalue_new_date() is used.
500 * So we force it, if this is an all day event.
502 prop = icalproperty_new_dtstart(event_start);
504 icalproperty_set_value(prop,
505 icalvalue_new_date(event_start)
509 if (prop) icalcomponent_add_property(vevent, prop);
510 else icalproperty_free(prop);
512 while (prop = icalcomponent_get_first_property(vevent,
513 ICAL_DTEND_PROPERTY), prop != NULL) {
514 icalcomponent_remove_property(vevent, prop);
515 icalproperty_free(prop);
517 while (prop = icalcomponent_get_first_property(vevent,
518 ICAL_DURATION_PROPERTY), prop != NULL) {
519 icalcomponent_remove_property(vevent, prop);
520 icalproperty_free(prop);
523 if (all_day_event == 0) {
524 icalcomponent_add_property(vevent,
525 icalproperty_new_dtend(icaltime_normalize(
526 icaltime_from_webform("dtend"))
531 /* See if transparency is indicated */
532 if (strlen(bstr("transp")) > 0) {
533 if (!strcasecmp(bstr("transp"), "opaque")) {
534 formtransp = ICAL_TRANSP_OPAQUE;
536 else if (!strcasecmp(bstr("transp"), "transparent")) {
537 formtransp = ICAL_TRANSP_TRANSPARENT;
540 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
542 icalcomponent_remove_property(vevent, prop);
543 icalproperty_free(prop);
546 lprintf(9, "adding new property...\n");
547 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
548 lprintf(9, "...added it.\n");
551 /* Give this event a UID if it doesn't have one. */
552 lprintf(9, "Give this event a UID if it doesn't have one.\n");
553 if (icalcomponent_get_first_property(vevent,
554 ICAL_UID_PROPERTY) == NULL) {
555 generate_new_uid(buf);
556 icalcomponent_add_property(vevent,
557 icalproperty_new_uid(buf)
561 /* Increment the sequence ID */
562 lprintf(9, "Increment the sequence ID\n");
563 while (prop = icalcomponent_get_first_property(vevent,
564 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
565 i = icalproperty_get_sequence(prop);
566 lprintf(9, "Sequence was %d\n", i);
567 if (i > sequence) sequence = i;
568 icalcomponent_remove_property(vevent, prop);
569 icalproperty_free(prop);
572 lprintf(9, "New sequence is %d. Adding...\n", sequence);
573 icalcomponent_add_property(vevent,
574 icalproperty_new_sequence(sequence)
577 /* Set the organizer, only if one does not already exist *and*
578 * the form is supplying one
580 lprintf(9, "Setting the organizer...\n");
581 strcpy(buf, bstr("organizer"));
582 if ( (icalcomponent_get_first_property(vevent,
583 ICAL_ORGANIZER_PROPERTY) == NULL)
584 && (strlen(buf) > 0) ) {
586 /* set new organizer */
587 sprintf(organizer_string, "MAILTO:%s", buf);
588 icalcomponent_add_property(vevent,
589 icalproperty_new_organizer(organizer_string)
595 * Add any new attendees listed in the web form
597 lprintf(9, "Add any new attendees\n");
599 /* First, strip out the parenthesized partstats. */
600 strcpy(form_attendees, bstr("attendees"));
601 stripout(form_attendees, '(', ')');
604 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
605 extract_token(buf, form_attendees, i, '\n');
607 if (strlen(buf) > 0) {
608 lprintf(9, "Attendee: <%s>\n", buf);
609 sprintf(attendee_string, "MAILTO:%s", buf);
612 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
613 if (!strcasecmp(attendee_string,
614 icalproperty_get_attendee(attendee)))
620 icalcomponent_add_property(vevent,
621 icalproperty_new_attendee(attendee_string)
628 * Remove any attendees *not* listed in the web form
630 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
631 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
632 strcpy(attendee_string, icalproperty_get_attendee(attendee));
633 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
634 strcpy(attendee_string, &attendee_string[7]);
635 striplt(attendee_string);
637 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
638 extract_token(buf, form_attendees, i, '\n');
640 if (!strcasecmp(buf, attendee_string)) ++foundit;
643 icalcomponent_remove_property(vevent, attendee);
644 icalproperty_free(attendee);
651 * Serialize it and save it to the message base. We clone it first,
652 * for two reasons: one, it's easier to just free the whole thing
653 * when we're done instead of unbundling, but more importantly, we
654 * can't encapsulate something that may already be encapsulated
657 lprintf(9, "Encapsulating into full VCALENDAR component\n");
658 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
659 lprintf(9, "Serializing it for saving\n");
660 if (encaps != NULL) {
661 serv_puts("ENT0 1|||4");
664 serv_puts("Content-type: text/calendar");
666 serv_puts(icalcomponent_as_ical_string(encaps));
669 icalcomponent_free(encaps);
674 * If the user clicked 'Delete' then delete it.
676 lprintf(9, "Checking to see if we have to delete an old event\n");
677 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
678 serv_printf("DELE %ld", atol(bstr("msgnum")));
682 if (created_new_vevent) {
683 icalcomponent_free(vevent);
686 /* Go back to the event list */
691 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */