* make readloop long-controlled
[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,
16         int unread, struct calview *calv)
17 {
18         icalcomponent *vevent;
19         icalproperty *p;
20         icalvalue *v;
21         struct icaltimetype t_start, t_end;
22         time_t now;
23         struct tm tm_now;
24         int created_new_vevent = 0;
25         icalproperty *organizer = NULL;
26         char organizer_string[SIZ];
27         icalproperty *attendee = NULL;
28         char attendee_string[SIZ];
29         char buf[SIZ];
30         int organizer_is_me = 0;
31         int i, j = 0;
32         int sequence = 0;
33         char weekday_labels[7][32];
34         char month_labels[12][32];
35         long weekstart = 0;
36
37         get_pref_long("weekstart", &weekstart, 17);
38         if (weekstart > 6) weekstart = 0;
39
40         lprintf(9, "display_edit_individual_event(%ld) calview=%s year=%s month=%s day=%s\n",
41                 msgnum, bstr("calview"), bstr("year"), bstr("month"), bstr("day")
42         );
43
44         /* populate the weekday names - begin */
45         now = time(NULL);
46         localtime_r(&now, &tm_now);
47         while (tm_now.tm_wday != 0) {
48                 now -= 86400L;
49                 localtime_r(&now, &tm_now);
50         }
51         for (i=0; i<7; ++i) {
52                 localtime_r(&now, &tm_now);
53                 wc_strftime(weekday_labels[i], 32, "%A", &tm_now);
54                 now += 86400L;
55         }
56         /* populate the weekday names - end */
57
58         /* populate the month names - begin */
59         now = 259200L;  /* 1970-jan-04 is the first Sunday ever */
60         localtime_r(&now, &tm_now);
61         for (i=0; i<12; ++i) {
62                 localtime_r(&now, &tm_now);
63                 wc_strftime(month_labels[i], 32, "%B", &tm_now);
64                 now += 2678400L;
65         }
66         /* populate the month names - end */
67
68         now = time(NULL);
69         strcpy(organizer_string, "");
70         strcpy(attendee_string, "");
71
72         if (supplied_vevent != NULL) {
73                 vevent = supplied_vevent;
74
75                 /* Convert all timestamps to UTC to make them easier to process. */
76                 ical_dezonify(vevent);
77
78                 /*
79                  * If we're looking at a fully encapsulated VCALENDAR
80                  * rather than a VEVENT component, attempt to use the first
81                  * relevant VEVENT subcomponent.  If there is none, the
82                  * NULL returned by icalcomponent_get_first_component() will
83                  * tell the next iteration of this function to create a
84                  * new one.
85                  */
86                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
87                         display_edit_individual_event(
88                                 icalcomponent_get_first_component(
89                                         vevent, ICAL_VEVENT_COMPONENT), 
90                                 msgnum, from, unread, NULL
91                         );
92                         return;
93                 }
94         }
95         else {
96                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
97                 created_new_vevent = 1;
98         }
99
100         /* Learn the sequence */
101         p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
102         if (p != NULL) {
103                 sequence = icalproperty_get_sequence(p);
104         }
105
106         /* Begin output */
107         output_headers(1, 1, 2, 0, 0, 0);
108         wprintf("<div id=\"banner\">\n");
109         wprintf("<h1>");
110         wprintf(_("Add or edit an event"));
111         wprintf("</h1>");
112         wprintf("</div>\n");
113
114         wprintf("<div id=\"content\" class=\"service\">\n");
115
116         wprintf("<div class=\"fix_scrollbar_bug\">");
117
118         /************************************************************
119          * Uncomment this to see the UID in calendar events for debugging
120         wprintf("UID == ");
121         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
122         if (p != NULL) {
123                 escputs((char *)icalproperty_get_comment(p));
124         }
125         wprintf("<br />\n");
126         wprintf("SEQUENCE == %d<br />\n", sequence);
127         *************************************************************/
128
129         wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" action=\"save_event\">\n");
130         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
131
132         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
133                 msgnum);
134         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
135                 bstr("calview"));
136         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
137                 bstr("year"));
138         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
139                 bstr("month"));
140         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
141                 bstr("day"));
142
143         char *tabnames[] = {
144                 _("Event"),
145                 _("Attendees"),
146                 _("Recurrence")
147         };
148
149         tabbed_dialog(3, tabnames);
150         begin_tab(0, 3);
151
152         /* Put it in a borderless table so it lines up nicely */
153         wprintf("<TABLE border=0 width=100%%>\n");
154
155         wprintf("<TR><TD><B>");
156         wprintf(_("Summary"));
157         wprintf("</B></TD><TD>\n"
158                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
159                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
160         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
161         if (p != NULL) {
162                 escputs((char *)icalproperty_get_comment(p));
163         }
164         wprintf("\"></TD></TR>\n");
165
166         wprintf("<TR><TD><B>");
167         wprintf(_("Location"));
168         wprintf("</B></TD><TD>\n"
169                 "<INPUT TYPE=\"text\" NAME=\"location\" "
170                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
171         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
172         if (p != NULL) {
173                 escputs((char *)icalproperty_get_comment(p));
174         }
175         wprintf("\"></TD></TR>\n");
176
177         wprintf("<TR><TD><B>");
178         wprintf(_("Start"));
179         wprintf("</B></TD><TD>\n");
180         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
181         if (p != NULL) {
182                 t_start = icalproperty_get_dtstart(p);
183                 if (t_start.is_date) {
184                         t_start.hour = 0;
185                         t_start.minute = 0;
186                         t_start.second = 0;
187                 }
188         }
189         else {
190                 localtime_r(&now, &tm_now);
191                 if (havebstr("year")) {
192                         tm_now.tm_year = ibstr("year") - 1900;
193                         tm_now.tm_mon = ibstr("month") - 1;
194                         tm_now.tm_mday = ibstr("day");
195                 }
196                 if (havebstr("hour")) {
197                         tm_now.tm_hour = ibstr("hour");
198                         tm_now.tm_min = ibstr("minute");
199                         tm_now.tm_sec = 0;
200                 }
201                 else {
202                         tm_now.tm_hour = 0;
203                         tm_now.tm_min = 0;
204                         tm_now.tm_sec = 0;
205                 }
206
207                 t_start = icaltime_from_timet_with_zone(
208                         mktime(&tm_now),
209                         ((yesbstr("alldayevent")) ? 1 : 0),
210                         icaltimezone_get_utc_timezone()
211                 );
212                 t_start.is_utc = 1;
213
214         }
215         display_icaltimetype_as_webform(&t_start, "dtstart", 0);
216
217         wprintf("<INPUT TYPE=\"checkbox\" id=\"alldayevent\" NAME=\"alldayevent\" "
218                 "VALUE=\"yes\" onclick=\"eventEditAllDay();\""
219                 " %s >%s",
220                 (t_start.is_date ? "CHECKED=\"CHECKED\"" : "" ),
221                 _("All day event")
222         );
223
224         wprintf("</TD></TR>\n");
225
226         /*
227          * If this is an all-day-event, set the end time to be identical to
228          * the start time (the hour/minute/second will be set to midnight).
229          * Otherwise extract or create it.
230          */
231         wprintf("<TR><TD><B>");
232         wprintf(_("End"));
233         wprintf("</B></TD><TD id=\"dtendcell\">\n");
234         if (t_start.is_date) {
235                 t_end = t_start;
236         }
237         else {
238                 p = icalcomponent_get_first_property(vevent,
239                                                         ICAL_DTEND_PROPERTY);
240                 if (p != NULL) {
241                         t_end = icalproperty_get_dtend(p);
242                 }
243                 else {
244                         /*
245                          * If this is not an all-day event and there is no
246                          * end time specified, make the default one hour
247                          * from the start time.
248                          */
249                         t_end = t_start;
250                         t_end.hour += 1;
251                         t_end.second = 0;
252                         t_end = icaltime_normalize(t_end);
253                         /* t_end = icaltime_from_timet(now, 0); */
254                 }
255         }
256         display_icaltimetype_as_webform(&t_end, "dtend", 0);
257         wprintf("</TD></TR>\n");
258
259         wprintf("<TR><TD><B>");
260         wprintf(_("Notes"));
261         wprintf("</B></TD><TD>\n"
262                 "<TEXTAREA NAME=\"description\" wrap=soft "
263                 "ROWS=5 COLS=80 WIDTH=80>\n"
264         );
265         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
266         if (p != NULL) {
267                 escputs((char *)icalproperty_get_comment(p));
268         }
269         wprintf("</TEXTAREA></TD></TR>");
270
271         /*
272          * For a new event, the user creating the event should be the
273          * organizer.  Set this field accordingly.
274          */
275         if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
276            == NULL) {
277                 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
278                 icalcomponent_add_property(vevent,
279                         icalproperty_new_organizer(organizer_string)
280                 );
281         }
282
283         /*
284          * Determine who is the organizer of this event.
285          * We need to determine "me" or "not me."
286          */
287         organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
288         if (organizer != NULL) {
289                 strcpy(organizer_string, icalproperty_get_organizer(organizer));
290                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
291                         strcpy(organizer_string, &organizer_string[7]);
292                         striplt(organizer_string);
293                         serv_printf("ISME %s", organizer_string);
294                         serv_getln(buf, sizeof buf);
295                         if (buf[0] == '2') {
296                                 organizer_is_me = 1;
297                         }
298                 }
299         }
300
301         wprintf("<TR><TD><B>");
302         wprintf(_("Organizer"));
303         wprintf("</B></TD><TD>");
304         escputs(organizer_string);
305         if (organizer_is_me) {
306                 wprintf(" <FONT SIZE=-1><I>");
307                 wprintf(_("(you are the organizer)"));
308                 wprintf("</I></FONT>\n");
309         }
310
311         /*
312          * Transmit the organizer as a hidden field.   We don't want the user
313          * to be able to change it, but we do want it fed back to the server,
314          * especially if this is a new event and there is no organizer already
315          * in the calendar object.
316          */
317         wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
318         escputs(organizer_string);
319         wprintf("\">");
320
321         wprintf("</TD></TR>\n");
322
323         /* Transparency */
324         wprintf("<TR><TD><B>");
325         wprintf(_("Show time as:"));
326         wprintf("</B></TD><TD>");
327
328         p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
329         if (p == NULL) {
330                 /* No transparency found.  Default to opaque (busy). */
331                 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
332                 if (p != NULL) {
333                         icalcomponent_add_property(vevent, p);
334                 }
335         }
336         if (p != NULL) {
337                 v = icalproperty_get_value(p);
338         }
339         else {
340                 v = NULL;
341         }
342
343         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
344         if ((v != NULL) && (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)) {
345                 wprintf(" CHECKED");
346         }
347         wprintf(">");
348         wprintf(_("Free"));
349         wprintf("&nbsp;&nbsp;");
350
351         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
352         if ((v != NULL) && (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)) {
353                 wprintf(" CHECKED");
354         }
355         wprintf(">");
356         wprintf(_("Busy"));
357
358         wprintf("</TD></TR>\n");
359
360
361         /* Done with properties. */
362         wprintf("</TABLE>\n");
363
364         end_tab(0, 3);
365
366         /* Attendees tab (need to move things here) */
367         begin_tab(1, 3);
368         wprintf("<TABLE border=0 width=100%%>\n");      /* same table style as the event tab */
369         wprintf("<TR><TD><B>");
370         wprintf(_("Attendees"));
371         wprintf("</B><br />"
372                 "<font size=-2>");
373         wprintf(_("(One per line)"));
374         wprintf("</font>\n");
375
376         /* Pop open an address book -- begin */
377         wprintf(
378                 "&nbsp;<a href=\"javascript:PopOpenAddressBook('attendees_box|%s');\" "
379                 "title=\"%s\">"
380                 "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
381                 "</a>",
382                 _("Attendees"),
383                 _("Contacts")
384         );
385         /* Pop open an address book -- end */
386
387         wprintf("</TD><TD>"
388                 "<TEXTAREA %s NAME=\"attendees\" id=\"attendees_box\" wrap=soft "
389                 "ROWS=3 COLS=80 WIDTH=80>\n",
390                 (organizer_is_me ? "" : "DISABLED ")
391         );
392         i = 0;
393         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
394             attendee != NULL;
395             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
396                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
397                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
398
399                         /* screen name or email address */
400                         strcpy(attendee_string, &attendee_string[7]);
401                         striplt(attendee_string);
402                         if (i++) wprintf("\n");
403                         escputs(attendee_string);
404                         wprintf(" ");
405
406                         /* participant status */
407                         partstat_as_string(buf, attendee);
408                         escputs(buf);
409                 }
410         }
411         wprintf("</TEXTAREA></TD></TR>\n");
412         wprintf("</TABLE>\n");
413         end_tab(1, 3);
414
415         /* Recurrence tab */
416         begin_tab(2, 3);
417         icalproperty *rrule = NULL;
418         struct icalrecurrencetype recur;
419
420         rrule = icalcomponent_get_first_property(vevent, ICAL_RRULE_PROPERTY);
421         if (rrule) {
422                 recur = icalproperty_get_rrule(rrule);
423         }
424         else {
425                 /* blank recurrence with some sensible defaults */
426                 memset(&recur, 0, sizeof(struct icalrecurrencetype));
427                 recur.count = 3;
428                 recur.until = icaltime_null_time();
429                 recur.interval = 1;
430                 recur.freq = ICAL_WEEKLY_RECURRENCE;
431         }
432
433         wprintf("<INPUT TYPE=\"checkbox\" id=\"is_recur\" NAME=\"is_recur\" "
434                 "VALUE=\"yes\" "
435                 "onclick=\"RecurrenceShowHide();\""
436                 " %s >%s",
437                 (rrule ? "CHECKED=\"CHECKED\"" : "" ),
438                 _("This is a recurring event")
439         );
440
441         wprintf("<div id=\"rrule_div\">\n");            /* begin 'rrule_div' div */
442
443         wprintf("<table border=0 cellspacing=\"10\" width=100%%>\n");
444
445         char *frequency_units[] = {
446                 _("seconds"),
447                 _("minutes"),
448                 _("hours"),
449                 _("days"),
450                 _("weeks"),
451                 _("months"),
452                 _("years"),
453                 _("never")
454         };
455
456         char *ordinals[] = {
457                 "0",
458                 _("first"),
459                 _("second"),
460                 _("third"),
461                 _("fourth"),
462                 _("fifth")
463         };
464
465         wprintf("<tr><td><b>");
466         wprintf(_("Recurrence rule"));
467         wprintf("</b></td><td>");
468
469         if ((recur.freq < 0) || (recur.freq > 6)) recur.freq = 4;
470         wprintf("%s ", _("Repeats every"));
471
472         wprintf("<input type=\"text\" name=\"interval\" maxlength=\"3\" size=\"3\" ");
473         wprintf("value=\"%d\">&nbsp;", recur.interval);
474
475         wprintf("<select name=\"freq\" id=\"freq_selector\" size=\"1\" "
476                 "onChange=\"RecurrenceShowHide();\">\n");
477         for (i=0; i<(sizeof frequency_units / sizeof(char *)); ++i) {
478                 wprintf("<option %s%svalue=\"%d\">%s</option>\n",
479                         ((i == recur.freq) ? "selected " : ""),
480                         (((i == recur.freq) || ((i>=3)&&(i<=6))) ? "" : "disabled "),
481                         i,
482                         frequency_units[i]
483                 );
484         }
485         wprintf("</select>\n");
486
487         wprintf("<div id=\"weekday_selector\">");       /* begin 'weekday_selector' div */
488         wprintf("%s<br>", _("on these weekdays:"));
489
490         char weekday_is_selected[7];
491         memset(weekday_is_selected, 0, 7);
492
493         for (i=0; i<ICAL_BY_DAY_SIZE; ++i) {
494                 if (recur.by_day[i] == ICAL_RECURRENCE_ARRAY_MAX) {
495                         i = ICAL_RECURRENCE_ARRAY_MAX;                  /* all done */
496                 }
497                 else {
498                         for (j=0; j<7; ++j) {
499                                 if (icalrecurrencetype_day_day_of_week(recur.by_day[i]) == j+1) {
500                                         weekday_is_selected[j] = 1;
501                                 }
502                         }
503                 }
504         }
505
506         for (j=0; j<7; ++j) {
507                 i = ((j + (int)weekstart) % 7);
508                 wprintf("<input type=\"checkbox\" name=\"weekday%d\" value=\"yes\"", i);
509                 if (weekday_is_selected[i]) wprintf(" checked");
510                 wprintf(">%s\n", weekday_labels[i]);
511         }
512         wprintf("</div>\n");                            /* end 'weekday_selector' div */
513
514
515
516
517
518         int which_rrmonthtype_is_preselected = 0;
519         wprintf("<div id=\"monthday_selector\">");      /* begin 'monthday_selector' div */
520
521         wprintf("<input type=\"radio\" name=\"rrmonthtype\" id=\"rrmonthtype_mday\" "
522                 "value=\"rrmonthtype_mday\" "
523                 "%s onChange=\"RecurrenceShowHide();\">",
524                 ((which_rrmonthtype_is_preselected == 0) ? "checked" : "")
525         );
526
527         int rrmday = t_start.day;
528         int rrmweekday = icaltime_day_of_week(t_start) - 1;
529
530         /* Figure out what week of the month we're in */
531         icaltimetype day1 = t_start;
532         day1.day = 1;
533         int weekbase = icaltime_week_number(day1);
534         int rrmweek = icaltime_week_number(t_start) - weekbase + 1;
535
536         /* Are we going by day of the month or week/day? */
537
538         if (recur.by_month_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
539                 which_rrmonthtype_is_preselected = 0;
540                 rrmday = recur.by_month_day[0];
541         }
542         else if (recur.by_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
543                 which_rrmonthtype_is_preselected = 1;
544                 rrmweek = icalrecurrencetype_day_position(recur.by_day[0]);
545                 rrmweekday = icalrecurrencetype_day_day_of_week(recur.by_day[0]) - 1;
546         }
547
548         wprintf(_("on day %s%d%s of the month"), "<span id=\"rrmday\">", rrmday, "</span>");
549         wprintf("<br />\n");
550
551         wprintf("<input type=\"radio\" name=\"rrmonthtype\" id=\"rrmonthtype_wday\" "
552                 "value=\"rrmonthtype_wday\" "
553                 "%s onChange=\"RecurrenceShowHide();\">",
554                 ((which_rrmonthtype_is_preselected == 1) ? "checked" : "")
555         );
556
557         wprintf(_("on the "));
558         wprintf("<select name=\"rrmweek\" id=\"rrmweek\" size=\"1\" "
559                 "onChange=\"RecurrenceShowHide();\">\n");
560         for (i=1; i<=5; ++i) {
561                 wprintf("<option %svalue=\"%d\">%s</option>\n",
562                         ((i==rrmweek) ? "selected " : ""),
563                         i,
564                         ordinals[i]
565                 );
566         }
567         wprintf("</select> \n");
568
569         wprintf("<select name=\"rrmweekday\" id=\"rrmweekday\" size=\"1\" "
570                 "onChange=\"RecurrenceShowHide();\">\n");
571         for (j=0; j<7; ++j) {
572                 i = ((j + (int)weekstart) % 7);
573                 wprintf("<option %svalue=\"%d\">%s</option>\n",
574                         ((i==rrmweekday) ? "selected " : ""),
575                         i,
576                         weekday_labels[i]
577                 );
578         }
579         wprintf("</select>");
580
581         wprintf(" %s<br />\n", _("of the month"));
582
583         wprintf("</div>\n");                            /* end 'monthday_selector' div */
584
585
586         int rrymweek = rrmweek;
587         int rrymweekday = rrmweekday;
588         int rrymonth = t_start.month;
589         int which_rryeartype_is_preselected = 0;
590
591         if ( (recur.by_day[0] != ICAL_RECURRENCE_ARRAY_MAX) 
592            && (recur.by_month[0] != ICAL_RECURRENCE_ARRAY_MAX) ) {
593                 which_rryeartype_is_preselected = 1;
594                 rrymweek = icalrecurrencetype_day_position(recur.by_day[0]);
595                 rrymweekday = icalrecurrencetype_day_day_of_week(recur.by_day[0]) - 1;
596                 rrymonth = recur.by_month[0];
597         }
598
599         wprintf("<div id=\"yearday_selector\">");       /* begin 'yearday_selector' div */
600
601         wprintf("<input type=\"radio\" name=\"rryeartype\" id=\"rryeartype_ymday\" "
602                 "value=\"rryeartype_ymday\" "
603                 "%s onChange=\"RecurrenceShowHide();\">",
604                 ((which_rryeartype_is_preselected == 0) ? "checked" : "")
605         );
606         wprintf(_("every "));
607         wprintf("<span id=\"ymday\">%s</span><br />", _("year on this date"));
608
609         wprintf("<input type=\"radio\" name=\"rryeartype\" id=\"rryeartype_ywday\" "
610                 "value=\"rryeartype_ywday\" "
611                 "%s onChange=\"RecurrenceShowHide();\">",
612                 ((which_rryeartype_is_preselected == 1) ? "checked" : "")
613         );
614
615         wprintf(_("on the "));
616         wprintf("<select name=\"rrymweek\" id=\"rrymweek\" size=\"1\" "
617                 "onChange=\"RecurrenceShowHide();\">\n");
618         for (i=1; i<=5; ++i) {
619                 wprintf("<option %svalue=\"%d\">%s</option>\n",
620                         ((i==rrymweek) ? "selected " : ""),
621                         i,
622                         ordinals[i]
623                 );
624         }
625         wprintf("</select> \n");
626
627         wprintf("<select name=\"rrymweekday\" id=\"rrymweekday\" size=\"1\" "
628                 "onChange=\"RecurrenceShowHide();\">\n");
629         for (j=0; j<7; ++j) {
630                 i = ((j + (int)weekstart) % 7);
631                 wprintf("<option %svalue=\"%d\">%s</option>\n",
632                         ((i==rrymweekday) ? "selected " : ""),
633                         i,
634                         weekday_labels[i]
635                 );
636         }
637         wprintf("</select>");
638
639         wprintf(" %s ", _("of"));
640
641         wprintf("<select name=\"rrymonth\" id=\"rrymonth\" size=\"1\" "
642                 "onChange=\"RecurrenceShowHide();\">\n");
643         for (i=1; i<=12; ++i) {
644                 wprintf("<option %svalue=\"%d\">%s</option>\n",
645                         ((i==rrymonth) ? "selected " : ""),
646                         i,
647                         month_labels[i-1]
648                 );
649         }
650         wprintf("</select>");
651         wprintf("<br />\n");
652
653         wprintf("</div>\n");                            /* end 'yearday_selector' div */
654
655         wprintf("</td></tr>\n");
656
657
658         int which_rrend_is_preselected = 0;
659         if (!icaltime_is_null_time(recur.until)) which_rrend_is_preselected = 2;
660         if (recur.count > 0) which_rrend_is_preselected = 1;
661
662         wprintf("<tr><td><b>");
663         wprintf(_("Recurrence range"));
664         wprintf("</b></td><td>\n");
665
666         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_none\" "
667                 "value=\"rrend_none\" "
668                 "%s onChange=\"RecurrenceShowHide();\">",
669                 ((which_rrend_is_preselected == 0) ? "checked" : "")
670         );
671         wprintf("%s<br />\n", _("No ending date"));
672
673         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_count\" "
674                 "value=\"rrend_count\" "
675                 "%s onChange=\"RecurrenceShowHide();\">",
676                 ((which_rrend_is_preselected == 1) ? "checked" : "")
677         );
678         wprintf(_("Repeat this event"));
679         wprintf(" <input type=\"text\" name=\"rrcount\" id=\"rrcount\" maxlength=\"3\" size=\"3\" ");
680         wprintf("value=\"%d\"> ", recur.count);
681         wprintf(_("times"));
682         wprintf("<br />\n");
683
684         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_until\" "
685                 "value=\"rrend_until\" "
686                 "%s onChange=\"RecurrenceShowHide();\">",
687                 ((which_rrend_is_preselected == 2) ? "checked" : "")
688         );
689         wprintf(_("Repeat this event until "));
690
691         if (icaltime_is_null_time(recur.until)) {
692                 recur.until = icaltime_add(t_start, icaldurationtype_from_int(604800));
693         }
694         display_icaltimetype_as_webform(&recur.until, "rruntil", 1);
695         wprintf("<br />\n");
696
697         wprintf("</td></tr>\n");
698
699         wprintf("</table>\n");
700         wprintf("</div>\n");                            /* end 'rrule' div */
701
702         end_tab(2, 3);
703
704         /* submit buttons (common area beneath the tabs) */
705         begin_tab(3, 3);
706         wprintf("<CENTER>"
707                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
708                 "&nbsp;&nbsp;"
709                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
710                 "&nbsp;&nbsp;"
711                 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
712                                 "VALUE=\"%s\">\n"
713                 "&nbsp;&nbsp;"
714                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
715                 "</CENTER>\n",
716                 _("Save"),
717                 _("Delete"),
718                 _("Check attendee availability"),
719                 _("Cancel")
720         );
721         wprintf("</FORM>\n");
722         end_tab(3, 3);
723
724         wprintf("</div>\n");                    /* end 'fix_scrollbar_bug' div */
725
726         StrBufAppendPrintf(WC->trailing_javascript,
727                 "eventEditAllDay();     \n"
728                 "RecurrenceShowHide();  \n"
729         );
730         address_book_popup();
731         wDumpContent(1);
732
733         if (created_new_vevent) {
734                 icalcomponent_free(vevent);
735         }
736 }
737
738 /*
739  * Save an edited event
740  *
741  * supplied_vevent:     the event to save
742  * msgnum:              the index on the citserver
743  */
744 void save_individual_event(icalcomponent *supplied_vevent, long msgnum, char *from,
745                         int unread, struct calview *calv) {
746         char buf[SIZ];
747         icalproperty *prop;
748         icalcomponent *vevent, *encaps;
749         int created_new_vevent = 0;
750         int all_day_event = 0;
751         struct icaltimetype event_start, t;
752         icalproperty *attendee = NULL;
753         char attendee_string[SIZ];
754         int i, j;
755         int foundit;
756         char form_attendees[SIZ];
757         char organizer_string[SIZ];
758         int sequence = 0;
759         enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
760
761         if (supplied_vevent != NULL) {
762                 vevent = supplied_vevent;
763
764                 /* Convert all timestamps to UTC to make them easier to process. */
765                 ical_dezonify(vevent);
766
767                 /*
768                  * If we're looking at a fully encapsulated VCALENDAR
769                  * rather than a VEVENT component, attempt to use the first
770                  * relevant VEVENT subcomponent.  If there is none, the
771                  * NULL returned by icalcomponent_get_first_component() will
772                  * tell the next iteration of this function to create a
773                  * new one.
774                  */
775                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
776                         save_individual_event(
777                                 icalcomponent_get_first_component(
778                                         vevent, ICAL_VEVENT_COMPONENT), 
779                                 msgnum, from, unread, NULL
780                         );
781                         return;
782                 }
783         }
784         else {
785                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
786                 created_new_vevent = 1;
787         }
788
789         if ( (havebstr("save_button"))
790            || (havebstr("check_button")) ) {
791
792                 /* Replace values in the component with ones from the form */
793
794                 while (prop = icalcomponent_get_first_property(vevent,
795                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
796                         icalcomponent_remove_property(vevent, prop);
797                         icalproperty_free(prop);
798                 }
799
800                 if (havebstr("summary")) {
801         
802                         icalcomponent_add_property(vevent,
803                                         icalproperty_new_summary(bstr("summary")));
804                 } else {
805                         icalcomponent_add_property(vevent,
806                                         icalproperty_new_summary("Untitled Event"));
807                 }
808         
809                 while (prop = icalcomponent_get_first_property(vevent,
810                                         ICAL_LOCATION_PROPERTY), prop != NULL) {
811                         icalcomponent_remove_property(vevent, prop);
812                         icalproperty_free(prop);
813                 }
814                 if (havebstr("location")) {
815                         icalcomponent_add_property(vevent,
816                                         icalproperty_new_location(bstr("location")));
817                 }
818                 while (prop = icalcomponent_get_first_property(vevent,
819                                   ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
820                         icalcomponent_remove_property(vevent, prop);
821                         icalproperty_free(prop);
822                 }
823                 if (havebstr("description")) {
824                         icalcomponent_add_property(vevent,
825                                 icalproperty_new_description(bstr("description")));
826                 }
827
828                 while (prop = icalcomponent_get_first_property(vevent,
829                       ICAL_DTSTART_PROPERTY), prop != NULL) {
830                         icalcomponent_remove_property(vevent, prop);
831                         icalproperty_free(prop);
832                 }
833
834                 if (yesbstr("alldayevent")) {
835                         all_day_event = 1;
836                 }
837                 else {
838                         all_day_event = 0;
839                 }
840
841                 if (all_day_event) {
842                         icaltime_from_webform_dateonly(&event_start, "dtstart");
843                 }
844                 else {
845                         icaltime_from_webform(&event_start, "dtstart");
846                 }
847
848                 prop = icalproperty_new_dtstart(event_start);
849
850                 if (all_day_event) {
851                         /* Force it to serialize as a date-only rather than date/time */
852                         icalproperty_set_value(prop, icalvalue_new_date(event_start));
853                 }
854
855                 if (prop) icalcomponent_add_property(vevent, prop);
856                 else icalproperty_free(prop);
857
858                 while (prop = icalcomponent_get_first_property(vevent,
859                       ICAL_DTEND_PROPERTY), prop != NULL) {
860                         icalcomponent_remove_property(vevent, prop);
861                         icalproperty_free(prop);
862                 }
863                 while (prop = icalcomponent_get_first_property(vevent,
864                       ICAL_DURATION_PROPERTY), prop != NULL) {
865                         icalcomponent_remove_property(vevent, prop);
866                         icalproperty_free(prop);
867                 }
868
869                 if (all_day_event == 0) {
870                         icaltime_from_webform(&t, "dtend");     
871                         icalcomponent_add_property(vevent,
872                                 icalproperty_new_dtend(icaltime_normalize(t)
873                                 )
874                         );
875                 }
876
877                 /* recurrence rules -- begin */
878
879                 /* remove any existing rule */
880                 while (prop = icalcomponent_get_first_property(vevent, ICAL_RRULE_PROPERTY), prop != NULL) {
881                         icalcomponent_remove_property(vevent, prop);
882                         icalproperty_free(prop);
883                 }
884
885                 if (yesbstr("is_recur")) {
886                         struct icalrecurrencetype recur;
887                         icalrecurrencetype_clear(&recur);
888
889                         recur.interval = atoi(bstr("interval"));
890                         recur.freq = atoi(bstr("freq"));
891
892                         switch(recur.freq) {
893
894                                 /* These can't happen; they're disabled. */
895                                 case ICAL_SECONDLY_RECURRENCE:
896                                         break;
897                                 case ICAL_MINUTELY_RECURRENCE:
898                                         break;
899                                 case ICAL_HOURLY_RECURRENCE:
900                                         break;
901
902                                 /* Daily is valid but there are no further inputs. */
903                                 case ICAL_DAILY_RECURRENCE:
904                                         break;
905
906                                 /* These are the real options. */
907
908                                 case ICAL_WEEKLY_RECURRENCE:
909                                         j=0;
910                                         for (i=0; i<7; ++i) {
911                                                 snprintf(buf, sizeof buf, "weekday%d", i);
912                                                 if (YESBSTR(buf)) recur.by_day[j++] =
913                                                         icalrecurrencetype_day_day_of_week(i+1);
914                                         }
915                                         recur.by_day[j++] = ICAL_RECURRENCE_ARRAY_MAX;
916                                         break;
917
918                                 case ICAL_MONTHLY_RECURRENCE:
919                                         if (!strcasecmp(bstr("rrmonthtype"), "rrmonthtype_mday")) {
920                                                 recur.by_month_day[0] = event_start.day;
921                                                 recur.by_month_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
922                                         }
923                                         else if (!strcasecmp(bstr("rrmonthtype"), "rrmonthtype_wday")) {
924                                                 recur.by_day[0] = (atoi(bstr("rrmweek")) * 8)
925                                                                 + atoi(bstr("rrmweekday")) + 1;
926                                                 recur.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
927                                         }
928                                         break;
929
930                                 case ICAL_YEARLY_RECURRENCE:
931                                         if (!strcasecmp(bstr("rryeartype"), "rryeartype_ymday")) {
932                                                 /* no further action is needed here */
933                                         }
934                                         else if (!strcasecmp(bstr("rryeartype"), "rryeartype_ywday")) {
935                                                 recur.by_month[0] = atoi(bstr("rrymonth"));
936                                                 recur.by_month[1] = ICAL_RECURRENCE_ARRAY_MAX;
937                                                 recur.by_day[0] = (atoi(bstr("rrymweek")) * 8)
938                                                                 + atoi(bstr("rrymweekday")) + 1;
939                                                 recur.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
940                                         }
941                                         break;
942
943                                 /* This one can't happen either. */
944                                 case ICAL_NO_RECURRENCE:
945                                         break;
946                         }
947
948                         if (!strcasecmp(bstr("rrend"), "rrend_count")) {
949                                 recur.count = atoi(bstr("rrcount"));
950                         }
951                         else if (!strcasecmp(bstr("rrend"), "rrend_until")) {
952                                 icaltime_from_webform_dateonly(&recur.until, "rruntil");
953                         }
954
955                         icalcomponent_add_property(vevent, icalproperty_new_rrule(recur));
956                 }
957
958                 /* recurrence rules -- end */
959
960                 /* See if transparency is indicated */
961                 if (havebstr("transp")) {
962                         if (!strcasecmp(bstr("transp"), "opaque")) {
963                                 formtransp = ICAL_TRANSP_OPAQUE;
964                         }
965                         else if (!strcasecmp(bstr("transp"), "transparent")) {
966                                 formtransp = ICAL_TRANSP_TRANSPARENT;
967                         }
968
969                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
970                               (prop != NULL)) {
971                                 icalcomponent_remove_property(vevent, prop);
972                                 icalproperty_free(prop);
973                         }
974
975                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
976                 }
977
978                 /* Give this event a UID if it doesn't have one. */
979                 if (icalcomponent_get_first_property(vevent,
980                    ICAL_UID_PROPERTY) == NULL) {
981                         generate_uuid(buf);
982                         icalcomponent_add_property(vevent, icalproperty_new_uid(buf));
983                 }
984
985                 /* Increment the sequence ID */
986                 while (prop = icalcomponent_get_first_property(vevent,
987                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
988                         i = icalproperty_get_sequence(prop);
989                         if (i > sequence) sequence = i;
990                         icalcomponent_remove_property(vevent, prop);
991                         icalproperty_free(prop);
992                 }
993                 ++sequence;
994                 icalcomponent_add_property(vevent,
995                         icalproperty_new_sequence(sequence)
996                 );
997                 
998                 /*
999                  * Set the organizer, only if one does not already exist *and*
1000                  * the form is supplying one
1001                  */
1002                 strcpy(buf, bstr("organizer"));
1003                 if ( (icalcomponent_get_first_property(vevent,
1004                    ICAL_ORGANIZER_PROPERTY) == NULL) 
1005                    && (!IsEmptyStr(buf)) ) {
1006
1007                         /* set new organizer */
1008                         sprintf(organizer_string, "MAILTO:%s", buf);
1009                         icalcomponent_add_property(vevent,
1010                                 icalproperty_new_organizer(organizer_string)
1011                         );
1012
1013                 }
1014
1015                 /*
1016                  * Add any new attendees listed in the web form
1017                  */
1018
1019                 /* First, strip out the parenthesized partstats.  */
1020                 strcpy(form_attendees, bstr("attendees"));
1021                 do {
1022                         i = strlen(form_attendees);
1023                         stripout(form_attendees, '(', ')');
1024                         j = strlen(form_attendees);
1025                 } while (i != j);
1026
1027                 /* Next, change any commas to newlines, because we want newline-separated attendees. */
1028                 j = strlen(form_attendees);
1029                 for (i=0; i<j; ++i) {
1030                         if (form_attendees[i] == ',') {
1031                                 form_attendees[i] = '\n';
1032                                 while (isspace(form_attendees[i+1])) {
1033                                         strcpy(&form_attendees[i+1], &form_attendees[i+2]);
1034                                 }
1035                         }
1036                 }
1037
1038                 /* Now iterate! */
1039                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
1040                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
1041                         striplt(buf);
1042                         if (!IsEmptyStr(buf)) {
1043                                 sprintf(attendee_string, "MAILTO:%s", buf);
1044                                 foundit = 0;
1045
1046                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
1047                                         if (!strcasecmp(attendee_string,
1048                                            icalproperty_get_attendee(attendee)))
1049                                                 ++foundit;
1050                                 }
1051
1052
1053                                 if (foundit == 0) {
1054                                         icalcomponent_add_property(vevent,
1055                                                 icalproperty_new_attendee(attendee_string)
1056                                         );
1057                                 }
1058                         }
1059                 }
1060
1061                 /*
1062                  * Remove any attendees *not* listed in the web form
1063                  */
1064 STARTOVER:      for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
1065                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
1066                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
1067                                 strcpy(attendee_string, &attendee_string[7]);
1068                                 striplt(attendee_string);
1069                                 foundit = 0;
1070                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
1071                                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
1072                                         striplt(buf);
1073                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
1074                                 }
1075                                 if (foundit == 0) {
1076                                         icalcomponent_remove_property(vevent, attendee);
1077                                         icalproperty_free(attendee);
1078                                         goto STARTOVER;
1079                                 }
1080                         }
1081                 }
1082
1083                 /*
1084                  * Encapsulate event into full VCALENDAR component.  Clone it first,
1085                  * for two reasons: one, it's easier to just free the whole thing
1086                  * when we're done instead of unbundling, but more importantly, we
1087                  * can't encapsulate something that may already be encapsulated
1088                  * somewhere else.
1089                  */
1090                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
1091
1092                 /* Set the method to PUBLISH */
1093                 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1094
1095                 /* If the user clicked 'Save' then save it to the server. */
1096                 if ( (encaps != NULL) && (havebstr("save_button")) ) {
1097                         serv_puts("ENT0 1|||4|||1|");
1098                         serv_getln(buf, sizeof buf);
1099                         if (buf[0] == '8') {
1100                                 serv_puts("Content-type: text/calendar");
1101                                 serv_puts("");
1102                                 serv_puts(icalcomponent_as_ical_string(encaps));
1103                                 serv_puts("000");
1104                         }
1105                         if ( (buf[0] == '8') || (buf[0] == '4') ) {
1106                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1107                                 }
1108                         }
1109                         if (buf[0] == '2') {
1110                                 strcpy(WC->ImportantMessage, &buf[4]);
1111                         }
1112                         icalmemory_free_ring ();
1113                         icalcomponent_free(encaps);
1114                         encaps = NULL;
1115                 }
1116
1117                 /* Or, check attendee availability if the user asked for that. */
1118                 if ( (encaps != NULL) && (havebstr("check_button")) ) {
1119
1120                         /* Call this function, which does the real work */
1121                         check_attendee_availability(encaps);
1122
1123                         /* This displays the form again, with our annotations */
1124                         display_edit_individual_event(encaps, msgnum, from, unread, NULL);
1125
1126                         icalcomponent_free(encaps);
1127                         encaps = NULL;
1128                 }
1129                 if (encaps != NULL) {
1130                         icalcomponent_free(encaps);
1131                         encaps = NULL;
1132                 }
1133
1134         }
1135
1136         /*
1137          * If the user clicked 'Delete' then delete it.
1138          */
1139         if ( (havebstr("delete_button")) && (msgnum > 0L) ) {
1140                 serv_printf("DELE %ld", lbstr("msgnum"));
1141                 serv_getln(buf, sizeof buf);
1142         }
1143
1144         if (created_new_vevent) {
1145                 icalcomponent_free(vevent);
1146         }
1147
1148         /* If this was a save or delete, go back to the calendar view. */
1149         if (!havebstr("check_button")) {
1150                 readloop(readfwd);
1151         }
1152 }