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