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