]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* Detect when a meeting invitation is actually an update for an existing UID
[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         escputs(buf);
294
295         wDumpContent(1);
296 }
297
298
299
300
301 /*****************************************************************************/
302
303
304
305 /*
306  * Display handlers for message reading
307  */
308
309
310
311 /*
312  * If we're reading calendar items, just store them for now.  We have to
313  * sort and re-output them later when we draw the calendar.
314  */
315 void display_individual_cal(icalcomponent *cal, long msgnum) {
316
317         WC->num_cal += 1;
318
319         WC->disp_cal = realloc(WC->disp_cal,
320                         (sizeof(icalcomponent *) * WC->num_cal) );
321         WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
322
323         WC->cal_msgnum = realloc(WC->cal_msgnum,
324                         (sizeof(long) * WC->num_cal) );
325         WC->cal_msgnum[WC->num_cal - 1] = msgnum;
326 }
327
328
329
330 /*
331  * Display a task in the task list
332  */
333 void display_individual_task(icalcomponent *vtodo, long msgnum) {
334         icalproperty *p;
335
336         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
337         wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
338         if (p != NULL) {
339                 escputs((char *)icalproperty_get_comment(p));
340         }
341         wprintf("</A>\n");
342 }
343
344
345 /*
346  * Display a task by itself (for editing)
347  */
348 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
349         icalcomponent *vtodo;
350         icalproperty *p;
351         struct icaltimetype t;
352         time_t now;
353         int created_new_vtodo = 0;
354
355         now = time(NULL);
356
357         if (supplied_vtodo != NULL) {
358                 vtodo = supplied_vtodo;
359         }
360         else {
361                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
362                 created_new_vtodo = 1;
363         }
364
365         output_headers(3);
366         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
367                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
368                 "<B>Edit task</B>"
369                 "</FONT></TD></TR></TABLE><BR>\n"
370         );
371
372         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
373         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
374                 msgnum);
375
376         wprintf("Summary: "
377                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
378                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
379         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
380         if (p != NULL) {
381                 escputs((char *)icalproperty_get_comment(p));
382         }
383         wprintf("\"><BR>\n");
384
385         wprintf("Start date: ");
386         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
387         if (p != NULL) {
388                 t = icalproperty_get_dtstart(p);
389         }
390         else {
391                 t = icaltime_from_timet(now, 0);
392         }
393         display_icaltimetype_as_webform(&t, "dtstart");
394         wprintf("<BR>\n");
395
396         wprintf("Due date: ");
397         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
398         if (p != NULL) {
399                 t = icalproperty_get_due(p);
400         }
401         else {
402                 t = icaltime_from_timet(now, 0);
403         }
404         display_icaltimetype_as_webform(&t, "due");
405         wprintf("<BR>\n");
406
407         wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
408                 "ROWS=10 COLS=80 WIDTH=80>\n"
409         );
410         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
411         if (p != NULL) {
412                 escputs((char *)icalproperty_get_comment(p));
413         }
414         wprintf("</TEXTAREA><BR>\n");
415
416         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
417                 "&nbsp;&nbsp;"
418                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
419                 "&nbsp;&nbsp;"
420                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
421                 "</CENTER>\n"
422         );
423
424         wprintf("</FORM>\n");
425
426         wDumpContent(1);
427
428         if (created_new_vtodo) {
429                 icalcomponent_free(vtodo);
430         }
431 }
432
433 /*
434  * Save an edited task
435  */
436 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
437         char buf[SIZ];
438         int delete_existing = 0;
439         icalproperty *prop;
440         icalcomponent *vtodo;
441         int created_new_vtodo = 0;
442
443         if (supplied_vtodo != NULL) {
444                 vtodo = supplied_vtodo;
445         }
446         else {
447                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
448                 created_new_vtodo = 1;
449         }
450
451         if (!strcasecmp(bstr("sc"), "Save")) {
452
453                 /* Replace values in the component with ones from the form */
454
455                 while (prop = icalcomponent_get_first_property(vtodo,
456                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
457                         icalcomponent_remove_property(vtodo, prop);
458                 }
459                 icalcomponent_add_property(vtodo,
460                         icalproperty_new_summary(bstr("summary")));
461                 
462                 while (prop = icalcomponent_get_first_property(vtodo,
463                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
464                         icalcomponent_remove_property(vtodo, prop);
465                 }
466                 icalcomponent_add_property(vtodo,
467                         icalproperty_new_description(bstr("description")));
468         
469                 while (prop = icalcomponent_get_first_property(vtodo,
470                       ICAL_DTSTART_PROPERTY), prop != NULL) {
471                         icalcomponent_remove_property(vtodo, prop);
472                 }
473                 icalcomponent_add_property(vtodo,
474                         icalproperty_new_dtstart(
475                                 icaltime_from_webform("dtstart")
476                         )
477                 );
478         
479                 while (prop = icalcomponent_get_first_property(vtodo,
480                       ICAL_DUE_PROPERTY), prop != NULL) {
481                         icalcomponent_remove_property(vtodo, prop);
482                 }
483                 icalcomponent_add_property(vtodo,
484                         icalproperty_new_due(
485                                 icaltime_from_webform("due")
486                         )
487                 );
488         
489                 /* Serialize it and save it to the message base */
490                 serv_puts("ENT0 1|||4");
491                 serv_gets(buf);
492                 if (buf[0] == '4') {
493                         serv_puts("Content-type: text/calendar");
494                         serv_puts("");
495                         serv_puts(icalcomponent_as_ical_string(vtodo));
496                         serv_puts("000");
497
498                         /* Probably not necessary; the server will see the UID
499                          * of the object and delete the old one anyway, but
500                          * just in case...
501                          */
502                         delete_existing = 1;
503                 }
504         }
505
506         /*
507          * If the user clicked 'Delete' then explicitly delete the message.
508          */
509         if (!strcasecmp(bstr("sc"), "Delete")) {
510                 delete_existing = 1;
511         }
512
513         if ( (delete_existing) && (msgnum > 0L) ) {
514                 serv_printf("DELE %ld", atol(bstr("msgnum")));
515                 serv_gets(buf);
516         }
517
518         if (created_new_vtodo) {
519                 icalcomponent_free(vtodo);
520         }
521
522         /* Go back to the task list */
523         readloop("readfwd");
524 }
525
526
527
528 /*
529  * Code common to all display handlers.  Given a message number and a MIME
530  * type, we load the message and hunt for that MIME type.  If found, we load
531  * the relevant part, deserialize it into a libical component, filter it for
532  * the requested object type, and feed it to the specified handler.
533  */
534 void display_using_handler(long msgnum,
535                         char *mimetype,
536                         icalcomponent_kind which_kind,
537                         void (*callback)(icalcomponent *, long)
538         ) {
539         char buf[SIZ];
540         char mime_partnum[SIZ];
541         char mime_filename[SIZ];
542         char mime_content_type[SIZ];
543         char mime_disposition[SIZ];
544         int mime_length;
545         char relevant_partnum[SIZ];
546         char *relevant_source = NULL;
547         icalcomponent *cal, *c;
548
549         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
550         serv_puts(buf);
551         serv_gets(buf);
552         if (buf[0] != '1') return;
553
554         while (serv_gets(buf), strcmp(buf, "000")) {
555                 if (!strncasecmp(buf, "part=", 5)) {
556                         extract(mime_filename, &buf[5], 1);
557                         extract(mime_partnum, &buf[5], 2);
558                         extract(mime_disposition, &buf[5], 3);
559                         extract(mime_content_type, &buf[5], 4);
560                         mime_length = extract_int(&buf[5], 5);
561
562                         if (!strcasecmp(mime_content_type, "text/calendar")) {
563                                 strcpy(relevant_partnum, mime_partnum);
564                         }
565
566                 }
567         }
568
569         if (strlen(relevant_partnum) > 0) {
570                 relevant_source = load_mimepart(msgnum, relevant_partnum);
571                 if (relevant_source != NULL) {
572
573                         cal = icalcomponent_new_from_string(relevant_source);
574                         if (cal != NULL) {
575
576                                 /* Simple components of desired type */
577                                 if (icalcomponent_isa(cal) == which_kind) {
578                                         callback(cal, msgnum);
579                                 }
580
581                                 /* Subcomponents of desired type */
582                                 for (c = icalcomponent_get_first_component(cal,
583                                     which_kind);
584                                     (c != 0);
585                                     c = icalcomponent_get_next_component(cal,
586                                     which_kind)) {
587                                         callback(c, msgnum);
588                                 }
589                                 icalcomponent_free(cal);
590                         }
591                         free(relevant_source);
592                 }
593         }
594
595 }
596
597 void display_calendar(long msgnum) {
598         display_using_handler(msgnum, "text/calendar",
599                                 ICAL_VEVENT_COMPONENT,
600                                 display_individual_cal);
601 }
602
603 void display_task(long msgnum) {
604         display_using_handler(msgnum, "text/calendar",
605                                 ICAL_VTODO_COMPONENT,
606                                 display_individual_task);
607 }
608
609 void display_edit_task(void) {
610         long msgnum = 0L;
611
612         msgnum = atol(bstr("msgnum"));
613         if (msgnum > 0L) {
614                 /* existing task */
615                 display_using_handler(msgnum, "text/calendar",
616                                 ICAL_VTODO_COMPONENT,
617                                 display_edit_individual_task);
618         }
619         else {
620                 /* new task */
621                 display_edit_individual_task(NULL, 0L);
622         }
623 }
624
625 void save_task(void) {
626         long msgnum = 0L;
627
628         msgnum = atol(bstr("msgnum"));
629         if (msgnum > 0L) {
630                 display_using_handler(msgnum, "text/calendar",
631                                 ICAL_VTODO_COMPONENT,
632                                 save_individual_task);
633         }
634         else {
635                 save_individual_task(NULL, 0L);
636         }
637 }
638
639 void display_edit_event(void) {
640         long msgnum = 0L;
641
642         msgnum = atol(bstr("msgnum"));
643         if (msgnum > 0L) {
644                 /* existing event */
645                 display_using_handler(msgnum, "text/calendar",
646                                 ICAL_VEVENT_COMPONENT,
647                                 display_edit_individual_event);
648         }
649         else {
650                 /* new event */
651                 display_edit_individual_event(NULL, 0L);
652         }
653 }
654
655 void save_event(void) {
656         long msgnum = 0L;
657
658         msgnum = atol(bstr("msgnum"));
659
660         if (msgnum > 0L) {
661                 display_using_handler(msgnum, "text/calendar",
662                                 ICAL_VEVENT_COMPONENT,
663                                 save_individual_event);
664         }
665         else {
666                 save_individual_event(NULL, 0L);
667         }
668 }
669
670 #endif /* HAVE_ICAL_H */