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