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