]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* Tasks view now uses the same buffer/dump logic as the calendar views, so
[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 WEBCIT_WITH_CALENDAR_SERVICE
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 /* WEBCIT_WITH_CALENDAR_SERVICE */
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  */
75 void cal_process_object(icalcomponent *cal,
76                         int recursion_level,
77                         long msgnum,
78                         char *cal_partnum
79 ) {
80         icalcomponent *c;
81         icalproperty *method = NULL;
82         icalproperty_method the_method = ICAL_METHOD_NONE;
83         icalproperty *p;
84         struct icaltimetype t;
85         time_t tt;
86         char buf[SIZ];
87         char conflict_name[SIZ];
88         int is_update = 0;
89
90         /* Leading HTML for the display of this object */
91         if (recursion_level == 0) {
92                 wprintf("<CENTER><TABLE border=0>\n");
93         }
94
95         /* Look for a method */
96         method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
97
98         /* See what we need to do with this */
99         if (method != NULL) {
100                 the_method = icalproperty_get_method(method);
101                 switch(the_method) {
102                     case ICAL_METHOD_REQUEST:
103                         wprintf("<TR><TD COLSPAN=2>\n"
104                                 "<IMG ALIGN=CENTER "
105                                 "SRC=\"/static/vcalendar.gif\">"
106                                 "&nbsp;&nbsp;"  
107                                 "<B>Meeting invitation</B>"
108                                 "</TD></TR>\n"
109                         );
110                         break;
111                     case ICAL_METHOD_REPLY:
112                         wprintf("<TR><TD COLSPAN=2>\n"
113                                 "<IMG ALIGN=CENTER "
114                                 "SRC=\"/static/vcalendar.gif\">"
115                                 "&nbsp;&nbsp;"  
116                                 "<B>Attendee's reply to your invitation</B>"
117                                 "</TD></TR>\n"
118                         );
119                         break;
120                     case ICAL_METHOD_PUBLISH:
121                         wprintf("<TR><TD COLSPAN=2>\n"
122                                 "<IMG ALIGN=CENTER "
123                                 "SRC=\"/static/vcalendar.gif\">"
124                                 "&nbsp;&nbsp;"  
125                                 "<B>Published event</B>"
126                                 "</TD></TR>\n"
127                         );
128                         break;
129                     default:
130                         wprintf("<TR><TD COLSPAN=2>"
131                                 "I don't know what to do with this.</TD></TR>"
132                                 "\n");
133                         break;
134                 }
135         }
136
137         p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
138         if (p != NULL) {
139                 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
140                 escputs((char *)icalproperty_get_comment(p));
141                 wprintf("</TD></TR>\n");
142         }
143
144         p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
145         if (p != NULL) {
146                 wprintf("<TR><TD><B>Location:</B></TD><TD>");
147                 escputs((char *)icalproperty_get_comment(p));
148                 wprintf("</TD></TR>\n");
149         }
150
151         /*
152          * Only show start/end times if we're actually looking at the VEVENT
153          * component.  Otherwise it shows bogus dates for things like timezone.
154          */
155         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
156
157                 p = icalcomponent_get_first_property(cal,
158                                                 ICAL_DTSTART_PROPERTY);
159                 if (p != NULL) {
160                         t = icalproperty_get_dtstart(p);
161
162                         if (t.is_date) {
163                                 wprintf("<TR><TD><B>Date:"
164                                         "</B></TD><TD>"
165                                         "%s %d, %d</TD></TR>",
166                                         months[t.month - 1],
167                                         t.day, t.year
168                                 );
169                         }
170                         else {
171                                 tt = icaltime_as_timet(t);
172                                 fmt_date(buf, tt);
173                                 wprintf("<TR><TD><B>Starting date/time:"
174                                         "</B></TD><TD>"
175                                         "%s</TD></TR>", buf
176                                 );
177                         }
178                 }
179         
180                 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
181                 if (p != NULL) {
182                         t = icalproperty_get_dtend(p);
183                         tt = icaltime_as_timet(t);
184                         fmt_date(buf, tt);
185                         wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
186                                 "%s</TD></TR>", buf
187                         );
188                 }
189
190         }
191
192         p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
193         if (p != NULL) {
194                 wprintf("<TR><TD><B>Description:</B></TD><TD>");
195                 escputs((char *)icalproperty_get_comment(p));
196                 wprintf("</TD></TR>\n");
197         }
198
199         /* If the component has attendees, iterate through them. */
200         for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
201                 wprintf("<TR><TD><B>Attendee:</B></TD><TD>");
202                 strcpy(buf, icalproperty_get_attendee(p));
203                 if (!strncasecmp(buf, "MAILTO:", 7)) {
204
205                         /* screen name or email address */
206                         strcpy(buf, &buf[7]);
207                         striplt(buf);
208                         escputs(buf);
209                         wprintf(" ");
210
211                         /* participant status */
212                         partstat_as_string(buf, p);
213                         escputs(buf);
214                 }
215                 wprintf("</TD></TR>\n");
216         }
217
218         /* If the component has subcomponents, recurse through them. */
219         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
220             (c != 0);
221             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
222                 /* Recursively process subcomponent */
223                 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
224         }
225
226         /* If this is a REQUEST, display conflicts and buttons */
227         if (the_method == ICAL_METHOD_REQUEST) {
228
229                 /* Check for conflicts */
230                 lprintf(9, "Checking server calendar for conflicts...\n");
231                 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
232                 serv_gets(buf);
233                 if (buf[0] == '1') {
234                         while (serv_gets(buf), strcmp(buf, "000")) {
235                                 extract(conflict_name, buf, 3);
236                                 is_update = extract_int(buf, 4);
237                                 wprintf("<TR><TD><B><I>%s</I></B></TD>"
238                                         "<TD>"
239                                         "%s "
240                                         "<I>&quot;",
241
242                                         (is_update ?
243                                                 "Update:" :
244                                                 "CONFLICT:"
245                                         ),
246
247                                         (is_update ?
248                                                 "This is an update of" :
249                                                 "This event would conflict with"
250                                         )
251                 
252                                 );
253                                 escputs(conflict_name);
254                                 wprintf("&quot;</I> "
255                                         "which is already in your calendar."
256                                         "</TD></TR>\n");
257                         }
258                 }
259                 lprintf(9, "...done.\n");
260
261                 /* Display the Accept/Decline buttons */
262                 wprintf("<TR><TD COLSPAN=2>"
263                         "<FORM METHOD=\"GET\" "
264                         "ACTION=\"/respond_to_request\">\n"
265                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
266                                 "VALUE=\"Accept\">\n"
267                         "&nbsp;&nbsp;"
268                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
269                                 "VALUE=\"Tentative\">\n"
270                         "&nbsp;&nbsp;"
271                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
272                                 "VALUE=\"Decline\">\n"
273                         "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
274                                 "VALUE=\"%ld\">"
275                         "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
276                                 "VALUE=\"%s\">"
277                         "</FORM>"
278                         "</TD></TR>\n",
279                         msgnum, cal_partnum
280                 );
281
282         }
283
284         /* If this is a REPLY, display update button */
285         if (the_method == ICAL_METHOD_REPLY) {
286
287                 /***********
288                  * In the future, if we want to validate this object before
289                  * continuing, we can do it this way:
290                 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
291                 serv_gets(buf);
292                 }
293                  ***********/
294
295                 /* Display the update buttons */
296                 wprintf("<TR><TD COLSPAN=2>"
297                         "Click <i>Update</i> to accept this reply and "
298                         "update your calendar."
299                         "<FORM METHOD=\"GET\" "
300                         "ACTION=\"/handle_rsvp\">\n"
301                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
302                                 "VALUE=\"Update\">\n"
303                         "&nbsp;&nbsp;"
304                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
305                                 "VALUE=\"Ignore\">\n"
306                         "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
307                                 "VALUE=\"%ld\">"
308                         "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
309                                 "VALUE=\"%s\">"
310                         "</FORM>"
311                         "</TD></TR>\n",
312                         msgnum, cal_partnum
313                 );
314
315         }
316
317         /* Trailing HTML for the display of this object */
318         if (recursion_level == 0) {
319
320                 wprintf("</TR></TABLE></CENTER>\n");
321         }
322 }
323
324
325 /*
326  * Deserialize a calendar object in a message so it can be processed.
327  * (This is the main entry point for these things)
328  */
329 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
330         icalcomponent *cal;
331
332         cal = icalcomponent_new_from_string(part_source);
333
334         if (cal == NULL) {
335                 wprintf("Error parsing calendar object<BR>\n");
336                 return;
337         }
338
339         ical_dezonify(cal);
340         cal_process_object(cal, 0, msgnum, cal_partnum);
341
342         /* Free the memory we obtained from libical's constructor */
343         icalcomponent_free(cal);
344 }
345
346
347
348
349 /*
350  * Respond to a meeting request
351  */
352 void respond_to_request(void) {
353         char buf[SIZ];
354
355         output_headers(3);
356
357         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
358                 "<SPAN CLASS=\"titlebar\">Respond to meeting request</SPAN>"
359                 "</TD></TR></TABLE><BR>\n"
360         );
361
362         serv_printf("ICAL respond|%s|%s|%s|",
363                 bstr("msgnum"),
364                 bstr("cal_partnum"),
365                 bstr("sc")
366         );
367         serv_gets(buf);
368
369         if (buf[0] == '2') {
370                 wprintf("<TABLE BORDER=0><TR><TD>"
371                         "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
372                         "</TD><TD>"
373                 );
374                 if (!strcasecmp(bstr("sc"), "accept")) {
375                         wprintf("You have accepted this meeting invitation.  "
376                                 "It has been entered into your calendar, "
377                         );
378                 } else if (!strcasecmp(bstr("sc"), "tentative")) {
379                         wprintf("You have tentatively accepted this meeting invitation.  "
380                                 "It has been 'pencilled in' to your calendar, "
381                         );
382                 } else if (!strcasecmp(bstr("sc"), "decline")) {
383                         wprintf("You have declined this meeting invitation.  "
384                                 "It has <b>not</b> been entered into your calendar, "
385                         );
386                 }
387                 wprintf("and a reply has been sent to the meeting organizer."
388                         "</TD></TR></TABLE>\n"
389                 );
390         } else {
391                 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
392                         "%s\n", &buf[4]);
393         }
394
395         wprintf("<A HREF=\"/dotskip?room=");
396         urlescputs(WC->wc_roomname);
397         wprintf("\"><BR>Return to messages</A><BR>\n");
398
399         wDumpContent(1);
400 }
401
402
403
404 /*
405  * Handle an incoming RSVP
406  */
407 void handle_rsvp(void) {
408         char buf[SIZ];
409
410         output_headers(3);
411
412         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
413                 "<SPAN CLASS=\"titlebar\">"
414                 "Update your calendar with this RSVP</SPAN>"
415                 "</TD></TR></TABLE><BR>\n"
416         );
417
418         serv_printf("ICAL handle_rsvp|%s|%s|%s|",
419                 bstr("msgnum"),
420                 bstr("cal_partnum"),
421                 bstr("sc")
422         );
423         serv_gets(buf);
424
425         if (buf[0] == '2') {
426                 wprintf("<TABLE BORDER=0><TR><TD>"
427                         "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
428                         "</TD><TD>"
429                 );
430                 if (!strcasecmp(bstr("sc"), "update")) {
431                         wprintf("Your calendar has been updated "
432                                 "to reflect this RSVP."
433                         );
434                 } else if (!strcasecmp(bstr("sc"), "ignore")) {
435                         wprintf("You have chosen to ignore this RSVP. "
436                                 "Your calendar has <b>not</b> been updated."
437                         );
438                 }
439                 wprintf("</TD></TR></TABLE>\n"
440                 );
441         } else {
442                 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
443                         "%s\n", &buf[4]);
444         }
445
446         wprintf("<A HREF=\"/dotskip?room=");
447         urlescputs(WC->wc_roomname);
448         wprintf("\"><BR>Return to messages</A><BR>\n");
449
450         wDumpContent(1);
451 }
452
453
454
455
456 /*****************************************************************************/
457
458
459
460 /*
461  * Display handlers for message reading
462  */
463
464
465
466 /*
467  * If we're reading calendar items, just store them for now.  We have to
468  * sort and re-output them later when we draw the calendar.
469  */
470 void display_individual_cal(icalcomponent *cal, long msgnum) {
471
472         WC->num_cal += 1;
473
474         WC->disp_cal = realloc(WC->disp_cal,
475                         (sizeof(icalcomponent *) * WC->num_cal) );
476         WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
477
478         WC->cal_msgnum = realloc(WC->cal_msgnum,
479                         (sizeof(long) * WC->num_cal) );
480         WC->cal_msgnum[WC->num_cal - 1] = msgnum;
481 }
482
483
484
485 /*
486  * Display a task by itself (for editing)
487  *
488  */
489 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
490         icalcomponent *vtodo;
491         icalproperty *p;
492         struct icaltimetype t;
493         time_t now;
494         int created_new_vtodo = 0;
495
496         now = time(NULL);
497
498         if (supplied_vtodo != NULL) {
499                 vtodo = supplied_vtodo;
500
501                 /* If we're looking at a fully encapsulated VCALENDAR
502                  * rather than a VTODO component, attempt to use the first
503                  * relevant VTODO subcomponent.  If there is none, the
504                  * NULL returned by icalcomponent_get_first_component() will
505                  * tell the next iteration of this function to create a
506                  * new one.
507                  */
508                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
509                         display_edit_individual_task(
510                                 icalcomponent_get_first_component(
511                                         vtodo, ICAL_VTODO_COMPONENT
512                                 ), msgnum
513                         );
514                         return;
515                 }
516         }
517         else {
518                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
519                 created_new_vtodo = 1;
520         }
521
522         output_headers(3);
523         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
524                 "<SPAN CLASS=\"titlebar\">Edit task</SPAN>"
525                 "</TD></TR></TABLE><BR>\n"
526         );
527         
528         do_template("beginbox_nt");
529
530         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
531         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
532                 msgnum);
533
534         wprintf("<TABLE border=0>\n");
535
536         wprintf("<TR><TD>Summary:</TD><TD>"
537                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
538                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
539         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
540         if (p != NULL) {
541                 escputs((char *)icalproperty_get_comment(p));
542         }
543         wprintf("\"></TD></TR>\n");
544
545         wprintf("<TR><TD>Start date:</TD><TD>");
546         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
547         if (p != NULL) {
548                 t = icalproperty_get_dtstart(p);
549         }
550         else {
551                 t = icaltime_from_timet(now, 0);
552         }
553         display_icaltimetype_as_webform(&t, "dtstart");
554         wprintf("</TD></TR>\n");
555
556         wprintf("<TR><TD>Due date:</TD><TD>");
557         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
558         if (p != NULL) {
559                 t = icalproperty_get_due(p);
560         }
561         else {
562                 t = icaltime_from_timet(now, 0);
563         }
564         display_icaltimetype_as_webform(&t, "due");
565         wprintf("</TD></TR>\n");
566         wprintf("<TR><TD>Description:</TD><TD>");
567         wprintf("<TEXTAREA NAME=\"description\" wrap=soft "
568                 "ROWS=10 COLS=80 WIDTH=80>\n"
569         );
570         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
571         if (p != NULL) {
572                 escputs((char *)icalproperty_get_comment(p));
573         }
574         wprintf("</TEXTAREA></TD></TR></TABLE>\n");
575
576         wprintf("<CENTER>"
577                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
578                 "&nbsp;&nbsp;"
579                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
580                 "&nbsp;&nbsp;"
581                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
582                 "</CENTER>\n"
583         );
584
585         wprintf("</FORM>\n");
586
587         do_template("endbox");
588         wDumpContent(1);
589
590         if (created_new_vtodo) {
591                 icalcomponent_free(vtodo);
592         }
593 }
594
595 /*
596  * Save an edited task
597  *
598  */
599 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
600         char buf[SIZ];
601         int delete_existing = 0;
602         icalproperty *prop;
603         icalcomponent *vtodo;
604         int created_new_vtodo = 0;
605
606         if (supplied_vtodo != NULL) {
607                 vtodo = supplied_vtodo;
608                 /* If we're looking at a fully encapsulated VCALENDAR
609                  * rather than a VTODO component, attempt to use the first
610                  * relevant VTODO subcomponent.  If there is none, the
611                  * NULL returned by icalcomponent_get_first_component() will
612                  * tell the next iteration of this function to create a
613                  * new one.
614                  */
615                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
616                         save_individual_task(
617                                 icalcomponent_get_first_component(
618                                         vtodo, ICAL_VTODO_COMPONENT
619                                 ), msgnum
620                         );
621                         return;
622                 }
623         }
624         else {
625                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
626                 created_new_vtodo = 1;
627         }
628
629         if (!strcasecmp(bstr("sc"), "Save")) {
630
631                 /* Replace values in the component with ones from the form */
632
633                 while (prop = icalcomponent_get_first_property(vtodo,
634                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
635                         icalcomponent_remove_property(vtodo, prop);
636                         icalproperty_free(prop);
637                 }
638                 icalcomponent_add_property(vtodo,
639                         icalproperty_new_summary(bstr("summary")));
640                 
641                 while (prop = icalcomponent_get_first_property(vtodo,
642                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
643                         icalcomponent_remove_property(vtodo, prop);
644                         icalproperty_free(prop);
645                 }
646                 icalcomponent_add_property(vtodo,
647                         icalproperty_new_description(bstr("description")));
648         
649                 while (prop = icalcomponent_get_first_property(vtodo,
650                       ICAL_DTSTART_PROPERTY), prop != NULL) {
651                         icalcomponent_remove_property(vtodo, prop);
652                         icalproperty_free(prop);
653                 }
654                 icalcomponent_add_property(vtodo,
655                         icalproperty_new_dtstart(
656                                 icaltime_from_webform("dtstart")
657                         )
658                 );
659         
660                 while (prop = icalcomponent_get_first_property(vtodo,
661                       ICAL_DUE_PROPERTY), prop != NULL) {
662                         icalcomponent_remove_property(vtodo, prop);
663                         icalproperty_free(prop);
664                 }
665                 icalcomponent_add_property(vtodo,
666                         icalproperty_new_due(
667                                 icaltime_from_webform("due")
668                         )
669                 );
670         
671                 /* Serialize it and save it to the message base */
672                 serv_puts("ENT0 1|||4");
673                 serv_gets(buf);
674                 if (buf[0] == '4') {
675                         serv_puts("Content-type: text/calendar");
676                         serv_puts("");
677                         serv_puts(icalcomponent_as_ical_string(vtodo));
678                         serv_puts("000");
679
680                         /* Probably not necessary; the server will see the UID
681                          * of the object and delete the old one anyway, but
682                          * just in case...
683                          */
684                         delete_existing = 1;
685                 }
686         }
687
688         /*
689          * If the user clicked 'Delete' then explicitly delete the message.
690          */
691         if (!strcasecmp(bstr("sc"), "Delete")) {
692                 delete_existing = 1;
693         }
694
695         if ( (delete_existing) && (msgnum > 0L) ) {
696                 serv_printf("DELE %ld", atol(bstr("msgnum")));
697                 serv_gets(buf);
698         }
699
700         if (created_new_vtodo) {
701                 icalcomponent_free(vtodo);
702         }
703
704         /* Go back to the task list */
705         readloop("readfwd");
706 }
707
708
709
710 /*
711  * Code common to all display handlers.  Given a message number and a MIME
712  * type, we load the message and hunt for that MIME type.  If found, we load
713  * the relevant part, deserialize it into a libical component, filter it for
714  * the requested object type, and feed it to the specified handler.
715  *
716  */
717 void display_using_handler(long msgnum,
718                         char *mimetype,
719                         icalcomponent_kind which_kind,
720                         void (*callback)(icalcomponent *, long)
721         ) {
722         char buf[SIZ];
723         char mime_partnum[SIZ];
724         char mime_filename[SIZ];
725         char mime_content_type[SIZ];
726         char mime_disposition[SIZ];
727         int mime_length;
728         char relevant_partnum[SIZ];
729         char *relevant_source = NULL;
730         icalcomponent *cal, *c;
731
732         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
733         serv_puts(buf);
734         serv_gets(buf);
735         if (buf[0] != '1') return;
736
737         while (serv_gets(buf), strcmp(buf, "000")) {
738                 if (!strncasecmp(buf, "part=", 5)) {
739                         extract(mime_filename, &buf[5], 1);
740                         extract(mime_partnum, &buf[5], 2);
741                         extract(mime_disposition, &buf[5], 3);
742                         extract(mime_content_type, &buf[5], 4);
743                         mime_length = extract_int(&buf[5], 5);
744
745                         if (!strcasecmp(mime_content_type, "text/calendar")) {
746                                 strcpy(relevant_partnum, mime_partnum);
747                         }
748
749                 }
750         }
751
752         if (strlen(relevant_partnum) > 0) {
753                 relevant_source = load_mimepart(msgnum, relevant_partnum);
754                 if (relevant_source != NULL) {
755
756                         cal = icalcomponent_new_from_string(relevant_source);
757                         if (cal != NULL) {
758
759                                 ical_dezonify(cal);
760
761                                 /* Simple components of desired type */
762                                 if (icalcomponent_isa(cal) == which_kind) {
763                                         callback(cal, msgnum);
764                                 }
765
766                                 /* Subcomponents of desired type */
767                                 for (c = icalcomponent_get_first_component(cal,
768                                     which_kind);
769                                     (c != 0);
770                                     c = icalcomponent_get_next_component(cal,
771                                     which_kind)) {
772                                         callback(c, msgnum);
773                                 }
774                                 icalcomponent_free(cal);
775                         }
776                         free(relevant_source);
777                 }
778         }
779
780 }
781
782 void display_calendar(long msgnum) {
783         display_using_handler(msgnum, "text/calendar",
784                                 ICAL_VEVENT_COMPONENT,
785                                 display_individual_cal);
786 }
787
788 void display_task(long msgnum) {
789         display_using_handler(msgnum, "text/calendar",
790                                 ICAL_VTODO_COMPONENT,
791                                 display_individual_cal);
792 }
793
794 void display_edit_task(void) {
795         long msgnum = 0L;
796
797         /* Force change the room if we have to */
798         if (strlen(bstr("taskrm")) > 0) {
799                 gotoroom(bstr("taskrm"), 0);
800         }
801
802         msgnum = atol(bstr("msgnum"));
803         if (msgnum > 0L) {
804                 /* existing task */
805                 display_using_handler(msgnum, "text/calendar",
806                                 ICAL_VTODO_COMPONENT,
807                                 display_edit_individual_task);
808         }
809         else {
810                 /* new task */
811                 display_edit_individual_task(NULL, 0L);
812         }
813 }
814
815 void save_task(void) {
816         long msgnum = 0L;
817
818         msgnum = atol(bstr("msgnum"));
819         if (msgnum > 0L) {
820                 display_using_handler(msgnum, "text/calendar",
821                                 ICAL_VTODO_COMPONENT,
822                                 save_individual_task);
823         }
824         else {
825                 save_individual_task(NULL, 0L);
826         }
827 }
828
829 void display_edit_event(void) {
830         long msgnum = 0L;
831
832         msgnum = atol(bstr("msgnum"));
833         if (msgnum > 0L) {
834                 /* existing event */
835                 display_using_handler(msgnum, "text/calendar",
836                                 ICAL_VEVENT_COMPONENT,
837                                 display_edit_individual_event);
838         }
839         else {
840                 /* new event */
841                 display_edit_individual_event(NULL, 0L);
842         }
843 }
844
845 void save_event(void) {
846         long msgnum = 0L;
847
848         msgnum = atol(bstr("msgnum"));
849
850         if (msgnum > 0L) {
851                 display_using_handler(msgnum, "text/calendar",
852                                 ICAL_VEVENT_COMPONENT,
853                                 save_individual_event);
854         }
855         else {
856                 save_individual_event(NULL, 0L);
857         }
858 }
859
860
861
862
863
864 /*
865  * freebusy display (for client software)
866  */
867 void do_freebusy(char *req) {
868         char who[SIZ];
869         char buf[SIZ];
870         char *fb;
871
872         extract_token(who, req, 1, ' ');
873         if (!strncasecmp(who, "/freebusy/", 10)) {
874                 strcpy(who, &who[10]);
875         }
876         unescape_input(who);
877
878         if ( (!strcasecmp(&who[strlen(who)-4], ".vcf"))
879            || (!strcasecmp(&who[strlen(who)-4], ".vfb")) ) {
880                 who[strlen(who)-4] = 0;
881         }
882
883         lprintf(9, "freebusy requested for <%s>\n", who);
884         serv_printf("ICAL freebusy|%s", who);
885         serv_gets(buf);
886
887         if (buf[0] != '1') {
888                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
889                 output_headers(0);
890                 wprintf("Content-Type: text/plain\n");
891                 wprintf("\n");
892                 wprintf("%s\n", &buf[4]);
893                 return;
894         }
895
896         fb = read_server_text();
897         http_transmit_thing(fb, strlen(fb), "text/calendar", 0);
898         free(fb);
899 }
900
901
902
903 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */