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