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