*** empty log message ***
[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 /*
35  * Display an event by itself (for editing)
36  */
37 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
38         icalcomponent *vevent;
39         icalproperty *p;
40         struct icaltimetype t_start, t_end;
41         time_t now;
42         int created_new_vevent = 0;
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         /* Determine who is the organizer of this event.  This is useless
213          * for now, but we'll need to determine "me" or "not me" soon.
214          */
215         organizer = icalcomponent_get_first_property(vevent,
216                                                 ICAL_ORGANIZER_PROPERTY);
217         if (organizer != NULL) {
218                 strcpy(organizer_string, icalproperty_get_organizer(organizer));
219                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
220                         strcpy(organizer_string, &organizer_string[7]);
221                         striplt(organizer_string);
222                         serv_printf("ISME %s", organizer_string);
223                         serv_gets(buf);
224                         if (buf[0] == '2') {
225                                 organizer_is_me = 1;
226                         }
227                 }
228         }
229         wprintf("<TR><TD>Organizer<BR>(FIXME)</TD><TD>");
230         escputs(organizer_string);
231         if (organizer_is_me) {
232                 wprintf("(you. FIXME)\n");
233         }
234         wprintf("</TD></TR>\n");
235
236         /* Attendees (do more with this later) */
237         wprintf("<TR><TD><B>Attendes</B><BR>"
238                 "<FONT SIZE=-2>(Separate multiple attendees with commas)"
239                 "</FONT></TD><TD>"
240                 "<TEXTAREA NAME=\"attendees\" wrap=soft "
241                 "ROWS=3 COLS=80 WIDTH=80>\n");
242         i = 0;
243         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
244                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
245                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
246                         strcpy(attendee_string, &attendee_string[7]);
247                         striplt(attendee_string);
248                         if (i++) wprintf(", ");
249                         escputs(attendee_string);
250                 }
251         }
252         wprintf("</TEXTAREA></TD></TR>\n");
253
254         /* Done with properties. */
255         wprintf("</TABLE>\n<CENTER>"
256                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
257                 "&nbsp;&nbsp;"
258                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
259                 "&nbsp;&nbsp;"
260                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
261                 "</CENTER>\n"
262         );
263
264         wprintf("</FORM>\n");
265         
266         wprintf("<SCRIPT language=\"javascript\">
267                 <!--
268
269                         if (document.EventForm.alldayevent.checked) {
270                                 document.EventForm.dtstart_hour.value='0';
271                                 document.EventForm.dtstart_hour.disabled = true;
272                                 document.EventForm.dtstart_minute.value='0';
273                                 document.EventForm.dtstart_minute.disabled = true;
274                                 document.EventForm.dtend_hour.value='0';
275                                 document.EventForm.dtend_hour.disabled = true;
276                                 document.EventForm.dtend_minute.value='0';
277                                 document.EventForm.dtend_minute.disabled = true;
278                                 document.EventForm.dtend_month.disabled = true;
279                                 document.EventForm.dtend_day.disabled = true;
280                                 document.EventForm.dtend_year.disabled = true;
281                         }
282                         else {
283                                 document.EventForm.dtstart_hour.disabled = false;
284                                 document.EventForm.dtstart_minute.disabled = false;
285                                 document.EventForm.dtend_hour.disabled = false;
286                                 document.EventForm.dtend_minute.disabled = false;
287                                 document.EventForm.dtend_month.disabled = false;
288                                 document.EventForm.dtend_day.disabled = false;
289                                 document.EventForm.dtend_year.disabled = false;
290                         }
291                 //-->
292                 </SCRIPT>
293         ");
294
295         wDumpContent(1);
296
297         if (created_new_vevent) {
298                 icalcomponent_free(vevent);
299         }
300 }
301
302 /*
303  * Save an edited event
304  */
305 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
306         char buf[SIZ];
307         int delete_existing = 0;
308         icalproperty *prop;
309         icalcomponent *vevent;
310         int created_new_vevent = 0;
311         int all_day_event = 0;
312         struct icaltimetype event_start;
313
314         if (supplied_vevent != NULL) {
315                 vevent = supplied_vevent;
316         }
317         else {
318                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
319                 created_new_vevent = 1;
320         }
321
322         if (!strcasecmp(bstr("sc"), "Save")) {
323
324                 /* Replace values in the component with ones from the form */
325
326                 while (prop = icalcomponent_get_first_property(vevent,
327                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
328                         icalcomponent_remove_property(vevent, prop);
329                 }
330                 icalcomponent_add_property(vevent,
331                         icalproperty_new_summary(bstr("summary")));
332                 
333                 while (prop = icalcomponent_get_first_property(vevent,
334                       ICAL_LOCATION_PROPERTY), prop != NULL) {
335                         icalcomponent_remove_property(vevent, prop);
336                 }
337                 icalcomponent_add_property(vevent,
338                         icalproperty_new_location(bstr("location")));
339                 
340                 while (prop = icalcomponent_get_first_property(vevent,
341                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
342                         icalcomponent_remove_property(vevent, prop);
343                 }
344                 icalcomponent_add_property(vevent,
345                         icalproperty_new_description(bstr("description")));
346         
347                 while (prop = icalcomponent_get_first_property(vevent,
348                       ICAL_DTSTART_PROPERTY), prop != NULL) {
349                         icalcomponent_remove_property(vevent, prop);
350                 }
351
352                 if (!strcmp(bstr("alldayevent"), "yes")) {
353                         all_day_event = 1;
354                         lprintf(9, "*** all day event ***\n");
355                 }
356                 else {
357                         all_day_event = 0;
358                 }
359
360                 event_start = icaltime_from_webform("dtstart");
361                 if (all_day_event) {
362                         event_start.is_date = 1;
363                         event_start.hour = 0;
364                         event_start.minute = 0;
365                         event_start.second = 0;
366                 }
367
368
369                 /* The following odd-looking snippet of code looks like it
370                  * takes some unnecessary steps.  It is done this way because
371                  * libical incorrectly turns an "all day event" into a normal
372                  * event starting at midnight (i.e. it serializes as date/time
373                  * instead of just date) unless icalvalue_new_date() is used.
374                  * So we force it, if this is an all day event.
375                  */
376                 prop = icalproperty_new_dtstart(event_start);
377                 if (all_day_event) {
378                         icalproperty_set_value(prop,
379                                 icalvalue_new_date(event_start)
380                         );
381                 }
382
383                 if (prop) icalcomponent_add_property(vevent, prop);
384                 else icalproperty_free(prop);
385
386
387
388                 while (prop = icalcomponent_get_first_property(vevent,
389                       ICAL_DTEND_PROPERTY), prop != NULL) {
390                         icalcomponent_remove_property(vevent, prop);
391                 }
392                 while (prop = icalcomponent_get_first_property(vevent,
393                       ICAL_DURATION_PROPERTY), prop != NULL) {
394                         icalcomponent_remove_property(vevent, prop);
395                 }
396
397                 if (all_day_event == 0) {
398                         icalcomponent_add_property(vevent,
399                                 icalproperty_new_dtend(icaltime_normalize(
400                                         icaltime_from_webform("dtend"))
401                                 )
402                         );
403                 }
404
405                 /* Give this event a UID if it doesn't have one. */
406                 if (icalcomponent_get_first_property(vevent,
407                    ICAL_UID_PROPERTY) == NULL) {
408                         generate_new_uid(buf);
409                         icalcomponent_add_property(vevent,
410                                 icalproperty_new_uid(buf)
411                         );
412                 }
413
414                 /* Serialize it and save it to the message base */
415                 serv_puts("ENT0 1|||4");
416                 serv_gets(buf);
417                 if (buf[0] == '4') {
418                         serv_puts("Content-type: text/calendar");
419                         serv_puts("");
420                         serv_puts(icalcomponent_as_ical_string(vevent));
421                         serv_puts("000");
422                         delete_existing = 1;
423                 }
424         }
425
426         /*
427          * If the user clicked 'Delete' then delete it, period.
428          */
429         if (!strcasecmp(bstr("sc"), "Delete")) {
430                 delete_existing = 1;
431         }
432
433         if ( (delete_existing) && (msgnum > 0L) ) {
434                 serv_printf("DELE %ld", atol(bstr("msgnum")));
435                 serv_gets(buf);
436         }
437
438         if (created_new_vevent) {
439                 icalcomponent_free(vevent);
440         }
441
442         /* Go back to the event list */
443         readloop("readfwd");
444 }
445
446
447 #endif /* HAVE_ICAL_H */