]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* Make the "all day event" shadebox smaller (month view) - no border.
[citadel.git] / webcit / calendar.c
1 /*
2  * $Id$
3  *
4  * Functions which handle calendar objects and their processing/display.
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 #ifndef HAVE_ICAL_H
31
32 /*
33  * Handler stubs for builds with no calendar library available
34  */
35 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
36
37         wprintf("<I>This message contains calendaring/scheduling information,"
38                 " but support for calendars is not available on this "
39                 "particular system.  Please ask your system administrator to "
40                 "install a new version of the Citadel web service with "
41                 "calendaring enabled.</I><BR>\n"
42         );
43
44 }
45
46 void display_calendar(long msgnum) {
47         wprintf("<i>"
48                 "Cannot display calendar item.  You are seeing this error "
49                 "because your WebCit service has not been installed with "
50                 "calendar support.  Please contact your system administrator."
51                 "</i><br>\n");
52 }
53
54 void display_task(long msgnum) {
55         wprintf("<i>"
56                 "Cannot display to-do item.  You are seeing this error "
57                 "because your WebCit service has not been installed with "
58                 "calendar support.  Please contact your system administrator."
59                 "</i><br>\n");
60 }
61
62 #else /* HAVE_ICAL_H */
63
64
65 /******   End of handler stubs.  Everything below this line is real.   ******/
66
67
68
69
70 /*
71  * Process a calendar object
72  * ...at this point it's already been deserialized by cal_process_attachment()
73  */
74 void cal_process_object(icalcomponent *cal,
75                         int recursion_level,
76                         long msgnum,
77                         char *cal_partnum
78 ) {
79         icalcomponent *c;
80         icalproperty *method = NULL;
81         icalproperty_method the_method = ICAL_METHOD_NONE;
82         icalproperty *p;
83         struct icaltimetype t;
84         time_t tt;
85         char buf[SIZ];
86         char conflict_name[SIZ];
87         int is_update = 0;
88
89         /* Leading HTML for the display of this object */
90         if (recursion_level == 0) {
91                 wprintf("<CENTER><TABLE border=0>\n");
92         }
93
94         /* Look for a method */
95         method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
96
97         /* See what we need to do with this */
98         if (method != NULL) {
99                 the_method = icalproperty_get_method(method);
100                 switch(the_method) {
101                     case ICAL_METHOD_REQUEST:
102                         wprintf("<TR><TD COLSPAN=2>\n"
103                                 "<IMG ALIGN=CENTER "
104                                 "SRC=\"/static/vcalendar.gif\">"
105                                 "&nbsp;&nbsp;"  
106                                 "<B>Meeting invitation</B>
107                                 </TD></TR>\n"
108                         );
109                         break;
110                     case ICAL_METHOD_PUBLISH:
111                         wprintf("<TR><TD COLSPAN=2>\n"
112                                 "<IMG ALIGN=CENTER "
113                                 "SRC=\"/static/vcalendar.gif\">"
114                                 "&nbsp;&nbsp;"  
115                                 "<B>Published event</B>
116                                 </TD></TR>\n"
117                         );
118                         break;
119                     default:
120                         wprintf("<TR><TD COLSPAN=2>"
121                                 "I don't know what to do with this.</TD></TR>"
122                                 "\n");
123                         break;
124                 }
125         }
126
127         p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
128         if (p != NULL) {
129                 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
130                 escputs((char *)icalproperty_get_comment(p));
131                 wprintf("</TD></TR>\n");
132         }
133
134         p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
135         if (p != NULL) {
136                 wprintf("<TR><TD><B>Location:</B></TD><TD>");
137                 escputs((char *)icalproperty_get_comment(p));
138                 wprintf("</TD></TR>\n");
139         }
140
141         /*
142          * Only show start/end times if we're actually looking at the VEVENT
143          * component.  Otherwise it shows bogus dates for things like timezone.
144          */
145         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
146
147                 p = icalcomponent_get_first_property(cal,
148                                                 ICAL_DTSTART_PROPERTY);
149                 if (p != NULL) {
150                         t = icalproperty_get_dtstart(p);
151
152                         if (t.is_date) {
153                                 wprintf("<TR><TD><B>Date:"
154                                         "</B></TD><TD>"
155                                         "%s %d, %d</TD></TR>",
156                                         months[t.month - 1],
157                                         t.day, t.year
158                                 );
159                         }
160                         else {
161                                 tt = icaltime_as_timet(t);
162                                 fmt_date(buf, tt);
163                                 wprintf("<TR><TD><B>Starting date/time:"
164                                         "</B></TD><TD>"
165                                         "%s</TD></TR>", buf
166                                 );
167                         }
168                 }
169         
170                 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
171                 if (p != NULL) {
172                         t = icalproperty_get_dtend(p);
173                         tt = icaltime_as_timet(t);
174                         fmt_date(buf, tt);
175                         wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
176                                 "%s</TD></TR>", buf
177                         );
178                 }
179
180         }
181
182         p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
183         if (p != NULL) {
184                 wprintf("<TR><TD><B>Description:</B></TD><TD>");
185                 escputs((char *)icalproperty_get_comment(p));
186                 wprintf("</TD></TR>\n");
187         }
188
189         /* If the component has subcomponents, recurse through them. */
190         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
191             (c != 0);
192             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
193                 /* Recursively process subcomponent */
194                 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
195         }
196
197         /* If this is a REQUEST, display conflicts and buttons */
198         if (the_method == ICAL_METHOD_REQUEST) {
199
200                 /* Check for conflicts */
201                 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
202                 serv_gets(buf);
203                 if (buf[0] == '1') {
204                         while (serv_gets(buf), strcmp(buf, "000")) {
205                                 extract(conflict_name, buf, 3);
206                                 is_update = extract_int(buf, 4);
207                                 wprintf("<TR><TD><B><I>%s</I></B></TD>"
208                                         "<TD>"
209                                         "%s "
210                                         "<I>&quot;",
211
212                                         (is_update ?
213                                                 "Update:" :
214                                                 "CONFLICT:"
215                                         ),
216
217                                         (is_update ?
218                                                 "This is an update of" :
219                                                 "This event would conflict with"
220                                         )
221                 
222                                 );
223                                 escputs(conflict_name);
224                                 wprintf("&quot;</I> "
225                                         "which is already in your calendar."
226                                         "</TD></TR>\n");
227                         }
228                 }
229
230                 /* Display the Accept/Decline buttons */
231                 wprintf("<TR><TD COLSPAN=2>"
232                         "<FORM METHOD=\"GET\" "
233                         "ACTION=\"/respond_to_request\">\n"
234                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
235                                 "VALUE=\"Accept\">\n"
236                         "&nbsp;&nbsp;"
237                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
238                                 "VALUE=\"Tentative\">\n"
239                         "&nbsp;&nbsp;"
240                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
241                                 "VALUE=\"Decline\">\n"
242                         "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
243                                 "VALUE=\"%ld\">"
244                         "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
245                                 "VALUE=\"%s\">"
246                         "</FORM>"
247                         "</TD></TR>\n",
248                         msgnum, cal_partnum
249                 );
250
251         }
252
253         /* Trailing HTML for the display of this object */
254         if (recursion_level == 0) {
255
256                 wprintf("</TR></TABLE></CENTER>\n");
257         }
258 }
259
260
261 /*
262  * Deserialize a calendar object in a message so it can be processed.
263  * (This is the main entry point for these things)
264  */
265 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
266         icalcomponent *cal;
267
268         cal = icalcomponent_new_from_string(part_source);
269
270         if (cal == NULL) {
271                 wprintf("Error parsing calendar object: %s<BR>\n",
272                         icalerror_strerror(icalerrno));
273                 return;
274         }
275
276         cal_process_object(cal, 0, msgnum, cal_partnum);
277
278         /* Free the memory we obtained from libical's constructor */
279         icalcomponent_free(cal);
280 }
281
282
283
284
285 /*
286  * Respond to a meeting request
287  */
288 void respond_to_request(void) {
289         char buf[SIZ];
290
291         output_headers(3);
292
293         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
294                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
295                 "<B>Respond to meeting request</B>"
296                 "</FONT></TD></TR></TABLE><BR>\n"
297         );
298
299         serv_printf("ICAL respond|%s|%s|%s|",
300                 bstr("msgnum"),
301                 bstr("cal_partnum"),
302                 bstr("sc")
303         );
304         serv_gets(buf);
305
306         if (buf[0] == '2') {
307                 wprintf("<TABLE BORDER=0><TR><TD>"
308                         "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
309                         "</TD><TD>"
310                 );
311                 if (!strcasecmp(bstr("sc"), "accept")) {
312                         wprintf("You have accepted this meeting invitation.  "
313                                 "It has been entered into your calendar, "
314                         );
315                 } else if (!strcasecmp(bstr("sc"), "tentative")) {
316                         wprintf("You have tentatively accepted this meeting invitation.  "
317                                 "It has been 'pencilled in' to your calendar, "
318                         );
319                 } else if (!strcasecmp(bstr("sc"), "decline")) {
320                         wprintf("You have declined this meeting invitation.  "
321                                 "It has <b>not</b> been entered into your calendar, "
322                         );
323                 }
324                 wprintf("and a reply has been sent to the meeting organizer."
325                         "</TD></TR></TABLE>\n"
326                 );
327         } else {
328                 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
329                         "%s\n", &buf[4]);
330         }
331
332         wprintf("<A HREF=\"/dotskip?room=");
333         urlescputs(WC->wc_roomname);
334         wprintf("\">Return to messages</A><BR>\n");
335
336         wDumpContent(1);
337 }
338
339
340
341
342 /*****************************************************************************/
343
344
345
346 /*
347  * Display handlers for message reading
348  */
349
350
351
352 /*
353  * If we're reading calendar items, just store them for now.  We have to
354  * sort and re-output them later when we draw the calendar.
355  */
356 void display_individual_cal(icalcomponent *cal, long msgnum) {
357
358         WC->num_cal += 1;
359
360         WC->disp_cal = realloc(WC->disp_cal,
361                         (sizeof(icalcomponent *) * WC->num_cal) );
362         WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
363
364         WC->cal_msgnum = realloc(WC->cal_msgnum,
365                         (sizeof(long) * WC->num_cal) );
366         WC->cal_msgnum[WC->num_cal - 1] = msgnum;
367 }
368
369
370
371 /*
372  * Display a task in the task list
373  */
374 void display_individual_task(icalcomponent *vtodo, long msgnum) {
375         icalproperty *p;
376
377         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
378         wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
379         if (p != NULL) {
380                 escputs((char *)icalproperty_get_comment(p));
381         }
382         wprintf("</A>\n");
383 }
384
385
386 /*
387  * Display a task by itself (for editing)
388  */
389 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
390         icalcomponent *vtodo;
391         icalproperty *p;
392         struct icaltimetype t;
393         time_t now;
394         int created_new_vtodo = 0;
395
396         now = time(NULL);
397
398         if (supplied_vtodo != NULL) {
399                 vtodo = supplied_vtodo;
400         }
401         else {
402                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
403                 created_new_vtodo = 1;
404         }
405
406         output_headers(3);
407         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
408                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
409                 "<B>Edit task</B>"
410                 "</FONT></TD></TR></TABLE><BR>\n"
411         );
412
413         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
414         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
415                 msgnum);
416
417         wprintf("Summary: "
418                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
419                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
420         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
421         if (p != NULL) {
422                 escputs((char *)icalproperty_get_comment(p));
423         }
424         wprintf("\"><BR>\n");
425
426         wprintf("Start date: ");
427         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
428         if (p != NULL) {
429                 t = icalproperty_get_dtstart(p);
430         }
431         else {
432                 t = icaltime_from_timet(now, 0);
433         }
434         display_icaltimetype_as_webform(&t, "dtstart");
435         wprintf("<BR>\n");
436
437         wprintf("Due date: ");
438         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
439         if (p != NULL) {
440                 t = icalproperty_get_due(p);
441         }
442         else {
443                 t = icaltime_from_timet(now, 0);
444         }
445         display_icaltimetype_as_webform(&t, "due");
446         wprintf("<BR>\n");
447
448         wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
449                 "ROWS=10 COLS=80 WIDTH=80>\n"
450         );
451         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
452         if (p != NULL) {
453                 escputs((char *)icalproperty_get_comment(p));
454         }
455         wprintf("</TEXTAREA><BR>\n");
456
457         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
458                 "&nbsp;&nbsp;"
459                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
460                 "&nbsp;&nbsp;"
461                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
462                 "</CENTER>\n"
463         );
464
465         wprintf("</FORM>\n");
466
467         wDumpContent(1);
468
469         if (created_new_vtodo) {
470                 icalcomponent_free(vtodo);
471         }
472 }
473
474 /*
475  * Save an edited task
476  */
477 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
478         char buf[SIZ];
479         int delete_existing = 0;
480         icalproperty *prop;
481         icalcomponent *vtodo;
482         int created_new_vtodo = 0;
483
484         if (supplied_vtodo != NULL) {
485                 vtodo = supplied_vtodo;
486         }
487         else {
488                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
489                 created_new_vtodo = 1;
490         }
491
492         if (!strcasecmp(bstr("sc"), "Save")) {
493
494                 /* Replace values in the component with ones from the form */
495
496                 while (prop = icalcomponent_get_first_property(vtodo,
497                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
498                         icalcomponent_remove_property(vtodo, prop);
499                 }
500                 icalcomponent_add_property(vtodo,
501                         icalproperty_new_summary(bstr("summary")));
502                 
503                 while (prop = icalcomponent_get_first_property(vtodo,
504                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
505                         icalcomponent_remove_property(vtodo, prop);
506                 }
507                 icalcomponent_add_property(vtodo,
508                         icalproperty_new_description(bstr("description")));
509         
510                 while (prop = icalcomponent_get_first_property(vtodo,
511                       ICAL_DTSTART_PROPERTY), prop != NULL) {
512                         icalcomponent_remove_property(vtodo, prop);
513                 }
514                 icalcomponent_add_property(vtodo,
515                         icalproperty_new_dtstart(
516                                 icaltime_from_webform("dtstart")
517                         )
518                 );
519         
520                 while (prop = icalcomponent_get_first_property(vtodo,
521                       ICAL_DUE_PROPERTY), prop != NULL) {
522                         icalcomponent_remove_property(vtodo, prop);
523                 }
524                 icalcomponent_add_property(vtodo,
525                         icalproperty_new_due(
526                                 icaltime_from_webform("due")
527                         )
528                 );
529         
530                 /* Serialize it and save it to the message base */
531                 serv_puts("ENT0 1|||4");
532                 serv_gets(buf);
533                 if (buf[0] == '4') {
534                         serv_puts("Content-type: text/calendar");
535                         serv_puts("");
536                         serv_puts(icalcomponent_as_ical_string(vtodo));
537                         serv_puts("000");
538
539                         /* Probably not necessary; the server will see the UID
540                          * of the object and delete the old one anyway, but
541                          * just in case...
542                          */
543                         delete_existing = 1;
544                 }
545         }
546
547         /*
548          * If the user clicked 'Delete' then explicitly delete the message.
549          */
550         if (!strcasecmp(bstr("sc"), "Delete")) {
551                 delete_existing = 1;
552         }
553
554         if ( (delete_existing) && (msgnum > 0L) ) {
555                 serv_printf("DELE %ld", atol(bstr("msgnum")));
556                 serv_gets(buf);
557         }
558
559         if (created_new_vtodo) {
560                 icalcomponent_free(vtodo);
561         }
562
563         /* Go back to the task list */
564         readloop("readfwd");
565 }
566
567
568
569 /*
570  * Code common to all display handlers.  Given a message number and a MIME
571  * type, we load the message and hunt for that MIME type.  If found, we load
572  * the relevant part, deserialize it into a libical component, filter it for
573  * the requested object type, and feed it to the specified handler.
574  */
575 void display_using_handler(long msgnum,
576                         char *mimetype,
577                         icalcomponent_kind which_kind,
578                         void (*callback)(icalcomponent *, long)
579         ) {
580         char buf[SIZ];
581         char mime_partnum[SIZ];
582         char mime_filename[SIZ];
583         char mime_content_type[SIZ];
584         char mime_disposition[SIZ];
585         int mime_length;
586         char relevant_partnum[SIZ];
587         char *relevant_source = NULL;
588         icalcomponent *cal, *c;
589
590         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
591         serv_puts(buf);
592         serv_gets(buf);
593         if (buf[0] != '1') return;
594
595         while (serv_gets(buf), strcmp(buf, "000")) {
596                 if (!strncasecmp(buf, "part=", 5)) {
597                         extract(mime_filename, &buf[5], 1);
598                         extract(mime_partnum, &buf[5], 2);
599                         extract(mime_disposition, &buf[5], 3);
600                         extract(mime_content_type, &buf[5], 4);
601                         mime_length = extract_int(&buf[5], 5);
602
603                         if (!strcasecmp(mime_content_type, "text/calendar")) {
604                                 strcpy(relevant_partnum, mime_partnum);
605                         }
606
607                 }
608         }
609
610         if (strlen(relevant_partnum) > 0) {
611                 relevant_source = load_mimepart(msgnum, relevant_partnum);
612                 if (relevant_source != NULL) {
613
614                         cal = icalcomponent_new_from_string(relevant_source);
615                         if (cal != NULL) {
616
617                                 /* Simple components of desired type */
618                                 if (icalcomponent_isa(cal) == which_kind) {
619                                         callback(cal, msgnum);
620                                 }
621
622                                 /* Subcomponents of desired type */
623                                 for (c = icalcomponent_get_first_component(cal,
624                                     which_kind);
625                                     (c != 0);
626                                     c = icalcomponent_get_next_component(cal,
627                                     which_kind)) {
628                                         callback(c, msgnum);
629                                 }
630                                 icalcomponent_free(cal);
631                         }
632                         free(relevant_source);
633                 }
634         }
635
636 }
637
638 void display_calendar(long msgnum) {
639         display_using_handler(msgnum, "text/calendar",
640                                 ICAL_VEVENT_COMPONENT,
641                                 display_individual_cal);
642 }
643
644 void display_task(long msgnum) {
645         display_using_handler(msgnum, "text/calendar",
646                                 ICAL_VTODO_COMPONENT,
647                                 display_individual_task);
648 }
649
650 void display_edit_task(void) {
651         long msgnum = 0L;
652
653         msgnum = atol(bstr("msgnum"));
654         if (msgnum > 0L) {
655                 /* existing task */
656                 display_using_handler(msgnum, "text/calendar",
657                                 ICAL_VTODO_COMPONENT,
658                                 display_edit_individual_task);
659         }
660         else {
661                 /* new task */
662                 display_edit_individual_task(NULL, 0L);
663         }
664 }
665
666 void save_task(void) {
667         long msgnum = 0L;
668
669         msgnum = atol(bstr("msgnum"));
670         if (msgnum > 0L) {
671                 display_using_handler(msgnum, "text/calendar",
672                                 ICAL_VTODO_COMPONENT,
673                                 save_individual_task);
674         }
675         else {
676                 save_individual_task(NULL, 0L);
677         }
678 }
679
680 void display_edit_event(void) {
681         long msgnum = 0L;
682
683         msgnum = atol(bstr("msgnum"));
684         if (msgnum > 0L) {
685                 /* existing event */
686                 display_using_handler(msgnum, "text/calendar",
687                                 ICAL_VEVENT_COMPONENT,
688                                 display_edit_individual_event);
689         }
690         else {
691                 /* new event */
692                 display_edit_individual_event(NULL, 0L);
693         }
694 }
695
696 void save_event(void) {
697         long msgnum = 0L;
698
699         msgnum = atol(bstr("msgnum"));
700
701         if (msgnum > 0L) {
702                 display_using_handler(msgnum, "text/calendar",
703                                 ICAL_VEVENT_COMPONENT,
704                                 save_individual_event);
705         }
706         else {
707                 save_individual_event(NULL, 0L);
708         }
709 }
710
711 #endif /* HAVE_ICAL_H */