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