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