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