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