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