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