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