]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* Calendar invitations: changed "Accept/Tentative/Decline" buttons to
[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/vcalendar.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/vcalendar.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/vcalendar.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                 );
300
301         }
302
303         /* Trailing HTML for the display of this object */
304         if (recursion_level == 0) {
305
306                 wprintf("</TR></TABLE></CENTER>\n");
307         }
308 }
309
310
311 /*
312  * Deserialize a calendar object in a message so it can be processed.
313  * (This is the main entry point for these things)
314  */
315 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum) {
316         icalcomponent *cal;
317
318         cal = icalcomponent_new_from_string(part_source);
319
320         if (cal == NULL) {
321                 wprintf("Error parsing calendar object<br />\n");
322                 return;
323         }
324
325         ical_dezonify(cal);
326         cal_process_object(cal, 0, msgnum, cal_partnum);
327
328         /* Free the memory we obtained from libical's constructor */
329         icalcomponent_free(cal);
330 }
331
332
333
334
335 /*
336  * Respond to a meeting request
337  */
338 void respond_to_request(void) {
339         char buf[SIZ];
340
341         output_headers(1, 1, 2, 0, 0, 0, 0);
342
343         wprintf("<div id=\"banner\">\n");
344         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
345                 "<SPAN CLASS=\"titlebar\">Respond to meeting request</SPAN>"
346                 "</TD></TR></TABLE>\n"
347         );
348         wprintf("</div>\n<div id=\"content\">\n");
349
350         serv_printf("ICAL respond|%s|%s|%s|",
351                 bstr("msgnum"),
352                 bstr("cal_partnum"),
353                 bstr("sc")
354         );
355         serv_getln(buf, sizeof buf);
356
357         if (buf[0] == '2') {
358                 wprintf("<TABLE BORDER=0><TR><TD>"
359                         "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
360                         "</TD><TD>"
361                 );
362                 if (!strcasecmp(bstr("sc"), "accept")) {
363                         wprintf("You have accepted this meeting invitation.  "
364                                 "It has been entered into your calendar, "
365                         );
366                 } else if (!strcasecmp(bstr("sc"), "tentative")) {
367                         wprintf("You have tentatively accepted this meeting invitation.  "
368                                 "It has been 'pencilled in' to your calendar, "
369                         );
370                 } else if (!strcasecmp(bstr("sc"), "decline")) {
371                         wprintf("You have declined this meeting invitation.  "
372                                 "It has <b>not</b> been entered into your calendar, "
373                         );
374                 }
375                 wprintf("and a reply has been sent to the meeting organizer."
376                         "</TD></TR></TABLE>\n"
377                 );
378         } else {
379                 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
380                         "%s\n", &buf[4]);
381         }
382
383         wprintf("<A HREF=\"/dotskip?room=");
384         urlescputs(WC->wc_roomname);
385         wprintf("\"><br />Return to messages</A><br />\n");
386
387         wDumpContent(1);
388 }
389
390
391
392 /*
393  * Handle an incoming RSVP
394  */
395 void handle_rsvp(void) {
396         char buf[SIZ];
397
398         output_headers(1, 1, 2, 0, 0, 0, 0);
399
400         wprintf("<div id=\"banner\">\n");
401         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
402                 "<SPAN CLASS=\"titlebar\">"
403                 "Update your calendar with this RSVP</SPAN>"
404                 "</TD></TR></TABLE>\n"
405                 "</div>\n<div id=\"content\">\n"
406         );
407
408         serv_printf("ICAL handle_rsvp|%s|%s|%s|",
409                 bstr("msgnum"),
410                 bstr("cal_partnum"),
411                 bstr("sc")
412         );
413         serv_getln(buf, sizeof buf);
414
415         if (buf[0] == '2') {
416                 wprintf("<TABLE BORDER=0><TR><TD>"
417                         "<IMG SRC=\"static/vcalendar.gif\" ALIGN=CENTER>"
418                         "</TD><TD>"
419                 );
420                 if (!strcasecmp(bstr("sc"), "update")) {
421                         wprintf("Your calendar has been updated "
422                                 "to reflect this RSVP."
423                         );
424                 } else if (!strcasecmp(bstr("sc"), "ignore")) {
425                         wprintf("You have chosen to ignore this RSVP. "
426                                 "Your calendar has <b>not</b> been updated."
427                         );
428                 }
429                 wprintf("</TD></TR></TABLE>\n"
430                 );
431         } else {
432                 wprintf("<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
433                         "%s\n", &buf[4]);
434         }
435
436         wprintf("<A HREF=\"/dotskip?room=");
437         urlescputs(WC->wc_roomname);
438         wprintf("\"><br />Return to messages</A><br />\n");
439
440         wDumpContent(1);
441 }
442
443
444
445
446 /*****************************************************************************/
447
448
449
450 /*
451  * Display handlers for message reading
452  */
453
454
455
456 /*
457  * If we're reading calendar items, just store them for now.  We have to
458  * sort and re-output them later when we draw the calendar.
459  */
460 void display_individual_cal(icalcomponent *cal, long msgnum) {
461
462         WC->num_cal += 1;
463
464         WC->disp_cal = realloc(WC->disp_cal,
465                         (sizeof(struct disp_cal) * WC->num_cal) );
466         WC->disp_cal[WC->num_cal - 1].cal = icalcomponent_new_clone(cal);
467
468         WC->disp_cal[WC->num_cal - 1].cal_msgnum = msgnum;
469 }
470
471
472
473 /*
474  * Display a task by itself (for editing)
475  *
476  */
477 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
478         icalcomponent *vtodo;
479         icalproperty *p;
480         struct icaltimetype t;
481         time_t now;
482         int created_new_vtodo = 0;
483
484         now = time(NULL);
485
486         if (supplied_vtodo != NULL) {
487                 vtodo = supplied_vtodo;
488
489                 /* If we're looking at a fully encapsulated VCALENDAR
490                  * rather than a VTODO component, attempt to use the first
491                  * relevant VTODO subcomponent.  If there is none, the
492                  * NULL returned by icalcomponent_get_first_component() will
493                  * tell the next iteration of this function to create a
494                  * new one.
495                  */
496                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
497                         display_edit_individual_task(
498                                 icalcomponent_get_first_component(
499                                         vtodo, ICAL_VTODO_COMPONENT
500                                 ), msgnum
501                         );
502                         return;
503                 }
504         }
505         else {
506                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
507                 created_new_vtodo = 1;
508         }
509
510         output_headers(1, 1, 2, 0, 0, 0, 0);
511         wprintf("<div id=\"banner\">\n"
512                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
513                 "<SPAN CLASS=\"titlebar\">Edit task</SPAN>"
514                 "</TD></TR></TABLE>\n"
515                 "</div>\n<div id=\"content\">\n"
516         );
517
518         wprintf("<div id=\"fix_scrollbar_bug\">"
519                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>");
520         
521         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_task\">\n");
522         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
523                 msgnum);
524
525         wprintf("<TABLE border=0>\n");
526
527         wprintf("<TR><TD>Summary:</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>Start date:</TD><TD>");
537         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
538         if (p != NULL) {
539                 t = icalproperty_get_dtstart(p);
540         }
541         else {
542                 t = icaltime_from_timet(now, 0);
543         }
544         display_icaltimetype_as_webform(&t, "dtstart");
545         wprintf("</TD></TR>\n");
546
547         wprintf("<TR><TD>Due date:</TD><TD>");
548         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
549         if (p != NULL) {
550                 t = icalproperty_get_due(p);
551         }
552         else {
553                 t = icaltime_from_timet(now, 0);
554         }
555         display_icaltimetype_as_webform(&t, "due");
556         wprintf("</TD></TR>\n");
557         wprintf("<TR><TD>Description:</TD><TD>");
558         wprintf("<TEXTAREA NAME=\"description\" wrap=soft "
559                 "ROWS=10 COLS=80 WIDTH=80>\n"
560         );
561         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
562         if (p != NULL) {
563                 escputs((char *)icalproperty_get_comment(p));
564         }
565         wprintf("</TEXTAREA></TD></TR></TABLE>\n");
566
567         wprintf("<CENTER>"
568                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
569                 "&nbsp;&nbsp;"
570                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
571                 "&nbsp;&nbsp;"
572                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
573                 "</CENTER>\n"
574         );
575
576         wprintf("</FORM>\n");
577
578         wprintf("</td></tr></table></div>\n");
579         wDumpContent(1);
580
581         if (created_new_vtodo) {
582                 icalcomponent_free(vtodo);
583         }
584 }
585
586 /*
587  * Save an edited task
588  *
589  */
590 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
591         char buf[SIZ];
592         int delete_existing = 0;
593         icalproperty *prop;
594         icalcomponent *vtodo, *encaps;
595         int created_new_vtodo = 0;
596         int i;
597         int sequence = 0;
598
599         if (supplied_vtodo != NULL) {
600                 vtodo = supplied_vtodo;
601                 /* If we're looking at a fully encapsulated VCALENDAR
602                  * rather than a VTODO component, attempt to use the first
603                  * relevant VTODO subcomponent.  If there is none, the
604                  * NULL returned by icalcomponent_get_first_component() will
605                  * tell the next iteration of this function to create a
606                  * new one.
607                  */
608                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
609                         save_individual_task(
610                                 icalcomponent_get_first_component(
611                                         vtodo, ICAL_VTODO_COMPONENT
612                                 ), msgnum
613                         );
614                         return;
615                 }
616         }
617         else {
618                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
619                 created_new_vtodo = 1;
620         }
621
622         if (!strcasecmp(bstr("sc"), "Save")) {
623
624                 /* Replace values in the component with ones from the form */
625
626                 while (prop = icalcomponent_get_first_property(vtodo,
627                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
628                         icalcomponent_remove_property(vtodo, prop);
629                         icalproperty_free(prop);
630                 }
631                 icalcomponent_add_property(vtodo,
632                         icalproperty_new_summary(bstr("summary")));
633                 
634                 while (prop = icalcomponent_get_first_property(vtodo,
635                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
636                         icalcomponent_remove_property(vtodo, prop);
637                         icalproperty_free(prop);
638                 }
639                 icalcomponent_add_property(vtodo,
640                         icalproperty_new_description(bstr("description")));
641         
642                 while (prop = icalcomponent_get_first_property(vtodo,
643                       ICAL_DTSTART_PROPERTY), prop != NULL) {
644                         icalcomponent_remove_property(vtodo, prop);
645                         icalproperty_free(prop);
646                 }
647                 icalcomponent_add_property(vtodo,
648                         icalproperty_new_dtstart(
649                                 icaltime_from_webform("dtstart")
650                         )
651                 );
652         
653                 while (prop = icalcomponent_get_first_property(vtodo,
654                       ICAL_DUE_PROPERTY), prop != NULL) {
655                         icalcomponent_remove_property(vtodo, prop);
656                         icalproperty_free(prop);
657                 }
658                 icalcomponent_add_property(vtodo,
659                         icalproperty_new_due(
660                                 icaltime_from_webform("due")
661                         )
662                 );
663
664                 /* Give this task a UID if it doesn't have one. */
665                 lprintf(9, "Give this task a UID if it doesn't have one.\n");
666                 if (icalcomponent_get_first_property(vtodo,
667                    ICAL_UID_PROPERTY) == NULL) {
668                         generate_uuid(buf);
669                         icalcomponent_add_property(vtodo,
670                                 icalproperty_new_uid(buf)
671                         );
672                 }
673
674                 /* Increment the sequence ID */
675                 lprintf(9, "Increment the sequence ID\n");
676                 while (prop = icalcomponent_get_first_property(vtodo,
677                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
678                         i = icalproperty_get_sequence(prop);
679                         lprintf(9, "Sequence was %d\n", i);
680                         if (i > sequence) sequence = i;
681                         icalcomponent_remove_property(vtodo, prop);
682                         icalproperty_free(prop);
683                 }
684                 ++sequence;
685                 lprintf(9, "New sequence is %d.  Adding...\n", sequence);
686                 icalcomponent_add_property(vtodo,
687                         icalproperty_new_sequence(sequence)
688                 );
689
690                 /*
691                  * Encapsulate event into full VCALENDAR component.  Clone it first,
692                  * for two reasons: one, it's easier to just free the whole thing
693                  * when we're done instead of unbundling, but more importantly, we
694                  * can't encapsulate something that may already be encapsulated
695                  * somewhere else.
696                  */
697                 lprintf(9, "Encapsulating into full VCALENDAR component\n");
698                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
699
700                 /* Serialize it and save it to the message base */
701                 serv_puts("ENT0 1|||4");
702                 serv_getln(buf, sizeof buf);
703                 if (buf[0] == '4') {
704                         serv_puts("Content-type: text/calendar");
705                         serv_puts("");
706                         serv_puts(icalcomponent_as_ical_string(encaps));
707                         serv_puts("000");
708
709                         /* Probably not necessary; the server will see the UID
710                          * of the object and delete the old one anyway, but
711                          * just in case...
712                          */
713                         delete_existing = 1;
714                 }
715                 icalcomponent_free(encaps);
716         }
717
718         /*
719          * If the user clicked 'Delete' then explicitly delete the message.
720          */
721         if (!strcasecmp(bstr("sc"), "Delete")) {
722                 delete_existing = 1;
723         }
724
725         if ( (delete_existing) && (msgnum > 0L) ) {
726                 serv_printf("DELE %ld", atol(bstr("msgnum")));
727                 serv_getln(buf, sizeof buf);
728         }
729
730         if (created_new_vtodo) {
731                 icalcomponent_free(vtodo);
732         }
733
734         /* Go back to the task list */
735         readloop("readfwd");
736 }
737
738
739
740 /*
741  * Code common to all display handlers.  Given a message number and a MIME
742  * type, we load the message and hunt for that MIME type.  If found, we load
743  * the relevant part, deserialize it into a libical component, filter it for
744  * the requested object type, and feed it to the specified handler.
745  *
746  */
747 void display_using_handler(long msgnum,
748                         char *mimetype,
749                         icalcomponent_kind which_kind,
750                         void (*callback)(icalcomponent *, long)
751         ) {
752         char buf[SIZ];
753         char mime_partnum[SIZ];
754         char mime_filename[SIZ];
755         char mime_content_type[SIZ];
756         char mime_disposition[SIZ];
757         int mime_length;
758         char relevant_partnum[SIZ];
759         char *relevant_source = NULL;
760         icalcomponent *cal, *c;
761
762         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
763         serv_puts(buf);
764         serv_getln(buf, sizeof buf);
765         if (buf[0] != '1') return;
766
767         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
768                 if (!strncasecmp(buf, "part=", 5)) {
769                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
770                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
771                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
772                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
773                         mime_length = extract_int(&buf[5], 5);
774
775                         if (!strcasecmp(mime_content_type, "text/calendar")) {
776                                 strcpy(relevant_partnum, mime_partnum);
777                         }
778
779                 }
780         }
781
782         if (strlen(relevant_partnum) > 0) {
783                 relevant_source = load_mimepart(msgnum, relevant_partnum);
784                 if (relevant_source != NULL) {
785
786                         cal = icalcomponent_new_from_string(relevant_source);
787                         if (cal != NULL) {
788
789                                 ical_dezonify(cal);
790
791                                 /* Simple components of desired type */
792                                 if (icalcomponent_isa(cal) == which_kind) {
793                                         callback(cal, msgnum);
794                                 }
795
796                                 /* Subcomponents of desired type */
797                                 for (c = icalcomponent_get_first_component(cal,
798                                     which_kind);
799                                     (c != 0);
800                                     c = icalcomponent_get_next_component(cal,
801                                     which_kind)) {
802                                         callback(c, msgnum);
803                                 }
804                                 icalcomponent_free(cal);
805                         }
806                         free(relevant_source);
807                 }
808         }
809
810 }
811
812 void display_calendar(long msgnum) {
813         display_using_handler(msgnum, "text/calendar",
814                                 ICAL_VEVENT_COMPONENT,
815                                 display_individual_cal);
816 }
817
818 void display_task(long msgnum) {
819         display_using_handler(msgnum, "text/calendar",
820                                 ICAL_VTODO_COMPONENT,
821                                 display_individual_cal);
822 }
823
824 void display_edit_task(void) {
825         long msgnum = 0L;
826
827         /* Force change the room if we have to */
828         if (strlen(bstr("taskrm")) > 0) {
829                 gotoroom(bstr("taskrm"));
830         }
831
832         msgnum = atol(bstr("msgnum"));
833         if (msgnum > 0L) {
834                 /* existing task */
835                 display_using_handler(msgnum, "text/calendar",
836                                 ICAL_VTODO_COMPONENT,
837                                 display_edit_individual_task);
838         }
839         else {
840                 /* new task */
841                 display_edit_individual_task(NULL, 0L);
842         }
843 }
844
845 void save_task(void) {
846         long msgnum = 0L;
847
848         msgnum = atol(bstr("msgnum"));
849         if (msgnum > 0L) {
850                 display_using_handler(msgnum, "text/calendar",
851                                 ICAL_VTODO_COMPONENT,
852                                 save_individual_task);
853         }
854         else {
855                 save_individual_task(NULL, 0L);
856         }
857 }
858
859 void display_edit_event(void) {
860         long msgnum = 0L;
861
862         msgnum = atol(bstr("msgnum"));
863         if (msgnum > 0L) {
864                 /* existing event */
865                 display_using_handler(msgnum, "text/calendar",
866                                 ICAL_VEVENT_COMPONENT,
867                                 display_edit_individual_event);
868         }
869         else {
870                 /* new event */
871                 display_edit_individual_event(NULL, 0L);
872         }
873 }
874
875 void save_event(void) {
876         long msgnum = 0L;
877
878         msgnum = atol(bstr("msgnum"));
879
880         if (msgnum > 0L) {
881                 display_using_handler(msgnum, "text/calendar",
882                                 ICAL_VEVENT_COMPONENT,
883                                 save_individual_event);
884         }
885         else {
886                 save_individual_event(NULL, 0L);
887         }
888 }
889
890
891
892
893
894 /*
895  * freebusy display (for client software)
896  */
897 void do_freebusy(char *req) {
898         char who[SIZ];
899         char buf[SIZ];
900         char *fb;
901
902         extract_token(who, req, 1, ' ', sizeof who);
903         if (!strncasecmp(who, "/freebusy/", 10)) {
904                 strcpy(who, &who[10]);
905         }
906         unescape_input(who);
907
908         if ( (!strcasecmp(&who[strlen(who)-4], ".vcf"))
909            || (!strcasecmp(&who[strlen(who)-4], ".vfb")) ) {
910                 who[strlen(who)-4] = 0;
911         }
912
913         lprintf(9, "freebusy requested for <%s>\n", who);
914         serv_printf("ICAL freebusy|%s", who);
915         serv_getln(buf, sizeof buf);
916
917         if (buf[0] != '1') {
918                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
919                 output_headers(0, 0, 0, 0, 0, 0, 0);
920                 wprintf("Content-Type: text/plain\r\n");
921                 wprintf("\r\n");
922                 wprintf("%s\n", &buf[4]);
923                 return;
924         }
925
926         fb = read_server_text();
927         http_transmit_thing(fb, strlen(fb), "text/calendar", 0);
928         free(fb);
929 }
930
931
932
933 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */