* Started some "me as organizer" stuff. (BROKEN BUILD ... I'll fix soon.)
[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         icalorganizertype me_as_organizer;
43         icalproperty *organizer = NULL;
44         char organizer_string[SIZ];
45         icalproperty *attendee = NULL;
46         char attendee_string[SIZ];
47         char buf[SIZ];
48         int i;
49         int organizer_is_me = 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         output_headers(3);
64         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
65                 "<IMG ALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
66                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
67                 "<B>Edit event</B>"
68                 "</FONT></TD></TR></TABLE><BR>\n"
69         );
70
71         /************************************************************
72          * Uncomment this to see the UID in calendar events for debugging
73         wprintf("UID == ");
74         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
75         if (p != NULL) {
76                 escputs((char *)icalproperty_get_comment(p));
77         }
78         *************************************************************/
79
80         wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
81
82         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
83                 msgnum);
84         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
85                 bstr("calview"));
86         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
87                 bstr("year"));
88         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
89                 bstr("month"));
90         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
91                 bstr("day"));
92
93         /* Put it in a borderless table so it lines up nicely */
94         wprintf("<TABLE border=0 width=100%%>\n");
95
96         wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
97                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
98                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
99         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
100         if (p != NULL) {
101                 escputs((char *)icalproperty_get_comment(p));
102         }
103         wprintf("\"></TD></TR>\n");
104
105         wprintf("<TR><TD><B>Location</B></TD><TD>\n"
106                 "<INPUT TYPE=\"text\" NAME=\"location\" "
107                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
108         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
109         if (p != NULL) {
110                 escputs((char *)icalproperty_get_comment(p));
111         }
112         wprintf("\"></TD></TR>\n");
113
114         wprintf("<TR><TD><B>Start</B></TD><TD>\n");
115         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
116         if (p != NULL) {
117                 t_start = icalproperty_get_dtstart(p);
118                 if (t_start.is_date) {
119                         t_start.hour = 0;
120                         t_start.minute = 0;
121                         t_start.second = 0;
122                 }
123         }
124         else {
125                 memset(&t_start, 0, sizeof t_start);
126                 t_start.year = atoi(bstr("year"));
127                 t_start.month = atoi(bstr("month"));
128                 t_start.day = atoi(bstr("day"));
129                 if (strlen(bstr("hour")) > 0) {
130                         t_start.hour = atoi(bstr("hour"));
131                         t_start.minute = atoi(bstr("minute"));
132                 }
133                 else {
134                         t_start.hour = 9;
135                         t_start.minute = 0;
136                 }
137                 /* t_start = icaltime_from_timet(now, 0); */
138         }
139         display_icaltimetype_as_webform(&t_start, "dtstart");
140
141         wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
142                 "VALUE=\"yes\" onClick=\"
143
144                         if (this.checked) {
145                                 this.form.dtstart_hour.value='0';
146                                 this.form.dtstart_hour.disabled = true;
147                                 this.form.dtstart_minute.value='0';
148                                 this.form.dtstart_minute.disabled = true;
149                                 this.form.dtend_hour.value='0';
150                                 this.form.dtend_hour.disabled = true;
151                                 this.form.dtend_minute.value='0';
152                                 this.form.dtend_minute.disabled = true;
153                                 this.form.dtend_month.disabled = true;
154                                 this.form.dtend_day.disabled = true;
155                                 this.form.dtend_year.disabled = true;
156                         }
157                         else {
158                                 this.form.dtstart_hour.disabled = false;
159                                 this.form.dtstart_minute.disabled = false;
160                                 this.form.dtend_hour.disabled = false;
161                                 this.form.dtend_minute.disabled = false;
162                                 this.form.dtend_month.disabled = false;
163                                 this.form.dtend_day.disabled = false;
164                                 this.form.dtend_year.disabled = false;
165                         }
166
167
168                 \" %s >All day event",
169                 (t_start.is_date ? "CHECKED" : "" )
170         );
171
172         wprintf("</TD></TR>\n");
173
174         /* If this is an all-day-event, set the end time to be identical to
175          * the start time (the hour/minute/second will be set to midnight).
176          * Otherwise extract or create it.
177          */
178         wprintf("<TR><TD><B>End</B></TD><TD>\n");
179         if (t_start.is_date) {
180                 t_end = t_start;
181         }
182         else {
183                 p = icalcomponent_get_first_property(vevent,
184                                                         ICAL_DTEND_PROPERTY);
185                 if (p != NULL) {
186                         t_end = icalproperty_get_dtend(p);
187                 }
188                 else {
189                         /* If this is not an all-day event and there is no
190                          * end time specified, make the default one hour
191                          * from the start time.
192                          */
193                         t_end = t_start;
194                         t_end.hour += 1;
195                         t_end = icaltime_normalize(t_end);
196                         /* t_end = icaltime_from_timet(now, 0); */
197                 }
198         }
199         display_icaltimetype_as_webform(&t_end, "dtend");
200         wprintf("</TD></TR>\n");
201
202         wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
203                 "<TEXTAREA NAME=\"description\" wrap=soft "
204                 "ROWS=10 COLS=80 WIDTH=80>\n"
205         );
206         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
207         if (p != NULL) {
208                 escputs((char *)icalproperty_get_comment(p));
209         }
210         wprintf("</TEXTAREA></TD></TR>");
211
212         /* For a new event, the user creating the event should be the
213          * organizer.  Set this field accordingly.
214          */
215         if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
216            == NULL) {
217                 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
218                 memset(&me_as_organizer, 0, sizeof(icalorganizertype));
219                 me_as_organizer.value = strdup(organizer_string);
220                 icalcomponent_set_organizer(vevent, me_as_organizer);
221         }
222
223         /* Determine who is the organizer of this event.  This is useless
224          * for now, but we'll need to determine "me" or "not me" soon.
225          */
226         organizer = icalcomponent_get_first_property(vevent,
227                                                 ICAL_ORGANIZER_PROPERTY);
228         if (organizer != NULL) {
229                 strcpy(organizer_string, icalproperty_get_organizer(organizer));
230                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
231                         strcpy(organizer_string, &organizer_string[7]);
232                         striplt(organizer_string);
233                         serv_printf("ISME %s", organizer_string);
234                         serv_gets(buf);
235                         if (buf[0] == '2') {
236                                 organizer_is_me = 1;
237                         }
238                 }
239         }
240         wprintf("<TR><TD>Organizer<BR>(FIXME)</TD><TD>");
241         escputs(organizer_string);
242         if (organizer_is_me) {
243                 wprintf("(you. FIXME)\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 NAME=\"attendees\" wrap=soft "
252                 "ROWS=3 COLS=80 WIDTH=80>\n");
253         i = 0;
254         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
255                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
256                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
257                         strcpy(attendee_string, &attendee_string[7]);
258                         striplt(attendee_string);
259                         if (i++) wprintf(", ");
260                         escputs(attendee_string);
261                 }
262         }
263         wprintf("</TEXTAREA></TD></TR>\n");
264
265         /* Done with properties. */
266         wprintf("</TABLE>\n<CENTER>"
267                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
268                 "&nbsp;&nbsp;"
269                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
270                 "&nbsp;&nbsp;"
271                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
272                 "</CENTER>\n"
273         );
274
275         wprintf("</FORM>\n");
276         
277         wprintf("<SCRIPT language=\"javascript\">
278                 <!--
279
280                         if (document.EventForm.alldayevent.checked) {
281                                 document.EventForm.dtstart_hour.value='0';
282                                 document.EventForm.dtstart_hour.disabled = true;
283                                 document.EventForm.dtstart_minute.value='0';
284                                 document.EventForm.dtstart_minute.disabled = true;
285                                 document.EventForm.dtend_hour.value='0';
286                                 document.EventForm.dtend_hour.disabled = true;
287                                 document.EventForm.dtend_minute.value='0';
288                                 document.EventForm.dtend_minute.disabled = true;
289                                 document.EventForm.dtend_month.disabled = true;
290                                 document.EventForm.dtend_day.disabled = true;
291                                 document.EventForm.dtend_year.disabled = true;
292                         }
293                         else {
294                                 document.EventForm.dtstart_hour.disabled = false;
295                                 document.EventForm.dtstart_minute.disabled = false;
296                                 document.EventForm.dtend_hour.disabled = false;
297                                 document.EventForm.dtend_minute.disabled = false;
298                                 document.EventForm.dtend_month.disabled = false;
299                                 document.EventForm.dtend_day.disabled = false;
300                                 document.EventForm.dtend_year.disabled = false;
301                         }
302                 //-->
303                 </SCRIPT>
304         ");
305
306         wDumpContent(1);
307
308         if (created_new_vevent) {
309                 icalcomponent_free(vevent);
310         }
311 }
312
313 /*
314  * Save an edited event
315  */
316 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
317         char buf[SIZ];
318         int delete_existing = 0;
319         icalproperty *prop;
320         icalcomponent *vevent;
321         int created_new_vevent = 0;
322         int all_day_event = 0;
323         struct icaltimetype event_start;
324
325         if (supplied_vevent != NULL) {
326                 vevent = supplied_vevent;
327         }
328         else {
329                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
330                 created_new_vevent = 1;
331         }
332
333         if (!strcasecmp(bstr("sc"), "Save")) {
334
335                 /* Replace values in the component with ones from the form */
336
337                 while (prop = icalcomponent_get_first_property(vevent,
338                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
339                         icalcomponent_remove_property(vevent, prop);
340                         icalproperty_free(prop);
341                 }
342                 icalcomponent_add_property(vevent,
343                         icalproperty_new_summary(bstr("summary")));
344                 
345                 while (prop = icalcomponent_get_first_property(vevent,
346                       ICAL_LOCATION_PROPERTY), prop != NULL) {
347                         icalcomponent_remove_property(vevent, prop);
348                         icalproperty_free(prop);
349                 }
350                 icalcomponent_add_property(vevent,
351                         icalproperty_new_location(bstr("location")));
352                 
353                 while (prop = icalcomponent_get_first_property(vevent,
354                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
355                         icalcomponent_remove_property(vevent, prop);
356                         icalproperty_free(prop);
357                 }
358                 icalcomponent_add_property(vevent,
359                         icalproperty_new_description(bstr("description")));
360         
361                 while (prop = icalcomponent_get_first_property(vevent,
362                       ICAL_DTSTART_PROPERTY), prop != NULL) {
363                         icalcomponent_remove_property(vevent, prop);
364                         icalproperty_free(prop);
365                 }
366
367                 if (!strcmp(bstr("alldayevent"), "yes")) {
368                         all_day_event = 1;
369                         lprintf(9, "*** all day event ***\n");
370                 }
371                 else {
372                         all_day_event = 0;
373                 }
374
375                 event_start = icaltime_from_webform("dtstart");
376                 if (all_day_event) {
377                         event_start.is_date = 1;
378                         event_start.hour = 0;
379                         event_start.minute = 0;
380                         event_start.second = 0;
381                 }
382
383
384                 /* The following odd-looking snippet of code looks like it
385                  * takes some unnecessary steps.  It is done this way because
386                  * libical incorrectly turns an "all day event" into a normal
387                  * event starting at midnight (i.e. it serializes as date/time
388                  * instead of just date) unless icalvalue_new_date() is used.
389                  * So we force it, if this is an all day event.
390                  */
391                 prop = icalproperty_new_dtstart(event_start);
392                 if (all_day_event) {
393                         icalproperty_set_value(prop,
394                                 icalvalue_new_date(event_start)
395                         );
396                 }
397
398                 if (prop) icalcomponent_add_property(vevent, prop);
399                 else icalproperty_free(prop);
400
401
402
403                 while (prop = icalcomponent_get_first_property(vevent,
404                       ICAL_DTEND_PROPERTY), prop != NULL) {
405                         icalcomponent_remove_property(vevent, prop);
406                         icalproperty_free(prop);
407                 }
408                 while (prop = icalcomponent_get_first_property(vevent,
409                       ICAL_DURATION_PROPERTY), prop != NULL) {
410                         icalcomponent_remove_property(vevent, prop);
411                         icalproperty_free(prop);
412                 }
413
414                 if (all_day_event == 0) {
415                         icalcomponent_add_property(vevent,
416                                 icalproperty_new_dtend(icaltime_normalize(
417                                         icaltime_from_webform("dtend"))
418                                 )
419                         );
420                 }
421
422                 /* Give this event a UID if it doesn't have one. */
423                 if (icalcomponent_get_first_property(vevent,
424                    ICAL_UID_PROPERTY) == NULL) {
425                         generate_new_uid(buf);
426                         icalcomponent_add_property(vevent,
427                                 icalproperty_new_uid(buf)
428                         );
429                 }
430
431                 /* Serialize it and save it to the message base */
432                 serv_puts("ENT0 1|||4");
433                 serv_gets(buf);
434                 if (buf[0] == '4') {
435                         serv_puts("Content-type: text/calendar");
436                         serv_puts("");
437                         serv_puts(icalcomponent_as_ical_string(vevent));
438                         serv_puts("000");
439                         delete_existing = 1;
440                 }
441         }
442
443         /*
444          * If the user clicked 'Delete' then delete it, period.
445          */
446         if (!strcasecmp(bstr("sc"), "Delete")) {
447                 delete_existing = 1;
448         }
449
450         if ( (delete_existing) && (msgnum > 0L) ) {
451                 serv_printf("DELE %ld", atol(bstr("msgnum")));
452                 serv_gets(buf);
453         }
454
455         if (created_new_vevent) {
456                 icalcomponent_free(vevent);
457         }
458
459         /* Go back to the event list */
460         readloop("readfwd");
461 }
462
463
464 #endif /* HAVE_ICAL_H */