Maximum length of a date input field is now 10 characters.
[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  * Display an event by itself (for editing)
12  * supplied_vevent      the event to edit
13  * msgnum               reference on the citserver
14  */
15 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum, char *from, int unread) {
16         icalcomponent *vevent;
17         icalproperty *p;
18         icalvalue *v;
19         struct icaltimetype t_start, t_end;
20         time_t now;
21         struct tm tm_now;
22         int created_new_vevent = 0;
23         icalproperty *organizer = NULL;
24         char organizer_string[SIZ];
25         icalproperty *attendee = NULL;
26         char attendee_string[SIZ];
27         char buf[SIZ];
28         int organizer_is_me = 0;
29         int i, j = 0;
30         int sequence = 0;
31         char weekday_labels[7][32];
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         /* populate the weekday names - begin */
38         now = time(NULL);
39         localtime_r(&now, &tm_now);
40         while (tm_now.tm_wday != 0) {
41                 now -= 86400L;
42                 localtime_r(&now, &tm_now);
43         }
44         for (i=0; i<7; ++i) {
45                 localtime_r(&now, &tm_now);
46                 wc_strftime(weekday_labels[i], 32, "%A", &tm_now);
47                 now += 86400L;
48         }
49         /* populate the weekday names - end */
50
51         now = time(NULL);
52         strcpy(organizer_string, "");
53         strcpy(attendee_string, "");
54
55         if (supplied_vevent != NULL) {
56                 vevent = supplied_vevent;
57                 /*
58                  * If we're looking at a fully encapsulated VCALENDAR
59                  * rather than a VEVENT component, attempt to use the first
60                  * relevant VEVENT subcomponent.  If there is none, the
61                  * NULL returned by icalcomponent_get_first_component() will
62                  * tell the next iteration of this function to create a
63                  * new one.
64                  */
65                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
66                         display_edit_individual_event(
67                                 icalcomponent_get_first_component(
68                                         vevent, ICAL_VEVENT_COMPONENT), 
69                                 msgnum, from, unread
70                         );
71                         return;
72                 }
73         }
74         else {
75                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
76                 created_new_vevent = 1;
77         }
78
79         /* Learn the sequence */
80         p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
81         if (p != NULL) {
82                 sequence = icalproperty_get_sequence(p);
83         }
84
85         /* Begin output */
86         output_headers(1, 1, 2, 0, 0, 0);
87         wprintf("<div id=\"banner\">\n");
88         wprintf("<h1>");
89         wprintf(_("Add or edit an event"));
90         wprintf("</h1>");
91         wprintf("</div>\n");
92
93         wprintf("<div id=\"content\" class=\"service\">\n");
94
95         wprintf("<div class=\"fix_scrollbar_bug\">");
96
97         /************************************************************
98          * Uncomment this to see the UID in calendar events for debugging
99         wprintf("UID == ");
100         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
101         if (p != NULL) {
102                 escputs((char *)icalproperty_get_comment(p));
103         }
104         wprintf("<br />\n");
105         wprintf("SEQUENCE == %d<br />\n", sequence);
106         *************************************************************/
107
108         wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" action=\"save_event\">\n");
109         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
110
111         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
112                 msgnum);
113         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
114                 bstr("calview"));
115         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
116                 bstr("year"));
117         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
118                 bstr("month"));
119         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
120                 bstr("day"));
121
122         char *tabnames[] = {
123                 _("Event"),
124                 _("Attendees"),
125                 _("Recurrence")
126         };
127
128         tabbed_dialog(3, tabnames);
129         begin_tab(0, 3);
130
131         /* Put it in a borderless table so it lines up nicely */
132         wprintf("<TABLE border=0 width=100%%>\n");
133
134         wprintf("<TR><TD><B>");
135         wprintf(_("Summary"));
136         wprintf("</B></TD><TD>\n"
137                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
138                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
139         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
140         if (p != NULL) {
141                 escputs((char *)icalproperty_get_comment(p));
142         }
143         wprintf("\"></TD></TR>\n");
144
145         wprintf("<TR><TD><B>");
146         wprintf(_("Location"));
147         wprintf("</B></TD><TD>\n"
148                 "<INPUT TYPE=\"text\" NAME=\"location\" "
149                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
150         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_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(_("Start"));
158         wprintf("</B></TD><TD>\n");
159         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
160         if (p != NULL) {
161                 t_start = icalproperty_get_dtstart(p);
162                 if (t_start.is_date) {
163                         t_start.hour = 0;
164                         t_start.minute = 0;
165                         t_start.second = 0;
166                 }
167         }
168         else {
169                 localtime_r(&now, &tm_now);
170                 if (havebstr("year")) {
171                         tm_now.tm_year = ibstr("year") - 1900;
172                         tm_now.tm_mon = ibstr("month") - 1;
173                         tm_now.tm_mday = ibstr("day");
174                 }
175                 if (havebstr("hour")) {
176                         tm_now.tm_hour = ibstr("hour");
177                         tm_now.tm_min = ibstr("minute");
178                         tm_now.tm_sec = 0;
179                 }
180                 else {
181                         tm_now.tm_hour = 0;
182                         tm_now.tm_min = 0;
183                         tm_now.tm_sec = 0;
184                 }
185
186                 t_start = icaltime_from_timet_with_zone(
187                         mktime(&tm_now),
188                         ((yesbstr("alldayevent")) ? 1 : 0),
189                         icaltimezone_get_utc_timezone()
190                 );
191                 t_start.is_utc = 1;
192
193         }
194         display_icaltimetype_as_webform(&t_start, "dtstart", 0);
195
196         wprintf("<INPUT TYPE=\"checkbox\" id=\"alldayevent\" NAME=\"alldayevent\" "
197                 "VALUE=\"yes\" onclick=\"eventEditAllDay();\""
198                 " %s >%s",
199                 (t_start.is_date ? "CHECKED=\"CHECKED\"" : "" ),
200                 _("All day event")
201         );
202
203         wprintf("</TD></TR>\n");
204
205         /*
206          * If this is an all-day-event, set the end time to be identical to
207          * the start time (the hour/minute/second will be set to midnight).
208          * Otherwise extract or create it.
209          */
210         wprintf("<TR><TD><B>");
211         wprintf(_("End"));
212         wprintf("</B></TD><TD id=\"dtendcell\">\n");
213         if (t_start.is_date) {
214                 t_end = t_start;
215         }
216         else {
217                 p = icalcomponent_get_first_property(vevent,
218                                                         ICAL_DTEND_PROPERTY);
219                 if (p != NULL) {
220                         t_end = icalproperty_get_dtend(p);
221                 }
222                 else {
223                         /*
224                          * If this is not an all-day event and there is no
225                          * end time specified, make the default one hour
226                          * from the start time.
227                          */
228                         t_end = t_start;
229                         t_end.hour += 1;
230                         t_end.second = 0;
231                         t_end = icaltime_normalize(t_end);
232                         /* t_end = icaltime_from_timet(now, 0); */
233                 }
234         }
235         display_icaltimetype_as_webform(&t_end, "dtend", 0);
236         wprintf("</TD></TR>\n");
237
238         wprintf("<TR><TD><B>");
239         wprintf(_("Notes"));
240         wprintf("</B></TD><TD>\n"
241                 "<TEXTAREA NAME=\"description\" wrap=soft "
242                 "ROWS=5 COLS=80 WIDTH=80>\n"
243         );
244         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
245         if (p != NULL) {
246                 escputs((char *)icalproperty_get_comment(p));
247         }
248         wprintf("</TEXTAREA></TD></TR>");
249
250         /**
251          * For a new event, the user creating the event should be the
252          * organizer.  Set this field accordingly.
253          */
254         if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
255            == NULL) {
256                 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
257                 icalcomponent_add_property(vevent,
258                         icalproperty_new_organizer(organizer_string)
259                 );
260         }
261
262         /**
263          * Determine who is the organizer of this event.
264          * We need to determine "me" or "not me."
265          */
266         organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
267         if (organizer != NULL) {
268                 strcpy(organizer_string, icalproperty_get_organizer(organizer));
269                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
270                         strcpy(organizer_string, &organizer_string[7]);
271                         striplt(organizer_string);
272                         serv_printf("ISME %s", organizer_string);
273                         serv_getln(buf, sizeof buf);
274                         if (buf[0] == '2') {
275                                 organizer_is_me = 1;
276                         }
277                 }
278         }
279
280         wprintf("<TR><TD><B>");
281         wprintf(_("Organizer"));
282         wprintf("</B></TD><TD>");
283         escputs(organizer_string);
284         if (organizer_is_me) {
285                 wprintf(" <FONT SIZE=-1><I>");
286                 wprintf(_("(you are the organizer)"));
287                 wprintf("</I></FONT>\n");
288         }
289
290         /**
291          * Transmit the organizer as a hidden field.   We don't want the user
292          * to be able to change it, but we do want it fed back to the server,
293          * especially if this is a new event and there is no organizer already
294          * in the calendar object.
295          */
296         wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
297         escputs(organizer_string);
298         wprintf("\">");
299
300         wprintf("</TD></TR>\n");
301
302         /** Transparency */
303         wprintf("<TR><TD><B>");
304         wprintf(_("Show time as:"));
305         wprintf("</B></TD><TD>");
306
307         p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
308         if (p == NULL) {
309                 /** No transparency found.  Default to opaque (busy). */
310                 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
311                 if (p != NULL) {
312                         icalcomponent_add_property(vevent, p);
313                 }
314         }
315         if (p != NULL) {
316                 v = icalproperty_get_value(p);
317         }
318         else {
319                 v = NULL;
320         }
321
322         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
323         if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
324                 wprintf(" CHECKED");
325         wprintf(">");
326         wprintf(_("Free"));
327         wprintf("&nbsp;&nbsp;");
328
329         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
330         if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
331                 wprintf(" CHECKED");
332         wprintf(">");
333         wprintf(_("Busy"));
334
335         wprintf("</TD></TR>\n");
336
337
338         /** Done with properties. */
339         wprintf("</TABLE>\n");
340
341         end_tab(0, 3);
342
343         /* Attendees tab (need to move things here) */
344         begin_tab(1, 3);
345         wprintf("<TABLE border=0 width=100%%>\n");      /* same table style as the event tab */
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         wprintf("</TABLE>\n");
390         end_tab(1, 3);
391
392         /* Recurrence tab */
393         begin_tab(2, 3);
394         icalproperty *rrule = NULL;
395         struct icalrecurrencetype recur;
396
397         rrule = icalcomponent_get_first_property(vevent, ICAL_RRULE_PROPERTY);
398         if (rrule) {
399                 recur = icalproperty_get_rrule(rrule);
400         }
401         else {
402                 /* blank recurrence with some sensible defaults */
403                 memset(&recur, 0, sizeof(struct icalrecurrencetype));
404                 recur.count = 3;
405                 recur.until = icaltime_null_time();
406                 recur.interval = 1;
407                 recur.freq = ICAL_WEEKLY_RECURRENCE;
408         }
409
410         wprintf("<INPUT TYPE=\"checkbox\" id=\"is_recur\" NAME=\"is_recur\" "
411                 "VALUE=\"yes\" "
412                 "onclick=\"RecurrenceShowHide();\""
413                 " %s >%s",
414                 (rrule ? "CHECKED=\"CHECKED\"" : "" ),
415                 _("This is a repeating event")
416         );
417
418         wprintf("<div id=\"rrule\">\n");                /* begin 'rrule' div */
419
420         wprintf("<table border=0 width=100%%>\n");      /* same table style as the event tab */
421
422         /* Table row displaying raw RRULE data, FIXME remove when finished */
423         if (rrule) {
424                 wprintf("<tr><td><b>");
425                 wprintf("Raw data");
426                 wprintf("</b></td><td>");
427                 wprintf("<tt>%s</tt>", icalrecurrencetype_as_string(&recur));
428                 wprintf("</td></tr>\n");
429         }
430
431         char *frequency_units[] = {
432                 _("seconds"),
433                 _("minutes"),
434                 _("hours"),
435                 _("days"),
436                 _("weeks"),
437                 _("months"),
438                 _("years"),
439                 _("never")
440         };
441
442
443
444
445         wprintf("<tr><td><b>");
446         wprintf(_("Recurrence rule"));
447         wprintf("</b></td><td>");
448         if ((recur.freq < 0) || (recur.freq > 6)) recur.freq = 4;
449         wprintf("%s ", _("Repeats every"));
450
451         wprintf("<input type=\"text\" name=\"interval\" maxlength=\"3\" size=\"3\" ");
452         wprintf("value=\"%d\"> ", recur.interval);
453
454         wprintf("<select name=\"freq\" id=\"freq_selector\" size=\"1\" "
455                 "onChange=\"RecurrenceShowHide();\">\n");
456         for (i=0; i<(sizeof frequency_units / sizeof(char *)); ++i) {
457                 wprintf("<option %s%svalue=\"%d\">%s</option>\n",
458                         ((i == recur.freq) ? "selected " : ""),
459                         (((i == recur.freq) || ((i>=3)&&(i<=5))) ? "" : "disabled "),
460                         i,
461                         frequency_units[i]
462                 );
463         }
464         wprintf("</select>\n");
465
466         wprintf("<div id=\"weekday_selector\">");       /* begin 'weekday_selector' div */
467         wprintf("%s<br>", _("on these weekdays:"));
468
469         char weekday_is_selected[7];
470         memset(weekday_is_selected, 0, 7);
471
472         for (i=0; i<ICAL_BY_DAY_SIZE; ++i) {
473                 if (recur.by_day[i] == ICAL_RECURRENCE_ARRAY_MAX) {
474                         i = ICAL_RECURRENCE_ARRAY_MAX;                  /* all done */
475                 }
476                 else {
477                         for (j=0; j<7; ++j) {
478                                 if (icalrecurrencetype_day_day_of_week(recur.by_day[i]) == j+1) {
479                                         weekday_is_selected[j] = 1;
480                                 }
481                         }
482                 }
483         }
484
485         for (i=0; i<7; ++i) {
486                 wprintf("<input type=\"checkbox\" name=\"weekday%d\" value=\"yes\"", i);
487                 if (weekday_is_selected[i]) wprintf(" checked");
488                 wprintf(">%s</input>\n", weekday_labels[i]);
489         }
490         wprintf("</div>\n");                            /* end 'weekday_selector' div */
491
492         wprintf("</td></tr>\n");
493
494
495         int which_rrend_is_preselected = 0;
496         if (!icaltime_is_null_time(recur.until)) which_rrend_is_preselected = 2;
497         if (recur.count > 0) which_rrend_is_preselected = 1;
498
499         wprintf("<tr><td><b>");
500         wprintf(_("Recurrence range"));
501         wprintf("</b></td><td>\n");
502
503         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_none\" "
504                 "%s onChange=\"RecurrenceShowHide();\">",
505                 ((which_rrend_is_preselected == 0) ? "checked" : "")
506         );
507         wprintf("%s</input><br />\n", _("No ending date"));
508
509         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_count\" "
510                 "%s onChange=\"RecurrenceShowHide();\">",
511                 ((which_rrend_is_preselected == 1) ? "checked" : "")
512         );
513         wprintf(_("Repeat this event"));
514         wprintf("</input> ");
515         wprintf("<input type=\"text\" name=\"rrcount\" id=\"rrcount\" maxlength=\"3\" size=\"3\" ");
516         wprintf("value=\"%d\"> ", recur.count);
517         wprintf(_("times"));
518         wprintf("<br />\n");
519
520         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_until\" "
521                 "%s onChange=\"RecurrenceShowHide();\">",
522                 ((which_rrend_is_preselected == 2) ? "checked" : "")
523         );
524         wprintf(_("Repeat this event until "));
525         wprintf("</input>");
526         display_icaltimetype_as_webform(&recur.until, "rruntil", 1);
527         wprintf("<br />\n");
528
529         wprintf("</td></tr>\n");
530
531
532
533
534         wprintf("</table>\n");
535         wprintf("</div>\n");                            /* end 'rrule' div */
536
537         end_tab(2, 3);
538
539         /* submit buttons (common area beneath the tabs) */
540         begin_tab(3, 3);
541         wprintf("<CENTER>"
542                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
543                 "&nbsp;&nbsp;"
544                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
545                 "&nbsp;&nbsp;"
546                 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
547                                 "VALUE=\"%s\">\n"
548                 "&nbsp;&nbsp;"
549                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
550                 "</CENTER>\n",
551                 _("Save"),
552                 _("Delete"),
553                 _("Check attendee availability"),
554                 _("Cancel")
555         );
556         wprintf("</FORM>\n");
557         end_tab(3, 3);
558
559         wprintf("</div>\n");
560
561         wprintf("<script type=\"text/javascript\">      \n"
562                 "eventEditAllDay();     \n"
563                 "RecurrenceShowHide();  \n"
564                 "</script>      \n"
565         );
566         address_book_popup();
567         wDumpContent(1);
568
569         if (created_new_vevent) {
570                 icalcomponent_free(vevent);
571         }
572 }
573
574 /*
575  * Save an edited event
576  *
577  * supplied_vevent:     the event to save
578  * msgnum:              the index on the citserver
579  */
580 void save_individual_event(icalcomponent *supplied_vevent, long msgnum, char *from, int unread) {
581         char buf[SIZ];
582         icalproperty *prop;
583         icalcomponent *vevent, *encaps;
584         int created_new_vevent = 0;
585         int all_day_event = 0;
586         struct icaltimetype event_start, t;
587         icalproperty *attendee = NULL;
588         char attendee_string[SIZ];
589         int i, j;
590         int foundit;
591         char form_attendees[SIZ];
592         char organizer_string[SIZ];
593         int sequence = 0;
594         enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
595
596         if (supplied_vevent != NULL) {
597                 vevent = supplied_vevent;
598                 /**
599                  * If we're looking at a fully encapsulated VCALENDAR
600                  * rather than a VEVENT component, attempt to use the first
601                  * relevant VEVENT subcomponent.  If there is none, the
602                  * NULL returned by icalcomponent_get_first_component() will
603                  * tell the next iteration of this function to create a
604                  * new one.
605                  */
606                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
607                         save_individual_event(
608                                 icalcomponent_get_first_component(
609                                         vevent, ICAL_VEVENT_COMPONENT), 
610                                 msgnum, from, unread
611                         );
612                         return;
613                 }
614         }
615         else {
616                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
617                 created_new_vevent = 1;
618         }
619
620         if ( (havebstr("save_button"))
621            || (havebstr("check_button")) ) {
622
623                 /** Replace values in the component with ones from the form */
624
625                 while (prop = icalcomponent_get_first_property(vevent,
626                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
627                         icalcomponent_remove_property(vevent, prop);
628                         icalproperty_free(prop);
629                 }
630
631                 if (havebstr("summary")) {
632         
633                         icalcomponent_add_property(vevent,
634                                         icalproperty_new_summary(bstr("summary")));
635                 } else {
636                         icalcomponent_add_property(vevent,
637                                         icalproperty_new_summary("Untitled Event"));
638                 }
639         
640                 while (prop = icalcomponent_get_first_property(vevent,
641                                         ICAL_LOCATION_PROPERTY), prop != NULL) {
642                         icalcomponent_remove_property(vevent, prop);
643                         icalproperty_free(prop);
644                 }
645                 if (havebstr("location")) {
646                         icalcomponent_add_property(vevent,
647                                         icalproperty_new_location(bstr("location")));
648                 }
649                 while (prop = icalcomponent_get_first_property(vevent,
650                                   ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
651                         icalcomponent_remove_property(vevent, prop);
652                         icalproperty_free(prop);
653                 }
654                 if (havebstr("description")) {
655                         icalcomponent_add_property(vevent,
656                                 icalproperty_new_description(bstr("description")));
657                 }
658
659                 while (prop = icalcomponent_get_first_property(vevent,
660                       ICAL_DTSTART_PROPERTY), prop != NULL) {
661                         icalcomponent_remove_property(vevent, prop);
662                         icalproperty_free(prop);
663                 }
664
665                 if (yesbstr("alldayevent")) {
666                         all_day_event = 1;
667                 }
668                 else {
669                         all_day_event = 0;
670                 }
671
672                 if (all_day_event) {
673                         icaltime_from_webform_dateonly(&event_start, "dtstart");
674                 }
675                 else {
676                         icaltime_from_webform(&event_start, "dtstart");
677                 }
678
679                 /**
680                  * The following odd-looking snippet of code looks like it
681                  * takes some unnecessary steps.  It is done this way because
682                  * libical incorrectly turns an "all day event" into a normal
683                  * event starting at midnight (i.e. it serializes as date/time
684                  * instead of just date) unless icalvalue_new_date() is used.
685                  * So we force it, if this is an all day event.
686                  */
687                 prop = icalproperty_new_dtstart(event_start);
688                 if (all_day_event) {
689                         icalproperty_set_value(prop, icalvalue_new_date(event_start));
690                 }
691
692                 if (prop) icalcomponent_add_property(vevent, prop);
693                 else icalproperty_free(prop);
694
695                 while (prop = icalcomponent_get_first_property(vevent,
696                       ICAL_DTEND_PROPERTY), prop != NULL) {
697                         icalcomponent_remove_property(vevent, prop);
698                         icalproperty_free(prop);
699                 }
700                 while (prop = icalcomponent_get_first_property(vevent,
701                       ICAL_DURATION_PROPERTY), prop != NULL) {
702                         icalcomponent_remove_property(vevent, prop);
703                         icalproperty_free(prop);
704                 }
705
706                 if (all_day_event == 0) {
707                         icaltime_from_webform(&t, "dtend");     
708                         icalcomponent_add_property(vevent,
709                                 icalproperty_new_dtend(icaltime_normalize(t)
710                                 )
711                         );
712                 }
713
714                 /** See if transparency is indicated */
715                 if (havebstr("transp")) {
716                         if (!strcasecmp(bstr("transp"), "opaque")) {
717                                 formtransp = ICAL_TRANSP_OPAQUE;
718                         }
719                         else if (!strcasecmp(bstr("transp"), "transparent")) {
720                                 formtransp = ICAL_TRANSP_TRANSPARENT;
721                         }
722
723                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
724                               (prop != NULL)) {
725                                 icalcomponent_remove_property(vevent, prop);
726                                 icalproperty_free(prop);
727                         }
728
729                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
730                 }
731
732                 /** Give this event a UID if it doesn't have one. */
733                 if (icalcomponent_get_first_property(vevent,
734                    ICAL_UID_PROPERTY) == NULL) {
735                         generate_uuid(buf);
736                         icalcomponent_add_property(vevent, icalproperty_new_uid(buf));
737                 }
738
739                 /** Increment the sequence ID */
740                 while (prop = icalcomponent_get_first_property(vevent,
741                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
742                         i = icalproperty_get_sequence(prop);
743                         if (i > sequence) sequence = i;
744                         icalcomponent_remove_property(vevent, prop);
745                         icalproperty_free(prop);
746                 }
747                 ++sequence;
748                 icalcomponent_add_property(vevent,
749                         icalproperty_new_sequence(sequence)
750                 );
751                 
752                 /**
753                  * Set the organizer, only if one does not already exist *and*
754                  * the form is supplying one
755                  */
756                 strcpy(buf, bstr("organizer"));
757                 if ( (icalcomponent_get_first_property(vevent,
758                    ICAL_ORGANIZER_PROPERTY) == NULL) 
759                    && (!IsEmptyStr(buf)) ) {
760
761                         /** set new organizer */
762                         sprintf(organizer_string, "MAILTO:%s", buf);
763                         icalcomponent_add_property(vevent,
764                                 icalproperty_new_organizer(organizer_string)
765                         );
766
767                 }
768
769                 /**
770                  * Add any new attendees listed in the web form
771                  */
772
773                 /* First, strip out the parenthesized partstats.  */
774                 strcpy(form_attendees, bstr("attendees"));
775                 stripout(form_attendees, '(', ')');
776
777                 /* Next, change any commas to newlines, because we want newline-separated attendees. */
778                 j = strlen(form_attendees);
779                 for (i=0; i<j; ++i) {
780                         if (form_attendees[i] == ',') {
781                                 form_attendees[i] = '\n';
782                                 while (isspace(form_attendees[i+1])) {
783                                         strcpy(&form_attendees[i+1], &form_attendees[i+2]);
784                                 }
785                         }
786                 }
787
788                 /** Now iterate! */
789                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
790                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
791                         striplt(buf);
792                         if (!IsEmptyStr(buf)) {
793                                 sprintf(attendee_string, "MAILTO:%s", buf);
794                                 foundit = 0;
795
796                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
797                                         if (!strcasecmp(attendee_string,
798                                            icalproperty_get_attendee(attendee)))
799                                                 ++foundit;
800                                 }
801
802
803                                 if (foundit == 0) {
804                                         icalcomponent_add_property(vevent,
805                                                 icalproperty_new_attendee(attendee_string)
806                                         );
807                                 }
808                         }
809                 }
810
811                 /**
812                  * Remove any attendees *not* listed in the web form
813                  */
814 STARTOVER:      for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
815                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
816                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
817                                 strcpy(attendee_string, &attendee_string[7]);
818                                 striplt(attendee_string);
819                                 foundit = 0;
820                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
821                                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
822                                         striplt(buf);
823                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
824                                 }
825                                 if (foundit == 0) {
826                                         icalcomponent_remove_property(vevent, attendee);
827                                         icalproperty_free(attendee);
828                                         goto STARTOVER;
829                                 }
830                         }
831                 }
832
833                 /**
834                  * Encapsulate event into full VCALENDAR component.  Clone it first,
835                  * for two reasons: one, it's easier to just free the whole thing
836                  * when we're done instead of unbundling, but more importantly, we
837                  * can't encapsulate something that may already be encapsulated
838                  * somewhere else.
839                  */
840                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
841
842                 /* Set the method to PUBLISH */
843                 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
844
845                 /** If the user clicked 'Save' then save it to the server. */
846                 if ( (encaps != NULL) && (havebstr("save_button")) ) {
847                         serv_puts("ENT0 1|||4|||1|");
848                         serv_getln(buf, sizeof buf);
849                         if (buf[0] == '8') {
850                                 serv_puts("Content-type: text/calendar");
851                                 serv_puts("");
852                                 serv_puts(icalcomponent_as_ical_string(encaps));
853                                 serv_puts("000");
854                         }
855                         if ( (buf[0] == '8') || (buf[0] == '4') ) {
856                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
857                                 }
858                         }
859                         if (buf[0] == '2') {
860                                 strcpy(WC->ImportantMessage, &buf[4]);
861                         }
862                         icalmemory_free_ring ();
863                         icalcomponent_free(encaps);
864                         encaps = NULL;
865                 }
866
867                 /** Or, check attendee availability if the user asked for that. */
868                 if ( (encaps != NULL) && (havebstr("check_button")) ) {
869
870                         /* Call this function, which does the real work */
871                         check_attendee_availability(encaps);
872
873                         /** This displays the form again, with our annotations */
874                         display_edit_individual_event(encaps, msgnum, from, unread);
875
876                         icalcomponent_free(encaps);
877                         encaps = NULL;
878                 }
879                 if (encaps != NULL) {
880                         icalcomponent_free(encaps);
881                         encaps = NULL;
882                 }
883
884         }
885
886         /*
887          * If the user clicked 'Delete' then delete it.
888          */
889         if ( (havebstr("delete_button")) && (msgnum > 0L) ) {
890                 serv_printf("DELE %ld", lbstr("msgnum"));
891                 serv_getln(buf, sizeof buf);
892         }
893
894         if (created_new_vevent) {
895                 icalcomponent_free(vevent);
896         }
897
898         /* If this was a save or delete, go back to the calendar view. */
899         if (!havebstr("check_button")) {
900                 readloop("readfwd");
901         }
902 }