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