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