c9deb38b69ebb1da80173188df78c99ed40967f4
[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  * \brief Display an event by itself (for editing)
13  * \param supplied_vevent the event to edit
14  * \param 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("<script type=\"text/javascript\">"
82                 "function grey_all_day() { "
83                         "if (document.EventForm.alldayevent.checked) {"
84                                 "document.EventForm.dtstart_hour.value='0';"
85                                 "document.EventForm.dtstart_hour.disabled = true;"
86                                 "document.EventForm.dtstart_minute.value='0';"
87                                 "document.EventForm.dtstart_minute.disabled = true;"
88                                 "document.EventForm.dtend_hour.value='0';"
89                                 "document.EventForm.dtend_hour.disabled = true;"
90                                 "document.EventForm.dtend_minute.value='0';"
91                                 "document.EventForm.dtend_minute.disabled = true;"
92                                 "document.EventForm.dtend_month.disabled = true;"
93                                 "document.EventForm.dtend_day.disabled = true;"
94                                 "document.EventForm.dtend_year.disabled = true;"
95                         "}"
96                         "else {"
97                                 "document.EventForm.dtstart_hour.disabled = false;"
98                                 "document.EventForm.dtstart_minute.disabled = false;"
99                                 "document.EventForm.dtend_hour.disabled = false;"
100                                 "document.EventForm.dtend_minute.disabled = false;"
101                                 "document.EventForm.dtend_month.disabled = false;"
102                                 "document.EventForm.dtend_day.disabled = false;"
103                                 "document.EventForm.dtend_year.disabled = false;"
104                         "}"
105                 "}"
106                 "</script>\n"
107         );
108
109
110         wprintf("<div class=\"fix_scrollbar_bug\">"
111                 "<table  class=\"event_background\"><tr><td>\n");
112
113         /************************************************************
114          * Uncomment this to see the UID in calendar events for debugging
115         wprintf("UID == ");
116         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
117         if (p != NULL) {
118                 escputs((char *)icalproperty_get_comment(p));
119         }
120         wprintf("<br />\n");
121         wprintf("SEQUENCE == %d<br />\n", sequence);
122         *************************************************************/
123
124         wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" action=\"save_event\">\n");
125         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
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 (!IsEmptyStr(bstr("year"))) {
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 (!IsEmptyStr(bstr("hour"))) {
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 = 0;
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>\n");
351
352         /** Pop open an address book -- begin **/
353         wprintf(
354                 "&nbsp;<a href=\"javascript:PopOpenAddressBook('attendees_box|%s');\" "
355                 "title=\"%s\">"
356                 "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
357                 "</a>",
358                 _("Attendees"),
359                 _("Contacts")
360         );
361         /** Pop open an address book -- end **/
362
363         wprintf("</TD><TD>"
364                 "<TEXTAREA %s NAME=\"attendees\" id=\"attendees_box\" wrap=soft "
365                 "ROWS=3 COLS=80 WIDTH=80>\n",
366                 (organizer_is_me ? "" : "DISABLED ")
367         );
368         i = 0;
369         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
370             attendee != NULL;
371             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
372                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
373                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
374
375                         /** screen name or email address */
376                         strcpy(attendee_string, &attendee_string[7]);
377                         striplt(attendee_string);
378                         if (i++) wprintf("\n");
379                         escputs(attendee_string);
380                         wprintf(" ");
381
382                         /** participant status */
383                         partstat_as_string(buf, attendee);
384                         escputs(buf);
385                 }
386         }
387         wprintf("</TEXTAREA></TD></TR>\n");
388
389         /** Done with properties. */
390         wprintf("</TABLE>\n<CENTER>"
391                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
392                 "&nbsp;&nbsp;"
393                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
394                 "&nbsp;&nbsp;"
395                 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
396                                 "VALUE=\"%s\">\n"
397                 "&nbsp;&nbsp;"
398                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
399                 "</CENTER>\n",
400                 _("Save"),
401                 _("Delete"),
402                 _("Check attendee availability"),
403                 _("Cancel")
404         );
405
406         wprintf("</FORM>\n");
407         
408         wprintf("</td></tr></table></div>\n");
409         wprintf("<script type=\"text/javascript\">"
410                 "grey_all_day();"
411                 "</script>\n"
412         );
413         
414         address_book_popup();
415         wDumpContent(1);
416
417         if (created_new_vevent) {
418                 icalcomponent_free(vevent);
419         }
420 }
421
422 /**
423  * \brief Save an edited event
424  * \param supplied_vevent the event to save
425  * \param msgnum the index on the citserver
426  */
427 void save_individual_event(icalcomponent *supplied_vevent, long msgnum, char *from, int unread) {
428         char buf[SIZ];
429         icalproperty *prop;
430         icalcomponent *vevent, *encaps;
431         int created_new_vevent = 0;
432         int all_day_event = 0;
433         struct icaltimetype event_start, t;
434         icalproperty *attendee = NULL;
435         char attendee_string[SIZ];
436         int i, j;
437         int foundit;
438         char form_attendees[SIZ];
439         char organizer_string[SIZ];
440         int sequence = 0;
441         enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
442
443         if (supplied_vevent != NULL) {
444                 vevent = supplied_vevent;
445                 /**
446                  * If we're looking at a fully encapsulated VCALENDAR
447                  * rather than a VEVENT component, attempt to use the first
448                  * relevant VEVENT subcomponent.  If there is none, the
449                  * NULL returned by icalcomponent_get_first_component() will
450                  * tell the next iteration of this function to create a
451                  * new one.
452                  */
453                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
454                         save_individual_event(
455                                 icalcomponent_get_first_component(
456                                         vevent, ICAL_VEVENT_COMPONENT), 
457                                 msgnum, from, unread
458                         );
459                         return;
460                 }
461         }
462         else {
463                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
464                 created_new_vevent = 1;
465         }
466
467         if ( (!IsEmptyStr(bstr("save_button")))
468            || (!IsEmptyStr(bstr("check_button"))) ) {
469
470                 /** Replace values in the component with ones from the form */
471
472                 while (prop = icalcomponent_get_first_property(vevent,
473                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
474                         icalcomponent_remove_property(vevent, prop);
475                         icalproperty_free(prop);
476                 }
477
478                 if (!IsEmptyStr(bstr("summary"))) {
479         
480                         icalcomponent_add_property(vevent,
481                                         icalproperty_new_summary(bstr("summary")));
482                 } else {
483                         icalcomponent_add_property(vevent,
484                                         icalproperty_new_summary("Untitled Event"));
485                 }
486         
487                 while (prop = icalcomponent_get_first_property(vevent,
488                                         ICAL_LOCATION_PROPERTY), prop != NULL) {
489                         icalcomponent_remove_property(vevent, prop);
490                         icalproperty_free(prop);
491                 }
492                 if (!IsEmptyStr(bstr("location"))) {
493                         icalcomponent_add_property(vevent,
494                                         icalproperty_new_location(bstr("location")));
495                 }
496                 while (prop = icalcomponent_get_first_property(vevent,
497                                   ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
498                         icalcomponent_remove_property(vevent, prop);
499                         icalproperty_free(prop);
500                 }
501                 if (!IsEmptyStr(bstr("description"))) {
502                         icalcomponent_add_property(vevent,
503                                 icalproperty_new_description(bstr("description")));
504                 }
505
506                 while (prop = icalcomponent_get_first_property(vevent,
507                       ICAL_DTSTART_PROPERTY), prop != NULL) {
508                         icalcomponent_remove_property(vevent, prop);
509                         icalproperty_free(prop);
510                 }
511
512                 if (!strcmp(bstr("alldayevent"), "yes")) {
513                         all_day_event = 1;
514                 }
515                 else {
516                         all_day_event = 0;
517                 }
518
519                 if (all_day_event) {
520                         icaltime_from_webform_dateonly(&event_start, "dtstart");
521                 }
522                 else {
523                         icaltime_from_webform(&event_start, "dtstart");
524                 }
525
526                 /**
527                  * The following odd-looking snippet of code looks like it
528                  * takes some unnecessary steps.  It is done this way because
529                  * libical incorrectly turns an "all day event" into a normal
530                  * event starting at midnight (i.e. it serializes as date/time
531                  * instead of just date) unless icalvalue_new_date() is used.
532                  * So we force it, if this is an all day event.
533                  */
534                 prop = icalproperty_new_dtstart(event_start);
535                 if (all_day_event) {
536                         icalproperty_set_value(prop, icalvalue_new_date(event_start));
537                 }
538
539                 if (prop) icalcomponent_add_property(vevent, prop);
540                 else icalproperty_free(prop);
541
542                 while (prop = icalcomponent_get_first_property(vevent,
543                       ICAL_DTEND_PROPERTY), prop != NULL) {
544                         icalcomponent_remove_property(vevent, prop);
545                         icalproperty_free(prop);
546                 }
547                 while (prop = icalcomponent_get_first_property(vevent,
548                       ICAL_DURATION_PROPERTY), prop != NULL) {
549                         icalcomponent_remove_property(vevent, prop);
550                         icalproperty_free(prop);
551                 }
552
553                 if (all_day_event == 0) {
554                         icaltime_from_webform(&t, "dtend");     
555                         icalcomponent_add_property(vevent,
556                                 icalproperty_new_dtend(icaltime_normalize(t)
557                                 )
558                         );
559                 }
560
561                 /** See if transparency is indicated */
562                 if (!IsEmptyStr(bstr("transp"))) {
563                         if (!strcasecmp(bstr("transp"), "opaque")) {
564                                 formtransp = ICAL_TRANSP_OPAQUE;
565                         }
566                         else if (!strcasecmp(bstr("transp"), "transparent")) {
567                                 formtransp = ICAL_TRANSP_TRANSPARENT;
568                         }
569
570                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
571                               (prop != NULL)) {
572                                 icalcomponent_remove_property(vevent, prop);
573                                 icalproperty_free(prop);
574                         }
575
576                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
577                 }
578
579                 /** Give this event a UID if it doesn't have one. */
580                 if (icalcomponent_get_first_property(vevent,
581                    ICAL_UID_PROPERTY) == NULL) {
582                         generate_uuid(buf);
583                         icalcomponent_add_property(vevent, icalproperty_new_uid(buf));
584                 }
585
586                 /** Increment the sequence ID */
587                 while (prop = icalcomponent_get_first_property(vevent,
588                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
589                         i = icalproperty_get_sequence(prop);
590                         if (i > sequence) sequence = i;
591                         icalcomponent_remove_property(vevent, prop);
592                         icalproperty_free(prop);
593                 }
594                 ++sequence;
595                 icalcomponent_add_property(vevent,
596                         icalproperty_new_sequence(sequence)
597                 );
598                 
599                 /**
600                  * Set the organizer, only if one does not already exist *and*
601                  * the form is supplying one
602                  */
603                 strcpy(buf, bstr("organizer"));
604                 if ( (icalcomponent_get_first_property(vevent,
605                    ICAL_ORGANIZER_PROPERTY) == NULL) 
606                    && (!IsEmptyStr(buf)) ) {
607
608                         /** set new organizer */
609                         sprintf(organizer_string, "MAILTO:%s", buf);
610                         icalcomponent_add_property(vevent,
611                                 icalproperty_new_organizer(organizer_string)
612                         );
613
614                 }
615
616                 /**
617                  * Add any new attendees listed in the web form
618                  */
619
620                 /* First, strip out the parenthesized partstats.  */
621                 strcpy(form_attendees, bstr("attendees"));
622                 stripout(form_attendees, '(', ')');
623
624                 /* Next, change any commas to newlines, because we want newline-separated attendees. */
625                 j = strlen(form_attendees);
626                 for (i=0; i<j; ++i) {
627                         if (form_attendees[i] == ',') {
628                                 form_attendees[i] = '\n';
629                                 while (isspace(form_attendees[i+1])) {
630                                         strcpy(&form_attendees[i+1], &form_attendees[i+2]);
631                                 }
632                         }
633                 }
634
635                 /** Now iterate! */
636                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
637                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
638                         striplt(buf);
639                         if (!IsEmptyStr(buf)) {
640                                 sprintf(attendee_string, "MAILTO:%s", buf);
641                                 foundit = 0;
642
643                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
644                                         if (!strcasecmp(attendee_string,
645                                            icalproperty_get_attendee(attendee)))
646                                                 ++foundit;
647                                 }
648
649
650                                 if (foundit == 0) {
651                                         icalcomponent_add_property(vevent,
652                                                 icalproperty_new_attendee(attendee_string)
653                                         );
654                                 }
655                         }
656                 }
657
658                 /**
659                  * Remove any attendees *not* listed in the web form
660                  */
661 STARTOVER:      for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
662                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
663                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
664                                 strcpy(attendee_string, &attendee_string[7]);
665                                 striplt(attendee_string);
666                                 foundit = 0;
667                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
668                                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
669                                         striplt(buf);
670                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
671                                 }
672                                 if (foundit == 0) {
673                                         icalcomponent_remove_property(vevent, attendee);
674                                         icalproperty_free(attendee);
675                                         goto STARTOVER;
676                                 }
677                         }
678                 }
679
680                 /**
681                  * Encapsulate event into full VCALENDAR component.  Clone it first,
682                  * for two reasons: one, it's easier to just free the whole thing
683                  * when we're done instead of unbundling, but more importantly, we
684                  * can't encapsulate something that may already be encapsulated
685                  * somewhere else.
686                  */
687                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
688
689                 /* Set the method to PUBLISH */
690                 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
691
692                 /** If the user clicked 'Save' then save it to the server. */
693                 if ( (encaps != NULL) && (!IsEmptyStr(bstr("save_button"))) ) {
694                         serv_puts("ENT0 1|||4|||1|");
695                         serv_getln(buf, sizeof buf);
696                         if (buf[0] == '8') {
697                                 serv_puts("Content-type: text/calendar");
698                                 serv_puts("");
699                                 serv_puts(icalcomponent_as_ical_string(encaps));
700                                 serv_puts("000");
701                         }
702                         if ( (buf[0] == '8') || (buf[0] == '4') ) {
703                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
704                                 }
705                         }
706                         if (buf[0] == '2') {
707                                 strcpy(WC->ImportantMessage, &buf[4]);
708                         }
709                         icalmemory_free_ring ();
710                         icalcomponent_free(encaps);
711                 }
712
713                 /** Or, check attendee availability if the user asked for that. */
714                 if ( (encaps != NULL) && (!IsEmptyStr(bstr("check_button"))) ) {
715
716                         /** Call this function, which does the real work */
717                         check_attendee_availability(encaps);
718
719                         /** This displays the form again, with our annotations */
720                         display_edit_individual_event(encaps, msgnum, from, unread);
721
722                         icalcomponent_free(encaps);
723                 }
724
725         }
726
727         /**
728          * If the user clicked 'Delete' then delete it.
729          */
730         if ( (!IsEmptyStr(bstr("delete_button"))) && (msgnum > 0L) ) {
731                 serv_printf("DELE %ld", atol(bstr("msgnum")));
732                 serv_getln(buf, sizeof buf);
733         }
734
735         if (created_new_vevent) {
736                 icalcomponent_free(vevent);
737         }
738
739         /** If this was a save or delete, go back to the calendar view. */
740         if (IsEmptyStr(bstr("check_button"))) {
741                 readloop("readfwd");
742         }
743 }
744
745