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