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