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