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