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