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