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