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