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