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