3b9bf2e862934d9a272017687e1a5f58b4117e1c
[citadel.git] / webcit / calendar.c
1 /*
2  * $Id$
3  *
4  * Functions which handle calendar objects and their processing/display.
5  *
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <time.h>
27 #include "webcit.h"
28 #include "webserver.h"
29
30 #ifndef HAVE_ICAL_H
31
32 /*
33  * Handler stubs for builds with no calendar library available
34  */
35 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
36
37         wprintf("<I>This message contains calendaring/scheduling information,"
38                 " but support for calendars is not available on this "
39                 "particular system.  Please ask your system administrator to "
40                 "install a new version of the Citadel web service with "
41                 "calendaring enabled.</I><BR>\n"
42         );
43
44 }
45
46 void display_calendar(long msgnum) {
47         wprintf("<i>"
48                 "Cannot display calendar item.  You are seeing this error "
49                 "because your WebCit service has not been installed with "
50                 "calendar support.  Please contact your system administrator."
51                 "</i><br>\n");
52 }
53
54 void display_task(long msgnum) {
55         wprintf("<i>"
56                 "Cannot display to-do item.  You are seeing this error "
57                 "because your WebCit service has not been installed with "
58                 "calendar support.  Please contact your system administrator."
59                 "</i><br>\n");
60 }
61
62 #else /* HAVE_ICAL_H */
63
64
65 /******   End of handler stubs.  Everything below this line is real.   ******/
66
67
68
69
70 /*
71  * Process a calendar object
72  * ...at this point it's already been deserialized by cal_process_attachment()
73  */
74 void cal_process_object(icalcomponent *cal,
75                         int recursion_level,
76                         long msgnum,
77                         char *cal_partnum
78 ) {
79         icalcomponent *c;
80         icalproperty *method = NULL;
81         icalproperty_method the_method = ICAL_METHOD_NONE;
82         icalproperty *p;
83         struct icaltimetype t;
84         time_t tt;
85         char buf[SIZ];
86         char conflict_name[SIZ];
87         int is_update = 0;
88
89         /* Leading HTML for the display of this object */
90         if (recursion_level == 0) {
91                 wprintf("<CENTER><TABLE border=0>\n");
92         }
93
94         /* Look for a method */
95         method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
96
97         /* See what we need to do with this */
98         if (method != NULL) {
99                 the_method = icalproperty_get_method(method);
100                 switch(the_method) {
101                     case ICAL_METHOD_REQUEST:
102                         wprintf("<TR><TD COLSPAN=2>\n"
103                                 "<IMG ALIGN=CENTER "
104                                 "SRC=\"/static/vcalendar.gif\">"
105                                 "&nbsp;&nbsp;"  
106                                 "<B>Meeting invitation</B>
107                                 </TD></TR>\n"
108                         );
109                         break;
110                     case ICAL_METHOD_REPLY:
111                         wprintf("<TR><TD COLSPAN=2>\n"
112                                 "<IMG ALIGN=CENTER "
113                                 "SRC=\"/static/vcalendar.gif\">"
114                                 "&nbsp;&nbsp;"  
115                                 "<B>Attendee's reply to your invitation</B>
116                                 </TD></TR>\n"
117                         );
118                         break;
119                     case ICAL_METHOD_PUBLISH:
120                         wprintf("<TR><TD COLSPAN=2>\n"
121                                 "<IMG ALIGN=CENTER "
122                                 "SRC=\"/static/vcalendar.gif\">"
123                                 "&nbsp;&nbsp;"  
124                                 "<B>Published event</B>
125                                 </TD></TR>\n"
126                         );
127                         break;
128                     default:
129                         wprintf("<TR><TD COLSPAN=2>"
130                                 "I don't know what to do with this.</TD></TR>"
131                                 "\n");
132                         break;
133                 }
134         }
135
136         p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
137         if (p != NULL) {
138                 wprintf("<TR><TD><B>Summary:</B></TD><TD>");
139                 escputs((char *)icalproperty_get_comment(p));
140                 wprintf("</TD></TR>\n");
141         }
142
143         p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
144         if (p != NULL) {
145                 wprintf("<TR><TD><B>Location:</B></TD><TD>");
146                 escputs((char *)icalproperty_get_comment(p));
147                 wprintf("</TD></TR>\n");
148         }
149
150         /*
151          * Only show start/end times if we're actually looking at the VEVENT
152          * component.  Otherwise it shows bogus dates for things like timezone.
153          */
154         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
155
156                 p = icalcomponent_get_first_property(cal,
157                                                 ICAL_DTSTART_PROPERTY);
158                 if (p != NULL) {
159                         t = icalproperty_get_dtstart(p);
160
161                         if (t.is_date) {
162                                 wprintf("<TR><TD><B>Date:"
163                                         "</B></TD><TD>"
164                                         "%s %d, %d</TD></TR>",
165                                         months[t.month - 1],
166                                         t.day, t.year
167                                 );
168                         }
169                         else {
170                                 tt = icaltime_as_timet(t);
171                                 fmt_date(buf, tt);
172                                 wprintf("<TR><TD><B>Starting date/time:"
173                                         "</B></TD><TD>"
174                                         "%s</TD></TR>", buf
175                                 );
176                         }
177                 }
178         
179                 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
180                 if (p != NULL) {
181                         t = icalproperty_get_dtend(p);
182                         tt = icaltime_as_timet(t);
183                         fmt_date(buf, tt);
184                         wprintf("<TR><TD><B>Ending date/time:</B></TD><TD>"
185                                 "%s</TD></TR>", buf
186                         );
187                 }
188
189         }
190
191         p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
192         if (p != NULL) {
193                 wprintf("<TR><TD><B>Description:</B></TD><TD>");
194                 escputs((char *)icalproperty_get_comment(p));
195                 wprintf("</TD></TR>\n");
196         }
197
198         /* If the component has attendees, iterate through them. */
199         for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
200                 wprintf("<TR><TD><B>Attendee:</B></TD><TD>");
201                 strcpy(buf, icalproperty_get_attendee(p));
202                 if (!strncasecmp(buf, "MAILTO:", 7)) {
203
204                         /* screen name or email address */
205                         strcpy(buf, &buf[7]);
206                         striplt(buf);
207                         escputs(buf);
208                         wprintf(" ");
209
210                         /* participant status */
211                         partstat_as_string(buf, p);
212                         escputs(buf);
213                 }
214                 wprintf("</TD></TR>\n");
215         }
216
217         /* If the component has subcomponents, recurse through them. */
218         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
219             (c != 0);
220             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
221                 /* Recursively process subcomponent */
222                 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
223         }
224
225         /* If this is a REQUEST, display conflicts and buttons */
226         if (the_method == ICAL_METHOD_REQUEST) {
227
228                 /* Check for conflicts */
229                 lprintf(9, "Checking server calendar for conflicts...\n");
230                 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
231                 serv_gets(buf);
232                 if (buf[0] == '1') {
233                         while (serv_gets(buf), strcmp(buf, "000")) {
234                                 extract(conflict_name, buf, 3);
235                                 is_update = extract_int(buf, 4);
236                                 wprintf("<TR><TD><B><I>%s</I></B></TD>"
237                                         "<TD>"
238                                         "%s "
239                                         "<I>&quot;",
240
241                                         (is_update ?
242                                                 "Update:" :
243                                                 "CONFLICT:"
244                                         ),
245
246                                         (is_update ?
247                                                 "This is an update of" :
248                                                 "This event would conflict with"
249                                         )
250                 
251                                 );
252                                 escputs(conflict_name);
253                                 wprintf("&quot;</I> "
254                                         "which is already in your calendar."
255                                         "</TD></TR>\n");
256                         }
257                 }
258                 lprintf(9, "...done.\n");
259
260                 /* Display the Accept/Decline buttons */
261                 wprintf("<TR><TD COLSPAN=2>"
262                         "<FORM METHOD=\"GET\" "
263                         "ACTION=\"/respond_to_request\">\n"
264                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
265                                 "VALUE=\"Accept\">\n"
266                         "&nbsp;&nbsp;"
267                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
268                                 "VALUE=\"Tentative\">\n"
269                         "&nbsp;&nbsp;"
270                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
271                                 "VALUE=\"Decline\">\n"
272                         "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
273                                 "VALUE=\"%ld\">"
274                         "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
275                                 "VALUE=\"%s\">"
276                         "</FORM>"
277                         "</TD></TR>\n",
278                         msgnum, cal_partnum
279                 );
280
281         }
282
283         /* If this is a REPLY, display update button */
284         if (the_method == ICAL_METHOD_REPLY) {
285
286                 /***********
287                  * In the future, if we want to validate this object before
288                  * continuing, we can do it this way:
289                 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
290                 serv_gets(buf);
291                 }
292                  ***********/
293
294                 /* Display the update buttons */
295                 wprintf("<TR><TD COLSPAN=2>"
296                         "Click <i>Update</i> to accept this reply and "
297                         "update your calendar."
298                         "<FORM METHOD=\"GET\" "
299                         "ACTION=\"/handle_rsvp\">\n"
300                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
301                                 "VALUE=\"Update\">\n"
302                         "&nbsp;&nbsp;"
303                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
304                                 "VALUE=\"Ignore\">\n"
305                         "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
306                                 "VALUE=\"%ld\">"
307                         "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
308                                 "VALUE=\"%s\">"
309                         "</FORM>"
310                         "</TD></TR>\n",
311                         msgnum, cal_partnum
312                 );
313
314         }
315
316         /* Trailing HTML for the display of this object */
317         if (recursion_level == 0) {
318
319                 wprintf("</TR></TABLE></CENTER>\n");
320         }
321 }
322
323
324 /*
325  * Deserialize a calendar object in a message so it can be processed.
326  * (This is the main entry point for these things)
327  */
328 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
329         icalcomponent *cal;
330
331         cal = icalcomponent_new_from_string(part_source);
332
333         if (cal == NULL) {
334                 wprintf("Error parsing calendar object<BR>\n");
335                 return;
336         }
337
338         ical_dezonify(cal);
339         cal_process_object(cal, 0, msgnum, cal_partnum);
340
341         /* Free the memory we obtained from libical's constructor */
342         icalcomponent_free(cal);
343 }
344
345
346
347
348 /*
349  * Respond to a meeting request
350  */
351 void respond_to_request(void) {
352         char buf[SIZ];
353
354         output_headers(3);
355
356         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
357                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
358                 "<B>Respond to meeting request</B>"
359                 "</FONT></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=007700><TR><TD>"
413                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
414                 "<B>Update your calendar with this RSVP</B>"
415                 "</FONT></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 in the task list
487  */
488 void display_individual_task(icalcomponent *vtodo, long msgnum) {
489         icalproperty *p;
490
491         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
492         wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld&taskrm=", msgnum);
493         urlescputs(WC->wc_roomname);
494         wprintf("\">");
495         if (p != NULL) {
496                 escputs((char *)icalproperty_get_comment(p));
497         }
498         wprintf("</A>\n");
499 }
500
501
502 /*
503  * Display a task by itself (for editing)
504  */
505 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
506         icalcomponent *vtodo;
507         icalproperty *p;
508         struct icaltimetype t;
509         time_t now;
510         int created_new_vtodo = 0;
511
512         now = time(NULL);
513
514         if (supplied_vtodo != NULL) {
515                 vtodo = supplied_vtodo;
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=007700><TR><TD>"
524                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
525                 "<B>Edit task</B>"
526                 "</FONT></TD></TR></TABLE><BR>\n"
527         );
528
529         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
530         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
531                 msgnum);
532
533         wprintf("Summary: "
534                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
535                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
536         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
537         if (p != NULL) {
538                 escputs((char *)icalproperty_get_comment(p));
539         }
540         wprintf("\"><BR>\n");
541
542         wprintf("Start date: ");
543         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
544         if (p != NULL) {
545                 t = icalproperty_get_dtstart(p);
546         }
547         else {
548                 t = icaltime_from_timet(now, 0);
549         }
550         display_icaltimetype_as_webform(&t, "dtstart");
551         wprintf("<BR>\n");
552
553         wprintf("Due date: ");
554         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
555         if (p != NULL) {
556                 t = icalproperty_get_due(p);
557         }
558         else {
559                 t = icaltime_from_timet(now, 0);
560         }
561         display_icaltimetype_as_webform(&t, "due");
562         wprintf("<BR>\n");
563
564         wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
565                 "ROWS=10 COLS=80 WIDTH=80>\n"
566         );
567         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
568         if (p != NULL) {
569                 escputs((char *)icalproperty_get_comment(p));
570         }
571         wprintf("</TEXTAREA><BR>\n");
572
573         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
574                 "&nbsp;&nbsp;"
575                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
576                 "&nbsp;&nbsp;"
577                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
578                 "</CENTER>\n"
579         );
580
581         wprintf("</FORM>\n");
582
583         wDumpContent(1);
584
585         if (created_new_vtodo) {
586                 icalcomponent_free(vtodo);
587         }
588 }
589
590 /*
591  * Save an edited task
592  */
593 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
594         char buf[SIZ];
595         int delete_existing = 0;
596         icalproperty *prop;
597         icalcomponent *vtodo;
598         int created_new_vtodo = 0;
599
600         if (supplied_vtodo != NULL) {
601                 vtodo = supplied_vtodo;
602         }
603         else {
604                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
605                 created_new_vtodo = 1;
606         }
607
608         if (!strcasecmp(bstr("sc"), "Save")) {
609
610                 /* Replace values in the component with ones from the form */
611
612                 while (prop = icalcomponent_get_first_property(vtodo,
613                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
614                         icalcomponent_remove_property(vtodo, prop);
615                         icalproperty_free(prop);
616                 }
617                 icalcomponent_add_property(vtodo,
618                         icalproperty_new_summary(bstr("summary")));
619                 
620                 while (prop = icalcomponent_get_first_property(vtodo,
621                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
622                         icalcomponent_remove_property(vtodo, prop);
623                         icalproperty_free(prop);
624                 }
625                 icalcomponent_add_property(vtodo,
626                         icalproperty_new_description(bstr("description")));
627         
628                 while (prop = icalcomponent_get_first_property(vtodo,
629                       ICAL_DTSTART_PROPERTY), prop != NULL) {
630                         icalcomponent_remove_property(vtodo, prop);
631                         icalproperty_free(prop);
632                 }
633                 icalcomponent_add_property(vtodo,
634                         icalproperty_new_dtstart(
635                                 icaltime_from_webform("dtstart")
636                         )
637                 );
638         
639                 while (prop = icalcomponent_get_first_property(vtodo,
640                       ICAL_DUE_PROPERTY), prop != NULL) {
641                         icalcomponent_remove_property(vtodo, prop);
642                         icalproperty_free(prop);
643                 }
644                 icalcomponent_add_property(vtodo,
645                         icalproperty_new_due(
646                                 icaltime_from_webform("due")
647                         )
648                 );
649         
650                 /* Serialize it and save it to the message base */
651                 serv_puts("ENT0 1|||4");
652                 serv_gets(buf);
653                 if (buf[0] == '4') {
654                         serv_puts("Content-type: text/calendar");
655                         serv_puts("");
656                         serv_puts(icalcomponent_as_ical_string(vtodo));
657                         serv_puts("000");
658
659                         /* Probably not necessary; the server will see the UID
660                          * of the object and delete the old one anyway, but
661                          * just in case...
662                          */
663                         delete_existing = 1;
664                 }
665         }
666
667         /*
668          * If the user clicked 'Delete' then explicitly delete the message.
669          */
670         if (!strcasecmp(bstr("sc"), "Delete")) {
671                 delete_existing = 1;
672         }
673
674         if ( (delete_existing) && (msgnum > 0L) ) {
675                 serv_printf("DELE %ld", atol(bstr("msgnum")));
676                 serv_gets(buf);
677         }
678
679         if (created_new_vtodo) {
680                 icalcomponent_free(vtodo);
681         }
682
683         /* Go back to the task list */
684         readloop("readfwd");
685 }
686
687
688
689 /*
690  * Code common to all display handlers.  Given a message number and a MIME
691  * type, we load the message and hunt for that MIME type.  If found, we load
692  * the relevant part, deserialize it into a libical component, filter it for
693  * the requested object type, and feed it to the specified handler.
694  */
695 void display_using_handler(long msgnum,
696                         char *mimetype,
697                         icalcomponent_kind which_kind,
698                         void (*callback)(icalcomponent *, long)
699         ) {
700         char buf[SIZ];
701         char mime_partnum[SIZ];
702         char mime_filename[SIZ];
703         char mime_content_type[SIZ];
704         char mime_disposition[SIZ];
705         int mime_length;
706         char relevant_partnum[SIZ];
707         char *relevant_source = NULL;
708         icalcomponent *cal, *c;
709
710         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
711         serv_puts(buf);
712         serv_gets(buf);
713         if (buf[0] != '1') return;
714
715         while (serv_gets(buf), strcmp(buf, "000")) {
716                 if (!strncasecmp(buf, "part=", 5)) {
717                         extract(mime_filename, &buf[5], 1);
718                         extract(mime_partnum, &buf[5], 2);
719                         extract(mime_disposition, &buf[5], 3);
720                         extract(mime_content_type, &buf[5], 4);
721                         mime_length = extract_int(&buf[5], 5);
722
723                         if (!strcasecmp(mime_content_type, "text/calendar")) {
724                                 strcpy(relevant_partnum, mime_partnum);
725                         }
726
727                 }
728         }
729
730         if (strlen(relevant_partnum) > 0) {
731                 relevant_source = load_mimepart(msgnum, relevant_partnum);
732                 if (relevant_source != NULL) {
733
734                         cal = icalcomponent_new_from_string(relevant_source);
735                         if (cal != NULL) {
736
737                                 ical_dezonify(cal);
738
739                                 /* Simple components of desired type */
740                                 if (icalcomponent_isa(cal) == which_kind) {
741                                         callback(cal, msgnum);
742                                 }
743
744                                 /* Subcomponents of desired type */
745                                 for (c = icalcomponent_get_first_component(cal,
746                                     which_kind);
747                                     (c != 0);
748                                     c = icalcomponent_get_next_component(cal,
749                                     which_kind)) {
750                                         callback(c, msgnum);
751                                 }
752                                 icalcomponent_free(cal);
753                         }
754                         free(relevant_source);
755                 }
756         }
757
758 }
759
760 void display_calendar(long msgnum) {
761         display_using_handler(msgnum, "text/calendar",
762                                 ICAL_VEVENT_COMPONENT,
763                                 display_individual_cal);
764 }
765
766 void display_task(long msgnum) {
767         display_using_handler(msgnum, "text/calendar",
768                                 ICAL_VTODO_COMPONENT,
769                                 display_individual_task);
770 }
771
772 void display_edit_task(void) {
773         long msgnum = 0L;
774
775         /* Force change the room if we have to */
776         if (strlen(bstr("taskrm")) > 0) {
777                 gotoroom(bstr("taskrm"), 0);
778         }
779
780         msgnum = atol(bstr("msgnum"));
781         if (msgnum > 0L) {
782                 /* existing task */
783                 display_using_handler(msgnum, "text/calendar",
784                                 ICAL_VTODO_COMPONENT,
785                                 display_edit_individual_task);
786         }
787         else {
788                 /* new task */
789                 display_edit_individual_task(NULL, 0L);
790         }
791 }
792
793 void save_task(void) {
794         long msgnum = 0L;
795
796         msgnum = atol(bstr("msgnum"));
797         if (msgnum > 0L) {
798                 display_using_handler(msgnum, "text/calendar",
799                                 ICAL_VTODO_COMPONENT,
800                                 save_individual_task);
801         }
802         else {
803                 save_individual_task(NULL, 0L);
804         }
805 }
806
807 void display_edit_event(void) {
808         long msgnum = 0L;
809
810         msgnum = atol(bstr("msgnum"));
811         if (msgnum > 0L) {
812                 /* existing event */
813                 display_using_handler(msgnum, "text/calendar",
814                                 ICAL_VEVENT_COMPONENT,
815                                 display_edit_individual_event);
816         }
817         else {
818                 /* new event */
819                 display_edit_individual_event(NULL, 0L);
820         }
821 }
822
823 void save_event(void) {
824         long msgnum = 0L;
825
826         msgnum = atol(bstr("msgnum"));
827
828         if (msgnum > 0L) {
829                 display_using_handler(msgnum, "text/calendar",
830                                 ICAL_VEVENT_COMPONENT,
831                                 save_individual_event);
832         }
833         else {
834                 save_individual_event(NULL, 0L);
835         }
836 }
837
838 #endif /* HAVE_ICAL_H */