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