]> code.citadel.org Git - citadel.git/blob - webcit/calendar.c
* Use the new icons in more places.
[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
601         if (supplied_vtodo != NULL) {
602                 vtodo = supplied_vtodo;
603                 /* If we're looking at a fully encapsulated VCALENDAR
604                  * rather than a VTODO component, attempt to use the first
605                  * relevant VTODO subcomponent.  If there is none, the
606                  * NULL returned by icalcomponent_get_first_component() will
607                  * tell the next iteration of this function to create a
608                  * new one.
609                  */
610                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
611                         save_individual_task(
612                                 icalcomponent_get_first_component(
613                                         vtodo, ICAL_VTODO_COMPONENT
614                                 ), msgnum
615                         );
616                         return;
617                 }
618         }
619         else {
620                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
621                 created_new_vtodo = 1;
622         }
623
624         if (!strcasecmp(bstr("sc"), "Save")) {
625
626                 /* Replace values in the component with ones from the form */
627
628                 while (prop = icalcomponent_get_first_property(vtodo,
629                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
630                         icalcomponent_remove_property(vtodo, prop);
631                         icalproperty_free(prop);
632                 }
633                 icalcomponent_add_property(vtodo,
634                         icalproperty_new_summary(bstr("summary")));
635                 
636                 while (prop = icalcomponent_get_first_property(vtodo,
637                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
638                         icalcomponent_remove_property(vtodo, prop);
639                         icalproperty_free(prop);
640                 }
641                 icalcomponent_add_property(vtodo,
642                         icalproperty_new_description(bstr("description")));
643         
644                 while (prop = icalcomponent_get_first_property(vtodo,
645                       ICAL_DTSTART_PROPERTY), prop != NULL) {
646                         icalcomponent_remove_property(vtodo, prop);
647                         icalproperty_free(prop);
648                 }
649                 icalcomponent_add_property(vtodo,
650                         icalproperty_new_dtstart(
651                                 icaltime_from_webform("dtstart")
652                         )
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                 icalcomponent_add_property(vtodo,
661                         icalproperty_new_due(
662                                 icaltime_from_webform("due")
663                         )
664                 );
665
666                 /* Give this task a UID if it doesn't have one. */
667                 lprintf(9, "Give this task a UID if it doesn't have one.\n");
668                 if (icalcomponent_get_first_property(vtodo,
669                    ICAL_UID_PROPERTY) == NULL) {
670                         generate_uuid(buf);
671                         icalcomponent_add_property(vtodo,
672                                 icalproperty_new_uid(buf)
673                         );
674                 }
675
676                 /* Increment the sequence ID */
677                 lprintf(9, "Increment the sequence ID\n");
678                 while (prop = icalcomponent_get_first_property(vtodo,
679                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
680                         i = icalproperty_get_sequence(prop);
681                         lprintf(9, "Sequence was %d\n", i);
682                         if (i > sequence) sequence = i;
683                         icalcomponent_remove_property(vtodo, prop);
684                         icalproperty_free(prop);
685                 }
686                 ++sequence;
687                 lprintf(9, "New sequence is %d.  Adding...\n", sequence);
688                 icalcomponent_add_property(vtodo,
689                         icalproperty_new_sequence(sequence)
690                 );
691
692                 /*
693                  * Encapsulate event into full VCALENDAR component.  Clone it first,
694                  * for two reasons: one, it's easier to just free the whole thing
695                  * when we're done instead of unbundling, but more importantly, we
696                  * can't encapsulate something that may already be encapsulated
697                  * somewhere else.
698                  */
699                 lprintf(9, "Encapsulating into full VCALENDAR component\n");
700                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
701
702                 /* Serialize it and save it to the message base */
703                 serv_puts("ENT0 1|||4");
704                 serv_getln(buf, sizeof buf);
705                 if (buf[0] == '4') {
706                         serv_puts("Content-type: text/calendar");
707                         serv_puts("");
708                         serv_puts(icalcomponent_as_ical_string(encaps));
709                         serv_puts("000");
710
711                         /* Probably not necessary; the server will see the UID
712                          * of the object and delete the old one anyway, but
713                          * just in case...
714                          */
715                         delete_existing = 1;
716                 }
717                 icalcomponent_free(encaps);
718         }
719
720         /*
721          * If the user clicked 'Delete' then explicitly delete the message.
722          */
723         if (!strcasecmp(bstr("sc"), "Delete")) {
724                 delete_existing = 1;
725         }
726
727         if ( (delete_existing) && (msgnum > 0L) ) {
728                 serv_printf("DELE %ld", atol(bstr("msgnum")));
729                 serv_getln(buf, sizeof buf);
730         }
731
732         if (created_new_vtodo) {
733                 icalcomponent_free(vtodo);
734         }
735
736         /* Go back to the task list */
737         readloop("readfwd");
738 }
739
740
741
742 /*
743  * Code common to all display handlers.  Given a message number and a MIME
744  * type, we load the message and hunt for that MIME type.  If found, we load
745  * the relevant part, deserialize it into a libical component, filter it for
746  * the requested object type, and feed it to the specified handler.
747  *
748  */
749 void display_using_handler(long msgnum,
750                         char *mimetype,
751                         icalcomponent_kind which_kind,
752                         void (*callback)(icalcomponent *, long)
753         ) {
754         char buf[SIZ];
755         char mime_partnum[SIZ];
756         char mime_filename[SIZ];
757         char mime_content_type[SIZ];
758         char mime_disposition[SIZ];
759         int mime_length;
760         char relevant_partnum[SIZ];
761         char *relevant_source = NULL;
762         icalcomponent *cal, *c;
763
764         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
765         serv_puts(buf);
766         serv_getln(buf, sizeof buf);
767         if (buf[0] != '1') return;
768
769         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
770                 if (!strncasecmp(buf, "part=", 5)) {
771                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
772                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
773                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
774                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
775                         mime_length = extract_int(&buf[5], 5);
776
777                         if (!strcasecmp(mime_content_type, "text/calendar")) {
778                                 strcpy(relevant_partnum, mime_partnum);
779                         }
780
781                 }
782         }
783
784         if (strlen(relevant_partnum) > 0) {
785                 relevant_source = load_mimepart(msgnum, relevant_partnum);
786                 if (relevant_source != NULL) {
787
788                         cal = icalcomponent_new_from_string(relevant_source);
789                         if (cal != NULL) {
790
791                                 ical_dezonify(cal);
792
793                                 /* Simple components of desired type */
794                                 if (icalcomponent_isa(cal) == which_kind) {
795                                         callback(cal, msgnum);
796                                 }
797
798                                 /* Subcomponents of desired type */
799                                 for (c = icalcomponent_get_first_component(cal,
800                                     which_kind);
801                                     (c != 0);
802                                     c = icalcomponent_get_next_component(cal,
803                                     which_kind)) {
804                                         callback(c, msgnum);
805                                 }
806                                 icalcomponent_free(cal);
807                         }
808                         free(relevant_source);
809                 }
810         }
811
812 }
813
814 void display_calendar(long msgnum) {
815         display_using_handler(msgnum, "text/calendar",
816                                 ICAL_VEVENT_COMPONENT,
817                                 display_individual_cal);
818 }
819
820 void display_task(long msgnum) {
821         display_using_handler(msgnum, "text/calendar",
822                                 ICAL_VTODO_COMPONENT,
823                                 display_individual_cal);
824 }
825
826 void display_edit_task(void) {
827         long msgnum = 0L;
828
829         /* Force change the room if we have to */
830         if (strlen(bstr("taskrm")) > 0) {
831                 gotoroom(bstr("taskrm"));
832         }
833
834         msgnum = atol(bstr("msgnum"));
835         if (msgnum > 0L) {
836                 /* existing task */
837                 display_using_handler(msgnum, "text/calendar",
838                                 ICAL_VTODO_COMPONENT,
839                                 display_edit_individual_task);
840         }
841         else {
842                 /* new task */
843                 display_edit_individual_task(NULL, 0L);
844         }
845 }
846
847 void save_task(void) {
848         long msgnum = 0L;
849
850         msgnum = atol(bstr("msgnum"));
851         if (msgnum > 0L) {
852                 display_using_handler(msgnum, "text/calendar",
853                                 ICAL_VTODO_COMPONENT,
854                                 save_individual_task);
855         }
856         else {
857                 save_individual_task(NULL, 0L);
858         }
859 }
860
861 void display_edit_event(void) {
862         long msgnum = 0L;
863
864         msgnum = atol(bstr("msgnum"));
865         if (msgnum > 0L) {
866                 /* existing event */
867                 display_using_handler(msgnum, "text/calendar",
868                                 ICAL_VEVENT_COMPONENT,
869                                 display_edit_individual_event);
870         }
871         else {
872                 /* new event */
873                 display_edit_individual_event(NULL, 0L);
874         }
875 }
876
877 void save_event(void) {
878         long msgnum = 0L;
879
880         msgnum = atol(bstr("msgnum"));
881
882         if (msgnum > 0L) {
883                 display_using_handler(msgnum, "text/calendar",
884                                 ICAL_VEVENT_COMPONENT,
885                                 save_individual_event);
886         }
887         else {
888                 save_individual_event(NULL, 0L);
889         }
890 }
891
892
893
894
895
896 /*
897  * freebusy display (for client software)
898  */
899 void do_freebusy(char *req) {
900         char who[SIZ];
901         char buf[SIZ];
902         char *fb;
903
904         extract_token(who, req, 1, ' ', sizeof who);
905         if (!strncasecmp(who, "/freebusy/", 10)) {
906                 strcpy(who, &who[10]);
907         }
908         unescape_input(who);
909
910         if ( (!strcasecmp(&who[strlen(who)-4], ".vcf"))
911            || (!strcasecmp(&who[strlen(who)-4], ".ifb"))
912            || (!strcasecmp(&who[strlen(who)-4], ".vfb")) ) {
913                 who[strlen(who)-4] = 0;
914         }
915
916         lprintf(9, "freebusy requested for <%s>\n", who);
917         serv_printf("ICAL freebusy|%s", who);
918         serv_getln(buf, sizeof buf);
919
920         if (buf[0] != '1') {
921                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
922                 output_headers(0, 0, 0, 0, 0, 0, 0);
923                 wprintf("Content-Type: text/plain\r\n");
924                 wprintf("\r\n");
925                 wprintf("%s\n", &buf[4]);
926                 return;
927         }
928
929         fb = read_server_text();
930         http_transmit_thing(fb, strlen(fb), "text/calendar", 0);
931         free(fb);
932 }
933
934
935
936 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */