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