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