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