]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* Tweaked the code to work with libical 0.24
[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                         strcpy(buf, &buf[7]);
204                         striplt(buf);
205                         escputs(buf);
206                 }
207                 /* FIXME add status */
208                 wprintf("</TD></TR>\n");
209         }
210
211         /* If the component has subcomponents, recurse through them. */
212         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
213             (c != 0);
214             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
215                 /* Recursively process subcomponent */
216                 cal_process_object(c, recursion_level+1, msgnum, cal_partnum);
217         }
218
219         /* If this is a REQUEST, display conflicts and buttons */
220         if (the_method == ICAL_METHOD_REQUEST) {
221
222                 /* Check for conflicts */
223                 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
224                 serv_gets(buf);
225                 if (buf[0] == '1') {
226                         while (serv_gets(buf), strcmp(buf, "000")) {
227                                 extract(conflict_name, buf, 3);
228                                 is_update = extract_int(buf, 4);
229                                 wprintf("<TR><TD><B><I>%s</I></B></TD>"
230                                         "<TD>"
231                                         "%s "
232                                         "<I>&quot;",
233
234                                         (is_update ?
235                                                 "Update:" :
236                                                 "CONFLICT:"
237                                         ),
238
239                                         (is_update ?
240                                                 "This is an update of" :
241                                                 "This event would conflict with"
242                                         )
243                 
244                                 );
245                                 escputs(conflict_name);
246                                 wprintf("&quot;</I> "
247                                         "which is already in your calendar."
248                                         "</TD></TR>\n");
249                         }
250                 }
251
252                 /* Display the Accept/Decline buttons */
253                 wprintf("<TR><TD COLSPAN=2>"
254                         "<FORM METHOD=\"GET\" "
255                         "ACTION=\"/respond_to_request\">\n"
256                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
257                                 "VALUE=\"Accept\">\n"
258                         "&nbsp;&nbsp;"
259                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
260                                 "VALUE=\"Tentative\">\n"
261                         "&nbsp;&nbsp;"
262                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
263                                 "VALUE=\"Decline\">\n"
264                         "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
265                                 "VALUE=\"%ld\">"
266                         "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
267                                 "VALUE=\"%s\">"
268                         "</FORM>"
269                         "</TD></TR>\n",
270                         msgnum, cal_partnum
271                 );
272
273         }
274
275         /* If this is a REPLY, display update button */
276         if (the_method == ICAL_METHOD_REPLY) {
277
278                 /***********
279                  * In the future, if we want to validate this object before
280                  * continuing, we can do it this way:
281                 serv_printf("ICAL whatever|%ld|%s|", msgnum, cal_partnum);
282                 serv_gets(buf);
283                 }
284                  ***********/
285
286                 /* Display the update buttons */
287                 wprintf("<TR><TD COLSPAN=2>"
288                         "Click <i>Update</i> to accept this reply and "
289                         "update your calendar."
290                         "<FORM METHOD=\"GET\" "
291                         "ACTION=\"/handle_rsvp\">\n"
292                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
293                                 "VALUE=\"Update\">\n"
294                         "&nbsp;&nbsp;"
295                         "<INPUT TYPE=\"submit\" NAME=\"sc\" "
296                                 "VALUE=\"Ignore\">\n"
297                         "<INPUT TYPE=\"hidden\" NAME=\"msgnum\" "
298                                 "VALUE=\"%ld\">"
299                         "<INPUT TYPE=\"hidden\" NAME=\"cal_partnum\" "
300                                 "VALUE=\"%s\">"
301                         "</FORM>"
302                         "</TD></TR>\n",
303                         msgnum, cal_partnum
304                 );
305
306         }
307
308         /* Trailing HTML for the display of this object */
309         if (recursion_level == 0) {
310
311                 wprintf("</TR></TABLE></CENTER>\n");
312         }
313 }
314
315
316 /*
317  * Deserialize a calendar object in a message so it can be processed.
318  * (This is the main entry point for these things)
319  */
320 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
321         icalcomponent *cal;
322
323         cal = icalcomponent_new_from_string(part_source);
324
325         if (cal == NULL) {
326                 wprintf("Error parsing calendar object<BR>\n");
327                 return;
328         }
329
330         cal_process_object(cal, 0, msgnum, cal_partnum);
331
332         /* Free the memory we obtained from libical's constructor */
333         icalcomponent_free(cal);
334 }
335
336
337
338
339 /*
340  * Respond to a meeting request
341  */
342 void respond_to_request(void) {
343         char buf[SIZ];
344
345         output_headers(3);
346
347         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
348                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
349                 "<B>Respond to meeting request</B>"
350                 "</FONT></TD></TR></TABLE><BR>\n"
351         );
352
353         serv_printf("ICAL respond|%s|%s|%s|",
354                 bstr("msgnum"),
355                 bstr("cal_partnum"),
356                 bstr("sc")
357         );
358         serv_gets(buf);
359
360         if (buf[0] == '2') {
361                 wprintf("<TABLE BORDER=0><TR><TD>"
362                         "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
363                         "</TD><TD>"
364                 );
365                 if (!strcasecmp(bstr("sc"), "accept")) {
366                         wprintf("You have accepted this meeting invitation.  "
367                                 "It has been entered into your calendar, "
368                         );
369                 } else if (!strcasecmp(bstr("sc"), "tentative")) {
370                         wprintf("You have tentatively accepted this meeting invitation.  "
371                                 "It has been 'pencilled in' to your calendar, "
372                         );
373                 } else if (!strcasecmp(bstr("sc"), "decline")) {
374                         wprintf("You have declined this meeting invitation.  "
375                                 "It has <b>not</b> been entered into your calendar, "
376                         );
377                 }
378                 wprintf("and a reply has been sent to the meeting organizer."
379                         "</TD></TR></TABLE>\n"
380                 );
381         } else {
382                 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
383                         "%s\n", &buf[4]);
384         }
385
386         wprintf("<A HREF=\"/dotskip?room=");
387         urlescputs(WC->wc_roomname);
388         wprintf("\"><BR>Return to messages</A><BR>\n");
389
390         wDumpContent(1);
391 }
392
393
394
395 /*
396  * Handle an incoming RSVP
397  */
398 void handle_rsvp(void) {
399         char buf[SIZ];
400
401         output_headers(3);
402
403         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
404                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
405                 "<B>Update your calendar with this RSVP</B>"
406                 "</FONT></TD></TR></TABLE><BR>\n"
407         );
408
409         serv_printf("ICAL handle_rsvp|%s|%s|%s|",
410                 bstr("msgnum"),
411                 bstr("cal_partnum"),
412                 bstr("sc")
413         );
414         serv_gets(buf);
415
416         if (buf[0] == '2') {
417                 wprintf("<TABLE BORDER=0><TR><TD>"
418                         "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
419                         "</TD><TD>"
420                 );
421                 if (!strcasecmp(bstr("sc"), "update")) {
422                         wprintf("Your calendar has been updated "
423                                 "to reflect this RSVP."
424                         );
425                 } else if (!strcasecmp(bstr("sc"), "ignore")) {
426                         wprintf("You have chosen to ignore this RSVP. "
427                                 "Your calendar has <b>not</b> been updated."
428                         );
429                 }
430                 wprintf("</TD></TR></TABLE>\n"
431                 );
432         } else {
433                 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
434                         "%s\n", &buf[4]);
435         }
436
437         wprintf("<A HREF=\"/dotskip?room=");
438         urlescputs(WC->wc_roomname);
439         wprintf("\"><BR>Return to messages</A><BR>\n");
440
441         wDumpContent(1);
442 }
443
444
445
446
447 /*****************************************************************************/
448
449
450
451 /*
452  * Display handlers for message reading
453  */
454
455
456
457 /*
458  * If we're reading calendar items, just store them for now.  We have to
459  * sort and re-output them later when we draw the calendar.
460  */
461 void display_individual_cal(icalcomponent *cal, long msgnum) {
462
463         WC->num_cal += 1;
464
465         WC->disp_cal = realloc(WC->disp_cal,
466                         (sizeof(icalcomponent *) * WC->num_cal) );
467         WC->disp_cal[WC->num_cal - 1] = icalcomponent_new_clone(cal);
468
469         WC->cal_msgnum = realloc(WC->cal_msgnum,
470                         (sizeof(long) * WC->num_cal) );
471         WC->cal_msgnum[WC->num_cal - 1] = msgnum;
472 }
473
474
475
476 /*
477  * Display a task in the task list
478  */
479 void display_individual_task(icalcomponent *vtodo, long msgnum) {
480         icalproperty *p;
481
482         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
483         wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
484         if (p != NULL) {
485                 escputs((char *)icalproperty_get_comment(p));
486         }
487         wprintf("</A>\n");
488 }
489
490
491 /*
492  * Display a task by itself (for editing)
493  */
494 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
495         icalcomponent *vtodo;
496         icalproperty *p;
497         struct icaltimetype t;
498         time_t now;
499         int created_new_vtodo = 0;
500
501         now = time(NULL);
502
503         if (supplied_vtodo != NULL) {
504                 vtodo = supplied_vtodo;
505         }
506         else {
507                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
508                 created_new_vtodo = 1;
509         }
510
511         output_headers(3);
512         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
513                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
514                 "<B>Edit task</B>"
515                 "</FONT></TD></TR></TABLE><BR>\n"
516         );
517
518         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
519         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
520                 msgnum);
521
522         wprintf("Summary: "
523                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
524                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
525         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
526         if (p != NULL) {
527                 escputs((char *)icalproperty_get_comment(p));
528         }
529         wprintf("\"><BR>\n");
530
531         wprintf("Start date: ");
532         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
533         if (p != NULL) {
534                 t = icalproperty_get_dtstart(p);
535         }
536         else {
537                 t = icaltime_from_timet(now, 0);
538         }
539         display_icaltimetype_as_webform(&t, "dtstart");
540         wprintf("<BR>\n");
541
542         wprintf("Due date: ");
543         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
544         if (p != NULL) {
545                 t = icalproperty_get_due(p);
546         }
547         else {
548                 t = icaltime_from_timet(now, 0);
549         }
550         display_icaltimetype_as_webform(&t, "due");
551         wprintf("<BR>\n");
552
553         wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
554                 "ROWS=10 COLS=80 WIDTH=80>\n"
555         );
556         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
557         if (p != NULL) {
558                 escputs((char *)icalproperty_get_comment(p));
559         }
560         wprintf("</TEXTAREA><BR>\n");
561
562         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
563                 "&nbsp;&nbsp;"
564                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
565                 "&nbsp;&nbsp;"
566                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
567                 "</CENTER>\n"
568         );
569
570         wprintf("</FORM>\n");
571
572         wDumpContent(1);
573
574         if (created_new_vtodo) {
575                 icalcomponent_free(vtodo);
576         }
577 }
578
579 /*
580  * Save an edited task
581  */
582 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
583         char buf[SIZ];
584         int delete_existing = 0;
585         icalproperty *prop;
586         icalcomponent *vtodo;
587         int created_new_vtodo = 0;
588
589         if (supplied_vtodo != NULL) {
590                 vtodo = supplied_vtodo;
591         }
592         else {
593                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
594                 created_new_vtodo = 1;
595         }
596
597         if (!strcasecmp(bstr("sc"), "Save")) {
598
599                 /* Replace values in the component with ones from the form */
600
601                 while (prop = icalcomponent_get_first_property(vtodo,
602                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
603                         icalcomponent_remove_property(vtodo, prop);
604                         icalproperty_free(prop);
605                 }
606                 icalcomponent_add_property(vtodo,
607                         icalproperty_new_summary(bstr("summary")));
608                 
609                 while (prop = icalcomponent_get_first_property(vtodo,
610                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
611                         icalcomponent_remove_property(vtodo, prop);
612                         icalproperty_free(prop);
613                 }
614                 icalcomponent_add_property(vtodo,
615                         icalproperty_new_description(bstr("description")));
616         
617                 while (prop = icalcomponent_get_first_property(vtodo,
618                       ICAL_DTSTART_PROPERTY), prop != NULL) {
619                         icalcomponent_remove_property(vtodo, prop);
620                         icalproperty_free(prop);
621                 }
622                 icalcomponent_add_property(vtodo,
623                         icalproperty_new_dtstart(
624                                 icaltime_from_webform("dtstart")
625                         )
626                 );
627         
628                 while (prop = icalcomponent_get_first_property(vtodo,
629                       ICAL_DUE_PROPERTY), prop != NULL) {
630                         icalcomponent_remove_property(vtodo, prop);
631                         icalproperty_free(prop);
632                 }
633                 icalcomponent_add_property(vtodo,
634                         icalproperty_new_due(
635                                 icaltime_from_webform("due")
636                         )
637                 );
638         
639                 /* Serialize it and save it to the message base */
640                 serv_puts("ENT0 1|||4");
641                 serv_gets(buf);
642                 if (buf[0] == '4') {
643                         serv_puts("Content-type: text/calendar");
644                         serv_puts("");
645                         serv_puts(icalcomponent_as_ical_string(vtodo));
646                         serv_puts("000");
647
648                         /* Probably not necessary; the server will see the UID
649                          * of the object and delete the old one anyway, but
650                          * just in case...
651                          */
652                         delete_existing = 1;
653                 }
654         }
655
656         /*
657          * If the user clicked 'Delete' then explicitly delete the message.
658          */
659         if (!strcasecmp(bstr("sc"), "Delete")) {
660                 delete_existing = 1;
661         }
662
663         if ( (delete_existing) && (msgnum > 0L) ) {
664                 serv_printf("DELE %ld", atol(bstr("msgnum")));
665                 serv_gets(buf);
666         }
667
668         if (created_new_vtodo) {
669                 icalcomponent_free(vtodo);
670         }
671
672         /* Go back to the task list */
673         readloop("readfwd");
674 }
675
676
677
678 /*
679  * Code common to all display handlers.  Given a message number and a MIME
680  * type, we load the message and hunt for that MIME type.  If found, we load
681  * the relevant part, deserialize it into a libical component, filter it for
682  * the requested object type, and feed it to the specified handler.
683  */
684 void display_using_handler(long msgnum,
685                         char *mimetype,
686                         icalcomponent_kind which_kind,
687                         void (*callback)(icalcomponent *, long)
688         ) {
689         char buf[SIZ];
690         char mime_partnum[SIZ];
691         char mime_filename[SIZ];
692         char mime_content_type[SIZ];
693         char mime_disposition[SIZ];
694         int mime_length;
695         char relevant_partnum[SIZ];
696         char *relevant_source = NULL;
697         icalcomponent *cal, *c;
698
699         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
700         serv_puts(buf);
701         serv_gets(buf);
702         if (buf[0] != '1') return;
703
704         while (serv_gets(buf), strcmp(buf, "000")) {
705                 if (!strncasecmp(buf, "part=", 5)) {
706                         extract(mime_filename, &buf[5], 1);
707                         extract(mime_partnum, &buf[5], 2);
708                         extract(mime_disposition, &buf[5], 3);
709                         extract(mime_content_type, &buf[5], 4);
710                         mime_length = extract_int(&buf[5], 5);
711
712                         if (!strcasecmp(mime_content_type, "text/calendar")) {
713                                 strcpy(relevant_partnum, mime_partnum);
714                         }
715
716                 }
717         }
718
719         if (strlen(relevant_partnum) > 0) {
720                 relevant_source = load_mimepart(msgnum, relevant_partnum);
721                 if (relevant_source != NULL) {
722
723                         cal = icalcomponent_new_from_string(relevant_source);
724                         if (cal != NULL) {
725
726                                 /* Simple components of desired type */
727                                 if (icalcomponent_isa(cal) == which_kind) {
728                                         callback(cal, msgnum);
729                                 }
730
731                                 /* Subcomponents of desired type */
732                                 for (c = icalcomponent_get_first_component(cal,
733                                     which_kind);
734                                     (c != 0);
735                                     c = icalcomponent_get_next_component(cal,
736                                     which_kind)) {
737                                         callback(c, msgnum);
738                                 }
739                                 icalcomponent_free(cal);
740                         }
741                         free(relevant_source);
742                 }
743         }
744
745 }
746
747 void display_calendar(long msgnum) {
748         display_using_handler(msgnum, "text/calendar",
749                                 ICAL_VEVENT_COMPONENT,
750                                 display_individual_cal);
751 }
752
753 void display_task(long msgnum) {
754         display_using_handler(msgnum, "text/calendar",
755                                 ICAL_VTODO_COMPONENT,
756                                 display_individual_task);
757 }
758
759 void display_edit_task(void) {
760         long msgnum = 0L;
761
762         msgnum = atol(bstr("msgnum"));
763         if (msgnum > 0L) {
764                 /* existing task */
765                 display_using_handler(msgnum, "text/calendar",
766                                 ICAL_VTODO_COMPONENT,
767                                 display_edit_individual_task);
768         }
769         else {
770                 /* new task */
771                 display_edit_individual_task(NULL, 0L);
772         }
773 }
774
775 void save_task(void) {
776         long msgnum = 0L;
777
778         msgnum = atol(bstr("msgnum"));
779         if (msgnum > 0L) {
780                 display_using_handler(msgnum, "text/calendar",
781                                 ICAL_VTODO_COMPONENT,
782                                 save_individual_task);
783         }
784         else {
785                 save_individual_task(NULL, 0L);
786         }
787 }
788
789 void display_edit_event(void) {
790         long msgnum = 0L;
791
792         msgnum = atol(bstr("msgnum"));
793         if (msgnum > 0L) {
794                 /* existing event */
795                 display_using_handler(msgnum, "text/calendar",
796                                 ICAL_VEVENT_COMPONENT,
797                                 display_edit_individual_event);
798         }
799         else {
800                 /* new event */
801                 display_edit_individual_event(NULL, 0L);
802         }
803 }
804
805 void save_event(void) {
806         long msgnum = 0L;
807
808         msgnum = atol(bstr("msgnum"));
809
810         if (msgnum > 0L) {
811                 display_using_handler(msgnum, "text/calendar",
812                                 ICAL_VEVENT_COMPONENT,
813                                 save_individual_event);
814         }
815         else {
816                 save_individual_event(NULL, 0L);
817         }
818 }
819
820 #endif /* HAVE_ICAL_H */