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