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