be58c3bb15c406acded70004f751f4a4965792eb
[citadel.git] / webcit / event.c
1 /*
2  * $Id$
3  *
4  * Editing calendar events.
5  *
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <time.h>
27 #include "webcit.h"
28 #include "webserver.h"
29
30
31 #ifdef HAVE_ICAL_H
32
33 /*
34  * Display an event by itself (for editing)
35  */
36 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
37         icalcomponent *vevent;
38         icalproperty *p;
39         struct icaltimetype t_start, t_end;
40         time_t now;
41         int created_new_vevent = 0;
42         icalproperty *organizer = NULL;
43         char organizer_string[SIZ];
44         icalproperty *attendee = NULL;
45         char attendee_string[SIZ];
46         char buf[SIZ];
47         int i;
48         int organizer_is_me = 0;
49
50         now = time(NULL);
51         strcpy(organizer_string, "");
52         strcpy(attendee_string, "");
53
54         if (supplied_vevent != NULL) {
55                 vevent = supplied_vevent;
56         }
57         else {
58                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
59                 created_new_vevent = 1;
60         }
61
62         output_headers(3);
63         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
64                 "<IMG ALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
65                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
66                 "<B>Edit event</B>"
67                 "</FONT></TD></TR></TABLE><BR>\n"
68         );
69
70         /************************************************************
71          * Uncomment this to see the UID in calendar events for debugging
72         wprintf("UID == ");
73         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
74         if (p != NULL) {
75                 escputs((char *)icalproperty_get_comment(p));
76         }
77         *************************************************************/
78
79         wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
80
81         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
82                 msgnum);
83         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
84                 bstr("calview"));
85         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
86                 bstr("year"));
87         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
88                 bstr("month"));
89         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
90                 bstr("day"));
91
92         /* Put it in a borderless table so it lines up nicely */
93         wprintf("<TABLE border=0 width=100%%>\n");
94
95         wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
96                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
97                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
98         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
99         if (p != NULL) {
100                 escputs((char *)icalproperty_get_comment(p));
101         }
102         wprintf("\"></TD></TR>\n");
103
104         wprintf("<TR><TD><B>Location</B></TD><TD>\n"
105                 "<INPUT TYPE=\"text\" NAME=\"location\" "
106                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
107         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
108         if (p != NULL) {
109                 escputs((char *)icalproperty_get_comment(p));
110         }
111         wprintf("\"></TD></TR>\n");
112
113         wprintf("<TR><TD><B>Start</B></TD><TD>\n");
114         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
115         if (p != NULL) {
116                 t_start = icalproperty_get_dtstart(p);
117                 if (t_start.is_date) {
118                         t_start.hour = 0;
119                         t_start.minute = 0;
120                         t_start.second = 0;
121                 }
122         }
123         else {
124                 memset(&t_start, 0, sizeof t_start);
125                 t_start.year = atoi(bstr("year"));
126                 t_start.month = atoi(bstr("month"));
127                 t_start.day = atoi(bstr("day"));
128                 if (strlen(bstr("hour")) > 0) {
129                         t_start.hour = atoi(bstr("hour"));
130                         t_start.minute = atoi(bstr("minute"));
131                 }
132                 else {
133                         t_start.hour = 9;
134                         t_start.minute = 0;
135                 }
136                 /* t_start = icaltime_from_timet(now, 0); */
137         }
138         display_icaltimetype_as_webform(&t_start, "dtstart");
139
140         wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
141                 "VALUE=\"yes\" onClick=\"
142
143                         if (this.checked) {
144                                 this.form.dtstart_hour.value='0';
145                                 this.form.dtstart_hour.disabled = true;
146                                 this.form.dtstart_minute.value='0';
147                                 this.form.dtstart_minute.disabled = true;
148                                 this.form.dtend_hour.value='0';
149                                 this.form.dtend_hour.disabled = true;
150                                 this.form.dtend_minute.value='0';
151                                 this.form.dtend_minute.disabled = true;
152                                 this.form.dtend_month.disabled = true;
153                                 this.form.dtend_day.disabled = true;
154                                 this.form.dtend_year.disabled = true;
155                         }
156                         else {
157                                 this.form.dtstart_hour.disabled = false;
158                                 this.form.dtstart_minute.disabled = false;
159                                 this.form.dtend_hour.disabled = false;
160                                 this.form.dtend_minute.disabled = false;
161                                 this.form.dtend_month.disabled = false;
162                                 this.form.dtend_day.disabled = false;
163                                 this.form.dtend_year.disabled = false;
164                         }
165
166
167                 \" %s >All day event",
168                 (t_start.is_date ? "CHECKED" : "" )
169         );
170
171         wprintf("</TD></TR>\n");
172
173         /* If this is an all-day-event, set the end time to be identical to
174          * the start time (the hour/minute/second will be set to midnight).
175          * Otherwise extract or create it.
176          */
177         wprintf("<TR><TD><B>End</B></TD><TD>\n");
178         if (t_start.is_date) {
179                 t_end = t_start;
180         }
181         else {
182                 p = icalcomponent_get_first_property(vevent,
183                                                         ICAL_DTEND_PROPERTY);
184                 if (p != NULL) {
185                         t_end = icalproperty_get_dtend(p);
186                 }
187                 else {
188                         /* If this is not an all-day event and there is no
189                          * end time specified, make the default one hour
190                          * from the start time.
191                          */
192                         t_end = t_start;
193                         t_end.hour += 1;
194                         t_end = icaltime_normalize(t_end);
195                         /* t_end = icaltime_from_timet(now, 0); */
196                 }
197         }
198         display_icaltimetype_as_webform(&t_end, "dtend");
199         wprintf("</TD></TR>\n");
200
201         wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
202                 "<TEXTAREA NAME=\"description\" wrap=soft "
203                 "ROWS=10 COLS=80 WIDTH=80>\n"
204         );
205         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
206         if (p != NULL) {
207                 escputs((char *)icalproperty_get_comment(p));
208         }
209         wprintf("</TEXTAREA></TD></TR>");
210
211         /* For a new event, the user creating the event should be the
212          * organizer.  Set this field accordingly.
213          */
214         if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
215            == NULL) {
216                 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
217                 icalcomponent_add_property(vevent,
218                         icalproperty_new_organizer(organizer_string)
219                 );
220         }
221
222         /* Determine who is the organizer of this event.
223          * We need to determine "me" or "not me."
224          */
225         organizer = icalcomponent_get_first_property(vevent,
226                                                 ICAL_ORGANIZER_PROPERTY);
227         if (organizer != NULL) {
228                 strcpy(organizer_string, icalproperty_get_organizer(organizer));
229                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
230                         strcpy(organizer_string, &organizer_string[7]);
231                         striplt(organizer_string);
232                         serv_printf("ISME %s", organizer_string);
233                         serv_gets(buf);
234                         if (buf[0] == '2') {
235                                 organizer_is_me = 1;
236                         }
237                 }
238         }
239         wprintf("<TR><TD><B>Organizer</B></TD><TD>");
240         escputs(organizer_string);
241         if (organizer_is_me) {
242                 wprintf(" <FONT SIZE=-1><I>"
243                         "(you are the organizer)</I></FONT>\n");
244         }
245
246         /*
247          * Transmit the organizer as a hidden field.   We don't want the user
248          * to be able to change it, but we do want it fed back to the server,
249          * especially if this is a new event and there is no organizer already
250          * in the calendar object.
251          */
252         wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
253         escputs(organizer_string);
254         wprintf("\">");
255
256         wprintf("</TD></TR>\n");
257
258         /* Attendees (do more with this later) */
259         wprintf("<TR><TD><B>Attendes</B><BR>"
260                 "<FONT SIZE=-2>(Separate multiple attendees with commas)"
261                 "</FONT></TD><TD>"
262                 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
263                 "ROWS=3 COLS=80 WIDTH=80>\n",
264                 (organizer_is_me ? "" : "DISABLED ")
265         );
266         i = 0;
267         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
268                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
269                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
270                         strcpy(attendee_string, &attendee_string[7]);
271                         striplt(attendee_string);
272                         if (i++) wprintf(", ");
273                         escputs(attendee_string);
274                 }
275         }
276         wprintf("</TEXTAREA></TD></TR>\n");
277
278         /* Done with properties. */
279         wprintf("</TABLE>\n<CENTER>"
280                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
281                 "&nbsp;&nbsp;"
282                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
283                 "&nbsp;&nbsp;"
284                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
285                 "</CENTER>\n"
286         );
287
288         wprintf("</FORM>\n");
289         
290         wprintf("<SCRIPT language=\"javascript\">
291                 <!--
292
293                         if (document.EventForm.alldayevent.checked) {
294                                 document.EventForm.dtstart_hour.value='0';
295                                 document.EventForm.dtstart_hour.disabled = true;
296                                 document.EventForm.dtstart_minute.value='0';
297                                 document.EventForm.dtstart_minute.disabled = true;
298                                 document.EventForm.dtend_hour.value='0';
299                                 document.EventForm.dtend_hour.disabled = true;
300                                 document.EventForm.dtend_minute.value='0';
301                                 document.EventForm.dtend_minute.disabled = true;
302                                 document.EventForm.dtend_month.disabled = true;
303                                 document.EventForm.dtend_day.disabled = true;
304                                 document.EventForm.dtend_year.disabled = true;
305                         }
306                         else {
307                                 document.EventForm.dtstart_hour.disabled = false;
308                                 document.EventForm.dtstart_minute.disabled = false;
309                                 document.EventForm.dtend_hour.disabled = false;
310                                 document.EventForm.dtend_minute.disabled = false;
311                                 document.EventForm.dtend_month.disabled = false;
312                                 document.EventForm.dtend_day.disabled = false;
313                                 document.EventForm.dtend_year.disabled = false;
314                         }
315                 //-->
316                 </SCRIPT>
317         ");
318
319         wDumpContent(1);
320
321         if (created_new_vevent) {
322                 icalcomponent_free(vevent);
323         }
324 }
325
326 /*
327  * Save an edited event
328  */
329 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
330         char buf[SIZ];
331         int delete_existing = 0;
332         icalproperty *prop;
333         icalcomponent *vevent;
334         int created_new_vevent = 0;
335         int all_day_event = 0;
336         struct icaltimetype event_start;
337         icalproperty *attendee = NULL;
338         char attendee_string[SIZ];
339         int i;
340         int foundit;
341         char form_attendees[SIZ];
342         char organizer_string[SIZ];
343
344         if (supplied_vevent != NULL) {
345                 vevent = supplied_vevent;
346         }
347         else {
348                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
349                 created_new_vevent = 1;
350         }
351
352         if (!strcasecmp(bstr("sc"), "Save")) {
353
354                 /* Replace values in the component with ones from the form */
355
356                 while (prop = icalcomponent_get_first_property(vevent,
357                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
358                         icalcomponent_remove_property(vevent, prop);
359                         icalproperty_free(prop);
360                 }
361                 icalcomponent_add_property(vevent,
362                         icalproperty_new_summary(bstr("summary")));
363                 
364                 while (prop = icalcomponent_get_first_property(vevent,
365                       ICAL_LOCATION_PROPERTY), prop != NULL) {
366                         icalcomponent_remove_property(vevent, prop);
367                         icalproperty_free(prop);
368                 }
369                 icalcomponent_add_property(vevent,
370                         icalproperty_new_location(bstr("location")));
371                 
372                 while (prop = icalcomponent_get_first_property(vevent,
373                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
374                         icalcomponent_remove_property(vevent, prop);
375                         icalproperty_free(prop);
376                 }
377                 icalcomponent_add_property(vevent,
378                         icalproperty_new_description(bstr("description")));
379         
380                 while (prop = icalcomponent_get_first_property(vevent,
381                       ICAL_DTSTART_PROPERTY), prop != NULL) {
382                         icalcomponent_remove_property(vevent, prop);
383                         icalproperty_free(prop);
384                 }
385
386                 if (!strcmp(bstr("alldayevent"), "yes")) {
387                         all_day_event = 1;
388                 }
389                 else {
390                         all_day_event = 0;
391                 }
392
393                 event_start = icaltime_from_webform("dtstart");
394                 if (all_day_event) {
395                         event_start.is_date = 1;
396                         event_start.hour = 0;
397                         event_start.minute = 0;
398                         event_start.second = 0;
399                 }
400
401
402                 /* The following odd-looking snippet of code looks like it
403                  * takes some unnecessary steps.  It is done this way because
404                  * libical incorrectly turns an "all day event" into a normal
405                  * event starting at midnight (i.e. it serializes as date/time
406                  * instead of just date) unless icalvalue_new_date() is used.
407                  * So we force it, if this is an all day event.
408                  */
409                 prop = icalproperty_new_dtstart(event_start);
410                 if (all_day_event) {
411                         icalproperty_set_value(prop,
412                                 icalvalue_new_date(event_start)
413                         );
414                 }
415
416                 if (prop) icalcomponent_add_property(vevent, prop);
417                 else icalproperty_free(prop);
418
419
420
421                 while (prop = icalcomponent_get_first_property(vevent,
422                       ICAL_DTEND_PROPERTY), prop != NULL) {
423                         icalcomponent_remove_property(vevent, prop);
424                         icalproperty_free(prop);
425                 }
426                 while (prop = icalcomponent_get_first_property(vevent,
427                       ICAL_DURATION_PROPERTY), prop != NULL) {
428                         icalcomponent_remove_property(vevent, prop);
429                         icalproperty_free(prop);
430                 }
431
432                 if (all_day_event == 0) {
433                         icalcomponent_add_property(vevent,
434                                 icalproperty_new_dtend(icaltime_normalize(
435                                         icaltime_from_webform("dtend"))
436                                 )
437                         );
438                 }
439
440                 /* Give this event a UID if it doesn't have one. */
441                 if (icalcomponent_get_first_property(vevent,
442                    ICAL_UID_PROPERTY) == NULL) {
443                         generate_new_uid(buf);
444                         icalcomponent_add_property(vevent,
445                                 icalproperty_new_uid(buf)
446                         );
447                 }
448
449                 /* Set the organizer, only if one does not already exist *and*
450                  * the form is supplying one
451                  */
452                 strcpy(buf, bstr("organizer"));
453                 if ( (icalcomponent_get_first_property(vevent,
454                    ICAL_ORGANIZER_PROPERTY) == NULL) 
455                    && (strlen(buf) > 0) ) {
456
457                         /* set new organizer */
458                         sprintf(organizer_string, "MAILTO:%s", buf);
459                         icalcomponent_add_property(vevent,
460                                 icalproperty_new_organizer(organizer_string)
461                         );
462
463                 }
464
465                 /*
466                  * Add any new attendees listed in the web form
467                  */
468                 strcpy(form_attendees, bstr("attendees"));
469                 for (i=0; i<num_tokens(form_attendees, ','); ++i) {
470                         extract_token(buf, form_attendees, i, ',');
471                         striplt(buf);
472                         if (strlen(buf) > 0) {
473                                 lprintf(9, "Attendee: <%s>\n", buf);
474                                 sprintf(attendee_string, "MAILTO:%s", buf);
475                                 foundit = 0;
476
477                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
478                                         if (!strcasecmp(attendee_string,
479                                            icalproperty_get_attendee(attendee)))
480                                                 ++foundit;
481                                 }
482
483
484                                 if (foundit == 0) {
485                                         icalcomponent_add_property(vevent,
486                                                 icalproperty_new_attendee(attendee_string)
487                                         );
488                                 }
489                         }
490                 }
491
492                 /*
493                  * Remove any attendees *not* listed in the web form
494                  */
495 STARTOVER:
496                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
497                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
498                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
499                                 strcpy(attendee_string, &attendee_string[7]);
500                                 striplt(attendee_string);
501                                 foundit = 0;
502                                 for (i=0; i<num_tokens(form_attendees, ','); ++i) {
503                                         extract_token(buf, form_attendees, i, ',');
504                                         striplt(buf);
505                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
506                                 }
507                                 if (foundit == 0) {
508                                         icalcomponent_remove_property(vevent, attendee);
509                                         icalproperty_free(attendee);
510                                         goto STARTOVER;
511                                 }
512                         }
513                 }
514
515                 /*
516                  * Serialize it and save it to the message base
517                  */
518                 serv_puts("ENT0 1|||4");
519                 serv_gets(buf);
520                 if (buf[0] == '4') {
521                         serv_puts("Content-type: text/calendar");
522                         serv_puts("");
523                         serv_puts(icalcomponent_as_ical_string(vevent));
524                         serv_puts("000");
525                         delete_existing = 1;
526                 }
527         }
528
529         /*
530          * If the user clicked 'Delete' then delete it, period.
531          */
532         if (!strcasecmp(bstr("sc"), "Delete")) {
533                 delete_existing = 1;
534         }
535
536         if ( (delete_existing) && (msgnum > 0L) ) {
537                 serv_printf("DELE %ld", atol(bstr("msgnum")));
538                 serv_gets(buf);
539         }
540
541         if (created_new_vevent) {
542                 icalcomponent_free(vevent);
543         }
544
545         /* Go back to the event list */
546         readloop("readfwd");
547 }
548
549
550 #endif /* HAVE_ICAL_H */