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