fix possible crashes of not NULL-checking the result of icalproperty_get_attendee()
[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         const char *ch;
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         syslog(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                 ch = icalproperty_get_attendee(attendee);
467                 if ((ch != NULL) && !strncasecmp(ch, "mailto:", 7)) {
468
469                         /* screen name or email address */
470                         safestrncpy(attendee_string, ch + 7, sizeof(attendee_string));
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         const char *ch;
809
810         if (supplied_vevent != NULL) {
811                 vevent = supplied_vevent;
812
813                 /* Convert all timestamps to UTC to make them easier to process. */
814                 ical_dezonify(vevent);
815
816                 /*
817                  * If we're looking at a fully encapsulated VCALENDAR
818                  * rather than a VEVENT component, attempt to use the first
819                  * relevant VEVENT subcomponent.  If there is none, the
820                  * NULL returned by icalcomponent_get_first_component() will
821                  * tell the next iteration of this function to create a
822                  * new one.
823                  */
824                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
825                         save_individual_event(
826                                 icalcomponent_get_first_component(
827                                         vevent, ICAL_VEVENT_COMPONENT),
828                                 msgnum, from, unread, NULL
829                         );
830                         return;
831                 }
832         }
833         else {
834                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
835                 created_new_vevent = 1;
836         }
837
838         if ( (havebstr("save_button"))
839            || (havebstr("check_button")) ) {
840
841                 /* Replace values in the component with ones from the form */
842
843                 while (prop = icalcomponent_get_first_property(vevent,
844                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
845                         icalcomponent_remove_property(vevent, prop);
846                         icalproperty_free(prop);
847                 }
848
849                 /* Add NOW() to the calendar object... */
850                 icalcomponent_set_dtstamp(vevent,
851                                           icaltime_from_timet(
852                                                   time(NULL), 0));
853
854                 if (havebstr("summary")) {
855                         icalcomponent_add_property(vevent,
856                                         icalproperty_new_summary(bstr("summary")));
857                 } else {
858                         icalcomponent_add_property(vevent,
859                                         icalproperty_new_summary(_("Untitled Event")));
860                 }
861
862                 while (prop = icalcomponent_get_first_property(vevent,
863                                         ICAL_LOCATION_PROPERTY), prop != NULL) {
864                         icalcomponent_remove_property(vevent, prop);
865                         icalproperty_free(prop);
866                 }
867                 if (havebstr("location")) {
868                         icalcomponent_add_property(vevent,
869                                         icalproperty_new_location(bstr("location")));
870                 }
871                 while (prop = icalcomponent_get_first_property(vevent,
872                                   ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
873                         icalcomponent_remove_property(vevent, prop);
874                         icalproperty_free(prop);
875                 }
876                 if (havebstr("description")) {
877                         icalcomponent_add_property(vevent,
878                                 icalproperty_new_description(bstr("description")));
879                 }
880
881                 while (prop = icalcomponent_get_first_property(vevent,
882                       ICAL_DTSTART_PROPERTY), prop != NULL) {
883                         icalcomponent_remove_property(vevent, prop);
884                         icalproperty_free(prop);
885                 }
886
887                 if (yesbstr("alldayevent")) {
888                         all_day_event = 1;
889                 }
890                 else {
891                         all_day_event = 0;
892                 }
893
894                 if (all_day_event) {
895                         icaltime_from_webform_dateonly(&event_start, "dtstart");
896                 }
897                 else {
898                         icaltime_from_webform(&event_start, "dtstart");
899                 }
900
901                 prop = icalproperty_new_dtstart(event_start);
902
903                 if (all_day_event) {
904                         /* Force it to serialize as a date-only rather than date/time */
905                         icalproperty_set_value(prop, icalvalue_new_date(event_start));
906                 }
907
908                 if (prop) icalcomponent_add_property(vevent, prop);
909                 else icalproperty_free(prop);
910
911                 while (prop = icalcomponent_get_first_property(vevent,
912                       ICAL_DTEND_PROPERTY), prop != NULL) {
913                         icalcomponent_remove_property(vevent, prop);
914                         icalproperty_free(prop);
915                 }
916                 while (prop = icalcomponent_get_first_property(vevent,
917                       ICAL_DURATION_PROPERTY), prop != NULL) {
918                         icalcomponent_remove_property(vevent, prop);
919                         icalproperty_free(prop);
920                 }
921
922                 if (all_day_event) {
923                         icaltime_from_webform_dateonly(&t, "dtend");
924
925                         /* with this field supposed to be non-inclusive we have to add one day */
926                         icaltime_adjust(&t, 1, 0, 0, 0);
927                 }
928                 else {
929                         icaltime_from_webform(&t, "dtend");
930                 }
931
932                 icalcomponent_add_property(vevent,
933                         icalproperty_new_dtend(icaltime_normalize(t)
934                         )
935                 );
936
937                 /* recurrence rules -- begin */
938
939                 /* remove any existing rule */
940                 while (prop = icalcomponent_get_first_property(vevent, ICAL_RRULE_PROPERTY), prop != NULL) {
941                         icalcomponent_remove_property(vevent, prop);
942                         icalproperty_free(prop);
943                 }
944
945                 if (yesbstr("is_recur")) {
946                         struct icalrecurrencetype recur;
947                         icalrecurrencetype_clear(&recur);
948
949                         recur.interval = atoi(bstr("interval"));
950                         recur.freq = atoi(bstr("freq"));
951
952                         switch(recur.freq) {
953
954                                 /* These can't happen; they're disabled. */
955                                 case ICAL_SECONDLY_RECURRENCE:
956                                         break;
957                                 case ICAL_MINUTELY_RECURRENCE:
958                                         break;
959                                 case ICAL_HOURLY_RECURRENCE:
960                                         break;
961
962                                 /* Daily is valid but there are no further inputs. */
963                                 case ICAL_DAILY_RECURRENCE:
964                                         break;
965
966                                 /* These are the real options. */
967
968                                 case ICAL_WEEKLY_RECURRENCE:
969                                         j=0;
970                                         for (i=0; i<7; ++i) {
971                                                 snprintf(buf, sizeof buf, "weekday%d", i);
972                                                 if (YESBSTR(buf)) recur.by_day[j++] =
973                                                         icalrecurrencetype_day_day_of_week(i+1);
974                                         }
975                                         recur.by_day[j++] = ICAL_RECURRENCE_ARRAY_MAX;
976                                         break;
977
978                                 case ICAL_MONTHLY_RECURRENCE:
979                                         if (!strcasecmp(bstr("rrmonthtype"), "rrmonthtype_mday")) {
980                                                 recur.by_month_day[0] = event_start.day;
981                                                 recur.by_month_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
982                                         }
983                                         else if (!strcasecmp(bstr("rrmonthtype"), "rrmonthtype_wday")) {
984                                                 recur.by_day[0] = (atoi(bstr("rrmweek")) * 8)
985                                                                 + atoi(bstr("rrmweekday")) + 1;
986                                                 recur.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
987                                         }
988                                         break;
989
990                                 case ICAL_YEARLY_RECURRENCE:
991                                         if (!strcasecmp(bstr("rryeartype"), "rryeartype_ymday")) {
992                                                 /* no further action is needed here */
993                                         }
994                                         else if (!strcasecmp(bstr("rryeartype"), "rryeartype_ywday")) {
995                                                 recur.by_month[0] = atoi(bstr("rrymonth"));
996                                                 recur.by_month[1] = ICAL_RECURRENCE_ARRAY_MAX;
997                                                 recur.by_day[0] = (atoi(bstr("rrymweek")) * 8)
998                                                                 + atoi(bstr("rrymweekday")) + 1;
999                                                 recur.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
1000                                         }
1001                                         break;
1002
1003                                 /* This one can't happen either. */
1004                                 case ICAL_NO_RECURRENCE:
1005                                         break;
1006                         }
1007
1008                         if (!strcasecmp(bstr("rrend"), "rrend_count")) {
1009                                 recur.count = atoi(bstr("rrcount"));
1010                         }
1011                         else if (!strcasecmp(bstr("rrend"), "rrend_until")) {
1012                                 icaltime_from_webform_dateonly(&recur.until, "rruntil");
1013                         }
1014
1015                         icalcomponent_add_property(vevent, icalproperty_new_rrule(recur));
1016                 }
1017
1018                 /* recurrence rules -- end */
1019
1020                 /* See if transparency is indicated */
1021                 if (havebstr("transp")) {
1022                         if (!strcasecmp(bstr("transp"), "opaque")) {
1023                                 formtransp = ICAL_TRANSP_OPAQUE;
1024                         }
1025                         else if (!strcasecmp(bstr("transp"), "transparent")) {
1026                                 formtransp = ICAL_TRANSP_TRANSPARENT;
1027                         }
1028
1029                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
1030                               (prop != NULL)) {
1031                                 icalcomponent_remove_property(vevent, prop);
1032                                 icalproperty_free(prop);
1033                         }
1034
1035                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
1036                 }
1037
1038                 /* Give this event a UID if it doesn't have one. */
1039                 if (icalcomponent_get_first_property(vevent,
1040                    ICAL_UID_PROPERTY) == NULL) {
1041                         generate_uuid(buf);
1042                         icalcomponent_add_property(vevent, icalproperty_new_uid(buf));
1043                 }
1044
1045                 /* Increment the sequence ID */
1046                 while (prop = icalcomponent_get_first_property(vevent,
1047                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
1048                         i = icalproperty_get_sequence(prop);
1049                         if (i > sequence) sequence = i;
1050                         icalcomponent_remove_property(vevent, prop);
1051                         icalproperty_free(prop);
1052                 }
1053                 ++sequence;
1054                 icalcomponent_add_property(vevent,
1055                         icalproperty_new_sequence(sequence)
1056                 );
1057
1058                 /*
1059                  * Set the organizer, only if one does not already exist *and*
1060                  * the form is supplying one
1061                  */
1062                 strcpy(buf, bstr("organizer"));
1063                 if ( (icalcomponent_get_first_property(vevent,
1064                    ICAL_ORGANIZER_PROPERTY) == NULL)
1065                    && (!IsEmptyStr(buf)) ) {
1066
1067                         /* set new organizer */
1068                         sprintf(organizer_string, "MAILTO:%s", buf);
1069                         icalcomponent_add_property(vevent,
1070                                 icalproperty_new_organizer(organizer_string)
1071                         );
1072
1073                 }
1074
1075                 /*
1076                  * Add any new attendees listed in the web form
1077                  */
1078
1079                 /* First, strip out the parenthesized partstats.  */
1080                 strcpy(form_attendees, bstr("attendees"));
1081                 while ( stripout(form_attendees, '(', ')') != 0);
1082
1083                 /* Next, change any commas to newlines, because we want newline-separated attendees. */
1084                 j = strlen(form_attendees);
1085                 for (i=0; i<j; ++i) {
1086                         if (form_attendees[i] == ',') {
1087                                 form_attendees[i] = '\n';
1088                                 while (isspace(form_attendees[i+1])) {
1089                                         strcpy(&form_attendees[i+1], &form_attendees[i+2]);
1090                                 }
1091                         }
1092                 }
1093
1094                 /* Now iterate! */
1095                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
1096                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
1097                         striplt(buf);
1098                         if (!IsEmptyStr(buf)) {
1099                                 sprintf(attendee_string, "MAILTO:%s", buf);
1100                                 foundit = 0;
1101
1102                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
1103                                         ch = icalproperty_get_attendee(attendee);
1104                                         if ((ch != NULL) && !strcasecmp(attendee_string, ch))
1105                                                 ++foundit;
1106                                 }
1107
1108
1109                                 if (foundit == 0) {
1110                                         icalcomponent_add_property(vevent,
1111                                                                    icalproperty_new_attendee(attendee_string)
1112                                         );
1113                                 }
1114                         }
1115                 }
1116
1117                 /*
1118                  * Remove any attendees *not* listed in the web form
1119                  */
1120 STARTOVER:      for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
1121                         ch = icalproperty_get_attendee(attendee);
1122                         if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
1123                                 safestrncpy(attendee_string, ch + 7, sizeof(attendee_string));
1124                                 striplt(attendee_string);
1125                                 foundit = 0;
1126                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
1127                                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
1128                                         striplt(buf);
1129                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
1130                                 }
1131                                 if (foundit == 0) {
1132                                         icalcomponent_remove_property(vevent, attendee);
1133                                         icalproperty_free(attendee);
1134                                         goto STARTOVER;
1135                                 }
1136                         }
1137                 }
1138
1139                 /*
1140                  * Encapsulate event into full VCALENDAR component.  Clone it first,
1141                  * for two reasons: one, it's easier to just free the whole thing
1142                  * when we're done instead of unbundling, but more importantly, we
1143                  * can't encapsulate something that may already be encapsulated
1144                  * somewhere else.
1145                  */
1146                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
1147
1148                 /* Set the method to PUBLISH */
1149                 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1150
1151                 /* If the user clicked 'Save' then save it to the server. */
1152                 if ( (encaps != NULL) && (havebstr("save_button")) ) {
1153                         serv_puts("ENT0 1|||4|||1|");
1154                         serv_getln(buf, sizeof buf);
1155                         if (buf[0] == '8') {
1156                                 serv_puts("Content-type: text/calendar");
1157                                 serv_puts("Content-Transfer-Encoding: quoted-printable");
1158                                 serv_puts("");
1159                                 text_to_server_qp(icalcomponent_as_ical_string(encaps));
1160 //                              serv_puts(icalcomponent_as_ical_string(encaps));
1161                                 serv_puts("000");
1162                         }
1163                         if ( (buf[0] == '8') || (buf[0] == '4') ) {
1164                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1165                                 }
1166                         }
1167                         if (buf[0] == '2') {
1168                                 strcpy(WC->ImportantMessage, &buf[4]);
1169                         }
1170                         icalmemory_free_ring ();
1171                         icalcomponent_free(encaps);
1172                         encaps = NULL;
1173                 }
1174
1175                 /* Or, check attendee availability if the user asked for that. */
1176                 if ( (encaps != NULL) && (havebstr("check_button")) ) {
1177
1178                         /* Call this function, which does the real work */
1179                         check_attendee_availability(encaps);
1180
1181                         /* This displays the form again, with our annotations */
1182                         display_edit_individual_event(encaps, msgnum, from, unread, NULL);
1183
1184                         icalcomponent_free(encaps);
1185                         encaps = NULL;
1186                 }
1187                 if (encaps != NULL) {
1188                         icalcomponent_free(encaps);
1189                         encaps = NULL;
1190                 }
1191
1192         }
1193
1194         /*
1195          * If the user clicked 'Delete' then delete it.
1196          */
1197         if ( (havebstr("delete_button")) && (msgnum > 0L) ) {
1198                 serv_printf("DELE %ld", lbstr("msgnum"));
1199                 serv_getln(buf, sizeof buf);
1200         }
1201
1202         if (created_new_vevent) {
1203                 icalcomponent_free(vevent);
1204         }
1205
1206         /* If this was a save or delete, go back to the calendar or summary view. */
1207         if (!havebstr("check_button")) {
1208                 if (!strcasecmp(bstr("calview"), "summary")) {
1209                         summary();
1210                 }
1211                 else {
1212                         readloop(readfwd, eUseDefault);
1213                 }
1214         }
1215 }