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