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