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