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