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 do_template("beginbox_nt");
91 wprintf("<h3> <IMG ALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
92 " Add or edit an event</h3>\n");
94 /************************************************************
95 * Uncomment this to see the UID in calendar events for debugging
97 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
99 escputs((char *)icalproperty_get_comment(p));
102 wprintf("SEQUENCE == %d<BR>\n", sequence);
103 *************************************************************/
105 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
107 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
109 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
111 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
113 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
115 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
118 /* Put it in a borderless table so it lines up nicely */
119 wprintf("<TABLE border=0 width=100%%>\n");
121 wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
122 "<INPUT TYPE=\"text\" NAME=\"summary\" "
123 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
124 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
126 escputs((char *)icalproperty_get_comment(p));
128 wprintf("\"></TD></TR>\n");
130 wprintf("<TR><TD><B>Location</B></TD><TD>\n"
131 "<INPUT TYPE=\"text\" NAME=\"location\" "
132 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
133 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
135 escputs((char *)icalproperty_get_comment(p));
137 wprintf("\"></TD></TR>\n");
139 wprintf("<TR><TD><B>Start</B></TD><TD>\n");
140 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
142 t_start = icalproperty_get_dtstart(p);
143 if (t_start.is_date) {
150 memcpy(&tm_now, localtime(&now), sizeof(struct tm));
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"));
154 if (strlen(bstr("hour")) > 0) {
155 tm_now.tm_hour = atoi(bstr("hour"));
156 tm_now.tm_min = atoi(bstr("minute"));
165 t_start = icaltime_from_timet_with_zone(
167 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
168 icaltimezone_get_utc_timezone
173 display_icaltimetype_as_webform(&t_start, "dtstart");
175 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
176 "VALUE=\"yes\" onClick=\"
179 this.form.dtstart_hour.value='0';
180 this.form.dtstart_hour.disabled = true;
181 this.form.dtstart_minute.value='0';
182 this.form.dtstart_minute.disabled = true;
183 this.form.dtend_hour.value='0';
184 this.form.dtend_hour.disabled = true;
185 this.form.dtend_minute.value='0';
186 this.form.dtend_minute.disabled = true;
187 this.form.dtend_month.disabled = true;
188 this.form.dtend_day.disabled = true;
189 this.form.dtend_year.disabled = true;
192 this.form.dtstart_hour.disabled = false;
193 this.form.dtstart_minute.disabled = false;
194 this.form.dtend_hour.disabled = false;
195 this.form.dtend_minute.disabled = false;
196 this.form.dtend_month.disabled = false;
197 this.form.dtend_day.disabled = false;
198 this.form.dtend_year.disabled = false;
202 \" %s >All day event",
203 (t_start.is_date ? "CHECKED" : "" )
206 wprintf("</TD></TR>\n");
208 /* If this is an all-day-event, set the end time to be identical to
209 * the start time (the hour/minute/second will be set to midnight).
210 * Otherwise extract or create it.
212 wprintf("<TR><TD><B>End</B></TD><TD>\n");
213 if (t_start.is_date) {
217 p = icalcomponent_get_first_property(vevent,
218 ICAL_DTEND_PROPERTY);
220 t_end = icalproperty_get_dtend(p);
223 /* If this is not an all-day event and there is no
224 * end time specified, make the default one hour
225 * from the start time.
230 t_end = icaltime_normalize(t_end);
231 /* t_end = icaltime_from_timet(now, 0); */
234 display_icaltimetype_as_webform(&t_end, "dtend");
235 wprintf("</TD></TR>\n");
237 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
238 "<TEXTAREA NAME=\"description\" wrap=soft "
239 "ROWS=5 COLS=80 WIDTH=80>\n"
241 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
243 escputs((char *)icalproperty_get_comment(p));
245 wprintf("</TEXTAREA></TD></TR>");
247 /* For a new event, the user creating the event should be the
248 * organizer. Set this field accordingly.
250 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
252 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
253 icalcomponent_add_property(vevent,
254 icalproperty_new_organizer(organizer_string)
258 /* Determine who is the organizer of this event.
259 * We need to determine "me" or "not me."
261 organizer = icalcomponent_get_first_property(vevent,
262 ICAL_ORGANIZER_PROPERTY);
263 if (organizer != NULL) {
264 strcpy(organizer_string, icalproperty_get_organizer(organizer));
265 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
266 strcpy(organizer_string, &organizer_string[7]);
267 striplt(organizer_string);
268 lprintf(9, "ISME %s\n", organizer_string);
269 serv_printf("ISME %s", organizer_string);
271 lprintf(9, "%s\n", buf);
278 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
279 escputs(organizer_string);
280 if (organizer_is_me) {
281 wprintf(" <FONT SIZE=-1><I>"
282 "(you are the organizer)</I></FONT>\n");
286 * Transmit the organizer as a hidden field. We don't want the user
287 * to be able to change it, but we do want it fed back to the server,
288 * especially if this is a new event and there is no organizer already
289 * in the calendar object.
291 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
292 escputs(organizer_string);
295 wprintf("</TD></TR>\n");
298 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
300 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
302 /* No transparency found. Default to opaque (busy). */
303 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
305 icalcomponent_add_property(vevent, p);
309 v = icalproperty_get_value(p);
315 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
316 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
318 wprintf(">Free ");
320 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
321 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
325 wprintf("</TD></TR>\n");
328 wprintf("<TR><TD><B>Attendees</B><BR>"
329 "<FONT SIZE=-2>(One per line)"
331 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
332 "ROWS=3 COLS=80 WIDTH=80>\n",
333 (organizer_is_me ? "" : "DISABLED ")
336 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; 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\">
369 if (document.EventForm.alldayevent.checked) {
370 document.EventForm.dtstart_hour.value='0';
371 document.EventForm.dtstart_hour.disabled = true;
372 document.EventForm.dtstart_minute.value='0';
373 document.EventForm.dtstart_minute.disabled = true;
374 document.EventForm.dtend_hour.value='0';
375 document.EventForm.dtend_hour.disabled = true;
376 document.EventForm.dtend_minute.value='0';
377 document.EventForm.dtend_minute.disabled = true;
378 document.EventForm.dtend_month.disabled = true;
379 document.EventForm.dtend_day.disabled = true;
380 document.EventForm.dtend_year.disabled = true;
383 document.EventForm.dtstart_hour.disabled = false;
384 document.EventForm.dtstart_minute.disabled = false;
385 document.EventForm.dtend_hour.disabled = false;
386 document.EventForm.dtend_minute.disabled = false;
387 document.EventForm.dtend_month.disabled = false;
388 document.EventForm.dtend_day.disabled = false;
389 document.EventForm.dtend_year.disabled = false;
395 do_template("endbox");
398 if (created_new_vevent) {
399 icalcomponent_free(vevent);
404 * Save an edited event
407 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
410 icalcomponent *vevent, *encaps;
411 int created_new_vevent = 0;
412 int all_day_event = 0;
413 struct icaltimetype event_start;
414 icalproperty *attendee = NULL;
415 char attendee_string[SIZ];
418 char form_attendees[SIZ];
419 char organizer_string[SIZ];
421 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
423 if (supplied_vevent != NULL) {
424 vevent = supplied_vevent;
425 /* If we're looking at a fully encapsulated VCALENDAR
426 * rather than a VTODO component, attempt to use the first
427 * relevant VTODO subcomponent. If there is none, the
428 * NULL returned by icalcomponent_get_first_component() will
429 * tell the next iteration of this function to create a
432 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
433 save_individual_event(
434 icalcomponent_get_first_component(
435 vevent, ICAL_VTODO_COMPONENT
442 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
443 created_new_vevent = 1;
446 if (!strcasecmp(bstr("sc"), "Save")) {
448 /* Replace values in the component with ones from the form */
450 while (prop = icalcomponent_get_first_property(vevent,
451 ICAL_SUMMARY_PROPERTY), prop != NULL) {
452 icalcomponent_remove_property(vevent, prop);
453 icalproperty_free(prop);
455 icalcomponent_add_property(vevent,
456 icalproperty_new_summary(bstr("summary")));
458 while (prop = icalcomponent_get_first_property(vevent,
459 ICAL_LOCATION_PROPERTY), prop != NULL) {
460 icalcomponent_remove_property(vevent, prop);
461 icalproperty_free(prop);
463 icalcomponent_add_property(vevent,
464 icalproperty_new_location(bstr("location")));
466 while (prop = icalcomponent_get_first_property(vevent,
467 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
468 icalcomponent_remove_property(vevent, prop);
469 icalproperty_free(prop);
471 icalcomponent_add_property(vevent,
472 icalproperty_new_description(bstr("description")));
474 while (prop = icalcomponent_get_first_property(vevent,
475 ICAL_DTSTART_PROPERTY), prop != NULL) {
476 icalcomponent_remove_property(vevent, prop);
477 icalproperty_free(prop);
480 if (!strcmp(bstr("alldayevent"), "yes")) {
487 event_start = icaltime_from_webform("dtstart");
489 event_start.is_date = 1;
490 event_start.hour = 0;
491 event_start.minute = 0;
492 event_start.second = 0;
496 /* The following odd-looking snippet of code looks like it
497 * takes some unnecessary steps. It is done this way because
498 * libical incorrectly turns an "all day event" into a normal
499 * event starting at midnight (i.e. it serializes as date/time
500 * instead of just date) unless icalvalue_new_date() is used.
501 * So we force it, if this is an all day event.
503 prop = icalproperty_new_dtstart(event_start);
505 icalproperty_set_value(prop,
506 icalvalue_new_date(event_start)
510 if (prop) icalcomponent_add_property(vevent, prop);
511 else icalproperty_free(prop);
513 while (prop = icalcomponent_get_first_property(vevent,
514 ICAL_DTEND_PROPERTY), prop != NULL) {
515 icalcomponent_remove_property(vevent, prop);
516 icalproperty_free(prop);
518 while (prop = icalcomponent_get_first_property(vevent,
519 ICAL_DURATION_PROPERTY), prop != NULL) {
520 icalcomponent_remove_property(vevent, prop);
521 icalproperty_free(prop);
524 if (all_day_event == 0) {
525 icalcomponent_add_property(vevent,
526 icalproperty_new_dtend(icaltime_normalize(
527 icaltime_from_webform("dtend"))
532 /* See if transparency is indicated */
533 if (strlen(bstr("transp")) > 0) {
534 if (!strcasecmp(bstr("transp"), "opaque")) {
535 formtransp = ICAL_TRANSP_OPAQUE;
537 else if (!strcasecmp(bstr("transp"), "transparent")) {
538 formtransp = ICAL_TRANSP_TRANSPARENT;
541 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
543 icalcomponent_remove_property(vevent, prop);
544 icalproperty_free(prop);
547 lprintf(9, "adding new property...\n");
548 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
549 lprintf(9, "...added it.\n");
552 /* Give this event a UID if it doesn't have one. */
553 lprintf(9, "Give this event a UID if it doesn't have one.\n");
554 if (icalcomponent_get_first_property(vevent,
555 ICAL_UID_PROPERTY) == NULL) {
556 generate_new_uid(buf);
557 icalcomponent_add_property(vevent,
558 icalproperty_new_uid(buf)
562 /* Increment the sequence ID */
563 lprintf(9, "Increment the sequence ID\n");
564 while (prop = icalcomponent_get_first_property(vevent,
565 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
566 i = icalproperty_get_sequence(prop);
567 lprintf(9, "Sequence was %d\n", i);
568 if (i > sequence) sequence = i;
569 icalcomponent_remove_property(vevent, prop);
570 icalproperty_free(prop);
573 lprintf(9, "New sequence is %d. Adding...\n", sequence);
574 icalcomponent_add_property(vevent,
575 icalproperty_new_sequence(sequence)
578 /* Set the organizer, only if one does not already exist *and*
579 * the form is supplying one
581 lprintf(9, "Setting the organizer...\n");
582 strcpy(buf, bstr("organizer"));
583 if ( (icalcomponent_get_first_property(vevent,
584 ICAL_ORGANIZER_PROPERTY) == NULL)
585 && (strlen(buf) > 0) ) {
587 /* set new organizer */
588 sprintf(organizer_string, "MAILTO:%s", buf);
589 icalcomponent_add_property(vevent,
590 icalproperty_new_organizer(organizer_string)
596 * Add any new attendees listed in the web form
598 lprintf(9, "Add any new attendees\n");
600 /* First, strip out the parenthesized partstats. */
601 strcpy(form_attendees, bstr("attendees"));
602 stripout(form_attendees, '(', ')');
605 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
606 extract_token(buf, form_attendees, i, '\n');
608 if (strlen(buf) > 0) {
609 lprintf(9, "Attendee: <%s>\n", buf);
610 sprintf(attendee_string, "MAILTO:%s", buf);
613 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
614 if (!strcasecmp(attendee_string,
615 icalproperty_get_attendee(attendee)))
621 icalcomponent_add_property(vevent,
622 icalproperty_new_attendee(attendee_string)
629 * Remove any attendees *not* listed in the web form
631 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
632 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
633 strcpy(attendee_string, icalproperty_get_attendee(attendee));
634 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
635 strcpy(attendee_string, &attendee_string[7]);
636 striplt(attendee_string);
638 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
639 extract_token(buf, form_attendees, i, '\n');
641 if (!strcasecmp(buf, attendee_string)) ++foundit;
644 icalcomponent_remove_property(vevent, attendee);
645 icalproperty_free(attendee);
652 * Serialize it and save it to the message base. We clone it first,
653 * for two reasons: one, it's easier to just free the whole thing
654 * when we're done instead of unbundling, but more importantly, we
655 * can't encapsulate something that may already be encapsulated
658 lprintf(9, "Encapsulating into full VCALENDAR component\n");
659 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
660 lprintf(9, "Serializing it for saving\n");
661 if (encaps != NULL) {
662 serv_puts("ENT0 1|||4");
665 serv_puts("Content-type: text/calendar");
667 serv_puts(icalcomponent_as_ical_string(encaps));
670 icalcomponent_free(encaps);
675 * If the user clicked 'Delete' then delete it.
677 lprintf(9, "Checking to see if we have to delete an old event\n");
678 if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
679 serv_printf("DELE %ld", atol(bstr("msgnum")));
683 if (created_new_vevent) {
684 icalcomponent_free(vevent);
687 /* Go back to the event list */
692 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */