* All OS-level includes are now included from webcit.h instead of from
[citadel.git] / webcit / event.c
1 /*
2  * $Id$
3  *
4  * Editing calendar events.
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10
11
12 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
13
14 /*
15  * Display an event by itself (for editing)
16  */
17 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
18         icalcomponent *vevent;
19         icalproperty *p;
20         icalvalue *v;
21         struct icaltimetype t_start, t_end;
22         time_t now;
23         struct tm tm_now;
24         int created_new_vevent = 0;
25         icalproperty *organizer = NULL;
26         char organizer_string[SIZ];
27         icalproperty *attendee = NULL;
28         char attendee_string[SIZ];
29         char buf[SIZ];
30         int organizer_is_me = 0;
31         int i;
32         int sequence = 0;
33
34         now = time(NULL);
35         strcpy(organizer_string, "");
36         strcpy(attendee_string, "");
37
38         if (supplied_vevent != NULL) {
39                 vevent = supplied_vevent;
40                 /* If we're looking at a fully encapsulated VCALENDAR
41                  * rather than a VEVENT component, attempt to use the first
42                  * relevant VEVENT subcomponent.  If there is none, the
43                  * NULL returned by icalcomponent_get_first_component() will
44                  * tell the next iteration of this function to create a
45                  * new one.
46                  */
47                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
48                         display_edit_individual_event(
49                                 icalcomponent_get_first_component(
50                                         vevent, ICAL_VEVENT_COMPONENT
51                                 ), msgnum
52                         );
53                         return;
54                 }
55         }
56         else {
57                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
58                 created_new_vevent = 1;
59         }
60
61         /* Learn the sequence */
62         p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
63         if (p != NULL) {
64                 sequence = icalproperty_get_sequence(p);
65         }
66
67         /* Begin output */
68         output_headers(1, 1, 2, 0, 0, 0, 0);
69         wprintf("<div id=\"banner\">\n"
70                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
71                 "<SPAN CLASS=\"titlebar\">Add or edit an event</SPAN>"
72                 "</TD></TR></TABLE>\n"
73                 "</div>\n<div id=\"content\">\n"
74         );
75
76         wprintf("<script type=\"text/javascript\">"
77                 "function grey_all_day() { "
78                         "if (document.EventForm.alldayevent.checked) {"
79                                 "document.EventForm.dtstart_hour.value='0';"
80                                 "document.EventForm.dtstart_hour.disabled = true;"
81                                 "document.EventForm.dtstart_minute.value='0';"
82                                 "document.EventForm.dtstart_minute.disabled = true;"
83                                 "document.EventForm.dtend_hour.value='0';"
84                                 "document.EventForm.dtend_hour.disabled = true;"
85                                 "document.EventForm.dtend_minute.value='0';"
86                                 "document.EventForm.dtend_minute.disabled = true;"
87                                 "document.EventForm.dtend_month.disabled = true;"
88                                 "document.EventForm.dtend_day.disabled = true;"
89                                 "document.EventForm.dtend_year.disabled = true;"
90                         "}"
91                         "else {"
92                                 "document.EventForm.dtstart_hour.disabled = false;"
93                                 "document.EventForm.dtstart_minute.disabled = false;"
94                                 "document.EventForm.dtend_hour.disabled = false;"
95                                 "document.EventForm.dtend_minute.disabled = false;"
96                                 "document.EventForm.dtend_month.disabled = false;"
97                                 "document.EventForm.dtend_day.disabled = false;"
98                                 "document.EventForm.dtend_year.disabled = false;"
99                         "}"
100                 "}"
101                 "</script>\n"
102         );
103
104
105         wprintf("<div id=\"fix_scrollbar_bug\">"
106                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
107
108         /************************************************************
109          * Uncomment this to see the UID in calendar events for debugging
110         wprintf("UID == ");
111         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
112         if (p != NULL) {
113                 escputs((char *)icalproperty_get_comment(p));
114         }
115         wprintf("<br />\n");
116         wprintf("SEQUENCE == %d<br />\n", sequence);
117         *************************************************************/
118
119         wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
120
121         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
122                 msgnum);
123         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
124                 bstr("calview"));
125         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
126                 bstr("year"));
127         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
128                 bstr("month"));
129         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
130                 bstr("day"));
131
132         /* Put it in a borderless table so it lines up nicely */
133         wprintf("<TABLE border=0 width=100%%>\n");
134
135         wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
136                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
137                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
138         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
139         if (p != NULL) {
140                 escputs((char *)icalproperty_get_comment(p));
141         }
142         wprintf("\"></TD></TR>\n");
143
144         wprintf("<TR><TD><B>Location</B></TD><TD>\n"
145                 "<INPUT TYPE=\"text\" NAME=\"location\" "
146                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
147         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
148         if (p != NULL) {
149                 escputs((char *)icalproperty_get_comment(p));
150         }
151         wprintf("\"></TD></TR>\n");
152
153         wprintf("<TR><TD><B>Start</B></TD><TD>\n");
154         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
155         if (p != NULL) {
156                 t_start = icalproperty_get_dtstart(p);
157                 if (t_start.is_date) {
158                         t_start.hour = 0;
159                         t_start.minute = 0;
160                         t_start.second = 0;
161                 }
162         }
163         else {
164                 localtime_r(&now, &tm_now);
165                 if (strlen(bstr("year")) > 0) {
166                         tm_now.tm_year = atoi(bstr("year")) - 1900;
167                         tm_now.tm_mon = atoi(bstr("month")) - 1;
168                         tm_now.tm_mday = atoi(bstr("day"));
169                 }
170                 if (strlen(bstr("hour")) > 0) {
171                         tm_now.tm_hour = atoi(bstr("hour"));
172                         tm_now.tm_min = atoi(bstr("minute"));
173                         tm_now.tm_sec = 0;
174                 }
175                 else {
176                         tm_now.tm_hour = 9;
177                         tm_now.tm_min = 0;
178                         tm_now.tm_sec = 0;
179                 }
180
181                 t_start = icaltime_from_timet_with_zone(
182                         mktime(&tm_now),
183                         ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
184                         icaltimezone_get_utc_timezone()
185                 );
186                 t_start.is_utc = 1;
187
188         }
189         display_icaltimetype_as_webform(&t_start, "dtstart");
190
191         wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
192                 "VALUE=\"yes\" onClick=\"grey_all_day();\""
193                 " %s >All day event",
194                 (t_start.is_date ? "CHECKED" : "" )
195         );
196
197         wprintf("</TD></TR>\n");
198
199         /* If this is an all-day-event, set the end time to be identical to
200          * the start time (the hour/minute/second will be set to midnight).
201          * Otherwise extract or create it.
202          */
203         wprintf("<TR><TD><B>End</B></TD><TD>\n");
204         if (t_start.is_date) {
205                 t_end = t_start;
206         }
207         else {
208                 p = icalcomponent_get_first_property(vevent,
209                                                         ICAL_DTEND_PROPERTY);
210                 if (p != NULL) {
211                         t_end = icalproperty_get_dtend(p);
212                 }
213                 else {
214                         /* If this is not an all-day event and there is no
215                          * end time specified, make the default one hour
216                          * from the start time.
217                          */
218                         t_end = t_start;
219                         t_end.hour += 1;
220                         t_end.second = 0;
221                         t_end = icaltime_normalize(t_end);
222                         /* t_end = icaltime_from_timet(now, 0); */
223                 }
224         }
225         display_icaltimetype_as_webform(&t_end, "dtend");
226         wprintf("</TD></TR>\n");
227
228         wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
229                 "<TEXTAREA NAME=\"description\" wrap=soft "
230                 "ROWS=5 COLS=80 WIDTH=80>\n"
231         );
232         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
233         if (p != NULL) {
234                 escputs((char *)icalproperty_get_comment(p));
235         }
236         wprintf("</TEXTAREA></TD></TR>");
237
238         /* For a new event, the user creating the event should be the
239          * organizer.  Set this field accordingly.
240          */
241         if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
242            == NULL) {
243                 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
244                 icalcomponent_add_property(vevent,
245                         icalproperty_new_organizer(organizer_string)
246                 );
247         }
248
249         /* Determine who is the organizer of this event.
250          * We need to determine "me" or "not me."
251          */
252         organizer = icalcomponent_get_first_property(vevent,
253                                                 ICAL_ORGANIZER_PROPERTY);
254         if (organizer != NULL) {
255                 strcpy(organizer_string, icalproperty_get_organizer(organizer));
256                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
257                         strcpy(organizer_string, &organizer_string[7]);
258                         striplt(organizer_string);
259                         lprintf(9, "ISME %s\n", organizer_string);
260                         serv_printf("ISME %s", organizer_string);
261                         serv_getln(buf, sizeof buf);
262                         lprintf(9, "%s\n", buf);
263                         if (buf[0] == '2') {
264                                 organizer_is_me = 1;
265                         }
266                 }
267         }
268
269         wprintf("<TR><TD><B>Organizer</B></TD><TD>");
270         escputs(organizer_string);
271         if (organizer_is_me) {
272                 wprintf(" <FONT SIZE=-1><I>"
273                         "(you are the organizer)</I></FONT>\n");
274         }
275
276         /*
277          * Transmit the organizer as a hidden field.   We don't want the user
278          * to be able to change it, but we do want it fed back to the server,
279          * especially if this is a new event and there is no organizer already
280          * in the calendar object.
281          */
282         wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
283         escputs(organizer_string);
284         wprintf("\">");
285
286         wprintf("</TD></TR>\n");
287
288         /* Transparency */
289         wprintf("<TR><TD><B>Show time as:</B></TD><TD>");
290
291         p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
292         if (p == NULL) {
293                 /* No transparency found.  Default to opaque (busy). */
294                 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
295                 if (p != NULL) {
296                         icalcomponent_add_property(vevent, p);
297                 }
298         }
299         if (p != NULL) {
300                 v = icalproperty_get_value(p);
301         }
302         else {
303                 v = NULL;
304         }
305
306         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
307         if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
308                 wprintf(" CHECKED");
309         wprintf(">Free&nbsp;&nbsp;");
310
311         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
312         if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
313                 wprintf(" CHECKED");
314         wprintf(">Busy");
315
316         wprintf("</TD></TR>\n");
317
318         /* Attendees */
319         wprintf("<TR><TD><B>Attendees</B><br />"
320                 "<FONT SIZE=-2>(One per line)"
321                 "</FONT></TD><TD>"
322                 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
323                 "ROWS=3 COLS=80 WIDTH=80>\n",
324                 (organizer_is_me ? "" : "DISABLED ")
325         );
326         i = 0;
327         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
328             attendee != NULL;
329             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
330                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
331                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
332
333                         /* screen name or email address */
334                         strcpy(attendee_string, &attendee_string[7]);
335                         striplt(attendee_string);
336                         if (i++) wprintf("\n");
337                         escputs(attendee_string);
338                         wprintf(" ");
339
340                         /* participant status */
341                         partstat_as_string(buf, attendee);
342                         escputs(buf);
343                 }
344         }
345         wprintf("</TEXTAREA></TD></TR>\n");
346
347         /* Done with properties. */
348         wprintf("</TABLE>\n<CENTER>"
349                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
350                 "&nbsp;&nbsp;"
351                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
352                 "&nbsp;&nbsp;"
353                 "<INPUT TYPE=\"submit\" NAME=\"sc\" "
354                                 "VALUE=\"Check attendee availability\">\n"
355                 "&nbsp;&nbsp;"
356                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
357                 "</CENTER>\n"
358         );
359
360         wprintf("</FORM>\n");
361         
362         wprintf("</td></tr></table></div>\n");
363         wprintf("<script type=\"text/javascript\">"
364                 "grey_all_day();"
365                 "</script>\n"
366         );
367         wDumpContent(1);
368
369         if (created_new_vevent) {
370                 icalcomponent_free(vevent);
371         }
372 }
373
374 /*
375  * Save an edited event
376  * 
377  */
378 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
379         char buf[SIZ];
380         icalproperty *prop;
381         icalcomponent *vevent, *encaps;
382         int created_new_vevent = 0;
383         int all_day_event = 0;
384         struct icaltimetype event_start, t;
385         icalproperty *attendee = NULL;
386         char attendee_string[SIZ];
387         int i;
388         int foundit;
389         char form_attendees[SIZ];
390         char organizer_string[SIZ];
391         int sequence = 0;
392         enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
393
394         if (supplied_vevent != NULL) {
395                 vevent = supplied_vevent;
396                 /* If we're looking at a fully encapsulated VCALENDAR
397                  * rather than a VEVENT component, attempt to use the first
398                  * relevant VEVENT subcomponent.  If there is none, the
399                  * NULL returned by icalcomponent_get_first_component() will
400                  * tell the next iteration of this function to create a
401                  * new one.
402                  */
403                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
404                         save_individual_event(
405                                 icalcomponent_get_first_component(
406                                         vevent, ICAL_VEVENT_COMPONENT
407                                 ), msgnum
408                         );
409                         return;
410                 }
411         }
412         else {
413                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
414                 created_new_vevent = 1;
415         }
416
417         if ( (!strcasecmp(bstr("sc"), "Save"))
418            || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
419
420                 /* Replace values in the component with ones from the form */
421
422                 while (prop = icalcomponent_get_first_property(vevent,
423                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
424                         icalcomponent_remove_property(vevent, prop);
425                         icalproperty_free(prop);
426                 }
427                 icalcomponent_add_property(vevent,
428                         icalproperty_new_summary(bstr("summary")));
429
430                 while (prop = icalcomponent_get_first_property(vevent,
431                       ICAL_LOCATION_PROPERTY), prop != NULL) {
432                         icalcomponent_remove_property(vevent, prop);
433                         icalproperty_free(prop);
434                 }
435                 icalcomponent_add_property(vevent,
436                         icalproperty_new_location(bstr("location")));
437                 
438                 while (prop = icalcomponent_get_first_property(vevent,
439                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
440                         icalcomponent_remove_property(vevent, prop);
441                         icalproperty_free(prop);
442                 }
443                 icalcomponent_add_property(vevent,
444                         icalproperty_new_description(bstr("description")));
445         
446                 while (prop = icalcomponent_get_first_property(vevent,
447                       ICAL_DTSTART_PROPERTY), prop != NULL) {
448                         icalcomponent_remove_property(vevent, prop);
449                         icalproperty_free(prop);
450                 }
451
452                 if (!strcmp(bstr("alldayevent"), "yes")) {
453                         all_day_event = 1;
454                 }
455                 else {
456                         all_day_event = 0;
457                 }
458
459                 if (all_day_event) {
460                         icaltime_from_webform_dateonly(&event_start, "dtstart");
461                 }
462                 else {
463                         icaltime_from_webform(&event_start, "dtstart");
464                 }
465
466                 /* The following odd-looking snippet of code looks like it
467                  * takes some unnecessary steps.  It is done this way because
468                  * libical incorrectly turns an "all day event" into a normal
469                  * event starting at midnight (i.e. it serializes as date/time
470                  * instead of just date) unless icalvalue_new_date() is used.
471                  * So we force it, if this is an all day event.
472                  */
473                 prop = icalproperty_new_dtstart(event_start);
474                 if (all_day_event) {
475                         icalproperty_set_value(prop,
476                                 icalvalue_new_date(event_start)
477                         );
478                 }
479
480                 if (prop) icalcomponent_add_property(vevent, prop);
481                 else icalproperty_free(prop);
482
483                 while (prop = icalcomponent_get_first_property(vevent,
484                       ICAL_DTEND_PROPERTY), prop != NULL) {
485                         icalcomponent_remove_property(vevent, prop);
486                         icalproperty_free(prop);
487                 }
488                 while (prop = icalcomponent_get_first_property(vevent,
489                       ICAL_DURATION_PROPERTY), prop != NULL) {
490                         icalcomponent_remove_property(vevent, prop);
491                         icalproperty_free(prop);
492                 }
493
494                 if (all_day_event == 0) {
495                         icaltime_from_webform(&t, "dtend");     
496                         icalcomponent_add_property(vevent,
497                                 icalproperty_new_dtend(icaltime_normalize(t)
498                                 )
499                         );
500                 }
501
502                 /* See if transparency is indicated */
503                 if (strlen(bstr("transp")) > 0) {
504                         if (!strcasecmp(bstr("transp"), "opaque")) {
505                                 formtransp = ICAL_TRANSP_OPAQUE;
506                         }
507                         else if (!strcasecmp(bstr("transp"), "transparent")) {
508                                 formtransp = ICAL_TRANSP_TRANSPARENT;
509                         }
510
511                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
512                               (prop != NULL)) {
513                                 icalcomponent_remove_property(vevent, prop);
514                                 icalproperty_free(prop);
515                         }
516
517                         lprintf(9, "adding new property...\n");
518                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
519                         lprintf(9, "...added it.\n");
520                 }
521
522                 /* Give this event a UID if it doesn't have one. */
523                 lprintf(9, "Give this event a UID if it doesn't have one.\n");
524                 if (icalcomponent_get_first_property(vevent,
525                    ICAL_UID_PROPERTY) == NULL) {
526                         generate_uuid(buf);
527                         icalcomponent_add_property(vevent,
528                                 icalproperty_new_uid(buf)
529                         );
530                 }
531
532                 /* Increment the sequence ID */
533                 lprintf(9, "Increment the sequence ID\n");
534                 while (prop = icalcomponent_get_first_property(vevent,
535                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
536                         i = icalproperty_get_sequence(prop);
537                         lprintf(9, "Sequence was %d\n", i);
538                         if (i > sequence) sequence = i;
539                         icalcomponent_remove_property(vevent, prop);
540                         icalproperty_free(prop);
541                 }
542                 ++sequence;
543                 lprintf(9, "New sequence is %d.  Adding...\n", sequence);
544                 icalcomponent_add_property(vevent,
545                         icalproperty_new_sequence(sequence)
546                 );
547                 
548                 /* Set the organizer, only if one does not already exist *and*
549                  * the form is supplying one
550                  */
551                 lprintf(9, "Setting the organizer...\n");
552                 strcpy(buf, bstr("organizer"));
553                 if ( (icalcomponent_get_first_property(vevent,
554                    ICAL_ORGANIZER_PROPERTY) == NULL) 
555                    && (strlen(buf) > 0) ) {
556
557                         /* set new organizer */
558                         sprintf(organizer_string, "MAILTO:%s", buf);
559                         icalcomponent_add_property(vevent,
560                                 icalproperty_new_organizer(organizer_string)
561                         );
562
563                 }
564
565                 /*
566                  * Add any new attendees listed in the web form
567                  */
568                 lprintf(9, "Add any new attendees\n");
569
570                 /* First, strip out the parenthesized partstats.  */
571                 strcpy(form_attendees, bstr("attendees"));
572                 stripout(form_attendees, '(', ')');
573
574                 /* Now iterate! */
575                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
576                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
577                         striplt(buf);
578                         if (strlen(buf) > 0) {
579                                 lprintf(9, "Attendee: <%s>\n", buf);
580                                 sprintf(attendee_string, "MAILTO:%s", buf);
581                                 foundit = 0;
582
583                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
584                                         if (!strcasecmp(attendee_string,
585                                            icalproperty_get_attendee(attendee)))
586                                                 ++foundit;
587                                 }
588
589
590                                 if (foundit == 0) {
591                                         icalcomponent_add_property(vevent,
592                                                 icalproperty_new_attendee(attendee_string)
593                                         );
594                                 }
595                         }
596                 }
597
598                 /*
599                  * Remove any attendees *not* listed in the web form
600                  */
601 STARTOVER:      lprintf(9, "Remove unlisted attendees\n");
602                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
603                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
604                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
605                                 strcpy(attendee_string, &attendee_string[7]);
606                                 striplt(attendee_string);
607                                 foundit = 0;
608                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
609                                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
610                                         striplt(buf);
611                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
612                                 }
613                                 if (foundit == 0) {
614                                         icalcomponent_remove_property(vevent, attendee);
615                                         icalproperty_free(attendee);
616                                         goto STARTOVER;
617                                 }
618                         }
619                 }
620
621                 /*
622                  * Encapsulate event into full VCALENDAR component.  Clone it first,
623                  * for two reasons: one, it's easier to just free the whole thing
624                  * when we're done instead of unbundling, but more importantly, we
625                  * can't encapsulate something that may already be encapsulated
626                  * somewhere else.
627                  */
628                 lprintf(9, "Encapsulating into full VCALENDAR component\n");
629                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
630
631                 /* If the user clicked 'Save' then save it to the server. */
632                 lprintf(9, "Serializing it for saving\n");
633                 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
634                         serv_puts("ENT0 1|||4|||1|");
635                         serv_getln(buf, sizeof buf);
636                         if (buf[0] == '8') {
637                                 serv_puts("Content-type: text/calendar");
638                                 serv_puts("");
639                                 serv_puts(icalcomponent_as_ical_string(encaps));
640                                 serv_puts("000");
641                         }
642                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
643                                 lprintf(9, "ENT0 REPLY: %s\n", buf);
644                         }
645                         icalcomponent_free(encaps);
646                 }
647
648                 /* Or, check attendee availability if the user asked for that. */
649                 if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
650
651                         /* Call this function, which does the real work */
652                         check_attendee_availability(encaps);
653
654                         /* This displays the form again, with our annotations */
655                         display_edit_individual_event(encaps, msgnum);
656
657                         icalcomponent_free(encaps);
658                 }
659
660         }
661
662         /*
663          * If the user clicked 'Delete' then delete it.
664          */
665         lprintf(9, "Checking to see if we have to delete an old event\n");
666         if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
667                 serv_printf("DELE %ld", atol(bstr("msgnum")));
668                 serv_getln(buf, sizeof buf);
669         }
670
671         if (created_new_vevent) {
672                 icalcomponent_free(vevent);
673         }
674
675         /* If this was a save or deelete, go back to the calendar view. */
676         if (strcasecmp(bstr("sc"), "Check attendee availability")) {
677                 readloop("readfwd");
678         }
679 }
680
681
682 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */