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 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
94 "</FONT></TD></TR></TABLE><BR>\n"
97 /************************************************************
98 * Uncomment this to see the UID in calendar events for debugging
100 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
102 escputs((char *)icalproperty_get_comment(p));
105 wprintf("SEQUENCE == %d<BR>\n", sequence);
106 *************************************************************/
108 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
110 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
112 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
114 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
116 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
118 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
121 /* Put it in a borderless table so it lines up nicely */
122 wprintf("<TABLE border=0 width=100%%>\n");
124 wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
125 "<INPUT TYPE=\"text\" NAME=\"summary\" "
126 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
127 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
129 escputs((char *)icalproperty_get_comment(p));
131 wprintf("\"></TD></TR>\n");
133 wprintf("<TR><TD><B>Location</B></TD><TD>\n"
134 "<INPUT TYPE=\"text\" NAME=\"location\" "
135 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
136 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
138 escputs((char *)icalproperty_get_comment(p));
140 wprintf("\"></TD></TR>\n");
142 wprintf("<TR><TD><B>Start</B></TD><TD>\n");
143 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
145 t_start = icalproperty_get_dtstart(p);
146 if (t_start.is_date) {
153 memcpy(&tm_now, localtime(&now), sizeof(struct tm));
154 tm_now.tm_year = atoi(bstr("year")) - 1900;
155 tm_now.tm_mon = atoi(bstr("month")) - 1;
156 tm_now.tm_mday = atoi(bstr("day"));
157 if (strlen(bstr("hour")) > 0) {
158 tm_now.tm_hour = atoi(bstr("hour"));
159 tm_now.tm_min = atoi(bstr("minute"));
168 t_start = icaltime_from_timet_with_zone(
170 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
171 icaltimezone_get_utc_timezone
176 display_icaltimetype_as_webform(&t_start, "dtstart");
178 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
179 "VALUE=\"yes\" onClick=\"
182 this.form.dtstart_hour.value='0';
183 this.form.dtstart_hour.disabled = true;
184 this.form.dtstart_minute.value='0';
185 this.form.dtstart_minute.disabled = true;
186 this.form.dtend_hour.value='0';
187 this.form.dtend_hour.disabled = true;
188 this.form.dtend_minute.value='0';
189 this.form.dtend_minute.disabled = true;
190 this.form.dtend_month.disabled = true;
191 this.form.dtend_day.disabled = true;
192 this.form.dtend_year.disabled = true;
195 this.form.dtstart_hour.disabled = false;
196 this.form.dtstart_minute.disabled = false;
197 this.form.dtend_hour.disabled = false;
198 this.form.dtend_minute.disabled = false;
199 this.form.dtend_month.disabled = false;
200 this.form.dtend_day.disabled = false;
201 this.form.dtend_year.disabled = false;
205 \" %s >All day event",
206 (t_start.is_date ? "CHECKED" : "" )
209 wprintf("</TD></TR>\n");
211 /* If this is an all-day-event, set the end time to be identical to
212 * the start time (the hour/minute/second will be set to midnight).
213 * Otherwise extract or create it.
215 wprintf("<TR><TD><B>End</B></TD><TD>\n");
216 if (t_start.is_date) {
220 p = icalcomponent_get_first_property(vevent,
221 ICAL_DTEND_PROPERTY);
223 t_end = icalproperty_get_dtend(p);
226 /* If this is not an all-day event and there is no
227 * end time specified, make the default one hour
228 * from the start time.
233 t_end = icaltime_normalize(t_end);
234 /* t_end = icaltime_from_timet(now, 0); */
237 display_icaltimetype_as_webform(&t_end, "dtend");
238 wprintf("</TD></TR>\n");
240 wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
241 "<TEXTAREA NAME=\"description\" wrap=soft "
242 "ROWS=10 COLS=80 WIDTH=80>\n"
244 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
246 escputs((char *)icalproperty_get_comment(p));
248 wprintf("</TEXTAREA></TD></TR>");
250 /* For a new event, the user creating the event should be the
251 * organizer. Set this field accordingly.
253 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
255 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
256 icalcomponent_add_property(vevent,
257 icalproperty_new_organizer(organizer_string)
261 /* Determine who is the organizer of this event.
262 * We need to determine "me" or "not me."
264 organizer = icalcomponent_get_first_property(vevent,
265 ICAL_ORGANIZER_PROPERTY);
266 if (organizer != NULL) {
267 strcpy(organizer_string, icalproperty_get_organizer(organizer));
268 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
269 strcpy(organizer_string, &organizer_string[7]);
270 striplt(organizer_string);
271 lprintf(9, "ISME %s\n", organizer_string);
272 serv_printf("ISME %s", organizer_string);
274 lprintf(9, "%s\n", buf);
281 wprintf("<TR><TD><B>Organizer</B></TD><TD>");
282 escputs(organizer_string);
283 if (organizer_is_me) {
284 wprintf(" <FONT SIZE=-1><I>"
285 "(you are the organizer)</I></FONT>\n");
289 * Transmit the organizer as a hidden field. We don't want the user
290 * to be able to change it, but we do want it fed back to the server,
291 * especially if this is a new event and there is no organizer already
292 * in the calendar object.
294 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
295 escputs(organizer_string);
298 wprintf("</TD></TR>\n");
301 wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
303 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
305 /* No transparency found. Default to opaque (busy). */
306 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
308 icalcomponent_add_property(vevent, p);
312 v = icalproperty_get_value(p);
318 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
319 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
321 wprintf(">Free ");
323 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
324 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
328 wprintf("</TD></TR>\n");
331 wprintf("<TR><TD><B>Attendees</B><BR>"
332 "<FONT SIZE=-2>(Separate multiple attendees with commas)"
334 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
335 "ROWS=3 COLS=80 WIDTH=80>\n",
336 (organizer_is_me ? "" : "DISABLED ")
339 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; 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(", ");
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\" VALUE=\"Cancel\">\n"
367 wprintf("</FORM>\n");
369 wprintf("<SCRIPT language=\"javascript\">
372 if (document.EventForm.alldayevent.checked) {
373 document.EventForm.dtstart_hour.value='0';
374 document.EventForm.dtstart_hour.disabled = true;
375 document.EventForm.dtstart_minute.value='0';
376 document.EventForm.dtstart_minute.disabled = true;
377 document.EventForm.dtend_hour.value='0';
378 document.EventForm.dtend_hour.disabled = true;
379 document.EventForm.dtend_minute.value='0';
380 document.EventForm.dtend_minute.disabled = true;
381 document.EventForm.dtend_month.disabled = true;
382 document.EventForm.dtend_day.disabled = true;
383 document.EventForm.dtend_year.disabled = true;
386 document.EventForm.dtstart_hour.disabled = false;
387 document.EventForm.dtstart_minute.disabled = false;
388 document.EventForm.dtend_hour.disabled = false;
389 document.EventForm.dtend_minute.disabled = false;
390 document.EventForm.dtend_month.disabled = false;
391 document.EventForm.dtend_day.disabled = false;
392 document.EventForm.dtend_year.disabled = false;
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 VTODO component, attempt to use the first
429 * relevant VTODO 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_VTODO_COMPONENT
444 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
445 created_new_vevent = 1;
448 if (!strcasecmp(bstr("sc"), "Save")) {
450 /* Replace values in the component with ones from the form */
452 while (prop = icalcomponent_get_first_property(vevent,
453 ICAL_SUMMARY_PROPERTY), prop != NULL) {
454 icalcomponent_remove_property(vevent, prop);
455 icalproperty_free(prop);
457 icalcomponent_add_property(vevent,
458 icalproperty_new_summary(bstr("summary")));
460 while (prop = icalcomponent_get_first_property(vevent,
461 ICAL_LOCATION_PROPERTY), prop != NULL) {
462 icalcomponent_remove_property(vevent, prop);
463 icalproperty_free(prop);
465 icalcomponent_add_property(vevent,
466 icalproperty_new_location(bstr("location")));
468 while (prop = icalcomponent_get_first_property(vevent,
469 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
470 icalcomponent_remove_property(vevent, prop);
471 icalproperty_free(prop);
473 icalcomponent_add_property(vevent,
474 icalproperty_new_description(bstr("description")));
476 while (prop = icalcomponent_get_first_property(vevent,
477 ICAL_DTSTART_PROPERTY), prop != NULL) {
478 icalcomponent_remove_property(vevent, prop);
479 icalproperty_free(prop);
482 if (!strcmp(bstr("alldayevent"), "yes")) {
489 event_start = icaltime_from_webform("dtstart");
491 event_start.is_date = 1;
492 event_start.hour = 0;
493 event_start.minute = 0;
494 event_start.second = 0;
498 /* The following odd-looking snippet of code looks like it
499 * takes some unnecessary steps. It is done this way because
500 * libical incorrectly turns an "all day event" into a normal
501 * event starting at midnight (i.e. it serializes as date/time
502 * instead of just date) unless icalvalue_new_date() is used.
503 * So we force it, if this is an all day event.
505 prop = icalproperty_new_dtstart(event_start);
507 icalproperty_set_value(prop,
508 icalvalue_new_date(event_start)
512 if (prop) icalcomponent_add_property(vevent, prop);
513 else icalproperty_free(prop);
515 while (prop = icalcomponent_get_first_property(vevent,
516 ICAL_DTEND_PROPERTY), prop != NULL) {
517 icalcomponent_remove_property(vevent, prop);
518 icalproperty_free(prop);
520 while (prop = icalcomponent_get_first_property(vevent,
521 ICAL_DURATION_PROPERTY), prop != NULL) {
522 icalcomponent_remove_property(vevent, prop);
523 icalproperty_free(prop);
526 if (all_day_event == 0) {
527 icalcomponent_add_property(vevent,
528 icalproperty_new_dtend(icaltime_normalize(
529 icaltime_from_webform("dtend"))
534 /* See if transparency is indicated */
535 if (strlen(bstr("transp")) > 0) {
536 if (!strcasecmp(bstr("transp"), "opaque")) {
537 formtransp = ICAL_TRANSP_OPAQUE;
539 else if (!strcasecmp(bstr("transp"), "transparent")) {
540 formtransp = ICAL_TRANSP_TRANSPARENT;
543 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
545 icalcomponent_remove_property(vevent, prop);
546 icalproperty_free(prop);
549 lprintf(9, "adding new property...\n");
550 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
551 lprintf(9, "...added it.\n");
554 /* Give this event a UID if it doesn't have one. */
555 lprintf(9, "Give this event a UID if it doesn't have one.\n");
556 if (icalcomponent_get_first_property(vevent,
557 ICAL_UID_PROPERTY) == NULL) {
558 generate_new_uid(buf);
559 icalcomponent_add_property(vevent,
560 icalproperty_new_uid(buf)
564 /* Increment the sequence ID */
565 lprintf(9, "Increment the sequence ID\n");
566 while (prop = icalcomponent_get_first_property(vevent,
567 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
568 i = icalproperty_get_sequence(prop);
569 lprintf(9, "Sequence was %d\n", i);
570 if (i > sequence) sequence = i;
571 icalcomponent_remove_property(vevent, prop);
572 icalproperty_free(prop);
575 lprintf(9, "New sequence is %d. Adding...\n", sequence);
576 icalcomponent_add_property(vevent,
577 icalproperty_new_sequence(sequence)
580 /* Set the organizer, only if one does not already exist *and*
581 * the form is supplying one
583 lprintf(9, "Setting the organizer...\n");
584 strcpy(buf, bstr("organizer"));
585 if ( (icalcomponent_get_first_property(vevent,
586 ICAL_ORGANIZER_PROPERTY) == NULL)
587 && (strlen(buf) > 0) ) {
589 /* set new organizer */
590 sprintf(organizer_string, "MAILTO:%s", buf);
591 icalcomponent_add_property(vevent,
592 icalproperty_new_organizer(organizer_string)
598 * Add any new attendees listed in the web form
600 lprintf(9, "Add any new attendees\n");
602 /* First, strip out the parenthesized partstats. */
603 strcpy(form_attendees, bstr("attendees"));
604 stripout(form_attendees, '(', ')');
607 for (i=0; i<num_tokens(form_attendees, ','); ++i) {
608 extract_token(buf, form_attendees, i, ',');
610 if (strlen(buf) > 0) {
611 lprintf(9, "Attendee: <%s>\n", buf);
612 sprintf(attendee_string, "MAILTO:%s", buf);
615 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
616 if (!strcasecmp(attendee_string,
617 icalproperty_get_attendee(attendee)))
623 icalcomponent_add_property(vevent,
624 icalproperty_new_attendee(attendee_string)
631 * Remove any attendees *not* listed in the web form
633 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
634 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
635 strcpy(attendee_string, icalproperty_get_attendee(attendee));
636 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
637 strcpy(attendee_string, &attendee_string[7]);
638 striplt(attendee_string);
640 for (i=0; i<num_tokens(form_attendees, ','); ++i) {
641 extract_token(buf, form_attendees, i, ',');
643 if (!strcasecmp(buf, attendee_string)) ++foundit;
646 icalcomponent_remove_property(vevent, attendee);
647 icalproperty_free(attendee);
654 * Serialize it and save it to the message base
656 lprintf(9, "Encapsulating into full VCALENDAR component\n");
657 encaps = ical_encapsulate_subcomponent(vevent);
658 lprintf(9, "Serializing it for saving\n");
659 if (encaps != NULL) {
660 serv_puts("ENT0 1|||4");
663 serv_puts("Content-type: text/calendar");
665 serv_puts(icalcomponent_as_ical_string(encaps));
668 if (encaps != vevent) {
669 icalcomponent_remove_component(encaps, vevent);
670 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 */