]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* Started some "me as organizer" stuff. (BROKEN BUILD ... I'll fix soon.)
[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                         icalproperty_free(prop);
500                 }
501                 icalcomponent_add_property(vtodo,
502                         icalproperty_new_summary(bstr("summary")));
503                 
504                 while (prop = icalcomponent_get_first_property(vtodo,
505                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
506                         icalcomponent_remove_property(vtodo, prop);
507                         icalproperty_free(prop);
508                 }
509                 icalcomponent_add_property(vtodo,
510                         icalproperty_new_description(bstr("description")));
511         
512                 while (prop = icalcomponent_get_first_property(vtodo,
513                       ICAL_DTSTART_PROPERTY), prop != NULL) {
514                         icalcomponent_remove_property(vtodo, prop);
515                         icalproperty_free(prop);
516                 }
517                 icalcomponent_add_property(vtodo,
518                         icalproperty_new_dtstart(
519                                 icaltime_from_webform("dtstart")
520                         )
521                 );
522         
523                 while (prop = icalcomponent_get_first_property(vtodo,
524                       ICAL_DUE_PROPERTY), prop != NULL) {
525                         icalcomponent_remove_property(vtodo, prop);
526                         icalproperty_free(prop);
527                 }
528                 icalcomponent_add_property(vtodo,
529                         icalproperty_new_due(
530                                 icaltime_from_webform("due")
531                         )
532                 );
533         
534                 /* Serialize it and save it to the message base */
535                 serv_puts("ENT0 1|||4");
536                 serv_gets(buf);
537                 if (buf[0] == '4') {
538                         serv_puts("Content-type: text/calendar");
539                         serv_puts("");
540                         serv_puts(icalcomponent_as_ical_string(vtodo));
541                         serv_puts("000");
542
543                         /* Probably not necessary; the server will see the UID
544                          * of the object and delete the old one anyway, but
545                          * just in case...
546                          */
547                         delete_existing = 1;
548                 }
549         }
550
551         /*
552          * If the user clicked 'Delete' then explicitly delete the message.
553          */
554         if (!strcasecmp(bstr("sc"), "Delete")) {
555                 delete_existing = 1;
556         }
557
558         if ( (delete_existing) && (msgnum > 0L) ) {
559                 serv_printf("DELE %ld", atol(bstr("msgnum")));
560                 serv_gets(buf);
561         }
562
563         if (created_new_vtodo) {
564                 icalcomponent_free(vtodo);
565         }
566
567         /* Go back to the task list */
568         readloop("readfwd");
569 }
570
571
572
573 /*
574  * Code common to all display handlers.  Given a message number and a MIME
575  * type, we load the message and hunt for that MIME type.  If found, we load
576  * the relevant part, deserialize it into a libical component, filter it for
577  * the requested object type, and feed it to the specified handler.
578  */
579 void display_using_handler(long msgnum,
580                         char *mimetype,
581                         icalcomponent_kind which_kind,
582                         void (*callback)(icalcomponent *, long)
583         ) {
584         char buf[SIZ];
585         char mime_partnum[SIZ];
586         char mime_filename[SIZ];
587         char mime_content_type[SIZ];
588         char mime_disposition[SIZ];
589         int mime_length;
590         char relevant_partnum[SIZ];
591         char *relevant_source = NULL;
592         icalcomponent *cal, *c;
593
594         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
595         serv_puts(buf);
596         serv_gets(buf);
597         if (buf[0] != '1') return;
598
599         while (serv_gets(buf), strcmp(buf, "000")) {
600                 if (!strncasecmp(buf, "part=", 5)) {
601                         extract(mime_filename, &buf[5], 1);
602                         extract(mime_partnum, &buf[5], 2);
603                         extract(mime_disposition, &buf[5], 3);
604                         extract(mime_content_type, &buf[5], 4);
605                         mime_length = extract_int(&buf[5], 5);
606
607                         if (!strcasecmp(mime_content_type, "text/calendar")) {
608                                 strcpy(relevant_partnum, mime_partnum);
609                         }
610
611                 }
612         }
613
614         if (strlen(relevant_partnum) > 0) {
615                 relevant_source = load_mimepart(msgnum, relevant_partnum);
616                 if (relevant_source != NULL) {
617
618                         cal = icalcomponent_new_from_string(relevant_source);
619                         if (cal != NULL) {
620
621                                 /* Simple components of desired type */
622                                 if (icalcomponent_isa(cal) == which_kind) {
623                                         callback(cal, msgnum);
624                                 }
625
626                                 /* Subcomponents of desired type */
627                                 for (c = icalcomponent_get_first_component(cal,
628                                     which_kind);
629                                     (c != 0);
630                                     c = icalcomponent_get_next_component(cal,
631                                     which_kind)) {
632                                         callback(c, msgnum);
633                                 }
634                                 icalcomponent_free(cal);
635                         }
636                         free(relevant_source);
637                 }
638         }
639
640 }
641
642 void display_calendar(long msgnum) {
643         display_using_handler(msgnum, "text/calendar",
644                                 ICAL_VEVENT_COMPONENT,
645                                 display_individual_cal);
646 }
647
648 void display_task(long msgnum) {
649         display_using_handler(msgnum, "text/calendar",
650                                 ICAL_VTODO_COMPONENT,
651                                 display_individual_task);
652 }
653
654 void display_edit_task(void) {
655         long msgnum = 0L;
656
657         msgnum = atol(bstr("msgnum"));
658         if (msgnum > 0L) {
659                 /* existing task */
660                 display_using_handler(msgnum, "text/calendar",
661                                 ICAL_VTODO_COMPONENT,
662                                 display_edit_individual_task);
663         }
664         else {
665                 /* new task */
666                 display_edit_individual_task(NULL, 0L);
667         }
668 }
669
670 void save_task(void) {
671         long msgnum = 0L;
672
673         msgnum = atol(bstr("msgnum"));
674         if (msgnum > 0L) {
675                 display_using_handler(msgnum, "text/calendar",
676                                 ICAL_VTODO_COMPONENT,
677                                 save_individual_task);
678         }
679         else {
680                 save_individual_task(NULL, 0L);
681         }
682 }
683
684 void display_edit_event(void) {
685         long msgnum = 0L;
686
687         msgnum = atol(bstr("msgnum"));
688         if (msgnum > 0L) {
689                 /* existing event */
690                 display_using_handler(msgnum, "text/calendar",
691                                 ICAL_VEVENT_COMPONENT,
692                                 display_edit_individual_event);
693         }
694         else {
695                 /* new event */
696                 display_edit_individual_event(NULL, 0L);
697         }
698 }
699
700 void save_event(void) {
701         long msgnum = 0L;
702
703         msgnum = atol(bstr("msgnum"));
704
705         if (msgnum > 0L) {
706                 display_using_handler(msgnum, "text/calendar",
707                                 ICAL_VEVENT_COMPONENT,
708                                 save_individual_event);
709         }
710         else {
711                 save_individual_event(NULL, 0L);
712         }
713 }
714
715 #endif /* HAVE_ICAL_H */