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