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