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