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