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