f1b20868f11c252532a62d93f6e1233f45f0ca7b
[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&taskrm=", msgnum);
487         urlescputs(WC->wc_roomname);
488         wprintf("\">");
489         if (p != NULL) {
490                 escputs((char *)icalproperty_get_comment(p));
491         }
492         wprintf("</A>\n");
493 }
494
495
496 /*
497  * Display a task by itself (for editing)
498  */
499 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
500         icalcomponent *vtodo;
501         icalproperty *p;
502         struct icaltimetype t;
503         time_t now;
504         int created_new_vtodo = 0;
505
506         now = time(NULL);
507
508         if (supplied_vtodo != NULL) {
509                 vtodo = supplied_vtodo;
510         }
511         else {
512                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
513                 created_new_vtodo = 1;
514         }
515
516         output_headers(3);
517         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
518                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
519                 "<B>Edit task</B>"
520                 "</FONT></TD></TR></TABLE><BR>\n"
521         );
522
523         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
524         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
525                 msgnum);
526
527         wprintf("Summary: "
528                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
529                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
530         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
531         if (p != NULL) {
532                 escputs((char *)icalproperty_get_comment(p));
533         }
534         wprintf("\"><BR>\n");
535
536         wprintf("Start date: ");
537         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
538         if (p != NULL) {
539                 t = icalproperty_get_dtstart(p);
540         }
541         else {
542                 t = icaltime_from_timet(now, 0);
543         }
544         display_icaltimetype_as_webform(&t, "dtstart");
545         wprintf("<BR>\n");
546
547         wprintf("Due date: ");
548         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
549         if (p != NULL) {
550                 t = icalproperty_get_due(p);
551         }
552         else {
553                 t = icaltime_from_timet(now, 0);
554         }
555         display_icaltimetype_as_webform(&t, "due");
556         wprintf("<BR>\n");
557
558         wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
559                 "ROWS=10 COLS=80 WIDTH=80>\n"
560         );
561         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
562         if (p != NULL) {
563                 escputs((char *)icalproperty_get_comment(p));
564         }
565         wprintf("</TEXTAREA><BR>\n");
566
567         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
568                 "&nbsp;&nbsp;"
569                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
570                 "&nbsp;&nbsp;"
571                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
572                 "</CENTER>\n"
573         );
574
575         wprintf("</FORM>\n");
576
577         wDumpContent(1);
578
579         if (created_new_vtodo) {
580                 icalcomponent_free(vtodo);
581         }
582 }
583
584 /*
585  * Save an edited task
586  */
587 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
588         char buf[SIZ];
589         int delete_existing = 0;
590         icalproperty *prop;
591         icalcomponent *vtodo;
592         int created_new_vtodo = 0;
593
594         if (supplied_vtodo != NULL) {
595                 vtodo = supplied_vtodo;
596         }
597         else {
598                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
599                 created_new_vtodo = 1;
600         }
601
602         if (!strcasecmp(bstr("sc"), "Save")) {
603
604                 /* Replace values in the component with ones from the form */
605
606                 while (prop = icalcomponent_get_first_property(vtodo,
607                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
608                         icalcomponent_remove_property(vtodo, prop);
609                         icalproperty_free(prop);
610                 }
611                 icalcomponent_add_property(vtodo,
612                         icalproperty_new_summary(bstr("summary")));
613                 
614                 while (prop = icalcomponent_get_first_property(vtodo,
615                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
616                         icalcomponent_remove_property(vtodo, prop);
617                         icalproperty_free(prop);
618                 }
619                 icalcomponent_add_property(vtodo,
620                         icalproperty_new_description(bstr("description")));
621         
622                 while (prop = icalcomponent_get_first_property(vtodo,
623                       ICAL_DTSTART_PROPERTY), prop != NULL) {
624                         icalcomponent_remove_property(vtodo, prop);
625                         icalproperty_free(prop);
626                 }
627                 icalcomponent_add_property(vtodo,
628                         icalproperty_new_dtstart(
629                                 icaltime_from_webform("dtstart")
630                         )
631                 );
632         
633                 while (prop = icalcomponent_get_first_property(vtodo,
634                       ICAL_DUE_PROPERTY), prop != NULL) {
635                         icalcomponent_remove_property(vtodo, prop);
636                         icalproperty_free(prop);
637                 }
638                 icalcomponent_add_property(vtodo,
639                         icalproperty_new_due(
640                                 icaltime_from_webform("due")
641                         )
642                 );
643         
644                 /* Serialize it and save it to the message base */
645                 serv_puts("ENT0 1|||4");
646                 serv_gets(buf);
647                 if (buf[0] == '4') {
648                         serv_puts("Content-type: text/calendar");
649                         serv_puts("");
650                         serv_puts(icalcomponent_as_ical_string(vtodo));
651                         serv_puts("000");
652
653                         /* Probably not necessary; the server will see the UID
654                          * of the object and delete the old one anyway, but
655                          * just in case...
656                          */
657                         delete_existing = 1;
658                 }
659         }
660
661         /*
662          * If the user clicked 'Delete' then explicitly delete the message.
663          */
664         if (!strcasecmp(bstr("sc"), "Delete")) {
665                 delete_existing = 1;
666         }
667
668         if ( (delete_existing) && (msgnum > 0L) ) {
669                 serv_printf("DELE %ld", atol(bstr("msgnum")));
670                 serv_gets(buf);
671         }
672
673         if (created_new_vtodo) {
674                 icalcomponent_free(vtodo);
675         }
676
677         /* Go back to the task list */
678         readloop("readfwd");
679 }
680
681
682
683 /*
684  * Code common to all display handlers.  Given a message number and a MIME
685  * type, we load the message and hunt for that MIME type.  If found, we load
686  * the relevant part, deserialize it into a libical component, filter it for
687  * the requested object type, and feed it to the specified handler.
688  */
689 void display_using_handler(long msgnum,
690                         char *mimetype,
691                         icalcomponent_kind which_kind,
692                         void (*callback)(icalcomponent *, long)
693         ) {
694         char buf[SIZ];
695         char mime_partnum[SIZ];
696         char mime_filename[SIZ];
697         char mime_content_type[SIZ];
698         char mime_disposition[SIZ];
699         int mime_length;
700         char relevant_partnum[SIZ];
701         char *relevant_source = NULL;
702         icalcomponent *cal, *c;
703
704         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
705         serv_puts(buf);
706         serv_gets(buf);
707         if (buf[0] != '1') return;
708
709         while (serv_gets(buf), strcmp(buf, "000")) {
710                 if (!strncasecmp(buf, "part=", 5)) {
711                         extract(mime_filename, &buf[5], 1);
712                         extract(mime_partnum, &buf[5], 2);
713                         extract(mime_disposition, &buf[5], 3);
714                         extract(mime_content_type, &buf[5], 4);
715                         mime_length = extract_int(&buf[5], 5);
716
717                         if (!strcasecmp(mime_content_type, "text/calendar")) {
718                                 strcpy(relevant_partnum, mime_partnum);
719                         }
720
721                 }
722         }
723
724         if (strlen(relevant_partnum) > 0) {
725                 relevant_source = load_mimepart(msgnum, relevant_partnum);
726                 if (relevant_source != NULL) {
727
728                         cal = icalcomponent_new_from_string(relevant_source);
729                         if (cal != NULL) {
730
731                                 ical_dezonify(cal);
732
733                                 /* Simple components of desired type */
734                                 if (icalcomponent_isa(cal) == which_kind) {
735                                         callback(cal, msgnum);
736                                 }
737
738                                 /* Subcomponents of desired type */
739                                 for (c = icalcomponent_get_first_component(cal,
740                                     which_kind);
741                                     (c != 0);
742                                     c = icalcomponent_get_next_component(cal,
743                                     which_kind)) {
744                                         callback(c, msgnum);
745                                 }
746                                 icalcomponent_free(cal);
747                         }
748                         free(relevant_source);
749                 }
750         }
751
752 }
753
754 void display_calendar(long msgnum) {
755         display_using_handler(msgnum, "text/calendar",
756                                 ICAL_VEVENT_COMPONENT,
757                                 display_individual_cal);
758 }
759
760 void display_task(long msgnum) {
761         display_using_handler(msgnum, "text/calendar",
762                                 ICAL_VTODO_COMPONENT,
763                                 display_individual_task);
764 }
765
766 void display_edit_task(void) {
767         long msgnum = 0L;
768
769         /* Force change the room if we have to */
770         if (strlen(bstr("taskrm")) > 0) {
771                 gotoroom(bstr("taskrm"), 0);
772         }
773
774         msgnum = atol(bstr("msgnum"));
775         if (msgnum > 0L) {
776                 /* existing task */
777                 display_using_handler(msgnum, "text/calendar",
778                                 ICAL_VTODO_COMPONENT,
779                                 display_edit_individual_task);
780         }
781         else {
782                 /* new task */
783                 display_edit_individual_task(NULL, 0L);
784         }
785 }
786
787 void save_task(void) {
788         long msgnum = 0L;
789
790         msgnum = atol(bstr("msgnum"));
791         if (msgnum > 0L) {
792                 display_using_handler(msgnum, "text/calendar",
793                                 ICAL_VTODO_COMPONENT,
794                                 save_individual_task);
795         }
796         else {
797                 save_individual_task(NULL, 0L);
798         }
799 }
800
801 void display_edit_event(void) {
802         long msgnum = 0L;
803
804         msgnum = atol(bstr("msgnum"));
805         if (msgnum > 0L) {
806                 /* existing event */
807                 display_using_handler(msgnum, "text/calendar",
808                                 ICAL_VEVENT_COMPONENT,
809                                 display_edit_individual_event);
810         }
811         else {
812                 /* new event */
813                 display_edit_individual_event(NULL, 0L);
814         }
815 }
816
817 void save_event(void) {
818         long msgnum = 0L;
819
820         msgnum = atol(bstr("msgnum"));
821
822         if (msgnum > 0L) {
823                 display_using_handler(msgnum, "text/calendar",
824                                 ICAL_VEVENT_COMPONENT,
825                                 save_individual_event);
826         }
827         else {
828                 save_individual_event(NULL, 0L);
829         }
830 }
831
832 #endif /* HAVE_ICAL_H */