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